1 /*
2  * Copyright (C) 2016 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.server.cts;
18 
19 import com.android.server.am.MemInfoDumpProto;
20 import com.android.server.am.MemInfoDumpProto.AppData;
21 import com.android.server.am.MemInfoDumpProto.MemItem;
22 import com.android.server.am.MemInfoDumpProto.ProcessMemory;
23 
24 /** Test to check that ActivityManager properly outputs meminfo data. */
25 public class MemInfoIncidentTest extends ProtoDumpTestCase {
26 
testMemInfoDump()27     public void testMemInfoDump() throws Exception {
28         final MemInfoDumpProto dump =
29                 getDump(MemInfoDumpProto.parser(), "dumpsys -t 30000 meminfo -a --proto");
30 
31         verifyMemInfoDumpProto(dump, PRIVACY_NONE);
32     }
33 
verifyMemInfoDumpProto(MemInfoDumpProto dump, final int filterLevel)34     static void verifyMemInfoDumpProto(MemInfoDumpProto dump, final int filterLevel) throws Exception {
35         assertTrue(dump.getUptimeDurationMs() >= 0);
36         assertTrue(dump.getElapsedRealtimeMs() >= 0);
37 
38         for (ProcessMemory pm : dump.getNativeProcessesList()) {
39             testProcessMemory(pm);
40         }
41 
42         for (AppData ad : dump.getAppProcessesList()) {
43             testAppData(ad);
44         }
45 
46         for (MemItem mi : dump.getTotalPssByProcessList()) {
47             testMemItem(mi);
48         }
49         for (MemItem mi : dump.getTotalPssByOomAdjustmentList()) {
50             testMemItem(mi);
51         }
52         for (MemItem mi : dump.getTotalPssByCategoryList()) {
53             testMemItem(mi);
54         }
55 
56         assertTrue(0 <= dump.getTotalRamKb());
57         assertTrue(0 <= dump.getCachedPssKb());
58         assertTrue(0 <= dump.getCachedKernelKb());
59         assertTrue(0 <= dump.getFreeKb());
60         assertTrue(0 <= dump.getUsedPssKb());
61         assertTrue(0 <= dump.getUsedKernelKb());
62 
63         // Ideally lost RAM would not be negative, but there's an issue where it's sometimes
64         // calculated to be negative.
65         // TODO: re-enable check once the underlying bug has been fixed.
66         // assertTrue(0 <= dump.getLostRamKb());
67 
68         assertTrue(0 <= dump.getTotalZramKb());
69         assertTrue(0 <= dump.getZramPhysicalUsedInSwapKb());
70         assertTrue(0 <= dump.getTotalZramSwapKb());
71 
72         assertTrue(0 <= dump.getKsmSharingKb());
73         assertTrue(0 <= dump.getKsmSharedKb());
74         assertTrue(0 <= dump.getKsmUnsharedKb());
75         assertTrue(0 <= dump.getKsmVolatileKb());
76 
77         assertTrue("Tuning_mb (" + dump.getTuningMb() + ") is not positive", 0 < dump.getTuningMb());
78         assertTrue(0 < dump.getTuningLargeMb());
79 
80         assertTrue(0 <= dump.getOomKb());
81 
82         assertTrue(0 < dump.getRestoreLimitKb());
83     }
84 
testProcessMemory(ProcessMemory pm)85     private static void testProcessMemory(ProcessMemory pm) throws Exception {
86         assertNotNull(pm);
87 
88         assertTrue(0 < pm.getPid());
89         // On most Linux machines, the max pid value is 32768 (=2^15), but, it can be set to any
90         // value up to 4194304 (=2^22) if necessary.
91         assertTrue(4194304 >= pm.getPid());
92 
93         testHeapInfo(pm.getNativeHeap());
94         testHeapInfo(pm.getDalvikHeap());
95 
96         for (ProcessMemory.MemoryInfo mi : pm.getOtherHeapsList()) {
97             testMemoryInfo(mi);
98         }
99         testMemoryInfo(pm.getUnknownHeap());
100         testHeapInfo(pm.getTotalHeap());
101 
102         for (ProcessMemory.MemoryInfo mi : pm.getDalvikDetailsList()) {
103             testMemoryInfo(mi);
104         }
105 
106         ProcessMemory.AppSummary as = pm.getAppSummary();
107         assertTrue(0 <= as.getJavaHeapPssKb());
108         assertTrue(0 <= as.getNativeHeapPssKb());
109         assertTrue(0 <= as.getCodePssKb());
110         assertTrue(0 <= as.getStackPssKb());
111         assertTrue(0 <= as.getGraphicsPssKb());
112         assertTrue(0 <= as.getPrivateOtherPssKb());
113         assertTrue(0 <= as.getSystemPssKb());
114         assertTrue(0 <= as.getTotalSwapPss());
115         assertTrue(0 <= as.getTotalSwapKb());
116     }
117 
testMemoryInfo(ProcessMemory.MemoryInfo mi)118     private static void testMemoryInfo(ProcessMemory.MemoryInfo mi) throws Exception {
119         assertNotNull(mi);
120 
121         assertTrue(0 <= mi.getTotalPssKb());
122         assertTrue(0 <= mi.getCleanPssKb());
123         assertTrue(0 <= mi.getSharedDirtyKb());
124         assertTrue(0 <= mi.getPrivateDirtyKb());
125         assertTrue(0 <= mi.getSharedCleanKb());
126         assertTrue(0 <= mi.getPrivateCleanKb());
127         assertTrue(0 <= mi.getDirtySwapKb());
128         assertTrue(0 <= mi.getDirtySwapPssKb());
129     }
130 
testHeapInfo(ProcessMemory.HeapInfo hi)131     private static void testHeapInfo(ProcessMemory.HeapInfo hi) throws Exception {
132         assertNotNull(hi);
133 
134         testMemoryInfo(hi.getMemInfo());
135         assertTrue(0 <= hi.getHeapSizeKb());
136         assertTrue(0 <= hi.getHeapAllocKb());
137         assertTrue(0 <= hi.getHeapFreeKb());
138     }
139 
testAppData(AppData ad)140     private static void testAppData(AppData ad) throws Exception {
141         assertNotNull(ad);
142 
143         testProcessMemory(ad.getProcessMemory());
144 
145         AppData.ObjectStats os = ad.getObjects();
146         assertTrue(0 <= os.getViewInstanceCount());
147         assertTrue(0 <= os.getViewRootInstanceCount());
148         assertTrue(0 <= os.getAppContextInstanceCount());
149         assertTrue(0 <= os.getActivityInstanceCount());
150         assertTrue(0 <= os.getGlobalAssetCount());
151         assertTrue(0 <= os.getGlobalAssetManagerCount());
152         assertTrue(0 <= os.getLocalBinderObjectCount());
153         assertTrue(0 <= os.getProxyBinderObjectCount());
154         assertTrue(0 <= os.getParcelMemoryKb());
155         assertTrue(0 <= os.getParcelCount());
156         assertTrue(0 <= os.getBinderObjectDeathCount());
157         assertTrue(0 <= os.getOpenSslSocketCount());
158         assertTrue(0 <= os.getWebviewInstanceCount());
159 
160         AppData.SqlStats ss = ad.getSql();
161         assertTrue(0 <= ss.getMemoryUsedKb());
162         assertTrue(0 <= ss.getPagecacheOverflowKb());
163         assertTrue(0 <= ss.getMallocSizeKb());
164         for (AppData.SqlStats.Database d : ss.getDatabasesList()) {
165             assertTrue(0 <= d.getPageSize());
166             assertTrue(0 <= d.getDbSize());
167             assertTrue(0 <= d.getLookasideB());
168         }
169     }
170 
testMemItem(MemItem mi)171     private static void testMemItem(MemItem mi) throws Exception {
172         assertNotNull(mi);
173 
174         assertTrue(0 <= mi.getPssKb());
175         assertTrue(0 <= mi.getSwapPssKb());
176 
177         for (MemItem smi : mi.getSubItemsList()) {
178             testMemItem(smi);
179         }
180     }
181 }
182