1 /*
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 
17 #define LOG_TAG "android.os.Debug"
18 
19 #include <assert.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <malloc.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/time.h>
29 #include <time.h>
30 #include <unistd.h>
31 
32 #include <iomanip>
33 #include <string>
34 #include <vector>
35 
36 #include <android-base/logging.h>
37 #include <android-base/properties.h>
38 #include <bionic/malloc.h>
39 #include <debuggerd/client.h>
40 #include <log/log.h>
41 #include <utils/misc.h>
42 #include <utils/String8.h>
43 
44 #include <nativehelper/JNIPlatformHelp.h>
45 #include <nativehelper/ScopedUtfChars.h>
46 #include "jni.h"
47 #include <dmabufinfo/dmabuf_sysfs_stats.h>
48 #include <dmabufinfo/dmabufinfo.h>
49 #include <meminfo/procmeminfo.h>
50 #include <meminfo/sysmeminfo.h>
51 #include <memtrack/memtrack.h>
52 #include <memunreachable/memunreachable.h>
53 #include <android-base/strings.h>
54 #include "android_os_Debug.h"
55 #include <vintf/VintfObject.h>
56 
57 namespace android
58 {
59 
60 enum {
61     HEAP_UNKNOWN,
62     HEAP_DALVIK,
63     HEAP_NATIVE,
64 
65     HEAP_DALVIK_OTHER,
66     HEAP_STACK,
67     HEAP_CURSOR,
68     HEAP_ASHMEM,
69     HEAP_GL_DEV,
70     HEAP_UNKNOWN_DEV,
71     HEAP_SO,
72     HEAP_JAR,
73     HEAP_APK,
74     HEAP_TTF,
75     HEAP_DEX,
76     HEAP_OAT,
77     HEAP_ART,
78     HEAP_UNKNOWN_MAP,
79     HEAP_GRAPHICS,
80     HEAP_GL,
81     HEAP_OTHER_MEMTRACK,
82 
83     // Dalvik extra sections (heap).
84     HEAP_DALVIK_NORMAL,
85     HEAP_DALVIK_LARGE,
86     HEAP_DALVIK_ZYGOTE,
87     HEAP_DALVIK_NON_MOVING,
88 
89     // Dalvik other extra sections.
90     HEAP_DALVIK_OTHER_LINEARALLOC,
91     HEAP_DALVIK_OTHER_ACCOUNTING,
92     HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE,
93     HEAP_DALVIK_OTHER_APP_CODE_CACHE,
94     HEAP_DALVIK_OTHER_COMPILER_METADATA,
95     HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE,
96 
97     // Boot vdex / app dex / app vdex
98     HEAP_DEX_BOOT_VDEX,
99     HEAP_DEX_APP_DEX,
100     HEAP_DEX_APP_VDEX,
101 
102     // App art, boot art.
103     HEAP_ART_APP,
104     HEAP_ART_BOOT,
105 
106     _NUM_HEAP,
107     _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1,
108     _NUM_CORE_HEAP = HEAP_NATIVE+1
109 };
110 
111 struct stat_fields {
112     jfieldID pss_field;
113     jfieldID pssSwappable_field;
114     jfieldID rss_field;
115     jfieldID privateDirty_field;
116     jfieldID sharedDirty_field;
117     jfieldID privateClean_field;
118     jfieldID sharedClean_field;
119     jfieldID swappedOut_field;
120     jfieldID swappedOutPss_field;
121 };
122 
123 struct stat_field_names {
124     const char* pss_name;
125     const char* pssSwappable_name;
126     const char* rss_name;
127     const char* privateDirty_name;
128     const char* sharedDirty_name;
129     const char* privateClean_name;
130     const char* sharedClean_name;
131     const char* swappedOut_name;
132     const char* swappedOutPss_name;
133 };
134 
135 static stat_fields stat_fields[_NUM_CORE_HEAP];
136 
137 static stat_field_names stat_field_names[_NUM_CORE_HEAP] = {
138     { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty",
139         "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" },
140     { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty",
141         "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" },
142     { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty",
143         "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" }
144 };
145 
146 static jfieldID otherStats_field;
147 static jfieldID hasSwappedOutPss_field;
148 
149 struct stats_t {
150     int pss;
151     int swappablePss;
152     int rss;
153     int privateDirty;
154     int sharedDirty;
155     int privateClean;
156     int sharedClean;
157     int swappedOut;
158     int swappedOutPss;
159 };
160 
161 #define BINDER_STATS "/proc/binder/stats"
162 
android_os_Debug_getNativeHeapSize(JNIEnv * env,jobject clazz)163 static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz)
164 {
165     struct mallinfo info = mallinfo();
166     return (jlong) info.usmblks;
167 }
168 
android_os_Debug_getNativeHeapAllocatedSize(JNIEnv * env,jobject clazz)169 static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz)
170 {
171     struct mallinfo info = mallinfo();
172     return (jlong) info.uordblks;
173 }
174 
android_os_Debug_getNativeHeapFreeSize(JNIEnv * env,jobject clazz)175 static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz)
176 {
177     struct mallinfo info = mallinfo();
178     return (jlong) info.fordblks;
179 }
180 
181 // Container used to retrieve graphics memory pss
182 struct graphics_memory_pss
183 {
184     int graphics;
185     int gl;
186     int other;
187 };
188 
189 /*
190  * Uses libmemtrack to retrieve graphics memory that the process is using.
191  * Any graphics memory reported in /proc/pid/smaps is not included here.
192  */
read_memtrack_memory(struct memtrack_proc * p,int pid,struct graphics_memory_pss * graphics_mem)193 static int read_memtrack_memory(struct memtrack_proc* p, int pid,
194         struct graphics_memory_pss* graphics_mem)
195 {
196     int err = memtrack_proc_get(p, pid);
197     if (err != 0) {
198         // The memtrack HAL may not be available, do not log to avoid flooding
199         // logcat.
200         return err;
201     }
202 
203     ssize_t pss = memtrack_proc_graphics_pss(p);
204     if (pss < 0) {
205         ALOGW("failed to get graphics pss: %zd", pss);
206         return pss;
207     }
208     graphics_mem->graphics = pss / 1024;
209 
210     pss = memtrack_proc_gl_pss(p);
211     if (pss < 0) {
212         ALOGW("failed to get gl pss: %zd", pss);
213         return pss;
214     }
215     graphics_mem->gl = pss / 1024;
216 
217     pss = memtrack_proc_other_pss(p);
218     if (pss < 0) {
219         ALOGW("failed to get other pss: %zd", pss);
220         return pss;
221     }
222     graphics_mem->other = pss / 1024;
223 
224     return 0;
225 }
226 
227 /*
228  * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps.
229  */
read_memtrack_memory(int pid,struct graphics_memory_pss * graphics_mem)230 static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem)
231 {
232     struct memtrack_proc* p = memtrack_proc_new();
233     if (p == NULL) {
234         ALOGW("failed to create memtrack_proc");
235         return -1;
236     }
237 
238     int err = read_memtrack_memory(p, pid, graphics_mem);
239     memtrack_proc_destroy(p);
240     return err;
241 }
242 
load_maps(int pid,stats_t * stats,bool * foundSwapPss)243 static bool load_maps(int pid, stats_t* stats, bool* foundSwapPss)
244 {
245     *foundSwapPss = false;
246     uint64_t prev_end = 0;
247     int prev_heap = HEAP_UNKNOWN;
248 
249     std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid);
250     auto vma_scan = [&](const meminfo::Vma& vma) {
251         int which_heap = HEAP_UNKNOWN;
252         int sub_heap = HEAP_UNKNOWN;
253         bool is_swappable = false;
254         std::string name;
255         if (base::EndsWith(vma.name, " (deleted)")) {
256             name = vma.name.substr(0, vma.name.size() - strlen(" (deleted)"));
257         } else {
258             name = vma.name;
259         }
260 
261         uint32_t namesz = name.size();
262         if (base::StartsWith(name, "[heap]")) {
263             which_heap = HEAP_NATIVE;
264         } else if (base::StartsWith(name, "[anon:libc_malloc]")) {
265             which_heap = HEAP_NATIVE;
266         } else if (base::StartsWith(name, "[anon:scudo:")) {
267             which_heap = HEAP_NATIVE;
268         } else if (base::StartsWith(name, "[anon:GWP-ASan")) {
269             which_heap = HEAP_NATIVE;
270         } else if (base::StartsWith(name, "[stack")) {
271             which_heap = HEAP_STACK;
272         } else if (base::StartsWith(name, "[anon:stack_and_tls:")) {
273             which_heap = HEAP_STACK;
274         } else if (base::EndsWith(name, ".so")) {
275             which_heap = HEAP_SO;
276             is_swappable = true;
277         } else if (base::EndsWith(name, ".jar")) {
278             which_heap = HEAP_JAR;
279             is_swappable = true;
280         } else if (base::EndsWith(name, ".apk")) {
281             which_heap = HEAP_APK;
282             is_swappable = true;
283         } else if (base::EndsWith(name, ".ttf")) {
284             which_heap = HEAP_TTF;
285             is_swappable = true;
286         } else if ((base::EndsWith(name, ".odex")) ||
287                 (namesz > 4 && strstr(name.c_str(), ".dex") != nullptr)) {
288             which_heap = HEAP_DEX;
289             sub_heap = HEAP_DEX_APP_DEX;
290             is_swappable = true;
291         } else if (base::EndsWith(name, ".vdex")) {
292             which_heap = HEAP_DEX;
293             // Handle system@framework@boot and system/framework/boot|apex
294             if ((strstr(name.c_str(), "@boot") != nullptr) ||
295                     (strstr(name.c_str(), "/boot") != nullptr) ||
296                     (strstr(name.c_str(), "/apex") != nullptr)) {
297                 sub_heap = HEAP_DEX_BOOT_VDEX;
298             } else {
299                 sub_heap = HEAP_DEX_APP_VDEX;
300             }
301             is_swappable = true;
302         } else if (base::EndsWith(name, ".oat")) {
303             which_heap = HEAP_OAT;
304             is_swappable = true;
305         } else if (base::EndsWith(name, ".art") || base::EndsWith(name, ".art]")) {
306             which_heap = HEAP_ART;
307             // Handle system@framework@boot* and system/framework/boot|apex*
308             if ((strstr(name.c_str(), "@boot") != nullptr) ||
309                     (strstr(name.c_str(), "/boot") != nullptr) ||
310                     (strstr(name.c_str(), "/apex") != nullptr)) {
311                 sub_heap = HEAP_ART_BOOT;
312             } else {
313                 sub_heap = HEAP_ART_APP;
314             }
315             is_swappable = true;
316         } else if (base::StartsWith(name, "/dev/")) {
317             which_heap = HEAP_UNKNOWN_DEV;
318             if (base::StartsWith(name, "/dev/kgsl-3d0")) {
319                 which_heap = HEAP_GL_DEV;
320             } else if (base::StartsWith(name, "/dev/ashmem/CursorWindow")) {
321                 which_heap = HEAP_CURSOR;
322             } else if (base::StartsWith(name, "/dev/ashmem/jit-zygote-cache")) {
323                 which_heap = HEAP_DALVIK_OTHER;
324                 sub_heap = HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE;
325             } else if (base::StartsWith(name, "/dev/ashmem")) {
326                 which_heap = HEAP_ASHMEM;
327             }
328         } else if (base::StartsWith(name, "/memfd:jit-cache")) {
329           which_heap = HEAP_DALVIK_OTHER;
330           sub_heap = HEAP_DALVIK_OTHER_APP_CODE_CACHE;
331         } else if (base::StartsWith(name, "/memfd:jit-zygote-cache")) {
332           which_heap = HEAP_DALVIK_OTHER;
333           sub_heap = HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE;
334         } else if (base::StartsWith(name, "[anon:")) {
335             which_heap = HEAP_UNKNOWN;
336             if (base::StartsWith(name, "[anon:dalvik-")) {
337                 which_heap = HEAP_DALVIK_OTHER;
338                 if (base::StartsWith(name, "[anon:dalvik-LinearAlloc")) {
339                     sub_heap = HEAP_DALVIK_OTHER_LINEARALLOC;
340                 } else if (base::StartsWith(name, "[anon:dalvik-alloc space") ||
341                         base::StartsWith(name, "[anon:dalvik-main space")) {
342                     // This is the regular Dalvik heap.
343                     which_heap = HEAP_DALVIK;
344                     sub_heap = HEAP_DALVIK_NORMAL;
345                 } else if (base::StartsWith(name,
346                             "[anon:dalvik-large object space") ||
347                         base::StartsWith(
348                             name, "[anon:dalvik-free list large object space")) {
349                     which_heap = HEAP_DALVIK;
350                     sub_heap = HEAP_DALVIK_LARGE;
351                 } else if (base::StartsWith(name, "[anon:dalvik-non moving space")) {
352                     which_heap = HEAP_DALVIK;
353                     sub_heap = HEAP_DALVIK_NON_MOVING;
354                 } else if (base::StartsWith(name, "[anon:dalvik-zygote space")) {
355                     which_heap = HEAP_DALVIK;
356                     sub_heap = HEAP_DALVIK_ZYGOTE;
357                 } else if (base::StartsWith(name, "[anon:dalvik-indirect ref")) {
358                     sub_heap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
359                 } else if (base::StartsWith(name, "[anon:dalvik-jit-code-cache") ||
360                         base::StartsWith(name, "[anon:dalvik-data-code-cache")) {
361                     sub_heap = HEAP_DALVIK_OTHER_APP_CODE_CACHE;
362                 } else if (base::StartsWith(name, "[anon:dalvik-CompilerMetadata")) {
363                     sub_heap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
364                 } else {
365                     sub_heap = HEAP_DALVIK_OTHER_ACCOUNTING;  // Default to accounting.
366                 }
367             }
368         } else if (namesz > 0) {
369             which_heap = HEAP_UNKNOWN_MAP;
370         } else if (vma.start == prev_end && prev_heap == HEAP_SO) {
371             // bss section of a shared library
372             which_heap = HEAP_SO;
373         }
374 
375         prev_end = vma.end;
376         prev_heap = which_heap;
377 
378         const meminfo::MemUsage& usage = vma.usage;
379         if (usage.swap_pss > 0 && *foundSwapPss != true) {
380             *foundSwapPss = true;
381         }
382 
383         uint64_t swapable_pss = 0;
384         if (is_swappable && (usage.pss > 0)) {
385             float sharing_proportion = 0.0;
386             if ((usage.shared_clean > 0) || (usage.shared_dirty > 0)) {
387                 sharing_proportion = (usage.pss - usage.uss) / (usage.shared_clean + usage.shared_dirty);
388             }
389             swapable_pss = (sharing_proportion * usage.shared_clean) + usage.private_clean;
390         }
391 
392         stats[which_heap].pss += usage.pss;
393         stats[which_heap].swappablePss += swapable_pss;
394         stats[which_heap].rss += usage.rss;
395         stats[which_heap].privateDirty += usage.private_dirty;
396         stats[which_heap].sharedDirty += usage.shared_dirty;
397         stats[which_heap].privateClean += usage.private_clean;
398         stats[which_heap].sharedClean += usage.shared_clean;
399         stats[which_heap].swappedOut += usage.swap;
400         stats[which_heap].swappedOutPss += usage.swap_pss;
401         if (which_heap == HEAP_DALVIK || which_heap == HEAP_DALVIK_OTHER ||
402                 which_heap == HEAP_DEX || which_heap == HEAP_ART) {
403             stats[sub_heap].pss += usage.pss;
404             stats[sub_heap].swappablePss += swapable_pss;
405             stats[sub_heap].rss += usage.rss;
406             stats[sub_heap].privateDirty += usage.private_dirty;
407             stats[sub_heap].sharedDirty += usage.shared_dirty;
408             stats[sub_heap].privateClean += usage.private_clean;
409             stats[sub_heap].sharedClean += usage.shared_clean;
410             stats[sub_heap].swappedOut += usage.swap;
411             stats[sub_heap].swappedOutPss += usage.swap_pss;
412         }
413     };
414 
415     return meminfo::ForEachVmaFromFile(smaps_path, vma_scan);
416 }
417 
android_os_Debug_getDirtyPagesPid(JNIEnv * env,jobject clazz,jint pid,jobject object)418 static jboolean android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz,
419         jint pid, jobject object)
420 {
421     bool foundSwapPss;
422     stats_t stats[_NUM_HEAP];
423     memset(&stats, 0, sizeof(stats));
424 
425     if (!load_maps(pid, stats, &foundSwapPss)) {
426         return JNI_FALSE;
427     }
428 
429     struct graphics_memory_pss graphics_mem;
430     if (read_memtrack_memory(pid, &graphics_mem) == 0) {
431         stats[HEAP_GRAPHICS].pss = graphics_mem.graphics;
432         stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics;
433         stats[HEAP_GRAPHICS].rss = graphics_mem.graphics;
434         stats[HEAP_GL].pss = graphics_mem.gl;
435         stats[HEAP_GL].privateDirty = graphics_mem.gl;
436         stats[HEAP_GL].rss = graphics_mem.gl;
437         stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
438         stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
439         stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other;
440     }
441 
442     for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
443         stats[HEAP_UNKNOWN].pss += stats[i].pss;
444         stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss;
445         stats[HEAP_UNKNOWN].rss += stats[i].rss;
446         stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty;
447         stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty;
448         stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean;
449         stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean;
450         stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut;
451         stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss;
452     }
453 
454     for (int i=0; i<_NUM_CORE_HEAP; i++) {
455         env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss);
456         env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss);
457         env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss);
458         env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty);
459         env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty);
460         env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean);
461         env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean);
462         env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut);
463         env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss);
464     }
465 
466 
467     env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss);
468     jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field);
469 
470     jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0);
471     if (otherArray == NULL) {
472         return JNI_FALSE;
473     }
474 
475     int j=0;
476     for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) {
477         otherArray[j++] = stats[i].pss;
478         otherArray[j++] = stats[i].swappablePss;
479         otherArray[j++] = stats[i].rss;
480         otherArray[j++] = stats[i].privateDirty;
481         otherArray[j++] = stats[i].sharedDirty;
482         otherArray[j++] = stats[i].privateClean;
483         otherArray[j++] = stats[i].sharedClean;
484         otherArray[j++] = stats[i].swappedOut;
485         otherArray[j++] = stats[i].swappedOutPss;
486     }
487 
488     env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0);
489     return JNI_TRUE;
490 }
491 
android_os_Debug_getDirtyPages(JNIEnv * env,jobject clazz,jobject object)492 static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object)
493 {
494     android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object);
495 }
496 
android_os_Debug_getPssPid(JNIEnv * env,jobject clazz,jint pid,jlongArray outUssSwapPssRss,jlongArray outMemtrack)497 static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid,
498         jlongArray outUssSwapPssRss, jlongArray outMemtrack)
499 {
500     jlong pss = 0;
501     jlong rss = 0;
502     jlong swapPss = 0;
503     jlong uss = 0;
504     jlong memtrack = 0;
505 
506     struct graphics_memory_pss graphics_mem;
507     if (read_memtrack_memory(pid, &graphics_mem) == 0) {
508         pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other;
509     }
510 
511     ::android::meminfo::ProcMemInfo proc_mem(pid);
512     ::android::meminfo::MemUsage stats;
513     if (proc_mem.SmapsOrRollup(&stats)) {
514         pss += stats.pss;
515         uss += stats.uss;
516         rss += stats.rss;
517         swapPss = stats.swap_pss;
518         pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP
519     } else {
520         return 0;
521     }
522 
523     if (outUssSwapPssRss != NULL) {
524         int outLen = env->GetArrayLength(outUssSwapPssRss);
525         if (outLen >= 1) {
526             jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0);
527             if (outUssSwapPssRssArray != NULL) {
528                 outUssSwapPssRssArray[0] = uss;
529                 if (outLen >= 2) {
530                     outUssSwapPssRssArray[1] = swapPss;
531                 }
532                 if (outLen >= 3) {
533                     outUssSwapPssRssArray[2] = rss;
534                 }
535             }
536             env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0);
537         }
538     }
539 
540     if (outMemtrack != NULL) {
541         int outLen = env->GetArrayLength(outMemtrack);
542         if (outLen >= 1) {
543             jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0);
544             if (outMemtrackArray != NULL) {
545                 outMemtrackArray[0] = memtrack;
546                 if (outLen >= 2) {
547                     outMemtrackArray[1] = graphics_mem.graphics;
548                 }
549                 if (outLen >= 3) {
550                     outMemtrackArray[2] = graphics_mem.gl;
551                 }
552                 if (outLen >= 4) {
553                     outMemtrackArray[3] = graphics_mem.other;
554                 }
555             }
556             env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0);
557         }
558     }
559 
560     return pss;
561 }
562 
android_os_Debug_getPss(JNIEnv * env,jobject clazz)563 static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz)
564 {
565     return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL);
566 }
567 
568 // The 1:1 mapping of MEMINFO_* enums here must match with the constants from
569 // Debug.java.
570 enum {
571     MEMINFO_TOTAL,
572     MEMINFO_FREE,
573     MEMINFO_BUFFERS,
574     MEMINFO_CACHED,
575     MEMINFO_SHMEM,
576     MEMINFO_SLAB,
577     MEMINFO_SLAB_RECLAIMABLE,
578     MEMINFO_SLAB_UNRECLAIMABLE,
579     MEMINFO_SWAP_TOTAL,
580     MEMINFO_SWAP_FREE,
581     MEMINFO_ZRAM_TOTAL,
582     MEMINFO_MAPPED,
583     MEMINFO_VMALLOC_USED,
584     MEMINFO_PAGE_TABLES,
585     MEMINFO_KERNEL_STACK,
586     MEMINFO_KERNEL_RECLAIMABLE,
587     MEMINFO_ACTIVE,
588     MEMINFO_INACTIVE,
589     MEMINFO_UNEVICTABLE,
590     MEMINFO_AVAILABLE,
591     MEMINFO_ACTIVE_ANON,
592     MEMINFO_INACTIVE_ANON,
593     MEMINFO_ACTIVE_FILE,
594     MEMINFO_INACTIVE_FILE,
595     MEMINFO_CMA_TOTAL,
596     MEMINFO_CMA_FREE,
597     MEMINFO_COUNT
598 };
599 
android_os_Debug_getMemInfo(JNIEnv * env,jobject clazz,jlongArray out)600 static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out)
601 {
602     if (out == NULL) {
603         jniThrowNullPointerException(env, "out == null");
604         return;
605     }
606 
607     int outLen = env->GetArrayLength(out);
608     if (outLen < MEMINFO_COUNT) {
609         jniThrowRuntimeException(env, "outLen < MEMINFO_COUNT");
610         return;
611     }
612 
613     // Read system memory info including ZRAM. The values are stored in the vector
614     // in the same order as MEMINFO_* enum
615     std::vector<std::string_view> tags(
616         ::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags.begin(),
617         ::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags.end());
618     tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:");
619     std::vector<uint64_t> mem(tags.size());
620     ::android::meminfo::SysMemInfo smi;
621     if (!smi.ReadMemInfo(tags.size(), tags.data(), mem.data())) {
622         jniThrowRuntimeException(env, "SysMemInfo read failed");
623         return;
624     }
625 
626     jlong* outArray = env->GetLongArrayElements(out, 0);
627     if (outArray != NULL) {
628         outLen = MEMINFO_COUNT;
629         for (int i = 0; i < outLen; i++) {
630             if (i == MEMINFO_VMALLOC_USED && mem[i] == 0) {
631                 outArray[i] = smi.ReadVmallocInfo() / 1024;
632                 continue;
633             }
634             outArray[i] = mem[i];
635         }
636     }
637 
638     env->ReleaseLongArrayElements(out, outArray, 0);
639 }
640 
read_binder_stat(const char * stat)641 static jint read_binder_stat(const char* stat)
642 {
643     UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re");
644     if (fp == nullptr) {
645         return -1;
646     }
647 
648     char line[1024];
649 
650     char compare[128];
651     int len = snprintf(compare, 128, "proc %d", getpid());
652 
653     // loop until we have the block that represents this process
654     do {
655         if (fgets(line, 1024, fp.get()) == 0) {
656             return -1;
657         }
658     } while (strncmp(compare, line, len));
659 
660     // now that we have this process, read until we find the stat that we are looking for
661     len = snprintf(compare, 128, "  %s: ", stat);
662 
663     do {
664         if (fgets(line, 1024, fp.get()) == 0) {
665             return -1;
666         }
667     } while (strncmp(compare, line, len));
668 
669     // we have the line, now increment the line ptr to the value
670     char* ptr = line + len;
671     jint result = atoi(ptr);
672     return result;
673 }
674 
android_os_Debug_getBinderSentTransactions(JNIEnv * env,jobject clazz)675 static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz)
676 {
677     return read_binder_stat("bcTRANSACTION");
678 }
679 
android_os_getBinderReceivedTransactions(JNIEnv * env,jobject clazz)680 static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz)
681 {
682     return read_binder_stat("brTRANSACTION");
683 }
684 
685 // these are implemented in android_util_Binder.cpp
686 jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz);
687 jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz);
688 jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz);
689 
openFile(JNIEnv * env,jobject fileDescriptor,UniqueFile & fp)690 static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp)
691 {
692     if (fileDescriptor == NULL) {
693         jniThrowNullPointerException(env, "fd == null");
694         return false;
695     }
696     int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor);
697     if (origFd < 0) {
698         jniThrowRuntimeException(env, "Invalid file descriptor");
699         return false;
700     }
701 
702     /* dup() the descriptor so we don't close the original with fclose() */
703     int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0);
704     if (fd < 0) {
705         ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno));
706         jniThrowRuntimeException(env, "dup() failed");
707         return false;
708     }
709 
710     fp.reset(fdopen(fd, "w"));
711     if (fp == nullptr) {
712         ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno));
713         close(fd);
714         jniThrowRuntimeException(env, "fdopen() failed");
715         return false;
716     }
717     return true;
718 }
719 
720 /*
721  * Dump the native heap, writing human-readable output to the specified
722  * file descriptor.
723  */
android_os_Debug_dumpNativeHeap(JNIEnv * env,jobject,jobject fileDescriptor)724 static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject,
725     jobject fileDescriptor)
726 {
727     UniqueFile fp(nullptr, safeFclose);
728     if (!openFile(env, fileDescriptor, fp)) {
729         return;
730     }
731 
732     ALOGD("Native heap dump starting...\n");
733     // Formatting of the native heap dump is handled by malloc debug itself.
734     // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format
735     if (android_mallopt(M_WRITE_MALLOC_LEAK_INFO_TO_FILE, fp.get(), sizeof(FILE*))) {
736       ALOGD("Native heap dump complete.\n");
737     } else {
738       PLOG(ERROR) << "Failed to write native heap dump to file";
739     }
740 }
741 
742 /*
743  * Dump the native malloc info, writing xml output to the specified
744  * file descriptor.
745  */
android_os_Debug_dumpNativeMallocInfo(JNIEnv * env,jobject,jobject fileDescriptor)746 static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject,
747     jobject fileDescriptor)
748 {
749     UniqueFile fp(nullptr, safeFclose);
750     if (!openFile(env, fileDescriptor, fp)) {
751         return;
752     }
753 
754     malloc_info(0, fp.get());
755 }
756 
dumpTraces(JNIEnv * env,jint pid,jstring fileName,jint timeoutSecs,DebuggerdDumpType dumpType)757 static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs,
758                        DebuggerdDumpType dumpType) {
759     const ScopedUtfChars fileNameChars(env, fileName);
760     if (fileNameChars.c_str() == nullptr) {
761         return false;
762     }
763 
764     android::base::unique_fd fd(open(fileNameChars.c_str(),
765                                      O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND,
766                                      0666));
767     if (fd < 0) {
768         PLOG(ERROR) << "Can't open " << fileNameChars.c_str();
769         return false;
770     }
771 
772     int res = dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd);
773     if (fdatasync(fd.get()) != 0) {
774         PLOG(ERROR) << "Failed flushing trace.";
775     }
776     return res == 0;
777 }
778 
android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv * env,jobject clazz,jint pid,jstring fileName,jint timeoutSecs)779 static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
780         jint pid, jstring fileName, jint timeoutSecs) {
781     const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace);
782     return ret ? JNI_TRUE : JNI_FALSE;
783 }
784 
android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv * env,jobject clazz,jint pid,jstring fileName,jint timeoutSecs)785 static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz,
786         jint pid, jstring fileName, jint timeoutSecs) {
787     const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace);
788     return ret ? JNI_TRUE : JNI_FALSE;
789 }
790 
android_os_Debug_getUnreachableMemory(JNIEnv * env,jobject clazz,jint limit,jboolean contents)791 static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz,
792     jint limit, jboolean contents)
793 {
794     std::string s = GetUnreachableMemoryString(contents, limit);
795     return env->NewStringUTF(s.c_str());
796 }
797 
android_os_Debug_getFreeZramKb(JNIEnv * env,jobject clazz)798 static jlong android_os_Debug_getFreeZramKb(JNIEnv* env, jobject clazz) {
799 
800     jlong zramFreeKb = 0;
801 
802     std::string status_path = android::base::StringPrintf("/proc/meminfo");
803     UniqueFile file = MakeUniqueFile(status_path.c_str(), "re");
804 
805     char line[256];
806     while (file != nullptr && fgets(line, sizeof(line), file.get())) {
807         jlong v;
808         if (sscanf(line, "SwapFree: %" SCNd64 " kB", &v) == 1) {
809             zramFreeKb = v;
810             break;
811         }
812     }
813 
814     return zramFreeKb;
815 }
816 
android_os_Debug_getIonHeapsSizeKb(JNIEnv * env,jobject clazz)817 static jlong android_os_Debug_getIonHeapsSizeKb(JNIEnv* env, jobject clazz) {
818     jlong heapsSizeKb = -1;
819     uint64_t size;
820 
821     if (meminfo::ReadIonHeapsSizeKb(&size)) {
822         heapsSizeKb = size;
823     }
824 
825     return heapsSizeKb;
826 }
827 
android_os_Debug_getDmabufTotalExportedKb(JNIEnv * env,jobject clazz)828 static jlong android_os_Debug_getDmabufTotalExportedKb(JNIEnv* env, jobject clazz) {
829     jlong dmabufTotalSizeKb = -1;
830     uint64_t size;
831 
832     if (dmabufinfo::GetDmabufTotalExportedKb(&size)) {
833         dmabufTotalSizeKb = size;
834     }
835     return dmabufTotalSizeKb;
836 }
837 
android_os_Debug_getDmabufHeapTotalExportedKb(JNIEnv * env,jobject clazz)838 static jlong android_os_Debug_getDmabufHeapTotalExportedKb(JNIEnv* env, jobject clazz) {
839     jlong dmabufHeapTotalSizeKb = -1;
840     uint64_t size;
841 
842     if (meminfo::ReadDmabufHeapTotalExportedKb(&size)) {
843         dmabufHeapTotalSizeKb = size;
844     }
845     return dmabufHeapTotalSizeKb;
846 }
847 
android_os_Debug_getIonPoolsSizeKb(JNIEnv * env,jobject clazz)848 static jlong android_os_Debug_getIonPoolsSizeKb(JNIEnv* env, jobject clazz) {
849     jlong poolsSizeKb = -1;
850     uint64_t size;
851 
852     if (meminfo::ReadIonPoolsSizeKb(&size)) {
853         poolsSizeKb = size;
854     }
855 
856     return poolsSizeKb;
857 }
858 
android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv * env,jobject clazz)859 static jlong android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv* env, jobject clazz) {
860     jlong poolsSizeKb = -1;
861     uint64_t size;
862 
863     if (meminfo::ReadDmabufHeapPoolsSizeKb(&size)) {
864         poolsSizeKb = size;
865     }
866 
867     return poolsSizeKb;
868 }
869 
halSupportsGpuPrivateMemory()870 static bool halSupportsGpuPrivateMemory() {
871     int productApiLevel =
872             android::base::GetIntProperty("ro.product.first_api_level",
873                                           android::base::GetIntProperty("ro.build.version.sdk",
874                                                                          __ANDROID_API_FUTURE__));
875     int boardApiLevel =
876             android::base::GetIntProperty("ro.board.api_level",
877                                           android::base::GetIntProperty("ro.board.first_api_level",
878                                                                          __ANDROID_API_FUTURE__));
879 
880     return std::min(productApiLevel, boardApiLevel) >= __ANDROID_API_S__;
881 }
882 
android_os_Debug_getGpuPrivateMemoryKb(JNIEnv * env,jobject clazz)883 static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) {
884     static bool gpuPrivateMemorySupported = halSupportsGpuPrivateMemory();
885 
886     struct memtrack_proc* p = memtrack_proc_new();
887     if (p == nullptr) {
888         LOG(ERROR) << "getGpuPrivateMemoryKb: Failed to create memtrack_proc";
889         return -1;
890     }
891 
892     // Memtrack hal defines PID 0 as global total for GPU-private (GL) memory.
893     if (memtrack_proc_get(p, 0) != 0) {
894         // The memtrack HAL may not be available, avoid flooding the log.
895         memtrack_proc_destroy(p);
896         return -1;
897     }
898 
899     ssize_t gpuPrivateMem = memtrack_proc_gl_pss(p);
900 
901     memtrack_proc_destroy(p);
902 
903     // Old HAL implementations may return 0 for GPU private memory if not supported
904     if (gpuPrivateMem == 0 && !gpuPrivateMemorySupported) {
905         return -1;
906     }
907 
908     return gpuPrivateMem / 1024;
909 }
910 
android_os_Debug_getDmabufMappedSizeKb(JNIEnv * env,jobject clazz)911 static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) {
912     jlong dmabufPss = 0;
913     std::vector<dmabufinfo::DmaBuffer> dmabufs;
914 
915     std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir("/proc"), closedir);
916     if (!dir) {
917         LOG(ERROR) << "Failed to open /proc directory";
918         return false;
919     }
920 
921     struct dirent* dent;
922     while ((dent = readdir(dir.get()))) {
923         if (dent->d_type != DT_DIR) continue;
924 
925         int pid = atoi(dent->d_name);
926         if (pid == 0) {
927             continue;
928         }
929 
930         if (!ReadDmaBufMapRefs(pid, &dmabufs)) {
931             LOG(ERROR) << "Failed to read maps for pid " << pid;
932         }
933     }
934 
935     for (const dmabufinfo::DmaBuffer& buf : dmabufs) {
936         dmabufPss += buf.size() / 1024;
937     }
938 
939     return dmabufPss;
940 }
941 
android_os_Debug_getGpuTotalUsageKb(JNIEnv * env,jobject clazz)942 static jlong android_os_Debug_getGpuTotalUsageKb(JNIEnv* env, jobject clazz) {
943     jlong sizeKb = -1;
944     uint64_t size;
945 
946     if (meminfo::ReadGpuTotalUsageKb(&size)) {
947         sizeKb = size;
948     }
949 
950     return sizeKb;
951 }
952 
android_os_Debug_isVmapStack(JNIEnv * env,jobject clazz)953 static jboolean android_os_Debug_isVmapStack(JNIEnv *env, jobject clazz)
954 {
955     static enum {
956         CONFIG_UNKNOWN,
957         CONFIG_SET,
958         CONFIG_UNSET,
959     } cfg_state = CONFIG_UNKNOWN;
960 
961     if (cfg_state == CONFIG_UNKNOWN) {
962         auto runtime_info = vintf::VintfObject::GetInstance()->getRuntimeInfo(
963                 vintf::RuntimeInfo::FetchFlag::CONFIG_GZ);
964         CHECK(runtime_info != nullptr) << "Kernel configs cannot be fetched. b/151092221";
965         const std::map<std::string, std::string>& configs = runtime_info->kernelConfigs();
966         std::map<std::string, std::string>::const_iterator it = configs.find("CONFIG_VMAP_STACK");
967         cfg_state = (it != configs.end() && it->second == "y") ? CONFIG_SET : CONFIG_UNSET;
968     }
969     return cfg_state == CONFIG_SET;
970 }
971 
972 /*
973  * JNI registration.
974  */
975 
976 static const JNINativeMethod gMethods[] = {
977     { "getNativeHeapSize",      "()J",
978             (void*) android_os_Debug_getNativeHeapSize },
979     { "getNativeHeapAllocatedSize", "()J",
980             (void*) android_os_Debug_getNativeHeapAllocatedSize },
981     { "getNativeHeapFreeSize",  "()J",
982             (void*) android_os_Debug_getNativeHeapFreeSize },
983     { "getMemoryInfo",          "(Landroid/os/Debug$MemoryInfo;)V",
984             (void*) android_os_Debug_getDirtyPages },
985     { "getMemoryInfo",          "(ILandroid/os/Debug$MemoryInfo;)Z",
986             (void*) android_os_Debug_getDirtyPagesPid },
987     { "getPss",                 "()J",
988             (void*) android_os_Debug_getPss },
989     { "getPss",                 "(I[J[J)J",
990             (void*) android_os_Debug_getPssPid },
991     { "getMemInfo",             "([J)V",
992             (void*) android_os_Debug_getMemInfo },
993     { "dumpNativeHeap",         "(Ljava/io/FileDescriptor;)V",
994             (void*) android_os_Debug_dumpNativeHeap },
995     { "dumpNativeMallocInfo",   "(Ljava/io/FileDescriptor;)V",
996             (void*) android_os_Debug_dumpNativeMallocInfo },
997     { "getBinderSentTransactions", "()I",
998             (void*) android_os_Debug_getBinderSentTransactions },
999     { "getBinderReceivedTransactions", "()I",
1000             (void*) android_os_getBinderReceivedTransactions },
1001     { "getBinderLocalObjectCount", "()I",
1002             (void*)android_os_Debug_getLocalObjectCount },
1003     { "getBinderProxyObjectCount", "()I",
1004             (void*)android_os_Debug_getProxyObjectCount },
1005     { "getBinderDeathObjectCount", "()I",
1006             (void*)android_os_Debug_getDeathObjectCount },
1007     { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
1008             (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout },
1009     { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z",
1010             (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout },
1011     { "getUnreachableMemory", "(IZ)Ljava/lang/String;",
1012             (void*)android_os_Debug_getUnreachableMemory },
1013     { "getZramFreeKb", "()J",
1014             (void*)android_os_Debug_getFreeZramKb },
1015     { "getIonHeapsSizeKb", "()J",
1016             (void*)android_os_Debug_getIonHeapsSizeKb },
1017     { "getDmabufTotalExportedKb", "()J",
1018             (void*)android_os_Debug_getDmabufTotalExportedKb },
1019     { "getGpuPrivateMemoryKb", "()J",
1020             (void*)android_os_Debug_getGpuPrivateMemoryKb },
1021     { "getDmabufHeapTotalExportedKb", "()J",
1022             (void*)android_os_Debug_getDmabufHeapTotalExportedKb },
1023     { "getIonPoolsSizeKb", "()J",
1024             (void*)android_os_Debug_getIonPoolsSizeKb },
1025     { "getDmabufMappedSizeKb", "()J",
1026             (void*)android_os_Debug_getDmabufMappedSizeKb },
1027     { "getDmabufHeapPoolsSizeKb", "()J",
1028             (void*)android_os_Debug_getDmabufHeapPoolsSizeKb },
1029     { "getGpuTotalUsageKb", "()J",
1030             (void*)android_os_Debug_getGpuTotalUsageKb },
1031     { "isVmapStack", "()Z",
1032             (void*)android_os_Debug_isVmapStack },
1033 };
1034 
register_android_os_Debug(JNIEnv * env)1035 int register_android_os_Debug(JNIEnv *env)
1036 {
1037     jclass clazz = env->FindClass("android/os/Debug$MemoryInfo");
1038 
1039     // Check the number of other statistics expected in Java matches here.
1040     jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I");
1041     jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field);
1042     jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I");
1043     jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field);
1044     int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP;
1045     if ((numOtherStats + numDvkStats) != expectedNumOtherStats) {
1046         jniThrowExceptionFmt(env, "java/lang/RuntimeException",
1047                              "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d",
1048                              numOtherStats+numDvkStats, expectedNumOtherStats);
1049         return JNI_ERR;
1050     }
1051 
1052     otherStats_field = env->GetFieldID(clazz, "otherStats", "[I");
1053     hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z");
1054 
1055     for (int i=0; i<_NUM_CORE_HEAP; i++) {
1056         stat_fields[i].pss_field =
1057                 env->GetFieldID(clazz, stat_field_names[i].pss_name, "I");
1058         stat_fields[i].pssSwappable_field =
1059                 env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I");
1060         stat_fields[i].rss_field =
1061                 env->GetFieldID(clazz, stat_field_names[i].rss_name, "I");
1062         stat_fields[i].privateDirty_field =
1063                 env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I");
1064         stat_fields[i].sharedDirty_field =
1065                 env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I");
1066         stat_fields[i].privateClean_field =
1067                 env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I");
1068         stat_fields[i].sharedClean_field =
1069                 env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I");
1070         stat_fields[i].swappedOut_field =
1071                 env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I");
1072         stat_fields[i].swappedOutPss_field =
1073                 env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I");
1074     }
1075 
1076     return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods));
1077 }
1078 
1079 }; // namespace android
1080