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 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <time.h> |
| 30 | #include <sys/time.h> |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <assert.h> |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 33 | #include <ctype.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | #ifdef HAVE_MALLOC_H |
| 36 | #include <malloc.h> |
| 37 | #endif |
| 38 | |
| 39 | namespace android |
| 40 | { |
| 41 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 42 | enum { |
| 43 | HEAP_UNKNOWN, |
| 44 | HEAP_DALVIK, |
| 45 | HEAP_NATIVE, |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 46 | HEAP_STACK, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 47 | HEAP_CURSOR, |
| 48 | HEAP_ASHMEM, |
| 49 | HEAP_UNKNOWN_DEV, |
| 50 | HEAP_SO, |
| 51 | HEAP_JAR, |
| 52 | HEAP_APK, |
| 53 | HEAP_TTF, |
| 54 | HEAP_DEX, |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 55 | HEAP_OAT, |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame^] | 56 | HEAP_ART, |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 57 | HEAP_UNKNOWN_MAP, |
| 58 | |
| 59 | _NUM_HEAP, |
| 60 | _NUM_CORE_HEAP = HEAP_NATIVE+1 |
| 61 | }; |
| 62 | |
| 63 | struct stat_fields { |
| 64 | jfieldID pss_field; |
| 65 | jfieldID privateDirty_field; |
| 66 | jfieldID sharedDirty_field; |
| 67 | }; |
| 68 | |
| 69 | struct stat_field_names { |
| 70 | const char* pss_name; |
| 71 | const char* privateDirty_name; |
| 72 | const char* sharedDirty_name; |
| 73 | }; |
| 74 | |
| 75 | static stat_fields stat_fields[_NUM_CORE_HEAP]; |
| 76 | |
| 77 | static stat_field_names stat_field_names[_NUM_CORE_HEAP] = { |
| 78 | { "otherPss", "otherPrivateDirty", "otherSharedDirty" }, |
| 79 | { "dalvikPss", "dalvikPrivateDirty", "dalvikSharedDirty" }, |
| 80 | { "nativePss", "nativePrivateDirty", "nativeSharedDirty" } |
| 81 | }; |
| 82 | |
| 83 | jfieldID otherStats_field; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | struct stats_t { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 86 | int pss; |
| 87 | int privateDirty; |
| 88 | int sharedDirty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | #define BINDER_STATS "/proc/binder/stats" |
| 92 | |
| 93 | static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) |
| 94 | { |
| 95 | #ifdef HAVE_MALLOC_H |
| 96 | struct mallinfo info = mallinfo(); |
| 97 | return (jlong) info.usmblks; |
| 98 | #else |
| 99 | return -1; |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz) |
| 104 | { |
| 105 | #ifdef HAVE_MALLOC_H |
| 106 | struct mallinfo info = mallinfo(); |
| 107 | return (jlong) info.uordblks; |
| 108 | #else |
| 109 | return -1; |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz) |
| 114 | { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 115 | #ifdef HAVE_MALLOC_H |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | struct mallinfo info = mallinfo(); |
| 117 | return (jlong) info.fordblks; |
| 118 | #else |
| 119 | return -1; |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | static void read_mapinfo(FILE *fp, stats_t* stats) |
| 124 | { |
| 125 | char line[1024]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 126 | int len, nameLen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | bool skip, done = false; |
| 128 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 129 | unsigned size = 0, resident = 0, pss = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | unsigned shared_clean = 0, shared_dirty = 0; |
| 131 | unsigned private_clean = 0, private_dirty = 0; |
| 132 | unsigned referenced = 0; |
| 133 | unsigned temp; |
| 134 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 135 | unsigned long int start; |
| 136 | unsigned long int end = 0; |
| 137 | unsigned long int prevEnd = 0; |
| 138 | char* name; |
| 139 | int name_pos; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 141 | int whichHeap = HEAP_UNKNOWN; |
| 142 | int prevHeap = HEAP_UNKNOWN; |
| 143 | |
| 144 | if(fgets(line, sizeof(line), fp) == 0) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | |
| 146 | while (!done) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 147 | prevHeap = whichHeap; |
| 148 | prevEnd = end; |
| 149 | whichHeap = HEAP_UNKNOWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | skip = false; |
| 151 | |
| 152 | len = strlen(line); |
| 153 | if (len < 1) return; |
| 154 | line[--len] = 0; |
| 155 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 156 | if (sscanf(line, "%lx-%lx %*s %*x %*x:%*x %*d%n", &start, &end, &name_pos) != 2) { |
| 157 | skip = true; |
| 158 | } else { |
| 159 | while (isspace(line[name_pos])) { |
| 160 | name_pos += 1; |
| 161 | } |
| 162 | name = line + name_pos; |
| 163 | nameLen = strlen(name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
Ian Rogers | afc10e1 | 2013-02-22 09:40:58 -0800 | [diff] [blame] | 165 | if ((strstr(name, "[heap]") == name) || |
| 166 | (strstr(name, "/dev/ashmem/libc malloc") == name)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 167 | whichHeap = HEAP_NATIVE; |
| 168 | } else if (strstr(name, "/dev/ashmem/dalvik-") == name) { |
| 169 | whichHeap = HEAP_DALVIK; |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 170 | } else if (strstr(name, "[stack") == name) { |
| 171 | whichHeap = HEAP_STACK; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 172 | } else if (strstr(name, "/dev/ashmem/CursorWindow") == name) { |
| 173 | whichHeap = HEAP_CURSOR; |
| 174 | } else if (strstr(name, "/dev/ashmem/") == name) { |
| 175 | whichHeap = HEAP_ASHMEM; |
| 176 | } else if (strstr(name, "/dev/") == name) { |
| 177 | whichHeap = HEAP_UNKNOWN_DEV; |
| 178 | } else if (nameLen > 3 && strcmp(name+nameLen-3, ".so") == 0) { |
| 179 | whichHeap = HEAP_SO; |
| 180 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".jar") == 0) { |
| 181 | whichHeap = HEAP_JAR; |
| 182 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".apk") == 0) { |
| 183 | whichHeap = HEAP_APK; |
| 184 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".ttf") == 0) { |
| 185 | whichHeap = HEAP_TTF; |
Ian Rogers | 9f8589c | 2013-02-22 20:35:06 -0800 | [diff] [blame] | 186 | } else if ((nameLen > 4 && strcmp(name+nameLen-4, ".dex") == 0) || |
| 187 | (nameLen > 5 && strcmp(name+nameLen-5, ".odex") == 0)) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 188 | whichHeap = HEAP_DEX; |
Anwar Ghuloum | 8884ef4 | 2013-03-15 12:56:59 -0700 | [diff] [blame] | 189 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".oat") == 0) { |
| 190 | whichHeap = HEAP_OAT; |
Anwar Ghuloum | 88887d0 | 2013-03-19 15:30:12 -0700 | [diff] [blame^] | 191 | } else if (nameLen > 4 && strcmp(name+nameLen-4, ".art") == 0) { |
| 192 | whichHeap = HEAP_ART; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 193 | } else if (nameLen > 0) { |
| 194 | whichHeap = HEAP_UNKNOWN_MAP; |
| 195 | } else if (start == prevEnd && prevHeap == HEAP_SO) { |
| 196 | // bss section of a shared library. |
| 197 | whichHeap = HEAP_SO; |
| 198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 201 | //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] | 202 | // isSqliteHeap, line); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 203 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | while (true) { |
| 205 | if (fgets(line, 1024, fp) == 0) { |
| 206 | done = true; |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | if (sscanf(line, "Size: %d kB", &temp) == 1) { |
| 211 | size = temp; |
| 212 | } else if (sscanf(line, "Rss: %d kB", &temp) == 1) { |
| 213 | resident = temp; |
| 214 | } else if (sscanf(line, "Pss: %d kB", &temp) == 1) { |
| 215 | pss = temp; |
| 216 | } else if (sscanf(line, "Shared_Clean: %d kB", &temp) == 1) { |
| 217 | shared_clean = temp; |
| 218 | } else if (sscanf(line, "Shared_Dirty: %d kB", &temp) == 1) { |
| 219 | shared_dirty = temp; |
| 220 | } else if (sscanf(line, "Private_Clean: %d kB", &temp) == 1) { |
| 221 | private_clean = temp; |
| 222 | } else if (sscanf(line, "Private_Dirty: %d kB", &temp) == 1) { |
| 223 | private_dirty = temp; |
| 224 | } else if (sscanf(line, "Referenced: %d kB", &temp) == 1) { |
| 225 | referenced = temp; |
Grace Kloba | bd51116 | 2009-07-08 23:32:25 -0700 | [diff] [blame] | 226 | } else if (strlen(line) > 30 && line[8] == '-' && line[17] == ' ') { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | // looks like a new mapping |
Grace Kloba | bd51116 | 2009-07-08 23:32:25 -0700 | [diff] [blame] | 228 | // example: "10000000-10001000 ---p 10000000 00:00 0" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (!skip) { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 234 | stats[whichHeap].pss += pss; |
| 235 | stats[whichHeap].privateDirty += private_dirty; |
| 236 | stats[whichHeap].sharedDirty += shared_dirty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void load_maps(int pid, stats_t* stats) |
| 242 | { |
| 243 | char tmp[128]; |
| 244 | FILE *fp; |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | sprintf(tmp, "/proc/%d/smaps", pid); |
| 247 | fp = fopen(tmp, "r"); |
| 248 | if (fp == 0) return; |
| 249 | |
| 250 | read_mapinfo(fp, stats); |
| 251 | fclose(fp); |
| 252 | } |
| 253 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 254 | static void android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz, |
| 255 | jint pid, jobject object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | { |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 257 | stats_t stats[_NUM_HEAP]; |
| 258 | memset(&stats, 0, sizeof(stats)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 259 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 260 | load_maps(pid, stats); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 262 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 263 | stats[HEAP_UNKNOWN].pss += stats[i].pss; |
| 264 | stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty; |
| 265 | stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty; |
| 266 | } |
| 267 | |
| 268 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 269 | env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss); |
| 270 | env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty); |
| 271 | env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty); |
| 272 | } |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 273 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 274 | jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 275 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 276 | jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0); |
| 277 | if (otherArray == NULL) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | int j=0; |
| 282 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 283 | otherArray[j++] = stats[i].pss; |
| 284 | otherArray[j++] = stats[i].privateDirty; |
| 285 | otherArray[j++] = stats[i].sharedDirty; |
| 286 | } |
| 287 | |
| 288 | env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 291 | static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object) |
| 292 | { |
| 293 | android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); |
| 294 | } |
| 295 | |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 296 | static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid) |
| 297 | { |
| 298 | char line[1024]; |
| 299 | jlong pss = 0; |
| 300 | unsigned temp; |
| 301 | |
| 302 | char tmp[128]; |
| 303 | FILE *fp; |
| 304 | |
| 305 | sprintf(tmp, "/proc/%d/smaps", pid); |
| 306 | fp = fopen(tmp, "r"); |
| 307 | if (fp == 0) return 0; |
| 308 | |
| 309 | while (true) { |
| 310 | if (fgets(line, 1024, fp) == 0) { |
| 311 | break; |
| 312 | } |
| 313 | |
| 314 | if (sscanf(line, "Pss: %d kB", &temp) == 1) { |
| 315 | pss += temp; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | fclose(fp); |
| 320 | |
| 321 | return pss; |
| 322 | } |
| 323 | |
| 324 | static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz) |
| 325 | { |
| 326 | return android_os_Debug_getPssPid(env, clazz, getpid()); |
| 327 | } |
| 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | static jint read_binder_stat(const char* stat) |
| 330 | { |
| 331 | FILE* fp = fopen(BINDER_STATS, "r"); |
| 332 | if (fp == NULL) { |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | char line[1024]; |
| 337 | |
| 338 | char compare[128]; |
| 339 | int len = snprintf(compare, 128, "proc %d", getpid()); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 340 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | // loop until we have the block that represents this process |
| 342 | do { |
| 343 | if (fgets(line, 1024, fp) == 0) { |
| 344 | return -1; |
| 345 | } |
| 346 | } while (strncmp(compare, line, len)); |
| 347 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 348 | // 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] | 349 | len = snprintf(compare, 128, " %s: ", stat); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | do { |
| 352 | if (fgets(line, 1024, fp) == 0) { |
| 353 | return -1; |
| 354 | } |
| 355 | } while (strncmp(compare, line, len)); |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 356 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | // we have the line, now increment the line ptr to the value |
| 358 | char* ptr = line + len; |
| 359 | return atoi(ptr); |
| 360 | } |
| 361 | |
| 362 | static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz) |
| 363 | { |
| 364 | return read_binder_stat("bcTRANSACTION"); |
| 365 | } |
| 366 | |
| 367 | static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz) |
| 368 | { |
| 369 | return read_binder_stat("brTRANSACTION"); |
| 370 | } |
| 371 | |
| 372 | // these are implemented in android_util_Binder.cpp |
| 373 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz); |
| 374 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz); |
| 375 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz); |
| 376 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 377 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 378 | /* pulled out of bionic */ |
| 379 | extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize, |
| 380 | size_t* infoSize, size_t* totalMemory, size_t* backtraceSize); |
| 381 | extern "C" void free_malloc_leak_info(uint8_t* info); |
| 382 | #define SIZE_FLAG_ZYGOTE_CHILD (1<<31) |
| 383 | #define BACKTRACE_SIZE 32 |
| 384 | |
| 385 | /* |
| 386 | * This is a qsort() callback. |
| 387 | * |
| 388 | * See dumpNativeHeap() for comments about the data format and sort order. |
| 389 | */ |
| 390 | static int compareHeapRecords(const void* vrec1, const void* vrec2) |
| 391 | { |
| 392 | const size_t* rec1 = (const size_t*) vrec1; |
| 393 | const size_t* rec2 = (const size_t*) vrec2; |
| 394 | size_t size1 = *rec1; |
| 395 | size_t size2 = *rec2; |
| 396 | |
| 397 | if (size1 < size2) { |
| 398 | return 1; |
| 399 | } else if (size1 > size2) { |
| 400 | return -1; |
| 401 | } |
| 402 | |
| 403 | intptr_t* bt1 = (intptr_t*)(rec1 + 2); |
| 404 | intptr_t* bt2 = (intptr_t*)(rec2 + 2); |
| 405 | for (size_t idx = 0; idx < BACKTRACE_SIZE; idx++) { |
| 406 | intptr_t addr1 = bt1[idx]; |
| 407 | intptr_t addr2 = bt2[idx]; |
| 408 | if (addr1 == addr2) { |
| 409 | if (addr1 == 0) |
| 410 | break; |
| 411 | continue; |
| 412 | } |
| 413 | if (addr1 < addr2) { |
| 414 | return -1; |
| 415 | } else if (addr1 > addr2) { |
| 416 | return 1; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * The get_malloc_leak_info() call returns an array of structs that |
| 425 | * look like this: |
| 426 | * |
| 427 | * size_t size |
| 428 | * size_t allocations |
| 429 | * intptr_t backtrace[32] |
| 430 | * |
| 431 | * "size" is the size of the allocation, "backtrace" is a fixed-size |
| 432 | * array of function pointers, and "allocations" is the number of |
| 433 | * allocations with the exact same size and backtrace. |
| 434 | * |
| 435 | * The entries are sorted by descending total size (i.e. size*allocations) |
| 436 | * then allocation count. For best results with "diff" we'd like to sort |
| 437 | * primarily by individual size then stack trace. Since the entries are |
| 438 | * fixed-size, and we're allowed (by the current implementation) to mangle |
| 439 | * them, we can do this in place. |
| 440 | */ |
| 441 | static void dumpNativeHeap(FILE* fp) |
| 442 | { |
| 443 | uint8_t* info = NULL; |
| 444 | size_t overallSize, infoSize, totalMemory, backtraceSize; |
| 445 | |
| 446 | get_malloc_leak_info(&info, &overallSize, &infoSize, &totalMemory, |
| 447 | &backtraceSize); |
| 448 | if (info == NULL) { |
| 449 | fprintf(fp, "Native heap dump not available. To enable, run these" |
| 450 | " commands (requires root):\n"); |
| 451 | fprintf(fp, "$ adb shell setprop libc.debug.malloc 1\n"); |
| 452 | fprintf(fp, "$ adb shell stop\n"); |
| 453 | fprintf(fp, "$ adb shell start\n"); |
| 454 | return; |
| 455 | } |
| 456 | assert(infoSize != 0); |
| 457 | assert(overallSize % infoSize == 0); |
| 458 | |
| 459 | fprintf(fp, "Android Native Heap Dump v1.0\n\n"); |
| 460 | |
| 461 | size_t recordCount = overallSize / infoSize; |
| 462 | fprintf(fp, "Total memory: %zu\n", totalMemory); |
| 463 | fprintf(fp, "Allocation records: %zd\n", recordCount); |
| 464 | if (backtraceSize != BACKTRACE_SIZE) { |
| 465 | fprintf(fp, "WARNING: mismatched backtrace sizes (%d vs. %d)\n", |
| 466 | backtraceSize, BACKTRACE_SIZE); |
| 467 | } |
| 468 | fprintf(fp, "\n"); |
| 469 | |
| 470 | /* re-sort the entries */ |
| 471 | qsort(info, recordCount, infoSize, compareHeapRecords); |
| 472 | |
| 473 | /* dump the entries to the file */ |
| 474 | const uint8_t* ptr = info; |
| 475 | for (size_t idx = 0; idx < recordCount; idx++) { |
| 476 | size_t size = *(size_t*) ptr; |
| 477 | size_t allocations = *(size_t*) (ptr + sizeof(size_t)); |
| 478 | intptr_t* backtrace = (intptr_t*) (ptr + sizeof(size_t) * 2); |
| 479 | |
| 480 | fprintf(fp, "z %d sz %8zu num %4zu bt", |
| 481 | (size & SIZE_FLAG_ZYGOTE_CHILD) != 0, |
| 482 | size & ~SIZE_FLAG_ZYGOTE_CHILD, |
| 483 | allocations); |
| 484 | for (size_t bt = 0; bt < backtraceSize; bt++) { |
| 485 | if (backtrace[bt] == 0) { |
| 486 | break; |
| 487 | } else { |
| 488 | fprintf(fp, " %08x", backtrace[bt]); |
| 489 | } |
| 490 | } |
| 491 | fprintf(fp, "\n"); |
| 492 | |
| 493 | ptr += infoSize; |
| 494 | } |
| 495 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 496 | free_malloc_leak_info(info); |
Brian Carlstrom | 393b84c1 | 2010-10-17 23:40:43 -0700 | [diff] [blame] | 497 | |
| 498 | fprintf(fp, "MAPS\n"); |
| 499 | const char* maps = "/proc/self/maps"; |
| 500 | FILE* in = fopen(maps, "r"); |
| 501 | if (in == NULL) { |
| 502 | fprintf(fp, "Could not open %s\n", maps); |
| 503 | return; |
| 504 | } |
| 505 | char buf[BUFSIZ]; |
| 506 | while (size_t n = fread(buf, sizeof(char), BUFSIZ, in)) { |
| 507 | fwrite(buf, sizeof(char), n, fp); |
| 508 | } |
| 509 | fclose(in); |
| 510 | |
| 511 | fprintf(fp, "END\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 512 | } |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 513 | |
| 514 | /* |
| 515 | * Dump the native heap, writing human-readable output to the specified |
| 516 | * file descriptor. |
| 517 | */ |
| 518 | static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject clazz, |
| 519 | jobject fileDescriptor) |
| 520 | { |
| 521 | if (fileDescriptor == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 522 | jniThrowNullPointerException(env, "fd == null"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 526 | if (origFd < 0) { |
| 527 | jniThrowRuntimeException(env, "Invalid file descriptor"); |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | /* dup() the descriptor so we don't close the original with fclose() */ |
| 532 | int fd = dup(origFd); |
| 533 | if (fd < 0) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 534 | ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 535 | jniThrowRuntimeException(env, "dup() failed"); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | FILE* fp = fdopen(fd, "w"); |
| 540 | if (fp == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 541 | ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno)); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 542 | close(fd); |
| 543 | jniThrowRuntimeException(env, "fdopen() failed"); |
| 544 | return; |
| 545 | } |
| 546 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 547 | ALOGD("Native heap dump starting...\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 548 | dumpNativeHeap(fp); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 549 | ALOGD("Native heap dump complete.\n"); |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 550 | |
| 551 | fclose(fp); |
| 552 | } |
| 553 | |
| 554 | |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 555 | static void android_os_Debug_dumpNativeBacktraceToFile(JNIEnv* env, jobject clazz, |
| 556 | jint pid, jstring fileName) |
| 557 | { |
| 558 | if (fileName == NULL) { |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 559 | jniThrowNullPointerException(env, "file == null"); |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | const jchar* str = env->GetStringCritical(fileName, 0); |
| 563 | String8 fileName8; |
| 564 | if (str) { |
| 565 | fileName8 = String8(str, env->GetStringLength(fileName)); |
| 566 | env->ReleaseStringCritical(fileName, str); |
| 567 | } |
| 568 | |
| 569 | int fd = open(fileName8.string(), O_CREAT | O_WRONLY | O_NOFOLLOW, 0666); /* -rw-rw-rw- */ |
| 570 | if (fd < 0) { |
| 571 | fprintf(stderr, "Can't open %s: %s\n", fileName8.string(), strerror(errno)); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | if (lseek(fd, 0, SEEK_END) < 0) { |
| 576 | fprintf(stderr, "lseek: %s\n", strerror(errno)); |
| 577 | } else { |
| 578 | dump_backtrace_to_file(pid, fd); |
| 579 | } |
| 580 | |
| 581 | close(fd); |
| 582 | } |
| 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | /* |
| 585 | * JNI registration. |
| 586 | */ |
| 587 | |
| 588 | static JNINativeMethod gMethods[] = { |
| 589 | { "getNativeHeapSize", "()J", |
| 590 | (void*) android_os_Debug_getNativeHeapSize }, |
| 591 | { "getNativeHeapAllocatedSize", "()J", |
| 592 | (void*) android_os_Debug_getNativeHeapAllocatedSize }, |
| 593 | { "getNativeHeapFreeSize", "()J", |
| 594 | (void*) android_os_Debug_getNativeHeapFreeSize }, |
| 595 | { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V", |
| 596 | (void*) android_os_Debug_getDirtyPages }, |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 597 | { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)V", |
| 598 | (void*) android_os_Debug_getDirtyPagesPid }, |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 599 | { "getPss", "()J", |
| 600 | (void*) android_os_Debug_getPss }, |
| 601 | { "getPss", "(I)J", |
| 602 | (void*) android_os_Debug_getPssPid }, |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 603 | { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", |
| 604 | (void*) android_os_Debug_dumpNativeHeap }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | { "getBinderSentTransactions", "()I", |
| 606 | (void*) android_os_Debug_getBinderSentTransactions }, |
| 607 | { "getBinderReceivedTransactions", "()I", |
| 608 | (void*) android_os_getBinderReceivedTransactions }, |
| 609 | { "getBinderLocalObjectCount", "()I", |
| 610 | (void*)android_os_Debug_getLocalObjectCount }, |
| 611 | { "getBinderProxyObjectCount", "()I", |
| 612 | (void*)android_os_Debug_getProxyObjectCount }, |
| 613 | { "getBinderDeathObjectCount", "()I", |
| 614 | (void*)android_os_Debug_getDeathObjectCount }, |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 615 | { "dumpNativeBacktraceToFile", "(ILjava/lang/String;)V", |
| 616 | (void*)android_os_Debug_dumpNativeBacktraceToFile }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | int register_android_os_Debug(JNIEnv *env) |
| 620 | { |
| 621 | jclass clazz = env->FindClass("android/os/Debug$MemoryInfo"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | |
Ian Rogers | 7c9f30b | 2013-02-27 10:57:13 -0800 | [diff] [blame] | 623 | // Sanity check the number of other statistics expected in Java matches here. |
| 624 | jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I"); |
| 625 | jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field); |
| 626 | int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP; |
| 627 | if (numOtherStats != expectedNumOtherStats) { |
| 628 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
| 629 | "android.os.Debug.Meminfo.NUM_OTHER_STATS=%d expected %d", |
| 630 | numOtherStats, expectedNumOtherStats); |
| 631 | return JNI_ERR; |
| 632 | } |
| 633 | |
| 634 | otherStats_field = env->GetFieldID(clazz, "otherStats", "[I"); |
| 635 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 636 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 637 | stat_fields[i].pss_field = |
| 638 | env->GetFieldID(clazz, stat_field_names[i].pss_name, "I"); |
| 639 | stat_fields[i].privateDirty_field = |
| 640 | env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I"); |
| 641 | stat_fields[i].sharedDirty_field = |
| 642 | env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I"); |
| 643 | } |
| 644 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods)); |
| 646 | } |
| 647 | |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 648 | }; // namespace android |