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
19481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown/**
20f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * Writes trace events to the system trace buffer.  These trace events can be
21f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * collected and visualized using the Systrace tool.
22481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
23c468240c1a2a393ec02d992f459c6586ae450161Scott Main * <p>This tracing mechanism is independent of the method tracing mechanism
24481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * offered by {@link Debug#startMethodTracing}.  In particular, it enables
25f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis * tracing of events that occur across multiple processes.
26c468240c1a2a393ec02d992f459c6586ae450161Scott Main * <p>For information about using the Systrace tool, read <a
27c468240c1a2a393ec02d992f459c6586ae450161Scott Main * href="{@docRoot}tools/debugging/systrace.html">Analyzing Display and Performance
28c468240c1a2a393ec02d992f459c6586ae450161Scott Main * with Systrace</a>.
29481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown */
30481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brownpublic final class Trace {
31f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /*
32f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes trace events to the kernel trace buffer.  These trace events can be
33f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * collected using the "atrace" program for offline analysis.
34f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
35f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
36d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static final String TAG = "Trace";
37d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
388a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    // These tags must be kept in sync with system/core/include/cutils/trace.h.
393edf5272fb2185403dfe64b9722b9fc9b9de80f8Jeff Brown    // They should also be added to frameworks/native/cmds/atrace/atrace.cpp.
40f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
41481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_NEVER = 0;
42f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
43481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_ALWAYS = 1L << 0;
44f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
45481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_GRAPHICS = 1L << 1;
46f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
47481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_INPUT = 1L << 2;
48f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
49481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_VIEW = 1L << 3;
50f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
51192a65e9f6ebdc452520e19f95c68c270b3f96daChris Craik    public static final long TRACE_TAG_WEBVIEW = 1L << 4;
52f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
531ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
54f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
551ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
56f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
5709b45a3ad96379b4181d32f8391f63e9c57dc316Andy Stadler    public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
58f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
59ed853fc4e0ba8ce0692d65064e12cf129b5d1f3eGlenn Kasten    public static final long TRACE_TAG_AUDIO = 1L << 8;
60f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
6124dae6c611455ec38675554033e4d18810d77d6cJamie Gennis    public static final long TRACE_TAG_VIDEO = 1L << 9;
62f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
639c0d9cf25ffdf4f6545e489b22ba621bf0b7ba29Eino-Ville Talvala    public static final long TRACE_TAG_CAMERA = 1L << 10;
64f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
658a6787b1c7f5192388436373465c35655cc8ef7cAlex Ray    public static final long TRACE_TAG_HAL = 1L << 11;
66f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /** @hide */
67f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static final long TRACE_TAG_APP = 1L << 12;
68f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    /** @hide */
69f7be4800df28d7cb6a96003046bf90245e7054abDianne Hackborn    public static final long TRACE_TAG_RESOURCES = 1L << 13;
700cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    /** @hide */
710cc84cefdd4e947f984678dfe854d3c53ded0475Jamie Gennis    public static final long TRACE_TAG_DALVIK = 1L << 14;
726d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray    /** @hide */
736d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray    public static final long TRACE_TAG_RS = 1L << 15;
74461ac24b8c2b400656a0bcd72743f03720af2fd0Brigid Smith    /** @hide */
75461ac24b8c2b400656a0bcd72743f03720af2fd0Brigid Smith    public static final long TRACE_TAG_BIONIC = 1L << 16;
763edf5272fb2185403dfe64b9722b9fc9b9de80f8Jeff Brown    /** @hide */
773edf5272fb2185403dfe64b9722b9fc9b9de80f8Jeff Brown    public static final long TRACE_TAG_POWER = 1L << 17;
78114beb21a071ec8dfba247de8ee76dcb45db3d43Todd Kennedy    /** @hide */
79114beb21a071ec8dfba247de8ee76dcb45db3d43Todd Kennedy    public static final long TRACE_TAG_PACKAGE_MANAGER = 1L << 18;
801ab43d5978813f56899dbd3115fd7d9f96b4fe55Yasuhiro Matsuda    /** @hide */
811ab43d5978813f56899dbd3115fd7d9f96b4fe55Yasuhiro Matsuda    public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
82e12350faf7ede07ebc7242dc55be2b4a3a86b532Greg Hackmann    /** @hide */
83e12350faf7ede07ebc7242dc55be2b4a3a86b532Greg Hackmann    public static final long TRACE_TAG_DATABASE = 1L << 20;
84873a83af3fff6b0ac85489ebc0b3106d11e97ca6Felipe Leme    /** @hide */
85873a83af3fff6b0ac85489ebc0b3106d11e97ca6Felipe Leme    public static final long TRACE_TAG_NETWORK = 1L << 21;
8683e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
87f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final long TRACE_TAG_NOT_READY = 1L << 63;
88f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static final int MAX_SECTION_NAME_LEN = 127;
8983e6eb11d7ec24e7c363beccab0806989ad89ec5Dianne Hackborn
90d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    // Must be volatile to avoid word tearing.
91325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden    private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
92481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
93481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native long nativeGetEnabledTags();
94481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceCounter(long tag, String name, int value);
95481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceBegin(long tag, String name);
96481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceEnd(long tag);
979425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
989425f923fae8977d09d924436148d3808032ea98Romain Guy    private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
99f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    private static native void nativeSetAppTracingAllowed(boolean allowed);
1006ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    private static native void nativeSetTracingEnabled(boolean allowed);
101481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
102a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    static {
103d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // We configure two separate change callbacks, one in Trace.cpp and one here.  The
104d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // native callback reads the tags from the system property, and this callback
105d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // reads the value that the native code retrieved.  It's essential that the native
106d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // callback executes first.
107d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        //
108d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // The system provides ordering through a priority level.  Callbacks made through
109d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // SystemProperties.addChangeCallback currently have a negative priority, while
110d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        // our native code is using a priority of zero.
111a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        SystemProperties.addChangeCallback(new Runnable() {
112a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            @Override public void run() {
113d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden                cacheEnabledTags();
114a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn            }
115a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn        });
116a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn    }
117a53de0629f3b94472c0f160f5bbe1090b020feabDianne Hackborn
118481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private Trace() {
119481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
120481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
121481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
122d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
123d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
124d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
125d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * If the native code hasn't yet read the property, we will cause it to do one-time
126d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * initialization.  We don't want to do this during class init, because this class is
127d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
128d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * doesn't see the system-property update broadcasts.)
129d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
130d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We want to defer initialization until the first use by an app, post-zygote.
131d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * <p>
132d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * We're okay if multiple threads call here simultaneously -- the native state is
133d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     * synchronized, and sEnabledTags is volatile (prevents word tearing).
134d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden     */
135d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    private static long cacheEnabledTags() {
136d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = nativeGetEnabledTags();
137d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        sEnabledTags = tags;
138d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return tags;
139d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    }
140d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden
141d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden    /**
142481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Returns true if a trace tag is enabled.
143481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
144481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag to check.
145481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @return True if the trace tag is valid.
146f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
147f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
148481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
149481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static boolean isTagEnabled(long traceTag) {
150d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        long tags = sEnabledTags;
151325be8a1ea03308de2ac35d613a2fe751bf16d94Andy McFadden        if (tags == TRACE_TAG_NOT_READY) {
152d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden            tags = cacheEnabledTags();
153d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        }
154d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        return (tags & traceTag) != 0;
155481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
156481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
157481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
158481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes trace message to indicate the value of a given counter.
159481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
160481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
161481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterName The counter name to appear in the trace.
162481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterValue The counter value.
163f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
164f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
165481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
166481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceCounter(long traceTag, String counterName, int counterValue) {
167d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
168481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceCounter(traceTag, counterName, counterValue);
169481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
170481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
171481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
172481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
173f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Set whether application tracing is allowed for this process.  This is intended to be set
174f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * once at application start-up time based on whether the application is debuggable.
175f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
176f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
177f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
178f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    public static void setAppTracingAllowed(boolean allowed) {
179f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        nativeSetAppTracingAllowed(allowed);
180f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
181f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // Setting whether app tracing is allowed may change the tags, so we update the cached
182f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        // tags here.
183f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        cacheEnabledTags();
184f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
185f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
186f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
1876ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
1886ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
1896ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
1906ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * caches the tracing tags, forked processes will inherit those stale tags.
1916ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     *
1926ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     * @hide
1936ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis     */
1946ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    public static void setTracingEnabled(boolean enabled) {
1956ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        nativeSetTracingEnabled(enabled);
1966ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
1976ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // Setting whether tracing is enabled may change the tags, so we update the cached tags
1986ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        // here.
1996ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis        cacheEnabledTags();
2006ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    }
2016ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis
2026ad0452e6301c0650f58f3991f7c523f6f279ddbJamie Gennis    /**
203f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has
204f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * begun. Must be followed by a call to {@link #traceEnd} using the same
205f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * tag.
206481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
207481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
208481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param methodName The method name to appear in the trace.
209f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
210f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
211481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
212481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceBegin(long traceTag, String methodName) {
213d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
214481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceBegin(traceTag, methodName);
215481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
216481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
217481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
218481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
219481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that the current method has ended.
220481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
221481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
222481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
223f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
224f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @hide
225481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
226481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceEnd(long traceTag) {
227d11ca4dd2ca90e1412b24f4526a56f7b963054a8Andy McFadden        if (isTagEnabled(traceTag)) {
228481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceEnd(traceTag);
229481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
230481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
231f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
232f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
2339425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that a given section of code has
2349425f923fae8977d09d924436148d3808032ea98Romain Guy     * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
2359425f923fae8977d09d924436148d3808032ea98Romain Guy     * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
2369425f923fae8977d09d924436148d3808032ea98Romain Guy     * asynchronous events do not need to be nested. The name and cookie used to
2379425f923fae8977d09d924436148d3808032ea98Romain Guy     * begin an event must be used to end it.
2389425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2399425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2409425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2419425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2429425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2439425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2449425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2459425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
2469425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2479425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceBegin(traceTag, methodName, cookie);
2489425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2499425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2509425f923fae8977d09d924436148d3808032ea98Romain Guy
2519425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
2529425f923fae8977d09d924436148d3808032ea98Romain Guy     * Writes a trace message to indicate that the current method has ended.
2539425f923fae8977d09d924436148d3808032ea98Romain Guy     * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
2549425f923fae8977d09d924436148d3808032ea98Romain Guy     * using the same tag, name and cookie.
2559425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2569425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param traceTag The trace tag.
2579425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param methodName The method name to appear in the trace.
2589425f923fae8977d09d924436148d3808032ea98Romain Guy     * @param cookie Unique identifier for distinguishing simultaneous events
2599425f923fae8977d09d924436148d3808032ea98Romain Guy     *
2609425f923fae8977d09d924436148d3808032ea98Romain Guy     * @hide
2619425f923fae8977d09d924436148d3808032ea98Romain Guy     */
2629425f923fae8977d09d924436148d3808032ea98Romain Guy    public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
2639425f923fae8977d09d924436148d3808032ea98Romain Guy        if (isTagEnabled(traceTag)) {
2649425f923fae8977d09d924436148d3808032ea98Romain Guy            nativeAsyncTraceEnd(traceTag, methodName, cookie);
2659425f923fae8977d09d924436148d3808032ea98Romain Guy        }
2669425f923fae8977d09d924436148d3808032ea98Romain Guy    }
2679425f923fae8977d09d924436148d3808032ea98Romain Guy
2689425f923fae8977d09d924436148d3808032ea98Romain Guy    /**
269f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has begun. This call must
2705800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be followed by a corresponding call to {@link #endSection()} on the same thread.
271f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
272f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
273f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
274f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * these characters they will be replaced with a space character in the trace.
275f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     *
276f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * @param sectionName The name of the code section to appear in the trace.  This may be at
277f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * most 127 Unicode code units long.
278f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2795800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void beginSection(String sectionName) {
280f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
281f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            if (sectionName.length() > MAX_SECTION_NAME_LEN) {
282f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis                throw new IllegalArgumentException("sectionName is too long");
283f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            }
284f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceBegin(TRACE_TAG_APP, sectionName);
285f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
286f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
287f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis
288f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    /**
289f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * Writes a trace message to indicate that a given section of code has ended. This call must
2905800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
291f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     * will mark the end of the most recently begun section of code, so care must be taken to
2925800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * ensure that beginSection / endSection pairs are properly nested and called from the same
2935800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis     * thread.
294f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis     */
2955800fc881e9919bc8a0ce12199f2a16230c6cbbfJamie Gennis    public static void endSection() {
296f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        if (isTagEnabled(TRACE_TAG_APP)) {
297f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis            nativeTraceEnd(TRACE_TAG_APP);
298f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis        }
299f9c7d6bc15b68393c1f0aa85c3c023c31244c3f2Jamie Gennis    }
300481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown}
301