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