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