blob: a395ed4dbb4313c275c06ad5f1b3957fa9f40393 [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
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060021import android.app.AppGlobals;
22import android.content.Context;
Dave Bort1ce5bd32009-04-22 17:36:56 -070023import android.util.Log;
24
Narayan Kamathf013daa2017-05-09 12:55:02 +010025import com.android.internal.util.FastPrintWriter;
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -070026import com.android.internal.util.Preconditions;
Narayan Kamathf013daa2017-05-09 12:55:02 +010027import com.android.internal.util.TypedProperties;
28
Narayan Kamathf013daa2017-05-09 12:55:02 +010029import dalvik.system.VMDebug;
30
31import org.apache.harmony.dalvik.ddmc.Chunk;
32import org.apache.harmony.dalvik.ddmc.ChunkHandler;
33import org.apache.harmony.dalvik.ddmc.DdmServer;
34
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060035import java.io.File;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070036import java.io.FileDescriptor;
Dave Bort1ce5bd32009-04-22 17:36:56 -070037import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import java.io.FileOutputStream;
Dave Bort1ce5bd32009-04-22 17:36:56 -070039import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.PrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070042import java.io.Reader;
Romain Guyc4b11a72009-05-13 15:46:37 -070043import java.lang.annotation.ElementType;
44import java.lang.annotation.Retention;
45import java.lang.annotation.RetentionPolicy;
Narayan Kamathf013daa2017-05-09 12:55:02 +010046import java.lang.annotation.Target;
47import java.lang.reflect.Field;
48import java.lang.reflect.Modifier;
Richard Uhler350e6dc2015-05-18 10:48:52 -070049import java.util.HashMap;
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -070050import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
Ian Rogersfe067a42013-02-22 19:59:23 -080054 * Provides various debugging methods for Android applications, including
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 * tracing and allocation counts.
56 * <p><strong>Logging Trace Files</strong></p>
57 * <p>Debug can create log files that give details about an application, such as
58 * a call stack and start/stop times for any running methods. See <a
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -080059 * href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs with
60 * Traceview</a> for information about reading trace files. To start logging
61 * trace files, call one of the startMethodTracing() methods. To stop tracing,
62 * call {@link #stopMethodTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 */
64public final class Debug
65{
Dan Egnor3eda9792010-03-05 13:28:36 -080066 private static final String TAG = "Debug";
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
69 * Flags for startMethodTracing(). These can be ORed together.
70 *
71 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
72 * trace key file.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080073 *
74 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080076 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS;
78
79 /**
80 * Flags for printLoadedClasses(). Default behavior is to only show
81 * the class name.
82 */
83 public static final int SHOW_FULL_DETAIL = 1;
84 public static final int SHOW_CLASSLOADER = (1 << 1);
85 public static final int SHOW_INITIALIZED = (1 << 2);
86
87 // set/cleared by waitForDebugger()
88 private static volatile boolean mWaiting = false;
89
90 private Debug() {}
91
92 /*
93 * How long to wait for the debugger to finish sending requests. I've
94 * seen this hit 800msec on the device while waiting for a response
95 * to travel over USB and get processed, so we take that and add
96 * half a second.
97 */
98 private static final int MIN_DEBUGGER_IDLE = 1300; // msec
99
100 /* how long to sleep when polling for activity */
101 private static final int SPIN_DELAY = 200; // msec
102
103 /**
104 * Default trace file path and file
105 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 private static final String DEFAULT_TRACE_BODY = "dmtrace";
107 private static final String DEFAULT_TRACE_EXTENSION = ".trace";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 /**
110 * This class is used to retrieved various statistics about the memory mappings for this
justinmuller64551382013-10-16 22:06:55 -0600111 * 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 -0800112 */
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700113 public static class MemoryInfo implements Parcelable {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700114 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public int dalvikPss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700116 /** The proportional set size that is swappable for dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700117 /** @hide We may want to expose this, eventually. */
118 public int dalvikSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800119 /** @hide The resident set size for dalvik heap. (Without other Dalvik overhead.) */
120 public int dalvikRss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700121 /** The private dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 public int dalvikPrivateDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700123 /** The shared dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public int dalvikSharedDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700125 /** The private clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700126 /** @hide We may want to expose this, eventually. */
127 public int dalvikPrivateClean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700128 /** The shared clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700129 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700130 public int dalvikSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700131 /** The dirty dalvik pages that have been swapped out. */
132 /** @hide We may want to expose this, eventually. */
133 public int dalvikSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800134 /** The dirty dalvik pages that have been swapped out, proportional. */
135 /** @hide We may want to expose this, eventually. */
136 public int dalvikSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
138 /** The proportional set size for the native heap. */
139 public int nativePss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700140 /** The proportional set size that is swappable for the native heap. */
141 /** @hide We may want to expose this, eventually. */
142 public int nativeSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800143 /** @hide The resident set size for the native heap. */
144 public int nativeRss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 /** The private dirty pages used by the native heap. */
146 public int nativePrivateDirty;
147 /** The shared dirty pages used by the native heap. */
148 public int nativeSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700149 /** The private clean pages used by the native heap. */
150 /** @hide We may want to expose this, eventually. */
151 public int nativePrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700152 /** The shared clean pages used by the native heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700153 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700154 public int nativeSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700155 /** The dirty native pages that have been swapped out. */
156 /** @hide We may want to expose this, eventually. */
157 public int nativeSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800158 /** The dirty native pages that have been swapped out, proportional. */
159 /** @hide We may want to expose this, eventually. */
160 public int nativeSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
162 /** The proportional set size for everything else. */
163 public int otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700164 /** The proportional set size that is swappable for everything else. */
165 /** @hide We may want to expose this, eventually. */
166 public int otherSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800167 /** @hide The resident set size for everything else. */
168 public int otherRss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /** The private dirty pages used by everything else. */
170 public int otherPrivateDirty;
171 /** The shared dirty pages used by everything else. */
172 public int otherSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700173 /** The private clean pages used by everything else. */
174 /** @hide We may want to expose this, eventually. */
175 public int otherPrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700176 /** The shared clean pages used by everything else. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700177 /** @hide We may want to expose this, eventually. */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700178 public int otherSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700179 /** The dirty pages used by anyting else that have been swapped out. */
180 /** @hide We may want to expose this, eventually. */
181 public int otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800182 /** The dirty pages used by anyting else that have been swapped out, proportional. */
183 /** @hide We may want to expose this, eventually. */
184 public int otherSwappedOutPss;
185
186 /** Whether the kernel reports proportional swap usage */
187 /** @hide */
188 public boolean hasSwappedOutPss;
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200189
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700190 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700191 public static final int HEAP_UNKNOWN = 0;
192 /** @hide */
193 public static final int HEAP_DALVIK = 1;
194 /** @hide */
195 public static final int HEAP_NATIVE = 2;
196
197 /** @hide */
198 public static final int OTHER_DALVIK_OTHER = 0;
199 /** @hide */
200 public static final int OTHER_STACK = 1;
201 /** @hide */
202 public static final int OTHER_CURSOR = 2;
203 /** @hide */
204 public static final int OTHER_ASHMEM = 3;
205 /** @hide */
206 public static final int OTHER_GL_DEV = 4;
207 /** @hide */
208 public static final int OTHER_UNKNOWN_DEV = 5;
209 /** @hide */
210 public static final int OTHER_SO = 6;
211 /** @hide */
212 public static final int OTHER_JAR = 7;
213 /** @hide */
214 public static final int OTHER_APK = 8;
215 /** @hide */
216 public static final int OTHER_TTF = 9;
217 /** @hide */
218 public static final int OTHER_DEX = 10;
219 /** @hide */
220 public static final int OTHER_OAT = 11;
221 /** @hide */
222 public static final int OTHER_ART = 12;
223 /** @hide */
224 public static final int OTHER_UNKNOWN_MAP = 13;
225 /** @hide */
226 public static final int OTHER_GRAPHICS = 14;
227 /** @hide */
228 public static final int OTHER_GL = 15;
229 /** @hide */
230 public static final int OTHER_OTHER_MEMTRACK = 16;
231
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700232 // Needs to be declared here for the DVK_STAT ranges below.
233 /** @hide */
234 public static final int NUM_OTHER_STATS = 17;
235
236 // Dalvik subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700237 /** @hide */
238 public static final int OTHER_DALVIK_NORMAL = 17;
239 /** @hide */
240 public static final int OTHER_DALVIK_LARGE = 18;
241 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700242 public static final int OTHER_DALVIK_ZYGOTE = 19;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700243 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700244 public static final int OTHER_DALVIK_NON_MOVING = 20;
245 // Section begins and ends for dumpsys, relative to the DALVIK categories.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700246 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700247 public static final int OTHER_DVK_STAT_DALVIK_START =
248 OTHER_DALVIK_NORMAL - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700249 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700250 public static final int OTHER_DVK_STAT_DALVIK_END =
251 OTHER_DALVIK_NON_MOVING - NUM_OTHER_STATS;
252
253 // Dalvik Other subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700254 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700255 public static final int OTHER_DALVIK_OTHER_LINEARALLOC = 21;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700256 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700257 public static final int OTHER_DALVIK_OTHER_ACCOUNTING = 22;
258 /** @hide */
259 public static final int OTHER_DALVIK_OTHER_CODE_CACHE = 23;
260 /** @hide */
261 public static final int OTHER_DALVIK_OTHER_COMPILER_METADATA = 24;
262 /** @hide */
263 public static final int OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE = 25;
264 /** @hide */
265 public static final int OTHER_DVK_STAT_DALVIK_OTHER_START =
266 OTHER_DALVIK_OTHER_LINEARALLOC - NUM_OTHER_STATS;
267 /** @hide */
268 public static final int OTHER_DVK_STAT_DALVIK_OTHER_END =
269 OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE - NUM_OTHER_STATS;
270
271 // Dex subsections (Boot vdex, App dex, and App vdex).
272 /** @hide */
273 public static final int OTHER_DEX_BOOT_VDEX = 26;
274 /** @hide */
275 public static final int OTHER_DEX_APP_DEX = 27;
276 /** @hide */
277 public static final int OTHER_DEX_APP_VDEX = 28;
278 /** @hide */
279 public static final int OTHER_DVK_STAT_DEX_START = OTHER_DEX_BOOT_VDEX - NUM_OTHER_STATS;
280 /** @hide */
281 public static final int OTHER_DVK_STAT_DEX_END = OTHER_DEX_APP_VDEX - NUM_OTHER_STATS;
282
283 // Art subsections (App image, boot image).
284 /** @hide */
285 public static final int OTHER_ART_APP = 29;
286 /** @hide */
287 public static final int OTHER_ART_BOOT = 30;
288 /** @hide */
289 public static final int OTHER_DVK_STAT_ART_START = OTHER_ART_APP - NUM_OTHER_STATS;
290 /** @hide */
291 public static final int OTHER_DVK_STAT_ART_END = OTHER_ART_BOOT - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700292
293 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700294 public static final int NUM_DVK_STATS = 14;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700295
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700296 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800297 public static final int NUM_CATEGORIES = 9;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700298
299 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800300 public static final int OFFSET_PSS = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700301 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800302 public static final int OFFSET_SWAPPABLE_PSS = 1;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700303 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800304 public static final int OFFSET_RSS = 2;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700305 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800306 public static final int OFFSET_PRIVATE_DIRTY = 3;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700307 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800308 public static final int OFFSET_SHARED_DIRTY = 4;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700309 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800310 public static final int OFFSET_PRIVATE_CLEAN = 5;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700311 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800312 public static final int OFFSET_SHARED_CLEAN = 6;
Martijn Coenene0764852016-01-07 17:04:22 -0800313 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800314 public static final int OFFSET_SWAPPED_OUT = 7;
315 /** @hide */
316 public static final int OFFSET_SWAPPED_OUT_PSS = 8;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700317
318 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700319
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700320 public MemoryInfo() {
321 }
322
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700323 /**
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700324 * @hide Copy contents from another object.
325 */
326 public void set(MemoryInfo other) {
327 dalvikPss = other.dalvikPss;
328 dalvikSwappablePss = other.dalvikSwappablePss;
329 dalvikRss = other.dalvikRss;
330 dalvikPrivateDirty = other.dalvikPrivateDirty;
331 dalvikSharedDirty = other.dalvikSharedDirty;
332 dalvikPrivateClean = other.dalvikPrivateClean;
333 dalvikSharedClean = other.dalvikSharedClean;
334 dalvikSwappedOut = other.dalvikSwappedOut;
335 dalvikSwappedOutPss = other.dalvikSwappedOutPss;
336
337 nativePss = other.nativePss;
338 nativeSwappablePss = other.nativeSwappablePss;
339 nativeRss = other.nativeRss;
340 nativePrivateDirty = other.nativePrivateDirty;
341 nativeSharedDirty = other.nativeSharedDirty;
342 nativePrivateClean = other.nativePrivateClean;
343 nativeSharedClean = other.nativeSharedClean;
344 nativeSwappedOut = other.nativeSwappedOut;
345 nativeSwappedOutPss = other.nativeSwappedOutPss;
346
347 otherPss = other.otherPss;
348 otherSwappablePss = other.otherSwappablePss;
349 otherRss = other.otherRss;
350 otherPrivateDirty = other.otherPrivateDirty;
351 otherSharedDirty = other.otherSharedDirty;
352 otherPrivateClean = other.otherPrivateClean;
353 otherSharedClean = other.otherSharedClean;
354 otherSwappedOut = other.otherSwappedOut;
355 otherSwappedOutPss = other.otherSwappedOutPss;
356
357 hasSwappedOutPss = other.hasSwappedOutPss;
358
359 System.arraycopy(other.otherStats, 0, otherStats, 0, otherStats.length);
360 }
361
362 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700363 * Return total PSS memory usage in kB.
364 */
365 public int getTotalPss() {
Martijn Coenene0764852016-01-07 17:04:22 -0800366 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700367 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200368
Dianne Hackbornc8230512013-07-13 21:32:12 -0700369 /**
370 * @hide Return total PSS memory usage in kB.
371 */
372 public int getTotalUss() {
373 return dalvikPrivateClean + dalvikPrivateDirty
374 + nativePrivateClean + nativePrivateDirty
375 + otherPrivateClean + otherPrivateDirty;
376 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700377
378 /**
Martijn Coenene0764852016-01-07 17:04:22 -0800379 * Return total PSS memory usage in kB mapping a file of one of the following extension:
380 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art .
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700381 */
382 public int getTotalSwappablePss() {
383 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss;
384 }
385
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700386 /**
Dianne Hackborne17b4452018-01-10 13:15:40 -0800387 * @hide Return total RSS memory usage in kB.
388 */
389 public int getTotalRss() {
390 return dalvikRss + nativeRss + otherRss;
391 }
392
393 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700394 * Return total private dirty memory usage in kB.
395 */
396 public int getTotalPrivateDirty() {
397 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty;
398 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200399
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700400 /**
401 * Return total shared dirty memory usage in kB.
402 */
403 public int getTotalSharedDirty() {
404 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty;
405 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200406
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700407 /**
408 * Return total shared clean memory usage in kB.
409 */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700410 public int getTotalPrivateClean() {
411 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean;
412 }
413
414 /**
415 * Return total shared clean memory usage in kB.
416 */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700417 public int getTotalSharedClean() {
418 return dalvikSharedClean + nativeSharedClean + otherSharedClean;
419 }
420
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700421 /**
422 * Return total swapped out memory in kB.
423 * @hide
424 */
425 public int getTotalSwappedOut() {
426 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut;
427 }
428
Martijn Coenene0764852016-01-07 17:04:22 -0800429 /**
430 * Return total swapped out memory in kB, proportional.
431 * @hide
432 */
433 public int getTotalSwappedOutPss() {
434 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss;
435 }
436
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700437 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700438 public int getOtherPss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800439 return otherStats[which * NUM_CATEGORIES + OFFSET_PSS];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700440 }
441
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700442 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700443 public int getOtherSwappablePss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800444 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPABLE_PSS];
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700445 }
446
Dianne Hackborne17b4452018-01-10 13:15:40 -0800447 /** @hide */
448 public int getOtherRss(int which) {
449 return otherStats[which * NUM_CATEGORIES + OFFSET_RSS];
450 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700451
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700452 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700453 public int getOtherPrivateDirty(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800454 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_DIRTY];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700455 }
456
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700457 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700458 public int getOtherSharedDirty(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800459 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_DIRTY];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700460 }
461
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700462 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700463 public int getOtherPrivateClean(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800464 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_CLEAN];
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700465 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700466
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700467 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700468 public int getOtherPrivate(int which) {
469 return getOtherPrivateClean(which) + getOtherPrivateDirty(which);
470 }
471
472 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700473 public int getOtherSharedClean(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800474 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_CLEAN];
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700475 }
476
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700477 /** @hide */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700478 public int getOtherSwappedOut(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800479 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT];
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700480 }
481
482 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800483 public int getOtherSwappedOutPss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800484 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT_PSS];
Martijn Coenene0764852016-01-07 17:04:22 -0800485 }
486
487 /** @hide */
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700488 public static String getOtherLabel(int which) {
489 switch (which) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700490 case OTHER_DALVIK_OTHER: return "Dalvik Other";
491 case OTHER_STACK: return "Stack";
492 case OTHER_CURSOR: return "Cursor";
493 case OTHER_ASHMEM: return "Ashmem";
494 case OTHER_GL_DEV: return "Gfx dev";
495 case OTHER_UNKNOWN_DEV: return "Other dev";
496 case OTHER_SO: return ".so mmap";
497 case OTHER_JAR: return ".jar mmap";
498 case OTHER_APK: return ".apk mmap";
499 case OTHER_TTF: return ".ttf mmap";
500 case OTHER_DEX: return ".dex mmap";
501 case OTHER_OAT: return ".oat mmap";
502 case OTHER_ART: return ".art mmap";
503 case OTHER_UNKNOWN_MAP: return "Other mmap";
504 case OTHER_GRAPHICS: return "EGL mtrack";
505 case OTHER_GL: return "GL mtrack";
506 case OTHER_OTHER_MEMTRACK: return "Other mtrack";
507 case OTHER_DALVIK_NORMAL: return ".Heap";
508 case OTHER_DALVIK_LARGE: return ".LOS";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700509 case OTHER_DALVIK_ZYGOTE: return ".Zygote";
510 case OTHER_DALVIK_NON_MOVING: return ".NonMoving";
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700511 case OTHER_DALVIK_OTHER_LINEARALLOC: return ".LinearAlloc";
512 case OTHER_DALVIK_OTHER_ACCOUNTING: return ".GC";
513 case OTHER_DALVIK_OTHER_CODE_CACHE: return ".JITCache";
514 case OTHER_DALVIK_OTHER_COMPILER_METADATA: return ".CompilerMetadata";
515 case OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE: return ".IndirectRef";
516 case OTHER_DEX_BOOT_VDEX: return ".Boot vdex";
517 case OTHER_DEX_APP_DEX: return ".App dex";
518 case OTHER_DEX_APP_VDEX: return ".App vdex";
519 case OTHER_ART_APP: return ".App art";
520 case OTHER_ART_BOOT: return ".Boot art";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700521 default: return "????";
522 }
523 }
524
Richard Uhler350e6dc2015-05-18 10:48:52 -0700525 /**
526 * Returns the value of a particular memory statistic or {@code null} if no
527 * such memory statistic exists.
528 *
529 * <p>The following table lists the memory statistics that are supported.
530 * Note that memory statistics may be added or removed in a future API level.</p>
531 *
532 * <table>
533 * <thead>
534 * <tr>
535 * <th>Memory statistic name</th>
536 * <th>Meaning</th>
537 * <th>Example</th>
538 * <th>Supported (API Levels)</th>
539 * </tr>
540 * </thead>
541 * <tbody>
542 * <tr>
543 * <td>summary.java-heap</td>
544 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field
545 * in the App Summary section output by dumpsys meminfo.</td>
546 * <td>{@code 1442}</td>
547 * <td>23</td>
548 * </tr>
549 * <tr>
550 * <td>summary.native-heap</td>
551 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap
552 * field in the App Summary section output by dumpsys meminfo.</td>
553 * <td>{@code 1442}</td>
554 * <td>23</td>
555 * </tr>
556 * <tr>
557 * <td>summary.code</td>
558 * <td>The memory usage for static code and resources in kB. This corresponds to
559 * the Code field in the App Summary section output by dumpsys meminfo.</td>
560 * <td>{@code 1442}</td>
561 * <td>23</td>
562 * </tr>
563 * <tr>
564 * <td>summary.stack</td>
565 * <td>The stack usage in kB. This corresponds to the Stack field in the
566 * App Summary section output by dumpsys meminfo.</td>
567 * <td>{@code 1442}</td>
568 * <td>23</td>
569 * </tr>
570 * <tr>
571 * <td>summary.graphics</td>
572 * <td>The graphics usage in kB. This corresponds to the Graphics field in the
573 * App Summary section output by dumpsys meminfo.</td>
574 * <td>{@code 1442}</td>
575 * <td>23</td>
576 * </tr>
577 * <tr>
578 * <td>summary.private-other</td>
579 * <td>Other private memory usage in kB. This corresponds to the Private Other
580 * field output in the App Summary section by dumpsys meminfo.</td>
581 * <td>{@code 1442}</td>
582 * <td>23</td>
583 * </tr>
584 * <tr>
585 * <td>summary.system</td>
586 * <td>Shared and system memory usage in kB. This corresponds to the System
587 * field output in the App Summary section by dumpsys meminfo.</td>
588 * <td>{@code 1442}</td>
589 * <td>23</td>
590 * </tr>
591 * <tr>
592 * <td>summary.total-pss</td>
593 * <td>Total PPS memory usage in kB.</td>
594 * <td>{@code 1442}</td>
595 * <td>23</td>
596 * </tr>
597 * <tr>
598 * <td>summary.total-swap</td>
599 * <td>Total swap usage in kB.</td>
600 * <td>{@code 1442}</td>
601 * <td>23</td>
602 * </tr>
603 * </tbody>
604 * </table>
605 */
Dianne Hackbornb02ce292015-10-12 15:14:16 -0700606 public String getMemoryStat(String statName) {
Richard Uhler350e6dc2015-05-18 10:48:52 -0700607 switch(statName) {
608 case "summary.java-heap":
609 return Integer.toString(getSummaryJavaHeap());
610 case "summary.native-heap":
611 return Integer.toString(getSummaryNativeHeap());
612 case "summary.code":
613 return Integer.toString(getSummaryCode());
614 case "summary.stack":
615 return Integer.toString(getSummaryStack());
616 case "summary.graphics":
617 return Integer.toString(getSummaryGraphics());
618 case "summary.private-other":
619 return Integer.toString(getSummaryPrivateOther());
620 case "summary.system":
621 return Integer.toString(getSummarySystem());
622 case "summary.total-pss":
623 return Integer.toString(getSummaryTotalPss());
624 case "summary.total-swap":
625 return Integer.toString(getSummaryTotalSwap());
626 default:
627 return null;
628 }
629 }
630
631 /**
632 * Returns a map of the names/values of the memory statistics
633 * that {@link #getMemoryStat(String)} supports.
634 *
635 * @return a map of the names/values of the supported memory statistics.
636 */
637 public Map<String, String> getMemoryStats() {
638 Map<String, String> stats = new HashMap<String, String>();
639 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap()));
640 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap()));
641 stats.put("summary.code", Integer.toString(getSummaryCode()));
642 stats.put("summary.stack", Integer.toString(getSummaryStack()));
643 stats.put("summary.graphics", Integer.toString(getSummaryGraphics()));
644 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther()));
645 stats.put("summary.system", Integer.toString(getSummarySystem()));
646 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss()));
647 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap()));
648 return stats;
649 }
650
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700651 /**
652 * Pss of Java Heap bytes in KB due to the application.
653 * Notes:
654 * * OTHER_ART is the boot image. Anything private here is blamed on
655 * the application, not the system.
656 * * dalvikPrivateDirty includes private zygote, which means the
657 * application dirtied something allocated by the zygote. We blame
658 * the application for that memory, not the system.
659 * * Does not include OTHER_DALVIK_OTHER, which is considered VM
660 * Overhead and lumped into Private Other.
661 * * We don't include dalvikPrivateClean, because there should be no
662 * such thing as private clean for the Java Heap.
663 * @hide
664 */
665 public int getSummaryJavaHeap() {
666 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART);
667 }
668
669 /**
670 * Pss of Native Heap bytes in KB due to the application.
671 * Notes:
672 * * Includes private dirty malloc space.
673 * * We don't include nativePrivateClean, because there should be no
674 * such thing as private clean for the Native Heap.
675 * @hide
676 */
677 public int getSummaryNativeHeap() {
678 return nativePrivateDirty;
679 }
680
681 /**
682 * Pss of code and other static resource bytes in KB due to
683 * the application.
684 * @hide
685 */
686 public int getSummaryCode() {
687 return getOtherPrivate(OTHER_SO)
688 + getOtherPrivate(OTHER_JAR)
689 + getOtherPrivate(OTHER_APK)
690 + getOtherPrivate(OTHER_TTF)
691 + getOtherPrivate(OTHER_DEX)
692 + getOtherPrivate(OTHER_OAT);
693 }
694
695 /**
696 * Pss in KB of the stack due to the application.
697 * Notes:
698 * * Includes private dirty stack, which includes both Java and Native
699 * stack.
700 * * Does not include private clean stack, because there should be no
701 * such thing as private clean for the stack.
702 * @hide
703 */
704 public int getSummaryStack() {
705 return getOtherPrivateDirty(OTHER_STACK);
706 }
707
708 /**
709 * Pss in KB of graphics due to the application.
710 * Notes:
711 * * Includes private Gfx, EGL, and GL.
712 * * Warning: These numbers can be misreported by the graphics drivers.
713 * * We don't include shared graphics. It may make sense to, because
714 * shared graphics are likely buffers due to the application
715 * anyway, but it's simpler to implement to just group all shared
716 * memory into the System category.
717 * @hide
718 */
719 public int getSummaryGraphics() {
720 return getOtherPrivate(OTHER_GL_DEV)
721 + getOtherPrivate(OTHER_GRAPHICS)
722 + getOtherPrivate(OTHER_GL);
723 }
724
725 /**
726 * Pss in KB due to the application that haven't otherwise been
727 * accounted for.
728 * @hide
729 */
730 public int getSummaryPrivateOther() {
731 return getTotalPrivateClean()
732 + getTotalPrivateDirty()
733 - getSummaryJavaHeap()
734 - getSummaryNativeHeap()
735 - getSummaryCode()
736 - getSummaryStack()
737 - getSummaryGraphics();
738 }
739
740 /**
741 * Pss in KB due to the system.
742 * Notes:
743 * * Includes all shared memory.
744 * @hide
745 */
746 public int getSummarySystem() {
747 return getTotalPss()
748 - getTotalPrivateClean()
749 - getTotalPrivateDirty();
750 }
751
752 /**
753 * Total Pss in KB.
754 * @hide
755 */
756 public int getSummaryTotalPss() {
757 return getTotalPss();
758 }
759
760 /**
761 * Total Swap in KB.
762 * Notes:
763 * * Some of this memory belongs in other categories, but we don't
764 * know if the Swap memory is shared or private, so we don't know
765 * what to blame on the application and what on the system.
766 * For now, just lump all the Swap in one place.
Martijn Coenene0764852016-01-07 17:04:22 -0800767 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()}
768 * will report the application proportional Swap.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700769 * @hide
770 */
771 public int getSummaryTotalSwap() {
772 return getTotalSwappedOut();
773 }
774
Martijn Coenene0764852016-01-07 17:04:22 -0800775 /**
776 * Total proportional Swap in KB.
777 * Notes:
778 * * Always 0 if {@link #hasSwappedOutPss} is false.
779 * @hide
780 */
781 public int getSummaryTotalSwapPss() {
782 return getTotalSwappedOutPss();
783 }
784
Dianne Hackbornef0a4022016-05-11 14:21:07 -0700785 /**
786 * Return true if the kernel is reporting pss swapped out... that is, if
787 * {@link #getSummaryTotalSwapPss()} will return non-0 values.
788 * @hide
789 */
790 public boolean hasSwappedOutPss() {
791 return hasSwappedOutPss;
792 }
793
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700794 public int describeContents() {
795 return 0;
796 }
797
798 public void writeToParcel(Parcel dest, int flags) {
799 dest.writeInt(dalvikPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700800 dest.writeInt(dalvikSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800801 dest.writeInt(dalvikRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700802 dest.writeInt(dalvikPrivateDirty);
803 dest.writeInt(dalvikSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700804 dest.writeInt(dalvikPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700805 dest.writeInt(dalvikSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700806 dest.writeInt(dalvikSwappedOut);
Richard Uhler91702eb32017-06-23 16:54:25 +0100807 dest.writeInt(dalvikSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700808 dest.writeInt(nativePss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700809 dest.writeInt(nativeSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800810 dest.writeInt(nativeRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700811 dest.writeInt(nativePrivateDirty);
812 dest.writeInt(nativeSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700813 dest.writeInt(nativePrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700814 dest.writeInt(nativeSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700815 dest.writeInt(nativeSwappedOut);
Richard Uhler91702eb32017-06-23 16:54:25 +0100816 dest.writeInt(nativeSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700817 dest.writeInt(otherPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700818 dest.writeInt(otherSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800819 dest.writeInt(otherRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700820 dest.writeInt(otherPrivateDirty);
821 dest.writeInt(otherSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700822 dest.writeInt(otherPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700823 dest.writeInt(otherSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700824 dest.writeInt(otherSwappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800825 dest.writeInt(hasSwappedOutPss ? 1 : 0);
826 dest.writeInt(otherSwappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700827 dest.writeIntArray(otherStats);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700828 }
829
830 public void readFromParcel(Parcel source) {
831 dalvikPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700832 dalvikSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800833 dalvikRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700834 dalvikPrivateDirty = source.readInt();
835 dalvikSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700836 dalvikPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700837 dalvikSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700838 dalvikSwappedOut = source.readInt();
Richard Uhler91702eb32017-06-23 16:54:25 +0100839 dalvikSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700840 nativePss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700841 nativeSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800842 nativeRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700843 nativePrivateDirty = source.readInt();
844 nativeSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700845 nativePrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700846 nativeSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700847 nativeSwappedOut = source.readInt();
Richard Uhler91702eb32017-06-23 16:54:25 +0100848 nativeSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700849 otherPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700850 otherSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800851 otherRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700852 otherPrivateDirty = source.readInt();
853 otherSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700854 otherPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700855 otherSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700856 otherSwappedOut = source.readInt();
Martijn Coenene0764852016-01-07 17:04:22 -0800857 hasSwappedOutPss = source.readInt() != 0;
858 otherSwappedOutPss = source.readInt();
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700859 otherStats = source.createIntArray();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700860 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200861
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700862 public static final @android.annotation.NonNull Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700863 public MemoryInfo createFromParcel(Parcel source) {
864 return new MemoryInfo(source);
865 }
866 public MemoryInfo[] newArray(int size) {
867 return new MemoryInfo[size];
868 }
869 };
870
871 private MemoryInfo(Parcel source) {
872 readFromParcel(source);
873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 }
875
876
877 /**
878 * Wait until a debugger attaches. As soon as the debugger attaches,
879 * this returns, so you will need to place a breakpoint after the
880 * waitForDebugger() call if you want to start tracing immediately.
881 */
882 public static void waitForDebugger() {
883 if (!VMDebug.isDebuggingEnabled()) {
884 //System.out.println("debugging not enabled, not waiting");
885 return;
886 }
887 if (isDebuggerConnected())
888 return;
889
890 // if DDMS is listening, inform them of our plight
891 System.out.println("Sending WAIT chunk");
892 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger"
893 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
894 DdmServer.sendChunk(waitChunk);
895
896 mWaiting = true;
897 while (!isDebuggerConnected()) {
898 try { Thread.sleep(SPIN_DELAY); }
899 catch (InterruptedException ie) {}
900 }
901 mWaiting = false;
902
903 System.out.println("Debugger has connected");
904
905 /*
906 * There is no "ready to go" signal from the debugger, and we're
907 * not allowed to suspend ourselves -- the debugger expects us to
908 * be running happily, and gets confused if we aren't. We need to
909 * allow the debugger a chance to set breakpoints before we start
910 * running again.
911 *
912 * Sit and spin until the debugger has been idle for a short while.
913 */
914 while (true) {
915 long delta = VMDebug.lastDebuggerActivity();
916 if (delta < 0) {
917 System.out.println("debugger detached?");
918 break;
919 }
920
921 if (delta < MIN_DEBUGGER_IDLE) {
922 System.out.println("waiting for debugger to settle...");
923 try { Thread.sleep(SPIN_DELAY); }
924 catch (InterruptedException ie) {}
925 } else {
926 System.out.println("debugger has settled (" + delta + ")");
927 break;
928 }
929 }
930 }
931
932 /**
933 * Returns "true" if one or more threads is waiting for a debugger
934 * to attach.
935 */
936 public static boolean waitingForDebugger() {
937 return mWaiting;
938 }
939
940 /**
941 * Determine if a debugger is currently attached.
942 */
943 public static boolean isDebuggerConnected() {
944 return VMDebug.isDebuggerConnected();
945 }
946
947 /**
Andy McFaddene5772322010-01-22 07:23:31 -0800948 * Returns an array of strings that identify VM features. This is
949 * used by DDMS to determine what sorts of operations the VM can
950 * perform.
951 *
952 * @hide
953 */
954 public static String[] getVmFeatureList() {
955 return VMDebug.getVmFeatureList();
956 }
957
958 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 * Change the JDWP port.
960 *
961 * @deprecated no longer needed or useful
962 */
963 @Deprecated
964 public static void changeDebugPort(int port) {}
965
966 /**
967 * This is the pathname to the sysfs file that enables and disables
968 * tracing on the qemu emulator.
969 */
970 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
971
972 /**
973 * Enable qemu tracing. For this to work requires running everything inside
974 * the qemu emulator; otherwise, this method will have no effect. The trace
975 * file is specified on the command line when the emulator is started. For
976 * example, the following command line <br />
977 * <code>emulator -trace foo</code><br />
978 * will start running the emulator and create a trace file named "foo". This
979 * method simply enables writing the trace records to the trace file.
980 *
981 * <p>
982 * The main differences between this and {@link #startMethodTracing()} are
983 * that tracing in the qemu emulator traces every cpu instruction of every
984 * process, including kernel code, so we have more complete information,
985 * including all context switches. We can also get more detailed information
986 * such as cache misses. The sequence of calls is determined by
987 * post-processing the instruction trace. The qemu tracing is also done
988 * without modifying the application or perturbing the timing of calls
989 * because no instrumentation is added to the application being traced.
990 * </p>
991 *
992 * <p>
993 * One limitation of using this method compared to using
994 * {@link #startMethodTracing()} on the real device is that the emulator
995 * does not model all of the real hardware effects such as memory and
996 * bus contention. The emulator also has a simple cache model and cannot
997 * capture all the complexities of a real cache.
998 * </p>
999 */
1000 public static void startNativeTracing() {
1001 // Open the sysfs file for writing and write "1" to it.
1002 PrintWriter outStream = null;
1003 try {
1004 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001005 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 outStream.println("1");
1007 } catch (Exception e) {
1008 } finally {
1009 if (outStream != null)
1010 outStream.close();
1011 }
1012
1013 VMDebug.startEmulatorTracing();
1014 }
1015
1016 /**
1017 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing.
1018 *
1019 * <p>Tracing can be started and stopped as many times as desired. When
1020 * the qemu emulator itself is stopped then the buffered trace records
1021 * are flushed and written to the trace file. In fact, it is not necessary
1022 * to call this method at all; simply killing qemu is sufficient. But
1023 * starting and stopping a trace is useful for examining a specific
1024 * region of code.</p>
1025 */
1026 public static void stopNativeTracing() {
1027 VMDebug.stopEmulatorTracing();
1028
1029 // Open the sysfs file for writing and write "0" to it.
1030 PrintWriter outStream = null;
1031 try {
1032 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001033 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 outStream.println("0");
1035 } catch (Exception e) {
1036 // We could print an error message here but we probably want
1037 // to quietly ignore errors if we are not running in the emulator.
1038 } finally {
1039 if (outStream != null)
1040 outStream.close();
1041 }
1042 }
1043
1044 /**
1045 * Enable "emulator traces", in which information about the current
1046 * method is made available to the "emulator -trace" feature. There
1047 * is no corresponding "disable" call -- this is intended for use by
1048 * the framework when tracing should be turned on and left that way, so
1049 * that traces captured with F9/F10 will include the necessary data.
1050 *
1051 * This puts the VM into "profile" mode, which has performance
1052 * consequences.
1053 *
1054 * To temporarily enable tracing, use {@link #startNativeTracing()}.
1055 */
1056 public static void enableEmulatorTraceOutput() {
1057 VMDebug.startEmulatorTracing();
1058 }
1059
1060 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001061 * Start method tracing with default log name and buffer size.
1062 * <p>
1063 * By default, the trace file is called "dmtrace.trace" and it's placed
1064 * under your package-specific directory on primary shared/external storage,
1065 * as returned by {@link Context#getExternalFilesDir(String)}.
1066 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001067 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1068 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001069 * <p class="note">
1070 * When method tracing is enabled, the VM will run more slowly than usual,
1071 * so the timings from the trace files should only be considered in relative
1072 * terms (e.g. was run #1 faster than run #2). The times for native methods
1073 * will not change, so don't try to use this to compare the performance of
1074 * interpreted and native implementations of the same method. As an
1075 * alternative, consider using sampling-based method tracing via
1076 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1077 * in the emulator via {@link #startNativeTracing()}.
1078 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 */
1080 public static void startMethodTracing() {
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001081 VMDebug.startMethodTracing(fixTracePath(null), 0, 0, false, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 }
1083
1084 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001085 * Start method tracing, specifying the trace log file path.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 * <p>
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001087 * When a relative file path is given, the trace file will be placed under
1088 * your package-specific directory on primary shared/external storage, as
1089 * returned by {@link Context#getExternalFilesDir(String)}.
1090 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001091 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1092 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001093 * <p class="note">
1094 * When method tracing is enabled, the VM will run more slowly than usual,
1095 * so the timings from the trace files should only be considered in relative
1096 * terms (e.g. was run #1 faster than run #2). The times for native methods
1097 * will not change, so don't try to use this to compare the performance of
1098 * interpreted and native implementations of the same method. As an
1099 * alternative, consider using sampling-based method tracing via
1100 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1101 * in the emulator via {@link #startNativeTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 * </p>
1103 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001104 * @param tracePath Path to the trace log file to create. If {@code null},
1105 * this will default to "dmtrace.trace". If the file already
1106 * exists, it will be truncated. If the path given does not end
1107 * in ".trace", it will be appended for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001109 public static void startMethodTracing(String tracePath) {
1110 startMethodTracing(tracePath, 0, 0);
1111 }
1112
1113 /**
1114 * Start method tracing, specifying the trace log file name and the buffer
1115 * size.
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>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001121 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1122 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001123 * <p class="note">
1124 * When method tracing is enabled, the VM will run more slowly than usual,
1125 * so the timings from the trace files should only be considered in relative
1126 * terms (e.g. was run #1 faster than run #2). The times for native methods
1127 * will not change, so don't try to use this to compare the performance of
1128 * interpreted and native implementations of the same method. As an
1129 * alternative, consider using sampling-based method tracing via
1130 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1131 * in the emulator via {@link #startNativeTracing()}.
1132 * </p>
1133 *
1134 * @param tracePath Path to the trace log file to create. If {@code null},
1135 * this will default to "dmtrace.trace". If the file already
1136 * exists, it will be truncated. If the path given does not end
1137 * in ".trace", it will be appended for you.
1138 * @param bufferSize The maximum amount of trace data we gather. If not
1139 * given, it defaults to 8MB.
1140 */
1141 public static void startMethodTracing(String tracePath, int bufferSize) {
1142 startMethodTracing(tracePath, bufferSize, 0);
1143 }
1144
1145 /**
1146 * Start method tracing, specifying the trace log file name, the buffer
1147 * size, and flags.
1148 * <p>
1149 * When a relative file path is given, the trace file will be placed under
1150 * your package-specific directory on primary shared/external storage, as
1151 * returned by {@link Context#getExternalFilesDir(String)}.
1152 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001153 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1154 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001155 * <p class="note">
1156 * When method tracing is enabled, the VM will run more slowly than usual,
1157 * so the timings from the trace files should only be considered in relative
1158 * terms (e.g. was run #1 faster than run #2). The times for native methods
1159 * will not change, so don't try to use this to compare the performance of
1160 * interpreted and native implementations of the same method. As an
1161 * alternative, consider using sampling-based method tracing via
1162 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1163 * in the emulator via {@link #startNativeTracing()}.
1164 * </p>
1165 *
1166 * @param tracePath Path to the trace log file to create. If {@code null},
1167 * this will default to "dmtrace.trace". If the file already
1168 * exists, it will be truncated. If the path given does not end
1169 * in ".trace", it will be appended for you.
1170 * @param bufferSize The maximum amount of trace data we gather. If not
1171 * given, it defaults to 8MB.
1172 * @param flags Flags to control method tracing. The only one that is
1173 * currently defined is {@link #TRACE_COUNT_ALLOCS}.
1174 */
1175 public static void startMethodTracing(String tracePath, int bufferSize, int flags) {
1176 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, flags, false, 0);
Jeff Haod02e60f2014-01-06 15:52:52 -08001177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178
Jeff Haod02e60f2014-01-06 15:52:52 -08001179 /**
1180 * Start sampling-based method tracing, specifying the trace log file name,
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001181 * the buffer size, and the sampling interval.
1182 * <p>
1183 * When a relative file path is given, the trace file will be placed under
1184 * your package-specific directory on primary shared/external storage, as
1185 * returned by {@link Context#getExternalFilesDir(String)}.
1186 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001187 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1188 * with Traceview</a> for information about reading trace files.
Jeff Haod02e60f2014-01-06 15:52:52 -08001189 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001190 * @param tracePath Path to the trace log file to create. If {@code null},
1191 * this will default to "dmtrace.trace". If the file already
1192 * exists, it will be truncated. If the path given does not end
1193 * in ".trace", it will be appended for you.
1194 * @param bufferSize The maximum amount of trace data we gather. If not
1195 * given, it defaults to 8MB.
1196 * @param intervalUs The amount of time between each sample in microseconds.
Jeff Haod02e60f2014-01-06 15:52:52 -08001197 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001198 public static void startMethodTracingSampling(String tracePath, int bufferSize,
1199 int intervalUs) {
1200 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, 0, true, intervalUs);
Jeff Haod02e60f2014-01-06 15:52:52 -08001201 }
Kweku Adams983829f2017-12-06 14:53:50 -08001202
Jeff Haod02e60f2014-01-06 15:52:52 -08001203 /**
1204 * Formats name of trace log file for method tracing.
1205 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001206 private static String fixTracePath(String tracePath) {
1207 if (tracePath == null || tracePath.charAt(0) != '/') {
1208 final Context context = AppGlobals.getInitialApplication();
1209 final File dir;
1210 if (context != null) {
1211 dir = context.getExternalFilesDir(null);
1212 } else {
1213 dir = Environment.getExternalStorageDirectory();
1214 }
Jeff Haod02e60f2014-01-06 15:52:52 -08001215
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001216 if (tracePath == null) {
1217 tracePath = new File(dir, DEFAULT_TRACE_BODY).getAbsolutePath();
1218 } else {
1219 tracePath = new File(dir, tracePath).getAbsolutePath();
1220 }
1221 }
1222 if (!tracePath.endsWith(DEFAULT_TRACE_EXTENSION)) {
1223 tracePath += DEFAULT_TRACE_EXTENSION;
1224 }
1225 return tracePath;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227
1228 /**
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001229 * Like startMethodTracing(String, int, int), but taking an already-opened
1230 * FileDescriptor in which the trace is written. The file name is also
1231 * supplied simply for logging. Makes a dup of the file descriptor.
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02001232 *
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001233 * Not exposed in the SDK unless we are really comfortable with supporting
1234 * this and find it would be useful.
1235 * @hide
1236 */
1237 public static void startMethodTracing(String traceName, FileDescriptor fd,
Shukang Zhou6ec0b7e2017-01-24 15:30:29 -08001238 int bufferSize, int flags, boolean streamOutput) {
1239 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0, streamOutput);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001240 }
1241
1242 /**
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001243 * Starts method tracing without a backing file. When stopMethodTracing
1244 * is called, the result is sent directly to DDMS. (If DDMS is not
1245 * attached when tracing ends, the profiling data will be discarded.)
1246 *
1247 * @hide
1248 */
Jeff Hao7be3a132013-08-22 15:53:12 -07001249 public static void startMethodTracingDdms(int bufferSize, int flags,
1250 boolean samplingEnabled, int intervalUs) {
1251 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs);
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001252 }
1253
1254 /**
Jeff Haoac277052013-08-29 11:19:39 -07001255 * Determine whether method tracing is currently active and what type is
1256 * active.
1257 *
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001258 * @hide
1259 */
Jeff Haoac277052013-08-29 11:19:39 -07001260 public static int getMethodTracingMode() {
1261 return VMDebug.getMethodTracingMode();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001262 }
1263
1264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 * Stop method tracing.
1266 */
1267 public static void stopMethodTracing() {
1268 VMDebug.stopMethodTracing();
1269 }
1270
1271 /**
1272 * Get an indication of thread CPU usage. The value returned
1273 * indicates the amount of time that the current thread has spent
1274 * executing code or waiting for certain types of I/O.
1275 *
1276 * The time is expressed in nanoseconds, and is only meaningful
1277 * when compared to the result from an earlier call. Note that
1278 * nanosecond resolution does not imply nanosecond accuracy.
1279 *
1280 * On system which don't support this operation, the call returns -1.
1281 */
1282 public static long threadCpuTimeNanos() {
1283 return VMDebug.threadCpuTimeNanos();
1284 }
1285
1286 /**
Chet Haase2970c492010-11-09 13:58:04 -08001287 * Start counting the number and aggregate size of memory allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001289 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
1290 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
1291 * code doesn't cause additional allocations. The various <code>get</code> methods return
1292 * the specified value. And the various <code>reset</code> methods reset the specified
Chet Haase2970c492010-11-09 13:58:04 -08001293 * count.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001295 * <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 -08001296 * The per-thread counts for threads other than the current thread
Chet Haase2970c492010-11-09 13:58:04 -08001297 * are not cleared by the "reset" or "start" calls.</p>
Ian Rogersfe067a42013-02-22 19:59:23 -08001298 *
1299 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 */
Ian Rogersfe067a42013-02-22 19:59:23 -08001301 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 public static void startAllocCounting() {
1303 VMDebug.startAllocCounting();
1304 }
Chet Haase2970c492010-11-09 13:58:04 -08001305
1306 /**
1307 * Stop counting the number and aggregate size of memory allocations.
1308 *
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001309 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Chet Haase2970c492010-11-09 13:58:04 -08001310 */
Ian Rogersc2a3adb2013-04-19 11:31:48 -07001311 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 public static void stopAllocCounting() {
1313 VMDebug.stopAllocCounting();
1314 }
1315
Ian Rogersfe067a42013-02-22 19:59:23 -08001316 /**
1317 * Returns the global count of objects allocated 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 getGlobalAllocCount() {
1324 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1325 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001326
1327 /**
1328 * Clears the global count of objects allocated.
1329 * @see #getGlobalAllocCount()
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 resetGlobalAllocCount() {
1335 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1336 }
1337
1338 /**
1339 * Returns the global size, in bytes, of objects allocated by the runtime 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 public static int getGlobalAllocSize() {
1346 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1347 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001348
1349 /**
1350 * Clears the global size of objects allocated.
Dianne Hackborn3fa89692013-09-13 17:20:00 -07001351 * @see #getGlobalAllocSize()
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 resetGlobalAllocSize() {
1357 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1358 }
1359
1360 /**
1361 * Returns the global count of objects freed by the runtime between a
1362 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001363 *
1364 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001365 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001366 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public static int getGlobalFreedCount() {
1368 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1369 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001370
1371 /**
1372 * Clears the global count of objects freed.
1373 * @see #getGlobalFreedCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001374 *
1375 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001376 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001377 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001378 public static void resetGlobalFreedCount() {
1379 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1380 }
1381
1382 /**
1383 * Returns the global size, in bytes, of objects freed by the runtime between a
1384 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001385 *
1386 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001387 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001388 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 public static int getGlobalFreedSize() {
1390 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1391 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001392
1393 /**
1394 * Clears the global size of objects freed.
1395 * @see #getGlobalFreedSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001396 *
1397 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001398 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001399 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001400 public static void resetGlobalFreedSize() {
1401 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1402 }
1403
1404 /**
1405 * Returns the number of non-concurrent GC invocations between a
1406 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001407 *
1408 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001409 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001410 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001411 public static int getGlobalGcInvocationCount() {
1412 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1413 }
1414
1415 /**
1416 * Clears the count of non-concurrent GC invocations.
1417 * @see #getGlobalGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001418 *
1419 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001420 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001421 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001422 public static void resetGlobalGcInvocationCount() {
1423 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1424 }
1425
1426 /**
1427 * Returns the number of classes successfully initialized (ie those that executed without
1428 * throwing an exception) between a {@link #startAllocCounting() start} and
1429 * {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001430 *
1431 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001432 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001433 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001434 public static int getGlobalClassInitCount() {
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001435 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1436 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001437
1438 /**
1439 * Clears the count of classes initialized.
1440 * @see #getGlobalClassInitCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001441 *
1442 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001443 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001444 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001445 public static void resetGlobalClassInitCount() {
1446 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1447 }
1448
1449 /**
1450 * Returns the time spent successfully initializing classes between a
1451 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001452 *
1453 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001454 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001455 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001456 public static int getGlobalClassInitTime() {
1457 /* cumulative elapsed time for class initialization, in usec */
1458 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1459 }
Carl Shapirob5961982010-12-22 15:54:53 -08001460
1461 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001462 * Clears the count of time spent initializing classes.
1463 * @see #getGlobalClassInitTime()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001464 *
1465 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001466 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001467 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001468 public static void resetGlobalClassInitTime() {
1469 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1470 }
1471
1472 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001473 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001474 * @deprecated This method is now obsolete.
1475 */
1476 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 public static int getGlobalExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001478 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 }
Carl Shapirob5961982010-12-22 15:54:53 -08001480
1481 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001482 * This method exists for compatibility and has no effect.
1483 * @deprecated This method is now obsolete.
1484 */
1485 @Deprecated
1486 public static void resetGlobalExternalAllocSize() {}
1487
1488 /**
1489 * This method exists for compatibility and has no effect.
1490 * @deprecated This method is now obsolete.
1491 */
1492 @Deprecated
1493 public static void resetGlobalExternalAllocCount() {}
1494
1495 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001496 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001497 * @deprecated This method is now obsolete.
1498 */
1499 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 public static int getGlobalExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001501 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 }
Carl Shapirob5961982010-12-22 15:54:53 -08001503
1504 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001505 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001506 * @deprecated This method is now obsolete.
1507 */
1508 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 public static int getGlobalExternalFreedCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001510 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
Carl Shapirob5961982010-12-22 15:54:53 -08001512
1513 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001514 * This method exists for compatibility and has no effect.
1515 * @deprecated This method is now obsolete.
1516 */
1517 @Deprecated
1518 public static void resetGlobalExternalFreedCount() {}
1519
1520 /**
1521 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001522 * @deprecated This method is now obsolete.
1523 */
1524 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 public static int getGlobalExternalFreedSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001526 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
Carl Shapirob5961982010-12-22 15:54:53 -08001528
Ian Rogersfe067a42013-02-22 19:59:23 -08001529 /**
1530 * This method exists for compatibility and has no effect.
1531 * @deprecated This method is now obsolete.
1532 */
1533 @Deprecated
1534 public static void resetGlobalExternalFreedSize() {}
1535
1536 /**
1537 * Returns the thread-local count of objects allocated by the runtime between a
1538 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001539 *
1540 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001541 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001542 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 public static int getThreadAllocCount() {
1544 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1545 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001546
1547 /**
1548 * Clears the thread-local count of objects allocated.
1549 * @see #getThreadAllocCount()
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 void resetThreadAllocCount() {
1555 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1556 }
1557
1558 /**
1559 * Returns the thread-local size of objects allocated by the runtime between a
1560 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
1561 * @return The allocated size in bytes.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001562 *
1563 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001564 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001565 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 public static int getThreadAllocSize() {
1567 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1568 }
Carl Shapirob5961982010-12-22 15:54:53 -08001569
1570 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001571 * Clears the thread-local count of objects allocated.
1572 * @see #getThreadAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001573 *
1574 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001575 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001576 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001577 public static void resetThreadAllocSize() {
1578 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1579 }
1580
1581 /**
1582 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001583 * @deprecated This method is now obsolete.
1584 */
1585 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 public static int getThreadExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001587 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
Carl Shapirob5961982010-12-22 15:54:53 -08001589
1590 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001591 * This method exists for compatibility and has no effect.
1592 * @deprecated This method is now obsolete.
1593 */
1594 @Deprecated
1595 public static void resetThreadExternalAllocCount() {}
1596
1597 /**
1598 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001599 * @deprecated This method is now obsolete.
1600 */
1601 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 public static int getThreadExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001603 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
Carl Shapirob5961982010-12-22 15:54:53 -08001605
Carl Shapirob5961982010-12-22 15:54:53 -08001606 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001607 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001608 * @deprecated This method is now obsolete.
1609 */
1610 @Deprecated
1611 public static void resetThreadExternalAllocSize() {}
1612
Ian Rogersfe067a42013-02-22 19:59:23 -08001613 /**
1614 * Returns the number of thread-local non-concurrent GC invocations between a
1615 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001616 *
1617 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001618 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001619 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001620 public static int getThreadGcInvocationCount() {
1621 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1622 }
1623
1624 /**
1625 * Clears the thread-local count of non-concurrent GC invocations.
1626 * @see #getThreadGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001627 *
1628 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001629 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001630 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public static void resetThreadGcInvocationCount() {
1632 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1633 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001634
1635 /**
1636 * Clears all the global and thread-local memory allocation counters.
1637 * @see #startAllocCounting()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001638 *
1639 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001640 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001641 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 public static void resetAllCounts() {
1643 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
1644 }
1645
1646 /**
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001647 * Returns the value of a particular runtime statistic or {@code null} if no
1648 * such runtime statistic exists.
1649 *
1650 * <p>The following table lists the runtime statistics that the runtime supports.
1651 * Note runtime statistics may be added or removed in a future API level.</p>
1652 *
1653 * <table>
1654 * <thead>
1655 * <tr>
1656 * <th>Runtime statistic name</th>
1657 * <th>Meaning</th>
1658 * <th>Example</th>
1659 * <th>Supported (API Levels)</th>
1660 * </tr>
1661 * </thead>
1662 * <tbody>
1663 * <tr>
1664 * <td>art.gc.gc-count</td>
1665 * <td>The number of garbage collection runs.</td>
1666 * <td>{@code 164}</td>
1667 * <td>23</td>
1668 * </tr>
1669 * <tr>
1670 * <td>art.gc.gc-time</td>
1671 * <td>The total duration of garbage collection runs in ms.</td>
1672 * <td>{@code 62364}</td>
1673 * <td>23</td>
1674 * </tr>
1675 * <tr>
1676 * <td>art.gc.bytes-allocated</td>
1677 * <td>The total number of bytes that the application allocated.</td>
1678 * <td>{@code 1463948408}</td>
1679 * <td>23</td>
1680 * </tr>
1681 * <tr>
1682 * <td>art.gc.bytes-freed</td>
1683 * <td>The total number of bytes that garbage collection reclaimed.</td>
1684 * <td>{@code 1313493084}</td>
1685 * <td>23</td>
1686 * </tr>
1687 * <tr>
1688 * <td>art.gc.blocking-gc-count</td>
1689 * <td>The number of blocking garbage collection runs.</td>
1690 * <td>{@code 2}</td>
1691 * <td>23</td>
1692 * </tr>
1693 * <tr>
1694 * <td>art.gc.blocking-gc-time</td>
1695 * <td>The total duration of blocking garbage collection runs in ms.</td>
1696 * <td>{@code 804}</td>
1697 * <td>23</td>
1698 * </tr>
1699 * <tr>
1700 * <td>art.gc.gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001701 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage
1702 * collection runs that have occurred over the last 10
1703 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate
1704 * samples taken since the process began. The histogram can be used to identify
1705 * instances of high rates of garbage collection runs. For example, a histogram
1706 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time
1707 * there are between 0 and 2 garbage collection runs every 10 seconds, but there
1708 * were 8 distinct 10-second intervals in which 5 garbage collection runs
1709 * occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001710 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td>
1711 * <td>23</td>
1712 * </tr>
1713 * <tr>
1714 * <td>art.gc.blocking-gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001715 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of
1716 * blocking garbage collection runs that have occurred over the last 10
1717 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the
1718 * blocking-gc-count-rate samples taken since the process began. The histogram
1719 * can be used to identify instances of high rates of blocking garbage
1720 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that
1721 * most of the time there are zero blocking garbage collection runs every 10
1722 * seconds, but there was one 10-second interval in which one blocking garbage
1723 * collection run occurred, and there was one interval in which two blocking
1724 * garbage collection runs occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001725 * <td>{@code 0:99269,1:1,2:1}</td>
1726 * <td>23</td>
1727 * </tr>
1728 * </tbody>
1729 * </table>
1730 *
1731 * @param statName
1732 * the name of the runtime statistic to look up.
1733 * @return the value of the specified runtime statistic or {@code null} if the
1734 * runtime statistic doesn't exist.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001735 */
1736 public static String getRuntimeStat(String statName) {
1737 return VMDebug.getRuntimeStat(statName);
1738 }
1739
1740 /**
1741 * Returns a map of the names/values of the runtime statistics
Hiroshi Yamauchid8001672015-04-14 16:07:26 -07001742 * that {@link #getRuntimeStat(String)} supports.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001743 *
1744 * @return a map of the names/values of the supported runtime statistics.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001745 */
1746 public static Map<String, String> getRuntimeStats() {
1747 return VMDebug.getRuntimeStats();
1748 }
1749
1750 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 * Returns the size of the native heap.
1752 * @return The size of the native heap in bytes.
1753 */
1754 public static native long getNativeHeapSize();
1755
1756 /**
1757 * Returns the amount of allocated memory in the native heap.
1758 * @return The allocated size in bytes.
1759 */
1760 public static native long getNativeHeapAllocatedSize();
1761
1762 /**
1763 * Returns the amount of free memory in the native heap.
1764 * @return The freed size in bytes.
1765 */
1766 public static native long getNativeHeapFreeSize();
1767
1768 /**
1769 * Retrieves information about this processes memory usages. This information is broken down by
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001770 * how much is in use by dalvik, the native heap, and everything else.
1771 *
Kweku Adams983829f2017-12-06 14:53:50 -08001772 * <p><b>Note:</b> this method directly retrieves memory information for the given process
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001773 * from low-level data available to it. It may not be able to retrieve information about
1774 * some protected allocations, such as graphics. If you want to be sure you can see
Kweku Adams983829f2017-12-06 14:53:50 -08001775 * all information about allocations by the process, use
1776 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])} instead.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 */
1778 public static native void getMemoryInfo(MemoryInfo memoryInfo);
1779
1780 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001781 * Note: currently only works when the requested pid has the same UID
1782 * as the caller.
1783 * @hide
1784 */
1785 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
1786
1787 /**
Dianne Hackbornb437e092011-08-05 17:50:29 -07001788 * Retrieves the PSS memory used by the process as given by the
1789 * smaps.
1790 */
1791 public static native long getPss();
1792
1793 /**
Rafal Slawik4d078962018-08-20 18:23:49 +01001794 * Retrieves the PSS memory used by the process as given by the smaps. Optionally supply a long
1795 * array of up to 3 entries to also receive (up to 3 values in order): the Uss and SwapPss and
1796 * Rss (only filled in as of {@link android.os.Build.VERSION_CODES#P}) of the process, and
1797 * another array to also retrieve the separate memtrack size.
Martijn Coenene0764852016-01-07 17:04:22 -08001798 * @hide
Dianne Hackbornb437e092011-08-05 17:50:29 -07001799 */
Rafal Slawik4d078962018-08-20 18:23:49 +01001800 public static native long getPss(int pid, long[] outUssSwapPssRss, long[] outMemtrack);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001801
Dianne Hackborn8e692572013-09-10 19:06:15 -07001802 /** @hide */
1803 public static final int MEMINFO_TOTAL = 0;
1804 /** @hide */
1805 public static final int MEMINFO_FREE = 1;
1806 /** @hide */
1807 public static final int MEMINFO_BUFFERS = 2;
1808 /** @hide */
1809 public static final int MEMINFO_CACHED = 3;
1810 /** @hide */
1811 public static final int MEMINFO_SHMEM = 4;
1812 /** @hide */
1813 public static final int MEMINFO_SLAB = 5;
Robert Benea5e099802017-10-04 18:28:01 -07001814 /** @hide */
1815 public static final int MEMINFO_SLAB_RECLAIMABLE = 6;
1816 /** @hide */
1817 public static final int MEMINFO_SLAB_UNRECLAIMABLE = 7;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001818 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001819 public static final int MEMINFO_SWAP_TOTAL = 8;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001820 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001821 public static final int MEMINFO_SWAP_FREE = 9;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001822 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001823 public static final int MEMINFO_ZRAM_TOTAL = 10;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001824 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001825 public static final int MEMINFO_MAPPED = 11;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001826 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001827 public static final int MEMINFO_VM_ALLOC_USED = 12;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001828 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001829 public static final int MEMINFO_PAGE_TABLES = 13;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001830 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001831 public static final int MEMINFO_KERNEL_STACK = 14;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001832 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001833 public static final int MEMINFO_COUNT = 15;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001834
1835 /**
1836 * Retrieves /proc/meminfo. outSizes is filled with fields
1837 * as defined by MEMINFO_* offsets.
1838 * @hide
1839 */
1840 public static native void getMemInfo(long[] outSizes);
1841
Dianne Hackbornb437e092011-08-05 17:50:29 -07001842 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001843 * Establish an object allocation limit in the current thread.
Carl Shapiro7e942842011-01-12 17:17:45 -08001844 * This feature was never enabled in release builds. The
1845 * allocation limits feature was removed in Honeycomb. This
1846 * method exists for compatibility and always returns -1 and has
1847 * no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 *
Carl Shapiro11073832011-01-12 16:28:57 -08001849 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 */
Carl Shapiro11073832011-01-12 16:28:57 -08001851 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 public static int setAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001853 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855
1856 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001857 * Establish a global object allocation limit. This feature was
Carl Shapiro7e942842011-01-12 17:17:45 -08001858 * never enabled in release builds. The allocation limits feature
1859 * was removed in Honeycomb. This method exists for compatibility
1860 * and always returns -1 and has no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 *
Carl Shapiro11073832011-01-12 16:28:57 -08001862 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 */
Carl Shapiro11073832011-01-12 16:28:57 -08001864 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 public static int setGlobalAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001866 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
1869 /**
1870 * Dump a list of all currently loaded class to the log file.
1871 *
1872 * @param flags See constants above.
1873 */
1874 public static void printLoadedClasses(int flags) {
1875 VMDebug.printLoadedClasses(flags);
1876 }
1877
1878 /**
1879 * Get the number of loaded classes.
1880 * @return the number of loaded classes.
1881 */
1882 public static int getLoadedClassCount() {
1883 return VMDebug.getLoadedClassCount();
1884 }
1885
1886 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001887 * Dump "hprof" data to the specified file. This may cause a GC.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 *
1889 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
1890 * @throws UnsupportedOperationException if the VM was built without
1891 * HPROF support.
1892 * @throws IOException if an error occurs while opening or writing files.
1893 */
1894 public static void dumpHprofData(String fileName) throws IOException {
1895 VMDebug.dumpHprofData(fileName);
1896 }
1897
1898 /**
Andy McFadden824c5102010-07-09 16:26:57 -07001899 * Like dumpHprofData(String), but takes an already-opened
1900 * FileDescriptor to which the trace is written. The file name is also
1901 * supplied simply for logging. Makes a dup of the file descriptor.
1902 *
1903 * Primarily for use by the "am" shell command.
1904 *
1905 * @hide
1906 */
1907 public static void dumpHprofData(String fileName, FileDescriptor fd)
1908 throws IOException {
1909 VMDebug.dumpHprofData(fileName, fd);
1910 }
1911
1912 /**
1913 * Collect "hprof" and send it to DDMS. This may cause a GC.
Andy McFadden07a96612010-01-28 16:54:37 -08001914 *
1915 * @throws UnsupportedOperationException if the VM was built without
1916 * HPROF support.
Andy McFadden07a96612010-01-28 16:54:37 -08001917 * @hide
1918 */
1919 public static void dumpHprofDataDdms() {
1920 VMDebug.dumpHprofDataDdms();
1921 }
1922
1923 /**
Andy McFadden06a6b552010-07-13 16:28:09 -07001924 * Writes native heap data to the specified file descriptor.
1925 *
1926 * @hide
1927 */
1928 public static native void dumpNativeHeap(FileDescriptor fd);
1929
1930 /**
Christopher Ferris8d652f82017-04-11 16:29:18 -07001931 * Writes malloc info data to the specified file descriptor.
1932 *
1933 * @hide
1934 */
1935 public static native void dumpNativeMallocInfo(FileDescriptor fd);
1936
1937 /**
Brian Carlstromc21550a2010-10-05 21:34:06 -07001938 * Returns a count of the extant instances of a class.
1939 *
1940 * @hide
1941 */
1942 public static long countInstancesOfClass(Class cls) {
Brian Carlstrom7495cfa2010-11-30 18:06:00 -08001943 return VMDebug.countInstancesOfClass(cls, true);
Brian Carlstromc21550a2010-10-05 21:34:06 -07001944 }
1945
1946 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 * Returns the number of sent transactions from this process.
1948 * @return The number of sent transactions or -1 if it could not read t.
1949 */
1950 public static native int getBinderSentTransactions();
1951
1952 /**
1953 * Returns the number of received transactions from the binder driver.
1954 * @return The number of received transactions or -1 if it could not read the stats.
1955 */
1956 public static native int getBinderReceivedTransactions();
1957
1958 /**
1959 * Returns the number of active local Binder objects that exist in the
1960 * current process.
1961 */
1962 public static final native int getBinderLocalObjectCount();
1963
1964 /**
1965 * Returns the number of references to remote proxy Binder objects that
1966 * exist in the current process.
1967 */
1968 public static final native int getBinderProxyObjectCount();
1969
1970 /**
1971 * Returns the number of death notification links to Binder objects that
1972 * exist in the current process.
1973 */
1974 public static final native int getBinderDeathObjectCount();
1975
1976 /**
Andy McFadden599c9182009-04-08 00:35:56 -07001977 * Primes the register map cache.
1978 *
1979 * Only works for classes in the bootstrap class loader. Does not
1980 * cause classes to be loaded if they're not already present.
1981 *
1982 * The classAndMethodDesc argument is a concatentation of the VM-internal
1983 * class descriptor, method name, and method descriptor. Examples:
1984 * Landroid/os/Looper;.loop:()V
1985 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
1986 *
1987 * @param classAndMethodDesc the method to prepare
1988 *
1989 * @hide
1990 */
1991 public static final boolean cacheRegisterMap(String classAndMethodDesc) {
1992 return VMDebug.cacheRegisterMap(classAndMethodDesc);
1993 }
1994
1995 /**
Andy McFaddenbfd6d482009-10-22 17:25:57 -07001996 * Dumps the contents of VM reference tables (e.g. JNI locals and
1997 * globals) to the log file.
1998 *
1999 * @hide
2000 */
2001 public static final void dumpReferenceTables() {
2002 VMDebug.dumpReferenceTables();
2003 }
2004
2005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 * API for gathering and querying instruction counts.
2007 *
2008 * Example usage:
Chet Haase2970c492010-11-09 13:58:04 -08002009 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 * Debug.InstructionCount icount = new Debug.InstructionCount();
2011 * icount.resetAndStart();
2012 * [... do lots of stuff ...]
2013 * if (icount.collect()) {
2014 * System.out.println("Total instructions executed: "
2015 * + icount.globalTotal());
2016 * System.out.println("Method invocations: "
2017 * + icount.globalMethodInvocations());
2018 * }
Chet Haase2970c492010-11-09 13:58:04 -08002019 * </pre>
Jeff Hao7d0b3d42014-09-17 15:45:05 -07002020 *
2021 * @deprecated Instruction counting is no longer supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 */
Jeff Hao7d0b3d42014-09-17 15:45:05 -07002023 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 public static class InstructionCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 public InstructionCount() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027
2028 /**
2029 * Reset counters and ensure counts are running. Counts may
2030 * have already been running.
2031 *
2032 * @return true if counting was started
2033 */
2034 public boolean resetAndStart() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002035 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
2037
2038 /**
2039 * Collect instruction counts. May or may not stop the
2040 * counting process.
2041 */
2042 public boolean collect() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002043 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045
2046 /**
2047 * Return the total number of instructions executed globally (i.e. in
2048 * all threads).
2049 */
2050 public int globalTotal() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002051 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
2054 /**
2055 * Return the total number of method-invocation instructions
2056 * executed globally.
2057 */
2058 public int globalMethodInvocations() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002059 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002061 }
2062
Dave Bort1ce5bd32009-04-22 17:36:56 -07002063 /**
2064 * A Map of typed debug properties.
2065 */
2066 private static final TypedProperties debugProperties;
2067
2068 /*
2069 * Load the debug properties from the standard files into debugProperties.
2070 */
2071 static {
Joe Onorato43a17652011-04-06 19:22:23 -07002072 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002073 final String TAG = "DebugProperties";
2074 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
2075 final TypedProperties tp = new TypedProperties();
2076
2077 // Read the properties from each of the files, if present.
Dave Borte9bfd9b2009-05-04 14:35:23 -07002078 for (String file : files) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002079 Reader r;
2080 try {
2081 r = new FileReader(file);
2082 } catch (FileNotFoundException ex) {
2083 // It's ok if a file is missing.
2084 continue;
2085 }
2086
Dave Bort1ce5bd32009-04-22 17:36:56 -07002087 try {
2088 tp.load(r);
Dave Borte9bfd9b2009-05-04 14:35:23 -07002089 } catch (Exception ex) {
2090 throw new RuntimeException("Problem loading " + file, ex);
2091 } finally {
2092 try {
2093 r.close();
2094 } catch (IOException ex) {
2095 // Ignore this error.
2096 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002097 }
2098 }
2099
2100 debugProperties = tp.isEmpty() ? null : tp;
2101 } else {
2102 debugProperties = null;
2103 }
2104 }
2105
2106
2107 /**
2108 * Returns true if the type of the field matches the specified class.
2109 * Handles the case where the class is, e.g., java.lang.Boolean, but
2110 * the field is of the primitive "boolean" type. Also handles all of
2111 * the java.lang.Number subclasses.
2112 */
2113 private static boolean fieldTypeMatches(Field field, Class<?> cl) {
2114 Class<?> fieldClass = field.getType();
2115 if (fieldClass == cl) {
2116 return true;
2117 }
2118 Field primitiveTypeField;
2119 try {
2120 /* All of the classes we care about (Boolean, Integer, etc.)
2121 * have a Class field called "TYPE" that points to the corresponding
2122 * primitive class.
2123 */
2124 primitiveTypeField = cl.getField("TYPE");
2125 } catch (NoSuchFieldException ex) {
2126 return false;
2127 }
2128 try {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002129 return fieldClass == (Class<?>) primitiveTypeField.get(null);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002130 } catch (IllegalAccessException ex) {
2131 return false;
2132 }
2133 }
2134
2135
2136 /**
2137 * Looks up the property that corresponds to the field, and sets the field's value
2138 * if the types match.
2139 */
Dave Borte9bfd9b2009-05-04 14:35:23 -07002140 private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
2141 final String propertyName) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002142 if (field.getType() == java.lang.String.class) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002143 int stringInfo = properties.getStringInfo(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002144 switch (stringInfo) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002145 case TypedProperties.STRING_SET:
2146 // Handle as usual below.
2147 break;
2148 case TypedProperties.STRING_NULL:
2149 try {
2150 field.set(null, null); // null object for static fields; null string
2151 } catch (IllegalAccessException ex) {
2152 throw new IllegalArgumentException(
2153 "Cannot set field for " + propertyName, ex);
2154 }
2155 return;
2156 case TypedProperties.STRING_NOT_SET:
2157 return;
2158 case TypedProperties.STRING_TYPE_MISMATCH:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002159 throw new IllegalArgumentException(
Dave Borte9bfd9b2009-05-04 14:35:23 -07002160 "Type of " + propertyName + " " +
2161 " does not match field type (" + field.getType() + ")");
2162 default:
2163 throw new IllegalStateException(
2164 "Unexpected getStringInfo(" + propertyName + ") return value " +
2165 stringInfo);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002166 }
2167 }
Dave Borte9bfd9b2009-05-04 14:35:23 -07002168 Object value = properties.get(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002169 if (value != null) {
2170 if (!fieldTypeMatches(field, value.getClass())) {
2171 throw new IllegalArgumentException(
2172 "Type of " + propertyName + " (" + value.getClass() + ") " +
2173 " does not match field type (" + field.getType() + ")");
2174 }
2175 try {
2176 field.set(null, value); // null object for static fields
2177 } catch (IllegalAccessException ex) {
2178 throw new IllegalArgumentException(
2179 "Cannot set field for " + propertyName, ex);
2180 }
2181 }
2182 }
2183
2184
2185 /**
Romain Guyc4b11a72009-05-13 15:46:37 -07002186 * Equivalent to <code>setFieldsOn(cl, false)</code>.
2187 *
2188 * @see #setFieldsOn(Class, boolean)
Romain Guyd4103d02009-05-14 12:24:21 -07002189 *
2190 * @hide
Romain Guyc4b11a72009-05-13 15:46:37 -07002191 */
2192 public static void setFieldsOn(Class<?> cl) {
2193 setFieldsOn(cl, false);
2194 }
2195
2196 /**
Dave Bort1ce5bd32009-04-22 17:36:56 -07002197 * Reflectively sets static fields of a class based on internal debugging
Joe Onorato43a17652011-04-06 19:22:23 -07002198 * properties. This method is a no-op if false is
Dave Bort1ce5bd32009-04-22 17:36:56 -07002199 * false.
2200 * <p>
Joe Onorato43a17652011-04-06 19:22:23 -07002201 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will
Dave Bort1ce5bd32009-04-22 17:36:56 -07002202 * always be false in release builds. This API is typically only useful
2203 * for platform developers.
2204 * </p>
2205 * Class setup: define a class whose only fields are non-final, static
2206 * primitive types (except for "char") or Strings. In a static block
2207 * after the field definitions/initializations, pass the class to
Romain Guyc4b11a72009-05-13 15:46:37 -07002208 * this method, Debug.setFieldsOn(). Example:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002209 * <pre>
2210 * package com.example;
2211 *
2212 * import android.os.Debug;
2213 *
2214 * public class MyDebugVars {
2215 * public static String s = "a string";
2216 * public static String s2 = "second string";
2217 * public static String ns = null;
2218 * public static boolean b = false;
2219 * public static int i = 5;
Romain Guyc4b11a72009-05-13 15:46:37 -07002220 * @Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002221 * public static float f = 0.1f;
Romain Guyc4b11a72009-05-13 15:46:37 -07002222 * @@Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002223 * public static double d = 0.5d;
2224 *
2225 * // This MUST appear AFTER all fields are defined and initialized!
2226 * static {
Romain Guyc4b11a72009-05-13 15:46:37 -07002227 * // Sets all the fields
Dave Borte9bfd9b2009-05-04 14:35:23 -07002228 * Debug.setFieldsOn(MyDebugVars.class);
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02002229 *
Romain Guyc4b11a72009-05-13 15:46:37 -07002230 * // Sets only the fields annotated with @Debug.DebugProperty
2231 * // Debug.setFieldsOn(MyDebugVars.class, true);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002232 * }
2233 * }
2234 * </pre>
Dave Borte9bfd9b2009-05-04 14:35:23 -07002235 * setFieldsOn() may override the value of any field in the class based
Dave Bort1ce5bd32009-04-22 17:36:56 -07002236 * on internal properties that are fixed at boot time.
2237 * <p>
2238 * These properties are only set during platform debugging, and are not
2239 * meant to be used as a general-purpose properties store.
2240 *
2241 * {@hide}
2242 *
2243 * @param cl The class to (possibly) modify
Romain Guyc4b11a72009-05-13 15:46:37 -07002244 * @param partial If false, sets all static fields, otherwise, only set
2245 * fields with the {@link android.os.Debug.DebugProperty}
2246 * annotation
Dave Bort1ce5bd32009-04-22 17:36:56 -07002247 * @throws IllegalArgumentException if any fields are final or non-static,
2248 * or if the type of the field does not match the type of
2249 * the internal debugging property value.
2250 */
Romain Guyc4b11a72009-05-13 15:46:37 -07002251 public static void setFieldsOn(Class<?> cl, boolean partial) {
Joe Onorato43a17652011-04-06 19:22:23 -07002252 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002253 if (debugProperties != null) {
2254 /* Only look for fields declared directly by the class,
2255 * so we don't mysteriously change static fields in superclasses.
2256 */
2257 for (Field field : cl.getDeclaredFields()) {
Romain Guyc4b11a72009-05-13 15:46:37 -07002258 if (!partial || field.getAnnotation(DebugProperty.class) != null) {
2259 final String propertyName = cl.getName() + "." + field.getName();
2260 boolean isStatic = Modifier.isStatic(field.getModifiers());
2261 boolean isFinal = Modifier.isFinal(field.getModifiers());
2262
2263 if (!isStatic || isFinal) {
2264 throw new IllegalArgumentException(propertyName +
2265 " must be static and non-final");
2266 }
2267 modifyFieldIfSet(field, debugProperties, propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002268 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002269 }
2270 }
2271 } else {
Dan Egnor3eda9792010-03-05 13:28:36 -08002272 Log.wtf(TAG,
Dave Borte9bfd9b2009-05-04 14:35:23 -07002273 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
Dave Bort1ce5bd32009-04-22 17:36:56 -07002274 ") called in non-DEBUG build");
2275 }
2276 }
Romain Guyc4b11a72009-05-13 15:46:37 -07002277
2278 /**
2279 * Annotation to put on fields you want to set with
2280 * {@link Debug#setFieldsOn(Class, boolean)}.
2281 *
2282 * @hide
2283 */
2284 @Target({ ElementType.FIELD })
2285 @Retention(RetentionPolicy.RUNTIME)
2286 public @interface DebugProperty {
2287 }
Dan Egnor3eda9792010-03-05 13:28:36 -08002288
2289 /**
2290 * Get a debugging dump of a system service by name.
2291 *
2292 * <p>Most services require the caller to hold android.permission.DUMP.
2293 *
2294 * @param name of the service to dump
2295 * @param fd to write dump output to (usually an output log file)
2296 * @param args to pass to the service's dump method, may be null
2297 * @return true if the service was dumped successfully, false if
2298 * the service could not be found or had an error while dumping
2299 */
2300 public static boolean dumpService(String name, FileDescriptor fd, String[] args) {
2301 IBinder service = ServiceManager.getService(name);
2302 if (service == null) {
2303 Log.e(TAG, "Can't find service to dump: " + name);
2304 return false;
2305 }
2306
2307 try {
2308 service.dump(fd, args);
2309 return true;
2310 } catch (RemoteException e) {
2311 Log.e(TAG, "Can't dump service: " + name, e);
2312 return false;
2313 }
2314 }
Craig Mautnera51a9562012-04-17 17:05:26 -07002315
2316 /**
Narayan Kamathf013daa2017-05-09 12:55:02 +01002317 * Append the Java stack traces of a given native process to a specified file.
2318 *
songjinshie02e3ea2016-12-16 17:48:21 +08002319 * @param pid pid to dump.
2320 * @param file path of file to append dump to.
2321 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002322 * @hide
2323 */
Narayan Kamathf013daa2017-05-09 12:55:02 +01002324 public static native boolean dumpJavaBacktraceToFileTimeout(int pid, String file,
2325 int timeoutSecs);
2326
2327 /**
2328 * Append the native stack traces of a given process to a specified file.
2329 *
2330 * @param pid pid to dump.
2331 * @param file path of file to append dump to.
2332 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
2333 * @hide
2334 */
2335 public static native boolean dumpNativeBacktraceToFileTimeout(int pid, String file,
2336 int timeoutSecs);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002337
2338 /**
Colin Crossc4fb5f92016-02-02 16:51:15 -08002339 * Get description of unreachable native memory.
2340 * @param limit the number of leaks to provide info on, 0 to only get a summary.
2341 * @param contents true to include a hex dump of the contents of unreachable memory.
2342 * @return the String containing a description of unreachable memory.
2343 * @hide */
2344 public static native String getUnreachableMemory(int limit, boolean contents);
2345
2346 /**
Craig Mautnera51a9562012-04-17 17:05:26 -07002347 * Return a String describing the calling method and location at a particular stack depth.
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07002348 * @param callStack the Thread stack
Craig Mautnera51a9562012-04-17 17:05:26 -07002349 * @param depth the depth of stack to return information for.
2350 * @return the String describing the caller at that depth.
2351 */
2352 private static String getCaller(StackTraceElement callStack[], int depth) {
2353 // callStack[4] is the caller of the method that called getCallers()
2354 if (4 + depth >= callStack.length) {
2355 return "<bottom of call stack>";
2356 }
2357 StackTraceElement caller = callStack[4 + depth];
2358 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2359 }
2360
2361 /**
2362 * Return a string consisting of methods and locations at multiple call stack levels.
2363 * @param depth the number of levels to return, starting with the immediate caller.
2364 * @return a string describing the call stack.
2365 * {@hide}
2366 */
2367 public static String getCallers(final int depth) {
2368 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2369 StringBuffer sb = new StringBuffer();
2370 for (int i = 0; i < depth; i++) {
2371 sb.append(getCaller(callStack, i)).append(" ");
2372 }
2373 return sb.toString();
2374 }
2375
2376 /**
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07002377 * Return a string consisting of methods and locations at multiple call stack levels.
2378 * @param depth the number of levels to return, starting with the immediate caller.
2379 * @return a string describing the call stack.
2380 * {@hide}
2381 */
2382 public static String getCallers(final int start, int depth) {
2383 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2384 StringBuffer sb = new StringBuffer();
2385 depth += start;
2386 for (int i = start; i < depth; i++) {
2387 sb.append(getCaller(callStack, i)).append(" ");
2388 }
2389 return sb.toString();
2390 }
2391
2392 /**
Dianne Hackbornef03a7f2012-10-29 18:46:52 -07002393 * Like {@link #getCallers(int)}, but each location is append to the string
2394 * as a new line with <var>linePrefix</var> in front of it.
2395 * @param depth the number of levels to return, starting with the immediate caller.
2396 * @param linePrefix prefix to put in front of each location.
2397 * @return a string describing the call stack.
2398 * {@hide}
2399 */
2400 public static String getCallers(final int depth, String linePrefix) {
2401 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2402 StringBuffer sb = new StringBuffer();
2403 for (int i = 0; i < depth; i++) {
2404 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n");
2405 }
2406 return sb.toString();
2407 }
2408
2409 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08002410 * @return a String describing the immediate caller of the calling method.
Craig Mautnera51a9562012-04-17 17:05:26 -07002411 * {@hide}
2412 */
2413 public static String getCaller() {
2414 return getCaller(Thread.currentThread().getStackTrace(), 0);
2415 }
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002416
2417 /**
Andreas Gampe571b7002018-01-16 15:11:29 -08002418 * Attach a library as a jvmti agent to the current runtime, with the given classloader
2419 * determining the library search path.
2420 * <p>
2421 * Note: agents may only be attached to debuggable apps. Otherwise, this function will
2422 * throw a SecurityException.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002423 *
Andreas Gampe571b7002018-01-16 15:11:29 -08002424 * @param library the library containing the agent.
2425 * @param options the options passed to the agent.
2426 * @param classLoader the classloader determining the library search path.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002427 *
Andreas Gampe571b7002018-01-16 15:11:29 -08002428 * @throws IOException if the agent could not be attached.
2429 * @throws SecurityException if the app is not debuggable.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002430 */
Andreas Gampe571b7002018-01-16 15:11:29 -08002431 public static void attachJvmtiAgent(@NonNull String library, @Nullable String options,
2432 @Nullable ClassLoader classLoader) throws IOException {
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002433 Preconditions.checkNotNull(library);
2434 Preconditions.checkArgument(!library.contains("="));
2435
2436 if (options == null) {
Andreas Gampe571b7002018-01-16 15:11:29 -08002437 VMDebug.attachAgent(library, classLoader);
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002438 } else {
Andreas Gampe571b7002018-01-16 15:11:29 -08002439 VMDebug.attachAgent(library + "=" + options, classLoader);
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002440 }
2441 }
Ben Murdochfdc55932019-01-30 10:43:15 +00002442
2443 /**
2444 * Return the current free ZRAM usage in kilobytes.
2445 *
2446 * @hide
2447 */
2448 public static native long getZramFreeKb();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449}