| 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 | |
| 17 | #define LOG_TAG "android.os.Debug" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <malloc.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <time.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <iomanip> |
| 33 | #include <string> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <android-base/logging.h> |
| 37 | #include <android-base/properties.h> |
| 38 | #include <bionic/malloc.h> |
| 39 | #include <debuggerd/client.h> |
| 40 | #include <log/log.h> |
| 41 | #include <utils/misc.h> |
| 42 | #include <utils/String8.h> |
| 43 | |
| 44 | #include <nativehelper/JNIPlatformHelp.h> |
| 45 | #include <nativehelper/ScopedUtfChars.h> |
| 46 | #include "jni.h" |
| 47 | #include <dmabufinfo/dmabuf_sysfs_stats.h> |
| 48 | #include <dmabufinfo/dmabufinfo.h> |
| 49 | #include <meminfo/procmeminfo.h> |
| 50 | #include <meminfo/sysmeminfo.h> |
| 51 | #include <memtrack/memtrack.h> |
| 52 | #include <memunreachable/memunreachable.h> |
| 53 | #include <android-base/strings.h> |
| 54 | #include "android_os_Debug.h" |
| 55 | #include <vintf/VintfObject.h> |
| 56 | |
| 57 | namespace android |
| 58 | { |
| 59 | |
| 60 | enum { |
| 61 | HEAP_UNKNOWN, |
| 62 | HEAP_DALVIK, |
| 63 | HEAP_NATIVE, |
| 64 | |
| 65 | HEAP_DALVIK_OTHER, |
| 66 | HEAP_STACK, |
| 67 | HEAP_CURSOR, |
| 68 | HEAP_ASHMEM, |
| 69 | HEAP_GL_DEV, |
| 70 | HEAP_UNKNOWN_DEV, |
| 71 | HEAP_SO, |
| 72 | HEAP_JAR, |
| 73 | HEAP_APK, |
| 74 | HEAP_TTF, |
| 75 | HEAP_DEX, |
| 76 | HEAP_OAT, |
| 77 | HEAP_ART, |
| 78 | HEAP_UNKNOWN_MAP, |
| 79 | HEAP_GRAPHICS, |
| 80 | HEAP_GL, |
| 81 | HEAP_OTHER_MEMTRACK, |
| 82 | |
| 83 | // Dalvik extra sections (heap). |
| 84 | HEAP_DALVIK_NORMAL, |
| 85 | HEAP_DALVIK_LARGE, |
| 86 | HEAP_DALVIK_ZYGOTE, |
| 87 | HEAP_DALVIK_NON_MOVING, |
| 88 | |
| 89 | // Dalvik other extra sections. |
| 90 | HEAP_DALVIK_OTHER_LINEARALLOC, |
| 91 | HEAP_DALVIK_OTHER_ACCOUNTING, |
| 92 | HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE, |
| 93 | HEAP_DALVIK_OTHER_APP_CODE_CACHE, |
| 94 | HEAP_DALVIK_OTHER_COMPILER_METADATA, |
| 95 | HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE, |
| 96 | |
| 97 | // Boot vdex / app dex / app vdex |
| 98 | HEAP_DEX_BOOT_VDEX, |
| 99 | HEAP_DEX_APP_DEX, |
| 100 | HEAP_DEX_APP_VDEX, |
| 101 | |
| 102 | // App art, boot art. |
| 103 | HEAP_ART_APP, |
| 104 | HEAP_ART_BOOT, |
| 105 | |
| 106 | _NUM_HEAP, |
| 107 | _NUM_EXCLUSIVE_HEAP = HEAP_OTHER_MEMTRACK+1, |
| 108 | _NUM_CORE_HEAP = HEAP_NATIVE+1 |
| 109 | }; |
| 110 | |
| 111 | struct stat_fields { |
| 112 | jfieldID pss_field; |
| 113 | jfieldID pssSwappable_field; |
| 114 | jfieldID rss_field; |
| 115 | jfieldID privateDirty_field; |
| 116 | jfieldID sharedDirty_field; |
| 117 | jfieldID privateClean_field; |
| 118 | jfieldID sharedClean_field; |
| 119 | jfieldID swappedOut_field; |
| 120 | jfieldID swappedOutPss_field; |
| 121 | }; |
| 122 | |
| 123 | struct stat_field_names { |
| 124 | const char* pss_name; |
| 125 | const char* pssSwappable_name; |
| 126 | const char* rss_name; |
| 127 | const char* privateDirty_name; |
| 128 | const char* sharedDirty_name; |
| 129 | const char* privateClean_name; |
| 130 | const char* sharedClean_name; |
| 131 | const char* swappedOut_name; |
| 132 | const char* swappedOutPss_name; |
| 133 | }; |
| 134 | |
| 135 | static stat_fields stat_fields[_NUM_CORE_HEAP]; |
| 136 | |
| 137 | static stat_field_names stat_field_names[_NUM_CORE_HEAP] = { |
| 138 | { "otherPss", "otherSwappablePss", "otherRss", "otherPrivateDirty", "otherSharedDirty", |
| 139 | "otherPrivateClean", "otherSharedClean", "otherSwappedOut", "otherSwappedOutPss" }, |
| 140 | { "dalvikPss", "dalvikSwappablePss", "dalvikRss", "dalvikPrivateDirty", "dalvikSharedDirty", |
| 141 | "dalvikPrivateClean", "dalvikSharedClean", "dalvikSwappedOut", "dalvikSwappedOutPss" }, |
| 142 | { "nativePss", "nativeSwappablePss", "nativeRss", "nativePrivateDirty", "nativeSharedDirty", |
| 143 | "nativePrivateClean", "nativeSharedClean", "nativeSwappedOut", "nativeSwappedOutPss" } |
| 144 | }; |
| 145 | |
| 146 | static jfieldID otherStats_field; |
| 147 | static jfieldID hasSwappedOutPss_field; |
| 148 | |
| 149 | struct stats_t { |
| 150 | int pss; |
| 151 | int swappablePss; |
| 152 | int rss; |
| 153 | int privateDirty; |
| 154 | int sharedDirty; |
| 155 | int privateClean; |
| 156 | int sharedClean; |
| 157 | int swappedOut; |
| 158 | int swappedOutPss; |
| 159 | }; |
| 160 | |
| 161 | #define BINDER_STATS "/proc/binder/stats" |
| 162 | |
| 163 | static jlong android_os_Debug_getNativeHeapSize(JNIEnv *env, jobject clazz) |
| 164 | { |
| 165 | struct mallinfo info = mallinfo(); |
| 166 | return (jlong) info.usmblks; |
| 167 | } |
| 168 | |
| 169 | static jlong android_os_Debug_getNativeHeapAllocatedSize(JNIEnv *env, jobject clazz) |
| 170 | { |
| 171 | struct mallinfo info = mallinfo(); |
| 172 | return (jlong) info.uordblks; |
| 173 | } |
| 174 | |
| 175 | static jlong android_os_Debug_getNativeHeapFreeSize(JNIEnv *env, jobject clazz) |
| 176 | { |
| 177 | struct mallinfo info = mallinfo(); |
| 178 | return (jlong) info.fordblks; |
| 179 | } |
| 180 | |
| 181 | // Container used to retrieve graphics memory pss |
| 182 | struct graphics_memory_pss |
| 183 | { |
| 184 | int graphics; |
| 185 | int gl; |
| 186 | int other; |
| 187 | }; |
| 188 | |
| 189 | /* |
| 190 | * Uses libmemtrack to retrieve graphics memory that the process is using. |
| 191 | * Any graphics memory reported in /proc/pid/smaps is not included here. |
| 192 | */ |
| 193 | static int read_memtrack_memory(struct memtrack_proc* p, int pid, |
| 194 | struct graphics_memory_pss* graphics_mem) |
| 195 | { |
| 196 | int err = memtrack_proc_get(p, pid); |
| 197 | if (err != 0) { |
| 198 | // The memtrack HAL may not be available, do not log to avoid flooding |
| 199 | // logcat. |
| 200 | return err; |
| 201 | } |
| 202 | |
| 203 | ssize_t pss = memtrack_proc_graphics_pss(p); |
| 204 | if (pss < 0) { |
| 205 | ALOGW("failed to get graphics pss: %zd", pss); |
| 206 | return pss; |
| 207 | } |
| 208 | graphics_mem->graphics = pss / 1024; |
| 209 | |
| 210 | pss = memtrack_proc_gl_pss(p); |
| 211 | if (pss < 0) { |
| 212 | ALOGW("failed to get gl pss: %zd", pss); |
| 213 | return pss; |
| 214 | } |
| 215 | graphics_mem->gl = pss / 1024; |
| 216 | |
| 217 | pss = memtrack_proc_other_pss(p); |
| 218 | if (pss < 0) { |
| 219 | ALOGW("failed to get other pss: %zd", pss); |
| 220 | return pss; |
| 221 | } |
| 222 | graphics_mem->other = pss / 1024; |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Retrieves the graphics memory that is unaccounted for in /proc/pid/smaps. |
| 229 | */ |
| 230 | static int read_memtrack_memory(int pid, struct graphics_memory_pss* graphics_mem) |
| 231 | { |
| 232 | struct memtrack_proc* p = memtrack_proc_new(); |
| 233 | if (p == NULL) { |
| 234 | ALOGW("failed to create memtrack_proc"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | int err = read_memtrack_memory(p, pid, graphics_mem); |
| 239 | memtrack_proc_destroy(p); |
| 240 | return err; |
| 241 | } |
| 242 | |
| 243 | static bool load_maps(int pid, stats_t* stats, bool* foundSwapPss) |
| 244 | { |
| 245 | *foundSwapPss = false; |
| 246 | uint64_t prev_end = 0; |
| 247 | int prev_heap = HEAP_UNKNOWN; |
| 248 | |
| 249 | std::string smaps_path = base::StringPrintf("/proc/%d/smaps", pid); |
| 250 | auto vma_scan = [&](const meminfo::Vma& vma) { |
| 251 | int which_heap = HEAP_UNKNOWN; |
| 252 | int sub_heap = HEAP_UNKNOWN; |
| 253 | bool is_swappable = false; |
| 254 | std::string name; |
| 255 | if (base::EndsWith(vma.name, " (deleted)")) { |
| 256 | name = vma.name.substr(0, vma.name.size() - strlen(" (deleted)")); |
| 257 | } else { |
| 258 | name = vma.name; |
| 259 | } |
| 260 | |
| 261 | uint32_t namesz = name.size(); |
| 262 | if (base::StartsWith(name, "[heap]")) { |
| 263 | which_heap = HEAP_NATIVE; |
| 264 | } else if (base::StartsWith(name, "[anon:libc_malloc]")) { |
| 265 | which_heap = HEAP_NATIVE; |
| 266 | } else if (base::StartsWith(name, "[anon:scudo:")) { |
| 267 | which_heap = HEAP_NATIVE; |
| 268 | } else if (base::StartsWith(name, "[anon:GWP-ASan")) { |
| 269 | which_heap = HEAP_NATIVE; |
| 270 | } else if (base::StartsWith(name, "[stack")) { |
| 271 | which_heap = HEAP_STACK; |
| 272 | } else if (base::StartsWith(name, "[anon:stack_and_tls:")) { |
| 273 | which_heap = HEAP_STACK; |
| 274 | } else if (base::EndsWith(name, ".so")) { |
| 275 | which_heap = HEAP_SO; |
| 276 | is_swappable = true; |
| 277 | } else if (base::EndsWith(name, ".jar")) { |
| 278 | which_heap = HEAP_JAR; |
| 279 | is_swappable = true; |
| 280 | } else if (base::EndsWith(name, ".apk")) { |
| 281 | which_heap = HEAP_APK; |
| 282 | is_swappable = true; |
| 283 | } else if (base::EndsWith(name, ".ttf")) { |
| 284 | which_heap = HEAP_TTF; |
| 285 | is_swappable = true; |
| 286 | } else if ((base::EndsWith(name, ".odex")) || |
| 287 | (namesz > 4 && strstr(name.c_str(), ".dex") != nullptr)) { |
| 288 | which_heap = HEAP_DEX; |
| 289 | sub_heap = HEAP_DEX_APP_DEX; |
| 290 | is_swappable = true; |
| 291 | } else if (base::EndsWith(name, ".vdex")) { |
| 292 | which_heap = HEAP_DEX; |
| 293 | // Handle system@framework@boot and system/framework/boot|apex |
| 294 | if ((strstr(name.c_str(), "@boot") != nullptr) || |
| 295 | (strstr(name.c_str(), "/boot") != nullptr) || |
| 296 | (strstr(name.c_str(), "/apex") != nullptr)) { |
| 297 | sub_heap = HEAP_DEX_BOOT_VDEX; |
| 298 | } else { |
| 299 | sub_heap = HEAP_DEX_APP_VDEX; |
| 300 | } |
| 301 | is_swappable = true; |
| 302 | } else if (base::EndsWith(name, ".oat")) { |
| 303 | which_heap = HEAP_OAT; |
| 304 | is_swappable = true; |
| 305 | } else if (base::EndsWith(name, ".art") || base::EndsWith(name, ".art]")) { |
| 306 | which_heap = HEAP_ART; |
| 307 | // Handle system@framework@boot* and system/framework/boot|apex* |
| 308 | if ((strstr(name.c_str(), "@boot") != nullptr) || |
| 309 | (strstr(name.c_str(), "/boot") != nullptr) || |
| 310 | (strstr(name.c_str(), "/apex") != nullptr)) { |
| 311 | sub_heap = HEAP_ART_BOOT; |
| 312 | } else { |
| 313 | sub_heap = HEAP_ART_APP; |
| 314 | } |
| 315 | is_swappable = true; |
| 316 | } else if (base::StartsWith(name, "/dev/")) { |
| 317 | which_heap = HEAP_UNKNOWN_DEV; |
| 318 | if (base::StartsWith(name, "/dev/kgsl-3d0")) { |
| 319 | which_heap = HEAP_GL_DEV; |
| 320 | } else if (base::StartsWith(name, "/dev/ashmem/CursorWindow")) { |
| 321 | which_heap = HEAP_CURSOR; |
| 322 | } else if (base::StartsWith(name, "/dev/ashmem/jit-zygote-cache")) { |
| 323 | which_heap = HEAP_DALVIK_OTHER; |
| 324 | sub_heap = HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE; |
| 325 | } else if (base::StartsWith(name, "/dev/ashmem")) { |
| 326 | which_heap = HEAP_ASHMEM; |
| 327 | } |
| 328 | } else if (base::StartsWith(name, "/memfd:jit-cache")) { |
| 329 | which_heap = HEAP_DALVIK_OTHER; |
| 330 | sub_heap = HEAP_DALVIK_OTHER_APP_CODE_CACHE; |
| 331 | } else if (base::StartsWith(name, "/memfd:jit-zygote-cache")) { |
| 332 | which_heap = HEAP_DALVIK_OTHER; |
| 333 | sub_heap = HEAP_DALVIK_OTHER_ZYGOTE_CODE_CACHE; |
| 334 | } else if (base::StartsWith(name, "[anon:")) { |
| 335 | which_heap = HEAP_UNKNOWN; |
| 336 | if (base::StartsWith(name, "[anon:dalvik-")) { |
| 337 | which_heap = HEAP_DALVIK_OTHER; |
| 338 | if (base::StartsWith(name, "[anon:dalvik-LinearAlloc")) { |
| 339 | sub_heap = HEAP_DALVIK_OTHER_LINEARALLOC; |
| 340 | } else if (base::StartsWith(name, "[anon:dalvik-alloc space") || |
| 341 | base::StartsWith(name, "[anon:dalvik-main space")) { |
| 342 | // This is the regular Dalvik heap. |
| 343 | which_heap = HEAP_DALVIK; |
| 344 | sub_heap = HEAP_DALVIK_NORMAL; |
| 345 | } else if (base::StartsWith(name, |
| 346 | "[anon:dalvik-large object space") || |
| 347 | base::StartsWith( |
| 348 | name, "[anon:dalvik-free list large object space")) { |
| 349 | which_heap = HEAP_DALVIK; |
| 350 | sub_heap = HEAP_DALVIK_LARGE; |
| 351 | } else if (base::StartsWith(name, "[anon:dalvik-non moving space")) { |
| 352 | which_heap = HEAP_DALVIK; |
| 353 | sub_heap = HEAP_DALVIK_NON_MOVING; |
| 354 | } else if (base::StartsWith(name, "[anon:dalvik-zygote space")) { |
| 355 | which_heap = HEAP_DALVIK; |
| 356 | sub_heap = HEAP_DALVIK_ZYGOTE; |
| 357 | } else if (base::StartsWith(name, "[anon:dalvik-indirect ref")) { |
| 358 | sub_heap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE; |
| 359 | } else if (base::StartsWith(name, "[anon:dalvik-jit-code-cache") || |
| 360 | base::StartsWith(name, "[anon:dalvik-data-code-cache")) { |
| 361 | sub_heap = HEAP_DALVIK_OTHER_APP_CODE_CACHE; |
| 362 | } else if (base::StartsWith(name, "[anon:dalvik-CompilerMetadata")) { |
| 363 | sub_heap = HEAP_DALVIK_OTHER_COMPILER_METADATA; |
| 364 | } else { |
| 365 | sub_heap = HEAP_DALVIK_OTHER_ACCOUNTING; // Default to accounting. |
| 366 | } |
| 367 | } |
| 368 | } else if (namesz > 0) { |
| 369 | which_heap = HEAP_UNKNOWN_MAP; |
| 370 | } else if (vma.start == prev_end && prev_heap == HEAP_SO) { |
| 371 | // bss section of a shared library |
| 372 | which_heap = HEAP_SO; |
| 373 | } |
| 374 | |
| 375 | prev_end = vma.end; |
| 376 | prev_heap = which_heap; |
| 377 | |
| 378 | const meminfo::MemUsage& usage = vma.usage; |
| 379 | if (usage.swap_pss > 0 && *foundSwapPss != true) { |
| 380 | *foundSwapPss = true; |
| 381 | } |
| 382 | |
| 383 | uint64_t swapable_pss = 0; |
| 384 | if (is_swappable && (usage.pss > 0)) { |
| 385 | float sharing_proportion = 0.0; |
| 386 | if ((usage.shared_clean > 0) || (usage.shared_dirty > 0)) { |
| 387 | sharing_proportion = (usage.pss - usage.uss) / (usage.shared_clean + usage.shared_dirty); |
| 388 | } |
| 389 | swapable_pss = (sharing_proportion * usage.shared_clean) + usage.private_clean; |
| 390 | } |
| 391 | |
| 392 | stats[which_heap].pss += usage.pss; |
| 393 | stats[which_heap].swappablePss += swapable_pss; |
| 394 | stats[which_heap].rss += usage.rss; |
| 395 | stats[which_heap].privateDirty += usage.private_dirty; |
| 396 | stats[which_heap].sharedDirty += usage.shared_dirty; |
| 397 | stats[which_heap].privateClean += usage.private_clean; |
| 398 | stats[which_heap].sharedClean += usage.shared_clean; |
| 399 | stats[which_heap].swappedOut += usage.swap; |
| 400 | stats[which_heap].swappedOutPss += usage.swap_pss; |
| 401 | if (which_heap == HEAP_DALVIK || which_heap == HEAP_DALVIK_OTHER || |
| 402 | which_heap == HEAP_DEX || which_heap == HEAP_ART) { |
| 403 | stats[sub_heap].pss += usage.pss; |
| 404 | stats[sub_heap].swappablePss += swapable_pss; |
| 405 | stats[sub_heap].rss += usage.rss; |
| 406 | stats[sub_heap].privateDirty += usage.private_dirty; |
| 407 | stats[sub_heap].sharedDirty += usage.shared_dirty; |
| 408 | stats[sub_heap].privateClean += usage.private_clean; |
| 409 | stats[sub_heap].sharedClean += usage.shared_clean; |
| 410 | stats[sub_heap].swappedOut += usage.swap; |
| 411 | stats[sub_heap].swappedOutPss += usage.swap_pss; |
| 412 | } |
| 413 | }; |
| 414 | |
| 415 | return meminfo::ForEachVmaFromFile(smaps_path, vma_scan); |
| 416 | } |
| 417 | |
| 418 | static jboolean android_os_Debug_getDirtyPagesPid(JNIEnv *env, jobject clazz, |
| 419 | jint pid, jobject object) |
| 420 | { |
| 421 | bool foundSwapPss; |
| 422 | stats_t stats[_NUM_HEAP]; |
| 423 | memset(&stats, 0, sizeof(stats)); |
| 424 | |
| 425 | if (!load_maps(pid, stats, &foundSwapPss)) { |
| 426 | return JNI_FALSE; |
| 427 | } |
| 428 | |
| 429 | struct graphics_memory_pss graphics_mem; |
| 430 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
| 431 | stats[HEAP_GRAPHICS].pss = graphics_mem.graphics; |
| 432 | stats[HEAP_GRAPHICS].privateDirty = graphics_mem.graphics; |
| 433 | stats[HEAP_GRAPHICS].rss = graphics_mem.graphics; |
| 434 | stats[HEAP_GL].pss = graphics_mem.gl; |
| 435 | stats[HEAP_GL].privateDirty = graphics_mem.gl; |
| 436 | stats[HEAP_GL].rss = graphics_mem.gl; |
| 437 | stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other; |
| 438 | stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other; |
| 439 | stats[HEAP_OTHER_MEMTRACK].rss = graphics_mem.other; |
| 440 | } |
| 441 | |
| 442 | for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) { |
| 443 | stats[HEAP_UNKNOWN].pss += stats[i].pss; |
| 444 | stats[HEAP_UNKNOWN].swappablePss += stats[i].swappablePss; |
| 445 | stats[HEAP_UNKNOWN].rss += stats[i].rss; |
| 446 | stats[HEAP_UNKNOWN].privateDirty += stats[i].privateDirty; |
| 447 | stats[HEAP_UNKNOWN].sharedDirty += stats[i].sharedDirty; |
| 448 | stats[HEAP_UNKNOWN].privateClean += stats[i].privateClean; |
| 449 | stats[HEAP_UNKNOWN].sharedClean += stats[i].sharedClean; |
| 450 | stats[HEAP_UNKNOWN].swappedOut += stats[i].swappedOut; |
| 451 | stats[HEAP_UNKNOWN].swappedOutPss += stats[i].swappedOutPss; |
| 452 | } |
| 453 | |
| 454 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 455 | env->SetIntField(object, stat_fields[i].pss_field, stats[i].pss); |
| 456 | env->SetIntField(object, stat_fields[i].pssSwappable_field, stats[i].swappablePss); |
| 457 | env->SetIntField(object, stat_fields[i].rss_field, stats[i].rss); |
| 458 | env->SetIntField(object, stat_fields[i].privateDirty_field, stats[i].privateDirty); |
| 459 | env->SetIntField(object, stat_fields[i].sharedDirty_field, stats[i].sharedDirty); |
| 460 | env->SetIntField(object, stat_fields[i].privateClean_field, stats[i].privateClean); |
| 461 | env->SetIntField(object, stat_fields[i].sharedClean_field, stats[i].sharedClean); |
| 462 | env->SetIntField(object, stat_fields[i].swappedOut_field, stats[i].swappedOut); |
| 463 | env->SetIntField(object, stat_fields[i].swappedOutPss_field, stats[i].swappedOutPss); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | env->SetBooleanField(object, hasSwappedOutPss_field, foundSwapPss); |
| 468 | jintArray otherIntArray = (jintArray)env->GetObjectField(object, otherStats_field); |
| 469 | |
| 470 | jint* otherArray = (jint*)env->GetPrimitiveArrayCritical(otherIntArray, 0); |
| 471 | if (otherArray == NULL) { |
| 472 | return JNI_FALSE; |
| 473 | } |
| 474 | |
| 475 | int j=0; |
| 476 | for (int i=_NUM_CORE_HEAP; i<_NUM_HEAP; i++) { |
| 477 | otherArray[j++] = stats[i].pss; |
| 478 | otherArray[j++] = stats[i].swappablePss; |
| 479 | otherArray[j++] = stats[i].rss; |
| 480 | otherArray[j++] = stats[i].privateDirty; |
| 481 | otherArray[j++] = stats[i].sharedDirty; |
| 482 | otherArray[j++] = stats[i].privateClean; |
| 483 | otherArray[j++] = stats[i].sharedClean; |
| 484 | otherArray[j++] = stats[i].swappedOut; |
| 485 | otherArray[j++] = stats[i].swappedOutPss; |
| 486 | } |
| 487 | |
| 488 | env->ReleasePrimitiveArrayCritical(otherIntArray, otherArray, 0); |
| 489 | return JNI_TRUE; |
| 490 | } |
| 491 | |
| 492 | static void android_os_Debug_getDirtyPages(JNIEnv *env, jobject clazz, jobject object) |
| 493 | { |
| 494 | android_os_Debug_getDirtyPagesPid(env, clazz, getpid(), object); |
| 495 | } |
| 496 | |
| 497 | static jlong android_os_Debug_getPssPid(JNIEnv *env, jobject clazz, jint pid, |
| 498 | jlongArray outUssSwapPssRss, jlongArray outMemtrack) |
| 499 | { |
| 500 | jlong pss = 0; |
| 501 | jlong rss = 0; |
| 502 | jlong swapPss = 0; |
| 503 | jlong uss = 0; |
| 504 | jlong memtrack = 0; |
| 505 | |
| 506 | struct graphics_memory_pss graphics_mem; |
| 507 | if (read_memtrack_memory(pid, &graphics_mem) == 0) { |
| 508 | pss = uss = rss = memtrack = graphics_mem.graphics + graphics_mem.gl + graphics_mem.other; |
| 509 | } |
| 510 | |
| 511 | ::android::meminfo::ProcMemInfo proc_mem(pid); |
| 512 | ::android::meminfo::MemUsage stats; |
| 513 | if (proc_mem.SmapsOrRollup(&stats)) { |
| 514 | pss += stats.pss; |
| 515 | uss += stats.uss; |
| 516 | rss += stats.rss; |
| 517 | swapPss = stats.swap_pss; |
| 518 | pss += swapPss; // Also in swap, those pages would be accounted as Pss without SWAP |
| 519 | } else { |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | if (outUssSwapPssRss != NULL) { |
| 524 | int outLen = env->GetArrayLength(outUssSwapPssRss); |
| 525 | if (outLen >= 1) { |
| 526 | jlong* outUssSwapPssRssArray = env->GetLongArrayElements(outUssSwapPssRss, 0); |
| 527 | if (outUssSwapPssRssArray != NULL) { |
| 528 | outUssSwapPssRssArray[0] = uss; |
| 529 | if (outLen >= 2) { |
| 530 | outUssSwapPssRssArray[1] = swapPss; |
| 531 | } |
| 532 | if (outLen >= 3) { |
| 533 | outUssSwapPssRssArray[2] = rss; |
| 534 | } |
| 535 | } |
| 536 | env->ReleaseLongArrayElements(outUssSwapPssRss, outUssSwapPssRssArray, 0); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | if (outMemtrack != NULL) { |
| 541 | int outLen = env->GetArrayLength(outMemtrack); |
| 542 | if (outLen >= 1) { |
| 543 | jlong* outMemtrackArray = env->GetLongArrayElements(outMemtrack, 0); |
| 544 | if (outMemtrackArray != NULL) { |
| 545 | outMemtrackArray[0] = memtrack; |
| 546 | if (outLen >= 2) { |
| 547 | outMemtrackArray[1] = graphics_mem.graphics; |
| 548 | } |
| 549 | if (outLen >= 3) { |
| 550 | outMemtrackArray[2] = graphics_mem.gl; |
| 551 | } |
| 552 | if (outLen >= 4) { |
| 553 | outMemtrackArray[3] = graphics_mem.other; |
| 554 | } |
| 555 | } |
| 556 | env->ReleaseLongArrayElements(outMemtrack, outMemtrackArray, 0); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | return pss; |
| 561 | } |
| 562 | |
| 563 | static jlong android_os_Debug_getPss(JNIEnv *env, jobject clazz) |
| 564 | { |
| 565 | return android_os_Debug_getPssPid(env, clazz, getpid(), NULL, NULL); |
| 566 | } |
| 567 | |
| 568 | // The 1:1 mapping of MEMINFO_* enums here must match with the constants from |
| 569 | // Debug.java. |
| 570 | enum { |
| 571 | MEMINFO_TOTAL, |
| 572 | MEMINFO_FREE, |
| 573 | MEMINFO_BUFFERS, |
| 574 | MEMINFO_CACHED, |
| 575 | MEMINFO_SHMEM, |
| 576 | MEMINFO_SLAB, |
| 577 | MEMINFO_SLAB_RECLAIMABLE, |
| 578 | MEMINFO_SLAB_UNRECLAIMABLE, |
| 579 | MEMINFO_SWAP_TOTAL, |
| 580 | MEMINFO_SWAP_FREE, |
| 581 | MEMINFO_ZRAM_TOTAL, |
| 582 | MEMINFO_MAPPED, |
| 583 | MEMINFO_VMALLOC_USED, |
| 584 | MEMINFO_PAGE_TABLES, |
| 585 | MEMINFO_KERNEL_STACK, |
| 586 | MEMINFO_KERNEL_RECLAIMABLE, |
| 587 | MEMINFO_ACTIVE, |
| 588 | MEMINFO_INACTIVE, |
| 589 | MEMINFO_UNEVICTABLE, |
| 590 | MEMINFO_COUNT |
| 591 | }; |
| 592 | |
| 593 | static void android_os_Debug_getMemInfo(JNIEnv *env, jobject clazz, jlongArray out) |
| 594 | { |
| 595 | if (out == NULL) { |
| 596 | jniThrowNullPointerException(env, "out == null"); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | int outLen = env->GetArrayLength(out); |
| 601 | if (outLen < MEMINFO_COUNT) { |
| 602 | jniThrowRuntimeException(env, "outLen < MEMINFO_COUNT"); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | // Read system memory info including ZRAM. The values are stored in the vector |
| 607 | // in the same order as MEMINFO_* enum |
| 608 | std::vector<std::string_view> tags( |
| 609 | ::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags.begin(), |
| 610 | ::android::meminfo::SysMemInfo::kDefaultSysMemInfoTags.end()); |
| 611 | tags.insert(tags.begin() + MEMINFO_ZRAM_TOTAL, "Zram:"); |
| 612 | std::vector<uint64_t> mem(tags.size()); |
| 613 | ::android::meminfo::SysMemInfo smi; |
| 614 | if (!smi.ReadMemInfo(tags.size(), tags.data(), mem.data())) { |
| 615 | jniThrowRuntimeException(env, "SysMemInfo read failed"); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | jlong* outArray = env->GetLongArrayElements(out, 0); |
| 620 | if (outArray != NULL) { |
| 621 | outLen = MEMINFO_COUNT; |
| 622 | for (int i = 0; i < outLen; i++) { |
| 623 | if (i == MEMINFO_VMALLOC_USED && mem[i] == 0) { |
| 624 | outArray[i] = smi.ReadVmallocInfo() / 1024; |
| 625 | continue; |
| 626 | } |
| 627 | outArray[i] = mem[i]; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | env->ReleaseLongArrayElements(out, outArray, 0); |
| 632 | } |
| 633 | |
| 634 | static jint read_binder_stat(const char* stat) |
| 635 | { |
| 636 | UniqueFile fp = MakeUniqueFile(BINDER_STATS, "re"); |
| 637 | if (fp == nullptr) { |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | char line[1024]; |
| 642 | |
| 643 | char compare[128]; |
| 644 | int len = snprintf(compare, 128, "proc %d", getpid()); |
| 645 | |
| 646 | // loop until we have the block that represents this process |
| 647 | do { |
| 648 | if (fgets(line, 1024, fp.get()) == 0) { |
| 649 | return -1; |
| 650 | } |
| 651 | } while (strncmp(compare, line, len)); |
| 652 | |
| 653 | // now that we have this process, read until we find the stat that we are looking for |
| 654 | len = snprintf(compare, 128, " %s: ", stat); |
| 655 | |
| 656 | do { |
| 657 | if (fgets(line, 1024, fp.get()) == 0) { |
| 658 | return -1; |
| 659 | } |
| 660 | } while (strncmp(compare, line, len)); |
| 661 | |
| 662 | // we have the line, now increment the line ptr to the value |
| 663 | char* ptr = line + len; |
| 664 | jint result = atoi(ptr); |
| 665 | return result; |
| 666 | } |
| 667 | |
| 668 | static jint android_os_Debug_getBinderSentTransactions(JNIEnv *env, jobject clazz) |
| 669 | { |
| 670 | return read_binder_stat("bcTRANSACTION"); |
| 671 | } |
| 672 | |
| 673 | static jint android_os_getBinderReceivedTransactions(JNIEnv *env, jobject clazz) |
| 674 | { |
| 675 | return read_binder_stat("brTRANSACTION"); |
| 676 | } |
| 677 | |
| 678 | // these are implemented in android_util_Binder.cpp |
| 679 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz); |
| 680 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz); |
| 681 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz); |
| 682 | |
| 683 | static bool openFile(JNIEnv* env, jobject fileDescriptor, UniqueFile& fp) |
| 684 | { |
| 685 | if (fileDescriptor == NULL) { |
| 686 | jniThrowNullPointerException(env, "fd == null"); |
| 687 | return false; |
| 688 | } |
| 689 | int origFd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 690 | if (origFd < 0) { |
| 691 | jniThrowRuntimeException(env, "Invalid file descriptor"); |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | /* dup() the descriptor so we don't close the original with fclose() */ |
| 696 | int fd = fcntl(origFd, F_DUPFD_CLOEXEC, 0); |
| 697 | if (fd < 0) { |
| 698 | ALOGW("dup(%d) failed: %s\n", origFd, strerror(errno)); |
| 699 | jniThrowRuntimeException(env, "dup() failed"); |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | fp.reset(fdopen(fd, "w")); |
| 704 | if (fp == nullptr) { |
| 705 | ALOGW("fdopen(%d) failed: %s\n", fd, strerror(errno)); |
| 706 | close(fd); |
| 707 | jniThrowRuntimeException(env, "fdopen() failed"); |
| 708 | return false; |
| 709 | } |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Dump the native heap, writing human-readable output to the specified |
| 715 | * file descriptor. |
| 716 | */ |
| 717 | static void android_os_Debug_dumpNativeHeap(JNIEnv* env, jobject, |
| 718 | jobject fileDescriptor) |
| 719 | { |
| 720 | UniqueFile fp(nullptr, safeFclose); |
| 721 | if (!openFile(env, fileDescriptor, fp)) { |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | ALOGD("Native heap dump starting...\n"); |
| 726 | // Formatting of the native heap dump is handled by malloc debug itself. |
| 727 | // See https://android.googlesource.com/platform/bionic/+/master/libc/malloc_debug/README.md#backtrace-heap-dump-format |
| 728 | if (android_mallopt(M_WRITE_MALLOC_LEAK_INFO_TO_FILE, fp.get(), sizeof(FILE*))) { |
| 729 | ALOGD("Native heap dump complete.\n"); |
| 730 | } else { |
| 731 | PLOG(ERROR) << "Failed to write native heap dump to file"; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Dump the native malloc info, writing xml output to the specified |
| 737 | * file descriptor. |
| 738 | */ |
| 739 | static void android_os_Debug_dumpNativeMallocInfo(JNIEnv* env, jobject, |
| 740 | jobject fileDescriptor) |
| 741 | { |
| 742 | UniqueFile fp(nullptr, safeFclose); |
| 743 | if (!openFile(env, fileDescriptor, fp)) { |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | malloc_info(0, fp.get()); |
| 748 | } |
| 749 | |
| 750 | static bool dumpTraces(JNIEnv* env, jint pid, jstring fileName, jint timeoutSecs, |
| 751 | DebuggerdDumpType dumpType) { |
| 752 | const ScopedUtfChars fileNameChars(env, fileName); |
| 753 | if (fileNameChars.c_str() == nullptr) { |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | android::base::unique_fd fd(open(fileNameChars.c_str(), |
| 758 | O_CREAT | O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_APPEND, |
| 759 | 0666)); |
| 760 | if (fd < 0) { |
| 761 | PLOG(ERROR) << "Can't open " << fileNameChars.c_str(); |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | int res = dump_backtrace_to_file_timeout(pid, dumpType, timeoutSecs, fd); |
| 766 | if (fdatasync(fd.get()) != 0) { |
| 767 | PLOG(ERROR) << "Failed flushing trace."; |
| 768 | } |
| 769 | return res == 0; |
| 770 | } |
| 771 | |
| 772 | static jboolean android_os_Debug_dumpJavaBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 773 | jint pid, jstring fileName, jint timeoutSecs) { |
| 774 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdJavaBacktrace); |
| 775 | return ret ? JNI_TRUE : JNI_FALSE; |
| 776 | } |
| 777 | |
| 778 | static jboolean android_os_Debug_dumpNativeBacktraceToFileTimeout(JNIEnv* env, jobject clazz, |
| 779 | jint pid, jstring fileName, jint timeoutSecs) { |
| 780 | const bool ret = dumpTraces(env, pid, fileName, timeoutSecs, kDebuggerdNativeBacktrace); |
| 781 | return ret ? JNI_TRUE : JNI_FALSE; |
| 782 | } |
| 783 | |
| 784 | static jstring android_os_Debug_getUnreachableMemory(JNIEnv* env, jobject clazz, |
| 785 | jint limit, jboolean contents) |
| 786 | { |
| 787 | std::string s = GetUnreachableMemoryString(contents, limit); |
| 788 | return env->NewStringUTF(s.c_str()); |
| 789 | } |
| 790 | |
| 791 | static jlong android_os_Debug_getFreeZramKb(JNIEnv* env, jobject clazz) { |
| 792 | |
| 793 | jlong zramFreeKb = 0; |
| 794 | |
| 795 | std::string status_path = android::base::StringPrintf("/proc/meminfo"); |
| 796 | UniqueFile file = MakeUniqueFile(status_path.c_str(), "re"); |
| 797 | |
| 798 | char line[256]; |
| 799 | while (file != nullptr && fgets(line, sizeof(line), file.get())) { |
| 800 | jlong v; |
| 801 | if (sscanf(line, "SwapFree: %" SCNd64 " kB", &v) == 1) { |
| 802 | zramFreeKb = v; |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return zramFreeKb; |
| 808 | } |
| 809 | |
| 810 | static jlong android_os_Debug_getIonHeapsSizeKb(JNIEnv* env, jobject clazz) { |
| 811 | jlong heapsSizeKb = -1; |
| 812 | uint64_t size; |
| 813 | |
| 814 | if (meminfo::ReadIonHeapsSizeKb(&size)) { |
| 815 | heapsSizeKb = size; |
| 816 | } |
| 817 | |
| 818 | return heapsSizeKb; |
| 819 | } |
| 820 | |
| 821 | static jlong android_os_Debug_getDmabufTotalExportedKb(JNIEnv* env, jobject clazz) { |
| 822 | jlong dmabufTotalSizeKb = -1; |
| 823 | uint64_t size; |
| 824 | |
| 825 | if (dmabufinfo::GetDmabufTotalExportedKb(&size)) { |
| 826 | dmabufTotalSizeKb = size; |
| 827 | } |
| 828 | return dmabufTotalSizeKb; |
| 829 | } |
| 830 | |
| 831 | static jlong android_os_Debug_getDmabufHeapTotalExportedKb(JNIEnv* env, jobject clazz) { |
| 832 | jlong dmabufHeapTotalSizeKb = -1; |
| 833 | uint64_t size; |
| 834 | |
| 835 | if (meminfo::ReadDmabufHeapTotalExportedKb(&size)) { |
| 836 | dmabufHeapTotalSizeKb = size; |
| 837 | } |
| 838 | return dmabufHeapTotalSizeKb; |
| 839 | } |
| 840 | |
| 841 | static jlong android_os_Debug_getIonPoolsSizeKb(JNIEnv* env, jobject clazz) { |
| 842 | jlong poolsSizeKb = -1; |
| 843 | uint64_t size; |
| 844 | |
| 845 | if (meminfo::ReadIonPoolsSizeKb(&size)) { |
| 846 | poolsSizeKb = size; |
| 847 | } |
| 848 | |
| 849 | return poolsSizeKb; |
| 850 | } |
| 851 | |
| 852 | static jlong android_os_Debug_getDmabufHeapPoolsSizeKb(JNIEnv* env, jobject clazz) { |
| 853 | jlong poolsSizeKb = -1; |
| 854 | uint64_t size; |
| 855 | |
| 856 | if (meminfo::ReadDmabufHeapPoolsSizeKb(&size)) { |
| 857 | poolsSizeKb = size; |
| 858 | } |
| 859 | |
| 860 | return poolsSizeKb; |
| 861 | } |
| 862 | |
| 863 | static bool halSupportsGpuPrivateMemory() { |
| 864 | int productApiLevel = |
| 865 | android::base::GetIntProperty("ro.product.first_api_level", |
| 866 | android::base::GetIntProperty("ro.build.version.sdk", |
| 867 | __ANDROID_API_FUTURE__)); |
| 868 | int boardApiLevel = |
| 869 | android::base::GetIntProperty("ro.board.api_level", |
| 870 | android::base::GetIntProperty("ro.board.first_api_level", |
| 871 | __ANDROID_API_FUTURE__)); |
| 872 | |
| 873 | return std::min(productApiLevel, boardApiLevel) >= __ANDROID_API_S__; |
| 874 | } |
| 875 | |
| 876 | static jlong android_os_Debug_getGpuPrivateMemoryKb(JNIEnv* env, jobject clazz) { |
| 877 | static bool gpuPrivateMemorySupported = halSupportsGpuPrivateMemory(); |
| 878 | |
| 879 | struct memtrack_proc* p = memtrack_proc_new(); |
| 880 | if (p == nullptr) { |
| 881 | LOG(ERROR) << "getGpuPrivateMemoryKb: Failed to create memtrack_proc"; |
| 882 | return -1; |
| 883 | } |
| 884 | |
| 885 | // Memtrack hal defines PID 0 as global total for GPU-private (GL) memory. |
| 886 | if (memtrack_proc_get(p, 0) != 0) { |
| 887 | // The memtrack HAL may not be available, avoid flooding the log. |
| 888 | memtrack_proc_destroy(p); |
| 889 | return -1; |
| 890 | } |
| 891 | |
| 892 | ssize_t gpuPrivateMem = memtrack_proc_gl_pss(p); |
| 893 | |
| 894 | memtrack_proc_destroy(p); |
| 895 | |
| 896 | // Old HAL implementations may return 0 for GPU private memory if not supported |
| 897 | if (gpuPrivateMem == 0 && !gpuPrivateMemorySupported) { |
| 898 | return -1; |
| 899 | } |
| 900 | |
| 901 | return gpuPrivateMem / 1024; |
| 902 | } |
| 903 | |
| 904 | static jlong android_os_Debug_getDmabufMappedSizeKb(JNIEnv* env, jobject clazz) { |
| 905 | jlong dmabufPss = 0; |
| 906 | std::vector<dmabufinfo::DmaBuffer> dmabufs; |
| 907 | |
| 908 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir("/proc"), closedir); |
| 909 | if (!dir) { |
| 910 | LOG(ERROR) << "Failed to open /proc directory"; |
| 911 | return false; |
| 912 | } |
| 913 | |
| 914 | struct dirent* dent; |
| 915 | while ((dent = readdir(dir.get()))) { |
| 916 | if (dent->d_type != DT_DIR) continue; |
| 917 | |
| 918 | int pid = atoi(dent->d_name); |
| 919 | if (pid == 0) { |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | if (!ReadDmaBufMapRefs(pid, &dmabufs)) { |
| 924 | LOG(ERROR) << "Failed to read maps for pid " << pid; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | for (const dmabufinfo::DmaBuffer& buf : dmabufs) { |
| 929 | dmabufPss += buf.size() / 1024; |
| 930 | } |
| 931 | |
| 932 | return dmabufPss; |
| 933 | } |
| 934 | |
| 935 | static jlong android_os_Debug_getGpuTotalUsageKb(JNIEnv* env, jobject clazz) { |
| 936 | jlong sizeKb = -1; |
| 937 | uint64_t size; |
| 938 | |
| 939 | if (meminfo::ReadGpuTotalUsageKb(&size)) { |
| 940 | sizeKb = size; |
| 941 | } |
| 942 | |
| 943 | return sizeKb; |
| 944 | } |
| 945 | |
| 946 | static jboolean android_os_Debug_isVmapStack(JNIEnv *env, jobject clazz) |
| 947 | { |
| 948 | static enum { |
| 949 | CONFIG_UNKNOWN, |
| 950 | CONFIG_SET, |
| 951 | CONFIG_UNSET, |
| 952 | } cfg_state = CONFIG_UNKNOWN; |
| 953 | |
| 954 | if (cfg_state == CONFIG_UNKNOWN) { |
| 955 | auto runtime_info = vintf::VintfObject::GetInstance()->getRuntimeInfo( |
| 956 | vintf::RuntimeInfo::FetchFlag::CONFIG_GZ); |
| 957 | CHECK(runtime_info != nullptr) << "Kernel configs cannot be fetched. b/151092221"; |
| 958 | const std::map<std::string, std::string>& configs = runtime_info->kernelConfigs(); |
| 959 | std::map<std::string, std::string>::const_iterator it = configs.find("CONFIG_VMAP_STACK"); |
| 960 | cfg_state = (it != configs.end() && it->second == "y") ? CONFIG_SET : CONFIG_UNSET; |
| 961 | } |
| 962 | return cfg_state == CONFIG_SET; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * JNI registration. |
| 967 | */ |
| 968 | |
| 969 | static const JNINativeMethod gMethods[] = { |
| 970 | { "getNativeHeapSize", "()J", |
| 971 | (void*) android_os_Debug_getNativeHeapSize }, |
| 972 | { "getNativeHeapAllocatedSize", "()J", |
| 973 | (void*) android_os_Debug_getNativeHeapAllocatedSize }, |
| 974 | { "getNativeHeapFreeSize", "()J", |
| 975 | (void*) android_os_Debug_getNativeHeapFreeSize }, |
| 976 | { "getMemoryInfo", "(Landroid/os/Debug$MemoryInfo;)V", |
| 977 | (void*) android_os_Debug_getDirtyPages }, |
| 978 | { "getMemoryInfo", "(ILandroid/os/Debug$MemoryInfo;)Z", |
| 979 | (void*) android_os_Debug_getDirtyPagesPid }, |
| 980 | { "getPss", "()J", |
| 981 | (void*) android_os_Debug_getPss }, |
| 982 | { "getPss", "(I[J[J)J", |
| 983 | (void*) android_os_Debug_getPssPid }, |
| 984 | { "getMemInfo", "([J)V", |
| 985 | (void*) android_os_Debug_getMemInfo }, |
| 986 | { "dumpNativeHeap", "(Ljava/io/FileDescriptor;)V", |
| 987 | (void*) android_os_Debug_dumpNativeHeap }, |
| 988 | { "dumpNativeMallocInfo", "(Ljava/io/FileDescriptor;)V", |
| 989 | (void*) android_os_Debug_dumpNativeMallocInfo }, |
| 990 | { "getBinderSentTransactions", "()I", |
| 991 | (void*) android_os_Debug_getBinderSentTransactions }, |
| 992 | { "getBinderReceivedTransactions", "()I", |
| 993 | (void*) android_os_getBinderReceivedTransactions }, |
| 994 | { "getBinderLocalObjectCount", "()I", |
| 995 | (void*)android_os_Debug_getLocalObjectCount }, |
| 996 | { "getBinderProxyObjectCount", "()I", |
| 997 | (void*)android_os_Debug_getProxyObjectCount }, |
| 998 | { "getBinderDeathObjectCount", "()I", |
| 999 | (void*)android_os_Debug_getDeathObjectCount }, |
| 1000 | { "dumpJavaBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
| 1001 | (void*)android_os_Debug_dumpJavaBacktraceToFileTimeout }, |
| 1002 | { "dumpNativeBacktraceToFileTimeout", "(ILjava/lang/String;I)Z", |
| 1003 | (void*)android_os_Debug_dumpNativeBacktraceToFileTimeout }, |
| 1004 | { "getUnreachableMemory", "(IZ)Ljava/lang/String;", |
| 1005 | (void*)android_os_Debug_getUnreachableMemory }, |
| 1006 | { "getZramFreeKb", "()J", |
| 1007 | (void*)android_os_Debug_getFreeZramKb }, |
| 1008 | { "getIonHeapsSizeKb", "()J", |
| 1009 | (void*)android_os_Debug_getIonHeapsSizeKb }, |
| 1010 | { "getDmabufTotalExportedKb", "()J", |
| 1011 | (void*)android_os_Debug_getDmabufTotalExportedKb }, |
| 1012 | { "getGpuPrivateMemoryKb", "()J", |
| 1013 | (void*)android_os_Debug_getGpuPrivateMemoryKb }, |
| 1014 | { "getDmabufHeapTotalExportedKb", "()J", |
| 1015 | (void*)android_os_Debug_getDmabufHeapTotalExportedKb }, |
| 1016 | { "getIonPoolsSizeKb", "()J", |
| 1017 | (void*)android_os_Debug_getIonPoolsSizeKb }, |
| 1018 | { "getDmabufMappedSizeKb", "()J", |
| 1019 | (void*)android_os_Debug_getDmabufMappedSizeKb }, |
| 1020 | { "getDmabufHeapPoolsSizeKb", "()J", |
| 1021 | (void*)android_os_Debug_getDmabufHeapPoolsSizeKb }, |
| 1022 | { "getGpuTotalUsageKb", "()J", |
| 1023 | (void*)android_os_Debug_getGpuTotalUsageKb }, |
| 1024 | { "isVmapStack", "()Z", |
| 1025 | (void*)android_os_Debug_isVmapStack }, |
| 1026 | }; |
| 1027 | |
| 1028 | int register_android_os_Debug(JNIEnv *env) |
| 1029 | { |
| 1030 | jclass clazz = env->FindClass("android/os/Debug$MemoryInfo"); |
| 1031 | |
| 1032 | // Check the number of other statistics expected in Java matches here. |
| 1033 | jfieldID numOtherStats_field = env->GetStaticFieldID(clazz, "NUM_OTHER_STATS", "I"); |
| 1034 | jint numOtherStats = env->GetStaticIntField(clazz, numOtherStats_field); |
| 1035 | jfieldID numDvkStats_field = env->GetStaticFieldID(clazz, "NUM_DVK_STATS", "I"); |
| 1036 | jint numDvkStats = env->GetStaticIntField(clazz, numDvkStats_field); |
| 1037 | int expectedNumOtherStats = _NUM_HEAP - _NUM_CORE_HEAP; |
| 1038 | if ((numOtherStats + numDvkStats) != expectedNumOtherStats) { |
| 1039 | jniThrowExceptionFmt(env, "java/lang/RuntimeException", |
| 1040 | "android.os.Debug.Meminfo.NUM_OTHER_STATS+android.os.Debug.Meminfo.NUM_DVK_STATS=%d expected %d", |
| 1041 | numOtherStats+numDvkStats, expectedNumOtherStats); |
| 1042 | return JNI_ERR; |
| 1043 | } |
| 1044 | |
| 1045 | otherStats_field = env->GetFieldID(clazz, "otherStats", "[I"); |
| 1046 | hasSwappedOutPss_field = env->GetFieldID(clazz, "hasSwappedOutPss", "Z"); |
| 1047 | |
| 1048 | for (int i=0; i<_NUM_CORE_HEAP; i++) { |
| 1049 | stat_fields[i].pss_field = |
| 1050 | env->GetFieldID(clazz, stat_field_names[i].pss_name, "I"); |
| 1051 | stat_fields[i].pssSwappable_field = |
| 1052 | env->GetFieldID(clazz, stat_field_names[i].pssSwappable_name, "I"); |
| 1053 | stat_fields[i].rss_field = |
| 1054 | env->GetFieldID(clazz, stat_field_names[i].rss_name, "I"); |
| 1055 | stat_fields[i].privateDirty_field = |
| 1056 | env->GetFieldID(clazz, stat_field_names[i].privateDirty_name, "I"); |
| 1057 | stat_fields[i].sharedDirty_field = |
| 1058 | env->GetFieldID(clazz, stat_field_names[i].sharedDirty_name, "I"); |
| 1059 | stat_fields[i].privateClean_field = |
| 1060 | env->GetFieldID(clazz, stat_field_names[i].privateClean_name, "I"); |
| 1061 | stat_fields[i].sharedClean_field = |
| 1062 | env->GetFieldID(clazz, stat_field_names[i].sharedClean_name, "I"); |
| 1063 | stat_fields[i].swappedOut_field = |
| 1064 | env->GetFieldID(clazz, stat_field_names[i].swappedOut_name, "I"); |
| 1065 | stat_fields[i].swappedOutPss_field = |
| 1066 | env->GetFieldID(clazz, stat_field_names[i].swappedOutPss_name, "I"); |
| 1067 | } |
| 1068 | |
| 1069 | return jniRegisterNativeMethods(env, "android/os/Debug", gMethods, NELEM(gMethods)); |
| 1070 | } |
| 1071 | |
| 1072 | }; // namespace android |