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