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 import android.util.Log;
     20 
     21 /**
     22  * Writes trace events to the system trace buffer.  These trace events can be
     23  * collected and visualized using the Systrace tool.
     24  *
     25  * This tracing mechanism is independent of the method tracing mechanism
     26  * offered by {@link Debug#startMethodTracing}.  In particular, it enables
     27  * tracing of events that occur across multiple processes.
     28  */
     29 public final class Trace {
     30     /*
     31      * Writes trace events to the kernel trace buffer.  These trace events can be
     32      * collected using the "atrace" program for offline analysis.
     33      */
     34 
     35     private static final String TAG = "Trace";
     36 
     37     // These tags must be kept in sync with system/core/include/cutils/trace.h.
     38     /** @hide */
     39     public static final long TRACE_TAG_NEVER = 0;
     40     /** @hide */
     41     public static final long TRACE_TAG_ALWAYS = 1L << 0;
     42     /** @hide */
     43     public static final long TRACE_TAG_GRAPHICS = 1L << 1;
     44     /** @hide */
     45     public static final long TRACE_TAG_INPUT = 1L << 2;
     46     /** @hide */
     47     public static final long TRACE_TAG_VIEW = 1L << 3;
     48     /** @hide */
     49     public static final long TRACE_TAG_WEBVIEW = 1L << 4;
     50     /** @hide */
     51     public static final long TRACE_TAG_WINDOW_MANAGER = 1L << 5;
     52     /** @hide */
     53     public static final long TRACE_TAG_ACTIVITY_MANAGER = 1L << 6;
     54     /** @hide */
     55     public static final long TRACE_TAG_SYNC_MANAGER = 1L << 7;
     56     /** @hide */
     57     public static final long TRACE_TAG_AUDIO = 1L << 8;
     58     /** @hide */
     59     public static final long TRACE_TAG_VIDEO = 1L << 9;
     60     /** @hide */
     61     public static final long TRACE_TAG_CAMERA = 1L << 10;
     62     /** @hide */
     63     public static final long TRACE_TAG_HAL = 1L << 11;
     64     /** @hide */
     65     public static final long TRACE_TAG_APP = 1L << 12;
     66     /** @hide */
     67     public static final long TRACE_TAG_RESOURCES = 1L << 13;
     68     /** @hide */
     69     public static final long TRACE_TAG_DALVIK = 1L << 14;
     70 
     71     private static final long TRACE_TAG_NOT_READY = 1L << 63;
     72     private static final int MAX_SECTION_NAME_LEN = 127;
     73 
     74     // Must be volatile to avoid word tearing.
     75     private static volatile long sEnabledTags = TRACE_TAG_NOT_READY;
     76 
     77     private static native long nativeGetEnabledTags();
     78     private static native void nativeTraceCounter(long tag, String name, int value);
     79     private static native void nativeTraceBegin(long tag, String name);
     80     private static native void nativeTraceEnd(long tag);
     81     private static native void nativeAsyncTraceBegin(long tag, String name, int cookie);
     82     private static native void nativeAsyncTraceEnd(long tag, String name, int cookie);
     83     private static native void nativeSetAppTracingAllowed(boolean allowed);
     84     private static native void nativeSetTracingEnabled(boolean allowed);
     85 
     86     static {
     87         // We configure two separate change callbacks, one in Trace.cpp and one here.  The
     88         // native callback reads the tags from the system property, and this callback
     89         // reads the value that the native code retrieved.  It's essential that the native
     90         // callback executes first.
     91         //
     92         // The system provides ordering through a priority level.  Callbacks made through
     93         // SystemProperties.addChangeCallback currently have a negative priority, while
     94         // our native code is using a priority of zero.
     95         SystemProperties.addChangeCallback(new Runnable() {
     96             @Override public void run() {
     97                 cacheEnabledTags();
     98             }
     99         });
    100     }
    101 
    102     private Trace() {
    103     }
    104 
    105     /**
    106      * Caches a copy of the enabled-tag bits.  The "master" copy is held by the native code,
    107      * and comes from the PROPERTY_TRACE_TAG_ENABLEFLAGS property.
    108      * <p>
    109      * If the native code hasn't yet read the property, we will cause it to do one-time
    110      * initialization.  We don't want to do this during class init, because this class is
    111      * preloaded, so all apps would be stuck with whatever the zygote saw.  (The zygote
    112      * doesn't see the system-property update broadcasts.)
    113      * <p>
    114      * We want to defer initialization until the first use by an app, post-zygote.
    115      * <p>
    116      * We're okay if multiple threads call here simultaneously -- the native state is
    117      * synchronized, and sEnabledTags is volatile (prevents word tearing).
    118      */
    119     private static long cacheEnabledTags() {
    120         long tags = nativeGetEnabledTags();
    121         sEnabledTags = tags;
    122         return tags;
    123     }
    124 
    125     /**
    126      * Returns true if a trace tag is enabled.
    127      *
    128      * @param traceTag The trace tag to check.
    129      * @return True if the trace tag is valid.
    130      *
    131      * @hide
    132      */
    133     public static boolean isTagEnabled(long traceTag) {
    134         long tags = sEnabledTags;
    135         if (tags == TRACE_TAG_NOT_READY) {
    136             tags = cacheEnabledTags();
    137         }
    138         return (tags & traceTag) != 0;
    139     }
    140 
    141     /**
    142      * Writes trace message to indicate the value of a given counter.
    143      *
    144      * @param traceTag The trace tag.
    145      * @param counterName The counter name to appear in the trace.
    146      * @param counterValue The counter value.
    147      *
    148      * @hide
    149      */
    150     public static void traceCounter(long traceTag, String counterName, int counterValue) {
    151         if (isTagEnabled(traceTag)) {
    152             nativeTraceCounter(traceTag, counterName, counterValue);
    153         }
    154     }
    155 
    156     /**
    157      * Set whether application tracing is allowed for this process.  This is intended to be set
    158      * once at application start-up time based on whether the application is debuggable.
    159      *
    160      * @hide
    161      */
    162     public static void setAppTracingAllowed(boolean allowed) {
    163         nativeSetAppTracingAllowed(allowed);
    164 
    165         // Setting whether app tracing is allowed may change the tags, so we update the cached
    166         // tags here.
    167         cacheEnabledTags();
    168     }
    169 
    170     /**
    171      * Set whether tracing is enabled in this process.  Tracing is disabled shortly after Zygote
    172      * initializes and re-enabled after processes fork from Zygote.  This is done because Zygote
    173      * has no way to be notified about changes to the tracing tags, and if Zygote ever reads and
    174      * caches the tracing tags, forked processes will inherit those stale tags.
    175      *
    176      * @hide
    177      */
    178     public static void setTracingEnabled(boolean enabled) {
    179         nativeSetTracingEnabled(enabled);
    180 
    181         // Setting whether tracing is enabled may change the tags, so we update the cached tags
    182         // here.
    183         cacheEnabledTags();
    184     }
    185 
    186     /**
    187      * Writes a trace message to indicate that a given section of code has
    188      * begun. Must be followed by a call to {@link #traceEnd} using the same
    189      * tag.
    190      *
    191      * @param traceTag The trace tag.
    192      * @param methodName The method name to appear in the trace.
    193      *
    194      * @hide
    195      */
    196     public static void traceBegin(long traceTag, String methodName) {
    197         if (isTagEnabled(traceTag)) {
    198             nativeTraceBegin(traceTag, methodName);
    199         }
    200     }
    201 
    202     /**
    203      * Writes a trace message to indicate that the current method has ended.
    204      * Must be called exactly once for each call to {@link #traceBegin} using the same tag.
    205      *
    206      * @param traceTag The trace tag.
    207      *
    208      * @hide
    209      */
    210     public static void traceEnd(long traceTag) {
    211         if (isTagEnabled(traceTag)) {
    212             nativeTraceEnd(traceTag);
    213         }
    214     }
    215 
    216     /**
    217      * Writes a trace message to indicate that a given section of code has
    218      * begun. Must be followed by a call to {@link #asyncTraceEnd} using the same
    219      * tag. Unlike {@link #traceBegin(long, String)} and {@link #traceEnd(long)},
    220      * asynchronous events do not need to be nested. The name and cookie used to
    221      * begin an event must be used to end it.
    222      *
    223      * @param traceTag The trace tag.
    224      * @param methodName The method name to appear in the trace.
    225      * @param cookie Unique identifier for distinguishing simultaneous events
    226      *
    227      * @hide
    228      */
    229     public static void asyncTraceBegin(long traceTag, String methodName, int cookie) {
    230         if (isTagEnabled(traceTag)) {
    231             nativeAsyncTraceBegin(traceTag, methodName, cookie);
    232         }
    233     }
    234 
    235     /**
    236      * Writes a trace message to indicate that the current method has ended.
    237      * Must be called exactly once for each call to {@link #asyncTraceBegin(long, String, int)}
    238      * using the same tag, name and cookie.
    239      *
    240      * @param traceTag The trace tag.
    241      * @param methodName The method name to appear in the trace.
    242      * @param cookie Unique identifier for distinguishing simultaneous events
    243      *
    244      * @hide
    245      */
    246     public static void asyncTraceEnd(long traceTag, String methodName, int cookie) {
    247         if (isTagEnabled(traceTag)) {
    248             nativeAsyncTraceEnd(traceTag, methodName, cookie);
    249         }
    250     }
    251 
    252     /**
    253      * Writes a trace message to indicate that a given section of code has begun. This call must
    254      * be followed by a corresponding call to {@link #endSection()} on the same thread.
    255      *
    256      * <p class="note"> At this time the vertical bar character '|', newline character '\n', and
    257      * null character '\0' are used internally by the tracing mechanism.  If sectionName contains
    258      * these characters they will be replaced with a space character in the trace.
    259      *
    260      * @param sectionName The name of the code section to appear in the trace.  This may be at
    261      * most 127 Unicode code units long.
    262      */
    263     public static void beginSection(String sectionName) {
    264         if (isTagEnabled(TRACE_TAG_APP)) {
    265             if (sectionName.length() > MAX_SECTION_NAME_LEN) {
    266                 throw new IllegalArgumentException("sectionName is too long");
    267             }
    268             nativeTraceBegin(TRACE_TAG_APP, sectionName);
    269         }
    270     }
    271 
    272     /**
    273      * Writes a trace message to indicate that a given section of code has ended. This call must
    274      * be preceeded by a corresponding call to {@link #beginSection(String)}. Calling this method
    275      * will mark the end of the most recently begun section of code, so care must be taken to
    276      * ensure that beginSection / endSection pairs are properly nested and called from the same
    277      * thread.
    278      */
    279     public static void endSection() {
    280         if (isTagEnabled(TRACE_TAG_APP)) {
    281             nativeTraceEnd(TRACE_TAG_APP);
    282         }
    283     }
    284 }
    285