blob: 9b8243d93705a3182dcb3a619a36a2228bb8ca54 [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
38#ifdef HAVE_MALLOC_H
39#include <malloc.h>
40#endif
41
42namespace android
43{
44
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070045enum {
46 HEAP_UNKNOWN,
47 HEAP_DALVIK,
48 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080049
Dianne Hackborn64770d12013-05-23 17:51:19 -070050 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080051 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070052 HEAP_CURSOR,
53 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080054 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070055 HEAP_UNKNOWN_DEV,
56 HEAP_SO,
57 HEAP_JAR,
58 HEAP_APK,
59 HEAP_TTF,
60 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070061 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070062 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070063 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070064 HEAP_GRAPHICS,
65 HEAP_GL,
66 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070067
Anwar Ghuloum3c615062013-05-13 14:18:02 -070068 HEAP_DALVIK_NORMAL,
69 HEAP_DALVIK_LARGE,
70 HEAP_DALVIK_LINEARALLOC,
71 HEAP_DALVIK_ACCOUNTING,
72 HEAP_DALVIK_CODE_CACHE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080073 HEAP_DALVIK_ZYGOTE,
74 HEAP_DALVIK_NON_MOVING,
75 HEAP_DALVIK_INDIRECT_REFERENCE_TABLE,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070076
77 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070078 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070079 _NUM_CORE_HEAP = HEAP_NATIVE+1
80};
81
82struct stat_fields {
83 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070084 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070085 jfieldID privateDirty_field;
86 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070087 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070088 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070089 jfieldID swappedOut_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070090};
91
92struct stat_field_names {
93 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070094 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070095 const char* privateDirty_name;
96 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070097 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070098 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070099 const char* swappedOut_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700100};
101
102static stat_fields stat_fields[_NUM_CORE_HEAP];
103
104static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700105 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
106 "otherPrivateClean", "otherSharedClean", "otherSwappedOut" },
107 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
108 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut" },
109 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
110 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700111};
112
113jfieldID otherStats_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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125};
126
127#define BINDER_STATS "/proc/binder/stats"
128
129static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
130{
131#ifdef HAVE_MALLOC_H
132 struct mallinfo info = mallinfo();
133 return (jlong) info.usmblks;
134#else
135 return -1;
136#endif
137}
138
139static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
140{
141#ifdef HAVE_MALLOC_H
142 struct mallinfo info = mallinfo();
143 return (jlong) info.uordblks;
144#else
145 return -1;
146#endif
147}
148
149static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
150{
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800151#ifdef HAVE_MALLOC_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 struct mallinfo info = mallinfo();
153 return (jlong) info.fordblks;
154#else
155 return -1;
156#endif
157}
158
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700159// Container used to retrieve graphics memory pss
160struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700161{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700162 int graphics;
163 int gl;
164 int other;
165};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700166
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700167/*
168 * Uses libmemtrack to retrieve graphics memory that the process is using.
169 * Any graphics memory reported in /proc/pid/smaps is not included here.
170 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700171static int read_memtrack_memory(struct memtrack_proc* p, int pid,
172 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700173{
174 int err = memtrack_proc_get(p, pid);
175 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700176 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700177 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700178 }
179
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700180 ssize_t pss = memtrack_proc_graphics_pss(p);
181 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700182 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700183 return pss;
184 }
185 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700186
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700187 pss = memtrack_proc_gl_pss(p);
188 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700189 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700190 return pss;
191 }
192 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700193
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700194 pss = memtrack_proc_other_pss(p);
195 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700196 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700197 return pss;
198 }
199 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700200
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700201 return 0;
202}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700203
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700204/*
205 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
206 */
207static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
208{
209 if (!memtrackLoaded) {
210 return -1;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700211 }
212
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700213 struct memtrack_proc* p = memtrack_proc_new();
214 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700215 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700216 return -1;
217 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700218
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700219 int err = read_memtrack_memory(p, pid, graphics_mem);
220 memtrack_proc_destroy(p);
221 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700222}
223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224static void read_mapinfo(FILE *fp, stats_t* stats)
225{
226 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700227 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 bool skip, done = false;
229
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800230 unsigned pss = 0, swappable_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700231 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 unsigned shared_clean = 0, shared_dirty = 0;
233 unsigned private_clean = 0, private_dirty = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700234 unsigned swapped_out = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700235 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 unsigned temp;
237
Colin Cross0c6bc732014-06-17 15:18:07 -0700238 uint64_t start;
239 uint64_t end = 0;
240 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700241 char* name;
242 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700244 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700245 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700246 int prevHeap = HEAP_UNKNOWN;
247
248 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700251 prevHeap = whichHeap;
252 prevEnd = end;
253 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700254 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700256 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
258 len = strlen(line);
259 if (len < 1) return;
260 line[--len] = 0;
261
Colin Cross0c6bc732014-06-17 15:18:07 -0700262 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700263 skip = true;
264 } else {
265 while (isspace(line[name_pos])) {
266 name_pos += 1;
267 }
268 name = line + name_pos;
269 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
Dianne Hackborn64770d12013-05-23 17:51:19 -0700271 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700272 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700273 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
274 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700275 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800276 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700277 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
278 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700279 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700280 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
281 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700282 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700283 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
284 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700285 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700286 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
287 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700288 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700289 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Ian Rogers9f8589c2013-02-22 20:35:06 -0800290 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700291 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700292 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700293 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
294 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700295 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700296 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
297 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700298 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700299 } else if (strncmp(name, "/dev/", 5) == 0) {
300 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
301 whichHeap = HEAP_GL_DEV;
302 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
303 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
304 whichHeap = HEAP_DALVIK_OTHER;
305 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
306 subHeap = HEAP_DALVIK_LINEARALLOC;
307 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
308 (strstr(name, "/dev/ashmem/dalvik-main space") == name)) {
309 // This is the regular Dalvik heap.
310 whichHeap = HEAP_DALVIK;
311 subHeap = HEAP_DALVIK_NORMAL;
Mathieu Chartier2e2069d2015-10-19 13:48:34 -0700312 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name ||
313 strstr(name, "/dev/ashmem/dalvik-free list large object space")
314 == name) {
Mathieu Chartier93084622015-05-01 11:30:22 -0700315 whichHeap = HEAP_DALVIK;
316 subHeap = HEAP_DALVIK_LARGE;
317 } else if (strstr(name, "/dev/ashmem/dalvik-non moving space") == name) {
318 whichHeap = HEAP_DALVIK;
319 subHeap = HEAP_DALVIK_NON_MOVING;
320 } else if (strstr(name, "/dev/ashmem/dalvik-zygote space") == name) {
321 whichHeap = HEAP_DALVIK;
322 subHeap = HEAP_DALVIK_ZYGOTE;
323 } else if (strstr(name, "/dev/ashmem/dalvik-indirect ref") == name) {
324 subHeap = HEAP_DALVIK_INDIRECT_REFERENCE_TABLE;
325 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
326 subHeap = HEAP_DALVIK_CODE_CACHE;
327 } else {
328 subHeap = HEAP_DALVIK_ACCOUNTING; // Default to accounting.
329 }
330 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
331 whichHeap = HEAP_CURSOR;
332 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
333 whichHeap = HEAP_NATIVE;
334 } else {
335 whichHeap = HEAP_ASHMEM;
336 }
337 } else {
338 whichHeap = HEAP_UNKNOWN_DEV;
339 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700340 } else if (strncmp(name, "[anon:", 6) == 0) {
341 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700342 } else if (nameLen > 0) {
343 whichHeap = HEAP_UNKNOWN_MAP;
344 } else if (start == prevEnd && prevHeap == HEAP_SO) {
345 // bss section of a shared library.
346 whichHeap = HEAP_SO;
347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 }
349
Steve Block6215d3f2012-01-04 20:05:49 +0000350 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800352
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700353 shared_clean = 0;
354 shared_dirty = 0;
355 private_clean = 0;
356 private_dirty = 0;
357 swapped_out = 0;
358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 while (true) {
360 if (fgets(line, 1024, fp) == 0) {
361 done = true;
362 break;
363 }
364
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700365 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800366 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700367 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800368 /* resident = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700369 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700371 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700373 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700375 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700377 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700379 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800380 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700381 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
382 swapped_out = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700383 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700385 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 break;
387 }
388 }
389
390 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700391 if (is_swappable && (pss > 0)) {
392 sharing_proportion = 0.0;
393 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700394 sharing_proportion = (pss - private_clean
395 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700396 }
397 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
398 } else
399 swappable_pss = 0;
400
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700401 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700402 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700403 stats[whichHeap].privateDirty += private_dirty;
404 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700405 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700406 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700407 stats[whichHeap].swappedOut += swapped_out;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700408 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700409 stats[subHeap].pss += pss;
410 stats[subHeap].swappablePss += swappable_pss;
411 stats[subHeap].privateDirty += private_dirty;
412 stats[subHeap].sharedDirty += shared_dirty;
413 stats[subHeap].privateClean += private_clean;
414 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700415 stats[subHeap].swappedOut += swapped_out;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418 }
419}
420
421static void load_maps(int pid, stats_t* stats)
422{
423 char tmp[128];
424 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 sprintf(tmp, "/proc/%d/smaps", pid);
427 fp = fopen(tmp, "r");
428 if (fp == 0) return;
429
430 read_mapinfo(fp, stats);
431 fclose(fp);
432}
433
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700434static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
435 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700437 stats_t stats[_NUM_HEAP];
438 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800439
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700440 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700442 struct graphics_memory_pss graphics_mem;
443 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
444 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
445 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
446 stats[HEAP_GL].pss = graphics_mem.gl;
447 stats[HEAP_GL].privateDirty = graphics_mem.gl;
448 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
449 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700450 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700451
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700452 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700453 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700454 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700455 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
456 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700457 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700458 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700459 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700460 }
461
462 for (int i=0; i<_NUM_CORE_HEAP; i++) {
463 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700464 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700465 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
466 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700467 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700468 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700469 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700470 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800471
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700472
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700473 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800474
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700475 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
476 if (otherArray == NULL) {
477 return;
478 }
479
480 int j=0;
481 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
482 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700483 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 otherArray[j++] = stats[i].privateDirty;
485 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700486 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700487 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700488 otherArray[j++] = stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700489 }
490
491 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492}
493
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700494static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
495{
496 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
497}
498
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800499static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, jlongArray outUss,
500 jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700501{
502 char line[1024];
503 jlong pss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700504 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800505 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700506
507 char tmp[128];
508 FILE *fp;
509
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700510 struct graphics_memory_pss graphics_mem;
511 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800512 pss = uss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700513 }
514
Dianne Hackbornb437e092011-08-05 17:50:29 -0700515 sprintf(tmp, "/proc/%d/smaps", pid);
516 fp = fopen(tmp, "r");
Dianne Hackbornb437e092011-08-05 17:50:29 -0700517
Dianne Hackborn37c99432013-09-05 19:34:57 -0700518 if (fp != 0) {
519 while (true) {
520 if (fgets(line, 1024, fp) == NULL) {
521 break;
522 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700523
Dianne Hackborn37c99432013-09-05 19:34:57 -0700524 if (line[0] == 'P') {
525 if (strncmp(line, "Pss:", 4) == 0) {
526 char* c = line + 4;
527 while (*c != 0 && (*c < '0' || *c > '9')) {
528 c++;
529 }
530 pss += atoi(c);
Iliyan Malchev45838de2014-12-11 14:29:36 -0800531 } else if (strncmp(line, "Private_Clean:", 14) == 0
532 || strncmp(line, "Private_Dirty:", 14) == 0) {
Dianne Hackborn37c99432013-09-05 19:34:57 -0700533 char* c = line + 14;
534 while (*c != 0 && (*c < '0' || *c > '9')) {
535 c++;
536 }
537 uss += atoi(c);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700538 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700539 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700540 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700541
Dianne Hackborn37c99432013-09-05 19:34:57 -0700542 fclose(fp);
543 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700544
Dianne Hackbornc8230512013-07-13 21:32:12 -0700545 if (outUss != NULL) {
546 if (env->GetArrayLength(outUss) >= 1) {
547 jlong* outUssArray = env->GetLongArrayElements(outUss, 0);
548 if (outUssArray != NULL) {
549 outUssArray[0] = uss;
550 }
551 env->ReleaseLongArrayElements(outUss, outUssArray, 0);
552 }
553 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700554
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800555 if (outMemtrack != NULL) {
556 if (env->GetArrayLength(outMemtrack) >= 1) {
557 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
558 if (outMemtrackArray != NULL) {
559 outMemtrackArray[0] = memtrack;
560 }
561 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
562 }
563 }
564
Dianne Hackbornb437e092011-08-05 17:50:29 -0700565 return pss;
566}
567
568static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
569{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800570 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700571}
572
Martijn Coenen398e24e2015-04-23 21:55:58 +0200573static long get_allocated_vmalloc_memory() {
574 char line[1024];
575 long size, vmalloc_allocated_size = 0;
576 FILE* fp = fopen("/proc/vmallocinfo", "r");
577 if (fp == NULL) {
578 return 0;
579 }
580 while (true) {
581 if (fgets(line, 1024, fp) == NULL) {
582 break;
583 }
584
Martijn Coenenab3576e2015-05-22 09:16:45 +0200585 if (!strstr(line, "ioremap") && !strstr(line, "map_lowmem")) {
586 // Ignore ioremap and map_lowmem regions, since they don't actually consume memory
Martijn Coenen398e24e2015-04-23 21:55:58 +0200587 if (sscanf(line, "%*x-%*x %ld", &size) == 1) {
588 vmalloc_allocated_size += size;
589 }
590 }
591 }
592 fclose(fp);
593 return vmalloc_allocated_size;
594}
595
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700596enum {
597 MEMINFO_TOTAL,
598 MEMINFO_FREE,
599 MEMINFO_BUFFERS,
600 MEMINFO_CACHED,
601 MEMINFO_SHMEM,
602 MEMINFO_SLAB,
603 MEMINFO_SWAP_TOTAL,
604 MEMINFO_SWAP_FREE,
605 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700606 MEMINFO_MAPPED,
607 MEMINFO_VMALLOC_USED,
608 MEMINFO_PAGE_TABLES,
609 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700610 MEMINFO_COUNT
611};
612
Dianne Hackborn8e692572013-09-10 19:06:15 -0700613static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
614{
615 char buffer[1024];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200616 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700617
618 if (out == NULL) {
619 jniThrowNullPointerException(env, "out == null");
620 return;
621 }
622
623 int fd = open("/proc/meminfo", O_RDONLY);
624
625 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700626 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700627 return;
628 }
629
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700630 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700631 close(fd);
632
633 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700634 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700635 return;
636 }
637 buffer[len] = 0;
638
639 static const char* const tags[] = {
640 "MemTotal:",
641 "MemFree:",
642 "Buffers:",
643 "Cached:",
644 "Shmem:",
645 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700646 "SwapTotal:",
647 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700648 "ZRam:",
649 "Mapped:",
650 "VmallocUsed:",
651 "PageTables:",
652 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700653 NULL
654 };
655 static const int tagsLen[] = {
656 9,
657 8,
658 8,
659 7,
660 6,
661 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700662 10,
663 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700664 5,
665 7,
666 12,
667 11,
668 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700669 0
670 };
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700671 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700672
673 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200674 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700675 int i = 0;
676 while (tags[i]) {
677 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
678 p += tagsLen[i];
679 while (*p == ' ') p++;
680 char* num = p;
681 while (*p >= '0' && *p <= '9') p++;
682 if (*p != 0) {
683 *p = 0;
684 p++;
685 }
686 mem[i] = atoll(num);
687 numFound++;
688 break;
689 }
690 i++;
691 }
692 while (*p && *p != '\n') {
693 p++;
694 }
695 if (*p) p++;
696 }
697
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700698 fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
699 if (fd >= 0) {
700 len = read(fd, buffer, sizeof(buffer)-1);
701 close(fd);
702 if (len > 0) {
703 buffer[len] = 0;
Dianne Hackborncb428552013-09-26 11:07:17 -0700704 mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700705 }
706 }
Martijn Coenen398e24e2015-04-23 21:55:58 +0200707 // Recompute Vmalloc Used since the value in meminfo
708 // doesn't account for I/O remapping which doesn't use RAM.
709 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700710
Dianne Hackborn8e692572013-09-10 19:06:15 -0700711 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700712 if (maxNum > MEMINFO_COUNT) {
713 maxNum = MEMINFO_COUNT;
714 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700715 jlong* outArray = env->GetLongArrayElements(out, 0);
716 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700717 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700718 outArray[i] = mem[i];
719 }
720 }
721 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722}
723
Martijn Coenen398e24e2015-04-23 21:55:58 +0200724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725static jint read_binder_stat(const char* stat)
726{
727 FILE* fp = fopen(BINDER_STATS, "r");
728 if (fp == NULL) {
729 return -1;
730 }
731
732 char line[1024];
733
734 char compare[128];
735 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 // loop until we have the block that represents this process
738 do {
739 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700740 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 return -1;
742 }
743 } while (strncmp(compare, line, len));
744
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800745 // 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 -0800746 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 do {
749 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700750 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 return -1;
752 }
753 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 // we have the line, now increment the line ptr to the value
756 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700757 jint result = atoi(ptr);
758 fclose(fp);
759 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760}
761
762static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
763{
764 return read_binder_stat("bcTRANSACTION");
765}
766
767static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
768{
769 return read_binder_stat("brTRANSACTION");
770}
771
772// these are implemented in android_util_Binder.cpp
773jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
774jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
775jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
776
Andy McFadden06a6b552010-07-13 16:28:09 -0700777
Andy McFadden06a6b552010-07-13 16:28:09 -0700778/* pulled out of bionic */
779extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
780 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
781extern "C" void free_malloc_leak_info(uint8_t* info);
782#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
783#define BACKTRACE_SIZE 32
784
785/*
786 * This is a qsort() callback.
787 *
788 * See dumpNativeHeap() for comments about the data format and sort order.
789 */
790static int compareHeapRecords(const void* vrec1, const void* vrec2)
791{
792 const size_t* rec1 = (const size_t*) vrec1;
793 const size_t* rec2 = (const size_t*) vrec2;
794 size_t size1 = *rec1;
795 size_t size2 = *rec2;
796
797 if (size1 < size2) {
798 return 1;
799 } else if (size1 > size2) {
800 return -1;
801 }
802
803 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
804 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
805 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
806 intptr_t addr1 = bt1[idx];
807 intptr_t addr2 = bt2[idx];
808 if (addr1 == addr2) {
809 if (addr1 == 0)
810 break;
811 continue;
812 }
813 if (addr1 < addr2) {
814 return -1;
815 } else if (addr1 > addr2) {
816 return 1;
817 }
818 }
819
820 return 0;
821}
822
823/*
824 * The get_malloc_leak_info() call returns an array of structs that
825 * look like this:
826 *
827 * size_t size
828 * size_t allocations
829 * intptr_t backtrace[32]
830 *
831 * "size" is the size of the allocation, "backtrace" is a fixed-size
832 * array of function pointers, and "allocations" is the number of
833 * allocations with the exact same size and backtrace.
834 *
835 * The entries are sorted by descending total size (i.e. size*allocations)
836 * then allocation count. For best results with "diff" we'd like to sort
837 * primarily by individual size then stack trace. Since the entries are
838 * fixed-size, and we're allowed (by the current implementation) to mangle
839 * them, we can do this in place.
840 */
841static void dumpNativeHeap(FILE* fp)
842{
843 uint8_t* info = NULL;
844 size_t overallSize, infoSize, totalMemory, backtraceSize;
845
846 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
847 &backtraceSize);
848 if (info == NULL) {
849 fprintf(fp, "Native heap dump not available. To enable, run these"
850 " commands (requires root):\n");
851 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
852 fprintf(fp, "$ adb shell stop\n");
853 fprintf(fp, "$ adb shell start\n");
854 return;
855 }
856 assert(infoSize != 0);
857 assert(overallSize % infoSize == 0);
858
859 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
860
861 size_t recordCount = overallSize / infoSize;
862 fprintf(fp, "Total memory: %zu\n", totalMemory);
863 fprintf(fp, "Allocation records: %zd\n", recordCount);
864 if (backtraceSize != BACKTRACE_SIZE) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000865 fprintf(fp, "WARNING: mismatched backtrace sizes (%zu vs. %d)\n",
Andy McFadden06a6b552010-07-13 16:28:09 -0700866 backtraceSize, BACKTRACE_SIZE);
867 }
868 fprintf(fp, "\n");
869
870 /* re-sort the entries */
871 qsort(info, recordCount, infoSize, compareHeapRecords);
872
873 /* dump the entries to the file */
874 const uint8_t* ptr = info;
875 for (size_t idx = 0; idx < recordCount; idx++) {
876 size_t size = *(size_t*) ptr;
877 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
878 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
879
880 fprintf(fp, "z %d sz %8zu num %4zu bt",
881 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
882 size & ~SIZE_FLAG_ZYGOTE_CHILD,
883 allocations);
884 for (size_t bt = 0; bt < backtraceSize; bt++) {
885 if (backtrace[bt] == 0) {
886 break;
887 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000888#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -0700889 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000890#else
Mark Salyzyn85394032014-04-16 10:28:37 -0700891 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000892#endif
Andy McFadden06a6b552010-07-13 16:28:09 -0700893 }
894 }
895 fprintf(fp, "\n");
896
897 ptr += infoSize;
898 }
899
Andy McFadden06a6b552010-07-13 16:28:09 -0700900 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700901
902 fprintf(fp, "MAPS\n");
903 const char* maps = "/proc/self/maps";
904 FILE* in = fopen(maps, "r");
905 if (in == NULL) {
906 fprintf(fp, "Could not open %s\n", maps);
907 return;
908 }
909 char buf[BUFSIZ];
910 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
911 fwrite(buf, sizeof(char), n, fp);
912 }
913 fclose(in);
914
915 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700916}
Andy McFadden06a6b552010-07-13 16:28:09 -0700917
918/*
919 * Dump the native heap, writing human-readable output to the specified
920 * file descriptor.
921 */
922static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
923 jobject fileDescriptor)
924{
925 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800926 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700927 return;
928 }
929 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
930 if (origFd < 0) {
931 jniThrowRuntimeException(env, "Invalid file descriptor");
932 return;
933 }
934
935 /* dup() the descriptor so we don't close the original with fclose() */
936 int fd = dup(origFd);
937 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000938 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700939 jniThrowRuntimeException(env, "dup() failed");
940 return;
941 }
942
943 FILE* fp = fdopen(fd, "w");
944 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000945 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700946 close(fd);
947 jniThrowRuntimeException(env, "fdopen() failed");
948 return;
949 }
950
Steve Block5baa3a62011-12-20 16:23:08 +0000951 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700952 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000953 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700954
955 fclose(fp);
956}
957
958
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700959static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
960 jint pid, jstring fileName)
961{
962 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800963 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700964 return;
965 }
966 const jchar* str = env->GetStringCritical(fileName, 0);
967 String8 fileName8;
968 if (str) {
Dan Albert66987492014-11-20 11:41:21 -0800969 fileName8 = String8(reinterpret_cast<const char16_t*>(str),
970 env->GetStringLength(fileName));
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700971 env->ReleaseStringCritical(fileName, str);
972 }
973
974 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
975 if (fd < 0) {
976 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
977 return;
978 }
979
980 if (lseek(fd, 0, SEEK_END) < 0) {
981 fprintf(stderr, "lseek: %s\n", strerror(errno));
982 } else {
983 dump_backtrace_to_file(pid, fd);
984 }
985
986 close(fd);
987}
988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989/*
990 * JNI registration.
991 */
992
993static JNINativeMethod gMethods[] = {
994 { "getNativeHeapSize", "()J",
995 (void*) android_os_Debug_getNativeHeapSize },
996 { "getNativeHeapAllocatedSize", "()J",
997 (void*) android_os_Debug_getNativeHeapAllocatedSize },
998 { "getNativeHeapFreeSize", "()J",
999 (void*) android_os_Debug_getNativeHeapFreeSize },
1000 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1001 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001002 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1003 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001004 { "getPss", "()J",
1005 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001006 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001007 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001008 { "getMemInfo", "([J)V",
1009 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001010 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1011 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 { "getBinderSentTransactions", "()I",
1013 (void*) android_os_Debug_getBinderSentTransactions },
1014 { "getBinderReceivedTransactions", "()I",
1015 (void*) android_os_getBinderReceivedTransactions },
1016 { "getBinderLocalObjectCount", "()I",
1017 (void*)android_os_Debug_getLocalObjectCount },
1018 { "getBinderProxyObjectCount", "()I",
1019 (void*)android_os_Debug_getProxyObjectCount },
1020 { "getBinderDeathObjectCount", "()I",
1021 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001022 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
1023 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024};
1025
1026int register_android_os_Debug(JNIEnv *env)
1027{
Adam Lesinski5b4ef812013-09-23 10:06:09 -07001028 int err = memtrack_init();
1029 if (err != 0) {
1030 memtrackLoaded = false;
1031 ALOGE("failed to load memtrack module: %d", err);
1032 } else {
1033 memtrackLoaded = true;
1034 }
1035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001038 // Sanity check the number of other statistics expected in Java matches here.
1039 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1040 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001041 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1042 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001043 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001044 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001045 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001046 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1047 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001048 return JNI_ERR;
1049 }
1050
1051 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
1052
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001053 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1054 stat_fields[i].pss_field =
1055 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001056 stat_fields[i].pssSwappable_field =
1057 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001058 stat_fields[i].privateDirty_field =
1059 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1060 stat_fields[i].sharedDirty_field =
1061 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001062 stat_fields[i].privateClean_field =
1063 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001064 stat_fields[i].sharedClean_field =
1065 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001066 stat_fields[i].swappedOut_field =
1067 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001068 }
1069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1071}
1072
Andy McFadden06a6b552010-07-13 16:28:09 -07001073}; // namespace android