Home | History | Annotate | Download | only in cutils
      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 #ifndef _LIBS_CUTILS_TRACE_H
     18 #define _LIBS_CUTILS_TRACE_H
     19 
     20 #include <sys/cdefs.h>
     21 #include <sys/types.h>
     22 #include <stdint.h>
     23 #include <stdbool.h>
     24 #include <unistd.h>
     25 #include <cutils/compiler.h>
     26 
     27 #ifdef ANDROID_SMP
     28 #include <cutils/atomic-inline.h>
     29 #else
     30 #include <cutils/atomic.h>
     31 #endif
     32 
     33 __BEGIN_DECLS
     34 
     35 /**
     36  * The ATRACE_TAG macro can be defined before including this header to trace
     37  * using one of the tags defined below.  It must be defined to one of the
     38  * following ATRACE_TAG_* macros.  The trace tag is used to filter tracing in
     39  * userland to avoid some of the runtime cost of tracing when it is not desired.
     40  *
     41  * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always
     42  * being enabled - this should ONLY be done for debug code, as userland tracing
     43  * has a performance cost even when the trace is not being recorded.  Defining
     44  * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result
     45  * in the tracing always being disabled.
     46  *
     47  * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing
     48  * within a hardware module.  For example a camera hardware module would set:
     49  * #define ATRACE_TAG  (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
     50  *
     51  * Keep these in sync with frameworks/base/core/java/android/os/Trace.java.
     52  */
     53 #define ATRACE_TAG_NEVER            0       // This tag is never enabled.
     54 #define ATRACE_TAG_ALWAYS           (1<<0)  // This tag is always enabled.
     55 #define ATRACE_TAG_GRAPHICS         (1<<1)
     56 #define ATRACE_TAG_INPUT            (1<<2)
     57 #define ATRACE_TAG_VIEW             (1<<3)
     58 #define ATRACE_TAG_WEBVIEW          (1<<4)
     59 #define ATRACE_TAG_WINDOW_MANAGER   (1<<5)
     60 #define ATRACE_TAG_ACTIVITY_MANAGER (1<<6)
     61 #define ATRACE_TAG_SYNC_MANAGER     (1<<7)
     62 #define ATRACE_TAG_AUDIO            (1<<8)
     63 #define ATRACE_TAG_VIDEO            (1<<9)
     64 #define ATRACE_TAG_CAMERA           (1<<10)
     65 #define ATRACE_TAG_HAL              (1<<11)
     66 #define ATRACE_TAG_APP              (1<<12)
     67 #define ATRACE_TAG_RESOURCES        (1<<13)
     68 #define ATRACE_TAG_DALVIK           (1<<14)
     69 #define ATRACE_TAG_RS               (1<<15)
     70 #define ATRACE_TAG_LAST             ATRACE_TAG_RS
     71 
     72 // Reserved for initialization.
     73 #define ATRACE_TAG_NOT_READY        (1LL<<63)
     74 
     75 #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST)
     76 
     77 #ifndef ATRACE_TAG
     78 #define ATRACE_TAG ATRACE_TAG_NEVER
     79 #elif ATRACE_TAG > ATRACE_TAG_VALID_MASK
     80 #error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h
     81 #endif
     82 
     83 #ifdef HAVE_ANDROID_OS
     84 /**
     85  * Maximum size of a message that can be logged to the trace buffer.
     86  * Note this message includes a tag, the pid, and the string given as the name.
     87  * Names should be kept short to get the most use of the trace buffer.
     88  */
     89 #define ATRACE_MESSAGE_LENGTH 1024
     90 
     91 /**
     92  * Opens the trace file for writing and reads the property for initial tags.
     93  * The atrace.tags.enableflags property sets the tags to trace.
     94  * This function should not be explicitly called, the first call to any normal
     95  * trace function will cause it to be run safely.
     96  */
     97 void atrace_setup();
     98 
     99 /**
    100  * If tracing is ready, set atrace_enabled_tags to the system property
    101  * debug.atrace.tags.enableflags. Can be used as a sysprop change callback.
    102  */
    103 void atrace_update_tags();
    104 
    105 /**
    106  * Set whether the process is debuggable.  By default the process is not
    107  * considered debuggable.  If the process is not debuggable then application-
    108  * level tracing is not allowed unless the ro.debuggable system property is
    109  * set to '1'.
    110  */
    111 void atrace_set_debuggable(bool debuggable);
    112 
    113 /**
    114  * Set whether tracing is enabled for the current process.  This is used to
    115  * prevent tracing within the Zygote process.
    116  */
    117 void atrace_set_tracing_enabled(bool enabled);
    118 
    119 /**
    120  * Flag indicating whether setup has been completed, initialized to 0.
    121  * Nonzero indicates setup has completed.
    122  * Note: This does NOT indicate whether or not setup was successful.
    123  */
    124 extern volatile int32_t atrace_is_ready;
    125 
    126 /**
    127  * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY.
    128  * A value of zero indicates setup has failed.
    129  * Any other nonzero value indicates setup has succeeded, and tracing is on.
    130  */
    131 extern uint64_t atrace_enabled_tags;
    132 
    133 /**
    134  * Handle to the kernel's trace buffer, initialized to -1.
    135  * Any other value indicates setup has succeeded, and is a valid fd for tracing.
    136  */
    137 extern int atrace_marker_fd;
    138 
    139 /**
    140  * atrace_init readies the process for tracing by opening the trace_marker file.
    141  * Calling any trace function causes this to be run, so calling it is optional.
    142  * This can be explicitly run to avoid setup delay on first trace function.
    143  */
    144 #define ATRACE_INIT() atrace_init()
    145 static inline void atrace_init()
    146 {
    147     if (CC_UNLIKELY(!android_atomic_acquire_load(&atrace_is_ready))) {
    148         atrace_setup();
    149     }
    150 }
    151 
    152 /**
    153  * Get the mask of all tags currently enabled.
    154  * It can be used as a guard condition around more expensive trace calculations.
    155  * Every trace function calls this, which ensures atrace_init is run.
    156  */
    157 #define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()
    158 static inline uint64_t atrace_get_enabled_tags()
    159 {
    160     atrace_init();
    161     return atrace_enabled_tags;
    162 }
    163 
    164 /**
    165  * Test if a given tag is currently enabled.
    166  * Returns nonzero if the tag is enabled, otherwise zero.
    167  * It can be used as a guard condition around more expensive trace calculations.
    168  */
    169 #define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
    170 static inline uint64_t atrace_is_tag_enabled(uint64_t tag)
    171 {
    172     return atrace_get_enabled_tags() & tag;
    173 }
    174 
    175 /**
    176  * Trace the beginning of a context.  name is used to identify the context.
    177  * This is often used to time function execution.
    178  */
    179 #define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
    180 static inline void atrace_begin(uint64_t tag, const char* name)
    181 {
    182     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    183         char buf[ATRACE_MESSAGE_LENGTH];
    184         size_t len;
    185 
    186         len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "B|%d|%s", getpid(), name);
    187         write(atrace_marker_fd, buf, len);
    188     }
    189 }
    190 
    191 /**
    192  * Trace the end of a context.
    193  * This should match up (and occur after) a corresponding ATRACE_BEGIN.
    194  */
    195 #define ATRACE_END() atrace_end(ATRACE_TAG)
    196 static inline void atrace_end(uint64_t tag)
    197 {
    198     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    199         char c = 'E';
    200         write(atrace_marker_fd, &c, 1);
    201     }
    202 }
    203 
    204 /**
    205  * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END
    206  * contexts, asynchronous events do not need to be nested. The name describes
    207  * the event, and the cookie provides a unique identifier for distinguishing
    208  * simultaneous events. The name and cookie used to begin an event must be
    209  * used to end it.
    210  */
    211 #define ATRACE_ASYNC_BEGIN(name, cookie) \
    212     atrace_async_begin(ATRACE_TAG, name, cookie)
    213 static inline void atrace_async_begin(uint64_t tag, const char* name,
    214         int32_t cookie)
    215 {
    216     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    217         char buf[ATRACE_MESSAGE_LENGTH];
    218         size_t len;
    219 
    220         len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "S|%d|%s|%d", getpid(),
    221                 name, cookie);
    222         write(atrace_marker_fd, buf, len);
    223     }
    224 }
    225 
    226 /**
    227  * Trace the end of an asynchronous event.
    228  * This should have a corresponding ATRACE_ASYNC_BEGIN.
    229  */
    230 #define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
    231 static inline void atrace_async_end(uint64_t tag, const char* name,
    232         int32_t cookie)
    233 {
    234     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    235         char buf[ATRACE_MESSAGE_LENGTH];
    236         size_t len;
    237 
    238         len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "F|%d|%s|%d", getpid(),
    239                 name, cookie);
    240         write(atrace_marker_fd, buf, len);
    241     }
    242 }
    243 
    244 
    245 /**
    246  * Traces an integer counter value.  name is used to identify the counter.
    247  * This can be used to track how a value changes over time.
    248  */
    249 #define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
    250 static inline void atrace_int(uint64_t tag, const char* name, int32_t value)
    251 {
    252     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    253         char buf[ATRACE_MESSAGE_LENGTH];
    254         size_t len;
    255 
    256         len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%d",
    257                 getpid(), name, value);
    258         write(atrace_marker_fd, buf, len);
    259     }
    260 }
    261 
    262 /**
    263  * Traces a 64-bit integer counter value.  name is used to identify the
    264  * counter. This can be used to track how a value changes over time.
    265  */
    266 #define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
    267 static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)
    268 {
    269     if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) {
    270         char buf[ATRACE_MESSAGE_LENGTH];
    271         size_t len;
    272 
    273         len = snprintf(buf, ATRACE_MESSAGE_LENGTH, "C|%d|%s|%lld",
    274                 getpid(), name, value);
    275         write(atrace_marker_fd, buf, len);
    276     }
    277 }
    278 
    279 #else // not HAVE_ANDROID_OS
    280 
    281 #define ATRACE_INIT()
    282 #define ATRACE_GET_ENABLED_TAGS()
    283 #define ATRACE_ENABLED() 0
    284 #define ATRACE_BEGIN(name)
    285 #define ATRACE_END()
    286 #define ATRACE_ASYNC_BEGIN(name, cookie)
    287 #define ATRACE_ASYNC_END(name, cookie)
    288 #define ATRACE_INT(name, value)
    289 
    290 #endif // not HAVE_ANDROID_OS
    291 
    292 __END_DECLS
    293 
    294 #endif // _LIBS_CUTILS_TRACE_H
    295