blob: 3249bcb517433f6cf2320e2133260a2fd6637744 [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
Andy McFaddend11ca4d2012-10-22 16:16:06 -070019import android.util.Log;
20
Jeff Brown481c1572012-03-09 14:41:15 -080021/**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070022 * Writes trace events to the system trace buffer. These trace events can be
23 * collected and visualized using the Systrace tool.
Jeff Brown481c1572012-03-09 14:41:15 -080024 *
Scott Mainc4682402013-06-13 12:38:55 -070025 * <p>This tracing mechanism is independent of the method tracing mechanism
Jeff Brown481c1572012-03-09 14:41:15 -080026 * offered by {@link Debug#startMethodTracing}. In particular, it enables
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070027 * tracing of events that occur across multiple processes.
Scott Mainc4682402013-06-13 12:38:55 -070028 * <p>For information about using the Systrace tool, read <a
29 * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
30 * with Systrace</a>.
Jeff Brown481c1572012-03-09 14:41:15 -080031 */
32public final class Trace {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070033 /*
34 * Writes trace events to the kernel trace buffer. These trace events can be
35 * collected using the "atrace" program for offline analysis.
36 */
37
Andy McFaddend11ca4d2012-10-22 16:16:06 -070038 private static final String TAG = "Trace";
39
Alex Ray8a6787b2012-11-14 18:08:49 -080040 // These tags must be kept in sync with system/core/include/cutils/trace.h.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070041 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080042 public static final long TRACE_TAG_NEVER = 0;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070043 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080044 public static final long TRACE_TAG_ALWAYS = 1L << 0;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070045 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080046 public static final long TRACE_TAG_GRAPHICS = 1L << 1;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070047 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080048 public static final long TRACE_TAG_INPUT = 1L << 2;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070049 /** @hide */
Jeff Brown481c1572012-03-09 14:41:15 -080050 public static final long TRACE_TAG_VIEW = 1L << 3;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070051 /** @hide */
Chris Craik192a65e2012-04-16 16:08:40 -070052 public static final long TRACE_TAG_WEBVIEW = 1L << 4;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070053 /** @hide */
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070054 public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070055 /** @hide */
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070056 public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070057 /** @hide */
Andy Stadler09b45a32012-05-03 15:00:49 -070058 public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070059 /** @hide */
Glenn Kastened853fc2012-05-07 11:05:55 -070060 public static final long TRACE_TAG_AUDIO = 1L << 8;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070061 /** @hide */
Jamie Gennis24dae6c2012-05-11 04:43:35 -070062 public static final long TRACE_TAG_VIDEO = 1L << 9;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070063 /** @hide */
Eino-Ville Talvala9c0d9cf2012-05-31 15:49:31 -070064 public static final long TRACE_TAG_CAMERA = 1L << 10;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070065 /** @hide */
Alex Ray8a6787b2012-11-14 18:08:49 -080066 public static final long TRACE_TAG_HAL = 1L << 11;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070067 /** @hide */
68 public static final long TRACE_TAG_APP = 1L << 12;
Dianne Hackbornf7be4802013-04-12 14:52:58 -070069 /** @hide */
70 public static final long TRACE_TAG_RESOURCES = 1L << 13;
Jamie Gennis0cc84ce2013-05-07 15:22:31 -070071 /** @hide */
72 public static final long TRACE_TAG_DALVIK = 1L << 14;
Tim Murray6d7a53c2013-05-23 16:59:23 -070073 /** @hide */
74 public static final long TRACE_TAG_RS = 1L << 15;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070075
Andy McFadden325be8a2012-10-29 16:42:41 -070076 private static final long TRACE_TAG_NOT_READY = 1L << 63;
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070077 private static final int MAX_SECTION_NAME_LEN = 127;
Dianne Hackborn83e6eb12012-05-08 14:53:24 -070078
Andy McFaddend11ca4d2012-10-22 16:16:06 -070079 // Must be volatile to avoid word tearing.
Andy McFadden325be8a2012-10-29 16:42:41 -070080 private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
Jeff Brown481c1572012-03-09 14:41:15 -080081
82 private static native long nativeGetEnabledTags();
83 private static native void nativeTraceCounter(long tag, String name, int value);
84 private static native void nativeTraceBegin(long tag, String name);
85 private static native void nativeTraceEnd(long tag);
Romain Guy9425f922013-04-10 16:35:48 -070086 private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
87 private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -070088 private static native void nativeSetAppTracingAllowed(boolean allowed);
Jamie Gennis6ad04522013-04-15 18:53:24 -070089 private static native void nativeSetTracingEnabled(boolean allowed);
Jeff Brown481c1572012-03-09 14:41:15 -080090
Dianne Hackborna53de062012-05-08 18:53:51 -070091 static {
Andy McFaddend11ca4d2012-10-22 16:16:06 -070092 // We configure two separate change callbacks, one in Trace.cpp and one here. The
93 // native callback reads the tags from the system property, and this callback
94 // reads the value that the native code retrieved. It's essential that the native
95 // callback executes first.
96 //
97 // The system provides ordering through a priority level. Callbacks made through
98 // SystemProperties.addChangeCallback currently have a negative priority, while
99 // our native code is using a priority of zero.
Dianne Hackborna53de062012-05-08 18:53:51 -0700100 SystemProperties.addChangeCallback(new Runnable() {
101 @Override public void run() {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700102 cacheEnabledTags();
Dianne Hackborna53de062012-05-08 18:53:51 -0700103 }
104 });
105 }
106
Jeff Brown481c1572012-03-09 14:41:15 -0800107 private Trace() {
108 }
109
110 /**
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700111 * Caches a copy of the enabled-tag bits. The "master" copy is held by the native code,
112 * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
113 * <p>
114 * If the native code hasn't yet read the property, we will cause it to do one-time
115 * initialization. We don't want to do this during class init, because this class is
116 * preloaded, so all apps would be stuck with whatever the zygote saw. (The zygote
117 * doesn't see the system-property update broadcasts.)
118 * <p>
119 * We want to defer initialization until the first use by an app, post-zygote.
120 * <p>
121 * We're okay if multiple threads call here simultaneously -- the native state is
122 * synchronized, and sEnabledTags is volatile (prevents word tearing).
123 */
124 private static long cacheEnabledTags() {
125 long tags = nativeGetEnabledTags();
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700126 sEnabledTags = tags;
127 return tags;
128 }
129
130 /**
Jeff Brown481c1572012-03-09 14:41:15 -0800131 * Returns true if a trace tag is enabled.
132 *
133 * @param traceTag The trace tag to check.
134 * @return True if the trace tag is valid.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700135 *
136 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800137 */
138 public static boolean isTagEnabled(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700139 long tags = sEnabledTags;
Andy McFadden325be8a2012-10-29 16:42:41 -0700140 if (tags == TRACE_TAG_NOT_READY) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700141 tags = cacheEnabledTags();
142 }
143 return (tags & traceTag) != 0;
Jeff Brown481c1572012-03-09 14:41:15 -0800144 }
145
146 /**
147 * Writes trace message to indicate the value of a given counter.
148 *
149 * @param traceTag The trace tag.
150 * @param counterName The counter name to appear in the trace.
151 * @param counterValue The counter value.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700152 *
153 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800154 */
155 public static void traceCounter(long traceTag, String counterName, int counterValue) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700156 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800157 nativeTraceCounter(traceTag, counterName, counterValue);
158 }
159 }
160
161 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700162 * Set whether application tracing is allowed for this process. This is intended to be set
163 * once at application start-up time based on whether the application is debuggable.
164 *
165 * @hide
166 */
167 public static void setAppTracingAllowed(boolean allowed) {
168 nativeSetAppTracingAllowed(allowed);
169
170 // Setting whether app tracing is allowed may change the tags, so we update the cached
171 // tags here.
172 cacheEnabledTags();
173 }
174
175 /**
Jamie Gennis6ad04522013-04-15 18:53:24 -0700176 * Set whether tracing is enabled in this process. Tracing is disabled shortly after Zygote
177 * initializes and re-enabled after processes fork from Zygote. This is done because Zygote
178 * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
179 * caches the tracing tags, forked processes will inherit those stale tags.
180 *
181 * @hide
182 */
183 public static void setTracingEnabled(boolean enabled) {
184 nativeSetTracingEnabled(enabled);
185
186 // Setting whether tracing is enabled may change the tags, so we update the cached tags
187 // here.
188 cacheEnabledTags();
189 }
190
191 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700192 * Writes a trace message to indicate that a given section of code has
193 * begun. Must be followed by a call to {@link #traceEnd} using the same
194 * tag.
Jeff Brown481c1572012-03-09 14:41:15 -0800195 *
196 * @param traceTag The trace tag.
197 * @param methodName The method name to appear in the trace.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700198 *
199 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800200 */
201 public static void traceBegin(long traceTag, String methodName) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700202 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800203 nativeTraceBegin(traceTag, methodName);
204 }
205 }
206
207 /**
208 * Writes a trace message to indicate that the current method has ended.
209 * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
210 *
211 * @param traceTag The trace tag.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700212 *
213 * @hide
Jeff Brown481c1572012-03-09 14:41:15 -0800214 */
215 public static void traceEnd(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700216 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800217 nativeTraceEnd(traceTag);
218 }
219 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700220
221 /**
Romain Guy9425f922013-04-10 16:35:48 -0700222 * Writes a trace message to indicate that a given section of code has
223 * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
224 * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
225 * asynchronous events do not need to be nested. The name and cookie used to
226 * begin an event must be used to end it.
227 *
228 * @param traceTag The trace tag.
229 * @param methodName The method name to appear in the trace.
230 * @param cookie Unique identifier for distinguishing simultaneous events
231 *
232 * @hide
233 */
234 public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
235 if (isTagEnabled(traceTag)) {
236 nativeAsyncTraceBegin(traceTag, methodName, cookie);
237 }
238 }
239
240 /**
241 * Writes a trace message to indicate that the current method has ended.
242 * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
243 * using the same tag, name and cookie.
244 *
245 * @param traceTag The trace tag.
246 * @param methodName The method name to appear in the trace.
247 * @param cookie Unique identifier for distinguishing simultaneous events
248 *
249 * @hide
250 */
251 public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
252 if (isTagEnabled(traceTag)) {
253 nativeAsyncTraceEnd(traceTag, methodName, cookie);
254 }
255 }
256
257 /**
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700258 * Writes a trace message to indicate that a given section of code has begun. This call must
Jamie Gennis5800fc82013-04-09 18:37:22 -0700259 * be followed by a corresponding call to {@link #endSection()} on the same thread.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700260 *
261 * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
262 * null character '\0' are used internally by the tracing mechanism. If sectionName contains
263 * these characters they will be replaced with a space character in the trace.
264 *
265 * @param sectionName The name of the code section to appear in the trace. This may be at
266 * most 127 Unicode code units long.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700267 */
Jamie Gennis5800fc82013-04-09 18:37:22 -0700268 public static void beginSection(String sectionName) {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700269 if (isTagEnabled(TRACE_TAG_APP)) {
270 if (sectionName.length() > MAX_SECTION_NAME_LEN) {
271 throw new IllegalArgumentException("sectionName is too long");
272 }
273 nativeTraceBegin(TRACE_TAG_APP, sectionName);
274 }
275 }
276
277 /**
278 * Writes a trace message to indicate that a given section of code has ended. This call must
Jamie Gennis5800fc82013-04-09 18:37:22 -0700279 * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700280 * 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 -0700281 * ensure that beginSection / endSection pairs are properly nested and called from the same
282 * thread.
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700283 */
Jamie Gennis5800fc82013-04-09 18:37:22 -0700284 public static void endSection() {
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -0700285 if (isTagEnabled(TRACE_TAG_APP)) {
286 nativeTraceEnd(TRACE_TAG_APP);
287 }
288 }
Jeff Brown481c1572012-03-09 14:41:15 -0800289}