1 // Copyright 2015 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 #ifndef SkTraceEventCommon_DEFINED 5 #define SkTraceEventCommon_DEFINED 6 7 #include "include/core/SkTypes.h" 8 #include "include/utils/SkTraceEventPhase.h" 9 10 // Trace events are for tracking application performance and resource usage. 11 // Macros are provided to track: 12 // Duration of scoped regions 13 // Instantaneous events 14 // Counters 15 // 16 // The first two arguments to all TRACE macros are the category and name. Both are strings, and 17 // must have application lifetime (statics or literals). The same applies to arg_names, and string 18 // argument values. However, you can force a copy of a string argument value with TRACE_STR_COPY: 19 // TRACE_EVENT1("category", "name", "arg1", "literal string is only referenced"); 20 // TRACE_EVENT1("category", "name", "arg1", TRACE_STR_COPY("string will be copied")); 21 // 22 // 23 // Categories are used to group events, and 24 // can be enabled or disabled by the tracing framework. The trace system will automatically add the 25 // process id, thread id, and microsecond timestamp to all events. 26 // 27 // 28 // The TRACE_EVENT[0-2] macros trace the duration of entire scopes: 29 // void doSomethingCostly() { 30 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly"); 31 // ... 32 // } 33 // 34 // Additional parameters can be associated with an event: 35 // void doSomethingCostly2(int howMuch) { 36 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly", "howMuch", howMuch); 37 // ... 38 // } 39 // 40 // 41 // Trace event also supports counters, which is a way to track a quantity as it varies over time. 42 // Counters are created with the following macro: 43 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue); 44 // 45 // Counters are process-specific. The macro itself can be issued from any thread, however. 46 // 47 // Sometimes, you want to track two counters at once. You can do this with two counter macros: 48 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]); 49 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]); 50 // Or you can do it with a combined macro: 51 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter", 52 // "bytesPinned", g_myCounterValue[0], 53 // "bytesAllocated", g_myCounterValue[1]); 54 // The tracing UI will show these counters in a single graph, as a summed area chart. 55 56 #if defined(TRACE_EVENT0) 57 #error "Another copy of this file has already been included." 58 #endif 59 60 #define TRACE_EMPTY do {} while (0) 61 62 #ifdef SK_DISABLE_TRACING 63 64 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY 65 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY 66 #define TRACE_EVENT0(cg, n) TRACE_EMPTY 67 #define TRACE_EVENT0_ALWAYS(cg, n) TRACE_EMPTY 68 #define TRACE_EVENT1(cg, n, a1n, a1v) TRACE_EMPTY 69 #define TRACE_EVENT2(cg, n, a1n, a1v, a2n, a2v) TRACE_EMPTY 70 #define TRACE_EVENT_INSTANT0(cg, n, scope) TRACE_EMPTY 71 #define TRACE_EVENT_INSTANT1(cg, n, scope, a1n, a1v) TRACE_EMPTY 72 #define TRACE_EVENT_INSTANT2(cg, n, scope, a1n, a1v, a2n, a2v) TRACE_EMPTY 73 #define TRACE_COUNTER1(cg, n, value) TRACE_EMPTY 74 #define TRACE_COUNTER2(cg, n, v1n, v1v, v2n, v2v) TRACE_EMPTY 75 76 #elif defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) 77 78 #include <cutils/trace.h> 79 #include <stdarg.h> 80 81 class SkAndroidFrameworkTraceUtil { 82 public: SkAndroidFrameworkTraceUtil(const char * name)83 SkAndroidFrameworkTraceUtil(const char* name) { 84 if (CC_UNLIKELY(gEnableAndroidTracing)) { 85 ATRACE_BEGIN(name); 86 } 87 } SkAndroidFrameworkTraceUtil(bool,const char * fmt,...)88 SkAndroidFrameworkTraceUtil(bool, const char* fmt, ...) { 89 if (CC_LIKELY((!gEnableAndroidTracing) || (!ATRACE_ENABLED()))) return; 90 91 const int BUFFER_SIZE = 256; 92 va_list ap; 93 char buf[BUFFER_SIZE]; 94 95 va_start(ap, fmt); 96 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 97 va_end(ap); 98 99 ATRACE_BEGIN(buf); 100 } ~SkAndroidFrameworkTraceUtil()101 ~SkAndroidFrameworkTraceUtil() { 102 if (CC_UNLIKELY(gEnableAndroidTracing)) { 103 ATRACE_END(); 104 } 105 } 106 setEnableTracing(bool enableAndroidTracing)107 static void setEnableTracing(bool enableAndroidTracing) { 108 gEnableAndroidTracing = enableAndroidTracing; 109 } 110 getEnableTracing()111 static bool getEnableTracing() { 112 return gEnableAndroidTracing; 113 } 114 115 private: 116 static bool gEnableAndroidTracing; 117 }; 118 119 class SkAndroidFrameworkTraceUtilAlways { 120 public: SkAndroidFrameworkTraceUtilAlways(const char * fmt,...)121 SkAndroidFrameworkTraceUtilAlways(const char* fmt, ...) { 122 if (!ATRACE_ENABLED()) return; 123 124 const int BUFFER_SIZE = 256; 125 va_list ap; 126 char buf[BUFFER_SIZE]; 127 128 va_start(ap, fmt); 129 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 130 va_end(ap); 131 132 ATRACE_BEGIN(buf); 133 } ~SkAndroidFrameworkTraceUtilAlways()134 ~SkAndroidFrameworkTraceUtilAlways() { 135 ATRACE_END(); 136 } 137 }; 138 139 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) SkAndroidFrameworkTraceUtil __trace(true, fmt, ##__VA_ARGS__) 140 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) SkAndroidFrameworkTraceUtilAlways __trace_always(fmt, ##__VA_ARGS__) 141 142 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2 143 // associated arguments. In the framework, the arguments are ignored. 144 #define TRACE_EVENT0(category_group, name) \ 145 SkAndroidFrameworkTraceUtil __trace(name) 146 #define TRACE_EVENT0_ALWAYS(category_group, name) \ 147 SkAndroidFrameworkTraceUtilAlways __trace_always(name) 148 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \ 149 SkAndroidFrameworkTraceUtil __trace(name) 150 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \ 151 SkAndroidFrameworkTraceUtil __trace(name) 152 153 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the 154 // category is not enabled, then this does nothing. 155 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \ 156 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 157 158 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \ 159 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 160 161 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \ 162 arg2_name, arg2_val) \ 163 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 164 165 // Records the value of a counter called "name" immediately. Value 166 // must be representable as a 32 bit integer. 167 #define TRACE_COUNTER1(category_group, name, value) \ 168 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ 169 ATRACE_INT(name, value); \ 170 } 171 172 // Records the values of a multi-parted counter called "name" immediately. 173 // In Chrome, this macro produces a stacked bar chart. ATrace doesn't support 174 // that, so this just produces two separate counters. 175 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val) \ 176 do { \ 177 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ 178 ATRACE_INT(name "-" value1_name, value1_val); \ 179 ATRACE_INT(name "-" value2_name, value2_val); \ 180 } \ 181 } while (0) 182 183 // ATrace has no object tracking 184 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) TRACE_EMPTY 185 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) TRACE_EMPTY 186 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) TRACE_EMPTY 187 188 // Macro to efficiently determine if a given category group is enabled. 189 // This is only used for some shader text logging that isn't supported in ATrace anyway. 190 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \ 191 do { *ret = false; } while (0) 192 193 #else // !SK_BUILD_FOR_ANDROID_FRAMEWORK && !SK_DISABLE_TRACING 194 195 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY 196 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY 197 198 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2 199 // associated arguments. If the category is not enabled, then this does nothing. 200 #define TRACE_EVENT0(category_group, name) \ 201 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name) 202 203 #define TRACE_EVENT0_ALWAYS(category_group, name) \ 204 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name) 205 206 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \ 207 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val) 208 209 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \ 210 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) 211 212 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the 213 // category is not enabled, then this does nothing. 214 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \ 215 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 216 TRACE_EVENT_FLAG_NONE | scope) 217 218 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \ 219 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 220 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val) 221 222 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \ 223 arg2_name, arg2_val) \ 224 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 225 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \ 226 arg2_name, arg2_val) 227 228 // Records the value of a counter called "name" immediately. Value 229 // must be representable as a 32 bit integer. 230 #define TRACE_COUNTER1(category_group, name, value) \ 231 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \ 232 TRACE_EVENT_FLAG_NONE, "value", \ 233 static_cast<int>(value)) 234 235 // Records the values of a multi-parted counter called "name" immediately. 236 // The UI will treat value1 and value2 as parts of a whole, displaying their 237 // values as a stacked-bar chart. 238 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \ 239 value2_name, value2_val) \ 240 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \ 241 TRACE_EVENT_FLAG_NONE, value1_name, \ 242 static_cast<int>(value1_val), value2_name, \ 243 static_cast<int>(value2_val)) 244 245 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ 246 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 247 TRACE_EVENT_PHASE_ASYNC_BEGIN, category, name, id, TRACE_EVENT_FLAG_NONE) 248 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ 249 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 250 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 251 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \ 252 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 253 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val) 254 255 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ 256 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 257 category, name, id, TRACE_EVENT_FLAG_NONE) 258 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ 259 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 260 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 261 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \ 262 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 263 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val) 264 265 // Macros to track the life time and value of arbitrary client objects. 266 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \ 267 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 268 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name, id, \ 269 TRACE_EVENT_FLAG_NONE) 270 271 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \ 272 snapshot) \ 273 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 274 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \ 275 id, TRACE_EVENT_FLAG_NONE, "snapshot", snapshot) 276 277 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \ 278 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 279 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name, id, \ 280 TRACE_EVENT_FLAG_NONE) 281 282 // Macro to efficiently determine if a given category group is enabled. 283 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \ 284 do { \ 285 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 286 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 287 *ret = true; \ 288 } else { \ 289 *ret = false; \ 290 } \ 291 } while (0) 292 293 #endif 294 295 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. 296 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0)) 297 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0)) 298 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1)) 299 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2)) 300 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3)) 301 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4)) 302 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5)) 303 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6)) 304 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7)) 305 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8)) 306 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9)) 307 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10)) 308 309 #define TRACE_EVENT_FLAG_SCOPE_MASK \ 310 (static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \ 311 TRACE_EVENT_FLAG_SCOPE_EXTRA)) 312 313 // Type values for identifying types in the TraceValue union. 314 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) 315 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) 316 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) 317 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) 318 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) 319 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) 320 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) 321 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8)) 322 323 // Enum reflecting the scope of an INSTANT event. Must fit within TRACE_EVENT_FLAG_SCOPE_MASK. 324 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3)) 325 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3)) 326 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3)) 327 328 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g') 329 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p') 330 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t') 331 332 #endif // SkTraceEventCommon_DEFINED 333