blob: 8b7f3dd5fd3cb18ab57fbb3548a3ca2ec5086012 [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"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
Dianne Hackbornf72467a2012-06-08 17:23:59 -070024#include <fcntl.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29#include <time.h>
30#include <sys/time.h>
Andy McFadden06a6b552010-07-13 16:28:09 -070031#include <errno.h>
32#include <assert.h>
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070033#include <ctype.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#ifdef HAVE_MALLOC_H
36#include <malloc.h>
37#endif
38
39namespace android
40{
41
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070042enum {
43 HEAP_UNKNOWN,
44 HEAP_DALVIK,
45 HEAP_NATIVE,
Dianne Hackborn64770d12013-05-23 17:51:19 -070046 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080047 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070048 HEAP_CURSOR,
49 HEAP_ASHMEM,
50 HEAP_UNKNOWN_DEV,
51 HEAP_SO,
52 HEAP_JAR,
53 HEAP_APK,
54 HEAP_TTF,
55 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070056 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070057 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070058 HEAP_UNKNOWN_MAP,
Dianne Hackborn64770d12013-05-23 17:51:19 -070059
Anwar Ghuloum3c615062013-05-13 14:18:02 -070060 HEAP_DALVIK_NORMAL,
61 HEAP_DALVIK_LARGE,
62 HEAP_DALVIK_LINEARALLOC,
63 HEAP_DALVIK_ACCOUNTING,
64 HEAP_DALVIK_CODE_CACHE,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070065
66 _NUM_HEAP,
Anwar Ghuloum3c615062013-05-13 14:18:02 -070067 _NUM_EXCLUSIVE_HEAP = HEAP_UNKNOWN_MAP+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070068 _NUM_CORE_HEAP = HEAP_NATIVE+1
69};
70
71struct stat_fields {
72 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070073 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070074 jfieldID privateDirty_field;
75 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070076 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070077 jfieldID sharedClean_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070078};
79
80struct stat_field_names {
81 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070082 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070083 const char* privateDirty_name;
84 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070085 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070086 const char* sharedClean_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070087};
88
89static stat_fields stat_fields[_NUM_CORE_HEAP];
90
91static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Anwar Ghuloum3c615062013-05-13 14:18:02 -070092 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty", "otherPrivateClean", "otherSharedClean" },
93 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty", "dalvikPrivateClean", "dalvikSharedClean" },
94 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty", "nativePrivateClean", "nativeSharedClean" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070095};
96
97jfieldID otherStats_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
99struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700100 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700101 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700102 int privateDirty;
103 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700104 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700105 int sharedClean;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106};
107
108#define BINDER_STATS "/proc/binder/stats"
109
110static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
111{
112#ifdef HAVE_MALLOC_H
113 struct mallinfo info = mallinfo();
114 return (jlong) info.usmblks;
115#else
116 return -1;
117#endif
118}
119
120static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
121{
122#ifdef HAVE_MALLOC_H
123 struct mallinfo info = mallinfo();
124 return (jlong) info.uordblks;
125#else
126 return -1;
127#endif
128}
129
130static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
131{
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800132#ifdef HAVE_MALLOC_H
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 struct mallinfo info = mallinfo();
134 return (jlong) info.fordblks;
135#else
136 return -1;
137#endif
138}
139
140static void read_mapinfo(FILE *fp, stats_t* stats)
141{
142 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700143 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 bool skip, done = false;
145
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700146 unsigned size = 0, resident = 0, pss = 0, swappable_pss = 0;
147 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 unsigned shared_clean = 0, shared_dirty = 0;
149 unsigned private_clean = 0, private_dirty = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700150 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 unsigned referenced = 0;
152 unsigned temp;
153
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700154 unsigned long int start;
155 unsigned long int end = 0;
156 unsigned long int prevEnd = 0;
157 char* name;
158 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700160 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700161 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700162 int prevHeap = HEAP_UNKNOWN;
163
164 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700167 prevHeap = whichHeap;
168 prevEnd = end;
169 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700170 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700172 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
174 len = strlen(line);
175 if (len < 1) return;
176 line[--len] = 0;
177
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700178 if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
179 skip = true;
180 } else {
181 while (isspace(line[name_pos])) {
182 name_pos += 1;
183 }
184 name = line + name_pos;
185 nameLen = strlen(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn64770d12013-05-23 17:51:19 -0700187 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700188 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700189 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
190 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
191 whichHeap = HEAP_DALVIK_OTHER;
192 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
193 subHeap = HEAP_DALVIK_LINEARALLOC;
194 } else if ((strstr(name, "/dev/ashmem/dalvik-mark") == name) ||
195 (strstr(name, "/dev/ashmem/dalvik-allocspace alloc space live-bitmap") == name) ||
196 (strstr(name, "/dev/ashmem/dalvik-allocspace alloc space mark-bitmap") == name) ||
197 (strstr(name, "/dev/ashmem/dalvik-card table") == name) ||
198 (strstr(name, "/dev/ashmem/dalvik-allocation stack") == name) ||
199 (strstr(name, "/dev/ashmem/dalvik-live stack") == name) ||
200 (strstr(name, "/dev/ashmem/dalvik-imagespace") == name) ||
201 (strstr(name, "/dev/ashmem/dalvik-bitmap") == name) ||
202 (strstr(name, "/dev/ashmem/dalvik-card-table") == name) ||
203 (strstr(name, "/dev/ashmem/dalvik-mark-stack") == name) ||
204 (strstr(name, "/dev/ashmem/dalvik-aux-structure") == name)) {
205 subHeap = HEAP_DALVIK_ACCOUNTING;
206 } else if (strstr(name, "/dev/ashmem/dalvik-large") == name) {
207 whichHeap = HEAP_DALVIK;
208 subHeap = HEAP_DALVIK_LARGE;
209 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
210 subHeap = HEAP_DALVIK_CODE_CACHE;
211 } else {
212 // This is the regular Dalvik heap.
213 whichHeap = HEAP_DALVIK;
214 subHeap = HEAP_DALVIK_NORMAL;
215 }
216 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
217 whichHeap = HEAP_CURSOR;
218 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
219 whichHeap = HEAP_NATIVE;
220 } else {
221 whichHeap = HEAP_ASHMEM;
222 }
223 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800224 whichHeap = HEAP_STACK;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700225 } else if (strncmp(name, "/dev/", 5) == 0) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700226 whichHeap = HEAP_UNKNOWN_DEV;
227 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
228 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700229 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700230 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
231 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700232 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700233 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
234 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700235 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700236 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
237 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700238 is_swappable = true;
Ian Rogers9f8589c2013-02-22 20:35:06 -0800239 } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) ||
240 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700241 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700242 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700243 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
244 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700245 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700246 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
247 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700248 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700249 } else if (nameLen > 0) {
250 whichHeap = HEAP_UNKNOWN_MAP;
251 } else if (start == prevEnd && prevHeap == HEAP_SO) {
252 // bss section of a shared library.
253 whichHeap = HEAP_SO;
254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256
Steve Block6215d3f2012-01-04 20:05:49 +0000257 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 while (true) {
261 if (fgets(line, 1024, fp) == 0) {
262 done = true;
263 break;
264 }
265
266 if (sscanf(line, "Size: %d kB", &temp) == 1) {
267 size = temp;
268 } else if (sscanf(line, "Rss: %d kB", &temp) == 1) {
269 resident = temp;
270 } else if (sscanf(line, "Pss: %d kB", &temp) == 1) {
271 pss = temp;
272 } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
273 shared_clean = temp;
274 } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
275 shared_dirty = temp;
276 } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
277 private_clean = temp;
278 } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
279 private_dirty = temp;
280 } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) {
281 referenced = temp;
Grace Klobabd511162009-07-08 23:32:25 -0700282 } else if (strlen(line) > 30 && line[8] == '-' && line[17] == ' ') {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700284 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 break;
286 }
287 }
288
289 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700290 if (is_swappable && (pss > 0)) {
291 sharing_proportion = 0.0;
292 if ((shared_clean > 0) || (shared_dirty > 0)) {
293 sharing_proportion = (pss - private_clean - private_dirty)/(shared_clean+shared_dirty);
294 }
295 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
296 } else
297 swappable_pss = 0;
298
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700299 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700300 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700301 stats[whichHeap].privateDirty += private_dirty;
302 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700303 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700304 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700305 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700306 stats[subHeap].pss += pss;
307 stats[subHeap].swappablePss += swappable_pss;
308 stats[subHeap].privateDirty += private_dirty;
309 stats[subHeap].sharedDirty += shared_dirty;
310 stats[subHeap].privateClean += private_clean;
311 stats[subHeap].sharedClean += shared_clean;
312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
314 }
315}
316
317static void load_maps(int pid, stats_t* stats)
318{
319 char tmp[128];
320 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 sprintf(tmp, "/proc/%d/smaps", pid);
323 fp = fopen(tmp, "r");
324 if (fp == 0) return;
325
326 read_mapinfo(fp, stats);
327 fclose(fp);
328}
329
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700330static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
331 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332{
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700333 stats_t stats[_NUM_HEAP];
334 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800335
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700336
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700337 load_maps(pid, stats);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700339 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700340 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700341 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700342 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
343 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700344 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700345 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700346 }
347
348 for (int i=0; i<_NUM_CORE_HEAP; i++) {
349 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700350 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700351 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
352 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700353 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700354 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700355 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800356
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700357
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700358 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800359
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700360 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
361 if (otherArray == NULL) {
362 return;
363 }
364
365 int j=0;
366 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
367 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700368 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700369 otherArray[j++] = stats[i].privateDirty;
370 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700371 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700372 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700373 }
374
375 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376}
377
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700378static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
379{
380 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
381}
382
Dianne Hackbornb437e092011-08-05 17:50:29 -0700383static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid)
384{
385 char line[1024];
386 jlong pss = 0;
387 unsigned temp;
388
389 char tmp[128];
390 FILE *fp;
391
392 sprintf(tmp, "/proc/%d/smaps", pid);
393 fp = fopen(tmp, "r");
394 if (fp == 0) return 0;
395
396 while (true) {
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700397 if (fgets(line, 1024, fp) == NULL) {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700398 break;
399 }
400
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700401 if (strncmp(line, "Pss: ", 5) == 0) {
402 char* c = line + 5;
403 while (*c != 0 && (*c < '0' || *c > '9')) {
404 c++;
405 }
406 pss += atoi(c);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700407 }
408 }
409
410 fclose(fp);
411
412 return pss;
413}
414
415static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
416{
417 return android_os_Debug_getPssPid(env, clazz, getpid());
418}
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420static jint read_binder_stat(const char* stat)
421{
422 FILE* fp = fopen(BINDER_STATS, "r");
423 if (fp == NULL) {
424 return -1;
425 }
426
427 char line[1024];
428
429 char compare[128];
430 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 // loop until we have the block that represents this process
433 do {
434 if (fgets(line, 1024, fp) == 0) {
435 return -1;
436 }
437 } while (strncmp(compare, line, len));
438
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800439 // 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 -0800440 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 do {
443 if (fgets(line, 1024, fp) == 0) {
444 return -1;
445 }
446 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 // we have the line, now increment the line ptr to the value
449 char* ptr = line + len;
450 return atoi(ptr);
451}
452
453static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
454{
455 return read_binder_stat("bcTRANSACTION");
456}
457
458static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
459{
460 return read_binder_stat("brTRANSACTION");
461}
462
463// these are implemented in android_util_Binder.cpp
464jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
465jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
466jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
467
Andy McFadden06a6b552010-07-13 16:28:09 -0700468
Andy McFadden06a6b552010-07-13 16:28:09 -0700469/* pulled out of bionic */
470extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
471 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
472extern "C" void free_malloc_leak_info(uint8_t* info);
473#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
474#define BACKTRACE_SIZE 32
475
476/*
477 * This is a qsort() callback.
478 *
479 * See dumpNativeHeap() for comments about the data format and sort order.
480 */
481static int compareHeapRecords(const void* vrec1, const void* vrec2)
482{
483 const size_t* rec1 = (const size_t*) vrec1;
484 const size_t* rec2 = (const size_t*) vrec2;
485 size_t size1 = *rec1;
486 size_t size2 = *rec2;
487
488 if (size1 < size2) {
489 return 1;
490 } else if (size1 > size2) {
491 return -1;
492 }
493
494 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
495 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
496 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
497 intptr_t addr1 = bt1[idx];
498 intptr_t addr2 = bt2[idx];
499 if (addr1 == addr2) {
500 if (addr1 == 0)
501 break;
502 continue;
503 }
504 if (addr1 < addr2) {
505 return -1;
506 } else if (addr1 > addr2) {
507 return 1;
508 }
509 }
510
511 return 0;
512}
513
514/*
515 * The get_malloc_leak_info() call returns an array of structs that
516 * look like this:
517 *
518 * size_t size
519 * size_t allocations
520 * intptr_t backtrace[32]
521 *
522 * "size" is the size of the allocation, "backtrace" is a fixed-size
523 * array of function pointers, and "allocations" is the number of
524 * allocations with the exact same size and backtrace.
525 *
526 * The entries are sorted by descending total size (i.e. size*allocations)
527 * then allocation count. For best results with "diff" we'd like to sort
528 * primarily by individual size then stack trace. Since the entries are
529 * fixed-size, and we're allowed (by the current implementation) to mangle
530 * them, we can do this in place.
531 */
532static void dumpNativeHeap(FILE* fp)
533{
534 uint8_t* info = NULL;
535 size_t overallSize, infoSize, totalMemory, backtraceSize;
536
537 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
538 &backtraceSize);
539 if (info == NULL) {
540 fprintf(fp, "Native heap dump not available. To enable, run these"
541 " commands (requires root):\n");
542 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
543 fprintf(fp, "$ adb shell stop\n");
544 fprintf(fp, "$ adb shell start\n");
545 return;
546 }
547 assert(infoSize != 0);
548 assert(overallSize % infoSize == 0);
549
550 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
551
552 size_t recordCount = overallSize / infoSize;
553 fprintf(fp, "Total memory: %zu\n", totalMemory);
554 fprintf(fp, "Allocation records: %zd\n", recordCount);
555 if (backtraceSize != BACKTRACE_SIZE) {
556 fprintf(fp, "WARNING: mismatched backtrace sizes (%d vs. %d)\n",
557 backtraceSize, BACKTRACE_SIZE);
558 }
559 fprintf(fp, "\n");
560
561 /* re-sort the entries */
562 qsort(info, recordCount, infoSize, compareHeapRecords);
563
564 /* dump the entries to the file */
565 const uint8_t* ptr = info;
566 for (size_t idx = 0; idx < recordCount; idx++) {
567 size_t size = *(size_t*) ptr;
568 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
569 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
570
571 fprintf(fp, "z %d sz %8zu num %4zu bt",
572 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
573 size & ~SIZE_FLAG_ZYGOTE_CHILD,
574 allocations);
575 for (size_t bt = 0; bt < backtraceSize; bt++) {
576 if (backtrace[bt] == 0) {
577 break;
578 } else {
579 fprintf(fp, " %08x", backtrace[bt]);
580 }
581 }
582 fprintf(fp, "\n");
583
584 ptr += infoSize;
585 }
586
Andy McFadden06a6b552010-07-13 16:28:09 -0700587 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700588
589 fprintf(fp, "MAPS\n");
590 const char* maps = "/proc/self/maps";
591 FILE* in = fopen(maps, "r");
592 if (in == NULL) {
593 fprintf(fp, "Could not open %s\n", maps);
594 return;
595 }
596 char buf[BUFSIZ];
597 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
598 fwrite(buf, sizeof(char), n, fp);
599 }
600 fclose(in);
601
602 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700603}
Andy McFadden06a6b552010-07-13 16:28:09 -0700604
605/*
606 * Dump the native heap, writing human-readable output to the specified
607 * file descriptor.
608 */
609static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
610 jobject fileDescriptor)
611{
612 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800613 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700614 return;
615 }
616 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
617 if (origFd < 0) {
618 jniThrowRuntimeException(env, "Invalid file descriptor");
619 return;
620 }
621
622 /* dup() the descriptor so we don't close the original with fclose() */
623 int fd = dup(origFd);
624 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000625 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700626 jniThrowRuntimeException(env, "dup() failed");
627 return;
628 }
629
630 FILE* fp = fdopen(fd, "w");
631 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000632 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700633 close(fd);
634 jniThrowRuntimeException(env, "fdopen() failed");
635 return;
636 }
637
Steve Block5baa3a62011-12-20 16:23:08 +0000638 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700639 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +0000640 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700641
642 fclose(fp);
643}
644
645
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700646static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
647 jint pid, jstring fileName)
648{
649 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800650 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700651 return;
652 }
653 const jchar* str = env->GetStringCritical(fileName, 0);
654 String8 fileName8;
655 if (str) {
656 fileName8 = String8(str, env->GetStringLength(fileName));
657 env->ReleaseStringCritical(fileName, str);
658 }
659
660 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
661 if (fd < 0) {
662 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
663 return;
664 }
665
666 if (lseek(fd, 0, SEEK_END) < 0) {
667 fprintf(stderr, "lseek: %s\n", strerror(errno));
668 } else {
669 dump_backtrace_to_file(pid, fd);
670 }
671
672 close(fd);
673}
674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675/*
676 * JNI registration.
677 */
678
679static JNINativeMethod gMethods[] = {
680 { "getNativeHeapSize", "()J",
681 (void*) android_os_Debug_getNativeHeapSize },
682 { "getNativeHeapAllocatedSize", "()J",
683 (void*) android_os_Debug_getNativeHeapAllocatedSize },
684 { "getNativeHeapFreeSize", "()J",
685 (void*) android_os_Debug_getNativeHeapFreeSize },
686 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
687 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700688 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
689 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700690 { "getPss", "()J",
691 (void*) android_os_Debug_getPss },
692 { "getPss", "(I)J",
693 (void*) android_os_Debug_getPssPid },
Andy McFadden06a6b552010-07-13 16:28:09 -0700694 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
695 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 { "getBinderSentTransactions", "()I",
697 (void*) android_os_Debug_getBinderSentTransactions },
698 { "getBinderReceivedTransactions", "()I",
699 (void*) android_os_getBinderReceivedTransactions },
700 { "getBinderLocalObjectCount", "()I",
701 (void*)android_os_Debug_getLocalObjectCount },
702 { "getBinderProxyObjectCount", "()I",
703 (void*)android_os_Debug_getProxyObjectCount },
704 { "getBinderDeathObjectCount", "()I",
705 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700706 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
707 (void*)android_os_Debug_dumpNativeBacktraceToFile },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708};
709
710int register_android_os_Debug(JNIEnv *env)
711{
712 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800714 // Sanity check the number of other statistics expected in Java matches here.
715 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
716 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700717 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
718 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800719 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700720 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800721 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700722 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
723 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800724 return JNI_ERR;
725 }
726
727 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
728
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700729 for (int i=0; i<_NUM_CORE_HEAP; i++) {
730 stat_fields[i].pss_field =
731 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700732 stat_fields[i].pssSwappable_field =
733 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700734 stat_fields[i].privateDirty_field =
735 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
736 stat_fields[i].sharedDirty_field =
737 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700738 stat_fields[i].privateClean_field =
739 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700740 stat_fields[i].sharedClean_field =
741 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700742 }
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
745}
746
Andy McFadden06a6b552010-07-13 16:28:09 -0700747}; // namespace android