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