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 
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42 
43 @RunWith(DeviceJUnit4ClassRunner.class)
44 public class MemoryTests extends BaseHostJUnit4Test {
45     // The time in ms to wait before starting logcat on NativeDevice
46     private static final int LOG_START_DELAY = 5 * 1000;
47 
48     @Rule
49     public TestMetrics metrics = new TestMetrics();
50 
51     @Before
setUp()52     public void setUp() throws DeviceNotAvailableException {
53         TestUtils.assumeGameCoreCertified(getDevice());
54     }
55 
56     /**
57      * Device must be able to allocate at least 2.3GB of memory.
58      */
59     @Test
testMemoryAllocationLimit()60     public void testMemoryAllocationLimit()
61         throws DeviceNotAvailableException, IOException {
62             getDevice().startLogcat();
63             // Wait until starting logcat for the device.
64             try {
65                 Thread.sleep(LOG_START_DELAY);
66             } catch (InterruptedException e) {
67             }
68             getDevice().clearLogcat();
69 
70             String pkgname = "com.android.game.qualification.allocstress";
71             String actname = pkgname + ".MainActivity";
72             getDevice().executeShellCommand("am start -W " + pkgname + "/" + actname);
73 
74             // Wait until app is finished.
75             while((getDevice().executeShellCommand("dumpsys activity | grep top-activity")).contains("allocstress"));
76 
77             boolean hasAllocStats = false;
78             int totalAllocated = 0;
79 
80             try (
81                 InputStreamSource logcatSource = getDevice().getLogcat();
82                 BufferedReader logcat = new BufferedReader(new InputStreamReader(logcatSource.createInputStream()));
83             ) {
84 
85                 String s = logcat.readLine();
86                 String pattern = "total alloc: ";
87                 String p = null;
88                 while (s != null) {
89                     if (s.contains(pattern)) {
90                         hasAllocStats = true;
91                         p = s;
92                     }
93                     s = logcat.readLine();
94                 }
95                 int totalAllocIndex = p.indexOf(pattern) + pattern.length();
96                 totalAllocated = Integer.parseInt(p.substring(totalAllocIndex));
97 
98             }
99 
100             getDevice().stopLogcat();
101 
102             assertTrue(hasAllocStats);
103             metrics.addTestMetric("memory_allocated", Metric.newBuilder()
104                                                             .setType(DataType.RAW)
105                                                             .setMeasurements(Measurements.newBuilder().setSingleInt(totalAllocated))
106                                                             .build());
107             assertTrue("Device failed to allocate an appropriate amount of memory (2.3GB) before being killed", totalAllocated > 2300);
108     }
109 
110 }
111