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