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