blob: 195fe58f0beb134182668cd8c78686f124ce97f3 [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
Colin Crossc4fb5f92016-02-02 16:51:15 -080032#include <iomanip>
33#include <string>
Sandeep Patilbe825412018-12-11 10:09:46 -080034#include <vector>
Colin Crossc4fb5f92016-02-02 16:51:15 -080035
Josh Gao3ce36962017-01-05 18:28:24 -080036#include <debuggerd/client.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070037#include <log/log.h>
38#include <utils/misc.h>
39#include <utils/String8.h>
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070040
Steven Morelandbdd5c422017-08-11 10:57:59 -070041#include <nativehelper/JNIHelp.h>
42#include <nativehelper/ScopedUtfChars.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070043#include "jni.h"
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -080044#include <meminfo/procmeminfo.h>
Sandeep Patilbe825412018-12-11 10:09:46 -080045#include <meminfo/sysmeminfo.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070046#include <memtrack/memtrack.h>
47#include <memunreachable/memunreachable.h>
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -080048#include <android-base/strings.h>
Daniel Colascione6c518102017-07-27 03:33:34 -070049#include "android_os_Debug.h"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051namespace android
52{
53
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070054enum {
55 HEAP_UNKNOWN,
56 HEAP_DALVIK,
57 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080058
Dianne Hackborn64770d12013-05-23 17:51:19 -070059 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080060 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070061 HEAP_CURSOR,
62 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080063 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070064 HEAP_UNKNOWN_DEV,
65 HEAP_SO,
66 HEAP_JAR,
67 HEAP_APK,
68 HEAP_TTF,
69 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070070 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070071 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070072 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070073 HEAP_GRAPHICS,
74 HEAP_GL,
75 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070076
Mathieu Chartier95550dd2017-07-13 15:01:34 -070077 // Dalvik extra sections (heap).
Anwar Ghuloum3c615062013-05-13 14:18:02 -070078 HEAP_DALVIK_NORMAL,
79 HEAP_DALVIK_LARGE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080080 HEAP_DALVIK_ZYGOTE,
81 HEAP_DALVIK_NON_MOVING,
Mathieu Chartier95550dd2017-07-13 15:01:34 -070082
83 // Dalvik other extra sections.
84 HEAP_DALVIK_OTHER_LINEARALLOC,
85 HEAP_DALVIK_OTHER_ACCOUNTING,
86 HEAP_DALVIK_OTHER_CODE_CACHE,
87 HEAP_DALVIK_OTHER_COMPILER_METADATA,
88 HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE,
89
90 // Boot vdex / app dex / app vdex
91 HEAP_DEX_BOOT_VDEX,
92 HEAP_DEX_APP_DEX,
93 HEAP_DEX_APP_VDEX,
94
95 // App art, boot art.
96 HEAP_ART_APP,
97 HEAP_ART_BOOT,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070098
99 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700100 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700101 _NUM_CORE_HEAP = HEAP_NATIVE+1
102};
103
104struct stat_fields {
105 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700106 jfieldID pssSwappable_field;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800107 jfieldID rss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700108 jfieldID privateDirty_field;
109 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700110 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700111 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700112 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800113 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700114};
115
116struct stat_field_names {
117 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700118 const char* pssSwappable_name;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800119 const char* rss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700120 const char* privateDirty_name;
121 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700122 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700123 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700124 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800125 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700126};
127
128static stat_fields stat_fields[_NUM_CORE_HEAP];
129
130static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800131 { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800132 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800133 { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800134 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800135 { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800136 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700137};
138
139jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800140jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
142struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700143 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700144 int swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800145 int rss;
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
154#define BINDER_STATS "/proc/binder/stats"
155
156static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
157{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 struct mallinfo info = mallinfo();
159 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160}
161
162static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
163{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 struct mallinfo info = mallinfo();
165 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166}
167
168static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
169{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 struct mallinfo info = mallinfo();
171 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172}
173
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700174// Container used to retrieve graphics memory pss
175struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700176{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700177 int graphics;
178 int gl;
179 int other;
180};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700181
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700182/*
183 * Uses libmemtrack to retrieve graphics memory that the process is using.
184 * Any graphics memory reported in /proc/pid/smaps is not included here.
185 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700186static int read_memtrack_memory(struct memtrack_proc* p, int pid,
187 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700188{
189 int err = memtrack_proc_get(p, pid);
190 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700191 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700192 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700193 }
194
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700195 ssize_t pss = memtrack_proc_graphics_pss(p);
196 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700197 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700198 return pss;
199 }
200 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700201
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700202 pss = memtrack_proc_gl_pss(p);
203 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700204 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700205 return pss;
206 }
207 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700208
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700209 pss = memtrack_proc_other_pss(p);
210 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700211 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700212 return pss;
213 }
214 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700215
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700216 return 0;
217}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700218
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700219/*
220 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
221 */
222static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
223{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700224 struct memtrack_proc* p = memtrack_proc_new();
225 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700226 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700227 return -1;
228 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700229
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700230 int err = read_memtrack_memory(p, pid, graphics_mem);
231 memtrack_proc_destroy(p);
232 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700233}
234
Martijn Coenene0764852016-01-07 17:04:22 -0800235static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236{
Andreas Gampece711492016-10-04 18:06:42 -0700237 *foundSwapPss = false;
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800238 uint64_t prev_end = 0;
239 int prev_heap = HEAP_UNKNOWN;
Andreas Gampece711492016-10-04 18:06:42 -0700240
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700241 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800242 auto vma_scan = [&](const meminfo::Vma& vma) {
243 int which_heap = HEAP_UNKNOWN;
244 int sub_heap = HEAP_UNKNOWN;
245 bool is_swappable = false;
246 std::string name;
247 if (base::EndsWith(vma.name, " (deleted)")) {
248 name = vma.name.substr(0, vma.name.size() - strlen(" (deleted)"));
249 } else {
250 name = vma.name;
251 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800253 uint32_t namesz = name.size();
254 if (base::StartsWith(name, "[heap]")) {
255 which_heap = HEAP_NATIVE;
256 } else if (base::StartsWith(name, "[anon:libc_malloc]")) {
257 which_heap = HEAP_NATIVE;
258 } else if (base::StartsWith(name, "[stack")) {
259 which_heap = HEAP_NATIVE;
260 } else if (base::EndsWith(name, ".so")) {
261 which_heap = HEAP_SO;
262 is_swappable = true;
263 } else if (base::EndsWith(name, ".jar")) {
264 which_heap = HEAP_JAR;
265 is_swappable = true;
266 } else if (base::EndsWith(name, ".apk")) {
267 which_heap = HEAP_APK;
268 is_swappable = true;
269 } else if (base::EndsWith(name, ".ttf")) {
270 which_heap = HEAP_TTF;
271 is_swappable = true;
272 } else if ((base::EndsWith(name, ".odex")) ||
273 (namesz > 4 && strstr(name.c_str(), ".dex") != nullptr)) {
274 which_heap = HEAP_DEX;
275 sub_heap = HEAP_DEX_APP_DEX;
276 is_swappable = true;
277 } else if (base::EndsWith(name, ".vdex")) {
278 which_heap = HEAP_DEX;
279 // Handle system@framework@boot and system/framework/boot
280 if ((strstr(name.c_str(), "@boot") != nullptr) ||
281 (strstr(name.c_str(), "/boot"))) {
282 sub_heap = HEAP_DEX_BOOT_VDEX;
283 } else {
284 sub_heap = HEAP_DEX_APP_VDEX;
285 }
286 is_swappable = true;
287 } else if (base::EndsWith(name, ".oat")) {
288 which_heap = HEAP_OAT;
289 is_swappable = true;
290 } else if (base::EndsWith(name, ".art")) {
291 which_heap = HEAP_ART;
292 // Handle system@framework@boot* and system/framework/boot*
293 if ((strstr(name.c_str(), "@boot") != nullptr) ||
294 (strstr(name.c_str(), "/boot"))) {
295 sub_heap = HEAP_DEX_BOOT_VDEX;
296 } else {
297 sub_heap = HEAP_DEX_APP_VDEX;
298 }
299 is_swappable = true;
300 } else if (base::StartsWith(name, "/dev/")) {
301 which_heap = HEAP_UNKNOWN_DEV;
302 if (base::StartsWith(name, "/dev/kgsl-3d0")) {
303 which_heap = HEAP_GL_DEV;
304 } else if (base::StartsWith(name, "/dev/ashmem/CursorWindow")) {
305 which_heap = HEAP_CURSOR;
306 } else if (base::StartsWith(name, "/dev/ashmem")) {
307 which_heap = HEAP_ASHMEM;
308 }
309 } else if (base::StartsWith(name, "[anon:")) {
310 which_heap = HEAP_UNKNOWN;
311 if (base::StartsWith(name, "[anon:dalvik-")) {
312 which_heap = HEAP_DALVIK_OTHER;
313 if (base::StartsWith(name, "[anon:dalvik-LinearAlloc")) {
314 sub_heap = HEAP_DALVIK_OTHER_LINEARALLOC;
315 } else if (base::StartsWith(name, "[anon:dalvik-alloc space") ||
316 base::StartsWith(name, "[anon:dalvik-main space")) {
317 // This is the regular Dalvik heap.
318 which_heap = HEAP_DALVIK;
319 sub_heap = HEAP_DALVIK_NORMAL;
320 } else if (base::StartsWith(name,
321 "[anon:dalvik-large object space") ||
322 base::StartsWith(
323 name, "[anon:dalvik-free list large object space")) {
324 which_heap = HEAP_DALVIK;
325 sub_heap = HEAP_DALVIK_LARGE;
326 } else if (base::StartsWith(name, "[anon:dalvik-non moving space")) {
327 which_heap = HEAP_DALVIK;
328 sub_heap = HEAP_DALVIK_NON_MOVING;
329 } else if (base::StartsWith(name, "[anon:dalvik-zygote space")) {
330 which_heap = HEAP_DALVIK;
331 sub_heap = HEAP_DALVIK_ZYGOTE;
332 } else if (base::StartsWith(name, "[anon:dalvik-indirect ref")) {
333 sub_heap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
334 } else if (base::StartsWith(name, "[anon:dalvik-jit-code-cache") ||
335 base::StartsWith(name, "[anon:dalvik-data-code-cache")) {
336 sub_heap = HEAP_DALVIK_OTHER_CODE_CACHE;
337 } else if (base::StartsWith(name, "[anon:dalvik-CompilerMetadata")) {
338 sub_heap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
339 } else {
340 sub_heap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting.
341 }
342 }
343 } else if (namesz > 0) {
344 which_heap = HEAP_UNKNOWN_MAP;
345 } else if (vma.start == prev_end && prev_heap == HEAP_SO) {
346 // bss section of a shared library
347 which_heap = HEAP_SO;
348 }
349
350 prev_end = vma.end;
351 prev_heap = which_heap;
352
353 const meminfo::MemUsage& usage = vma.usage;
354 if (usage.swap_pss > 0 && *foundSwapPss != true) {
355 *foundSwapPss = true;
356 }
357
358 uint64_t swapable_pss = 0;
359 if (is_swappable && (usage.pss > 0)) {
360 float sharing_proportion = 0.0;
361 if ((usage.shared_clean > 0) || (usage.shared_dirty > 0)) {
362 sharing_proportion = (usage.pss - usage.uss) / (usage.shared_clean + usage.shared_dirty);
363 }
364 swapable_pss = (sharing_proportion * usage.shared_clean) + usage.private_clean;
365 }
366
367 stats[which_heap].pss += usage.pss;
368 stats[which_heap].swappablePss += swapable_pss;
369 stats[which_heap].rss += usage.rss;
370 stats[which_heap].privateDirty += usage.private_dirty;
371 stats[which_heap].sharedDirty += usage.shared_dirty;
372 stats[which_heap].privateClean += usage.private_clean;
373 stats[which_heap].sharedClean += usage.shared_clean;
374 stats[which_heap].swappedOut += usage.swap;
375 stats[which_heap].swappedOutPss += usage.swap_pss;
376 if (which_heap == HEAP_DALVIK || which_heap == HEAP_DALVIK_OTHER ||
377 which_heap == HEAP_DEX || which_heap == HEAP_ART) {
378 stats[sub_heap].pss += usage.pss;
379 stats[sub_heap].swappablePss += swapable_pss;
380 stats[sub_heap].rss += usage.rss;
381 stats[sub_heap].privateDirty += usage.private_dirty;
382 stats[sub_heap].sharedDirty += usage.shared_dirty;
383 stats[sub_heap].privateClean += usage.private_clean;
384 stats[sub_heap].sharedClean += usage.shared_clean;
385 stats[sub_heap].swappedOut += usage.swap;
386 stats[sub_heap].swappedOutPss += usage.swap_pss;
387 }
388 };
389
390 meminfo::ForEachVmaFromFile(smaps_path, vma_scan);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391}
392
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700393static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
394 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395{
Martijn Coenene0764852016-01-07 17:04:22 -0800396 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700397 stats_t stats[_NUM_HEAP];
398 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800399
Martijn Coenene0764852016-01-07 17:04:22 -0800400 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700402 struct graphics_memory_pss graphics_mem;
403 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
404 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
405 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800406 stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700407 stats[HEAP_GL].pss = graphics_mem.gl;
408 stats[HEAP_GL].privateDirty = graphics_mem.gl;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800409 stats[HEAP_GL].rss = graphics_mem.gl;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700410 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
411 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800412 stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700413 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700414
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700415 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700416 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700417 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800418 stats[HEAP_UNKNOWN].rss += stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700419 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
420 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700421 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700422 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700423 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800424 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700425 }
426
427 for (int i=0; i<_NUM_CORE_HEAP; i++) {
428 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700429 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800430 env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700431 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
432 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700433 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700434 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700435 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800436 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700437 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800438
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700439
Martijn Coenene0764852016-01-07 17:04:22 -0800440 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700441 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800442
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700443 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
444 if (otherArray == NULL) {
445 return;
446 }
447
448 int j=0;
449 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
450 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700451 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800452 otherArray[j++] = stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700453 otherArray[j++] = stats[i].privateDirty;
454 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700455 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700456 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700457 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800458 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700459 }
460
461 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462}
463
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700464static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
465{
466 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
467}
468
Martijn Coenene0764852016-01-07 17:04:22 -0800469static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
Dianne Hackborne17b4452018-01-10 13:15:40 -0800470 jlongArray outUssSwapPssRss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700471{
Dianne Hackbornb437e092011-08-05 17:50:29 -0700472 jlong pss = 0;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800473 jlong rss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800474 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700475 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800476 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700477
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700478 struct graphics_memory_pss graphics_mem;
479 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700480 pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700481 }
482
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -0800483 ::android::meminfo::ProcMemInfo proc_mem(pid);
484 ::android::meminfo::MemUsage stats;
485 if (proc_mem.SmapsOrRollup(&stats)) {
486 pss += stats.pss;
487 uss += stats.uss;
488 rss += stats.rss;
489 swapPss = stats.swap_pss;
490 pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP
Dianne Hackborn37c99432013-09-05 19:34:57 -0700491 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700492
Dianne Hackborne17b4452018-01-10 13:15:40 -0800493 if (outUssSwapPssRss != NULL) {
494 if (env->GetArrayLength(outUssSwapPssRss) >= 1) {
495 jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
496 if (outUssSwapPssRssArray != NULL) {
497 outUssSwapPssRssArray[0] = uss;
498 if (env->GetArrayLength(outUssSwapPssRss) >= 2) {
499 outUssSwapPssRssArray[1] = swapPss;
500 }
501 if (env->GetArrayLength(outUssSwapPssRss) >= 3) {
502 outUssSwapPssRssArray[2] = rss;
Martijn Coenene0764852016-01-07 17:04:22 -0800503 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700504 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800505 env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700506 }
507 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700508
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800509 if (outMemtrack != NULL) {
510 if (env->GetArrayLength(outMemtrack) >= 1) {
511 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
512 if (outMemtrackArray != NULL) {
513 outMemtrackArray[0] = memtrack;
514 }
515 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
516 }
517 }
518
Dianne Hackbornb437e092011-08-05 17:50:29 -0700519 return pss;
520}
521
522static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
523{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800524 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700525}
526
Sandeep Patilbe825412018-12-11 10:09:46 -0800527// The 1:1 mapping of MEMINFO_* enums here must match with the constants from
528// Debug.java.
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700529enum {
530 MEMINFO_TOTAL,
531 MEMINFO_FREE,
532 MEMINFO_BUFFERS,
533 MEMINFO_CACHED,
534 MEMINFO_SHMEM,
535 MEMINFO_SLAB,
Robert Benea5e099802017-10-04 18:28:01 -0700536 MEMINFO_SLAB_RECLAIMABLE,
537 MEMINFO_SLAB_UNRECLAIMABLE,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700538 MEMINFO_SWAP_TOTAL,
539 MEMINFO_SWAP_FREE,
540 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700541 MEMINFO_MAPPED,
542 MEMINFO_VMALLOC_USED,
543 MEMINFO_PAGE_TABLES,
544 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700545 MEMINFO_COUNT
546};
547
Dianne Hackborn8e692572013-09-10 19:06:15 -0700548static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
549{
Dianne Hackborn8e692572013-09-10 19:06:15 -0700550 if (out == NULL) {
551 jniThrowNullPointerException(env, "out == null");
552 return;
553 }
554
Sandeep Patilbe825412018-12-11 10:09:46 -0800555 int outLen = env->GetArrayLength(out);
556 if (outLen < MEMINFO_COUNT) {
557 jniThrowRuntimeException(env, "outLen < MEMINFO_COUNT");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700558 return;
559 }
560
Sandeep Patilbe825412018-12-11 10:09:46 -0800561 // Read system memory info including ZRAM. The values are stored in the vector
562 // in the same order as MEMINFO_* enum
563 std::vector<uint64_t> mem(MEMINFO_COUNT);
564 std::vector<std::string> tags(::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags);
565 tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:");
566 ::android::meminfo::SysMemInfo smi;
567 if (!smi.ReadMemInfo(tags, &mem)) {
568 jniThrowRuntimeException(env, "SysMemInfo read failed");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700569 return;
570 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700571
Dianne Hackborn8e692572013-09-10 19:06:15 -0700572 jlong* outArray = env->GetLongArrayElements(out, 0);
573 if (outArray != NULL) {
Sandeep Patilbe825412018-12-11 10:09:46 -0800574 outLen = MEMINFO_COUNT;
575 for (int i = 0; i < outLen; i++) {
Sandeep Patilbe825412018-12-11 10:09:46 -0800576 if (i == MEMINFO_VMALLOC_USED) {
Sandeep Patil2481ae02019-01-13 20:33:02 -0800577 outArray[i] = smi.ReadVmallocInfo() / 1024;
Sandeep Patilbe825412018-12-11 10:09:46 -0800578 continue;
579 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700580 outArray[i] = mem[i];
581 }
582 }
Sandeep Patilbe825412018-12-11 10:09:46 -0800583
Dianne Hackborn8e692572013-09-10 19:06:15 -0700584 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585}
586
587static jint read_binder_stat(const char* stat)
588{
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700589 UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
590 if (fp == nullptr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 return -1;
592 }
593
594 char line[1024];
595
596 char compare[128];
597 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 // loop until we have the block that represents this process
600 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700601 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 return -1;
603 }
604 } while (strncmp(compare, line, len));
605
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800606 // 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 -0800607 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700610 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 return -1;
612 }
613 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 // we have the line, now increment the line ptr to the value
616 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700617 jint result = atoi(ptr);
Elliott Hughesc367d482013-10-29 13:12:55 -0700618 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619}
620
621static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
622{
623 return read_binder_stat("bcTRANSACTION");
624}
625
626static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
627{
628 return read_binder_stat("brTRANSACTION");
629}
630
631// these are implemented in android_util_Binder.cpp
632jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
633jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
634jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
635
Christopher Ferris8d652f82017-04-11 16:29:18 -0700636static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
Andy McFadden06a6b552010-07-13 16:28:09 -0700637{
638 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800639 jniThrowNullPointerException(env, "fd == null");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700640 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700641 }
642 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
643 if (origFd < 0) {
644 jniThrowRuntimeException(env, "Invalid file descriptor");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700645 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700646 }
647
648 /* dup() the descriptor so we don't close the original with fclose() */
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800649 int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0);
Andy McFadden06a6b552010-07-13 16:28:09 -0700650 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000651 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700652 jniThrowRuntimeException(env, "dup() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700653 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700654 }
655
Christopher Ferris8d652f82017-04-11 16:29:18 -0700656 fp.reset(fdopen(fd, "w"));
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700657 if (fp == nullptr) {
Steve Block8564c8d2012-01-05 23:22:43 +0000658 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700659 close(fd);
660 jniThrowRuntimeException(env, "fdopen() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700661 return false;
662 }
663 return true;
664}
665
Christopher Ferris38e2c3b2018-06-13 13:19:57 -0700666/* pulled out of bionic */
667extern "C" void write_malloc_leak_info(FILE* fp);
668
Christopher Ferris8d652f82017-04-11 16:29:18 -0700669/*
670 * Dump the native heap, writing human-readable output to the specified
671 * file descriptor.
672 */
673static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
674 jobject fileDescriptor)
675{
676 UniqueFile fp(nullptr, safeFclose);
677 if (!openFile(env, fileDescriptor, fp)) {
Andy McFadden06a6b552010-07-13 16:28:09 -0700678 return;
679 }
680
Steve Block5baa3a62011-12-20 16:23:08 +0000681 ALOGD("Native heap dump starting...\n");
Christopher Ferris38e2c3b2018-06-13 13:19:57 -0700682 // Formatting of the native heap dump is handled by malloc debug itself.
683 // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
684 write_malloc_leak_info(fp.get());
Steve Block5baa3a62011-12-20 16:23:08 +0000685 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700686}
687
Christopher Ferris8d652f82017-04-11 16:29:18 -0700688/*
689 * Dump the native malloc info, writing xml output to the specified
690 * file descriptor.
691 */
692static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
693 jobject fileDescriptor)
694{
695 UniqueFile fp(nullptr, safeFclose);
696 if (!openFile(env, fileDescriptor, fp)) {
697 return;
698 }
699
700 malloc_info(0, fp.get());
701}
702
Narayan Kamathf013daa2017-05-09 12:55:02 +0100703static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
704 DebuggerdDumpType dumpType) {
705 const ScopedUtfChars fileNameChars(env, fileName);
706 if (fileNameChars.c_str() == nullptr) {
707 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700708 }
709
Narayan Kamathf013daa2017-05-09 12:55:02 +0100710 android::base::unique_fd fd(open(fileNameChars.c_str(),
711 O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
712 0666));
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700713 if (fd < 0) {
Narayan Kamathf013daa2017-05-09 12:55:02 +0100714 fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
715 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700716 }
717
Narayan Kamathf013daa2017-05-09 12:55:02 +0100718 return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0);
719}
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700720
Narayan Kamathf013daa2017-05-09 12:55:02 +0100721static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
722 jint pid, jstring fileName, jint timeoutSecs) {
723 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
724 return ret ? JNI_TRUE : JNI_FALSE;
725}
726
727static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
728 jint pid, jstring fileName, jint timeoutSecs) {
729 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
730 return ret ? JNI_TRUE : JNI_FALSE;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700731}
732
Colin Crossc4fb5f92016-02-02 16:51:15 -0800733static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
734 jint limit, jboolean contents)
735{
736 std::string s = GetUnreachableMemoryString(contents, limit);
737 return env->NewStringUTF(s.c_str());
738}
739
Ben Murdochfdc55932019-01-30 10:43:15 +0000740static jlong android_os_Debug_getFreeZramKb(JNIEnv* env, jobject clazz) {
741
742 jlong zramFreeKb = 0;
743
744 std::string status_path = android::base::StringPrintf("/proc/meminfo");
745 UniqueFile file = MakeUniqueFile(status_path.c_str(), "re");
746
747 char line[256];
748 while (file != nullptr && fgets(line, sizeof(line), file.get())) {
749 jlong v;
750 if (sscanf(line, "SwapFree: %" SCNd64 " kB", &v) == 1) {
751 zramFreeKb = v;
752 break;
753 }
754 }
755
756 return zramFreeKb;
757}
758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759/*
760 * JNI registration.
761 */
762
Daniel Micay76f6a862015-09-19 17:31:01 -0400763static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 { "getNativeHeapSize", "()J",
765 (void*) android_os_Debug_getNativeHeapSize },
766 { "getNativeHeapAllocatedSize", "()J",
767 (void*) android_os_Debug_getNativeHeapAllocatedSize },
768 { "getNativeHeapFreeSize", "()J",
769 (void*) android_os_Debug_getNativeHeapFreeSize },
770 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
771 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700772 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
773 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700774 { "getPss", "()J",
775 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800776 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -0700777 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -0700778 { "getMemInfo", "([J)V",
779 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -0700780 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
781 (void*) android_os_Debug_dumpNativeHeap },
Christopher Ferris8d652f82017-04-11 16:29:18 -0700782 { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
783 (void*) android_os_Debug_dumpNativeMallocInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 { "getBinderSentTransactions", "()I",
785 (void*) android_os_Debug_getBinderSentTransactions },
786 { "getBinderReceivedTransactions", "()I",
787 (void*) android_os_getBinderReceivedTransactions },
788 { "getBinderLocalObjectCount", "()I",
789 (void*)android_os_Debug_getLocalObjectCount },
790 { "getBinderProxyObjectCount", "()I",
791 (void*)android_os_Debug_getProxyObjectCount },
792 { "getBinderDeathObjectCount", "()I",
793 (void*)android_os_Debug_getDeathObjectCount },
Narayan Kamathf013daa2017-05-09 12:55:02 +0100794 { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
795 (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
796 { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
songjinshie02e3ea2016-12-16 17:48:21 +0800797 (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
Colin Crossc4fb5f92016-02-02 16:51:15 -0800798 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
799 (void*)android_os_Debug_getUnreachableMemory },
Ben Murdochfdc55932019-01-30 10:43:15 +0000800 { "getZramFreeKb", "()J",
801 (void*)android_os_Debug_getFreeZramKb },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802};
803
804int register_android_os_Debug(JNIEnv *env)
805{
806 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800808 // Sanity check the number of other statistics expected in Java matches here.
809 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
810 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700811 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
812 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800813 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700814 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800815 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700816 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
817 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800818 return JNI_ERR;
819 }
820
821 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -0800822 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800823
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700824 for (int i=0; i<_NUM_CORE_HEAP; i++) {
825 stat_fields[i].pss_field =
826 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700827 stat_fields[i].pssSwappable_field =
828 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborne17b4452018-01-10 13:15:40 -0800829 stat_fields[i].rss_field =
830 env->GetFieldID(clazz, stat_field_names[i].rss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700831 stat_fields[i].privateDirty_field =
832 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
833 stat_fields[i].sharedDirty_field =
834 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700835 stat_fields[i].privateClean_field =
836 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700837 stat_fields[i].sharedClean_field =
838 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700839 stat_fields[i].swappedOut_field =
840 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -0800841 stat_fields[i].swappedOutPss_field =
842 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700843 }
844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
846}
847
Andy McFadden06a6b552010-07-13 16:28:09 -0700848}; // namespace android