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