• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.internal.os.Zygote;
20 
21 import dalvik.annotation.optimization.FastNative;
22 
23 /**
24  * Writes trace events to the system trace buffer.  These trace events can be
25  * collected and visualized using the Systrace tool.
26  *
27  * <p>This tracing mechanism is independent of the method tracing mechanism
28  * offered by {@link Debug#startMethodTracing}.  In particular, it enables
29  * tracing of events that occur across multiple processes.
30  * <p>For information about using the Systrace tool, read <a
31  * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
32  * with Systrace</a>.
33  */
34 public final class Trace {
35     /*
36      * Writes trace events to the kernel trace buffer.  These trace events can be
37      * collected using the "atrace" program for offline analysis.
38      */
39 
40     private static final String TAG = "Trace";
41 
42     // These tags must be kept in sync with system/core/include/cutils/trace.h.
43     // They should also be added to frameworks/native/cmds/atrace/atrace.cpp.
44     /** @hide */
45     public static final long TRACE_TAG_NEVER = 0;
46     /** @hide */
47     public static final long TRACE_TAG_ALWAYS = 1L << 0;
48     /** @hide */
49     public static final long TRACE_TAG_GRAPHICS = 1L << 1;
50     /** @hide */
51     public static final long TRACE_TAG_INPUT = 1L << 2;
52     /** @hide */
53     public static final long TRACE_TAG_VIEW = 1L << 3;
54     /** @hide */
55     public static final long TRACE_TAG_WEBVIEW = 1L << 4;
56     /** @hide */
57     public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
58     /** @hide */
59     public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
60     /** @hide */
61     public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
62     /** @hide */
63     public static final long TRACE_TAG_AUDIO = 1L << 8;
64     /** @hide */
65     public static final long TRACE_TAG_VIDEO = 1L << 9;
66     /** @hide */
67     public static final long TRACE_TAG_CAMERA = 1L << 10;
68     /** @hide */
69     public static final long TRACE_TAG_HAL = 1L << 11;
70     /** @hide */
71     public static final long TRACE_TAG_APP = 1L << 12;
72     /** @hide */
73     public static final long TRACE_TAG_RESOURCES = 1L << 13;
74     /** @hide */
75     public static final long TRACE_TAG_DALVIK = 1L << 14;
76     /** @hide */
77     public static final long TRACE_TAG_RS = 1L << 15;
78     /** @hide */
79     public static final long TRACE_TAG_BIONIC = 1L << 16;
80     /** @hide */
81     public static final long TRACE_TAG_POWER = 1L << 17;
82     /** @hide */
83     public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18;
84     /** @hide */
85     public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
86     /** @hide */
87     public static final long TRACE_TAG_DATABASE = 1L << 20;
88     /** @hide */
89     public static final long TRACE_TAG_NETWORK = 1L << 21;
90     /** @hide */
91     public static final long TRACE_TAG_ADB = 1L << 22;
92 
93     private static final long TRACE_TAG_NOT_READY = 1L << 63;
94     private static final int MAX_SECTION_NAME_LEN = 127;
95 
96     // Must be volatile to avoid word tearing.
97     private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
98 
99     private static int sZygoteDebugFlags = 0;
100 
nativeGetEnabledTags()101     private static native long nativeGetEnabledTags();
nativeSetAppTracingAllowed(boolean allowed)102     private static native void nativeSetAppTracingAllowed(boolean allowed);
nativeSetTracingEnabled(boolean allowed)103     private static native void nativeSetTracingEnabled(boolean allowed);
104 
105     @FastNative
nativeTraceCounter(long tag, String name, int value)106     private static native void nativeTraceCounter(long tag, String name, int value);
107     @FastNative
nativeTraceBegin(long tag, String name)108     private static native void nativeTraceBegin(long tag, String name);
109     @FastNative
nativeTraceEnd(long tag)110     private static native void nativeTraceEnd(long tag);
111     @FastNative
nativeAsyncTraceBegin(long tag, String name, int cookie)112     private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
113     @FastNative
nativeAsyncTraceEnd(long tag, String name, int cookie)114     private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
115 
116     static {
117         // We configure two separate change callbacks, one in Trace.cpp and one here.  The
118         // native callback reads the tags from the system property, and this callback
119         // reads the value that the native code retrieved.  It's essential that the native
120         // callback executes first.
121         //
122         // The system provides ordering through a priority level.  Callbacks made through
123         // SystemProperties.addChangeCallback currently have a negative priority, while
124         // our native code is using a priority of zero.
SystemProperties.addChangeCallback()125         SystemProperties.addChangeCallback(() -> {
126             cacheEnabledTags();
127             if ((sZygoteDebugFlags & Zygote.DEBUG_JAVA_DEBUGGABLE) != 0) {
128                 traceCounter(TRACE_TAG_ALWAYS, "java_debuggable", 1);
129             }
130         });
131     }
132 
Trace()133     private Trace() {
134     }
135 
136     /**
137      * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
138      * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
139      * <p>
140      * If the native code hasn't yet read the property, we will cause it to do one-time
141      * initialization.  We don't want to do this during class init, because this class is
142      * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
143      * doesn't see the system-property update broadcasts.)
144      * <p>
145      * We want to defer initialization until the first use by an app, post-zygote.
146      * <p>
147      * We're okay if multiple threads call here simultaneously -- the native state is
148      * synchronized, and sEnabledTags is volatile (prevents word tearing).
149      */
cacheEnabledTags()150     private static long cacheEnabledTags() {
151         long tags = nativeGetEnabledTags();
152         sEnabledTags = tags;
153         return tags;
154     }
155 
156     /**
157      * Returns true if a trace tag is enabled.
158      *
159      * @param traceTag The trace tag to check.
160      * @return True if the trace tag is valid.
161      *
162      * @hide
163      */
isTagEnabled(long traceTag)164     public static boolean isTagEnabled(long traceTag) {
165         long tags = sEnabledTags;
166         if (tags == TRACE_TAG_NOT_READY) {
167             tags = cacheEnabledTags();
168         }
169         return (tags & traceTag) != 0;
170     }
171 
172     /**
173      * Writes trace message to indicate the value of a given counter.
174      *
175      * @param traceTag The trace tag.
176      * @param counterName The counter name to appear in the trace.
177      * @param counterValue The counter value.
178      *
179      * @hide
180      */
traceCounter(long traceTag, String counterName, int counterValue)181     public static void traceCounter(long traceTag, String counterName, int counterValue) {
182         if (isTagEnabled(traceTag)) {
183             nativeTraceCounter(traceTag, counterName, counterValue);
184         }
185     }
186 
187     /**
188      * Set whether application tracing is allowed for this process.  This is intended to be set
189      * once at application start-up time based on whether the application is debuggable.
190      *
191      * @hide
192      */
setAppTracingAllowed(boolean allowed)193     public static void setAppTracingAllowed(boolean allowed) {
194         nativeSetAppTracingAllowed(allowed);
195 
196         // Setting whether app tracing is allowed may change the tags, so we update the cached
197         // tags here.
198         cacheEnabledTags();
199     }
200 
201     /**
202      * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
203      * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
204      * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
205      * caches the tracing tags, forked processes will inherit those stale tags.
206      *
207      * @hide
208      */
setTracingEnabled(boolean enabled, int debugFlags)209     public static void setTracingEnabled(boolean enabled, int debugFlags) {
210         nativeSetTracingEnabled(enabled);
211         sZygoteDebugFlags = debugFlags;
212 
213         // Setting whether tracing is enabled may change the tags, so we update the cached tags
214         // here.
215         cacheEnabledTags();
216     }
217 
218     /**
219      * Writes a trace message to indicate that a given section of code has
220      * begun. Must be followed by a call to {@link #traceEnd} using the same
221      * tag.
222      *
223      * @param traceTag The trace tag.
224      * @param methodName The method name to appear in the trace.
225      *
226      * @hide
227      */
traceBegin(long traceTag, String methodName)228     public static void traceBegin(long traceTag, String methodName) {
229         if (isTagEnabled(traceTag)) {
230             nativeTraceBegin(traceTag, methodName);
231         }
232     }
233 
234     /**
235      * Writes a trace message to indicate that the current method has ended.
236      * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
237      *
238      * @param traceTag The trace tag.
239      *
240      * @hide
241      */
traceEnd(long traceTag)242     public static void traceEnd(long traceTag) {
243         if (isTagEnabled(traceTag)) {
244             nativeTraceEnd(traceTag);
245         }
246     }
247 
248     /**
249      * Writes a trace message to indicate that a given section of code has
250      * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
251      * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
252      * asynchronous events do not need to be nested. The name and cookie used to
253      * begin an event must be used to end it.
254      *
255      * @param traceTag The trace tag.
256      * @param methodName The method name to appear in the trace.
257      * @param cookie Unique identifier for distinguishing simultaneous events
258      *
259      * @hide
260      */
asyncTraceBegin(long traceTag, String methodName, int cookie)261     public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
262         if (isTagEnabled(traceTag)) {
263             nativeAsyncTraceBegin(traceTag, methodName, cookie);
264         }
265     }
266 
267     /**
268      * Writes a trace message to indicate that the current method has ended.
269      * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
270      * using the same tag, name and cookie.
271      *
272      * @param traceTag The trace tag.
273      * @param methodName The method name to appear in the trace.
274      * @param cookie Unique identifier for distinguishing simultaneous events
275      *
276      * @hide
277      */
asyncTraceEnd(long traceTag, String methodName, int cookie)278     public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
279         if (isTagEnabled(traceTag)) {
280             nativeAsyncTraceEnd(traceTag, methodName, cookie);
281         }
282     }
283 
284     /**
285      * Writes a trace message to indicate that a given section of code has begun. This call must
286      * be followed by a corresponding call to {@link #endSection()} on the same thread.
287      *
288      * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
289      * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
290      * these characters they will be replaced with a space character in the trace.
291      *
292      * @param sectionName The name of the code section to appear in the trace.  This may be at
293      * most 127 Unicode code units long.
294      */
beginSection(String sectionName)295     public static void beginSection(String sectionName) {
296         if (isTagEnabled(TRACE_TAG_APP)) {
297             if (sectionName.length() > MAX_SECTION_NAME_LEN) {
298                 throw new IllegalArgumentException("sectionName is too long");
299             }
300             nativeTraceBegin(TRACE_TAG_APP, sectionName);
301         }
302     }
303 
304     /**
305      * Writes a trace message to indicate that a given section of code has ended. This call must
306      * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
307      * will mark the end of the most recently begun section of code, so care must be taken to
308      * ensure that beginSection / endSection pairs are properly nested and called from the same
309      * thread.
310      */
endSection()311     public static void endSection() {
312         if (isTagEnabled(TRACE_TAG_APP)) {
313             nativeTraceEnd(TRACE_TAG_APP);
314         }
315     }
316 }
317