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 package android.os; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.UnsupportedAppUsage; 22 import android.app.AppGlobals; 23 import android.content.Context; 24 import android.util.Log; 25 26 import com.android.internal.util.FastPrintWriter; 27 import com.android.internal.util.Preconditions; 28 import com.android.internal.util.TypedProperties; 29 30 import dalvik.system.VMDebug; 31 32 import org.apache.harmony.dalvik.ddmc.Chunk; 33 import org.apache.harmony.dalvik.ddmc.ChunkHandler; 34 import org.apache.harmony.dalvik.ddmc.DdmServer; 35 36 import java.io.File; 37 import java.io.FileDescriptor; 38 import java.io.FileNotFoundException; 39 import java.io.FileOutputStream; 40 import java.io.FileReader; 41 import java.io.IOException; 42 import java.io.PrintWriter; 43 import java.io.Reader; 44 import java.lang.annotation.ElementType; 45 import java.lang.annotation.Retention; 46 import java.lang.annotation.RetentionPolicy; 47 import java.lang.annotation.Target; 48 import java.lang.reflect.Field; 49 import java.lang.reflect.Modifier; 50 import java.util.HashMap; 51 import java.util.Map; 52 53 54 /** 55 * Provides various debugging methods for Android applications, including 56 * tracing and allocation counts. 57 * <p><strong>Logging Trace Files</strong></p> 58 * <p>Debug can create log files that give details about an application, such as 59 * a call stack and start/stop times for any running methods. See <a 60 * href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs with 61 * Traceview</a> for information about reading trace files. To start logging 62 * trace files, call one of the startMethodTracing() methods. To stop tracing, 63 * call {@link #stopMethodTracing()}. 64 */ 65 public final class Debug 66 { 67 private static final String TAG = "Debug"; 68 69 /** 70 * Flags for startMethodTracing(). These can be ORed together. 71 * 72 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the 73 * trace key file. 74 * 75 * @deprecated Accurate counting is a burden on the runtime and may be removed. 76 */ 77 @Deprecated 78 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS; 79 80 /** 81 * Flags for printLoadedClasses(). Default behavior is to only show 82 * the class name. 83 */ 84 public static final int SHOW_FULL_DETAIL = 1; 85 public static final int SHOW_CLASSLOADER = (1 << 1); 86 public static final int SHOW_INITIALIZED = (1 << 2); 87 88 // set/cleared by waitForDebugger() 89 private static volatile boolean mWaiting = false; 90 91 @UnsupportedAppUsage Debug()92 private Debug() {} 93 94 /* 95 * How long to wait for the debugger to finish sending requests. I've 96 * seen this hit 800msec on the device while waiting for a response 97 * to travel over USB and get processed, so we take that and add 98 * half a second. 99 */ 100 private static final int MIN_DEBUGGER_IDLE = 1300; // msec 101 102 /* how long to sleep when polling for activity */ 103 private static final int SPIN_DELAY = 200; // msec 104 105 /** 106 * Default trace file path and file 107 */ 108 private static final String DEFAULT_TRACE_BODY = "dmtrace"; 109 private static final String DEFAULT_TRACE_EXTENSION = ".trace"; 110 111 /** 112 * This class is used to retrieved various statistics about the memory mappings for this 113 * process. The returned info is broken down by dalvik, native, and other. All results are in kB. 114 */ 115 public static class MemoryInfo implements Parcelable { 116 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */ 117 public int dalvikPss; 118 /** The proportional set size that is swappable for dalvik heap. */ 119 /** @hide We may want to expose this, eventually. */ 120 @UnsupportedAppUsage 121 public int dalvikSwappablePss; 122 /** @hide The resident set size for dalvik heap. (Without other Dalvik overhead.) */ 123 @UnsupportedAppUsage 124 public int dalvikRss; 125 /** The private dirty pages used by dalvik heap. */ 126 public int dalvikPrivateDirty; 127 /** The shared dirty pages used by dalvik heap. */ 128 public int dalvikSharedDirty; 129 /** The private clean pages used by dalvik heap. */ 130 /** @hide We may want to expose this, eventually. */ 131 @UnsupportedAppUsage 132 public int dalvikPrivateClean; 133 /** The shared clean pages used by dalvik heap. */ 134 /** @hide We may want to expose this, eventually. */ 135 @UnsupportedAppUsage 136 public int dalvikSharedClean; 137 /** The dirty dalvik pages that have been swapped out. */ 138 /** @hide We may want to expose this, eventually. */ 139 @UnsupportedAppUsage 140 public int dalvikSwappedOut; 141 /** The dirty dalvik pages that have been swapped out, proportional. */ 142 /** @hide We may want to expose this, eventually. */ 143 @UnsupportedAppUsage 144 public int dalvikSwappedOutPss; 145 146 /** The proportional set size for the native heap. */ 147 public int nativePss; 148 /** The proportional set size that is swappable for the native heap. */ 149 /** @hide We may want to expose this, eventually. */ 150 @UnsupportedAppUsage 151 public int nativeSwappablePss; 152 /** @hide The resident set size for the native heap. */ 153 @UnsupportedAppUsage 154 public int nativeRss; 155 /** The private dirty pages used by the native heap. */ 156 public int nativePrivateDirty; 157 /** The shared dirty pages used by the native heap. */ 158 public int nativeSharedDirty; 159 /** The private clean pages used by the native heap. */ 160 /** @hide We may want to expose this, eventually. */ 161 @UnsupportedAppUsage 162 public int nativePrivateClean; 163 /** The shared clean pages used by the native heap. */ 164 /** @hide We may want to expose this, eventually. */ 165 @UnsupportedAppUsage 166 public int nativeSharedClean; 167 /** The dirty native pages that have been swapped out. */ 168 /** @hide We may want to expose this, eventually. */ 169 @UnsupportedAppUsage 170 public int nativeSwappedOut; 171 /** The dirty native pages that have been swapped out, proportional. */ 172 /** @hide We may want to expose this, eventually. */ 173 @UnsupportedAppUsage 174 public int nativeSwappedOutPss; 175 176 /** The proportional set size for everything else. */ 177 public int otherPss; 178 /** The proportional set size that is swappable for everything else. */ 179 /** @hide We may want to expose this, eventually. */ 180 @UnsupportedAppUsage 181 public int otherSwappablePss; 182 /** @hide The resident set size for everything else. */ 183 @UnsupportedAppUsage 184 public int otherRss; 185 /** The private dirty pages used by everything else. */ 186 public int otherPrivateDirty; 187 /** The shared dirty pages used by everything else. */ 188 public int otherSharedDirty; 189 /** The private clean pages used by everything else. */ 190 /** @hide We may want to expose this, eventually. */ 191 @UnsupportedAppUsage 192 public int otherPrivateClean; 193 /** The shared clean pages used by everything else. */ 194 /** @hide We may want to expose this, eventually. */ 195 @UnsupportedAppUsage 196 public int otherSharedClean; 197 /** The dirty pages used by anyting else that have been swapped out. */ 198 /** @hide We may want to expose this, eventually. */ 199 @UnsupportedAppUsage 200 public int otherSwappedOut; 201 /** The dirty pages used by anyting else that have been swapped out, proportional. */ 202 /** @hide We may want to expose this, eventually. */ 203 @UnsupportedAppUsage 204 public int otherSwappedOutPss; 205 206 /** Whether the kernel reports proportional swap usage */ 207 /** @hide */ 208 @UnsupportedAppUsage 209 public boolean hasSwappedOutPss; 210 211 /** @hide */ 212 public static final int HEAP_UNKNOWN = 0; 213 /** @hide */ 214 public static final int HEAP_DALVIK = 1; 215 /** @hide */ 216 public static final int HEAP_NATIVE = 2; 217 218 /** @hide */ 219 public static final int OTHER_DALVIK_OTHER = 0; 220 /** @hide */ 221 public static final int OTHER_STACK = 1; 222 /** @hide */ 223 public static final int OTHER_CURSOR = 2; 224 /** @hide */ 225 public static final int OTHER_ASHMEM = 3; 226 /** @hide */ 227 public static final int OTHER_GL_DEV = 4; 228 /** @hide */ 229 public static final int OTHER_UNKNOWN_DEV = 5; 230 /** @hide */ 231 public static final int OTHER_SO = 6; 232 /** @hide */ 233 public static final int OTHER_JAR = 7; 234 /** @hide */ 235 public static final int OTHER_APK = 8; 236 /** @hide */ 237 public static final int OTHER_TTF = 9; 238 /** @hide */ 239 public static final int OTHER_DEX = 10; 240 /** @hide */ 241 public static final int OTHER_OAT = 11; 242 /** @hide */ 243 public static final int OTHER_ART = 12; 244 /** @hide */ 245 public static final int OTHER_UNKNOWN_MAP = 13; 246 /** @hide */ 247 public static final int OTHER_GRAPHICS = 14; 248 /** @hide */ 249 public static final int OTHER_GL = 15; 250 /** @hide */ 251 public static final int OTHER_OTHER_MEMTRACK = 16; 252 253 // Needs to be declared here for the DVK_STAT ranges below. 254 /** @hide */ 255 @UnsupportedAppUsage 256 public static final int NUM_OTHER_STATS = 17; 257 258 // Dalvik subsections. 259 /** @hide */ 260 public static final int OTHER_DALVIK_NORMAL = 17; 261 /** @hide */ 262 public static final int OTHER_DALVIK_LARGE = 18; 263 /** @hide */ 264 public static final int OTHER_DALVIK_ZYGOTE = 19; 265 /** @hide */ 266 public static final int OTHER_DALVIK_NON_MOVING = 20; 267 // Section begins and ends for dumpsys, relative to the DALVIK categories. 268 /** @hide */ 269 public static final int OTHER_DVK_STAT_DALVIK_START = 270 OTHER_DALVIK_NORMAL - NUM_OTHER_STATS; 271 /** @hide */ 272 public static final int OTHER_DVK_STAT_DALVIK_END = 273 OTHER_DALVIK_NON_MOVING - NUM_OTHER_STATS; 274 275 // Dalvik Other subsections. 276 /** @hide */ 277 public static final int OTHER_DALVIK_OTHER_LINEARALLOC = 21; 278 /** @hide */ 279 public static final int OTHER_DALVIK_OTHER_ACCOUNTING = 22; 280 /** @hide */ 281 public static final int OTHER_DALVIK_OTHER_CODE_CACHE = 23; 282 /** @hide */ 283 public static final int OTHER_DALVIK_OTHER_COMPILER_METADATA = 24; 284 /** @hide */ 285 public static final int OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE = 25; 286 /** @hide */ 287 public static final int OTHER_DVK_STAT_DALVIK_OTHER_START = 288 OTHER_DALVIK_OTHER_LINEARALLOC - NUM_OTHER_STATS; 289 /** @hide */ 290 public static final int OTHER_DVK_STAT_DALVIK_OTHER_END = 291 OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE - NUM_OTHER_STATS; 292 293 // Dex subsections (Boot vdex, App dex, and App vdex). 294 /** @hide */ 295 public static final int OTHER_DEX_BOOT_VDEX = 26; 296 /** @hide */ 297 public static final int OTHER_DEX_APP_DEX = 27; 298 /** @hide */ 299 public static final int OTHER_DEX_APP_VDEX = 28; 300 /** @hide */ 301 public static final int OTHER_DVK_STAT_DEX_START = OTHER_DEX_BOOT_VDEX - NUM_OTHER_STATS; 302 /** @hide */ 303 public static final int OTHER_DVK_STAT_DEX_END = OTHER_DEX_APP_VDEX - NUM_OTHER_STATS; 304 305 // Art subsections (App image, boot image). 306 /** @hide */ 307 public static final int OTHER_ART_APP = 29; 308 /** @hide */ 309 public static final int OTHER_ART_BOOT = 30; 310 /** @hide */ 311 public static final int OTHER_DVK_STAT_ART_START = OTHER_ART_APP - NUM_OTHER_STATS; 312 /** @hide */ 313 public static final int OTHER_DVK_STAT_ART_END = OTHER_ART_BOOT - NUM_OTHER_STATS; 314 315 /** @hide */ 316 @UnsupportedAppUsage 317 public static final int NUM_DVK_STATS = 14; 318 319 /** @hide */ 320 public static final int NUM_CATEGORIES = 9; 321 322 /** @hide */ 323 public static final int OFFSET_PSS = 0; 324 /** @hide */ 325 public static final int OFFSET_SWAPPABLE_PSS = 1; 326 /** @hide */ 327 public static final int OFFSET_RSS = 2; 328 /** @hide */ 329 public static final int OFFSET_PRIVATE_DIRTY = 3; 330 /** @hide */ 331 public static final int OFFSET_SHARED_DIRTY = 4; 332 /** @hide */ 333 public static final int OFFSET_PRIVATE_CLEAN = 5; 334 /** @hide */ 335 public static final int OFFSET_SHARED_CLEAN = 6; 336 /** @hide */ 337 public static final int OFFSET_SWAPPED_OUT = 7; 338 /** @hide */ 339 public static final int OFFSET_SWAPPED_OUT_PSS = 8; 340 341 @UnsupportedAppUsage 342 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES]; 343 MemoryInfo()344 public MemoryInfo() { 345 } 346 347 /** 348 * @hide Copy contents from another object. 349 */ set(MemoryInfo other)350 public void set(MemoryInfo other) { 351 dalvikPss = other.dalvikPss; 352 dalvikSwappablePss = other.dalvikSwappablePss; 353 dalvikRss = other.dalvikRss; 354 dalvikPrivateDirty = other.dalvikPrivateDirty; 355 dalvikSharedDirty = other.dalvikSharedDirty; 356 dalvikPrivateClean = other.dalvikPrivateClean; 357 dalvikSharedClean = other.dalvikSharedClean; 358 dalvikSwappedOut = other.dalvikSwappedOut; 359 dalvikSwappedOutPss = other.dalvikSwappedOutPss; 360 361 nativePss = other.nativePss; 362 nativeSwappablePss = other.nativeSwappablePss; 363 nativeRss = other.nativeRss; 364 nativePrivateDirty = other.nativePrivateDirty; 365 nativeSharedDirty = other.nativeSharedDirty; 366 nativePrivateClean = other.nativePrivateClean; 367 nativeSharedClean = other.nativeSharedClean; 368 nativeSwappedOut = other.nativeSwappedOut; 369 nativeSwappedOutPss = other.nativeSwappedOutPss; 370 371 otherPss = other.otherPss; 372 otherSwappablePss = other.otherSwappablePss; 373 otherRss = other.otherRss; 374 otherPrivateDirty = other.otherPrivateDirty; 375 otherSharedDirty = other.otherSharedDirty; 376 otherPrivateClean = other.otherPrivateClean; 377 otherSharedClean = other.otherSharedClean; 378 otherSwappedOut = other.otherSwappedOut; 379 otherSwappedOutPss = other.otherSwappedOutPss; 380 381 hasSwappedOutPss = other.hasSwappedOutPss; 382 383 System.arraycopy(other.otherStats, 0, otherStats, 0, otherStats.length); 384 } 385 386 /** 387 * Return total PSS memory usage in kB. 388 */ getTotalPss()389 public int getTotalPss() { 390 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss(); 391 } 392 393 /** 394 * @hide Return total PSS memory usage in kB. 395 */ 396 @UnsupportedAppUsage getTotalUss()397 public int getTotalUss() { 398 return dalvikPrivateClean + dalvikPrivateDirty 399 + nativePrivateClean + nativePrivateDirty 400 + otherPrivateClean + otherPrivateDirty; 401 } 402 403 /** 404 * Return total PSS memory usage in kB mapping a file of one of the following extension: 405 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art . 406 */ getTotalSwappablePss()407 public int getTotalSwappablePss() { 408 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss; 409 } 410 411 /** 412 * @hide Return total RSS memory usage in kB. 413 */ getTotalRss()414 public int getTotalRss() { 415 return dalvikRss + nativeRss + otherRss; 416 } 417 418 /** 419 * Return total private dirty memory usage in kB. 420 */ getTotalPrivateDirty()421 public int getTotalPrivateDirty() { 422 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty; 423 } 424 425 /** 426 * Return total shared dirty memory usage in kB. 427 */ getTotalSharedDirty()428 public int getTotalSharedDirty() { 429 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty; 430 } 431 432 /** 433 * Return total shared clean memory usage in kB. 434 */ getTotalPrivateClean()435 public int getTotalPrivateClean() { 436 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean; 437 } 438 439 /** 440 * Return total shared clean memory usage in kB. 441 */ getTotalSharedClean()442 public int getTotalSharedClean() { 443 return dalvikSharedClean + nativeSharedClean + otherSharedClean; 444 } 445 446 /** 447 * Return total swapped out memory in kB. 448 * @hide 449 */ getTotalSwappedOut()450 public int getTotalSwappedOut() { 451 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut; 452 } 453 454 /** 455 * Return total swapped out memory in kB, proportional. 456 * @hide 457 */ getTotalSwappedOutPss()458 public int getTotalSwappedOutPss() { 459 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss; 460 } 461 462 /** @hide */ 463 @UnsupportedAppUsage getOtherPss(int which)464 public int getOtherPss(int which) { 465 return otherStats[which * NUM_CATEGORIES + OFFSET_PSS]; 466 } 467 468 /** @hide */ getOtherSwappablePss(int which)469 public int getOtherSwappablePss(int which) { 470 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPABLE_PSS]; 471 } 472 473 /** @hide */ getOtherRss(int which)474 public int getOtherRss(int which) { 475 return otherStats[which * NUM_CATEGORIES + OFFSET_RSS]; 476 } 477 478 /** @hide */ 479 @UnsupportedAppUsage getOtherPrivateDirty(int which)480 public int getOtherPrivateDirty(int which) { 481 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_DIRTY]; 482 } 483 484 /** @hide */ 485 @UnsupportedAppUsage getOtherSharedDirty(int which)486 public int getOtherSharedDirty(int which) { 487 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_DIRTY]; 488 } 489 490 /** @hide */ getOtherPrivateClean(int which)491 public int getOtherPrivateClean(int which) { 492 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_CLEAN]; 493 } 494 495 /** @hide */ 496 @UnsupportedAppUsage getOtherPrivate(int which)497 public int getOtherPrivate(int which) { 498 return getOtherPrivateClean(which) + getOtherPrivateDirty(which); 499 } 500 501 /** @hide */ getOtherSharedClean(int which)502 public int getOtherSharedClean(int which) { 503 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_CLEAN]; 504 } 505 506 /** @hide */ getOtherSwappedOut(int which)507 public int getOtherSwappedOut(int which) { 508 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT]; 509 } 510 511 /** @hide */ getOtherSwappedOutPss(int which)512 public int getOtherSwappedOutPss(int which) { 513 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT_PSS]; 514 } 515 516 /** @hide */ 517 @UnsupportedAppUsage getOtherLabel(int which)518 public static String getOtherLabel(int which) { 519 switch (which) { 520 case OTHER_DALVIK_OTHER: return "Dalvik Other"; 521 case OTHER_STACK: return "Stack"; 522 case OTHER_CURSOR: return "Cursor"; 523 case OTHER_ASHMEM: return "Ashmem"; 524 case OTHER_GL_DEV: return "Gfx dev"; 525 case OTHER_UNKNOWN_DEV: return "Other dev"; 526 case OTHER_SO: return ".so mmap"; 527 case OTHER_JAR: return ".jar mmap"; 528 case OTHER_APK: return ".apk mmap"; 529 case OTHER_TTF: return ".ttf mmap"; 530 case OTHER_DEX: return ".dex mmap"; 531 case OTHER_OAT: return ".oat mmap"; 532 case OTHER_ART: return ".art mmap"; 533 case OTHER_UNKNOWN_MAP: return "Other mmap"; 534 case OTHER_GRAPHICS: return "EGL mtrack"; 535 case OTHER_GL: return "GL mtrack"; 536 case OTHER_OTHER_MEMTRACK: return "Other mtrack"; 537 case OTHER_DALVIK_NORMAL: return ".Heap"; 538 case OTHER_DALVIK_LARGE: return ".LOS"; 539 case OTHER_DALVIK_ZYGOTE: return ".Zygote"; 540 case OTHER_DALVIK_NON_MOVING: return ".NonMoving"; 541 case OTHER_DALVIK_OTHER_LINEARALLOC: return ".LinearAlloc"; 542 case OTHER_DALVIK_OTHER_ACCOUNTING: return ".GC"; 543 case OTHER_DALVIK_OTHER_CODE_CACHE: return ".JITCache"; 544 case OTHER_DALVIK_OTHER_COMPILER_METADATA: return ".CompilerMetadata"; 545 case OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE: return ".IndirectRef"; 546 case OTHER_DEX_BOOT_VDEX: return ".Boot vdex"; 547 case OTHER_DEX_APP_DEX: return ".App dex"; 548 case OTHER_DEX_APP_VDEX: return ".App vdex"; 549 case OTHER_ART_APP: return ".App art"; 550 case OTHER_ART_BOOT: return ".Boot art"; 551 default: return "????"; 552 } 553 } 554 555 /** 556 * Returns the value of a particular memory statistic or {@code null} if no 557 * such memory statistic exists. 558 * 559 * <p>The following table lists the memory statistics that are supported. 560 * Note that memory statistics may be added or removed in a future API level.</p> 561 * 562 * <table> 563 * <thead> 564 * <tr> 565 * <th>Memory statistic name</th> 566 * <th>Meaning</th> 567 * <th>Example</th> 568 * <th>Supported (API Levels)</th> 569 * </tr> 570 * </thead> 571 * <tbody> 572 * <tr> 573 * <td>summary.java-heap</td> 574 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field 575 * in the App Summary section output by dumpsys meminfo.</td> 576 * <td>{@code 1442}</td> 577 * <td>23</td> 578 * </tr> 579 * <tr> 580 * <td>summary.native-heap</td> 581 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap 582 * field in the App Summary section output by dumpsys meminfo.</td> 583 * <td>{@code 1442}</td> 584 * <td>23</td> 585 * </tr> 586 * <tr> 587 * <td>summary.code</td> 588 * <td>The memory usage for static code and resources in kB. This corresponds to 589 * the Code field in the App Summary section output by dumpsys meminfo.</td> 590 * <td>{@code 1442}</td> 591 * <td>23</td> 592 * </tr> 593 * <tr> 594 * <td>summary.stack</td> 595 * <td>The stack usage in kB. This corresponds to the Stack field in the 596 * App Summary section output by dumpsys meminfo.</td> 597 * <td>{@code 1442}</td> 598 * <td>23</td> 599 * </tr> 600 * <tr> 601 * <td>summary.graphics</td> 602 * <td>The graphics usage in kB. This corresponds to the Graphics field in the 603 * App Summary section output by dumpsys meminfo.</td> 604 * <td>{@code 1442}</td> 605 * <td>23</td> 606 * </tr> 607 * <tr> 608 * <td>summary.private-other</td> 609 * <td>Other private memory usage in kB. This corresponds to the Private Other 610 * field output in the App Summary section by dumpsys meminfo.</td> 611 * <td>{@code 1442}</td> 612 * <td>23</td> 613 * </tr> 614 * <tr> 615 * <td>summary.system</td> 616 * <td>Shared and system memory usage in kB. This corresponds to the System 617 * field output in the App Summary section by dumpsys meminfo.</td> 618 * <td>{@code 1442}</td> 619 * <td>23</td> 620 * </tr> 621 * <tr> 622 * <td>summary.total-pss</td> 623 * <td>Total PPS memory usage in kB.</td> 624 * <td>{@code 1442}</td> 625 * <td>23</td> 626 * </tr> 627 * <tr> 628 * <td>summary.total-swap</td> 629 * <td>Total swap usage in kB.</td> 630 * <td>{@code 1442}</td> 631 * <td>23</td> 632 * </tr> 633 * </tbody> 634 * </table> 635 */ getMemoryStat(String statName)636 public String getMemoryStat(String statName) { 637 switch(statName) { 638 case "summary.java-heap": 639 return Integer.toString(getSummaryJavaHeap()); 640 case "summary.native-heap": 641 return Integer.toString(getSummaryNativeHeap()); 642 case "summary.code": 643 return Integer.toString(getSummaryCode()); 644 case "summary.stack": 645 return Integer.toString(getSummaryStack()); 646 case "summary.graphics": 647 return Integer.toString(getSummaryGraphics()); 648 case "summary.private-other": 649 return Integer.toString(getSummaryPrivateOther()); 650 case "summary.system": 651 return Integer.toString(getSummarySystem()); 652 case "summary.total-pss": 653 return Integer.toString(getSummaryTotalPss()); 654 case "summary.total-swap": 655 return Integer.toString(getSummaryTotalSwap()); 656 default: 657 return null; 658 } 659 } 660 661 /** 662 * Returns a map of the names/values of the memory statistics 663 * that {@link #getMemoryStat(String)} supports. 664 * 665 * @return a map of the names/values of the supported memory statistics. 666 */ getMemoryStats()667 public Map<String, String> getMemoryStats() { 668 Map<String, String> stats = new HashMap<String, String>(); 669 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap())); 670 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap())); 671 stats.put("summary.code", Integer.toString(getSummaryCode())); 672 stats.put("summary.stack", Integer.toString(getSummaryStack())); 673 stats.put("summary.graphics", Integer.toString(getSummaryGraphics())); 674 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther())); 675 stats.put("summary.system", Integer.toString(getSummarySystem())); 676 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss())); 677 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap())); 678 return stats; 679 } 680 681 /** 682 * Pss of Java Heap bytes in KB due to the application. 683 * Notes: 684 * * OTHER_ART is the boot image. Anything private here is blamed on 685 * the application, not the system. 686 * * dalvikPrivateDirty includes private zygote, which means the 687 * application dirtied something allocated by the zygote. We blame 688 * the application for that memory, not the system. 689 * * Does not include OTHER_DALVIK_OTHER, which is considered VM 690 * Overhead and lumped into Private Other. 691 * * We don't include dalvikPrivateClean, because there should be no 692 * such thing as private clean for the Java Heap. 693 * @hide 694 */ 695 @UnsupportedAppUsage getSummaryJavaHeap()696 public int getSummaryJavaHeap() { 697 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART); 698 } 699 700 /** 701 * Pss of Native Heap bytes in KB due to the application. 702 * Notes: 703 * * Includes private dirty malloc space. 704 * * We don't include nativePrivateClean, because there should be no 705 * such thing as private clean for the Native Heap. 706 * @hide 707 */ 708 @UnsupportedAppUsage getSummaryNativeHeap()709 public int getSummaryNativeHeap() { 710 return nativePrivateDirty; 711 } 712 713 /** 714 * Pss of code and other static resource bytes in KB due to 715 * the application. 716 * @hide 717 */ 718 @UnsupportedAppUsage getSummaryCode()719 public int getSummaryCode() { 720 return getOtherPrivate(OTHER_SO) 721 + getOtherPrivate(OTHER_JAR) 722 + getOtherPrivate(OTHER_APK) 723 + getOtherPrivate(OTHER_TTF) 724 + getOtherPrivate(OTHER_DEX) 725 + getOtherPrivate(OTHER_OAT); 726 } 727 728 /** 729 * Pss in KB of the stack due to the application. 730 * Notes: 731 * * Includes private dirty stack, which includes both Java and Native 732 * stack. 733 * * Does not include private clean stack, because there should be no 734 * such thing as private clean for the stack. 735 * @hide 736 */ 737 @UnsupportedAppUsage getSummaryStack()738 public int getSummaryStack() { 739 return getOtherPrivateDirty(OTHER_STACK); 740 } 741 742 /** 743 * Pss in KB of graphics due to the application. 744 * Notes: 745 * * Includes private Gfx, EGL, and GL. 746 * * Warning: These numbers can be misreported by the graphics drivers. 747 * * We don't include shared graphics. It may make sense to, because 748 * shared graphics are likely buffers due to the application 749 * anyway, but it's simpler to implement to just group all shared 750 * memory into the System category. 751 * @hide 752 */ 753 @UnsupportedAppUsage getSummaryGraphics()754 public int getSummaryGraphics() { 755 return getOtherPrivate(OTHER_GL_DEV) 756 + getOtherPrivate(OTHER_GRAPHICS) 757 + getOtherPrivate(OTHER_GL); 758 } 759 760 /** 761 * Pss in KB due to the application that haven't otherwise been 762 * accounted for. 763 * @hide 764 */ 765 @UnsupportedAppUsage getSummaryPrivateOther()766 public int getSummaryPrivateOther() { 767 return getTotalPrivateClean() 768 + getTotalPrivateDirty() 769 - getSummaryJavaHeap() 770 - getSummaryNativeHeap() 771 - getSummaryCode() 772 - getSummaryStack() 773 - getSummaryGraphics(); 774 } 775 776 /** 777 * Pss in KB due to the system. 778 * Notes: 779 * * Includes all shared memory. 780 * @hide 781 */ 782 @UnsupportedAppUsage getSummarySystem()783 public int getSummarySystem() { 784 return getTotalPss() 785 - getTotalPrivateClean() 786 - getTotalPrivateDirty(); 787 } 788 789 /** 790 * Total Pss in KB. 791 * @hide 792 */ getSummaryTotalPss()793 public int getSummaryTotalPss() { 794 return getTotalPss(); 795 } 796 797 /** 798 * Total Swap in KB. 799 * Notes: 800 * * Some of this memory belongs in other categories, but we don't 801 * know if the Swap memory is shared or private, so we don't know 802 * what to blame on the application and what on the system. 803 * For now, just lump all the Swap in one place. 804 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()} 805 * will report the application proportional Swap. 806 * @hide 807 */ getSummaryTotalSwap()808 public int getSummaryTotalSwap() { 809 return getTotalSwappedOut(); 810 } 811 812 /** 813 * Total proportional Swap in KB. 814 * Notes: 815 * * Always 0 if {@link #hasSwappedOutPss} is false. 816 * @hide 817 */ getSummaryTotalSwapPss()818 public int getSummaryTotalSwapPss() { 819 return getTotalSwappedOutPss(); 820 } 821 822 /** 823 * Return true if the kernel is reporting pss swapped out... that is, if 824 * {@link #getSummaryTotalSwapPss()} will return non-0 values. 825 * @hide 826 */ hasSwappedOutPss()827 public boolean hasSwappedOutPss() { 828 return hasSwappedOutPss; 829 } 830 describeContents()831 public int describeContents() { 832 return 0; 833 } 834 writeToParcel(Parcel dest, int flags)835 public void writeToParcel(Parcel dest, int flags) { 836 dest.writeInt(dalvikPss); 837 dest.writeInt(dalvikSwappablePss); 838 dest.writeInt(dalvikRss); 839 dest.writeInt(dalvikPrivateDirty); 840 dest.writeInt(dalvikSharedDirty); 841 dest.writeInt(dalvikPrivateClean); 842 dest.writeInt(dalvikSharedClean); 843 dest.writeInt(dalvikSwappedOut); 844 dest.writeInt(dalvikSwappedOutPss); 845 dest.writeInt(nativePss); 846 dest.writeInt(nativeSwappablePss); 847 dest.writeInt(nativeRss); 848 dest.writeInt(nativePrivateDirty); 849 dest.writeInt(nativeSharedDirty); 850 dest.writeInt(nativePrivateClean); 851 dest.writeInt(nativeSharedClean); 852 dest.writeInt(nativeSwappedOut); 853 dest.writeInt(nativeSwappedOutPss); 854 dest.writeInt(otherPss); 855 dest.writeInt(otherSwappablePss); 856 dest.writeInt(otherRss); 857 dest.writeInt(otherPrivateDirty); 858 dest.writeInt(otherSharedDirty); 859 dest.writeInt(otherPrivateClean); 860 dest.writeInt(otherSharedClean); 861 dest.writeInt(otherSwappedOut); 862 dest.writeInt(hasSwappedOutPss ? 1 : 0); 863 dest.writeInt(otherSwappedOutPss); 864 dest.writeIntArray(otherStats); 865 } 866 readFromParcel(Parcel source)867 public void readFromParcel(Parcel source) { 868 dalvikPss = source.readInt(); 869 dalvikSwappablePss = source.readInt(); 870 dalvikRss = source.readInt(); 871 dalvikPrivateDirty = source.readInt(); 872 dalvikSharedDirty = source.readInt(); 873 dalvikPrivateClean = source.readInt(); 874 dalvikSharedClean = source.readInt(); 875 dalvikSwappedOut = source.readInt(); 876 dalvikSwappedOutPss = source.readInt(); 877 nativePss = source.readInt(); 878 nativeSwappablePss = source.readInt(); 879 nativeRss = source.readInt(); 880 nativePrivateDirty = source.readInt(); 881 nativeSharedDirty = source.readInt(); 882 nativePrivateClean = source.readInt(); 883 nativeSharedClean = source.readInt(); 884 nativeSwappedOut = source.readInt(); 885 nativeSwappedOutPss = source.readInt(); 886 otherPss = source.readInt(); 887 otherSwappablePss = source.readInt(); 888 otherRss = source.readInt(); 889 otherPrivateDirty = source.readInt(); 890 otherSharedDirty = source.readInt(); 891 otherPrivateClean = source.readInt(); 892 otherSharedClean = source.readInt(); 893 otherSwappedOut = source.readInt(); 894 hasSwappedOutPss = source.readInt() != 0; 895 otherSwappedOutPss = source.readInt(); 896 otherStats = source.createIntArray(); 897 } 898 899 public static final @android.annotation.NonNull Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() { 900 public MemoryInfo createFromParcel(Parcel source) { 901 return new MemoryInfo(source); 902 } 903 public MemoryInfo[] newArray(int size) { 904 return new MemoryInfo[size]; 905 } 906 }; 907 MemoryInfo(Parcel source)908 private MemoryInfo(Parcel source) { 909 readFromParcel(source); 910 } 911 } 912 913 914 /** 915 * Wait until a debugger attaches. As soon as the debugger attaches, 916 * this returns, so you will need to place a breakpoint after the 917 * waitForDebugger() call if you want to start tracing immediately. 918 */ waitForDebugger()919 public static void waitForDebugger() { 920 if (!VMDebug.isDebuggingEnabled()) { 921 //System.out.println("debugging not enabled, not waiting"); 922 return; 923 } 924 if (isDebuggerConnected()) 925 return; 926 927 // if DDMS is listening, inform them of our plight 928 System.out.println("Sending WAIT chunk"); 929 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger" 930 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1); 931 DdmServer.sendChunk(waitChunk); 932 933 mWaiting = true; 934 while (!isDebuggerConnected()) { 935 try { Thread.sleep(SPIN_DELAY); } 936 catch (InterruptedException ie) {} 937 } 938 mWaiting = false; 939 940 System.out.println("Debugger has connected"); 941 942 /* 943 * There is no "ready to go" signal from the debugger, and we're 944 * not allowed to suspend ourselves -- the debugger expects us to 945 * be running happily, and gets confused if we aren't. We need to 946 * allow the debugger a chance to set breakpoints before we start 947 * running again. 948 * 949 * Sit and spin until the debugger has been idle for a short while. 950 */ 951 while (true) { 952 long delta = VMDebug.lastDebuggerActivity(); 953 if (delta < 0) { 954 System.out.println("debugger detached?"); 955 break; 956 } 957 958 if (delta < MIN_DEBUGGER_IDLE) { 959 System.out.println("waiting for debugger to settle..."); 960 try { Thread.sleep(SPIN_DELAY); } 961 catch (InterruptedException ie) {} 962 } else { 963 System.out.println("debugger has settled (" + delta + ")"); 964 break; 965 } 966 } 967 } 968 969 /** 970 * Returns "true" if one or more threads is waiting for a debugger 971 * to attach. 972 */ waitingForDebugger()973 public static boolean waitingForDebugger() { 974 return mWaiting; 975 } 976 977 /** 978 * Determine if a debugger is currently attached. 979 */ isDebuggerConnected()980 public static boolean isDebuggerConnected() { 981 return VMDebug.isDebuggerConnected(); 982 } 983 984 /** 985 * Returns an array of strings that identify VM features. This is 986 * used by DDMS to determine what sorts of operations the VM can 987 * perform. 988 * 989 * @hide 990 */ getVmFeatureList()991 public static String[] getVmFeatureList() { 992 return VMDebug.getVmFeatureList(); 993 } 994 995 /** 996 * Change the JDWP port. 997 * 998 * @deprecated no longer needed or useful 999 */ 1000 @Deprecated changeDebugPort(int port)1001 public static void changeDebugPort(int port) {} 1002 1003 /** 1004 * This is the pathname to the sysfs file that enables and disables 1005 * tracing on the qemu emulator. 1006 */ 1007 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state"; 1008 1009 /** 1010 * Enable qemu tracing. For this to work requires running everything inside 1011 * the qemu emulator; otherwise, this method will have no effect. The trace 1012 * file is specified on the command line when the emulator is started. For 1013 * example, the following command line <br /> 1014 * <code>emulator -trace foo</code><br /> 1015 * will start running the emulator and create a trace file named "foo". This 1016 * method simply enables writing the trace records to the trace file. 1017 * 1018 * <p> 1019 * The main differences between this and {@link #startMethodTracing()} are 1020 * that tracing in the qemu emulator traces every cpu instruction of every 1021 * process, including kernel code, so we have more complete information, 1022 * including all context switches. We can also get more detailed information 1023 * such as cache misses. The sequence of calls is determined by 1024 * post-processing the instruction trace. The qemu tracing is also done 1025 * without modifying the application or perturbing the timing of calls 1026 * because no instrumentation is added to the application being traced. 1027 * </p> 1028 * 1029 * <p> 1030 * One limitation of using this method compared to using 1031 * {@link #startMethodTracing()} on the real device is that the emulator 1032 * does not model all of the real hardware effects such as memory and 1033 * bus contention. The emulator also has a simple cache model and cannot 1034 * capture all the complexities of a real cache. 1035 * </p> 1036 */ startNativeTracing()1037 public static void startNativeTracing() { 1038 // Open the sysfs file for writing and write "1" to it. 1039 PrintWriter outStream = null; 1040 try { 1041 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 1042 outStream = new FastPrintWriter(fos); 1043 outStream.println("1"); 1044 } catch (Exception e) { 1045 } finally { 1046 if (outStream != null) 1047 outStream.close(); 1048 } 1049 1050 VMDebug.startEmulatorTracing(); 1051 } 1052 1053 /** 1054 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing. 1055 * 1056 * <p>Tracing can be started and stopped as many times as desired. When 1057 * the qemu emulator itself is stopped then the buffered trace records 1058 * are flushed and written to the trace file. In fact, it is not necessary 1059 * to call this method at all; simply killing qemu is sufficient. But 1060 * starting and stopping a trace is useful for examining a specific 1061 * region of code.</p> 1062 */ stopNativeTracing()1063 public static void stopNativeTracing() { 1064 VMDebug.stopEmulatorTracing(); 1065 1066 // Open the sysfs file for writing and write "0" to it. 1067 PrintWriter outStream = null; 1068 try { 1069 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 1070 outStream = new FastPrintWriter(fos); 1071 outStream.println("0"); 1072 } catch (Exception e) { 1073 // We could print an error message here but we probably want 1074 // to quietly ignore errors if we are not running in the emulator. 1075 } finally { 1076 if (outStream != null) 1077 outStream.close(); 1078 } 1079 } 1080 1081 /** 1082 * Enable "emulator traces", in which information about the current 1083 * method is made available to the "emulator -trace" feature. There 1084 * is no corresponding "disable" call -- this is intended for use by 1085 * the framework when tracing should be turned on and left that way, so 1086 * that traces captured with F9/F10 will include the necessary data. 1087 * 1088 * This puts the VM into "profile" mode, which has performance 1089 * consequences. 1090 * 1091 * To temporarily enable tracing, use {@link #startNativeTracing()}. 1092 */ enableEmulatorTraceOutput()1093 public static void enableEmulatorTraceOutput() { 1094 VMDebug.startEmulatorTracing(); 1095 } 1096 1097 /** 1098 * Start method tracing with default log name and buffer size. 1099 * <p> 1100 * By default, the trace file is called "dmtrace.trace" and it's placed 1101 * under your package-specific directory on primary shared/external storage, 1102 * as returned by {@link Context#getExternalFilesDir(String)}. 1103 * <p> 1104 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs 1105 * with Traceview</a> for information about reading trace files. 1106 * <p class="note"> 1107 * When method tracing is enabled, the VM will run more slowly than usual, 1108 * so the timings from the trace files should only be considered in relative 1109 * terms (e.g. was run #1 faster than run #2). The times for native methods 1110 * will not change, so don't try to use this to compare the performance of 1111 * interpreted and native implementations of the same method. As an 1112 * alternative, consider using sampling-based method tracing via 1113 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing 1114 * in the emulator via {@link #startNativeTracing()}. 1115 * </p> 1116 */ startMethodTracing()1117 public static void startMethodTracing() { 1118 VMDebug.startMethodTracing(fixTracePath(null), 0, 0, false, 0); 1119 } 1120 1121 /** 1122 * Start method tracing, specifying the trace log file path. 1123 * <p> 1124 * When a relative file path is given, the trace file will be placed under 1125 * your package-specific directory on primary shared/external storage, as 1126 * returned by {@link Context#getExternalFilesDir(String)}. 1127 * <p> 1128 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs 1129 * with Traceview</a> for information about reading trace files. 1130 * <p class="note"> 1131 * When method tracing is enabled, the VM will run more slowly than usual, 1132 * so the timings from the trace files should only be considered in relative 1133 * terms (e.g. was run #1 faster than run #2). The times for native methods 1134 * will not change, so don't try to use this to compare the performance of 1135 * interpreted and native implementations of the same method. As an 1136 * alternative, consider using sampling-based method tracing via 1137 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing 1138 * in the emulator via {@link #startNativeTracing()}. 1139 * </p> 1140 * 1141 * @param tracePath Path to the trace log file to create. If {@code null}, 1142 * this will default to "dmtrace.trace". If the file already 1143 * exists, it will be truncated. If the path given does not end 1144 * in ".trace", it will be appended for you. 1145 */ startMethodTracing(String tracePath)1146 public static void startMethodTracing(String tracePath) { 1147 startMethodTracing(tracePath, 0, 0); 1148 } 1149 1150 /** 1151 * Start method tracing, specifying the trace log file name and the buffer 1152 * size. 1153 * <p> 1154 * When a relative file path is given, the trace file will be placed under 1155 * your package-specific directory on primary shared/external storage, as 1156 * returned by {@link Context#getExternalFilesDir(String)}. 1157 * <p> 1158 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs 1159 * with Traceview</a> for information about reading trace files. 1160 * <p class="note"> 1161 * When method tracing is enabled, the VM will run more slowly than usual, 1162 * so the timings from the trace files should only be considered in relative 1163 * terms (e.g. was run #1 faster than run #2). The times for native methods 1164 * will not change, so don't try to use this to compare the performance of 1165 * interpreted and native implementations of the same method. As an 1166 * alternative, consider using sampling-based method tracing via 1167 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing 1168 * in the emulator via {@link #startNativeTracing()}. 1169 * </p> 1170 * 1171 * @param tracePath Path to the trace log file to create. If {@code null}, 1172 * this will default to "dmtrace.trace". If the file already 1173 * exists, it will be truncated. If the path given does not end 1174 * in ".trace", it will be appended for you. 1175 * @param bufferSize The maximum amount of trace data we gather. If not 1176 * given, it defaults to 8MB. 1177 */ startMethodTracing(String tracePath, int bufferSize)1178 public static void startMethodTracing(String tracePath, int bufferSize) { 1179 startMethodTracing(tracePath, bufferSize, 0); 1180 } 1181 1182 /** 1183 * Start method tracing, specifying the trace log file name, the buffer 1184 * size, and flags. 1185 * <p> 1186 * When a relative file path is given, the trace file will be placed under 1187 * your package-specific directory on primary shared/external storage, as 1188 * returned by {@link Context#getExternalFilesDir(String)}. 1189 * <p> 1190 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs 1191 * with Traceview</a> for information about reading trace files. 1192 * <p class="note"> 1193 * When method tracing is enabled, the VM will run more slowly than usual, 1194 * so the timings from the trace files should only be considered in relative 1195 * terms (e.g. was run #1 faster than run #2). The times for native methods 1196 * will not change, so don't try to use this to compare the performance of 1197 * interpreted and native implementations of the same method. As an 1198 * alternative, consider using sampling-based method tracing via 1199 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing 1200 * in the emulator via {@link #startNativeTracing()}. 1201 * </p> 1202 * 1203 * @param tracePath Path to the trace log file to create. If {@code null}, 1204 * this will default to "dmtrace.trace". If the file already 1205 * exists, it will be truncated. If the path given does not end 1206 * in ".trace", it will be appended for you. 1207 * @param bufferSize The maximum amount of trace data we gather. If not 1208 * given, it defaults to 8MB. 1209 * @param flags Flags to control method tracing. The only one that is 1210 * currently defined is {@link #TRACE_COUNT_ALLOCS}. 1211 */ startMethodTracing(String tracePath, int bufferSize, int flags)1212 public static void startMethodTracing(String tracePath, int bufferSize, int flags) { 1213 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, flags, false, 0); 1214 } 1215 1216 /** 1217 * Start sampling-based method tracing, specifying the trace log file name, 1218 * the buffer size, and the sampling interval. 1219 * <p> 1220 * When a relative file path is given, the trace file will be placed under 1221 * your package-specific directory on primary shared/external storage, as 1222 * returned by {@link Context#getExternalFilesDir(String)}. 1223 * <p> 1224 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs 1225 * with Traceview</a> for information about reading trace files. 1226 * 1227 * @param tracePath Path to the trace log file to create. If {@code null}, 1228 * this will default to "dmtrace.trace". If the file already 1229 * exists, it will be truncated. If the path given does not end 1230 * in ".trace", it will be appended for you. 1231 * @param bufferSize The maximum amount of trace data we gather. If not 1232 * given, it defaults to 8MB. 1233 * @param intervalUs The amount of time between each sample in microseconds. 1234 */ startMethodTracingSampling(String tracePath, int bufferSize, int intervalUs)1235 public static void startMethodTracingSampling(String tracePath, int bufferSize, 1236 int intervalUs) { 1237 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, 0, true, intervalUs); 1238 } 1239 1240 /** 1241 * Formats name of trace log file for method tracing. 1242 */ fixTracePath(String tracePath)1243 private static String fixTracePath(String tracePath) { 1244 if (tracePath == null || tracePath.charAt(0) != '/') { 1245 final Context context = AppGlobals.getInitialApplication(); 1246 final File dir; 1247 if (context != null) { 1248 dir = context.getExternalFilesDir(null); 1249 } else { 1250 dir = Environment.getExternalStorageDirectory(); 1251 } 1252 1253 if (tracePath == null) { 1254 tracePath = new File(dir, DEFAULT_TRACE_BODY).getAbsolutePath(); 1255 } else { 1256 tracePath = new File(dir, tracePath).getAbsolutePath(); 1257 } 1258 } 1259 if (!tracePath.endsWith(DEFAULT_TRACE_EXTENSION)) { 1260 tracePath += DEFAULT_TRACE_EXTENSION; 1261 } 1262 return tracePath; 1263 } 1264 1265 /** 1266 * Like startMethodTracing(String, int, int), but taking an already-opened 1267 * FileDescriptor in which the trace is written. The file name is also 1268 * supplied simply for logging. Makes a dup of the file descriptor. 1269 * 1270 * Not exposed in the SDK unless we are really comfortable with supporting 1271 * this and find it would be useful. 1272 * @hide 1273 */ startMethodTracing(String traceName, FileDescriptor fd, int bufferSize, int flags, boolean streamOutput)1274 public static void startMethodTracing(String traceName, FileDescriptor fd, 1275 int bufferSize, int flags, boolean streamOutput) { 1276 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0, streamOutput); 1277 } 1278 1279 /** 1280 * Starts method tracing without a backing file. When stopMethodTracing 1281 * is called, the result is sent directly to DDMS. (If DDMS is not 1282 * attached when tracing ends, the profiling data will be discarded.) 1283 * 1284 * @hide 1285 */ startMethodTracingDdms(int bufferSize, int flags, boolean samplingEnabled, int intervalUs)1286 public static void startMethodTracingDdms(int bufferSize, int flags, 1287 boolean samplingEnabled, int intervalUs) { 1288 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs); 1289 } 1290 1291 /** 1292 * Determine whether method tracing is currently active and what type is 1293 * active. 1294 * 1295 * @hide 1296 */ getMethodTracingMode()1297 public static int getMethodTracingMode() { 1298 return VMDebug.getMethodTracingMode(); 1299 } 1300 1301 /** 1302 * Stop method tracing. 1303 */ stopMethodTracing()1304 public static void stopMethodTracing() { 1305 VMDebug.stopMethodTracing(); 1306 } 1307 1308 /** 1309 * Get an indication of thread CPU usage. The value returned 1310 * indicates the amount of time that the current thread has spent 1311 * executing code or waiting for certain types of I/O. 1312 * 1313 * The time is expressed in nanoseconds, and is only meaningful 1314 * when compared to the result from an earlier call. Note that 1315 * nanosecond resolution does not imply nanosecond accuracy. 1316 * 1317 * On system which don't support this operation, the call returns -1. 1318 */ threadCpuTimeNanos()1319 public static long threadCpuTimeNanos() { 1320 return VMDebug.threadCpuTimeNanos(); 1321 } 1322 1323 /** 1324 * Start counting the number and aggregate size of memory allocations. 1325 * 1326 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting. 1327 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis 1328 * code doesn't cause additional allocations. The various <code>get</code> methods return 1329 * the specified value. And the various <code>reset</code> methods reset the specified 1330 * count.</p> 1331 * 1332 * <p>Counts are kept for the system as a whole (global) and for each thread. 1333 * The per-thread counts for threads other than the current thread 1334 * are not cleared by the "reset" or "start" calls.</p> 1335 * 1336 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1337 */ 1338 @Deprecated startAllocCounting()1339 public static void startAllocCounting() { 1340 VMDebug.startAllocCounting(); 1341 } 1342 1343 /** 1344 * Stop counting the number and aggregate size of memory allocations. 1345 * 1346 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1347 */ 1348 @Deprecated stopAllocCounting()1349 public static void stopAllocCounting() { 1350 VMDebug.stopAllocCounting(); 1351 } 1352 1353 /** 1354 * Returns the global count of objects allocated by the runtime between a 1355 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1356 * 1357 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1358 */ 1359 @Deprecated getGlobalAllocCount()1360 public static int getGlobalAllocCount() { 1361 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 1362 } 1363 1364 /** 1365 * Clears the global count of objects allocated. 1366 * @see #getGlobalAllocCount() 1367 * 1368 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1369 */ 1370 @Deprecated resetGlobalAllocCount()1371 public static void resetGlobalAllocCount() { 1372 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 1373 } 1374 1375 /** 1376 * Returns the global size, in bytes, of objects allocated by the runtime between a 1377 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1378 * 1379 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1380 */ 1381 @Deprecated getGlobalAllocSize()1382 public static int getGlobalAllocSize() { 1383 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 1384 } 1385 1386 /** 1387 * Clears the global size of objects allocated. 1388 * @see #getGlobalAllocSize() 1389 * 1390 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1391 */ 1392 @Deprecated resetGlobalAllocSize()1393 public static void resetGlobalAllocSize() { 1394 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 1395 } 1396 1397 /** 1398 * Returns the global count of objects freed by the runtime between a 1399 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1400 * 1401 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1402 */ 1403 @Deprecated getGlobalFreedCount()1404 public static int getGlobalFreedCount() { 1405 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 1406 } 1407 1408 /** 1409 * Clears the global count of objects freed. 1410 * @see #getGlobalFreedCount() 1411 * 1412 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1413 */ 1414 @Deprecated resetGlobalFreedCount()1415 public static void resetGlobalFreedCount() { 1416 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 1417 } 1418 1419 /** 1420 * Returns the global size, in bytes, of objects freed by the runtime between a 1421 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1422 * 1423 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1424 */ 1425 @Deprecated getGlobalFreedSize()1426 public static int getGlobalFreedSize() { 1427 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 1428 } 1429 1430 /** 1431 * Clears the global size of objects freed. 1432 * @see #getGlobalFreedSize() 1433 * 1434 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1435 */ 1436 @Deprecated resetGlobalFreedSize()1437 public static void resetGlobalFreedSize() { 1438 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 1439 } 1440 1441 /** 1442 * Returns the number of non-concurrent GC invocations between a 1443 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1444 * 1445 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1446 */ 1447 @Deprecated getGlobalGcInvocationCount()1448 public static int getGlobalGcInvocationCount() { 1449 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 1450 } 1451 1452 /** 1453 * Clears the count of non-concurrent GC invocations. 1454 * @see #getGlobalGcInvocationCount() 1455 * 1456 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1457 */ 1458 @Deprecated resetGlobalGcInvocationCount()1459 public static void resetGlobalGcInvocationCount() { 1460 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 1461 } 1462 1463 /** 1464 * Returns the number of classes successfully initialized (ie those that executed without 1465 * throwing an exception) between a {@link #startAllocCounting() start} and 1466 * {@link #stopAllocCounting() stop}. 1467 * 1468 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1469 */ 1470 @Deprecated getGlobalClassInitCount()1471 public static int getGlobalClassInitCount() { 1472 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 1473 } 1474 1475 /** 1476 * Clears the count of classes initialized. 1477 * @see #getGlobalClassInitCount() 1478 * 1479 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1480 */ 1481 @Deprecated resetGlobalClassInitCount()1482 public static void resetGlobalClassInitCount() { 1483 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 1484 } 1485 1486 /** 1487 * Returns the time spent successfully initializing classes between a 1488 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1489 * 1490 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1491 */ 1492 @Deprecated getGlobalClassInitTime()1493 public static int getGlobalClassInitTime() { 1494 /* cumulative elapsed time for class initialization, in usec */ 1495 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 1496 } 1497 1498 /** 1499 * Clears the count of time spent initializing classes. 1500 * @see #getGlobalClassInitTime() 1501 * 1502 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1503 */ 1504 @Deprecated resetGlobalClassInitTime()1505 public static void resetGlobalClassInitTime() { 1506 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 1507 } 1508 1509 /** 1510 * This method exists for compatibility and always returns 0. 1511 * @deprecated This method is now obsolete. 1512 */ 1513 @Deprecated getGlobalExternalAllocCount()1514 public static int getGlobalExternalAllocCount() { 1515 return 0; 1516 } 1517 1518 /** 1519 * This method exists for compatibility and has no effect. 1520 * @deprecated This method is now obsolete. 1521 */ 1522 @Deprecated resetGlobalExternalAllocSize()1523 public static void resetGlobalExternalAllocSize() {} 1524 1525 /** 1526 * This method exists for compatibility and has no effect. 1527 * @deprecated This method is now obsolete. 1528 */ 1529 @Deprecated resetGlobalExternalAllocCount()1530 public static void resetGlobalExternalAllocCount() {} 1531 1532 /** 1533 * This method exists for compatibility and always returns 0. 1534 * @deprecated This method is now obsolete. 1535 */ 1536 @Deprecated getGlobalExternalAllocSize()1537 public static int getGlobalExternalAllocSize() { 1538 return 0; 1539 } 1540 1541 /** 1542 * This method exists for compatibility and always returns 0. 1543 * @deprecated This method is now obsolete. 1544 */ 1545 @Deprecated getGlobalExternalFreedCount()1546 public static int getGlobalExternalFreedCount() { 1547 return 0; 1548 } 1549 1550 /** 1551 * This method exists for compatibility and has no effect. 1552 * @deprecated This method is now obsolete. 1553 */ 1554 @Deprecated resetGlobalExternalFreedCount()1555 public static void resetGlobalExternalFreedCount() {} 1556 1557 /** 1558 * This method exists for compatibility and has no effect. 1559 * @deprecated This method is now obsolete. 1560 */ 1561 @Deprecated getGlobalExternalFreedSize()1562 public static int getGlobalExternalFreedSize() { 1563 return 0; 1564 } 1565 1566 /** 1567 * This method exists for compatibility and has no effect. 1568 * @deprecated This method is now obsolete. 1569 */ 1570 @Deprecated resetGlobalExternalFreedSize()1571 public static void resetGlobalExternalFreedSize() {} 1572 1573 /** 1574 * Returns the thread-local count of objects allocated by the runtime between a 1575 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1576 * 1577 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1578 */ 1579 @Deprecated getThreadAllocCount()1580 public static int getThreadAllocCount() { 1581 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 1582 } 1583 1584 /** 1585 * Clears the thread-local count of objects allocated. 1586 * @see #getThreadAllocCount() 1587 * 1588 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1589 */ 1590 @Deprecated resetThreadAllocCount()1591 public static void resetThreadAllocCount() { 1592 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 1593 } 1594 1595 /** 1596 * Returns the thread-local size of objects allocated by the runtime between a 1597 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1598 * @return The allocated size in bytes. 1599 * 1600 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1601 */ 1602 @Deprecated getThreadAllocSize()1603 public static int getThreadAllocSize() { 1604 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 1605 } 1606 1607 /** 1608 * Clears the thread-local count of objects allocated. 1609 * @see #getThreadAllocSize() 1610 * 1611 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1612 */ 1613 @Deprecated resetThreadAllocSize()1614 public static void resetThreadAllocSize() { 1615 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 1616 } 1617 1618 /** 1619 * This method exists for compatibility and has no effect. 1620 * @deprecated This method is now obsolete. 1621 */ 1622 @Deprecated getThreadExternalAllocCount()1623 public static int getThreadExternalAllocCount() { 1624 return 0; 1625 } 1626 1627 /** 1628 * This method exists for compatibility and has no effect. 1629 * @deprecated This method is now obsolete. 1630 */ 1631 @Deprecated resetThreadExternalAllocCount()1632 public static void resetThreadExternalAllocCount() {} 1633 1634 /** 1635 * This method exists for compatibility and has no effect. 1636 * @deprecated This method is now obsolete. 1637 */ 1638 @Deprecated getThreadExternalAllocSize()1639 public static int getThreadExternalAllocSize() { 1640 return 0; 1641 } 1642 1643 /** 1644 * This method exists for compatibility and has no effect. 1645 * @deprecated This method is now obsolete. 1646 */ 1647 @Deprecated resetThreadExternalAllocSize()1648 public static void resetThreadExternalAllocSize() {} 1649 1650 /** 1651 * Returns the number of thread-local non-concurrent GC invocations between a 1652 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. 1653 * 1654 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1655 */ 1656 @Deprecated getThreadGcInvocationCount()1657 public static int getThreadGcInvocationCount() { 1658 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 1659 } 1660 1661 /** 1662 * Clears the thread-local count of non-concurrent GC invocations. 1663 * @see #getThreadGcInvocationCount() 1664 * 1665 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1666 */ 1667 @Deprecated resetThreadGcInvocationCount()1668 public static void resetThreadGcInvocationCount() { 1669 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 1670 } 1671 1672 /** 1673 * Clears all the global and thread-local memory allocation counters. 1674 * @see #startAllocCounting() 1675 * 1676 * @deprecated Accurate counting is a burden on the runtime and may be removed. 1677 */ 1678 @Deprecated resetAllCounts()1679 public static void resetAllCounts() { 1680 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS); 1681 } 1682 1683 /** 1684 * Returns the value of a particular runtime statistic or {@code null} if no 1685 * such runtime statistic exists. 1686 * 1687 * <p>The following table lists the runtime statistics that the runtime supports. 1688 * Note runtime statistics may be added or removed in a future API level.</p> 1689 * 1690 * <table> 1691 * <thead> 1692 * <tr> 1693 * <th>Runtime statistic name</th> 1694 * <th>Meaning</th> 1695 * <th>Example</th> 1696 * <th>Supported (API Levels)</th> 1697 * </tr> 1698 * </thead> 1699 * <tbody> 1700 * <tr> 1701 * <td>art.gc.gc-count</td> 1702 * <td>The number of garbage collection runs.</td> 1703 * <td>{@code 164}</td> 1704 * <td>23</td> 1705 * </tr> 1706 * <tr> 1707 * <td>art.gc.gc-time</td> 1708 * <td>The total duration of garbage collection runs in ms.</td> 1709 * <td>{@code 62364}</td> 1710 * <td>23</td> 1711 * </tr> 1712 * <tr> 1713 * <td>art.gc.bytes-allocated</td> 1714 * <td>The total number of bytes that the application allocated.</td> 1715 * <td>{@code 1463948408}</td> 1716 * <td>23</td> 1717 * </tr> 1718 * <tr> 1719 * <td>art.gc.bytes-freed</td> 1720 * <td>The total number of bytes that garbage collection reclaimed.</td> 1721 * <td>{@code 1313493084}</td> 1722 * <td>23</td> 1723 * </tr> 1724 * <tr> 1725 * <td>art.gc.blocking-gc-count</td> 1726 * <td>The number of blocking garbage collection runs.</td> 1727 * <td>{@code 2}</td> 1728 * <td>23</td> 1729 * </tr> 1730 * <tr> 1731 * <td>art.gc.blocking-gc-time</td> 1732 * <td>The total duration of blocking garbage collection runs in ms.</td> 1733 * <td>{@code 804}</td> 1734 * <td>23</td> 1735 * </tr> 1736 * <tr> 1737 * <td>art.gc.gc-count-rate-histogram</td> 1738 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage 1739 * collection runs that have occurred over the last 10 1740 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate 1741 * samples taken since the process began. The histogram can be used to identify 1742 * instances of high rates of garbage collection runs. For example, a histogram 1743 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time 1744 * there are between 0 and 2 garbage collection runs every 10 seconds, but there 1745 * were 8 distinct 10-second intervals in which 5 garbage collection runs 1746 * occurred.</td> 1747 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td> 1748 * <td>23</td> 1749 * </tr> 1750 * <tr> 1751 * <td>art.gc.blocking-gc-count-rate-histogram</td> 1752 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of 1753 * blocking garbage collection runs that have occurred over the last 10 1754 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the 1755 * blocking-gc-count-rate samples taken since the process began. The histogram 1756 * can be used to identify instances of high rates of blocking garbage 1757 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that 1758 * most of the time there are zero blocking garbage collection runs every 10 1759 * seconds, but there was one 10-second interval in which one blocking garbage 1760 * collection run occurred, and there was one interval in which two blocking 1761 * garbage collection runs occurred.</td> 1762 * <td>{@code 0:99269,1:1,2:1}</td> 1763 * <td>23</td> 1764 * </tr> 1765 * </tbody> 1766 * </table> 1767 * 1768 * @param statName 1769 * the name of the runtime statistic to look up. 1770 * @return the value of the specified runtime statistic or {@code null} if the 1771 * runtime statistic doesn't exist. 1772 */ getRuntimeStat(String statName)1773 public static String getRuntimeStat(String statName) { 1774 return VMDebug.getRuntimeStat(statName); 1775 } 1776 1777 /** 1778 * Returns a map of the names/values of the runtime statistics 1779 * that {@link #getRuntimeStat(String)} supports. 1780 * 1781 * @return a map of the names/values of the supported runtime statistics. 1782 */ getRuntimeStats()1783 public static Map<String, String> getRuntimeStats() { 1784 return VMDebug.getRuntimeStats(); 1785 } 1786 1787 /** 1788 * Returns the size of the native heap. 1789 * @return The size of the native heap in bytes. 1790 */ getNativeHeapSize()1791 public static native long getNativeHeapSize(); 1792 1793 /** 1794 * Returns the amount of allocated memory in the native heap. 1795 * @return The allocated size in bytes. 1796 */ getNativeHeapAllocatedSize()1797 public static native long getNativeHeapAllocatedSize(); 1798 1799 /** 1800 * Returns the amount of free memory in the native heap. 1801 * @return The freed size in bytes. 1802 */ getNativeHeapFreeSize()1803 public static native long getNativeHeapFreeSize(); 1804 1805 /** 1806 * Retrieves information about this processes memory usages. This information is broken down by 1807 * how much is in use by dalvik, the native heap, and everything else. 1808 * 1809 * <p><b>Note:</b> this method directly retrieves memory information for the given process 1810 * from low-level data available to it. It may not be able to retrieve information about 1811 * some protected allocations, such as graphics. If you want to be sure you can see 1812 * all information about allocations by the process, use 1813 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])} instead.</p> 1814 */ getMemoryInfo(MemoryInfo memoryInfo)1815 public static native void getMemoryInfo(MemoryInfo memoryInfo); 1816 1817 /** 1818 * Note: currently only works when the requested pid has the same UID 1819 * as the caller. 1820 * @hide 1821 */ 1822 @UnsupportedAppUsage getMemoryInfo(int pid, MemoryInfo memoryInfo)1823 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo); 1824 1825 /** 1826 * Retrieves the PSS memory used by the process as given by the 1827 * smaps. 1828 */ getPss()1829 public static native long getPss(); 1830 1831 /** 1832 * Retrieves the PSS memory used by the process as given by the smaps. Optionally supply a long 1833 * array of up to 3 entries to also receive (up to 3 values in order): the Uss and SwapPss and 1834 * Rss (only filled in as of {@link android.os.Build.VERSION_CODES#P}) of the process, and 1835 * another array to also retrieve the separate memtrack size. 1836 * @hide 1837 */ getPss(int pid, long[] outUssSwapPssRss, long[] outMemtrack)1838 public static native long getPss(int pid, long[] outUssSwapPssRss, long[] outMemtrack); 1839 1840 /** @hide */ 1841 public static final int MEMINFO_TOTAL = 0; 1842 /** @hide */ 1843 public static final int MEMINFO_FREE = 1; 1844 /** @hide */ 1845 public static final int MEMINFO_BUFFERS = 2; 1846 /** @hide */ 1847 public static final int MEMINFO_CACHED = 3; 1848 /** @hide */ 1849 public static final int MEMINFO_SHMEM = 4; 1850 /** @hide */ 1851 public static final int MEMINFO_SLAB = 5; 1852 /** @hide */ 1853 public static final int MEMINFO_SLAB_RECLAIMABLE = 6; 1854 /** @hide */ 1855 public static final int MEMINFO_SLAB_UNRECLAIMABLE = 7; 1856 /** @hide */ 1857 public static final int MEMINFO_SWAP_TOTAL = 8; 1858 /** @hide */ 1859 public static final int MEMINFO_SWAP_FREE = 9; 1860 /** @hide */ 1861 public static final int MEMINFO_ZRAM_TOTAL = 10; 1862 /** @hide */ 1863 public static final int MEMINFO_MAPPED = 11; 1864 /** @hide */ 1865 public static final int MEMINFO_VM_ALLOC_USED = 12; 1866 /** @hide */ 1867 public static final int MEMINFO_PAGE_TABLES = 13; 1868 /** @hide */ 1869 public static final int MEMINFO_KERNEL_STACK = 14; 1870 /** @hide */ 1871 public static final int MEMINFO_COUNT = 15; 1872 1873 /** 1874 * Retrieves /proc/meminfo. outSizes is filled with fields 1875 * as defined by MEMINFO_* offsets. 1876 * @hide 1877 */ 1878 @UnsupportedAppUsage getMemInfo(long[] outSizes)1879 public static native void getMemInfo(long[] outSizes); 1880 1881 /** 1882 * Establish an object allocation limit in the current thread. 1883 * This feature was never enabled in release builds. The 1884 * allocation limits feature was removed in Honeycomb. This 1885 * method exists for compatibility and always returns -1 and has 1886 * no effect. 1887 * 1888 * @deprecated This method is now obsolete. 1889 */ 1890 @Deprecated setAllocationLimit(int limit)1891 public static int setAllocationLimit(int limit) { 1892 return -1; 1893 } 1894 1895 /** 1896 * Establish a global object allocation limit. This feature was 1897 * never enabled in release builds. The allocation limits feature 1898 * was removed in Honeycomb. This method exists for compatibility 1899 * and always returns -1 and has no effect. 1900 * 1901 * @deprecated This method is now obsolete. 1902 */ 1903 @Deprecated setGlobalAllocationLimit(int limit)1904 public static int setGlobalAllocationLimit(int limit) { 1905 return -1; 1906 } 1907 1908 /** 1909 * Dump a list of all currently loaded class to the log file. 1910 * 1911 * @param flags See constants above. 1912 */ printLoadedClasses(int flags)1913 public static void printLoadedClasses(int flags) { 1914 VMDebug.printLoadedClasses(flags); 1915 } 1916 1917 /** 1918 * Get the number of loaded classes. 1919 * @return the number of loaded classes. 1920 */ getLoadedClassCount()1921 public static int getLoadedClassCount() { 1922 return VMDebug.getLoadedClassCount(); 1923 } 1924 1925 /** 1926 * Dump "hprof" data to the specified file. This may cause a GC. 1927 * 1928 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). 1929 * @throws UnsupportedOperationException if the VM was built without 1930 * HPROF support. 1931 * @throws IOException if an error occurs while opening or writing files. 1932 */ dumpHprofData(String fileName)1933 public static void dumpHprofData(String fileName) throws IOException { 1934 VMDebug.dumpHprofData(fileName); 1935 } 1936 1937 /** 1938 * Like dumpHprofData(String), but takes an already-opened 1939 * FileDescriptor to which the trace is written. The file name is also 1940 * supplied simply for logging. Makes a dup of the file descriptor. 1941 * 1942 * Primarily for use by the "am" shell command. 1943 * 1944 * @hide 1945 */ dumpHprofData(String fileName, FileDescriptor fd)1946 public static void dumpHprofData(String fileName, FileDescriptor fd) 1947 throws IOException { 1948 VMDebug.dumpHprofData(fileName, fd); 1949 } 1950 1951 /** 1952 * Collect "hprof" and send it to DDMS. This may cause a GC. 1953 * 1954 * @throws UnsupportedOperationException if the VM was built without 1955 * HPROF support. 1956 * @hide 1957 */ dumpHprofDataDdms()1958 public static void dumpHprofDataDdms() { 1959 VMDebug.dumpHprofDataDdms(); 1960 } 1961 1962 /** 1963 * Writes native heap data to the specified file descriptor. 1964 * 1965 * @hide 1966 */ 1967 @UnsupportedAppUsage dumpNativeHeap(FileDescriptor fd)1968 public static native void dumpNativeHeap(FileDescriptor fd); 1969 1970 /** 1971 * Writes malloc info data to the specified file descriptor. 1972 * 1973 * @hide 1974 */ dumpNativeMallocInfo(FileDescriptor fd)1975 public static native void dumpNativeMallocInfo(FileDescriptor fd); 1976 1977 /** 1978 * Returns a count of the extant instances of a class. 1979 * 1980 * @hide 1981 */ 1982 @UnsupportedAppUsage countInstancesOfClass(Class cls)1983 public static long countInstancesOfClass(Class cls) { 1984 return VMDebug.countInstancesOfClass(cls, true); 1985 } 1986 1987 /** 1988 * Returns the number of sent transactions from this process. 1989 * @return The number of sent transactions or -1 if it could not read t. 1990 */ getBinderSentTransactions()1991 public static native int getBinderSentTransactions(); 1992 1993 /** 1994 * Returns the number of received transactions from the binder driver. 1995 * @return The number of received transactions or -1 if it could not read the stats. 1996 */ getBinderReceivedTransactions()1997 public static native int getBinderReceivedTransactions(); 1998 1999 /** 2000 * Returns the number of active local Binder objects that exist in the 2001 * current process. 2002 */ getBinderLocalObjectCount()2003 public static final native int getBinderLocalObjectCount(); 2004 2005 /** 2006 * Returns the number of references to remote proxy Binder objects that 2007 * exist in the current process. 2008 */ getBinderProxyObjectCount()2009 public static final native int getBinderProxyObjectCount(); 2010 2011 /** 2012 * Returns the number of death notification links to Binder objects that 2013 * exist in the current process. 2014 */ getBinderDeathObjectCount()2015 public static final native int getBinderDeathObjectCount(); 2016 2017 /** 2018 * Primes the register map cache. 2019 * 2020 * Only works for classes in the bootstrap class loader. Does not 2021 * cause classes to be loaded if they're not already present. 2022 * 2023 * The classAndMethodDesc argument is a concatentation of the VM-internal 2024 * class descriptor, method name, and method descriptor. Examples: 2025 * Landroid/os/Looper;.loop:()V 2026 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V 2027 * 2028 * @param classAndMethodDesc the method to prepare 2029 * 2030 * @hide 2031 */ cacheRegisterMap(String classAndMethodDesc)2032 public static final boolean cacheRegisterMap(String classAndMethodDesc) { 2033 return VMDebug.cacheRegisterMap(classAndMethodDesc); 2034 } 2035 2036 /** 2037 * Dumps the contents of VM reference tables (e.g. JNI locals and 2038 * globals) to the log file. 2039 * 2040 * @hide 2041 */ 2042 @UnsupportedAppUsage dumpReferenceTables()2043 public static final void dumpReferenceTables() { 2044 VMDebug.dumpReferenceTables(); 2045 } 2046 2047 /** 2048 * API for gathering and querying instruction counts. 2049 * 2050 * Example usage: 2051 * <pre> 2052 * Debug.InstructionCount icount = new Debug.InstructionCount(); 2053 * icount.resetAndStart(); 2054 * [... do lots of stuff ...] 2055 * if (icount.collect()) { 2056 * System.out.println("Total instructions executed: " 2057 * + icount.globalTotal()); 2058 * System.out.println("Method invocations: " 2059 * + icount.globalMethodInvocations()); 2060 * } 2061 * </pre> 2062 * 2063 * @deprecated Instruction counting is no longer supported. 2064 */ 2065 @Deprecated 2066 public static class InstructionCount { InstructionCount()2067 public InstructionCount() { 2068 } 2069 2070 /** 2071 * Reset counters and ensure counts are running. Counts may 2072 * have already been running. 2073 * 2074 * @return true if counting was started 2075 */ resetAndStart()2076 public boolean resetAndStart() { 2077 return false; 2078 } 2079 2080 /** 2081 * Collect instruction counts. May or may not stop the 2082 * counting process. 2083 */ collect()2084 public boolean collect() { 2085 return false; 2086 } 2087 2088 /** 2089 * Return the total number of instructions executed globally (i.e. in 2090 * all threads). 2091 */ globalTotal()2092 public int globalTotal() { 2093 return 0; 2094 } 2095 2096 /** 2097 * Return the total number of method-invocation instructions 2098 * executed globally. 2099 */ globalMethodInvocations()2100 public int globalMethodInvocations() { 2101 return 0; 2102 } 2103 } 2104 2105 /** 2106 * A Map of typed debug properties. 2107 */ 2108 private static final TypedProperties debugProperties; 2109 2110 /* 2111 * Load the debug properties from the standard files into debugProperties. 2112 */ 2113 static { 2114 if (false) { 2115 final String TAG = "DebugProperties"; 2116 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; 2117 final TypedProperties tp = new TypedProperties(); 2118 2119 // Read the properties from each of the files, if present. 2120 for (String file : files) { 2121 Reader r; 2122 try { 2123 r = new FileReader(file); 2124 } catch (FileNotFoundException ex) { 2125 // It's ok if a file is missing. 2126 continue; 2127 } 2128 2129 try { 2130 tp.load(r); 2131 } catch (Exception ex) { 2132 throw new RuntimeException("Problem loading " + file, ex); 2133 } finally { 2134 try { r.close()2135 r.close(); 2136 } catch (IOException ex) { 2137 // Ignore this error. 2138 } 2139 } 2140 } 2141 2142 debugProperties = tp.isEmpty() ? null : tp; 2143 } else { 2144 debugProperties = null; 2145 } 2146 } 2147 2148 2149 /** 2150 * Returns true if the type of the field matches the specified class. 2151 * Handles the case where the class is, e.g., java.lang.Boolean, but 2152 * the field is of the primitive "boolean" type. Also handles all of 2153 * the java.lang.Number subclasses. 2154 */ fieldTypeMatches(Field field, Class<?> cl)2155 private static boolean fieldTypeMatches(Field field, Class<?> cl) { 2156 Class<?> fieldClass = field.getType(); 2157 if (fieldClass == cl) { 2158 return true; 2159 } 2160 Field primitiveTypeField; 2161 try { 2162 /* All of the classes we care about (Boolean, Integer, etc.) 2163 * have a Class field called "TYPE" that points to the corresponding 2164 * primitive class. 2165 */ 2166 primitiveTypeField = cl.getField("TYPE"); 2167 } catch (NoSuchFieldException ex) { 2168 return false; 2169 } 2170 try { 2171 return fieldClass == (Class<?>) primitiveTypeField.get(null); 2172 } catch (IllegalAccessException ex) { 2173 return false; 2174 } 2175 } 2176 2177 2178 /** 2179 * Looks up the property that corresponds to the field, and sets the field's value 2180 * if the types match. 2181 */ modifyFieldIfSet(final Field field, final TypedProperties properties, final String propertyName)2182 private static void modifyFieldIfSet(final Field field, final TypedProperties properties, 2183 final String propertyName) { 2184 if (field.getType() == java.lang.String.class) { 2185 int stringInfo = properties.getStringInfo(propertyName); 2186 switch (stringInfo) { 2187 case TypedProperties.STRING_SET: 2188 // Handle as usual below. 2189 break; 2190 case TypedProperties.STRING_NULL: 2191 try { 2192 field.set(null, null); // null object for static fields; null string 2193 } catch (IllegalAccessException ex) { 2194 throw new IllegalArgumentException( 2195 "Cannot set field for " + propertyName, ex); 2196 } 2197 return; 2198 case TypedProperties.STRING_NOT_SET: 2199 return; 2200 case TypedProperties.STRING_TYPE_MISMATCH: 2201 throw new IllegalArgumentException( 2202 "Type of " + propertyName + " " + 2203 " does not match field type (" + field.getType() + ")"); 2204 default: 2205 throw new IllegalStateException( 2206 "Unexpected getStringInfo(" + propertyName + ") return value " + 2207 stringInfo); 2208 } 2209 } 2210 Object value = properties.get(propertyName); 2211 if (value != null) { 2212 if (!fieldTypeMatches(field, value.getClass())) { 2213 throw new IllegalArgumentException( 2214 "Type of " + propertyName + " (" + value.getClass() + ") " + 2215 " does not match field type (" + field.getType() + ")"); 2216 } 2217 try { 2218 field.set(null, value); // null object for static fields 2219 } catch (IllegalAccessException ex) { 2220 throw new IllegalArgumentException( 2221 "Cannot set field for " + propertyName, ex); 2222 } 2223 } 2224 } 2225 2226 2227 /** 2228 * Equivalent to <code>setFieldsOn(cl, false)</code>. 2229 * 2230 * @see #setFieldsOn(Class, boolean) 2231 * 2232 * @hide 2233 */ setFieldsOn(Class<?> cl)2234 public static void setFieldsOn(Class<?> cl) { 2235 setFieldsOn(cl, false); 2236 } 2237 2238 /** 2239 * Reflectively sets static fields of a class based on internal debugging 2240 * properties. This method is a no-op if false is 2241 * false. 2242 * <p> 2243 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will 2244 * always be false in release builds. This API is typically only useful 2245 * for platform developers. 2246 * </p> 2247 * Class setup: define a class whose only fields are non-final, static 2248 * primitive types (except for "char") or Strings. In a static block 2249 * after the field definitions/initializations, pass the class to 2250 * this method, Debug.setFieldsOn(). Example: 2251 * <pre> 2252 * package com.example; 2253 * 2254 * import android.os.Debug; 2255 * 2256 * public class MyDebugVars { 2257 * public static String s = "a string"; 2258 * public static String s2 = "second string"; 2259 * public static String ns = null; 2260 * public static boolean b = false; 2261 * public static int i = 5; 2262 * @Debug.DebugProperty 2263 * public static float f = 0.1f; 2264 * @@Debug.DebugProperty 2265 * public static double d = 0.5d; 2266 * 2267 * // This MUST appear AFTER all fields are defined and initialized! 2268 * static { 2269 * // Sets all the fields 2270 * Debug.setFieldsOn(MyDebugVars.class); 2271 * 2272 * // Sets only the fields annotated with @Debug.DebugProperty 2273 * // Debug.setFieldsOn(MyDebugVars.class, true); 2274 * } 2275 * } 2276 * </pre> 2277 * setFieldsOn() may override the value of any field in the class based 2278 * on internal properties that are fixed at boot time. 2279 * <p> 2280 * These properties are only set during platform debugging, and are not 2281 * meant to be used as a general-purpose properties store. 2282 * 2283 * {@hide} 2284 * 2285 * @param cl The class to (possibly) modify 2286 * @param partial If false, sets all static fields, otherwise, only set 2287 * fields with the {@link android.os.Debug.DebugProperty} 2288 * annotation 2289 * @throws IllegalArgumentException if any fields are final or non-static, 2290 * or if the type of the field does not match the type of 2291 * the internal debugging property value. 2292 */ setFieldsOn(Class<?> cl, boolean partial)2293 public static void setFieldsOn(Class<?> cl, boolean partial) { 2294 if (false) { 2295 if (debugProperties != null) { 2296 /* Only look for fields declared directly by the class, 2297 * so we don't mysteriously change static fields in superclasses. 2298 */ 2299 for (Field field : cl.getDeclaredFields()) { 2300 if (!partial || field.getAnnotation(DebugProperty.class) != null) { 2301 final String propertyName = cl.getName() + "." + field.getName(); 2302 boolean isStatic = Modifier.isStatic(field.getModifiers()); 2303 boolean isFinal = Modifier.isFinal(field.getModifiers()); 2304 2305 if (!isStatic || isFinal) { 2306 throw new IllegalArgumentException(propertyName + 2307 " must be static and non-final"); 2308 } 2309 modifyFieldIfSet(field, debugProperties, propertyName); 2310 } 2311 } 2312 } 2313 } else { 2314 Log.wtf(TAG, 2315 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + 2316 ") called in non-DEBUG build"); 2317 } 2318 } 2319 2320 /** 2321 * Annotation to put on fields you want to set with 2322 * {@link Debug#setFieldsOn(Class, boolean)}. 2323 * 2324 * @hide 2325 */ 2326 @Target({ ElementType.FIELD }) 2327 @Retention(RetentionPolicy.RUNTIME) 2328 public @interface DebugProperty { 2329 } 2330 2331 /** 2332 * Get a debugging dump of a system service by name. 2333 * 2334 * <p>Most services require the caller to hold android.permission.DUMP. 2335 * 2336 * @param name of the service to dump 2337 * @param fd to write dump output to (usually an output log file) 2338 * @param args to pass to the service's dump method, may be null 2339 * @return true if the service was dumped successfully, false if 2340 * the service could not be found or had an error while dumping 2341 */ dumpService(String name, FileDescriptor fd, String[] args)2342 public static boolean dumpService(String name, FileDescriptor fd, String[] args) { 2343 IBinder service = ServiceManager.getService(name); 2344 if (service == null) { 2345 Log.e(TAG, "Can't find service to dump: " + name); 2346 return false; 2347 } 2348 2349 try { 2350 service.dump(fd, args); 2351 return true; 2352 } catch (RemoteException e) { 2353 Log.e(TAG, "Can't dump service: " + name, e); 2354 return false; 2355 } 2356 } 2357 2358 /** 2359 * Append the Java stack traces of a given native process to a specified file. 2360 * 2361 * @param pid pid to dump. 2362 * @param file path of file to append dump to. 2363 * @param timeoutSecs time to wait in seconds, or 0 to wait forever. 2364 * @hide 2365 */ dumpJavaBacktraceToFileTimeout(int pid, String file, int timeoutSecs)2366 public static native boolean dumpJavaBacktraceToFileTimeout(int pid, String file, 2367 int timeoutSecs); 2368 2369 /** 2370 * Append the native stack traces of a given process to a specified file. 2371 * 2372 * @param pid pid to dump. 2373 * @param file path of file to append dump to. 2374 * @param timeoutSecs time to wait in seconds, or 0 to wait forever. 2375 * @hide 2376 */ dumpNativeBacktraceToFileTimeout(int pid, String file, int timeoutSecs)2377 public static native boolean dumpNativeBacktraceToFileTimeout(int pid, String file, 2378 int timeoutSecs); 2379 2380 /** 2381 * Get description of unreachable native memory. 2382 * @param limit the number of leaks to provide info on, 0 to only get a summary. 2383 * @param contents true to include a hex dump of the contents of unreachable memory. 2384 * @return the String containing a description of unreachable memory. 2385 * @hide */ getUnreachableMemory(int limit, boolean contents)2386 public static native String getUnreachableMemory(int limit, boolean contents); 2387 2388 /** 2389 * Return a String describing the calling method and location at a particular stack depth. 2390 * @param callStack the Thread stack 2391 * @param depth the depth of stack to return information for. 2392 * @return the String describing the caller at that depth. 2393 */ getCaller(StackTraceElement callStack[], int depth)2394 private static String getCaller(StackTraceElement callStack[], int depth) { 2395 // callStack[4] is the caller of the method that called getCallers() 2396 if (4 + depth >= callStack.length) { 2397 return "<bottom of call stack>"; 2398 } 2399 StackTraceElement caller = callStack[4 + depth]; 2400 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber(); 2401 } 2402 2403 /** 2404 * Return a string consisting of methods and locations at multiple call stack levels. 2405 * @param depth the number of levels to return, starting with the immediate caller. 2406 * @return a string describing the call stack. 2407 * {@hide} 2408 */ 2409 @UnsupportedAppUsage getCallers(final int depth)2410 public static String getCallers(final int depth) { 2411 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 2412 StringBuffer sb = new StringBuffer(); 2413 for (int i = 0; i < depth; i++) { 2414 sb.append(getCaller(callStack, i)).append(" "); 2415 } 2416 return sb.toString(); 2417 } 2418 2419 /** 2420 * Return a string consisting of methods and locations at multiple call stack levels. 2421 * @param depth the number of levels to return, starting with the immediate caller. 2422 * @return a string describing the call stack. 2423 * {@hide} 2424 */ getCallers(final int start, int depth)2425 public static String getCallers(final int start, int depth) { 2426 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 2427 StringBuffer sb = new StringBuffer(); 2428 depth += start; 2429 for (int i = start; i < depth; i++) { 2430 sb.append(getCaller(callStack, i)).append(" "); 2431 } 2432 return sb.toString(); 2433 } 2434 2435 /** 2436 * Like {@link #getCallers(int)}, but each location is append to the string 2437 * as a new line with <var>linePrefix</var> in front of it. 2438 * @param depth the number of levels to return, starting with the immediate caller. 2439 * @param linePrefix prefix to put in front of each location. 2440 * @return a string describing the call stack. 2441 * {@hide} 2442 */ getCallers(final int depth, String linePrefix)2443 public static String getCallers(final int depth, String linePrefix) { 2444 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 2445 StringBuffer sb = new StringBuffer(); 2446 for (int i = 0; i < depth; i++) { 2447 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n"); 2448 } 2449 return sb.toString(); 2450 } 2451 2452 /** 2453 * @return a String describing the immediate caller of the calling method. 2454 * {@hide} 2455 */ 2456 @UnsupportedAppUsage getCaller()2457 public static String getCaller() { 2458 return getCaller(Thread.currentThread().getStackTrace(), 0); 2459 } 2460 2461 /** 2462 * Attach a library as a jvmti agent to the current runtime, with the given classloader 2463 * determining the library search path. 2464 * <p> 2465 * Note: agents may only be attached to debuggable apps. Otherwise, this function will 2466 * throw a SecurityException. 2467 * 2468 * @param library the library containing the agent. 2469 * @param options the options passed to the agent. 2470 * @param classLoader the classloader determining the library search path. 2471 * 2472 * @throws IOException if the agent could not be attached. 2473 * @throws SecurityException if the app is not debuggable. 2474 */ attachJvmtiAgent(@onNull String library, @Nullable String options, @Nullable ClassLoader classLoader)2475 public static void attachJvmtiAgent(@NonNull String library, @Nullable String options, 2476 @Nullable ClassLoader classLoader) throws IOException { 2477 Preconditions.checkNotNull(library); 2478 Preconditions.checkArgument(!library.contains("=")); 2479 2480 if (options == null) { 2481 VMDebug.attachAgent(library, classLoader); 2482 } else { 2483 VMDebug.attachAgent(library + "=" + options, classLoader); 2484 } 2485 } 2486 2487 /** 2488 * Return the current free ZRAM usage in kilobytes. 2489 * 2490 * @hide 2491 */ getZramFreeKb()2492 public static native long getZramFreeKb(); 2493 } 2494