blob: 03a1e718d2ea5a8b7b15f0c6926e2b94288f7f53 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Andy McFadden06a6b552010-07-13 16:28:09 -070017#define LOG_TAG "android.os.Debug"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include "JNIHelp.h"
19#include "jni.h"
Dianne Hackbornf72467a2012-06-08 17:23:59 -070020#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "utils/misc.h"
Dianne Hackbornf72467a2012-06-08 17:23:59 -070022#include "cutils/debugger.h"
Adam Lesinski5b4ef812013-09-23 10:06:09 -070023#include <memtrack/memtrack.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
Ruben Brunk87eac992013-09-09 17:44:59 -070025#include <cutils/log.h>
Dianne Hackbornf72467a2012-06-08 17:23:59 -070026#include <fcntl.h>
Mark Salyzyn85394032014-04-16 10:28:37 -070027#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <time.h>
33#include <sys/time.h>
Andy McFadden06a6b552010-07-13 16:28:09 -070034#include <errno.h>
35#include <assert.h>
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070036#include <ctype.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <malloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39namespace android
40{
41
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070042enum {
43 HEAP_UNKNOWN,
44 HEAP_DALVIK,
45 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080046
Dianne Hackborn64770d12013-05-23 17:51:19 -070047 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080048 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070049 HEAP_CURSOR,
50 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080051 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070052 HEAP_UNKNOWN_DEV,
53 HEAP_SO,
54 HEAP_JAR,
55 HEAP_APK,
56 HEAP_TTF,
57 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070058 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070059 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070060 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070061 HEAP_GRAPHICS,
62 HEAP_GL,
63 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070064
Anwar Ghuloum3c615062013-05-13 14:18:02 -070065 HEAP_DALVIK_NORMAL,
66 HEAP_DALVIK_LARGE,
67 HEAP_DALVIK_LINEARALLOC,
68 HEAP_DALVIK_ACCOUNTING,
69 HEAP_DALVIK_CODE_CACHE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080070 HEAP_DALVIK_ZYGOTE,
71 HEAP_DALVIK_NON_MOVING,
72 HEAP_DALVIK_INDIRECT_REFERENCE_TABLE,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070073
74 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070075 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070076 _NUM_CORE_HEAP = HEAP_NATIVE+1
77};
78
79struct stat_fields {
80 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070081 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070082 jfieldID privateDirty_field;
83 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070084 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070085 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070086 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -080087 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070088};
89
90struct stat_field_names {
91 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070092 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070093 const char* privateDirty_name;
94 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070095 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070096 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070097 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -080098 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070099};
100
101static stat_fields stat_fields[_NUM_CORE_HEAP];
102
103static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700104 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800105 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700106 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800107 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700108 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800109 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700110};
111
112jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800113jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700115static bool memtrackLoaded;
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700118 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700119 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700120 int privateDirty;
121 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700122 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700123 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700124 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800125 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126};
127
128#define BINDER_STATS "/proc/binder/stats"
129
130static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
131{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 struct mallinfo info = mallinfo();
133 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134}
135
136static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
137{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 struct mallinfo info = mallinfo();
139 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141
142static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
143{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 struct mallinfo info = mallinfo();
145 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146}
147
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700148// Container used to retrieve graphics memory pss
149struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700150{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700151 int graphics;
152 int gl;
153 int other;
154};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700155
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700156/*
157 * Uses libmemtrack to retrieve graphics memory that the process is using.
158 * Any graphics memory reported in /proc/pid/smaps is not included here.
159 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700160static int read_memtrack_memory(struct memtrack_proc* p, int pid,
161 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700162{
163 int err = memtrack_proc_get(p, pid);
164 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700165 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700166 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700167 }
168
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700169 ssize_t pss = memtrack_proc_graphics_pss(p);
170 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700171 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700172 return pss;
173 }
174 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700175
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700176 pss = memtrack_proc_gl_pss(p);
177 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700178 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700179 return pss;
180 }
181 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700182
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700183 pss = memtrack_proc_other_pss(p);
184 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700185 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700186 return pss;
187 }
188 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700189
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700190 return 0;
191}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700192
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700193/*
194 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
195 */
196static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
197{
198 if (!memtrackLoaded) {
199 return -1;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700200 }
201
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700202 struct memtrack_proc* p = memtrack_proc_new();
203 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700204 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700205 return -1;
206 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700207
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700208 int err = read_memtrack_memory(p, pid, graphics_mem);
209 memtrack_proc_destroy(p);
210 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700211}
212
Martijn Coenene0764852016-01-07 17:04:22 -0800213static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214{
215 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700216 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 bool skip, done = false;
218
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800219 unsigned pss = 0, swappable_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700220 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 unsigned shared_clean = 0, shared_dirty = 0;
222 unsigned private_clean = 0, private_dirty = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800223 unsigned swapped_out = 0, swapped_out_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700224 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 unsigned temp;
226
Colin Cross0c6bc732014-06-17 15:18:07 -0700227 uint64_t start;
228 uint64_t end = 0;
229 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700230 char* name;
231 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700233 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700234 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700235 int prevHeap = HEAP_UNKNOWN;
236
Martijn Coenene0764852016-01-07 17:04:22 -0800237 *foundSwapPss = false;
238
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700239 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
241 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700242 prevHeap = whichHeap;
243 prevEnd = end;
244 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700245 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700247 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 len = strlen(line);
250 if (len < 1) return;
251 line[--len] = 0;
252
Colin Cross0c6bc732014-06-17 15:18:07 -0700253 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700254 skip = true;
255 } else {
256 while (isspace(line[name_pos])) {
257 name_pos += 1;
258 }
259 name = line + name_pos;
260 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
Dianne Hackborn64770d12013-05-23 17:51:19 -0700262 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700263 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700264 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
265 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700266 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800267 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700268 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
269 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700270 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700271 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
272 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700273 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700274 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
275 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700276 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700277 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
278 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700279 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700280 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Ian Rogers9f8589c2013-02-22 20:35:06 -0800281 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700282 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700283 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700284 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
285 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700286 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700287 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
288 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700289 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700290 } else if (strncmp(name, "/dev/", 5) == 0) {
291 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
292 whichHeap = HEAP_GL_DEV;
293 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
294 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
295 whichHeap = HEAP_DALVIK_OTHER;
296 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
297 subHeap = HEAP_DALVIK_LINEARALLOC;
298 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
299 (strstr(name, "/dev/ashmem/dalvik-main space") == name)) {
300 // This is the regular Dalvik heap.
301 whichHeap = HEAP_DALVIK;
302 subHeap = HEAP_DALVIK_NORMAL;
Mathieu Chartier2e2069d2015-10-19 13:48:34 -0700303 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name ||
304 strstr(name, "/dev/ashmem/dalvik-free list large object space")
305 == name) {
Mathieu Chartier93084622015-05-01 11:30:22 -0700306 whichHeap = HEAP_DALVIK;
307 subHeap = HEAP_DALVIK_LARGE;
308 } else if (strstr(name, "/dev/ashmem/dalvik-non moving space") == name) {
309 whichHeap = HEAP_DALVIK;
310 subHeap = HEAP_DALVIK_NON_MOVING;
311 } else if (strstr(name, "/dev/ashmem/dalvik-zygote space") == name) {
312 whichHeap = HEAP_DALVIK;
313 subHeap = HEAP_DALVIK_ZYGOTE;
314 } else if (strstr(name, "/dev/ashmem/dalvik-indirect ref") == name) {
315 subHeap = HEAP_DALVIK_INDIRECT_REFERENCE_TABLE;
316 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
317 subHeap = HEAP_DALVIK_CODE_CACHE;
318 } else {
319 subHeap = HEAP_DALVIK_ACCOUNTING; // Default to accounting.
320 }
321 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
322 whichHeap = HEAP_CURSOR;
323 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
324 whichHeap = HEAP_NATIVE;
325 } else {
326 whichHeap = HEAP_ASHMEM;
327 }
328 } else {
329 whichHeap = HEAP_UNKNOWN_DEV;
330 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700331 } else if (strncmp(name, "[anon:", 6) == 0) {
332 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700333 } else if (nameLen > 0) {
334 whichHeap = HEAP_UNKNOWN_MAP;
335 } else if (start == prevEnd && prevHeap == HEAP_SO) {
336 // bss section of a shared library.
337 whichHeap = HEAP_SO;
338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 }
340
Steve Block6215d3f2012-01-04 20:05:49 +0000341 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800343
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700344 shared_clean = 0;
345 shared_dirty = 0;
346 private_clean = 0;
347 private_dirty = 0;
348 swapped_out = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800349 swapped_out_pss = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 while (true) {
352 if (fgets(line, 1024, fp) == 0) {
353 done = true;
354 break;
355 }
356
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700357 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800358 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700359 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800360 /* resident = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700361 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700363 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700365 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700367 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700369 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700371 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800372 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700373 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
374 swapped_out = temp;
Martijn Coenene0764852016-01-07 17:04:22 -0800375 } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) {
376 *foundSwapPss = true;
377 swapped_out_pss = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700378 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700380 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 break;
382 }
383 }
384
385 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700386 if (is_swappable && (pss > 0)) {
387 sharing_proportion = 0.0;
388 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700389 sharing_proportion = (pss - private_clean
390 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700391 }
392 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
393 } else
394 swappable_pss = 0;
395
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700396 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700397 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700398 stats[whichHeap].privateDirty += private_dirty;
399 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700400 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700401 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700402 stats[whichHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800403 stats[whichHeap].swappedOutPss += swapped_out_pss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700404 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700405 stats[subHeap].pss += pss;
406 stats[subHeap].swappablePss += swappable_pss;
407 stats[subHeap].privateDirty += private_dirty;
408 stats[subHeap].sharedDirty += shared_dirty;
409 stats[subHeap].privateClean += private_clean;
410 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700411 stats[subHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800412 stats[subHeap].swappedOutPss += swapped_out_pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 }
415 }
416}
417
Martijn Coenene0764852016-01-07 17:04:22 -0800418static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419{
420 char tmp[128];
421 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 sprintf(tmp, "/proc/%d/smaps", pid);
424 fp = fopen(tmp, "r");
425 if (fp == 0) return;
426
Martijn Coenene0764852016-01-07 17:04:22 -0800427 read_mapinfo(fp, stats, foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 fclose(fp);
429}
430
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700431static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
432 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433{
Martijn Coenene0764852016-01-07 17:04:22 -0800434 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700435 stats_t stats[_NUM_HEAP];
436 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800437
Martijn Coenene0764852016-01-07 17:04:22 -0800438 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700440 struct graphics_memory_pss graphics_mem;
441 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
442 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
443 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
444 stats[HEAP_GL].pss = graphics_mem.gl;
445 stats[HEAP_GL].privateDirty = graphics_mem.gl;
446 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
447 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700448 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700449
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700450 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700451 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700452 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700453 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
454 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700455 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700456 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700457 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800458 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700459 }
460
461 for (int i=0; i<_NUM_CORE_HEAP; i++) {
462 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700463 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700464 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
465 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700466 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700467 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700468 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800469 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700470 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800471
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700472
Martijn Coenene0764852016-01-07 17:04:22 -0800473 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700474 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800475
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700476 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
477 if (otherArray == NULL) {
478 return;
479 }
480
481 int j=0;
482 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
483 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700484 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700485 otherArray[j++] = stats[i].privateDirty;
486 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700487 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700488 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700489 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800490 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700491 }
492
493 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494}
495
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700496static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
497{
498 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
499}
500
Martijn Coenene0764852016-01-07 17:04:22 -0800501static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
502 jlongArray outUssSwapPss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700503{
504 char line[1024];
505 jlong pss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800506 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700507 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800508 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700509
510 char tmp[128];
511 FILE *fp;
512
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700513 struct graphics_memory_pss graphics_mem;
514 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800515 pss = uss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700516 }
517
Dianne Hackbornb437e092011-08-05 17:50:29 -0700518 sprintf(tmp, "/proc/%d/smaps", pid);
519 fp = fopen(tmp, "r");
Dianne Hackbornb437e092011-08-05 17:50:29 -0700520
Dianne Hackborn37c99432013-09-05 19:34:57 -0700521 if (fp != 0) {
522 while (true) {
523 if (fgets(line, 1024, fp) == NULL) {
524 break;
525 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700526
Dianne Hackborn37c99432013-09-05 19:34:57 -0700527 if (line[0] == 'P') {
528 if (strncmp(line, "Pss:", 4) == 0) {
529 char* c = line + 4;
530 while (*c != 0 && (*c < '0' || *c > '9')) {
531 c++;
532 }
533 pss += atoi(c);
Iliyan Malchev45838de2014-12-11 14:29:36 -0800534 } else if (strncmp(line, "Private_Clean:", 14) == 0
535 || strncmp(line, "Private_Dirty:", 14) == 0) {
Dianne Hackborn37c99432013-09-05 19:34:57 -0700536 char* c = line + 14;
537 while (*c != 0 && (*c < '0' || *c > '9')) {
538 c++;
539 }
540 uss += atoi(c);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700541 }
Martijn Coenene0764852016-01-07 17:04:22 -0800542 } else if (line[0] == 'S' && strncmp(line, "SwapPss:", 8) == 0) {
543 char* c = line + 8;
544 jlong lSwapPss;
545 while (*c != 0 && (*c < '0' || *c > '9')) {
546 c++;
547 }
548 lSwapPss = atoi(c);
549 swapPss += lSwapPss;
550 pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700551 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700552 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700553
Dianne Hackborn37c99432013-09-05 19:34:57 -0700554 fclose(fp);
555 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700556
Martijn Coenene0764852016-01-07 17:04:22 -0800557 if (outUssSwapPss != NULL) {
558 if (env->GetArrayLength(outUssSwapPss) >= 1) {
559 jlong* outUssSwapPssArray = env->GetLongArrayElements(outUssSwapPss, 0);
560 if (outUssSwapPssArray != NULL) {
561 outUssSwapPssArray[0] = uss;
562 if (env->GetArrayLength(outUssSwapPss) >= 2) {
563 outUssSwapPssArray[1] = swapPss;
564 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700565 }
Martijn Coenene0764852016-01-07 17:04:22 -0800566 env->ReleaseLongArrayElements(outUssSwapPss, outUssSwapPssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700567 }
568 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700569
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800570 if (outMemtrack != NULL) {
571 if (env->GetArrayLength(outMemtrack) >= 1) {
572 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
573 if (outMemtrackArray != NULL) {
574 outMemtrackArray[0] = memtrack;
575 }
576 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
577 }
578 }
579
Dianne Hackbornb437e092011-08-05 17:50:29 -0700580 return pss;
581}
582
583static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
584{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800585 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700586}
587
Martijn Coenen398e24e2015-04-23 21:55:58 +0200588static long get_allocated_vmalloc_memory() {
589 char line[1024];
590 long size, vmalloc_allocated_size = 0;
591 FILE* fp = fopen("/proc/vmallocinfo", "r");
592 if (fp == NULL) {
593 return 0;
594 }
595 while (true) {
596 if (fgets(line, 1024, fp) == NULL) {
597 break;
598 }
599
Martijn Coenenab3576e2015-05-22 09:16:45 +0200600 if (!strstr(line, "ioremap") && !strstr(line, "map_lowmem")) {
601 // Ignore ioremap and map_lowmem regions, since they don't actually consume memory
Martijn Coenen398e24e2015-04-23 21:55:58 +0200602 if (sscanf(line, "%*x-%*x %ld", &size) == 1) {
603 vmalloc_allocated_size += size;
604 }
605 }
606 }
607 fclose(fp);
608 return vmalloc_allocated_size;
609}
610
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700611enum {
612 MEMINFO_TOTAL,
613 MEMINFO_FREE,
614 MEMINFO_BUFFERS,
615 MEMINFO_CACHED,
616 MEMINFO_SHMEM,
617 MEMINFO_SLAB,
618 MEMINFO_SWAP_TOTAL,
619 MEMINFO_SWAP_FREE,
620 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700621 MEMINFO_MAPPED,
622 MEMINFO_VMALLOC_USED,
623 MEMINFO_PAGE_TABLES,
624 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700625 MEMINFO_COUNT
626};
627
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800628static long long get_zram_mem_used()
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800629{
630#define ZRAM_SYSFS "/sys/block/zram0/"
631 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
632 if (f) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800633 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800634
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800635 int matched = fscanf(f, "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800636 if (matched != 1)
637 ALOGW("failed to parse " ZRAM_SYSFS "mm_stat");
638
639 fclose(f);
640 return mem_used_total;
641 }
642
643 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
644 if (f) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800645 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800646
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800647 int matched = fscanf(f, "%lld", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800648 if (matched != 1)
649 ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total");
650
651 fclose(f);
652 return mem_used_total;
653 }
654
655 return 0;
656}
657
Dianne Hackborn8e692572013-09-10 19:06:15 -0700658static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
659{
660 char buffer[1024];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200661 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700662
663 if (out == NULL) {
664 jniThrowNullPointerException(env, "out == null");
665 return;
666 }
667
668 int fd = open("/proc/meminfo", O_RDONLY);
669
670 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700671 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700672 return;
673 }
674
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700675 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700676 close(fd);
677
678 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700679 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700680 return;
681 }
682 buffer[len] = 0;
683
684 static const char* const tags[] = {
685 "MemTotal:",
686 "MemFree:",
687 "Buffers:",
688 "Cached:",
689 "Shmem:",
690 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700691 "SwapTotal:",
692 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700693 "ZRam:",
694 "Mapped:",
695 "VmallocUsed:",
696 "PageTables:",
697 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700698 NULL
699 };
700 static const int tagsLen[] = {
701 9,
702 8,
703 8,
704 7,
705 6,
706 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700707 10,
708 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700709 5,
710 7,
711 12,
712 11,
713 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700714 0
715 };
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700716 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700717
718 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200719 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700720 int i = 0;
721 while (tags[i]) {
722 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
723 p += tagsLen[i];
724 while (*p == ' ') p++;
725 char* num = p;
726 while (*p >= '0' && *p <= '9') p++;
727 if (*p != 0) {
728 *p = 0;
729 p++;
730 }
731 mem[i] = atoll(num);
732 numFound++;
733 break;
734 }
735 i++;
736 }
737 while (*p && *p != '\n') {
738 p++;
739 }
740 if (*p) p++;
741 }
742
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800743 mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200744 // Recompute Vmalloc Used since the value in meminfo
745 // doesn't account for I/O remapping which doesn't use RAM.
746 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700747
Dianne Hackborn8e692572013-09-10 19:06:15 -0700748 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700749 if (maxNum > MEMINFO_COUNT) {
750 maxNum = MEMINFO_COUNT;
751 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700752 jlong* outArray = env->GetLongArrayElements(out, 0);
753 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700754 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700755 outArray[i] = mem[i];
756 }
757 }
758 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759}
760
Martijn Coenen398e24e2015-04-23 21:55:58 +0200761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762static jint read_binder_stat(const char* stat)
763{
764 FILE* fp = fopen(BINDER_STATS, "r");
765 if (fp == NULL) {
766 return -1;
767 }
768
769 char line[1024];
770
771 char compare[128];
772 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 // loop until we have the block that represents this process
775 do {
776 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700777 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 return -1;
779 }
780 } while (strncmp(compare, line, len));
781
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800782 // now that we have this process, read until we find the stat that we are looking for
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 do {
786 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700787 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 return -1;
789 }
790 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 // we have the line, now increment the line ptr to the value
793 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700794 jint result = atoi(ptr);
795 fclose(fp);
796 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797}
798
799static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
800{
801 return read_binder_stat("bcTRANSACTION");
802}
803
804static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
805{
806 return read_binder_stat("brTRANSACTION");
807}
808
809// these are implemented in android_util_Binder.cpp
810jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
811jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
812jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
813
Andy McFadden06a6b552010-07-13 16:28:09 -0700814
Andy McFadden06a6b552010-07-13 16:28:09 -0700815/* pulled out of bionic */
816extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
817 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
818extern "C" void free_malloc_leak_info(uint8_t* info);
819#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
820#define BACKTRACE_SIZE 32
821
822/*
823 * This is a qsort() callback.
824 *
825 * See dumpNativeHeap() for comments about the data format and sort order.
826 */
827static int compareHeapRecords(const void* vrec1, const void* vrec2)
828{
829 const size_t* rec1 = (const size_t*) vrec1;
830 const size_t* rec2 = (const size_t*) vrec2;
831 size_t size1 = *rec1;
832 size_t size2 = *rec2;
833
834 if (size1 < size2) {
835 return 1;
836 } else if (size1 > size2) {
837 return -1;
838 }
839
840 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
841 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
842 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
843 intptr_t addr1 = bt1[idx];
844 intptr_t addr2 = bt2[idx];
845 if (addr1 == addr2) {
846 if (addr1 == 0)
847 break;
848 continue;
849 }
850 if (addr1 < addr2) {
851 return -1;
852 } else if (addr1 > addr2) {
853 return 1;
854 }
855 }
856
857 return 0;
858}
859
860/*
861 * The get_malloc_leak_info() call returns an array of structs that
862 * look like this:
863 *
864 * size_t size
865 * size_t allocations
866 * intptr_t backtrace[32]
867 *
868 * "size" is the size of the allocation, "backtrace" is a fixed-size
869 * array of function pointers, and "allocations" is the number of
870 * allocations with the exact same size and backtrace.
871 *
872 * The entries are sorted by descending total size (i.e. size*allocations)
873 * then allocation count. For best results with "diff" we'd like to sort
874 * primarily by individual size then stack trace. Since the entries are
875 * fixed-size, and we're allowed (by the current implementation) to mangle
876 * them, we can do this in place.
877 */
878static void dumpNativeHeap(FILE* fp)
879{
880 uint8_t* info = NULL;
881 size_t overallSize, infoSize, totalMemory, backtraceSize;
882
883 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
884 &backtraceSize);
885 if (info == NULL) {
886 fprintf(fp, "Native heap dump not available. To enable, run these"
887 " commands (requires root):\n");
888 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
889 fprintf(fp, "$ adb shell stop\n");
890 fprintf(fp, "$ adb shell start\n");
891 return;
892 }
893 assert(infoSize != 0);
894 assert(overallSize % infoSize == 0);
895
896 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
897
898 size_t recordCount = overallSize / infoSize;
899 fprintf(fp, "Total memory: %zu\n", totalMemory);
900 fprintf(fp, "Allocation records: %zd\n", recordCount);
901 if (backtraceSize != BACKTRACE_SIZE) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000902 fprintf(fp, "WARNING: mismatched backtrace sizes (%zu vs. %d)\n",
Andy McFadden06a6b552010-07-13 16:28:09 -0700903 backtraceSize, BACKTRACE_SIZE);
904 }
905 fprintf(fp, "\n");
906
907 /* re-sort the entries */
908 qsort(info, recordCount, infoSize, compareHeapRecords);
909
910 /* dump the entries to the file */
911 const uint8_t* ptr = info;
912 for (size_t idx = 0; idx < recordCount; idx++) {
913 size_t size = *(size_t*) ptr;
914 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
915 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
916
917 fprintf(fp, "z %d sz %8zu num %4zu bt",
918 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
919 size & ~SIZE_FLAG_ZYGOTE_CHILD,
920 allocations);
921 for (size_t bt = 0; bt < backtraceSize; bt++) {
922 if (backtrace[bt] == 0) {
923 break;
924 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000925#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -0700926 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000927#else
Mark Salyzyn85394032014-04-16 10:28:37 -0700928 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000929#endif
Andy McFadden06a6b552010-07-13 16:28:09 -0700930 }
931 }
932 fprintf(fp, "\n");
933
934 ptr += infoSize;
935 }
936
Andy McFadden06a6b552010-07-13 16:28:09 -0700937 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700938
939 fprintf(fp, "MAPS\n");
940 const char* maps = "/proc/self/maps";
941 FILE* in = fopen(maps, "r");
942 if (in == NULL) {
943 fprintf(fp, "Could not open %s\n", maps);
944 return;
945 }
946 char buf[BUFSIZ];
947 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
948 fwrite(buf, sizeof(char), n, fp);
949 }
950 fclose(in);
951
952 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700953}
Andy McFadden06a6b552010-07-13 16:28:09 -0700954
955/*
956 * Dump the native heap, writing human-readable output to the specified
957 * file descriptor.
958 */
959static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
960 jobject fileDescriptor)
961{
962 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800963 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700964 return;
965 }
966 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
967 if (origFd < 0) {
968 jniThrowRuntimeException(env, "Invalid file descriptor");
969 return;
970 }
971
972 /* dup() the descriptor so we don't close the original with fclose() */
973 int fd = dup(origFd);
974 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000975 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700976 jniThrowRuntimeException(env, "dup() failed");
977 return;
978 }
979
980 FILE* fp = fdopen(fd, "w");
981 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000982 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700983 close(fd);
984 jniThrowRuntimeException(env, "fdopen() failed");
985 return;
986 }
987
Steve Block5baa3a62011-12-20 16:23:08 +0000988 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700989 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000990 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700991
992 fclose(fp);
993}
994
995
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700996static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
997 jint pid, jstring fileName)
998{
999 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001000 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001001 return;
1002 }
1003 const jchar* str = env->GetStringCritical(fileName, 0);
1004 String8 fileName8;
1005 if (str) {
Dan Albert66987492014-11-20 11:41:21 -08001006 fileName8 = String8(reinterpret_cast<const char16_t*>(str),
1007 env->GetStringLength(fileName));
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001008 env->ReleaseStringCritical(fileName, str);
1009 }
1010
1011 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
1012 if (fd < 0) {
1013 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
1014 return;
1015 }
1016
1017 if (lseek(fd, 0, SEEK_END) < 0) {
1018 fprintf(stderr, "lseek: %s\n", strerror(errno));
1019 } else {
1020 dump_backtrace_to_file(pid, fd);
1021 }
1022
1023 close(fd);
1024}
1025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026/*
1027 * JNI registration.
1028 */
1029
Daniel Micay76f6a862015-09-19 17:31:01 -04001030static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 { "getNativeHeapSize", "()J",
1032 (void*) android_os_Debug_getNativeHeapSize },
1033 { "getNativeHeapAllocatedSize", "()J",
1034 (void*) android_os_Debug_getNativeHeapAllocatedSize },
1035 { "getNativeHeapFreeSize", "()J",
1036 (void*) android_os_Debug_getNativeHeapFreeSize },
1037 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1038 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001039 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1040 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001041 { "getPss", "()J",
1042 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001043 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001044 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001045 { "getMemInfo", "([J)V",
1046 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001047 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1048 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 { "getBinderSentTransactions", "()I",
1050 (void*) android_os_Debug_getBinderSentTransactions },
1051 { "getBinderReceivedTransactions", "()I",
1052 (void*) android_os_getBinderReceivedTransactions },
1053 { "getBinderLocalObjectCount", "()I",
1054 (void*)android_os_Debug_getLocalObjectCount },
1055 { "getBinderProxyObjectCount", "()I",
1056 (void*)android_os_Debug_getProxyObjectCount },
1057 { "getBinderDeathObjectCount", "()I",
1058 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001059 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
1060 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061};
1062
1063int register_android_os_Debug(JNIEnv *env)
1064{
Adam Lesinski5b4ef812013-09-23 10:06:09 -07001065 int err = memtrack_init();
1066 if (err != 0) {
1067 memtrackLoaded = false;
1068 ALOGE("failed to load memtrack module: %d", err);
1069 } else {
1070 memtrackLoaded = true;
1071 }
1072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001075 // Sanity check the number of other statistics expected in Java matches here.
1076 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1077 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001078 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1079 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001080 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001081 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001082 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001083 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1084 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001085 return JNI_ERR;
1086 }
1087
1088 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -08001089 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001090
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001091 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1092 stat_fields[i].pss_field =
1093 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001094 stat_fields[i].pssSwappable_field =
1095 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001096 stat_fields[i].privateDirty_field =
1097 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1098 stat_fields[i].sharedDirty_field =
1099 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001100 stat_fields[i].privateClean_field =
1101 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001102 stat_fields[i].sharedClean_field =
1103 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001104 stat_fields[i].swappedOut_field =
1105 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -08001106 stat_fields[i].swappedOutPss_field =
1107 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001108 }
1109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1111}
1112
Andy McFadden06a6b552010-07-13 16:28:09 -07001113}; // namespace android