blob: e9d16c162021851be25f1e3681a953c384dc574c [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 Hackborn64770d12013-05-23 17:51:19 -070049 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080050 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070051 HEAP_CURSOR,
52 HEAP_ASHMEM,
53 HEAP_UNKNOWN_DEV,
54 HEAP_SO,
55 HEAP_JAR,
56 HEAP_APK,
57 HEAP_TTF,
58 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070059 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070060 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070061 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070062 HEAP_GRAPHICS,
63 HEAP_GL,
64 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070065
Anwar Ghuloum3c615062013-05-13 14:18:02 -070066 HEAP_DALVIK_NORMAL,
67 HEAP_DALVIK_LARGE,
68 HEAP_DALVIK_LINEARALLOC,
69 HEAP_DALVIK_ACCOUNTING,
70 HEAP_DALVIK_CODE_CACHE,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070071
72 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070073 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070074 _NUM_CORE_HEAP = HEAP_NATIVE+1
75};
76
77struct stat_fields {
78 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070079 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070080 jfieldID privateDirty_field;
81 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070082 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070083 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070084 jfieldID swappedOut_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070085};
86
87struct stat_field_names {
88 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070089 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070090 const char* privateDirty_name;
91 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070092 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070093 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070094 const char* swappedOut_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070095};
96
97static stat_fields stat_fields[_NUM_CORE_HEAP];
98
99static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700100 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
101 "otherPrivateClean", "otherSharedClean", "otherSwappedOut" },
102 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
103 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut" },
104 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
105 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700106};
107
108jfieldID otherStats_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700110static bool memtrackLoaded;
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700113 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700114 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700115 int privateDirty;
116 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700117 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700118 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700119 int swappedOut;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120};
121
122#define BINDER_STATS "/proc/binder/stats"
123
124static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
125{
126#ifdef HAVE_MALLOC_H
127 struct mallinfo info = mallinfo();
128 return (jlong) info.usmblks;
129#else
130 return -1;
131#endif
132}
133
134static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
135{
136#ifdef HAVE_MALLOC_H
137 struct mallinfo info = mallinfo();
138 return (jlong) info.uordblks;
139#else
140 return -1;
141#endif
142}
143
144static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
145{
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800146#ifdef HAVE_MALLOC_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 struct mallinfo info = mallinfo();
148 return (jlong) info.fordblks;
149#else
150 return -1;
151#endif
152}
153
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700154// Container used to retrieve graphics memory pss
155struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700156{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700157 int graphics;
158 int gl;
159 int other;
160};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700161
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700162/*
163 * Uses libmemtrack to retrieve graphics memory that the process is using.
164 * Any graphics memory reported in /proc/pid/smaps is not included here.
165 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700166static int read_memtrack_memory(struct memtrack_proc* p, int pid,
167 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700168{
169 int err = memtrack_proc_get(p, pid);
170 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700171 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700172 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700173 }
174
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700175 ssize_t pss = memtrack_proc_graphics_pss(p);
176 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700177 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700178 return pss;
179 }
180 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700181
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700182 pss = memtrack_proc_gl_pss(p);
183 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700184 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700185 return pss;
186 }
187 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700188
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700189 pss = memtrack_proc_other_pss(p);
190 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700191 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700192 return pss;
193 }
194 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700195
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700196 return 0;
197}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700198
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700199/*
200 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
201 */
202static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
203{
204 if (!memtrackLoaded) {
205 return -1;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700206 }
207
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700208 struct memtrack_proc* p = memtrack_proc_new();
209 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700210 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700211 return -1;
212 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700213
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700214 int err = read_memtrack_memory(p, pid, graphics_mem);
215 memtrack_proc_destroy(p);
216 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700217}
218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219static void read_mapinfo(FILE *fp, stats_t* stats)
220{
221 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700222 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 bool skip, done = false;
224
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800225 unsigned pss = 0, swappable_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700226 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 unsigned shared_clean = 0, shared_dirty = 0;
228 unsigned private_clean = 0, private_dirty = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700229 unsigned swapped_out = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700230 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 unsigned temp;
232
Colin Cross0c6bc732014-06-17 15:18:07 -0700233 uint64_t start;
234 uint64_t end = 0;
235 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700236 char* name;
237 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700239 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700240 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700241 int prevHeap = HEAP_UNKNOWN;
242
243 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
245 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700246 prevHeap = whichHeap;
247 prevEnd = end;
248 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700249 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700251 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 len = strlen(line);
254 if (len < 1) return;
255 line[--len] = 0;
256
Colin Cross0c6bc732014-06-17 15:18:07 -0700257 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700258 skip = true;
259 } else {
260 while (isspace(line[name_pos])) {
261 name_pos += 1;
262 }
263 name = line + name_pos;
264 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Dianne Hackborn64770d12013-05-23 17:51:19 -0700266 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700267 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700268 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
269 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
270 whichHeap = HEAP_DALVIK_OTHER;
271 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
272 subHeap = HEAP_DALVIK_LINEARALLOC;
Mathieu Chartiereee97642014-09-23 17:24:30 -0700273 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
274 (strstr(name, "/dev/ashmem/dalvik-main space") == name) ||
275 (strstr(name, "/dev/ashmem/dalvik-non moving space") == name)) {
276 // This is the regular Dalvik heap.
277 whichHeap = HEAP_DALVIK;
278 subHeap = HEAP_DALVIK_NORMAL;
279 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name) {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700280 whichHeap = HEAP_DALVIK;
281 subHeap = HEAP_DALVIK_LARGE;
282 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
283 subHeap = HEAP_DALVIK_CODE_CACHE;
284 } else {
Mathieu Chartiereee97642014-09-23 17:24:30 -0700285 subHeap = HEAP_DALVIK_ACCOUNTING; // Default to accounting.
Dianne Hackborn64770d12013-05-23 17:51:19 -0700286 }
287 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
288 whichHeap = HEAP_CURSOR;
289 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
290 whichHeap = HEAP_NATIVE;
291 } else {
292 whichHeap = HEAP_ASHMEM;
293 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700294 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
295 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700296 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800297 whichHeap = HEAP_STACK;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700298 } else if (strncmp(name, "/dev/", 5) == 0) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700299 whichHeap = HEAP_UNKNOWN_DEV;
300 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
301 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700302 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700303 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
304 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700305 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700306 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
307 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700308 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700309 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
310 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700311 is_swappable = true;
Ian Rogers9f8589c2013-02-22 20:35:06 -0800312 } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) ||
313 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700314 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700315 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700316 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
317 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700318 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700319 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
320 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700321 is_swappable = true;
Colin Crosscb4728f2013-08-09 13:23:46 -0700322 } else if (strncmp(name, "[anon:", 6) == 0) {
323 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700324 } else if (nameLen > 0) {
325 whichHeap = HEAP_UNKNOWN_MAP;
326 } else if (start == prevEnd && prevHeap == HEAP_SO) {
327 // bss section of a shared library.
328 whichHeap = HEAP_SO;
329 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
331
Steve Block6215d3f2012-01-04 20:05:49 +0000332 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800334
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700335 shared_clean = 0;
336 shared_dirty = 0;
337 private_clean = 0;
338 private_dirty = 0;
339 swapped_out = 0;
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 while (true) {
342 if (fgets(line, 1024, fp) == 0) {
343 done = true;
344 break;
345 }
346
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700347 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800348 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700349 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800350 /* resident = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700351 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700353 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700355 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700357 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700359 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700361 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800362 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700363 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
364 swapped_out = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700365 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700367 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 break;
369 }
370 }
371
372 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700373 if (is_swappable && (pss > 0)) {
374 sharing_proportion = 0.0;
375 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700376 sharing_proportion = (pss - private_clean
377 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700378 }
379 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
380 } else
381 swappable_pss = 0;
382
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700383 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700384 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700385 stats[whichHeap].privateDirty += private_dirty;
386 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700387 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700388 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700389 stats[whichHeap].swappedOut += swapped_out;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700390 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700391 stats[subHeap].pss += pss;
392 stats[subHeap].swappablePss += swappable_pss;
393 stats[subHeap].privateDirty += private_dirty;
394 stats[subHeap].sharedDirty += shared_dirty;
395 stats[subHeap].privateClean += private_clean;
396 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700397 stats[subHeap].swappedOut += swapped_out;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 }
400 }
401}
402
403static void load_maps(int pid, stats_t* stats)
404{
405 char tmp[128];
406 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 sprintf(tmp, "/proc/%d/smaps", pid);
409 fp = fopen(tmp, "r");
410 if (fp == 0) return;
411
412 read_mapinfo(fp, stats);
413 fclose(fp);
414}
415
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700416static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
417 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700419 stats_t stats[_NUM_HEAP];
420 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800421
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700422 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700424 struct graphics_memory_pss graphics_mem;
425 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
426 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
427 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
428 stats[HEAP_GL].pss = graphics_mem.gl;
429 stats[HEAP_GL].privateDirty = graphics_mem.gl;
430 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
431 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700432 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700433
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700434 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700435 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700436 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700437 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
438 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700439 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700440 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700441 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700442 }
443
444 for (int i=0; i<_NUM_CORE_HEAP; i++) {
445 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700446 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700447 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
448 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700449 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700450 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700451 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700452 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800453
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700454
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700455 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800456
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700457 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
458 if (otherArray == NULL) {
459 return;
460 }
461
462 int j=0;
463 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
464 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700465 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700466 otherArray[j++] = stats[i].privateDirty;
467 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700468 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700469 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700470 otherArray[j++] = stats[i].swappedOut;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700471 }
472
473 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474}
475
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700476static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
477{
478 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
479}
480
Dianne Hackbornc8230512013-07-13 21:32:12 -0700481static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, jlongArray outUss)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700482{
483 char line[1024];
484 jlong pss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700485 jlong uss = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700486
487 char tmp[128];
488 FILE *fp;
489
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700490 struct graphics_memory_pss graphics_mem;
491 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
492 pss = uss = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700493 }
494
Dianne Hackbornb437e092011-08-05 17:50:29 -0700495 sprintf(tmp, "/proc/%d/smaps", pid);
496 fp = fopen(tmp, "r");
Dianne Hackbornb437e092011-08-05 17:50:29 -0700497
Dianne Hackborn37c99432013-09-05 19:34:57 -0700498 if (fp != 0) {
499 while (true) {
500 if (fgets(line, 1024, fp) == NULL) {
501 break;
502 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700503
Dianne Hackborn37c99432013-09-05 19:34:57 -0700504 if (line[0] == 'P') {
505 if (strncmp(line, "Pss:", 4) == 0) {
506 char* c = line + 4;
507 while (*c != 0 && (*c < '0' || *c > '9')) {
508 c++;
509 }
510 pss += atoi(c);
511 } else if (strncmp(line, "Private_Clean:", 14)
512 || strncmp(line, "Private_Dirty:", 14)) {
513 char* c = line + 14;
514 while (*c != 0 && (*c < '0' || *c > '9')) {
515 c++;
516 }
517 uss += atoi(c);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700518 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700519 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700520 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700521
Dianne Hackborn37c99432013-09-05 19:34:57 -0700522 fclose(fp);
523 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700524
Dianne Hackbornc8230512013-07-13 21:32:12 -0700525 if (outUss != NULL) {
526 if (env->GetArrayLength(outUss) >= 1) {
527 jlong* outUssArray = env->GetLongArrayElements(outUss, 0);
528 if (outUssArray != NULL) {
529 outUssArray[0] = uss;
530 }
531 env->ReleaseLongArrayElements(outUss, outUssArray, 0);
532 }
533 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700534
535 return pss;
536}
537
538static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
539{
Dianne Hackbornc8230512013-07-13 21:32:12 -0700540 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700541}
542
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700543enum {
544 MEMINFO_TOTAL,
545 MEMINFO_FREE,
546 MEMINFO_BUFFERS,
547 MEMINFO_CACHED,
548 MEMINFO_SHMEM,
549 MEMINFO_SLAB,
550 MEMINFO_SWAP_TOTAL,
551 MEMINFO_SWAP_FREE,
552 MEMINFO_ZRAM_TOTAL,
553 MEMINFO_COUNT
554};
555
Dianne Hackborn8e692572013-09-10 19:06:15 -0700556static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
557{
558 char buffer[1024];
559 int numFound = 0;
560
561 if (out == NULL) {
562 jniThrowNullPointerException(env, "out == null");
563 return;
564 }
565
566 int fd = open("/proc/meminfo", O_RDONLY);
567
568 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700569 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700570 return;
571 }
572
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700573 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700574 close(fd);
575
576 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700577 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700578 return;
579 }
580 buffer[len] = 0;
581
582 static const char* const tags[] = {
583 "MemTotal:",
584 "MemFree:",
585 "Buffers:",
586 "Cached:",
587 "Shmem:",
588 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700589 "SwapTotal:",
590 "SwapFree:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700591 NULL
592 };
593 static const int tagsLen[] = {
594 9,
595 8,
596 8,
597 7,
598 6,
599 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700600 10,
601 9,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700602 0
603 };
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700604 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700605
606 char* p = buffer;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700607 while (*p && numFound < 8) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700608 int i = 0;
609 while (tags[i]) {
610 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
611 p += tagsLen[i];
612 while (*p == ' ') p++;
613 char* num = p;
614 while (*p >= '0' && *p <= '9') p++;
615 if (*p != 0) {
616 *p = 0;
617 p++;
618 }
619 mem[i] = atoll(num);
620 numFound++;
621 break;
622 }
623 i++;
624 }
625 while (*p && *p != '\n') {
626 p++;
627 }
628 if (*p) p++;
629 }
630
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700631 fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
632 if (fd >= 0) {
633 len = read(fd, buffer, sizeof(buffer)-1);
634 close(fd);
635 if (len > 0) {
636 buffer[len] = 0;
Dianne Hackborncb428552013-09-26 11:07:17 -0700637 mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700638 }
639 }
640
Dianne Hackborn8e692572013-09-10 19:06:15 -0700641 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700642 if (maxNum > MEMINFO_COUNT) {
643 maxNum = MEMINFO_COUNT;
644 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700645 jlong* outArray = env->GetLongArrayElements(out, 0);
646 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700647 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700648 outArray[i] = mem[i];
649 }
650 }
651 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652}
653
654static jint read_binder_stat(const char* stat)
655{
656 FILE* fp = fopen(BINDER_STATS, "r");
657 if (fp == NULL) {
658 return -1;
659 }
660
661 char line[1024];
662
663 char compare[128];
664 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 // loop until we have the block that represents this process
667 do {
668 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700669 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 return -1;
671 }
672 } while (strncmp(compare, line, len));
673
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800674 // 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 -0800675 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 do {
678 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700679 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 return -1;
681 }
682 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 // we have the line, now increment the line ptr to the value
685 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700686 jint result = atoi(ptr);
687 fclose(fp);
688 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689}
690
691static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
692{
693 return read_binder_stat("bcTRANSACTION");
694}
695
696static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
697{
698 return read_binder_stat("brTRANSACTION");
699}
700
701// these are implemented in android_util_Binder.cpp
702jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
703jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
704jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
705
Andy McFadden06a6b552010-07-13 16:28:09 -0700706
Andy McFadden06a6b552010-07-13 16:28:09 -0700707/* pulled out of bionic */
708extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
709 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
710extern "C" void free_malloc_leak_info(uint8_t* info);
711#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
712#define BACKTRACE_SIZE 32
713
714/*
715 * This is a qsort() callback.
716 *
717 * See dumpNativeHeap() for comments about the data format and sort order.
718 */
719static int compareHeapRecords(const void* vrec1, const void* vrec2)
720{
721 const size_t* rec1 = (const size_t*) vrec1;
722 const size_t* rec2 = (const size_t*) vrec2;
723 size_t size1 = *rec1;
724 size_t size2 = *rec2;
725
726 if (size1 < size2) {
727 return 1;
728 } else if (size1 > size2) {
729 return -1;
730 }
731
732 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
733 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
734 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
735 intptr_t addr1 = bt1[idx];
736 intptr_t addr2 = bt2[idx];
737 if (addr1 == addr2) {
738 if (addr1 == 0)
739 break;
740 continue;
741 }
742 if (addr1 < addr2) {
743 return -1;
744 } else if (addr1 > addr2) {
745 return 1;
746 }
747 }
748
749 return 0;
750}
751
752/*
753 * The get_malloc_leak_info() call returns an array of structs that
754 * look like this:
755 *
756 * size_t size
757 * size_t allocations
758 * intptr_t backtrace[32]
759 *
760 * "size" is the size of the allocation, "backtrace" is a fixed-size
761 * array of function pointers, and "allocations" is the number of
762 * allocations with the exact same size and backtrace.
763 *
764 * The entries are sorted by descending total size (i.e. size*allocations)
765 * then allocation count. For best results with "diff" we'd like to sort
766 * primarily by individual size then stack trace. Since the entries are
767 * fixed-size, and we're allowed (by the current implementation) to mangle
768 * them, we can do this in place.
769 */
770static void dumpNativeHeap(FILE* fp)
771{
772 uint8_t* info = NULL;
773 size_t overallSize, infoSize, totalMemory, backtraceSize;
774
775 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
776 &backtraceSize);
777 if (info == NULL) {
778 fprintf(fp, "Native heap dump not available. To enable, run these"
779 " commands (requires root):\n");
780 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
781 fprintf(fp, "$ adb shell stop\n");
782 fprintf(fp, "$ adb shell start\n");
783 return;
784 }
785 assert(infoSize != 0);
786 assert(overallSize % infoSize == 0);
787
788 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
789
790 size_t recordCount = overallSize / infoSize;
791 fprintf(fp, "Total memory: %zu\n", totalMemory);
792 fprintf(fp, "Allocation records: %zd\n", recordCount);
793 if (backtraceSize != BACKTRACE_SIZE) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000794 fprintf(fp, "WARNING: mismatched backtrace sizes (%zu vs. %d)\n",
Andy McFadden06a6b552010-07-13 16:28:09 -0700795 backtraceSize, BACKTRACE_SIZE);
796 }
797 fprintf(fp, "\n");
798
799 /* re-sort the entries */
800 qsort(info, recordCount, infoSize, compareHeapRecords);
801
802 /* dump the entries to the file */
803 const uint8_t* ptr = info;
804 for (size_t idx = 0; idx < recordCount; idx++) {
805 size_t size = *(size_t*) ptr;
806 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
807 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
808
809 fprintf(fp, "z %d sz %8zu num %4zu bt",
810 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
811 size & ~SIZE_FLAG_ZYGOTE_CHILD,
812 allocations);
813 for (size_t bt = 0; bt < backtraceSize; bt++) {
814 if (backtrace[bt] == 0) {
815 break;
816 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000817#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -0700818 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000819#else
Mark Salyzyn85394032014-04-16 10:28:37 -0700820 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000821#endif
Andy McFadden06a6b552010-07-13 16:28:09 -0700822 }
823 }
824 fprintf(fp, "\n");
825
826 ptr += infoSize;
827 }
828
Andy McFadden06a6b552010-07-13 16:28:09 -0700829 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700830
831 fprintf(fp, "MAPS\n");
832 const char* maps = "/proc/self/maps";
833 FILE* in = fopen(maps, "r");
834 if (in == NULL) {
835 fprintf(fp, "Could not open %s\n", maps);
836 return;
837 }
838 char buf[BUFSIZ];
839 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
840 fwrite(buf, sizeof(char), n, fp);
841 }
842 fclose(in);
843
844 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700845}
Andy McFadden06a6b552010-07-13 16:28:09 -0700846
847/*
848 * Dump the native heap, writing human-readable output to the specified
849 * file descriptor.
850 */
851static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
852 jobject fileDescriptor)
853{
854 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800855 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700856 return;
857 }
858 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
859 if (origFd < 0) {
860 jniThrowRuntimeException(env, "Invalid file descriptor");
861 return;
862 }
863
864 /* dup() the descriptor so we don't close the original with fclose() */
865 int fd = dup(origFd);
866 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000867 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700868 jniThrowRuntimeException(env, "dup() failed");
869 return;
870 }
871
872 FILE* fp = fdopen(fd, "w");
873 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000874 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700875 close(fd);
876 jniThrowRuntimeException(env, "fdopen() failed");
877 return;
878 }
879
Steve Block5baa3a62011-12-20 16:23:08 +0000880 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700881 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000882 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700883
884 fclose(fp);
885}
886
887
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700888static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
889 jint pid, jstring fileName)
890{
891 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800892 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700893 return;
894 }
895 const jchar* str = env->GetStringCritical(fileName, 0);
896 String8 fileName8;
897 if (str) {
898 fileName8 = String8(str, env->GetStringLength(fileName));
899 env->ReleaseStringCritical(fileName, str);
900 }
901
902 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
903 if (fd < 0) {
904 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
905 return;
906 }
907
908 if (lseek(fd, 0, SEEK_END) < 0) {
909 fprintf(stderr, "lseek: %s\n", strerror(errno));
910 } else {
911 dump_backtrace_to_file(pid, fd);
912 }
913
914 close(fd);
915}
916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917/*
918 * JNI registration.
919 */
920
921static JNINativeMethod gMethods[] = {
922 { "getNativeHeapSize", "()J",
923 (void*) android_os_Debug_getNativeHeapSize },
924 { "getNativeHeapAllocatedSize", "()J",
925 (void*) android_os_Debug_getNativeHeapAllocatedSize },
926 { "getNativeHeapFreeSize", "()J",
927 (void*) android_os_Debug_getNativeHeapFreeSize },
928 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
929 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700930 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
931 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700932 { "getPss", "()J",
933 (void*) android_os_Debug_getPss },
Dianne Hackbornc8230512013-07-13 21:32:12 -0700934 { "getPss", "(I[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -0700935 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -0700936 { "getMemInfo", "([J)V",
937 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -0700938 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
939 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 { "getBinderSentTransactions", "()I",
941 (void*) android_os_Debug_getBinderSentTransactions },
942 { "getBinderReceivedTransactions", "()I",
943 (void*) android_os_getBinderReceivedTransactions },
944 { "getBinderLocalObjectCount", "()I",
945 (void*)android_os_Debug_getLocalObjectCount },
946 { "getBinderProxyObjectCount", "()I",
947 (void*)android_os_Debug_getProxyObjectCount },
948 { "getBinderDeathObjectCount", "()I",
949 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700950 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
951 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952};
953
954int register_android_os_Debug(JNIEnv *env)
955{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700956 int err = memtrack_init();
957 if (err != 0) {
958 memtrackLoaded = false;
959 ALOGE("failed to load memtrack module: %d", err);
960 } else {
961 memtrackLoaded = true;
962 }
963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800966 // Sanity check the number of other statistics expected in Java matches here.
967 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
968 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700969 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
970 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800971 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700972 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800973 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700974 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
975 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800976 return JNI_ERR;
977 }
978
979 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
980
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700981 for (int i=0; i<_NUM_CORE_HEAP; i++) {
982 stat_fields[i].pss_field =
983 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700984 stat_fields[i].pssSwappable_field =
985 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 stat_fields[i].privateDirty_field =
987 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
988 stat_fields[i].sharedDirty_field =
989 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700990 stat_fields[i].privateClean_field =
991 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700992 stat_fields[i].sharedClean_field =
993 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700994 stat_fields[i].swappedOut_field =
995 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700996 }
997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
999}
1000
Andy McFadden06a6b552010-07-13 16:28:09 -07001001}; // namespace android