1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.game.qualification.test;
18 
19 import com.android.tradefed.device.DeviceNotAvailableException;
20 import com.android.tradefed.device.NativeDevice;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.metrics.proto.MetricMeasurement.DataType;
23 import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements;
24 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
25 import com.android.tradefed.result.InputStreamSource;
26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestMetrics;
27 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
28 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
29 import com.android.tradefed.util.RunUtil;
30 
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
35 
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43 
44 @RunWith(DeviceJUnit4ClassRunner.class)
45 public class MemoryTests extends BaseHostJUnit4Test {
46     // The time in ms to wait before starting logcat on NativeDevice
47     private static final int LOG_START_DELAY = 5 * 1000;
48 
49     @Rule
50     public TestMetrics metrics = new TestMetrics();
51 
52     @Before
setUp()53     public void setUp() throws DeviceNotAvailableException {
54         TestUtils.assumeGameCoreCertified(getDevice());
55     }
56 
57     /**
58      * Device must be able to allocate at least 2.3GB of memory.
59      */
60     @Test
testMemoryAllocationLimit()61     public void testMemoryAllocationLimit()
62         throws DeviceNotAvailableException, IOException {
63             getDevice().startLogcat();
64             // Wait until starting logcat for the device.
65             RunUtil.getDefault().sleep(LOG_START_DELAY);
66             getDevice().clearLogcat();
67 
68             String pkgname = "com.android.game.qualification.allocstress";
69             String actname = pkgname + ".MainActivity";
70             getDevice().executeShellCommand("am start -W " + pkgname + "/" + actname);
71 
72             // Wait until app is finished.
73             while((getDevice().executeShellCommand("dumpsys activity | grep top-activity")).contains("allocstress"));
74 
75             boolean hasAllocStats = false;
76             int totalAllocated = 0;
77 
78             try (
79                 InputStreamSource logcatSource = getDevice().getLogcat();
80                 BufferedReader logcat = new BufferedReader(new InputStreamReader(logcatSource.createInputStream()));
81             ) {
82 
83                 String s = logcat.readLine();
84                 String pattern = "total alloc: ";
85                 String p = null;
86                 while (s != null) {
87                     if (s.contains(pattern)) {
88                         hasAllocStats = true;
89                         p = s;
90                     }
91                     s = logcat.readLine();
92                 }
93                 int totalAllocIndex = p.indexOf(pattern) + pattern.length();
94                 totalAllocated = Integer.parseInt(p.substring(totalAllocIndex));
95 
96             }
97 
98             getDevice().stopLogcat();
99 
100             assertTrue(hasAllocStats);
101             metrics.addTestMetric("memory_allocated", Metric.newBuilder()
102                                                             .setType(DataType.RAW)
103                                                             .setMeasurements(Measurements.newBuilder().setSingleInt(totalAllocated))
104                                                             .build());
105             assertTrue("Device failed to allocate an appropriate amount of memory (2.3GB) before being killed", totalAllocated > 2300);
106     }
107 
108 }
109