Debian Codegraph @

core/java/android/os/Trace.java

raw symbols SCIP
1 /*
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
17 package android.os;
18
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.compat.annotation.UnsupportedAppUsage;
24
25 import dalvik.annotation.optimization.CriticalNative;
26 import dalvik.annotation.optimization.FastNative;
27
28 /**
29 * Writes trace events to the system trace buffer. These trace events can be
30 * collected and visualized using the Systrace tool.
31 *
32 * <p>This tracing mechanism is independent of the method tracing mechanism
33 * offered by {@link Debug#startMethodTracing}. In particular, it enables
34 * tracing of events that occur across multiple processes.
35 * <p>For information about using the Systrace tool, read <a
36 * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
37 * with Systrace</a>.
38 */
39 public final class Trace {
40 /*
41 * Writes trace events to the kernel trace buffer. These trace events can be
42 * collected using the "atrace" program for offline analysis.
43 */
44
45 private static final String TAG = "Trace";
46
47 // These tags must be kept in sync with system/core/include/cutils/trace.h.
48 // They should also be added to frameworks/native/cmds/atrace/atrace.cpp.
49 /** @hide */
50 public static final long TRACE_TAG_NEVER = 0;
51 /** @hide */
52 public static final long TRACE_TAG_ALWAYS = 1L << 0;
53 /** @hide */
54 public static final long TRACE_TAG_GRAPHICS = 1L << 1;
55 /** @hide */
56 public static final long TRACE_TAG_INPUT = 1L << 2;
57 /** @hide */
58 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
59 public static final long TRACE_TAG_VIEW = 1L << 3;
60 /** @hide */
61 public static final long TRACE_TAG_WEBVIEW = 1L << 4;
62 /** @hide */
63 public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
64 /** @hide */
65 public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
66 /** @hide */
67 public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
68 /** @hide */
69 public static final long TRACE_TAG_AUDIO = 1L << 8;
70 /** @hide */
71 public static final long TRACE_TAG_VIDEO = 1L << 9;
72 /** @hide */
73 public static final long TRACE_TAG_CAMERA = 1L << 10;
74 /** @hide */
75 public static final long TRACE_TAG_HAL = 1L << 11;
76 /** @hide */
77 @UnsupportedAppUsage
78 public static final long TRACE_TAG_APP = 1L << 12;
79 /** @hide */
80 public static final long TRACE_TAG_RESOURCES = 1L << 13;
81 /** @hide */
82 public static final long TRACE_TAG_DALVIK = 1L << 14;
83 /** @hide */
84 public static final long TRACE_TAG_RS = 1L << 15;
85 /** @hide */
86 public static final long TRACE_TAG_BIONIC = 1L << 16;
87 /** @hide */
88 public static final long TRACE_TAG_POWER = 1L << 17;
89 /** @hide */
90 public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18;
91 /** @hide */
92 public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
93 /** @hide */
94 public static final long TRACE_TAG_DATABASE = 1L << 20;
95 /** @hide */
96 @SystemApi(client = MODULE_LIBRARIES)
97 public static final long TRACE_TAG_NETWORK = 1L << 21;
98 /** @hide */
99 public static final long TRACE_TAG_ADB = 1L << 22;
100 /** @hide */
101 public static final long TRACE_TAG_VIBRATOR = 1L << 23;
102 /** @hide */
103 @SystemApi
104 public static final long TRACE_TAG_AIDL = 1L << 24;
105 /** @hide */
106 public static final long TRACE_TAG_NNAPI = 1L << 25;
107 /** @hide */
108 public static final long TRACE_TAG_RRO = 1L << 26;
109 /** @hide */
110 public static final long TRACE_TAG_THERMAL = 1L << 27;
111
112 private static final long TRACE_TAG_NOT_READY = 1L << 63;
113 private static final int MAX_SECTION_NAME_LEN = 127;
114
115 // Must be volatile to avoid word tearing.
116 // This is only kept in case any apps get this by reflection but do not
117 // check the return value for null.
118 @UnsupportedAppUsage
119 private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
120
121 private static int sZygoteDebugFlags = 0;
122
123 @UnsupportedAppUsage
124 @CriticalNative
125 private static native long nativeGetEnabledTags();
126 private static native void nativeSetAppTracingAllowed(boolean allowed);
127 private static native void nativeSetTracingEnabled(boolean allowed);
128
129 @FastNative
130 private static native void nativeTraceCounter(long tag, String name, long value);
131 @FastNative
132 private static native void nativeTraceBegin(long tag, String name);
133 @FastNative
134 private static native void nativeTraceEnd(long tag);
135 @FastNative
136 private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
137 @FastNative
138 private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
139 @FastNative
140 private static native void nativeAsyncTraceForTrackBegin(long tag,
141 String trackName, String name, int cookie);
142 @FastNative
143 private static native void nativeAsyncTraceForTrackEnd(long tag,
144 String trackName, int cookie);
145 @FastNative
146 private static native void nativeInstant(long tag, String name);
147 @FastNative
148 private static native void nativeInstantForTrack(long tag, String trackName, String name);
149
150 private Trace() {
151 }
152
153 /**
154 * Returns true if a trace tag is enabled.
155 *
156 * @param traceTag The trace tag to check.
157 * @return True if the trace tag is valid.
158 *
159 * @hide
160 */
161 @UnsupportedAppUsage
162 @SystemApi(client = MODULE_LIBRARIES)
163 public static boolean isTagEnabled(long traceTag) {
164 long tags = nativeGetEnabledTags();
165 return (tags & traceTag) != 0;
166 }
167
168 /**
169 * Writes trace message to indicate the value of a given counter.
170 *
171 * @param traceTag The trace tag.
172 * @param counterName The counter name to appear in the trace.
173 * @param counterValue The counter value.
174 *
175 * @hide
176 */
177 @UnsupportedAppUsage
178 @SystemApi(client = MODULE_LIBRARIES)
179 public static void traceCounter(long traceTag, @NonNull String counterName, int counterValue) {
180 if (isTagEnabled(traceTag)) {
181 nativeTraceCounter(traceTag, counterName, counterValue);
182 }
183 }
184
185 /**
186 * From Android S, this is no-op.
187 *
188 * Before, set whether application tracing is allowed for this process. This is intended to be
189 * set once at application start-up time based on whether the application is debuggable.
190 *
191 * @hide
192 */
193 @UnsupportedAppUsage
194 public static void setAppTracingAllowed(boolean allowed) {
195 nativeSetAppTracingAllowed(allowed);
196 }
197
198 /**
199 * Set whether tracing is enabled in this process.
200 * @hide
201 */
202 public static void setTracingEnabled(boolean enabled, int debugFlags) {
203 nativeSetTracingEnabled(enabled);
204 sZygoteDebugFlags = debugFlags;
205 }
206
207 /**
208 * Writes a trace message to indicate that a given section of code has
209 * begun. Must be followed by a call to {@link #traceEnd} using the same
210 * tag.
211 *
212 * @param traceTag The trace tag.
213 * @param methodName The method name to appear in the trace.
214 *
215 * @hide
216 */
217 @UnsupportedAppUsage
218 @SystemApi(client = MODULE_LIBRARIES)
219 public static void traceBegin(long traceTag, @NonNull String methodName) {
220 if (isTagEnabled(traceTag)) {
221 nativeTraceBegin(traceTag, methodName);
222 }
223 }
224
225 /**
226 * Writes a trace message to indicate that the current method has ended.
227 * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
228 *
229 * @param traceTag The trace tag.
230 *
231 * @hide
232 */
233 @UnsupportedAppUsage
234 @SystemApi(client = MODULE_LIBRARIES)
235 public static void traceEnd(long traceTag) {
236 if (isTagEnabled(traceTag)) {
237 nativeTraceEnd(traceTag);
238 }
239 }
240
241 /**
242 * Writes a trace message to indicate that a given section of code has
243 * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
244 * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
245 * asynchronous events do not need to be nested. The name and cookie used to
246 * begin an event must be used to end it.
247 *
248 * @param traceTag The trace tag.
249 * @param methodName The method name to appear in the trace.
250 * @param cookie Unique identifier for distinguishing simultaneous events
251 *
252 * @hide
253 */
254 @UnsupportedAppUsage
255 @SystemApi(client = MODULE_LIBRARIES)
256 public static void asyncTraceBegin(long traceTag, @NonNull String methodName, int cookie) {
257 if (isTagEnabled(traceTag)) {
258 nativeAsyncTraceBegin(traceTag, methodName, cookie);
259 }
260 }
261
262 /**
263 * Writes a trace message to indicate that the current method has ended.
264 * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
265 * using the same tag, name and cookie.
266 *
267 * @param traceTag The trace tag.
268 * @param methodName The method name to appear in the trace.
269 * @param cookie Unique identifier for distinguishing simultaneous events
270 *
271 * @hide
272 */
273 @UnsupportedAppUsage
274 @SystemApi(client = MODULE_LIBRARIES)
275 public static void asyncTraceEnd(long traceTag, @NonNull String methodName, int cookie) {
276 if (isTagEnabled(traceTag)) {
277 nativeAsyncTraceEnd(traceTag, methodName, cookie);
278 }
279 }
280
281
282 /**
283 * Writes a trace message to indicate that a given section of code has
284 * begun. Must be followed by a call to {@link #asyncTraceForTrackEnd} using the same
285 * track name and cookie.
286 * This function operates exactly like {@link #asyncTraceBegin(long, String, int)},
287 * except with the inclusion of a track name argument for where this method should appear.
288 * The cookie must be unique on the trackName level, not the methodName level
289 *
290 * @param traceTag The trace tag.
291 * @param trackName The track where the event should appear in the trace.
292 * @param methodName The method name to appear in the trace.
293 * @param cookie Unique identifier for distinguishing simultaneous events
294 *
295 * @hide
296 */
297 public static void asyncTraceForTrackBegin(long traceTag,
298 @NonNull String trackName, @NonNull String methodName, int cookie) {
299 if (isTagEnabled(traceTag)) {
300 nativeAsyncTraceForTrackBegin(traceTag, trackName, methodName, cookie);
301 }
302 }
303
304 /**
305 * Writes a trace message to indicate that the current method has ended.
306 * Must be called exactly once for each call to
307 * {@link #asyncTraceForTrackBegin(long, String, String, int)}
308 * using the same tag, track name, and cookie.
309 *
310 * @param traceTag The trace tag.
311 * @param trackName The track where the event should appear in the trace.
312 * @param cookie Unique identifier for distinguishing simultaneous events
313 *
314 * @hide
315 */
316 public static void asyncTraceForTrackEnd(long traceTag,
317 @NonNull String trackName, int cookie) {
318 if (isTagEnabled(traceTag)) {
319 nativeAsyncTraceForTrackEnd(traceTag, trackName, cookie);
320 }
321 }
322
323 /**
324 * @deprecated use asyncTraceForTrackEnd without methodName argument
325 *
326 * @hide
327 */
328 @Deprecated
329 public static void asyncTraceForTrackEnd(long traceTag,
330 @NonNull String trackName, @NonNull String methodName, int cookie) {
331 if (isTagEnabled(traceTag)) {
332 nativeAsyncTraceForTrackEnd(traceTag, trackName, cookie);
333 }
334 }
335
336 /**
337 * Writes a trace message to indicate that a given section of code was invoked.
338 *
339 * @param traceTag The trace tag.
340 * @param methodName The method name to appear in the trace.
341 * @hide
342 */
343 public static void instant(long traceTag, String methodName) {
344 if (methodName == null) {
345 throw new IllegalArgumentException("methodName cannot be null");
346 }
347 if (isTagEnabled(traceTag)) {
348 nativeInstant(traceTag, methodName);
349 }
350 }
351
352 /**
353 * Writes a trace message to indicate that a given section of code was invoked.
354 *
355 * @param traceTag The trace tag.
356 * @param trackName The track where the event should appear in the trace.
357 * @param methodName The method name to appear in the trace.
358 * @hide
359 */
360 public static void instantForTrack(long traceTag, String trackName, String methodName) {
361 if (trackName == null) {
362 throw new IllegalArgumentException("trackName cannot be null");
363 }
364 if (methodName == null) {
365 throw new IllegalArgumentException("methodName cannot be null");
366 }
367 if (isTagEnabled(traceTag)) {
368 nativeInstantForTrack(traceTag, trackName, methodName);
369 }
370 }
371
372 /**
373 * Checks whether or not tracing is currently enabled. This is useful to avoid intermediate
374 * string creation for trace sections that require formatting. It is not necessary
375 * to guard all Trace method calls as they internally already check this. However it is
376 * recommended to use this to prevent creating any temporary objects that would then be
377 * passed to those methods to reduce runtime cost when tracing isn't enabled.
378 *
379 * @return true if tracing is currently enabled, false otherwise
380 */
381 public static boolean isEnabled() {
382 return isTagEnabled(TRACE_TAG_APP);
383 }
384
385 /**
386 * Writes a trace message to indicate that a given section of code has begun. This call must
387 * be followed by a corresponding call to {@link #endSection()} on the same thread.
388 *
389 * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
390 * null character '\0' are used internally by the tracing mechanism. If sectionName contains
391 * these characters they will be replaced with a space character in the trace.
392 *
393 * @param sectionName The name of the code section to appear in the trace. This may be at
394 * most 127 Unicode code units long.
395 */
396 public static void beginSection(@NonNull String sectionName) {
397 if (isTagEnabled(TRACE_TAG_APP)) {
398 if (sectionName.length() > MAX_SECTION_NAME_LEN) {
399 throw new IllegalArgumentException("sectionName is too long");
400 }
401 nativeTraceBegin(TRACE_TAG_APP, sectionName);
402 }
403 }
404
405 /**
406 * Writes a trace message to indicate that a given section of code has ended. This call must
407 * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
408 * will mark the end of the most recently begun section of code, so care must be taken to
409 * ensure that beginSection / endSection pairs are properly nested and called from the same
410 * thread.
411 */
412 public static void endSection() {
413 if (isTagEnabled(TRACE_TAG_APP)) {
414 nativeTraceEnd(TRACE_TAG_APP);
415 }
416 }
417
418 /**
419 * Writes a trace message to indicate that a given section of code has
420 * begun. Must be followed by a call to {@link #endAsyncSection(String, int)} with the same
421 * methodName and cookie. Unlike {@link #beginSection(String)} and {@link #endSection()},
422 * asynchronous events do not need to be nested. The name and cookie used to
423 * begin an event must be used to end it.
424 *
425 * @param methodName The method name to appear in the trace.
426 * @param cookie Unique identifier for distinguishing simultaneous events
427 */
428 public static void beginAsyncSection(@NonNull String methodName, int cookie) {
429 asyncTraceBegin(TRACE_TAG_APP, methodName, cookie);
430 }
431
432 /**
433 * Writes a trace message to indicate that the current method has ended.
434 * Must be called exactly once for each call to {@link #beginAsyncSection(String, int)}
435 * using the same name and cookie.
436 *
437 * @param methodName The method name to appear in the trace.
438 * @param cookie Unique identifier for distinguishing simultaneous events
439 */
440 public static void endAsyncSection(@NonNull String methodName, int cookie) {
441 asyncTraceEnd(TRACE_TAG_APP, methodName, cookie);
442 }
443
444 /**
445 * Writes trace message to indicate the value of a given counter.
446 *
447 * @param counterName The counter name to appear in the trace.
448 * @param counterValue The counter value.
449 */
450 public static void setCounter(@NonNull String counterName, long counterValue) {
451 if (isTagEnabled(TRACE_TAG_APP)) {
452 nativeTraceCounter(TRACE_TAG_APP, counterName, counterValue);
453 }
454 }
455 }