Trace.java revision 83e6eb11d7ec24e7c363beccab0806989ad89ec5
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19/**
20 * Writes trace events to the kernel trace buffer.  These trace events can be
21 * collected using the "atrace" program for offline analysis.
22 *
23 * This tracing mechanism is independent of the method tracing mechanism
24 * offered by {@link Debug#startMethodTracing}.  In particular, it enables
25 * tracing of events that occur across processes.
26 *
27 * @hide
28 */
29public final class Trace {
30    // These tags must be kept in sync with frameworks/native/include/utils/Trace.h.
31    public static final long TRACE_TAG_NEVER = 0;
32    public static final long TRACE_TAG_ALWAYS = 1L << 0;
33    public static final long TRACE_TAG_GRAPHICS = 1L << 1;
34    public static final long TRACE_TAG_INPUT = 1L << 2;
35    public static final long TRACE_TAG_VIEW = 1L << 3;
36    public static final long TRACE_TAG_WEBVIEW = 1L << 4;
37    public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
38    public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
39    public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
40    public static final long TRACE_TAG_AUDIO = 1L << 8;
41
42    public static final int TRACE_FLAGS_START_BIT = 1;
43    public static final String[] TRACE_TAGS = {
44        "Graphics", "Input", "View", "WebView", "Window Manager",
45        "Activity Manager", "Sync Manager", "Audio"
46    };
47
48    public static final String PROPERTY_TRACE_TAG_ENABLEFLAGS = "debug.atrace.tags.enableflags";
49
50    private static final long sEnabledTags = nativeGetEnabledTags();
51
52    private static native long nativeGetEnabledTags();
53    private static native void nativeTraceCounter(long tag, String name, int value);
54    private static native void nativeTraceBegin(long tag, String name);
55    private static native void nativeTraceEnd(long tag);
56
57    private Trace() {
58    }
59
60    /**
61     * Returns true if a trace tag is enabled.
62     *
63     * @param traceTag The trace tag to check.
64     * @return True if the trace tag is valid.
65     */
66    public static boolean isTagEnabled(long traceTag) {
67        return (sEnabledTags & traceTag) != 0;
68    }
69
70    /**
71     * Writes trace message to indicate the value of a given counter.
72     *
73     * @param traceTag The trace tag.
74     * @param counterName The counter name to appear in the trace.
75     * @param counterValue The counter value.
76     */
77    public static void traceCounter(long traceTag, String counterName, int counterValue) {
78        if ((sEnabledTags & traceTag) != 0) {
79            nativeTraceCounter(traceTag, counterName, counterValue);
80        }
81    }
82
83    /**
84     * Writes a trace message to indicate that a given method has begun.
85     * Must be followed by a call to {@link #traceEnd} using the same tag.
86     *
87     * @param traceTag The trace tag.
88     * @param methodName The method name to appear in the trace.
89     */
90    public static void traceBegin(long traceTag, String methodName) {
91        if ((sEnabledTags & traceTag) != 0) {
92            nativeTraceBegin(traceTag, methodName);
93        }
94    }
95
96    /**
97     * Writes a trace message to indicate that the current method has ended.
98     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
99     *
100     * @param traceTag The trace tag.
101     */
102    public static void traceEnd(long traceTag) {
103        if ((sEnabledTags & traceTag) != 0) {
104            nativeTraceEnd(traceTag);
105        }
106    }
107}
108