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;
680cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    /** @hide */
690cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    public static final long TRACE_TAG_DALVIK = 1L << 14;
7083e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
71f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final long TRACE_TAG_NOT_READY = 1L << 63;
72f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final int MAX_SECTION_NAME_LEN = 127;
7383e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
74d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    // Must be volatile to avoid word tearing.
75325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden    private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
76481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
77481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native long nativeGetEnabledTags();
78481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceCounter(long tag, String name, int value);
79481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceBegin(long tag, String name);
80481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceEnd(long tag);
819425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
829425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
83f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static native void nativeSetAppTracingAllowed(boolean allowed);
846ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    private static native void nativeSetTracingEnabled(boolean allowed);
85481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
86a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    static {
87d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // We configure two separate change callbacks, one in Trace.cpp and one here.  The
88d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // native callback reads the tags from the system property, and this callback
89d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // reads the value that the native code retrieved.  It's essential that the native
90d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // callback executes first.
91d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        //
92d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // The system provides ordering through a priority level.  Callbacks made through
93d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // SystemProperties.addChangeCallback currently have a negative priority, while
94d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // our native code is using a priority of zero.
95a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        SystemProperties.addChangeCallback(new Runnable() {
96a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            @Override public void run() {
97d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden                cacheEnabledTags();
98a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            }
99a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        });
100a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    }
101a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn
102481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private Trace() {
103481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
104481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
105481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
106d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
107d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
108d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
109d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * If the native code hasn't yet read the property, we will cause it to do one-time
110d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * initialization.  We don't want to do this during class init, because this class is
111d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
112d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * doesn't see the system-property update broadcasts.)
113d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
114d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We want to defer initialization until the first use by an app, post-zygote.
115d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
116d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We're okay if multiple threads call here simultaneously -- the native state is
117d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * synchronized, and sEnabledTags is volatile (prevents word tearing).
118d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     */
119d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static long cacheEnabledTags() {
120d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = nativeGetEnabledTags();
121d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        sEnabledTags = tags;
122d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return tags;
123d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    }
124d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
125d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    /**
126481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Returns true if a trace tag is enabled.
127481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
128481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag to check.
129481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @return True if the trace tag is valid.
130f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
131f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
132481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
133481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static boolean isTagEnabled(long traceTag) {
134d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = sEnabledTags;
135325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden        if (tags == TRACE_TAG_NOT_READY) {
136d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden            tags = cacheEnabledTags();
137d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        }
138d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return (tags & traceTag) != 0;
139481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
140481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
141481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
142481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes trace message to indicate the value of a given counter.
143481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
144481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
145481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterName The counter name to appear in the trace.
146481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterValue The counter value.
147f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
148f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
149481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
150481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceCounter(long traceTag, String counterName, int counterValue) {
151d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
152481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceCounter(traceTag, counterName, counterValue);
153481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
154481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
155481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
156481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
157f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Set whether application tracing is allowed for this process.  This is intended to be set
158f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * once at application start-up time based on whether the application is debuggable.
159f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
160f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
161f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
162f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static void setAppTracingAllowed(boolean allowed) {
163f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        nativeSetAppTracingAllowed(allowed);
164f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
165f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // Setting whether app tracing is allowed may change the tags, so we update the cached
166f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // tags here.
167f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        cacheEnabledTags();
168f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
169f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
170f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
1716ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
1726ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
1736ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
1746ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * caches the tracing tags, forked processes will inherit those stale tags.
1756ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     *
1766ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * @hide
1776ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     */
1786ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    public static void setTracingEnabled(boolean enabled) {
1796ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        nativeSetTracingEnabled(enabled);
1806ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1816ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // Setting whether tracing is enabled may change the tags, so we update the cached tags
1826ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // here.
1836ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        cacheEnabledTags();
1846ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    }
1856ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1866ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    /**
187f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has
188f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * begun. Must be followed by a call to {@link #traceEnd} using the same
189f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * tag.
190481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
191481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
192481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param methodName The method name to appear in the trace.
193f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
194f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
195481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
196481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceBegin(long traceTag, String methodName) {
197d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
198481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceBegin(traceTag, methodName);
199481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
200481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
201481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
202481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
203481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that the current method has ended.
204481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
205481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
206481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
207f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
208f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
209481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
210481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceEnd(long traceTag) {
211d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
212481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceEnd(traceTag);
213481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
214481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
215f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
216f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
2179425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that a given section of code has
2189425f923fae8977d09d924436148d3808032ea98Romain Guy     * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
2199425f923fae8977d09d924436148d3808032ea98Romain Guy     * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
2209425f923fae8977d09d924436148d3808032ea98Romain Guy     * asynchronous events do not need to be nested. The name and cookie used to
2219425f923fae8977d09d924436148d3808032ea98Romain Guy     * begin an event must be used to end it.
2229425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2239425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2249425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2259425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2269425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2279425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2289425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2299425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
2309425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2319425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceBegin(traceTag, methodName, cookie);
2329425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2339425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2349425f923fae8977d09d924436148d3808032ea98Romain Guy
2359425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
2369425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that the current method has ended.
2379425f923fae8977d09d924436148d3808032ea98Romain Guy     * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
2389425f923fae8977d09d924436148d3808032ea98Romain Guy     * using the same tag, name and cookie.
2399425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2409425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2419425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2429425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2439425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2449425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2459425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2469425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
2479425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2489425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceEnd(traceTag, methodName, cookie);
2499425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2509425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2519425f923fae8977d09d924436148d3808032ea98Romain Guy
2529425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
253f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has begun. This call must
2545800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be followed by a corresponding call to {@link #endSection()} on the same thread.
255f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
256f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
257f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
258f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * these characters they will be replaced with a space character in the trace.
259f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
260f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @param sectionName The name of the code section to appear in the trace.  This may be at
261f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * most 127 Unicode code units long.
262f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2635800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void beginSection(String sectionName) {
264f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
265f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            if (sectionName.length() > MAX_SECTION_NAME_LEN) {
266f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis                throw new IllegalArgumentException("sectionName is too long");
267f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            }
268f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceBegin(TRACE_TAG_APP, sectionName);
269f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
270f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
271f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
272f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
273f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has ended. This call must
2745800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
275f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * will mark the end of the most recently begun section of code, so care must be taken to
2765800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * ensure that beginSection / endSection pairs are properly nested and called from the same
2775800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * thread.
278f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2795800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void endSection() {
280f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
281f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceEnd(TRACE_TAG_APP);
282f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
283f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
284481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown}
285