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