Trace.java revision 461ac24b8c2b400656a0bcd72743f03720af2fd0
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 *
25c468240c1a2a393ec02d992f459c6586ae450161Scott Main * <p>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.
28c468240c1a2a393ec02d992f459c6586ae450161Scott Main * <p>For information about using the Systrace tool, read <a
29c468240c1a2a393ec02d992f459c6586ae450161Scott Main * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
30c468240c1a2a393ec02d992f459c6586ae450161Scott Main * with Systrace</a>.
31481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown */
32481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brownpublic final class Trace {
33f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /*
34f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes trace events to the kernel trace buffer.  These trace events can be
35f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * collected using the "atrace" program for offline analysis.
36f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
37f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
38d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static final String TAG = "Trace";
39d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
408a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    // These tags must be kept in sync with system/core/include/cutils/trace.h.
41f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
42481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_NEVER = 0;
43f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
44481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_ALWAYS = 1L << 0;
45f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
46481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_GRAPHICS = 1L << 1;
47f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
48481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_INPUT = 1L << 2;
49f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
50481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_VIEW = 1L << 3;
51f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
52192a65e9f6ebdc452520e19f95c68c270b3f96daChris Craik    public static final long TRACE_TAG_WEBVIEW = 1L << 4;
53f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
541ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
55f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
561ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
57f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
5809b45a3ad96379b4181d32f8391f63e9c57dc316Andy Stadler    public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
59f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
60ed853fc4e0ba8ce0692d65064e12cf129b5d1f3eGlenn Kasten    public static final long TRACE_TAG_AUDIO = 1L << 8;
61f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
6224dae6c611455ec38675554033e4d18810d77d6cJamie Gennis    public static final long TRACE_TAG_VIDEO = 1L << 9;
63f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
649c0d9cf25ffdf4f6545e489b22ba621bf0b7ba29Eino-Ville Talvala    public static final long TRACE_TAG_CAMERA = 1L << 10;
65f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
668a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    public static final long TRACE_TAG_HAL = 1L << 11;
67f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
68f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static final long TRACE_TAG_APP = 1L << 12;
69f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    /** @hide */
70f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    public static final long TRACE_TAG_RESOURCES = 1L << 13;
710cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    /** @hide */
720cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    public static final long TRACE_TAG_DALVIK = 1L << 14;
736d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray    /** @hide */
746d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray    public static final long TRACE_TAG_RS = 1L << 15;
75461ac24b8c2b400656a0bcd72743f03720af2fd0Brigid Smith    /** @hide */
76461ac24b8c2b400656a0bcd72743f03720af2fd0Brigid Smith    public static final long TRACE_TAG_BIONIC = 1L << 16;
7783e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
78f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final long TRACE_TAG_NOT_READY = 1L << 63;
79f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final int MAX_SECTION_NAME_LEN = 127;
8083e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
81d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    // Must be volatile to avoid word tearing.
82325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden    private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
83481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
84481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native long nativeGetEnabledTags();
85481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceCounter(long tag, String name, int value);
86481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceBegin(long tag, String name);
87481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceEnd(long tag);
889425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
899425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
90f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static native void nativeSetAppTracingAllowed(boolean allowed);
916ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    private static native void nativeSetTracingEnabled(boolean allowed);
92481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
93a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    static {
94d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // We configure two separate change callbacks, one in Trace.cpp and one here.  The
95d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // native callback reads the tags from the system property, and this callback
96d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // reads the value that the native code retrieved.  It's essential that the native
97d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // callback executes first.
98d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        //
99d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // The system provides ordering through a priority level.  Callbacks made through
100d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // SystemProperties.addChangeCallback currently have a negative priority, while
101d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // our native code is using a priority of zero.
102a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        SystemProperties.addChangeCallback(new Runnable() {
103a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            @Override public void run() {
104d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden                cacheEnabledTags();
105a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            }
106a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        });
107a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    }
108a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn
109481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private Trace() {
110481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
111481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
112481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
113d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
114d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
115d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
116d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * If the native code hasn't yet read the property, we will cause it to do one-time
117d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * initialization.  We don't want to do this during class init, because this class is
118d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
119d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * doesn't see the system-property update broadcasts.)
120d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
121d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We want to defer initialization until the first use by an app, post-zygote.
122d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
123d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We're okay if multiple threads call here simultaneously -- the native state is
124d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * synchronized, and sEnabledTags is volatile (prevents word tearing).
125d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     */
126d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static long cacheEnabledTags() {
127d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = nativeGetEnabledTags();
128d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        sEnabledTags = tags;
129d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return tags;
130d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    }
131d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
132d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    /**
133481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Returns true if a trace tag is enabled.
134481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
135481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag to check.
136481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @return True if the trace tag is valid.
137f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
138f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
139481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
140481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static boolean isTagEnabled(long traceTag) {
141d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = sEnabledTags;
142325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden        if (tags == TRACE_TAG_NOT_READY) {
143d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden            tags = cacheEnabledTags();
144d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        }
145d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return (tags & traceTag) != 0;
146481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
147481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
148481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
149481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes trace message to indicate the value of a given counter.
150481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
151481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
152481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterName The counter name to appear in the trace.
153481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterValue The counter value.
154f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
155f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
156481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
157481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceCounter(long traceTag, String counterName, int counterValue) {
158d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
159481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceCounter(traceTag, counterName, counterValue);
160481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
161481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
162481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
163481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
164f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Set whether application tracing is allowed for this process.  This is intended to be set
165f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * once at application start-up time based on whether the application is debuggable.
166f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
167f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
168f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
169f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static void setAppTracingAllowed(boolean allowed) {
170f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        nativeSetAppTracingAllowed(allowed);
171f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
172f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // Setting whether app tracing is allowed may change the tags, so we update the cached
173f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // tags here.
174f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        cacheEnabledTags();
175f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
176f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
177f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
1786ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
1796ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
1806ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
1816ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * caches the tracing tags, forked processes will inherit those stale tags.
1826ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     *
1836ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * @hide
1846ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     */
1856ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    public static void setTracingEnabled(boolean enabled) {
1866ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        nativeSetTracingEnabled(enabled);
1876ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1886ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // Setting whether tracing is enabled may change the tags, so we update the cached tags
1896ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // here.
1906ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        cacheEnabledTags();
1916ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    }
1926ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1936ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    /**
194f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has
195f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * begun. Must be followed by a call to {@link #traceEnd} using the same
196f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * tag.
197481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
198481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
199481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param methodName The method name to appear in the trace.
200f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
201f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
202481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
203481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceBegin(long traceTag, String methodName) {
204d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
205481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceBegin(traceTag, methodName);
206481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
207481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
208481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
209481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
210481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that the current method has ended.
211481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
212481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
213481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
214f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
215f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
216481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
217481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceEnd(long traceTag) {
218d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
219481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceEnd(traceTag);
220481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
221481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
222f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
223f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
2249425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that a given section of code has
2259425f923fae8977d09d924436148d3808032ea98Romain Guy     * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
2269425f923fae8977d09d924436148d3808032ea98Romain Guy     * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
2279425f923fae8977d09d924436148d3808032ea98Romain Guy     * asynchronous events do not need to be nested. The name and cookie used to
2289425f923fae8977d09d924436148d3808032ea98Romain Guy     * begin an event must be used to end it.
2299425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2309425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2319425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2329425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2339425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2349425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2359425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2369425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
2379425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2389425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceBegin(traceTag, methodName, cookie);
2399425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2409425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2419425f923fae8977d09d924436148d3808032ea98Romain Guy
2429425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
2439425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that the current method has ended.
2449425f923fae8977d09d924436148d3808032ea98Romain Guy     * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
2459425f923fae8977d09d924436148d3808032ea98Romain Guy     * using the same tag, name and cookie.
2469425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2479425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2489425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2499425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2509425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2519425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2529425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2539425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
2549425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2559425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceEnd(traceTag, methodName, cookie);
2569425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2579425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2589425f923fae8977d09d924436148d3808032ea98Romain Guy
2599425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
260f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has begun. This call must
2615800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be followed by a corresponding call to {@link #endSection()} on the same thread.
262f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
263f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
264f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
265f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * these characters they will be replaced with a space character in the trace.
266f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
267f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @param sectionName The name of the code section to appear in the trace.  This may be at
268f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * most 127 Unicode code units long.
269f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2705800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void beginSection(String sectionName) {
271f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
272f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            if (sectionName.length() > MAX_SECTION_NAME_LEN) {
273f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis                throw new IllegalArgumentException("sectionName is too long");
274f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            }
275f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceBegin(TRACE_TAG_APP, sectionName);
276f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
277f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
278f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
279f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
280f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has ended. This call must
2815800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
282f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * will mark the end of the most recently begun section of code, so care must be taken to
2835800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * ensure that beginSection / endSection pairs are properly nested and called from the same
2845800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * thread.
285f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2865800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void endSection() {
287f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
288f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceEnd(TRACE_TAG_APP);
289f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
290f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
291481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown}
292