blob: 175d883da29de179406629633da483f2733fcb77 [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
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060022import android.app.AppGlobals;
23import android.content.Context;
Dave Bort1ce5bd32009-04-22 17:36:56 -070024import android.util.Log;
25
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060026import java.io.File;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070027import java.io.FileDescriptor;
Dave Bort1ce5bd32009-04-22 17:36:56 -070028import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import java.io.FileOutputStream;
Dave Bort1ce5bd32009-04-22 17:36:56 -070030import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import java.io.PrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070033import java.io.Reader;
34import java.lang.reflect.Field;
35import java.lang.reflect.Modifier;
Romain Guyc4b11a72009-05-13 15:46:37 -070036import java.lang.annotation.Target;
37import java.lang.annotation.ElementType;
38import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
Richard Uhler350e6dc2015-05-18 10:48:52 -070040import java.util.HashMap;
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -070041import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43import org.apache.harmony.dalvik.ddmc.Chunk;
44import org.apache.harmony.dalvik.ddmc.ChunkHandler;
45import org.apache.harmony.dalvik.ddmc.DdmServer;
46
Dan Bornstein9f315542010-11-19 18:12:55 -080047import dalvik.bytecode.OpcodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import dalvik.system.VMDebug;
49
50
51/**
Ian Rogersfe067a42013-02-22 19:59:23 -080052 * Provides various debugging methods for Android applications, including
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 * tracing and allocation counts.
54 * <p><strong>Logging Trace Files</strong></p>
55 * <p>Debug can create log files that give details about an application, such as
56 * a call stack and start/stop times for any running methods. See <a
57href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
58 * information about reading trace files. To start logging trace files, call one
59 * of the startMethodTracing() methods. To stop tracing, call
60 * {@link #stopMethodTracing()}.
61 */
62public final class Debug
63{
Dan Egnor3eda9792010-03-05 13:28:36 -080064 private static final String TAG = "Debug";
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 /**
67 * Flags for startMethodTracing(). These can be ORed together.
68 *
69 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
70 * trace key file.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080071 *
72 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080074 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS;
76
77 /**
78 * Flags for printLoadedClasses(). Default behavior is to only show
79 * the class name.
80 */
81 public static final int SHOW_FULL_DETAIL = 1;
82 public static final int SHOW_CLASSLOADER = (1 << 1);
83 public static final int SHOW_INITIALIZED = (1 << 2);
84
85 // set/cleared by waitForDebugger()
86 private static volatile boolean mWaiting = false;
87
88 private Debug() {}
89
90 /*
91 * How long to wait for the debugger to finish sending requests. I've
92 * seen this hit 800msec on the device while waiting for a response
93 * to travel over USB and get processed, so we take that and add
94 * half a second.
95 */
96 private static final int MIN_DEBUGGER_IDLE = 1300; // msec
97
98 /* how long to sleep when polling for activity */
99 private static final int SPIN_DELAY = 200; // msec
100
101 /**
102 * Default trace file path and file
103 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 private static final String DEFAULT_TRACE_BODY = "dmtrace";
105 private static final String DEFAULT_TRACE_EXTENSION = ".trace";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 /**
108 * This class is used to retrieved various statistics about the memory mappings for this
justinmuller64551382013-10-16 22:06:55 -0600109 * 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 -0800110 */
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700111 public static class MemoryInfo implements Parcelable {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700112 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public int dalvikPss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700114 /** The proportional set size that is swappable for dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700115 /** @hide We may want to expose this, eventually. */
116 public int dalvikSwappablePss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700117 /** The private dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public int dalvikPrivateDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700119 /** The shared dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public int dalvikSharedDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700121 /** The private clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700122 /** @hide We may want to expose this, eventually. */
123 public int dalvikPrivateClean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700124 /** The shared clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700125 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700126 public int dalvikSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700127 /** The dirty dalvik pages that have been swapped out. */
128 /** @hide We may want to expose this, eventually. */
129 public int dalvikSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800130 /** The dirty dalvik pages that have been swapped out, proportional. */
131 /** @hide We may want to expose this, eventually. */
132 public int dalvikSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134 /** The proportional set size for the native heap. */
135 public int nativePss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700136 /** The proportional set size that is swappable for the native heap. */
137 /** @hide We may want to expose this, eventually. */
138 public int nativeSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 /** The private dirty pages used by the native heap. */
140 public int nativePrivateDirty;
141 /** The shared dirty pages used by the native heap. */
142 public int nativeSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700143 /** The private clean pages used by the native heap. */
144 /** @hide We may want to expose this, eventually. */
145 public int nativePrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700146 /** The shared clean pages used by the native heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700147 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700148 public int nativeSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700149 /** The dirty native pages that have been swapped out. */
150 /** @hide We may want to expose this, eventually. */
151 public int nativeSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800152 /** The dirty native pages that have been swapped out, proportional. */
153 /** @hide We may want to expose this, eventually. */
154 public int nativeSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 /** The proportional set size for everything else. */
157 public int otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700158 /** The proportional set size that is swappable for everything else. */
159 /** @hide We may want to expose this, eventually. */
160 public int otherSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /** The private dirty pages used by everything else. */
162 public int otherPrivateDirty;
163 /** The shared dirty pages used by everything else. */
164 public int otherSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700165 /** The private clean pages used by everything else. */
166 /** @hide We may want to expose this, eventually. */
167 public int otherPrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700168 /** The shared clean pages used by everything else. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700169 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700170 public int otherSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700171 /** The dirty pages used by anyting else that have been swapped out. */
172 /** @hide We may want to expose this, eventually. */
173 public int otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800174 /** The dirty pages used by anyting else that have been swapped out, proportional. */
175 /** @hide We may want to expose this, eventually. */
176 public int otherSwappedOutPss;
177
178 /** Whether the kernel reports proportional swap usage */
179 /** @hide */
180 public boolean hasSwappedOutPss;
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200181
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700182 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700183 public static final int HEAP_UNKNOWN = 0;
184 /** @hide */
185 public static final int HEAP_DALVIK = 1;
186 /** @hide */
187 public static final int HEAP_NATIVE = 2;
188
189 /** @hide */
190 public static final int OTHER_DALVIK_OTHER = 0;
191 /** @hide */
192 public static final int OTHER_STACK = 1;
193 /** @hide */
194 public static final int OTHER_CURSOR = 2;
195 /** @hide */
196 public static final int OTHER_ASHMEM = 3;
197 /** @hide */
198 public static final int OTHER_GL_DEV = 4;
199 /** @hide */
200 public static final int OTHER_UNKNOWN_DEV = 5;
201 /** @hide */
202 public static final int OTHER_SO = 6;
203 /** @hide */
204 public static final int OTHER_JAR = 7;
205 /** @hide */
206 public static final int OTHER_APK = 8;
207 /** @hide */
208 public static final int OTHER_TTF = 9;
209 /** @hide */
210 public static final int OTHER_DEX = 10;
211 /** @hide */
212 public static final int OTHER_OAT = 11;
213 /** @hide */
214 public static final int OTHER_ART = 12;
215 /** @hide */
216 public static final int OTHER_UNKNOWN_MAP = 13;
217 /** @hide */
218 public static final int OTHER_GRAPHICS = 14;
219 /** @hide */
220 public static final int OTHER_GL = 15;
221 /** @hide */
222 public static final int OTHER_OTHER_MEMTRACK = 16;
223
224 /** @hide */
225 public static final int OTHER_DALVIK_NORMAL = 17;
226 /** @hide */
227 public static final int OTHER_DALVIK_LARGE = 18;
228 /** @hide */
229 public static final int OTHER_DALVIK_LINEARALLOC = 19;
230 /** @hide */
231 public static final int OTHER_DALVIK_ACCOUNTING = 20;
232 /** @hide */
233 public static final int OTHER_DALVIK_CODE_CACHE = 21;
234 /** @hide */
235 public static final int OTHER_DALVIK_ZYGOTE = 22;
236 /** @hide */
237 public static final int OTHER_DALVIK_NON_MOVING = 23;
238 /** @hide */
239 public static final int OTHER_DALVIK_INDIRECT_REFERENCE_TABLE = 24;
240
241 /** @hide */
Dianne Hackborn18429302014-11-03 13:58:11 -0800242 public static final int NUM_OTHER_STATS = 17;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700243
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700244 /** @hide */
Mathieu Chartier25c5e2b2014-12-08 16:20:26 -0800245 public static final int NUM_DVK_STATS = 8;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700246
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700247 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800248 public static final int NUM_CATEGORIES = 8;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700249
250 /** @hide */
251 public static final int offsetPss = 0;
252 /** @hide */
253 public static final int offsetSwappablePss = 1;
254 /** @hide */
255 public static final int offsetPrivateDirty = 2;
256 /** @hide */
257 public static final int offsetSharedDirty = 3;
258 /** @hide */
259 public static final int offsetPrivateClean = 4;
260 /** @hide */
261 public static final int offsetSharedClean = 5;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700262 /** @hide */
263 public static final int offsetSwappedOut = 6;
Martijn Coenene0764852016-01-07 17:04:22 -0800264 /** @hide */
265 public static final int offsetSwappedOutPss = 7;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700266
267 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700268
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700269 public MemoryInfo() {
270 }
271
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700272 /**
273 * Return total PSS memory usage in kB.
274 */
275 public int getTotalPss() {
Martijn Coenene0764852016-01-07 17:04:22 -0800276 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700277 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200278
Dianne Hackbornc8230512013-07-13 21:32:12 -0700279 /**
280 * @hide Return total PSS memory usage in kB.
281 */
282 public int getTotalUss() {
283 return dalvikPrivateClean + dalvikPrivateDirty
284 + nativePrivateClean + nativePrivateDirty
285 + otherPrivateClean + otherPrivateDirty;
286 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700287
288 /**
Martijn Coenene0764852016-01-07 17:04:22 -0800289 * Return total PSS memory usage in kB mapping a file of one of the following extension:
290 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art .
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700291 */
292 public int getTotalSwappablePss() {
293 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss;
294 }
295
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700296 /**
297 * Return total private dirty memory usage in kB.
298 */
299 public int getTotalPrivateDirty() {
300 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty;
301 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200302
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700303 /**
304 * Return total shared dirty memory usage in kB.
305 */
306 public int getTotalSharedDirty() {
307 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty;
308 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200309
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700310 /**
311 * Return total shared clean memory usage in kB.
312 */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700313 public int getTotalPrivateClean() {
314 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean;
315 }
316
317 /**
318 * Return total shared clean memory usage in kB.
319 */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700320 public int getTotalSharedClean() {
321 return dalvikSharedClean + nativeSharedClean + otherSharedClean;
322 }
323
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700324 /**
325 * Return total swapped out memory in kB.
326 * @hide
327 */
328 public int getTotalSwappedOut() {
329 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut;
330 }
331
Martijn Coenene0764852016-01-07 17:04:22 -0800332 /**
333 * Return total swapped out memory in kB, proportional.
334 * @hide
335 */
336 public int getTotalSwappedOutPss() {
337 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss;
338 }
339
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700340 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700341 public int getOtherPss(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700342 return otherStats[which*NUM_CATEGORIES + offsetPss];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700343 }
344
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700345
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700346 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700347 public int getOtherSwappablePss(int which) {
348 return otherStats[which*NUM_CATEGORIES + offsetSwappablePss];
349 }
350
351
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700352 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700353 public int getOtherPrivateDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700354 return otherStats[which*NUM_CATEGORIES + offsetPrivateDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700355 }
356
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700357 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700358 public int getOtherSharedDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700359 return otherStats[which*NUM_CATEGORIES + offsetSharedDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700360 }
361
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700362 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700363 public int getOtherPrivateClean(int which) {
364 return otherStats[which*NUM_CATEGORIES + offsetPrivateClean];
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700365 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700366
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700367 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700368 public int getOtherPrivate(int which) {
369 return getOtherPrivateClean(which) + getOtherPrivateDirty(which);
370 }
371
372 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700373 public int getOtherSharedClean(int which) {
374 return otherStats[which*NUM_CATEGORIES + offsetSharedClean];
375 }
376
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700377 /** @hide */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700378 public int getOtherSwappedOut(int which) {
379 return otherStats[which*NUM_CATEGORIES + offsetSwappedOut];
380 }
381
382 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800383 public int getOtherSwappedOutPss(int which) {
384 return otherStats[which*NUM_CATEGORIES + offsetSwappedOutPss];
385 }
386
387 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700388 public static String getOtherLabel(int which) {
389 switch (which) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700390 case OTHER_DALVIK_OTHER: return "Dalvik Other";
391 case OTHER_STACK: return "Stack";
392 case OTHER_CURSOR: return "Cursor";
393 case OTHER_ASHMEM: return "Ashmem";
394 case OTHER_GL_DEV: return "Gfx dev";
395 case OTHER_UNKNOWN_DEV: return "Other dev";
396 case OTHER_SO: return ".so mmap";
397 case OTHER_JAR: return ".jar mmap";
398 case OTHER_APK: return ".apk mmap";
399 case OTHER_TTF: return ".ttf mmap";
400 case OTHER_DEX: return ".dex mmap";
401 case OTHER_OAT: return ".oat mmap";
402 case OTHER_ART: return ".art mmap";
403 case OTHER_UNKNOWN_MAP: return "Other mmap";
404 case OTHER_GRAPHICS: return "EGL mtrack";
405 case OTHER_GL: return "GL mtrack";
406 case OTHER_OTHER_MEMTRACK: return "Other mtrack";
407 case OTHER_DALVIK_NORMAL: return ".Heap";
408 case OTHER_DALVIK_LARGE: return ".LOS";
409 case OTHER_DALVIK_LINEARALLOC: return ".LinearAlloc";
410 case OTHER_DALVIK_ACCOUNTING: return ".GC";
411 case OTHER_DALVIK_CODE_CACHE: return ".JITCache";
412 case OTHER_DALVIK_ZYGOTE: return ".Zygote";
413 case OTHER_DALVIK_NON_MOVING: return ".NonMoving";
414 case OTHER_DALVIK_INDIRECT_REFERENCE_TABLE: return ".IndirectRef";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700415 default: return "????";
416 }
417 }
418
Richard Uhler350e6dc2015-05-18 10:48:52 -0700419 /**
420 * Returns the value of a particular memory statistic or {@code null} if no
421 * such memory statistic exists.
422 *
423 * <p>The following table lists the memory statistics that are supported.
424 * Note that memory statistics may be added or removed in a future API level.</p>
425 *
426 * <table>
427 * <thead>
428 * <tr>
429 * <th>Memory statistic name</th>
430 * <th>Meaning</th>
431 * <th>Example</th>
432 * <th>Supported (API Levels)</th>
433 * </tr>
434 * </thead>
435 * <tbody>
436 * <tr>
437 * <td>summary.java-heap</td>
438 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field
439 * in the App Summary section output by dumpsys meminfo.</td>
440 * <td>{@code 1442}</td>
441 * <td>23</td>
442 * </tr>
443 * <tr>
444 * <td>summary.native-heap</td>
445 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap
446 * field in the App Summary section output by dumpsys meminfo.</td>
447 * <td>{@code 1442}</td>
448 * <td>23</td>
449 * </tr>
450 * <tr>
451 * <td>summary.code</td>
452 * <td>The memory usage for static code and resources in kB. This corresponds to
453 * the Code field in the App Summary section output by dumpsys meminfo.</td>
454 * <td>{@code 1442}</td>
455 * <td>23</td>
456 * </tr>
457 * <tr>
458 * <td>summary.stack</td>
459 * <td>The stack usage in kB. This corresponds to the Stack field in the
460 * App Summary section output by dumpsys meminfo.</td>
461 * <td>{@code 1442}</td>
462 * <td>23</td>
463 * </tr>
464 * <tr>
465 * <td>summary.graphics</td>
466 * <td>The graphics usage in kB. This corresponds to the Graphics field in the
467 * App Summary section output by dumpsys meminfo.</td>
468 * <td>{@code 1442}</td>
469 * <td>23</td>
470 * </tr>
471 * <tr>
472 * <td>summary.private-other</td>
473 * <td>Other private memory usage in kB. This corresponds to the Private Other
474 * field output in the App Summary section by dumpsys meminfo.</td>
475 * <td>{@code 1442}</td>
476 * <td>23</td>
477 * </tr>
478 * <tr>
479 * <td>summary.system</td>
480 * <td>Shared and system memory usage in kB. This corresponds to the System
481 * field output in the App Summary section by dumpsys meminfo.</td>
482 * <td>{@code 1442}</td>
483 * <td>23</td>
484 * </tr>
485 * <tr>
486 * <td>summary.total-pss</td>
487 * <td>Total PPS memory usage in kB.</td>
488 * <td>{@code 1442}</td>
489 * <td>23</td>
490 * </tr>
491 * <tr>
492 * <td>summary.total-swap</td>
493 * <td>Total swap usage in kB.</td>
494 * <td>{@code 1442}</td>
495 * <td>23</td>
496 * </tr>
497 * </tbody>
498 * </table>
499 */
Dianne Hackbornb02ce292015-10-12 15:14:16 -0700500 public String getMemoryStat(String statName) {
Richard Uhler350e6dc2015-05-18 10:48:52 -0700501 switch(statName) {
502 case "summary.java-heap":
503 return Integer.toString(getSummaryJavaHeap());
504 case "summary.native-heap":
505 return Integer.toString(getSummaryNativeHeap());
506 case "summary.code":
507 return Integer.toString(getSummaryCode());
508 case "summary.stack":
509 return Integer.toString(getSummaryStack());
510 case "summary.graphics":
511 return Integer.toString(getSummaryGraphics());
512 case "summary.private-other":
513 return Integer.toString(getSummaryPrivateOther());
514 case "summary.system":
515 return Integer.toString(getSummarySystem());
516 case "summary.total-pss":
517 return Integer.toString(getSummaryTotalPss());
518 case "summary.total-swap":
519 return Integer.toString(getSummaryTotalSwap());
520 default:
521 return null;
522 }
523 }
524
525 /**
526 * Returns a map of the names/values of the memory statistics
527 * that {@link #getMemoryStat(String)} supports.
528 *
529 * @return a map of the names/values of the supported memory statistics.
530 */
531 public Map<String, String> getMemoryStats() {
532 Map<String, String> stats = new HashMap<String, String>();
533 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap()));
534 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap()));
535 stats.put("summary.code", Integer.toString(getSummaryCode()));
536 stats.put("summary.stack", Integer.toString(getSummaryStack()));
537 stats.put("summary.graphics", Integer.toString(getSummaryGraphics()));
538 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther()));
539 stats.put("summary.system", Integer.toString(getSummarySystem()));
540 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss()));
541 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap()));
542 return stats;
543 }
544
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700545 /**
546 * Pss of Java Heap bytes in KB due to the application.
547 * Notes:
548 * * OTHER_ART is the boot image. Anything private here is blamed on
549 * the application, not the system.
550 * * dalvikPrivateDirty includes private zygote, which means the
551 * application dirtied something allocated by the zygote. We blame
552 * the application for that memory, not the system.
553 * * Does not include OTHER_DALVIK_OTHER, which is considered VM
554 * Overhead and lumped into Private Other.
555 * * We don't include dalvikPrivateClean, because there should be no
556 * such thing as private clean for the Java Heap.
557 * @hide
558 */
559 public int getSummaryJavaHeap() {
560 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART);
561 }
562
563 /**
564 * Pss of Native Heap bytes in KB due to the application.
565 * Notes:
566 * * Includes private dirty malloc space.
567 * * We don't include nativePrivateClean, because there should be no
568 * such thing as private clean for the Native Heap.
569 * @hide
570 */
571 public int getSummaryNativeHeap() {
572 return nativePrivateDirty;
573 }
574
575 /**
576 * Pss of code and other static resource bytes in KB due to
577 * the application.
578 * @hide
579 */
580 public int getSummaryCode() {
581 return getOtherPrivate(OTHER_SO)
582 + getOtherPrivate(OTHER_JAR)
583 + getOtherPrivate(OTHER_APK)
584 + getOtherPrivate(OTHER_TTF)
585 + getOtherPrivate(OTHER_DEX)
586 + getOtherPrivate(OTHER_OAT);
587 }
588
589 /**
590 * Pss in KB of the stack due to the application.
591 * Notes:
592 * * Includes private dirty stack, which includes both Java and Native
593 * stack.
594 * * Does not include private clean stack, because there should be no
595 * such thing as private clean for the stack.
596 * @hide
597 */
598 public int getSummaryStack() {
599 return getOtherPrivateDirty(OTHER_STACK);
600 }
601
602 /**
603 * Pss in KB of graphics due to the application.
604 * Notes:
605 * * Includes private Gfx, EGL, and GL.
606 * * Warning: These numbers can be misreported by the graphics drivers.
607 * * We don't include shared graphics. It may make sense to, because
608 * shared graphics are likely buffers due to the application
609 * anyway, but it's simpler to implement to just group all shared
610 * memory into the System category.
611 * @hide
612 */
613 public int getSummaryGraphics() {
614 return getOtherPrivate(OTHER_GL_DEV)
615 + getOtherPrivate(OTHER_GRAPHICS)
616 + getOtherPrivate(OTHER_GL);
617 }
618
619 /**
620 * Pss in KB due to the application that haven't otherwise been
621 * accounted for.
622 * @hide
623 */
624 public int getSummaryPrivateOther() {
625 return getTotalPrivateClean()
626 + getTotalPrivateDirty()
627 - getSummaryJavaHeap()
628 - getSummaryNativeHeap()
629 - getSummaryCode()
630 - getSummaryStack()
631 - getSummaryGraphics();
632 }
633
634 /**
635 * Pss in KB due to the system.
636 * Notes:
637 * * Includes all shared memory.
638 * @hide
639 */
640 public int getSummarySystem() {
641 return getTotalPss()
642 - getTotalPrivateClean()
643 - getTotalPrivateDirty();
644 }
645
646 /**
647 * Total Pss in KB.
648 * @hide
649 */
650 public int getSummaryTotalPss() {
651 return getTotalPss();
652 }
653
654 /**
655 * Total Swap in KB.
656 * Notes:
657 * * Some of this memory belongs in other categories, but we don't
658 * know if the Swap memory is shared or private, so we don't know
659 * what to blame on the application and what on the system.
660 * For now, just lump all the Swap in one place.
Martijn Coenene0764852016-01-07 17:04:22 -0800661 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()}
662 * will report the application proportional Swap.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700663 * @hide
664 */
665 public int getSummaryTotalSwap() {
666 return getTotalSwappedOut();
667 }
668
Martijn Coenene0764852016-01-07 17:04:22 -0800669 /**
670 * Total proportional Swap in KB.
671 * Notes:
672 * * Always 0 if {@link #hasSwappedOutPss} is false.
673 * @hide
674 */
675 public int getSummaryTotalSwapPss() {
676 return getTotalSwappedOutPss();
677 }
678
Dianne Hackbornef0a4022016-05-11 14:21:07 -0700679 /**
680 * Return true if the kernel is reporting pss swapped out... that is, if
681 * {@link #getSummaryTotalSwapPss()} will return non-0 values.
682 * @hide
683 */
684 public boolean hasSwappedOutPss() {
685 return hasSwappedOutPss;
686 }
687
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700688 public int describeContents() {
689 return 0;
690 }
691
692 public void writeToParcel(Parcel dest, int flags) {
693 dest.writeInt(dalvikPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700694 dest.writeInt(dalvikSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700695 dest.writeInt(dalvikPrivateDirty);
696 dest.writeInt(dalvikSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700697 dest.writeInt(dalvikPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700698 dest.writeInt(dalvikSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700699 dest.writeInt(dalvikSwappedOut);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700700 dest.writeInt(nativePss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700701 dest.writeInt(nativeSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700702 dest.writeInt(nativePrivateDirty);
703 dest.writeInt(nativeSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700704 dest.writeInt(nativePrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700705 dest.writeInt(nativeSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700706 dest.writeInt(nativeSwappedOut);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700707 dest.writeInt(otherPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700708 dest.writeInt(otherSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700709 dest.writeInt(otherPrivateDirty);
710 dest.writeInt(otherSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700711 dest.writeInt(otherPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700712 dest.writeInt(otherSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700713 dest.writeInt(otherSwappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800714 dest.writeInt(hasSwappedOutPss ? 1 : 0);
715 dest.writeInt(otherSwappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700716 dest.writeIntArray(otherStats);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700717 }
718
719 public void readFromParcel(Parcel source) {
720 dalvikPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700721 dalvikSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700722 dalvikPrivateDirty = source.readInt();
723 dalvikSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700724 dalvikPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700725 dalvikSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700726 dalvikSwappedOut = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700727 nativePss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700728 nativeSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700729 nativePrivateDirty = source.readInt();
730 nativeSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700731 nativePrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700732 nativeSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700733 nativeSwappedOut = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700734 otherPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700735 otherSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700736 otherPrivateDirty = source.readInt();
737 otherSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700738 otherPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700739 otherSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700740 otherSwappedOut = source.readInt();
Martijn Coenene0764852016-01-07 17:04:22 -0800741 hasSwappedOutPss = source.readInt() != 0;
742 otherSwappedOutPss = source.readInt();
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700743 otherStats = source.createIntArray();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700744 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200745
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700746 public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
747 public MemoryInfo createFromParcel(Parcel source) {
748 return new MemoryInfo(source);
749 }
750 public MemoryInfo[] newArray(int size) {
751 return new MemoryInfo[size];
752 }
753 };
754
755 private MemoryInfo(Parcel source) {
756 readFromParcel(source);
757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
760
761 /**
762 * Wait until a debugger attaches. As soon as the debugger attaches,
763 * this returns, so you will need to place a breakpoint after the
764 * waitForDebugger() call if you want to start tracing immediately.
765 */
766 public static void waitForDebugger() {
767 if (!VMDebug.isDebuggingEnabled()) {
768 //System.out.println("debugging not enabled, not waiting");
769 return;
770 }
771 if (isDebuggerConnected())
772 return;
773
774 // if DDMS is listening, inform them of our plight
775 System.out.println("Sending WAIT chunk");
776 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger"
777 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
778 DdmServer.sendChunk(waitChunk);
779
780 mWaiting = true;
781 while (!isDebuggerConnected()) {
782 try { Thread.sleep(SPIN_DELAY); }
783 catch (InterruptedException ie) {}
784 }
785 mWaiting = false;
786
787 System.out.println("Debugger has connected");
788
789 /*
790 * There is no "ready to go" signal from the debugger, and we're
791 * not allowed to suspend ourselves -- the debugger expects us to
792 * be running happily, and gets confused if we aren't. We need to
793 * allow the debugger a chance to set breakpoints before we start
794 * running again.
795 *
796 * Sit and spin until the debugger has been idle for a short while.
797 */
798 while (true) {
799 long delta = VMDebug.lastDebuggerActivity();
800 if (delta < 0) {
801 System.out.println("debugger detached?");
802 break;
803 }
804
805 if (delta < MIN_DEBUGGER_IDLE) {
806 System.out.println("waiting for debugger to settle...");
807 try { Thread.sleep(SPIN_DELAY); }
808 catch (InterruptedException ie) {}
809 } else {
810 System.out.println("debugger has settled (" + delta + ")");
811 break;
812 }
813 }
814 }
815
816 /**
817 * Returns "true" if one or more threads is waiting for a debugger
818 * to attach.
819 */
820 public static boolean waitingForDebugger() {
821 return mWaiting;
822 }
823
824 /**
825 * Determine if a debugger is currently attached.
826 */
827 public static boolean isDebuggerConnected() {
828 return VMDebug.isDebuggerConnected();
829 }
830
831 /**
Andy McFaddene5772322010-01-22 07:23:31 -0800832 * Returns an array of strings that identify VM features. This is
833 * used by DDMS to determine what sorts of operations the VM can
834 * perform.
835 *
836 * @hide
837 */
838 public static String[] getVmFeatureList() {
839 return VMDebug.getVmFeatureList();
840 }
841
842 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 * Change the JDWP port.
844 *
845 * @deprecated no longer needed or useful
846 */
847 @Deprecated
848 public static void changeDebugPort(int port) {}
849
850 /**
851 * This is the pathname to the sysfs file that enables and disables
852 * tracing on the qemu emulator.
853 */
854 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
855
856 /**
857 * Enable qemu tracing. For this to work requires running everything inside
858 * the qemu emulator; otherwise, this method will have no effect. The trace
859 * file is specified on the command line when the emulator is started. For
860 * example, the following command line <br />
861 * <code>emulator -trace foo</code><br />
862 * will start running the emulator and create a trace file named "foo". This
863 * method simply enables writing the trace records to the trace file.
864 *
865 * <p>
866 * The main differences between this and {@link #startMethodTracing()} are
867 * that tracing in the qemu emulator traces every cpu instruction of every
868 * process, including kernel code, so we have more complete information,
869 * including all context switches. We can also get more detailed information
870 * such as cache misses. The sequence of calls is determined by
871 * post-processing the instruction trace. The qemu tracing is also done
872 * without modifying the application or perturbing the timing of calls
873 * because no instrumentation is added to the application being traced.
874 * </p>
875 *
876 * <p>
877 * One limitation of using this method compared to using
878 * {@link #startMethodTracing()} on the real device is that the emulator
879 * does not model all of the real hardware effects such as memory and
880 * bus contention. The emulator also has a simple cache model and cannot
881 * capture all the complexities of a real cache.
882 * </p>
883 */
884 public static void startNativeTracing() {
885 // Open the sysfs file for writing and write "1" to it.
886 PrintWriter outStream = null;
887 try {
888 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700889 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 outStream.println("1");
891 } catch (Exception e) {
892 } finally {
893 if (outStream != null)
894 outStream.close();
895 }
896
897 VMDebug.startEmulatorTracing();
898 }
899
900 /**
901 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing.
902 *
903 * <p>Tracing can be started and stopped as many times as desired. When
904 * the qemu emulator itself is stopped then the buffered trace records
905 * are flushed and written to the trace file. In fact, it is not necessary
906 * to call this method at all; simply killing qemu is sufficient. But
907 * starting and stopping a trace is useful for examining a specific
908 * region of code.</p>
909 */
910 public static void stopNativeTracing() {
911 VMDebug.stopEmulatorTracing();
912
913 // Open the sysfs file for writing and write "0" to it.
914 PrintWriter outStream = null;
915 try {
916 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700917 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 outStream.println("0");
919 } catch (Exception e) {
920 // We could print an error message here but we probably want
921 // to quietly ignore errors if we are not running in the emulator.
922 } finally {
923 if (outStream != null)
924 outStream.close();
925 }
926 }
927
928 /**
929 * Enable "emulator traces", in which information about the current
930 * method is made available to the "emulator -trace" feature. There
931 * is no corresponding "disable" call -- this is intended for use by
932 * the framework when tracing should be turned on and left that way, so
933 * that traces captured with F9/F10 will include the necessary data.
934 *
935 * This puts the VM into "profile" mode, which has performance
936 * consequences.
937 *
938 * To temporarily enable tracing, use {@link #startNativeTracing()}.
939 */
940 public static void enableEmulatorTraceOutput() {
941 VMDebug.startEmulatorTracing();
942 }
943
944 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600945 * Start method tracing with default log name and buffer size.
946 * <p>
947 * By default, the trace file is called "dmtrace.trace" and it's placed
948 * under your package-specific directory on primary shared/external storage,
949 * as returned by {@link Context#getExternalFilesDir(String)}.
950 * <p>
951 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
952 * A Graphical Log Viewer</a> for information about reading trace files.
953 * <p class="note">
954 * When method tracing is enabled, the VM will run more slowly than usual,
955 * so the timings from the trace files should only be considered in relative
956 * terms (e.g. was run #1 faster than run #2). The times for native methods
957 * will not change, so don't try to use this to compare the performance of
958 * interpreted and native implementations of the same method. As an
959 * alternative, consider using sampling-based method tracing via
960 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
961 * in the emulator via {@link #startNativeTracing()}.
962 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 */
964 public static void startMethodTracing() {
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600965 VMDebug.startMethodTracing(fixTracePath(null), 0, 0, false, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 }
967
968 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600969 * Start method tracing, specifying the trace log file path.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 * <p>
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600971 * When a relative file path is given, the trace file will be placed under
972 * your package-specific directory on primary shared/external storage, as
973 * returned by {@link Context#getExternalFilesDir(String)}.
974 * <p>
975 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
976 * A Graphical Log Viewer</a> for information about reading trace files.
977 * <p class="note">
978 * When method tracing is enabled, the VM will run more slowly than usual,
979 * so the timings from the trace files should only be considered in relative
980 * terms (e.g. was run #1 faster than run #2). The times for native methods
981 * will not change, so don't try to use this to compare the performance of
982 * interpreted and native implementations of the same method. As an
983 * alternative, consider using sampling-based method tracing via
984 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
985 * in the emulator via {@link #startNativeTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 * </p>
987 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600988 * @param tracePath Path to the trace log file to create. If {@code null},
989 * this will default to "dmtrace.trace". If the file already
990 * exists, it will be truncated. If the path given does not end
991 * in ".trace", it will be appended for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600993 public static void startMethodTracing(String tracePath) {
994 startMethodTracing(tracePath, 0, 0);
995 }
996
997 /**
998 * Start method tracing, specifying the trace log file name and the buffer
999 * size.
1000 * <p>
1001 * When a relative file path is given, the trace file will be placed under
1002 * your package-specific directory on primary shared/external storage, as
1003 * returned by {@link Context#getExternalFilesDir(String)}.
1004 * <p>
1005 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1006 * A Graphical Log Viewer</a> for information about reading trace files.
1007 * <p class="note">
1008 * When method tracing is enabled, the VM will run more slowly than usual,
1009 * so the timings from the trace files should only be considered in relative
1010 * terms (e.g. was run #1 faster than run #2). The times for native methods
1011 * will not change, so don't try to use this to compare the performance of
1012 * interpreted and native implementations of the same method. As an
1013 * alternative, consider using sampling-based method tracing via
1014 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1015 * in the emulator via {@link #startNativeTracing()}.
1016 * </p>
1017 *
1018 * @param tracePath Path to the trace log file to create. If {@code null},
1019 * this will default to "dmtrace.trace". If the file already
1020 * exists, it will be truncated. If the path given does not end
1021 * in ".trace", it will be appended for you.
1022 * @param bufferSize The maximum amount of trace data we gather. If not
1023 * given, it defaults to 8MB.
1024 */
1025 public static void startMethodTracing(String tracePath, int bufferSize) {
1026 startMethodTracing(tracePath, bufferSize, 0);
1027 }
1028
1029 /**
1030 * Start method tracing, specifying the trace log file name, the buffer
1031 * size, and flags.
1032 * <p>
1033 * When a relative file path is given, the trace file will be placed under
1034 * your package-specific directory on primary shared/external storage, as
1035 * returned by {@link Context#getExternalFilesDir(String)}.
1036 * <p>
1037 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1038 * A Graphical Log Viewer</a> for information about reading trace files.
1039 * <p class="note">
1040 * When method tracing is enabled, the VM will run more slowly than usual,
1041 * so the timings from the trace files should only be considered in relative
1042 * terms (e.g. was run #1 faster than run #2). The times for native methods
1043 * will not change, so don't try to use this to compare the performance of
1044 * interpreted and native implementations of the same method. As an
1045 * alternative, consider using sampling-based method tracing via
1046 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1047 * in the emulator via {@link #startNativeTracing()}.
1048 * </p>
1049 *
1050 * @param tracePath Path to the trace log file to create. If {@code null},
1051 * this will default to "dmtrace.trace". If the file already
1052 * exists, it will be truncated. If the path given does not end
1053 * in ".trace", it will be appended for you.
1054 * @param bufferSize The maximum amount of trace data we gather. If not
1055 * given, it defaults to 8MB.
1056 * @param flags Flags to control method tracing. The only one that is
1057 * currently defined is {@link #TRACE_COUNT_ALLOCS}.
1058 */
1059 public static void startMethodTracing(String tracePath, int bufferSize, int flags) {
1060 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, flags, false, 0);
Jeff Haod02e60f2014-01-06 15:52:52 -08001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
Jeff Haod02e60f2014-01-06 15:52:52 -08001063 /**
1064 * Start sampling-based method tracing, specifying the trace log file name,
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001065 * the buffer size, and the sampling interval.
1066 * <p>
1067 * When a relative file path is given, the trace file will be placed under
1068 * your package-specific directory on primary shared/external storage, as
1069 * returned by {@link Context#getExternalFilesDir(String)}.
1070 * <p>
1071 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1072 * A Graphical Log Viewer</a> for information about reading trace files.
Jeff Haod02e60f2014-01-06 15:52:52 -08001073 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001074 * @param tracePath Path to the trace log file to create. If {@code null},
1075 * this will default to "dmtrace.trace". If the file already
1076 * exists, it will be truncated. If the path given does not end
1077 * in ".trace", it will be appended for you.
1078 * @param bufferSize The maximum amount of trace data we gather. If not
1079 * given, it defaults to 8MB.
1080 * @param intervalUs The amount of time between each sample in microseconds.
Jeff Haod02e60f2014-01-06 15:52:52 -08001081 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001082 public static void startMethodTracingSampling(String tracePath, int bufferSize,
1083 int intervalUs) {
1084 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, 0, true, intervalUs);
Jeff Haod02e60f2014-01-06 15:52:52 -08001085 }
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001086
Jeff Haod02e60f2014-01-06 15:52:52 -08001087 /**
1088 * Formats name of trace log file for method tracing.
1089 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001090 private static String fixTracePath(String tracePath) {
1091 if (tracePath == null || tracePath.charAt(0) != '/') {
1092 final Context context = AppGlobals.getInitialApplication();
1093 final File dir;
1094 if (context != null) {
1095 dir = context.getExternalFilesDir(null);
1096 } else {
1097 dir = Environment.getExternalStorageDirectory();
1098 }
Jeff Haod02e60f2014-01-06 15:52:52 -08001099
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001100 if (tracePath == null) {
1101 tracePath = new File(dir, DEFAULT_TRACE_BODY).getAbsolutePath();
1102 } else {
1103 tracePath = new File(dir, tracePath).getAbsolutePath();
1104 }
1105 }
1106 if (!tracePath.endsWith(DEFAULT_TRACE_EXTENSION)) {
1107 tracePath += DEFAULT_TRACE_EXTENSION;
1108 }
1109 return tracePath;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 }
1111
1112 /**
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001113 * Like startMethodTracing(String, int, int), but taking an already-opened
1114 * FileDescriptor in which the trace is written. The file name is also
1115 * supplied simply for logging. Makes a dup of the file descriptor.
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02001116 *
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001117 * Not exposed in the SDK unless we are really comfortable with supporting
1118 * this and find it would be useful.
1119 * @hide
1120 */
1121 public static void startMethodTracing(String traceName, FileDescriptor fd,
1122 int bufferSize, int flags) {
Jeff Haod02e60f2014-01-06 15:52:52 -08001123 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001124 }
1125
1126 /**
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001127 * Starts method tracing without a backing file. When stopMethodTracing
1128 * is called, the result is sent directly to DDMS. (If DDMS is not
1129 * attached when tracing ends, the profiling data will be discarded.)
1130 *
1131 * @hide
1132 */
Jeff Hao7be3a132013-08-22 15:53:12 -07001133 public static void startMethodTracingDdms(int bufferSize, int flags,
1134 boolean samplingEnabled, int intervalUs) {
1135 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs);
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001136 }
1137
1138 /**
Jeff Haoac277052013-08-29 11:19:39 -07001139 * Determine whether method tracing is currently active and what type is
1140 * active.
1141 *
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001142 * @hide
1143 */
Jeff Haoac277052013-08-29 11:19:39 -07001144 public static int getMethodTracingMode() {
1145 return VMDebug.getMethodTracingMode();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001146 }
1147
1148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 * Stop method tracing.
1150 */
1151 public static void stopMethodTracing() {
1152 VMDebug.stopMethodTracing();
1153 }
1154
1155 /**
1156 * Get an indication of thread CPU usage. The value returned
1157 * indicates the amount of time that the current thread has spent
1158 * executing code or waiting for certain types of I/O.
1159 *
1160 * The time is expressed in nanoseconds, and is only meaningful
1161 * when compared to the result from an earlier call. Note that
1162 * nanosecond resolution does not imply nanosecond accuracy.
1163 *
1164 * On system which don't support this operation, the call returns -1.
1165 */
1166 public static long threadCpuTimeNanos() {
1167 return VMDebug.threadCpuTimeNanos();
1168 }
1169
1170 /**
Chet Haase2970c492010-11-09 13:58:04 -08001171 * Start counting the number and aggregate size of memory allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001173 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
1174 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
1175 * code doesn't cause additional allocations. The various <code>get</code> methods return
1176 * the specified value. And the various <code>reset</code> methods reset the specified
Chet Haase2970c492010-11-09 13:58:04 -08001177 * count.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001179 * <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 -08001180 * The per-thread counts for threads other than the current thread
Chet Haase2970c492010-11-09 13:58:04 -08001181 * are not cleared by the "reset" or "start" calls.</p>
Ian Rogersfe067a42013-02-22 19:59:23 -08001182 *
1183 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 */
Ian Rogersfe067a42013-02-22 19:59:23 -08001185 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 public static void startAllocCounting() {
1187 VMDebug.startAllocCounting();
1188 }
Chet Haase2970c492010-11-09 13:58:04 -08001189
1190 /**
1191 * Stop counting the number and aggregate size of memory allocations.
1192 *
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001193 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Chet Haase2970c492010-11-09 13:58:04 -08001194 */
Ian Rogersc2a3adb2013-04-19 11:31:48 -07001195 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 public static void stopAllocCounting() {
1197 VMDebug.stopAllocCounting();
1198 }
1199
Ian Rogersfe067a42013-02-22 19:59:23 -08001200 /**
1201 * Returns the global count of objects allocated 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 getGlobalAllocCount() {
1208 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1209 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001210
1211 /**
1212 * Clears the global count of objects allocated.
1213 * @see #getGlobalAllocCount()
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 resetGlobalAllocCount() {
1219 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1220 }
1221
1222 /**
1223 * Returns the global size, in bytes, of objects allocated by the runtime 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 public static int getGlobalAllocSize() {
1230 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1231 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001232
1233 /**
1234 * Clears the global size of objects allocated.
Dianne Hackborn3fa89692013-09-13 17:20:00 -07001235 * @see #getGlobalAllocSize()
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 resetGlobalAllocSize() {
1241 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1242 }
1243
1244 /**
1245 * Returns the global count of objects freed by the runtime between a
1246 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001247 *
1248 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001249 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001250 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public static int getGlobalFreedCount() {
1252 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1253 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001254
1255 /**
1256 * Clears the global count of objects freed.
1257 * @see #getGlobalFreedCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001258 *
1259 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001260 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001261 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001262 public static void resetGlobalFreedCount() {
1263 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1264 }
1265
1266 /**
1267 * Returns the global size, in bytes, of objects freed by the runtime between a
1268 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001269 *
1270 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001271 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001272 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 public static int getGlobalFreedSize() {
1274 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1275 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001276
1277 /**
1278 * Clears the global size of objects freed.
1279 * @see #getGlobalFreedSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001280 *
1281 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001282 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001283 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001284 public static void resetGlobalFreedSize() {
1285 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1286 }
1287
1288 /**
1289 * Returns the number of non-concurrent GC invocations between a
1290 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001291 *
1292 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001293 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001294 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001295 public static int getGlobalGcInvocationCount() {
1296 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1297 }
1298
1299 /**
1300 * Clears the count of non-concurrent GC invocations.
1301 * @see #getGlobalGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001302 *
1303 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001304 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001305 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001306 public static void resetGlobalGcInvocationCount() {
1307 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1308 }
1309
1310 /**
1311 * Returns the number of classes successfully initialized (ie those that executed without
1312 * throwing an exception) between a {@link #startAllocCounting() start} and
1313 * {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001314 *
1315 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001316 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001317 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001318 public static int getGlobalClassInitCount() {
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001319 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1320 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001321
1322 /**
1323 * Clears the count of classes initialized.
1324 * @see #getGlobalClassInitCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001325 *
1326 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001327 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001328 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001329 public static void resetGlobalClassInitCount() {
1330 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1331 }
1332
1333 /**
1334 * Returns the time spent successfully initializing classes between a
1335 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001336 *
1337 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001338 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001339 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001340 public static int getGlobalClassInitTime() {
1341 /* cumulative elapsed time for class initialization, in usec */
1342 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1343 }
Carl Shapirob5961982010-12-22 15:54:53 -08001344
1345 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001346 * Clears the count of time spent initializing classes.
1347 * @see #getGlobalClassInitTime()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001348 *
1349 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001350 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001351 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001352 public static void resetGlobalClassInitTime() {
1353 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1354 }
1355
1356 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001357 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001358 * @deprecated This method is now obsolete.
1359 */
1360 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 public static int getGlobalExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001362 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 }
Carl Shapirob5961982010-12-22 15:54:53 -08001364
1365 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001366 * This method exists for compatibility and has no effect.
1367 * @deprecated This method is now obsolete.
1368 */
1369 @Deprecated
1370 public static void resetGlobalExternalAllocSize() {}
1371
1372 /**
1373 * This method exists for compatibility and has no effect.
1374 * @deprecated This method is now obsolete.
1375 */
1376 @Deprecated
1377 public static void resetGlobalExternalAllocCount() {}
1378
1379 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001380 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001381 * @deprecated This method is now obsolete.
1382 */
1383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 public static int getGlobalExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001385 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
Carl Shapirob5961982010-12-22 15:54:53 -08001387
1388 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001389 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001390 * @deprecated This method is now obsolete.
1391 */
1392 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 public static int getGlobalExternalFreedCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001394 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 }
Carl Shapirob5961982010-12-22 15:54:53 -08001396
1397 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001398 * This method exists for compatibility and has no effect.
1399 * @deprecated This method is now obsolete.
1400 */
1401 @Deprecated
1402 public static void resetGlobalExternalFreedCount() {}
1403
1404 /**
1405 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001406 * @deprecated This method is now obsolete.
1407 */
1408 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 public static int getGlobalExternalFreedSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001410 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 }
Carl Shapirob5961982010-12-22 15:54:53 -08001412
Ian Rogersfe067a42013-02-22 19:59:23 -08001413 /**
1414 * This method exists for compatibility and has no effect.
1415 * @deprecated This method is now obsolete.
1416 */
1417 @Deprecated
1418 public static void resetGlobalExternalFreedSize() {}
1419
1420 /**
1421 * Returns the thread-local count of objects allocated by the runtime between a
1422 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001423 *
1424 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001425 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001426 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 public static int getThreadAllocCount() {
1428 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1429 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001430
1431 /**
1432 * Clears the thread-local count of objects allocated.
1433 * @see #getThreadAllocCount()
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 void resetThreadAllocCount() {
1439 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1440 }
1441
1442 /**
1443 * Returns the thread-local size of objects allocated by the runtime between a
1444 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
1445 * @return The allocated size in bytes.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001446 *
1447 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001448 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001449 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 public static int getThreadAllocSize() {
1451 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1452 }
Carl Shapirob5961982010-12-22 15:54:53 -08001453
1454 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001455 * Clears the thread-local count of objects allocated.
1456 * @see #getThreadAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001457 *
1458 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001459 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001460 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001461 public static void resetThreadAllocSize() {
1462 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1463 }
1464
1465 /**
1466 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001467 * @deprecated This method is now obsolete.
1468 */
1469 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 public static int getThreadExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001471 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
Carl Shapirob5961982010-12-22 15:54:53 -08001473
1474 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001475 * This method exists for compatibility and has no effect.
1476 * @deprecated This method is now obsolete.
1477 */
1478 @Deprecated
1479 public static void resetThreadExternalAllocCount() {}
1480
1481 /**
1482 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001483 * @deprecated This method is now obsolete.
1484 */
1485 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 public static int getThreadExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001487 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
Carl Shapirob5961982010-12-22 15:54:53 -08001489
Carl Shapirob5961982010-12-22 15:54:53 -08001490 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001491 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001492 * @deprecated This method is now obsolete.
1493 */
1494 @Deprecated
1495 public static void resetThreadExternalAllocSize() {}
1496
Ian Rogersfe067a42013-02-22 19:59:23 -08001497 /**
1498 * Returns the number of thread-local non-concurrent GC invocations between a
1499 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001500 *
1501 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001502 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001503 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001504 public static int getThreadGcInvocationCount() {
1505 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1506 }
1507
1508 /**
1509 * Clears the thread-local count of non-concurrent GC invocations.
1510 * @see #getThreadGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001511 *
1512 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001513 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001514 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 public static void resetThreadGcInvocationCount() {
1516 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1517 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001518
1519 /**
1520 * Clears all the global and thread-local memory allocation counters.
1521 * @see #startAllocCounting()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001522 *
1523 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001524 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001525 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 public static void resetAllCounts() {
1527 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
1528 }
1529
1530 /**
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001531 * Returns the value of a particular runtime statistic or {@code null} if no
1532 * such runtime statistic exists.
1533 *
1534 * <p>The following table lists the runtime statistics that the runtime supports.
1535 * Note runtime statistics may be added or removed in a future API level.</p>
1536 *
1537 * <table>
1538 * <thead>
1539 * <tr>
1540 * <th>Runtime statistic name</th>
1541 * <th>Meaning</th>
1542 * <th>Example</th>
1543 * <th>Supported (API Levels)</th>
1544 * </tr>
1545 * </thead>
1546 * <tbody>
1547 * <tr>
1548 * <td>art.gc.gc-count</td>
1549 * <td>The number of garbage collection runs.</td>
1550 * <td>{@code 164}</td>
1551 * <td>23</td>
1552 * </tr>
1553 * <tr>
1554 * <td>art.gc.gc-time</td>
1555 * <td>The total duration of garbage collection runs in ms.</td>
1556 * <td>{@code 62364}</td>
1557 * <td>23</td>
1558 * </tr>
1559 * <tr>
1560 * <td>art.gc.bytes-allocated</td>
1561 * <td>The total number of bytes that the application allocated.</td>
1562 * <td>{@code 1463948408}</td>
1563 * <td>23</td>
1564 * </tr>
1565 * <tr>
1566 * <td>art.gc.bytes-freed</td>
1567 * <td>The total number of bytes that garbage collection reclaimed.</td>
1568 * <td>{@code 1313493084}</td>
1569 * <td>23</td>
1570 * </tr>
1571 * <tr>
1572 * <td>art.gc.blocking-gc-count</td>
1573 * <td>The number of blocking garbage collection runs.</td>
1574 * <td>{@code 2}</td>
1575 * <td>23</td>
1576 * </tr>
1577 * <tr>
1578 * <td>art.gc.blocking-gc-time</td>
1579 * <td>The total duration of blocking garbage collection runs in ms.</td>
1580 * <td>{@code 804}</td>
1581 * <td>23</td>
1582 * </tr>
1583 * <tr>
1584 * <td>art.gc.gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001585 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage
1586 * collection runs that have occurred over the last 10
1587 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate
1588 * samples taken since the process began. The histogram can be used to identify
1589 * instances of high rates of garbage collection runs. For example, a histogram
1590 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time
1591 * there are between 0 and 2 garbage collection runs every 10 seconds, but there
1592 * were 8 distinct 10-second intervals in which 5 garbage collection runs
1593 * occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001594 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td>
1595 * <td>23</td>
1596 * </tr>
1597 * <tr>
1598 * <td>art.gc.blocking-gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001599 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of
1600 * blocking garbage collection runs that have occurred over the last 10
1601 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the
1602 * blocking-gc-count-rate samples taken since the process began. The histogram
1603 * can be used to identify instances of high rates of blocking garbage
1604 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that
1605 * most of the time there are zero blocking garbage collection runs every 10
1606 * seconds, but there was one 10-second interval in which one blocking garbage
1607 * collection run occurred, and there was one interval in which two blocking
1608 * garbage collection runs occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001609 * <td>{@code 0:99269,1:1,2:1}</td>
1610 * <td>23</td>
1611 * </tr>
1612 * </tbody>
1613 * </table>
1614 *
1615 * @param statName
1616 * the name of the runtime statistic to look up.
1617 * @return the value of the specified runtime statistic or {@code null} if the
1618 * runtime statistic doesn't exist.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001619 */
1620 public static String getRuntimeStat(String statName) {
1621 return VMDebug.getRuntimeStat(statName);
1622 }
1623
1624 /**
1625 * Returns a map of the names/values of the runtime statistics
Hiroshi Yamauchid8001672015-04-14 16:07:26 -07001626 * that {@link #getRuntimeStat(String)} supports.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001627 *
1628 * @return a map of the names/values of the supported runtime statistics.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001629 */
1630 public static Map<String, String> getRuntimeStats() {
1631 return VMDebug.getRuntimeStats();
1632 }
1633
1634 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 * Returns the size of the native heap.
1636 * @return The size of the native heap in bytes.
1637 */
1638 public static native long getNativeHeapSize();
1639
1640 /**
1641 * Returns the amount of allocated memory in the native heap.
1642 * @return The allocated size in bytes.
1643 */
1644 public static native long getNativeHeapAllocatedSize();
1645
1646 /**
1647 * Returns the amount of free memory in the native heap.
1648 * @return The freed size in bytes.
1649 */
1650 public static native long getNativeHeapFreeSize();
1651
1652 /**
1653 * Retrieves information about this processes memory usages. This information is broken down by
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001654 * how much is in use by dalvik, the native heap, and everything else.
1655 *
1656 * <p><b>Note:</b> this method directly retrieves memory information for the give process
1657 * from low-level data available to it. It may not be able to retrieve information about
1658 * some protected allocations, such as graphics. If you want to be sure you can see
1659 * all information about allocations by the process, use instead
1660 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 */
1662 public static native void getMemoryInfo(MemoryInfo memoryInfo);
1663
1664 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001665 * Note: currently only works when the requested pid has the same UID
1666 * as the caller.
1667 * @hide
1668 */
1669 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
1670
1671 /**
Dianne Hackbornb437e092011-08-05 17:50:29 -07001672 * Retrieves the PSS memory used by the process as given by the
1673 * smaps.
1674 */
1675 public static native long getPss();
1676
1677 /**
1678 * Retrieves the PSS memory used by the process as given by the
Martijn Coenene0764852016-01-07 17:04:22 -08001679 * smaps. Optionally supply a long array of 2 entries to also
1680 * receive the Uss and SwapPss of the process, and another array to also
1681 * retrieve the separate memtrack size.
1682 * @hide
Dianne Hackbornb437e092011-08-05 17:50:29 -07001683 */
Martijn Coenene0764852016-01-07 17:04:22 -08001684 public static native long getPss(int pid, long[] outUssSwapPss, long[] outMemtrack);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001685
Dianne Hackborn8e692572013-09-10 19:06:15 -07001686 /** @hide */
1687 public static final int MEMINFO_TOTAL = 0;
1688 /** @hide */
1689 public static final int MEMINFO_FREE = 1;
1690 /** @hide */
1691 public static final int MEMINFO_BUFFERS = 2;
1692 /** @hide */
1693 public static final int MEMINFO_CACHED = 3;
1694 /** @hide */
1695 public static final int MEMINFO_SHMEM = 4;
1696 /** @hide */
1697 public static final int MEMINFO_SLAB = 5;
1698 /** @hide */
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001699 public static final int MEMINFO_SWAP_TOTAL = 6;
1700 /** @hide */
1701 public static final int MEMINFO_SWAP_FREE = 7;
1702 /** @hide */
1703 public static final int MEMINFO_ZRAM_TOTAL = 8;
1704 /** @hide */
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001705 public static final int MEMINFO_MAPPED = 9;
1706 /** @hide */
1707 public static final int MEMINFO_VM_ALLOC_USED = 10;
1708 /** @hide */
1709 public static final int MEMINFO_PAGE_TABLES = 11;
1710 /** @hide */
1711 public static final int MEMINFO_KERNEL_STACK = 12;
1712 /** @hide */
1713 public static final int MEMINFO_COUNT = 13;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001714
1715 /**
1716 * Retrieves /proc/meminfo. outSizes is filled with fields
1717 * as defined by MEMINFO_* offsets.
1718 * @hide
1719 */
1720 public static native void getMemInfo(long[] outSizes);
1721
Dianne Hackbornb437e092011-08-05 17:50:29 -07001722 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001723 * Establish an object allocation limit in the current thread.
Carl Shapiro7e942842011-01-12 17:17:45 -08001724 * This feature was never enabled in release builds. The
1725 * allocation limits feature was removed in Honeycomb. This
1726 * method exists for compatibility and always returns -1 and has
1727 * no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 *
Carl Shapiro11073832011-01-12 16:28:57 -08001729 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 */
Carl Shapiro11073832011-01-12 16:28:57 -08001731 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 public static int setAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001733 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 }
1735
1736 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001737 * Establish a global object allocation limit. This feature was
Carl Shapiro7e942842011-01-12 17:17:45 -08001738 * never enabled in release builds. The allocation limits feature
1739 * was removed in Honeycomb. This method exists for compatibility
1740 * and always returns -1 and has no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 *
Carl Shapiro11073832011-01-12 16:28:57 -08001742 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 */
Carl Shapiro11073832011-01-12 16:28:57 -08001744 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 public static int setGlobalAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001746 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 }
1748
1749 /**
1750 * Dump a list of all currently loaded class to the log file.
1751 *
1752 * @param flags See constants above.
1753 */
1754 public static void printLoadedClasses(int flags) {
1755 VMDebug.printLoadedClasses(flags);
1756 }
1757
1758 /**
1759 * Get the number of loaded classes.
1760 * @return the number of loaded classes.
1761 */
1762 public static int getLoadedClassCount() {
1763 return VMDebug.getLoadedClassCount();
1764 }
1765
1766 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001767 * Dump "hprof" data to the specified file. This may cause a GC.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 *
1769 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
1770 * @throws UnsupportedOperationException if the VM was built without
1771 * HPROF support.
1772 * @throws IOException if an error occurs while opening or writing files.
1773 */
1774 public static void dumpHprofData(String fileName) throws IOException {
1775 VMDebug.dumpHprofData(fileName);
1776 }
1777
1778 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001779 * Like dumpHprofData(String), but takes an already-opened
1780 * FileDescriptor to which the trace is written. The file name is also
1781 * supplied simply for logging. Makes a dup of the file descriptor.
1782 *
1783 * Primarily for use by the "am" shell command.
1784 *
1785 * @hide
1786 */
1787 public static void dumpHprofData(String fileName, FileDescriptor fd)
1788 throws IOException {
1789 VMDebug.dumpHprofData(fileName, fd);
1790 }
1791
1792 /**
1793 * Collect "hprof" and send it to DDMS. This may cause a GC.
Andy McFadden07a96612010-01-28 16:54:37 -08001794 *
1795 * @throws UnsupportedOperationException if the VM was built without
1796 * HPROF support.
Andy McFadden07a96612010-01-28 16:54:37 -08001797 * @hide
1798 */
1799 public static void dumpHprofDataDdms() {
1800 VMDebug.dumpHprofDataDdms();
1801 }
1802
1803 /**
Andy McFadden06a6b552010-07-13 16:28:09 -07001804 * Writes native heap data to the specified file descriptor.
1805 *
1806 * @hide
1807 */
1808 public static native void dumpNativeHeap(FileDescriptor fd);
1809
1810 /**
Brian Carlstromc21550a2010-10-05 21:34:06 -07001811 * Returns a count of the extant instances of a class.
1812 *
1813 * @hide
1814 */
1815 public static long countInstancesOfClass(Class cls) {
Brian Carlstrom7495cfa2010-11-30 18:06:00 -08001816 return VMDebug.countInstancesOfClass(cls, true);
Brian Carlstromc21550a2010-10-05 21:34:06 -07001817 }
1818
1819 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 * Returns the number of sent transactions from this process.
1821 * @return The number of sent transactions or -1 if it could not read t.
1822 */
1823 public static native int getBinderSentTransactions();
1824
1825 /**
1826 * Returns the number of received transactions from the binder driver.
1827 * @return The number of received transactions or -1 if it could not read the stats.
1828 */
1829 public static native int getBinderReceivedTransactions();
1830
1831 /**
1832 * Returns the number of active local Binder objects that exist in the
1833 * current process.
1834 */
1835 public static final native int getBinderLocalObjectCount();
1836
1837 /**
1838 * Returns the number of references to remote proxy Binder objects that
1839 * exist in the current process.
1840 */
1841 public static final native int getBinderProxyObjectCount();
1842
1843 /**
1844 * Returns the number of death notification links to Binder objects that
1845 * exist in the current process.
1846 */
1847 public static final native int getBinderDeathObjectCount();
1848
1849 /**
Andy McFadden599c9182009-04-08 00:35:56 -07001850 * Primes the register map cache.
1851 *
1852 * Only works for classes in the bootstrap class loader. Does not
1853 * cause classes to be loaded if they're not already present.
1854 *
1855 * The classAndMethodDesc argument is a concatentation of the VM-internal
1856 * class descriptor, method name, and method descriptor. Examples:
1857 * Landroid/os/Looper;.loop:()V
1858 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
1859 *
1860 * @param classAndMethodDesc the method to prepare
1861 *
1862 * @hide
1863 */
1864 public static final boolean cacheRegisterMap(String classAndMethodDesc) {
1865 return VMDebug.cacheRegisterMap(classAndMethodDesc);
1866 }
1867
1868 /**
Andy McFaddenbfd6d482009-10-22 17:25:57 -07001869 * Dumps the contents of VM reference tables (e.g. JNI locals and
1870 * globals) to the log file.
1871 *
1872 * @hide
1873 */
1874 public static final void dumpReferenceTables() {
1875 VMDebug.dumpReferenceTables();
1876 }
1877
1878 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 * API for gathering and querying instruction counts.
1880 *
1881 * Example usage:
Chet Haase2970c492010-11-09 13:58:04 -08001882 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 * Debug.InstructionCount icount = new Debug.InstructionCount();
1884 * icount.resetAndStart();
1885 * [... do lots of stuff ...]
1886 * if (icount.collect()) {
1887 * System.out.println("Total instructions executed: "
1888 * + icount.globalTotal());
1889 * System.out.println("Method invocations: "
1890 * + icount.globalMethodInvocations());
1891 * }
Chet Haase2970c492010-11-09 13:58:04 -08001892 * </pre>
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001893 *
1894 * @deprecated Instruction counting is no longer supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 */
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001896 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 public static class InstructionCount {
Dan Bornsteinb96f5892010-12-02 17:19:53 -08001898 private static final int NUM_INSTR =
1899 OpcodeInfo.MAXIMUM_PACKED_VALUE + 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900
1901 private int[] mCounts;
1902
1903 public InstructionCount() {
1904 mCounts = new int[NUM_INSTR];
1905 }
1906
1907 /**
1908 * Reset counters and ensure counts are running. Counts may
1909 * have already been running.
1910 *
1911 * @return true if counting was started
1912 */
1913 public boolean resetAndStart() {
1914 try {
1915 VMDebug.startInstructionCounting();
1916 VMDebug.resetInstructionCount();
1917 } catch (UnsupportedOperationException uoe) {
1918 return false;
1919 }
1920 return true;
1921 }
1922
1923 /**
1924 * Collect instruction counts. May or may not stop the
1925 * counting process.
1926 */
1927 public boolean collect() {
1928 try {
1929 VMDebug.stopInstructionCounting();
1930 VMDebug.getInstructionCount(mCounts);
1931 } catch (UnsupportedOperationException uoe) {
1932 return false;
1933 }
1934 return true;
1935 }
1936
1937 /**
1938 * Return the total number of instructions executed globally (i.e. in
1939 * all threads).
1940 */
1941 public int globalTotal() {
1942 int count = 0;
Dan Bornstein1d99b062010-11-30 12:26:52 -08001943
1944 for (int i = 0; i < NUM_INSTR; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 count += mCounts[i];
Dan Bornstein1d99b062010-11-30 12:26:52 -08001946 }
1947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 return count;
1949 }
1950
1951 /**
1952 * Return the total number of method-invocation instructions
1953 * executed globally.
1954 */
1955 public int globalMethodInvocations() {
1956 int count = 0;
1957
Dan Bornstein1d99b062010-11-30 12:26:52 -08001958 for (int i = 0; i < NUM_INSTR; i++) {
1959 if (OpcodeInfo.isInvoke(i)) {
1960 count += mCounts[i];
1961 }
1962 }
1963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 return count;
1965 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07001966 }
1967
Dave Bort1ce5bd32009-04-22 17:36:56 -07001968 /**
1969 * A Map of typed debug properties.
1970 */
1971 private static final TypedProperties debugProperties;
1972
1973 /*
1974 * Load the debug properties from the standard files into debugProperties.
1975 */
1976 static {
Joe Onorato43a17652011-04-06 19:22:23 -07001977 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001978 final String TAG = "DebugProperties";
1979 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
1980 final TypedProperties tp = new TypedProperties();
1981
1982 // Read the properties from each of the files, if present.
Dave Borte9bfd9b2009-05-04 14:35:23 -07001983 for (String file : files) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001984 Reader r;
1985 try {
1986 r = new FileReader(file);
1987 } catch (FileNotFoundException ex) {
1988 // It's ok if a file is missing.
1989 continue;
1990 }
1991
Dave Bort1ce5bd32009-04-22 17:36:56 -07001992 try {
1993 tp.load(r);
Dave Borte9bfd9b2009-05-04 14:35:23 -07001994 } catch (Exception ex) {
1995 throw new RuntimeException("Problem loading " + file, ex);
1996 } finally {
1997 try {
1998 r.close();
1999 } catch (IOException ex) {
2000 // Ignore this error.
2001 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002002 }
2003 }
2004
2005 debugProperties = tp.isEmpty() ? null : tp;
2006 } else {
2007 debugProperties = null;
2008 }
2009 }
2010
2011
2012 /**
2013 * Returns true if the type of the field matches the specified class.
2014 * Handles the case where the class is, e.g., java.lang.Boolean, but
2015 * the field is of the primitive "boolean" type. Also handles all of
2016 * the java.lang.Number subclasses.
2017 */
2018 private static boolean fieldTypeMatches(Field field, Class<?> cl) {
2019 Class<?> fieldClass = field.getType();
2020 if (fieldClass == cl) {
2021 return true;
2022 }
2023 Field primitiveTypeField;
2024 try {
2025 /* All of the classes we care about (Boolean, Integer, etc.)
2026 * have a Class field called "TYPE" that points to the corresponding
2027 * primitive class.
2028 */
2029 primitiveTypeField = cl.getField("TYPE");
2030 } catch (NoSuchFieldException ex) {
2031 return false;
2032 }
2033 try {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002034 return fieldClass == (Class<?>) primitiveTypeField.get(null);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002035 } catch (IllegalAccessException ex) {
2036 return false;
2037 }
2038 }
2039
2040
2041 /**
2042 * Looks up the property that corresponds to the field, and sets the field's value
2043 * if the types match.
2044 */
Dave Borte9bfd9b2009-05-04 14:35:23 -07002045 private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
2046 final String propertyName) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002047 if (field.getType() == java.lang.String.class) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002048 int stringInfo = properties.getStringInfo(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002049 switch (stringInfo) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002050 case TypedProperties.STRING_SET:
2051 // Handle as usual below.
2052 break;
2053 case TypedProperties.STRING_NULL:
2054 try {
2055 field.set(null, null); // null object for static fields; null string
2056 } catch (IllegalAccessException ex) {
2057 throw new IllegalArgumentException(
2058 "Cannot set field for " + propertyName, ex);
2059 }
2060 return;
2061 case TypedProperties.STRING_NOT_SET:
2062 return;
2063 case TypedProperties.STRING_TYPE_MISMATCH:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002064 throw new IllegalArgumentException(
Dave Borte9bfd9b2009-05-04 14:35:23 -07002065 "Type of " + propertyName + " " +
2066 " does not match field type (" + field.getType() + ")");
2067 default:
2068 throw new IllegalStateException(
2069 "Unexpected getStringInfo(" + propertyName + ") return value " +
2070 stringInfo);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002071 }
2072 }
Dave Borte9bfd9b2009-05-04 14:35:23 -07002073 Object value = properties.get(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002074 if (value != null) {
2075 if (!fieldTypeMatches(field, value.getClass())) {
2076 throw new IllegalArgumentException(
2077 "Type of " + propertyName + " (" + value.getClass() + ") " +
2078 " does not match field type (" + field.getType() + ")");
2079 }
2080 try {
2081 field.set(null, value); // null object for static fields
2082 } catch (IllegalAccessException ex) {
2083 throw new IllegalArgumentException(
2084 "Cannot set field for " + propertyName, ex);
2085 }
2086 }
2087 }
2088
2089
2090 /**
Romain Guyc4b11a72009-05-13 15:46:37 -07002091 * Equivalent to <code>setFieldsOn(cl, false)</code>.
2092 *
2093 * @see #setFieldsOn(Class, boolean)
Romain Guyd4103d02009-05-14 12:24:21 -07002094 *
2095 * @hide
Romain Guyc4b11a72009-05-13 15:46:37 -07002096 */
2097 public static void setFieldsOn(Class<?> cl) {
2098 setFieldsOn(cl, false);
2099 }
2100
2101 /**
Dave Bort1ce5bd32009-04-22 17:36:56 -07002102 * Reflectively sets static fields of a class based on internal debugging
Joe Onorato43a17652011-04-06 19:22:23 -07002103 * properties. This method is a no-op if false is
Dave Bort1ce5bd32009-04-22 17:36:56 -07002104 * false.
2105 * <p>
Joe Onorato43a17652011-04-06 19:22:23 -07002106 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will
Dave Bort1ce5bd32009-04-22 17:36:56 -07002107 * always be false in release builds. This API is typically only useful
2108 * for platform developers.
2109 * </p>
2110 * Class setup: define a class whose only fields are non-final, static
2111 * primitive types (except for "char") or Strings. In a static block
2112 * after the field definitions/initializations, pass the class to
Romain Guyc4b11a72009-05-13 15:46:37 -07002113 * this method, Debug.setFieldsOn(). Example:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002114 * <pre>
2115 * package com.example;
2116 *
2117 * import android.os.Debug;
2118 *
2119 * public class MyDebugVars {
2120 * public static String s = "a string";
2121 * public static String s2 = "second string";
2122 * public static String ns = null;
2123 * public static boolean b = false;
2124 * public static int i = 5;
Romain Guyc4b11a72009-05-13 15:46:37 -07002125 * @Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002126 * public static float f = 0.1f;
Romain Guyc4b11a72009-05-13 15:46:37 -07002127 * @@Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002128 * public static double d = 0.5d;
2129 *
2130 * // This MUST appear AFTER all fields are defined and initialized!
2131 * static {
Romain Guyc4b11a72009-05-13 15:46:37 -07002132 * // Sets all the fields
Dave Borte9bfd9b2009-05-04 14:35:23 -07002133 * Debug.setFieldsOn(MyDebugVars.class);
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02002134 *
Romain Guyc4b11a72009-05-13 15:46:37 -07002135 * // Sets only the fields annotated with @Debug.DebugProperty
2136 * // Debug.setFieldsOn(MyDebugVars.class, true);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002137 * }
2138 * }
2139 * </pre>
Dave Borte9bfd9b2009-05-04 14:35:23 -07002140 * setFieldsOn() may override the value of any field in the class based
Dave Bort1ce5bd32009-04-22 17:36:56 -07002141 * on internal properties that are fixed at boot time.
2142 * <p>
2143 * These properties are only set during platform debugging, and are not
2144 * meant to be used as a general-purpose properties store.
2145 *
2146 * {@hide}
2147 *
2148 * @param cl The class to (possibly) modify
Romain Guyc4b11a72009-05-13 15:46:37 -07002149 * @param partial If false, sets all static fields, otherwise, only set
2150 * fields with the {@link android.os.Debug.DebugProperty}
2151 * annotation
Dave Bort1ce5bd32009-04-22 17:36:56 -07002152 * @throws IllegalArgumentException if any fields are final or non-static,
2153 * or if the type of the field does not match the type of
2154 * the internal debugging property value.
2155 */
Romain Guyc4b11a72009-05-13 15:46:37 -07002156 public static void setFieldsOn(Class<?> cl, boolean partial) {
Joe Onorato43a17652011-04-06 19:22:23 -07002157 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002158 if (debugProperties != null) {
2159 /* Only look for fields declared directly by the class,
2160 * so we don't mysteriously change static fields in superclasses.
2161 */
2162 for (Field field : cl.getDeclaredFields()) {
Romain Guyc4b11a72009-05-13 15:46:37 -07002163 if (!partial || field.getAnnotation(DebugProperty.class) != null) {
2164 final String propertyName = cl.getName() + "." + field.getName();
2165 boolean isStatic = Modifier.isStatic(field.getModifiers());
2166 boolean isFinal = Modifier.isFinal(field.getModifiers());
2167
2168 if (!isStatic || isFinal) {
2169 throw new IllegalArgumentException(propertyName +
2170 " must be static and non-final");
2171 }
2172 modifyFieldIfSet(field, debugProperties, propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002173 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002174 }
2175 }
2176 } else {
Dan Egnor3eda9792010-03-05 13:28:36 -08002177 Log.wtf(TAG,
Dave Borte9bfd9b2009-05-04 14:35:23 -07002178 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
Dave Bort1ce5bd32009-04-22 17:36:56 -07002179 ") called in non-DEBUG build");
2180 }
2181 }
Romain Guyc4b11a72009-05-13 15:46:37 -07002182
2183 /**
2184 * Annotation to put on fields you want to set with
2185 * {@link Debug#setFieldsOn(Class, boolean)}.
2186 *
2187 * @hide
2188 */
2189 @Target({ ElementType.FIELD })
2190 @Retention(RetentionPolicy.RUNTIME)
2191 public @interface DebugProperty {
2192 }
Dan Egnor3eda9792010-03-05 13:28:36 -08002193
2194 /**
2195 * Get a debugging dump of a system service by name.
2196 *
2197 * <p>Most services require the caller to hold android.permission.DUMP.
2198 *
2199 * @param name of the service to dump
2200 * @param fd to write dump output to (usually an output log file)
2201 * @param args to pass to the service's dump method, may be null
2202 * @return true if the service was dumped successfully, false if
2203 * the service could not be found or had an error while dumping
2204 */
2205 public static boolean dumpService(String name, FileDescriptor fd, String[] args) {
2206 IBinder service = ServiceManager.getService(name);
2207 if (service == null) {
2208 Log.e(TAG, "Can't find service to dump: " + name);
2209 return false;
2210 }
2211
2212 try {
2213 service.dump(fd, args);
2214 return true;
2215 } catch (RemoteException e) {
2216 Log.e(TAG, "Can't dump service: " + name, e);
2217 return false;
2218 }
2219 }
Craig Mautnera51a9562012-04-17 17:05:26 -07002220
2221 /**
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002222 * Have the stack traces of the given native process dumped to the
2223 * specified file. Will be appended to the file.
2224 * @hide
2225 */
2226 public static native void dumpNativeBacktraceToFile(int pid, String file);
2227
2228 /**
Colin Crossc4fb5f92016-02-02 16:51:15 -08002229 * Get description of unreachable native memory.
2230 * @param limit the number of leaks to provide info on, 0 to only get a summary.
2231 * @param contents true to include a hex dump of the contents of unreachable memory.
2232 * @return the String containing a description of unreachable memory.
2233 * @hide */
2234 public static native String getUnreachableMemory(int limit, boolean contents);
2235
2236 /**
Craig Mautnera51a9562012-04-17 17:05:26 -07002237 * Return a String describing the calling method and location at a particular stack depth.
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07002238 * @param callStack the Thread stack
Craig Mautnera51a9562012-04-17 17:05:26 -07002239 * @param depth the depth of stack to return information for.
2240 * @return the String describing the caller at that depth.
2241 */
2242 private static String getCaller(StackTraceElement callStack[], int depth) {
2243 // callStack[4] is the caller of the method that called getCallers()
2244 if (4 + depth >= callStack.length) {
2245 return "<bottom of call stack>";
2246 }
2247 StackTraceElement caller = callStack[4 + depth];
2248 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2249 }
2250
2251 /**
2252 * Return a string consisting of methods and locations at multiple call stack levels.
2253 * @param depth the number of levels to return, starting with the immediate caller.
2254 * @return a string describing the call stack.
2255 * {@hide}
2256 */
2257 public static String getCallers(final int depth) {
2258 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2259 StringBuffer sb = new StringBuffer();
2260 for (int i = 0; i < depth; i++) {
2261 sb.append(getCaller(callStack, i)).append(" ");
2262 }
2263 return sb.toString();
2264 }
2265
2266 /**
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07002267 * Return a string consisting of methods and locations at multiple call stack levels.
2268 * @param depth the number of levels to return, starting with the immediate caller.
2269 * @return a string describing the call stack.
2270 * {@hide}
2271 */
2272 public static String getCallers(final int start, int depth) {
2273 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2274 StringBuffer sb = new StringBuffer();
2275 depth += start;
2276 for (int i = start; i < depth; i++) {
2277 sb.append(getCaller(callStack, i)).append(" ");
2278 }
2279 return sb.toString();
2280 }
2281
2282 /**
Dianne Hackbornef03a7f2012-10-29 18:46:52 -07002283 * Like {@link #getCallers(int)}, but each location is append to the string
2284 * as a new line with <var>linePrefix</var> in front of it.
2285 * @param depth the number of levels to return, starting with the immediate caller.
2286 * @param linePrefix prefix to put in front of each location.
2287 * @return a string describing the call stack.
2288 * {@hide}
2289 */
2290 public static String getCallers(final int depth, String linePrefix) {
2291 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2292 StringBuffer sb = new StringBuffer();
2293 for (int i = 0; i < depth; i++) {
2294 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n");
2295 }
2296 return sb.toString();
2297 }
2298
2299 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08002300 * @return a String describing the immediate caller of the calling method.
Craig Mautnera51a9562012-04-17 17:05:26 -07002301 * {@hide}
2302 */
2303 public static String getCaller() {
2304 return getCaller(Thread.currentThread().getStackTrace(), 0);
2305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306}