blob: 69a7c4d805328b39a9bd222f44527a0b05c0b433 [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
Christopher Ferris4740d542019-04-16 11:34:58 -070036#include <android-base/logging.h>
37#include <bionic_malloc.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"
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -080046#include <meminfo/procmeminfo.h>
Sandeep Patilbe825412018-12-11 10:09:46 -080047#include <meminfo/sysmeminfo.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070048#include <memtrack/memtrack.h>
49#include <memunreachable/memunreachable.h>
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -080050#include <android-base/strings.h>
Daniel Colascione6c518102017-07-27 03:33:34 -070051#include "android_os_Debug.h"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053namespace android
54{
55
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070056enum {
57 HEAP_UNKNOWN,
58 HEAP_DALVIK,
59 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080060
Dianne Hackborn64770d12013-05-23 17:51:19 -070061 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080062 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070063 HEAP_CURSOR,
64 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080065 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070066 HEAP_UNKNOWN_DEV,
67 HEAP_SO,
68 HEAP_JAR,
69 HEAP_APK,
70 HEAP_TTF,
71 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070072 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070073 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070074 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070075 HEAP_GRAPHICS,
76 HEAP_GL,
77 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070078
Mathieu Chartier95550dd2017-07-13 15:01:34 -070079 // Dalvik extra sections (heap).
Anwar Ghuloum3c615062013-05-13 14:18:02 -070080 HEAP_DALVIK_NORMAL,
81 HEAP_DALVIK_LARGE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080082 HEAP_DALVIK_ZYGOTE,
83 HEAP_DALVIK_NON_MOVING,
Mathieu Chartier95550dd2017-07-13 15:01:34 -070084
85 // Dalvik other extra sections.
86 HEAP_DALVIK_OTHER_LINEARALLOC,
87 HEAP_DALVIK_OTHER_ACCOUNTING,
88 HEAP_DALVIK_OTHER_CODE_CACHE,
89 HEAP_DALVIK_OTHER_COMPILER_METADATA,
90 HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE,
91
92 // Boot vdex / app dex / app vdex
93 HEAP_DEX_BOOT_VDEX,
94 HEAP_DEX_APP_DEX,
95 HEAP_DEX_APP_VDEX,
96
97 // App art, boot art.
98 HEAP_ART_APP,
99 HEAP_ART_BOOT,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700100
101 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700102 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700103 _NUM_CORE_HEAP = HEAP_NATIVE+1
104};
105
106struct stat_fields {
107 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700108 jfieldID pssSwappable_field;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800109 jfieldID rss_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 Hackborne17b4452018-01-10 13:15:40 -0800121 const char* rss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700122 const char* privateDirty_name;
123 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700124 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700125 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700126 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800127 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700128};
129
130static stat_fields stat_fields[_NUM_CORE_HEAP];
131
132static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800133 { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800134 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800135 { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800136 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800137 { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800138 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700139};
140
141jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800142jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
144struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700145 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700146 int swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800147 int rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700148 int privateDirty;
149 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700150 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700151 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700152 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800153 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154};
155
156#define BINDER_STATS "/proc/binder/stats"
157
158static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
159{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 struct mallinfo info = mallinfo();
161 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162}
163
164static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
165{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 struct mallinfo info = mallinfo();
167 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168}
169
170static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
171{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 struct mallinfo info = mallinfo();
173 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174}
175
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700176// Container used to retrieve graphics memory pss
177struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700178{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700179 int graphics;
180 int gl;
181 int other;
182};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700183
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700184/*
185 * Uses libmemtrack to retrieve graphics memory that the process is using.
186 * Any graphics memory reported in /proc/pid/smaps is not included here.
187 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700188static int read_memtrack_memory(struct memtrack_proc* p, int pid,
189 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700190{
191 int err = memtrack_proc_get(p, pid);
192 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700193 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700194 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700195 }
196
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700197 ssize_t pss = memtrack_proc_graphics_pss(p);
198 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700199 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700200 return pss;
201 }
202 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700203
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700204 pss = memtrack_proc_gl_pss(p);
205 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700206 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700207 return pss;
208 }
209 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700210
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700211 pss = memtrack_proc_other_pss(p);
212 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700213 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700214 return pss;
215 }
216 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700217
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700218 return 0;
219}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700220
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700221/*
222 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
223 */
224static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
225{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700226 struct memtrack_proc* p = memtrack_proc_new();
227 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700228 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700229 return -1;
230 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700231
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700232 int err = read_memtrack_memory(p, pid, graphics_mem);
233 memtrack_proc_destroy(p);
234 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700235}
236
Martijn Coenene0764852016-01-07 17:04:22 -0800237static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238{
Andreas Gampece711492016-10-04 18:06:42 -0700239 *foundSwapPss = false;
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800240 uint64_t prev_end = 0;
241 int prev_heap = HEAP_UNKNOWN;
Andreas Gampece711492016-10-04 18:06:42 -0700242
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700243 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800244 auto vma_scan = [&](const meminfo::Vma& vma) {
245 int which_heap = HEAP_UNKNOWN;
246 int sub_heap = HEAP_UNKNOWN;
247 bool is_swappable = false;
248 std::string name;
249 if (base::EndsWith(vma.name, " (deleted)")) {
250 name = vma.name.substr(0, vma.name.size() - strlen(" (deleted)"));
251 } else {
252 name = vma.name;
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Sandeep Patilcb4e8ee2019-01-21 12:00:59 -0800255 uint32_t namesz = name.size();
256 if (base::StartsWith(name, "[heap]")) {
257 which_heap = HEAP_NATIVE;
258 } else if (base::StartsWith(name, "[anon:libc_malloc]")) {
259 which_heap = HEAP_NATIVE;
260 } else if (base::StartsWith(name, "[stack")) {
261 which_heap = HEAP_NATIVE;
262 } else if (base::EndsWith(name, ".so")) {
263 which_heap = HEAP_SO;
264 is_swappable = true;
265 } else if (base::EndsWith(name, ".jar")) {
266 which_heap = HEAP_JAR;
267 is_swappable = true;
268 } else if (base::EndsWith(name, ".apk")) {
269 which_heap = HEAP_APK;
270 is_swappable = true;
271 } else if (base::EndsWith(name, ".ttf")) {
272 which_heap = HEAP_TTF;
273 is_swappable = true;
274 } else if ((base::EndsWith(name, ".odex")) ||
275 (namesz > 4 && strstr(name.c_str(), ".dex") != nullptr)) {
276 which_heap = HEAP_DEX;
277 sub_heap = HEAP_DEX_APP_DEX;
278 is_swappable = true;
279 } else if (base::EndsWith(name, ".vdex")) {
280 which_heap = HEAP_DEX;
281 // Handle system@framework@boot and system/framework/boot
282 if ((strstr(name.c_str(), "@boot") != nullptr) ||
283 (strstr(name.c_str(), "/boot"))) {
284 sub_heap = HEAP_DEX_BOOT_VDEX;
285 } else {
286 sub_heap = HEAP_DEX_APP_VDEX;
287 }
288 is_swappable = true;
289 } else if (base::EndsWith(name, ".oat")) {
290 which_heap = HEAP_OAT;
291 is_swappable = true;
292 } else if (base::EndsWith(name, ".art")) {
293 which_heap = HEAP_ART;
294 // Handle system@framework@boot* and system/framework/boot*
295 if ((strstr(name.c_str(), "@boot") != nullptr) ||
296 (strstr(name.c_str(), "/boot"))) {
297 sub_heap = HEAP_DEX_BOOT_VDEX;
298 } else {
299 sub_heap = HEAP_DEX_APP_VDEX;
300 }
301 is_swappable = true;
302 } else if (base::StartsWith(name, "/dev/")) {
303 which_heap = HEAP_UNKNOWN_DEV;
304 if (base::StartsWith(name, "/dev/kgsl-3d0")) {
305 which_heap = HEAP_GL_DEV;
306 } else if (base::StartsWith(name, "/dev/ashmem/CursorWindow")) {
307 which_heap = HEAP_CURSOR;
308 } else if (base::StartsWith(name, "/dev/ashmem")) {
309 which_heap = HEAP_ASHMEM;
310 }
311 } else if (base::StartsWith(name, "[anon:")) {
312 which_heap = HEAP_UNKNOWN;
313 if (base::StartsWith(name, "[anon:dalvik-")) {
314 which_heap = HEAP_DALVIK_OTHER;
315 if (base::StartsWith(name, "[anon:dalvik-LinearAlloc")) {
316 sub_heap = HEAP_DALVIK_OTHER_LINEARALLOC;
317 } else if (base::StartsWith(name, "[anon:dalvik-alloc space") ||
318 base::StartsWith(name, "[anon:dalvik-main space")) {
319 // This is the regular Dalvik heap.
320 which_heap = HEAP_DALVIK;
321 sub_heap = HEAP_DALVIK_NORMAL;
322 } else if (base::StartsWith(name,
323 "[anon:dalvik-large object space") ||
324 base::StartsWith(
325 name, "[anon:dalvik-free list large object space")) {
326 which_heap = HEAP_DALVIK;
327 sub_heap = HEAP_DALVIK_LARGE;
328 } else if (base::StartsWith(name, "[anon:dalvik-non moving space")) {
329 which_heap = HEAP_DALVIK;
330 sub_heap = HEAP_DALVIK_NON_MOVING;
331 } else if (base::StartsWith(name, "[anon:dalvik-zygote space")) {
332 which_heap = HEAP_DALVIK;
333 sub_heap = HEAP_DALVIK_ZYGOTE;
334 } else if (base::StartsWith(name, "[anon:dalvik-indirect ref")) {
335 sub_heap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
336 } else if (base::StartsWith(name, "[anon:dalvik-jit-code-cache") ||
337 base::StartsWith(name, "[anon:dalvik-data-code-cache")) {
338 sub_heap = HEAP_DALVIK_OTHER_CODE_CACHE;
339 } else if (base::StartsWith(name, "[anon:dalvik-CompilerMetadata")) {
340 sub_heap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
341 } else {
342 sub_heap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting.
343 }
344 }
345 } else if (namesz > 0) {
346 which_heap = HEAP_UNKNOWN_MAP;
347 } else if (vma.start == prev_end && prev_heap == HEAP_SO) {
348 // bss section of a shared library
349 which_heap = HEAP_SO;
350 }
351
352 prev_end = vma.end;
353 prev_heap = which_heap;
354
355 const meminfo::MemUsage& usage = vma.usage;
356 if (usage.swap_pss > 0 && *foundSwapPss != true) {
357 *foundSwapPss = true;
358 }
359
360 uint64_t swapable_pss = 0;
361 if (is_swappable && (usage.pss > 0)) {
362 float sharing_proportion = 0.0;
363 if ((usage.shared_clean > 0) || (usage.shared_dirty > 0)) {
364 sharing_proportion = (usage.pss - usage.uss) / (usage.shared_clean + usage.shared_dirty);
365 }
366 swapable_pss = (sharing_proportion * usage.shared_clean) + usage.private_clean;
367 }
368
369 stats[which_heap].pss += usage.pss;
370 stats[which_heap].swappablePss += swapable_pss;
371 stats[which_heap].rss += usage.rss;
372 stats[which_heap].privateDirty += usage.private_dirty;
373 stats[which_heap].sharedDirty += usage.shared_dirty;
374 stats[which_heap].privateClean += usage.private_clean;
375 stats[which_heap].sharedClean += usage.shared_clean;
376 stats[which_heap].swappedOut += usage.swap;
377 stats[which_heap].swappedOutPss += usage.swap_pss;
378 if (which_heap == HEAP_DALVIK || which_heap == HEAP_DALVIK_OTHER ||
379 which_heap == HEAP_DEX || which_heap == HEAP_ART) {
380 stats[sub_heap].pss += usage.pss;
381 stats[sub_heap].swappablePss += swapable_pss;
382 stats[sub_heap].rss += usage.rss;
383 stats[sub_heap].privateDirty += usage.private_dirty;
384 stats[sub_heap].sharedDirty += usage.shared_dirty;
385 stats[sub_heap].privateClean += usage.private_clean;
386 stats[sub_heap].sharedClean += usage.shared_clean;
387 stats[sub_heap].swappedOut += usage.swap;
388 stats[sub_heap].swappedOutPss += usage.swap_pss;
389 }
390 };
391
392 meminfo::ForEachVmaFromFile(smaps_path, vma_scan);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393}
394
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700395static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
396 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397{
Martijn Coenene0764852016-01-07 17:04:22 -0800398 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700399 stats_t stats[_NUM_HEAP];
400 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800401
Martijn Coenene0764852016-01-07 17:04:22 -0800402 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700404 struct graphics_memory_pss graphics_mem;
405 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
406 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
407 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800408 stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700409 stats[HEAP_GL].pss = graphics_mem.gl;
410 stats[HEAP_GL].privateDirty = graphics_mem.gl;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800411 stats[HEAP_GL].rss = graphics_mem.gl;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700412 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
413 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800414 stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700415 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700416
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700417 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700418 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700419 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800420 stats[HEAP_UNKNOWN].rss += stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700421 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
422 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700423 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700424 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700425 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800426 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700427 }
428
429 for (int i=0; i<_NUM_CORE_HEAP; i++) {
430 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700431 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800432 env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700433 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
434 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700435 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700436 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700437 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800438 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700439 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800440
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700441
Martijn Coenene0764852016-01-07 17:04:22 -0800442 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700443 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800444
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700445 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
446 if (otherArray == NULL) {
447 return;
448 }
449
450 int j=0;
451 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
452 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700453 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800454 otherArray[j++] = stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700455 otherArray[j++] = stats[i].privateDirty;
456 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700457 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700458 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700459 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800460 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700461 }
462
463 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464}
465
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700466static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
467{
468 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
469}
470
Martijn Coenene0764852016-01-07 17:04:22 -0800471static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
Dianne Hackborne17b4452018-01-10 13:15:40 -0800472 jlongArray outUssSwapPssRss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700473{
Dianne Hackbornb437e092011-08-05 17:50:29 -0700474 jlong pss = 0;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800475 jlong rss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800476 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700477 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800478 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700479
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700480 struct graphics_memory_pss graphics_mem;
481 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700482 pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700483 }
484
Sandeep Patilfcd4a4a2019-01-12 22:42:05 -0800485 ::android::meminfo::ProcMemInfo proc_mem(pid);
486 ::android::meminfo::MemUsage stats;
487 if (proc_mem.SmapsOrRollup(&stats)) {
488 pss += stats.pss;
489 uss += stats.uss;
490 rss += stats.rss;
491 swapPss = stats.swap_pss;
492 pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP
Dianne Hackborn37c99432013-09-05 19:34:57 -0700493 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700494
Dianne Hackborne17b4452018-01-10 13:15:40 -0800495 if (outUssSwapPssRss != NULL) {
496 if (env->GetArrayLength(outUssSwapPssRss) >= 1) {
497 jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
498 if (outUssSwapPssRssArray != NULL) {
499 outUssSwapPssRssArray[0] = uss;
500 if (env->GetArrayLength(outUssSwapPssRss) >= 2) {
501 outUssSwapPssRssArray[1] = swapPss;
502 }
503 if (env->GetArrayLength(outUssSwapPssRss) >= 3) {
504 outUssSwapPssRssArray[2] = rss;
Martijn Coenene0764852016-01-07 17:04:22 -0800505 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700506 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800507 env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700508 }
509 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700510
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800511 if (outMemtrack != NULL) {
512 if (env->GetArrayLength(outMemtrack) >= 1) {
513 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
514 if (outMemtrackArray != NULL) {
515 outMemtrackArray[0] = memtrack;
516 }
517 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
518 }
519 }
520
Dianne Hackbornb437e092011-08-05 17:50:29 -0700521 return pss;
522}
523
524static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
525{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800526 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700527}
528
Sandeep Patilbe825412018-12-11 10:09:46 -0800529// The 1:1 mapping of MEMINFO_* enums here must match with the constants from
530// Debug.java.
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700531enum {
532 MEMINFO_TOTAL,
533 MEMINFO_FREE,
534 MEMINFO_BUFFERS,
535 MEMINFO_CACHED,
536 MEMINFO_SHMEM,
537 MEMINFO_SLAB,
Robert Benea5e099802017-10-04 18:28:01 -0700538 MEMINFO_SLAB_RECLAIMABLE,
539 MEMINFO_SLAB_UNRECLAIMABLE,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700540 MEMINFO_SWAP_TOTAL,
541 MEMINFO_SWAP_FREE,
542 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700543 MEMINFO_MAPPED,
544 MEMINFO_VMALLOC_USED,
545 MEMINFO_PAGE_TABLES,
546 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700547 MEMINFO_COUNT
548};
549
Dianne Hackborn8e692572013-09-10 19:06:15 -0700550static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
551{
Dianne Hackborn8e692572013-09-10 19:06:15 -0700552 if (out == NULL) {
553 jniThrowNullPointerException(env, "out == null");
554 return;
555 }
556
Sandeep Patilbe825412018-12-11 10:09:46 -0800557 int outLen = env->GetArrayLength(out);
558 if (outLen < MEMINFO_COUNT) {
559 jniThrowRuntimeException(env, "outLen < MEMINFO_COUNT");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700560 return;
561 }
562
Sandeep Patilbe825412018-12-11 10:09:46 -0800563 // Read system memory info including ZRAM. The values are stored in the vector
564 // in the same order as MEMINFO_* enum
565 std::vector<uint64_t> mem(MEMINFO_COUNT);
566 std::vector<std::string> tags(::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags);
567 tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:");
568 ::android::meminfo::SysMemInfo smi;
569 if (!smi.ReadMemInfo(tags, &mem)) {
570 jniThrowRuntimeException(env, "SysMemInfo read failed");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700571 return;
572 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700573
Dianne Hackborn8e692572013-09-10 19:06:15 -0700574 jlong* outArray = env->GetLongArrayElements(out, 0);
575 if (outArray != NULL) {
Sandeep Patilbe825412018-12-11 10:09:46 -0800576 outLen = MEMINFO_COUNT;
577 for (int i = 0; i < outLen; i++) {
Sandeep Patilbe825412018-12-11 10:09:46 -0800578 if (i == MEMINFO_VMALLOC_USED) {
Sandeep Patil2481ae02019-01-13 20:33:02 -0800579 outArray[i] = smi.ReadVmallocInfo() / 1024;
Sandeep Patilbe825412018-12-11 10:09:46 -0800580 continue;
581 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700582 outArray[i] = mem[i];
583 }
584 }
Sandeep Patilbe825412018-12-11 10:09:46 -0800585
Dianne Hackborn8e692572013-09-10 19:06:15 -0700586 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587}
588
589static jint read_binder_stat(const char* stat)
590{
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700591 UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
592 if (fp == nullptr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 return -1;
594 }
595
596 char line[1024];
597
598 char compare[128];
599 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 // loop until we have the block that represents this process
602 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700603 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 return -1;
605 }
606 } while (strncmp(compare, line, len));
607
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800608 // 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 -0800609 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700612 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 return -1;
614 }
615 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 // we have the line, now increment the line ptr to the value
618 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700619 jint result = atoi(ptr);
Elliott Hughesc367d482013-10-29 13:12:55 -0700620 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621}
622
623static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
624{
625 return read_binder_stat("bcTRANSACTION");
626}
627
628static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
629{
630 return read_binder_stat("brTRANSACTION");
631}
632
633// these are implemented in android_util_Binder.cpp
634jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
635jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
636jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
637
Christopher Ferris8d652f82017-04-11 16:29:18 -0700638static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
Andy McFadden06a6b552010-07-13 16:28:09 -0700639{
640 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800641 jniThrowNullPointerException(env, "fd == null");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700642 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700643 }
644 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
645 if (origFd < 0) {
646 jniThrowRuntimeException(env, "Invalid file descriptor");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700647 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700648 }
649
650 /* dup() the descriptor so we don't close the original with fclose() */
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800651 int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0);
Andy McFadden06a6b552010-07-13 16:28:09 -0700652 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000653 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700654 jniThrowRuntimeException(env, "dup() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700655 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700656 }
657
Christopher Ferris8d652f82017-04-11 16:29:18 -0700658 fp.reset(fdopen(fd, "w"));
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700659 if (fp == nullptr) {
Steve Block8564c8d2012-01-05 23:22:43 +0000660 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700661 close(fd);
662 jniThrowRuntimeException(env, "fdopen() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700663 return false;
664 }
665 return true;
666}
667
668/*
669 * Dump the native heap, writing human-readable output to the specified
670 * file descriptor.
671 */
672static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
673 jobject fileDescriptor)
674{
675 UniqueFile fp(nullptr, safeFclose);
676 if (!openFile(env, fileDescriptor, fp)) {
Andy McFadden06a6b552010-07-13 16:28:09 -0700677 return;
678 }
679
Steve Block5baa3a62011-12-20 16:23:08 +0000680 ALOGD("Native heap dump starting...\n");
Christopher Ferris38e2c3b2018-06-13 13:19:57 -0700681 // Formatting of the native heap dump is handled by malloc debug itself.
682 // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
Christopher Ferris4740d542019-04-16 11:34:58 -0700683 if (android_mallopt(M_WRITE_MALLOC_LEAK_INFO_TO_FILE, fp.get(), sizeof(FILE*))) {
684 ALOGD("Native heap dump complete.\n");
685 } else {
686 PLOG(ERROR) << "Failed to write native heap dump to file";
687 }
Andy McFadden06a6b552010-07-13 16:28:09 -0700688}
689
Christopher Ferris8d652f82017-04-11 16:29:18 -0700690/*
691 * Dump the native malloc info, writing xml output to the specified
692 * file descriptor.
693 */
694static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
695 jobject fileDescriptor)
696{
697 UniqueFile fp(nullptr, safeFclose);
698 if (!openFile(env, fileDescriptor, fp)) {
699 return;
700 }
701
702 malloc_info(0, fp.get());
703}
704
Narayan Kamathf013daa2017-05-09 12:55:02 +0100705static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
706 DebuggerdDumpType dumpType) {
707 const ScopedUtfChars fileNameChars(env, fileName);
708 if (fileNameChars.c_str() == nullptr) {
709 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700710 }
711
Narayan Kamathf013daa2017-05-09 12:55:02 +0100712 android::base::unique_fd fd(open(fileNameChars.c_str(),
713 O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
714 0666));
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700715 if (fd < 0) {
Narayan Kamathf013daa2017-05-09 12:55:02 +0100716 fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
717 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700718 }
719
Narayan Kamathf013daa2017-05-09 12:55:02 +0100720 return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0);
721}
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700722
Narayan Kamathf013daa2017-05-09 12:55:02 +0100723static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
724 jint pid, jstring fileName, jint timeoutSecs) {
725 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
726 return ret ? JNI_TRUE : JNI_FALSE;
727}
728
729static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
730 jint pid, jstring fileName, jint timeoutSecs) {
731 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
732 return ret ? JNI_TRUE : JNI_FALSE;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700733}
734
Colin Crossc4fb5f92016-02-02 16:51:15 -0800735static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
736 jint limit, jboolean contents)
737{
738 std::string s = GetUnreachableMemoryString(contents, limit);
739 return env->NewStringUTF(s.c_str());
740}
741
Ben Murdochfdc55932019-01-30 10:43:15 +0000742static jlong android_os_Debug_getFreeZramKb(JNIEnv* env, jobject clazz) {
743
744 jlong zramFreeKb = 0;
745
746 std::string status_path = android::base::StringPrintf("/proc/meminfo");
747 UniqueFile file = MakeUniqueFile(status_path.c_str(), "re");
748
749 char line[256];
750 while (file != nullptr && fgets(line, sizeof(line), file.get())) {
751 jlong v;
752 if (sscanf(line, "SwapFree: %" SCNd64 " kB", &v) == 1) {
753 zramFreeKb = v;
754 break;
755 }
756 }
757
758 return zramFreeKb;
759}
760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761/*
762 * JNI registration.
763 */
764
Daniel Micay76f6a862015-09-19 17:31:01 -0400765static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 { "getNativeHeapSize", "()J",
767 (void*) android_os_Debug_getNativeHeapSize },
768 { "getNativeHeapAllocatedSize", "()J",
769 (void*) android_os_Debug_getNativeHeapAllocatedSize },
770 { "getNativeHeapFreeSize", "()J",
771 (void*) android_os_Debug_getNativeHeapFreeSize },
772 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
773 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700774 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
775 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -0700776 { "getPss", "()J",
777 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800778 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -0700779 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -0700780 { "getMemInfo", "([J)V",
781 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -0700782 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
783 (void*) android_os_Debug_dumpNativeHeap },
Christopher Ferris8d652f82017-04-11 16:29:18 -0700784 { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
785 (void*) android_os_Debug_dumpNativeMallocInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 { "getBinderSentTransactions", "()I",
787 (void*) android_os_Debug_getBinderSentTransactions },
788 { "getBinderReceivedTransactions", "()I",
789 (void*) android_os_getBinderReceivedTransactions },
790 { "getBinderLocalObjectCount", "()I",
791 (void*)android_os_Debug_getLocalObjectCount },
792 { "getBinderProxyObjectCount", "()I",
793 (void*)android_os_Debug_getProxyObjectCount },
794 { "getBinderDeathObjectCount", "()I",
795 (void*)android_os_Debug_getDeathObjectCount },
Narayan Kamathf013daa2017-05-09 12:55:02 +0100796 { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
797 (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
798 { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
songjinshie02e3ea2016-12-16 17:48:21 +0800799 (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
Colin Crossc4fb5f92016-02-02 16:51:15 -0800800 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
801 (void*)android_os_Debug_getUnreachableMemory },
Ben Murdochfdc55932019-01-30 10:43:15 +0000802 { "getZramFreeKb", "()J",
803 (void*)android_os_Debug_getFreeZramKb },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804};
805
806int register_android_os_Debug(JNIEnv *env)
807{
808 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800810 // Sanity check the number of other statistics expected in Java matches here.
811 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
812 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700813 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
814 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800815 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700816 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800817 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700818 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
819 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800820 return JNI_ERR;
821 }
822
823 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -0800824 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800825
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700826 for (int i=0; i<_NUM_CORE_HEAP; i++) {
827 stat_fields[i].pss_field =
828 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700829 stat_fields[i].pssSwappable_field =
830 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborne17b4452018-01-10 13:15:40 -0800831 stat_fields[i].rss_field =
832 env->GetFieldID(clazz, stat_field_names[i].rss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700833 stat_fields[i].privateDirty_field =
834 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
835 stat_fields[i].sharedDirty_field =
836 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700837 stat_fields[i].privateClean_field =
838 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700839 stat_fields[i].sharedClean_field =
840 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700841 stat_fields[i].swappedOut_field =
842 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -0800843 stat_fields[i].swappedOutPss_field =
844 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700845 }
846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
848}
849
Andy McFadden06a6b552010-07-13 16:28:09 -0700850}; // namespace android