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