blob: fa1da4bfbf3a4393b07527dce1ab314314915b07 [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
Josh Gao3ce36962017-01-05 18:28:24 -080036#include <debuggerd/client.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070037#include <log/log.h>
38#include <utils/misc.h>
39#include <utils/String8.h>
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070040
Steven Morelandbdd5c422017-08-11 10:57:59 -070041#include <nativehelper/JNIHelp.h>
42#include <nativehelper/ScopedUtfChars.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070043#include "jni.h"
44#include <memtrack/memtrack.h>
45#include <memunreachable/memunreachable.h>
Daniel Colascione6c518102017-07-27 03:33:34 -070046#include "android_os_Debug.h"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -070047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048namespace android
49{
50
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070051enum {
52 HEAP_UNKNOWN,
53 HEAP_DALVIK,
54 HEAP_NATIVE,
Dianne Hackborn18429302014-11-03 13:58:11 -080055
Dianne Hackborn64770d12013-05-23 17:51:19 -070056 HEAP_DALVIK_OTHER,
Ian Rogers7c9f30b2013-02-27 10:57:13 -080057 HEAP_STACK,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070058 HEAP_CURSOR,
59 HEAP_ASHMEM,
Dianne Hackborn18429302014-11-03 13:58:11 -080060 HEAP_GL_DEV,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070061 HEAP_UNKNOWN_DEV,
62 HEAP_SO,
63 HEAP_JAR,
64 HEAP_APK,
65 HEAP_TTF,
66 HEAP_DEX,
Anwar Ghuloum8884ef42013-03-15 12:56:59 -070067 HEAP_OAT,
Anwar Ghuloum88887d02013-03-19 15:30:12 -070068 HEAP_ART,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070069 HEAP_UNKNOWN_MAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070070 HEAP_GRAPHICS,
71 HEAP_GL,
72 HEAP_OTHER_MEMTRACK,
Dianne Hackborn64770d12013-05-23 17:51:19 -070073
Mathieu Chartier95550dd2017-07-13 15:01:34 -070074 // Dalvik extra sections (heap).
Anwar Ghuloum3c615062013-05-13 14:18:02 -070075 HEAP_DALVIK_NORMAL,
76 HEAP_DALVIK_LARGE,
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -080077 HEAP_DALVIK_ZYGOTE,
78 HEAP_DALVIK_NON_MOVING,
Mathieu Chartier95550dd2017-07-13 15:01:34 -070079
80 // Dalvik other extra sections.
81 HEAP_DALVIK_OTHER_LINEARALLOC,
82 HEAP_DALVIK_OTHER_ACCOUNTING,
83 HEAP_DALVIK_OTHER_CODE_CACHE,
84 HEAP_DALVIK_OTHER_COMPILER_METADATA,
85 HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE,
86
87 // Boot vdex / app dex / app vdex
88 HEAP_DEX_BOOT_VDEX,
89 HEAP_DEX_APP_DEX,
90 HEAP_DEX_APP_VDEX,
91
92 // App art, boot art.
93 HEAP_ART_APP,
94 HEAP_ART_BOOT,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070095
96 _NUM_HEAP,
Adam Lesinski5b4ef812013-09-23 10:06:09 -070097 _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070098 _NUM_CORE_HEAP = HEAP_NATIVE+1
99};
100
101struct stat_fields {
102 jfieldID pss_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700103 jfieldID pssSwappable_field;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800104 jfieldID rss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700105 jfieldID privateDirty_field;
106 jfieldID sharedDirty_field;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700107 jfieldID privateClean_field;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700108 jfieldID sharedClean_field;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700109 jfieldID swappedOut_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800110 jfieldID swappedOutPss_field;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700111};
112
113struct stat_field_names {
114 const char* pss_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700115 const char* pssSwappable_name;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800116 const char* rss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700117 const char* privateDirty_name;
118 const char* sharedDirty_name;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700119 const char* privateClean_name;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700120 const char* sharedClean_name;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700121 const char* swappedOut_name;
Martijn Coenene0764852016-01-07 17:04:22 -0800122 const char* swappedOutPss_name;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700123};
124
125static stat_fields stat_fields[_NUM_CORE_HEAP];
126
127static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800128 { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800129 "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800130 { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800131 "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
Dianne Hackborne17b4452018-01-10 13:15:40 -0800132 { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty",
Martijn Coenene0764852016-01-07 17:04:22 -0800133 "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700134};
135
136jfieldID otherStats_field;
Martijn Coenene0764852016-01-07 17:04:22 -0800137jfieldID hasSwappedOutPss_field;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139struct stats_t {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700140 int pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700141 int swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800142 int rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700143 int privateDirty;
144 int sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700145 int privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700146 int sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700147 int swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800148 int swappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149};
150
Daniel Colascione6c518102017-07-27 03:33:34 -0700151enum pss_rollup_support {
152 PSS_ROLLUP_UNTRIED,
153 PSS_ROLLUP_SUPPORTED,
154 PSS_ROLLUP_UNSUPPORTED
155};
156
157static std::atomic<pss_rollup_support> g_pss_rollup_support;
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159#define BINDER_STATS "/proc/binder/stats"
160
161static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
162{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 struct mallinfo info = mallinfo();
164 return (jlong) info.usmblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165}
166
167static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
168{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 struct mallinfo info = mallinfo();
170 return (jlong) info.uordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171}
172
173static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
174{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 struct mallinfo info = mallinfo();
176 return (jlong) info.fordblks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700179// Container used to retrieve graphics memory pss
180struct graphics_memory_pss
Dianne Hackborn37c99432013-09-05 19:34:57 -0700181{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700182 int graphics;
183 int gl;
184 int other;
185};
Dianne Hackborn37c99432013-09-05 19:34:57 -0700186
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700187/*
188 * Uses libmemtrack to retrieve graphics memory that the process is using.
189 * Any graphics memory reported in /proc/pid/smaps is not included here.
190 */
Dianne Hackborncb428552013-09-26 11:07:17 -0700191static int read_memtrack_memory(struct memtrack_proc* p, int pid,
192 struct graphics_memory_pss* graphics_mem)
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700193{
194 int err = memtrack_proc_get(p, pid);
195 if (err != 0) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700196 ALOGW("failed to get memory consumption info: %d", err);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700197 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700198 }
199
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700200 ssize_t pss = memtrack_proc_graphics_pss(p);
201 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700202 ALOGW("failed to get graphics pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700203 return pss;
204 }
205 graphics_mem->graphics = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700206
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700207 pss = memtrack_proc_gl_pss(p);
208 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700209 ALOGW("failed to get gl pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700210 return pss;
211 }
212 graphics_mem->gl = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700213
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700214 pss = memtrack_proc_other_pss(p);
215 if (pss < 0) {
Colin Cross0c6bc732014-06-17 15:18:07 -0700216 ALOGW("failed to get other pss: %zd", pss);
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700217 return pss;
218 }
219 graphics_mem->other = pss / 1024;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700220
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700221 return 0;
222}
Dianne Hackborn37c99432013-09-05 19:34:57 -0700223
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700224/*
225 * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
226 */
227static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
228{
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700229 struct memtrack_proc* p = memtrack_proc_new();
230 if (p == NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700231 ALOGW("failed to create memtrack_proc");
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700232 return -1;
233 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700234
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700235 int err = read_memtrack_memory(p, pid, graphics_mem);
236 memtrack_proc_destroy(p);
237 return err;
Dianne Hackborn37c99432013-09-05 19:34:57 -0700238}
239
Martijn Coenene0764852016-01-07 17:04:22 -0800240static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241{
242 char line[1024];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700243 int len, nameLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 bool skip, done = false;
245
Dianne Hackborne17b4452018-01-10 13:15:40 -0800246 unsigned pss = 0, swappable_pss = 0, rss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700247 float sharing_proportion = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 unsigned shared_clean = 0, shared_dirty = 0;
249 unsigned private_clean = 0, private_dirty = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800250 unsigned swapped_out = 0, swapped_out_pss = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700251 bool is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 unsigned temp;
253
Colin Cross0c6bc732014-06-17 15:18:07 -0700254 uint64_t start;
255 uint64_t end = 0;
256 uint64_t prevEnd = 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700257 char* name;
258 int name_pos;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700260 int whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700261 int subHeap = HEAP_UNKNOWN;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700262 int prevHeap = HEAP_UNKNOWN;
263
Martijn Coenene0764852016-01-07 17:04:22 -0800264 *foundSwapPss = false;
265
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700266 if(fgets(line, sizeof(line), fp) == 0) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
268 while (!done) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700269 prevHeap = whichHeap;
270 prevEnd = end;
271 whichHeap = HEAP_UNKNOWN;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700272 subHeap = HEAP_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 skip = false;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700274 is_swappable = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
276 len = strlen(line);
277 if (len < 1) return;
278 line[--len] = 0;
279
Colin Cross0c6bc732014-06-17 15:18:07 -0700280 if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700281 skip = true;
282 } else {
283 while (isspace(line[name_pos])) {
284 name_pos += 1;
285 }
286 name = line + name_pos;
287 nameLen = strlen(name);
Mathieu Chartierd47cbea2016-03-03 10:59:33 -0800288 // Trim the end of the line if it is " (deleted)".
289 const char* deleted_str = " (deleted)";
290 if (nameLen > (int)strlen(deleted_str) &&
291 strcmp(name+nameLen-strlen(deleted_str), deleted_str) == 0) {
292 nameLen -= strlen(deleted_str);
293 name[nameLen] = '\0';
294 }
Dianne Hackborn64770d12013-05-23 17:51:19 -0700295 if ((strstr(name, "[heap]") == name)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700296 whichHeap = HEAP_NATIVE;
Colin Crosscb4728f2013-08-09 13:23:46 -0700297 } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) {
298 whichHeap = HEAP_NATIVE;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700299 } else if (strncmp(name, "[stack", 6) == 0) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800300 whichHeap = HEAP_STACK;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700301 } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) {
302 whichHeap = HEAP_SO;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700303 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700304 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) {
305 whichHeap = HEAP_JAR;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700306 is_swappable = true;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700307 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) {
308 whichHeap = HEAP_APK;
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, ".ttf") == 0) {
311 whichHeap = HEAP_TTF;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700312 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700313 } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) ||
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700314 (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700315 whichHeap = HEAP_DEX;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700316 subHeap = HEAP_DEX_APP_DEX;
317 is_swappable = true;
318 } else if (nameLen > 5 && strcmp(name+nameLen-5, ".vdex") == 0) {
319 whichHeap = HEAP_DEX;
320 // Handle system@framework@boot* and system/framework/boot*
321 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
322 subHeap = HEAP_DEX_BOOT_VDEX;
323 } else {
324 subHeap = HEAP_DEX_APP_VDEX;
325 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700326 is_swappable = true;
Anwar Ghuloum8884ef42013-03-15 12:56:59 -0700327 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) {
328 whichHeap = HEAP_OAT;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700329 is_swappable = true;
Anwar Ghuloum88887d02013-03-19 15:30:12 -0700330 } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) {
331 whichHeap = HEAP_ART;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700332 // Handle system@framework@boot* and system/framework/boot*
333 if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) {
334 subHeap = HEAP_ART_BOOT;
335 } else {
336 subHeap = HEAP_ART_APP;
337 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700338 is_swappable = true;
Mathieu Chartier93084622015-05-01 11:30:22 -0700339 } else if (strncmp(name, "/dev/", 5) == 0) {
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700340 whichHeap = HEAP_UNKNOWN_DEV;
Mathieu Chartier93084622015-05-01 11:30:22 -0700341 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
342 whichHeap = HEAP_GL_DEV;
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700343 } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
344 whichHeap = HEAP_CURSOR;
345 } else if (strncmp(name, "/dev/ashmem", 11)) {
346 whichHeap = HEAP_ASHMEM;
Mathieu Chartier93084622015-05-01 11:30:22 -0700347 }
Colin Crosscb4728f2013-08-09 13:23:46 -0700348 } else if (strncmp(name, "[anon:", 6) == 0) {
349 whichHeap = HEAP_UNKNOWN;
Joel Fernandes (Google)299488d2018-08-20 23:43:55 -0700350 if (strncmp(name, "[anon:dalvik-", 13) == 0) {
351 whichHeap = HEAP_DALVIK_OTHER;
352 if (strstr(name, "[anon:dalvik-LinearAlloc") == name) {
353 subHeap = HEAP_DALVIK_OTHER_LINEARALLOC;
354 } else if ((strstr(name, "[anon:dalvik-alloc space") == name) ||
355 (strstr(name, "[anon:dalvik-main space") == name)) {
356 // This is the regular Dalvik heap.
357 whichHeap = HEAP_DALVIK;
358 subHeap = HEAP_DALVIK_NORMAL;
359 } else if (strstr(name, "[anon:dalvik-large object space") == name ||
360 strstr(name, "[anon:dalvik-free list large object space")
361 == name) {
362 whichHeap = HEAP_DALVIK;
363 subHeap = HEAP_DALVIK_LARGE;
364 } else if (strstr(name, "[anon:dalvik-non moving space") == name) {
365 whichHeap = HEAP_DALVIK;
366 subHeap = HEAP_DALVIK_NON_MOVING;
367 } else if (strstr(name, "[anon:dalvik-zygote space") == name) {
368 whichHeap = HEAP_DALVIK;
369 subHeap = HEAP_DALVIK_ZYGOTE;
370 } else if (strstr(name, "[anon:dalvik-indirect ref") == name) {
371 subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
372 } else if (strstr(name, "[anon:dalvik-jit-code-cache") == name ||
373 strstr(name, "[anon:dalvik-data-code-cache") == name) {
374 subHeap = HEAP_DALVIK_OTHER_CODE_CACHE;
375 } else if (strstr(name, "[anon:dalvik-CompilerMetadata") == name) {
376 subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
377 } else {
378 subHeap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting.
379 }
380 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700381 } else if (nameLen > 0) {
382 whichHeap = HEAP_UNKNOWN_MAP;
383 } else if (start == prevEnd && prevHeap == HEAP_SO) {
384 // bss section of a shared library.
385 whichHeap = HEAP_SO;
386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 }
388
Steve Block6215d3f2012-01-04 20:05:49 +0000389 //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 // isSqliteHeap, line);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800391
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700392 shared_clean = 0;
393 shared_dirty = 0;
394 private_clean = 0;
395 private_dirty = 0;
396 swapped_out = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800397 swapped_out_pss = 0;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 while (true) {
400 if (fgets(line, 1024, fp) == 0) {
401 done = true;
402 break;
403 }
404
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700405 if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800406 /* size = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700407 } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800408 rss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700409 } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 pss = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700411 } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 shared_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700413 } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 shared_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700415 } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 private_clean = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700417 } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 private_dirty = temp;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700419 } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) {
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800420 /* referenced = temp; */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700421 } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) {
422 swapped_out = temp;
Martijn Coenene0764852016-01-07 17:04:22 -0800423 } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) {
424 *foundSwapPss = true;
425 swapped_out_pss = temp;
Colin Cross0c6bc732014-06-17 15:18:07 -0700426 } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 // looks like a new mapping
Grace Klobabd511162009-07-08 23:32:25 -0700428 // example: "10000000-10001000 ---p 10000000 00:00 0"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 break;
430 }
431 }
432
433 if (!skip) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700434 if (is_swappable && (pss > 0)) {
435 sharing_proportion = 0.0;
436 if ((shared_clean > 0) || (shared_dirty > 0)) {
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700437 sharing_proportion = (pss - private_clean
438 - private_dirty)/(shared_clean+shared_dirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700439 }
440 swappable_pss = (sharing_proportion*shared_clean) + private_clean;
441 } else
442 swappable_pss = 0;
443
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700444 stats[whichHeap].pss += pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700445 stats[whichHeap].swappablePss += swappable_pss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800446 stats[whichHeap].rss += rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700447 stats[whichHeap].privateDirty += private_dirty;
448 stats[whichHeap].sharedDirty += shared_dirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700449 stats[whichHeap].privateClean += private_clean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700450 stats[whichHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700451 stats[whichHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800452 stats[whichHeap].swappedOutPss += swapped_out_pss;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700453 if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER ||
454 whichHeap == HEAP_DEX || whichHeap == HEAP_ART) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700455 stats[subHeap].pss += pss;
456 stats[subHeap].swappablePss += swappable_pss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800457 stats[subHeap].rss += rss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700458 stats[subHeap].privateDirty += private_dirty;
459 stats[subHeap].sharedDirty += shared_dirty;
460 stats[subHeap].privateClean += private_clean;
461 stats[subHeap].sharedClean += shared_clean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700462 stats[subHeap].swappedOut += swapped_out;
Martijn Coenene0764852016-01-07 17:04:22 -0800463 stats[subHeap].swappedOutPss += swapped_out_pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 }
467}
468
Martijn Coenene0764852016-01-07 17:04:22 -0800469static void load_maps(int pid, stats_t* stats, bool* foundSwapPss)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470{
Andreas Gampece711492016-10-04 18:06:42 -0700471 *foundSwapPss = false;
472
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700473 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
474 UniqueFile fp = MakeUniqueFile(smaps_path.c_str(), "re");
475 if (fp == nullptr) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700477 read_mapinfo(fp.get(), stats, foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478}
479
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700480static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
481 jint pid, jobject object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482{
Martijn Coenene0764852016-01-07 17:04:22 -0800483 bool foundSwapPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 stats_t stats[_NUM_HEAP];
485 memset(&stats, 0, sizeof(stats));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800486
Martijn Coenene0764852016-01-07 17:04:22 -0800487 load_maps(pid, stats, &foundSwapPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700489 struct graphics_memory_pss graphics_mem;
490 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
491 stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
492 stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800493 stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700494 stats[HEAP_GL].pss = graphics_mem.gl;
495 stats[HEAP_GL].privateDirty = graphics_mem.gl;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800496 stats[HEAP_GL].rss = graphics_mem.gl;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700497 stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
498 stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800499 stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700500 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700501
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700502 for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700503 stats[HEAP_UNKNOWN].pss += stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700504 stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800505 stats[HEAP_UNKNOWN].rss += stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700506 stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
507 stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700508 stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700509 stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700510 stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800511 stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700512 }
513
514 for (int i=0; i<_NUM_CORE_HEAP; i++) {
515 env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700516 env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800517 env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700518 env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
519 env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700520 env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700521 env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700522 env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800523 env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700524 }
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800525
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700526
Martijn Coenene0764852016-01-07 17:04:22 -0800527 env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700528 jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800529
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700530 jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
531 if (otherArray == NULL) {
532 return;
533 }
534
535 int j=0;
536 for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
537 otherArray[j++] = stats[i].pss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700538 otherArray[j++] = stats[i].swappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800539 otherArray[j++] = stats[i].rss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700540 otherArray[j++] = stats[i].privateDirty;
541 otherArray[j++] = stats[i].sharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700542 otherArray[j++] = stats[i].privateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700543 otherArray[j++] = stats[i].sharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700544 otherArray[j++] = stats[i].swappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800545 otherArray[j++] = stats[i].swappedOutPss;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700546 }
547
548 env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549}
550
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700551static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
552{
553 android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
554}
555
Daniel Colascione6c518102017-07-27 03:33:34 -0700556UniqueFile OpenSmapsOrRollup(int pid)
557{
558 enum pss_rollup_support rollup_support =
559 g_pss_rollup_support.load(std::memory_order_relaxed);
560 if (rollup_support != PSS_ROLLUP_UNSUPPORTED) {
561 std::string smaps_rollup_path =
562 base::StringPrintf("/proc/%d/smaps_rollup", pid);
563 UniqueFile fp_rollup = MakeUniqueFile(smaps_rollup_path.c_str(), "re");
564 if (fp_rollup == nullptr && errno != ENOENT) {
565 return fp_rollup; // Actual error, not just old kernel.
566 }
567 if (fp_rollup != nullptr) {
568 if (rollup_support == PSS_ROLLUP_UNTRIED) {
569 ALOGI("using rollup pss collection");
570 g_pss_rollup_support.store(PSS_ROLLUP_SUPPORTED,
571 std::memory_order_relaxed);
572 }
573 return fp_rollup;
574 }
575 g_pss_rollup_support.store(PSS_ROLLUP_UNSUPPORTED,
576 std::memory_order_relaxed);
577 }
578
579 std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
580 return MakeUniqueFile(smaps_path.c_str(), "re");
581}
582
Martijn Coenene0764852016-01-07 17:04:22 -0800583static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
Dianne Hackborne17b4452018-01-10 13:15:40 -0800584 jlongArray outUssSwapPssRss, jlongArray outMemtrack)
Dianne Hackbornb437e092011-08-05 17:50:29 -0700585{
Dianne Hackborne17b4452018-01-10 13:15:40 -0800586 char lineBuffer[1024];
Dianne Hackbornb437e092011-08-05 17:50:29 -0700587 jlong pss = 0;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800588 jlong rss = 0;
Martijn Coenene0764852016-01-07 17:04:22 -0800589 jlong swapPss = 0;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700590 jlong uss = 0;
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800591 jlong memtrack = 0;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700592
Adam Lesinski5b4ef812013-09-23 10:06:09 -0700593 struct graphics_memory_pss graphics_mem;
594 if (read_memtrack_memory(pid, &graphics_mem) == 0) {
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700595 pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700596 }
597
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700598 {
Daniel Colascione6c518102017-07-27 03:33:34 -0700599 UniqueFile fp = OpenSmapsOrRollup(pid);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700600
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700601 if (fp != nullptr) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800602 char* line;
603
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700604 while (true) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800605 if (fgets(lineBuffer, sizeof (lineBuffer), fp.get()) == NULL) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700606 break;
607 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800608 line = lineBuffer;
Dianne Hackbornb437e092011-08-05 17:50:29 -0700609
Dianne Hackborne17b4452018-01-10 13:15:40 -0800610 switch (line[0]) {
611 case 'P':
612 if (strncmp(line, "Pss:", 4) == 0) {
613 char* c = line + 4;
614 while (*c != 0 && (*c < '0' || *c > '9')) {
615 c++;
616 }
617 pss += atoi(c);
618 } else if (strncmp(line, "Private_Clean:", 14) == 0
619 || strncmp(line, "Private_Dirty:", 14) == 0) {
620 char* c = line + 14;
621 while (*c != 0 && (*c < '0' || *c > '9')) {
622 c++;
623 }
624 uss += atoi(c);
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700625 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800626 break;
627 case 'R':
628 if (strncmp(line, "Rss:", 4) == 0) {
629 char* c = line + 4;
630 while (*c != 0 && (*c < '0' || *c > '9')) {
631 c++;
632 }
633 rss += atoi(c);
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700634 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800635 break;
636 case 'S':
637 if (strncmp(line, "SwapPss:", 8) == 0) {
638 char* c = line + 8;
639 jlong lSwapPss;
640 while (*c != 0 && (*c < '0' || *c > '9')) {
641 c++;
642 }
643 lSwapPss = atoi(c);
644 swapPss += lSwapPss;
645 pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP
646 }
647 break;
Dianne Hackbornc8230512013-07-13 21:32:12 -0700648 }
Dianne Hackborncfc837f2013-06-27 18:32:07 -0700649 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700650 }
Dianne Hackborn37c99432013-09-05 19:34:57 -0700651 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700652
Dianne Hackborne17b4452018-01-10 13:15:40 -0800653 if (outUssSwapPssRss != NULL) {
654 if (env->GetArrayLength(outUssSwapPssRss) >= 1) {
655 jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
656 if (outUssSwapPssRssArray != NULL) {
657 outUssSwapPssRssArray[0] = uss;
658 if (env->GetArrayLength(outUssSwapPssRss) >= 2) {
659 outUssSwapPssRssArray[1] = swapPss;
660 }
661 if (env->GetArrayLength(outUssSwapPssRss) >= 3) {
662 outUssSwapPssRssArray[2] = rss;
Martijn Coenene0764852016-01-07 17:04:22 -0800663 }
Dianne Hackbornc8230512013-07-13 21:32:12 -0700664 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800665 env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0);
Dianne Hackbornc8230512013-07-13 21:32:12 -0700666 }
667 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700668
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800669 if (outMemtrack != NULL) {
670 if (env->GetArrayLength(outMemtrack) >= 1) {
671 jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
672 if (outMemtrackArray != NULL) {
673 outMemtrackArray[0] = memtrack;
674 }
675 env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
676 }
677 }
678
Dianne Hackbornb437e092011-08-05 17:50:29 -0700679 return pss;
680}
681
682static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
683{
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -0800684 return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700685}
686
Martijn Coenen398e24e2015-04-23 21:55:58 +0200687static long get_allocated_vmalloc_memory() {
688 char line[1024];
Sandeep Patild70e2022018-11-20 17:26:56 -0800689
690 long vmalloc_allocated_size = 0;
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700691
692 UniqueFile fp = MakeUniqueFile("/proc/vmallocinfo", "re");
693 if (fp == nullptr) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200694 return 0;
695 }
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700696
Martijn Coenen398e24e2015-04-23 21:55:58 +0200697 while (true) {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700698 if (fgets(line, 1024, fp.get()) == NULL) {
Martijn Coenen398e24e2015-04-23 21:55:58 +0200699 break;
700 }
Sandeep Patild70e2022018-11-20 17:26:56 -0800701
702 // check to see if there are pages mapped in vmalloc area
703 if (!strstr(line, "pages=")) {
704 continue;
Martijn Coenen6b8a5222016-04-27 14:28:17 +0200705 }
Sandeep Patild70e2022018-11-20 17:26:56 -0800706
707 long nr_pages;
708 if (sscanf(line, "%*x-%*x %*ld %*s pages=%ld", &nr_pages) == 1) {
709 vmalloc_allocated_size += (nr_pages * getpagesize());
Martijn Coenen398e24e2015-04-23 21:55:58 +0200710 }
711 }
Martijn Coenen398e24e2015-04-23 21:55:58 +0200712 return vmalloc_allocated_size;
713}
714
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700715enum {
716 MEMINFO_TOTAL,
717 MEMINFO_FREE,
718 MEMINFO_BUFFERS,
719 MEMINFO_CACHED,
720 MEMINFO_SHMEM,
721 MEMINFO_SLAB,
Robert Benea5e099802017-10-04 18:28:01 -0700722 MEMINFO_SLAB_RECLAIMABLE,
723 MEMINFO_SLAB_UNRECLAIMABLE,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700724 MEMINFO_SWAP_TOTAL,
725 MEMINFO_SWAP_FREE,
726 MEMINFO_ZRAM_TOTAL,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700727 MEMINFO_MAPPED,
728 MEMINFO_VMALLOC_USED,
729 MEMINFO_PAGE_TABLES,
730 MEMINFO_KERNEL_STACK,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700731 MEMINFO_COUNT
732};
733
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800734static long long get_zram_mem_used()
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800735{
736#define ZRAM_SYSFS "/sys/block/zram0/"
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700737 UniqueFile mm_stat_file = MakeUniqueFile(ZRAM_SYSFS "mm_stat", "re");
738 if (mm_stat_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800739 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800740
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700741 int matched = fscanf(mm_stat_file.get(), "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800742 if (matched != 1)
743 ALOGW("failed to parse " ZRAM_SYSFS "mm_stat");
744
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800745 return mem_used_total;
746 }
747
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700748 UniqueFile mem_used_total_file = MakeUniqueFile(ZRAM_SYSFS "mem_used_total", "re");
749 if (mem_used_total_file) {
Greg Hackmann228fa5f2015-12-09 16:36:08 -0800750 long long mem_used_total = 0;
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800751
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700752 int matched = fscanf(mem_used_total_file.get(), "%lld", &mem_used_total);
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800753 if (matched != 1)
754 ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total");
755
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800756 return mem_used_total;
757 }
758
759 return 0;
760}
761
Dianne Hackborn8e692572013-09-10 19:06:15 -0700762static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
763{
Dianne Hackbornb79a5f92017-12-08 17:09:26 -0800764 char buffer[4096];
Martijn Coenen398e24e2015-04-23 21:55:58 +0200765 size_t numFound = 0;
Dianne Hackborn8e692572013-09-10 19:06:15 -0700766
767 if (out == NULL) {
768 jniThrowNullPointerException(env, "out == null");
769 return;
770 }
771
Nick Kralevich422dd00f2017-10-19 17:51:50 -0700772 int fd = open("/proc/meminfo", O_RDONLY | O_CLOEXEC);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700773
774 if (fd < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700775 ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno));
Dianne Hackborn8e692572013-09-10 19:06:15 -0700776 return;
777 }
778
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700779 int len = read(fd, buffer, sizeof(buffer)-1);
Dianne Hackborn8e692572013-09-10 19:06:15 -0700780 close(fd);
781
782 if (len < 0) {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700783 ALOGW("Empty /proc/meminfo");
Dianne Hackborn8e692572013-09-10 19:06:15 -0700784 return;
785 }
786 buffer[len] = 0;
787
788 static const char* const tags[] = {
789 "MemTotal:",
790 "MemFree:",
791 "Buffers:",
792 "Cached:",
793 "Shmem:",
794 "Slab:",
Robert Benea5e099802017-10-04 18:28:01 -0700795 "SReclaimable:",
796 "SUnreclaim:",
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700797 "SwapTotal:",
798 "SwapFree:",
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700799 "ZRam:",
800 "Mapped:",
801 "VmallocUsed:",
802 "PageTables:",
803 "KernelStack:",
Dianne Hackborn8e692572013-09-10 19:06:15 -0700804 NULL
805 };
806 static const int tagsLen[] = {
807 9,
808 8,
809 8,
810 7,
811 6,
812 5,
Robert Benea5e099802017-10-04 18:28:01 -0700813 13,
814 11,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700815 10,
816 9,
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -0700817 5,
818 7,
819 12,
820 11,
821 12,
Dianne Hackborn8e692572013-09-10 19:06:15 -0700822 0
823 };
Robert Benea5e099802017-10-04 18:28:01 -0700824 long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Dianne Hackborn8e692572013-09-10 19:06:15 -0700825
826 char* p = buffer;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200827 while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700828 int i = 0;
829 while (tags[i]) {
830 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
831 p += tagsLen[i];
832 while (*p == ' ') p++;
833 char* num = p;
834 while (*p >= '0' && *p <= '9') p++;
835 if (*p != 0) {
836 *p = 0;
837 p++;
838 }
839 mem[i] = atoll(num);
840 numFound++;
841 break;
842 }
843 i++;
844 }
845 while (*p && *p != '\n') {
846 p++;
847 }
848 if (*p) p++;
849 }
850
Greg Hackmann187a6ae2015-12-09 09:36:12 -0800851 mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024;
Martijn Coenen398e24e2015-04-23 21:55:58 +0200852 // Recompute Vmalloc Used since the value in meminfo
853 // doesn't account for I/O remapping which doesn't use RAM.
854 mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024;
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700855
Dianne Hackborn8e692572013-09-10 19:06:15 -0700856 int maxNum = env->GetArrayLength(out);
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700857 if (maxNum > MEMINFO_COUNT) {
858 maxNum = MEMINFO_COUNT;
859 }
Dianne Hackborn8e692572013-09-10 19:06:15 -0700860 jlong* outArray = env->GetLongArrayElements(out, 0);
861 if (outArray != NULL) {
Dianne Hackborncb428552013-09-26 11:07:17 -0700862 for (int i=0; i<maxNum; i++) {
Dianne Hackborn8e692572013-09-10 19:06:15 -0700863 outArray[i] = mem[i];
864 }
865 }
866 env->ReleaseLongArrayElements(out, outArray, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867}
868
Martijn Coenen398e24e2015-04-23 21:55:58 +0200869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870static jint read_binder_stat(const char* stat)
871{
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700872 UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
873 if (fp == nullptr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 return -1;
875 }
876
877 char line[1024];
878
879 char compare[128];
880 int len = snprintf(compare, 128, "proc %d", getpid());
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 // loop until we have the block that represents this process
883 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700884 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 return -1;
886 }
887 } while (strncmp(compare, line, len));
888
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800889 // 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 -0800890 len = snprintf(compare, 128, " %s: ", stat);
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 do {
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700893 if (fgets(line, 1024, fp.get()) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 return -1;
895 }
896 } while (strncmp(compare, line, len));
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // we have the line, now increment the line ptr to the value
899 char* ptr = line + len;
Elliott Hughesc367d482013-10-29 13:12:55 -0700900 jint result = atoi(ptr);
Elliott Hughesc367d482013-10-29 13:12:55 -0700901 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902}
903
904static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
905{
906 return read_binder_stat("bcTRANSACTION");
907}
908
909static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
910{
911 return read_binder_stat("brTRANSACTION");
912}
913
914// these are implemented in android_util_Binder.cpp
915jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
916jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
917jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
918
Christopher Ferris8d652f82017-04-11 16:29:18 -0700919static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
Andy McFadden06a6b552010-07-13 16:28:09 -0700920{
921 if (fileDescriptor == NULL) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -0800922 jniThrowNullPointerException(env, "fd == null");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700923 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700924 }
925 int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
926 if (origFd < 0) {
927 jniThrowRuntimeException(env, "Invalid file descriptor");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700928 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700929 }
930
931 /* dup() the descriptor so we don't close the original with fclose() */
932 int fd = dup(origFd);
933 if (fd < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000934 ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700935 jniThrowRuntimeException(env, "dup() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700936 return false;
Andy McFadden06a6b552010-07-13 16:28:09 -0700937 }
938
Christopher Ferris8d652f82017-04-11 16:29:18 -0700939 fp.reset(fdopen(fd, "w"));
Andreas Gampe7ad2f6e2016-10-05 12:59:18 -0700940 if (fp == nullptr) {
Steve Block8564c8d2012-01-05 23:22:43 +0000941 ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
Andy McFadden06a6b552010-07-13 16:28:09 -0700942 close(fd);
943 jniThrowRuntimeException(env, "fdopen() failed");
Christopher Ferris8d652f82017-04-11 16:29:18 -0700944 return false;
945 }
946 return true;
947}
948
Christopher Ferris38e2c3b2018-06-13 13:19:57 -0700949/* pulled out of bionic */
950extern "C" void write_malloc_leak_info(FILE* fp);
951
Christopher Ferris8d652f82017-04-11 16:29:18 -0700952/*
953 * Dump the native heap, writing human-readable output to the specified
954 * file descriptor.
955 */
956static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
957 jobject fileDescriptor)
958{
959 UniqueFile fp(nullptr, safeFclose);
960 if (!openFile(env, fileDescriptor, fp)) {
Andy McFadden06a6b552010-07-13 16:28:09 -0700961 return;
962 }
963
Steve Block5baa3a62011-12-20 16:23:08 +0000964 ALOGD("Native heap dump starting...\n");
Christopher Ferris38e2c3b2018-06-13 13:19:57 -0700965 // Formatting of the native heap dump is handled by malloc debug itself.
966 // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
967 write_malloc_leak_info(fp.get());
Steve Block5baa3a62011-12-20 16:23:08 +0000968 ALOGD("Native heap dump complete.\n");
Andy McFadden06a6b552010-07-13 16:28:09 -0700969}
970
Christopher Ferris8d652f82017-04-11 16:29:18 -0700971/*
972 * Dump the native malloc info, writing xml output to the specified
973 * file descriptor.
974 */
975static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
976 jobject fileDescriptor)
977{
978 UniqueFile fp(nullptr, safeFclose);
979 if (!openFile(env, fileDescriptor, fp)) {
980 return;
981 }
982
983 malloc_info(0, fp.get());
984}
985
Narayan Kamathf013daa2017-05-09 12:55:02 +0100986static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
987 DebuggerdDumpType dumpType) {
988 const ScopedUtfChars fileNameChars(env, fileName);
989 if (fileNameChars.c_str() == nullptr) {
990 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700991 }
992
Narayan Kamathf013daa2017-05-09 12:55:02 +0100993 android::base::unique_fd fd(open(fileNameChars.c_str(),
994 O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
995 0666));
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700996 if (fd < 0) {
Narayan Kamathf013daa2017-05-09 12:55:02 +0100997 fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno));
998 return false;
Dianne Hackbornf72467a2012-06-08 17:23:59 -0700999 }
1000
Narayan Kamathf013daa2017-05-09 12:55:02 +01001001 return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0);
1002}
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001003
Narayan Kamathf013daa2017-05-09 12:55:02 +01001004static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1005 jint pid, jstring fileName, jint timeoutSecs) {
1006 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
1007 return ret ? JNI_TRUE : JNI_FALSE;
1008}
1009
1010static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
1011 jint pid, jstring fileName, jint timeoutSecs) {
1012 const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
1013 return ret ? JNI_TRUE : JNI_FALSE;
Dianne Hackbornf72467a2012-06-08 17:23:59 -07001014}
1015
Colin Crossc4fb5f92016-02-02 16:51:15 -08001016static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
1017 jint limit, jboolean contents)
1018{
1019 std::string s = GetUnreachableMemoryString(contents, limit);
1020 return env->NewStringUTF(s.c_str());
1021}
1022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023/*
1024 * JNI registration.
1025 */
1026
Daniel Micay76f6a862015-09-19 17:31:01 -04001027static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 { "getNativeHeapSize", "()J",
1029 (void*) android_os_Debug_getNativeHeapSize },
1030 { "getNativeHeapAllocatedSize", "()J",
1031 (void*) android_os_Debug_getNativeHeapAllocatedSize },
1032 { "getNativeHeapFreeSize", "()J",
1033 (void*) android_os_Debug_getNativeHeapFreeSize },
1034 { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V",
1035 (void*) android_os_Debug_getDirtyPages },
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001036 { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V",
1037 (void*) android_os_Debug_getDirtyPagesPid },
Dianne Hackbornb437e092011-08-05 17:50:29 -07001038 { "getPss", "()J",
1039 (void*) android_os_Debug_getPss },
Dianne Hackborn1a4b5a42014-12-08 17:43:31 -08001040 { "getPss", "(I[J[J)J",
Dianne Hackbornb437e092011-08-05 17:50:29 -07001041 (void*) android_os_Debug_getPssPid },
Dianne Hackborn8e692572013-09-10 19:06:15 -07001042 { "getMemInfo", "([J)V",
1043 (void*) android_os_Debug_getMemInfo },
Andy McFadden06a6b552010-07-13 16:28:09 -07001044 { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V",
1045 (void*) android_os_Debug_dumpNativeHeap },
Christopher Ferris8d652f82017-04-11 16:29:18 -07001046 { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V",
1047 (void*) android_os_Debug_dumpNativeMallocInfo },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 { "getBinderSentTransactions", "()I",
1049 (void*) android_os_Debug_getBinderSentTransactions },
1050 { "getBinderReceivedTransactions", "()I",
1051 (void*) android_os_getBinderReceivedTransactions },
1052 { "getBinderLocalObjectCount", "()I",
1053 (void*)android_os_Debug_getLocalObjectCount },
1054 { "getBinderProxyObjectCount", "()I",
1055 (void*)android_os_Debug_getProxyObjectCount },
1056 { "getBinderDeathObjectCount", "()I",
1057 (void*)android_os_Debug_getDeathObjectCount },
Narayan Kamathf013daa2017-05-09 12:55:02 +01001058 { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
1059 (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
1060 { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
songjinshie02e3ea2016-12-16 17:48:21 +08001061 (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
Colin Crossc4fb5f92016-02-02 16:51:15 -08001062 { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
1063 (void*)android_os_Debug_getUnreachableMemory },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064};
1065
1066int register_android_os_Debug(JNIEnv *env)
1067{
1068 jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001070 // Sanity check the number of other statistics expected in Java matches here.
1071 jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1072 jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001073 jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1074 jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001075 int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001076 if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001077 jniThrowExceptionFmt(env, "java/lang/RuntimeException",
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001078 "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1079 numOtherStats+numDvkStats, expectedNumOtherStats);
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001080 return JNI_ERR;
1081 }
1082
1083 otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
Martijn Coenene0764852016-01-07 17:04:22 -08001084 hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
Ian Rogers7c9f30b2013-02-27 10:57:13 -08001085
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001086 for (int i=0; i<_NUM_CORE_HEAP; i++) {
1087 stat_fields[i].pss_field =
1088 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001089 stat_fields[i].pssSwappable_field =
1090 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
Dianne Hackborne17b4452018-01-10 13:15:40 -08001091 stat_fields[i].rss_field =
1092 env->GetFieldID(clazz, stat_field_names[i].rss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001093 stat_fields[i].privateDirty_field =
1094 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1095 stat_fields[i].sharedDirty_field =
1096 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001097 stat_fields[i].privateClean_field =
1098 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07001099 stat_fields[i].sharedClean_field =
1100 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
Dianne Hackborn8883ced2013-10-02 16:58:06 -07001101 stat_fields[i].swappedOut_field =
1102 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
Martijn Coenene0764852016-01-07 17:04:22 -08001103 stat_fields[i].swappedOutPss_field =
1104 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001105 }
1106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1108}
1109
Andy McFadden06a6b552010-07-13 16:28:09 -07001110}; // namespace android