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" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame^] | 24 | #include <cutils/log.h> |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | #include <time.h> |
| 31 | #include <sys/time.h> |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 32 | #include <errno.h> |
| 33 | #include <assert.h> |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 34 | #include <ctype.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | #ifdef HAVE_MALLOC_H |
| 37 | #include <malloc.h> |
| 38 | #endif |
| 39 | |
| 40 | namespace android |
| 41 | { |
| 42 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 43 | enum { |
| 44 | HEAP_UNKNOWN, |
| 45 | HEAP_DALVIK, |
| 46 | HEAP_NATIVE, |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 47 | HEAP_DALVIK_OTHER, |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 48 | HEAP_STACK, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 49 | HEAP_CURSOR, |
| 50 | HEAP_ASHMEM, |
| 51 | HEAP_UNKNOWN_DEV, |
| 52 | HEAP_SO, |
| 53 | HEAP_JAR, |
| 54 | HEAP_APK, |
| 55 | HEAP_TTF, |
| 56 | HEAP_DEX, |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 57 | HEAP_OAT, |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame] | 58 | HEAP_ART, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 59 | HEAP_UNKNOWN_MAP, |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 60 | HEAP_GPU, |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 61 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 62 | HEAP_DALVIK_NORMAL, |
| 63 | HEAP_DALVIK_LARGE, |
| 64 | HEAP_DALVIK_LINEARALLOC, |
| 65 | HEAP_DALVIK_ACCOUNTING, |
| 66 | HEAP_DALVIK_CODE_CACHE, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 67 | |
| 68 | _NUM_HEAP, |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 69 | _NUM_EXCLUSIVE_HEAP = HEAP_GPU+1, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 70 | _NUM_CORE_HEAP = HEAP_NATIVE+1 |
| 71 | }; |
| 72 | |
| 73 | struct stat_fields { |
| 74 | jfieldID pss_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 75 | jfieldID pssSwappable_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 76 | jfieldID privateDirty_field; |
| 77 | jfieldID sharedDirty_field; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 78 | jfieldID privateClean_field; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 79 | jfieldID sharedClean_field; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | struct stat_field_names { |
| 83 | const char* pss_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 84 | const char* pssSwappable_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 85 | const char* privateDirty_name; |
| 86 | const char* sharedDirty_name; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 87 | const char* privateClean_name; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 88 | const char* sharedClean_name; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static stat_fields stat_fields[_NUM_CORE_HEAP]; |
| 92 | |
| 93 | static stat_field_names stat_field_names[_NUM_CORE_HEAP] = { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 94 | { "otherPss", "otherSwappablePss", "otherPrivateDirty", "otherSharedDirty", "otherPrivateClean", "otherSharedClean" }, |
| 95 | { "dalvikPss", "dalvikSwappablePss", "dalvikPrivateDirty", "dalvikSharedDirty", "dalvikPrivateClean", "dalvikSharedClean" }, |
| 96 | { "nativePss", "nativeSwappablePss", "nativePrivateDirty", "nativeSharedDirty", "nativePrivateClean", "nativeSharedClean" } |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | jfieldID otherStats_field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
| 101 | struct stats_t { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 102 | int pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 103 | int swappablePss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 104 | int privateDirty; |
| 105 | int sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 106 | int privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 107 | int sharedClean; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | #define BINDER_STATS "/proc/binder/stats" |
| 111 | |
| 112 | static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) |
| 113 | { |
| 114 | #ifdef HAVE_MALLOC_H |
| 115 | struct mallinfo info = mallinfo(); |
| 116 | return (jlong) info.usmblks; |
| 117 | #else |
| 118 | return -1; |
| 119 | #endif |
| 120 | } |
| 121 | |
| 122 | static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz) |
| 123 | { |
| 124 | #ifdef HAVE_MALLOC_H |
| 125 | struct mallinfo info = mallinfo(); |
| 126 | return (jlong) info.uordblks; |
| 127 | #else |
| 128 | return -1; |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz) |
| 133 | { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 134 | #ifdef HAVE_MALLOC_H |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | struct mallinfo info = mallinfo(); |
| 136 | return (jlong) info.fordblks; |
| 137 | #else |
| 138 | return -1; |
| 139 | #endif |
| 140 | } |
| 141 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 142 | // XXX Qualcom-specific! |
| 143 | static jlong read_gpu_mem(int pid) |
| 144 | { |
| 145 | char line[1024]; |
| 146 | jlong uss = 0; |
| 147 | unsigned temp; |
| 148 | |
| 149 | char tmp[128]; |
| 150 | FILE *fp; |
| 151 | |
| 152 | sprintf(tmp, "/d/kgsl/proc/%d/mem", pid); |
| 153 | fp = fopen(tmp, "r"); |
| 154 | if (fp == 0) { |
| 155 | //ALOGI("Unable to open: %s", tmp); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | while (true) { |
| 160 | if (fgets(line, 1024, fp) == NULL) { |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | //ALOGI("Read: %s", line); |
| 165 | |
| 166 | // Format is: |
| 167 | // gpuaddr useraddr size id flags type usage sglen |
| 168 | // 54676000 54676000 4096 1 ----p gpumem arraybuffer 1 |
| 169 | // |
| 170 | // If useraddr is 0, this is gpu mem not otherwise accounted |
| 171 | // against the process. |
| 172 | |
| 173 | // Make sure line is long enough. |
| 174 | int i = 0; |
| 175 | while (i < 9) { |
| 176 | if (line[i] == 0) { |
| 177 | break; |
| 178 | } |
| 179 | i++; |
| 180 | } |
| 181 | if (i < 9) { |
| 182 | //ALOGI("Early line term!"); |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | // Look to see if useraddr is 00000000. |
| 187 | while (i < 17) { |
| 188 | if (line[i] != '0') { |
| 189 | break; |
| 190 | } |
| 191 | i++; |
| 192 | } |
| 193 | if (i < 17) { |
| 194 | //ALOGI("useraddr not 0!"); |
| 195 | continue; |
| 196 | } |
| 197 | |
| 198 | uss += atoi(line + i); |
| 199 | //ALOGI("Uss now: %ld", uss); |
| 200 | } |
| 201 | |
| 202 | fclose(fp); |
| 203 | |
| 204 | // Convert from bytes to KB. |
| 205 | return uss / 1024; |
| 206 | } |
| 207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | static void read_mapinfo(FILE *fp, stats_t* stats) |
| 209 | { |
| 210 | char line[1024]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 211 | int len, nameLen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | bool skip, done = false; |
| 213 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 214 | unsigned size = 0, resident = 0, pss = 0, swappable_pss = 0; |
| 215 | float sharing_proportion = 0.0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | unsigned shared_clean = 0, shared_dirty = 0; |
| 217 | unsigned private_clean = 0, private_dirty = 0; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 218 | bool is_swappable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | unsigned referenced = 0; |
| 220 | unsigned temp; |
| 221 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 222 | unsigned long int start; |
| 223 | unsigned long int end = 0; |
| 224 | unsigned long int prevEnd = 0; |
| 225 | char* name; |
| 226 | int name_pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 228 | int whichHeap = HEAP_UNKNOWN; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 229 | int subHeap = HEAP_UNKNOWN; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 230 | int prevHeap = HEAP_UNKNOWN; |
| 231 | |
| 232 | if(fgets(line, sizeof(line), fp) == 0) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | |
| 234 | while (!done) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 235 | prevHeap = whichHeap; |
| 236 | prevEnd = end; |
| 237 | whichHeap = HEAP_UNKNOWN; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 238 | subHeap = HEAP_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | skip = false; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 240 | is_swappable = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
| 242 | len = strlen(line); |
| 243 | if (len < 1) return; |
| 244 | line[--len] = 0; |
| 245 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 246 | if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) { |
| 247 | skip = true; |
| 248 | } else { |
| 249 | while (isspace(line[name_pos])) { |
| 250 | name_pos += 1; |
| 251 | } |
| 252 | name = line + name_pos; |
| 253 | nameLen = strlen(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 255 | if ((strstr(name, "[heap]") == name)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 256 | whichHeap = HEAP_NATIVE; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 257 | } else if (strncmp(name, "/dev/ashmem", 11) == 0) { |
| 258 | if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) { |
| 259 | whichHeap = HEAP_DALVIK_OTHER; |
| 260 | if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) { |
| 261 | subHeap = HEAP_DALVIK_LINEARALLOC; |
| 262 | } else if ((strstr(name, "/dev/ashmem/dalvik-mark") == name) || |
| 263 | (strstr(name, "/dev/ashmem/dalvik-allocspace alloc space live-bitmap") == name) || |
| 264 | (strstr(name, "/dev/ashmem/dalvik-allocspace alloc space mark-bitmap") == name) || |
| 265 | (strstr(name, "/dev/ashmem/dalvik-card table") == name) || |
| 266 | (strstr(name, "/dev/ashmem/dalvik-allocation stack") == name) || |
| 267 | (strstr(name, "/dev/ashmem/dalvik-live stack") == name) || |
| 268 | (strstr(name, "/dev/ashmem/dalvik-imagespace") == name) || |
| 269 | (strstr(name, "/dev/ashmem/dalvik-bitmap") == name) || |
| 270 | (strstr(name, "/dev/ashmem/dalvik-card-table") == name) || |
| 271 | (strstr(name, "/dev/ashmem/dalvik-mark-stack") == name) || |
| 272 | (strstr(name, "/dev/ashmem/dalvik-aux-structure") == name)) { |
| 273 | subHeap = HEAP_DALVIK_ACCOUNTING; |
| 274 | } else if (strstr(name, "/dev/ashmem/dalvik-large") == name) { |
| 275 | whichHeap = HEAP_DALVIK; |
| 276 | subHeap = HEAP_DALVIK_LARGE; |
| 277 | } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name) { |
| 278 | subHeap = HEAP_DALVIK_CODE_CACHE; |
| 279 | } else { |
| 280 | // This is the regular Dalvik heap. |
| 281 | whichHeap = HEAP_DALVIK; |
| 282 | subHeap = HEAP_DALVIK_NORMAL; |
| 283 | } |
| 284 | } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) { |
| 285 | whichHeap = HEAP_CURSOR; |
| 286 | } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) { |
| 287 | whichHeap = HEAP_NATIVE; |
| 288 | } else { |
| 289 | whichHeap = HEAP_ASHMEM; |
| 290 | } |
Colin Cross | cb4728f | 2013-08-09 13:23:46 -0700 | [diff] [blame] | 291 | } else if (strncmp(name, "[anon:libc_malloc]", 18) == 0) { |
| 292 | whichHeap = HEAP_NATIVE; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 293 | } else if (strncmp(name, "[stack", 6) == 0) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 294 | whichHeap = HEAP_STACK; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 295 | } else if (strncmp(name, "/dev/", 5) == 0) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 296 | whichHeap = HEAP_UNKNOWN_DEV; |
| 297 | } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) { |
| 298 | whichHeap = HEAP_SO; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 299 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 300 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) { |
| 301 | whichHeap = HEAP_JAR; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 302 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 303 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) { |
| 304 | whichHeap = HEAP_APK; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 305 | is_swappable = true; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 306 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) { |
| 307 | whichHeap = HEAP_TTF; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 308 | is_swappable = true; |
Ian Rogers | 9f8589c | 2013-02-22 20:35:06 -0800 | [diff] [blame] | 309 | } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) || |
| 310 | (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 311 | whichHeap = HEAP_DEX; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 312 | is_swappable = true; |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 313 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) { |
| 314 | whichHeap = HEAP_OAT; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 315 | is_swappable = true; |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame] | 316 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) { |
| 317 | whichHeap = HEAP_ART; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 318 | is_swappable = true; |
Colin Cross | cb4728f | 2013-08-09 13:23:46 -0700 | [diff] [blame] | 319 | } else if (strncmp(name, "[anon:", 6) == 0) { |
| 320 | whichHeap = HEAP_UNKNOWN; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 321 | } else if (nameLen > 0) { |
| 322 | whichHeap = HEAP_UNKNOWN_MAP; |
| 323 | } else if (start == prevEnd && prevHeap == HEAP_SO) { |
| 324 | // bss section of a shared library. |
| 325 | whichHeap = HEAP_SO; |
| 326 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 329 | //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] | 330 | // isSqliteHeap, line); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | while (true) { |
| 333 | if (fgets(line, 1024, fp) == 0) { |
| 334 | done = true; |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | if (sscanf(line, "Size: %d kB", &temp) == 1) { |
| 339 | size = temp; |
| 340 | } else if (sscanf(line, "Rss: %d kB", &temp) == 1) { |
| 341 | resident = temp; |
| 342 | } else if (sscanf(line, "Pss: %d kB", &temp) == 1) { |
| 343 | pss = temp; |
| 344 | } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) { |
| 345 | shared_clean = temp; |
| 346 | } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) { |
| 347 | shared_dirty = temp; |
| 348 | } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) { |
| 349 | private_clean = temp; |
| 350 | } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) { |
| 351 | private_dirty = temp; |
| 352 | } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) { |
| 353 | referenced = temp; |
Grace Kloba | bd51116 | 2009-07-08 23:32:25 -0700 | [diff] [blame] | 354 | } else if (strlen(line) > 30 && line[8] == '-' && line[17] == ' ') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | // looks like a new mapping |
Grace Kloba | bd51116 | 2009-07-08 23:32:25 -0700 | [diff] [blame] | 356 | // example: "10000000-10001000 ---p 10000000 00:00 0" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (!skip) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 362 | if (is_swappable && (pss > 0)) { |
| 363 | sharing_proportion = 0.0; |
| 364 | if ((shared_clean > 0) || (shared_dirty > 0)) { |
| 365 | sharing_proportion = (pss - private_clean - private_dirty)/(shared_clean+shared_dirty); |
| 366 | } |
| 367 | swappable_pss = (sharing_proportion*shared_clean) + private_clean; |
| 368 | } else |
| 369 | swappable_pss = 0; |
| 370 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 371 | stats[whichHeap].pss += pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 372 | stats[whichHeap].swappablePss += swappable_pss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 373 | stats[whichHeap].privateDirty += private_dirty; |
| 374 | stats[whichHeap].sharedDirty += shared_dirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 375 | stats[whichHeap].privateClean += private_clean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 376 | stats[whichHeap].sharedClean += shared_clean; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 377 | if (whichHeap == HEAP_DALVIK || whichHeap == HEAP_DALVIK_OTHER) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 378 | stats[subHeap].pss += pss; |
| 379 | stats[subHeap].swappablePss += swappable_pss; |
| 380 | stats[subHeap].privateDirty += private_dirty; |
| 381 | stats[subHeap].sharedDirty += shared_dirty; |
| 382 | stats[subHeap].privateClean += private_clean; |
| 383 | stats[subHeap].sharedClean += shared_clean; |
| 384 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void load_maps(int pid, stats_t* stats) |
| 390 | { |
| 391 | char tmp[128]; |
| 392 | FILE *fp; |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 393 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | sprintf(tmp, "/proc/%d/smaps", pid); |
| 395 | fp = fopen(tmp, "r"); |
| 396 | if (fp == 0) return; |
| 397 | |
| 398 | read_mapinfo(fp, stats); |
| 399 | fclose(fp); |
| 400 | } |
| 401 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 402 | static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz, |
| 403 | jint pid, jobject object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 405 | stats_t stats[_NUM_HEAP]; |
| 406 | memset(&stats, 0, sizeof(stats)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 407 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 408 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 409 | load_maps(pid, stats); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 411 | jlong gpu = read_gpu_mem(pid); |
| 412 | stats[HEAP_GPU].pss += gpu; |
| 413 | stats[HEAP_GPU].privateDirty += gpu; |
| 414 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 415 | for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 416 | stats[HEAP_UNKNOWN].pss += stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 417 | stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 418 | stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty; |
| 419 | stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 420 | stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 421 | stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 425 | env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 426 | env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 427 | env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty); |
| 428 | env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 429 | env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 430 | env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 431 | } |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 432 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 433 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 434 | jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 435 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 436 | jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0); |
| 437 | if (otherArray == NULL) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | int j=0; |
| 442 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 443 | otherArray[j++] = stats[i].pss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 444 | otherArray[j++] = stats[i].swappablePss; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 445 | otherArray[j++] = stats[i].privateDirty; |
| 446 | otherArray[j++] = stats[i].sharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 447 | otherArray[j++] = stats[i].privateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 448 | otherArray[j++] = stats[i].sharedClean; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 454 | static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object) |
| 455 | { |
| 456 | android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); |
| 457 | } |
| 458 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 459 | static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, jlongArray outUss) |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 460 | { |
| 461 | char line[1024]; |
| 462 | jlong pss = 0; |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 463 | jlong uss = 0; |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 464 | unsigned temp; |
| 465 | |
| 466 | char tmp[128]; |
| 467 | FILE *fp; |
| 468 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 469 | pss = uss = read_gpu_mem(pid); |
| 470 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 471 | sprintf(tmp, "/proc/%d/smaps", pid); |
| 472 | fp = fopen(tmp, "r"); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 473 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 474 | if (fp != 0) { |
| 475 | while (true) { |
| 476 | if (fgets(line, 1024, fp) == NULL) { |
| 477 | break; |
| 478 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 479 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 480 | if (line[0] == 'P') { |
| 481 | if (strncmp(line, "Pss:", 4) == 0) { |
| 482 | char* c = line + 4; |
| 483 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 484 | c++; |
| 485 | } |
| 486 | pss += atoi(c); |
| 487 | } else if (strncmp(line, "Private_Clean:", 14) |
| 488 | || strncmp(line, "Private_Dirty:", 14)) { |
| 489 | char* c = line + 14; |
| 490 | while (*c != 0 && (*c < '0' || *c > '9')) { |
| 491 | c++; |
| 492 | } |
| 493 | uss += atoi(c); |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 494 | } |
Dianne Hackborn | cfc837f | 2013-06-27 18:32:07 -0700 | [diff] [blame] | 495 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 496 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 497 | |
Dianne Hackborn | 37c9943 | 2013-09-05 19:34:57 -0700 | [diff] [blame] | 498 | fclose(fp); |
| 499 | } |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 500 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 501 | if (outUss != NULL) { |
| 502 | if (env->GetArrayLength(outUss) >= 1) { |
| 503 | jlong* outUssArray = env->GetLongArrayElements(outUss, 0); |
| 504 | if (outUssArray != NULL) { |
| 505 | outUssArray[0] = uss; |
| 506 | } |
| 507 | env->ReleaseLongArrayElements(outUss, outUssArray, 0); |
| 508 | } |
| 509 | } |
| 510 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 511 | return pss; |
| 512 | } |
| 513 | |
| 514 | static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz) |
| 515 | { |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 516 | return android_os_Debug_getPssPid(env, clazz, getpid(), NULL); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 517 | } |
| 518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | static jint read_binder_stat(const char* stat) |
| 520 | { |
| 521 | FILE* fp = fopen(BINDER_STATS, "r"); |
| 522 | if (fp == NULL) { |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | char line[1024]; |
| 527 | |
| 528 | char compare[128]; |
| 529 | int len = snprintf(compare, 128, "proc %d", getpid()); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | // loop until we have the block that represents this process |
| 532 | do { |
| 533 | if (fgets(line, 1024, fp) == 0) { |
| 534 | return -1; |
| 535 | } |
| 536 | } while (strncmp(compare, line, len)); |
| 537 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 538 | // 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] | 539 | len = snprintf(compare, 128, " %s: ", stat); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | do { |
| 542 | if (fgets(line, 1024, fp) == 0) { |
| 543 | return -1; |
| 544 | } |
| 545 | } while (strncmp(compare, line, len)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | // we have the line, now increment the line ptr to the value |
| 548 | char* ptr = line + len; |
| 549 | return atoi(ptr); |
| 550 | } |
| 551 | |
| 552 | static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz) |
| 553 | { |
| 554 | return read_binder_stat("bcTRANSACTION"); |
| 555 | } |
| 556 | |
| 557 | static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz) |
| 558 | { |
| 559 | return read_binder_stat("brTRANSACTION"); |
| 560 | } |
| 561 | |
| 562 | // these are implemented in android_util_Binder.cpp |
| 563 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz); |
| 564 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz); |
| 565 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz); |
| 566 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 567 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 568 | /* pulled out of bionic */ |
| 569 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 570 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize); |
| 571 | extern "C" void free_malloc_leak_info(uint8_t* info); |
| 572 | #define SIZE_FLAG_ZYGOTE_CHILD (1<<31) |
| 573 | #define BACKTRACE_SIZE 32 |
| 574 | |
| 575 | /* |
| 576 | * This is a qsort() callback. |
| 577 | * |
| 578 | * See dumpNativeHeap() for comments about the data format and sort order. |
| 579 | */ |
| 580 | static int compareHeapRecords(const void* vrec1, const void* vrec2) |
| 581 | { |
| 582 | const size_t* rec1 = (const size_t*) vrec1; |
| 583 | const size_t* rec2 = (const size_t*) vrec2; |
| 584 | size_t size1 = *rec1; |
| 585 | size_t size2 = *rec2; |
| 586 | |
| 587 | if (size1 < size2) { |
| 588 | return 1; |
| 589 | } else if (size1 > size2) { |
| 590 | return -1; |
| 591 | } |
| 592 | |
| 593 | intptr_t* bt1 = (intptr_t*)(rec1 + 2); |
| 594 | intptr_t* bt2 = (intptr_t*)(rec2 + 2); |
| 595 | for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) { |
| 596 | intptr_t addr1 = bt1[idx]; |
| 597 | intptr_t addr2 = bt2[idx]; |
| 598 | if (addr1 == addr2) { |
| 599 | if (addr1 == 0) |
| 600 | break; |
| 601 | continue; |
| 602 | } |
| 603 | if (addr1 < addr2) { |
| 604 | return -1; |
| 605 | } else if (addr1 > addr2) { |
| 606 | return 1; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * The get_malloc_leak_info() call returns an array of structs that |
| 615 | * look like this: |
| 616 | * |
| 617 | * size_t size |
| 618 | * size_t allocations |
| 619 | * intptr_t backtrace[32] |
| 620 | * |
| 621 | * "size" is the size of the allocation, "backtrace" is a fixed-size |
| 622 | * array of function pointers, and "allocations" is the number of |
| 623 | * allocations with the exact same size and backtrace. |
| 624 | * |
| 625 | * The entries are sorted by descending total size (i.e. size*allocations) |
| 626 | * then allocation count. For best results with "diff" we'd like to sort |
| 627 | * primarily by individual size then stack trace. Since the entries are |
| 628 | * fixed-size, and we're allowed (by the current implementation) to mangle |
| 629 | * them, we can do this in place. |
| 630 | */ |
| 631 | static void dumpNativeHeap(FILE* fp) |
| 632 | { |
| 633 | uint8_t* info = NULL; |
| 634 | size_t overallSize, infoSize, totalMemory, backtraceSize; |
| 635 | |
| 636 | get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, |
| 637 | &backtraceSize); |
| 638 | if (info == NULL) { |
| 639 | fprintf(fp, "Native heap dump not available. To enable, run these" |
| 640 | " commands (requires root):\n"); |
| 641 | fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n"); |
| 642 | fprintf(fp, "$ adb shell stop\n"); |
| 643 | fprintf(fp, "$ adb shell start\n"); |
| 644 | return; |
| 645 | } |
| 646 | assert(infoSize != 0); |
| 647 | assert(overallSize % infoSize == 0); |
| 648 | |
| 649 | fprintf(fp, "Android Native Heap Dump v1.0\n\n"); |
| 650 | |
| 651 | size_t recordCount = overallSize / infoSize; |
| 652 | fprintf(fp, "Total memory: %zu\n", totalMemory); |
| 653 | fprintf(fp, "Allocation records: %zd\n", recordCount); |
| 654 | if (backtraceSize != BACKTRACE_SIZE) { |
| 655 | fprintf(fp, "WARNING: mismatched backtrace sizes (%d vs. %d)\n", |
| 656 | backtraceSize, BACKTRACE_SIZE); |
| 657 | } |
| 658 | fprintf(fp, "\n"); |
| 659 | |
| 660 | /* re-sort the entries */ |
| 661 | qsort(info, recordCount, infoSize, compareHeapRecords); |
| 662 | |
| 663 | /* dump the entries to the file */ |
| 664 | const uint8_t* ptr = info; |
| 665 | for (size_t idx = 0; idx < recordCount; idx++) { |
| 666 | size_t size = *(size_t*) ptr; |
| 667 | size_t allocations = *(size_t*) (ptr + sizeof(size_t)); |
| 668 | intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2); |
| 669 | |
| 670 | fprintf(fp, "z %d sz %8zu num %4zu bt", |
| 671 | (size & SIZE_FLAG_ZYGOTE_CHILD) != 0, |
| 672 | size & ~SIZE_FLAG_ZYGOTE_CHILD, |
| 673 | allocations); |
| 674 | for (size_t bt = 0; bt < backtraceSize; bt++) { |
| 675 | if (backtrace[bt] == 0) { |
| 676 | break; |
| 677 | } else { |
| 678 | fprintf(fp, " %08x", backtrace[bt]); |
| 679 | } |
| 680 | } |
| 681 | fprintf(fp, "\n"); |
| 682 | |
| 683 | ptr += infoSize; |
| 684 | } |
| 685 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 686 | free_malloc_leak_info(info); |
Brian Carlstrom | 393b84c1 | 2010-10-17 23:40:43 -0700 | [diff] [blame] | 687 | |
| 688 | fprintf(fp, "MAPS\n"); |
| 689 | const char* maps = "/proc/self/maps"; |
| 690 | FILE* in = fopen(maps, "r"); |
| 691 | if (in == NULL) { |
| 692 | fprintf(fp, "Could not open %s\n", maps); |
| 693 | return; |
| 694 | } |
| 695 | char buf[BUFSIZ]; |
| 696 | while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) { |
| 697 | fwrite(buf, sizeof(char), n, fp); |
| 698 | } |
| 699 | fclose(in); |
| 700 | |
| 701 | fprintf(fp, "END\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 702 | } |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 703 | |
| 704 | /* |
| 705 | * Dump the native heap, writing human-readable output to the specified |
| 706 | * file descriptor. |
| 707 | */ |
| 708 | static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz, |
| 709 | jobject fileDescriptor) |
| 710 | { |
| 711 | if (fileDescriptor == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 712 | jniThrowNullPointerException(env, "fd == null"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 716 | if (origFd < 0) { |
| 717 | jniThrowRuntimeException(env, "Invalid file descriptor"); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | /* dup() the descriptor so we don't close the original with fclose() */ |
| 722 | int fd = dup(origFd); |
| 723 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 724 | ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 725 | jniThrowRuntimeException(env, "dup() failed"); |
| 726 | return; |
| 727 | } |
| 728 | |
| 729 | FILE* fp = fdopen(fd, "w"); |
| 730 | if (fp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 731 | ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 732 | close(fd); |
| 733 | jniThrowRuntimeException(env, "fdopen() failed"); |
| 734 | return; |
| 735 | } |
| 736 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 737 | ALOGD("Native heap dump starting...\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 738 | dumpNativeHeap(fp); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 739 | ALOGD("Native heap dump complete.\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 740 | |
| 741 | fclose(fp); |
| 742 | } |
| 743 | |
| 744 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 745 | static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz, |
| 746 | jint pid, jstring fileName) |
| 747 | { |
| 748 | if (fileName == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 749 | jniThrowNullPointerException(env, "file == null"); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 750 | return; |
| 751 | } |
| 752 | const jchar* str = env->GetStringCritical(fileName, 0); |
| 753 | String8 fileName8; |
| 754 | if (str) { |
| 755 | fileName8 = String8(str, env->GetStringLength(fileName)); |
| 756 | env->ReleaseStringCritical(fileName, str); |
| 757 | } |
| 758 | |
| 759 | int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */ |
| 760 | if (fd < 0) { |
| 761 | fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno)); |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 766 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 767 | } else { |
| 768 | dump_backtrace_to_file(pid, fd); |
| 769 | } |
| 770 | |
| 771 | close(fd); |
| 772 | } |
| 773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | /* |
| 775 | * JNI registration. |
| 776 | */ |
| 777 | |
| 778 | static JNINativeMethod gMethods[] = { |
| 779 | { "getNativeHeapSize", "()J", |
| 780 | (void*) android_os_Debug_getNativeHeapSize }, |
| 781 | { "getNativeHeapAllocatedSize", "()J", |
| 782 | (void*) android_os_Debug_getNativeHeapAllocatedSize }, |
| 783 | { "getNativeHeapFreeSize", "()J", |
| 784 | (void*) android_os_Debug_getNativeHeapFreeSize }, |
| 785 | { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V", |
| 786 | (void*) android_os_Debug_getDirtyPages }, |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 787 | { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V", |
| 788 | (void*) android_os_Debug_getDirtyPagesPid }, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 789 | { "getPss", "()J", |
| 790 | (void*) android_os_Debug_getPss }, |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 791 | { "getPss", "(I[J)J", |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 792 | (void*) android_os_Debug_getPssPid }, |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 793 | { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", |
| 794 | (void*) android_os_Debug_dumpNativeHeap }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | { "getBinderSentTransactions", "()I", |
| 796 | (void*) android_os_Debug_getBinderSentTransactions }, |
| 797 | { "getBinderReceivedTransactions", "()I", |
| 798 | (void*) android_os_getBinderReceivedTransactions }, |
| 799 | { "getBinderLocalObjectCount", "()I", |
| 800 | (void*)android_os_Debug_getLocalObjectCount }, |
| 801 | { "getBinderProxyObjectCount", "()I", |
| 802 | (void*)android_os_Debug_getProxyObjectCount }, |
| 803 | { "getBinderDeathObjectCount", "()I", |
| 804 | (void*)android_os_Debug_getDeathObjectCount }, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 805 | { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V", |
| 806 | (void*)android_os_Debug_dumpNativeBacktraceToFile }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | }; |
| 808 | |
| 809 | int register_android_os_Debug(JNIEnv *env) |
| 810 | { |
| 811 | jclass clazz = env->FindClass("android/os/Debug$MemoryInfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 813 | // Sanity check the number of other statistics expected in Java matches here. |
| 814 | jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I"); |
| 815 | jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 816 | jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I"); |
| 817 | jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 818 | int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 819 | if ((numOtherStats + numDvkStats) != expectedNumOtherStats) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 820 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 821 | "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d", |
| 822 | numOtherStats+numDvkStats, expectedNumOtherStats); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 823 | return JNI_ERR; |
| 824 | } |
| 825 | |
| 826 | otherStats_field = env->GetFieldID(clazz, "otherStats", "[I"); |
| 827 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 828 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 829 | stat_fields[i].pss_field = |
| 830 | env->GetFieldID(clazz, stat_field_names[i].pss_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 831 | stat_fields[i].pssSwappable_field = |
| 832 | env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 833 | stat_fields[i].privateDirty_field = |
| 834 | env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I"); |
| 835 | stat_fields[i].sharedDirty_field = |
| 836 | env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I"); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 837 | stat_fields[i].privateClean_field = |
| 838 | env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I"); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 839 | stat_fields[i].sharedClean_field = |
| 840 | env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I"); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 841 | } |
| 842 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods)); |
| 844 | } |
| 845 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 846 | }; // namespace android |