The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 17 | #define LOG_TAG "android.os.Debug" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Mark Salyzyn | 8539403 | 2014-04-16 10:28:37 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 24 | #include <malloc.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <sys/time.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 29 | #include <time.h> |
| 30 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 32 | #include <iomanip> |
| 33 | #include <string> |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 34 | #include <vector> |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 35 | |
Christopher Ferris | 4740d54 | 2019-04-16 11:34:58 -0700 | [diff] [blame] | 36 | #include <android-base/logging.h> |
Christopher Ferris | 15c0233 | 2019-09-11 19:08:52 -0700 | [diff] [blame^] | 37 | #include <bionic/malloc.h> |
Josh Gao | 3ce3696 | 2017-01-05 18:28:24 -0800 | [diff] [blame] | 38 | #include <debuggerd/client.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 39 | #include <log/log.h> |
| 40 | #include <utils/misc.h> |
| 41 | #include <utils/String8.h> |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 42 | |
Steven Moreland | bdd5c42 | 2017-08-11 10:57:59 -0700 | [diff] [blame] | 43 | #include <nativehelper/JNIHelp.h> |
| 44 | #include <nativehelper/ScopedUtfChars.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 45 | #include "jni.h" |
Sandeep Patil | fcd4a4a | 2019-01-12 22:42:05 -0800 | [diff] [blame] | 46 | #include <meminfo/procmeminfo.h> |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 47 | #include <meminfo/sysmeminfo.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 48 | #include <memtrack/memtrack.h> |
| 49 | #include <memunreachable/memunreachable.h> |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 50 | #include <android-base/strings.h> |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 51 | #include "android_os_Debug.h" |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | namespace android |
| 54 | { |
| 55 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 56 | enum { |
| 57 | HEAP_UNKNOWN, |
| 58 | HEAP_DALVIK, |
| 59 | HEAP_NATIVE, |
Dianne Hackborn | 1842930 | 2014-11-03 13:58:11 -0800 | [diff] [blame] | 60 | |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 61 | HEAP_DALVIK_OTHER, |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 62 | HEAP_STACK, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 63 | HEAP_CURSOR, |
| 64 | HEAP_ASHMEM, |
Dianne Hackborn | 1842930 | 2014-11-03 13:58:11 -0800 | [diff] [blame] | 65 | HEAP_GL_DEV, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 66 | HEAP_UNKNOWN_DEV, |
| 67 | HEAP_SO, |
| 68 | HEAP_JAR, |
| 69 | HEAP_APK, |
| 70 | HEAP_TTF, |
| 71 | HEAP_DEX, |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 72 | HEAP_OAT, |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame] | 73 | HEAP_ART, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 74 | HEAP_UNKNOWN_MAP, |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 75 | HEAP_GRAPHICS, |
| 76 | HEAP_GL, |
| 77 | HEAP_OTHER_MEMTRACK, |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 78 | |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 79 | // Dalvik extra sections (heap). |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 80 | HEAP_DALVIK_NORMAL, |
| 81 | HEAP_DALVIK_LARGE, |
Mathieu Chartier | 25c5e2b | 2014-12-08 16:20:26 -0800 | [diff] [blame] | 82 | HEAP_DALVIK_ZYGOTE, |
| 83 | HEAP_DALVIK_NON_MOVING, |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 84 | |
| 85 | // Dalvik other extra sections. |
| 86 | HEAP_DALVIK_OTHER_LINEARALLOC, |
| 87 | HEAP_DALVIK_OTHER_ACCOUNTING, |
| 88 | HEAP_DALVIK_OTHER_CODE_CACHE, |
| 89 | HEAP_DALVIK_OTHER_COMPILER_METADATA, |
| 90 | HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE, |
| 91 | |
| 92 | // Boot vdex / app dex / app vdex |
| 93 | HEAP_DEX_BOOT_VDEX, |
| 94 | HEAP_DEX_APP_DEX, |
| 95 | HEAP_DEX_APP_VDEX, |
| 96 | |
| 97 | // App art, boot art. |
| 98 | HEAP_ART_APP, |
| 99 | HEAP_ART_BOOT, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 100 | |
| 101 | _NUM_HEAP, |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 102 | _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 103 | _NUM_CORE_HEAP = HEAP_NATIVE+1 |
| 104 | }; |
| 105 | |
| 106 | struct stat_fields { |
| 107 | jfieldID pss_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 108 | jfieldID pssSwappable_field; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 109 | jfieldID rss_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 110 | jfieldID privateDirty_field; |
| 111 | jfieldID sharedDirty_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 112 | jfieldID privateClean_field; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 113 | jfieldID sharedClean_field; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 114 | jfieldID swappedOut_field; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 115 | jfieldID swappedOutPss_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct stat_field_names { |
| 119 | const char* pss_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 120 | const char* pssSwappable_name; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 121 | const char* rss_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 122 | const char* privateDirty_name; |
| 123 | const char* sharedDirty_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 124 | const char* privateClean_name; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 125 | const char* sharedClean_name; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 126 | const char* swappedOut_name; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 127 | const char* swappedOutPss_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static stat_fields stat_fields[_NUM_CORE_HEAP]; |
| 131 | |
| 132 | static stat_field_names stat_field_names[_NUM_CORE_HEAP] = { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 133 | { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 134 | "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" }, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 135 | { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 136 | "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" }, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 137 | { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 138 | "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | jfieldID otherStats_field; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 142 | jfieldID hasSwappedOutPss_field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
| 144 | struct stats_t { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 145 | int pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 146 | int swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 147 | int rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 148 | int privateDirty; |
| 149 | int sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 150 | int privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 151 | int sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 152 | int swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 153 | int swappedOutPss; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #define BINDER_STATS "/proc/binder/stats" |
| 157 | |
| 158 | static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) |
| 159 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | struct mallinfo info = mallinfo(); |
| 161 | return (jlong) info.usmblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz) |
| 165 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | struct mallinfo info = mallinfo(); |
| 167 | return (jlong) info.uordblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz) |
| 171 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | struct mallinfo info = mallinfo(); |
| 173 | return (jlong) info.fordblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 176 | // Container used to retrieve graphics memory pss |
| 177 | struct graphics_memory_pss |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 178 | { |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 179 | int graphics; |
| 180 | int gl; |
| 181 | int other; |
| 182 | }; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 183 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 184 | /* |
| 185 | * Uses libmemtrack to retrieve graphics memory that the process is using. |
| 186 | * Any graphics memory reported in /proc/pid/smaps is not included here. |
| 187 | */ |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 188 | static int read_memtrack_memory(struct memtrack_proc* p, int pid, |
| 189 | struct graphics_memory_pss* graphics_mem) |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 190 | { |
| 191 | int err = memtrack_proc_get(p, pid); |
| 192 | if (err != 0) { |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 193 | ALOGW("failed to get memory consumption info: %d", err); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 194 | return err; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 197 | ssize_t pss = memtrack_proc_graphics_pss(p); |
| 198 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 199 | ALOGW("failed to get graphics pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 200 | return pss; |
| 201 | } |
| 202 | graphics_mem->graphics = pss / 1024; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 203 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 204 | pss = memtrack_proc_gl_pss(p); |
| 205 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 206 | ALOGW("failed to get gl pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 207 | return pss; |
| 208 | } |
| 209 | graphics_mem->gl = pss / 1024; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 210 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 211 | pss = memtrack_proc_other_pss(p); |
| 212 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 213 | ALOGW("failed to get other pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 214 | return pss; |
| 215 | } |
| 216 | graphics_mem->other = pss / 1024; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 217 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 220 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 221 | /* |
| 222 | * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps. |
| 223 | */ |
| 224 | static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem) |
| 225 | { |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 226 | struct memtrack_proc* p = memtrack_proc_new(); |
| 227 | if (p == NULL) { |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 228 | ALOGW("failed to create memtrack_proc"); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 229 | return -1; |
| 230 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 231 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 232 | int err = read_memtrack_memory(p, pid, graphics_mem); |
| 233 | memtrack_proc_destroy(p); |
| 234 | return err; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 237 | static void load_maps(int pid, stats_t* stats, bool* foundSwapPss) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | { |
Andreas Gampe | ce71149 | 2016-10-04 18:06:42 -0700 | [diff] [blame] | 239 | *foundSwapPss = false; |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 240 | uint64_t prev_end = 0; |
| 241 | int prev_heap = HEAP_UNKNOWN; |
Andreas Gampe | ce71149 | 2016-10-04 18:06:42 -0700 | [diff] [blame] | 242 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 243 | std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid); |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 244 | auto vma_scan = [&](const meminfo::Vma& vma) { |
| 245 | int which_heap = HEAP_UNKNOWN; |
| 246 | int sub_heap = HEAP_UNKNOWN; |
| 247 | bool is_swappable = false; |
| 248 | std::string name; |
| 249 | if (base::EndsWith(vma.name, " (deleted)")) { |
| 250 | name = vma.name.substr(0, vma.name.size() - strlen(" (deleted)")); |
| 251 | } else { |
| 252 | name = vma.name; |
| 253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 255 | uint32_t namesz = name.size(); |
| 256 | if (base::StartsWith(name, "[heap]")) { |
| 257 | which_heap = HEAP_NATIVE; |
| 258 | } else if (base::StartsWith(name, "[anon:libc_malloc]")) { |
| 259 | which_heap = HEAP_NATIVE; |
| 260 | } else if (base::StartsWith(name, "[stack")) { |
Sandeep Patil | 290811a | 2019-05-31 10:33:59 -0700 | [diff] [blame] | 261 | which_heap = HEAP_STACK; |
Ryan Prichard | 4031383 | 2019-07-19 13:32:32 -0700 | [diff] [blame] | 262 | } else if (base::StartsWith(name, "[anon:stack_and_tls:")) { |
| 263 | which_heap = HEAP_STACK; |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 264 | } else if (base::EndsWith(name, ".so")) { |
| 265 | which_heap = HEAP_SO; |
| 266 | is_swappable = true; |
| 267 | } else if (base::EndsWith(name, ".jar")) { |
| 268 | which_heap = HEAP_JAR; |
| 269 | is_swappable = true; |
| 270 | } else if (base::EndsWith(name, ".apk")) { |
| 271 | which_heap = HEAP_APK; |
| 272 | is_swappable = true; |
| 273 | } else if (base::EndsWith(name, ".ttf")) { |
| 274 | which_heap = HEAP_TTF; |
| 275 | is_swappable = true; |
| 276 | } else if ((base::EndsWith(name, ".odex")) || |
| 277 | (namesz > 4 && strstr(name.c_str(), ".dex") != nullptr)) { |
| 278 | which_heap = HEAP_DEX; |
| 279 | sub_heap = HEAP_DEX_APP_DEX; |
| 280 | is_swappable = true; |
| 281 | } else if (base::EndsWith(name, ".vdex")) { |
| 282 | which_heap = HEAP_DEX; |
| 283 | // Handle system@framework@boot and system/framework/boot |
| 284 | if ((strstr(name.c_str(), "@boot") != nullptr) || |
| 285 | (strstr(name.c_str(), "/boot"))) { |
| 286 | sub_heap = HEAP_DEX_BOOT_VDEX; |
| 287 | } else { |
| 288 | sub_heap = HEAP_DEX_APP_VDEX; |
| 289 | } |
| 290 | is_swappable = true; |
| 291 | } else if (base::EndsWith(name, ".oat")) { |
| 292 | which_heap = HEAP_OAT; |
| 293 | is_swappable = true; |
Mathieu Chartier | 8da8cbf | 2019-06-04 13:11:16 -0700 | [diff] [blame] | 294 | } else if (base::EndsWith(name, ".art") || base::EndsWith(name, ".art]")) { |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 295 | which_heap = HEAP_ART; |
| 296 | // Handle system@framework@boot* and system/framework/boot* |
| 297 | if ((strstr(name.c_str(), "@boot") != nullptr) || |
| 298 | (strstr(name.c_str(), "/boot"))) { |
Mathieu Chartier | 8da8cbf | 2019-06-04 13:11:16 -0700 | [diff] [blame] | 299 | sub_heap = HEAP_ART_BOOT; |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 300 | } else { |
Mathieu Chartier | 8da8cbf | 2019-06-04 13:11:16 -0700 | [diff] [blame] | 301 | sub_heap = HEAP_ART_APP; |
Sandeep Patil | cb4e8ee | 2019-01-21 12:00:59 -0800 | [diff] [blame] | 302 | } |
| 303 | is_swappable = true; |
| 304 | } else if (base::StartsWith(name, "/dev/")) { |
| 305 | which_heap = HEAP_UNKNOWN_DEV; |
| 306 | if (base::StartsWith(name, "/dev/kgsl-3d0")) { |
| 307 | which_heap = HEAP_GL_DEV; |
| 308 | } else if (base::StartsWith(name, "/dev/ashmem/CursorWindow")) { |
| 309 | which_heap = HEAP_CURSOR; |
| 310 | } else if (base::StartsWith(name, "/dev/ashmem")) { |
| 311 | which_heap = HEAP_ASHMEM; |
| 312 | } |
| 313 | } else if (base::StartsWith(name, "[anon:")) { |
| 314 | which_heap = HEAP_UNKNOWN; |
| 315 | if (base::StartsWith(name, "[anon:dalvik-")) { |
| 316 | which_heap = HEAP_DALVIK_OTHER; |
| 317 | if (base::StartsWith(name, "[anon:dalvik-LinearAlloc")) { |
| 318 | sub_heap = HEAP_DALVIK_OTHER_LINEARALLOC; |
| 319 | } else if (base::StartsWith(name, "[anon:dalvik-alloc space") || |
| 320 | base::StartsWith(name, "[anon:dalvik-main space")) { |
| 321 | // This is the regular Dalvik heap. |
| 322 | which_heap = HEAP_DALVIK; |
| 323 | sub_heap = HEAP_DALVIK_NORMAL; |
| 324 | } else if (base::StartsWith(name, |
| 325 | "[anon:dalvik-large object space") || |
| 326 | base::StartsWith( |
| 327 | name, "[anon:dalvik-free list large object space")) { |
| 328 | which_heap = HEAP_DALVIK; |
| 329 | sub_heap = HEAP_DALVIK_LARGE; |
| 330 | } else if (base::StartsWith(name, "[anon:dalvik-non moving space")) { |
| 331 | which_heap = HEAP_DALVIK; |
| 332 | sub_heap = HEAP_DALVIK_NON_MOVING; |
| 333 | } else if (base::StartsWith(name, "[anon:dalvik-zygote space")) { |
| 334 | which_heap = HEAP_DALVIK; |
| 335 | sub_heap = HEAP_DALVIK_ZYGOTE; |
| 336 | } else if (base::StartsWith(name, "[anon:dalvik-indirect ref")) { |
| 337 | sub_heap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE; |
| 338 | } else if (base::StartsWith(name, "[anon:dalvik-jit-code-cache") || |
| 339 | base::StartsWith(name, "[anon:dalvik-data-code-cache")) { |
| 340 | sub_heap = HEAP_DALVIK_OTHER_CODE_CACHE; |
| 341 | } else if (base::StartsWith(name, "[anon:dalvik-CompilerMetadata")) { |
| 342 | sub_heap = HEAP_DALVIK_OTHER_COMPILER_METADATA; |
| 343 | } else { |
| 344 | sub_heap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting. |
| 345 | } |
| 346 | } |
| 347 | } else if (namesz > 0) { |
| 348 | which_heap = HEAP_UNKNOWN_MAP; |
| 349 | } else if (vma.start == prev_end && prev_heap == HEAP_SO) { |
| 350 | // bss section of a shared library |
| 351 | which_heap = HEAP_SO; |
| 352 | } |
| 353 | |
| 354 | prev_end = vma.end; |
| 355 | prev_heap = which_heap; |
| 356 | |
| 357 | const meminfo::MemUsage& usage = vma.usage; |
| 358 | if (usage.swap_pss > 0 && *foundSwapPss != true) { |
| 359 | *foundSwapPss = true; |
| 360 | } |
| 361 | |
| 362 | uint64_t swapable_pss = 0; |
| 363 | if (is_swappable && (usage.pss > 0)) { |
| 364 | float sharing_proportion = 0.0; |
| 365 | if ((usage.shared_clean > 0) || (usage.shared_dirty > 0)) { |
| 366 | sharing_proportion = (usage.pss - usage.uss) / (usage.shared_clean + usage.shared_dirty); |
| 367 | } |
| 368 | swapable_pss = (sharing_proportion * usage.shared_clean) + usage.private_clean; |
| 369 | } |
| 370 | |
| 371 | stats[which_heap].pss += usage.pss; |
| 372 | stats[which_heap].swappablePss += swapable_pss; |
| 373 | stats[which_heap].rss += usage.rss; |
| 374 | stats[which_heap].privateDirty += usage.private_dirty; |
| 375 | stats[which_heap].sharedDirty += usage.shared_dirty; |
| 376 | stats[which_heap].privateClean += usage.private_clean; |
| 377 | stats[which_heap].sharedClean += usage.shared_clean; |
| 378 | stats[which_heap].swappedOut += usage.swap; |
| 379 | stats[which_heap].swappedOutPss += usage.swap_pss; |
| 380 | if (which_heap == HEAP_DALVIK || which_heap == HEAP_DALVIK_OTHER || |
| 381 | which_heap == HEAP_DEX || which_heap == HEAP_ART) { |
| 382 | stats[sub_heap].pss += usage.pss; |
| 383 | stats[sub_heap].swappablePss += swapable_pss; |
| 384 | stats[sub_heap].rss += usage.rss; |
| 385 | stats[sub_heap].privateDirty += usage.private_dirty; |
| 386 | stats[sub_heap].sharedDirty += usage.shared_dirty; |
| 387 | stats[sub_heap].privateClean += usage.private_clean; |
| 388 | stats[sub_heap].sharedClean += usage.shared_clean; |
| 389 | stats[sub_heap].swappedOut += usage.swap; |
| 390 | stats[sub_heap].swappedOutPss += usage.swap_pss; |
| 391 | } |
| 392 | }; |
| 393 | |
| 394 | meminfo::ForEachVmaFromFile(smaps_path, vma_scan); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 397 | static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz, |
| 398 | jint pid, jobject object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | { |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 400 | bool foundSwapPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 401 | stats_t stats[_NUM_HEAP]; |
| 402 | memset(&stats, 0, sizeof(stats)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 403 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 404 | load_maps(pid, stats, &foundSwapPss); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 406 | struct graphics_memory_pss graphics_mem; |
| 407 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
| 408 | stats[HEAP_GRAPHICS].pss = graphics_mem.graphics; |
| 409 | stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 410 | stats[HEAP_GRAPHICS].rss = graphics_mem.graphics; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 411 | stats[HEAP_GL].pss = graphics_mem.gl; |
| 412 | stats[HEAP_GL].privateDirty = graphics_mem.gl; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 413 | stats[HEAP_GL].rss = graphics_mem.gl; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 414 | stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other; |
| 415 | stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 416 | stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 417 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 418 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 419 | for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 420 | stats[HEAP_UNKNOWN].pss += stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 421 | stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 422 | stats[HEAP_UNKNOWN].rss += stats[i].rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 423 | stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty; |
| 424 | stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 425 | stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 426 | stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 427 | stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 428 | stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 432 | env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 433 | env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 434 | env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 435 | env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty); |
| 436 | env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 437 | env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 438 | env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean); |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 439 | env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 440 | env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 441 | } |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 442 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 443 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 444 | env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 445 | jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 446 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 447 | jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0); |
| 448 | if (otherArray == NULL) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | int j=0; |
| 453 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 454 | otherArray[j++] = stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 455 | otherArray[j++] = stats[i].swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 456 | otherArray[j++] = stats[i].rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 457 | otherArray[j++] = stats[i].privateDirty; |
| 458 | otherArray[j++] = stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 459 | otherArray[j++] = stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 460 | otherArray[j++] = stats[i].sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 461 | otherArray[j++] = stats[i].swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 462 | otherArray[j++] = stats[i].swappedOutPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 468 | static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object) |
| 469 | { |
| 470 | android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); |
| 471 | } |
| 472 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 473 | static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 474 | jlongArray outUssSwapPssRss, jlongArray outMemtrack) |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 475 | { |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 476 | jlong pss = 0; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 477 | jlong rss = 0; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 478 | jlong swapPss = 0; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 479 | jlong uss = 0; |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 480 | jlong memtrack = 0; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 481 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 482 | struct graphics_memory_pss graphics_mem; |
| 483 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
Dianne Hackborn | 8c76d91 | 2018-08-23 15:20:05 -0700 | [diff] [blame] | 484 | pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Sandeep Patil | fcd4a4a | 2019-01-12 22:42:05 -0800 | [diff] [blame] | 487 | ::android::meminfo::ProcMemInfo proc_mem(pid); |
| 488 | ::android::meminfo::MemUsage stats; |
| 489 | if (proc_mem.SmapsOrRollup(&stats)) { |
| 490 | pss += stats.pss; |
| 491 | uss += stats.uss; |
| 492 | rss += stats.rss; |
| 493 | swapPss = stats.swap_pss; |
| 494 | pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 495 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 496 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 497 | if (outUssSwapPssRss != NULL) { |
| 498 | if (env->GetArrayLength(outUssSwapPssRss) >= 1) { |
| 499 | jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0); |
| 500 | if (outUssSwapPssRssArray != NULL) { |
| 501 | outUssSwapPssRssArray[0] = uss; |
| 502 | if (env->GetArrayLength(outUssSwapPssRss) >= 2) { |
| 503 | outUssSwapPssRssArray[1] = swapPss; |
| 504 | } |
| 505 | if (env->GetArrayLength(outUssSwapPssRss) >= 3) { |
| 506 | outUssSwapPssRssArray[2] = rss; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 507 | } |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 508 | } |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 509 | env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 512 | |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 513 | if (outMemtrack != NULL) { |
| 514 | if (env->GetArrayLength(outMemtrack) >= 1) { |
| 515 | jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0); |
| 516 | if (outMemtrackArray != NULL) { |
| 517 | outMemtrackArray[0] = memtrack; |
| 518 | } |
| 519 | env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0); |
| 520 | } |
| 521 | } |
| 522 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 523 | return pss; |
| 524 | } |
| 525 | |
| 526 | static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz) |
| 527 | { |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 528 | return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 531 | // The 1:1 mapping of MEMINFO_* enums here must match with the constants from |
| 532 | // Debug.java. |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 533 | enum { |
| 534 | MEMINFO_TOTAL, |
| 535 | MEMINFO_FREE, |
| 536 | MEMINFO_BUFFERS, |
| 537 | MEMINFO_CACHED, |
| 538 | MEMINFO_SHMEM, |
| 539 | MEMINFO_SLAB, |
Robert Benea | 5e09980 | 2017-10-04 18:28:01 -0700 | [diff] [blame] | 540 | MEMINFO_SLAB_RECLAIMABLE, |
| 541 | MEMINFO_SLAB_UNRECLAIMABLE, |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 542 | MEMINFO_SWAP_TOTAL, |
| 543 | MEMINFO_SWAP_FREE, |
| 544 | MEMINFO_ZRAM_TOTAL, |
Dianne Hackborn | b3af4ec | 2014-10-17 15:25:13 -0700 | [diff] [blame] | 545 | MEMINFO_MAPPED, |
| 546 | MEMINFO_VMALLOC_USED, |
| 547 | MEMINFO_PAGE_TABLES, |
| 548 | MEMINFO_KERNEL_STACK, |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 549 | MEMINFO_COUNT |
| 550 | }; |
| 551 | |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 552 | static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out) |
| 553 | { |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 554 | if (out == NULL) { |
| 555 | jniThrowNullPointerException(env, "out == null"); |
| 556 | return; |
| 557 | } |
| 558 | |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 559 | int outLen = env->GetArrayLength(out); |
| 560 | if (outLen < MEMINFO_COUNT) { |
| 561 | jniThrowRuntimeException(env, "outLen < MEMINFO_COUNT"); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 562 | return; |
| 563 | } |
| 564 | |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 565 | // Read system memory info including ZRAM. The values are stored in the vector |
| 566 | // in the same order as MEMINFO_* enum |
| 567 | std::vector<uint64_t> mem(MEMINFO_COUNT); |
| 568 | std::vector<std::string> tags(::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags); |
| 569 | tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:"); |
| 570 | ::android::meminfo::SysMemInfo smi; |
| 571 | if (!smi.ReadMemInfo(tags, &mem)) { |
| 572 | jniThrowRuntimeException(env, "SysMemInfo read failed"); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 573 | return; |
| 574 | } |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 575 | |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 576 | jlong* outArray = env->GetLongArrayElements(out, 0); |
| 577 | if (outArray != NULL) { |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 578 | outLen = MEMINFO_COUNT; |
| 579 | for (int i = 0; i < outLen; i++) { |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 580 | if (i == MEMINFO_VMALLOC_USED) { |
Sandeep Patil | 2481ae0 | 2019-01-13 20:33:02 -0800 | [diff] [blame] | 581 | outArray[i] = smi.ReadVmallocInfo() / 1024; |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 582 | continue; |
| 583 | } |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 584 | outArray[i] = mem[i]; |
| 585 | } |
| 586 | } |
Sandeep Patil | be82541 | 2018-12-11 10:09:46 -0800 | [diff] [blame] | 587 | |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 588 | env->ReleaseLongArrayElements(out, outArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static jint read_binder_stat(const char* stat) |
| 592 | { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 593 | UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re"); |
| 594 | if (fp == nullptr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | char line[1024]; |
| 599 | |
| 600 | char compare[128]; |
| 601 | int len = snprintf(compare, 128, "proc %d", getpid()); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 602 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | // loop until we have the block that represents this process |
| 604 | do { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 605 | if (fgets(line, 1024, fp.get()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | return -1; |
| 607 | } |
| 608 | } while (strncmp(compare, line, len)); |
| 609 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 610 | // now that we have this process, read until we find the stat that we are looking for |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | len = snprintf(compare, 128, " %s: ", stat); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 612 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | do { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 614 | if (fgets(line, 1024, fp.get()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | return -1; |
| 616 | } |
| 617 | } while (strncmp(compare, line, len)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 618 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | // we have the line, now increment the line ptr to the value |
| 620 | char* ptr = line + len; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 621 | jint result = atoi(ptr); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 622 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz) |
| 626 | { |
| 627 | return read_binder_stat("bcTRANSACTION"); |
| 628 | } |
| 629 | |
| 630 | static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz) |
| 631 | { |
| 632 | return read_binder_stat("brTRANSACTION"); |
| 633 | } |
| 634 | |
| 635 | // these are implemented in android_util_Binder.cpp |
| 636 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz); |
| 637 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz); |
| 638 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz); |
| 639 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 640 | static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp) |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 641 | { |
| 642 | if (fileDescriptor == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 643 | jniThrowNullPointerException(env, "fd == null"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 644 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 645 | } |
| 646 | int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 647 | if (origFd < 0) { |
| 648 | jniThrowRuntimeException(env, "Invalid file descriptor"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 649 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* dup() the descriptor so we don't close the original with fclose() */ |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 653 | int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 654 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 655 | ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 656 | jniThrowRuntimeException(env, "dup() failed"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 657 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 660 | fp.reset(fdopen(fd, "w")); |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 661 | if (fp == nullptr) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 662 | ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 663 | close(fd); |
| 664 | jniThrowRuntimeException(env, "fdopen() failed"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 665 | return false; |
| 666 | } |
| 667 | return true; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Dump the native heap, writing human-readable output to the specified |
| 672 | * file descriptor. |
| 673 | */ |
| 674 | static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject, |
| 675 | jobject fileDescriptor) |
| 676 | { |
| 677 | UniqueFile fp(nullptr, safeFclose); |
| 678 | if (!openFile(env, fileDescriptor, fp)) { |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 682 | ALOGD("Native heap dump starting...\n"); |
Christopher Ferris | 38e2c3b | 2018-06-13 13:19:57 -0700 | [diff] [blame] | 683 | // Formatting of the native heap dump is handled by malloc debug itself. |
| 684 | // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format |
Christopher Ferris | 4740d54 | 2019-04-16 11:34:58 -0700 | [diff] [blame] | 685 | if (android_mallopt(M_WRITE_MALLOC_LEAK_INFO_TO_FILE, fp.get(), sizeof(FILE*))) { |
| 686 | ALOGD("Native heap dump complete.\n"); |
| 687 | } else { |
| 688 | PLOG(ERROR) << "Failed to write native heap dump to file"; |
| 689 | } |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Dump the native malloc info, writing xml output to the specified |
| 694 | * file descriptor. |
| 695 | */ |
| 696 | static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject, |
| 697 | jobject fileDescriptor) |
| 698 | { |
| 699 | UniqueFile fp(nullptr, safeFclose); |
| 700 | if (!openFile(env, fileDescriptor, fp)) { |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | malloc_info(0, fp.get()); |
| 705 | } |
| 706 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 707 | static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs, |
| 708 | DebuggerdDumpType dumpType) { |
| 709 | const ScopedUtfChars fileNameChars(env, fileName); |
| 710 | if (fileNameChars.c_str() == nullptr) { |
| 711 | return false; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 714 | android::base::unique_fd fd(open(fileNameChars.c_str(), |
| 715 | O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND, |
| 716 | 0666)); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 717 | if (fd < 0) { |
Andreas Gampe | fa178e1 | 2019-06-11 13:30:15 -0700 | [diff] [blame] | 718 | PLOG(ERROR) << "Can't open " << fileNameChars.c_str(); |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 719 | return false; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Andreas Gampe | 72768ed | 2019-06-11 13:49:48 -0700 | [diff] [blame] | 722 | int res = dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd); |
| 723 | if (fdatasync(fd.get()) != 0) { |
| 724 | PLOG(ERROR) << "Failed flushing trace."; |
| 725 | } |
| 726 | return res == 0; |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 727 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 728 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 729 | static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 730 | jint pid, jstring fileName, jint timeoutSecs) { |
Andreas Gampe | fa178e1 | 2019-06-11 13:30:15 -0700 | [diff] [blame] | 731 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace); |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 732 | return ret ? JNI_TRUE : JNI_FALSE; |
| 733 | } |
| 734 | |
| 735 | static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 736 | jint pid, jstring fileName, jint timeoutSecs) { |
| 737 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace); |
| 738 | return ret ? JNI_TRUE : JNI_FALSE; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 741 | static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz, |
| 742 | jint limit, jboolean contents) |
| 743 | { |
| 744 | std::string s = GetUnreachableMemoryString(contents, limit); |
| 745 | return env->NewStringUTF(s.c_str()); |
| 746 | } |
| 747 | |
Ben Murdoch | fdc5593 | 2019-01-30 10:43:15 +0000 | [diff] [blame] | 748 | static jlong android_os_Debug_getFreeZramKb(JNIEnv* env, jobject clazz) { |
| 749 | |
| 750 | jlong zramFreeKb = 0; |
| 751 | |
| 752 | std::string status_path = android::base::StringPrintf("/proc/meminfo"); |
| 753 | UniqueFile file = MakeUniqueFile(status_path.c_str(), "re"); |
| 754 | |
| 755 | char line[256]; |
| 756 | while (file != nullptr && fgets(line, sizeof(line), file.get())) { |
| 757 | jlong v; |
| 758 | if (sscanf(line, "SwapFree: %" SCNd64 " kB", &v) == 1) { |
| 759 | zramFreeKb = v; |
| 760 | break; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | return zramFreeKb; |
| 765 | } |
| 766 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 767 | /* |
| 768 | * JNI registration. |
| 769 | */ |
| 770 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 771 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | { "getNativeHeapSize", "()J", |
| 773 | (void*) android_os_Debug_getNativeHeapSize }, |
| 774 | { "getNativeHeapAllocatedSize", "()J", |
| 775 | (void*) android_os_Debug_getNativeHeapAllocatedSize }, |
| 776 | { "getNativeHeapFreeSize", "()J", |
| 777 | (void*) android_os_Debug_getNativeHeapFreeSize }, |
| 778 | { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V", |
| 779 | (void*) android_os_Debug_getDirtyPages }, |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 780 | { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V", |
| 781 | (void*) android_os_Debug_getDirtyPagesPid }, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 782 | { "getPss", "()J", |
| 783 | (void*) android_os_Debug_getPss }, |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 784 | { "getPss", "(I[J[J)J", |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 785 | (void*) android_os_Debug_getPssPid }, |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 786 | { "getMemInfo", "([J)V", |
| 787 | (void*) android_os_Debug_getMemInfo }, |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 788 | { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", |
| 789 | (void*) android_os_Debug_dumpNativeHeap }, |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 790 | { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V", |
| 791 | (void*) android_os_Debug_dumpNativeMallocInfo }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | { "getBinderSentTransactions", "()I", |
| 793 | (void*) android_os_Debug_getBinderSentTransactions }, |
| 794 | { "getBinderReceivedTransactions", "()I", |
| 795 | (void*) android_os_getBinderReceivedTransactions }, |
| 796 | { "getBinderLocalObjectCount", "()I", |
| 797 | (void*)android_os_Debug_getLocalObjectCount }, |
| 798 | { "getBinderProxyObjectCount", "()I", |
| 799 | (void*)android_os_Debug_getProxyObjectCount }, |
| 800 | { "getBinderDeathObjectCount", "()I", |
| 801 | (void*)android_os_Debug_getDeathObjectCount }, |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 802 | { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
| 803 | (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout }, |
| 804 | { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
songjinshi | e02e3ea | 2016-12-16 17:48:21 +0800 | [diff] [blame] | 805 | (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout }, |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 806 | { "getUnreachableMemory", "(IZ)Ljava/lang/String;", |
| 807 | (void*)android_os_Debug_getUnreachableMemory }, |
Ben Murdoch | fdc5593 | 2019-01-30 10:43:15 +0000 | [diff] [blame] | 808 | { "getZramFreeKb", "()J", |
| 809 | (void*)android_os_Debug_getFreeZramKb }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 810 | }; |
| 811 | |
| 812 | int register_android_os_Debug(JNIEnv *env) |
| 813 | { |
| 814 | jclass clazz = env->FindClass("android/os/Debug$MemoryInfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 816 | // Sanity check the number of other statistics expected in Java matches here. |
| 817 | jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I"); |
| 818 | jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 819 | jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I"); |
| 820 | jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 821 | int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 822 | if ((numOtherStats + numDvkStats) != expectedNumOtherStats) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 823 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 824 | "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d", |
| 825 | numOtherStats+numDvkStats, expectedNumOtherStats); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 826 | return JNI_ERR; |
| 827 | } |
| 828 | |
| 829 | otherStats_field = env->GetFieldID(clazz, "otherStats", "[I"); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 830 | hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z"); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 831 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 832 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 833 | stat_fields[i].pss_field = |
| 834 | env->GetFieldID(clazz, stat_field_names[i].pss_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 835 | stat_fields[i].pssSwappable_field = |
| 836 | env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I"); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 837 | stat_fields[i].rss_field = |
| 838 | env->GetFieldID(clazz, stat_field_names[i].rss_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 839 | stat_fields[i].privateDirty_field = |
| 840 | env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I"); |
| 841 | stat_fields[i].sharedDirty_field = |
| 842 | env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 843 | stat_fields[i].privateClean_field = |
| 844 | env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I"); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 845 | stat_fields[i].sharedClean_field = |
| 846 | env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I"); |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 847 | stat_fields[i].swappedOut_field = |
| 848 | env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I"); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 849 | stat_fields[i].swappedOutPss_field = |
| 850 | env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 851 | } |
| 852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods)); |
| 854 | } |
| 855 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 856 | }; // namespace android |