blob: 2efde23275ab2ef7d2b14f0919c8b0ea91bc3f79 [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
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060019import android.app.AppGlobals;
20import android.content.Context;
Dave Bort1ce5bd32009-04-22 17:36:56 -070021import android.util.Log;
22
Narayan Kamathf013daa2017-05-09 12:55:02 +010023import com.android.internal.util.FastPrintWriter;
24import com.android.internal.util.TypedProperties;
25
Narayan Kamathf013daa2017-05-09 12:55:02 +010026import dalvik.system.VMDebug;
27
28import org.apache.harmony.dalvik.ddmc.Chunk;
29import org.apache.harmony.dalvik.ddmc.ChunkHandler;
30import org.apache.harmony.dalvik.ddmc.DdmServer;
31
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060032import java.io.File;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070033import java.io.FileDescriptor;
Dave Bort1ce5bd32009-04-22 17:36:56 -070034import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import java.io.FileOutputStream;
Dave Bort1ce5bd32009-04-22 17:36:56 -070036import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import java.io.PrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070039import java.io.Reader;
Romain Guyc4b11a72009-05-13 15:46:37 -070040import java.lang.annotation.ElementType;
41import java.lang.annotation.Retention;
42import java.lang.annotation.RetentionPolicy;
Narayan Kamathf013daa2017-05-09 12:55:02 +010043import java.lang.annotation.Target;
44import java.lang.reflect.Field;
45import java.lang.reflect.Modifier;
Richard Uhler350e6dc2015-05-18 10:48:52 -070046import java.util.HashMap;
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -070047import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050/**
Ian Rogersfe067a42013-02-22 19:59:23 -080051 * Provides various debugging methods for Android applications, including
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 * tracing and allocation counts.
53 * <p><strong>Logging Trace Files</strong></p>
54 * <p>Debug can create log files that give details about an application, such as
55 * a call stack and start/stop times for any running methods. See <a
56href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for
57 * information about reading trace files. To start logging trace files, call one
58 * of the startMethodTracing() methods. To stop tracing, call
59 * {@link #stopMethodTracing()}.
60 */
61public final class Debug
62{
Dan Egnor3eda9792010-03-05 13:28:36 -080063 private static final String TAG = "Debug";
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 /**
66 * Flags for startMethodTracing(). These can be ORed together.
67 *
68 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
69 * trace key file.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080070 *
71 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080073 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS;
75
76 /**
77 * Flags for printLoadedClasses(). Default behavior is to only show
78 * the class name.
79 */
80 public static final int SHOW_FULL_DETAIL = 1;
81 public static final int SHOW_CLASSLOADER = (1 << 1);
82 public static final int SHOW_INITIALIZED = (1 << 2);
83
84 // set/cleared by waitForDebugger()
85 private static volatile boolean mWaiting = false;
86
87 private Debug() {}
88
89 /*
90 * How long to wait for the debugger to finish sending requests. I've
91 * seen this hit 800msec on the device while waiting for a response
92 * to travel over USB and get processed, so we take that and add
93 * half a second.
94 */
95 private static final int MIN_DEBUGGER_IDLE = 1300; // msec
96
97 /* how long to sleep when polling for activity */
98 private static final int SPIN_DELAY = 200; // msec
99
100 /**
101 * Default trace file path and file
102 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static final String DEFAULT_TRACE_BODY = "dmtrace";
104 private static final String DEFAULT_TRACE_EXTENSION = ".trace";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 /**
107 * This class is used to retrieved various statistics about the memory mappings for this
justinmuller64551382013-10-16 22:06:55 -0600108 * 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 -0800109 */
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700110 public static class MemoryInfo implements Parcelable {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700111 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 public int dalvikPss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700113 /** The proportional set size that is swappable for dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700114 /** @hide We may want to expose this, eventually. */
115 public int dalvikSwappablePss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700116 /** The private dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public int dalvikPrivateDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700118 /** The shared dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 public int dalvikSharedDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700120 /** The private clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700121 /** @hide We may want to expose this, eventually. */
122 public int dalvikPrivateClean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700123 /** The shared clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700124 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700125 public int dalvikSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700126 /** The dirty dalvik pages that have been swapped out. */
127 /** @hide We may want to expose this, eventually. */
128 public int dalvikSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800129 /** The dirty dalvik pages that have been swapped out, proportional. */
130 /** @hide We may want to expose this, eventually. */
131 public int dalvikSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133 /** The proportional set size for the native heap. */
134 public int nativePss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700135 /** The proportional set size that is swappable for the native heap. */
136 /** @hide We may want to expose this, eventually. */
137 public int nativeSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /** The private dirty pages used by the native heap. */
139 public int nativePrivateDirty;
140 /** The shared dirty pages used by the native heap. */
141 public int nativeSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700142 /** The private clean pages used by the native heap. */
143 /** @hide We may want to expose this, eventually. */
144 public int nativePrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700145 /** The shared clean pages used by the native heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700146 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700147 public int nativeSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700148 /** The dirty native pages that have been swapped out. */
149 /** @hide We may want to expose this, eventually. */
150 public int nativeSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800151 /** The dirty native pages that have been swapped out, proportional. */
152 /** @hide We may want to expose this, eventually. */
153 public int nativeSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155 /** The proportional set size for everything else. */
156 public int otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700157 /** The proportional set size that is swappable for everything else. */
158 /** @hide We may want to expose this, eventually. */
159 public int otherSwappablePss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 /** The private dirty pages used by everything else. */
161 public int otherPrivateDirty;
162 /** The shared dirty pages used by everything else. */
163 public int otherSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700164 /** The private clean pages used by everything else. */
165 /** @hide We may want to expose this, eventually. */
166 public int otherPrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700167 /** The shared clean pages used by everything else. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700168 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700169 public int otherSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700170 /** The dirty pages used by anyting else that have been swapped out. */
171 /** @hide We may want to expose this, eventually. */
172 public int otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800173 /** The dirty pages used by anyting else that have been swapped out, proportional. */
174 /** @hide We may want to expose this, eventually. */
175 public int otherSwappedOutPss;
176
177 /** Whether the kernel reports proportional swap usage */
178 /** @hide */
179 public boolean hasSwappedOutPss;
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200180
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700181 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700182 public static final int HEAP_UNKNOWN = 0;
183 /** @hide */
184 public static final int HEAP_DALVIK = 1;
185 /** @hide */
186 public static final int HEAP_NATIVE = 2;
187
188 /** @hide */
189 public static final int OTHER_DALVIK_OTHER = 0;
190 /** @hide */
191 public static final int OTHER_STACK = 1;
192 /** @hide */
193 public static final int OTHER_CURSOR = 2;
194 /** @hide */
195 public static final int OTHER_ASHMEM = 3;
196 /** @hide */
197 public static final int OTHER_GL_DEV = 4;
198 /** @hide */
199 public static final int OTHER_UNKNOWN_DEV = 5;
200 /** @hide */
201 public static final int OTHER_SO = 6;
202 /** @hide */
203 public static final int OTHER_JAR = 7;
204 /** @hide */
205 public static final int OTHER_APK = 8;
206 /** @hide */
207 public static final int OTHER_TTF = 9;
208 /** @hide */
209 public static final int OTHER_DEX = 10;
210 /** @hide */
211 public static final int OTHER_OAT = 11;
212 /** @hide */
213 public static final int OTHER_ART = 12;
214 /** @hide */
215 public static final int OTHER_UNKNOWN_MAP = 13;
216 /** @hide */
217 public static final int OTHER_GRAPHICS = 14;
218 /** @hide */
219 public static final int OTHER_GL = 15;
220 /** @hide */
221 public static final int OTHER_OTHER_MEMTRACK = 16;
222
Mathieu Chartiercb862452017-07-13 15:01:34 -0700223 // Needs to be declared here for the DVK_STAT ranges below.
224 /** @hide */
225 public static final int NUM_OTHER_STATS = 17;
226
227 // Dalvik subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700228 /** @hide */
229 public static final int OTHER_DALVIK_NORMAL = 17;
230 /** @hide */
231 public static final int OTHER_DALVIK_LARGE = 18;
232 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700233 public static final int OTHER_DALVIK_ZYGOTE = 19;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700234 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700235 public static final int OTHER_DALVIK_NON_MOVING = 20;
236 // Section begins and ends for dumpsys, relative to the DALVIK categories.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700237 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700238 public static final int OTHER_DVK_STAT_DALVIK_START =
239 OTHER_DALVIK_NORMAL - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700240 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700241 public static final int OTHER_DVK_STAT_DALVIK_END =
242 OTHER_DALVIK_NON_MOVING - NUM_OTHER_STATS;
243
244 // Dalvik Other subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700245 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700246 public static final int OTHER_DALVIK_OTHER_LINEARALLOC = 21;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700247 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700248 public static final int OTHER_DALVIK_OTHER_ACCOUNTING = 22;
249 /** @hide */
250 public static final int OTHER_DALVIK_OTHER_CODE_CACHE = 23;
251 /** @hide */
252 public static final int OTHER_DALVIK_OTHER_COMPILER_METADATA = 24;
253 /** @hide */
254 public static final int OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE = 25;
255 /** @hide */
256 public static final int OTHER_DVK_STAT_DALVIK_OTHER_START =
257 OTHER_DALVIK_OTHER_LINEARALLOC - NUM_OTHER_STATS;
258 /** @hide */
259 public static final int OTHER_DVK_STAT_DALVIK_OTHER_END =
260 OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE - NUM_OTHER_STATS;
261
262 // Dex subsections (Boot vdex, App dex, and App vdex).
263 /** @hide */
264 public static final int OTHER_DEX_BOOT_VDEX = 26;
265 /** @hide */
266 public static final int OTHER_DEX_APP_DEX = 27;
267 /** @hide */
268 public static final int OTHER_DEX_APP_VDEX = 28;
269 /** @hide */
270 public static final int OTHER_DVK_STAT_DEX_START = OTHER_DEX_BOOT_VDEX - NUM_OTHER_STATS;
271 /** @hide */
272 public static final int OTHER_DVK_STAT_DEX_END = OTHER_DEX_APP_VDEX - NUM_OTHER_STATS;
273
274 // Art subsections (App image, boot image).
275 /** @hide */
276 public static final int OTHER_ART_APP = 29;
277 /** @hide */
278 public static final int OTHER_ART_BOOT = 30;
279 /** @hide */
280 public static final int OTHER_DVK_STAT_ART_START = OTHER_ART_APP - NUM_OTHER_STATS;
281 /** @hide */
282 public static final int OTHER_DVK_STAT_ART_END = OTHER_ART_BOOT - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700283
284 /** @hide */
Mathieu Chartiercb862452017-07-13 15:01:34 -0700285 public static final int NUM_DVK_STATS = 14;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700286
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700287 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800288 public static final int NUM_CATEGORIES = 8;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700289
290 /** @hide */
291 public static final int offsetPss = 0;
292 /** @hide */
293 public static final int offsetSwappablePss = 1;
294 /** @hide */
295 public static final int offsetPrivateDirty = 2;
296 /** @hide */
297 public static final int offsetSharedDirty = 3;
298 /** @hide */
299 public static final int offsetPrivateClean = 4;
300 /** @hide */
301 public static final int offsetSharedClean = 5;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700302 /** @hide */
303 public static final int offsetSwappedOut = 6;
Martijn Coenene0764852016-01-07 17:04:22 -0800304 /** @hide */
305 public static final int offsetSwappedOutPss = 7;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700306
307 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700308
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700309 public MemoryInfo() {
310 }
311
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700312 /**
313 * Return total PSS memory usage in kB.
314 */
315 public int getTotalPss() {
Martijn Coenene0764852016-01-07 17:04:22 -0800316 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700317 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200318
Dianne Hackbornc8230512013-07-13 21:32:12 -0700319 /**
320 * @hide Return total PSS memory usage in kB.
321 */
322 public int getTotalUss() {
323 return dalvikPrivateClean + dalvikPrivateDirty
324 + nativePrivateClean + nativePrivateDirty
325 + otherPrivateClean + otherPrivateDirty;
326 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700327
328 /**
Martijn Coenene0764852016-01-07 17:04:22 -0800329 * Return total PSS memory usage in kB mapping a file of one of the following extension:
330 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art .
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700331 */
332 public int getTotalSwappablePss() {
333 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss;
334 }
335
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700336 /**
337 * Return total private dirty memory usage in kB.
338 */
339 public int getTotalPrivateDirty() {
340 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty;
341 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200342
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700343 /**
344 * Return total shared dirty memory usage in kB.
345 */
346 public int getTotalSharedDirty() {
347 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty;
348 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200349
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700350 /**
351 * Return total shared clean memory usage in kB.
352 */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700353 public int getTotalPrivateClean() {
354 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean;
355 }
356
357 /**
358 * Return total shared clean memory usage in kB.
359 */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700360 public int getTotalSharedClean() {
361 return dalvikSharedClean + nativeSharedClean + otherSharedClean;
362 }
363
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700364 /**
365 * Return total swapped out memory in kB.
366 * @hide
367 */
368 public int getTotalSwappedOut() {
369 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut;
370 }
371
Martijn Coenene0764852016-01-07 17:04:22 -0800372 /**
373 * Return total swapped out memory in kB, proportional.
374 * @hide
375 */
376 public int getTotalSwappedOutPss() {
377 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss;
378 }
379
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700380 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700381 public int getOtherPss(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700382 return otherStats[which*NUM_CATEGORIES + offsetPss];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700383 }
384
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700385
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700386 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700387 public int getOtherSwappablePss(int which) {
388 return otherStats[which*NUM_CATEGORIES + offsetSwappablePss];
389 }
390
391
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700392 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700393 public int getOtherPrivateDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700394 return otherStats[which*NUM_CATEGORIES + offsetPrivateDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700395 }
396
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700397 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700398 public int getOtherSharedDirty(int which) {
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700399 return otherStats[which*NUM_CATEGORIES + offsetSharedDirty];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700400 }
401
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700402 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700403 public int getOtherPrivateClean(int which) {
404 return otherStats[which*NUM_CATEGORIES + offsetPrivateClean];
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700405 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700406
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700407 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700408 public int getOtherPrivate(int which) {
409 return getOtherPrivateClean(which) + getOtherPrivateDirty(which);
410 }
411
412 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700413 public int getOtherSharedClean(int which) {
414 return otherStats[which*NUM_CATEGORIES + offsetSharedClean];
415 }
416
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700417 /** @hide */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700418 public int getOtherSwappedOut(int which) {
419 return otherStats[which*NUM_CATEGORIES + offsetSwappedOut];
420 }
421
422 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800423 public int getOtherSwappedOutPss(int which) {
424 return otherStats[which*NUM_CATEGORIES + offsetSwappedOutPss];
425 }
426
427 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700428 public static String getOtherLabel(int which) {
429 switch (which) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700430 case OTHER_DALVIK_OTHER: return "Dalvik Other";
431 case OTHER_STACK: return "Stack";
432 case OTHER_CURSOR: return "Cursor";
433 case OTHER_ASHMEM: return "Ashmem";
434 case OTHER_GL_DEV: return "Gfx dev";
435 case OTHER_UNKNOWN_DEV: return "Other dev";
436 case OTHER_SO: return ".so mmap";
437 case OTHER_JAR: return ".jar mmap";
438 case OTHER_APK: return ".apk mmap";
439 case OTHER_TTF: return ".ttf mmap";
440 case OTHER_DEX: return ".dex mmap";
441 case OTHER_OAT: return ".oat mmap";
442 case OTHER_ART: return ".art mmap";
443 case OTHER_UNKNOWN_MAP: return "Other mmap";
444 case OTHER_GRAPHICS: return "EGL mtrack";
445 case OTHER_GL: return "GL mtrack";
446 case OTHER_OTHER_MEMTRACK: return "Other mtrack";
447 case OTHER_DALVIK_NORMAL: return ".Heap";
448 case OTHER_DALVIK_LARGE: return ".LOS";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700449 case OTHER_DALVIK_ZYGOTE: return ".Zygote";
450 case OTHER_DALVIK_NON_MOVING: return ".NonMoving";
Mathieu Chartiercb862452017-07-13 15:01:34 -0700451 case OTHER_DALVIK_OTHER_LINEARALLOC: return ".LinearAlloc";
452 case OTHER_DALVIK_OTHER_ACCOUNTING: return ".GC";
453 case OTHER_DALVIK_OTHER_CODE_CACHE: return ".JITCache";
454 case OTHER_DALVIK_OTHER_COMPILER_METADATA: return ".CompilerMetadata";
455 case OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE: return ".IndirectRef";
456 case OTHER_DEX_BOOT_VDEX: return ".Boot vdex";
457 case OTHER_DEX_APP_DEX: return ".App dex";
458 case OTHER_DEX_APP_VDEX: return ".App vdex";
459 case OTHER_ART_APP: return ".App art";
460 case OTHER_ART_BOOT: return ".Boot art";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700461 default: return "????";
462 }
463 }
464
Richard Uhler350e6dc2015-05-18 10:48:52 -0700465 /**
466 * Returns the value of a particular memory statistic or {@code null} if no
467 * such memory statistic exists.
468 *
469 * <p>The following table lists the memory statistics that are supported.
470 * Note that memory statistics may be added or removed in a future API level.</p>
471 *
472 * <table>
473 * <thead>
474 * <tr>
475 * <th>Memory statistic name</th>
476 * <th>Meaning</th>
477 * <th>Example</th>
478 * <th>Supported (API Levels)</th>
479 * </tr>
480 * </thead>
481 * <tbody>
482 * <tr>
483 * <td>summary.java-heap</td>
484 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field
485 * in the App Summary section output by dumpsys meminfo.</td>
486 * <td>{@code 1442}</td>
487 * <td>23</td>
488 * </tr>
489 * <tr>
490 * <td>summary.native-heap</td>
491 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap
492 * field in the App Summary section output by dumpsys meminfo.</td>
493 * <td>{@code 1442}</td>
494 * <td>23</td>
495 * </tr>
496 * <tr>
497 * <td>summary.code</td>
498 * <td>The memory usage for static code and resources in kB. This corresponds to
499 * the Code field in the App Summary section output by dumpsys meminfo.</td>
500 * <td>{@code 1442}</td>
501 * <td>23</td>
502 * </tr>
503 * <tr>
504 * <td>summary.stack</td>
505 * <td>The stack usage in kB. This corresponds to the Stack field in the
506 * App Summary section output by dumpsys meminfo.</td>
507 * <td>{@code 1442}</td>
508 * <td>23</td>
509 * </tr>
510 * <tr>
511 * <td>summary.graphics</td>
512 * <td>The graphics usage in kB. This corresponds to the Graphics field in the
513 * App Summary section output by dumpsys meminfo.</td>
514 * <td>{@code 1442}</td>
515 * <td>23</td>
516 * </tr>
517 * <tr>
518 * <td>summary.private-other</td>
519 * <td>Other private memory usage in kB. This corresponds to the Private Other
520 * field output in the App Summary section by dumpsys meminfo.</td>
521 * <td>{@code 1442}</td>
522 * <td>23</td>
523 * </tr>
524 * <tr>
525 * <td>summary.system</td>
526 * <td>Shared and system memory usage in kB. This corresponds to the System
527 * field output in the App Summary section by dumpsys meminfo.</td>
528 * <td>{@code 1442}</td>
529 * <td>23</td>
530 * </tr>
531 * <tr>
532 * <td>summary.total-pss</td>
533 * <td>Total PPS memory usage in kB.</td>
534 * <td>{@code 1442}</td>
535 * <td>23</td>
536 * </tr>
537 * <tr>
538 * <td>summary.total-swap</td>
539 * <td>Total swap usage in kB.</td>
540 * <td>{@code 1442}</td>
541 * <td>23</td>
542 * </tr>
543 * </tbody>
544 * </table>
545 */
Dianne Hackbornb02ce292015-10-12 15:14:16 -0700546 public String getMemoryStat(String statName) {
Richard Uhler350e6dc2015-05-18 10:48:52 -0700547 switch(statName) {
548 case "summary.java-heap":
549 return Integer.toString(getSummaryJavaHeap());
550 case "summary.native-heap":
551 return Integer.toString(getSummaryNativeHeap());
552 case "summary.code":
553 return Integer.toString(getSummaryCode());
554 case "summary.stack":
555 return Integer.toString(getSummaryStack());
556 case "summary.graphics":
557 return Integer.toString(getSummaryGraphics());
558 case "summary.private-other":
559 return Integer.toString(getSummaryPrivateOther());
560 case "summary.system":
561 return Integer.toString(getSummarySystem());
562 case "summary.total-pss":
563 return Integer.toString(getSummaryTotalPss());
564 case "summary.total-swap":
565 return Integer.toString(getSummaryTotalSwap());
566 default:
567 return null;
568 }
569 }
570
571 /**
572 * Returns a map of the names/values of the memory statistics
573 * that {@link #getMemoryStat(String)} supports.
574 *
575 * @return a map of the names/values of the supported memory statistics.
576 */
577 public Map<String, String> getMemoryStats() {
578 Map<String, String> stats = new HashMap<String, String>();
579 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap()));
580 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap()));
581 stats.put("summary.code", Integer.toString(getSummaryCode()));
582 stats.put("summary.stack", Integer.toString(getSummaryStack()));
583 stats.put("summary.graphics", Integer.toString(getSummaryGraphics()));
584 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther()));
585 stats.put("summary.system", Integer.toString(getSummarySystem()));
586 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss()));
587 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap()));
588 return stats;
589 }
590
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700591 /**
592 * Pss of Java Heap bytes in KB due to the application.
593 * Notes:
594 * * OTHER_ART is the boot image. Anything private here is blamed on
595 * the application, not the system.
596 * * dalvikPrivateDirty includes private zygote, which means the
597 * application dirtied something allocated by the zygote. We blame
598 * the application for that memory, not the system.
599 * * Does not include OTHER_DALVIK_OTHER, which is considered VM
600 * Overhead and lumped into Private Other.
601 * * We don't include dalvikPrivateClean, because there should be no
602 * such thing as private clean for the Java Heap.
603 * @hide
604 */
605 public int getSummaryJavaHeap() {
606 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART);
607 }
608
609 /**
610 * Pss of Native Heap bytes in KB due to the application.
611 * Notes:
612 * * Includes private dirty malloc space.
613 * * We don't include nativePrivateClean, because there should be no
614 * such thing as private clean for the Native Heap.
615 * @hide
616 */
617 public int getSummaryNativeHeap() {
618 return nativePrivateDirty;
619 }
620
621 /**
622 * Pss of code and other static resource bytes in KB due to
623 * the application.
624 * @hide
625 */
626 public int getSummaryCode() {
627 return getOtherPrivate(OTHER_SO)
628 + getOtherPrivate(OTHER_JAR)
629 + getOtherPrivate(OTHER_APK)
630 + getOtherPrivate(OTHER_TTF)
631 + getOtherPrivate(OTHER_DEX)
632 + getOtherPrivate(OTHER_OAT);
633 }
634
635 /**
636 * Pss in KB of the stack due to the application.
637 * Notes:
638 * * Includes private dirty stack, which includes both Java and Native
639 * stack.
640 * * Does not include private clean stack, because there should be no
641 * such thing as private clean for the stack.
642 * @hide
643 */
644 public int getSummaryStack() {
645 return getOtherPrivateDirty(OTHER_STACK);
646 }
647
648 /**
649 * Pss in KB of graphics due to the application.
650 * Notes:
651 * * Includes private Gfx, EGL, and GL.
652 * * Warning: These numbers can be misreported by the graphics drivers.
653 * * We don't include shared graphics. It may make sense to, because
654 * shared graphics are likely buffers due to the application
655 * anyway, but it's simpler to implement to just group all shared
656 * memory into the System category.
657 * @hide
658 */
659 public int getSummaryGraphics() {
660 return getOtherPrivate(OTHER_GL_DEV)
661 + getOtherPrivate(OTHER_GRAPHICS)
662 + getOtherPrivate(OTHER_GL);
663 }
664
665 /**
666 * Pss in KB due to the application that haven't otherwise been
667 * accounted for.
668 * @hide
669 */
670 public int getSummaryPrivateOther() {
671 return getTotalPrivateClean()
672 + getTotalPrivateDirty()
673 - getSummaryJavaHeap()
674 - getSummaryNativeHeap()
675 - getSummaryCode()
676 - getSummaryStack()
677 - getSummaryGraphics();
678 }
679
680 /**
681 * Pss in KB due to the system.
682 * Notes:
683 * * Includes all shared memory.
684 * @hide
685 */
686 public int getSummarySystem() {
687 return getTotalPss()
688 - getTotalPrivateClean()
689 - getTotalPrivateDirty();
690 }
691
692 /**
693 * Total Pss in KB.
694 * @hide
695 */
696 public int getSummaryTotalPss() {
697 return getTotalPss();
698 }
699
700 /**
701 * Total Swap in KB.
702 * Notes:
703 * * Some of this memory belongs in other categories, but we don't
704 * know if the Swap memory is shared or private, so we don't know
705 * what to blame on the application and what on the system.
706 * For now, just lump all the Swap in one place.
Martijn Coenene0764852016-01-07 17:04:22 -0800707 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()}
708 * will report the application proportional Swap.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700709 * @hide
710 */
711 public int getSummaryTotalSwap() {
712 return getTotalSwappedOut();
713 }
714
Martijn Coenene0764852016-01-07 17:04:22 -0800715 /**
716 * Total proportional Swap in KB.
717 * Notes:
718 * * Always 0 if {@link #hasSwappedOutPss} is false.
719 * @hide
720 */
721 public int getSummaryTotalSwapPss() {
722 return getTotalSwappedOutPss();
723 }
724
Dianne Hackbornef0a4022016-05-11 14:21:07 -0700725 /**
726 * Return true if the kernel is reporting pss swapped out... that is, if
727 * {@link #getSummaryTotalSwapPss()} will return non-0 values.
728 * @hide
729 */
730 public boolean hasSwappedOutPss() {
731 return hasSwappedOutPss;
732 }
733
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700734 public int describeContents() {
735 return 0;
736 }
737
738 public void writeToParcel(Parcel dest, int flags) {
739 dest.writeInt(dalvikPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700740 dest.writeInt(dalvikSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700741 dest.writeInt(dalvikPrivateDirty);
742 dest.writeInt(dalvikSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700743 dest.writeInt(dalvikPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700744 dest.writeInt(dalvikSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700745 dest.writeInt(dalvikSwappedOut);
Richard Uhlera1782052017-06-23 16:54:25 +0100746 dest.writeInt(dalvikSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700747 dest.writeInt(nativePss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700748 dest.writeInt(nativeSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700749 dest.writeInt(nativePrivateDirty);
750 dest.writeInt(nativeSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700751 dest.writeInt(nativePrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700752 dest.writeInt(nativeSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700753 dest.writeInt(nativeSwappedOut);
Richard Uhlera1782052017-06-23 16:54:25 +0100754 dest.writeInt(nativeSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700755 dest.writeInt(otherPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700756 dest.writeInt(otherSwappablePss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700757 dest.writeInt(otherPrivateDirty);
758 dest.writeInt(otherSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700759 dest.writeInt(otherPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700760 dest.writeInt(otherSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700761 dest.writeInt(otherSwappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800762 dest.writeInt(hasSwappedOutPss ? 1 : 0);
763 dest.writeInt(otherSwappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700764 dest.writeIntArray(otherStats);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700765 }
766
767 public void readFromParcel(Parcel source) {
768 dalvikPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700769 dalvikSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700770 dalvikPrivateDirty = source.readInt();
771 dalvikSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700772 dalvikPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700773 dalvikSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700774 dalvikSwappedOut = source.readInt();
Richard Uhlera1782052017-06-23 16:54:25 +0100775 dalvikSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700776 nativePss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700777 nativeSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700778 nativePrivateDirty = source.readInt();
779 nativeSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700780 nativePrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700781 nativeSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700782 nativeSwappedOut = source.readInt();
Richard Uhlera1782052017-06-23 16:54:25 +0100783 nativeSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700784 otherPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700785 otherSwappablePss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700786 otherPrivateDirty = source.readInt();
787 otherSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700788 otherPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700789 otherSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700790 otherSwappedOut = source.readInt();
Martijn Coenene0764852016-01-07 17:04:22 -0800791 hasSwappedOutPss = source.readInt() != 0;
792 otherSwappedOutPss = source.readInt();
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700793 otherStats = source.createIntArray();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700794 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200795
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700796 public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
797 public MemoryInfo createFromParcel(Parcel source) {
798 return new MemoryInfo(source);
799 }
800 public MemoryInfo[] newArray(int size) {
801 return new MemoryInfo[size];
802 }
803 };
804
805 private MemoryInfo(Parcel source) {
806 readFromParcel(source);
807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810
811 /**
812 * Wait until a debugger attaches. As soon as the debugger attaches,
813 * this returns, so you will need to place a breakpoint after the
814 * waitForDebugger() call if you want to start tracing immediately.
815 */
816 public static void waitForDebugger() {
817 if (!VMDebug.isDebuggingEnabled()) {
818 //System.out.println("debugging not enabled, not waiting");
819 return;
820 }
821 if (isDebuggerConnected())
822 return;
823
824 // if DDMS is listening, inform them of our plight
825 System.out.println("Sending WAIT chunk");
826 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger"
827 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
828 DdmServer.sendChunk(waitChunk);
829
830 mWaiting = true;
831 while (!isDebuggerConnected()) {
832 try { Thread.sleep(SPIN_DELAY); }
833 catch (InterruptedException ie) {}
834 }
835 mWaiting = false;
836
837 System.out.println("Debugger has connected");
838
839 /*
840 * There is no "ready to go" signal from the debugger, and we're
841 * not allowed to suspend ourselves -- the debugger expects us to
842 * be running happily, and gets confused if we aren't. We need to
843 * allow the debugger a chance to set breakpoints before we start
844 * running again.
845 *
846 * Sit and spin until the debugger has been idle for a short while.
847 */
848 while (true) {
849 long delta = VMDebug.lastDebuggerActivity();
850 if (delta < 0) {
851 System.out.println("debugger detached?");
852 break;
853 }
854
855 if (delta < MIN_DEBUGGER_IDLE) {
856 System.out.println("waiting for debugger to settle...");
857 try { Thread.sleep(SPIN_DELAY); }
858 catch (InterruptedException ie) {}
859 } else {
860 System.out.println("debugger has settled (" + delta + ")");
861 break;
862 }
863 }
864 }
865
866 /**
867 * Returns "true" if one or more threads is waiting for a debugger
868 * to attach.
869 */
870 public static boolean waitingForDebugger() {
871 return mWaiting;
872 }
873
874 /**
875 * Determine if a debugger is currently attached.
876 */
877 public static boolean isDebuggerConnected() {
878 return VMDebug.isDebuggerConnected();
879 }
880
881 /**
Andy McFaddene5772322010-01-22 07:23:31 -0800882 * Returns an array of strings that identify VM features. This is
883 * used by DDMS to determine what sorts of operations the VM can
884 * perform.
885 *
886 * @hide
887 */
888 public static String[] getVmFeatureList() {
889 return VMDebug.getVmFeatureList();
890 }
891
892 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 * Change the JDWP port.
894 *
895 * @deprecated no longer needed or useful
896 */
897 @Deprecated
898 public static void changeDebugPort(int port) {}
899
900 /**
901 * This is the pathname to the sysfs file that enables and disables
902 * tracing on the qemu emulator.
903 */
904 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
905
906 /**
907 * Enable qemu tracing. For this to work requires running everything inside
908 * the qemu emulator; otherwise, this method will have no effect. The trace
909 * file is specified on the command line when the emulator is started. For
910 * example, the following command line <br />
911 * <code>emulator -trace foo</code><br />
912 * will start running the emulator and create a trace file named "foo". This
913 * method simply enables writing the trace records to the trace file.
914 *
915 * <p>
916 * The main differences between this and {@link #startMethodTracing()} are
917 * that tracing in the qemu emulator traces every cpu instruction of every
918 * process, including kernel code, so we have more complete information,
919 * including all context switches. We can also get more detailed information
920 * such as cache misses. The sequence of calls is determined by
921 * post-processing the instruction trace. The qemu tracing is also done
922 * without modifying the application or perturbing the timing of calls
923 * because no instrumentation is added to the application being traced.
924 * </p>
925 *
926 * <p>
927 * One limitation of using this method compared to using
928 * {@link #startMethodTracing()} on the real device is that the emulator
929 * does not model all of the real hardware effects such as memory and
930 * bus contention. The emulator also has a simple cache model and cannot
931 * capture all the complexities of a real cache.
932 * </p>
933 */
934 public static void startNativeTracing() {
935 // Open the sysfs file for writing and write "1" to it.
936 PrintWriter outStream = null;
937 try {
938 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700939 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 outStream.println("1");
941 } catch (Exception e) {
942 } finally {
943 if (outStream != null)
944 outStream.close();
945 }
946
947 VMDebug.startEmulatorTracing();
948 }
949
950 /**
951 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing.
952 *
953 * <p>Tracing can be started and stopped as many times as desired. When
954 * the qemu emulator itself is stopped then the buffered trace records
955 * are flushed and written to the trace file. In fact, it is not necessary
956 * to call this method at all; simply killing qemu is sufficient. But
957 * starting and stopping a trace is useful for examining a specific
958 * region of code.</p>
959 */
960 public static void stopNativeTracing() {
961 VMDebug.stopEmulatorTracing();
962
963 // Open the sysfs file for writing and write "0" to it.
964 PrintWriter outStream = null;
965 try {
966 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700967 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 outStream.println("0");
969 } catch (Exception e) {
970 // We could print an error message here but we probably want
971 // to quietly ignore errors if we are not running in the emulator.
972 } finally {
973 if (outStream != null)
974 outStream.close();
975 }
976 }
977
978 /**
979 * Enable "emulator traces", in which information about the current
980 * method is made available to the "emulator -trace" feature. There
981 * is no corresponding "disable" call -- this is intended for use by
982 * the framework when tracing should be turned on and left that way, so
983 * that traces captured with F9/F10 will include the necessary data.
984 *
985 * This puts the VM into "profile" mode, which has performance
986 * consequences.
987 *
988 * To temporarily enable tracing, use {@link #startNativeTracing()}.
989 */
990 public static void enableEmulatorTraceOutput() {
991 VMDebug.startEmulatorTracing();
992 }
993
994 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -0600995 * Start method tracing with default log name and buffer size.
996 * <p>
997 * By default, the trace file is called "dmtrace.trace" and it's placed
998 * under your package-specific directory on primary shared/external storage,
999 * as returned by {@link Context#getExternalFilesDir(String)}.
1000 * <p>
1001 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1002 * A Graphical Log Viewer</a> for information about reading trace files.
1003 * <p class="note">
1004 * When method tracing is enabled, the VM will run more slowly than usual,
1005 * so the timings from the trace files should only be considered in relative
1006 * terms (e.g. was run #1 faster than run #2). The times for native methods
1007 * will not change, so don't try to use this to compare the performance of
1008 * interpreted and native implementations of the same method. As an
1009 * alternative, consider using sampling-based method tracing via
1010 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1011 * in the emulator via {@link #startNativeTracing()}.
1012 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 */
1014 public static void startMethodTracing() {
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001015 VMDebug.startMethodTracing(fixTracePath(null), 0, 0, false, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
1017
1018 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001019 * Start method tracing, specifying the trace log file path.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * <p>
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001021 * When a relative file path is given, the trace file will be placed under
1022 * your package-specific directory on primary shared/external storage, as
1023 * returned by {@link Context#getExternalFilesDir(String)}.
1024 * <p>
1025 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1026 * A Graphical Log Viewer</a> for information about reading trace files.
1027 * <p class="note">
1028 * When method tracing is enabled, the VM will run more slowly than usual,
1029 * so the timings from the trace files should only be considered in relative
1030 * terms (e.g. was run #1 faster than run #2). The times for native methods
1031 * will not change, so don't try to use this to compare the performance of
1032 * interpreted and native implementations of the same method. As an
1033 * alternative, consider using sampling-based method tracing via
1034 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1035 * in the emulator via {@link #startNativeTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 * </p>
1037 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001038 * @param tracePath Path to the trace log file to create. If {@code null},
1039 * this will default to "dmtrace.trace". If the file already
1040 * exists, it will be truncated. If the path given does not end
1041 * in ".trace", it will be appended for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001043 public static void startMethodTracing(String tracePath) {
1044 startMethodTracing(tracePath, 0, 0);
1045 }
1046
1047 /**
1048 * Start method tracing, specifying the trace log file name and the buffer
1049 * size.
1050 * <p>
1051 * When a relative file path is given, the trace file will be placed under
1052 * your package-specific directory on primary shared/external storage, as
1053 * returned by {@link Context#getExternalFilesDir(String)}.
1054 * <p>
1055 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1056 * A Graphical Log Viewer</a> for information about reading trace files.
1057 * <p class="note">
1058 * When method tracing is enabled, the VM will run more slowly than usual,
1059 * so the timings from the trace files should only be considered in relative
1060 * terms (e.g. was run #1 faster than run #2). The times for native methods
1061 * will not change, so don't try to use this to compare the performance of
1062 * interpreted and native implementations of the same method. As an
1063 * alternative, consider using sampling-based method tracing via
1064 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1065 * in the emulator via {@link #startNativeTracing()}.
1066 * </p>
1067 *
1068 * @param tracePath Path to the trace log file to create. If {@code null},
1069 * this will default to "dmtrace.trace". If the file already
1070 * exists, it will be truncated. If the path given does not end
1071 * in ".trace", it will be appended for you.
1072 * @param bufferSize The maximum amount of trace data we gather. If not
1073 * given, it defaults to 8MB.
1074 */
1075 public static void startMethodTracing(String tracePath, int bufferSize) {
1076 startMethodTracing(tracePath, bufferSize, 0);
1077 }
1078
1079 /**
1080 * Start method tracing, specifying the trace log file name, the buffer
1081 * size, and flags.
1082 * <p>
1083 * When a relative file path is given, the trace file will be placed under
1084 * your package-specific directory on primary shared/external storage, as
1085 * returned by {@link Context#getExternalFilesDir(String)}.
1086 * <p>
1087 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1088 * A Graphical Log Viewer</a> for information about reading trace files.
1089 * <p class="note">
1090 * When method tracing is enabled, the VM will run more slowly than usual,
1091 * so the timings from the trace files should only be considered in relative
1092 * terms (e.g. was run #1 faster than run #2). The times for native methods
1093 * will not change, so don't try to use this to compare the performance of
1094 * interpreted and native implementations of the same method. As an
1095 * alternative, consider using sampling-based method tracing via
1096 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1097 * in the emulator via {@link #startNativeTracing()}.
1098 * </p>
1099 *
1100 * @param tracePath Path to the trace log file to create. If {@code null},
1101 * this will default to "dmtrace.trace". If the file already
1102 * exists, it will be truncated. If the path given does not end
1103 * in ".trace", it will be appended for you.
1104 * @param bufferSize The maximum amount of trace data we gather. If not
1105 * given, it defaults to 8MB.
1106 * @param flags Flags to control method tracing. The only one that is
1107 * currently defined is {@link #TRACE_COUNT_ALLOCS}.
1108 */
1109 public static void startMethodTracing(String tracePath, int bufferSize, int flags) {
1110 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, flags, false, 0);
Jeff Haod02e60f2014-01-06 15:52:52 -08001111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112
Jeff Haod02e60f2014-01-06 15:52:52 -08001113 /**
1114 * Start sampling-based method tracing, specifying the trace log file name,
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001115 * the buffer size, and the sampling interval.
1116 * <p>
1117 * When a relative file path is given, the trace file will be placed under
1118 * your package-specific directory on primary shared/external storage, as
1119 * returned by {@link Context#getExternalFilesDir(String)}.
1120 * <p>
1121 * See <a href="{@docRoot}guide/developing/tools/traceview.html">Traceview:
1122 * A Graphical Log Viewer</a> for information about reading trace files.
Jeff Haod02e60f2014-01-06 15:52:52 -08001123 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001124 * @param tracePath Path to the trace log file to create. If {@code null},
1125 * this will default to "dmtrace.trace". If the file already
1126 * exists, it will be truncated. If the path given does not end
1127 * in ".trace", it will be appended for you.
1128 * @param bufferSize The maximum amount of trace data we gather. If not
1129 * given, it defaults to 8MB.
1130 * @param intervalUs The amount of time between each sample in microseconds.
Jeff Haod02e60f2014-01-06 15:52:52 -08001131 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001132 public static void startMethodTracingSampling(String tracePath, int bufferSize,
1133 int intervalUs) {
1134 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, 0, true, intervalUs);
Jeff Haod02e60f2014-01-06 15:52:52 -08001135 }
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001136
Jeff Haod02e60f2014-01-06 15:52:52 -08001137 /**
1138 * Formats name of trace log file for method tracing.
1139 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001140 private static String fixTracePath(String tracePath) {
1141 if (tracePath == null || tracePath.charAt(0) != '/') {
1142 final Context context = AppGlobals.getInitialApplication();
1143 final File dir;
1144 if (context != null) {
1145 dir = context.getExternalFilesDir(null);
1146 } else {
1147 dir = Environment.getExternalStorageDirectory();
1148 }
Jeff Haod02e60f2014-01-06 15:52:52 -08001149
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001150 if (tracePath == null) {
1151 tracePath = new File(dir, DEFAULT_TRACE_BODY).getAbsolutePath();
1152 } else {
1153 tracePath = new File(dir, tracePath).getAbsolutePath();
1154 }
1155 }
1156 if (!tracePath.endsWith(DEFAULT_TRACE_EXTENSION)) {
1157 tracePath += DEFAULT_TRACE_EXTENSION;
1158 }
1159 return tracePath;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
1161
1162 /**
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001163 * Like startMethodTracing(String, int, int), but taking an already-opened
1164 * FileDescriptor in which the trace is written. The file name is also
1165 * supplied simply for logging. Makes a dup of the file descriptor.
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02001166 *
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001167 * Not exposed in the SDK unless we are really comfortable with supporting
1168 * this and find it would be useful.
1169 * @hide
1170 */
1171 public static void startMethodTracing(String traceName, FileDescriptor fd,
Shukang Zhou6ec0b7e2017-01-24 15:30:29 -08001172 int bufferSize, int flags, boolean streamOutput) {
1173 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0, streamOutput);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001174 }
1175
1176 /**
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001177 * Starts method tracing without a backing file. When stopMethodTracing
1178 * is called, the result is sent directly to DDMS. (If DDMS is not
1179 * attached when tracing ends, the profiling data will be discarded.)
1180 *
1181 * @hide
1182 */
Jeff Hao7be3a132013-08-22 15:53:12 -07001183 public static void startMethodTracingDdms(int bufferSize, int flags,
1184 boolean samplingEnabled, int intervalUs) {
1185 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs);
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001186 }
1187
1188 /**
Jeff Haoac277052013-08-29 11:19:39 -07001189 * Determine whether method tracing is currently active and what type is
1190 * active.
1191 *
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001192 * @hide
1193 */
Jeff Haoac277052013-08-29 11:19:39 -07001194 public static int getMethodTracingMode() {
1195 return VMDebug.getMethodTracingMode();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001196 }
1197
1198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 * Stop method tracing.
1200 */
1201 public static void stopMethodTracing() {
1202 VMDebug.stopMethodTracing();
1203 }
1204
1205 /**
1206 * Get an indication of thread CPU usage. The value returned
1207 * indicates the amount of time that the current thread has spent
1208 * executing code or waiting for certain types of I/O.
1209 *
1210 * The time is expressed in nanoseconds, and is only meaningful
1211 * when compared to the result from an earlier call. Note that
1212 * nanosecond resolution does not imply nanosecond accuracy.
1213 *
1214 * On system which don't support this operation, the call returns -1.
1215 */
1216 public static long threadCpuTimeNanos() {
1217 return VMDebug.threadCpuTimeNanos();
1218 }
1219
1220 /**
Chet Haase2970c492010-11-09 13:58:04 -08001221 * Start counting the number and aggregate size of memory allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001223 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
1224 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
1225 * code doesn't cause additional allocations. The various <code>get</code> methods return
1226 * the specified value. And the various <code>reset</code> methods reset the specified
Chet Haase2970c492010-11-09 13:58:04 -08001227 * count.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001229 * <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 -08001230 * The per-thread counts for threads other than the current thread
Chet Haase2970c492010-11-09 13:58:04 -08001231 * are not cleared by the "reset" or "start" calls.</p>
Ian Rogersfe067a42013-02-22 19:59:23 -08001232 *
1233 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 */
Ian Rogersfe067a42013-02-22 19:59:23 -08001235 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 public static void startAllocCounting() {
1237 VMDebug.startAllocCounting();
1238 }
Chet Haase2970c492010-11-09 13:58:04 -08001239
1240 /**
1241 * Stop counting the number and aggregate size of memory allocations.
1242 *
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001243 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Chet Haase2970c492010-11-09 13:58:04 -08001244 */
Ian Rogersc2a3adb2013-04-19 11:31:48 -07001245 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 public static void stopAllocCounting() {
1247 VMDebug.stopAllocCounting();
1248 }
1249
Ian Rogersfe067a42013-02-22 19:59:23 -08001250 /**
1251 * Returns the global count of objects allocated by the runtime between a
1252 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001253 *
1254 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001255 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001256 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 public static int getGlobalAllocCount() {
1258 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1259 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001260
1261 /**
1262 * Clears the global count of objects allocated.
1263 * @see #getGlobalAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001264 *
1265 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001266 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001267 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001268 public static void resetGlobalAllocCount() {
1269 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1270 }
1271
1272 /**
1273 * Returns the global size, in bytes, of objects allocated by the runtime between a
1274 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001275 *
1276 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001277 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001278 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 public static int getGlobalAllocSize() {
1280 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1281 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001282
1283 /**
1284 * Clears the global size of objects allocated.
Dianne Hackborn3fa89692013-09-13 17:20:00 -07001285 * @see #getGlobalAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001286 *
1287 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001288 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001289 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001290 public static void resetGlobalAllocSize() {
1291 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1292 }
1293
1294 /**
1295 * Returns the global count of objects freed by the runtime between a
1296 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001297 *
1298 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001299 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001300 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 public static int getGlobalFreedCount() {
1302 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1303 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001304
1305 /**
1306 * Clears the global count of objects freed.
1307 * @see #getGlobalFreedCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001308 *
1309 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001310 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001311 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001312 public static void resetGlobalFreedCount() {
1313 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1314 }
1315
1316 /**
1317 * Returns the global size, in bytes, of objects freed by the runtime between a
1318 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001319 *
1320 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001321 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001322 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 public static int getGlobalFreedSize() {
1324 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1325 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001326
1327 /**
1328 * Clears the global size of objects freed.
1329 * @see #getGlobalFreedSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001330 *
1331 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001332 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001333 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001334 public static void resetGlobalFreedSize() {
1335 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1336 }
1337
1338 /**
1339 * Returns the number of non-concurrent GC invocations between a
1340 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001341 *
1342 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001343 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001344 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001345 public static int getGlobalGcInvocationCount() {
1346 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1347 }
1348
1349 /**
1350 * Clears the count of non-concurrent GC invocations.
1351 * @see #getGlobalGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001352 *
1353 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001354 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001355 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001356 public static void resetGlobalGcInvocationCount() {
1357 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1358 }
1359
1360 /**
1361 * Returns the number of classes successfully initialized (ie those that executed without
1362 * throwing an exception) between a {@link #startAllocCounting() start} and
1363 * {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001364 *
1365 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001366 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001367 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001368 public static int getGlobalClassInitCount() {
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001369 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1370 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001371
1372 /**
1373 * Clears the count of classes initialized.
1374 * @see #getGlobalClassInitCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001375 *
1376 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001377 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001378 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001379 public static void resetGlobalClassInitCount() {
1380 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1381 }
1382
1383 /**
1384 * Returns the time spent successfully initializing classes between a
1385 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001386 *
1387 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001388 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001389 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001390 public static int getGlobalClassInitTime() {
1391 /* cumulative elapsed time for class initialization, in usec */
1392 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1393 }
Carl Shapirob5961982010-12-22 15:54:53 -08001394
1395 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001396 * Clears the count of time spent initializing classes.
1397 * @see #getGlobalClassInitTime()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001398 *
1399 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001400 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001401 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001402 public static void resetGlobalClassInitTime() {
1403 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1404 }
1405
1406 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001407 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001408 * @deprecated This method is now obsolete.
1409 */
1410 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 public static int getGlobalExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001412 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
Carl Shapirob5961982010-12-22 15:54:53 -08001414
1415 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001416 * This method exists for compatibility and has no effect.
1417 * @deprecated This method is now obsolete.
1418 */
1419 @Deprecated
1420 public static void resetGlobalExternalAllocSize() {}
1421
1422 /**
1423 * This method exists for compatibility and has no effect.
1424 * @deprecated This method is now obsolete.
1425 */
1426 @Deprecated
1427 public static void resetGlobalExternalAllocCount() {}
1428
1429 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001430 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001431 * @deprecated This method is now obsolete.
1432 */
1433 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 public static int getGlobalExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001435 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 }
Carl Shapirob5961982010-12-22 15:54:53 -08001437
1438 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001439 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001440 * @deprecated This method is now obsolete.
1441 */
1442 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 public static int getGlobalExternalFreedCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001444 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 }
Carl Shapirob5961982010-12-22 15:54:53 -08001446
1447 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001448 * This method exists for compatibility and has no effect.
1449 * @deprecated This method is now obsolete.
1450 */
1451 @Deprecated
1452 public static void resetGlobalExternalFreedCount() {}
1453
1454 /**
1455 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001456 * @deprecated This method is now obsolete.
1457 */
1458 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 public static int getGlobalExternalFreedSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001460 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
Carl Shapirob5961982010-12-22 15:54:53 -08001462
Ian Rogersfe067a42013-02-22 19:59:23 -08001463 /**
1464 * This method exists for compatibility and has no effect.
1465 * @deprecated This method is now obsolete.
1466 */
1467 @Deprecated
1468 public static void resetGlobalExternalFreedSize() {}
1469
1470 /**
1471 * Returns the thread-local count of objects allocated by the runtime between a
1472 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001473 *
1474 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001475 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001476 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 public static int getThreadAllocCount() {
1478 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1479 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001480
1481 /**
1482 * Clears the thread-local count of objects allocated.
1483 * @see #getThreadAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001484 *
1485 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001486 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001487 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001488 public static void resetThreadAllocCount() {
1489 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1490 }
1491
1492 /**
1493 * Returns the thread-local size of objects allocated by the runtime between a
1494 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
1495 * @return The allocated size in bytes.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001496 *
1497 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001498 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001499 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 public static int getThreadAllocSize() {
1501 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1502 }
Carl Shapirob5961982010-12-22 15:54:53 -08001503
1504 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001505 * Clears the thread-local count of objects allocated.
1506 * @see #getThreadAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001507 *
1508 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001509 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001510 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001511 public static void resetThreadAllocSize() {
1512 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1513 }
1514
1515 /**
1516 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001517 * @deprecated This method is now obsolete.
1518 */
1519 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 public static int getThreadExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001521 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
Carl Shapirob5961982010-12-22 15:54:53 -08001523
1524 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001525 * This method exists for compatibility and has no effect.
1526 * @deprecated This method is now obsolete.
1527 */
1528 @Deprecated
1529 public static void resetThreadExternalAllocCount() {}
1530
1531 /**
1532 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001533 * @deprecated This method is now obsolete.
1534 */
1535 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 public static int getThreadExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001537 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 }
Carl Shapirob5961982010-12-22 15:54:53 -08001539
Carl Shapirob5961982010-12-22 15:54:53 -08001540 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001541 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001542 * @deprecated This method is now obsolete.
1543 */
1544 @Deprecated
1545 public static void resetThreadExternalAllocSize() {}
1546
Ian Rogersfe067a42013-02-22 19:59:23 -08001547 /**
1548 * Returns the number of thread-local non-concurrent GC invocations between a
1549 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001550 *
1551 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001552 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001553 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001554 public static int getThreadGcInvocationCount() {
1555 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1556 }
1557
1558 /**
1559 * Clears the thread-local count of non-concurrent GC invocations.
1560 * @see #getThreadGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001561 *
1562 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001563 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001564 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 public static void resetThreadGcInvocationCount() {
1566 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1567 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001568
1569 /**
1570 * Clears all the global and thread-local memory allocation counters.
1571 * @see #startAllocCounting()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001572 *
1573 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001574 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001575 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 public static void resetAllCounts() {
1577 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
1578 }
1579
1580 /**
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001581 * Returns the value of a particular runtime statistic or {@code null} if no
1582 * such runtime statistic exists.
1583 *
1584 * <p>The following table lists the runtime statistics that the runtime supports.
1585 * Note runtime statistics may be added or removed in a future API level.</p>
1586 *
1587 * <table>
1588 * <thead>
1589 * <tr>
1590 * <th>Runtime statistic name</th>
1591 * <th>Meaning</th>
1592 * <th>Example</th>
1593 * <th>Supported (API Levels)</th>
1594 * </tr>
1595 * </thead>
1596 * <tbody>
1597 * <tr>
1598 * <td>art.gc.gc-count</td>
1599 * <td>The number of garbage collection runs.</td>
1600 * <td>{@code 164}</td>
1601 * <td>23</td>
1602 * </tr>
1603 * <tr>
1604 * <td>art.gc.gc-time</td>
1605 * <td>The total duration of garbage collection runs in ms.</td>
1606 * <td>{@code 62364}</td>
1607 * <td>23</td>
1608 * </tr>
1609 * <tr>
1610 * <td>art.gc.bytes-allocated</td>
1611 * <td>The total number of bytes that the application allocated.</td>
1612 * <td>{@code 1463948408}</td>
1613 * <td>23</td>
1614 * </tr>
1615 * <tr>
1616 * <td>art.gc.bytes-freed</td>
1617 * <td>The total number of bytes that garbage collection reclaimed.</td>
1618 * <td>{@code 1313493084}</td>
1619 * <td>23</td>
1620 * </tr>
1621 * <tr>
1622 * <td>art.gc.blocking-gc-count</td>
1623 * <td>The number of blocking garbage collection runs.</td>
1624 * <td>{@code 2}</td>
1625 * <td>23</td>
1626 * </tr>
1627 * <tr>
1628 * <td>art.gc.blocking-gc-time</td>
1629 * <td>The total duration of blocking garbage collection runs in ms.</td>
1630 * <td>{@code 804}</td>
1631 * <td>23</td>
1632 * </tr>
1633 * <tr>
1634 * <td>art.gc.gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001635 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage
1636 * collection runs that have occurred over the last 10
1637 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate
1638 * samples taken since the process began. The histogram can be used to identify
1639 * instances of high rates of garbage collection runs. For example, a histogram
1640 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time
1641 * there are between 0 and 2 garbage collection runs every 10 seconds, but there
1642 * were 8 distinct 10-second intervals in which 5 garbage collection runs
1643 * occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001644 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td>
1645 * <td>23</td>
1646 * </tr>
1647 * <tr>
1648 * <td>art.gc.blocking-gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001649 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of
1650 * blocking garbage collection runs that have occurred over the last 10
1651 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the
1652 * blocking-gc-count-rate samples taken since the process began. The histogram
1653 * can be used to identify instances of high rates of blocking garbage
1654 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that
1655 * most of the time there are zero blocking garbage collection runs every 10
1656 * seconds, but there was one 10-second interval in which one blocking garbage
1657 * collection run occurred, and there was one interval in which two blocking
1658 * garbage collection runs occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001659 * <td>{@code 0:99269,1:1,2:1}</td>
1660 * <td>23</td>
1661 * </tr>
1662 * </tbody>
1663 * </table>
1664 *
1665 * @param statName
1666 * the name of the runtime statistic to look up.
1667 * @return the value of the specified runtime statistic or {@code null} if the
1668 * runtime statistic doesn't exist.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001669 */
1670 public static String getRuntimeStat(String statName) {
1671 return VMDebug.getRuntimeStat(statName);
1672 }
1673
1674 /**
1675 * Returns a map of the names/values of the runtime statistics
Hiroshi Yamauchid8001672015-04-14 16:07:26 -07001676 * that {@link #getRuntimeStat(String)} supports.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001677 *
1678 * @return a map of the names/values of the supported runtime statistics.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001679 */
1680 public static Map<String, String> getRuntimeStats() {
1681 return VMDebug.getRuntimeStats();
1682 }
1683
1684 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 * Returns the size of the native heap.
1686 * @return The size of the native heap in bytes.
1687 */
1688 public static native long getNativeHeapSize();
1689
1690 /**
1691 * Returns the amount of allocated memory in the native heap.
1692 * @return The allocated size in bytes.
1693 */
1694 public static native long getNativeHeapAllocatedSize();
1695
1696 /**
1697 * Returns the amount of free memory in the native heap.
1698 * @return The freed size in bytes.
1699 */
1700 public static native long getNativeHeapFreeSize();
1701
1702 /**
1703 * Retrieves information about this processes memory usages. This information is broken down by
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001704 * how much is in use by dalvik, the native heap, and everything else.
1705 *
1706 * <p><b>Note:</b> this method directly retrieves memory information for the give process
1707 * from low-level data available to it. It may not be able to retrieve information about
1708 * some protected allocations, such as graphics. If you want to be sure you can see
1709 * all information about allocations by the process, use instead
1710 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 */
1712 public static native void getMemoryInfo(MemoryInfo memoryInfo);
1713
1714 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001715 * Note: currently only works when the requested pid has the same UID
1716 * as the caller.
1717 * @hide
1718 */
1719 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
1720
1721 /**
Dianne Hackbornb437e092011-08-05 17:50:29 -07001722 * Retrieves the PSS memory used by the process as given by the
1723 * smaps.
1724 */
1725 public static native long getPss();
1726
1727 /**
1728 * Retrieves the PSS memory used by the process as given by the
Martijn Coenene0764852016-01-07 17:04:22 -08001729 * smaps. Optionally supply a long array of 2 entries to also
1730 * receive the Uss and SwapPss of the process, and another array to also
1731 * retrieve the separate memtrack size.
1732 * @hide
Dianne Hackbornb437e092011-08-05 17:50:29 -07001733 */
Martijn Coenene0764852016-01-07 17:04:22 -08001734 public static native long getPss(int pid, long[] outUssSwapPss, long[] outMemtrack);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001735
Dianne Hackborn8e692572013-09-10 19:06:15 -07001736 /** @hide */
1737 public static final int MEMINFO_TOTAL = 0;
1738 /** @hide */
1739 public static final int MEMINFO_FREE = 1;
1740 /** @hide */
1741 public static final int MEMINFO_BUFFERS = 2;
1742 /** @hide */
1743 public static final int MEMINFO_CACHED = 3;
1744 /** @hide */
1745 public static final int MEMINFO_SHMEM = 4;
1746 /** @hide */
1747 public static final int MEMINFO_SLAB = 5;
1748 /** @hide */
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001749 public static final int MEMINFO_SWAP_TOTAL = 6;
1750 /** @hide */
1751 public static final int MEMINFO_SWAP_FREE = 7;
1752 /** @hide */
1753 public static final int MEMINFO_ZRAM_TOTAL = 8;
1754 /** @hide */
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001755 public static final int MEMINFO_MAPPED = 9;
1756 /** @hide */
1757 public static final int MEMINFO_VM_ALLOC_USED = 10;
1758 /** @hide */
1759 public static final int MEMINFO_PAGE_TABLES = 11;
1760 /** @hide */
1761 public static final int MEMINFO_KERNEL_STACK = 12;
1762 /** @hide */
1763 public static final int MEMINFO_COUNT = 13;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001764
1765 /**
1766 * Retrieves /proc/meminfo. outSizes is filled with fields
1767 * as defined by MEMINFO_* offsets.
1768 * @hide
1769 */
1770 public static native void getMemInfo(long[] outSizes);
1771
Dianne Hackbornb437e092011-08-05 17:50:29 -07001772 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001773 * Establish an object allocation limit in the current thread.
Carl Shapiro7e942842011-01-12 17:17:45 -08001774 * This feature was never enabled in release builds. The
1775 * allocation limits feature was removed in Honeycomb. This
1776 * method exists for compatibility and always returns -1 and has
1777 * no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 *
Carl Shapiro11073832011-01-12 16:28:57 -08001779 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 */
Carl Shapiro11073832011-01-12 16:28:57 -08001781 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 public static int setAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001783 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 }
1785
1786 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001787 * Establish a global object allocation limit. This feature was
Carl Shapiro7e942842011-01-12 17:17:45 -08001788 * never enabled in release builds. The allocation limits feature
1789 * was removed in Honeycomb. This method exists for compatibility
1790 * and always returns -1 and has no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 *
Carl Shapiro11073832011-01-12 16:28:57 -08001792 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 */
Carl Shapiro11073832011-01-12 16:28:57 -08001794 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 public static int setGlobalAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001796 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798
1799 /**
1800 * Dump a list of all currently loaded class to the log file.
1801 *
1802 * @param flags See constants above.
1803 */
1804 public static void printLoadedClasses(int flags) {
1805 VMDebug.printLoadedClasses(flags);
1806 }
1807
1808 /**
1809 * Get the number of loaded classes.
1810 * @return the number of loaded classes.
1811 */
1812 public static int getLoadedClassCount() {
1813 return VMDebug.getLoadedClassCount();
1814 }
1815
1816 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001817 * Dump "hprof" data to the specified file. This may cause a GC.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 *
1819 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
1820 * @throws UnsupportedOperationException if the VM was built without
1821 * HPROF support.
1822 * @throws IOException if an error occurs while opening or writing files.
1823 */
1824 public static void dumpHprofData(String fileName) throws IOException {
1825 VMDebug.dumpHprofData(fileName);
1826 }
1827
1828 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001829 * Like dumpHprofData(String), but takes an already-opened
1830 * FileDescriptor to which the trace is written. The file name is also
1831 * supplied simply for logging. Makes a dup of the file descriptor.
1832 *
1833 * Primarily for use by the "am" shell command.
1834 *
1835 * @hide
1836 */
1837 public static void dumpHprofData(String fileName, FileDescriptor fd)
1838 throws IOException {
1839 VMDebug.dumpHprofData(fileName, fd);
1840 }
1841
1842 /**
1843 * Collect "hprof" and send it to DDMS. This may cause a GC.
Andy McFadden07a96612010-01-28 16:54:37 -08001844 *
1845 * @throws UnsupportedOperationException if the VM was built without
1846 * HPROF support.
Andy McFadden07a96612010-01-28 16:54:37 -08001847 * @hide
1848 */
1849 public static void dumpHprofDataDdms() {
1850 VMDebug.dumpHprofDataDdms();
1851 }
1852
1853 /**
Andy McFadden06a6b552010-07-13 16:28:09 -07001854 * Writes native heap data to the specified file descriptor.
1855 *
1856 * @hide
1857 */
1858 public static native void dumpNativeHeap(FileDescriptor fd);
1859
1860 /**
Brian Carlstromc21550a2010-10-05 21:34:06 -07001861 * Returns a count of the extant instances of a class.
1862 *
1863 * @hide
1864 */
1865 public static long countInstancesOfClass(Class cls) {
Brian Carlstrom7495cfa2010-11-30 18:06:00 -08001866 return VMDebug.countInstancesOfClass(cls, true);
Brian Carlstromc21550a2010-10-05 21:34:06 -07001867 }
1868
1869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 * Returns the number of sent transactions from this process.
1871 * @return The number of sent transactions or -1 if it could not read t.
1872 */
1873 public static native int getBinderSentTransactions();
1874
1875 /**
1876 * Returns the number of received transactions from the binder driver.
1877 * @return The number of received transactions or -1 if it could not read the stats.
1878 */
1879 public static native int getBinderReceivedTransactions();
1880
1881 /**
1882 * Returns the number of active local Binder objects that exist in the
1883 * current process.
1884 */
1885 public static final native int getBinderLocalObjectCount();
1886
1887 /**
1888 * Returns the number of references to remote proxy Binder objects that
1889 * exist in the current process.
1890 */
1891 public static final native int getBinderProxyObjectCount();
1892
1893 /**
1894 * Returns the number of death notification links to Binder objects that
1895 * exist in the current process.
1896 */
1897 public static final native int getBinderDeathObjectCount();
1898
1899 /**
Andy McFadden599c9182009-04-08 00:35:56 -07001900 * Primes the register map cache.
1901 *
1902 * Only works for classes in the bootstrap class loader. Does not
1903 * cause classes to be loaded if they're not already present.
1904 *
1905 * The classAndMethodDesc argument is a concatentation of the VM-internal
1906 * class descriptor, method name, and method descriptor. Examples:
1907 * Landroid/os/Looper;.loop:()V
1908 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
1909 *
1910 * @param classAndMethodDesc the method to prepare
1911 *
1912 * @hide
1913 */
1914 public static final boolean cacheRegisterMap(String classAndMethodDesc) {
1915 return VMDebug.cacheRegisterMap(classAndMethodDesc);
1916 }
1917
1918 /**
Andy McFaddenbfd6d482009-10-22 17:25:57 -07001919 * Dumps the contents of VM reference tables (e.g. JNI locals and
1920 * globals) to the log file.
1921 *
1922 * @hide
1923 */
1924 public static final void dumpReferenceTables() {
1925 VMDebug.dumpReferenceTables();
1926 }
1927
1928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 * API for gathering and querying instruction counts.
1930 *
1931 * Example usage:
Chet Haase2970c492010-11-09 13:58:04 -08001932 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 * Debug.InstructionCount icount = new Debug.InstructionCount();
1934 * icount.resetAndStart();
1935 * [... do lots of stuff ...]
1936 * if (icount.collect()) {
1937 * System.out.println("Total instructions executed: "
1938 * + icount.globalTotal());
1939 * System.out.println("Method invocations: "
1940 * + icount.globalMethodInvocations());
1941 * }
Chet Haase2970c492010-11-09 13:58:04 -08001942 * </pre>
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001943 *
1944 * @deprecated Instruction counting is no longer supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 */
Jeff Hao7d0b3d42014-09-17 15:45:05 -07001946 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 public static class InstructionCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 public InstructionCount() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
1950
1951 /**
1952 * Reset counters and ensure counts are running. Counts may
1953 * have already been running.
1954 *
1955 * @return true if counting was started
1956 */
1957 public boolean resetAndStart() {
Narayan Kamath19541e82017-05-30 18:04:36 +01001958 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 }
1960
1961 /**
1962 * Collect instruction counts. May or may not stop the
1963 * counting process.
1964 */
1965 public boolean collect() {
Narayan Kamath19541e82017-05-30 18:04:36 +01001966 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968
1969 /**
1970 * Return the total number of instructions executed globally (i.e. in
1971 * all threads).
1972 */
1973 public int globalTotal() {
Narayan Kamath19541e82017-05-30 18:04:36 +01001974 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
1976
1977 /**
1978 * Return the total number of method-invocation instructions
1979 * executed globally.
1980 */
1981 public int globalMethodInvocations() {
Narayan Kamath19541e82017-05-30 18:04:36 +01001982 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07001984 }
1985
Dave Bort1ce5bd32009-04-22 17:36:56 -07001986 /**
1987 * A Map of typed debug properties.
1988 */
1989 private static final TypedProperties debugProperties;
1990
1991 /*
1992 * Load the debug properties from the standard files into debugProperties.
1993 */
1994 static {
Joe Onorato43a17652011-04-06 19:22:23 -07001995 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07001996 final String TAG = "DebugProperties";
1997 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
1998 final TypedProperties tp = new TypedProperties();
1999
2000 // Read the properties from each of the files, if present.
Dave Borte9bfd9b2009-05-04 14:35:23 -07002001 for (String file : files) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002002 Reader r;
2003 try {
2004 r = new FileReader(file);
2005 } catch (FileNotFoundException ex) {
2006 // It's ok if a file is missing.
2007 continue;
2008 }
2009
Dave Bort1ce5bd32009-04-22 17:36:56 -07002010 try {
2011 tp.load(r);
Dave Borte9bfd9b2009-05-04 14:35:23 -07002012 } catch (Exception ex) {
2013 throw new RuntimeException("Problem loading " + file, ex);
2014 } finally {
2015 try {
2016 r.close();
2017 } catch (IOException ex) {
2018 // Ignore this error.
2019 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002020 }
2021 }
2022
2023 debugProperties = tp.isEmpty() ? null : tp;
2024 } else {
2025 debugProperties = null;
2026 }
2027 }
2028
2029
2030 /**
2031 * Returns true if the type of the field matches the specified class.
2032 * Handles the case where the class is, e.g., java.lang.Boolean, but
2033 * the field is of the primitive "boolean" type. Also handles all of
2034 * the java.lang.Number subclasses.
2035 */
2036 private static boolean fieldTypeMatches(Field field, Class<?> cl) {
2037 Class<?> fieldClass = field.getType();
2038 if (fieldClass == cl) {
2039 return true;
2040 }
2041 Field primitiveTypeField;
2042 try {
2043 /* All of the classes we care about (Boolean, Integer, etc.)
2044 * have a Class field called "TYPE" that points to the corresponding
2045 * primitive class.
2046 */
2047 primitiveTypeField = cl.getField("TYPE");
2048 } catch (NoSuchFieldException ex) {
2049 return false;
2050 }
2051 try {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002052 return fieldClass == (Class<?>) primitiveTypeField.get(null);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002053 } catch (IllegalAccessException ex) {
2054 return false;
2055 }
2056 }
2057
2058
2059 /**
2060 * Looks up the property that corresponds to the field, and sets the field's value
2061 * if the types match.
2062 */
Dave Borte9bfd9b2009-05-04 14:35:23 -07002063 private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
2064 final String propertyName) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002065 if (field.getType() == java.lang.String.class) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002066 int stringInfo = properties.getStringInfo(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002067 switch (stringInfo) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002068 case TypedProperties.STRING_SET:
2069 // Handle as usual below.
2070 break;
2071 case TypedProperties.STRING_NULL:
2072 try {
2073 field.set(null, null); // null object for static fields; null string
2074 } catch (IllegalAccessException ex) {
2075 throw new IllegalArgumentException(
2076 "Cannot set field for " + propertyName, ex);
2077 }
2078 return;
2079 case TypedProperties.STRING_NOT_SET:
2080 return;
2081 case TypedProperties.STRING_TYPE_MISMATCH:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002082 throw new IllegalArgumentException(
Dave Borte9bfd9b2009-05-04 14:35:23 -07002083 "Type of " + propertyName + " " +
2084 " does not match field type (" + field.getType() + ")");
2085 default:
2086 throw new IllegalStateException(
2087 "Unexpected getStringInfo(" + propertyName + ") return value " +
2088 stringInfo);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002089 }
2090 }
Dave Borte9bfd9b2009-05-04 14:35:23 -07002091 Object value = properties.get(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002092 if (value != null) {
2093 if (!fieldTypeMatches(field, value.getClass())) {
2094 throw new IllegalArgumentException(
2095 "Type of " + propertyName + " (" + value.getClass() + ") " +
2096 " does not match field type (" + field.getType() + ")");
2097 }
2098 try {
2099 field.set(null, value); // null object for static fields
2100 } catch (IllegalAccessException ex) {
2101 throw new IllegalArgumentException(
2102 "Cannot set field for " + propertyName, ex);
2103 }
2104 }
2105 }
2106
2107
2108 /**
Romain Guyc4b11a72009-05-13 15:46:37 -07002109 * Equivalent to <code>setFieldsOn(cl, false)</code>.
2110 *
2111 * @see #setFieldsOn(Class, boolean)
Romain Guyd4103d02009-05-14 12:24:21 -07002112 *
2113 * @hide
Romain Guyc4b11a72009-05-13 15:46:37 -07002114 */
2115 public static void setFieldsOn(Class<?> cl) {
2116 setFieldsOn(cl, false);
2117 }
2118
2119 /**
Dave Bort1ce5bd32009-04-22 17:36:56 -07002120 * Reflectively sets static fields of a class based on internal debugging
Joe Onorato43a17652011-04-06 19:22:23 -07002121 * properties. This method is a no-op if false is
Dave Bort1ce5bd32009-04-22 17:36:56 -07002122 * false.
2123 * <p>
Joe Onorato43a17652011-04-06 19:22:23 -07002124 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will
Dave Bort1ce5bd32009-04-22 17:36:56 -07002125 * always be false in release builds. This API is typically only useful
2126 * for platform developers.
2127 * </p>
2128 * Class setup: define a class whose only fields are non-final, static
2129 * primitive types (except for "char") or Strings. In a static block
2130 * after the field definitions/initializations, pass the class to
Romain Guyc4b11a72009-05-13 15:46:37 -07002131 * this method, Debug.setFieldsOn(). Example:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002132 * <pre>
2133 * package com.example;
2134 *
2135 * import android.os.Debug;
2136 *
2137 * public class MyDebugVars {
2138 * public static String s = "a string";
2139 * public static String s2 = "second string";
2140 * public static String ns = null;
2141 * public static boolean b = false;
2142 * public static int i = 5;
Romain Guyc4b11a72009-05-13 15:46:37 -07002143 * @Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002144 * public static float f = 0.1f;
Romain Guyc4b11a72009-05-13 15:46:37 -07002145 * @@Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002146 * public static double d = 0.5d;
2147 *
2148 * // This MUST appear AFTER all fields are defined and initialized!
2149 * static {
Romain Guyc4b11a72009-05-13 15:46:37 -07002150 * // Sets all the fields
Dave Borte9bfd9b2009-05-04 14:35:23 -07002151 * Debug.setFieldsOn(MyDebugVars.class);
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02002152 *
Romain Guyc4b11a72009-05-13 15:46:37 -07002153 * // Sets only the fields annotated with @Debug.DebugProperty
2154 * // Debug.setFieldsOn(MyDebugVars.class, true);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002155 * }
2156 * }
2157 * </pre>
Dave Borte9bfd9b2009-05-04 14:35:23 -07002158 * setFieldsOn() may override the value of any field in the class based
Dave Bort1ce5bd32009-04-22 17:36:56 -07002159 * on internal properties that are fixed at boot time.
2160 * <p>
2161 * These properties are only set during platform debugging, and are not
2162 * meant to be used as a general-purpose properties store.
2163 *
2164 * {@hide}
2165 *
2166 * @param cl The class to (possibly) modify
Romain Guyc4b11a72009-05-13 15:46:37 -07002167 * @param partial If false, sets all static fields, otherwise, only set
2168 * fields with the {@link android.os.Debug.DebugProperty}
2169 * annotation
Dave Bort1ce5bd32009-04-22 17:36:56 -07002170 * @throws IllegalArgumentException if any fields are final or non-static,
2171 * or if the type of the field does not match the type of
2172 * the internal debugging property value.
2173 */
Romain Guyc4b11a72009-05-13 15:46:37 -07002174 public static void setFieldsOn(Class<?> cl, boolean partial) {
Joe Onorato43a17652011-04-06 19:22:23 -07002175 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002176 if (debugProperties != null) {
2177 /* Only look for fields declared directly by the class,
2178 * so we don't mysteriously change static fields in superclasses.
2179 */
2180 for (Field field : cl.getDeclaredFields()) {
Romain Guyc4b11a72009-05-13 15:46:37 -07002181 if (!partial || field.getAnnotation(DebugProperty.class) != null) {
2182 final String propertyName = cl.getName() + "." + field.getName();
2183 boolean isStatic = Modifier.isStatic(field.getModifiers());
2184 boolean isFinal = Modifier.isFinal(field.getModifiers());
2185
2186 if (!isStatic || isFinal) {
2187 throw new IllegalArgumentException(propertyName +
2188 " must be static and non-final");
2189 }
2190 modifyFieldIfSet(field, debugProperties, propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002191 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002192 }
2193 }
2194 } else {
Dan Egnor3eda9792010-03-05 13:28:36 -08002195 Log.wtf(TAG,
Dave Borte9bfd9b2009-05-04 14:35:23 -07002196 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
Dave Bort1ce5bd32009-04-22 17:36:56 -07002197 ") called in non-DEBUG build");
2198 }
2199 }
Romain Guyc4b11a72009-05-13 15:46:37 -07002200
2201 /**
2202 * Annotation to put on fields you want to set with
2203 * {@link Debug#setFieldsOn(Class, boolean)}.
2204 *
2205 * @hide
2206 */
2207 @Target({ ElementType.FIELD })
2208 @Retention(RetentionPolicy.RUNTIME)
2209 public @interface DebugProperty {
2210 }
Dan Egnor3eda9792010-03-05 13:28:36 -08002211
2212 /**
2213 * Get a debugging dump of a system service by name.
2214 *
2215 * <p>Most services require the caller to hold android.permission.DUMP.
2216 *
2217 * @param name of the service to dump
2218 * @param fd to write dump output to (usually an output log file)
2219 * @param args to pass to the service's dump method, may be null
2220 * @return true if the service was dumped successfully, false if
2221 * the service could not be found or had an error while dumping
2222 */
2223 public static boolean dumpService(String name, FileDescriptor fd, String[] args) {
2224 IBinder service = ServiceManager.getService(name);
2225 if (service == null) {
2226 Log.e(TAG, "Can't find service to dump: " + name);
2227 return false;
2228 }
2229
2230 try {
2231 service.dump(fd, args);
2232 return true;
2233 } catch (RemoteException e) {
2234 Log.e(TAG, "Can't dump service: " + name, e);
2235 return false;
2236 }
2237 }
Craig Mautnera51a9562012-04-17 17:05:26 -07002238
2239 /**
Narayan Kamathf013daa2017-05-09 12:55:02 +01002240 * Append the Java stack traces of a given native process to a specified file.
2241 *
songjinshie02e3ea2016-12-16 17:48:21 +08002242 * @param pid pid to dump.
2243 * @param file path of file to append dump to.
2244 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002245 * @hide
2246 */
Narayan Kamathf013daa2017-05-09 12:55:02 +01002247 public static native boolean dumpJavaBacktraceToFileTimeout(int pid, String file,
2248 int timeoutSecs);
2249
2250 /**
2251 * Append the native stack traces of a given process to a specified file.
2252 *
2253 * @param pid pid to dump.
2254 * @param file path of file to append dump to.
2255 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
2256 * @hide
2257 */
2258 public static native boolean dumpNativeBacktraceToFileTimeout(int pid, String file,
2259 int timeoutSecs);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002260
2261 /**
Colin Crossc4fb5f92016-02-02 16:51:15 -08002262 * Get description of unreachable native memory.
2263 * @param limit the number of leaks to provide info on, 0 to only get a summary.
2264 * @param contents true to include a hex dump of the contents of unreachable memory.
2265 * @return the String containing a description of unreachable memory.
2266 * @hide */
2267 public static native String getUnreachableMemory(int limit, boolean contents);
2268
2269 /**
Craig Mautnera51a9562012-04-17 17:05:26 -07002270 * Return a String describing the calling method and location at a particular stack depth.
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07002271 * @param callStack the Thread stack
Craig Mautnera51a9562012-04-17 17:05:26 -07002272 * @param depth the depth of stack to return information for.
2273 * @return the String describing the caller at that depth.
2274 */
2275 private static String getCaller(StackTraceElement callStack[], int depth) {
2276 // callStack[4] is the caller of the method that called getCallers()
2277 if (4 + depth >= callStack.length) {
2278 return "<bottom of call stack>";
2279 }
2280 StackTraceElement caller = callStack[4 + depth];
2281 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2282 }
2283
2284 /**
2285 * Return a string consisting of methods and locations at multiple call stack levels.
2286 * @param depth the number of levels to return, starting with the immediate caller.
2287 * @return a string describing the call stack.
2288 * {@hide}
2289 */
2290 public static String getCallers(final int depth) {
2291 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2292 StringBuffer sb = new StringBuffer();
2293 for (int i = 0; i < depth; i++) {
2294 sb.append(getCaller(callStack, i)).append(" ");
2295 }
2296 return sb.toString();
2297 }
2298
2299 /**
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07002300 * Return a string consisting of methods and locations at multiple call stack levels.
2301 * @param depth the number of levels to return, starting with the immediate caller.
2302 * @return a string describing the call stack.
2303 * {@hide}
2304 */
2305 public static String getCallers(final int start, int depth) {
2306 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2307 StringBuffer sb = new StringBuffer();
2308 depth += start;
2309 for (int i = start; i < depth; i++) {
2310 sb.append(getCaller(callStack, i)).append(" ");
2311 }
2312 return sb.toString();
2313 }
2314
2315 /**
Dianne Hackbornef03a7f2012-10-29 18:46:52 -07002316 * Like {@link #getCallers(int)}, but each location is append to the string
2317 * as a new line with <var>linePrefix</var> in front of it.
2318 * @param depth the number of levels to return, starting with the immediate caller.
2319 * @param linePrefix prefix to put in front of each location.
2320 * @return a string describing the call stack.
2321 * {@hide}
2322 */
2323 public static String getCallers(final int depth, String linePrefix) {
2324 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2325 StringBuffer sb = new StringBuffer();
2326 for (int i = 0; i < depth; i++) {
2327 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n");
2328 }
2329 return sb.toString();
2330 }
2331
2332 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08002333 * @return a String describing the immediate caller of the calling method.
Craig Mautnera51a9562012-04-17 17:05:26 -07002334 * {@hide}
2335 */
2336 public static String getCaller() {
2337 return getCaller(Thread.currentThread().getStackTrace(), 0);
2338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339}