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