blob: 0ca9183f6562044ca46e3c3a54e596203d80590e [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/**
22 * Writes trace events to the kernel trace buffer. These trace events can be
23 * collected using the "atrace" program for offline analysis.
24 *
25 * This tracing mechanism is independent of the method tracing mechanism
26 * offered by {@link Debug#startMethodTracing}. In particular, it enables
27 * tracing of events that occur across processes.
28 *
29 * @hide
30 */
31public final class Trace {
Andy McFaddend11ca4d2012-10-22 16:16:06 -070032 private static final String TAG = "Trace";
33
Jeff Brown481c1572012-03-09 14:41:15 -080034 // These tags must be kept in sync with frameworks/native/include/utils/Trace.h.
35 public static final long TRACE_TAG_NEVER = 0;
36 public static final long TRACE_TAG_ALWAYS = 1L << 0;
37 public static final long TRACE_TAG_GRAPHICS = 1L << 1;
38 public static final long TRACE_TAG_INPUT = 1L << 2;
39 public static final long TRACE_TAG_VIEW = 1L << 3;
Chris Craik192a65e2012-04-16 16:08:40 -070040 public static final long TRACE_TAG_WEBVIEW = 1L << 4;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070041 public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
42 public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
Andy Stadler09b45a32012-05-03 15:00:49 -070043 public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
Glenn Kastened853fc2012-05-07 11:05:55 -070044 public static final long TRACE_TAG_AUDIO = 1L << 8;
Jamie Gennis24dae6c2012-05-11 04:43:35 -070045 public static final long TRACE_TAG_VIDEO = 1L << 9;
Eino-Ville Talvala9c0d9cf2012-05-31 15:49:31 -070046 public static final long TRACE_TAG_CAMERA = 1L << 10;
Andy McFadden325be8a2012-10-29 16:42:41 -070047 private static final long TRACE_TAG_NOT_READY = 1L << 63;
Jeff Brown481c1572012-03-09 14:41:15 -080048
Dianne Hackborn83e6eb12012-05-08 14:53:24 -070049 public static final int TRACE_FLAGS_START_BIT = 1;
50 public static final String[] TRACE_TAGS = {
51 "Graphics", "Input", "View", "WebView", "Window Manager",
Eino-Ville Talvala9c0d9cf2012-05-31 15:49:31 -070052 "Activity Manager", "Sync Manager", "Audio", "Video", "Camera",
Dianne Hackborn83e6eb12012-05-08 14:53:24 -070053 };
54
55 public static final String PROPERTY_TRACE_TAG_ENABLEFLAGS = "debug.atrace.tags.enableflags";
56
Andy McFaddend11ca4d2012-10-22 16:16:06 -070057 // Must be volatile to avoid word tearing.
Andy McFadden325be8a2012-10-29 16:42:41 -070058 private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
Jeff Brown481c1572012-03-09 14:41:15 -080059
60 private static native long nativeGetEnabledTags();
61 private static native void nativeTraceCounter(long tag, String name, int value);
62 private static native void nativeTraceBegin(long tag, String name);
63 private static native void nativeTraceEnd(long tag);
64
Dianne Hackborna53de062012-05-08 18:53:51 -070065 static {
Andy McFaddend11ca4d2012-10-22 16:16:06 -070066 // We configure two separate change callbacks, one in Trace.cpp and one here. The
67 // native callback reads the tags from the system property, and this callback
68 // reads the value that the native code retrieved. It's essential that the native
69 // callback executes first.
70 //
71 // The system provides ordering through a priority level. Callbacks made through
72 // SystemProperties.addChangeCallback currently have a negative priority, while
73 // our native code is using a priority of zero.
Dianne Hackborna53de062012-05-08 18:53:51 -070074 SystemProperties.addChangeCallback(new Runnable() {
75 @Override public void run() {
Andy McFaddend11ca4d2012-10-22 16:16:06 -070076 cacheEnabledTags();
Dianne Hackborna53de062012-05-08 18:53:51 -070077 }
78 });
79 }
80
Jeff Brown481c1572012-03-09 14:41:15 -080081 private Trace() {
82 }
83
84 /**
Andy McFaddend11ca4d2012-10-22 16:16:06 -070085 * Caches a copy of the enabled-tag bits. The "master" copy is held by the native code,
86 * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
87 * <p>
88 * If the native code hasn't yet read the property, we will cause it to do one-time
89 * initialization. We don't want to do this during class init, because this class is
90 * preloaded, so all apps would be stuck with whatever the zygote saw. (The zygote
91 * doesn't see the system-property update broadcasts.)
92 * <p>
93 * We want to defer initialization until the first use by an app, post-zygote.
94 * <p>
95 * We're okay if multiple threads call here simultaneously -- the native state is
96 * synchronized, and sEnabledTags is volatile (prevents word tearing).
97 */
98 private static long cacheEnabledTags() {
99 long tags = nativeGetEnabledTags();
Andy McFadden325be8a2012-10-29 16:42:41 -0700100 if (tags == TRACE_TAG_NOT_READY) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700101 Log.w(TAG, "Unexpected value from nativeGetEnabledTags: " + tags);
102 // keep going
103 }
104 sEnabledTags = tags;
105 return tags;
106 }
107
108 /**
Jeff Brown481c1572012-03-09 14:41:15 -0800109 * Returns true if a trace tag is enabled.
110 *
111 * @param traceTag The trace tag to check.
112 * @return True if the trace tag is valid.
113 */
114 public static boolean isTagEnabled(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700115 long tags = sEnabledTags;
Andy McFadden325be8a2012-10-29 16:42:41 -0700116 if (tags == TRACE_TAG_NOT_READY) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700117 tags = cacheEnabledTags();
118 }
119 return (tags & traceTag) != 0;
Jeff Brown481c1572012-03-09 14:41:15 -0800120 }
121
122 /**
123 * Writes trace message to indicate the value of a given counter.
124 *
125 * @param traceTag The trace tag.
126 * @param counterName The counter name to appear in the trace.
127 * @param counterValue The counter value.
128 */
129 public static void traceCounter(long traceTag, String counterName, int counterValue) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700130 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800131 nativeTraceCounter(traceTag, counterName, counterValue);
132 }
133 }
134
135 /**
136 * Writes a trace message to indicate that a given method has begun.
137 * Must be followed by a call to {@link #traceEnd} using the same tag.
138 *
139 * @param traceTag The trace tag.
140 * @param methodName The method name to appear in the trace.
141 */
142 public static void traceBegin(long traceTag, String methodName) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700143 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800144 nativeTraceBegin(traceTag, methodName);
145 }
146 }
147
148 /**
149 * Writes a trace message to indicate that the current method has ended.
150 * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
151 *
152 * @param traceTag The trace tag.
153 */
154 public static void traceEnd(long traceTag) {
Andy McFaddend11ca4d2012-10-22 16:16:06 -0700155 if (isTagEnabled(traceTag)) {
Jeff Brown481c1572012-03-09 14:41:15 -0800156 nativeTraceEnd(traceTag);
157 }
158 }
159}