blob: a140b57268d1067e5c8a85330fab2db727483dff [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
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070019#include <assert.h>
20#include <ctype.h>
21#include <errno.h>
Dianne Hackbornf72467a2012-06-08 17:23:59 -070022#include <fcntl.h>
Mark Salyzyn85394032014-04-16 10:28:37 -070023#include <inttypes.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070024#include <malloc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <sys/time.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070029#include <time.h>
30#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Daniel Colascione6c518102017-07-27 03:33:34 -070032#include <atomic>
Colin Crossc4fb5f92016-02-02 16:51:15 -080033#include <iomanip>
34#include <string>
35
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070036#include <android-base/stringprintf.h>
Narayan Kamathf013daa2017-05-09 12:55:02 +010037#include <android-base/unique_fd.h>
Josh Gao3ce36962017-01-05 18:28:24 -080038#include <debuggerd/client.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070039#include <log/log.h>
40#include <utils/misc.h>
41#include <utils/String8.h>
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070042
Steven Morelandbdd5c422017-08-11 10:57:59 -070043#include <nativehelper/JNIHelp.h>
44#include <nativehelper/ScopedUtfChars.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070045#include "jni.h"
46#include <memtrack/memtrack.h>
47#include <memunreachable/memunreachable.h>
Daniel Colascione6c518102017-07-27 03:33:34 -070048#include "android_os_Debug.h"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050namespace android
51{
52
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070053static inline UniqueFile MakeUniqueFile(const char* path, const char* mode) {
Christopher Ferris8d652f82017-04-11 16:29:18 -070054 return UniqueFile(fopen(path, mode), safeFclose);
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070055}
56
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070057enum {
58 HEAP_UNKNOWN,
59 HEAP_DALVIK,
60 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080061
Dianne Hackborn64770d12013-05-23 17:51:19 -070062 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080063 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070064 HEAP_CURSOR,
65 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080066 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070067 HEAP_UNKNOWN_DEV,
68 HEAP_SO,
69 HEAP_JAR,
70 HEAP_APK,
71 HEAP_TTF,
72 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070073 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070074 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070075 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070076 HEAP_GRAPHICS,
77 HEAP_GL,
78 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070079
Mathieu Chartier95550dd2017-07-13 15:01:34 -070080 // Dalvik extra sections (heap).
Anwar Ghuloum3c615062013-05-13 14:18:02 -070081 HEAP_DALVIK_NORMAL,
82 HEAP_DALVIK_LARGE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080083 HEAP_DALVIK_ZYGOTE,
84 HEAP_DALVIK_NON_MOVING,
Mathieu Chartier95550dd2017-07-13 15:01:34 -070085
86 // Dalvik other extra sections.
87 HEAP_DALVIK_OTHER_LINEARALLOC,
88 HEAP_DALVIK_OTHER_ACCOUNTING,
89 HEAP_DALVIK_OTHER_CODE_CACHE,
90 HEAP_DALVIK_OTHER_COMPILER_METADATA,
91 HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE,
92
93 // Boot vdex / app dex / app vdex
94 HEAP_DEX_BOOT_VDEX,
95 HEAP_DEX_APP_DEX,
96 HEAP_DEX_APP_VDEX,
97
98 // App art, boot art.
99 HEAP_ART_APP,
100 HEAP_ART_BOOT,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700101
102 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700103 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700104 _NUM_CORE_HEAP = HEAP_NATIVE+1
105};
106
107struct stat_fields {
108 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700109 jfieldID pssSwappable_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700110 jfieldID privateDirty_field;
111 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700112 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700113 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700114 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800115 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700116};
117
118struct stat_field_names {
119 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700120 const char* pssSwappable_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700121 const char* privateDirty_name;
122 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700123 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700124 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700125 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800126 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700127};
128
129static stat_fields stat_fields[_NUM_CORE_HEAP];
130
131static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700132 { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800133 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700134 { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800135 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700136 { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800137 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700138};
139
140jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800141jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
143struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700144 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700145 int swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700146 int privateDirty;
147 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700148 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700149 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700150 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800151 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152};
153
Daniel Colascione6c518102017-07-27 03:33:34 -0700154enum pss_rollup_support {
155 PSS_ROLLUP_UNTRIED,
156 PSS_ROLLUP_SUPPORTED,
157 PSS_ROLLUP_UNSUPPORTED
158};
159
160static std::atomic<pss_rollup_support> g_pss_rollup_support;
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162#define BINDER_STATS "/proc/binder/stats"
163
164static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
165{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 struct mallinfo info = mallinfo();
167 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}
169
170static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
171{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 struct mallinfo info = mallinfo();
173 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174}
175
176static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
177{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 struct mallinfo info = mallinfo();
179 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180}
181
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700182// Container used to retrieve graphics memory pss
183struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700184{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700185 int graphics;
186 int gl;
187 int other;
188};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700189
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700190/*
191 * Uses libmemtrack to retrieve graphics memory that the process is using.
192 * Any graphics memory reported in /proc/pid/smaps is not included here.
193 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700194static int read_memtrack_memory(struct memtrack_proc* p, int pid,
195 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700196{
197 int err = memtrack_proc_get(p, pid);
198 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700199 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700200 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700201 }
202
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700203 ssize_t pss = memtrack_proc_graphics_pss(p);
204 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700205 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700206 return pss;
207 }
208 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700209
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700210 pss = memtrack_proc_gl_pss(p);
211 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700212 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700213 return pss;
214 }
215 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700216
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700217 pss = memtrack_proc_other_pss(p);
218 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700219 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700220 return pss;
221 }
222 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700223
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700224 return 0;
225}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700226
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700227/*
228 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
229 */
230static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
231{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700232 struct memtrack_proc* p = memtrack_proc_new();
233 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700234 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700235 return -1;
236 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700237
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700238 int err = read_memtrack_memory(p, pid, graphics_mem);
239 memtrack_proc_destroy(p);
240 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700241}
242
Martijn Coenene0764852016-01-07 17:04:22 -0800243static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244{
245 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700246 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 bool skip, done = false;
248
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800249 unsigned pss = 0, swappable_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700250 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 unsigned shared_clean = 0, shared_dirty = 0;
252 unsigned private_clean = 0, private_dirty = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800253 unsigned swapped_out = 0, swapped_out_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700254 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 unsigned temp;
256
Colin Cross0c6bc732014-06-17 15:18:07 -0700257 uint64_t start;
258 uint64_t end = 0;
259 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700260 char* name;
261 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700263 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700264 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700265 int prevHeap = HEAP_UNKNOWN;
266
Martijn Coenene0764852016-01-07 17:04:22 -0800267 *foundSwapPss = false;
268
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700269 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
271 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700272 prevHeap = whichHeap;
273 prevEnd = end;
274 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700275 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700277 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
279 len = strlen(line);
280 if (len < 1) return;
281 line[--len] = 0;
282
Colin Cross0c6bc732014-06-17 15:18:07 -0700283 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700284 skip = true;
285 } else {
286 while (isspace(line[name_pos])) {
287 name_pos += 1;
288 }
289 name = line + name_pos;
290 nameLen = strlen(name);
Mathieu Chartierd47cbea2016-03-03 10:59:33 -0800291 // Trim the end of the line if it is " (deleted)".
292 const char* deleted_str = " (deleted)";
293 if (nameLen > (int)strlen(deleted_str) &&
294 strcmp(name+nameLen-strlen(deleted_str), deleted_str) == 0) {
295 nameLen -= strlen(deleted_str);
296 name[nameLen] = '\0';
297 }
Dianne Hackborn64770d12013-05-23 17:51:19 -0700298 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700299 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700300 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
301 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700302 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800303 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700304 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
305 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700306 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700307 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
308 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700309 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700310 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
311 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700312 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700313 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
314 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700315 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700316 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700317 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700318 whichHeap = HEAP_DEX;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700319 subHeap = HEAP_DEX_APP_DEX;
320 is_swappable = true;
321 } else if (nameLen > 5 && strcmp(name+nameLen-5, ".vdex") == 0) {
322 whichHeap = HEAP_DEX;
323 // Handle system@framework@boot* and system/framework/boot*
324 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
325 subHeap = HEAP_DEX_BOOT_VDEX;
326 } else {
327 subHeap = HEAP_DEX_APP_VDEX;
328 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700329 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700330 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
331 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700332 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700333 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
334 whichHeap = HEAP_ART;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700335 // Handle system@framework@boot* and system/framework/boot*
336 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
337 subHeap = HEAP_ART_BOOT;
338 } else {
339 subHeap = HEAP_ART_APP;
340 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700341 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700342 } else if (strncmp(name, "/dev/", 5) == 0) {
343 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
344 whichHeap = HEAP_GL_DEV;
345 } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
346 if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
347 whichHeap = HEAP_DALVIK_OTHER;
348 if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700349 subHeap = HEAP_DALVIK_OTHER_LINEARALLOC;
Mathieu Chartier93084622015-05-01 11:30:22 -0700350 } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
351 (strstr(name, "/dev/ashmem/dalvik-main space") == name)) {
352 // This is the regular Dalvik heap.
353 whichHeap = HEAP_DALVIK;
354 subHeap = HEAP_DALVIK_NORMAL;
Mathieu Chartier2e2069d2015-10-19 13:48:34 -0700355 } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name ||
356 strstr(name, "/dev/ashmem/dalvik-free list large object space")
357 == name) {
Mathieu Chartier93084622015-05-01 11:30:22 -0700358 whichHeap = HEAP_DALVIK;
359 subHeap = HEAP_DALVIK_LARGE;
360 } else if (strstr(name, "/dev/ashmem/dalvik-non moving space") == name) {
361 whichHeap = HEAP_DALVIK;
362 subHeap = HEAP_DALVIK_NON_MOVING;
363 } else if (strstr(name, "/dev/ashmem/dalvik-zygote space") == name) {
364 whichHeap = HEAP_DALVIK;
365 subHeap = HEAP_DALVIK_ZYGOTE;
366 } else if (strstr(name, "/dev/ashmem/dalvik-indirect ref") == name) {
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700367 subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
Mathieu Chartier94f11c52017-04-10 18:28:03 -0700368 } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name ||
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700369 strstr(name, "/dev/ashmem/dalvik-data-code-cache") == name) {
370 subHeap = HEAP_DALVIK_OTHER_CODE_CACHE;
371 } else if (strstr(name, "/dev/ashmem/dalvik-CompilerMetadata") == name) {
372 subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
Mathieu Chartier93084622015-05-01 11:30:22 -0700373 } else {
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700374 subHeap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting.
Mathieu Chartier93084622015-05-01 11:30:22 -0700375 }
376 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
377 whichHeap = HEAP_CURSOR;
378 } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
379 whichHeap = HEAP_NATIVE;
380 } else {
381 whichHeap = HEAP_ASHMEM;
382 }
383 } else {
384 whichHeap = HEAP_UNKNOWN_DEV;
385 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700386 } else if (strncmp(name, "[anon:", 6) == 0) {
387 whichHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700388 } else if (nameLen > 0) {
389 whichHeap = HEAP_UNKNOWN_MAP;
390 } else if (start == prevEnd && prevHeap == HEAP_SO) {
391 // bss section of a shared library.
392 whichHeap = HEAP_SO;
393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395
Steve Block6215d3f2012-01-04 20:05:49 +0000396 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800398
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700399 shared_clean = 0;
400 shared_dirty = 0;
401 private_clean = 0;
402 private_dirty = 0;
403 swapped_out = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800404 swapped_out_pss = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 while (true) {
407 if (fgets(line, 1024, fp) == 0) {
408 done = true;
409 break;
410 }
411
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700412 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800413 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700414 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800415 /* resident = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700416 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700418 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700420 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700422 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700424 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700426 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800427 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700428 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
429 swapped_out = temp;
Martijn Coenene0764852016-01-07 17:04:22 -0800430 } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) {
431 *foundSwapPss = true;
432 swapped_out_pss = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700433 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700435 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 break;
437 }
438 }
439
440 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700441 if (is_swappable && (pss > 0)) {
442 sharing_proportion = 0.0;
443 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700444 sharing_proportion = (pss - private_clean
445 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700446 }
447 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
448 } else
449 swappable_pss = 0;
450
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700451 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700452 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700453 stats[whichHeap].privateDirty += private_dirty;
454 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700455 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700456 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700457 stats[whichHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800458 stats[whichHeap].swappedOutPss += swapped_out_pss;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700459 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER ||
460 whichHeap == HEAP_DEX || whichHeap == HEAP_ART) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700461 stats[subHeap].pss += pss;
462 stats[subHeap].swappablePss += swappable_pss;
463 stats[subHeap].privateDirty += private_dirty;
464 stats[subHeap].sharedDirty += shared_dirty;
465 stats[subHeap].privateClean += private_clean;
466 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700467 stats[subHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800468 stats[subHeap].swappedOutPss += swapped_out_pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471 }
472}
473
Martijn Coenene0764852016-01-07 17:04:22 -0800474static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475{
Andreas Gampece711492016-10-04 18:06:42 -0700476 *foundSwapPss = false;
477
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700478 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
479 UniqueFile fp = MakeUniqueFile(smaps_path.c_str(), "re");
480 if (fp == nullptr) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700482 read_mapinfo(fp.get(), stats, foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483}
484
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700485static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
486 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487{
Martijn Coenene0764852016-01-07 17:04:22 -0800488 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700489 stats_t stats[_NUM_HEAP];
490 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800491
Martijn Coenene0764852016-01-07 17:04:22 -0800492 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700494 struct graphics_memory_pss graphics_mem;
495 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
496 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
497 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
498 stats[HEAP_GL].pss = graphics_mem.gl;
499 stats[HEAP_GL].privateDirty = graphics_mem.gl;
500 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
501 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700502 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700503
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700504 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700505 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700506 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700507 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
508 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700509 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700510 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700511 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800512 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700513 }
514
515 for (int i=0; i<_NUM_CORE_HEAP; i++) {
516 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700517 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700518 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
519 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700520 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700521 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700522 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800523 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700524 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800525
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700526
Martijn Coenene0764852016-01-07 17:04:22 -0800527 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700528 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800529
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700530 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
531 if (otherArray == NULL) {
532 return;
533 }
534
535 int j=0;
536 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
537 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700538 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700539 otherArray[j++] = stats[i].privateDirty;
540 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700541 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700542 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700543 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800544 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700545 }
546
547 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548}
549
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700550static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
551{
552 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
553}
554
Daniel Colascione6c518102017-07-27 03:33:34 -0700555UniqueFile OpenSmapsOrRollup(int pid)
556{
557 enum pss_rollup_support rollup_support =
558 g_pss_rollup_support.load(std::memory_order_relaxed);
559 if (rollup_support != PSS_ROLLUP_UNSUPPORTED) {
560 std::string smaps_rollup_path =
561 base::StringPrintf("/proc/%d/smaps_rollup", pid);
562 UniqueFile fp_rollup = MakeUniqueFile(smaps_rollup_path.c_str(), "re");
563 if (fp_rollup == nullptr && errno != ENOENT) {
564 return fp_rollup; // Actual error, not just old kernel.
565 }
566 if (fp_rollup != nullptr) {
567 if (rollup_support == PSS_ROLLUP_UNTRIED) {
568 ALOGI("using rollup pss collection");
569 g_pss_rollup_support.store(PSS_ROLLUP_SUPPORTED,
570 std::memory_order_relaxed);
571 }
572 return fp_rollup;
573 }
574 g_pss_rollup_support.store(PSS_ROLLUP_UNSUPPORTED,
575 std::memory_order_relaxed);
576 }
577
578 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
579 return MakeUniqueFile(smaps_path.c_str(), "re");
580}
581
Martijn Coenene0764852016-01-07 17:04:22 -0800582static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
583 jlongArray outUssSwapPss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700584{
585 char line[1024];
586 jlong pss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800587 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700588 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800589 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700590
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700591 struct graphics_memory_pss graphics_mem;
592 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800593 pss = uss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700594 }
595
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700596 {
Daniel Colascione6c518102017-07-27 03:33:34 -0700597 UniqueFile fp = OpenSmapsOrRollup(pid);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700598
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700599 if (fp != nullptr) {
600 while (true) {
Daniel Colascione6c518102017-07-27 03:33:34 -0700601 if (fgets(line, sizeof (line), fp.get()) == NULL) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700602 break;
603 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700604
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700605 if (line[0] == 'P') {
606 if (strncmp(line, "Pss:", 4) == 0) {
607 char* c = line + 4;
608 while (*c != 0 && (*c < '0' || *c > '9')) {
609 c++;
610 }
611 pss += atoi(c);
612 } else if (strncmp(line, "Private_Clean:", 14) == 0
613 || strncmp(line, "Private_Dirty:", 14) == 0) {
614 char* c = line + 14;
615 while (*c != 0 && (*c < '0' || *c > '9')) {
616 c++;
617 }
618 uss += atoi(c);
619 }
620 } else if (line[0] == 'S' && strncmp(line, "SwapPss:", 8) == 0) {
621 char* c = line + 8;
622 jlong lSwapPss;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700623 while (*c != 0 && (*c < '0' || *c > '9')) {
624 c++;
625 }
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700626 lSwapPss = atoi(c);
627 swapPss += lSwapPss;
628 pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP
Dianne Hackbornc8230512013-07-13 21:32:12 -0700629 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700630 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700631 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700632 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700633
Martijn Coenene0764852016-01-07 17:04:22 -0800634 if (outUssSwapPss != NULL) {
635 if (env->GetArrayLength(outUssSwapPss) >= 1) {
636 jlong* outUssSwapPssArray = env->GetLongArrayElements(outUssSwapPss, 0);
637 if (outUssSwapPssArray != NULL) {
638 outUssSwapPssArray[0] = uss;
639 if (env->GetArrayLength(outUssSwapPss) >= 2) {
640 outUssSwapPssArray[1] = swapPss;
641 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700642 }
Martijn Coenene0764852016-01-07 17:04:22 -0800643 env->ReleaseLongArrayElements(outUssSwapPss, outUssSwapPssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700644 }
645 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700646
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800647 if (outMemtrack != NULL) {
648 if (env->GetArrayLength(outMemtrack) >= 1) {
649 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
650 if (outMemtrackArray != NULL) {
651 outMemtrackArray[0] = memtrack;
652 }
653 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
654 }
655 }
656
Dianne Hackbornb437e092011-08-05 17:50:29 -0700657 return pss;
658}
659
660static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
661{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800662 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700663}
664
Martijn Coenen398e24e2015-04-23 21:55:58 +0200665static long get_allocated_vmalloc_memory() {
666 char line[1024];
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200667 // Ignored tags that don't actually consume memory (ie remappings)
668 static const char* const ignored_tags[] = {
669 "ioremap",
670 "map_lowmem",
671 "vm_map_ram",
672 NULL
673 };
Martijn Coenen398e24e2015-04-23 21:55:58 +0200674 long size, vmalloc_allocated_size = 0;
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700675
676 UniqueFile fp = MakeUniqueFile("/proc/vmallocinfo", "re");
677 if (fp == nullptr) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200678 return 0;
679 }
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700680
Martijn Coenen398e24e2015-04-23 21:55:58 +0200681 while (true) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700682 if (fgets(line, 1024, fp.get()) == NULL) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200683 break;
684 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200685 bool valid_line = true;
686 int i = 0;
687 while (ignored_tags[i]) {
688 if (strstr(line, ignored_tags[i]) != NULL) {
689 valid_line = false;
690 break;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200691 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200692 i++;
693 }
694 if (valid_line && (sscanf(line, "%*x-%*x %ld", &size) == 1)) {
695 vmalloc_allocated_size += size;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200696 }
697 }
Martijn Coenen398e24e2015-04-23 21:55:58 +0200698 return vmalloc_allocated_size;
699}
700
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700701enum {
702 MEMINFO_TOTAL,
703 MEMINFO_FREE,
704 MEMINFO_BUFFERS,
705 MEMINFO_CACHED,
706 MEMINFO_SHMEM,
707 MEMINFO_SLAB,
708 MEMINFO_SWAP_TOTAL,
709 MEMINFO_SWAP_FREE,
710 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700711 MEMINFO_MAPPED,
712 MEMINFO_VMALLOC_USED,
713 MEMINFO_PAGE_TABLES,
714 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700715 MEMINFO_COUNT
716};
717
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800718static long long get_zram_mem_used()
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800719{
720#define ZRAM_SYSFS "/sys/block/zram0/"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700721 UniqueFile mm_stat_file = MakeUniqueFile(ZRAM_SYSFS "mm_stat", "re");
722 if (mm_stat_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800723 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800724
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700725 int matched = fscanf(mm_stat_file.get(), "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800726 if (matched != 1)
727 ALOGW("failed to parse " ZRAM_SYSFS "mm_stat");
728
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800729 return mem_used_total;
730 }
731
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700732 UniqueFile mem_used_total_file = MakeUniqueFile(ZRAM_SYSFS "mem_used_total", "re");
733 if (mem_used_total_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800734 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800735
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700736 int matched = fscanf(mem_used_total_file.get(), "%lld", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800737 if (matched != 1)
738 ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total");
739
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800740 return mem_used_total;
741 }
742
743 return 0;
744}
745
Dianne Hackborn8e692572013-09-10 19:06:15 -0700746static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
747{
748 char buffer[1024];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200749 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700750
751 if (out == NULL) {
752 jniThrowNullPointerException(env, "out == null");
753 return;
754 }
755
756 int fd = open("/proc/meminfo", O_RDONLY);
757
758 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700759 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700760 return;
761 }
762
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700763 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700764 close(fd);
765
766 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700767 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700768 return;
769 }
770 buffer[len] = 0;
771
772 static const char* const tags[] = {
773 "MemTotal:",
774 "MemFree:",
775 "Buffers:",
776 "Cached:",
777 "Shmem:",
778 "Slab:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700779 "SwapTotal:",
780 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700781 "ZRam:",
782 "Mapped:",
783 "VmallocUsed:",
784 "PageTables:",
785 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700786 NULL
787 };
788 static const int tagsLen[] = {
789 9,
790 8,
791 8,
792 7,
793 6,
794 5,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700795 10,
796 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700797 5,
798 7,
799 12,
800 11,
801 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700802 0
803 };
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700804 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700805
806 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200807 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700808 int i = 0;
809 while (tags[i]) {
810 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
811 p += tagsLen[i];
812 while (*p == ' ') p++;
813 char* num = p;
814 while (*p >= '0' && *p <= '9') p++;
815 if (*p != 0) {
816 *p = 0;
817 p++;
818 }
819 mem[i] = atoll(num);
820 numFound++;
821 break;
822 }
823 i++;
824 }
825 while (*p && *p != '\n') {
826 p++;
827 }
828 if (*p) p++;
829 }
830
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800831 mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200832 // Recompute Vmalloc Used since the value in meminfo
833 // doesn't account for I/O remapping which doesn't use RAM.
834 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700835
Dianne Hackborn8e692572013-09-10 19:06:15 -0700836 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700837 if (maxNum > MEMINFO_COUNT) {
838 maxNum = MEMINFO_COUNT;
839 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700840 jlong* outArray = env->GetLongArrayElements(out, 0);
841 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700842 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700843 outArray[i] = mem[i];
844 }
845 }
846 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847}
848
Martijn Coenen398e24e2015-04-23 21:55:58 +0200849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850static jint read_binder_stat(const char* stat)
851{
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700852 UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
853 if (fp == nullptr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 return -1;
855 }
856
857 char line[1024];
858
859 char compare[128];
860 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 // loop until we have the block that represents this process
863 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700864 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 return -1;
866 }
867 } while (strncmp(compare, line, len));
868
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800869 // 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 -0800870 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700873 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 return -1;
875 }
876 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 // we have the line, now increment the line ptr to the value
879 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700880 jint result = atoi(ptr);
Elliott Hughesc367d482013-10-29 13:12:55 -0700881 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882}
883
884static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
885{
886 return read_binder_stat("bcTRANSACTION");
887}
888
889static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
890{
891 return read_binder_stat("brTRANSACTION");
892}
893
894// these are implemented in android_util_Binder.cpp
895jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
896jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
897jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
898
Andy McFadden06a6b552010-07-13 16:28:09 -0700899
Andy McFadden06a6b552010-07-13 16:28:09 -0700900/* pulled out of bionic */
901extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
902 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize);
903extern "C" void free_malloc_leak_info(uint8_t* info);
904#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
Christopher Ferrise336eea2016-09-28 14:24:41 -0700905
906static size_t gNumBacktraceElements;
Andy McFadden06a6b552010-07-13 16:28:09 -0700907
908/*
909 * This is a qsort() callback.
910 *
911 * See dumpNativeHeap() for comments about the data format and sort order.
912 */
913static int compareHeapRecords(const void* vrec1, const void* vrec2)
914{
915 const size_t* rec1 = (const size_t*) vrec1;
916 const size_t* rec2 = (const size_t*) vrec2;
917 size_t size1 = *rec1;
918 size_t size2 = *rec2;
919
920 if (size1 < size2) {
921 return 1;
922 } else if (size1 > size2) {
923 return -1;
924 }
925
Christopher Ferrise336eea2016-09-28 14:24:41 -0700926 uintptr_t* bt1 = (uintptr_t*)(rec1 + 2);
927 uintptr_t* bt2 = (uintptr_t*)(rec2 + 2);
928 for (size_t idx = 0; idx < gNumBacktraceElements; idx++) {
929 uintptr_t addr1 = bt1[idx];
930 uintptr_t addr2 = bt2[idx];
Andy McFadden06a6b552010-07-13 16:28:09 -0700931 if (addr1 == addr2) {
932 if (addr1 == 0)
933 break;
934 continue;
935 }
936 if (addr1 < addr2) {
937 return -1;
938 } else if (addr1 > addr2) {
939 return 1;
940 }
941 }
942
943 return 0;
944}
945
946/*
947 * The get_malloc_leak_info() call returns an array of structs that
948 * look like this:
949 *
950 * size_t size
951 * size_t allocations
952 * intptr_t backtrace[32]
953 *
954 * "size" is the size of the allocation, "backtrace" is a fixed-size
955 * array of function pointers, and "allocations" is the number of
956 * allocations with the exact same size and backtrace.
957 *
958 * The entries are sorted by descending total size (i.e. size*allocations)
959 * then allocation count. For best results with "diff" we'd like to sort
960 * primarily by individual size then stack trace. Since the entries are
961 * fixed-size, and we're allowed (by the current implementation) to mangle
962 * them, we can do this in place.
963 */
964static void dumpNativeHeap(FILE* fp)
965{
966 uint8_t* info = NULL;
967 size_t overallSize, infoSize, totalMemory, backtraceSize;
968
969 get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory,
970 &backtraceSize);
971 if (info == NULL) {
972 fprintf(fp, "Native heap dump not available. To enable, run these"
973 " commands (requires root):\n");
Christopher Ferrise336eea2016-09-28 14:24:41 -0700974 fprintf(fp, "# adb shell stop\n");
975 fprintf(fp, "# adb shell setprop libc.debug.malloc.options "
976 "backtrace\n");
977 fprintf(fp, "# adb shell start\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700978 return;
979 }
980 assert(infoSize != 0);
981 assert(overallSize % infoSize == 0);
982
983 fprintf(fp, "Android Native Heap Dump v1.0\n\n");
984
985 size_t recordCount = overallSize / infoSize;
986 fprintf(fp, "Total memory: %zu\n", totalMemory);
987 fprintf(fp, "Allocation records: %zd\n", recordCount);
Christopher Ferrise336eea2016-09-28 14:24:41 -0700988 fprintf(fp, "Backtrace size: %zd\n", backtraceSize);
Andy McFadden06a6b552010-07-13 16:28:09 -0700989 fprintf(fp, "\n");
990
991 /* re-sort the entries */
Christopher Ferrise336eea2016-09-28 14:24:41 -0700992 gNumBacktraceElements = backtraceSize;
Andy McFadden06a6b552010-07-13 16:28:09 -0700993 qsort(info, recordCount, infoSize, compareHeapRecords);
994
995 /* dump the entries to the file */
996 const uint8_t* ptr = info;
997 for (size_t idx = 0; idx < recordCount; idx++) {
998 size_t size = *(size_t*) ptr;
999 size_t allocations = *(size_t*) (ptr + sizeof(size_t));
Christopher Ferrise336eea2016-09-28 14:24:41 -07001000 uintptr_t* backtrace = (uintptr_t*) (ptr + sizeof(size_t) * 2);
Andy McFadden06a6b552010-07-13 16:28:09 -07001001
1002 fprintf(fp, "z %d sz %8zu num %4zu bt",
1003 (size & SIZE_FLAG_ZYGOTE_CHILD) != 0,
1004 size & ~SIZE_FLAG_ZYGOTE_CHILD,
1005 allocations);
1006 for (size_t bt = 0; bt < backtraceSize; bt++) {
1007 if (backtrace[bt] == 0) {
1008 break;
1009 } else {
Ashok Bhatf5df7002014-03-25 20:51:35 +00001010#ifdef __LP64__
Mark Salyzyn85394032014-04-16 10:28:37 -07001011 fprintf(fp, " %016" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +00001012#else
Mark Salyzyn85394032014-04-16 10:28:37 -07001013 fprintf(fp, " %08" PRIxPTR, backtrace[bt]);
Ashok Bhatf5df7002014-03-25 20:51:35 +00001014#endif
Andy McFadden06a6b552010-07-13 16:28:09 -07001015 }
1016 }
1017 fprintf(fp, "\n");
1018
1019 ptr += infoSize;
1020 }
1021
Andy McFadden06a6b552010-07-13 16:28:09 -07001022 free_malloc_leak_info(info);
Brian Carlstrom393b84c12010-10-17 23:40:43 -07001023
1024 fprintf(fp, "MAPS\n");
1025 const char* maps = "/proc/self/maps";
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -07001026 UniqueFile in = MakeUniqueFile(maps, "re");
1027 if (in == nullptr) {
Brian Carlstrom393b84c12010-10-17 23:40:43 -07001028 fprintf(fp, "Could not open %s\n", maps);
1029 return;
1030 }
1031 char buf[BUFSIZ];
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -07001032 while (size_t n = fread(buf, sizeof(char), BUFSIZ, in.get())) {
Brian Carlstrom393b84c12010-10-17 23:40:43 -07001033 fwrite(buf, sizeof(char), n, fp);
1034 }
Brian Carlstrom393b84c12010-10-17 23:40:43 -07001035
1036 fprintf(fp, "END\n");
Andy McFadden06a6b552010-07-13 16:28:09 -07001037}
Andy McFadden06a6b552010-07-13 16:28:09 -07001038
Christopher Ferris8d652f82017-04-11 16:29:18 -07001039static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
Andy McFadden06a6b552010-07-13 16:28:09 -07001040{
1041 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001042 jniThrowNullPointerException(env, "fd == null");
Christopher Ferris8d652f82017-04-11 16:29:18 -07001043 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -07001044 }
1045 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
1046 if (origFd < 0) {
1047 jniThrowRuntimeException(env, "Invalid file descriptor");
Christopher Ferris8d652f82017-04-11 16:29:18 -07001048 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -07001049 }
1050
1051 /* dup() the descriptor so we don't close the original with fclose() */
1052 int fd = dup(origFd);
1053 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001054 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -07001055 jniThrowRuntimeException(env, "dup() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -07001056 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -07001057 }
1058
Christopher Ferris8d652f82017-04-11 16:29:18 -07001059 fp.reset(fdopen(fd, "w"));
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -07001060 if (fp == nullptr) {
Steve Block8564c8d2012-01-05 23:22:43 +00001061 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -07001062 close(fd);
1063 jniThrowRuntimeException(env, "fdopen() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -07001064 return false;
1065 }
1066 return true;
1067}
1068
1069/*
1070 * Dump the native heap, writing human-readable output to the specified
1071 * file descriptor.
1072 */
1073static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
1074 jobject fileDescriptor)
1075{
1076 UniqueFile fp(nullptr, safeFclose);
1077 if (!openFile(env, fileDescriptor, fp)) {
Andy McFadden06a6b552010-07-13 16:28:09 -07001078 return;
1079 }
1080
Steve Block5baa3a62011-12-20 16:23:08 +00001081 ALOGD("Native heap dump starting...\n");
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -07001082 dumpNativeHeap(fp.get());
Steve Block5baa3a62011-12-20 16:23:08 +00001083 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -07001084}
1085
Christopher Ferris8d652f82017-04-11 16:29:18 -07001086/*
1087 * Dump the native malloc info, writing xml output to the specified
1088 * file descriptor.
1089 */
1090static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
1091 jobject fileDescriptor)
1092{
1093 UniqueFile fp(nullptr, safeFclose);
1094 if (!openFile(env, fileDescriptor, fp)) {
1095 return;
1096 }
1097
1098 malloc_info(0, fp.get());
1099}
1100
Narayan Kamathf013daa2017-05-09 12:55:02 +01001101static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
1102 DebuggerdDumpType dumpType) {
1103 const ScopedUtfChars fileNameChars(env, fileName);
1104 if (fileNameChars.c_str() == nullptr) {
1105 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001106 }
1107
Narayan Kamathf013daa2017-05-09 12:55:02 +01001108 android::base::unique_fd fd(open(fileNameChars.c_str(),
1109 O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
1110 0666));
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001111 if (fd < 0) {
Narayan Kamathf013daa2017-05-09 12:55:02 +01001112 fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
1113 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001114 }
1115
Narayan Kamathf013daa2017-05-09 12:55:02 +01001116 return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0);
1117}
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001118
Narayan Kamathf013daa2017-05-09 12:55:02 +01001119static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1120 jint pid, jstring fileName, jint timeoutSecs) {
1121 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
1122 return ret ? JNI_TRUE : JNI_FALSE;
1123}
1124
1125static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1126 jint pid, jstring fileName, jint timeoutSecs) {
1127 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
1128 return ret ? JNI_TRUE : JNI_FALSE;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001129}
1130
Colin Crossc4fb5f92016-02-02 16:51:15 -08001131static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
1132 jint limit, jboolean contents)
1133{
1134 std::string s = GetUnreachableMemoryString(contents, limit);
1135 return env->NewStringUTF(s.c_str());
1136}
1137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138/*
1139 * JNI registration.
1140 */
1141
Daniel Micay76f6a862015-09-19 17:31:01 -04001142static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 { "getNativeHeapSize", "()J",
1144 (void*) android_os_Debug_getNativeHeapSize },
1145 { "getNativeHeapAllocatedSize", "()J",
1146 (void*) android_os_Debug_getNativeHeapAllocatedSize },
1147 { "getNativeHeapFreeSize", "()J",
1148 (void*) android_os_Debug_getNativeHeapFreeSize },
1149 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1150 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001151 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1152 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001153 { "getPss", "()J",
1154 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001155 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001156 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001157 { "getMemInfo", "([J)V",
1158 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001159 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1160 (void*) android_os_Debug_dumpNativeHeap },
Christopher Ferris8d652f82017-04-11 16:29:18 -07001161 { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
1162 (void*) android_os_Debug_dumpNativeMallocInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 { "getBinderSentTransactions", "()I",
1164 (void*) android_os_Debug_getBinderSentTransactions },
1165 { "getBinderReceivedTransactions", "()I",
1166 (void*) android_os_getBinderReceivedTransactions },
1167 { "getBinderLocalObjectCount", "()I",
1168 (void*)android_os_Debug_getLocalObjectCount },
1169 { "getBinderProxyObjectCount", "()I",
1170 (void*)android_os_Debug_getProxyObjectCount },
1171 { "getBinderDeathObjectCount", "()I",
1172 (void*)android_os_Debug_getDeathObjectCount },
Narayan Kamathf013daa2017-05-09 12:55:02 +01001173 { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
1174 (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
1175 { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
songjinshie02e3ea2016-12-16 17:48:21 +08001176 (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
Colin Crossc4fb5f92016-02-02 16:51:15 -08001177 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
1178 (void*)android_os_Debug_getUnreachableMemory },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179};
1180
1181int register_android_os_Debug(JNIEnv *env)
1182{
1183 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001185 // Sanity check the number of other statistics expected in Java matches here.
1186 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1187 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001188 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1189 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001190 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001191 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001192 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001193 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1194 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001195 return JNI_ERR;
1196 }
1197
1198 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -08001199 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001200
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001201 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1202 stat_fields[i].pss_field =
1203 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001204 stat_fields[i].pssSwappable_field =
1205 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001206 stat_fields[i].privateDirty_field =
1207 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1208 stat_fields[i].sharedDirty_field =
1209 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001210 stat_fields[i].privateClean_field =
1211 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001212 stat_fields[i].sharedClean_field =
1213 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001214 stat_fields[i].swappedOut_field =
1215 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -08001216 stat_fields[i].swappedOutPss_field =
1217 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001218 }
1219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1221}
1222
Andy McFadden06a6b552010-07-13 16:28:09 -07001223}; // namespace android