| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | a459d0b | 2014-03-20 13:02:57 -0700 | [diff] [blame] | 20 | #include <inttypes.h> | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 21 | #include <stdatomic.h> | 
| Mark Salyzyn | a459d0b | 2014-03-20 13:02:57 -0700 | [diff] [blame] | 22 | #include <stdbool.h> | 
|  | 23 | #include <stdint.h> | 
| Mark Salyzyn | 4887842 | 2014-05-22 16:08:52 -0700 | [diff] [blame] | 24 | #include <stdio.h> | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 25 | #include <sys/cdefs.h> | 
|  | 26 | #include <sys/types.h> | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 27 | #include <unistd.h> | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 28 |  | 
| Mark Salyzyn | a459d0b | 2014-03-20 13:02:57 -0700 | [diff] [blame] | 29 | #include <cutils/compiler.h> | 
| Alex Ray | 448f76a | 2012-11-30 19:37:53 -0800 | [diff] [blame] | 30 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 31 | __BEGIN_DECLS | 
|  | 32 |  | 
|  | 33 | /** | 
|  | 34 | * The ATRACE_TAG macro can be defined before including this header to trace | 
|  | 35 | * using one of the tags defined below.  It must be defined to one of the | 
|  | 36 | * following ATRACE_TAG_* macros.  The trace tag is used to filter tracing in | 
|  | 37 | * userland to avoid some of the runtime cost of tracing when it is not desired. | 
|  | 38 | * | 
|  | 39 | * Defining ATRACE_TAG to be ATRACE_TAG_ALWAYS will result in the tracing always | 
|  | 40 | * being enabled - this should ONLY be done for debug code, as userland tracing | 
|  | 41 | * has a performance cost even when the trace is not being recorded.  Defining | 
|  | 42 | * ATRACE_TAG to be ATRACE_TAG_NEVER or leaving ATRACE_TAG undefined will result | 
|  | 43 | * in the tracing always being disabled. | 
|  | 44 | * | 
|  | 45 | * ATRACE_TAG_HAL should be bitwise ORed with the relevant tags for tracing | 
|  | 46 | * within a hardware module.  For example a camera hardware module would set: | 
|  | 47 | * #define ATRACE_TAG  (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) | 
|  | 48 | * | 
|  | 49 | * Keep these in sync with frameworks/base/core/java/android/os/Trace.java. | 
|  | 50 | */ | 
|  | 51 | #define ATRACE_TAG_NEVER            0       // This tag is never enabled. | 
|  | 52 | #define ATRACE_TAG_ALWAYS           (1<<0)  // This tag is always enabled. | 
|  | 53 | #define ATRACE_TAG_GRAPHICS         (1<<1) | 
|  | 54 | #define ATRACE_TAG_INPUT            (1<<2) | 
|  | 55 | #define ATRACE_TAG_VIEW             (1<<3) | 
|  | 56 | #define ATRACE_TAG_WEBVIEW          (1<<4) | 
|  | 57 | #define ATRACE_TAG_WINDOW_MANAGER   (1<<5) | 
|  | 58 | #define ATRACE_TAG_ACTIVITY_MANAGER (1<<6) | 
|  | 59 | #define ATRACE_TAG_SYNC_MANAGER     (1<<7) | 
|  | 60 | #define ATRACE_TAG_AUDIO            (1<<8) | 
|  | 61 | #define ATRACE_TAG_VIDEO            (1<<9) | 
|  | 62 | #define ATRACE_TAG_CAMERA           (1<<10) | 
|  | 63 | #define ATRACE_TAG_HAL              (1<<11) | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 64 | #define ATRACE_TAG_APP              (1<<12) | 
| Dianne Hackborn | 24bc41b | 2013-04-12 14:51:43 -0700 | [diff] [blame] | 65 | #define ATRACE_TAG_RESOURCES        (1<<13) | 
| Jamie Gennis | 2b68e06 | 2013-05-07 15:09:17 -0700 | [diff] [blame] | 66 | #define ATRACE_TAG_DALVIK           (1<<14) | 
| Tim Murray | d8b11c1 | 2013-05-23 13:46:12 -0700 | [diff] [blame] | 67 | #define ATRACE_TAG_RS               (1<<15) | 
| Brigid Smith | 8c82b35 | 2014-05-27 12:37:48 -0700 | [diff] [blame] | 68 | #define ATRACE_TAG_BIONIC           (1<<16) | 
| Jeff Brown | de4d921 | 2014-08-14 19:23:30 -0700 | [diff] [blame] | 69 | #define ATRACE_TAG_POWER            (1<<17) | 
| Todd Kennedy | da106ad | 2015-08-03 09:58:01 -0700 | [diff] [blame] | 70 | #define ATRACE_TAG_PACKAGE_MANAGER  (1<<18) | 
| Yasuhiro Matsuda | 715f463 | 2015-07-01 01:54:49 +0900 | [diff] [blame] | 71 | #define ATRACE_TAG_SYSTEM_SERVER    (1<<19) | 
| Greg Hackmann | c314b2b | 2014-12-01 16:06:30 -0800 | [diff] [blame] | 72 | #define ATRACE_TAG_DATABASE         (1<<20) | 
| Felipe Leme | 785735b | 2016-09-07 11:32:13 -0700 | [diff] [blame] | 73 | #define ATRACE_TAG_NETWORK          (1<<21) | 
| Josh Gao | e2d9732 | 2016-12-05 11:38:30 -0800 | [diff] [blame] | 74 | #define ATRACE_TAG_ADB              (1<<22) | 
|  | 75 | #define ATRACE_TAG_LAST             ATRACE_TAG_ADB | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 76 |  | 
|  | 77 | // Reserved for initialization. | 
| Nick Kralevich | 7390478 | 2015-08-26 10:40:00 -0700 | [diff] [blame] | 78 | #define ATRACE_TAG_NOT_READY        (1ULL<<63) | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) | 
|  | 81 |  | 
|  | 82 | #ifndef ATRACE_TAG | 
|  | 83 | #define ATRACE_TAG ATRACE_TAG_NEVER | 
|  | 84 | #elif ATRACE_TAG > ATRACE_TAG_VALID_MASK | 
|  | 85 | #error ATRACE_TAG must be defined to be one of the tags defined in cutils/trace.h | 
|  | 86 | #endif | 
|  | 87 |  | 
|  | 88 | /** | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 89 | * Opens the trace file for writing and reads the property for initial tags. | 
|  | 90 | * The atrace.tags.enableflags property sets the tags to trace. | 
|  | 91 | * This function should not be explicitly called, the first call to any normal | 
|  | 92 | * trace function will cause it to be run safely. | 
|  | 93 | */ | 
|  | 94 | void atrace_setup(); | 
|  | 95 |  | 
|  | 96 | /** | 
| Alex Ray | e7bb7bc | 2012-11-20 01:39:09 -0800 | [diff] [blame] | 97 | * If tracing is ready, set atrace_enabled_tags to the system property | 
|  | 98 | * debug.atrace.tags.enableflags. Can be used as a sysprop change callback. | 
|  | 99 | */ | 
|  | 100 | void atrace_update_tags(); | 
|  | 101 |  | 
|  | 102 | /** | 
| Jamie Gennis | 774f929 | 2013-02-25 18:15:40 -0800 | [diff] [blame] | 103 | * Set whether the process is debuggable.  By default the process is not | 
|  | 104 | * considered debuggable.  If the process is not debuggable then application- | 
|  | 105 | * level tracing is not allowed unless the ro.debuggable system property is | 
|  | 106 | * set to '1'. | 
|  | 107 | */ | 
|  | 108 | void atrace_set_debuggable(bool debuggable); | 
|  | 109 |  | 
|  | 110 | /** | 
| Jamie Gennis | b13ea45 | 2013-04-15 18:50:22 -0700 | [diff] [blame] | 111 | * Set whether tracing is enabled for the current process.  This is used to | 
|  | 112 | * prevent tracing within the Zygote process. | 
|  | 113 | */ | 
|  | 114 | void atrace_set_tracing_enabled(bool enabled); | 
|  | 115 |  | 
|  | 116 | /** | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 117 | * Flag indicating whether setup has been completed, initialized to 0. | 
|  | 118 | * Nonzero indicates setup has completed. | 
|  | 119 | * Note: This does NOT indicate whether or not setup was successful. | 
|  | 120 | */ | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 121 | extern atomic_bool atrace_is_ready; | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 122 |  | 
|  | 123 | /** | 
|  | 124 | * Set of ATRACE_TAG flags to trace for, initialized to ATRACE_TAG_NOT_READY. | 
|  | 125 | * A value of zero indicates setup has failed. | 
|  | 126 | * Any other nonzero value indicates setup has succeeded, and tracing is on. | 
|  | 127 | */ | 
|  | 128 | extern uint64_t atrace_enabled_tags; | 
|  | 129 |  | 
|  | 130 | /** | 
|  | 131 | * Handle to the kernel's trace buffer, initialized to -1. | 
|  | 132 | * Any other value indicates setup has succeeded, and is a valid fd for tracing. | 
|  | 133 | */ | 
|  | 134 | extern int atrace_marker_fd; | 
|  | 135 |  | 
|  | 136 | /** | 
|  | 137 | * atrace_init readies the process for tracing by opening the trace_marker file. | 
|  | 138 | * Calling any trace function causes this to be run, so calling it is optional. | 
|  | 139 | * This can be explicitly run to avoid setup delay on first trace function. | 
|  | 140 | */ | 
|  | 141 | #define ATRACE_INIT() atrace_init() | 
|  | 142 | static inline void atrace_init() | 
|  | 143 | { | 
| Yabin Cui | a8ac32c | 2015-04-15 14:50:27 -0700 | [diff] [blame] | 144 | if (CC_UNLIKELY(!atomic_load_explicit(&atrace_is_ready, memory_order_acquire))) { | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 145 | atrace_setup(); | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | /** | 
|  | 150 | * Get the mask of all tags currently enabled. | 
|  | 151 | * It can be used as a guard condition around more expensive trace calculations. | 
|  | 152 | * Every trace function calls this, which ensures atrace_init is run. | 
|  | 153 | */ | 
|  | 154 | #define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags() | 
|  | 155 | static inline uint64_t atrace_get_enabled_tags() | 
|  | 156 | { | 
|  | 157 | atrace_init(); | 
|  | 158 | return atrace_enabled_tags; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | /** | 
|  | 162 | * Test if a given tag is currently enabled. | 
|  | 163 | * Returns nonzero if the tag is enabled, otherwise zero. | 
|  | 164 | * It can be used as a guard condition around more expensive trace calculations. | 
|  | 165 | */ | 
|  | 166 | #define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG) | 
|  | 167 | static inline uint64_t atrace_is_tag_enabled(uint64_t tag) | 
|  | 168 | { | 
|  | 169 | return atrace_get_enabled_tags() & tag; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | /** | 
|  | 173 | * Trace the beginning of a context.  name is used to identify the context. | 
|  | 174 | * This is often used to time function execution. | 
|  | 175 | */ | 
|  | 176 | #define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name) | 
|  | 177 | static inline void atrace_begin(uint64_t tag, const char* name) | 
|  | 178 | { | 
|  | 179 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 180 | void atrace_begin_body(const char*); | 
|  | 181 | atrace_begin_body(name); | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | /** | 
|  | 186 | * Trace the end of a context. | 
|  | 187 | * This should match up (and occur after) a corresponding ATRACE_BEGIN. | 
|  | 188 | */ | 
|  | 189 | #define ATRACE_END() atrace_end(ATRACE_TAG) | 
|  | 190 | static inline void atrace_end(uint64_t tag) | 
|  | 191 | { | 
|  | 192 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Colin Cross | 9993e79 | 2016-09-16 10:12:52 -0700 | [diff] [blame] | 193 | void atrace_end_body(); | 
|  | 194 | atrace_end_body(); | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | /** | 
| Alex Ray | 6631777 | 2013-03-17 22:39:01 -0700 | [diff] [blame] | 199 | * Trace the beginning of an asynchronous event. Unlike ATRACE_BEGIN/ATRACE_END | 
|  | 200 | * contexts, asynchronous events do not need to be nested. The name describes | 
|  | 201 | * the event, and the cookie provides a unique identifier for distinguishing | 
|  | 202 | * simultaneous events. The name and cookie used to begin an event must be | 
|  | 203 | * used to end it. | 
|  | 204 | */ | 
|  | 205 | #define ATRACE_ASYNC_BEGIN(name, cookie) \ | 
|  | 206 | atrace_async_begin(ATRACE_TAG, name, cookie) | 
|  | 207 | static inline void atrace_async_begin(uint64_t tag, const char* name, | 
|  | 208 | int32_t cookie) | 
|  | 209 | { | 
|  | 210 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 211 | void atrace_async_begin_body(const char*, int32_t); | 
|  | 212 | atrace_async_begin_body(name, cookie); | 
| Alex Ray | 6631777 | 2013-03-17 22:39:01 -0700 | [diff] [blame] | 213 | } | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | /** | 
|  | 217 | * Trace the end of an asynchronous event. | 
|  | 218 | * This should have a corresponding ATRACE_ASYNC_BEGIN. | 
|  | 219 | */ | 
|  | 220 | #define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie) | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 221 | static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie) | 
| Alex Ray | 6631777 | 2013-03-17 22:39:01 -0700 | [diff] [blame] | 222 | { | 
|  | 223 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 224 | void atrace_async_end_body(const char*, int32_t); | 
|  | 225 | atrace_async_end_body(name, cookie); | 
| Alex Ray | 6631777 | 2013-03-17 22:39:01 -0700 | [diff] [blame] | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
| Alex Ray | 6631777 | 2013-03-17 22:39:01 -0700 | [diff] [blame] | 229 | /** | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 230 | * Traces an integer counter value.  name is used to identify the counter. | 
|  | 231 | * This can be used to track how a value changes over time. | 
|  | 232 | */ | 
|  | 233 | #define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value) | 
|  | 234 | static inline void atrace_int(uint64_t tag, const char* name, int32_t value) | 
|  | 235 | { | 
|  | 236 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 237 | void atrace_int_body(const char*, int32_t); | 
|  | 238 | atrace_int_body(name, value); | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 239 | } | 
|  | 240 | } | 
|  | 241 |  | 
| Jamie Gennis | f1921c7 | 2013-09-18 12:01:18 -0700 | [diff] [blame] | 242 | /** | 
|  | 243 | * Traces a 64-bit integer counter value.  name is used to identify the | 
|  | 244 | * counter. This can be used to track how a value changes over time. | 
|  | 245 | */ | 
|  | 246 | #define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value) | 
|  | 247 | static inline void atrace_int64(uint64_t tag, const char* name, int64_t value) | 
|  | 248 | { | 
|  | 249 | if (CC_UNLIKELY(atrace_is_tag_enabled(tag))) { | 
| Chih-Hung Hsieh | 2d3150e | 2014-10-13 16:30:24 -0700 | [diff] [blame] | 250 | void atrace_int64_body(const char*, int64_t); | 
|  | 251 | atrace_int64_body(name, value); | 
| Jamie Gennis | f1921c7 | 2013-09-18 12:01:18 -0700 | [diff] [blame] | 252 | } | 
|  | 253 | } | 
|  | 254 |  | 
| Alex Ray | 0a34643 | 2012-11-14 17:25:28 -0800 | [diff] [blame] | 255 | __END_DECLS | 
|  | 256 |  | 
|  | 257 | #endif // _LIBS_CUTILS_TRACE_H |