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