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