1 // Copyright (c) 2014 Google Inc.
2 //
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 // This header file defines implementation details of how the trace macros in
7 // SkTraceEventCommon.h collect and store trace events. Anything not
8 // implementation-specific should go in SkTraceEventCommon.h instead of here.
9
10 #ifndef SkTraceEvent_DEFINED
11 #define SkTraceEvent_DEFINED
12
13 #include "include/utils/SkEventTracer.h"
14 #include "src/core/SkTraceEventCommon.h"
15 #include <atomic>
16
17 #if defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
18 #include <string>
19 #include <utility>
20 #endif
21
22 ////////////////////////////////////////////////////////////////////////////////
23 // Implementation specific tracing API definitions.
24
25 // Makes it easier to add traces with a simple TRACE_EVENT0("skia", TRACE_FUNC).
26 #if defined(_MSC_VER)
27 #define TRACE_FUNC __FUNCSIG__
28 #else
29 #define TRACE_FUNC __PRETTY_FUNCTION__
30 #endif
31
32
33 #if defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
34 // By default, const char* argument values are assumed to have long-lived scope
35 // and will not be copied. Use this macro to force a const char* to be copied.
36 //
37 // TRACE_STR_COPY should be used with short-lived strings that should be copied immediately.
38 // TRACE_STR_STATIC should be used with pointers to string literals with process lifetime.
39 // Neither should be used for string literals known at compile time.
40 //
41 // E.g. TRACE_EVENT0("skia", TRACE_STR_COPY(something.c_str()));
42 #define TRACE_STR_COPY(str) (::perfetto::DynamicString{str})
43
44 // Allows callers to pass static strings that aren't known at compile time to trace functions.
45 //
46 // TRACE_STR_COPY should be used with short-lived strings that should be copied immediately.
47 // TRACE_STR_STATIC should be used with pointers to string literals with process lifetime.
48 // Neither should be used for string literals known at compile time.
49 //
50 // E.g. TRACE_EVENT0("skia", TRACE_STR_STATIC(this->name()));
51 // No-op when Perfetto is disabled, or outside of Android framework.
52 #define TRACE_STR_STATIC(str) (::perfetto::StaticString{str})
53 #else // !SK_ANDROID_FRAMEWORK_USE_PERFETTO
54 // By default, const char* argument values are assumed to have long-lived scope
55 // and will not be copied. Use this macro to force a const char* to be copied.
56 //
57 // TRACE_STR_COPY should be used with short-lived strings that should be copied immediately.
58 // TRACE_STR_STATIC should be used with pointers to string literals with process lifetime.
59 // Neither should be used for string literals known at compile time.
60 //
61 // E.g. TRACE_EVENT0("skia", TRACE_STR_COPY(something.c_str()));
62 #define TRACE_STR_COPY(str) (::skia_private::TraceStringWithCopy(str))
63
64 // Allows callers to pass static strings that aren't known at compile time to trace functions.
65 //
66 // TRACE_STR_COPY should be used with short-lived strings that should be copied immediately.
67 // TRACE_STR_STATIC should be used with pointers to string literals with process lifetime.
68 // Neither should be used for string literals known at compile time.
69 //
70 // E.g. TRACE_EVENT0("skia", TRACE_STR_STATIC(this->name()));
71 // No-op when Perfetto is disabled, or outside of Android framework.
72 #define TRACE_STR_STATIC(str) (str)
73 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
74
75 #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
76 *INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
77 (SkEventTracer::kEnabledForRecording_CategoryGroupEnabledFlags | \
78 SkEventTracer::kEnabledForEventCallback_CategoryGroupEnabledFlags)
79
80 // Get a pointer to the enabled state of the given trace category. Only
81 // long-lived literal strings should be given as the category group. The
82 // returned pointer can be held permanently in a local static for example. If
83 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
84 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
85 // between the load of the tracing state and the call to
86 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
87 // for best performance when tracing is disabled.
88 // const uint8_t*
89 // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
90 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
91 SkEventTracer::GetInstance()->getCategoryGroupEnabled
92
93 // Add a trace event to the platform tracing system.
94 // SkEventTracer::Handle TRACE_EVENT_API_ADD_TRACE_EVENT(
95 // char phase,
96 // const uint8_t* category_group_enabled,
97 // const char* name,
98 // uint64_t id,
99 // int num_args,
100 // const char** arg_names,
101 // const uint8_t* arg_types,
102 // const uint64_t* arg_values,
103 // unsigned char flags)
104 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
105 SkEventTracer::GetInstance()->addTraceEvent
106
107 // Set the duration field of a COMPLETE trace event.
108 // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
109 // const uint8_t* category_group_enabled,
110 // const char* name,
111 // SkEventTracer::Handle id)
112 #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
113 SkEventTracer::GetInstance()->updateTraceEventDuration
114
115 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
116 #define TRACE_EVENT_API_NEW_TRACE_SECTION(...) do {} while (0)
117 #else
118 // Start writing to a new trace output section (file, etc.).
119 // Accepts a label for the new section.
120 // void TRACE_EVENT_API_NEW_TRACE_SECTION(const char* name)
121 #define TRACE_EVENT_API_NEW_TRACE_SECTION \
122 SkEventTracer::GetInstance()->newTracingSection
123 #endif
124
125 // Defines visibility for classes in trace_event.h
126 #define TRACE_EVENT_API_CLASS_EXPORT SK_API
127
128 // We prepend this string to all category names, so that ALL Skia trace events are
129 // disabled by default when tracing in Chrome.
130 #define TRACE_CATEGORY_PREFIX "disabled-by-default-"
131
132 ////////////////////////////////////////////////////////////////////////////////
133
134 // Implementation detail: trace event macros create temporary variables
135 // to keep instrumentation overhead low. These macros give each temporary
136 // variable a unique name based on the line number to prevent name collisions.
137 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
138 trace_event_unique_##a##b
139 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
140 INTERNAL_TRACE_EVENT_UID3(a,b)
141 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
142 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
143
144 // Implementation detail: internal macro to create static category.
145 // No barriers are needed, because this code is designed to operate safely
146 // even when the unsigned char* points to garbage data (which may be the case
147 // on processors without cache coherency).
148 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
149 category_group, atomic, category_group_enabled) \
150 category_group_enabled = \
151 reinterpret_cast<const uint8_t*>(atomic.load(std::memory_order_relaxed)); \
152 if (!category_group_enabled) { \
153 category_group_enabled = TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
154 atomic.store(reinterpret_cast<intptr_t>(category_group_enabled), \
155 std::memory_order_relaxed); \
156 }
157
158 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
159 static std::atomic<intptr_t> INTERNAL_TRACE_EVENT_UID(atomic){0}; \
160 const uint8_t* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
161 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
162 TRACE_CATEGORY_PREFIX category_group, \
163 INTERNAL_TRACE_EVENT_UID(atomic), \
164 INTERNAL_TRACE_EVENT_UID(category_group_enabled));
165
166 // Implementation detail: internal macro to create static category and add
167 // event if the category is enabled.
168 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
169 do { \
170 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
171 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
172 skia_private::AddTraceEvent( \
173 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
174 skia_private::kNoEventId, flags, ##__VA_ARGS__); \
175 } \
176 } while (0)
177
178 // Implementation detail: internal macro to create static category and add
179 // event if the category is enabled.
180 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
181 flags, ...) \
182 do { \
183 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
184 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
185 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
186 skia_private::TraceID trace_event_trace_id( \
187 id, &trace_event_flags); \
188 skia_private::AddTraceEvent( \
189 phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
190 name, trace_event_trace_id.data(), trace_event_flags, \
191 ##__VA_ARGS__); \
192 } \
193 } while (0)
194
195 // Implementation detail: internal macro to create static category and add begin
196 // event if the category is enabled. Also adds the end event when the scope
197 // ends.
198 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
199 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
200 skia_private::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
201 do { \
202 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
203 SkEventTracer::Handle h = skia_private::AddTraceEvent( \
204 TRACE_EVENT_PHASE_COMPLETE, \
205 INTERNAL_TRACE_EVENT_UID(category_group_enabled), \
206 name, skia_private::kNoEventId, \
207 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
208 INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
209 INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
210 } \
211 } while (0)
212
213 namespace skia_private {
214
215 // Specify these values when the corresponding argument of AddTraceEvent is not
216 // used.
217 const int kZeroNumArgs = 0;
218 const uint64_t kNoEventId = 0;
219
220 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
221 // are by default mangled with the Process ID so that they are unlikely to
222 // collide when the same pointer is used on different processes.
223 class TraceID {
224 public:
TraceID(const void * id,unsigned char * flags)225 TraceID(const void* id, unsigned char* flags)
226 : data_(static_cast<uint64_t>(reinterpret_cast<uintptr_t>(id))) {
227 *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
228 }
TraceID(uint64_t id,unsigned char * flags)229 TraceID(uint64_t id, unsigned char* flags)
230 : data_(id) { (void)flags; }
TraceID(unsigned int id,unsigned char * flags)231 TraceID(unsigned int id, unsigned char* flags)
232 : data_(id) { (void)flags; }
TraceID(unsigned short id,unsigned char * flags)233 TraceID(unsigned short id, unsigned char* flags)
234 : data_(id) { (void)flags; }
TraceID(unsigned char id,unsigned char * flags)235 TraceID(unsigned char id, unsigned char* flags)
236 : data_(id) { (void)flags; }
TraceID(long long id,unsigned char * flags)237 TraceID(long long id, unsigned char* flags)
238 : data_(static_cast<uint64_t>(id)) { (void)flags; }
TraceID(long id,unsigned char * flags)239 TraceID(long id, unsigned char* flags)
240 : data_(static_cast<uint64_t>(id)) { (void)flags; }
TraceID(int id,unsigned char * flags)241 TraceID(int id, unsigned char* flags)
242 : data_(static_cast<uint64_t>(id)) { (void)flags; }
TraceID(short id,unsigned char * flags)243 TraceID(short id, unsigned char* flags)
244 : data_(static_cast<uint64_t>(id)) { (void)flags; }
TraceID(signed char id,unsigned char * flags)245 TraceID(signed char id, unsigned char* flags)
246 : data_(static_cast<uint64_t>(id)) { (void)flags; }
247
data()248 uint64_t data() const { return data_; }
249
250 private:
251 uint64_t data_;
252 };
253
254 // Simple union to store various types as uint64_t.
255 union TraceValueUnion {
256 bool as_bool;
257 uint64_t as_uint;
258 long long as_int;
259 double as_double;
260 const void* as_pointer;
261 const char* as_string;
262 };
263
264 // Simple container for const char* that should be copied instead of retained.
265 class TraceStringWithCopy {
266 public:
TraceStringWithCopy(const char * str)267 explicit TraceStringWithCopy(const char* str) : str_(str) {}
268 operator const char* () const { return str_; }
269 private:
270 const char* str_;
271 };
272
273 // Define SetTraceValue for each allowed type. It stores the type and
274 // value in the return arguments. This allows this API to avoid declaring any
275 // structures so that it is portable to third_party libraries.
276 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
277 union_member, \
278 value_type_id) \
279 static inline void SetTraceValue( \
280 actual_type arg, \
281 unsigned char* type, \
282 uint64_t* value) { \
283 TraceValueUnion type_value; \
284 type_value.union_member = arg; \
285 *type = value_type_id; \
286 *value = type_value.as_uint; \
287 }
288 // Simpler form for int types that can be safely casted.
289 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
290 value_type_id) \
291 static inline void SetTraceValue( \
292 actual_type arg, \
293 unsigned char* type, \
294 uint64_t* value) { \
295 *type = value_type_id; \
296 *value = static_cast<uint64_t>(arg); \
297 }
298
INTERNAL_DECLARE_SET_TRACE_VALUE_INT(uint64_t,TRACE_VALUE_TYPE_UINT)299 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(uint64_t, TRACE_VALUE_TYPE_UINT)
300 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
301 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
302 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
303 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
304 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
305 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
306 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
307 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
308 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
309 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
310 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, TRACE_VALUE_TYPE_POINTER)
311 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, TRACE_VALUE_TYPE_STRING)
312 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
313 TRACE_VALUE_TYPE_COPY_STRING)
314
315 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
316 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
317
318 // These AddTraceEvent and AddTraceEvent template
319 // functions are defined here instead of in the macro, because the arg_values
320 // could be temporary objects, such as std::string. In order to store
321 // pointers to the internal c_str and pass through to the tracing API,
322 // the arg_values must live throughout these procedures.
323
324 static inline SkEventTracer::Handle
325 AddTraceEvent(
326 char phase,
327 const uint8_t* category_group_enabled,
328 const char* name,
329 uint64_t id,
330 unsigned char flags) {
331 return TRACE_EVENT_API_ADD_TRACE_EVENT(
332 phase, category_group_enabled, name, id,
333 kZeroNumArgs, nullptr, nullptr, nullptr, flags);
334 }
335
336 template<class ARG1_TYPE>
337 static inline SkEventTracer::Handle
AddTraceEvent(char phase,const uint8_t * category_group_enabled,const char * name,uint64_t id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val)338 AddTraceEvent(
339 char phase,
340 const uint8_t* category_group_enabled,
341 const char* name,
342 uint64_t id,
343 unsigned char flags,
344 const char* arg1_name,
345 const ARG1_TYPE& arg1_val) {
346 const int num_args = 1;
347 uint8_t arg_types[1];
348 uint64_t arg_values[1];
349 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
350 return TRACE_EVENT_API_ADD_TRACE_EVENT(
351 phase, category_group_enabled, name, id,
352 num_args, &arg1_name, arg_types, arg_values, flags);
353 }
354
355 template<class ARG1_TYPE, class ARG2_TYPE>
356 static inline SkEventTracer::Handle
AddTraceEvent(char phase,const uint8_t * category_group_enabled,const char * name,uint64_t id,unsigned char flags,const char * arg1_name,const ARG1_TYPE & arg1_val,const char * arg2_name,const ARG2_TYPE & arg2_val)357 AddTraceEvent(
358 char phase,
359 const uint8_t* category_group_enabled,
360 const char* name,
361 uint64_t id,
362 unsigned char flags,
363 const char* arg1_name,
364 const ARG1_TYPE& arg1_val,
365 const char* arg2_name,
366 const ARG2_TYPE& arg2_val) {
367 const int num_args = 2;
368 const char* arg_names[2] = { arg1_name, arg2_name };
369 unsigned char arg_types[2];
370 uint64_t arg_values[2];
371 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
372 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
373 return TRACE_EVENT_API_ADD_TRACE_EVENT(
374 phase, category_group_enabled, name, id,
375 num_args, arg_names, arg_types, arg_values, flags);
376 }
377
378 // Used by TRACE_EVENTx macros. Do not use directly.
379 class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
380 public:
381 // Note: members of data_ intentionally left uninitialized. See Initialize.
ScopedTracer()382 ScopedTracer() : p_data_(nullptr) {}
383
~ScopedTracer()384 ~ScopedTracer() {
385 if (p_data_ && *data_.category_group_enabled)
386 TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
387 data_.category_group_enabled, data_.name, data_.event_handle);
388 }
389
Initialize(const uint8_t * category_group_enabled,const char * name,SkEventTracer::Handle event_handle)390 void Initialize(const uint8_t* category_group_enabled,
391 const char* name,
392 SkEventTracer::Handle event_handle) {
393 data_.category_group_enabled = category_group_enabled;
394 data_.name = name;
395 data_.event_handle = event_handle;
396 p_data_ = &data_;
397 }
398
399 private:
400 ScopedTracer(const ScopedTracer&) = delete;
401 ScopedTracer& operator=(const ScopedTracer&) = delete;
402
403 // This Data struct workaround is to avoid initializing all the members
404 // in Data during construction of this object, since this object is always
405 // constructed, even when tracing is disabled. If the members of Data were
406 // members of this class instead, compiler warnings occur about potential
407 // uninitialized accesses.
408 struct Data {
409 const uint8_t* category_group_enabled;
410 const char* name;
411 SkEventTracer::Handle event_handle;
412 };
413 Data* p_data_;
414 Data data_;
415 };
416
417 } // namespace skia_private
418
419 #endif
420