blob: f3822413add3d3278371d728fc0c22787596a043 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.os;
18
Dianne Hackborn8c841092013-06-24 13:46:13 -070019import com.android.internal.util.FastPrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070020import com.android.internal.util.TypedProperties;
21
Dave Bort1ce5bd32009-04-22 17:36:56 -070022import android.util.Log;
23
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070024import java.io.FileDescriptor;
Dave Bort1ce5bd32009-04-22 17:36:56 -070025import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import java.io.FileOutputStream;
Dave Bort1ce5bd32009-04-22 17:36:56 -070027import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.io.PrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070030import java.io.Reader;
31import java.lang.reflect.Field;
32import java.lang.reflect.Modifier;
Romain Guyc4b11a72009-05-13 15:46:37 -070033import java.lang.annotation.Target;
34import java.lang.annotation.ElementType;
35import java.lang.annotation.Retention;
36import java.lang.annotation.RetentionPolicy;
Richard Uhler350e6dc2015-05-18 10:48:52 -070037import java.util.HashMap;
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -070038import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40import org.apache.harmony.dalvik.ddmc.Chunk;
41import org.apache.harmony.dalvik.ddmc.ChunkHandler;
42import org.apache.harmony.dalvik.ddmc.DdmServer;
43
Dan Bornstein9f315542010-11-19 18:12:55 -080044import dalvik.bytecode.OpcodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import dalvik.system.VMDebug;
46
47
48/**
Ian Rogersfe067a42013-02-22 19:59:23 -080049 * Provides various debugging methods for Android applications, including
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 * tracing and allocation counts.
51 * <p><strong>Logging Trace Files</strong></p>
52 * <p>Debug can create log files that give details about an application, such as
53 * a call stack and start/stop times for any running methods. See <a
54href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
55 * information about reading trace files. To start logging trace files, call one
56 * of the startMethodTracing() methods. To stop tracing, call
57 * {@link #stopMethodTracing()}.
58 */
59public final class Debug
60{
Dan Egnor3eda9792010-03-05 13:28:36 -080061 private static final String TAG = "Debug";
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 /**
64 * Flags for startMethodTracing(). These can be ORed together.
65 *
66 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
67 * trace key file.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080068 *
69 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080071 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS;
73
74 /**
75 * Flags for printLoadedClasses(). Default behavior is to only show
76 * the class name.
77 */
78 public static final int SHOW_FULL_DETAIL = 1;
79 public static final int SHOW_CLASSLOADER = (1 << 1);
80 public static final int SHOW_INITIALIZED = (1 << 2);
81
82 // set/cleared by waitForDebugger()
83 private static volatile boolean mWaiting = false;
84
85 private Debug() {}
86
87 /*
88 * How long to wait for the debugger to finish sending requests. I've
89 * seen this hit 800msec on the device while waiting for a response
90 * to travel over USB and get processed, so we take that and add
91 * half a second.
92 */
93 private static final int MIN_DEBUGGER_IDLE = 1300; // msec
94
95 /* how long to sleep when polling for activity */
96 private static final int SPIN_DELAY = 200; // msec
97
98 /**
99 * Default trace file path and file
100 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private static final String DEFAULT_TRACE_BODY = "dmtrace";
102 private static final String DEFAULT_TRACE_EXTENSION = ".trace";
Andreas Gampe8b9f4942016-02-25 18:03:03 -0800103 private static class NoPreloadHolder {
104 private static final String DEFAULT_TRACE_PATH_PREFIX =
105 Environment.getLegacyExternalStorageDirectory().getPath() + "/";
106 private static final String DEFAULT_TRACE_FILE_PATH =
107 DEFAULT_TRACE_PATH_PREFIX + DEFAULT_TRACE_BODY
108 + DEFAULT_TRACE_EXTENSION;
109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111
112 /**
113 * This class is used to retrieved various statistics about the memory mappings for this
justinmuller64551382013-10-16 22:06:55 -0600114 * process. The returned info is broken down by dalvik, native, and other. All results are in kB.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 */
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700116 public static class MemoryInfo implements Parcelable {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700117 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public int dalvikPss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700119 /** The proportional set size that is swappable for dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700120 /** @hide We may want to expose this, eventually. */
121 public int dalvikSwappablePss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700122 /** The private dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public int dalvikPrivateDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700124 /** The shared dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public int dalvikSharedDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700126 /** The private clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700127 /** @hide We may want to expose this, eventually. */
128 public int dalvikPrivateClean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700129 /** The shared clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700130 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700131 public int dalvikSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700132 /** The dirty dalvik pages that have been swapped out. */
133 /** @hide We may want to expose this, eventually. */
134 public int dalvikSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800135 /** The dirty dalvik pages that have been swapped out, proportional. */
136 /** @hide We may want to expose this, eventually. */
137 public int dalvikSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
139 /** The proportional set size for the native heap. */
140 public int nativePss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700141 /** The proportional set size that is swappable for the native heap. */
142 /** @hide We may want to expose this, eventually. */
143 public int nativeSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 /** The private dirty pages used by the native heap. */
145 public int nativePrivateDirty;
146 /** The shared dirty pages used by the native heap. */
147 public int nativeSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700148 /** The private clean pages used by the native heap. */
149 /** @hide We may want to expose this, eventually. */
150 public int nativePrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700151 /** The shared clean pages used by the native heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700152 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700153 public int nativeSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700154 /** The dirty native pages that have been swapped out. */
155 /** @hide We may want to expose this, eventually. */
156 public int nativeSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800157 /** The dirty native pages that have been swapped out, proportional. */
158 /** @hide We may want to expose this, eventually. */
159 public int nativeSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 /** The proportional set size for everything else. */
162 public int otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700163 /** The proportional set size that is swappable for everything else. */
164 /** @hide We may want to expose this, eventually. */
165 public int otherSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /** The private dirty pages used by everything else. */
167 public int otherPrivateDirty;
168 /** The shared dirty pages used by everything else. */
169 public int otherSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700170 /** The private clean pages used by everything else. */
171 /** @hide We may want to expose this, eventually. */
172 public int otherPrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700173 /** The shared clean pages used by everything else. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700174 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700175 public int otherSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700176 /** The dirty pages used by anyting else that have been swapped out. */
177 /** @hide We may want to expose this, eventually. */
178 public int otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800179 /** The dirty pages used by anyting else that have been swapped out, proportional. */
180 /** @hide We may want to expose this, eventually. */
181 public int otherSwappedOutPss;
182
183 /** Whether the kernel reports proportional swap usage */
184 /** @hide */
185 public boolean hasSwappedOutPss;
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200186
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700187 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700188 public static final int HEAP_UNKNOWN = 0;
189 /** @hide */
190 public static final int HEAP_DALVIK = 1;
191 /** @hide */
192 public static final int HEAP_NATIVE = 2;
193
194 /** @hide */
195 public static final int OTHER_DALVIK_OTHER = 0;
196 /** @hide */
197 public static final int OTHER_STACK = 1;
198 /** @hide */
199 public static final int OTHER_CURSOR = 2;
200 /** @hide */
201 public static final int OTHER_ASHMEM = 3;
202 /** @hide */
203 public static final int OTHER_GL_DEV = 4;
204 /** @hide */
205 public static final int OTHER_UNKNOWN_DEV = 5;
206 /** @hide */
207 public static final int OTHER_SO = 6;
208 /** @hide */
209 public static final int OTHER_JAR = 7;
210 /** @hide */
211 public static final int OTHER_APK = 8;
212 /** @hide */
213 public static final int OTHER_TTF = 9;
214 /** @hide */
215 public static final int OTHER_DEX = 10;
216 /** @hide */
217 public static final int OTHER_OAT = 11;
218 /** @hide */
219 public static final int OTHER_ART = 12;
220 /** @hide */
221 public static final int OTHER_UNKNOWN_MAP = 13;
222 /** @hide */
223 public static final int OTHER_GRAPHICS = 14;
224 /** @hide */
225 public static final int OTHER_GL = 15;
226 /** @hide */
227 public static final int OTHER_OTHER_MEMTRACK = 16;
228
229 /** @hide */
230 public static final int OTHER_DALVIK_NORMAL = 17;
231 /** @hide */
232 public static final int OTHER_DALVIK_LARGE = 18;
233 /** @hide */
234 public static final int OTHER_DALVIK_LINEARALLOC = 19;
235 /** @hide */
236 public static final int OTHER_DALVIK_ACCOUNTING = 20;
237 /** @hide */
238 public static final int OTHER_DALVIK_CODE_CACHE = 21;
239 /** @hide */
240 public static final int OTHER_DALVIK_ZYGOTE = 22;
241 /** @hide */
242 public static final int OTHER_DALVIK_NON_MOVING = 23;
243 /** @hide */
244 public static final int OTHER_DALVIK_INDIRECT_REFERENCE_TABLE = 24;
245
246 /** @hide */
Dianne Hackborn18429302014-11-03 13:58:11 -0800247 public static final int NUM_OTHER_STATS = 17;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700248
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700249 /** @hide */
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -0800250 public static final int NUM_DVK_STATS = 8;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700251
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700252 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800253 public static final int NUM_CATEGORIES = 8;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700254
255 /** @hide */
256 public static final int offsetPss = 0;
257 /** @hide */
258 public static final int offsetSwappablePss = 1;
259 /** @hide */
260 public static final int offsetPrivateDirty = 2;
261 /** @hide */
262 public static final int offsetSharedDirty = 3;
263 /** @hide */
264 public static final int offsetPrivateClean = 4;
265 /** @hide */
266 public static final int offsetSharedClean = 5;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700267 /** @hide */
268 public static final int offsetSwappedOut = 6;
Martijn Coenene0764852016-01-07 17:04:22 -0800269 /** @hide */
270 public static final int offsetSwappedOutPss = 7;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700271
272 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700273
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700274 public MemoryInfo() {
275 }
276
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700277 /**
278 * Return total PSS memory usage in kB.
279 */
280 public int getTotalPss() {
Martijn Coenene0764852016-01-07 17:04:22 -0800281 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700282 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200283
Dianne Hackbornc8230512013-07-13 21:32:12 -0700284 /**
285 * @hide Return total PSS memory usage in kB.
286 */
287 public int getTotalUss() {
288 return dalvikPrivateClean + dalvikPrivateDirty
289 + nativePrivateClean + nativePrivateDirty
290 + otherPrivateClean + otherPrivateDirty;
291 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700292
293 /**
Martijn Coenene0764852016-01-07 17:04:22 -0800294 * Return total PSS memory usage in kB mapping a file of one of the following extension:
295 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art .
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700296 */
297 public int getTotalSwappablePss() {
298 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss;
299 }
300
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700301 /**
302 * Return total private dirty memory usage in kB.
303 */
304 public int getTotalPrivateDirty() {
305 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty;
306 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200307
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700308 /**
309 * Return total shared dirty memory usage in kB.
310 */
311 public int getTotalSharedDirty() {
312 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty;
313 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200314
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700315 /**
316 * Return total shared clean memory usage in kB.
317 */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700318 public int getTotalPrivateClean() {
319 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean;
320 }
321
322 /**
323 * Return total shared clean memory usage in kB.
324 */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700325 public int getTotalSharedClean() {
326 return dalvikSharedClean + nativeSharedClean + otherSharedClean;
327 }
328
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700329 /**
330 * Return total swapped out memory in kB.
331 * @hide
332 */
333 public int getTotalSwappedOut() {
334 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut;
335 }
336
Martijn Coenene0764852016-01-07 17:04:22 -0800337 /**
338 * Return total swapped out memory in kB, proportional.
339 * @hide
340 */
341 public int getTotalSwappedOutPss() {
342 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss;
343 }
344
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700345 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700346 public int getOtherPss(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700347 return otherStats[which*NUM_CATEGORIES + offsetPss];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700348 }
349
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700350
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700351 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700352 public int getOtherSwappablePss(int which) {
353 return otherStats[which*NUM_CATEGORIES + offsetSwappablePss];
354 }
355
356
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700357 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700358 public int getOtherPrivateDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700359 return otherStats[which*NUM_CATEGORIES + offsetPrivateDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700360 }
361
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700362 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700363 public int getOtherSharedDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700364 return otherStats[which*NUM_CATEGORIES + offsetSharedDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700365 }
366
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700367 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700368 public int getOtherPrivateClean(int which) {
369 return otherStats[which*NUM_CATEGORIES + offsetPrivateClean];
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700370 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700371
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700372 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700373 public int getOtherPrivate(int which) {
374 return getOtherPrivateClean(which) + getOtherPrivateDirty(which);
375 }
376
377 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700378 public int getOtherSharedClean(int which) {
379 return otherStats[which*NUM_CATEGORIES + offsetSharedClean];
380 }
381
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700382 /** @hide */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700383 public int getOtherSwappedOut(int which) {
384 return otherStats[which*NUM_CATEGORIES + offsetSwappedOut];
385 }
386
387 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800388 public int getOtherSwappedOutPss(int which) {
389 return otherStats[which*NUM_CATEGORIES + offsetSwappedOutPss];
390 }
391
392 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700393 public static String getOtherLabel(int which) {
394 switch (which) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700395 case OTHER_DALVIK_OTHER: return "Dalvik Other";
396 case OTHER_STACK: return "Stack";
397 case OTHER_CURSOR: return "Cursor";
398 case OTHER_ASHMEM: return "Ashmem";
399 case OTHER_GL_DEV: return "Gfx dev";
400 case OTHER_UNKNOWN_DEV: return "Other dev";
401 case OTHER_SO: return ".so mmap";
402 case OTHER_JAR: return ".jar mmap";
403 case OTHER_APK: return ".apk mmap";
404 case OTHER_TTF: return ".ttf mmap";
405 case OTHER_DEX: return ".dex mmap";
406 case OTHER_OAT: return ".oat mmap";
407 case OTHER_ART: return ".art mmap";
408 case OTHER_UNKNOWN_MAP: return "Other mmap";
409 case OTHER_GRAPHICS: return "EGL mtrack";
410 case OTHER_GL: return "GL mtrack";
411 case OTHER_OTHER_MEMTRACK: return "Other mtrack";
412 case OTHER_DALVIK_NORMAL: return ".Heap";
413 case OTHER_DALVIK_LARGE: return ".LOS";
414 case OTHER_DALVIK_LINEARALLOC: return ".LinearAlloc";
415 case OTHER_DALVIK_ACCOUNTING: return ".GC";
416 case OTHER_DALVIK_CODE_CACHE: return ".JITCache";
417 case OTHER_DALVIK_ZYGOTE: return ".Zygote";
418 case OTHER_DALVIK_NON_MOVING: return ".NonMoving";
419 case OTHER_DALVIK_INDIRECT_REFERENCE_TABLE: return ".IndirectRef";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700420 default: return "????";
421 }
422 }
423
Richard Uhler350e6dc2015-05-18 10:48:52 -0700424 /**
425 * Returns the value of a particular memory statistic or {@code null} if no
426 * such memory statistic exists.
427 *
428 * <p>The following table lists the memory statistics that are supported.
429 * Note that memory statistics may be added or removed in a future API level.</p>
430 *
431 * <table>
432 * <thead>
433 * <tr>
434 * <th>Memory statistic name</th>
435 * <th>Meaning</th>
436 * <th>Example</th>
437 * <th>Supported (API Levels)</th>
438 * </tr>
439 * </thead>
440 * <tbody>
441 * <tr>
442 * <td>summary.java-heap</td>
443 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field
444 * in the App Summary section output by dumpsys meminfo.</td>
445 * <td>{@code 1442}</td>
446 * <td>23</td>
447 * </tr>
448 * <tr>
449 * <td>summary.native-heap</td>
450 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap
451 * field in the App Summary section output by dumpsys meminfo.</td>
452 * <td>{@code 1442}</td>
453 * <td>23</td>
454 * </tr>
455 * <tr>
456 * <td>summary.code</td>
457 * <td>The memory usage for static code and resources in kB. This corresponds to
458 * the Code field in the App Summary section output by dumpsys meminfo.</td>
459 * <td>{@code 1442}</td>
460 * <td>23</td>
461 * </tr>
462 * <tr>
463 * <td>summary.stack</td>
464 * <td>The stack usage in kB. This corresponds to the Stack field in the
465 * App Summary section output by dumpsys meminfo.</td>
466 * <td>{@code 1442}</td>
467 * <td>23</td>
468 * </tr>
469 * <tr>
470 * <td>summary.graphics</td>
471 * <td>The graphics usage in kB. This corresponds to the Graphics field in the
472 * App Summary section output by dumpsys meminfo.</td>
473 * <td>{@code 1442}</td>
474 * <td>23</td>
475 * </tr>
476 * <tr>
477 * <td>summary.private-other</td>
478 * <td>Other private memory usage in kB. This corresponds to the Private Other
479 * field output in the App Summary section by dumpsys meminfo.</td>
480 * <td>{@code 1442}</td>
481 * <td>23</td>
482 * </tr>
483 * <tr>
484 * <td>summary.system</td>
485 * <td>Shared and system memory usage in kB. This corresponds to the System
486 * field output in the App Summary section by dumpsys meminfo.</td>
487 * <td>{@code 1442}</td>
488 * <td>23</td>
489 * </tr>
490 * <tr>
491 * <td>summary.total-pss</td>
492 * <td>Total PPS memory usage in kB.</td>
493 * <td>{@code 1442}</td>
494 * <td>23</td>
495 * </tr>
496 * <tr>
497 * <td>summary.total-swap</td>
498 * <td>Total swap usage in kB.</td>
499 * <td>{@code 1442}</td>
500 * <td>23</td>
501 * </tr>
502 * </tbody>
503 * </table>
504 */
Dianne Hackbornb02ce292015-10-12 15:14:16 -0700505 public String getMemoryStat(String statName) {
Richard Uhler350e6dc2015-05-18 10:48:52 -0700506 switch(statName) {
507 case "summary.java-heap":
508 return Integer.toString(getSummaryJavaHeap());
509 case "summary.native-heap":
510 return Integer.toString(getSummaryNativeHeap());
511 case "summary.code":
512 return Integer.toString(getSummaryCode());
513 case "summary.stack":
514 return Integer.toString(getSummaryStack());
515 case "summary.graphics":
516 return Integer.toString(getSummaryGraphics());
517 case "summary.private-other":
518 return Integer.toString(getSummaryPrivateOther());
519 case "summary.system":
520 return Integer.toString(getSummarySystem());
521 case "summary.total-pss":
522 return Integer.toString(getSummaryTotalPss());
523 case "summary.total-swap":
524 return Integer.toString(getSummaryTotalSwap());
525 default:
526 return null;
527 }
528 }
529
530 /**
531 * Returns a map of the names/values of the memory statistics
532 * that {@link #getMemoryStat(String)} supports.
533 *
534 * @return a map of the names/values of the supported memory statistics.
535 */
536 public Map<String, String> getMemoryStats() {
537 Map<String, String> stats = new HashMap<String, String>();
538 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap()));
539 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap()));
540 stats.put("summary.code", Integer.toString(getSummaryCode()));
541 stats.put("summary.stack", Integer.toString(getSummaryStack()));
542 stats.put("summary.graphics", Integer.toString(getSummaryGraphics()));
543 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther()));
544 stats.put("summary.system", Integer.toString(getSummarySystem()));
545 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss()));
546 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap()));
547 return stats;
548 }
549
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700550 /**
551 * Pss of Java Heap bytes in KB due to the application.
552 * Notes:
553 * * OTHER_ART is the boot image. Anything private here is blamed on
554 * the application, not the system.
555 * * dalvikPrivateDirty includes private zygote, which means the
556 * application dirtied something allocated by the zygote. We blame
557 * the application for that memory, not the system.
558 * * Does not include OTHER_DALVIK_OTHER, which is considered VM
559 * Overhead and lumped into Private Other.
560 * * We don't include dalvikPrivateClean, because there should be no
561 * such thing as private clean for the Java Heap.
562 * @hide
563 */
564 public int getSummaryJavaHeap() {
565 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART);
566 }
567
568 /**
569 * Pss of Native Heap bytes in KB due to the application.
570 * Notes:
571 * * Includes private dirty malloc space.
572 * * We don't include nativePrivateClean, because there should be no
573 * such thing as private clean for the Native Heap.
574 * @hide
575 */
576 public int getSummaryNativeHeap() {
577 return nativePrivateDirty;
578 }
579
580 /**
581 * Pss of code and other static resource bytes in KB due to
582 * the application.
583 * @hide
584 */
585 public int getSummaryCode() {
586 return getOtherPrivate(OTHER_SO)
587 + getOtherPrivate(OTHER_JAR)
588 + getOtherPrivate(OTHER_APK)
589 + getOtherPrivate(OTHER_TTF)
590 + getOtherPrivate(OTHER_DEX)
591 + getOtherPrivate(OTHER_OAT);
592 }
593
594 /**
595 * Pss in KB of the stack due to the application.
596 * Notes:
597 * * Includes private dirty stack, which includes both Java and Native
598 * stack.
599 * * Does not include private clean stack, because there should be no
600 * such thing as private clean for the stack.
601 * @hide
602 */
603 public int getSummaryStack() {
604 return getOtherPrivateDirty(OTHER_STACK);
605 }
606
607 /**
608 * Pss in KB of graphics due to the application.
609 * Notes:
610 * * Includes private Gfx, EGL, and GL.
611 * * Warning: These numbers can be misreported by the graphics drivers.
612 * * We don't include shared graphics. It may make sense to, because
613 * shared graphics are likely buffers due to the application
614 * anyway, but it's simpler to implement to just group all shared
615 * memory into the System category.
616 * @hide
617 */
618 public int getSummaryGraphics() {
619 return getOtherPrivate(OTHER_GL_DEV)
620 + getOtherPrivate(OTHER_GRAPHICS)
621 + getOtherPrivate(OTHER_GL);
622 }
623
624 /**
625 * Pss in KB due to the application that haven't otherwise been
626 * accounted for.
627 * @hide
628 */
629 public int getSummaryPrivateOther() {
630 return getTotalPrivateClean()
631 + getTotalPrivateDirty()
632 - getSummaryJavaHeap()
633 - getSummaryNativeHeap()
634 - getSummaryCode()
635 - getSummaryStack()
636 - getSummaryGraphics();
637 }
638
639 /**
640 * Pss in KB due to the system.
641 * Notes:
642 * * Includes all shared memory.
643 * @hide
644 */
645 public int getSummarySystem() {
646 return getTotalPss()
647 - getTotalPrivateClean()
648 - getTotalPrivateDirty();
649 }
650
651 /**
652 * Total Pss in KB.
653 * @hide
654 */
655 public int getSummaryTotalPss() {
656 return getTotalPss();
657 }
658
659 /**
660 * Total Swap in KB.
661 * Notes:
662 * * Some of this memory belongs in other categories, but we don't
663 * know if the Swap memory is shared or private, so we don't know
664 * what to blame on the application and what on the system.
665 * For now, just lump all the Swap in one place.
Martijn Coenene0764852016-01-07 17:04:22 -0800666 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()}
667 * will report the application proportional Swap.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700668 * @hide
669 */
670 public int getSummaryTotalSwap() {
671 return getTotalSwappedOut();
672 }
673
Martijn Coenene0764852016-01-07 17:04:22 -0800674 /**
675 * Total proportional Swap in KB.
676 * Notes:
677 * * Always 0 if {@link #hasSwappedOutPss} is false.
678 * @hide
679 */
680 public int getSummaryTotalSwapPss() {
681 return getTotalSwappedOutPss();
682 }
683
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700684 public int describeContents() {
685 return 0;
686 }
687
688 public void writeToParcel(Parcel dest, int flags) {
689 dest.writeInt(dalvikPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700690 dest.writeInt(dalvikSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700691 dest.writeInt(dalvikPrivateDirty);
692 dest.writeInt(dalvikSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700693 dest.writeInt(dalvikPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700694 dest.writeInt(dalvikSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700695 dest.writeInt(dalvikSwappedOut);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700696 dest.writeInt(nativePss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700697 dest.writeInt(nativeSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700698 dest.writeInt(nativePrivateDirty);
699 dest.writeInt(nativeSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700700 dest.writeInt(nativePrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700701 dest.writeInt(nativeSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700702 dest.writeInt(nativeSwappedOut);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700703 dest.writeInt(otherPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700704 dest.writeInt(otherSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700705 dest.writeInt(otherPrivateDirty);
706 dest.writeInt(otherSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700707 dest.writeInt(otherPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700708 dest.writeInt(otherSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700709 dest.writeInt(otherSwappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800710 dest.writeInt(hasSwappedOutPss ? 1 : 0);
711 dest.writeInt(otherSwappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700712 dest.writeIntArray(otherStats);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700713 }
714
715 public void readFromParcel(Parcel source) {
716 dalvikPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700717 dalvikSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700718 dalvikPrivateDirty = source.readInt();
719 dalvikSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700720 dalvikPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700721 dalvikSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700722 dalvikSwappedOut = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700723 nativePss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700724 nativeSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700725 nativePrivateDirty = source.readInt();
726 nativeSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700727 nativePrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700728 nativeSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700729 nativeSwappedOut = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700730 otherPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700731 otherSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700732 otherPrivateDirty = source.readInt();
733 otherSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700734 otherPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700735 otherSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700736 otherSwappedOut = source.readInt();
Martijn Coenene0764852016-01-07 17:04:22 -0800737 hasSwappedOutPss = source.readInt() != 0;
738 otherSwappedOutPss = source.readInt();
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700739 otherStats = source.createIntArray();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700740 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200741
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700742 public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
743 public MemoryInfo createFromParcel(Parcel source) {
744 return new MemoryInfo(source);
745 }
746 public MemoryInfo[] newArray(int size) {
747 return new MemoryInfo[size];
748 }
749 };
750
751 private MemoryInfo(Parcel source) {
752 readFromParcel(source);
753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 }
755
756
757 /**
758 * Wait until a debugger attaches. As soon as the debugger attaches,
759 * this returns, so you will need to place a breakpoint after the
760 * waitForDebugger() call if you want to start tracing immediately.
761 */
762 public static void waitForDebugger() {
763 if (!VMDebug.isDebuggingEnabled()) {
764 //System.out.println("debugging not enabled, not waiting");
765 return;
766 }
767 if (isDebuggerConnected())
768 return;
769
770 // if DDMS is listening, inform them of our plight
771 System.out.println("Sending WAIT chunk");
772 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger"
773 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
774 DdmServer.sendChunk(waitChunk);
775
776 mWaiting = true;
777 while (!isDebuggerConnected()) {
778 try { Thread.sleep(SPIN_DELAY); }
779 catch (InterruptedException ie) {}
780 }
781 mWaiting = false;
782
783 System.out.println("Debugger has connected");
784
785 /*
786 * There is no "ready to go" signal from the debugger, and we're
787 * not allowed to suspend ourselves -- the debugger expects us to
788 * be running happily, and gets confused if we aren't. We need to
789 * allow the debugger a chance to set breakpoints before we start
790 * running again.
791 *
792 * Sit and spin until the debugger has been idle for a short while.
793 */
794 while (true) {
795 long delta = VMDebug.lastDebuggerActivity();
796 if (delta < 0) {
797 System.out.println("debugger detached?");
798 break;
799 }
800
801 if (delta < MIN_DEBUGGER_IDLE) {
802 System.out.println("waiting for debugger to settle...");
803 try { Thread.sleep(SPIN_DELAY); }
804 catch (InterruptedException ie) {}
805 } else {
806 System.out.println("debugger has settled (" + delta + ")");
807 break;
808 }
809 }
810 }
811
812 /**
813 * Returns "true" if one or more threads is waiting for a debugger
814 * to attach.
815 */
816 public static boolean waitingForDebugger() {
817 return mWaiting;
818 }
819
820 /**
821 * Determine if a debugger is currently attached.
822 */
823 public static boolean isDebuggerConnected() {
824 return VMDebug.isDebuggerConnected();
825 }
826
827 /**
Andy McFaddene5772322010-01-22 07:23:31 -0800828 * Returns an array of strings that identify VM features. This is
829 * used by DDMS to determine what sorts of operations the VM can
830 * perform.
831 *
832 * @hide
833 */
834 public static String[] getVmFeatureList() {
835 return VMDebug.getVmFeatureList();
836 }
837
838 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 * Change the JDWP port.
840 *
841 * @deprecated no longer needed or useful
842 */
843 @Deprecated
844 public static void changeDebugPort(int port) {}
845
846 /**
847 * This is the pathname to the sysfs file that enables and disables
848 * tracing on the qemu emulator.
849 */
850 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
851
852 /**
853 * Enable qemu tracing. For this to work requires running everything inside
854 * the qemu emulator; otherwise, this method will have no effect. The trace
855 * file is specified on the command line when the emulator is started. For
856 * example, the following command line <br />
857 * <code>emulator -trace foo</code><br />
858 * will start running the emulator and create a trace file named "foo". This
859 * method simply enables writing the trace records to the trace file.
860 *
861 * <p>
862 * The main differences between this and {@link #startMethodTracing()} are
863 * that tracing in the qemu emulator traces every cpu instruction of every
864 * process, including kernel code, so we have more complete information,
865 * including all context switches. We can also get more detailed information
866 * such as cache misses. The sequence of calls is determined by
867 * post-processing the instruction trace. The qemu tracing is also done
868 * without modifying the application or perturbing the timing of calls
869 * because no instrumentation is added to the application being traced.
870 * </p>
871 *
872 * <p>
873 * One limitation of using this method compared to using
874 * {@link #startMethodTracing()} on the real device is that the emulator
875 * does not model all of the real hardware effects such as memory and
876 * bus contention. The emulator also has a simple cache model and cannot
877 * capture all the complexities of a real cache.
878 * </p>
879 */
880 public static void startNativeTracing() {
881 // Open the sysfs file for writing and write "1" to it.
882 PrintWriter outStream = null;
883 try {
884 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700885 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 outStream.println("1");
887 } catch (Exception e) {
888 } finally {
889 if (outStream != null)
890 outStream.close();
891 }
892
893 VMDebug.startEmulatorTracing();
894 }
895
896 /**
897 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing.
898 *
899 * <p>Tracing can be started and stopped as many times as desired. When
900 * the qemu emulator itself is stopped then the buffered trace records
901 * are flushed and written to the trace file. In fact, it is not necessary
902 * to call this method at all; simply killing qemu is sufficient. But
903 * starting and stopping a trace is useful for examining a specific
904 * region of code.</p>
905 */
906 public static void stopNativeTracing() {
907 VMDebug.stopEmulatorTracing();
908
909 // Open the sysfs file for writing and write "0" to it.
910 PrintWriter outStream = null;
911 try {
912 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700913 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 outStream.println("0");
915 } catch (Exception e) {
916 // We could print an error message here but we probably want
917 // to quietly ignore errors if we are not running in the emulator.
918 } finally {
919 if (outStream != null)
920 outStream.close();
921 }
922 }
923
924 /**
925 * Enable "emulator traces", in which information about the current
926 * method is made available to the "emulator -trace" feature. There
927 * is no corresponding "disable" call -- this is intended for use by
928 * the framework when tracing should be turned on and left that way, so
929 * that traces captured with F9/F10 will include the necessary data.
930 *
931 * This puts the VM into "profile" mode, which has performance
932 * consequences.
933 *
934 * To temporarily enable tracing, use {@link #startNativeTracing()}.
935 */
936 public static void enableEmulatorTraceOutput() {
937 VMDebug.startEmulatorTracing();
938 }
939
940 /**
941 * Start method tracing with default log name and buffer size. See <a
942href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
943 * information about reading these files. Call stopMethodTracing() to stop
944 * tracing.
945 */
946 public static void startMethodTracing() {
Andreas Gampe8b9f4942016-02-25 18:03:03 -0800947 VMDebug.startMethodTracing(fixTraceName(null), 0, 0, false, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 /**
951 * Start method tracing, specifying the trace log file name. The trace
952 * file will be put under "/sdcard" unless an absolute path is given.
953 * See <a
954 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
955 * information about reading trace files.
956 *
957 * @param traceName Name for the trace log file to create.
Jeff Haod02e60f2014-01-06 15:52:52 -0800958 * If {@code traceName} is null, this value defaults to "/sdcard/dmtrace.trace".
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 * If the files already exist, they will be truncated.
960 * If the trace file given does not end in ".trace", it will be appended for you.
961 */
962 public static void startMethodTracing(String traceName) {
963 startMethodTracing(traceName, 0, 0);
964 }
965
966 /**
967 * Start method tracing, specifying the trace log file name and the
968 * buffer size. The trace files will be put under "/sdcard" unless an
969 * absolute path is given. See <a
970 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
971 * information about reading trace files.
972 * @param traceName Name for the trace log file to create.
Jeff Haod02e60f2014-01-06 15:52:52 -0800973 * If {@code traceName} is null, this value defaults to "/sdcard/dmtrace.trace".
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 * If the files already exist, they will be truncated.
975 * If the trace file given does not end in ".trace", it will be appended for you.
976 *
977 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB.
978 */
979 public static void startMethodTracing(String traceName, int bufferSize) {
980 startMethodTracing(traceName, bufferSize, 0);
981 }
982
983 /**
984 * Start method tracing, specifying the trace log file name and the
985 * buffer size. The trace files will be put under "/sdcard" unless an
986 * absolute path is given. See <a
987 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
988 * information about reading trace files.
989 *
990 * <p>
991 * When method tracing is enabled, the VM will run more slowly than
992 * usual, so the timings from the trace files should only be considered
993 * in relative terms (e.g. was run #1 faster than run #2). The times
994 * for native methods will not change, so don't try to use this to
995 * compare the performance of interpreted and native implementations of the
Jeff Haod02e60f2014-01-06 15:52:52 -0800996 * same method. As an alternative, consider using sampling-based method
997 * tracing via {@link #startMethodTracingSampling(String, int, int)} or
998 * "native" tracing in the emulator via {@link #startNativeTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 * </p>
1000 *
1001 * @param traceName Name for the trace log file to create.
Jeff Haod02e60f2014-01-06 15:52:52 -08001002 * If {@code traceName} is null, this value defaults to "/sdcard/dmtrace.trace".
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * If the files already exist, they will be truncated.
1004 * If the trace file given does not end in ".trace", it will be appended for you.
1005 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB.
Jeff Haod02e60f2014-01-06 15:52:52 -08001006 * @param flags Flags to control method tracing. The only one that is currently defined is {@link #TRACE_COUNT_ALLOCS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 */
1008 public static void startMethodTracing(String traceName, int bufferSize,
1009 int flags) {
Jeff Haod02e60f2014-01-06 15:52:52 -08001010 VMDebug.startMethodTracing(fixTraceName(traceName), bufferSize, flags, false, 0);
1011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012
Jeff Haod02e60f2014-01-06 15:52:52 -08001013 /**
1014 * Start sampling-based method tracing, specifying the trace log file name,
1015 * the buffer size, and the sampling interval. The trace files will be put
1016 * under "/sdcard" unless an absolute path is given. See <a
1017 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a>
1018 * for information about reading trace files.
1019 *
1020 * @param traceName Name for the trace log file to create.
1021 * If {@code traceName} is null, this value defaults to "/sdcard/dmtrace.trace".
1022 * If the files already exist, they will be truncated.
1023 * If the trace file given does not end in ".trace", it will be appended for you.
1024 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB.
1025 * @param intervalUs The amount of time between each sample in microseconds.
1026 */
1027 public static void startMethodTracingSampling(String traceName,
1028 int bufferSize, int intervalUs) {
1029 VMDebug.startMethodTracing(fixTraceName(traceName), bufferSize, 0, true, intervalUs);
1030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031
Jeff Haod02e60f2014-01-06 15:52:52 -08001032 /**
1033 * Formats name of trace log file for method tracing.
1034 */
1035 private static String fixTraceName(String traceName) {
1036 if (traceName == null)
Andreas Gampe8b9f4942016-02-25 18:03:03 -08001037 traceName = NoPreloadHolder.DEFAULT_TRACE_FILE_PATH;
Jeff Haod02e60f2014-01-06 15:52:52 -08001038 if (traceName.charAt(0) != '/')
Andreas Gampe8b9f4942016-02-25 18:03:03 -08001039 traceName = NoPreloadHolder.DEFAULT_TRACE_PATH_PREFIX + traceName;
Jeff Haod02e60f2014-01-06 15:52:52 -08001040 if (!traceName.endsWith(DEFAULT_TRACE_EXTENSION))
1041 traceName = traceName + DEFAULT_TRACE_EXTENSION;
1042
1043 return traceName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 }
1045
1046 /**
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001047 * Like startMethodTracing(String, int, int), but taking an already-opened
1048 * FileDescriptor in which the trace is written. The file name is also
1049 * supplied simply for logging. Makes a dup of the file descriptor.
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02001050 *
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001051 * Not exposed in the SDK unless we are really comfortable with supporting
1052 * this and find it would be useful.
1053 * @hide
1054 */
1055 public static void startMethodTracing(String traceName, FileDescriptor fd,
1056 int bufferSize, int flags) {
Jeff Haod02e60f2014-01-06 15:52:52 -08001057 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001058 }
1059
1060 /**
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001061 * Starts method tracing without a backing file. When stopMethodTracing
1062 * is called, the result is sent directly to DDMS. (If DDMS is not
1063 * attached when tracing ends, the profiling data will be discarded.)
1064 *
1065 * @hide
1066 */
Jeff Hao7be3a132013-08-22 15:53:12 -07001067 public static void startMethodTracingDdms(int bufferSize, int flags,
1068 boolean samplingEnabled, int intervalUs) {
1069 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs);
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001070 }
1071
1072 /**
Jeff Haoac277052013-08-29 11:19:39 -07001073 * Determine whether method tracing is currently active and what type is
1074 * active.
1075 *
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001076 * @hide
1077 */
Jeff Haoac277052013-08-29 11:19:39 -07001078 public static int getMethodTracingMode() {
1079 return VMDebug.getMethodTracingMode();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001080 }
1081
1082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * Stop method tracing.
1084 */
1085 public static void stopMethodTracing() {
1086 VMDebug.stopMethodTracing();
1087 }
1088
1089 /**
1090 * Get an indication of thread CPU usage. The value returned
1091 * indicates the amount of time that the current thread has spent
1092 * executing code or waiting for certain types of I/O.
1093 *
1094 * The time is expressed in nanoseconds, and is only meaningful
1095 * when compared to the result from an earlier call. Note that
1096 * nanosecond resolution does not imply nanosecond accuracy.
1097 *
1098 * On system which don't support this operation, the call returns -1.
1099 */
1100 public static long threadCpuTimeNanos() {
1101 return VMDebug.threadCpuTimeNanos();
1102 }
1103
1104 /**
Chet Haase2970c492010-11-09 13:58:04 -08001105 * Start counting the number and aggregate size of memory allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001107 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
1108 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
1109 * code doesn't cause additional allocations. The various <code>get</code> methods return
1110 * the specified value. And the various <code>reset</code> methods reset the specified
Chet Haase2970c492010-11-09 13:58:04 -08001111 * count.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001113 * <p>Counts are kept for the system as a whole (global) and for each thread.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 * The per-thread counts for threads other than the current thread
Chet Haase2970c492010-11-09 13:58:04 -08001115 * are not cleared by the "reset" or "start" calls.</p>
Ian Rogersfe067a42013-02-22 19:59:23 -08001116 *
1117 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 */
Ian Rogersfe067a42013-02-22 19:59:23 -08001119 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 public static void startAllocCounting() {
1121 VMDebug.startAllocCounting();
1122 }
Chet Haase2970c492010-11-09 13:58:04 -08001123
1124 /**
1125 * Stop counting the number and aggregate size of memory allocations.
1126 *
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001127 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Chet Haase2970c492010-11-09 13:58:04 -08001128 */
Ian Rogersc2a3adb2013-04-19 11:31:48 -07001129 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 public static void stopAllocCounting() {
1131 VMDebug.stopAllocCounting();
1132 }
1133
Ian Rogersfe067a42013-02-22 19:59:23 -08001134 /**
1135 * Returns the global count of objects allocated by the runtime between a
1136 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001137 *
1138 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001139 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001140 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public static int getGlobalAllocCount() {
1142 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1143 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001144
1145 /**
1146 * Clears the global count of objects allocated.
1147 * @see #getGlobalAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001148 *
1149 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001150 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001151 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001152 public static void resetGlobalAllocCount() {
1153 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1154 }
1155
1156 /**
1157 * Returns the global size, in bytes, of objects allocated by the runtime between a
1158 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001159 *
1160 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001161 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001162 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 public static int getGlobalAllocSize() {
1164 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1165 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001166
1167 /**
1168 * Clears the global size of objects allocated.
Dianne Hackborn3fa89692013-09-13 17:20:00 -07001169 * @see #getGlobalAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001170 *
1171 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001172 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001173 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001174 public static void resetGlobalAllocSize() {
1175 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1176 }
1177
1178 /**
1179 * Returns the global count of objects freed by the runtime between a
1180 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001181 *
1182 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001183 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001184 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public static int getGlobalFreedCount() {
1186 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1187 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001188
1189 /**
1190 * Clears the global count of objects freed.
1191 * @see #getGlobalFreedCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001192 *
1193 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001194 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001195 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001196 public static void resetGlobalFreedCount() {
1197 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1198 }
1199
1200 /**
1201 * Returns the global size, in bytes, of objects freed by the runtime between a
1202 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001203 *
1204 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001205 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001206 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 public static int getGlobalFreedSize() {
1208 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1209 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001210
1211 /**
1212 * Clears the global size of objects freed.
1213 * @see #getGlobalFreedSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001214 *
1215 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001216 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001217 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001218 public static void resetGlobalFreedSize() {
1219 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1220 }
1221
1222 /**
1223 * Returns the number of non-concurrent GC invocations between a
1224 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001225 *
1226 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001227 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001228 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001229 public static int getGlobalGcInvocationCount() {
1230 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1231 }
1232
1233 /**
1234 * Clears the count of non-concurrent GC invocations.
1235 * @see #getGlobalGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001236 *
1237 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001238 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001239 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001240 public static void resetGlobalGcInvocationCount() {
1241 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1242 }
1243
1244 /**
1245 * Returns the number of classes successfully initialized (ie those that executed without
1246 * throwing an exception) between a {@link #startAllocCounting() start} and
1247 * {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001248 *
1249 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001250 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001251 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001252 public static int getGlobalClassInitCount() {
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001253 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1254 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001255
1256 /**
1257 * Clears the count of classes initialized.
1258 * @see #getGlobalClassInitCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001259 *
1260 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001261 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001262 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001263 public static void resetGlobalClassInitCount() {
1264 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1265 }
1266
1267 /**
1268 * Returns the time spent successfully initializing classes between a
1269 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001270 *
1271 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001272 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001273 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001274 public static int getGlobalClassInitTime() {
1275 /* cumulative elapsed time for class initialization, in usec */
1276 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1277 }
Carl Shapirob5961982010-12-22 15:54:53 -08001278
1279 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001280 * Clears the count of time spent initializing classes.
1281 * @see #getGlobalClassInitTime()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001282 *
1283 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001284 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001285 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001286 public static void resetGlobalClassInitTime() {
1287 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1288 }
1289
1290 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001291 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001292 * @deprecated This method is now obsolete.
1293 */
1294 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 public static int getGlobalExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001296 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
Carl Shapirob5961982010-12-22 15:54:53 -08001298
1299 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001300 * This method exists for compatibility and has no effect.
1301 * @deprecated This method is now obsolete.
1302 */
1303 @Deprecated
1304 public static void resetGlobalExternalAllocSize() {}
1305
1306 /**
1307 * This method exists for compatibility and has no effect.
1308 * @deprecated This method is now obsolete.
1309 */
1310 @Deprecated
1311 public static void resetGlobalExternalAllocCount() {}
1312
1313 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001314 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001315 * @deprecated This method is now obsolete.
1316 */
1317 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 public static int getGlobalExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001319 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 }
Carl Shapirob5961982010-12-22 15:54:53 -08001321
1322 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001323 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001324 * @deprecated This method is now obsolete.
1325 */
1326 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 public static int getGlobalExternalFreedCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001328 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Carl Shapirob5961982010-12-22 15:54:53 -08001330
1331 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001332 * This method exists for compatibility and has no effect.
1333 * @deprecated This method is now obsolete.
1334 */
1335 @Deprecated
1336 public static void resetGlobalExternalFreedCount() {}
1337
1338 /**
1339 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001340 * @deprecated This method is now obsolete.
1341 */
1342 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 public static int getGlobalExternalFreedSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001344 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
Carl Shapirob5961982010-12-22 15:54:53 -08001346
Ian Rogersfe067a42013-02-22 19:59:23 -08001347 /**
1348 * This method exists for compatibility and has no effect.
1349 * @deprecated This method is now obsolete.
1350 */
1351 @Deprecated
1352 public static void resetGlobalExternalFreedSize() {}
1353
1354 /**
1355 * Returns the thread-local count of objects allocated by the runtime between a
1356 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001357 *
1358 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001359 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001360 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 public static int getThreadAllocCount() {
1362 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1363 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001364
1365 /**
1366 * Clears the thread-local count of objects allocated.
1367 * @see #getThreadAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001368 *
1369 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001370 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001371 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001372 public static void resetThreadAllocCount() {
1373 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1374 }
1375
1376 /**
1377 * Returns the thread-local size of objects allocated by the runtime between a
1378 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
1379 * @return The allocated size in bytes.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001380 *
1381 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001382 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 public static int getThreadAllocSize() {
1385 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1386 }
Carl Shapirob5961982010-12-22 15:54:53 -08001387
1388 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001389 * Clears the thread-local count of objects allocated.
1390 * @see #getThreadAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001391 *
1392 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001393 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001394 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001395 public static void resetThreadAllocSize() {
1396 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1397 }
1398
1399 /**
1400 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001401 * @deprecated This method is now obsolete.
1402 */
1403 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 public static int getThreadExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001405 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 }
Carl Shapirob5961982010-12-22 15:54:53 -08001407
1408 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001409 * This method exists for compatibility and has no effect.
1410 * @deprecated This method is now obsolete.
1411 */
1412 @Deprecated
1413 public static void resetThreadExternalAllocCount() {}
1414
1415 /**
1416 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001417 * @deprecated This method is now obsolete.
1418 */
1419 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 public static int getThreadExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001421 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Carl Shapirob5961982010-12-22 15:54:53 -08001423
Carl Shapirob5961982010-12-22 15:54:53 -08001424 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001425 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001426 * @deprecated This method is now obsolete.
1427 */
1428 @Deprecated
1429 public static void resetThreadExternalAllocSize() {}
1430
Ian Rogersfe067a42013-02-22 19:59:23 -08001431 /**
1432 * Returns the number of thread-local non-concurrent GC invocations between a
1433 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001434 *
1435 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001436 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001437 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001438 public static int getThreadGcInvocationCount() {
1439 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1440 }
1441
1442 /**
1443 * Clears the thread-local count of non-concurrent GC invocations.
1444 * @see #getThreadGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001445 *
1446 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001447 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001448 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 public static void resetThreadGcInvocationCount() {
1450 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1451 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001452
1453 /**
1454 * Clears all the global and thread-local memory allocation counters.
1455 * @see #startAllocCounting()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001456 *
1457 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001458 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001459 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 public static void resetAllCounts() {
1461 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
1462 }
1463
1464 /**
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001465 * Returns the value of a particular runtime statistic or {@code null} if no
1466 * such runtime statistic exists.
1467 *
1468 * <p>The following table lists the runtime statistics that the runtime supports.
1469 * Note runtime statistics may be added or removed in a future API level.</p>
1470 *
1471 * <table>
1472 * <thead>
1473 * <tr>
1474 * <th>Runtime statistic name</th>
1475 * <th>Meaning</th>
1476 * <th>Example</th>
1477 * <th>Supported (API Levels)</th>
1478 * </tr>
1479 * </thead>
1480 * <tbody>
1481 * <tr>
1482 * <td>art.gc.gc-count</td>
1483 * <td>The number of garbage collection runs.</td>
1484 * <td>{@code 164}</td>
1485 * <td>23</td>
1486 * </tr>
1487 * <tr>
1488 * <td>art.gc.gc-time</td>
1489 * <td>The total duration of garbage collection runs in ms.</td>
1490 * <td>{@code 62364}</td>
1491 * <td>23</td>
1492 * </tr>
1493 * <tr>
1494 * <td>art.gc.bytes-allocated</td>
1495 * <td>The total number of bytes that the application allocated.</td>
1496 * <td>{@code 1463948408}</td>
1497 * <td>23</td>
1498 * </tr>
1499 * <tr>
1500 * <td>art.gc.bytes-freed</td>
1501 * <td>The total number of bytes that garbage collection reclaimed.</td>
1502 * <td>{@code 1313493084}</td>
1503 * <td>23</td>
1504 * </tr>
1505 * <tr>
1506 * <td>art.gc.blocking-gc-count</td>
1507 * <td>The number of blocking garbage collection runs.</td>
1508 * <td>{@code 2}</td>
1509 * <td>23</td>
1510 * </tr>
1511 * <tr>
1512 * <td>art.gc.blocking-gc-time</td>
1513 * <td>The total duration of blocking garbage collection runs in ms.</td>
1514 * <td>{@code 804}</td>
1515 * <td>23</td>
1516 * </tr>
1517 * <tr>
1518 * <td>art.gc.gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001519 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage
1520 * collection runs that have occurred over the last 10
1521 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate
1522 * samples taken since the process began. The histogram can be used to identify
1523 * instances of high rates of garbage collection runs. For example, a histogram
1524 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time
1525 * there are between 0 and 2 garbage collection runs every 10 seconds, but there
1526 * were 8 distinct 10-second intervals in which 5 garbage collection runs
1527 * occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001528 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td>
1529 * <td>23</td>
1530 * </tr>
1531 * <tr>
1532 * <td>art.gc.blocking-gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001533 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of
1534 * blocking garbage collection runs that have occurred over the last 10
1535 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the
1536 * blocking-gc-count-rate samples taken since the process began. The histogram
1537 * can be used to identify instances of high rates of blocking garbage
1538 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that
1539 * most of the time there are zero blocking garbage collection runs every 10
1540 * seconds, but there was one 10-second interval in which one blocking garbage
1541 * collection run occurred, and there was one interval in which two blocking
1542 * garbage collection runs occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001543 * <td>{@code 0:99269,1:1,2:1}</td>
1544 * <td>23</td>
1545 * </tr>
1546 * </tbody>
1547 * </table>
1548 *
1549 * @param statName
1550 * the name of the runtime statistic to look up.
1551 * @return the value of the specified runtime statistic or {@code null} if the
1552 * runtime statistic doesn't exist.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001553 */
1554 public static String getRuntimeStat(String statName) {
1555 return VMDebug.getRuntimeStat(statName);
1556 }
1557
1558 /**
1559 * Returns a map of the names/values of the runtime statistics
Hiroshi Yamauchid8001672015-04-14 16:07:26 -07001560 * that {@link #getRuntimeStat(String)} supports.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001561 *
1562 * @return a map of the names/values of the supported runtime statistics.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001563 */
1564 public static Map<String, String> getRuntimeStats() {
1565 return VMDebug.getRuntimeStats();
1566 }
1567
1568 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 * Returns the size of the native heap.
1570 * @return The size of the native heap in bytes.
1571 */
1572 public static native long getNativeHeapSize();
1573
1574 /**
1575 * Returns the amount of allocated memory in the native heap.
1576 * @return The allocated size in bytes.
1577 */
1578 public static native long getNativeHeapAllocatedSize();
1579
1580 /**
1581 * Returns the amount of free memory in the native heap.
1582 * @return The freed size in bytes.
1583 */
1584 public static native long getNativeHeapFreeSize();
1585
1586 /**
1587 * Retrieves information about this processes memory usages. This information is broken down by
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001588 * how much is in use by dalvik, the native heap, and everything else.
1589 *
1590 * <p><b>Note:</b> this method directly retrieves memory information for the give process
1591 * from low-level data available to it. It may not be able to retrieve information about
1592 * some protected allocations, such as graphics. If you want to be sure you can see
1593 * all information about allocations by the process, use instead
1594 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 */
1596 public static native void getMemoryInfo(MemoryInfo memoryInfo);
1597
1598 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001599 * Note: currently only works when the requested pid has the same UID
1600 * as the caller.
1601 * @hide
1602 */
1603 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
1604
1605 /**
Dianne Hackbornb437e092011-08-05 17:50:29 -07001606 * Retrieves the PSS memory used by the process as given by the
1607 * smaps.
1608 */
1609 public static native long getPss();
1610
1611 /**
1612 * Retrieves the PSS memory used by the process as given by the
Martijn Coenene0764852016-01-07 17:04:22 -08001613 * smaps. Optionally supply a long array of 2 entries to also
1614 * receive the Uss and SwapPss of the process, and another array to also
1615 * retrieve the separate memtrack size.
1616 * @hide
Dianne Hackbornb437e092011-08-05 17:50:29 -07001617 */
Martijn Coenene0764852016-01-07 17:04:22 -08001618 public static native long getPss(int pid, long[] outUssSwapPss, long[] outMemtrack);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001619
Dianne Hackborn8e692572013-09-10 19:06:15 -07001620 /** @hide */
1621 public static final int MEMINFO_TOTAL = 0;
1622 /** @hide */
1623 public static final int MEMINFO_FREE = 1;
1624 /** @hide */
1625 public static final int MEMINFO_BUFFERS = 2;
1626 /** @hide */
1627 public static final int MEMINFO_CACHED = 3;
1628 /** @hide */
1629 public static final int MEMINFO_SHMEM = 4;
1630 /** @hide */
1631 public static final int MEMINFO_SLAB = 5;
1632 /** @hide */
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001633 public static final int MEMINFO_SWAP_TOTAL = 6;
1634 /** @hide */
1635 public static final int MEMINFO_SWAP_FREE = 7;
1636 /** @hide */
1637 public static final int MEMINFO_ZRAM_TOTAL = 8;
1638 /** @hide */
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001639 public static final int MEMINFO_MAPPED = 9;
1640 /** @hide */
1641 public static final int MEMINFO_VM_ALLOC_USED = 10;
1642 /** @hide */
1643 public static final int MEMINFO_PAGE_TABLES = 11;
1644 /** @hide */
1645 public static final int MEMINFO_KERNEL_STACK = 12;
1646 /** @hide */
1647 public static final int MEMINFO_COUNT = 13;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001648
1649 /**
1650 * Retrieves /proc/meminfo. outSizes is filled with fields
1651 * as defined by MEMINFO_* offsets.
1652 * @hide
1653 */
1654 public static native void getMemInfo(long[] outSizes);
1655
Dianne Hackbornb437e092011-08-05 17:50:29 -07001656 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001657 * Establish an object allocation limit in the current thread.
Carl Shapiro7e942842011-01-12 17:17:45 -08001658 * This feature was never enabled in release builds. The
1659 * allocation limits feature was removed in Honeycomb. This
1660 * method exists for compatibility and always returns -1 and has
1661 * no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 *
Carl Shapiro11073832011-01-12 16:28:57 -08001663 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 */
Carl Shapiro11073832011-01-12 16:28:57 -08001665 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 public static int setAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001667 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669
1670 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001671 * Establish a global object allocation limit. This feature was
Carl Shapiro7e942842011-01-12 17:17:45 -08001672 * never enabled in release builds. The allocation limits feature
1673 * was removed in Honeycomb. This method exists for compatibility
1674 * and always returns -1 and has no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 *
Carl Shapiro11073832011-01-12 16:28:57 -08001676 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 */
Carl Shapiro11073832011-01-12 16:28:57 -08001678 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 public static int setGlobalAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001680 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
1682
1683 /**
1684 * Dump a list of all currently loaded class to the log file.
1685 *
1686 * @param flags See constants above.
1687 */
1688 public static void printLoadedClasses(int flags) {
1689 VMDebug.printLoadedClasses(flags);
1690 }
1691
1692 /**
1693 * Get the number of loaded classes.
1694 * @return the number of loaded classes.
1695 */
1696 public static int getLoadedClassCount() {
1697 return VMDebug.getLoadedClassCount();
1698 }
1699
1700 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001701 * Dump "hprof" data to the specified file. This may cause a GC.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 *
1703 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
1704 * @throws UnsupportedOperationException if the VM was built without
1705 * HPROF support.
1706 * @throws IOException if an error occurs while opening or writing files.
1707 */
1708 public static void dumpHprofData(String fileName) throws IOException {
1709 VMDebug.dumpHprofData(fileName);
1710 }
1711
1712 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001713 * Like dumpHprofData(String), but takes an already-opened
1714 * FileDescriptor to which the trace is written. The file name is also
1715 * supplied simply for logging. Makes a dup of the file descriptor.
1716 *
1717 * Primarily for use by the "am" shell command.
1718 *
1719 * @hide
1720 */
1721 public static void dumpHprofData(String fileName, FileDescriptor fd)
1722 throws IOException {
1723 VMDebug.dumpHprofData(fileName, fd);
1724 }
1725
1726 /**
1727 * Collect "hprof" and send it to DDMS. This may cause a GC.
Andy McFadden07a96612010-01-28 16:54:37 -08001728 *
1729 * @throws UnsupportedOperationException if the VM was built without
1730 * HPROF support.
Andy McFadden07a96612010-01-28 16:54:37 -08001731 * @hide
1732 */
1733 public static void dumpHprofDataDdms() {
1734 VMDebug.dumpHprofDataDdms();
1735 }
1736
1737 /**
Andy McFadden06a6b552010-07-13 16:28:09 -07001738 * Writes native heap data to the specified file descriptor.
1739 *
1740 * @hide
1741 */
1742 public static native void dumpNativeHeap(FileDescriptor fd);
1743
1744 /**
Brian Carlstromc21550a2010-10-05 21:34:06 -07001745 * Returns a count of the extant instances of a class.
1746 *
1747 * @hide
1748 */
1749 public static long countInstancesOfClass(Class cls) {
Brian Carlstrom7495cfa2010-11-30 18:06:00 -08001750 return VMDebug.countInstancesOfClass(cls, true);
Brian Carlstromc21550a2010-10-05 21:34:06 -07001751 }
1752
1753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 * Returns the number of sent transactions from this process.
1755 * @return The number of sent transactions or -1 if it could not read t.
1756 */
1757 public static native int getBinderSentTransactions();
1758
1759 /**
1760 * Returns the number of received transactions from the binder driver.
1761 * @return The number of received transactions or -1 if it could not read the stats.
1762 */
1763 public static native int getBinderReceivedTransactions();
1764
1765 /**
1766 * Returns the number of active local Binder objects that exist in the
1767 * current process.
1768 */
1769 public static final native int getBinderLocalObjectCount();
1770
1771 /**
1772 * Returns the number of references to remote proxy Binder objects that
1773 * exist in the current process.
1774 */
1775 public static final native int getBinderProxyObjectCount();
1776
1777 /**
1778 * Returns the number of death notification links to Binder objects that
1779 * exist in the current process.
1780 */
1781 public static final native int getBinderDeathObjectCount();
1782
1783 /**
Andy McFadden599c9182009-04-08 00:35:56 -07001784 * Primes the register map cache.
1785 *
1786 * Only works for classes in the bootstrap class loader. Does not
1787 * cause classes to be loaded if they're not already present.
1788 *
1789 * The classAndMethodDesc argument is a concatentation of the VM-internal
1790 * class descriptor, method name, and method descriptor. Examples:
1791 * Landroid/os/Looper;.loop:()V
1792 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
1793 *
1794 * @param classAndMethodDesc the method to prepare
1795 *
1796 * @hide
1797 */
1798 public static final boolean cacheRegisterMap(String classAndMethodDesc) {
1799 return VMDebug.cacheRegisterMap(classAndMethodDesc);
1800 }
1801
1802 /**
Andy McFaddenbfd6d482009-10-22 17:25:57 -07001803 * Dumps the contents of VM reference tables (e.g. JNI locals and
1804 * globals) to the log file.
1805 *
1806 * @hide
1807 */
1808 public static final void dumpReferenceTables() {
1809 VMDebug.dumpReferenceTables();
1810 }
1811
1812 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 * API for gathering and querying instruction counts.
1814 *
1815 * Example usage:
Chet Haase2970c492010-11-09 13:58:04 -08001816 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 * Debug.InstructionCount icount = new Debug.InstructionCount();
1818 * icount.resetAndStart();
1819 * [... do lots of stuff ...]
1820 * if (icount.collect()) {
1821 * System.out.println("Total instructions executed: "
1822 * + icount.globalTotal());
1823 * System.out.println("Method invocations: "
1824 * + icount.globalMethodInvocations());
1825 * }
Chet Haase2970c492010-11-09 13:58:04 -08001826 * </pre>
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001827 *
1828 * @deprecated Instruction counting is no longer supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 */
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001830 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 public static class InstructionCount {
Dan Bornsteinb96f5892010-12-02 17:19:53 -08001832 private static final int NUM_INSTR =
1833 OpcodeInfo.MAXIMUM_PACKED_VALUE + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834
1835 private int[] mCounts;
1836
1837 public InstructionCount() {
1838 mCounts = new int[NUM_INSTR];
1839 }
1840
1841 /**
1842 * Reset counters and ensure counts are running. Counts may
1843 * have already been running.
1844 *
1845 * @return true if counting was started
1846 */
1847 public boolean resetAndStart() {
1848 try {
1849 VMDebug.startInstructionCounting();
1850 VMDebug.resetInstructionCount();
1851 } catch (UnsupportedOperationException uoe) {
1852 return false;
1853 }
1854 return true;
1855 }
1856
1857 /**
1858 * Collect instruction counts. May or may not stop the
1859 * counting process.
1860 */
1861 public boolean collect() {
1862 try {
1863 VMDebug.stopInstructionCounting();
1864 VMDebug.getInstructionCount(mCounts);
1865 } catch (UnsupportedOperationException uoe) {
1866 return false;
1867 }
1868 return true;
1869 }
1870
1871 /**
1872 * Return the total number of instructions executed globally (i.e. in
1873 * all threads).
1874 */
1875 public int globalTotal() {
1876 int count = 0;
Dan Bornstein1d99b062010-11-30 12:26:52 -08001877
1878 for (int i = 0; i < NUM_INSTR; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 count += mCounts[i];
Dan Bornstein1d99b062010-11-30 12:26:52 -08001880 }
1881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 return count;
1883 }
1884
1885 /**
1886 * Return the total number of method-invocation instructions
1887 * executed globally.
1888 */
1889 public int globalMethodInvocations() {
1890 int count = 0;
1891
Dan Bornstein1d99b062010-11-30 12:26:52 -08001892 for (int i = 0; i < NUM_INSTR; i++) {
1893 if (OpcodeInfo.isInvoke(i)) {
1894 count += mCounts[i];
1895 }
1896 }
1897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 return count;
1899 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07001900 }
1901
Dave Bort1ce5bd32009-04-22 17:36:56 -07001902 /**
1903 * A Map of typed debug properties.
1904 */
1905 private static final TypedProperties debugProperties;
1906
1907 /*
1908 * Load the debug properties from the standard files into debugProperties.
1909 */
1910 static {
Joe Onorato43a17652011-04-06 19:22:23 -07001911 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001912 final String TAG = "DebugProperties";
1913 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
1914 final TypedProperties tp = new TypedProperties();
1915
1916 // Read the properties from each of the files, if present.
Dave Borte9bfd9b2009-05-04 14:35:23 -07001917 for (String file : files) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001918 Reader r;
1919 try {
1920 r = new FileReader(file);
1921 } catch (FileNotFoundException ex) {
1922 // It's ok if a file is missing.
1923 continue;
1924 }
1925
Dave Bort1ce5bd32009-04-22 17:36:56 -07001926 try {
1927 tp.load(r);
Dave Borte9bfd9b2009-05-04 14:35:23 -07001928 } catch (Exception ex) {
1929 throw new RuntimeException("Problem loading " + file, ex);
1930 } finally {
1931 try {
1932 r.close();
1933 } catch (IOException ex) {
1934 // Ignore this error.
1935 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07001936 }
1937 }
1938
1939 debugProperties = tp.isEmpty() ? null : tp;
1940 } else {
1941 debugProperties = null;
1942 }
1943 }
1944
1945
1946 /**
1947 * Returns true if the type of the field matches the specified class.
1948 * Handles the case where the class is, e.g., java.lang.Boolean, but
1949 * the field is of the primitive "boolean" type. Also handles all of
1950 * the java.lang.Number subclasses.
1951 */
1952 private static boolean fieldTypeMatches(Field field, Class<?> cl) {
1953 Class<?> fieldClass = field.getType();
1954 if (fieldClass == cl) {
1955 return true;
1956 }
1957 Field primitiveTypeField;
1958 try {
1959 /* All of the classes we care about (Boolean, Integer, etc.)
1960 * have a Class field called "TYPE" that points to the corresponding
1961 * primitive class.
1962 */
1963 primitiveTypeField = cl.getField("TYPE");
1964 } catch (NoSuchFieldException ex) {
1965 return false;
1966 }
1967 try {
Dave Borte9bfd9b2009-05-04 14:35:23 -07001968 return fieldClass == (Class<?>) primitiveTypeField.get(null);
Dave Bort1ce5bd32009-04-22 17:36:56 -07001969 } catch (IllegalAccessException ex) {
1970 return false;
1971 }
1972 }
1973
1974
1975 /**
1976 * Looks up the property that corresponds to the field, and sets the field's value
1977 * if the types match.
1978 */
Dave Borte9bfd9b2009-05-04 14:35:23 -07001979 private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
1980 final String propertyName) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001981 if (field.getType() == java.lang.String.class) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07001982 int stringInfo = properties.getStringInfo(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07001983 switch (stringInfo) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07001984 case TypedProperties.STRING_SET:
1985 // Handle as usual below.
1986 break;
1987 case TypedProperties.STRING_NULL:
1988 try {
1989 field.set(null, null); // null object for static fields; null string
1990 } catch (IllegalAccessException ex) {
1991 throw new IllegalArgumentException(
1992 "Cannot set field for " + propertyName, ex);
1993 }
1994 return;
1995 case TypedProperties.STRING_NOT_SET:
1996 return;
1997 case TypedProperties.STRING_TYPE_MISMATCH:
Dave Bort1ce5bd32009-04-22 17:36:56 -07001998 throw new IllegalArgumentException(
Dave Borte9bfd9b2009-05-04 14:35:23 -07001999 "Type of " + propertyName + " " +
2000 " does not match field type (" + field.getType() + ")");
2001 default:
2002 throw new IllegalStateException(
2003 "Unexpected getStringInfo(" + propertyName + ") return value " +
2004 stringInfo);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002005 }
2006 }
Dave Borte9bfd9b2009-05-04 14:35:23 -07002007 Object value = properties.get(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002008 if (value != null) {
2009 if (!fieldTypeMatches(field, value.getClass())) {
2010 throw new IllegalArgumentException(
2011 "Type of " + propertyName + " (" + value.getClass() + ") " +
2012 " does not match field type (" + field.getType() + ")");
2013 }
2014 try {
2015 field.set(null, value); // null object for static fields
2016 } catch (IllegalAccessException ex) {
2017 throw new IllegalArgumentException(
2018 "Cannot set field for " + propertyName, ex);
2019 }
2020 }
2021 }
2022
2023
2024 /**
Romain Guyc4b11a72009-05-13 15:46:37 -07002025 * Equivalent to <code>setFieldsOn(cl, false)</code>.
2026 *
2027 * @see #setFieldsOn(Class, boolean)
Romain Guyd4103d02009-05-14 12:24:21 -07002028 *
2029 * @hide
Romain Guyc4b11a72009-05-13 15:46:37 -07002030 */
2031 public static void setFieldsOn(Class<?> cl) {
2032 setFieldsOn(cl, false);
2033 }
2034
2035 /**
Dave Bort1ce5bd32009-04-22 17:36:56 -07002036 * Reflectively sets static fields of a class based on internal debugging
Joe Onorato43a17652011-04-06 19:22:23 -07002037 * properties. This method is a no-op if false is
Dave Bort1ce5bd32009-04-22 17:36:56 -07002038 * false.
2039 * <p>
Joe Onorato43a17652011-04-06 19:22:23 -07002040 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will
Dave Bort1ce5bd32009-04-22 17:36:56 -07002041 * always be false in release builds. This API is typically only useful
2042 * for platform developers.
2043 * </p>
2044 * Class setup: define a class whose only fields are non-final, static
2045 * primitive types (except for "char") or Strings. In a static block
2046 * after the field definitions/initializations, pass the class to
Romain Guyc4b11a72009-05-13 15:46:37 -07002047 * this method, Debug.setFieldsOn(). Example:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002048 * <pre>
2049 * package com.example;
2050 *
2051 * import android.os.Debug;
2052 *
2053 * public class MyDebugVars {
2054 * public static String s = "a string";
2055 * public static String s2 = "second string";
2056 * public static String ns = null;
2057 * public static boolean b = false;
2058 * public static int i = 5;
Romain Guyc4b11a72009-05-13 15:46:37 -07002059 * @Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002060 * public static float f = 0.1f;
Romain Guyc4b11a72009-05-13 15:46:37 -07002061 * @@Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002062 * public static double d = 0.5d;
2063 *
2064 * // This MUST appear AFTER all fields are defined and initialized!
2065 * static {
Romain Guyc4b11a72009-05-13 15:46:37 -07002066 * // Sets all the fields
Dave Borte9bfd9b2009-05-04 14:35:23 -07002067 * Debug.setFieldsOn(MyDebugVars.class);
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02002068 *
Romain Guyc4b11a72009-05-13 15:46:37 -07002069 * // Sets only the fields annotated with @Debug.DebugProperty
2070 * // Debug.setFieldsOn(MyDebugVars.class, true);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002071 * }
2072 * }
2073 * </pre>
Dave Borte9bfd9b2009-05-04 14:35:23 -07002074 * setFieldsOn() may override the value of any field in the class based
Dave Bort1ce5bd32009-04-22 17:36:56 -07002075 * on internal properties that are fixed at boot time.
2076 * <p>
2077 * These properties are only set during platform debugging, and are not
2078 * meant to be used as a general-purpose properties store.
2079 *
2080 * {@hide}
2081 *
2082 * @param cl The class to (possibly) modify
Romain Guyc4b11a72009-05-13 15:46:37 -07002083 * @param partial If false, sets all static fields, otherwise, only set
2084 * fields with the {@link android.os.Debug.DebugProperty}
2085 * annotation
Dave Bort1ce5bd32009-04-22 17:36:56 -07002086 * @throws IllegalArgumentException if any fields are final or non-static,
2087 * or if the type of the field does not match the type of
2088 * the internal debugging property value.
2089 */
Romain Guyc4b11a72009-05-13 15:46:37 -07002090 public static void setFieldsOn(Class<?> cl, boolean partial) {
Joe Onorato43a17652011-04-06 19:22:23 -07002091 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002092 if (debugProperties != null) {
2093 /* Only look for fields declared directly by the class,
2094 * so we don't mysteriously change static fields in superclasses.
2095 */
2096 for (Field field : cl.getDeclaredFields()) {
Romain Guyc4b11a72009-05-13 15:46:37 -07002097 if (!partial || field.getAnnotation(DebugProperty.class) != null) {
2098 final String propertyName = cl.getName() + "." + field.getName();
2099 boolean isStatic = Modifier.isStatic(field.getModifiers());
2100 boolean isFinal = Modifier.isFinal(field.getModifiers());
2101
2102 if (!isStatic || isFinal) {
2103 throw new IllegalArgumentException(propertyName +
2104 " must be static and non-final");
2105 }
2106 modifyFieldIfSet(field, debugProperties, propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002107 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002108 }
2109 }
2110 } else {
Dan Egnor3eda9792010-03-05 13:28:36 -08002111 Log.wtf(TAG,
Dave Borte9bfd9b2009-05-04 14:35:23 -07002112 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
Dave Bort1ce5bd32009-04-22 17:36:56 -07002113 ") called in non-DEBUG build");
2114 }
2115 }
Romain Guyc4b11a72009-05-13 15:46:37 -07002116
2117 /**
2118 * Annotation to put on fields you want to set with
2119 * {@link Debug#setFieldsOn(Class, boolean)}.
2120 *
2121 * @hide
2122 */
2123 @Target({ ElementType.FIELD })
2124 @Retention(RetentionPolicy.RUNTIME)
2125 public @interface DebugProperty {
2126 }
Dan Egnor3eda9792010-03-05 13:28:36 -08002127
2128 /**
2129 * Get a debugging dump of a system service by name.
2130 *
2131 * <p>Most services require the caller to hold android.permission.DUMP.
2132 *
2133 * @param name of the service to dump
2134 * @param fd to write dump output to (usually an output log file)
2135 * @param args to pass to the service's dump method, may be null
2136 * @return true if the service was dumped successfully, false if
2137 * the service could not be found or had an error while dumping
2138 */
2139 public static boolean dumpService(String name, FileDescriptor fd, String[] args) {
2140 IBinder service = ServiceManager.getService(name);
2141 if (service == null) {
2142 Log.e(TAG, "Can't find service to dump: " + name);
2143 return false;
2144 }
2145
2146 try {
2147 service.dump(fd, args);
2148 return true;
2149 } catch (RemoteException e) {
2150 Log.e(TAG, "Can't dump service: " + name, e);
2151 return false;
2152 }
2153 }
Craig Mautnera51a9562012-04-17 17:05:26 -07002154
2155 /**
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002156 * Have the stack traces of the given native process dumped to the
2157 * specified file. Will be appended to the file.
2158 * @hide
2159 */
2160 public static native void dumpNativeBacktraceToFile(int pid, String file);
2161
2162 /**
Colin Crossc4fb5f92016-02-02 16:51:15 -08002163 * Get description of unreachable native memory.
2164 * @param limit the number of leaks to provide info on, 0 to only get a summary.
2165 * @param contents true to include a hex dump of the contents of unreachable memory.
2166 * @return the String containing a description of unreachable memory.
2167 * @hide */
2168 public static native String getUnreachableMemory(int limit, boolean contents);
2169
2170 /**
Craig Mautnera51a9562012-04-17 17:05:26 -07002171 * Return a String describing the calling method and location at a particular stack depth.
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07002172 * @param callStack the Thread stack
Craig Mautnera51a9562012-04-17 17:05:26 -07002173 * @param depth the depth of stack to return information for.
2174 * @return the String describing the caller at that depth.
2175 */
2176 private static String getCaller(StackTraceElement callStack[], int depth) {
2177 // callStack[4] is the caller of the method that called getCallers()
2178 if (4 + depth >= callStack.length) {
2179 return "<bottom of call stack>";
2180 }
2181 StackTraceElement caller = callStack[4 + depth];
2182 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2183 }
2184
2185 /**
2186 * Return a string consisting of methods and locations at multiple call stack levels.
2187 * @param depth the number of levels to return, starting with the immediate caller.
2188 * @return a string describing the call stack.
2189 * {@hide}
2190 */
2191 public static String getCallers(final int depth) {
2192 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2193 StringBuffer sb = new StringBuffer();
2194 for (int i = 0; i < depth; i++) {
2195 sb.append(getCaller(callStack, i)).append(" ");
2196 }
2197 return sb.toString();
2198 }
2199
2200 /**
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07002201 * Return a string consisting of methods and locations at multiple call stack levels.
2202 * @param depth the number of levels to return, starting with the immediate caller.
2203 * @return a string describing the call stack.
2204 * {@hide}
2205 */
2206 public static String getCallers(final int start, int depth) {
2207 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2208 StringBuffer sb = new StringBuffer();
2209 depth += start;
2210 for (int i = start; i < depth; i++) {
2211 sb.append(getCaller(callStack, i)).append(" ");
2212 }
2213 return sb.toString();
2214 }
2215
2216 /**
Dianne Hackbornef03a7f2012-10-29 18:46:52 -07002217 * Like {@link #getCallers(int)}, but each location is append to the string
2218 * as a new line with <var>linePrefix</var> in front of it.
2219 * @param depth the number of levels to return, starting with the immediate caller.
2220 * @param linePrefix prefix to put in front of each location.
2221 * @return a string describing the call stack.
2222 * {@hide}
2223 */
2224 public static String getCallers(final int depth, String linePrefix) {
2225 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2226 StringBuffer sb = new StringBuffer();
2227 for (int i = 0; i < depth; i++) {
2228 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n");
2229 }
2230 return sb.toString();
2231 }
2232
2233 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08002234 * @return a String describing the immediate caller of the calling method.
Craig Mautnera51a9562012-04-17 17:05:26 -07002235 * {@hide}
2236 */
2237 public static String getCaller() {
2238 return getCaller(Thread.currentThread().getStackTrace(), 0);
2239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240}