Trace.java revision 6ad0452e6301c0650f58f3991f7c523f6f279ddb
1481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown/*
2481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * Copyright (C) 2012 The Android Open Source Project
3481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
4481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * Licensed under the Apache License, Version 2.0 (the "License");
5481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * you may not use this file except in compliance with the License.
6481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * You may obtain a copy of the License at
7481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
8481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *      http://www.apache.org/licenses/LICENSE-2.0
9481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
10481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * Unless required by applicable law or agreed to in writing, software
11481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * distributed under the License is distributed on an "AS IS" BASIS,
12481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * See the License for the specific language governing permissions and
14481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * limitations under the License.
15481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown */
16481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
17481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brownpackage android.os;
18481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
19d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFaddenimport android.util.Log;
20d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
21481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown/**
22f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * Writes trace events to the system trace buffer.  These trace events can be
23f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * collected and visualized using the Systrace tool.
24481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
25481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * This tracing mechanism is independent of the method tracing mechanism
26481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * offered by {@link Debug#startMethodTracing}.  In particular, it enables
27f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * tracing of events that occur across multiple processes.
28481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown */
29481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brownpublic final class Trace {
30f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /*
31f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes trace events to the kernel trace buffer.  These trace events can be
32f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * collected using the "atrace" program for offline analysis.
33f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
34f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
35d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static final String TAG = "Trace";
36d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
378a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    // These tags must be kept in sync with system/core/include/cutils/trace.h.
38f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
39481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_NEVER = 0;
40f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
41481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_ALWAYS = 1L << 0;
42f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
43481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_GRAPHICS = 1L << 1;
44f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
45481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_INPUT = 1L << 2;
46f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
47481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_VIEW = 1L << 3;
48f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
49192a65e9f6ebdc452520e19f95c68c270b3f96daChris Craik    public static final long TRACE_TAG_WEBVIEW = 1L << 4;
50f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
511ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
52f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
531ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
54f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
5509b45a3ad96379b4181d32f8391f63e9c57dc316Andy Stadler    public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
56f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
57ed853fc4e0ba8ce0692d65064e12cf129b5d1f3eGlenn Kasten    public static final long TRACE_TAG_AUDIO = 1L << 8;
58f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
5924dae6c611455ec38675554033e4d18810d77d6cJamie Gennis    public static final long TRACE_TAG_VIDEO = 1L << 9;
60f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
619c0d9cf25ffdf4f6545e489b22ba621bf0b7ba29Eino-Ville Talvala    public static final long TRACE_TAG_CAMERA = 1L << 10;
62f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
638a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    public static final long TRACE_TAG_HAL = 1L << 11;
64f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
65f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static final long TRACE_TAG_APP = 1L << 12;
66f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    /** @hide */
67f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    public static final long TRACE_TAG_RESOURCES = 1L << 13;
6883e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
69f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final long TRACE_TAG_NOT_READY = 1L << 63;
70f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final int MAX_SECTION_NAME_LEN = 127;
7183e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
72d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    // Must be volatile to avoid word tearing.
73325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden    private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
74481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
75481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native long nativeGetEnabledTags();
76481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceCounter(long tag, String name, int value);
77481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceBegin(long tag, String name);
78481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceEnd(long tag);
799425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
809425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
81f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static native void nativeSetAppTracingAllowed(boolean allowed);
826ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    private static native void nativeSetTracingEnabled(boolean allowed);
83481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
84a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    static {
85d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // We configure two separate change callbacks, one in Trace.cpp and one here.  The
86d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // native callback reads the tags from the system property, and this callback
87d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // reads the value that the native code retrieved.  It's essential that the native
88d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // callback executes first.
89d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        //
90d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // The system provides ordering through a priority level.  Callbacks made through
91d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // SystemProperties.addChangeCallback currently have a negative priority, while
92d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // our native code is using a priority of zero.
93a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        SystemProperties.addChangeCallback(new Runnable() {
94a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            @Override public void run() {
95d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden                cacheEnabledTags();
96a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            }
97a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        });
98a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    }
99a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn
100481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private Trace() {
101481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
102481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
103481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
104d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
105d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
106d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
107d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * If the native code hasn't yet read the property, we will cause it to do one-time
108d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * initialization.  We don't want to do this during class init, because this class is
109d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
110d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * doesn't see the system-property update broadcasts.)
111d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
112d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We want to defer initialization until the first use by an app, post-zygote.
113d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
114d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We're okay if multiple threads call here simultaneously -- the native state is
115d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * synchronized, and sEnabledTags is volatile (prevents word tearing).
116d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     */
117d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static long cacheEnabledTags() {
118d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = nativeGetEnabledTags();
119d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        sEnabledTags = tags;
120d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return tags;
121d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    }
122d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
123d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    /**
124481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Returns true if a trace tag is enabled.
125481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
126481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag to check.
127481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @return True if the trace tag is valid.
128f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
129f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
130481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
131481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static boolean isTagEnabled(long traceTag) {
132d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = sEnabledTags;
133325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden        if (tags == TRACE_TAG_NOT_READY) {
134d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden            tags = cacheEnabledTags();
135d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        }
136d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return (tags & traceTag) != 0;
137481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
138481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
139481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
140481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes trace message to indicate the value of a given counter.
141481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
142481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
143481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterName The counter name to appear in the trace.
144481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterValue The counter value.
145f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
146f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
147481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
148481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceCounter(long traceTag, String counterName, int counterValue) {
149d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
150481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceCounter(traceTag, counterName, counterValue);
151481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
152481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
153481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
154481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
155f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Set whether application tracing is allowed for this process.  This is intended to be set
156f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * once at application start-up time based on whether the application is debuggable.
157f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
158f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
159f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
160f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static void setAppTracingAllowed(boolean allowed) {
161f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        nativeSetAppTracingAllowed(allowed);
162f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
163f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // Setting whether app tracing is allowed may change the tags, so we update the cached
164f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // tags here.
165f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        cacheEnabledTags();
166f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
167f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
168f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
1696ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
1706ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
1716ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
1726ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * caches the tracing tags, forked processes will inherit those stale tags.
1736ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     *
1746ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * @hide
1756ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     */
1766ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    public static void setTracingEnabled(boolean enabled) {
1776ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        nativeSetTracingEnabled(enabled);
1786ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1796ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // Setting whether tracing is enabled may change the tags, so we update the cached tags
1806ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // here.
1816ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        cacheEnabledTags();
1826ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    }
1836ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1846ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    /**
185f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has
186f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * begun. Must be followed by a call to {@link #traceEnd} using the same
187f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * tag.
188481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
189481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
190481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param methodName The method name to appear in the trace.
191f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
192f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
193481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
194481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceBegin(long traceTag, String methodName) {
195d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
196481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceBegin(traceTag, methodName);
197481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
198481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
199481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
200481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
201481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that the current method has ended.
202481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
203481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
204481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
205f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
206f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
207481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
208481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceEnd(long traceTag) {
209d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
210481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceEnd(traceTag);
211481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
212481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
213f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
214f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
2159425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that a given section of code has
2169425f923fae8977d09d924436148d3808032ea98Romain Guy     * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
2179425f923fae8977d09d924436148d3808032ea98Romain Guy     * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
2189425f923fae8977d09d924436148d3808032ea98Romain Guy     * asynchronous events do not need to be nested. The name and cookie used to
2199425f923fae8977d09d924436148d3808032ea98Romain Guy     * begin an event must be used to end it.
2209425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2219425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2229425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2239425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2249425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2259425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2269425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2279425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
2289425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2299425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceBegin(traceTag, methodName, cookie);
2309425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2319425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2329425f923fae8977d09d924436148d3808032ea98Romain Guy
2339425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
2349425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that the current method has ended.
2359425f923fae8977d09d924436148d3808032ea98Romain Guy     * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
2369425f923fae8977d09d924436148d3808032ea98Romain Guy     * using the same tag, name and cookie.
2379425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2389425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2399425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2409425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2419425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2429425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2439425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2449425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
2459425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2469425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceEnd(traceTag, methodName, cookie);
2479425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2489425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2499425f923fae8977d09d924436148d3808032ea98Romain Guy
2509425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
251f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has begun. This call must
2525800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be followed by a corresponding call to {@link #endSection()} on the same thread.
253f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
254f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
255f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
256f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * these characters they will be replaced with a space character in the trace.
257f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
258f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @param sectionName The name of the code section to appear in the trace.  This may be at
259f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * most 127 Unicode code units long.
260f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2615800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void beginSection(String sectionName) {
262f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
263f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            if (sectionName.length() > MAX_SECTION_NAME_LEN) {
264f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis                throw new IllegalArgumentException("sectionName is too long");
265f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            }
266f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceBegin(TRACE_TAG_APP, sectionName);
267f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
268f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
269f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
270f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
271f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has ended. This call must
2725800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
273f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * will mark the end of the most recently begun section of code, so care must be taken to
2745800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * ensure that beginSection / endSection pairs are properly nested and called from the same
2755800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * thread.
276f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2775800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void endSection() {
278f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
279f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceEnd(TRACE_TAG_APP);
280f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
281f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
282481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown}
283