blob: e0bfecb140407959e2108f56920453d2ac351019 [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>
Colin Crossc4fb5f92016-02-02 16:51:15 -080024#include <memunreachable/memunreachable.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Ruben Brunk87eac992013-09-09 17:44:59 -070026#include <cutils/log.h>
Dianne Hackbornf72467a2012-06-08 17:23:59 -070027#include <fcntl.h>
Mark Salyzyn85394032014-04-16 10:28:37 -070028#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <time.h>
34#include <sys/time.h>
Andy McFadden06a6b552010-07-13 16:28:09 -070035#include <errno.h>
36#include <assert.h>
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070037#include <ctype.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include <malloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Colin Crossc4fb5f92016-02-02 16:51:15 -080040#include <iomanip>
41#include <string>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043namespace android
44{
45
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070046enum {
47 HEAP_UNKNOWN,
48 HEAP_DALVIK,
49 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080050
Dianne Hackborn64770d12013-05-23 17:51:19 -070051 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080052 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070053 HEAP_CURSOR,
54 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080055 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070056 HEAP_UNKNOWN_DEV,
57 HEAP_SO,
58 HEAP_JAR,
59 HEAP_APK,
60 HEAP_TTF,
61 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070062 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070063 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070064 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070065 HEAP_GRAPHICS,
66 HEAP_GL,
67 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070068
Anwar Ghuloum3c615062013-05-13 14:18:02 -070069 HEAP_DALVIK_NORMAL,
70 HEAP_DALVIK_LARGE,
71 HEAP_DALVIK_LINEARALLOC,
72 HEAP_DALVIK_ACCOUNTING,
73 HEAP_DALVIK_CODE_CACHE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080074 HEAP_DALVIK_ZYGOTE,
75 HEAP_DALVIK_NON_MOVING,
76 HEAP_DALVIK_INDIRECT_REFERENCE_TABLE,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070077
78 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070079 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070080 _NUM_CORE_HEAP = HEAP_NATIVE+1
81};
82
83struct stat_fields {
84 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070085 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070086 jfieldID privateDirty_field;
87 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070088 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -070089 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -070090 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -080091 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070092};
93
94struct stat_field_names {
95 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070096 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070097 const char* privateDirty_name;
98 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -070099 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700100 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700101 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800102 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700103};
104
105static stat_fields stat_fields[_NUM_CORE_HEAP];
106
107static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700108 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800109 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700110 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800111 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700112 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800113 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700114};
115
116jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800117jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700119static bool memtrackLoaded;
120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700122 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700123 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700124 int privateDirty;
125 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700126 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700127 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700128 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800129 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130};
131
132#define BINDER_STATS "/proc/binder/stats"
133
134static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
135{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 struct mallinfo info = mallinfo();
137 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
140static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
141{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 struct mallinfo info = mallinfo();
143 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144}
145
146static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
147{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 struct mallinfo info = mallinfo();
149 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150}
151
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700152// Container used to retrieve graphics memory pss
153struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700154{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700155 int graphics;
156 int gl;
157 int other;
158};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700159
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700160/*
161 * Uses libmemtrack to retrieve graphics memory that the process is using.
162 * Any graphics memory reported in /proc/pid/smaps is not included here.
163 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700164static int read_memtrack_memory(struct memtrack_proc* p, int pid,
165 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700166{
167 int err = memtrack_proc_get(p, pid);
168 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700169 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700170 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700171 }
172
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700173 ssize_t pss = memtrack_proc_graphics_pss(p);
174 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700175 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700176 return pss;
177 }
178 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700179
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700180 pss = memtrack_proc_gl_pss(p);
181 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700182 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700183 return pss;
184 }
185 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700186
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700187 pss = memtrack_proc_other_pss(p);
188 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700189 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700190 return pss;
191 }
192 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700193
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700194 return 0;
195}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700196
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700197/*
198 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
199 */
200static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
201{
202 if (!memtrackLoaded) {
203 return -1;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700204 }
205
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700206 struct memtrack_proc* p = memtrack_proc_new();
207 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700208 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700209 return -1;
210 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700211
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700212 int err = read_memtrack_memory(p, pid, graphics_mem);
213 memtrack_proc_destroy(p);
214 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700215}
216
Martijn Coenene0764852016-01-07 17:04:22 -0800217static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218{
219 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700220 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 bool skip, done = false;
222
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800223 unsigned pss = 0, swappable_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700224 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 unsigned shared_clean = 0, shared_dirty = 0;
226 unsigned private_clean = 0, private_dirty = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800227 unsigned swapped_out = 0, swapped_out_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700228 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 unsigned temp;
230
Colin Cross0c6bc732014-06-17 15:18:07 -0700231 uint64_t start;
232 uint64_t end = 0;
233 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700234 char* name;
235 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700237 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700238 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700239 int prevHeap = HEAP_UNKNOWN;
240
Martijn Coenene0764852016-01-07 17:04:22 -0800241 *foundSwapPss = false;
242
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700243 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);
Mathieu Chartierd47cbea2016-03-03 10:59:33 -0800265 // Trim the end of the line if it is " (deleted)".
266 const char* deleted_str = " (deleted)";
267 if (nameLen > (int)strlen(deleted_str) &&
268 strcmp(name+nameLen-strlen(deleted_str), deleted_str) == 0) {
269 nameLen -= strlen(deleted_str);
270 name[nameLen] = '\0';
271 }
Dianne Hackborn64770d12013-05-23 17:51:19 -0700272 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700273 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700274 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
275 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700276 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800277 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700278 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
279 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700280 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700281 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
282 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700283 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700284 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
285 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700286 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700287 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
288 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700289 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700290 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Ian Rogers9f8589c2013-02-22 20:35:06 -0800291 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700292 whichHeap = HEAP_DEX;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700293 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700294 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
295 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700296 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700297 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
298 whichHeap = HEAP_ART;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700299 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700300 } else if (strncmp(name, "/dev/", 5) == 0) {
301 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
302 whichHeap = HEAP_GL_DEV;
303 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
304 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
305 whichHeap = HEAP_DALVIK_OTHER;
306 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
307 subHeap = HEAP_DALVIK_LINEARALLOC;
308 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
309 (strstr(name, "/dev/ashmem/dalvik-main space") == name)) {
310 // This is the regular Dalvik heap.
311 whichHeap = HEAP_DALVIK;
312 subHeap = HEAP_DALVIK_NORMAL;
Mathieu Chartier2e2069d2015-10-19 13:48:34 -0700313 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name ||
314 strstr(name, "/dev/ashmem/dalvik-free list large object space")
315 == name) {
Mathieu Chartier93084622015-05-01 11:30:22 -0700316 whichHeap = HEAP_DALVIK;
317 subHeap = HEAP_DALVIK_LARGE;
318 } else if (strstr(name, "/dev/ashmem/dalvik-non moving space") == name) {
319 whichHeap = HEAP_DALVIK;
320 subHeap = HEAP_DALVIK_NON_MOVING;
321 } else if (strstr(name, "/dev/ashmem/dalvik-zygote space") == name) {
322 whichHeap = HEAP_DALVIK;
323 subHeap = HEAP_DALVIK_ZYGOTE;
324 } else if (strstr(name, "/dev/ashmem/dalvik-indirect ref") == name) {
325 subHeap = HEAP_DALVIK_INDIRECT_REFERENCE_TABLE;
326 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) {
327 subHeap = HEAP_DALVIK_CODE_CACHE;
328 } else {
329 subHeap = HEAP_DALVIK_ACCOUNTING; // Default to accounting.
330 }
331 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
332 whichHeap = HEAP_CURSOR;
333 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
334 whichHeap = HEAP_NATIVE;
335 } else {
336 whichHeap = HEAP_ASHMEM;
337 }
338 } else {
339 whichHeap = HEAP_UNKNOWN_DEV;
340 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700341 } else if (strncmp(name, "[anon:", 6) == 0) {
342 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700343 } else if (nameLen > 0) {
344 whichHeap = HEAP_UNKNOWN_MAP;
345 } else if (start == prevEnd && prevHeap == HEAP_SO) {
346 // bss section of a shared library.
347 whichHeap = HEAP_SO;
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350
Steve Block6215d3f2012-01-04 20:05:49 +0000351 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800353
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700354 shared_clean = 0;
355 shared_dirty = 0;
356 private_clean = 0;
357 private_dirty = 0;
358 swapped_out = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800359 swapped_out_pss = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 while (true) {
362 if (fgets(line, 1024, fp) == 0) {
363 done = true;
364 break;
365 }
366
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700367 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800368 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700369 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800370 /* resident = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700371 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700373 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700375 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700377 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700379 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700381 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800382 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700383 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
384 swapped_out = temp;
Martijn Coenene0764852016-01-07 17:04:22 -0800385 } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) {
386 *foundSwapPss = true;
387 swapped_out_pss = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700388 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700390 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 break;
392 }
393 }
394
395 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700396 if (is_swappable && (pss > 0)) {
397 sharing_proportion = 0.0;
398 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700399 sharing_proportion = (pss - private_clean
400 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700401 }
402 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
403 } else
404 swappable_pss = 0;
405
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700406 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700407 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700408 stats[whichHeap].privateDirty += private_dirty;
409 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700410 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700411 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700412 stats[whichHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800413 stats[whichHeap].swappedOutPss += swapped_out_pss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700414 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700415 stats[subHeap].pss += pss;
416 stats[subHeap].swappablePss += swappable_pss;
417 stats[subHeap].privateDirty += private_dirty;
418 stats[subHeap].sharedDirty += shared_dirty;
419 stats[subHeap].privateClean += private_clean;
420 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700421 stats[subHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800422 stats[subHeap].swappedOutPss += swapped_out_pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
426}
427
Martijn Coenene0764852016-01-07 17:04:22 -0800428static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429{
430 char tmp[128];
431 FILE *fp;
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 sprintf(tmp, "/proc/%d/smaps", pid);
434 fp = fopen(tmp, "r");
435 if (fp == 0) return;
436
Martijn Coenene0764852016-01-07 17:04:22 -0800437 read_mapinfo(fp, stats, foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 fclose(fp);
439}
440
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700441static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
442 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443{
Martijn Coenene0764852016-01-07 17:04:22 -0800444 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700445 stats_t stats[_NUM_HEAP];
446 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800447
Martijn Coenene0764852016-01-07 17:04:22 -0800448 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700450 struct graphics_memory_pss graphics_mem;
451 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
452 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
453 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
454 stats[HEAP_GL].pss = graphics_mem.gl;
455 stats[HEAP_GL].privateDirty = graphics_mem.gl;
456 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
457 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700458 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700459
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700460 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700461 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700462 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700463 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
464 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700465 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700466 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700467 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800468 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700469 }
470
471 for (int i=0; i<_NUM_CORE_HEAP; i++) {
472 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700473 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700474 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
475 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700476 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700477 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700478 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800479 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700480 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800481
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700482
Martijn Coenene0764852016-01-07 17:04:22 -0800483 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800485
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700486 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
487 if (otherArray == NULL) {
488 return;
489 }
490
491 int j=0;
492 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
493 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700494 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700495 otherArray[j++] = stats[i].privateDirty;
496 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700497 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700498 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700499 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800500 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700501 }
502
503 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504}
505
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700506static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
507{
508 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
509}
510
Martijn Coenene0764852016-01-07 17:04:22 -0800511static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
512 jlongArray outUssSwapPss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700513{
514 char line[1024];
515 jlong pss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800516 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700517 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800518 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700519
520 char tmp[128];
521 FILE *fp;
522
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700523 struct graphics_memory_pss graphics_mem;
524 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800525 pss = uss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700526 }
527
Dianne Hackbornb437e092011-08-05 17:50:29 -0700528 sprintf(tmp, "/proc/%d/smaps", pid);
529 fp = fopen(tmp, "r");
Dianne Hackbornb437e092011-08-05 17:50:29 -0700530
Dianne Hackborn37c99432013-09-05 19:34:57 -0700531 if (fp != 0) {
532 while (true) {
533 if (fgets(line, 1024, fp) == NULL) {
534 break;
535 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700536
Dianne Hackborn37c99432013-09-05 19:34:57 -0700537 if (line[0] == 'P') {
538 if (strncmp(line, "Pss:", 4) == 0) {
539 char* c = line + 4;
540 while (*c != 0 && (*c < '0' || *c > '9')) {
541 c++;
542 }
543 pss += atoi(c);
Iliyan Malchev45838de2014-12-11 14:29:36 -0800544 } else if (strncmp(line, "Private_Clean:", 14) == 0
545 || strncmp(line, "Private_Dirty:", 14) == 0) {
Dianne Hackborn37c99432013-09-05 19:34:57 -0700546 char* c = line + 14;
547 while (*c != 0 && (*c < '0' || *c > '9')) {
548 c++;
549 }
550 uss += atoi(c);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700551 }
Martijn Coenene0764852016-01-07 17:04:22 -0800552 } else if (line[0] == 'S' && strncmp(line, "SwapPss:", 8) == 0) {
553 char* c = line + 8;
554 jlong lSwapPss;
555 while (*c != 0 && (*c < '0' || *c > '9')) {
556 c++;
557 }
558 lSwapPss = atoi(c);
559 swapPss += lSwapPss;
560 pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700561 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700562 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700563
Dianne Hackborn37c99432013-09-05 19:34:57 -0700564 fclose(fp);
565 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700566
Martijn Coenene0764852016-01-07 17:04:22 -0800567 if (outUssSwapPss != NULL) {
568 if (env->GetArrayLength(outUssSwapPss) >= 1) {
569 jlong* outUssSwapPssArray = env->GetLongArrayElements(outUssSwapPss, 0);
570 if (outUssSwapPssArray != NULL) {
571 outUssSwapPssArray[0] = uss;
572 if (env->GetArrayLength(outUssSwapPss) >= 2) {
573 outUssSwapPssArray[1] = swapPss;
574 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700575 }
Martijn Coenene0764852016-01-07 17:04:22 -0800576 env->ReleaseLongArrayElements(outUssSwapPss, outUssSwapPssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700577 }
578 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700579
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800580 if (outMemtrack != NULL) {
581 if (env->GetArrayLength(outMemtrack) >= 1) {
582 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
583 if (outMemtrackArray != NULL) {
584 outMemtrackArray[0] = memtrack;
585 }
586 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
587 }
588 }
589
Dianne Hackbornb437e092011-08-05 17:50:29 -0700590 return pss;
591}
592
593static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
594{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800595 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700596}
597
Martijn Coenen398e24e2015-04-23 21:55:58 +0200598static long get_allocated_vmalloc_memory() {
599 char line[1024];
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200600 // Ignored tags that don't actually consume memory (ie remappings)
601 static const char* const ignored_tags[] = {
602 "ioremap",
603 "map_lowmem",
604 "vm_map_ram",
605 NULL
606 };
Martijn Coenen398e24e2015-04-23 21:55:58 +0200607 long size, vmalloc_allocated_size = 0;
608 FILE* fp = fopen("/proc/vmallocinfo", "r");
609 if (fp == NULL) {
610 return 0;
611 }
612 while (true) {
613 if (fgets(line, 1024, fp) == NULL) {
614 break;
615 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200616 bool valid_line = true;
617 int i = 0;
618 while (ignored_tags[i]) {
619 if (strstr(line, ignored_tags[i]) != NULL) {
620 valid_line = false;
621 break;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200622 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200623 i++;
624 }
625 if (valid_line && (sscanf(line, "%*x-%*x %ld", &size) == 1)) {
626 vmalloc_allocated_size += size;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200627 }
628 }
629 fclose(fp);
630 return vmalloc_allocated_size;
631}
632
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700633enum {
634 MEMINFO_TOTAL,
635 MEMINFO_FREE,
636 MEMINFO_BUFFERS,
637 MEMINFO_CACHED,
638 MEMINFO_SHMEM,
639 MEMINFO_SLAB,
640 MEMINFO_SWAP_TOTAL,
641 MEMINFO_SWAP_FREE,
642 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700643 MEMINFO_MAPPED,
644 MEMINFO_VMALLOC_USED,
645 MEMINFO_PAGE_TABLES,
646 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700647 MEMINFO_COUNT
648};
649
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800650static long long get_zram_mem_used()
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800651{
652#define ZRAM_SYSFS "/sys/block/zram0/"
653 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
654 if (f) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800655 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800656
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800657 int matched = fscanf(f, "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800658 if (matched != 1)
659 ALOGW("failed to parse " ZRAM_SYSFS "mm_stat");
660
661 fclose(f);
662 return mem_used_total;
663 }
664
665 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
666 if (f) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800667 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800668
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800669 int matched = fscanf(f, "%lld", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800670 if (matched != 1)
671 ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total");
672
673 fclose(f);
674 return mem_used_total;
675 }
676
677 return 0;
678}
679
Dianne Hackborn8e692572013-09-10 19:06:15 -0700680static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
681{
682 char buffer[1024];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200683 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700684
685 if (out == NULL) {
686 jniThrowNullPointerException(env, "out == null");
687 return;
688 }
689
690 int fd = open("/proc/meminfo", O_RDONLY);
691
692 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700693 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700694 return;
695 }
696
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700697 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700698 close(fd);
699
700 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700701 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700702 return;
703 }
704 buffer[len] = 0;
705
706 static const char* const tags[] = {
707 "MemTotal:",
708 "MemFree:",
709 "Buffers:",
710 "Cached:",
711 "Shmem:",
712 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700713 "SwapTotal:",
714 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700715 "ZRam:",
716 "Mapped:",
717 "VmallocUsed:",
718 "PageTables:",
719 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700720 NULL
721 };
722 static const int tagsLen[] = {
723 9,
724 8,
725 8,
726 7,
727 6,
728 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700729 10,
730 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700731 5,
732 7,
733 12,
734 11,
735 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700736 0
737 };
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700738 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700739
740 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200741 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700742 int i = 0;
743 while (tags[i]) {
744 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
745 p += tagsLen[i];
746 while (*p == ' ') p++;
747 char* num = p;
748 while (*p >= '0' && *p <= '9') p++;
749 if (*p != 0) {
750 *p = 0;
751 p++;
752 }
753 mem[i] = atoll(num);
754 numFound++;
755 break;
756 }
757 i++;
758 }
759 while (*p && *p != '\n') {
760 p++;
761 }
762 if (*p) p++;
763 }
764
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800765 mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200766 // Recompute Vmalloc Used since the value in meminfo
767 // doesn't account for I/O remapping which doesn't use RAM.
768 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700769
Dianne Hackborn8e692572013-09-10 19:06:15 -0700770 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700771 if (maxNum > MEMINFO_COUNT) {
772 maxNum = MEMINFO_COUNT;
773 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700774 jlong* outArray = env->GetLongArrayElements(out, 0);
775 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700776 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700777 outArray[i] = mem[i];
778 }
779 }
780 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781}
782
Martijn Coenen398e24e2015-04-23 21:55:58 +0200783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784static jint read_binder_stat(const char* stat)
785{
786 FILE* fp = fopen(BINDER_STATS, "r");
787 if (fp == NULL) {
788 return -1;
789 }
790
791 char line[1024];
792
793 char compare[128];
794 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 // loop until we have the block that represents this process
797 do {
798 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700799 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 return -1;
801 }
802 } while (strncmp(compare, line, len));
803
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800804 // 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 -0800805 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 do {
808 if (fgets(line, 1024, fp) == 0) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700809 fclose(fp);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 return -1;
811 }
812 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 // we have the line, now increment the line ptr to the value
815 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700816 jint result = atoi(ptr);
817 fclose(fp);
818 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819}
820
821static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
822{
823 return read_binder_stat("bcTRANSACTION");
824}
825
826static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
827{
828 return read_binder_stat("brTRANSACTION");
829}
830
831// these are implemented in android_util_Binder.cpp
832jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
833jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
834jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
835
Andy McFadden06a6b552010-07-13 16:28:09 -0700836
Andy McFadden06a6b552010-07-13 16:28:09 -0700837/* pulled out of bionic */
838extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
839 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
840extern "C" void free_malloc_leak_info(uint8_t* info);
841#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
842#define BACKTRACE_SIZE 32
843
844/*
845 * This is a qsort() callback.
846 *
847 * See dumpNativeHeap() for comments about the data format and sort order.
848 */
849static int compareHeapRecords(const void* vrec1, const void* vrec2)
850{
851 const size_t* rec1 = (const size_t*) vrec1;
852 const size_t* rec2 = (const size_t*) vrec2;
853 size_t size1 = *rec1;
854 size_t size2 = *rec2;
855
856 if (size1 < size2) {
857 return 1;
858 } else if (size1 > size2) {
859 return -1;
860 }
861
862 intptr_t* bt1 = (intptr_t*)(rec1 + 2);
863 intptr_t* bt2 = (intptr_t*)(rec2 + 2);
864 for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) {
865 intptr_t addr1 = bt1[idx];
866 intptr_t addr2 = bt2[idx];
867 if (addr1 == addr2) {
868 if (addr1 == 0)
869 break;
870 continue;
871 }
872 if (addr1 < addr2) {
873 return -1;
874 } else if (addr1 > addr2) {
875 return 1;
876 }
877 }
878
879 return 0;
880}
881
882/*
883 * The get_malloc_leak_info() call returns an array of structs that
884 * look like this:
885 *
886 * size_t size
887 * size_t allocations
888 * intptr_t backtrace[32]
889 *
890 * "size" is the size of the allocation, "backtrace" is a fixed-size
891 * array of function pointers, and "allocations" is the number of
892 * allocations with the exact same size and backtrace.
893 *
894 * The entries are sorted by descending total size (i.e. size*allocations)
895 * then allocation count. For best results with "diff" we'd like to sort
896 * primarily by individual size then stack trace. Since the entries are
897 * fixed-size, and we're allowed (by the current implementation) to mangle
898 * them, we can do this in place.
899 */
900static void dumpNativeHeap(FILE* fp)
901{
902 uint8_t* info = NULL;
903 size_t overallSize, infoSize, totalMemory, backtraceSize;
904
905 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
906 &backtraceSize);
907 if (info == NULL) {
908 fprintf(fp, "Native heap dump not available. To enable, run these"
909 " commands (requires root):\n");
910 fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n");
911 fprintf(fp, "$ adb shell stop\n");
912 fprintf(fp, "$ adb shell start\n");
913 return;
914 }
915 assert(infoSize != 0);
916 assert(overallSize % infoSize == 0);
917
918 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
919
920 size_t recordCount = overallSize / infoSize;
921 fprintf(fp, "Total memory: %zu\n", totalMemory);
922 fprintf(fp, "Allocation records: %zd\n", recordCount);
923 if (backtraceSize != BACKTRACE_SIZE) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000924 fprintf(fp, "WARNING: mismatched backtrace sizes (%zu vs. %d)\n",
Andy McFadden06a6b552010-07-13 16:28:09 -0700925 backtraceSize, BACKTRACE_SIZE);
926 }
927 fprintf(fp, "\n");
928
929 /* re-sort the entries */
930 qsort(info, recordCount, infoSize, compareHeapRecords);
931
932 /* dump the entries to the file */
933 const uint8_t* ptr = info;
934 for (size_t idx = 0; idx < recordCount; idx++) {
935 size_t size = *(size_t*) ptr;
936 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
937 intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2);
938
939 fprintf(fp, "z %d sz %8zu num %4zu bt",
940 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
941 size & ~SIZE_FLAG_ZYGOTE_CHILD,
942 allocations);
943 for (size_t bt = 0; bt < backtraceSize; bt++) {
944 if (backtrace[bt] == 0) {
945 break;
946 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000947#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -0700948 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000949#else
Mark Salyzyn85394032014-04-16 10:28:37 -0700950 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000951#endif
Andy McFadden06a6b552010-07-13 16:28:09 -0700952 }
953 }
954 fprintf(fp, "\n");
955
956 ptr += infoSize;
957 }
958
Andy McFadden06a6b552010-07-13 16:28:09 -0700959 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -0700960
961 fprintf(fp, "MAPS\n");
962 const char* maps = "/proc/self/maps";
963 FILE* in = fopen(maps, "r");
964 if (in == NULL) {
965 fprintf(fp, "Could not open %s\n", maps);
966 return;
967 }
968 char buf[BUFSIZ];
969 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) {
970 fwrite(buf, sizeof(char), n, fp);
971 }
972 fclose(in);
973
974 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700975}
Andy McFadden06a6b552010-07-13 16:28:09 -0700976
977/*
978 * Dump the native heap, writing human-readable output to the specified
979 * file descriptor.
980 */
981static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz,
982 jobject fileDescriptor)
983{
984 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800985 jniThrowNullPointerException(env, "fd == null");
Andy McFadden06a6b552010-07-13 16:28:09 -0700986 return;
987 }
988 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
989 if (origFd < 0) {
990 jniThrowRuntimeException(env, "Invalid file descriptor");
991 return;
992 }
993
994 /* dup() the descriptor so we don't close the original with fclose() */
995 int fd = dup(origFd);
996 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000997 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700998 jniThrowRuntimeException(env, "dup() failed");
999 return;
1000 }
1001
1002 FILE* fp = fdopen(fd, "w");
1003 if (fp == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001004 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -07001005 close(fd);
1006 jniThrowRuntimeException(env, "fdopen() failed");
1007 return;
1008 }
1009
Steve Block5baa3a62011-12-20 16:23:08 +00001010 ALOGD("Native heap dump starting...\n");
Andy McFadden06a6b552010-07-13 16:28:09 -07001011 dumpNativeHeap(fp);
Steve Block5baa3a62011-12-20 16:23:08 +00001012 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -07001013
1014 fclose(fp);
1015}
1016
1017
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001018static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz,
1019 jint pid, jstring fileName)
1020{
1021 if (fileName == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001022 jniThrowNullPointerException(env, "file == null");
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001023 return;
1024 }
1025 const jchar* str = env->GetStringCritical(fileName, 0);
1026 String8 fileName8;
1027 if (str) {
Dan Albert66987492014-11-20 11:41:21 -08001028 fileName8 = String8(reinterpret_cast<const char16_t*>(str),
1029 env->GetStringLength(fileName));
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001030 env->ReleaseStringCritical(fileName, str);
1031 }
1032
1033 int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */
1034 if (fd < 0) {
1035 fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno));
1036 return;
1037 }
1038
1039 if (lseek(fd, 0, SEEK_END) < 0) {
1040 fprintf(stderr, "lseek: %s\n", strerror(errno));
1041 } else {
1042 dump_backtrace_to_file(pid, fd);
1043 }
1044
1045 close(fd);
1046}
1047
Colin Crossc4fb5f92016-02-02 16:51:15 -08001048static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
1049 jint limit, jboolean contents)
1050{
1051 std::string s = GetUnreachableMemoryString(contents, limit);
1052 return env->NewStringUTF(s.c_str());
1053}
1054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055/*
1056 * JNI registration.
1057 */
1058
Daniel Micay76f6a862015-09-19 17:31:01 -04001059static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 { "getNativeHeapSize", "()J",
1061 (void*) android_os_Debug_getNativeHeapSize },
1062 { "getNativeHeapAllocatedSize", "()J",
1063 (void*) android_os_Debug_getNativeHeapAllocatedSize },
1064 { "getNativeHeapFreeSize", "()J",
1065 (void*) android_os_Debug_getNativeHeapFreeSize },
1066 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1067 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001068 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1069 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001070 { "getPss", "()J",
1071 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001072 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001073 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001074 { "getMemInfo", "([J)V",
1075 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001076 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1077 (void*) android_os_Debug_dumpNativeHeap },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 { "getBinderSentTransactions", "()I",
1079 (void*) android_os_Debug_getBinderSentTransactions },
1080 { "getBinderReceivedTransactions", "()I",
1081 (void*) android_os_getBinderReceivedTransactions },
1082 { "getBinderLocalObjectCount", "()I",
1083 (void*)android_os_Debug_getLocalObjectCount },
1084 { "getBinderProxyObjectCount", "()I",
1085 (void*)android_os_Debug_getProxyObjectCount },
1086 { "getBinderDeathObjectCount", "()I",
1087 (void*)android_os_Debug_getDeathObjectCount },
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001088 { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V",
1089 (void*)android_os_Debug_dumpNativeBacktraceToFile },
Colin Crossc4fb5f92016-02-02 16:51:15 -08001090 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
1091 (void*)android_os_Debug_getUnreachableMemory },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092};
1093
1094int register_android_os_Debug(JNIEnv *env)
1095{
Adam Lesinski5b4ef812013-09-23 10:06:09 -07001096 int err = memtrack_init();
1097 if (err != 0) {
1098 memtrackLoaded = false;
1099 ALOGE("failed to load memtrack module: %d", err);
1100 } else {
1101 memtrackLoaded = true;
1102 }
1103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001106 // Sanity check the number of other statistics expected in Java matches here.
1107 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1108 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001109 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1110 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001111 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001112 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001113 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001114 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1115 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001116 return JNI_ERR;
1117 }
1118
1119 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -08001120 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001121
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001122 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1123 stat_fields[i].pss_field =
1124 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001125 stat_fields[i].pssSwappable_field =
1126 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001127 stat_fields[i].privateDirty_field =
1128 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1129 stat_fields[i].sharedDirty_field =
1130 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001131 stat_fields[i].privateClean_field =
1132 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001133 stat_fields[i].sharedClean_field =
1134 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001135 stat_fields[i].swappedOut_field =
1136 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -08001137 stat_fields[i].swappedOutPss_field =
1138 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001139 }
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1142}
1143
Andy McFadden06a6b552010-07-13 16:28:09 -07001144}; // namespace android