Trace.java revision 1ded0b1f6af65c2f95f8327f7f3df4cee1bf2346
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/**
20481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * Writes trace events to the kernel trace buffer.  These trace events can be
21481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * collected using the "atrace" program for offline analysis.
22481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
23481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * This tracing mechanism is independent of the method tracing mechanism
24481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * offered by {@link Debug#startMethodTracing}.  In particular, it enables
25481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * tracing of events that occur across processes.
26481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown *
27481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown * @hide
28481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown */
29481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brownpublic final class Trace {
30481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    // These tags must be kept in sync with frameworks/native/include/utils/Trace.h.
31481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_NEVER = 0;
32481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_ALWAYS = 1L << 0;
33481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_GRAPHICS = 1L << 1;
34481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_INPUT = 1L << 2;
35481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static final long TRACE_TAG_VIEW = 1L << 3;
36192a65e9f6ebdc452520e19f95c68c270b3f96daChris Craik    public static final long TRACE_TAG_WEBVIEW = 1L << 4;
371ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
381ded0b1f6af65c2f95f8327f7f3df4cee1bf2346Dianne Hackborn    public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
39481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
40481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static final long sEnabledTags = nativeGetEnabledTags();
41481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
42481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native long nativeGetEnabledTags();
43481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceCounter(long tag, String name, int value);
44481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceBegin(long tag, String name);
45481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private static native void nativeTraceEnd(long tag);
46481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
47481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    private Trace() {
48481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
49481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
50481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
51481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Returns true if a trace tag is enabled.
52481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
53481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag to check.
54481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @return True if the trace tag is valid.
55481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
56481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static boolean isTagEnabled(long traceTag) {
57481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        return (sEnabledTags & traceTag) != 0;
58481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
59481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
60481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
61481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes trace message to indicate the value of a given counter.
62481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
63481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
64481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterName The counter name to appear in the trace.
65481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param counterValue The counter value.
66481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
67481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceCounter(long traceTag, String counterName, int counterValue) {
68481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        if ((sEnabledTags & traceTag) != 0) {
69481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceCounter(traceTag, counterName, counterValue);
70481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
71481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
72481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
73481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
74481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that a given method has begun.
75481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be followed by a call to {@link #traceEnd} using the same tag.
76481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
77481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
78481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param methodName The method name to appear in the trace.
79481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
80481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceBegin(long traceTag, String methodName) {
81481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        if ((sEnabledTags & traceTag) != 0) {
82481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceBegin(traceTag, methodName);
83481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
84481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
85481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown
86481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    /**
87481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Writes a trace message to indicate that the current method has ended.
88481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
89481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     *
90481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     * @param traceTag The trace tag.
91481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown     */
92481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    public static void traceEnd(long traceTag) {
93481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        if ((sEnabledTags & traceTag) != 0) {
94481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown            nativeTraceEnd(traceTag);
95481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown        }
96481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown    }
97481c1570dc5cdf58265b53f657801709dd05d1dfJeff Brown}
98