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 "SkTraceEventPhase.h" 8 #include "SkTypes.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_BUILD_FOR_ANDROID_FRAMEWORK 63 64 #include <cutils/trace.h> 65 #include <stdarg.h> 66 67 class SkAndroidFrameworkTraceUtil { 68 public: SkAndroidFrameworkTraceUtil(const char * name)69 SkAndroidFrameworkTraceUtil(const char* name) { 70 if (CC_UNLIKELY(gEnableAndroidTracing)) { 71 ATRACE_BEGIN(name); 72 } 73 } SkAndroidFrameworkTraceUtil(bool,const char * fmt,...)74 SkAndroidFrameworkTraceUtil(bool, const char* fmt, ...) { 75 if (CC_LIKELY((!gEnableAndroidTracing) || (!ATRACE_ENABLED()))) return; 76 77 const int BUFFER_SIZE = 256; 78 va_list ap; 79 char buf[BUFFER_SIZE]; 80 81 va_start(ap, fmt); 82 vsnprintf(buf, BUFFER_SIZE, fmt, ap); 83 va_end(ap); 84 85 ATRACE_BEGIN(buf); 86 } ~SkAndroidFrameworkTraceUtil()87 ~SkAndroidFrameworkTraceUtil() { 88 if (CC_UNLIKELY(gEnableAndroidTracing)) { 89 ATRACE_END(); 90 } 91 } 92 setEnableTracing(bool enableAndroidTracing)93 static void setEnableTracing(bool enableAndroidTracing) { 94 gEnableAndroidTracing = enableAndroidTracing; 95 } 96 getEnableTracing()97 static bool getEnableTracing() { 98 return gEnableAndroidTracing; 99 } 100 101 private: 102 static bool gEnableAndroidTracing; 103 }; 104 105 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) SkAndroidFrameworkTraceUtil __trace(true, fmt, ##__VA_ARGS__) 106 107 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2 108 // associated arguments. In the framework, the arguments are ignored. 109 #define TRACE_EVENT0(category_group, name) \ 110 SkAndroidFrameworkTraceUtil __trace(name) 111 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \ 112 SkAndroidFrameworkTraceUtil __trace(name) 113 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \ 114 SkAndroidFrameworkTraceUtil __trace(name) 115 116 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the 117 // category is not enabled, then this does nothing. 118 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \ 119 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 120 121 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \ 122 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 123 124 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \ 125 arg2_name, arg2_val) \ 126 do { SkAndroidFrameworkTraceUtil __trace(name); } while(0) 127 128 // Records the value of a counter called "name" immediately. Value 129 // must be representable as a 32 bit integer. 130 #define TRACE_COUNTER1(category_group, name, value) \ 131 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ 132 ATRACE_INT(name, value); \ 133 } 134 135 // Records the values of a multi-parted counter called "name" immediately. 136 // In Chrome, this macro produces a stacked bar chart. ATrace doesn't support 137 // that, so this just produces two separate counters. 138 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val) \ 139 do { \ 140 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \ 141 ATRACE_INT(name "-" value1_name, value1_val); \ 142 ATRACE_INT(name "-" value2_name, value2_val); \ 143 } \ 144 } while (0) 145 146 // ATrace has no object tracking 147 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) TRACE_EMPTY 148 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) TRACE_EMPTY 149 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) TRACE_EMPTY 150 151 // Macro to efficiently determine if a given category group is enabled. 152 // This is only used for some shader text logging that isn't supported in ATrace anyway. 153 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \ 154 do { *ret = false; } while (0) 155 156 #else // !SK_BUILD_FOR_ANDROID_FRAMEWORK 157 158 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY 159 160 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2 161 // associated arguments. If the category is not enabled, then this does nothing. 162 #define TRACE_EVENT0(category_group, name) \ 163 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name) 164 165 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \ 166 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val) 167 168 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \ 169 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) 170 171 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the 172 // category is not enabled, then this does nothing. 173 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \ 174 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 175 TRACE_EVENT_FLAG_NONE | scope) 176 177 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \ 178 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 179 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val) 180 181 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \ 182 arg2_name, arg2_val) \ 183 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \ 184 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \ 185 arg2_name, arg2_val) 186 187 // Records the value of a counter called "name" immediately. Value 188 // must be representable as a 32 bit integer. 189 #define TRACE_COUNTER1(category_group, name, value) \ 190 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \ 191 TRACE_EVENT_FLAG_NONE, "value", \ 192 static_cast<int>(value)) 193 194 // Records the values of a multi-parted counter called "name" immediately. 195 // The UI will treat value1 and value2 as parts of a whole, displaying their 196 // values as a stacked-bar chart. 197 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \ 198 value2_name, value2_val) \ 199 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \ 200 TRACE_EVENT_FLAG_NONE, value1_name, \ 201 static_cast<int>(value1_val), value2_name, \ 202 static_cast<int>(value2_val)) 203 204 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ 205 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 206 TRACE_EVENT_PHASE_ASYNC_BEGIN, category, name, id, TRACE_EVENT_FLAG_NONE) 207 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ 208 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 209 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 210 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \ 211 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ 212 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val) 213 214 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ 215 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 216 category, name, id, TRACE_EVENT_FLAG_NONE) 217 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ 218 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 219 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) 220 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \ 221 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ 222 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val) 223 224 // Macros to track the life time and value of arbitrary client objects. 225 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \ 226 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 227 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name, id, \ 228 TRACE_EVENT_FLAG_NONE) 229 230 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \ 231 snapshot) \ 232 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 233 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \ 234 id, TRACE_EVENT_FLAG_NONE, "snapshot", snapshot) 235 236 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \ 237 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \ 238 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name, id, \ 239 TRACE_EVENT_FLAG_NONE) 240 241 // Macro to efficiently determine if a given category group is enabled. 242 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \ 243 do { \ 244 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \ 245 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \ 246 *ret = true; \ 247 } else { \ 248 *ret = false; \ 249 } \ 250 } while (0) 251 252 #endif 253 254 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. 255 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0)) 256 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0)) 257 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1)) 258 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2)) 259 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3)) 260 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4)) 261 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5)) 262 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6)) 263 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7)) 264 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8)) 265 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9)) 266 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10)) 267 268 #define TRACE_EVENT_FLAG_SCOPE_MASK \ 269 (static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \ 270 TRACE_EVENT_FLAG_SCOPE_EXTRA)) 271 272 // Type values for identifying types in the TraceValue union. 273 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) 274 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) 275 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) 276 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) 277 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) 278 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) 279 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) 280 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8)) 281 282 // Enum reflecting the scope of an INSTANT event. Must fit within TRACE_EVENT_FLAG_SCOPE_MASK. 283 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3)) 284 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3)) 285 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3)) 286 287 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g') 288 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p') 289 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t') 290 291 #endif // SkTraceEventCommon_DEFINED 292