| 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 <stdatomic.h> |
| 22 |
#include <stdbool.h> |
| 23 |
#include <stdint.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <sys/cdefs.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <cutils/compiler.h> |
| 29 |
|
| 30 |
__BEGIN_DECLS |
| 31 |
|
| 32 |
/** |
| 33 |
* The ATRACE_TAG macro can be defined before including this header to trace |
| 34 |
* using one of the tags defined below. It must be defined to one of the |
| 35 |
* following ATRACE_TAG_* macros. The trace tag is used to filter tracing in |
| 36 |
* userland to avoid some of the runtime cost of tracing when it is not desired. |
| 37 |
* |
| 38 |
* Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always |
| 39 |
* being enabled - this should ONLY be done for debug code, as userland tracing |
| 40 |
* has a performance cost even when the trace is not being recorded. Defining |
| 41 |
* ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result |
| 42 |
* in the tracing always being disabled. |
| 43 |
* |
| 44 |
* ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing |
| 45 |
* within a hardware module. For example a camera hardware module would set: |
| 46 |
* #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
| 47 |
* |
| 48 |
* Keep these in sync with frameworks/base/core/java/android/os/Trace.java. |
| 49 |
*/ |
| 50 |
#define ATRACE_TAG_NEVER 0 // This tag is never enabled. |
| 51 |
#define ATRACE_TAG_ALWAYS (1<<0) // This tag is always enabled. |
| 52 |
#define ATRACE_TAG_GRAPHICS (1<<1) |
| 53 |
#define ATRACE_TAG_INPUT (1<<2) |
| 54 |
#define ATRACE_TAG_VIEW (1<<3) |
| 55 |
#define ATRACE_TAG_WEBVIEW (1<<4) |
| 56 |
#define ATRACE_TAG_WINDOW_MANAGER (1<<5) |
| 57 |
#define ATRACE_TAG_ACTIVITY_MANAGER (1<<6) |
| 58 |
#define ATRACE_TAG_SYNC_MANAGER (1<<7) |
| 59 |
#define ATRACE_TAG_AUDIO (1<<8) |
| 60 |
#define ATRACE_TAG_VIDEO (1<<9) |
| 61 |
#define ATRACE_TAG_CAMERA (1<<10) |
| 62 |
#define ATRACE_TAG_HAL (1<<11) |
| 63 |
#define ATRACE_TAG_APP (1<<12) |
| 64 |
#define ATRACE_TAG_RESOURCES (1<<13) |
| 65 |
#define ATRACE_TAG_DALVIK (1<<14) |
| 66 |
#define ATRACE_TAG_RS (1<<15) |
| 67 |
#define ATRACE_TAG_BIONIC (1<<16) |
| 68 |
#define ATRACE_TAG_POWER (1<<17) |
| 69 |
#define ATRACE_TAG_PACKAGE_MANAGER (1<<18) |
| 70 |
#define ATRACE_TAG_SYSTEM_SERVER (1<<19) |
| 71 |
#define ATRACE_TAG_DATABASE (1<<20) |
| 72 |
#define ATRACE_TAG_NETWORK (1<<21) |
| 73 |
#define ATRACE_TAG_ADB (1<<22) |
| 74 |
#define ATRACE_TAG_VIBRATOR (1<<23) |
| 75 |
#define ATRACE_TAG_AIDL (1<<24) |
| 76 |
#define ATRACE_TAG_NNAPI (1<<25) |
| 77 |
#define ATRACE_TAG_RRO (1<<26) |
| 78 |
#define ATRACE_TAG_THERMAL (1 << 27) |
| 79 |
#define ATRACE_TAG_LAST ATRACE_TAG_THERMAL |
| 80 |
|
| 81 |
// Reserved for initialization. |
| 82 |
#define ATRACE_TAG_NOT_READY (1ULL<<63) |
| 83 |
|
| 84 |
#define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) |
| 85 |
|
| 86 |
#ifndef ATRACE_TAG |
| 87 |
#define ATRACE_TAG ATRACE_TAG_NEVER |
| 88 |
#elif ATRACE_TAG > ATRACE_TAG_VALID_MASK |
| 89 |
#error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h |
| 90 |
#endif |
| 91 |
|
| 92 |
/** Internal implementation detail. Do not use. */ |
| 93 |
void atrace_begin_body(const char*); |
| 94 |
|
| 95 |
/** Internal implementation detail. Do not use. */ |
| 96 |
void atrace_end_body(); |
| 97 |
|
| 98 |
/** Internal implementation detail. Do not use. */ |
| 99 |
void atrace_async_begin_body(const char*, int32_t); |
| 100 |
|
| 101 |
/** Internal implementation detail. Do not use. */ |
| 102 |
void atrace_async_end_body(const char*, int32_t); |
| 103 |
|
| 104 |
/** Internal implementation detail. Do not use. */ |
| 105 |
void atrace_async_for_track_begin_body(const char*, const char*, int32_t); |
| 106 |
|
| 107 |
/** Internal implementation detail. Do not use. */ |
| 108 |
void atrace_async_for_track_end_body(const char*, int32_t); |
| 109 |
|
| 110 |
/** Internal implementation detail. Do not use. */ |
| 111 |
void atrace_instant_body(const char*); |
| 112 |
|
| 113 |
/** Internal implementation detail. Do not use. */ |
| 114 |
void atrace_instant_for_track_body(const char*, const char*); |
| 115 |
|
| 116 |
/** Internal implementation detail. Do not use. */ |
| 117 |
void atrace_int_body(const char*, int32_t); |
| 118 |
|
| 119 |
/** Internal implementation detail. Do not use. */ |
| 120 |
void atrace_int64_body(const char*, int64_t); |
| 121 |
|
| 122 |
/** |
| 123 |
* Opens the trace file for writing and reads the property for initial tags. |
| 124 |
* The atrace.tags.enableflags property sets the tags to trace. |
| 125 |
* This function should not be explicitly called, the first call to any normal |
| 126 |
* trace function will cause it to be run safely. |
| 127 |
*/ |
| 128 |
void atrace_setup(); |
| 129 |
|
| 130 |
/** |
| 131 |
* If tracing is ready, set atrace_enabled_tags to the system property |
| 132 |
* debug.atrace.tags.enableflags. Can be used as a sysprop change callback. |
| 133 |
*/ |
| 134 |
void atrace_update_tags(); |
| 135 |
|
| 136 |
/** |
| 137 |
* Set whether tracing is enabled for the current process. This is used to |
| 138 |
* prevent tracing within the Zygote process. |
| 139 |
*/ |
| 140 |
void atrace_set_tracing_enabled(bool enabled); |
| 141 |
|
| 142 |
/** |
| 143 |
* This is always set to false. This forces code that uses an old version |
| 144 |
* of this header to always call into atrace_setup, in which we call |
| 145 |
* atrace_init unconditionally. |
| 146 |
*/ |
| 147 |
extern atomic_bool atrace_is_ready; |
| 148 |
|
| 149 |
/** |
| 150 |
* Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY. |
| 151 |
* A value of zero indicates setup has failed. |
| 152 |
* Any other nonzero value indicates setup has succeeded, and tracing is on. |
| 153 |
*/ |
| 154 |
extern uint64_t atrace_enabled_tags; |
| 155 |
|
| 156 |
/** |
| 157 |
* Handle to the kernel's trace buffer, initialized to -1. |
| 158 |
* Any other value indicates setup has succeeded, and is a valid fd for tracing. |
| 159 |
*/ |
| 160 |
extern int atrace_marker_fd; |
| 161 |
|
| 162 |
/** |
| 163 |
* atrace_init readies the process for tracing by opening the trace_marker file. |
| 164 |
* Calling any trace function causes this to be run, so calling it is optional. |
| 165 |
* This can be explicitly run to avoid setup delay on first trace function. |
| 166 |
*/ |
| 167 |
#define ATRACE_INIT() atrace_init() |
| 168 |
#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags() |
| 169 |
|
| 170 |
void atrace_init(); |
| 171 |
uint64_t atrace_get_enabled_tags(); |
| 172 |
|
| 173 |
/** |
| 174 |
* Test if a given tag is currently enabled. |
| 175 |
* Returns nonzero if the tag is enabled, otherwise zero. |
| 176 |
* It can be used as a guard condition around more expensive trace calculations. |
| 177 |
*/ |
| 178 |
#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG) |
| 179 |
static inline uint64_t atrace_is_tag_enabled(uint64_t tag) |
| 180 |
{ |
| 181 |
return atrace_get_enabled_tags() & tag; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Trace the beginning of a context. name is used to identify the context. |
| 186 |
* This is often used to time function execution. |
| 187 |
*/ |
| 188 |
#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name) |
| 189 |
static inline void atrace_begin(uint64_t tag, const char* name) |
| 190 |
{ |
| 191 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 192 |
atrace_begin_body(name); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Trace the end of a context. |
| 198 |
* This should match up (and occur after) a corresponding ATRACE_BEGIN. |
| 199 |
*/ |
| 200 |
#define ATRACE_END() atrace_end(ATRACE_TAG) |
| 201 |
static inline void atrace_end(uint64_t tag) |
| 202 |
{ |
| 203 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 204 |
atrace_end_body(); |
| 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) |
| 217 |
static 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 |
atrace_async_begin_body(name, cookie); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Trace the end of an asynchronous event. |
| 227 |
* This should have a corresponding ATRACE_ASYNC_BEGIN. |
| 228 |
*/ |
| 229 |
#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie) |
| 230 |
static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie) |
| 231 |
{ |
| 232 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 233 |
atrace_async_end_body(name, cookie); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Trace the beginning of an asynchronous event. In addition to the name and a |
| 239 |
* cookie as in ATRACE_ASYNC_BEGIN/ATRACE_ASYNC_END, a track name argument is |
| 240 |
* provided, which is the name of the row where this async event should be |
| 241 |
* recorded. The track name, name, and cookie used to begin an event must be |
| 242 |
* used to end it. |
| 243 |
* The cookie here must be unique on the track_name level, not the name level. |
| 244 |
*/ |
| 245 |
#define ATRACE_ASYNC_FOR_TRACK_BEGIN(track_name, name, cookie) \ |
| 246 |
atrace_async_for_track_begin(ATRACE_TAG, track_name, name, cookie) |
| 247 |
static inline void atrace_async_for_track_begin(uint64_t tag, const char* track_name, |
| 248 |
const char* name, int32_t cookie) { |
| 249 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 250 |
atrace_async_for_track_begin_body(track_name, name, cookie); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Trace the end of an asynchronous event. |
| 256 |
* This should correspond to a previous ATRACE_ASYNC_FOR_TRACK_BEGIN. |
| 257 |
*/ |
| 258 |
#define ATRACE_ASYNC_FOR_TRACK_END(track_name, cookie) \ |
| 259 |
atrace_async_for_track_end(ATRACE_TAG, track_name, cookie) |
| 260 |
static inline void atrace_async_for_track_end(uint64_t tag, const char* track_name, |
| 261 |
int32_t cookie) { |
| 262 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 263 |
atrace_async_for_track_end_body(track_name, cookie); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Trace an instantaneous context. name is used to identify the context. |
| 269 |
* |
| 270 |
* An "instant" is an event with no defined duration. Visually is displayed like a single marker |
| 271 |
* in the timeline (rather than a span, in the case of begin/end events). |
| 272 |
* |
| 273 |
* By default, instant events are added into a dedicated track that has the same name of the event. |
| 274 |
* Use atrace_instant_for_track to put different instant events into the same timeline track/row. |
| 275 |
*/ |
| 276 |
#define ATRACE_INSTANT(name) atrace_instant(ATRACE_TAG, name) |
| 277 |
static inline void atrace_instant(uint64_t tag, const char* name) { |
| 278 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 279 |
atrace_instant_body(name); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Trace an instantaneous context. name is used to identify the context. |
| 285 |
* track_name is the name of the row where the event should be recorded. |
| 286 |
* |
| 287 |
* An "instant" is an event with no defined duration. Visually is displayed like a single marker |
| 288 |
* in the timeline (rather than a span, in the case of begin/end events). |
| 289 |
*/ |
| 290 |
#define ATRACE_INSTANT_FOR_TRACK(trackName, name) \ |
| 291 |
atrace_instant_for_track(ATRACE_TAG, trackName, name) |
| 292 |
static inline void atrace_instant_for_track(uint64_t tag, const char* track_name, |
| 293 |
const char* name) { |
| 294 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 295 |
atrace_instant_for_track_body(track_name, name); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Traces an integer counter value. name is used to identify the counter. |
| 301 |
* This can be used to track how a value changes over time. |
| 302 |
*/ |
| 303 |
#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value) |
| 304 |
static inline void atrace_int(uint64_t tag, const char* name, int32_t value) |
| 305 |
{ |
| 306 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 307 |
atrace_int_body(name, value); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Traces a 64-bit integer counter value. name is used to identify the |
| 313 |
* counter. This can be used to track how a value changes over time. |
| 314 |
*/ |
| 315 |
#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value) |
| 316 |
static inline void atrace_int64(uint64_t tag, const char* name, int64_t value) |
| 317 |
{ |
| 318 |
if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { |
| 319 |
atrace_int64_body(name, value); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
__END_DECLS |
| 324 |
|
| 325 |
#endif // _LIBS_CUTILS_TRACE_H |