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 | |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 32 | #include <atomic> |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 33 | #include <iomanip> |
| 34 | #include <string> |
| 35 | |
Josh Gao | 3ce3696 | 2017-01-05 18:28:24 -0800 | [diff] [blame] | 36 | #include <debuggerd/client.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 37 | #include <log/log.h> |
| 38 | #include <utils/misc.h> |
| 39 | #include <utils/String8.h> |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 40 | |
Steven Moreland | bdd5c42 | 2017-08-11 10:57:59 -0700 | [diff] [blame] | 41 | #include <nativehelper/JNIHelp.h> |
| 42 | #include <nativehelper/ScopedUtfChars.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 43 | #include "jni.h" |
| 44 | #include <memtrack/memtrack.h> |
| 45 | #include <memunreachable/memunreachable.h> |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 46 | #include "android_os_Debug.h" |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | namespace android |
| 49 | { |
| 50 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 51 | enum { |
| 52 | HEAP_UNKNOWN, |
| 53 | HEAP_DALVIK, |
| 54 | HEAP_NATIVE, |
Dianne Hackborn | 1842930 | 2014-11-03 13:58:11 -0800 | [diff] [blame] | 55 | |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 56 | HEAP_DALVIK_OTHER, |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 57 | HEAP_STACK, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 58 | HEAP_CURSOR, |
| 59 | HEAP_ASHMEM, |
Dianne Hackborn | 1842930 | 2014-11-03 13:58:11 -0800 | [diff] [blame] | 60 | HEAP_GL_DEV, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 61 | HEAP_UNKNOWN_DEV, |
| 62 | HEAP_SO, |
| 63 | HEAP_JAR, |
| 64 | HEAP_APK, |
| 65 | HEAP_TTF, |
| 66 | HEAP_DEX, |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 67 | HEAP_OAT, |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame] | 68 | HEAP_ART, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 69 | HEAP_UNKNOWN_MAP, |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 70 | HEAP_GRAPHICS, |
| 71 | HEAP_GL, |
| 72 | HEAP_OTHER_MEMTRACK, |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 73 | |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 74 | // Dalvik extra sections (heap). |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 75 | HEAP_DALVIK_NORMAL, |
| 76 | HEAP_DALVIK_LARGE, |
Mathieu Chartier | 25c5e2b | 2014-12-08 16:20:26 -0800 | [diff] [blame] | 77 | HEAP_DALVIK_ZYGOTE, |
| 78 | HEAP_DALVIK_NON_MOVING, |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 79 | |
| 80 | // Dalvik other extra sections. |
| 81 | HEAP_DALVIK_OTHER_LINEARALLOC, |
| 82 | HEAP_DALVIK_OTHER_ACCOUNTING, |
| 83 | HEAP_DALVIK_OTHER_CODE_CACHE, |
| 84 | HEAP_DALVIK_OTHER_COMPILER_METADATA, |
| 85 | HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE, |
| 86 | |
| 87 | // Boot vdex / app dex / app vdex |
| 88 | HEAP_DEX_BOOT_VDEX, |
| 89 | HEAP_DEX_APP_DEX, |
| 90 | HEAP_DEX_APP_VDEX, |
| 91 | |
| 92 | // App art, boot art. |
| 93 | HEAP_ART_APP, |
| 94 | HEAP_ART_BOOT, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 95 | |
| 96 | _NUM_HEAP, |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 97 | _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 98 | _NUM_CORE_HEAP = HEAP_NATIVE+1 |
| 99 | }; |
| 100 | |
| 101 | struct stat_fields { |
| 102 | jfieldID pss_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 103 | jfieldID pssSwappable_field; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 104 | jfieldID rss_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 105 | jfieldID privateDirty_field; |
| 106 | jfieldID sharedDirty_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 107 | jfieldID privateClean_field; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 108 | jfieldID sharedClean_field; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 109 | jfieldID swappedOut_field; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 110 | jfieldID swappedOutPss_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | struct stat_field_names { |
| 114 | const char* pss_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 115 | const char* pssSwappable_name; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 116 | const char* rss_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 117 | const char* privateDirty_name; |
| 118 | const char* sharedDirty_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 119 | const char* privateClean_name; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 120 | const char* sharedClean_name; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 121 | const char* swappedOut_name; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 122 | const char* swappedOutPss_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static stat_fields stat_fields[_NUM_CORE_HEAP]; |
| 126 | |
| 127 | static stat_field_names stat_field_names[_NUM_CORE_HEAP] = { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 128 | { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 129 | "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" }, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 130 | { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 131 | "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" }, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 132 | { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty", |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 133 | "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | jfieldID otherStats_field; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 137 | jfieldID hasSwappedOutPss_field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | |
| 139 | struct stats_t { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 140 | int pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 141 | int swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 142 | int rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 143 | int privateDirty; |
| 144 | int sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 145 | int privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 146 | int sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 147 | int swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 148 | int swappedOutPss; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 151 | enum pss_rollup_support { |
| 152 | PSS_ROLLUP_UNTRIED, |
| 153 | PSS_ROLLUP_SUPPORTED, |
| 154 | PSS_ROLLUP_UNSUPPORTED |
| 155 | }; |
| 156 | |
| 157 | static std::atomic<pss_rollup_support> g_pss_rollup_support; |
| 158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | #define BINDER_STATS "/proc/binder/stats" |
| 160 | |
| 161 | static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) |
| 162 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | struct mallinfo info = mallinfo(); |
| 164 | return (jlong) info.usmblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz) |
| 168 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | struct mallinfo info = mallinfo(); |
| 170 | return (jlong) info.uordblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz) |
| 174 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | struct mallinfo info = mallinfo(); |
| 176 | return (jlong) info.fordblks; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 179 | // Container used to retrieve graphics memory pss |
| 180 | struct graphics_memory_pss |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 181 | { |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 182 | int graphics; |
| 183 | int gl; |
| 184 | int other; |
| 185 | }; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Uses libmemtrack to retrieve graphics memory that the process is using. |
| 189 | * Any graphics memory reported in /proc/pid/smaps is not included here. |
| 190 | */ |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 191 | static int read_memtrack_memory(struct memtrack_proc* p, int pid, |
| 192 | struct graphics_memory_pss* graphics_mem) |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 193 | { |
| 194 | int err = memtrack_proc_get(p, pid); |
| 195 | if (err != 0) { |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 196 | ALOGW("failed to get memory consumption info: %d", err); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 197 | return err; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 200 | ssize_t pss = memtrack_proc_graphics_pss(p); |
| 201 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 202 | ALOGW("failed to get graphics pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 203 | return pss; |
| 204 | } |
| 205 | graphics_mem->graphics = pss / 1024; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 206 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 207 | pss = memtrack_proc_gl_pss(p); |
| 208 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 209 | ALOGW("failed to get gl pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 210 | return pss; |
| 211 | } |
| 212 | graphics_mem->gl = pss / 1024; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 214 | pss = memtrack_proc_other_pss(p); |
| 215 | if (pss < 0) { |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 216 | ALOGW("failed to get other pss: %zd", pss); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 217 | return pss; |
| 218 | } |
| 219 | graphics_mem->other = pss / 1024; |
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 | return 0; |
| 222 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 223 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 224 | /* |
| 225 | * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps. |
| 226 | */ |
| 227 | static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem) |
| 228 | { |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 229 | struct memtrack_proc* p = memtrack_proc_new(); |
| 230 | if (p == NULL) { |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 231 | ALOGW("failed to create memtrack_proc"); |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 232 | return -1; |
| 233 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 234 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 235 | int err = read_memtrack_memory(p, pid, graphics_mem); |
| 236 | memtrack_proc_destroy(p); |
| 237 | return err; |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 240 | static void read_mapinfo(FILE *fp, stats_t* stats, bool* foundSwapPss) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | { |
| 242 | char line[1024]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 243 | int len, nameLen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | bool skip, done = false; |
| 245 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 246 | unsigned pss = 0, swappable_pss = 0, rss = 0; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 247 | float sharing_proportion = 0.0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | unsigned shared_clean = 0, shared_dirty = 0; |
| 249 | unsigned private_clean = 0, private_dirty = 0; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 250 | unsigned swapped_out = 0, swapped_out_pss = 0; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 251 | bool is_swappable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | unsigned temp; |
| 253 | |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 254 | uint64_t start; |
| 255 | uint64_t end = 0; |
| 256 | uint64_t prevEnd = 0; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 257 | char* name; |
| 258 | int name_pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 260 | int whichHeap = HEAP_UNKNOWN; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 261 | int subHeap = HEAP_UNKNOWN; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 262 | int prevHeap = HEAP_UNKNOWN; |
| 263 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 264 | *foundSwapPss = false; |
| 265 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 266 | if(fgets(line, sizeof(line), fp) == 0) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | |
| 268 | while (!done) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 269 | prevHeap = whichHeap; |
| 270 | prevEnd = end; |
| 271 | whichHeap = HEAP_UNKNOWN; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 272 | subHeap = HEAP_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | skip = false; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 274 | is_swappable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
| 276 | len = strlen(line); |
| 277 | if (len < 1) return; |
| 278 | line[--len] = 0; |
| 279 | |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 280 | if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 281 | skip = true; |
| 282 | } else { |
| 283 | while (isspace(line[name_pos])) { |
| 284 | name_pos += 1; |
| 285 | } |
| 286 | name = line + name_pos; |
| 287 | nameLen = strlen(name); |
Mathieu Chartier | d47cbea | 2016-03-03 10:59:33 -0800 | [diff] [blame] | 288 | // Trim the end of the line if it is " (deleted)". |
| 289 | const char* deleted_str = " (deleted)"; |
| 290 | if (nameLen > (int)strlen(deleted_str) && |
| 291 | strcmp(name+nameLen-strlen(deleted_str), deleted_str) == 0) { |
| 292 | nameLen -= strlen(deleted_str); |
| 293 | name[nameLen] = '\0'; |
| 294 | } |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 295 | if ((strstr(name, "[heap]") == name)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 296 | whichHeap = HEAP_NATIVE; |
Colin Cross | cb4728f | 2013-08-09 13:23:46 -0700 | [diff] [blame] | 297 | } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) { |
| 298 | whichHeap = HEAP_NATIVE; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 299 | } else if (strncmp(name, "[stack", 6) == 0) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 300 | whichHeap = HEAP_STACK; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 301 | } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) { |
| 302 | whichHeap = HEAP_SO; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 303 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 304 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) { |
| 305 | whichHeap = HEAP_JAR; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 306 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 307 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) { |
| 308 | whichHeap = HEAP_APK; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 309 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 310 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) { |
| 311 | whichHeap = HEAP_TTF; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 312 | is_swappable = true; |
Mathieu Chartier | 9308462 | 2015-05-01 11:30:22 -0700 | [diff] [blame] | 313 | } else if ((nameLen > 4 && strstr(name, ".dex") != NULL) || |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 314 | (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 315 | whichHeap = HEAP_DEX; |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 316 | subHeap = HEAP_DEX_APP_DEX; |
| 317 | is_swappable = true; |
| 318 | } else if (nameLen > 5 && strcmp(name+nameLen-5, ".vdex") == 0) { |
| 319 | whichHeap = HEAP_DEX; |
| 320 | // Handle system@framework@boot* and system/framework/boot* |
| 321 | if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) { |
| 322 | subHeap = HEAP_DEX_BOOT_VDEX; |
| 323 | } else { |
| 324 | subHeap = HEAP_DEX_APP_VDEX; |
| 325 | } |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 326 | is_swappable = true; |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 327 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) { |
| 328 | whichHeap = HEAP_OAT; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 329 | is_swappable = true; |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame] | 330 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) { |
| 331 | whichHeap = HEAP_ART; |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 332 | // Handle system@framework@boot* and system/framework/boot* |
| 333 | if (strstr(name, "@boot") != NULL || strstr(name, "/boot") != NULL) { |
| 334 | subHeap = HEAP_ART_BOOT; |
| 335 | } else { |
| 336 | subHeap = HEAP_ART_APP; |
| 337 | } |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 338 | is_swappable = true; |
Mathieu Chartier | 9308462 | 2015-05-01 11:30:22 -0700 | [diff] [blame] | 339 | } else if (strncmp(name, "/dev/", 5) == 0) { |
Joel Fernandes (Google) | 299488d | 2018-08-20 23:43:55 -0700 | [diff] [blame] | 340 | whichHeap = HEAP_UNKNOWN_DEV; |
Mathieu Chartier | 9308462 | 2015-05-01 11:30:22 -0700 | [diff] [blame] | 341 | if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) { |
| 342 | whichHeap = HEAP_GL_DEV; |
Joel Fernandes (Google) | 299488d | 2018-08-20 23:43:55 -0700 | [diff] [blame] | 343 | } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) { |
| 344 | whichHeap = HEAP_CURSOR; |
| 345 | } else if (strncmp(name, "/dev/ashmem", 11)) { |
| 346 | whichHeap = HEAP_ASHMEM; |
Mathieu Chartier | 9308462 | 2015-05-01 11:30:22 -0700 | [diff] [blame] | 347 | } |
Colin Cross | cb4728f | 2013-08-09 13:23:46 -0700 | [diff] [blame] | 348 | } else if (strncmp(name, "[anon:", 6) == 0) { |
| 349 | whichHeap = HEAP_UNKNOWN; |
Joel Fernandes (Google) | 299488d | 2018-08-20 23:43:55 -0700 | [diff] [blame] | 350 | if (strncmp(name, "[anon:dalvik-", 13) == 0) { |
| 351 | whichHeap = HEAP_DALVIK_OTHER; |
| 352 | if (strstr(name, "[anon:dalvik-LinearAlloc") == name) { |
| 353 | subHeap = HEAP_DALVIK_OTHER_LINEARALLOC; |
| 354 | } else if ((strstr(name, "[anon:dalvik-alloc space") == name) || |
| 355 | (strstr(name, "[anon:dalvik-main space") == name)) { |
| 356 | // This is the regular Dalvik heap. |
| 357 | whichHeap = HEAP_DALVIK; |
| 358 | subHeap = HEAP_DALVIK_NORMAL; |
| 359 | } else if (strstr(name, "[anon:dalvik-large object space") == name || |
| 360 | strstr(name, "[anon:dalvik-free list large object space") |
| 361 | == name) { |
| 362 | whichHeap = HEAP_DALVIK; |
| 363 | subHeap = HEAP_DALVIK_LARGE; |
| 364 | } else if (strstr(name, "[anon:dalvik-non moving space") == name) { |
| 365 | whichHeap = HEAP_DALVIK; |
| 366 | subHeap = HEAP_DALVIK_NON_MOVING; |
| 367 | } else if (strstr(name, "[anon:dalvik-zygote space") == name) { |
| 368 | whichHeap = HEAP_DALVIK; |
| 369 | subHeap = HEAP_DALVIK_ZYGOTE; |
| 370 | } else if (strstr(name, "[anon:dalvik-indirect ref") == name) { |
| 371 | subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE; |
| 372 | } else if (strstr(name, "[anon:dalvik-jit-code-cache") == name || |
| 373 | strstr(name, "[anon:dalvik-data-code-cache") == name) { |
| 374 | subHeap = HEAP_DALVIK_OTHER_CODE_CACHE; |
| 375 | } else if (strstr(name, "[anon:dalvik-CompilerMetadata") == name) { |
| 376 | subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA; |
| 377 | } else { |
| 378 | subHeap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting. |
| 379 | } |
| 380 | } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 381 | } else if (nameLen > 0) { |
| 382 | whichHeap = HEAP_UNKNOWN_MAP; |
| 383 | } else if (start == prevEnd && prevHeap == HEAP_SO) { |
| 384 | // bss section of a shared library. |
| 385 | whichHeap = HEAP_SO; |
| 386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 389 | //ALOGI("native=%d dalvik=%d sqlite=%d: %s\n", isNativeHeap, isDalvikHeap, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | // isSqliteHeap, line); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 391 | |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 392 | shared_clean = 0; |
| 393 | shared_dirty = 0; |
| 394 | private_clean = 0; |
| 395 | private_dirty = 0; |
| 396 | swapped_out = 0; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 397 | swapped_out_pss = 0; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | while (true) { |
| 400 | if (fgets(line, 1024, fp) == 0) { |
| 401 | done = true; |
| 402 | break; |
| 403 | } |
| 404 | |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 405 | if (line[0] == 'S' && sscanf(line, "Size: %d kB", &temp) == 1) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 406 | /* size = temp; */ |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 407 | } else if (line[0] == 'R' && sscanf(line, "Rss: %d kB", &temp) == 1) { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 408 | rss = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 409 | } else if (line[0] == 'P' && sscanf(line, "Pss: %d kB", &temp) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | pss = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 411 | } else if (line[0] == 'S' && sscanf(line, "Shared_Clean: %d kB", &temp) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | shared_clean = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 413 | } else if (line[0] == 'S' && sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | shared_dirty = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 415 | } else if (line[0] == 'P' && sscanf(line, "Private_Clean: %d kB", &temp) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | private_clean = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 417 | } else if (line[0] == 'P' && sscanf(line, "Private_Dirty: %d kB", &temp) == 1) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | private_dirty = temp; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 419 | } else if (line[0] == 'R' && sscanf(line, "Referenced: %d kB", &temp) == 1) { |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 420 | /* referenced = temp; */ |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 421 | } else if (line[0] == 'S' && sscanf(line, "Swap: %d kB", &temp) == 1) { |
| 422 | swapped_out = temp; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 423 | } else if (line[0] == 'S' && sscanf(line, "SwapPss: %d kB", &temp) == 1) { |
| 424 | *foundSwapPss = true; |
| 425 | swapped_out_pss = temp; |
Colin Cross | 0c6bc73 | 2014-06-17 15:18:07 -0700 | [diff] [blame] | 426 | } else if (sscanf(line, "%" SCNx64 "-%" SCNx64 " %*s %*x %*x:%*x %*d", &start, &end) == 2) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | // looks like a new mapping |
Grace Kloba | bd51116 | 2009-07-08 23:32:25 -0700 | [diff] [blame] | 428 | // example: "10000000-10001000 ---p 10000000 00:00 0" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (!skip) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 434 | if (is_swappable && (pss > 0)) { |
| 435 | sharing_proportion = 0.0; |
| 436 | if ((shared_clean > 0) || (shared_dirty > 0)) { |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 437 | sharing_proportion = (pss - private_clean |
| 438 | - private_dirty)/(shared_clean+shared_dirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 439 | } |
| 440 | swappable_pss = (sharing_proportion*shared_clean) + private_clean; |
| 441 | } else |
| 442 | swappable_pss = 0; |
| 443 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 444 | stats[whichHeap].pss += pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 445 | stats[whichHeap].swappablePss += swappable_pss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 446 | stats[whichHeap].rss += rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 447 | stats[whichHeap].privateDirty += private_dirty; |
| 448 | stats[whichHeap].sharedDirty += shared_dirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 449 | stats[whichHeap].privateClean += private_clean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 450 | stats[whichHeap].sharedClean += shared_clean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 451 | stats[whichHeap].swappedOut += swapped_out; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 452 | stats[whichHeap].swappedOutPss += swapped_out_pss; |
Mathieu Chartier | 95550dd | 2017-07-13 15:01:34 -0700 | [diff] [blame] | 453 | if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER || |
| 454 | whichHeap == HEAP_DEX || whichHeap == HEAP_ART) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 455 | stats[subHeap].pss += pss; |
| 456 | stats[subHeap].swappablePss += swappable_pss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 457 | stats[subHeap].rss += rss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 458 | stats[subHeap].privateDirty += private_dirty; |
| 459 | stats[subHeap].sharedDirty += shared_dirty; |
| 460 | stats[subHeap].privateClean += private_clean; |
| 461 | stats[subHeap].sharedClean += shared_clean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 462 | stats[subHeap].swappedOut += swapped_out; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 463 | stats[subHeap].swappedOutPss += swapped_out_pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 464 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 469 | 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] | 470 | { |
Andreas Gampe | ce71149 | 2016-10-04 18:06:42 -0700 | [diff] [blame] | 471 | *foundSwapPss = false; |
| 472 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 473 | std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid); |
| 474 | UniqueFile fp = MakeUniqueFile(smaps_path.c_str(), "re"); |
| 475 | if (fp == nullptr) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 477 | read_mapinfo(fp.get(), stats, foundSwapPss); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 480 | static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz, |
| 481 | jint pid, jobject object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | { |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 483 | bool foundSwapPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 484 | stats_t stats[_NUM_HEAP]; |
| 485 | memset(&stats, 0, sizeof(stats)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 486 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 487 | load_maps(pid, stats, &foundSwapPss); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 489 | struct graphics_memory_pss graphics_mem; |
| 490 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
| 491 | stats[HEAP_GRAPHICS].pss = graphics_mem.graphics; |
| 492 | stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 493 | stats[HEAP_GRAPHICS].rss = graphics_mem.graphics; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 494 | stats[HEAP_GL].pss = graphics_mem.gl; |
| 495 | stats[HEAP_GL].privateDirty = graphics_mem.gl; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 496 | stats[HEAP_GL].rss = graphics_mem.gl; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 497 | stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other; |
| 498 | stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 499 | stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other; |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 500 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 501 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 502 | for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 503 | stats[HEAP_UNKNOWN].pss += stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 504 | stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 505 | stats[HEAP_UNKNOWN].rss += stats[i].rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 506 | stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty; |
| 507 | stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 508 | stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 509 | stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 510 | stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 511 | stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 515 | env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 516 | env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 517 | env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 518 | env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty); |
| 519 | env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 520 | env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 521 | env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean); |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 522 | env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 523 | env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 524 | } |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 525 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 526 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 527 | env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 528 | jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 529 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 530 | jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0); |
| 531 | if (otherArray == NULL) { |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | int j=0; |
| 536 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 537 | otherArray[j++] = stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 538 | otherArray[j++] = stats[i].swappablePss; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 539 | otherArray[j++] = stats[i].rss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 540 | otherArray[j++] = stats[i].privateDirty; |
| 541 | otherArray[j++] = stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 542 | otherArray[j++] = stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 543 | otherArray[j++] = stats[i].sharedClean; |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 544 | otherArray[j++] = stats[i].swappedOut; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 545 | otherArray[j++] = stats[i].swappedOutPss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 551 | static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object) |
| 552 | { |
| 553 | android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); |
| 554 | } |
| 555 | |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 556 | UniqueFile OpenSmapsOrRollup(int pid) |
| 557 | { |
| 558 | enum pss_rollup_support rollup_support = |
| 559 | g_pss_rollup_support.load(std::memory_order_relaxed); |
| 560 | if (rollup_support != PSS_ROLLUP_UNSUPPORTED) { |
| 561 | std::string smaps_rollup_path = |
| 562 | base::StringPrintf("/proc/%d/smaps_rollup", pid); |
| 563 | UniqueFile fp_rollup = MakeUniqueFile(smaps_rollup_path.c_str(), "re"); |
| 564 | if (fp_rollup == nullptr && errno != ENOENT) { |
| 565 | return fp_rollup; // Actual error, not just old kernel. |
| 566 | } |
| 567 | if (fp_rollup != nullptr) { |
| 568 | if (rollup_support == PSS_ROLLUP_UNTRIED) { |
| 569 | ALOGI("using rollup pss collection"); |
| 570 | g_pss_rollup_support.store(PSS_ROLLUP_SUPPORTED, |
| 571 | std::memory_order_relaxed); |
| 572 | } |
| 573 | return fp_rollup; |
| 574 | } |
| 575 | g_pss_rollup_support.store(PSS_ROLLUP_UNSUPPORTED, |
| 576 | std::memory_order_relaxed); |
| 577 | } |
| 578 | |
| 579 | std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid); |
| 580 | return MakeUniqueFile(smaps_path.c_str(), "re"); |
| 581 | } |
| 582 | |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 583 | static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 584 | jlongArray outUssSwapPssRss, jlongArray outMemtrack) |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 585 | { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 586 | char lineBuffer[1024]; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 587 | jlong pss = 0; |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 588 | jlong rss = 0; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 589 | jlong swapPss = 0; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 590 | jlong uss = 0; |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 591 | jlong memtrack = 0; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 592 | |
Adam Lesinski | 5b4ef81 | 2013-09-23 10:06:09 -0700 | [diff] [blame] | 593 | struct graphics_memory_pss graphics_mem; |
| 594 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
Dianne Hackborn | 8c76d91 | 2018-08-23 15:20:05 -0700 | [diff] [blame] | 595 | 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] | 596 | } |
| 597 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 598 | { |
Daniel Colascione | 6c51810 | 2017-07-27 03:33:34 -0700 | [diff] [blame] | 599 | UniqueFile fp = OpenSmapsOrRollup(pid); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 600 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 601 | if (fp != nullptr) { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 602 | char* line; |
| 603 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 604 | while (true) { |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 605 | if (fgets(lineBuffer, sizeof (lineBuffer), fp.get()) == NULL) { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 606 | break; |
| 607 | } |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 608 | line = lineBuffer; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 609 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 610 | switch (line[0]) { |
| 611 | case 'P': |
| 612 | if (strncmp(line, "Pss:", 4) == 0) { |
| 613 | char* c = line + 4; |
| 614 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 615 | c++; |
| 616 | } |
| 617 | pss += atoi(c); |
| 618 | } else if (strncmp(line, "Private_Clean:", 14) == 0 |
| 619 | || strncmp(line, "Private_Dirty:", 14) == 0) { |
| 620 | char* c = line + 14; |
| 621 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 622 | c++; |
| 623 | } |
| 624 | uss += atoi(c); |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 625 | } |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 626 | break; |
| 627 | case 'R': |
| 628 | if (strncmp(line, "Rss:", 4) == 0) { |
| 629 | char* c = line + 4; |
| 630 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 631 | c++; |
| 632 | } |
| 633 | rss += atoi(c); |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 634 | } |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 635 | break; |
| 636 | case 'S': |
| 637 | if (strncmp(line, "SwapPss:", 8) == 0) { |
| 638 | char* c = line + 8; |
| 639 | jlong lSwapPss; |
| 640 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 641 | c++; |
| 642 | } |
| 643 | lSwapPss = atoi(c); |
| 644 | swapPss += lSwapPss; |
| 645 | pss += lSwapPss; // Also in swap, those pages would be accounted as Pss without SWAP |
| 646 | } |
| 647 | break; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 648 | } |
Dianne Hackborn | cfc837f | 2013-06-27 18:32:07 -0700 | [diff] [blame] | 649 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 650 | } |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 651 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 652 | |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 653 | if (outUssSwapPssRss != NULL) { |
| 654 | if (env->GetArrayLength(outUssSwapPssRss) >= 1) { |
| 655 | jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0); |
| 656 | if (outUssSwapPssRssArray != NULL) { |
| 657 | outUssSwapPssRssArray[0] = uss; |
| 658 | if (env->GetArrayLength(outUssSwapPssRss) >= 2) { |
| 659 | outUssSwapPssRssArray[1] = swapPss; |
| 660 | } |
| 661 | if (env->GetArrayLength(outUssSwapPssRss) >= 3) { |
| 662 | outUssSwapPssRssArray[2] = rss; |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 663 | } |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 664 | } |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 665 | env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 668 | |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 669 | if (outMemtrack != NULL) { |
| 670 | if (env->GetArrayLength(outMemtrack) >= 1) { |
| 671 | jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0); |
| 672 | if (outMemtrackArray != NULL) { |
| 673 | outMemtrackArray[0] = memtrack; |
| 674 | } |
| 675 | env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0); |
| 676 | } |
| 677 | } |
| 678 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 679 | return pss; |
| 680 | } |
| 681 | |
| 682 | static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz) |
| 683 | { |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 684 | return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 687 | static long get_allocated_vmalloc_memory() { |
| 688 | char line[1024]; |
Sandeep Patil | d70e202 | 2018-11-20 17:26:56 -0800 | [diff] [blame] | 689 | |
| 690 | long vmalloc_allocated_size = 0; |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 691 | |
| 692 | UniqueFile fp = MakeUniqueFile("/proc/vmallocinfo", "re"); |
| 693 | if (fp == nullptr) { |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 694 | return 0; |
| 695 | } |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 696 | |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 697 | while (true) { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 698 | if (fgets(line, 1024, fp.get()) == NULL) { |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 699 | break; |
| 700 | } |
Sandeep Patil | d70e202 | 2018-11-20 17:26:56 -0800 | [diff] [blame] | 701 | |
| 702 | // check to see if there are pages mapped in vmalloc area |
| 703 | if (!strstr(line, "pages=")) { |
| 704 | continue; |
Martijn Coenen | 6b8a522 | 2016-04-27 14:28:17 +0200 | [diff] [blame] | 705 | } |
Sandeep Patil | d70e202 | 2018-11-20 17:26:56 -0800 | [diff] [blame] | 706 | |
| 707 | long nr_pages; |
| 708 | if (sscanf(line, "%*x-%*x %*ld %*s pages=%ld", &nr_pages) == 1) { |
| 709 | vmalloc_allocated_size += (nr_pages * getpagesize()); |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 710 | } |
| 711 | } |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 712 | return vmalloc_allocated_size; |
| 713 | } |
| 714 | |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 715 | enum { |
| 716 | MEMINFO_TOTAL, |
| 717 | MEMINFO_FREE, |
| 718 | MEMINFO_BUFFERS, |
| 719 | MEMINFO_CACHED, |
| 720 | MEMINFO_SHMEM, |
| 721 | MEMINFO_SLAB, |
Robert Benea | 5e09980 | 2017-10-04 18:28:01 -0700 | [diff] [blame] | 722 | MEMINFO_SLAB_RECLAIMABLE, |
| 723 | MEMINFO_SLAB_UNRECLAIMABLE, |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 724 | MEMINFO_SWAP_TOTAL, |
| 725 | MEMINFO_SWAP_FREE, |
| 726 | MEMINFO_ZRAM_TOTAL, |
Dianne Hackborn | b3af4ec | 2014-10-17 15:25:13 -0700 | [diff] [blame] | 727 | MEMINFO_MAPPED, |
| 728 | MEMINFO_VMALLOC_USED, |
| 729 | MEMINFO_PAGE_TABLES, |
| 730 | MEMINFO_KERNEL_STACK, |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 731 | MEMINFO_COUNT |
| 732 | }; |
| 733 | |
Greg Hackmann | 228fa5f | 2015-12-09 16:36:08 -0800 | [diff] [blame] | 734 | static long long get_zram_mem_used() |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 735 | { |
| 736 | #define ZRAM_SYSFS "/sys/block/zram0/" |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 737 | UniqueFile mm_stat_file = MakeUniqueFile(ZRAM_SYSFS "mm_stat", "re"); |
| 738 | if (mm_stat_file) { |
Greg Hackmann | 228fa5f | 2015-12-09 16:36:08 -0800 | [diff] [blame] | 739 | long long mem_used_total = 0; |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 740 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 741 | int matched = fscanf(mm_stat_file.get(), "%*d %*d %lld %*d %*d %*d %*d", &mem_used_total); |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 742 | if (matched != 1) |
| 743 | ALOGW("failed to parse " ZRAM_SYSFS "mm_stat"); |
| 744 | |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 745 | return mem_used_total; |
| 746 | } |
| 747 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 748 | UniqueFile mem_used_total_file = MakeUniqueFile(ZRAM_SYSFS "mem_used_total", "re"); |
| 749 | if (mem_used_total_file) { |
Greg Hackmann | 228fa5f | 2015-12-09 16:36:08 -0800 | [diff] [blame] | 750 | long long mem_used_total = 0; |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 751 | |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 752 | int matched = fscanf(mem_used_total_file.get(), "%lld", &mem_used_total); |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 753 | if (matched != 1) |
| 754 | ALOGW("failed to parse " ZRAM_SYSFS "mem_used_total"); |
| 755 | |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 756 | return mem_used_total; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 762 | static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out) |
| 763 | { |
Dianne Hackborn | b79a5f9 | 2017-12-08 17:09:26 -0800 | [diff] [blame] | 764 | char buffer[4096]; |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 765 | size_t numFound = 0; |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 766 | |
| 767 | if (out == NULL) { |
| 768 | jniThrowNullPointerException(env, "out == null"); |
| 769 | return; |
| 770 | } |
| 771 | |
Nick Kralevich | 422dd00f | 2017-10-19 17:51:50 -0700 | [diff] [blame] | 772 | int fd = open("/proc/meminfo", O_RDONLY | O_CLOEXEC); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 773 | |
| 774 | if (fd < 0) { |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 775 | ALOGW("Unable to open /proc/meminfo: %s\n", strerror(errno)); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 779 | int len = read(fd, buffer, sizeof(buffer)-1); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 780 | close(fd); |
| 781 | |
| 782 | if (len < 0) { |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 783 | ALOGW("Empty /proc/meminfo"); |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | buffer[len] = 0; |
| 787 | |
| 788 | static const char* const tags[] = { |
| 789 | "MemTotal:", |
| 790 | "MemFree:", |
| 791 | "Buffers:", |
| 792 | "Cached:", |
| 793 | "Shmem:", |
| 794 | "Slab:", |
Robert Benea | 5e09980 | 2017-10-04 18:28:01 -0700 | [diff] [blame] | 795 | "SReclaimable:", |
| 796 | "SUnreclaim:", |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 797 | "SwapTotal:", |
| 798 | "SwapFree:", |
Dianne Hackborn | b3af4ec | 2014-10-17 15:25:13 -0700 | [diff] [blame] | 799 | "ZRam:", |
| 800 | "Mapped:", |
| 801 | "VmallocUsed:", |
| 802 | "PageTables:", |
| 803 | "KernelStack:", |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 804 | NULL |
| 805 | }; |
| 806 | static const int tagsLen[] = { |
| 807 | 9, |
| 808 | 8, |
| 809 | 8, |
| 810 | 7, |
| 811 | 6, |
| 812 | 5, |
Robert Benea | 5e09980 | 2017-10-04 18:28:01 -0700 | [diff] [blame] | 813 | 13, |
| 814 | 11, |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 815 | 10, |
| 816 | 9, |
Dianne Hackborn | b3af4ec | 2014-10-17 15:25:13 -0700 | [diff] [blame] | 817 | 5, |
| 818 | 7, |
| 819 | 12, |
| 820 | 11, |
| 821 | 12, |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 822 | 0 |
| 823 | }; |
Robert Benea | 5e09980 | 2017-10-04 18:28:01 -0700 | [diff] [blame] | 824 | long mem[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 825 | |
| 826 | char* p = buffer; |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 827 | while (*p && numFound < (sizeof(tagsLen) / sizeof(tagsLen[0]))) { |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 828 | int i = 0; |
| 829 | while (tags[i]) { |
| 830 | if (strncmp(p, tags[i], tagsLen[i]) == 0) { |
| 831 | p += tagsLen[i]; |
| 832 | while (*p == ' ') p++; |
| 833 | char* num = p; |
| 834 | while (*p >= '0' && *p <= '9') p++; |
| 835 | if (*p != 0) { |
| 836 | *p = 0; |
| 837 | p++; |
| 838 | } |
| 839 | mem[i] = atoll(num); |
| 840 | numFound++; |
| 841 | break; |
| 842 | } |
| 843 | i++; |
| 844 | } |
| 845 | while (*p && *p != '\n') { |
| 846 | p++; |
| 847 | } |
| 848 | if (*p) p++; |
| 849 | } |
| 850 | |
Greg Hackmann | 187a6ae | 2015-12-09 09:36:12 -0800 | [diff] [blame] | 851 | mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used() / 1024; |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 852 | // Recompute Vmalloc Used since the value in meminfo |
| 853 | // doesn't account for I/O remapping which doesn't use RAM. |
| 854 | mem[MEMINFO_VMALLOC_USED] = get_allocated_vmalloc_memory() / 1024; |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 855 | |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 856 | int maxNum = env->GetArrayLength(out); |
Dianne Hackborn | cbd9a52 | 2013-09-24 23:10:14 -0700 | [diff] [blame] | 857 | if (maxNum > MEMINFO_COUNT) { |
| 858 | maxNum = MEMINFO_COUNT; |
| 859 | } |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 860 | jlong* outArray = env->GetLongArrayElements(out, 0); |
| 861 | if (outArray != NULL) { |
Dianne Hackborn | cb42855 | 2013-09-26 11:07:17 -0700 | [diff] [blame] | 862 | for (int i=0; i<maxNum; i++) { |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 863 | outArray[i] = mem[i]; |
| 864 | } |
| 865 | } |
| 866 | env->ReleaseLongArrayElements(out, outArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Martijn Coenen | 398e24e | 2015-04-23 21:55:58 +0200 | [diff] [blame] | 869 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 870 | static jint read_binder_stat(const char* stat) |
| 871 | { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 872 | UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re"); |
| 873 | if (fp == nullptr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | return -1; |
| 875 | } |
| 876 | |
| 877 | char line[1024]; |
| 878 | |
| 879 | char compare[128]; |
| 880 | int len = snprintf(compare, 128, "proc %d", getpid()); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | // loop until we have the block that represents this process |
| 883 | do { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 884 | if (fgets(line, 1024, fp.get()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | return -1; |
| 886 | } |
| 887 | } while (strncmp(compare, line, len)); |
| 888 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 889 | // 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] | 890 | len = snprintf(compare, 128, " %s: ", stat); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | do { |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 893 | if (fgets(line, 1024, fp.get()) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 894 | return -1; |
| 895 | } |
| 896 | } while (strncmp(compare, line, len)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 897 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | // we have the line, now increment the line ptr to the value |
| 899 | char* ptr = line + len; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 900 | jint result = atoi(ptr); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 901 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz) |
| 905 | { |
| 906 | return read_binder_stat("bcTRANSACTION"); |
| 907 | } |
| 908 | |
| 909 | static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz) |
| 910 | { |
| 911 | return read_binder_stat("brTRANSACTION"); |
| 912 | } |
| 913 | |
| 914 | // these are implemented in android_util_Binder.cpp |
| 915 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz); |
| 916 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz); |
| 917 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz); |
| 918 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 919 | static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp) |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 920 | { |
| 921 | if (fileDescriptor == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 922 | jniThrowNullPointerException(env, "fd == null"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 923 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 924 | } |
| 925 | int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 926 | if (origFd < 0) { |
| 927 | jniThrowRuntimeException(env, "Invalid file descriptor"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 928 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* dup() the descriptor so we don't close the original with fclose() */ |
| 932 | int fd = dup(origFd); |
| 933 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 934 | ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 935 | jniThrowRuntimeException(env, "dup() failed"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 936 | return false; |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 939 | fp.reset(fdopen(fd, "w")); |
Andreas Gampe | 7ad2f6e | 2016-10-05 12:59:18 -0700 | [diff] [blame] | 940 | if (fp == nullptr) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 941 | ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 942 | close(fd); |
| 943 | jniThrowRuntimeException(env, "fdopen() failed"); |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | return true; |
| 947 | } |
| 948 | |
Christopher Ferris | 38e2c3b | 2018-06-13 13:19:57 -0700 | [diff] [blame] | 949 | /* pulled out of bionic */ |
| 950 | extern "C" void write_malloc_leak_info(FILE* fp); |
| 951 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 952 | /* |
| 953 | * Dump the native heap, writing human-readable output to the specified |
| 954 | * file descriptor. |
| 955 | */ |
| 956 | static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject, |
| 957 | jobject fileDescriptor) |
| 958 | { |
| 959 | UniqueFile fp(nullptr, safeFclose); |
| 960 | if (!openFile(env, fileDescriptor, fp)) { |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 964 | ALOGD("Native heap dump starting...\n"); |
Christopher Ferris | 38e2c3b | 2018-06-13 13:19:57 -0700 | [diff] [blame] | 965 | // Formatting of the native heap dump is handled by malloc debug itself. |
| 966 | // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format |
| 967 | write_malloc_leak_info(fp.get()); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 968 | ALOGD("Native heap dump complete.\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 971 | /* |
| 972 | * Dump the native malloc info, writing xml output to the specified |
| 973 | * file descriptor. |
| 974 | */ |
| 975 | static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject, |
| 976 | jobject fileDescriptor) |
| 977 | { |
| 978 | UniqueFile fp(nullptr, safeFclose); |
| 979 | if (!openFile(env, fileDescriptor, fp)) { |
| 980 | return; |
| 981 | } |
| 982 | |
| 983 | malloc_info(0, fp.get()); |
| 984 | } |
| 985 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 986 | static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs, |
| 987 | DebuggerdDumpType dumpType) { |
| 988 | const ScopedUtfChars fileNameChars(env, fileName); |
| 989 | if (fileNameChars.c_str() == nullptr) { |
| 990 | return false; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 993 | android::base::unique_fd fd(open(fileNameChars.c_str(), |
| 994 | O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND, |
| 995 | 0666)); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 996 | if (fd < 0) { |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 997 | fprintf(stderr, "Can't open %s: %s\n", fileNameChars.c_str(), strerror(errno)); |
| 998 | return false; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 1001 | return (dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd) == 0); |
| 1002 | } |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1003 | |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 1004 | static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 1005 | jint pid, jstring fileName, jint timeoutSecs) { |
| 1006 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace); |
| 1007 | return ret ? JNI_TRUE : JNI_FALSE; |
| 1008 | } |
| 1009 | |
| 1010 | static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 1011 | jint pid, jstring fileName, jint timeoutSecs) { |
| 1012 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace); |
| 1013 | return ret ? JNI_TRUE : JNI_FALSE; |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 1016 | static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz, |
| 1017 | jint limit, jboolean contents) |
| 1018 | { |
| 1019 | std::string s = GetUnreachableMemoryString(contents, limit); |
| 1020 | return env->NewStringUTF(s.c_str()); |
| 1021 | } |
| 1022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | /* |
| 1024 | * JNI registration. |
| 1025 | */ |
| 1026 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1027 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | { "getNativeHeapSize", "()J", |
| 1029 | (void*) android_os_Debug_getNativeHeapSize }, |
| 1030 | { "getNativeHeapAllocatedSize", "()J", |
| 1031 | (void*) android_os_Debug_getNativeHeapAllocatedSize }, |
| 1032 | { "getNativeHeapFreeSize", "()J", |
| 1033 | (void*) android_os_Debug_getNativeHeapFreeSize }, |
| 1034 | { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V", |
| 1035 | (void*) android_os_Debug_getDirtyPages }, |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 1036 | { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V", |
| 1037 | (void*) android_os_Debug_getDirtyPagesPid }, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 1038 | { "getPss", "()J", |
| 1039 | (void*) android_os_Debug_getPss }, |
Dianne Hackborn | 1a4b5a4 | 2014-12-08 17:43:31 -0800 | [diff] [blame] | 1040 | { "getPss", "(I[J[J)J", |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 1041 | (void*) android_os_Debug_getPssPid }, |
Dianne Hackborn | 8e69257 | 2013-09-10 19:06:15 -0700 | [diff] [blame] | 1042 | { "getMemInfo", "([J)V", |
| 1043 | (void*) android_os_Debug_getMemInfo }, |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 1044 | { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", |
| 1045 | (void*) android_os_Debug_dumpNativeHeap }, |
Christopher Ferris | 8d652f8 | 2017-04-11 16:29:18 -0700 | [diff] [blame] | 1046 | { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V", |
| 1047 | (void*) android_os_Debug_dumpNativeMallocInfo }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | { "getBinderSentTransactions", "()I", |
| 1049 | (void*) android_os_Debug_getBinderSentTransactions }, |
| 1050 | { "getBinderReceivedTransactions", "()I", |
| 1051 | (void*) android_os_getBinderReceivedTransactions }, |
| 1052 | { "getBinderLocalObjectCount", "()I", |
| 1053 | (void*)android_os_Debug_getLocalObjectCount }, |
| 1054 | { "getBinderProxyObjectCount", "()I", |
| 1055 | (void*)android_os_Debug_getProxyObjectCount }, |
| 1056 | { "getBinderDeathObjectCount", "()I", |
| 1057 | (void*)android_os_Debug_getDeathObjectCount }, |
Narayan Kamath | f013daa | 2017-05-09 12:55:02 +0100 | [diff] [blame] | 1058 | { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
| 1059 | (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout }, |
| 1060 | { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
songjinshi | e02e3ea | 2016-12-16 17:48:21 +0800 | [diff] [blame] | 1061 | (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout }, |
Colin Cross | c4fb5f9 | 2016-02-02 16:51:15 -0800 | [diff] [blame] | 1062 | { "getUnreachableMemory", "(IZ)Ljava/lang/String;", |
| 1063 | (void*)android_os_Debug_getUnreachableMemory }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | }; |
| 1065 | |
| 1066 | int register_android_os_Debug(JNIEnv *env) |
| 1067 | { |
| 1068 | jclass clazz = env->FindClass("android/os/Debug$MemoryInfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 1070 | // Sanity check the number of other statistics expected in Java matches here. |
| 1071 | jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I"); |
| 1072 | jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 1073 | jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I"); |
| 1074 | jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 1075 | int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 1076 | if ((numOtherStats + numDvkStats) != expectedNumOtherStats) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 1077 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 1078 | "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d", |
| 1079 | numOtherStats+numDvkStats, expectedNumOtherStats); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 1080 | return JNI_ERR; |
| 1081 | } |
| 1082 | |
| 1083 | otherStats_field = env->GetFieldID(clazz, "otherStats", "[I"); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 1084 | hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z"); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 1085 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 1086 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 1087 | stat_fields[i].pss_field = |
| 1088 | env->GetFieldID(clazz, stat_field_names[i].pss_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 1089 | stat_fields[i].pssSwappable_field = |
| 1090 | env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I"); |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 1091 | stat_fields[i].rss_field = |
| 1092 | env->GetFieldID(clazz, stat_field_names[i].rss_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 1093 | stat_fields[i].privateDirty_field = |
| 1094 | env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I"); |
| 1095 | stat_fields[i].sharedDirty_field = |
| 1096 | env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 1097 | stat_fields[i].privateClean_field = |
| 1098 | env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I"); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 1099 | stat_fields[i].sharedClean_field = |
| 1100 | env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I"); |
Dianne Hackborn | 8883ced | 2013-10-02 16:58:06 -0700 | [diff] [blame] | 1101 | stat_fields[i].swappedOut_field = |
| 1102 | env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I"); |
Martijn Coenen | e076485 | 2016-01-07 17:04:22 -0800 | [diff] [blame] | 1103 | stat_fields[i].swappedOutPss_field = |
| 1104 | env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods)); |
| 1108 | } |
| 1109 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 1110 | }; // namespace android |