blob: 3d449446af826b3c37f2cb2c52f1e062aee5ab72 [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;
Artur Satayevafdb23a2019-12-10 17:47:53 +000022import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060023import android.content.Context;
Dave Bort1ce5bd32009-04-22 17:36:56 -070024import android.util.Log;
25
Narayan Kamathf013daa2017-05-09 12:55:02 +010026import com.android.internal.util.FastPrintWriter;
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -070027import com.android.internal.util.Preconditions;
Narayan Kamathf013daa2017-05-09 12:55:02 +010028import com.android.internal.util.TypedProperties;
29
Narayan Kamathf013daa2017-05-09 12:55:02 +010030import dalvik.system.VMDebug;
31
32import org.apache.harmony.dalvik.ddmc.Chunk;
33import org.apache.harmony.dalvik.ddmc.ChunkHandler;
34import org.apache.harmony.dalvik.ddmc.DdmServer;
35
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -060036import java.io.File;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070037import java.io.FileDescriptor;
Dave Bort1ce5bd32009-04-22 17:36:56 -070038import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.FileOutputStream;
Dave Bort1ce5bd32009-04-22 17:36:56 -070040import java.io.FileReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.io.PrintWriter;
Dave Bort1ce5bd32009-04-22 17:36:56 -070043import java.io.Reader;
Romain Guyc4b11a72009-05-13 15:46:37 -070044import java.lang.annotation.ElementType;
45import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
Narayan Kamathf013daa2017-05-09 12:55:02 +010047import java.lang.annotation.Target;
48import java.lang.reflect.Field;
49import java.lang.reflect.Modifier;
Richard Uhler350e6dc2015-05-18 10:48:52 -070050import java.util.HashMap;
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -070051import java.util.Map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054/**
Ian Rogersfe067a42013-02-22 19:59:23 -080055 * Provides various debugging methods for Android applications, including
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * tracing and allocation counts.
57 * <p><strong>Logging Trace Files</strong></p>
58 * <p>Debug can create log files that give details about an application, such as
59 * a call stack and start/stop times for any running methods. See <a
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -080060 * href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs with
61 * Traceview</a> for information about reading trace files. To start logging
62 * trace files, call one of the startMethodTracing() methods. To stop tracing,
63 * call {@link #stopMethodTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 */
65public final class Debug
66{
Dan Egnor3eda9792010-03-05 13:28:36 -080067 private static final String TAG = "Debug";
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 /**
70 * Flags for startMethodTracing(). These can be ORed together.
71 *
72 * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the
73 * trace key file.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080074 *
75 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -080077 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS;
79
80 /**
81 * Flags for printLoadedClasses(). Default behavior is to only show
82 * the class name.
83 */
84 public static final int SHOW_FULL_DETAIL = 1;
85 public static final int SHOW_CLASSLOADER = (1 << 1);
86 public static final int SHOW_INITIALIZED = (1 << 2);
87
88 // set/cleared by waitForDebugger()
89 private static volatile boolean mWaiting = false;
90
Andrei Onea24ec3212019-03-15 17:35:05 +000091 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 private Debug() {}
93
94 /*
95 * How long to wait for the debugger to finish sending requests. I've
96 * seen this hit 800msec on the device while waiting for a response
97 * to travel over USB and get processed, so we take that and add
98 * half a second.
99 */
100 private static final int MIN_DEBUGGER_IDLE = 1300; // msec
101
102 /* how long to sleep when polling for activity */
103 private static final int SPIN_DELAY = 200; // msec
104
105 /**
106 * Default trace file path and file
107 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String DEFAULT_TRACE_BODY = "dmtrace";
109 private static final String DEFAULT_TRACE_EXTENSION = ".trace";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111 /**
112 * This class is used to retrieved various statistics about the memory mappings for this
justinmuller64551382013-10-16 22:06:55 -0600113 * 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 -0800114 */
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700115 public static class MemoryInfo implements Parcelable {
Dianne Hackborn64770d12013-05-23 17:51:19 -0700116 /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 public int dalvikPss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700118 /** The proportional set size that is swappable for dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700119 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000120 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700121 public int dalvikSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800122 /** @hide The resident set size for dalvik heap. (Without other Dalvik overhead.) */
Andrei Onea24ec3212019-03-15 17:35:05 +0000123 @UnsupportedAppUsage
Dianne Hackborne17b4452018-01-10 13:15:40 -0800124 public int dalvikRss;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700125 /** The private dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public int dalvikPrivateDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700127 /** The shared dirty pages used by dalvik heap. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public int dalvikSharedDirty;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700129 /** The private clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700130 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000131 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700132 public int dalvikPrivateClean;
Dianne Hackborn64770d12013-05-23 17:51:19 -0700133 /** The shared clean pages used by dalvik heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700134 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000135 @UnsupportedAppUsage
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700136 public int dalvikSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700137 /** The dirty dalvik pages that have been swapped out. */
138 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000139 @UnsupportedAppUsage
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700140 public int dalvikSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800141 /** The dirty dalvik pages that have been swapped out, proportional. */
142 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000143 @UnsupportedAppUsage
Martijn Coenene0764852016-01-07 17:04:22 -0800144 public int dalvikSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 /** The proportional set size for the native heap. */
147 public int nativePss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700148 /** The proportional set size that is swappable for the native heap. */
149 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000150 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700151 public int nativeSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800152 /** @hide The resident set size for the native heap. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000153 @UnsupportedAppUsage
Dianne Hackborne17b4452018-01-10 13:15:40 -0800154 public int nativeRss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 /** The private dirty pages used by the native heap. */
156 public int nativePrivateDirty;
157 /** The shared dirty pages used by the native heap. */
158 public int nativeSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700159 /** The private clean pages used by the native heap. */
160 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000161 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700162 public int nativePrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700163 /** The shared clean pages used by the native heap. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700164 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000165 @UnsupportedAppUsage
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700166 public int nativeSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700167 /** The dirty native pages that have been swapped out. */
168 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000169 @UnsupportedAppUsage
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700170 public int nativeSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800171 /** The dirty native pages that have been swapped out, proportional. */
172 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000173 @UnsupportedAppUsage
Martijn Coenene0764852016-01-07 17:04:22 -0800174 public int nativeSwappedOutPss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
176 /** The proportional set size for everything else. */
177 public int otherPss;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700178 /** The proportional set size that is swappable for everything else. */
179 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000180 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700181 public int otherSwappablePss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800182 /** @hide The resident set size for everything else. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000183 @UnsupportedAppUsage
Dianne Hackborne17b4452018-01-10 13:15:40 -0800184 public int otherRss;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /** The private dirty pages used by everything else. */
186 public int otherPrivateDirty;
187 /** The shared dirty pages used by everything else. */
188 public int otherSharedDirty;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700189 /** The private clean pages used by everything else. */
190 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000191 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700192 public int otherPrivateClean;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700193 /** The shared clean pages used by everything else. */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700194 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000195 @UnsupportedAppUsage
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700196 public int otherSharedClean;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700197 /** The dirty pages used by anyting else that have been swapped out. */
198 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000199 @UnsupportedAppUsage
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700200 public int otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -0800201 /** The dirty pages used by anyting else that have been swapped out, proportional. */
202 /** @hide We may want to expose this, eventually. */
Andrei Onea24ec3212019-03-15 17:35:05 +0000203 @UnsupportedAppUsage
Martijn Coenene0764852016-01-07 17:04:22 -0800204 public int otherSwappedOutPss;
205
206 /** Whether the kernel reports proportional swap usage */
207 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000208 @UnsupportedAppUsage
Martijn Coenene0764852016-01-07 17:04:22 -0800209 public boolean hasSwappedOutPss;
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200210
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700211 /** @hide */
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700212 public static final int HEAP_UNKNOWN = 0;
213 /** @hide */
214 public static final int HEAP_DALVIK = 1;
215 /** @hide */
216 public static final int HEAP_NATIVE = 2;
217
218 /** @hide */
219 public static final int OTHER_DALVIK_OTHER = 0;
220 /** @hide */
221 public static final int OTHER_STACK = 1;
222 /** @hide */
223 public static final int OTHER_CURSOR = 2;
224 /** @hide */
225 public static final int OTHER_ASHMEM = 3;
226 /** @hide */
227 public static final int OTHER_GL_DEV = 4;
228 /** @hide */
229 public static final int OTHER_UNKNOWN_DEV = 5;
230 /** @hide */
231 public static final int OTHER_SO = 6;
232 /** @hide */
233 public static final int OTHER_JAR = 7;
234 /** @hide */
235 public static final int OTHER_APK = 8;
236 /** @hide */
237 public static final int OTHER_TTF = 9;
238 /** @hide */
239 public static final int OTHER_DEX = 10;
240 /** @hide */
241 public static final int OTHER_OAT = 11;
242 /** @hide */
243 public static final int OTHER_ART = 12;
244 /** @hide */
245 public static final int OTHER_UNKNOWN_MAP = 13;
246 /** @hide */
247 public static final int OTHER_GRAPHICS = 14;
248 /** @hide */
249 public static final int OTHER_GL = 15;
250 /** @hide */
251 public static final int OTHER_OTHER_MEMTRACK = 16;
252
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700253 // Needs to be declared here for the DVK_STAT ranges below.
254 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000255 @UnsupportedAppUsage
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700256 public static final int NUM_OTHER_STATS = 17;
257
258 // Dalvik subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700259 /** @hide */
260 public static final int OTHER_DALVIK_NORMAL = 17;
261 /** @hide */
262 public static final int OTHER_DALVIK_LARGE = 18;
263 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700264 public static final int OTHER_DALVIK_ZYGOTE = 19;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700265 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700266 public static final int OTHER_DALVIK_NON_MOVING = 20;
267 // Section begins and ends for dumpsys, relative to the DALVIK categories.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700268 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700269 public static final int OTHER_DVK_STAT_DALVIK_START =
270 OTHER_DALVIK_NORMAL - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700271 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700272 public static final int OTHER_DVK_STAT_DALVIK_END =
273 OTHER_DALVIK_NON_MOVING - NUM_OTHER_STATS;
274
275 // Dalvik Other subsections.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700276 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700277 public static final int OTHER_DALVIK_OTHER_LINEARALLOC = 21;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700278 /** @hide */
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700279 public static final int OTHER_DALVIK_OTHER_ACCOUNTING = 22;
280 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100281 public static final int OTHER_DALVIK_OTHER_ZYGOTE_CODE_CACHE = 23;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700282 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100283 public static final int OTHER_DALVIK_OTHER_APP_CODE_CACHE = 24;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700284 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100285 public static final int OTHER_DALVIK_OTHER_COMPILER_METADATA = 25;
286 /** @hide */
287 public static final int OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE = 26;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700288 /** @hide */
289 public static final int OTHER_DVK_STAT_DALVIK_OTHER_START =
290 OTHER_DALVIK_OTHER_LINEARALLOC - NUM_OTHER_STATS;
291 /** @hide */
292 public static final int OTHER_DVK_STAT_DALVIK_OTHER_END =
293 OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE - NUM_OTHER_STATS;
294
295 // Dex subsections (Boot vdex, App dex, and App vdex).
296 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100297 public static final int OTHER_DEX_BOOT_VDEX = 27;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700298 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100299 public static final int OTHER_DEX_APP_DEX = 28;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700300 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100301 public static final int OTHER_DEX_APP_VDEX = 29;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700302 /** @hide */
303 public static final int OTHER_DVK_STAT_DEX_START = OTHER_DEX_BOOT_VDEX - NUM_OTHER_STATS;
304 /** @hide */
305 public static final int OTHER_DVK_STAT_DEX_END = OTHER_DEX_APP_VDEX - NUM_OTHER_STATS;
306
307 // Art subsections (App image, boot image).
308 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100309 public static final int OTHER_ART_APP = 30;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700310 /** @hide */
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100311 public static final int OTHER_ART_BOOT = 31;
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700312 /** @hide */
313 public static final int OTHER_DVK_STAT_ART_START = OTHER_ART_APP - NUM_OTHER_STATS;
314 /** @hide */
315 public static final int OTHER_DVK_STAT_ART_END = OTHER_ART_BOOT - NUM_OTHER_STATS;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700316
317 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000318 @UnsupportedAppUsage
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100319 public static final int NUM_DVK_STATS = OTHER_ART_BOOT + 1 - OTHER_DALVIK_NORMAL;
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700320
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700321 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800322 public static final int NUM_CATEGORIES = 9;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700323
324 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800325 public static final int OFFSET_PSS = 0;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700326 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800327 public static final int OFFSET_SWAPPABLE_PSS = 1;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700328 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800329 public static final int OFFSET_RSS = 2;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700330 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800331 public static final int OFFSET_PRIVATE_DIRTY = 3;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700332 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800333 public static final int OFFSET_SHARED_DIRTY = 4;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700334 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800335 public static final int OFFSET_PRIVATE_CLEAN = 5;
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700336 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800337 public static final int OFFSET_SHARED_CLEAN = 6;
Martijn Coenene0764852016-01-07 17:04:22 -0800338 /** @hide */
Dianne Hackborne17b4452018-01-10 13:15:40 -0800339 public static final int OFFSET_SWAPPED_OUT = 7;
340 /** @hide */
341 public static final int OFFSET_SWAPPED_OUT_PSS = 8;
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700342
Andrei Onea24ec3212019-03-15 17:35:05 +0000343 @UnsupportedAppUsage
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700344 private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700345
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700346 public MemoryInfo() {
347 }
348
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700349 /**
Dianne Hackborn8c76d912018-08-23 15:20:05 -0700350 * @hide Copy contents from another object.
351 */
352 public void set(MemoryInfo other) {
353 dalvikPss = other.dalvikPss;
354 dalvikSwappablePss = other.dalvikSwappablePss;
355 dalvikRss = other.dalvikRss;
356 dalvikPrivateDirty = other.dalvikPrivateDirty;
357 dalvikSharedDirty = other.dalvikSharedDirty;
358 dalvikPrivateClean = other.dalvikPrivateClean;
359 dalvikSharedClean = other.dalvikSharedClean;
360 dalvikSwappedOut = other.dalvikSwappedOut;
361 dalvikSwappedOutPss = other.dalvikSwappedOutPss;
362
363 nativePss = other.nativePss;
364 nativeSwappablePss = other.nativeSwappablePss;
365 nativeRss = other.nativeRss;
366 nativePrivateDirty = other.nativePrivateDirty;
367 nativeSharedDirty = other.nativeSharedDirty;
368 nativePrivateClean = other.nativePrivateClean;
369 nativeSharedClean = other.nativeSharedClean;
370 nativeSwappedOut = other.nativeSwappedOut;
371 nativeSwappedOutPss = other.nativeSwappedOutPss;
372
373 otherPss = other.otherPss;
374 otherSwappablePss = other.otherSwappablePss;
375 otherRss = other.otherRss;
376 otherPrivateDirty = other.otherPrivateDirty;
377 otherSharedDirty = other.otherSharedDirty;
378 otherPrivateClean = other.otherPrivateClean;
379 otherSharedClean = other.otherSharedClean;
380 otherSwappedOut = other.otherSwappedOut;
381 otherSwappedOutPss = other.otherSwappedOutPss;
382
383 hasSwappedOutPss = other.hasSwappedOutPss;
384
385 System.arraycopy(other.otherStats, 0, otherStats, 0, otherStats.length);
386 }
387
388 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700389 * Return total PSS memory usage in kB.
390 */
391 public int getTotalPss() {
Martijn Coenene0764852016-01-07 17:04:22 -0800392 return dalvikPss + nativePss + otherPss + getTotalSwappedOutPss();
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700393 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200394
Dianne Hackbornc8230512013-07-13 21:32:12 -0700395 /**
396 * @hide Return total PSS memory usage in kB.
397 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000398 @UnsupportedAppUsage
Dianne Hackbornc8230512013-07-13 21:32:12 -0700399 public int getTotalUss() {
400 return dalvikPrivateClean + dalvikPrivateDirty
401 + nativePrivateClean + nativePrivateDirty
402 + otherPrivateClean + otherPrivateDirty;
403 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700404
405 /**
Martijn Coenene0764852016-01-07 17:04:22 -0800406 * Return total PSS memory usage in kB mapping a file of one of the following extension:
407 * .so, .jar, .apk, .ttf, .dex, .odex, .oat, .art .
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700408 */
409 public int getTotalSwappablePss() {
410 return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss;
411 }
412
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700413 /**
Dianne Hackborne17b4452018-01-10 13:15:40 -0800414 * @hide Return total RSS memory usage in kB.
415 */
416 public int getTotalRss() {
417 return dalvikRss + nativeRss + otherRss;
418 }
419
420 /**
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700421 * Return total private dirty memory usage in kB.
422 */
423 public int getTotalPrivateDirty() {
424 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty;
425 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200426
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -0700427 /**
428 * Return total shared dirty memory usage in kB.
429 */
430 public int getTotalSharedDirty() {
431 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty;
432 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200433
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700434 /**
435 * Return total shared clean memory usage in kB.
436 */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700437 public int getTotalPrivateClean() {
438 return dalvikPrivateClean + nativePrivateClean + otherPrivateClean;
439 }
440
441 /**
442 * Return total shared clean memory usage in kB.
443 */
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700444 public int getTotalSharedClean() {
445 return dalvikSharedClean + nativeSharedClean + otherSharedClean;
446 }
447
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700448 /**
449 * Return total swapped out memory in kB.
450 * @hide
451 */
452 public int getTotalSwappedOut() {
453 return dalvikSwappedOut + nativeSwappedOut + otherSwappedOut;
454 }
455
Martijn Coenene0764852016-01-07 17:04:22 -0800456 /**
457 * Return total swapped out memory in kB, proportional.
458 * @hide
459 */
460 public int getTotalSwappedOutPss() {
461 return dalvikSwappedOutPss + nativeSwappedOutPss + otherSwappedOutPss;
462 }
463
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700464 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000465 @UnsupportedAppUsage
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700466 public int getOtherPss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800467 return otherStats[which * NUM_CATEGORIES + OFFSET_PSS];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700468 }
469
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700470 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700471 public int getOtherSwappablePss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800472 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPABLE_PSS];
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700473 }
474
Dianne Hackborne17b4452018-01-10 13:15:40 -0800475 /** @hide */
476 public int getOtherRss(int which) {
477 return otherStats[which * NUM_CATEGORIES + OFFSET_RSS];
478 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700479
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700480 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000481 @UnsupportedAppUsage
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700482 public int getOtherPrivateDirty(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800483 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_DIRTY];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 }
485
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700486 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000487 @UnsupportedAppUsage
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700488 public int getOtherSharedDirty(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800489 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_DIRTY];
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700490 }
491
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700492 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700493 public int getOtherPrivateClean(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800494 return otherStats[which * NUM_CATEGORIES + OFFSET_PRIVATE_CLEAN];
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700495 }
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700496
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700497 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000498 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700499 public int getOtherPrivate(int which) {
500 return getOtherPrivateClean(which) + getOtherPrivateDirty(which);
501 }
502
503 /** @hide */
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700504 public int getOtherSharedClean(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800505 return otherStats[which * NUM_CATEGORIES + OFFSET_SHARED_CLEAN];
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700506 }
507
Dianne Hackborn3fa89692013-09-13 17:20:00 -0700508 /** @hide */
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700509 public int getOtherSwappedOut(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800510 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT];
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700511 }
512
513 /** @hide */
Martijn Coenene0764852016-01-07 17:04:22 -0800514 public int getOtherSwappedOutPss(int which) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800515 return otherStats[which * NUM_CATEGORIES + OFFSET_SWAPPED_OUT_PSS];
Martijn Coenene0764852016-01-07 17:04:22 -0800516 }
517
518 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000519 @UnsupportedAppUsage
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700520 public static String getOtherLabel(int which) {
521 switch (which) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700522 case OTHER_DALVIK_OTHER: return "Dalvik Other";
523 case OTHER_STACK: return "Stack";
524 case OTHER_CURSOR: return "Cursor";
525 case OTHER_ASHMEM: return "Ashmem";
526 case OTHER_GL_DEV: return "Gfx dev";
527 case OTHER_UNKNOWN_DEV: return "Other dev";
528 case OTHER_SO: return ".so mmap";
529 case OTHER_JAR: return ".jar mmap";
530 case OTHER_APK: return ".apk mmap";
531 case OTHER_TTF: return ".ttf mmap";
532 case OTHER_DEX: return ".dex mmap";
533 case OTHER_OAT: return ".oat mmap";
534 case OTHER_ART: return ".art mmap";
535 case OTHER_UNKNOWN_MAP: return "Other mmap";
536 case OTHER_GRAPHICS: return "EGL mtrack";
537 case OTHER_GL: return "GL mtrack";
538 case OTHER_OTHER_MEMTRACK: return "Other mtrack";
539 case OTHER_DALVIK_NORMAL: return ".Heap";
540 case OTHER_DALVIK_LARGE: return ".LOS";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700541 case OTHER_DALVIK_ZYGOTE: return ".Zygote";
542 case OTHER_DALVIK_NON_MOVING: return ".NonMoving";
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700543 case OTHER_DALVIK_OTHER_LINEARALLOC: return ".LinearAlloc";
544 case OTHER_DALVIK_OTHER_ACCOUNTING: return ".GC";
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100545 case OTHER_DALVIK_OTHER_ZYGOTE_CODE_CACHE: return ".ZygoteJIT";
546 case OTHER_DALVIK_OTHER_APP_CODE_CACHE: return ".AppJIT";
Mathieu Chartier95550dd2017-07-13 15:01:34 -0700547 case OTHER_DALVIK_OTHER_COMPILER_METADATA: return ".CompilerMetadata";
548 case OTHER_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE: return ".IndirectRef";
549 case OTHER_DEX_BOOT_VDEX: return ".Boot vdex";
550 case OTHER_DEX_APP_DEX: return ".App dex";
551 case OTHER_DEX_APP_VDEX: return ".App vdex";
552 case OTHER_ART_APP: return ".App art";
553 case OTHER_ART_BOOT: return ".Boot art";
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700554 default: return "????";
555 }
556 }
557
Richard Uhler350e6dc2015-05-18 10:48:52 -0700558 /**
559 * Returns the value of a particular memory statistic or {@code null} if no
560 * such memory statistic exists.
561 *
562 * <p>The following table lists the memory statistics that are supported.
563 * Note that memory statistics may be added or removed in a future API level.</p>
564 *
565 * <table>
566 * <thead>
567 * <tr>
568 * <th>Memory statistic name</th>
569 * <th>Meaning</th>
570 * <th>Example</th>
571 * <th>Supported (API Levels)</th>
572 * </tr>
573 * </thead>
574 * <tbody>
575 * <tr>
576 * <td>summary.java-heap</td>
577 * <td>The private Java Heap usage in kB. This corresponds to the Java Heap field
578 * in the App Summary section output by dumpsys meminfo.</td>
579 * <td>{@code 1442}</td>
580 * <td>23</td>
581 * </tr>
582 * <tr>
583 * <td>summary.native-heap</td>
584 * <td>The private Native Heap usage in kB. This corresponds to the Native Heap
585 * field in the App Summary section output by dumpsys meminfo.</td>
586 * <td>{@code 1442}</td>
587 * <td>23</td>
588 * </tr>
589 * <tr>
590 * <td>summary.code</td>
591 * <td>The memory usage for static code and resources in kB. This corresponds to
592 * the Code field in the App Summary section output by dumpsys meminfo.</td>
593 * <td>{@code 1442}</td>
594 * <td>23</td>
595 * </tr>
596 * <tr>
597 * <td>summary.stack</td>
598 * <td>The stack usage in kB. This corresponds to the Stack field in the
599 * App Summary section output by dumpsys meminfo.</td>
600 * <td>{@code 1442}</td>
601 * <td>23</td>
602 * </tr>
603 * <tr>
604 * <td>summary.graphics</td>
605 * <td>The graphics usage in kB. This corresponds to the Graphics field in the
606 * App Summary section output by dumpsys meminfo.</td>
607 * <td>{@code 1442}</td>
608 * <td>23</td>
609 * </tr>
610 * <tr>
611 * <td>summary.private-other</td>
612 * <td>Other private memory usage in kB. This corresponds to the Private Other
613 * field output in the App Summary section by dumpsys meminfo.</td>
614 * <td>{@code 1442}</td>
615 * <td>23</td>
616 * </tr>
617 * <tr>
618 * <td>summary.system</td>
619 * <td>Shared and system memory usage in kB. This corresponds to the System
620 * field output in the App Summary section by dumpsys meminfo.</td>
621 * <td>{@code 1442}</td>
622 * <td>23</td>
623 * </tr>
624 * <tr>
625 * <td>summary.total-pss</td>
626 * <td>Total PPS memory usage in kB.</td>
627 * <td>{@code 1442}</td>
628 * <td>23</td>
629 * </tr>
630 * <tr>
631 * <td>summary.total-swap</td>
632 * <td>Total swap usage in kB.</td>
633 * <td>{@code 1442}</td>
634 * <td>23</td>
635 * </tr>
636 * </tbody>
637 * </table>
638 */
Dianne Hackbornb02ce292015-10-12 15:14:16 -0700639 public String getMemoryStat(String statName) {
Richard Uhler350e6dc2015-05-18 10:48:52 -0700640 switch(statName) {
641 case "summary.java-heap":
642 return Integer.toString(getSummaryJavaHeap());
643 case "summary.native-heap":
644 return Integer.toString(getSummaryNativeHeap());
645 case "summary.code":
646 return Integer.toString(getSummaryCode());
647 case "summary.stack":
648 return Integer.toString(getSummaryStack());
649 case "summary.graphics":
650 return Integer.toString(getSummaryGraphics());
651 case "summary.private-other":
652 return Integer.toString(getSummaryPrivateOther());
653 case "summary.system":
654 return Integer.toString(getSummarySystem());
655 case "summary.total-pss":
656 return Integer.toString(getSummaryTotalPss());
657 case "summary.total-swap":
658 return Integer.toString(getSummaryTotalSwap());
659 default:
660 return null;
661 }
662 }
663
664 /**
665 * Returns a map of the names/values of the memory statistics
666 * that {@link #getMemoryStat(String)} supports.
667 *
668 * @return a map of the names/values of the supported memory statistics.
669 */
670 public Map<String, String> getMemoryStats() {
671 Map<String, String> stats = new HashMap<String, String>();
672 stats.put("summary.java-heap", Integer.toString(getSummaryJavaHeap()));
673 stats.put("summary.native-heap", Integer.toString(getSummaryNativeHeap()));
674 stats.put("summary.code", Integer.toString(getSummaryCode()));
675 stats.put("summary.stack", Integer.toString(getSummaryStack()));
676 stats.put("summary.graphics", Integer.toString(getSummaryGraphics()));
677 stats.put("summary.private-other", Integer.toString(getSummaryPrivateOther()));
678 stats.put("summary.system", Integer.toString(getSummarySystem()));
679 stats.put("summary.total-pss", Integer.toString(getSummaryTotalPss()));
680 stats.put("summary.total-swap", Integer.toString(getSummaryTotalSwap()));
681 return stats;
682 }
683
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700684 /**
685 * Pss of Java Heap bytes in KB due to the application.
686 * Notes:
687 * * OTHER_ART is the boot image. Anything private here is blamed on
688 * the application, not the system.
689 * * dalvikPrivateDirty includes private zygote, which means the
690 * application dirtied something allocated by the zygote. We blame
691 * the application for that memory, not the system.
692 * * Does not include OTHER_DALVIK_OTHER, which is considered VM
693 * Overhead and lumped into Private Other.
694 * * We don't include dalvikPrivateClean, because there should be no
695 * such thing as private clean for the Java Heap.
696 * @hide
697 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000698 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700699 public int getSummaryJavaHeap() {
700 return dalvikPrivateDirty + getOtherPrivate(OTHER_ART);
701 }
702
703 /**
704 * Pss of Native Heap bytes in KB due to the application.
705 * Notes:
706 * * Includes private dirty malloc space.
707 * * We don't include nativePrivateClean, because there should be no
708 * such thing as private clean for the Native Heap.
709 * @hide
710 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000711 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700712 public int getSummaryNativeHeap() {
713 return nativePrivateDirty;
714 }
715
716 /**
717 * Pss of code and other static resource bytes in KB due to
718 * the application.
719 * @hide
720 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000721 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700722 public int getSummaryCode() {
723 return getOtherPrivate(OTHER_SO)
724 + getOtherPrivate(OTHER_JAR)
725 + getOtherPrivate(OTHER_APK)
726 + getOtherPrivate(OTHER_TTF)
727 + getOtherPrivate(OTHER_DEX)
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100728 + getOtherPrivate(OTHER_OAT)
729 + getOtherPrivate(OTHER_DALVIK_OTHER_ZYGOTE_CODE_CACHE)
730 + getOtherPrivate(OTHER_DALVIK_OTHER_APP_CODE_CACHE);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700731 }
732
733 /**
734 * Pss in KB of the stack due to the application.
735 * Notes:
736 * * Includes private dirty stack, which includes both Java and Native
737 * stack.
738 * * Does not include private clean stack, because there should be no
739 * such thing as private clean for the stack.
740 * @hide
741 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000742 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700743 public int getSummaryStack() {
744 return getOtherPrivateDirty(OTHER_STACK);
745 }
746
747 /**
748 * Pss in KB of graphics due to the application.
749 * Notes:
750 * * Includes private Gfx, EGL, and GL.
751 * * Warning: These numbers can be misreported by the graphics drivers.
752 * * We don't include shared graphics. It may make sense to, because
753 * shared graphics are likely buffers due to the application
754 * anyway, but it's simpler to implement to just group all shared
755 * memory into the System category.
756 * @hide
757 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000758 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700759 public int getSummaryGraphics() {
760 return getOtherPrivate(OTHER_GL_DEV)
761 + getOtherPrivate(OTHER_GRAPHICS)
762 + getOtherPrivate(OTHER_GL);
763 }
764
765 /**
766 * Pss in KB due to the application that haven't otherwise been
767 * accounted for.
768 * @hide
769 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000770 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700771 public int getSummaryPrivateOther() {
772 return getTotalPrivateClean()
773 + getTotalPrivateDirty()
774 - getSummaryJavaHeap()
775 - getSummaryNativeHeap()
776 - getSummaryCode()
777 - getSummaryStack()
778 - getSummaryGraphics();
779 }
780
781 /**
782 * Pss in KB due to the system.
783 * Notes:
784 * * Includes all shared memory.
785 * @hide
786 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000787 @UnsupportedAppUsage
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700788 public int getSummarySystem() {
789 return getTotalPss()
790 - getTotalPrivateClean()
791 - getTotalPrivateDirty();
792 }
793
794 /**
Jing Jia736da82019-08-06 15:08:02 -0700795 * Rss of Java Heap bytes in KB due to the application.
796 * @hide
797 */
798 public int getSummaryJavaHeapRss() {
799 return dalvikRss + getOtherRss(OTHER_ART);
800 }
801
802 /**
803 * Rss of Native Heap bytes in KB due to the application.
804 * @hide
805 */
806 public int getSummaryNativeHeapRss() {
807 return nativeRss;
808 }
809
810 /**
811 * Rss of code and other static resource bytes in KB due to
812 * the application.
813 * @hide
814 */
815 public int getSummaryCodeRss() {
816 return getOtherRss(OTHER_SO)
817 + getOtherRss(OTHER_JAR)
818 + getOtherRss(OTHER_APK)
819 + getOtherRss(OTHER_TTF)
820 + getOtherRss(OTHER_DEX)
Nicolas Geoffray08fcd7582019-10-02 21:11:43 +0100821 + getOtherRss(OTHER_OAT)
822 + getOtherRss(OTHER_DALVIK_OTHER_ZYGOTE_CODE_CACHE)
823 + getOtherRss(OTHER_DALVIK_OTHER_APP_CODE_CACHE);
Jing Jia736da82019-08-06 15:08:02 -0700824 }
825
826 /**
827 * Rss in KB of the stack due to the application.
828 * @hide
829 */
830 public int getSummaryStackRss() {
831 return getOtherRss(OTHER_STACK);
832 }
833
834 /**
835 * Rss in KB of graphics due to the application.
836 * @hide
837 */
838 public int getSummaryGraphicsRss() {
839 return getOtherRss(OTHER_GL_DEV)
840 + getOtherRss(OTHER_GRAPHICS)
841 + getOtherRss(OTHER_GL);
842 }
843
844 /**
845 * Rss in KB due to either the application or system that haven't otherwise been
846 * accounted for.
847 * @hide
848 */
849 public int getSummaryUnknownRss() {
850 return getTotalRss()
851 - getSummaryJavaHeapRss()
852 - getSummaryNativeHeapRss()
853 - getSummaryCodeRss()
854 - getSummaryStackRss()
855 - getSummaryGraphicsRss();
856 }
857
858 /**
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700859 * Total Pss in KB.
860 * @hide
861 */
862 public int getSummaryTotalPss() {
863 return getTotalPss();
864 }
865
866 /**
867 * Total Swap in KB.
868 * Notes:
869 * * Some of this memory belongs in other categories, but we don't
870 * know if the Swap memory is shared or private, so we don't know
871 * what to blame on the application and what on the system.
872 * For now, just lump all the Swap in one place.
Martijn Coenene0764852016-01-07 17:04:22 -0800873 * For kernels reporting SwapPss {@link #getSummaryTotalSwapPss()}
874 * will report the application proportional Swap.
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700875 * @hide
876 */
877 public int getSummaryTotalSwap() {
878 return getTotalSwappedOut();
879 }
880
Martijn Coenene0764852016-01-07 17:04:22 -0800881 /**
882 * Total proportional Swap in KB.
883 * Notes:
884 * * Always 0 if {@link #hasSwappedOutPss} is false.
885 * @hide
886 */
887 public int getSummaryTotalSwapPss() {
888 return getTotalSwappedOutPss();
889 }
890
Dianne Hackbornef0a4022016-05-11 14:21:07 -0700891 /**
892 * Return true if the kernel is reporting pss swapped out... that is, if
893 * {@link #getSummaryTotalSwapPss()} will return non-0 values.
894 * @hide
895 */
896 public boolean hasSwappedOutPss() {
897 return hasSwappedOutPss;
898 }
899
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700900 public int describeContents() {
901 return 0;
902 }
903
904 public void writeToParcel(Parcel dest, int flags) {
905 dest.writeInt(dalvikPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700906 dest.writeInt(dalvikSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800907 dest.writeInt(dalvikRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700908 dest.writeInt(dalvikPrivateDirty);
909 dest.writeInt(dalvikSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700910 dest.writeInt(dalvikPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700911 dest.writeInt(dalvikSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700912 dest.writeInt(dalvikSwappedOut);
Richard Uhler91702eb32017-06-23 16:54:25 +0100913 dest.writeInt(dalvikSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700914 dest.writeInt(nativePss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700915 dest.writeInt(nativeSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800916 dest.writeInt(nativeRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700917 dest.writeInt(nativePrivateDirty);
918 dest.writeInt(nativeSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700919 dest.writeInt(nativePrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700920 dest.writeInt(nativeSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700921 dest.writeInt(nativeSwappedOut);
Richard Uhler91702eb32017-06-23 16:54:25 +0100922 dest.writeInt(nativeSwappedOutPss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700923 dest.writeInt(otherPss);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700924 dest.writeInt(otherSwappablePss);
Dianne Hackborne17b4452018-01-10 13:15:40 -0800925 dest.writeInt(otherRss);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700926 dest.writeInt(otherPrivateDirty);
927 dest.writeInt(otherSharedDirty);
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700928 dest.writeInt(otherPrivateClean);
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700929 dest.writeInt(otherSharedClean);
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700930 dest.writeInt(otherSwappedOut);
Martijn Coenene0764852016-01-07 17:04:22 -0800931 dest.writeInt(hasSwappedOutPss ? 1 : 0);
932 dest.writeInt(otherSwappedOutPss);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700933 dest.writeIntArray(otherStats);
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700934 }
935
936 public void readFromParcel(Parcel source) {
937 dalvikPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700938 dalvikSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800939 dalvikRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700940 dalvikPrivateDirty = source.readInt();
941 dalvikSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700942 dalvikPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700943 dalvikSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700944 dalvikSwappedOut = source.readInt();
Richard Uhler91702eb32017-06-23 16:54:25 +0100945 dalvikSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700946 nativePss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700947 nativeSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800948 nativeRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700949 nativePrivateDirty = source.readInt();
950 nativeSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700951 nativePrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700952 nativeSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700953 nativeSwappedOut = source.readInt();
Richard Uhler91702eb32017-06-23 16:54:25 +0100954 nativeSwappedOutPss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700955 otherPss = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700956 otherSwappablePss = source.readInt();
Dianne Hackborne17b4452018-01-10 13:15:40 -0800957 otherRss = source.readInt();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700958 otherPrivateDirty = source.readInt();
959 otherSharedDirty = source.readInt();
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700960 otherPrivateClean = source.readInt();
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700961 otherSharedClean = source.readInt();
Dianne Hackborn8883ced2013-10-02 16:58:06 -0700962 otherSwappedOut = source.readInt();
Martijn Coenene0764852016-01-07 17:04:22 -0800963 hasSwappedOutPss = source.readInt() != 0;
964 otherSwappedOutPss = source.readInt();
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700965 otherStats = source.createIntArray();
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700966 }
Christian Mehlmauer798e2d32010-06-17 18:24:07 +0200967
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700968 public static final @android.annotation.NonNull Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() {
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700969 public MemoryInfo createFromParcel(Parcel source) {
970 return new MemoryInfo(source);
971 }
972 public MemoryInfo[] newArray(int size) {
973 return new MemoryInfo[size];
974 }
975 };
976
977 private MemoryInfo(Parcel source) {
978 readFromParcel(source);
979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981
982
983 /**
984 * Wait until a debugger attaches. As soon as the debugger attaches,
985 * this returns, so you will need to place a breakpoint after the
986 * waitForDebugger() call if you want to start tracing immediately.
987 */
988 public static void waitForDebugger() {
989 if (!VMDebug.isDebuggingEnabled()) {
990 //System.out.println("debugging not enabled, not waiting");
991 return;
992 }
993 if (isDebuggerConnected())
994 return;
995
996 // if DDMS is listening, inform them of our plight
997 System.out.println("Sending WAIT chunk");
998 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger"
999 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1);
1000 DdmServer.sendChunk(waitChunk);
1001
1002 mWaiting = true;
1003 while (!isDebuggerConnected()) {
1004 try { Thread.sleep(SPIN_DELAY); }
1005 catch (InterruptedException ie) {}
1006 }
1007 mWaiting = false;
1008
1009 System.out.println("Debugger has connected");
1010
1011 /*
1012 * There is no "ready to go" signal from the debugger, and we're
1013 * not allowed to suspend ourselves -- the debugger expects us to
1014 * be running happily, and gets confused if we aren't. We need to
1015 * allow the debugger a chance to set breakpoints before we start
1016 * running again.
1017 *
1018 * Sit and spin until the debugger has been idle for a short while.
1019 */
1020 while (true) {
1021 long delta = VMDebug.lastDebuggerActivity();
1022 if (delta < 0) {
1023 System.out.println("debugger detached?");
1024 break;
1025 }
1026
1027 if (delta < MIN_DEBUGGER_IDLE) {
1028 System.out.println("waiting for debugger to settle...");
1029 try { Thread.sleep(SPIN_DELAY); }
1030 catch (InterruptedException ie) {}
1031 } else {
1032 System.out.println("debugger has settled (" + delta + ")");
1033 break;
1034 }
1035 }
1036 }
1037
1038 /**
1039 * Returns "true" if one or more threads is waiting for a debugger
1040 * to attach.
1041 */
1042 public static boolean waitingForDebugger() {
1043 return mWaiting;
1044 }
1045
1046 /**
1047 * Determine if a debugger is currently attached.
1048 */
1049 public static boolean isDebuggerConnected() {
1050 return VMDebug.isDebuggerConnected();
1051 }
1052
1053 /**
Andy McFaddene5772322010-01-22 07:23:31 -08001054 * Returns an array of strings that identify VM features. This is
1055 * used by DDMS to determine what sorts of operations the VM can
1056 * perform.
1057 *
1058 * @hide
1059 */
1060 public static String[] getVmFeatureList() {
1061 return VMDebug.getVmFeatureList();
1062 }
1063
1064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 * Change the JDWP port.
1066 *
1067 * @deprecated no longer needed or useful
1068 */
1069 @Deprecated
1070 public static void changeDebugPort(int port) {}
1071
1072 /**
1073 * This is the pathname to the sysfs file that enables and disables
1074 * tracing on the qemu emulator.
1075 */
1076 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state";
1077
1078 /**
1079 * Enable qemu tracing. For this to work requires running everything inside
1080 * the qemu emulator; otherwise, this method will have no effect. The trace
1081 * file is specified on the command line when the emulator is started. For
1082 * example, the following command line <br />
1083 * <code>emulator -trace foo</code><br />
1084 * will start running the emulator and create a trace file named "foo". This
1085 * method simply enables writing the trace records to the trace file.
1086 *
1087 * <p>
1088 * The main differences between this and {@link #startMethodTracing()} are
1089 * that tracing in the qemu emulator traces every cpu instruction of every
1090 * process, including kernel code, so we have more complete information,
1091 * including all context switches. We can also get more detailed information
1092 * such as cache misses. The sequence of calls is determined by
1093 * post-processing the instruction trace. The qemu tracing is also done
1094 * without modifying the application or perturbing the timing of calls
1095 * because no instrumentation is added to the application being traced.
1096 * </p>
1097 *
1098 * <p>
1099 * One limitation of using this method compared to using
1100 * {@link #startMethodTracing()} on the real device is that the emulator
1101 * does not model all of the real hardware effects such as memory and
1102 * bus contention. The emulator also has a simple cache model and cannot
1103 * capture all the complexities of a real cache.
1104 * </p>
1105 */
1106 public static void startNativeTracing() {
1107 // Open the sysfs file for writing and write "1" to it.
1108 PrintWriter outStream = null;
1109 try {
1110 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001111 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 outStream.println("1");
1113 } catch (Exception e) {
1114 } finally {
1115 if (outStream != null)
1116 outStream.close();
1117 }
1118
1119 VMDebug.startEmulatorTracing();
1120 }
1121
1122 /**
1123 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing.
1124 *
1125 * <p>Tracing can be started and stopped as many times as desired. When
1126 * the qemu emulator itself is stopped then the buffered trace records
1127 * are flushed and written to the trace file. In fact, it is not necessary
1128 * to call this method at all; simply killing qemu is sufficient. But
1129 * starting and stopping a trace is useful for examining a specific
1130 * region of code.</p>
1131 */
1132 public static void stopNativeTracing() {
1133 VMDebug.stopEmulatorTracing();
1134
1135 // Open the sysfs file for writing and write "0" to it.
1136 PrintWriter outStream = null;
1137 try {
1138 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001139 outStream = new FastPrintWriter(fos);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 outStream.println("0");
1141 } catch (Exception e) {
1142 // We could print an error message here but we probably want
1143 // to quietly ignore errors if we are not running in the emulator.
1144 } finally {
1145 if (outStream != null)
1146 outStream.close();
1147 }
1148 }
1149
1150 /**
1151 * Enable "emulator traces", in which information about the current
1152 * method is made available to the "emulator -trace" feature. There
1153 * is no corresponding "disable" call -- this is intended for use by
1154 * the framework when tracing should be turned on and left that way, so
1155 * that traces captured with F9/F10 will include the necessary data.
1156 *
1157 * This puts the VM into "profile" mode, which has performance
1158 * consequences.
1159 *
1160 * To temporarily enable tracing, use {@link #startNativeTracing()}.
1161 */
1162 public static void enableEmulatorTraceOutput() {
1163 VMDebug.startEmulatorTracing();
1164 }
1165
1166 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001167 * Start method tracing with default log name and buffer size.
1168 * <p>
1169 * By default, the trace file is called "dmtrace.trace" and it's placed
1170 * under your package-specific directory on primary shared/external storage,
1171 * as returned by {@link Context#getExternalFilesDir(String)}.
1172 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001173 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1174 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001175 * <p class="note">
1176 * When method tracing is enabled, the VM will run more slowly than usual,
1177 * so the timings from the trace files should only be considered in relative
1178 * terms (e.g. was run #1 faster than run #2). The times for native methods
1179 * will not change, so don't try to use this to compare the performance of
1180 * interpreted and native implementations of the same method. As an
1181 * alternative, consider using sampling-based method tracing via
1182 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1183 * in the emulator via {@link #startNativeTracing()}.
1184 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 */
1186 public static void startMethodTracing() {
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001187 VMDebug.startMethodTracing(fixTracePath(null), 0, 0, false, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189
1190 /**
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001191 * Start method tracing, specifying the trace log file path.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 * <p>
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001193 * When a relative file path is given, the trace file will be placed under
1194 * your package-specific directory on primary shared/external storage, as
1195 * returned by {@link Context#getExternalFilesDir(String)}.
1196 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001197 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1198 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001199 * <p class="note">
1200 * When method tracing is enabled, the VM will run more slowly than usual,
1201 * so the timings from the trace files should only be considered in relative
1202 * terms (e.g. was run #1 faster than run #2). The times for native methods
1203 * will not change, so don't try to use this to compare the performance of
1204 * interpreted and native implementations of the same method. As an
1205 * alternative, consider using sampling-based method tracing via
1206 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1207 * in the emulator via {@link #startNativeTracing()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 * </p>
1209 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001210 * @param tracePath Path to the trace log file to create. If {@code null},
1211 * this will default to "dmtrace.trace". If the file already
1212 * exists, it will be truncated. If the path given does not end
1213 * in ".trace", it will be appended for you.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001215 public static void startMethodTracing(String tracePath) {
1216 startMethodTracing(tracePath, 0, 0);
1217 }
1218
1219 /**
1220 * Start method tracing, specifying the trace log file name and the buffer
1221 * size.
1222 * <p>
1223 * When a relative file path is given, the trace file will be placed under
1224 * your package-specific directory on primary shared/external storage, as
1225 * returned by {@link Context#getExternalFilesDir(String)}.
1226 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001227 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1228 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001229 * <p class="note">
1230 * When method tracing is enabled, the VM will run more slowly than usual,
1231 * so the timings from the trace files should only be considered in relative
1232 * terms (e.g. was run #1 faster than run #2). The times for native methods
1233 * will not change, so don't try to use this to compare the performance of
1234 * interpreted and native implementations of the same method. As an
1235 * alternative, consider using sampling-based method tracing via
1236 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1237 * in the emulator via {@link #startNativeTracing()}.
1238 * </p>
1239 *
1240 * @param tracePath Path to the trace log file to create. If {@code null},
1241 * this will default to "dmtrace.trace". If the file already
1242 * exists, it will be truncated. If the path given does not end
1243 * in ".trace", it will be appended for you.
1244 * @param bufferSize The maximum amount of trace data we gather. If not
1245 * given, it defaults to 8MB.
1246 */
1247 public static void startMethodTracing(String tracePath, int bufferSize) {
1248 startMethodTracing(tracePath, bufferSize, 0);
1249 }
1250
1251 /**
1252 * Start method tracing, specifying the trace log file name, the buffer
1253 * size, and flags.
1254 * <p>
1255 * When a relative file path is given, the trace file will be placed under
1256 * your package-specific directory on primary shared/external storage, as
1257 * returned by {@link Context#getExternalFilesDir(String)}.
1258 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001259 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1260 * with Traceview</a> for information about reading trace files.
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001261 * <p class="note">
1262 * When method tracing is enabled, the VM will run more slowly than usual,
1263 * so the timings from the trace files should only be considered in relative
1264 * terms (e.g. was run #1 faster than run #2). The times for native methods
1265 * will not change, so don't try to use this to compare the performance of
1266 * interpreted and native implementations of the same method. As an
1267 * alternative, consider using sampling-based method tracing via
1268 * {@link #startMethodTracingSampling(String, int, int)} or "native" tracing
1269 * in the emulator via {@link #startNativeTracing()}.
1270 * </p>
1271 *
1272 * @param tracePath Path to the trace log file to create. If {@code null},
1273 * this will default to "dmtrace.trace". If the file already
1274 * exists, it will be truncated. If the path given does not end
1275 * in ".trace", it will be appended for you.
1276 * @param bufferSize The maximum amount of trace data we gather. If not
1277 * given, it defaults to 8MB.
1278 * @param flags Flags to control method tracing. The only one that is
1279 * currently defined is {@link #TRACE_COUNT_ALLOCS}.
1280 */
1281 public static void startMethodTracing(String tracePath, int bufferSize, int flags) {
1282 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, flags, false, 0);
Jeff Haod02e60f2014-01-06 15:52:52 -08001283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284
Jeff Haod02e60f2014-01-06 15:52:52 -08001285 /**
1286 * Start sampling-based method tracing, specifying the trace log file name,
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001287 * the buffer size, and the sampling interval.
1288 * <p>
1289 * When a relative file path is given, the trace file will be placed under
1290 * your package-specific directory on primary shared/external storage, as
1291 * returned by {@link Context#getExternalFilesDir(String)}.
1292 * <p>
Ricardo Loo Forondaac750a82018-01-25 09:10:47 -08001293 * See <a href="{@docRoot}studio/profile/traceview.html">Inspect Trace Logs
1294 * with Traceview</a> for information about reading trace files.
Jeff Haod02e60f2014-01-06 15:52:52 -08001295 *
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001296 * @param tracePath Path to the trace log file to create. If {@code null},
1297 * this will default to "dmtrace.trace". If the file already
1298 * exists, it will be truncated. If the path given does not end
1299 * in ".trace", it will be appended for you.
1300 * @param bufferSize The maximum amount of trace data we gather. If not
1301 * given, it defaults to 8MB.
1302 * @param intervalUs The amount of time between each sample in microseconds.
Jeff Haod02e60f2014-01-06 15:52:52 -08001303 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001304 public static void startMethodTracingSampling(String tracePath, int bufferSize,
1305 int intervalUs) {
1306 VMDebug.startMethodTracing(fixTracePath(tracePath), bufferSize, 0, true, intervalUs);
Jeff Haod02e60f2014-01-06 15:52:52 -08001307 }
Kweku Adams983829f2017-12-06 14:53:50 -08001308
Jeff Haod02e60f2014-01-06 15:52:52 -08001309 /**
1310 * Formats name of trace log file for method tracing.
1311 */
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001312 private static String fixTracePath(String tracePath) {
1313 if (tracePath == null || tracePath.charAt(0) != '/') {
1314 final Context context = AppGlobals.getInitialApplication();
1315 final File dir;
1316 if (context != null) {
1317 dir = context.getExternalFilesDir(null);
1318 } else {
1319 dir = Environment.getExternalStorageDirectory();
1320 }
Jeff Haod02e60f2014-01-06 15:52:52 -08001321
Jeff Sharkey3a6e0ec2016-03-21 16:42:57 -06001322 if (tracePath == null) {
1323 tracePath = new File(dir, DEFAULT_TRACE_BODY).getAbsolutePath();
1324 } else {
1325 tracePath = new File(dir, tracePath).getAbsolutePath();
1326 }
1327 }
1328 if (!tracePath.endsWith(DEFAULT_TRACE_EXTENSION)) {
1329 tracePath += DEFAULT_TRACE_EXTENSION;
1330 }
1331 return tracePath;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 }
1333
1334 /**
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001335 * Like startMethodTracing(String, int, int), but taking an already-opened
1336 * FileDescriptor in which the trace is written. The file name is also
1337 * supplied simply for logging. Makes a dup of the file descriptor.
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02001338 *
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001339 * Not exposed in the SDK unless we are really comfortable with supporting
1340 * this and find it would be useful.
1341 * @hide
1342 */
1343 public static void startMethodTracing(String traceName, FileDescriptor fd,
Shukang Zhou6ec0b7e2017-01-24 15:30:29 -08001344 int bufferSize, int flags, boolean streamOutput) {
1345 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags, false, 0, streamOutput);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001346 }
1347
1348 /**
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001349 * Starts method tracing without a backing file. When stopMethodTracing
1350 * is called, the result is sent directly to DDMS. (If DDMS is not
1351 * attached when tracing ends, the profiling data will be discarded.)
1352 *
1353 * @hide
1354 */
Jeff Hao7be3a132013-08-22 15:53:12 -07001355 public static void startMethodTracingDdms(int bufferSize, int flags,
1356 boolean samplingEnabled, int intervalUs) {
1357 VMDebug.startMethodTracingDdms(bufferSize, flags, samplingEnabled, intervalUs);
Andy McFadden72a20db0c2010-01-22 12:20:41 -08001358 }
1359
1360 /**
Jeff Haoac277052013-08-29 11:19:39 -07001361 * Determine whether method tracing is currently active and what type is
1362 * active.
1363 *
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001364 * @hide
1365 */
Jeff Haoac277052013-08-29 11:19:39 -07001366 public static int getMethodTracingMode() {
1367 return VMDebug.getMethodTracingMode();
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -07001368 }
1369
1370 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 * Stop method tracing.
1372 */
1373 public static void stopMethodTracing() {
1374 VMDebug.stopMethodTracing();
1375 }
1376
1377 /**
1378 * Get an indication of thread CPU usage. The value returned
1379 * indicates the amount of time that the current thread has spent
1380 * executing code or waiting for certain types of I/O.
1381 *
1382 * The time is expressed in nanoseconds, and is only meaningful
1383 * when compared to the result from an earlier call. Note that
1384 * nanosecond resolution does not imply nanosecond accuracy.
1385 *
1386 * On system which don't support this operation, the call returns -1.
1387 */
1388 public static long threadCpuTimeNanos() {
1389 return VMDebug.threadCpuTimeNanos();
1390 }
1391
1392 /**
Chet Haase2970c492010-11-09 13:58:04 -08001393 * Start counting the number and aggregate size of memory allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001395 * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting.
1396 * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis
1397 * code doesn't cause additional allocations. The various <code>get</code> methods return
1398 * the specified value. And the various <code>reset</code> methods reset the specified
Chet Haase2970c492010-11-09 13:58:04 -08001399 * count.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 *
Ian Rogersfe067a42013-02-22 19:59:23 -08001401 * <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 -08001402 * The per-thread counts for threads other than the current thread
Chet Haase2970c492010-11-09 13:58:04 -08001403 * are not cleared by the "reset" or "start" calls.</p>
Ian Rogersfe067a42013-02-22 19:59:23 -08001404 *
1405 * @deprecated Accurate counting is a burden on the runtime and may be removed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 */
Ian Rogersfe067a42013-02-22 19:59:23 -08001407 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 public static void startAllocCounting() {
1409 VMDebug.startAllocCounting();
1410 }
Chet Haase2970c492010-11-09 13:58:04 -08001411
1412 /**
1413 * Stop counting the number and aggregate size of memory allocations.
1414 *
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001415 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Chet Haase2970c492010-11-09 13:58:04 -08001416 */
Ian Rogersc2a3adb2013-04-19 11:31:48 -07001417 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 public static void stopAllocCounting() {
1419 VMDebug.stopAllocCounting();
1420 }
1421
Ian Rogersfe067a42013-02-22 19:59:23 -08001422 /**
1423 * Returns the global count of objects allocated by the runtime between a
1424 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001425 *
1426 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001427 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001428 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 public static int getGlobalAllocCount() {
1430 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1431 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001432
1433 /**
1434 * Clears the global count of objects allocated.
1435 * @see #getGlobalAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001436 *
1437 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001438 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001439 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001440 public static void resetGlobalAllocCount() {
1441 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS);
1442 }
1443
1444 /**
1445 * Returns the global size, in bytes, of objects allocated by the runtime between a
1446 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001447 *
1448 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001449 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001450 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 public static int getGlobalAllocSize() {
1452 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1453 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001454
1455 /**
1456 * Clears the global size of objects allocated.
Dianne Hackborn3fa89692013-09-13 17:20:00 -07001457 * @see #getGlobalAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001458 *
1459 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001460 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001461 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001462 public static void resetGlobalAllocSize() {
1463 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES);
1464 }
1465
1466 /**
1467 * Returns the global count of objects freed by the runtime between a
1468 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001469 *
1470 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001471 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001472 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 public static int getGlobalFreedCount() {
1474 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1475 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001476
1477 /**
1478 * Clears the global count of objects freed.
1479 * @see #getGlobalFreedCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001480 *
1481 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001482 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001483 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001484 public static void resetGlobalFreedCount() {
1485 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS);
1486 }
1487
1488 /**
1489 * Returns the global size, in bytes, of objects freed by the runtime between a
1490 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001491 *
1492 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001493 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001494 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 public static int getGlobalFreedSize() {
1496 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1497 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001498
1499 /**
1500 * Clears the global size of objects freed.
1501 * @see #getGlobalFreedSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001502 *
1503 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001504 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001505 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001506 public static void resetGlobalFreedSize() {
1507 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES);
1508 }
1509
1510 /**
1511 * Returns the number of non-concurrent GC invocations between a
1512 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001513 *
1514 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001515 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001516 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001517 public static int getGlobalGcInvocationCount() {
1518 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1519 }
1520
1521 /**
1522 * Clears the count of non-concurrent GC invocations.
1523 * @see #getGlobalGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001524 *
1525 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001526 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001527 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001528 public static void resetGlobalGcInvocationCount() {
1529 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS);
1530 }
1531
1532 /**
1533 * Returns the number of classes successfully initialized (ie those that executed without
1534 * throwing an exception) between a {@link #startAllocCounting() start} and
1535 * {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001536 *
1537 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001538 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001539 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001540 public static int getGlobalClassInitCount() {
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001541 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1542 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001543
1544 /**
1545 * Clears the count of classes initialized.
1546 * @see #getGlobalClassInitCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001547 *
1548 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001549 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001550 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001551 public static void resetGlobalClassInitCount() {
1552 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT);
1553 }
1554
1555 /**
1556 * Returns the time spent successfully initializing classes between a
1557 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001558 *
1559 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001560 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001561 @Deprecated
Andy McFaddenc4e1bf72010-02-22 17:07:36 -08001562 public static int getGlobalClassInitTime() {
1563 /* cumulative elapsed time for class initialization, in usec */
1564 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1565 }
Carl Shapirob5961982010-12-22 15:54:53 -08001566
1567 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001568 * Clears the count of time spent initializing classes.
1569 * @see #getGlobalClassInitTime()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001570 *
1571 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001572 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001573 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001574 public static void resetGlobalClassInitTime() {
1575 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME);
1576 }
1577
1578 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001579 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001580 * @deprecated This method is now obsolete.
1581 */
1582 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 public static int getGlobalExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001584 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
Carl Shapirob5961982010-12-22 15:54:53 -08001586
1587 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001588 * This method exists for compatibility and has no effect.
1589 * @deprecated This method is now obsolete.
1590 */
1591 @Deprecated
1592 public static void resetGlobalExternalAllocSize() {}
1593
1594 /**
1595 * This method exists for compatibility and has no effect.
1596 * @deprecated This method is now obsolete.
1597 */
1598 @Deprecated
1599 public static void resetGlobalExternalAllocCount() {}
1600
1601 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001602 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001603 * @deprecated This method is now obsolete.
1604 */
1605 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 public static int getGlobalExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001607 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 }
Carl Shapirob5961982010-12-22 15:54:53 -08001609
1610 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001611 * This method exists for compatibility and always returns 0.
Carl Shapirob5961982010-12-22 15:54:53 -08001612 * @deprecated This method is now obsolete.
1613 */
1614 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 public static int getGlobalExternalFreedCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001616 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 }
Carl Shapirob5961982010-12-22 15:54:53 -08001618
1619 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001620 * This method exists for compatibility and has no effect.
1621 * @deprecated This method is now obsolete.
1622 */
1623 @Deprecated
1624 public static void resetGlobalExternalFreedCount() {}
1625
1626 /**
1627 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001628 * @deprecated This method is now obsolete.
1629 */
1630 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 public static int getGlobalExternalFreedSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001632 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 }
Carl Shapirob5961982010-12-22 15:54:53 -08001634
Ian Rogersfe067a42013-02-22 19:59:23 -08001635 /**
1636 * This method exists for compatibility and has no effect.
1637 * @deprecated This method is now obsolete.
1638 */
1639 @Deprecated
1640 public static void resetGlobalExternalFreedSize() {}
1641
1642 /**
1643 * Returns the thread-local count of objects allocated by the runtime between a
1644 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001645 *
1646 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001647 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001648 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 public static int getThreadAllocCount() {
1650 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1651 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001652
1653 /**
1654 * Clears the thread-local count of objects allocated.
1655 * @see #getThreadAllocCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001656 *
1657 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001658 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001659 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001660 public static void resetThreadAllocCount() {
1661 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS);
1662 }
1663
1664 /**
1665 * Returns the thread-local size of objects allocated by the runtime between a
1666 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
1667 * @return The allocated size in bytes.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001668 *
1669 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001670 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001671 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 public static int getThreadAllocSize() {
1673 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1674 }
Carl Shapirob5961982010-12-22 15:54:53 -08001675
1676 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001677 * Clears the thread-local count of objects allocated.
1678 * @see #getThreadAllocSize()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001679 *
1680 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001681 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001682 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001683 public static void resetThreadAllocSize() {
1684 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES);
1685 }
1686
1687 /**
1688 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001689 * @deprecated This method is now obsolete.
1690 */
1691 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 public static int getThreadExternalAllocCount() {
Carl Shapirob5961982010-12-22 15:54:53 -08001693 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
Carl Shapirob5961982010-12-22 15:54:53 -08001695
1696 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08001697 * This method exists for compatibility and has no effect.
1698 * @deprecated This method is now obsolete.
1699 */
1700 @Deprecated
1701 public static void resetThreadExternalAllocCount() {}
1702
1703 /**
1704 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001705 * @deprecated This method is now obsolete.
1706 */
1707 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 public static int getThreadExternalAllocSize() {
Carl Shapirob5961982010-12-22 15:54:53 -08001709 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
Carl Shapirob5961982010-12-22 15:54:53 -08001711
Carl Shapirob5961982010-12-22 15:54:53 -08001712 /**
Carl Shapiro7e942842011-01-12 17:17:45 -08001713 * This method exists for compatibility and has no effect.
Carl Shapirob5961982010-12-22 15:54:53 -08001714 * @deprecated This method is now obsolete.
1715 */
1716 @Deprecated
1717 public static void resetThreadExternalAllocSize() {}
1718
Ian Rogersfe067a42013-02-22 19:59:23 -08001719 /**
1720 * Returns the number of thread-local non-concurrent GC invocations between a
1721 * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}.
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001722 *
1723 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001724 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001725 @Deprecated
Ian Rogersfe067a42013-02-22 19:59:23 -08001726 public static int getThreadGcInvocationCount() {
1727 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1728 }
1729
1730 /**
1731 * Clears the thread-local count of non-concurrent GC invocations.
1732 * @see #getThreadGcInvocationCount()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001733 *
1734 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001735 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001736 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 public static void resetThreadGcInvocationCount() {
1738 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS);
1739 }
Ian Rogersfe067a42013-02-22 19:59:23 -08001740
1741 /**
1742 * Clears all the global and thread-local memory allocation counters.
1743 * @see #startAllocCounting()
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001744 *
1745 * @deprecated Accurate counting is a burden on the runtime and may be removed.
Ian Rogersfe067a42013-02-22 19:59:23 -08001746 */
Hiroshi Yamauchi172da262015-03-04 12:29:19 -08001747 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 public static void resetAllCounts() {
1749 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS);
1750 }
1751
1752 /**
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001753 * Returns the value of a particular runtime statistic or {@code null} if no
1754 * such runtime statistic exists.
1755 *
1756 * <p>The following table lists the runtime statistics that the runtime supports.
Hans Boehm98491ba2019-10-08 17:54:47 -07001757 * All statistics are approximate. Individual allocations may not be immediately reflected
1758 * in the results.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001759 * Note runtime statistics may be added or removed in a future API level.</p>
1760 *
1761 * <table>
1762 * <thead>
1763 * <tr>
1764 * <th>Runtime statistic name</th>
1765 * <th>Meaning</th>
1766 * <th>Example</th>
1767 * <th>Supported (API Levels)</th>
1768 * </tr>
1769 * </thead>
1770 * <tbody>
1771 * <tr>
1772 * <td>art.gc.gc-count</td>
1773 * <td>The number of garbage collection runs.</td>
1774 * <td>{@code 164}</td>
1775 * <td>23</td>
1776 * </tr>
1777 * <tr>
1778 * <td>art.gc.gc-time</td>
1779 * <td>The total duration of garbage collection runs in ms.</td>
1780 * <td>{@code 62364}</td>
1781 * <td>23</td>
1782 * </tr>
1783 * <tr>
1784 * <td>art.gc.bytes-allocated</td>
1785 * <td>The total number of bytes that the application allocated.</td>
1786 * <td>{@code 1463948408}</td>
1787 * <td>23</td>
1788 * </tr>
1789 * <tr>
1790 * <td>art.gc.bytes-freed</td>
1791 * <td>The total number of bytes that garbage collection reclaimed.</td>
1792 * <td>{@code 1313493084}</td>
1793 * <td>23</td>
1794 * </tr>
1795 * <tr>
1796 * <td>art.gc.blocking-gc-count</td>
1797 * <td>The number of blocking garbage collection runs.</td>
1798 * <td>{@code 2}</td>
1799 * <td>23</td>
1800 * </tr>
1801 * <tr>
1802 * <td>art.gc.blocking-gc-time</td>
1803 * <td>The total duration of blocking garbage collection runs in ms.</td>
1804 * <td>{@code 804}</td>
1805 * <td>23</td>
1806 * </tr>
1807 * <tr>
1808 * <td>art.gc.gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001809 * <td>Every 10 seconds, the gc-count-rate is computed as the number of garbage
1810 * collection runs that have occurred over the last 10
1811 * seconds. art.gc.gc-count-rate-histogram is a histogram of the gc-count-rate
1812 * samples taken since the process began. The histogram can be used to identify
1813 * instances of high rates of garbage collection runs. For example, a histogram
1814 * of "0:34503,1:45350,2:11281,3:8088,4:43,5:8" shows that most of the time
1815 * there are between 0 and 2 garbage collection runs every 10 seconds, but there
1816 * were 8 distinct 10-second intervals in which 5 garbage collection runs
1817 * occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001818 * <td>{@code 0:34503,1:45350,2:11281,3:8088,4:43,5:8}</td>
1819 * <td>23</td>
1820 * </tr>
1821 * <tr>
1822 * <td>art.gc.blocking-gc-count-rate-histogram</td>
Hiroshi Yamauchi2d6327d2015-05-28 15:28:16 -07001823 * <td>Every 10 seconds, the blocking-gc-count-rate is computed as the number of
1824 * blocking garbage collection runs that have occurred over the last 10
1825 * seconds. art.gc.blocking-gc-count-rate-histogram is a histogram of the
1826 * blocking-gc-count-rate samples taken since the process began. The histogram
1827 * can be used to identify instances of high rates of blocking garbage
1828 * collection runs. For example, a histogram of "0:99269,1:1,2:1" shows that
1829 * most of the time there are zero blocking garbage collection runs every 10
1830 * seconds, but there was one 10-second interval in which one blocking garbage
1831 * collection run occurred, and there was one interval in which two blocking
1832 * garbage collection runs occurred.</td>
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001833 * <td>{@code 0:99269,1:1,2:1}</td>
1834 * <td>23</td>
1835 * </tr>
1836 * </tbody>
1837 * </table>
1838 *
1839 * @param statName
1840 * the name of the runtime statistic to look up.
1841 * @return the value of the specified runtime statistic or {@code null} if the
1842 * runtime statistic doesn't exist.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001843 */
1844 public static String getRuntimeStat(String statName) {
1845 return VMDebug.getRuntimeStat(statName);
1846 }
1847
1848 /**
1849 * Returns a map of the names/values of the runtime statistics
Hiroshi Yamauchid8001672015-04-14 16:07:26 -07001850 * that {@link #getRuntimeStat(String)} supports.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001851 *
1852 * @return a map of the names/values of the supported runtime statistics.
Hiroshi Yamauchi8b5a293d2015-04-02 12:26:10 -07001853 */
1854 public static Map<String, String> getRuntimeStats() {
1855 return VMDebug.getRuntimeStats();
1856 }
1857
1858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 * Returns the size of the native heap.
1860 * @return The size of the native heap in bytes.
1861 */
1862 public static native long getNativeHeapSize();
1863
1864 /**
1865 * Returns the amount of allocated memory in the native heap.
1866 * @return The allocated size in bytes.
1867 */
1868 public static native long getNativeHeapAllocatedSize();
1869
1870 /**
1871 * Returns the amount of free memory in the native heap.
1872 * @return The freed size in bytes.
1873 */
1874 public static native long getNativeHeapFreeSize();
1875
1876 /**
1877 * Retrieves information about this processes memory usages. This information is broken down by
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001878 * how much is in use by dalvik, the native heap, and everything else.
1879 *
Kweku Adams983829f2017-12-06 14:53:50 -08001880 * <p><b>Note:</b> this method directly retrieves memory information for the given process
Dianne Hackbornb02ce292015-10-12 15:14:16 -07001881 * from low-level data available to it. It may not be able to retrieve information about
1882 * some protected allocations, such as graphics. If you want to be sure you can see
Kweku Adams983829f2017-12-06 14:53:50 -08001883 * all information about allocations by the process, use
1884 * {@link android.app.ActivityManager#getProcessMemoryInfo(int[])} instead.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 */
1886 public static native void getMemoryInfo(MemoryInfo memoryInfo);
1887
1888 /**
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001889 * Note: currently only works when the requested pid has the same UID
1890 * as the caller.
1891 * @hide
1892 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001893 @UnsupportedAppUsage
Dianne Hackborn3025ef32009-08-31 21:31:47 -07001894 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo);
1895
1896 /**
Dianne Hackbornb437e092011-08-05 17:50:29 -07001897 * Retrieves the PSS memory used by the process as given by the
1898 * smaps.
1899 */
1900 public static native long getPss();
1901
1902 /**
Rafal Slawik4d078962018-08-20 18:23:49 +01001903 * Retrieves the PSS memory used by the process as given by the smaps. Optionally supply a long
1904 * array of up to 3 entries to also receive (up to 3 values in order): the Uss and SwapPss and
1905 * Rss (only filled in as of {@link android.os.Build.VERSION_CODES#P}) of the process, and
1906 * another array to also retrieve the separate memtrack size.
Martijn Coenene0764852016-01-07 17:04:22 -08001907 * @hide
Dianne Hackbornb437e092011-08-05 17:50:29 -07001908 */
Rafal Slawik4d078962018-08-20 18:23:49 +01001909 public static native long getPss(int pid, long[] outUssSwapPssRss, long[] outMemtrack);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001910
Dianne Hackborn8e692572013-09-10 19:06:15 -07001911 /** @hide */
1912 public static final int MEMINFO_TOTAL = 0;
1913 /** @hide */
1914 public static final int MEMINFO_FREE = 1;
1915 /** @hide */
1916 public static final int MEMINFO_BUFFERS = 2;
1917 /** @hide */
1918 public static final int MEMINFO_CACHED = 3;
1919 /** @hide */
1920 public static final int MEMINFO_SHMEM = 4;
1921 /** @hide */
1922 public static final int MEMINFO_SLAB = 5;
Robert Benea5e099802017-10-04 18:28:01 -07001923 /** @hide */
1924 public static final int MEMINFO_SLAB_RECLAIMABLE = 6;
1925 /** @hide */
1926 public static final int MEMINFO_SLAB_UNRECLAIMABLE = 7;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001927 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001928 public static final int MEMINFO_SWAP_TOTAL = 8;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001929 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001930 public static final int MEMINFO_SWAP_FREE = 9;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001931 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001932 public static final int MEMINFO_ZRAM_TOTAL = 10;
Dianne Hackborncbd9a522013-09-24 23:10:14 -07001933 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001934 public static final int MEMINFO_MAPPED = 11;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001935 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001936 public static final int MEMINFO_VM_ALLOC_USED = 12;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001937 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001938 public static final int MEMINFO_PAGE_TABLES = 13;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001939 /** @hide */
Robert Benea5e099802017-10-04 18:28:01 -07001940 public static final int MEMINFO_KERNEL_STACK = 14;
Suren Baghdasaryan5f8e17b2019-11-17 14:25:50 -08001941 /**
1942 * Note: MEMINFO_KRECLAIMABLE includes MEMINFO_SLAB_RECLAIMABLE (see KReclaimable field
1943 * description in kernel documentation).
1944 * @hide
1945 */
1946 public static final int MEMINFO_KRECLAIMABLE = 15;
Dianne Hackbornb3af4ec2014-10-17 15:25:13 -07001947 /** @hide */
Suren Baghdasaryan5f8e17b2019-11-17 14:25:50 -08001948 public static final int MEMINFO_COUNT = 16;
Dianne Hackborn8e692572013-09-10 19:06:15 -07001949
1950 /**
1951 * Retrieves /proc/meminfo. outSizes is filled with fields
1952 * as defined by MEMINFO_* offsets.
1953 * @hide
1954 */
Andrei Onea24ec3212019-03-15 17:35:05 +00001955 @UnsupportedAppUsage
Dianne Hackborn8e692572013-09-10 19:06:15 -07001956 public static native void getMemInfo(long[] outSizes);
1957
Dianne Hackbornb437e092011-08-05 17:50:29 -07001958 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001959 * Establish an object allocation limit in the current thread.
Carl Shapiro7e942842011-01-12 17:17:45 -08001960 * This feature was never enabled in release builds. The
1961 * allocation limits feature was removed in Honeycomb. This
1962 * method exists for compatibility and always returns -1 and has
1963 * no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 *
Carl Shapiro11073832011-01-12 16:28:57 -08001965 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 */
Carl Shapiro11073832011-01-12 16:28:57 -08001967 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 public static int setAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001969 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971
1972 /**
Carl Shapiro11073832011-01-12 16:28:57 -08001973 * Establish a global object allocation limit. This feature was
Carl Shapiro7e942842011-01-12 17:17:45 -08001974 * never enabled in release builds. The allocation limits feature
1975 * was removed in Honeycomb. This method exists for compatibility
1976 * and always returns -1 and has no effect.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 *
Carl Shapiro11073832011-01-12 16:28:57 -08001978 * @deprecated This method is now obsolete.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 */
Carl Shapiro11073832011-01-12 16:28:57 -08001980 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 public static int setGlobalAllocationLimit(int limit) {
Carl Shapiro11073832011-01-12 16:28:57 -08001982 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984
1985 /**
1986 * Dump a list of all currently loaded class to the log file.
1987 *
1988 * @param flags See constants above.
1989 */
1990 public static void printLoadedClasses(int flags) {
1991 VMDebug.printLoadedClasses(flags);
1992 }
1993
1994 /**
1995 * Get the number of loaded classes.
1996 * @return the number of loaded classes.
1997 */
1998 public static int getLoadedClassCount() {
1999 return VMDebug.getLoadedClassCount();
2000 }
2001
2002 /**
Andy McFadden824c5102010-07-09 16:26:57 -07002003 * Dump "hprof" data to the specified file. This may cause a GC.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 *
2005 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof").
2006 * @throws UnsupportedOperationException if the VM was built without
2007 * HPROF support.
2008 * @throws IOException if an error occurs while opening or writing files.
2009 */
2010 public static void dumpHprofData(String fileName) throws IOException {
2011 VMDebug.dumpHprofData(fileName);
2012 }
2013
2014 /**
Andy McFadden824c5102010-07-09 16:26:57 -07002015 * Like dumpHprofData(String), but takes an already-opened
2016 * FileDescriptor to which the trace is written. The file name is also
2017 * supplied simply for logging. Makes a dup of the file descriptor.
2018 *
2019 * Primarily for use by the "am" shell command.
2020 *
2021 * @hide
2022 */
2023 public static void dumpHprofData(String fileName, FileDescriptor fd)
2024 throws IOException {
2025 VMDebug.dumpHprofData(fileName, fd);
2026 }
2027
2028 /**
2029 * Collect "hprof" and send it to DDMS. This may cause a GC.
Andy McFadden07a96612010-01-28 16:54:37 -08002030 *
2031 * @throws UnsupportedOperationException if the VM was built without
2032 * HPROF support.
Andy McFadden07a96612010-01-28 16:54:37 -08002033 * @hide
2034 */
2035 public static void dumpHprofDataDdms() {
2036 VMDebug.dumpHprofDataDdms();
2037 }
2038
2039 /**
Andy McFadden06a6b552010-07-13 16:28:09 -07002040 * Writes native heap data to the specified file descriptor.
2041 *
2042 * @hide
2043 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002044 @UnsupportedAppUsage
Andy McFadden06a6b552010-07-13 16:28:09 -07002045 public static native void dumpNativeHeap(FileDescriptor fd);
2046
2047 /**
Christopher Ferris8d652f82017-04-11 16:29:18 -07002048 * Writes malloc info data to the specified file descriptor.
2049 *
2050 * @hide
2051 */
2052 public static native void dumpNativeMallocInfo(FileDescriptor fd);
2053
2054 /**
Brian Carlstromc21550a2010-10-05 21:34:06 -07002055 * Returns a count of the extant instances of a class.
2056 *
2057 * @hide
2058 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002059 @UnsupportedAppUsage
Brian Carlstromc21550a2010-10-05 21:34:06 -07002060 public static long countInstancesOfClass(Class cls) {
Brian Carlstrom7495cfa2010-11-30 18:06:00 -08002061 return VMDebug.countInstancesOfClass(cls, true);
Brian Carlstromc21550a2010-10-05 21:34:06 -07002062 }
2063
2064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 * Returns the number of sent transactions from this process.
2066 * @return The number of sent transactions or -1 if it could not read t.
2067 */
2068 public static native int getBinderSentTransactions();
2069
2070 /**
2071 * Returns the number of received transactions from the binder driver.
2072 * @return The number of received transactions or -1 if it could not read the stats.
2073 */
2074 public static native int getBinderReceivedTransactions();
2075
2076 /**
2077 * Returns the number of active local Binder objects that exist in the
2078 * current process.
2079 */
2080 public static final native int getBinderLocalObjectCount();
2081
2082 /**
2083 * Returns the number of references to remote proxy Binder objects that
2084 * exist in the current process.
2085 */
2086 public static final native int getBinderProxyObjectCount();
2087
2088 /**
2089 * Returns the number of death notification links to Binder objects that
2090 * exist in the current process.
2091 */
2092 public static final native int getBinderDeathObjectCount();
2093
2094 /**
Andy McFadden599c9182009-04-08 00:35:56 -07002095 * Primes the register map cache.
2096 *
2097 * Only works for classes in the bootstrap class loader. Does not
2098 * cause classes to be loaded if they're not already present.
2099 *
2100 * The classAndMethodDesc argument is a concatentation of the VM-internal
2101 * class descriptor, method name, and method descriptor. Examples:
2102 * Landroid/os/Looper;.loop:()V
2103 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V
2104 *
2105 * @param classAndMethodDesc the method to prepare
2106 *
2107 * @hide
2108 */
2109 public static final boolean cacheRegisterMap(String classAndMethodDesc) {
2110 return VMDebug.cacheRegisterMap(classAndMethodDesc);
2111 }
2112
2113 /**
Andy McFaddenbfd6d482009-10-22 17:25:57 -07002114 * Dumps the contents of VM reference tables (e.g. JNI locals and
2115 * globals) to the log file.
2116 *
2117 * @hide
2118 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002119 @UnsupportedAppUsage
Andy McFaddenbfd6d482009-10-22 17:25:57 -07002120 public static final void dumpReferenceTables() {
2121 VMDebug.dumpReferenceTables();
2122 }
2123
2124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 * API for gathering and querying instruction counts.
2126 *
2127 * Example usage:
Chet Haase2970c492010-11-09 13:58:04 -08002128 * <pre>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 * Debug.InstructionCount icount = new Debug.InstructionCount();
2130 * icount.resetAndStart();
2131 * [... do lots of stuff ...]
2132 * if (icount.collect()) {
2133 * System.out.println("Total instructions executed: "
2134 * + icount.globalTotal());
2135 * System.out.println("Method invocations: "
2136 * + icount.globalMethodInvocations());
2137 * }
Chet Haase2970c492010-11-09 13:58:04 -08002138 * </pre>
Jeff Hao7d0b3d42014-09-17 15:45:05 -07002139 *
2140 * @deprecated Instruction counting is no longer supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 */
Jeff Hao7d0b3d42014-09-17 15:45:05 -07002142 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 public static class InstructionCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 public InstructionCount() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146
2147 /**
2148 * Reset counters and ensure counts are running. Counts may
2149 * have already been running.
2150 *
2151 * @return true if counting was started
2152 */
2153 public boolean resetAndStart() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002154 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 }
2156
2157 /**
2158 * Collect instruction counts. May or may not stop the
2159 * counting process.
2160 */
2161 public boolean collect() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164
2165 /**
2166 * Return the total number of instructions executed globally (i.e. in
2167 * all threads).
2168 */
2169 public int globalTotal() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002170 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172
2173 /**
2174 * Return the total number of method-invocation instructions
2175 * executed globally.
2176 */
2177 public int globalMethodInvocations() {
Narayan Kamath19541e82017-05-30 18:04:36 +01002178 return 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002179 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002180 }
2181
Dave Bort1ce5bd32009-04-22 17:36:56 -07002182 /**
2183 * A Map of typed debug properties.
2184 */
2185 private static final TypedProperties debugProperties;
2186
2187 /*
2188 * Load the debug properties from the standard files into debugProperties.
2189 */
2190 static {
Joe Onorato43a17652011-04-06 19:22:23 -07002191 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002192 final String TAG = "DebugProperties";
2193 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" };
2194 final TypedProperties tp = new TypedProperties();
2195
2196 // Read the properties from each of the files, if present.
Dave Borte9bfd9b2009-05-04 14:35:23 -07002197 for (String file : files) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002198 Reader r;
2199 try {
2200 r = new FileReader(file);
2201 } catch (FileNotFoundException ex) {
2202 // It's ok if a file is missing.
2203 continue;
2204 }
2205
Dave Bort1ce5bd32009-04-22 17:36:56 -07002206 try {
2207 tp.load(r);
Dave Borte9bfd9b2009-05-04 14:35:23 -07002208 } catch (Exception ex) {
2209 throw new RuntimeException("Problem loading " + file, ex);
2210 } finally {
2211 try {
2212 r.close();
2213 } catch (IOException ex) {
2214 // Ignore this error.
2215 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002216 }
2217 }
2218
2219 debugProperties = tp.isEmpty() ? null : tp;
2220 } else {
2221 debugProperties = null;
2222 }
2223 }
2224
2225
2226 /**
2227 * Returns true if the type of the field matches the specified class.
2228 * Handles the case where the class is, e.g., java.lang.Boolean, but
2229 * the field is of the primitive "boolean" type. Also handles all of
2230 * the java.lang.Number subclasses.
2231 */
2232 private static boolean fieldTypeMatches(Field field, Class<?> cl) {
2233 Class<?> fieldClass = field.getType();
2234 if (fieldClass == cl) {
2235 return true;
2236 }
2237 Field primitiveTypeField;
2238 try {
2239 /* All of the classes we care about (Boolean, Integer, etc.)
2240 * have a Class field called "TYPE" that points to the corresponding
2241 * primitive class.
2242 */
2243 primitiveTypeField = cl.getField("TYPE");
2244 } catch (NoSuchFieldException ex) {
2245 return false;
2246 }
2247 try {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002248 return fieldClass == (Class<?>) primitiveTypeField.get(null);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002249 } catch (IllegalAccessException ex) {
2250 return false;
2251 }
2252 }
2253
2254
2255 /**
2256 * Looks up the property that corresponds to the field, and sets the field's value
2257 * if the types match.
2258 */
Dave Borte9bfd9b2009-05-04 14:35:23 -07002259 private static void modifyFieldIfSet(final Field field, final TypedProperties properties,
2260 final String propertyName) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002261 if (field.getType() == java.lang.String.class) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002262 int stringInfo = properties.getStringInfo(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002263 switch (stringInfo) {
Dave Borte9bfd9b2009-05-04 14:35:23 -07002264 case TypedProperties.STRING_SET:
2265 // Handle as usual below.
2266 break;
2267 case TypedProperties.STRING_NULL:
2268 try {
2269 field.set(null, null); // null object for static fields; null string
2270 } catch (IllegalAccessException ex) {
2271 throw new IllegalArgumentException(
2272 "Cannot set field for " + propertyName, ex);
2273 }
2274 return;
2275 case TypedProperties.STRING_NOT_SET:
2276 return;
2277 case TypedProperties.STRING_TYPE_MISMATCH:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002278 throw new IllegalArgumentException(
Dave Borte9bfd9b2009-05-04 14:35:23 -07002279 "Type of " + propertyName + " " +
2280 " does not match field type (" + field.getType() + ")");
2281 default:
2282 throw new IllegalStateException(
2283 "Unexpected getStringInfo(" + propertyName + ") return value " +
2284 stringInfo);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002285 }
2286 }
Dave Borte9bfd9b2009-05-04 14:35:23 -07002287 Object value = properties.get(propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002288 if (value != null) {
2289 if (!fieldTypeMatches(field, value.getClass())) {
2290 throw new IllegalArgumentException(
2291 "Type of " + propertyName + " (" + value.getClass() + ") " +
2292 " does not match field type (" + field.getType() + ")");
2293 }
2294 try {
2295 field.set(null, value); // null object for static fields
2296 } catch (IllegalAccessException ex) {
2297 throw new IllegalArgumentException(
2298 "Cannot set field for " + propertyName, ex);
2299 }
2300 }
2301 }
2302
2303
2304 /**
Romain Guyc4b11a72009-05-13 15:46:37 -07002305 * Equivalent to <code>setFieldsOn(cl, false)</code>.
2306 *
2307 * @see #setFieldsOn(Class, boolean)
Romain Guyd4103d02009-05-14 12:24:21 -07002308 *
2309 * @hide
Romain Guyc4b11a72009-05-13 15:46:37 -07002310 */
2311 public static void setFieldsOn(Class<?> cl) {
2312 setFieldsOn(cl, false);
2313 }
2314
2315 /**
Dave Bort1ce5bd32009-04-22 17:36:56 -07002316 * Reflectively sets static fields of a class based on internal debugging
Joe Onorato43a17652011-04-06 19:22:23 -07002317 * properties. This method is a no-op if false is
Dave Bort1ce5bd32009-04-22 17:36:56 -07002318 * false.
2319 * <p>
Joe Onorato43a17652011-04-06 19:22:23 -07002320 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will
Dave Bort1ce5bd32009-04-22 17:36:56 -07002321 * always be false in release builds. This API is typically only useful
2322 * for platform developers.
2323 * </p>
2324 * Class setup: define a class whose only fields are non-final, static
2325 * primitive types (except for "char") or Strings. In a static block
2326 * after the field definitions/initializations, pass the class to
Romain Guyc4b11a72009-05-13 15:46:37 -07002327 * this method, Debug.setFieldsOn(). Example:
Dave Bort1ce5bd32009-04-22 17:36:56 -07002328 * <pre>
2329 * package com.example;
2330 *
2331 * import android.os.Debug;
2332 *
2333 * public class MyDebugVars {
2334 * public static String s = "a string";
2335 * public static String s2 = "second string";
2336 * public static String ns = null;
2337 * public static boolean b = false;
2338 * public static int i = 5;
Romain Guyc4b11a72009-05-13 15:46:37 -07002339 * @Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002340 * public static float f = 0.1f;
Romain Guyc4b11a72009-05-13 15:46:37 -07002341 * @@Debug.DebugProperty
Dave Bort1ce5bd32009-04-22 17:36:56 -07002342 * public static double d = 0.5d;
2343 *
2344 * // This MUST appear AFTER all fields are defined and initialized!
2345 * static {
Romain Guyc4b11a72009-05-13 15:46:37 -07002346 * // Sets all the fields
Dave Borte9bfd9b2009-05-04 14:35:23 -07002347 * Debug.setFieldsOn(MyDebugVars.class);
Christian Mehlmauer798e2d32010-06-17 18:24:07 +02002348 *
Romain Guyc4b11a72009-05-13 15:46:37 -07002349 * // Sets only the fields annotated with @Debug.DebugProperty
2350 * // Debug.setFieldsOn(MyDebugVars.class, true);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002351 * }
2352 * }
2353 * </pre>
Dave Borte9bfd9b2009-05-04 14:35:23 -07002354 * setFieldsOn() may override the value of any field in the class based
Dave Bort1ce5bd32009-04-22 17:36:56 -07002355 * on internal properties that are fixed at boot time.
2356 * <p>
2357 * These properties are only set during platform debugging, and are not
2358 * meant to be used as a general-purpose properties store.
2359 *
2360 * {@hide}
2361 *
2362 * @param cl The class to (possibly) modify
Romain Guyc4b11a72009-05-13 15:46:37 -07002363 * @param partial If false, sets all static fields, otherwise, only set
2364 * fields with the {@link android.os.Debug.DebugProperty}
2365 * annotation
Dave Bort1ce5bd32009-04-22 17:36:56 -07002366 * @throws IllegalArgumentException if any fields are final or non-static,
2367 * or if the type of the field does not match the type of
2368 * the internal debugging property value.
2369 */
Romain Guyc4b11a72009-05-13 15:46:37 -07002370 public static void setFieldsOn(Class<?> cl, boolean partial) {
Joe Onorato43a17652011-04-06 19:22:23 -07002371 if (false) {
Dave Bort1ce5bd32009-04-22 17:36:56 -07002372 if (debugProperties != null) {
2373 /* Only look for fields declared directly by the class,
2374 * so we don't mysteriously change static fields in superclasses.
2375 */
2376 for (Field field : cl.getDeclaredFields()) {
Romain Guyc4b11a72009-05-13 15:46:37 -07002377 if (!partial || field.getAnnotation(DebugProperty.class) != null) {
2378 final String propertyName = cl.getName() + "." + field.getName();
2379 boolean isStatic = Modifier.isStatic(field.getModifiers());
2380 boolean isFinal = Modifier.isFinal(field.getModifiers());
2381
2382 if (!isStatic || isFinal) {
2383 throw new IllegalArgumentException(propertyName +
2384 " must be static and non-final");
2385 }
2386 modifyFieldIfSet(field, debugProperties, propertyName);
Dave Bort1ce5bd32009-04-22 17:36:56 -07002387 }
Dave Bort1ce5bd32009-04-22 17:36:56 -07002388 }
2389 }
2390 } else {
Dan Egnor3eda9792010-03-05 13:28:36 -08002391 Log.wtf(TAG,
Dave Borte9bfd9b2009-05-04 14:35:23 -07002392 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) +
Dave Bort1ce5bd32009-04-22 17:36:56 -07002393 ") called in non-DEBUG build");
2394 }
2395 }
Romain Guyc4b11a72009-05-13 15:46:37 -07002396
2397 /**
2398 * Annotation to put on fields you want to set with
2399 * {@link Debug#setFieldsOn(Class, boolean)}.
2400 *
2401 * @hide
2402 */
2403 @Target({ ElementType.FIELD })
2404 @Retention(RetentionPolicy.RUNTIME)
2405 public @interface DebugProperty {
2406 }
Dan Egnor3eda9792010-03-05 13:28:36 -08002407
2408 /**
2409 * Get a debugging dump of a system service by name.
2410 *
2411 * <p>Most services require the caller to hold android.permission.DUMP.
2412 *
2413 * @param name of the service to dump
2414 * @param fd to write dump output to (usually an output log file)
2415 * @param args to pass to the service's dump method, may be null
2416 * @return true if the service was dumped successfully, false if
2417 * the service could not be found or had an error while dumping
2418 */
2419 public static boolean dumpService(String name, FileDescriptor fd, String[] args) {
2420 IBinder service = ServiceManager.getService(name);
2421 if (service == null) {
2422 Log.e(TAG, "Can't find service to dump: " + name);
2423 return false;
2424 }
2425
2426 try {
2427 service.dump(fd, args);
2428 return true;
2429 } catch (RemoteException e) {
2430 Log.e(TAG, "Can't dump service: " + name, e);
2431 return false;
2432 }
2433 }
Craig Mautnera51a9562012-04-17 17:05:26 -07002434
2435 /**
Narayan Kamathf013daa2017-05-09 12:55:02 +01002436 * Append the Java stack traces of a given native process to a specified file.
2437 *
songjinshie02e3ea2016-12-16 17:48:21 +08002438 * @param pid pid to dump.
2439 * @param file path of file to append dump to.
2440 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002441 * @hide
2442 */
Narayan Kamathf013daa2017-05-09 12:55:02 +01002443 public static native boolean dumpJavaBacktraceToFileTimeout(int pid, String file,
2444 int timeoutSecs);
2445
2446 /**
2447 * Append the native stack traces of a given process to a specified file.
2448 *
2449 * @param pid pid to dump.
2450 * @param file path of file to append dump to.
2451 * @param timeoutSecs time to wait in seconds, or 0 to wait forever.
2452 * @hide
2453 */
2454 public static native boolean dumpNativeBacktraceToFileTimeout(int pid, String file,
2455 int timeoutSecs);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07002456
2457 /**
Colin Crossc4fb5f92016-02-02 16:51:15 -08002458 * Get description of unreachable native memory.
2459 * @param limit the number of leaks to provide info on, 0 to only get a summary.
2460 * @param contents true to include a hex dump of the contents of unreachable memory.
2461 * @return the String containing a description of unreachable memory.
2462 * @hide */
2463 public static native String getUnreachableMemory(int limit, boolean contents);
2464
2465 /**
Craig Mautnera51a9562012-04-17 17:05:26 -07002466 * Return a String describing the calling method and location at a particular stack depth.
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -07002467 * @param callStack the Thread stack
Craig Mautnera51a9562012-04-17 17:05:26 -07002468 * @param depth the depth of stack to return information for.
2469 * @return the String describing the caller at that depth.
2470 */
2471 private static String getCaller(StackTraceElement callStack[], int depth) {
2472 // callStack[4] is the caller of the method that called getCallers()
2473 if (4 + depth >= callStack.length) {
2474 return "<bottom of call stack>";
2475 }
2476 StackTraceElement caller = callStack[4 + depth];
2477 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber();
2478 }
2479
2480 /**
2481 * Return a string consisting of methods and locations at multiple call stack levels.
2482 * @param depth the number of levels to return, starting with the immediate caller.
2483 * @return a string describing the call stack.
2484 * {@hide}
2485 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002486 @UnsupportedAppUsage
Craig Mautnera51a9562012-04-17 17:05:26 -07002487 public static String getCallers(final int depth) {
2488 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2489 StringBuffer sb = new StringBuffer();
2490 for (int i = 0; i < depth; i++) {
2491 sb.append(getCaller(callStack, i)).append(" ");
2492 }
2493 return sb.toString();
2494 }
2495
2496 /**
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07002497 * Return a string consisting of methods and locations at multiple call stack levels.
2498 * @param depth the number of levels to return, starting with the immediate caller.
2499 * @return a string describing the call stack.
2500 * {@hide}
2501 */
2502 public static String getCallers(final int start, int depth) {
2503 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2504 StringBuffer sb = new StringBuffer();
2505 depth += start;
2506 for (int i = start; i < depth; i++) {
2507 sb.append(getCaller(callStack, i)).append(" ");
2508 }
2509 return sb.toString();
2510 }
2511
2512 /**
Dianne Hackbornef03a7f2012-10-29 18:46:52 -07002513 * Like {@link #getCallers(int)}, but each location is append to the string
2514 * as a new line with <var>linePrefix</var> in front of it.
2515 * @param depth the number of levels to return, starting with the immediate caller.
2516 * @param linePrefix prefix to put in front of each location.
2517 * @return a string describing the call stack.
2518 * {@hide}
2519 */
2520 public static String getCallers(final int depth, String linePrefix) {
2521 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
2522 StringBuffer sb = new StringBuffer();
2523 for (int i = 0; i < depth; i++) {
2524 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n");
2525 }
2526 return sb.toString();
2527 }
2528
2529 /**
Ian Rogersfe067a42013-02-22 19:59:23 -08002530 * @return a String describing the immediate caller of the calling method.
Craig Mautnera51a9562012-04-17 17:05:26 -07002531 * {@hide}
2532 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002533 @UnsupportedAppUsage
Craig Mautnera51a9562012-04-17 17:05:26 -07002534 public static String getCaller() {
2535 return getCaller(Thread.currentThread().getStackTrace(), 0);
2536 }
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002537
2538 /**
Andreas Gampe571b7002018-01-16 15:11:29 -08002539 * Attach a library as a jvmti agent to the current runtime, with the given classloader
2540 * determining the library search path.
2541 * <p>
2542 * Note: agents may only be attached to debuggable apps. Otherwise, this function will
2543 * throw a SecurityException.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002544 *
Andreas Gampe571b7002018-01-16 15:11:29 -08002545 * @param library the library containing the agent.
2546 * @param options the options passed to the agent.
2547 * @param classLoader the classloader determining the library search path.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002548 *
Andreas Gampe571b7002018-01-16 15:11:29 -08002549 * @throws IOException if the agent could not be attached.
2550 * @throws SecurityException if the app is not debuggable.
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002551 */
Andreas Gampe571b7002018-01-16 15:11:29 -08002552 public static void attachJvmtiAgent(@NonNull String library, @Nullable String options,
2553 @Nullable ClassLoader classLoader) throws IOException {
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002554 Preconditions.checkNotNull(library);
2555 Preconditions.checkArgument(!library.contains("="));
2556
2557 if (options == null) {
Andreas Gampe571b7002018-01-16 15:11:29 -08002558 VMDebug.attachAgent(library, classLoader);
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002559 } else {
Andreas Gampe571b7002018-01-16 15:11:29 -08002560 VMDebug.attachAgent(library + "=" + options, classLoader);
Philip P. Moltmannfd8ed852017-11-01 15:22:02 -07002561 }
2562 }
Ben Murdochfdc55932019-01-30 10:43:15 +00002563
2564 /**
2565 * Return the current free ZRAM usage in kilobytes.
2566 *
2567 * @hide
2568 */
2569 public static native long getZramFreeKb();
Suren Baghdasaryan5bf47062019-11-25 19:24:46 -08002570
2571 /**
2572 * Return memory size in kilobytes allocated for ION heaps.
2573 *
2574 * @hide
2575 */
2576 public static native long getIonHeapsSizeKb();
2577
2578 /**
2579 * Return memory size in kilobytes allocated for ION pools.
2580 *
2581 * @hide
2582 */
2583 public static native long getIonPoolsSizeKb();
2584
2585 /**
2586 * Return ION memory mapped by processes in kB.
2587 * Notes:
2588 * * Warning: Might impact performance as it reads /proc/<pid>/maps files for each process.
2589 *
2590 * @hide
2591 */
2592 public static native long getIonMappedSizeKb();
Suren Baghdasaryan2018bf02019-12-18 09:52:42 -08002593
2594 /**
2595 * Return whether virtually-mapped kernel stacks are enabled (CONFIG_VMAP_STACK).
2596 * Note: caller needs config_gz read sepolicy permission
2597 *
2598 * @hide
2599 */
2600 public static native boolean isVmapStack();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601}