Home | History | Annotate | Download | only in os
      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 
     17 package 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  */
     29 public 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     public static final long TRACE_TAG_VIDEO = 1L << 9;
     42 
     43     public static final int TRACE_FLAGS_START_BIT = 1;
     44     public static final String[] TRACE_TAGS = {
     45         "Graphics", "Input", "View", "WebView", "Window Manager",
     46         "Activity Manager", "Sync Manager", "Audio", "Video",
     47     };
     48 
     49     public static final String PROPERTY_TRACE_TAG_ENABLEFLAGS = "debug.atrace.tags.enableflags";
     50 
     51     private static long sEnabledTags = nativeGetEnabledTags();
     52 
     53     private static native long nativeGetEnabledTags();
     54     private static native void nativeTraceCounter(long tag, String name, int value);
     55     private static native void nativeTraceBegin(long tag, String name);
     56     private static native void nativeTraceEnd(long tag);
     57 
     58     static {
     59         SystemProperties.addChangeCallback(new Runnable() {
     60             @Override public void run() {
     61                 sEnabledTags = nativeGetEnabledTags();
     62             }
     63         });
     64     }
     65 
     66     private Trace() {
     67     }
     68 
     69     /**
     70      * Returns true if a trace tag is enabled.
     71      *
     72      * @param traceTag The trace tag to check.
     73      * @return True if the trace tag is valid.
     74      */
     75     public static boolean isTagEnabled(long traceTag) {
     76         return (sEnabledTags & traceTag) != 0;
     77     }
     78 
     79     /**
     80      * Writes trace message to indicate the value of a given counter.
     81      *
     82      * @param traceTag The trace tag.
     83      * @param counterName The counter name to appear in the trace.
     84      * @param counterValue The counter value.
     85      */
     86     public static void traceCounter(long traceTag, String counterName, int counterValue) {
     87         if ((sEnabledTags & traceTag) != 0) {
     88             nativeTraceCounter(traceTag, counterName, counterValue);
     89         }
     90     }
     91 
     92     /**
     93      * Writes a trace message to indicate that a given method has begun.
     94      * Must be followed by a call to {@link #traceEnd} using the same tag.
     95      *
     96      * @param traceTag The trace tag.
     97      * @param methodName The method name to appear in the trace.
     98      */
     99     public static void traceBegin(long traceTag, String methodName) {
    100         if ((sEnabledTags & traceTag) != 0) {
    101             nativeTraceBegin(traceTag, methodName);
    102         }
    103     }
    104 
    105     /**
    106      * Writes a trace message to indicate that the current method has ended.
    107      * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
    108      *
    109      * @param traceTag The trace tag.
    110      */
    111     public static void traceEnd(long traceTag) {
    112         if ((sEnabledTags & traceTag) != 0) {
    113             nativeTraceEnd(traceTag);
    114         }
    115     }
    116 }
    117