blob: 22252968e8a918eb193d3638ef57c71c00cbe373 [file] [log] [blame]
Jeff Brown481c1572012-03-09 14:41:15 -08001/*
2 * Copyright (C) 2012 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
19/**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070020 * Writes trace events to the system trace buffer. These trace events can be
21 * collected and visualized using the Systrace tool.
Jeff Brown481c1572012-03-09 14:41:15 -080022 *
Scott Mainc4682402013-06-13 12:38:55 -070023 * <p>This tracing mechanism is independent of the method tracing mechanism
Jeff Brown481c1572012-03-09 14:41:15 -080024 * offered by {@link Debug#startMethodTracing}. In particular, it enables
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070025 * tracing of events that occur across multiple processes.
Scott Mainc4682402013-06-13 12:38:55 -070026 * <p>For information about using the Systrace tool, read <a
27 * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
28 * with Systrace</a>.
Jeff Brown481c1572012-03-09 14:41:15 -080029 */
30public final class Trace {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070031 /*
32 * Writes trace events to the kernel trace buffer. These trace events can be
33 * collected using the "atrace" program for offline analysis.
34 */
35
Andy McFaddend11ca4d2012-10-22 16:16:06 -070036 private static final String TAG = "Trace";
37
Alex Ray8a6787b2012-11-14 18:08:49 -080038 // These tags must be kept in sync with system/core/include/cutils/trace.h.
Jeff Brown3edf5272014-08-14 19:25:14 -070039 // They should also be added to frameworks/native/cmds/atrace/atrace.cpp.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070040 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080041 public static final long TRACE_TAG_NEVER = 0;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070042 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080043 public static final long TRACE_TAG_ALWAYS = 1L << 0;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070044 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080045 public static final long TRACE_TAG_GRAPHICS = 1L << 1;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070046 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080047 public static final long TRACE_TAG_INPUT = 1L << 2;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070048 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080049 public static final long TRACE_TAG_VIEW = 1L << 3;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070050 /** @hide */
Chris Craik192a65e2012-04-16 16:08:40 -070051 public static final long TRACE_TAG_WEBVIEW = 1L << 4;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070052 /** @hide */
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070053 public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070054 /** @hide */
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070055 public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070056 /** @hide */
Andy Stadler09b45a32012-05-03 15:00:49 -070057 public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070058 /** @hide */
Glenn Kastened853fc2012-05-07 11:05:55 -070059 public static final long TRACE_TAG_AUDIO = 1L << 8;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070060 /** @hide */
Jamie Gennis24dae6c2012-05-11 04:43:35 -070061 public static final long TRACE_TAG_VIDEO = 1L << 9;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070062 /** @hide */
Eino-Ville Talvala9c0d9cf2012-05-31 15:49:31 -070063 public static final long TRACE_TAG_CAMERA = 1L << 10;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070064 /** @hide */
Alex Ray8a6787b2012-11-14 18:08:49 -080065 public static final long TRACE_TAG_HAL = 1L << 11;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070066 /** @hide */
67 public static final long TRACE_TAG_APP = 1L << 12;
Dianne Hackbornf7be4802013-04-12 14:52:58 -070068 /** @hide */
69 public static final long TRACE_TAG_RESOURCES = 1L << 13;
Jamie Gennis0cc84ce2013-05-07 15:22:31 -070070 /** @hide */
71 public static final long TRACE_TAG_DALVIK = 1L << 14;
Tim Murray6d7a53c2013-05-23 16:59:23 -070072 /** @hide */
73 public static final long TRACE_TAG_RS = 1L << 15;
Brigid Smith461ac242014-05-28 14:13:41 -070074 /** @hide */
75 public static final long TRACE_TAG_BIONIC = 1L << 16;
Jeff Brown3edf5272014-08-14 19:25:14 -070076 /** @hide */
77 public static final long TRACE_TAG_POWER = 1L << 17;
Todd Kennedy114beb22015-08-03 09:56:07 -070078 /** @hide */
79 public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18;
Yasuhiro Matsuda1ab43d52015-06-30 17:07:32 +090080 /** @hide */
81 public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
Greg Hackmanne12350f2014-12-01 14:31:21 -080082 /** @hide */
83 public static final long TRACE_TAG_DATABASE = 1L << 20;
Felipe Leme873a83a2016-09-07 11:34:10 -070084 /** @hide */
85 public static final long TRACE_TAG_NETWORK = 1L << 21;
Josh Gao552d86b52016-11-29 10:57:01 -080086 /** @hide */
87 public static final long TRACE_TAG_ADB = 1L << 22;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070088
Andy McFadden325be8a2012-10-29 16:42:41 -070089 private static final long TRACE_TAG_NOT_READY = 1L << 63;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070090 private static final int MAX_SECTION_NAME_LEN = 127;
Dianne Hackborn83e6eb12012-05-08 14:53:24 -070091
Andy McFaddend11ca4d2012-10-22 16:16:06 -070092 // Must be volatile to avoid word tearing.
Andy McFadden325be8a2012-10-29 16:42:41 -070093 private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
Jeff Brown481c1572012-03-09 14:41:15 -080094
95 private static native long nativeGetEnabledTags();
96 private static native void nativeTraceCounter(long tag, String name, int value);
97 private static native void nativeTraceBegin(long tag, String name);
98 private static native void nativeTraceEnd(long tag);
Romain Guy9425f922013-04-10 16:35:48 -070099 private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
100 private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700101 private static native void nativeSetAppTracingAllowed(boolean allowed);
Jamie Gennis6ad04522013-04-15 18:53:24 -0700102 private static native void nativeSetTracingEnabled(boolean allowed);
Jeff Brown481c1572012-03-09 14:41:15 -0800103
Dianne Hackborna53de062012-05-08 18:53:51 -0700104 static {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700105 // We configure two separate change callbacks, one in Trace.cpp and one here. The
106 // native callback reads the tags from the system property, and this callback
107 // reads the value that the native code retrieved. It's essential that the native
108 // callback executes first.
109 //
110 // The system provides ordering through a priority level. Callbacks made through
111 // SystemProperties.addChangeCallback currently have a negative priority, while
112 // our native code is using a priority of zero.
Dianne Hackborna53de062012-05-08 18:53:51 -0700113 SystemProperties.addChangeCallback(new Runnable() {
114 @Override public void run() {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700115 cacheEnabledTags();
Dianne Hackborna53de062012-05-08 18:53:51 -0700116 }
117 });
118 }
119
Jeff Brown481c1572012-03-09 14:41:15 -0800120 private Trace() {
121 }
122
123 /**
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700124 * Caches a copy of the enabled-tag bits. The "master" copy is held by the native code,
125 * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
126 * <p>
127 * If the native code hasn't yet read the property, we will cause it to do one-time
128 * initialization. We don't want to do this during class init, because this class is
129 * preloaded, so all apps would be stuck with whatever the zygote saw. (The zygote
130 * doesn't see the system-property update broadcasts.)
131 * <p>
132 * We want to defer initialization until the first use by an app, post-zygote.
133 * <p>
134 * We're okay if multiple threads call here simultaneously -- the native state is
135 * synchronized, and sEnabledTags is volatile (prevents word tearing).
136 */
137 private static long cacheEnabledTags() {
138 long tags = nativeGetEnabledTags();
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700139 sEnabledTags = tags;
140 return tags;
141 }
142
143 /**
Jeff Brown481c1572012-03-09 14:41:15 -0800144 * Returns true if a trace tag is enabled.
145 *
146 * @param traceTag The trace tag to check.
147 * @return True if the trace tag is valid.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700148 *
149 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800150 */
151 public static boolean isTagEnabled(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700152 long tags = sEnabledTags;
Andy McFadden325be8a2012-10-29 16:42:41 -0700153 if (tags == TRACE_TAG_NOT_READY) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700154 tags = cacheEnabledTags();
155 }
156 return (tags & traceTag) != 0;
Jeff Brown481c1572012-03-09 14:41:15 -0800157 }
158
159 /**
160 * Writes trace message to indicate the value of a given counter.
161 *
162 * @param traceTag The trace tag.
163 * @param counterName The counter name to appear in the trace.
164 * @param counterValue The counter value.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700165 *
166 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800167 */
168 public static void traceCounter(long traceTag, String counterName, int counterValue) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700169 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800170 nativeTraceCounter(traceTag, counterName, counterValue);
171 }
172 }
173
174 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700175 * Set whether application tracing is allowed for this process. This is intended to be set
176 * once at application start-up time based on whether the application is debuggable.
177 *
178 * @hide
179 */
180 public static void setAppTracingAllowed(boolean allowed) {
181 nativeSetAppTracingAllowed(allowed);
182
183 // Setting whether app tracing is allowed may change the tags, so we update the cached
184 // tags here.
185 cacheEnabledTags();
186 }
187
188 /**
Jamie Gennis6ad04522013-04-15 18:53:24 -0700189 * Set whether tracing is enabled in this process. Tracing is disabled shortly after Zygote
190 * initializes and re-enabled after processes fork from Zygote. This is done because Zygote
191 * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
192 * caches the tracing tags, forked processes will inherit those stale tags.
193 *
194 * @hide
195 */
196 public static void setTracingEnabled(boolean enabled) {
197 nativeSetTracingEnabled(enabled);
198
199 // Setting whether tracing is enabled may change the tags, so we update the cached tags
200 // here.
201 cacheEnabledTags();
202 }
203
204 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700205 * Writes a trace message to indicate that a given section of code has
206 * begun. Must be followed by a call to {@link #traceEnd} using the same
207 * tag.
Jeff Brown481c1572012-03-09 14:41:15 -0800208 *
209 * @param traceTag The trace tag.
210 * @param methodName The method name to appear in the trace.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700211 *
212 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800213 */
214 public static void traceBegin(long traceTag, String methodName) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700215 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800216 nativeTraceBegin(traceTag, methodName);
217 }
218 }
219
220 /**
221 * Writes a trace message to indicate that the current method has ended.
222 * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
223 *
224 * @param traceTag The trace tag.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700225 *
226 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800227 */
228 public static void traceEnd(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700229 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800230 nativeTraceEnd(traceTag);
231 }
232 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700233
234 /**
Romain Guy9425f922013-04-10 16:35:48 -0700235 * Writes a trace message to indicate that a given section of code has
236 * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
237 * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
238 * asynchronous events do not need to be nested. The name and cookie used to
239 * begin an event must be used to end it.
240 *
241 * @param traceTag The trace tag.
242 * @param methodName The method name to appear in the trace.
243 * @param cookie Unique identifier for distinguishing simultaneous events
244 *
245 * @hide
246 */
247 public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
248 if (isTagEnabled(traceTag)) {
249 nativeAsyncTraceBegin(traceTag, methodName, cookie);
250 }
251 }
252
253 /**
254 * Writes a trace message to indicate that the current method has ended.
255 * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
256 * using the same tag, name and cookie.
257 *
258 * @param traceTag The trace tag.
259 * @param methodName The method name to appear in the trace.
260 * @param cookie Unique identifier for distinguishing simultaneous events
261 *
262 * @hide
263 */
264 public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
265 if (isTagEnabled(traceTag)) {
266 nativeAsyncTraceEnd(traceTag, methodName, cookie);
267 }
268 }
269
270 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700271 * Writes a trace message to indicate that a given section of code has begun. This call must
Jamie Gennis5800fc82013-04-09 18:37:22 -0700272 * be followed by a corresponding call to {@link #endSection()} on the same thread.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700273 *
274 * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
275 * null character '\0' are used internally by the tracing mechanism. If sectionName contains
276 * these characters they will be replaced with a space character in the trace.
277 *
278 * @param sectionName The name of the code section to appear in the trace. This may be at
279 * most 127 Unicode code units long.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700280 */
Jamie Gennis5800fc82013-04-09 18:37:22 -0700281 public static void beginSection(String sectionName) {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700282 if (isTagEnabled(TRACE_TAG_APP)) {
283 if (sectionName.length() > MAX_SECTION_NAME_LEN) {
284 throw new IllegalArgumentException("sectionName is too long");
285 }
286 nativeTraceBegin(TRACE_TAG_APP, sectionName);
287 }
288 }
289
290 /**
291 * Writes a trace message to indicate that a given section of code has ended. This call must
Jamie Gennis5800fc82013-04-09 18:37:22 -0700292 * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700293 * will mark the end of the most recently begun section of code, so care must be taken to
Jamie Gennis5800fc82013-04-09 18:37:22 -0700294 * ensure that beginSection / endSection pairs are properly nested and called from the same
295 * thread.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700296 */
Jamie Gennis5800fc82013-04-09 18:37:22 -0700297 public static void endSection() {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700298 if (isTagEnabled(TRACE_TAG_APP)) {
299 nativeTraceEnd(TRACE_TAG_APP);
300 }
301 }
Jeff Brown481c1572012-03-09 14:41:15 -0800302}