blob: 178bab6bba5d695a868a44d9ee969e76947f5aa2 [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,
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;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070087};
88
89struct stat_field_names {
90 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070091 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070092 const char* privateDirty_name;
93 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070094 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070095 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070096 const char* swappedOut_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070097};
98
99static stat_fields stat_fields[_NUM_CORE_HEAP];
100
101static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700102 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
103 "otherPrivateClean", "otherSharedClean", "otherSwappedOut" },
104 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
105 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut" },
106 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
107 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700108};
109
110jfieldID otherStats_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700112static bool memtrackLoaded;
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700115 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700116 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700117 int privateDirty;
118 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700119 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700120 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700121 int swappedOut;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122};
123
124#define BINDER_STATS "/proc/binder/stats"
125
126static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
127{
128#ifdef HAVE_MALLOC_H
129 struct mallinfo info = mallinfo();
130 return (jlong) info.usmblks;
131#else
132 return -1;
133#endif
134}
135
136static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
137{
138#ifdef HAVE_MALLOC_H
139 struct mallinfo info = mallinfo();
140 return (jlong) info.uordblks;
141#else
142 return -1;
143#endif
144}
145
146static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
147{
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800148#ifdef HAVE_MALLOC_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 struct mallinfo info = mallinfo();
150 return (jlong) info.fordblks;
151#else
152 return -1;
153#endif
154}
155
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700156// Container used to retrieve graphics memory pss
157struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700158{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700159 int graphics;
160 int gl;
161 int other;
162};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700163
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700164/*
165 * Uses libmemtrack to retrieve graphics memory that the process is using.
166 * Any graphics memory reported in /proc/pid/smaps is not included here.
167 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700168static int read_memtrack_memory(struct memtrack_proc* p, int pid,
169 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700170{
171 int err = memtrack_proc_get(p, pid);
172 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700173 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700174 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700175 }
176
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700177 ssize_t pss = memtrack_proc_graphics_pss(p);
178 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700179 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700180 return pss;
181 }
182 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700183
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700184 pss = memtrack_proc_gl_pss(p);
185 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700186 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700187 return pss;
188 }
189 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700190
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700191 pss = memtrack_proc_other_pss(p);
192 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700193 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700194 return pss;
195 }
196 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700197
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700198 return 0;
199}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700200
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700201/*
202 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
203 */
204static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
205{
206 if (!memtrackLoaded) {
207 return -1;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700208 }
209
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700210 struct memtrack_proc* p = memtrack_proc_new();
211 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700212 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700213 return -1;
214 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700215
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700216 int err = read_memtrack_memory(p, pid, graphics_mem);
217 memtrack_proc_destroy(p);
218 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700219}
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221static void read_mapinfo(FILE *fp, stats_t* stats)
222{
223 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700224 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 bool skip, done = false;
226
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700227 unsigned size = 0, resident = 0, pss = 0, swappable_pss = 0;
228 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 unsigned shared_clean = 0, shared_dirty = 0;
230 unsigned private_clean = 0, private_dirty = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700231 unsigned swapped_out = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700232 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 unsigned referenced = 0;
234 unsigned temp;
235
Colin Cross0c6bc732014-06-17 15:18:07 -0700236 uint64_t start;
237 uint64_t end = 0;
238 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700239 char* name;
240 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700242 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700243 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700244 int prevHeap = HEAP_UNKNOWN;
245
246 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700249 prevHeap = whichHeap;
250 prevEnd = end;
251 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700252 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700254 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 len = strlen(line);
257 if (len < 1) return;
258 line[--len] = 0;
259
Colin Cross0c6bc732014-06-17 15:18:07 -0700260 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700261 skip = true;
262 } else {
263 while (isspace(line[name_pos])) {
264 name_pos += 1;
265 }
266 name = line + name_pos;
267 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Dianne Hackborn64770d12013-05-23 17:51:19 -0700269 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700270 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700271 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
272 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
273 whichHeap = HEAP_DALVIK_OTHER;
274 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
275 subHeap = HEAP_DALVIK_LINEARALLOC;
Mathieu Chartiereee97642014-09-23 17:24:30 -0700276 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
277 (strstr(name, "/dev/ashmem/dalvik-main space") == name) ||
278 (strstr(name, "/dev/ashmem/dalvik-non moving space") == name)) {
279 // This is the regular Dalvik heap.
280 whichHeap = HEAP_DALVIK;
281 subHeap = HEAP_DALVIK_NORMAL;
282 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name) {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700283 whichHeap = HEAP_DALVIK;
284 subHeap = HEAP_DALVIK_LARGE;
285 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
286 subHeap = HEAP_DALVIK_CODE_CACHE;
287 } else {
Mathieu Chartiereee97642014-09-23 17:24:30 -0700288 subHeap = HEAP_DALVIK_ACCOUNTING; // Default to accounting.
Dianne Hackborn64770d12013-05-23 17:51:19 -0700289 }
290 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
291 whichHeap = HEAP_CURSOR;
292 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
293 whichHeap = HEAP_NATIVE;
294 } else {
295 whichHeap = HEAP_ASHMEM;
296 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700297 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
298 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700299 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800300 whichHeap = HEAP_STACK;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700301 } else if (strncmp(name, "/dev/", 5) == 0) {
Dianne Hackborn18429302014-11-03 13:58:11 -0800302 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
303 whichHeap = HEAP_GL_DEV;
304 } else {
305 whichHeap = HEAP_UNKNOWN_DEV;
306 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700307 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
308 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700309 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700310 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
311 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700312 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700313 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
314 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700315 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700316 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
317 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700318 is_swappable = true;
Ian Rogers9f8589c2013-02-22 20:35:06 -0800319 } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) ||
320 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700321 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700322 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700323 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
324 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700325 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700326 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
327 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700328 is_swappable = true;
Colin Crosscb4728f2013-08-09 13:23:46 -0700329 } else if (strncmp(name, "[anon:", 6) == 0) {
330 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700331 } else if (nameLen > 0) {
332 whichHeap = HEAP_UNKNOWN_MAP;
333 } else if (start == prevEnd && prevHeap == HEAP_SO) {
334 // bss section of a shared library.
335 whichHeap = HEAP_SO;
336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338
Steve Block6215d3f2012-01-04 20:05:49 +0000339 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800341
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700342 shared_clean = 0;
343 shared_dirty = 0;
344 private_clean = 0;
345 private_dirty = 0;
346 swapped_out = 0;
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 while (true) {
349 if (fgets(line, 1024, fp) == 0) {
350 done = true;
351 break;
352 }
353
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700354 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 size = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700356 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 resident = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700358 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700360 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700362 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700364 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700366 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700368 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 referenced = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700370 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
371 swapped_out = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700372 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700374 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 break;
376 }
377 }
378
379 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700380 if (is_swappable && (pss > 0)) {
381 sharing_proportion = 0.0;
382 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700383 sharing_proportion = (pss - private_clean
384 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700385 }
386 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
387 } else
388 swappable_pss = 0;
389
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700390 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700391 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700392 stats[whichHeap].privateDirty += private_dirty;
393 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700394 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700395 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700396 stats[whichHeap].swappedOut += swapped_out;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700397 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700398 stats[subHeap].pss += pss;
399 stats[subHeap].swappablePss += swappable_pss;
400 stats[subHeap].privateDirty += private_dirty;
401 stats[subHeap].sharedDirty += shared_dirty;
402 stats[subHeap].privateClean += private_clean;
403 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700404 stats[subHeap].swappedOut += swapped_out;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407 }
408}
409
410static void load_maps(int pid, stats_t* stats)
411{
412 char tmp[128];
413 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 sprintf(tmp, "/proc/%d/smaps", pid);
416 fp = fopen(tmp, "r");
417 if (fp == 0) return;
418
419 read_mapinfo(fp, stats);
420 fclose(fp);
421}
422
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700423static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
424 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700426 stats_t stats[_NUM_HEAP];
427 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800428
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700429 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700431 struct graphics_memory_pss graphics_mem;
432 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
433 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
434 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
435 stats[HEAP_GL].pss = graphics_mem.gl;
436 stats[HEAP_GL].privateDirty = graphics_mem.gl;
437 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
438 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700439 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700440
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700441 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700442 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700443 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700444 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
445 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700446 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700447 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700448 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700449 }
450
451 for (int i=0; i<_NUM_CORE_HEAP; i++) {
452 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700453 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700454 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
455 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700456 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700457 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700458 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700459 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800460
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700461
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700462 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800463
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700464 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
465 if (otherArray == NULL) {
466 return;
467 }
468
469 int j=0;
470 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
471 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700472 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700473 otherArray[j++] = stats[i].privateDirty;
474 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700475 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700476 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700477 otherArray[j++] = stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700478 }
479
480 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481}
482
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700483static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
484{
485 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
486}
487
Dianne Hackbornc8230512013-07-13 21:32:12 -0700488static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, jlongArray outUss)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700489{
490 char line[1024];
491 jlong pss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700492 jlong uss = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700493 unsigned temp;
494
495 char tmp[128];
496 FILE *fp;
497
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700498 struct graphics_memory_pss graphics_mem;
499 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
500 pss = uss = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700501 }
502
Dianne Hackbornb437e092011-08-05 17:50:29 -0700503 sprintf(tmp, "/proc/%d/smaps", pid);
504 fp = fopen(tmp, "r");
Dianne Hackbornb437e092011-08-05 17:50:29 -0700505
Dianne Hackborn37c99432013-09-05 19:34:57 -0700506 if (fp != 0) {
507 while (true) {
508 if (fgets(line, 1024, fp) == NULL) {
509 break;
510 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700511
Dianne Hackborn37c99432013-09-05 19:34:57 -0700512 if (line[0] == 'P') {
513 if (strncmp(line, "Pss:", 4) == 0) {
514 char* c = line + 4;
515 while (*c != 0 && (*c < '0' || *c > '9')) {
516 c++;
517 }
518 pss += atoi(c);
519 } else if (strncmp(line, "Private_Clean:", 14)
520 || strncmp(line, "Private_Dirty:", 14)) {
521 char* c = line + 14;
522 while (*c != 0 && (*c < '0' || *c > '9')) {
523 c++;
524 }
525 uss += atoi(c);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700526 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700527 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700528 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700529
Dianne Hackborn37c99432013-09-05 19:34:57 -0700530 fclose(fp);
531 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700532
Dianne Hackbornc8230512013-07-13 21:32:12 -0700533 if (outUss != NULL) {
534 if (env->GetArrayLength(outUss) >= 1) {
535 jlong* outUssArray = env->GetLongArrayElements(outUss, 0);
536 if (outUssArray != NULL) {
537 outUssArray[0] = uss;
538 }
539 env->ReleaseLongArrayElements(outUss, outUssArray, 0);
540 }
541 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700542
543 return pss;
544}
545
546static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
547{
Dianne Hackbornc8230512013-07-13 21:32:12 -0700548 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700549}
550
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700551enum {
552 MEMINFO_TOTAL,
553 MEMINFO_FREE,
554 MEMINFO_BUFFERS,
555 MEMINFO_CACHED,
556 MEMINFO_SHMEM,
557 MEMINFO_SLAB,
558 MEMINFO_SWAP_TOTAL,
559 MEMINFO_SWAP_FREE,
560 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700561 MEMINFO_MAPPED,
562 MEMINFO_VMALLOC_USED,
563 MEMINFO_PAGE_TABLES,
564 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700565 MEMINFO_COUNT
566};
567
Dianne Hackborn8e692572013-09-10 19:06:15 -0700568static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
569{
570 char buffer[1024];
571 int numFound = 0;
572
573 if (out == NULL) {
574 jniThrowNullPointerException(env, "out == null");
575 return;
576 }
577
578 int fd = open("/proc/meminfo", O_RDONLY);
579
580 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700581 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700582 return;
583 }
584
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700585 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700586 close(fd);
587
588 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700589 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700590 return;
591 }
592 buffer[len] = 0;
593
594 static const char* const tags[] = {
595 "MemTotal:",
596 "MemFree:",
597 "Buffers:",
598 "Cached:",
599 "Shmem:",
600 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700601 "SwapTotal:",
602 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700603 "ZRam:",
604 "Mapped:",
605 "VmallocUsed:",
606 "PageTables:",
607 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700608 NULL
609 };
610 static const int tagsLen[] = {
611 9,
612 8,
613 8,
614 7,
615 6,
616 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700617 10,
618 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700619 5,
620 7,
621 12,
622 11,
623 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700624 0
625 };
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700626 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700627
628 char* p = buffer;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700629 while (*p && numFound < 13) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700630 int i = 0;
631 while (tags[i]) {
632 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
633 p += tagsLen[i];
634 while (*p == ' ') p++;
635 char* num = p;
636 while (*p >= '0' && *p <= '9') p++;
637 if (*p != 0) {
638 *p = 0;
639 p++;
640 }
641 mem[i] = atoll(num);
642 numFound++;
643 break;
644 }
645 i++;
646 }
647 while (*p && *p != '\n') {
648 p++;
649 }
650 if (*p) p++;
651 }
652
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700653 fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
654 if (fd >= 0) {
655 len = read(fd, buffer, sizeof(buffer)-1);
656 close(fd);
657 if (len > 0) {
658 buffer[len] = 0;
Dianne Hackborncb428552013-09-26 11:07:17 -0700659 mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700660 }
661 }
662
Dianne Hackborn8e692572013-09-10 19:06:15 -0700663 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700664 if (maxNum > MEMINFO_COUNT) {
665 maxNum = MEMINFO_COUNT;
666 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700667 jlong* outArray = env->GetLongArrayElements(out, 0);
668 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700669 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700670 outArray[i] = mem[i];
671 }
672 }
673 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674}
675
676static jint read_binder_stat(const char* stat)
677{
678 FILE* fp = fopen(BINDER_STATS, "r");
679 if (fp == NULL) {
680 return -1;
681 }
682
683 char line[1024];
684
685 char compare[128];
686 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 // loop until we have the block that represents this process
689 do {
690 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700691 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 return -1;
693 }
694 } while (strncmp(compare, line, len));
695
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800696 // 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 -0800697 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 do {
700 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700701 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 return -1;
703 }
704 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 // we have the line, now increment the line ptr to the value
707 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700708 jint result = atoi(ptr);
709 fclose(fp);
710 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711}
712
713static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
714{
715 return read_binder_stat("bcTRANSACTION");
716}
717
718static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
719{
720 return read_binder_stat("brTRANSACTION");
721}
722
723// these are implemented in android_util_Binder.cpp
724jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
725jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
726jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
727
Andy McFadden06a6b552010-07-13 16:28:09 -0700728
Andy McFadden06a6b552010-07-13 16:28:09 -0700729/* pulled out of bionic */
730extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
731 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
732extern "C" void free_malloc_leak_info(uint8_t* info);
733#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
734#define BACKTRACE_SIZE 32
735
736/*
737 * This is a qsort() callback.
738 *
739 * See dumpNativeHeap() for comments about the data format and sort order.
740 */
741static int compareHeapRecords(const void* vrec1, const void* vrec2)
742{
743 const size_t* rec1 = (const size_t*) vrec1;
744 const size_t* rec2 = (const size_t*) vrec2;
745 size_t size1 = *rec1;
746 size_t size2 = *rec2;
747
748 if (size1 < size2) {
749 return 1;
750 } else if (size1 > size2) {
751 return -1;
752 }
753
754 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
755 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
756 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
757 intptr_t addr1 = bt1[idx];
758 intptr_t addr2 = bt2[idx];
759 if (addr1 == addr2) {
760 if (addr1 == 0)
761 break;
762 continue;
763 }
764 if (addr1 < addr2) {
765 return -1;
766 } else if (addr1 > addr2) {
767 return 1;
768 }
769 }
770
771 return 0;
772}
773
774/*
775 * The get_malloc_leak_info() call returns an array of structs that
776 * look like this:
777 *
778 * size_t size
779 * size_t allocations
780 * intptr_t backtrace[32]
781 *
782 * "size" is the size of the allocation, "backtrace" is a fixed-size
783 * array of function pointers, and "allocations" is the number of
784 * allocations with the exact same size and backtrace.
785 *
786 * The entries are sorted by descending total size (i.e. size*allocations)
787 * then allocation count. For best results with "diff" we'd like to sort
788 * primarily by individual size then stack trace. Since the entries are
789 * fixed-size, and we're allowed (by the current implementation) to mangle
790 * them, we can do this in place.
791 */
792static void dumpNativeHeap(FILE* fp)
793{
794 uint8_t* info = NULL;
795 size_t overallSize, infoSize, totalMemory, backtraceSize;
796
797 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
798 &backtraceSize);
799 if (info == NULL) {
800 fprintf(fp, "Native heap dump not available. To enable, run these"
801 " commands (requires root):\n");
802 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
803 fprintf(fp, "$ adb shell stop\n");
804 fprintf(fp, "$ adb shell start\n");
805 return;
806 }
807 assert(infoSize != 0);
808 assert(overallSize % infoSize == 0);
809
810 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
811
812 size_t recordCount = overallSize / infoSize;
813 fprintf(fp, "Total memory: %zu\n", totalMemory);
814 fprintf(fp, "Allocation records: %zd\n", recordCount);
815 if (backtraceSize != BACKTRACE_SIZE) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000816 fprintf(fp, "WARNING: mismatched backtrace sizes (%zu vs. %d)\n",
Andy McFadden06a6b552010-07-13 16:28:09 -0700817 backtraceSize, BACKTRACE_SIZE);
818 }
819 fprintf(fp, "\n");
820
821 /* re-sort the entries */
822 qsort(info, recordCount, infoSize, compareHeapRecords);
823
824 /* dump the entries to the file */
825 const uint8_t* ptr = info;
826 for (size_t idx = 0; idx < recordCount; idx++) {
827 size_t size = *(size_t*) ptr;
828 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
829 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
830
831 fprintf(fp, "z %d sz %8zu num %4zu bt",
832 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
833 size & ~SIZE_FLAG_ZYGOTE_CHILD,
834 allocations);
835 for (size_t bt = 0; bt < backtraceSize; bt++) {
836 if (backtrace[bt] == 0) {
837 break;
838 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000839#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -0700840 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000841#else
Mark Salyzyn85394032014-04-16 10:28:37 -0700842 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000843#endif
Andy McFadden06a6b552010-07-13 16:28:09 -0700844 }
845 }
846 fprintf(fp, "\n");
847
848 ptr += infoSize;
849 }
850
Andy McFadden06a6b552010-07-13 16:28:09 -0700851 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700852
853 fprintf(fp, "MAPS\n");
854 const char* maps = "/proc/self/maps";
855 FILE* in = fopen(maps, "r");
856 if (in == NULL) {
857 fprintf(fp, "Could not open %s\n", maps);
858 return;
859 }
860 char buf[BUFSIZ];
861 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
862 fwrite(buf, sizeof(char), n, fp);
863 }
864 fclose(in);
865
866 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700867}
Andy McFadden06a6b552010-07-13 16:28:09 -0700868
869/*
870 * Dump the native heap, writing human-readable output to the specified
871 * file descriptor.
872 */
873static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
874 jobject fileDescriptor)
875{
876 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800877 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700878 return;
879 }
880 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
881 if (origFd < 0) {
882 jniThrowRuntimeException(env, "Invalid file descriptor");
883 return;
884 }
885
886 /* dup() the descriptor so we don't close the original with fclose() */
887 int fd = dup(origFd);
888 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000889 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700890 jniThrowRuntimeException(env, "dup() failed");
891 return;
892 }
893
894 FILE* fp = fdopen(fd, "w");
895 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000896 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700897 close(fd);
898 jniThrowRuntimeException(env, "fdopen() failed");
899 return;
900 }
901
Steve Block5baa3a62011-12-20 16:23:08 +0000902 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700903 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000904 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700905
906 fclose(fp);
907}
908
909
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700910static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
911 jint pid, jstring fileName)
912{
913 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800914 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700915 return;
916 }
917 const jchar* str = env->GetStringCritical(fileName, 0);
918 String8 fileName8;
919 if (str) {
920 fileName8 = String8(str, env->GetStringLength(fileName));
921 env->ReleaseStringCritical(fileName, str);
922 }
923
924 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
925 if (fd < 0) {
926 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
927 return;
928 }
929
930 if (lseek(fd, 0, SEEK_END) < 0) {
931 fprintf(stderr, "lseek: %s\n", strerror(errno));
932 } else {
933 dump_backtrace_to_file(pid, fd);
934 }
935
936 close(fd);
937}
938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939/*
940 * JNI registration.
941 */
942
943static JNINativeMethod gMethods[] = {
944 { "getNativeHeapSize", "()J",
945 (void*) android_os_Debug_getNativeHeapSize },
946 { "getNativeHeapAllocatedSize", "()J",
947 (void*) android_os_Debug_getNativeHeapAllocatedSize },
948 { "getNativeHeapFreeSize", "()J",
949 (void*) android_os_Debug_getNativeHeapFreeSize },
950 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
951 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700952 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
953 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700954 { "getPss", "()J",
955 (void*) android_os_Debug_getPss },
Dianne Hackbornc8230512013-07-13 21:32:12 -0700956 { "getPss", "(I[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -0700957 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -0700958 { "getMemInfo", "([J)V",
959 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -0700960 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
961 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 { "getBinderSentTransactions", "()I",
963 (void*) android_os_Debug_getBinderSentTransactions },
964 { "getBinderReceivedTransactions", "()I",
965 (void*) android_os_getBinderReceivedTransactions },
966 { "getBinderLocalObjectCount", "()I",
967 (void*)android_os_Debug_getLocalObjectCount },
968 { "getBinderProxyObjectCount", "()I",
969 (void*)android_os_Debug_getProxyObjectCount },
970 { "getBinderDeathObjectCount", "()I",
971 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700972 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
973 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974};
975
976int register_android_os_Debug(JNIEnv *env)
977{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700978 int err = memtrack_init();
979 if (err != 0) {
980 memtrackLoaded = false;
981 ALOGE("failed to load memtrack module: %d", err);
982 } else {
983 memtrackLoaded = true;
984 }
985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800988 // Sanity check the number of other statistics expected in Java matches here.
989 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
990 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700991 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
992 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800993 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700994 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800995 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700996 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
997 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800998 return JNI_ERR;
999 }
1000
1001 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
1002
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001003 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1004 stat_fields[i].pss_field =
1005 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001006 stat_fields[i].pssSwappable_field =
1007 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001008 stat_fields[i].privateDirty_field =
1009 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1010 stat_fields[i].sharedDirty_field =
1011 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001012 stat_fields[i].privateClean_field =
1013 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001014 stat_fields[i].sharedClean_field =
1015 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001016 stat_fields[i].swappedOut_field =
1017 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001018 }
1019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1021}
1022
Andy McFadden06a6b552010-07-13 16:28:09 -07001023}; // namespace android