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