blob: 8be617f08ac3f4197e4ed7b1654de108ffdd0068 [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 Hackborne17b4452018-01-10 13:15:40 -0800110 jfieldID rss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700111 jfieldID privateDirty_field;
112 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700113 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700114 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700115 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800116 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700117};
118
119struct stat_field_names {
120 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700121 const char* pssSwappable_name;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800122 const char* rss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700123 const char* privateDirty_name;
124 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700125 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700126 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700127 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800128 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700129};
130
131static stat_fields stat_fields[_NUM_CORE_HEAP];
132
133static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800134 { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800135 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800136 { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800137 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800138 { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800139 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700140};
141
142jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800143jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
145struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700146 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700147 int swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800148 int rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700149 int privateDirty;
150 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700151 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700152 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700153 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800154 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155};
156
Daniel Colascione6c518102017-07-27 03:33:34 -0700157enum pss_rollup_support {
158 PSS_ROLLUP_UNTRIED,
159 PSS_ROLLUP_SUPPORTED,
160 PSS_ROLLUP_UNSUPPORTED
161};
162
163static std::atomic<pss_rollup_support> g_pss_rollup_support;
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165#define BINDER_STATS "/proc/binder/stats"
166
167static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
168{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 struct mallinfo info = mallinfo();
170 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171}
172
173static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
174{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 struct mallinfo info = mallinfo();
176 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
179static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
180{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 struct mallinfo info = mallinfo();
182 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183}
184
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700185// Container used to retrieve graphics memory pss
186struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700187{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700188 int graphics;
189 int gl;
190 int other;
191};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700192
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700193/*
194 * Uses libmemtrack to retrieve graphics memory that the process is using.
195 * Any graphics memory reported in /proc/pid/smaps is not included here.
196 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700197static int read_memtrack_memory(struct memtrack_proc* p, int pid,
198 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700199{
200 int err = memtrack_proc_get(p, pid);
201 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700202 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700203 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700204 }
205
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700206 ssize_t pss = memtrack_proc_graphics_pss(p);
207 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700208 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700209 return pss;
210 }
211 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700212
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700213 pss = memtrack_proc_gl_pss(p);
214 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700215 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700216 return pss;
217 }
218 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700219
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700220 pss = memtrack_proc_other_pss(p);
221 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700222 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700223 return pss;
224 }
225 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700226
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700227 return 0;
228}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700229
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700230/*
231 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
232 */
233static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
234{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700235 struct memtrack_proc* p = memtrack_proc_new();
236 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700237 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700238 return -1;
239 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700240
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700241 int err = read_memtrack_memory(p, pid, graphics_mem);
242 memtrack_proc_destroy(p);
243 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700244}
245
Martijn Coenene0764852016-01-07 17:04:22 -0800246static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247{
248 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700249 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 bool skip, done = false;
251
Dianne Hackborne17b4452018-01-10 13:15:40 -0800252 unsigned pss = 0, swappable_pss = 0, rss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700253 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 unsigned shared_clean = 0, shared_dirty = 0;
255 unsigned private_clean = 0, private_dirty = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800256 unsigned swapped_out = 0, swapped_out_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700257 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 unsigned temp;
259
Colin Cross0c6bc732014-06-17 15:18:07 -0700260 uint64_t start;
261 uint64_t end = 0;
262 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700263 char* name;
264 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700266 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700267 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700268 int prevHeap = HEAP_UNKNOWN;
269
Martijn Coenene0764852016-01-07 17:04:22 -0800270 *foundSwapPss = false;
271
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700272 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
274 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700275 prevHeap = whichHeap;
276 prevEnd = end;
277 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700278 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700280 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281
282 len = strlen(line);
283 if (len < 1) return;
284 line[--len] = 0;
285
Colin Cross0c6bc732014-06-17 15:18:07 -0700286 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700287 skip = true;
288 } else {
289 while (isspace(line[name_pos])) {
290 name_pos += 1;
291 }
292 name = line + name_pos;
293 nameLen = strlen(name);
Mathieu Chartierd47cbea2016-03-03 10:59:33 -0800294 // Trim the end of the line if it is " (deleted)".
295 const char* deleted_str = " (deleted)";
296 if (nameLen > (int)strlen(deleted_str) &&
297 strcmp(name+nameLen-strlen(deleted_str), deleted_str) == 0) {
298 nameLen -= strlen(deleted_str);
299 name[nameLen] = '\0';
300 }
Dianne Hackborn64770d12013-05-23 17:51:19 -0700301 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700302 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700303 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
304 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700305 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800306 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700307 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
308 whichHeap = HEAP_SO;
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, ".jar") == 0) {
311 whichHeap = HEAP_JAR;
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, ".apk") == 0) {
314 whichHeap = HEAP_APK;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700315 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700316 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) {
317 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700318 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700319 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700320 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700321 whichHeap = HEAP_DEX;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700322 subHeap = HEAP_DEX_APP_DEX;
323 is_swappable = true;
324 } else if (nameLen > 5 && strcmp(name+nameLen-5, ".vdex") == 0) {
325 whichHeap = HEAP_DEX;
326 // Handle system@framework@boot* and system/framework/boot*
327 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
328 subHeap = HEAP_DEX_BOOT_VDEX;
329 } else {
330 subHeap = HEAP_DEX_APP_VDEX;
331 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700332 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700333 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
334 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700335 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700336 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
337 whichHeap = HEAP_ART;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700338 // Handle system@framework@boot* and system/framework/boot*
339 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
340 subHeap = HEAP_ART_BOOT;
341 } else {
342 subHeap = HEAP_ART_APP;
343 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700344 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700345 } else if (strncmp(name, "/dev/", 5) == 0) {
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700346 whichHeap = HEAP_UNKNOWN_DEV;
Mathieu Chartier93084622015-05-01 11:30:22 -0700347 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
348 whichHeap = HEAP_GL_DEV;
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700349 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
350 whichHeap = HEAP_CURSOR;
351 } else if (strncmp(name, "/dev/ashmem", 11)) {
352 whichHeap = HEAP_ASHMEM;
Mathieu Chartier93084622015-05-01 11:30:22 -0700353 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700354 } else if (strncmp(name, "[anon:", 6) == 0) {
355 whichHeap = HEAP_UNKNOWN;
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700356 if (strncmp(name, "[anon:dalvik-", 13) == 0) {
357 whichHeap = HEAP_DALVIK_OTHER;
358 if (strstr(name, "[anon:dalvik-LinearAlloc") == name) {
359 subHeap = HEAP_DALVIK_OTHER_LINEARALLOC;
360 } else if ((strstr(name, "[anon:dalvik-alloc space") == name) ||
361 (strstr(name, "[anon:dalvik-main space") == name)) {
362 // This is the regular Dalvik heap.
363 whichHeap = HEAP_DALVIK;
364 subHeap = HEAP_DALVIK_NORMAL;
365 } else if (strstr(name, "[anon:dalvik-large object space") == name ||
366 strstr(name, "[anon:dalvik-free list large object space")
367 == name) {
368 whichHeap = HEAP_DALVIK;
369 subHeap = HEAP_DALVIK_LARGE;
370 } else if (strstr(name, "[anon:dalvik-non moving space") == name) {
371 whichHeap = HEAP_DALVIK;
372 subHeap = HEAP_DALVIK_NON_MOVING;
373 } else if (strstr(name, "[anon:dalvik-zygote space") == name) {
374 whichHeap = HEAP_DALVIK;
375 subHeap = HEAP_DALVIK_ZYGOTE;
376 } else if (strstr(name, "[anon:dalvik-indirect ref") == name) {
377 subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
378 } else if (strstr(name, "[anon:dalvik-jit-code-cache") == name ||
379 strstr(name, "[anon:dalvik-data-code-cache") == name) {
380 subHeap = HEAP_DALVIK_OTHER_CODE_CACHE;
381 } else if (strstr(name, "[anon:dalvik-CompilerMetadata") == name) {
382 subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
383 } else {
384 subHeap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting.
385 }
386 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700387 } else if (nameLen > 0) {
388 whichHeap = HEAP_UNKNOWN_MAP;
389 } else if (start == prevEnd && prevHeap == HEAP_SO) {
390 // bss section of a shared library.
391 whichHeap = HEAP_SO;
392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394
Steve Block6215d3f2012-01-04 20:05:49 +0000395 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800397
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700398 shared_clean = 0;
399 shared_dirty = 0;
400 private_clean = 0;
401 private_dirty = 0;
402 swapped_out = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800403 swapped_out_pss = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 while (true) {
406 if (fgets(line, 1024, fp) == 0) {
407 done = true;
408 break;
409 }
410
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700411 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800412 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700413 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800414 rss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700415 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700417 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700419 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700421 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700423 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700425 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800426 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700427 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
428 swapped_out = temp;
Martijn Coenene0764852016-01-07 17:04:22 -0800429 } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) {
430 *foundSwapPss = true;
431 swapped_out_pss = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700432 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700434 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 break;
436 }
437 }
438
439 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700440 if (is_swappable && (pss > 0)) {
441 sharing_proportion = 0.0;
442 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700443 sharing_proportion = (pss - private_clean
444 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700445 }
446 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
447 } else
448 swappable_pss = 0;
449
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700450 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700451 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800452 stats[whichHeap].rss += rss;
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;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800463 stats[subHeap].rss += rss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700464 stats[subHeap].privateDirty += private_dirty;
465 stats[subHeap].sharedDirty += shared_dirty;
466 stats[subHeap].privateClean += private_clean;
467 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700468 stats[subHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800469 stats[subHeap].swappedOutPss += swapped_out_pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472 }
473}
474
Martijn Coenene0764852016-01-07 17:04:22 -0800475static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476{
Andreas Gampece711492016-10-04 18:06:42 -0700477 *foundSwapPss = false;
478
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700479 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
480 UniqueFile fp = MakeUniqueFile(smaps_path.c_str(), "re");
481 if (fp == nullptr) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700483 read_mapinfo(fp.get(), stats, foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484}
485
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700486static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
487 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488{
Martijn Coenene0764852016-01-07 17:04:22 -0800489 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700490 stats_t stats[_NUM_HEAP];
491 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800492
Martijn Coenene0764852016-01-07 17:04:22 -0800493 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700495 struct graphics_memory_pss graphics_mem;
496 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
497 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
498 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800499 stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700500 stats[HEAP_GL].pss = graphics_mem.gl;
501 stats[HEAP_GL].privateDirty = graphics_mem.gl;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800502 stats[HEAP_GL].rss = graphics_mem.gl;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700503 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
504 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800505 stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700506 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700507
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700508 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700509 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700510 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800511 stats[HEAP_UNKNOWN].rss += stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700512 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
513 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700514 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700515 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700516 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800517 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700518 }
519
520 for (int i=0; i<_NUM_CORE_HEAP; i++) {
521 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700522 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800523 env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700524 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
525 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700526 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700527 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700528 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800529 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700530 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800531
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700532
Martijn Coenene0764852016-01-07 17:04:22 -0800533 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700534 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800535
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700536 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
537 if (otherArray == NULL) {
538 return;
539 }
540
541 int j=0;
542 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
543 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700544 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800545 otherArray[j++] = stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700546 otherArray[j++] = stats[i].privateDirty;
547 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700548 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700549 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700550 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800551 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700552 }
553
554 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555}
556
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700557static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
558{
559 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
560}
561
Daniel Colascione6c518102017-07-27 03:33:34 -0700562UniqueFile OpenSmapsOrRollup(int pid)
563{
564 enum pss_rollup_support rollup_support =
565 g_pss_rollup_support.load(std::memory_order_relaxed);
566 if (rollup_support != PSS_ROLLUP_UNSUPPORTED) {
567 std::string smaps_rollup_path =
568 base::StringPrintf("/proc/%d/smaps_rollup", pid);
569 UniqueFile fp_rollup = MakeUniqueFile(smaps_rollup_path.c_str(), "re");
570 if (fp_rollup == nullptr && errno != ENOENT) {
571 return fp_rollup; // Actual error, not just old kernel.
572 }
573 if (fp_rollup != nullptr) {
574 if (rollup_support == PSS_ROLLUP_UNTRIED) {
575 ALOGI("using rollup pss collection");
576 g_pss_rollup_support.store(PSS_ROLLUP_SUPPORTED,
577 std::memory_order_relaxed);
578 }
579 return fp_rollup;
580 }
581 g_pss_rollup_support.store(PSS_ROLLUP_UNSUPPORTED,
582 std::memory_order_relaxed);
583 }
584
585 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
586 return MakeUniqueFile(smaps_path.c_str(), "re");
587}
588
Martijn Coenene0764852016-01-07 17:04:22 -0800589static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
Dianne Hackborne17b4452018-01-10 13:15:40 -0800590 jlongArray outUssSwapPssRss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700591{
Dianne Hackborne17b4452018-01-10 13:15:40 -0800592 char lineBuffer[1024];
Dianne Hackbornb437e092011-08-05 17:50:29 -0700593 jlong pss = 0;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800594 jlong rss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800595 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700596 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800597 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700598
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700599 struct graphics_memory_pss graphics_mem;
600 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800601 pss = uss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700602 }
603
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700604 {
Daniel Colascione6c518102017-07-27 03:33:34 -0700605 UniqueFile fp = OpenSmapsOrRollup(pid);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700606
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700607 if (fp != nullptr) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800608 char* line;
609
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700610 while (true) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800611 if (fgets(lineBuffer, sizeof (lineBuffer), fp.get()) == NULL) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700612 break;
613 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800614 line = lineBuffer;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700615
Dianne Hackborne17b4452018-01-10 13:15:40 -0800616 switch (line[0]) {
617 case 'P':
618 if (strncmp(line, "Pss:", 4) == 0) {
619 char* c = line + 4;
620 while (*c != 0 && (*c < '0' || *c > '9')) {
621 c++;
622 }
623 pss += atoi(c);
624 } else if (strncmp(line, "Private_Clean:", 14) == 0
625 || strncmp(line, "Private_Dirty:", 14) == 0) {
626 char* c = line + 14;
627 while (*c != 0 && (*c < '0' || *c > '9')) {
628 c++;
629 }
630 uss += atoi(c);
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700631 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800632 break;
633 case 'R':
634 if (strncmp(line, "Rss:", 4) == 0) {
635 char* c = line + 4;
636 while (*c != 0 && (*c < '0' || *c > '9')) {
637 c++;
638 }
639 rss += atoi(c);
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700640 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800641 break;
642 case 'S':
643 if (strncmp(line, "SwapPss:", 8) == 0) {
644 char* c = line + 8;
645 jlong lSwapPss;
646 while (*c != 0 && (*c < '0' || *c > '9')) {
647 c++;
648 }
649 lSwapPss = atoi(c);
650 swapPss += lSwapPss;
651 pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP
652 }
653 break;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700654 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700655 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700656 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700657 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700658
Dianne Hackborne17b4452018-01-10 13:15:40 -0800659 if (outUssSwapPssRss != NULL) {
660 if (env->GetArrayLength(outUssSwapPssRss) >= 1) {
661 jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
662 if (outUssSwapPssRssArray != NULL) {
663 outUssSwapPssRssArray[0] = uss;
664 if (env->GetArrayLength(outUssSwapPssRss) >= 2) {
665 outUssSwapPssRssArray[1] = swapPss;
666 }
667 if (env->GetArrayLength(outUssSwapPssRss) >= 3) {
668 outUssSwapPssRssArray[2] = rss;
Martijn Coenene0764852016-01-07 17:04:22 -0800669 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700670 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800671 env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700672 }
673 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700674
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800675 if (outMemtrack != NULL) {
676 if (env->GetArrayLength(outMemtrack) >= 1) {
677 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
678 if (outMemtrackArray != NULL) {
679 outMemtrackArray[0] = memtrack;
680 }
681 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
682 }
683 }
684
Dianne Hackbornb437e092011-08-05 17:50:29 -0700685 return pss;
686}
687
688static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
689{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800690 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700691}
692
Martijn Coenen398e24e2015-04-23 21:55:58 +0200693static long get_allocated_vmalloc_memory() {
694 char line[1024];
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200695 // Ignored tags that don't actually consume memory (ie remappings)
696 static const char* const ignored_tags[] = {
697 "ioremap",
698 "map_lowmem",
699 "vm_map_ram",
700 NULL
701 };
Martijn Coenen398e24e2015-04-23 21:55:58 +0200702 long size, vmalloc_allocated_size = 0;
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700703
704 UniqueFile fp = MakeUniqueFile("/proc/vmallocinfo", "re");
705 if (fp == nullptr) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200706 return 0;
707 }
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700708
Martijn Coenen398e24e2015-04-23 21:55:58 +0200709 while (true) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700710 if (fgets(line, 1024, fp.get()) == NULL) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200711 break;
712 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200713 bool valid_line = true;
714 int i = 0;
715 while (ignored_tags[i]) {
716 if (strstr(line, ignored_tags[i]) != NULL) {
717 valid_line = false;
718 break;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200719 }
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200720 i++;
721 }
722 if (valid_line && (sscanf(line, "%*x-%*x %ld", &size) == 1)) {
723 vmalloc_allocated_size += size;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200724 }
725 }
Martijn Coenen398e24e2015-04-23 21:55:58 +0200726 return vmalloc_allocated_size;
727}
728
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700729enum {
730 MEMINFO_TOTAL,
731 MEMINFO_FREE,
732 MEMINFO_BUFFERS,
733 MEMINFO_CACHED,
734 MEMINFO_SHMEM,
735 MEMINFO_SLAB,
Robert Benea5e099802017-10-04 18:28:01 -0700736 MEMINFO_SLAB_RECLAIMABLE,
737 MEMINFO_SLAB_UNRECLAIMABLE,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700738 MEMINFO_SWAP_TOTAL,
739 MEMINFO_SWAP_FREE,
740 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700741 MEMINFO_MAPPED,
742 MEMINFO_VMALLOC_USED,
743 MEMINFO_PAGE_TABLES,
744 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700745 MEMINFO_COUNT
746};
747
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800748static long long get_zram_mem_used()
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800749{
750#define ZRAM_SYSFS "/sys/block/zram0/"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700751 UniqueFile mm_stat_file = MakeUniqueFile(ZRAM_SYSFS "mm_stat", "re");
752 if (mm_stat_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800753 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800754
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700755 int matched = fscanf(mm_stat_file.get(), "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800756 if (matched != 1)
757 ALOGW("failed to parse " ZRAM_SYSFS "mm_stat");
758
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800759 return mem_used_total;
760 }
761
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700762 UniqueFile mem_used_total_file = MakeUniqueFile(ZRAM_SYSFS "mem_used_total", "re");
763 if (mem_used_total_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800764 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800765
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700766 int matched = fscanf(mem_used_total_file.get(), "%lld", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800767 if (matched != 1)
768 ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total");
769
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800770 return mem_used_total;
771 }
772
773 return 0;
774}
775
Dianne Hackborn8e692572013-09-10 19:06:15 -0700776static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
777{
Dianne Hackbornb79a5f92017-12-08 17:09:26 -0800778 char buffer[4096];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200779 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700780
781 if (out == NULL) {
782 jniThrowNullPointerException(env, "out == null");
783 return;
784 }
785
Nick Kralevich422dd00f2017-10-19 17:51:50 -0700786 int fd = open("/proc/meminfo", O_RDONLY | O_CLOEXEC);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700787
788 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700789 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700790 return;
791 }
792
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700793 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700794 close(fd);
795
796 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700797 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700798 return;
799 }
800 buffer[len] = 0;
801
802 static const char* const tags[] = {
803 "MemTotal:",
804 "MemFree:",
805 "Buffers:",
806 "Cached:",
807 "Shmem:",
808 "Slab:",
Robert Benea5e099802017-10-04 18:28:01 -0700809 "SReclaimable:",
810 "SUnreclaim:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700811 "SwapTotal:",
812 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700813 "ZRam:",
814 "Mapped:",
815 "VmallocUsed:",
816 "PageTables:",
817 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700818 NULL
819 };
820 static const int tagsLen[] = {
821 9,
822 8,
823 8,
824 7,
825 6,
826 5,
Robert Benea5e099802017-10-04 18:28:01 -0700827 13,
828 11,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700829 10,
830 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700831 5,
832 7,
833 12,
834 11,
835 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700836 0
837 };
Robert Benea5e099802017-10-04 18:28:01 -0700838 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700839
840 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200841 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700842 int i = 0;
843 while (tags[i]) {
844 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
845 p += tagsLen[i];
846 while (*p == ' ') p++;
847 char* num = p;
848 while (*p >= '0' && *p <= '9') p++;
849 if (*p != 0) {
850 *p = 0;
851 p++;
852 }
853 mem[i] = atoll(num);
854 numFound++;
855 break;
856 }
857 i++;
858 }
859 while (*p && *p != '\n') {
860 p++;
861 }
862 if (*p) p++;
863 }
864
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800865 mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200866 // Recompute Vmalloc Used since the value in meminfo
867 // doesn't account for I/O remapping which doesn't use RAM.
868 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700869
Dianne Hackborn8e692572013-09-10 19:06:15 -0700870 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700871 if (maxNum > MEMINFO_COUNT) {
872 maxNum = MEMINFO_COUNT;
873 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700874 jlong* outArray = env->GetLongArrayElements(out, 0);
875 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700876 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700877 outArray[i] = mem[i];
878 }
879 }
880 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881}
882
Martijn Coenen398e24e2015-04-23 21:55:58 +0200883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884static jint read_binder_stat(const char* stat)
885{
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700886 UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
887 if (fp == nullptr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 return -1;
889 }
890
891 char line[1024];
892
893 char compare[128];
894 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // loop until we have the block that represents this process
897 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700898 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 return -1;
900 }
901 } while (strncmp(compare, line, len));
902
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800903 // 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 -0800904 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700907 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 return -1;
909 }
910 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 // we have the line, now increment the line ptr to the value
913 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700914 jint result = atoi(ptr);
Elliott Hughesc367d482013-10-29 13:12:55 -0700915 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916}
917
918static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
919{
920 return read_binder_stat("bcTRANSACTION");
921}
922
923static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
924{
925 return read_binder_stat("brTRANSACTION");
926}
927
928// these are implemented in android_util_Binder.cpp
929jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
930jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
931jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
932
Christopher Ferris8d652f82017-04-11 16:29:18 -0700933static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
Andy McFadden06a6b552010-07-13 16:28:09 -0700934{
935 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800936 jniThrowNullPointerException(env, "fd == null");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700937 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700938 }
939 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
940 if (origFd < 0) {
941 jniThrowRuntimeException(env, "Invalid file descriptor");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700942 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700943 }
944
945 /* dup() the descriptor so we don't close the original with fclose() */
Nick Kralevich4b3a08c2019-01-28 10:39:10 -0800946 int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0);
Andy McFadden06a6b552010-07-13 16:28:09 -0700947 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000948 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700949 jniThrowRuntimeException(env, "dup() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700950 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700951 }
952
Christopher Ferris8d652f82017-04-11 16:29:18 -0700953 fp.reset(fdopen(fd, "w"));
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700954 if (fp == nullptr) {
Steve Block8564c8d2012-01-05 23:22:43 +0000955 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700956 close(fd);
957 jniThrowRuntimeException(env, "fdopen() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700958 return false;
959 }
960 return true;
961}
962
Christopher Ferrisffa139e2018-06-13 13:19:57 -0700963/* pulled out of bionic */
964extern "C" void write_malloc_leak_info(FILE* fp);
965
Christopher Ferris8d652f82017-04-11 16:29:18 -0700966/*
967 * Dump the native heap, writing human-readable output to the specified
968 * file descriptor.
969 */
970static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
971 jobject fileDescriptor)
972{
973 UniqueFile fp(nullptr, safeFclose);
974 if (!openFile(env, fileDescriptor, fp)) {
Andy McFadden06a6b552010-07-13 16:28:09 -0700975 return;
976 }
977
Steve Block5baa3a62011-12-20 16:23:08 +0000978 ALOGD("Native heap dump starting...\n");
Christopher Ferrisffa139e2018-06-13 13:19:57 -0700979 // Formatting of the native heap dump is handled by malloc debug itself.
980 // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
981 write_malloc_leak_info(fp.get());
Steve Block5baa3a62011-12-20 16:23:08 +0000982 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700983}
984
Christopher Ferris8d652f82017-04-11 16:29:18 -0700985/*
986 * Dump the native malloc info, writing xml output to the specified
987 * file descriptor.
988 */
989static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
990 jobject fileDescriptor)
991{
992 UniqueFile fp(nullptr, safeFclose);
993 if (!openFile(env, fileDescriptor, fp)) {
994 return;
995 }
996
997 malloc_info(0, fp.get());
998}
999
Narayan Kamathf013daa2017-05-09 12:55:02 +01001000static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
1001 DebuggerdDumpType dumpType) {
1002 const ScopedUtfChars fileNameChars(env, fileName);
1003 if (fileNameChars.c_str() == nullptr) {
1004 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001005 }
1006
Narayan Kamathf013daa2017-05-09 12:55:02 +01001007 android::base::unique_fd fd(open(fileNameChars.c_str(),
1008 O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
1009 0666));
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001010 if (fd < 0) {
Narayan Kamathf013daa2017-05-09 12:55:02 +01001011 fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
1012 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001013 }
1014
Narayan Kamathf013daa2017-05-09 12:55:02 +01001015 return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0);
1016}
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001017
Narayan Kamathf013daa2017-05-09 12:55:02 +01001018static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1019 jint pid, jstring fileName, jint timeoutSecs) {
1020 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
1021 return ret ? JNI_TRUE : JNI_FALSE;
1022}
1023
1024static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1025 jint pid, jstring fileName, jint timeoutSecs) {
1026 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
1027 return ret ? JNI_TRUE : JNI_FALSE;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001028}
1029
Colin Crossc4fb5f92016-02-02 16:51:15 -08001030static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
1031 jint limit, jboolean contents)
1032{
1033 std::string s = GetUnreachableMemoryString(contents, limit);
1034 return env->NewStringUTF(s.c_str());
1035}
1036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037/*
1038 * JNI registration.
1039 */
1040
Daniel Micay76f6a862015-09-19 17:31:01 -04001041static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 { "getNativeHeapSize", "()J",
1043 (void*) android_os_Debug_getNativeHeapSize },
1044 { "getNativeHeapAllocatedSize", "()J",
1045 (void*) android_os_Debug_getNativeHeapAllocatedSize },
1046 { "getNativeHeapFreeSize", "()J",
1047 (void*) android_os_Debug_getNativeHeapFreeSize },
1048 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1049 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001050 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1051 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001052 { "getPss", "()J",
1053 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001054 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001055 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001056 { "getMemInfo", "([J)V",
1057 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001058 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1059 (void*) android_os_Debug_dumpNativeHeap },
Christopher Ferris8d652f82017-04-11 16:29:18 -07001060 { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
1061 (void*) android_os_Debug_dumpNativeMallocInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 { "getBinderSentTransactions", "()I",
1063 (void*) android_os_Debug_getBinderSentTransactions },
1064 { "getBinderReceivedTransactions", "()I",
1065 (void*) android_os_getBinderReceivedTransactions },
1066 { "getBinderLocalObjectCount", "()I",
1067 (void*)android_os_Debug_getLocalObjectCount },
1068 { "getBinderProxyObjectCount", "()I",
1069 (void*)android_os_Debug_getProxyObjectCount },
1070 { "getBinderDeathObjectCount", "()I",
1071 (void*)android_os_Debug_getDeathObjectCount },
Narayan Kamathf013daa2017-05-09 12:55:02 +01001072 { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
1073 (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
1074 { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
songjinshie02e3ea2016-12-16 17:48:21 +08001075 (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
Colin Crossc4fb5f92016-02-02 16:51:15 -08001076 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
1077 (void*)android_os_Debug_getUnreachableMemory },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078};
1079
1080int register_android_os_Debug(JNIEnv *env)
1081{
1082 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001084 // Sanity check the number of other statistics expected in Java matches here.
1085 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1086 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001087 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1088 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001089 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001090 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001091 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001092 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1093 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001094 return JNI_ERR;
1095 }
1096
1097 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -08001098 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001099
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001100 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1101 stat_fields[i].pss_field =
1102 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001103 stat_fields[i].pssSwappable_field =
1104 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborne17b4452018-01-10 13:15:40 -08001105 stat_fields[i].rss_field =
1106 env->GetFieldID(clazz, stat_field_names[i].rss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001107 stat_fields[i].privateDirty_field =
1108 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1109 stat_fields[i].sharedDirty_field =
1110 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001111 stat_fields[i].privateClean_field =
1112 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001113 stat_fields[i].sharedClean_field =
1114 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001115 stat_fields[i].swappedOut_field =
1116 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -08001117 stat_fields[i].swappedOutPss_field =
1118 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001119 }
1120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1122}
1123
Andy McFadden06a6b552010-07-13 16:28:09 -07001124}; // namespace android