Debian Codegraph @

src/core/SkTraceEventCommon.h

raw
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 // --- Temporary Perfetto migration shim preamble ---
61 // Tracing in the Android framework, and tracing with Perfetto, are both in a partially migrated
62 // state (but fully functional).
63 //
64 // See go/skia-perfetto
65 //
66 // For Android framework:
67 // ---
68 // 1. If SK_ANDROID_FRAMEWORK_USE_PERFETTO is not defined, then all tracing macros map to no-ops.
69 // This is only relevant to host-mode builds, where ATrace isn't supported anyway, and tracing with
70 // Perfetto seems unnecessary. Note that SkAndroidFrameworkTraceUtil is still defined (assuming
71 // SK_BUILD_FOR_ANDROID_FRAMEWORK is defined) to support HWUI referencing it in host-mode builds.
72 //
73 // 2. If SK_ANDROID_FRAMEWORK_USE_PERFETTO *is* defined, then the tracing backend can be switched
74 // between ATrace and Perfetto at runtime. This is currently *only* supported in Android framework.
75 // SkAndroidFrameworkTraceUtil::setEnableTracing(bool) will still control broad tracing overall, but
76 // SkAndroidFrameworkTraceUtil::setUsePerfettoTrackEvents(bool) will now determine whether that
77 // tracing is done with ATrace (default/false) or Perfetto (true).
78 //
79 // Note: if setUsePerfettoTrackEvents(true) is called, then Perfetto will remain initialized until
80 // the process ends. This means some minimal state overhead will remain even after subseqently
81 // switching the process back to ATrace, but individual trace events will be correctly routed to
82 // whichever system is active in the moment. However, trace events which have begun but have not yet
83 // ended when a switch occurs will likely be corrupted. Thus, it's best to minimize the frequency of
84 // switching backend tracing systems at runtime.
85 //
86 // For Perfetto outside of Android framework (e.g. tools):
87 // ---
88 // SK_USE_PERFETTO (mutually exclusive with SK_ANDROID_FRAMEWORK_USE_PERFETTO) can be used to unlock
89 // SkPerfettoTrace, which can be used for in-process tracing via the standard Skia tracing flow of
90 // SkEventTracer::SetInstance(...). This is enabled in tools with the `--trace perfetto` argument.
91 // See https://skia.org/docs/dev/tools/tracing/#tracing-with-perfetto for more on SK_USE_PERFETTO.
92
93 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
94
95 // PERFETTO_TRACK_EVENT_NAMESPACE must be defined before including Perfetto. This allows Skia to
96 // maintain separate "track event" category storage, etc. from codebases linked into the same
97 // executable, and avoid symbol duplication errors.
98 //
99 // NOTE: A side-effect of this is we must use skia::TrackEvent instead of perfetto::TrackEvent.
100 #define PERFETTO_TRACK_EVENT_NAMESPACE skia
101 #include <perfetto/tracing.h>
102
103 #include <cutils/trace.h>
104 #include <stdarg.h>
105 #include <string_view>
106
107 // WARNING: this list must be kept up to date with every category we use for tracing!
108 //
109 // When adding a new category it's likely best to add both "new_category" and "new_category.always",
110 // though not strictly required. "new_category.always" is used internally when "new_category" is
111 // given to TRACE_EVENTx_ALWAYS macros, which are used for core events that should always show up in
112 // traces for the Android framework. Adding both to begin with will likely reduce churn if/when
113 // "new_category" is used across both normal tracing macros and _ALWAYS variants in the future, but
114 // it's not a strict requirement.
115 //
116 // See stages section of go/skia-perfetto for timeline of when this should improve.
117 //
118 // TODO(b/262718654): make this compilation failure happen sooner than the Skia -> Android roll.
119 //
120 // Currently kept entirely separate from SkPerfettoTrace for simplicity, which uses dynamic
121 // categories and doesn't need these static category definitions.
122 PERFETTO_DEFINE_CATEGORIES(
123 perfetto::Category("GM"),
124 perfetto::Category("skia"),
125 perfetto::Category("skia.android"),
126 perfetto::Category("skia.gpu"),
127 perfetto::Category("skia.gpu.cache"),
128 perfetto::Category("skia.objects"),
129 perfetto::Category("skia.shaders"),
130 perfetto::Category("skottie"),
131 perfetto::Category("test"),
132 perfetto::Category("test_cpu"),
133 perfetto::Category("test_ganesh"),
134 perfetto::Category("test_graphite"),
135 // ".always" variants are currently required for any category used in TRACE_EVENTx_ALWAYS.
136 perfetto::Category("GM.always").SetTags("skia.always"),
137 perfetto::Category("skia.always").SetTags("skia.always"),
138 perfetto::Category("skia.android.always").SetTags("skia.always"),
139 perfetto::Category("skia.gpu.always").SetTags("skia.always"),
140 perfetto::Category("skia.gpu.cache.always").SetTags("skia.always"),
141 perfetto::Category("skia.objects.always").SetTags("skia.always"),
142 perfetto::Category("skia.shaders.always").SetTags("skia.always"),
143 perfetto::Category("skottie.always").SetTags("skia.always"),
144 perfetto::Category("test.always").SetTags("skia.always"),
145 perfetto::Category("test_cpu.always").SetTags("skia.always"),
146 perfetto::Category("test_ganesh.always").SetTags("skia.always"),
147 perfetto::Category("test_graphite.always").SetTags("skia.always"),
148 );
149
150 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
151
152 #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
153
154 #ifdef SK_DISABLE_TRACING
155 #error SK_DISABLE_TRACING and SK_BUILD_FOR_ANDROID_FRAMEWORK are mutually exclusive.
156 #endif // SK_DISABLE_TRACING [&& SK_BUILD_FOR_ANDROID_FRAMEWORK]
157
158 #define SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE 512
159
160 class SkAndroidFrameworkTraceUtil {
161 public:
162 SkAndroidFrameworkTraceUtil() = delete;
163
164 // Controls whether broad tracing is enabled. Warning: not thread-safe!
165 //
166 // Some key trace events may still be recorded when this is disabled, if a relevant tracing
167 // session is active.
168 //
169 // ATrace is used by default, but can be replaced with Perfetto by calling
170 // setUsePerfettoTrackEvents(true)
171 static void setEnableTracing(bool enableAndroidTracing) {
172 gEnableAndroidTracing = enableAndroidTracing;
173 }
174
175 // Controls whether tracing uses Perfetto instead of ATrace. Warning: not thread-safe!
176 //
177 // Returns true if Skia was built with Perfetto, false otherwise.
178 static bool setUsePerfettoTrackEvents(bool usePerfettoTrackEvents) {
179 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
180 // Ensure Perfetto is initialized if it wasn't already the preferred tracing backend.
181 if (!gUsePerfettoTrackEvents && usePerfettoTrackEvents) {
182 initPerfetto();
183 }
184 gUsePerfettoTrackEvents = usePerfettoTrackEvents;
185 return true;
186 #else // !SK_ANDROID_FRAMEWORK_USE_PERFETTO
187 return false;
188 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
189 }
190
191 static bool getEnableTracing() {
192 return gEnableAndroidTracing;
193 }
194
195 static bool getUsePerfettoTrackEvents() {
196 return gUsePerfettoTrackEvents;
197 }
198
199 private:
200 static bool gEnableAndroidTracing;
201 static bool gUsePerfettoTrackEvents;
202
203 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
204 // Initializes tracing systems, and establishes a connection to the 'traced' daemon.
205 //
206 // Can be called multiple times.
207 static void initPerfetto() {
208 ::perfetto::TracingInitArgs perfettoArgs;
209 perfettoArgs.backends |= perfetto::kSystemBackend;
210 ::perfetto::Tracing::Initialize(perfettoArgs);
211 ::skia::TrackEvent::Register();
212 }
213 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
214 };
215 #endif // SK_BUILD_FOR_ANDROID_FRAMEWORK
216
217 #ifdef SK_DEBUG
218 static void skprintf_like_noop(const char format[], ...) SK_PRINTF_LIKE(1, 2);
219 static inline void skprintf_like_noop(const char format[], ...) {}
220 template <typename... Args>
221 static inline void sk_noop(Args...) {}
222 #define TRACE_EMPTY(...) do { sk_noop(__VA_ARGS__); } while (0)
223 #define TRACE_EMPTY_FMT(fmt, ...) do { skprintf_like_noop(fmt, ##__VA_ARGS__); } while (0)
224 #else
225 #define TRACE_EMPTY(...) do {} while (0)
226 #define TRACE_EMPTY_FMT(fmt, ...) do {} while (0)
227 #endif
228
229 #if defined(SK_DISABLE_TRACING) || \
230 (defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO))
231
232 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
233 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
234 #define TRACE_EVENT0(cg, n) TRACE_EMPTY(cg, n)
235 #define TRACE_EVENT1(cg, n, a1n, a1v) TRACE_EMPTY(cg, n, a1n, a1v)
236 #define TRACE_EVENT2(cg, n, a1n, a1v, a2n, a2v) TRACE_EMPTY(cg, n, a1n, a1v, a2n, a2v)
237 #define TRACE_EVENT0_ALWAYS(cg, n) TRACE_EMPTY(cg, n)
238 #define TRACE_EVENT1_ALWAYS(cg, n, a1n, a1v) TRACE_EMPTY(cg, n, a1n, a1v)
239 #define TRACE_EVENT2_ALWAYS(cg, n, a1n, a1v, a2n, a2v) TRACE_EMPTY(cg, n, a1n, a1v, a2n, a2v)
240 #define TRACE_EVENT_INSTANT0(cg, n, scope) TRACE_EMPTY(cg, n, scope)
241 #define TRACE_EVENT_INSTANT1(cg, n, scope, a1n, a1v) TRACE_EMPTY(cg, n, scope, a1n, a1v)
242 #define TRACE_EVENT_INSTANT2(cg, n, scope, a1n, a1v, a2n, a2v) \
243 TRACE_EMPTY(cg, n, scope, a1n, a1v, a2n, a2v)
244 #define TRACE_EVENT_INSTANT0_ALWAYS(cg, n, scope) TRACE_EMPTY(cg, n, scope)
245 #define TRACE_EVENT_INSTANT1_ALWAYS(cg, n, scope, a1n, a1v) TRACE_EMPTY(cg, n, scope, a1n, a1v)
246 #define TRACE_EVENT_INSTANT2_ALWAYS(cg, n, scope, a1n, a1v, a2n, a2v) \
247 TRACE_EMPTY(cg, n, scope, a1n, a1v, a2n, a2v)
248 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(cg, n, id) TRACE_EMPTY(cg, n, id)
249 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(cg, n, id, ss) TRACE_EMPTY(cg, n, id, ss)
250 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(cg, n, id) TRACE_EMPTY(cg, n, id)
251 #define TRACE_COUNTER1(cg, n, value) TRACE_EMPTY(cg, n, value)
252 #define TRACE_COUNTER2(cg, n, v1n, v1v, v2n, v2v) TRACE_EMPTY(cg, n, v1n, v1v, v2n, v2v)
253
254 #elif defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
255
256 namespace skia_private {
257 // ATrace can't accept ::perfetto::DynamicString or ::perfetto::StaticString, so any trace event
258 // names that were wrapped in TRACE_STR_COPY or TRACE_STR_STATIC need to be unboxed back to
259 // char* before being passed to ATrace.
260 inline const char* UnboxPerfettoString(const ::perfetto::DynamicString& str) {
261 return str.value;
262 }
263 inline const char* UnboxPerfettoString(const ::perfetto::StaticString& str) {
264 return str.value;
265 }
266 inline const char* UnboxPerfettoString(const char* str) {
267 return str;
268 }
269
270 // WrapTraceArgInStdString serves a similar purpose to UnboxPerfettoString, but also accepts
271 // numeric values that are often used as trace arguments. This necessitates always wrapping the
272 // argument in a new std::string, instead of just passing along/unboxing an existing C-style
273 // string. This comes at a slight cost, and should only be used for trace slice arguments but
274 // not slice names, where UnboxPerfettoString should be used instead.
275 template<typename T>
276 inline std::string WrapTraceArgInStdString(const T numeric) {
277 return std::to_string(numeric);
278 }
279 inline std::string WrapTraceArgInStdString(const ::perfetto::DynamicString& str) {
280 return std::string(str.value);
281 }
282 inline std::string WrapTraceArgInStdString(const ::perfetto::StaticString& str) {
283 return std::string(str.value);
284 }
285 inline std::string WrapTraceArgInStdString(const char* str) {
286 return std::string(str);
287 }
288
289 constexpr bool StrEndsWithAndLongerThan(const char* str, const char* suffix) {
290 auto strView = std::basic_string_view(str);
291 auto suffixView = std::basic_string_view(suffix);
292 // string_view::ends_with isn't available until C++20
293 return strView.size() > suffixView.size() &&
294 strView.compare(strView.size() - suffixView.size(),
295 std::string_view::npos, suffixView) == 0;
296 }
297 }
298
299 // Generate a unique variable name with a given prefix.
300 // The indirection in this multi-level macro lets __LINE__ expand at the right time/place to get
301 // prefix123 instead of prefix__LINE__.
302 #define SK_PERFETTO_INTERNAL_CONCAT2(a, b) a##b
303 #define SK_PERFETTO_INTERNAL_CONCAT(a, b) SK_PERFETTO_INTERNAL_CONCAT2(a, b)
304 #define SK_PERFETTO_UID(prefix) SK_PERFETTO_INTERNAL_CONCAT(prefix, __LINE__)
305
306 // Used by SK_INTERNAL_ATRACE_ARGS_BEGIN and SK_INTERNAL_ATRACE_ARGS_END to select which overloaded
307 // macro to use depending on how many __VA_ARGS__ are present. The number of __VA_ARGS__ piped in
308 // from this macro caller's parent caller determines which macro name given by the caller ends up in
309 // the macro_name slot here. See usage for nuance.
310 #define SK_INTERNAL_GET_ATRACE_ARGS_MACRO(_0, _1a, _1b, _2a, _2b, macro_name, ...) macro_name
311
312 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
313 #define SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_0(name) \
314 atrace_begin_body(::skia_private::UnboxPerfettoString(name));
315
316 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
317 #define SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_1(name, arg1_name, arg1_val) \
318 char SK_PERFETTO_UID(skTraceStrBuf1)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE]; \
319 snprintf(SK_PERFETTO_UID(skTraceStrBuf1), \
320 SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE, \
321 "^(%s: %s)", \
322 ::skia_private::UnboxPerfettoString(arg1_name), \
323 ::skia_private::WrapTraceArgInStdString(arg1_val).c_str()); \
324 atrace_begin_body(::skia_private::UnboxPerfettoString(name)); \
325 atrace_begin_body(SK_PERFETTO_UID(skTraceStrBuf1));
326
327 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
328 #define SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_2( \
329 name, arg1_name, arg1_val, arg2_name, arg2_val, ...) \
330 char SK_PERFETTO_UID(skTraceStrBuf1)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE]; \
331 char SK_PERFETTO_UID(skTraceStrBuf2)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE]; \
332 snprintf(SK_PERFETTO_UID(skTraceStrBuf1), \
333 SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE, \
334 "^(%s: %s)", \
335 ::skia_private::UnboxPerfettoString(arg1_name), \
336 ::skia_private::WrapTraceArgInStdString(arg1_val).c_str()); \
337 snprintf(SK_PERFETTO_UID(skTraceStrBuf2), \
338 SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE, \
339 "^(%s: %s)", \
340 ::skia_private::UnboxPerfettoString(arg2_name), \
341 ::skia_private::WrapTraceArgInStdString(arg2_val).c_str()); \
342 atrace_begin_body(::skia_private::UnboxPerfettoString(name)); \
343 atrace_begin_body(SK_PERFETTO_UID(skTraceStrBuf1)); \
344 atrace_begin_body(SK_PERFETTO_UID(skTraceStrBuf2));
345
346 // Will map to either the 0, 1, or 2 argument variant of this macro, which will trigger an
347 // ATRACE_BEGIN event for the slice name, and one for each argument <name, value> pair. The caller
348 // must ensure each of these 1-3 slices are properly terminated with 1-3 matching ATRACE_END events.
349 //
350 // Note: ATRACE_ENABLED() is checked here to allow the actual implmenting macros to avoid redundant
351 // checks within each of their calls to the standard ATRACE_BEGIN() macro, as checking
352 // ATRACE_ENABLED() can be non-trivial. But more importantly, if tracing isn't enabled then we
353 // should avoid the string formatting work required for how we hack "arguments" into separate ATrace
354 // slices.
355 #define SK_INTERNAL_ATRACE_ARGS_BEGIN(slice_name, ...) \
356 if (CC_UNLIKELY(ATRACE_ENABLED())) { \
357 SK_INTERNAL_GET_ATRACE_ARGS_MACRO(0, \
358 ##__VA_ARGS__, \
359 SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_2, \
360 0, \
361 SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_1, \
362 0, \
363 SK_INTERNAL_ATRACE_ARGS_BEGIN_DANGEROUS_0) \
364 (slice_name, ##__VA_ARGS__); \
365 }
366
367 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
368 #define SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_2(arg1_name, arg1_val, arg2_name, arg2_val, ...) \
369 atrace_end_body(); \
370 atrace_end_body(); \
371 atrace_end_body();
372
373 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
374 #define SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_1(arg1_name, arg1_val) \
375 atrace_end_body(); \
376 atrace_end_body();
377
378 // WARNING: must always be guarded by an outer call to CC_UNLIKELY(ATRACE_ENABLED())
379 #define SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_0() \
380 atrace_end_body();
381
382 // Will map to either the 0, 1, or 2 argument variant of this macro, which will trigger an
383 // ATRACE_END event for the slice name, and one for each argument <name, value> pair. The caller
384 // must ensure each of these 1-3 slices already existed from 1-3 matching ATRACE_BEGIN events.
385 //
386 // Note: ATRACE_ENABLED() is checked here to allow the actual implmenting macros to avoid redundant
387 // checks within each of their calls to the standard ATRACE_END() macro, as checking
388 // ATRACE_ENABLED() can be non-trivial.
389 #define SK_INTERNAL_ATRACE_ARGS_END(...) \
390 if (CC_UNLIKELY(ATRACE_ENABLED())) { \
391 SK_INTERNAL_GET_ATRACE_ARGS_MACRO(0, \
392 ##__VA_ARGS__, \
393 SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_2, \
394 0, \
395 SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_1, \
396 0, \
397 SK_INTERNAL_ATRACE_ARGS_END_DANGEROUS_0) \
398 (__VA_ARGS__); \
399 }
400
401 // Assuming there is an active tracing session, this call will create a trace event if tracing is
402 // enabled (with SkAndroidFrameworkTraceUtil::setEnableTracing(true)) or if force_always_trace is
403 // true. The event goes through ATrace by default, but can be routed to Perfetto instead by calling
404 // SkAndroidFrameworkTraceUtil::setUsePerfettoTrackEvents(true).
405 //
406 // If ATrace is used, then additional sub-events will be created for each trace event argument
407 // <name, value> pair (up to a max of two argument pairs). If Perfetto is used, then any arguments
408 // will be associated with a single event. In either case, trace arguments will only be evaluated if
409 // the event will actually be recorded in the underlying tracing system (i.e. if an applicable
410 // tracing session is active.)
411 //
412 // If force_always_trace = true, then the caller *must* append the ".always" suffix to the provided
413 // category. This allows Perfetto tracing sessions to optionally filter to just the "skia.always"
414 // category tag. This requirement is enforced at compile time.
415 #define TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE(force_always_trace, category, name, ...) \
416 struct SK_PERFETTO_UID(ScopedEvent) { \
417 struct EventFinalizer { \
418 /* The ... parameter slot is an implementation detail. It allows the */ \
419 /* anonymous struct to use aggregate initialization to invoke the */ \
420 /* lambda (which emits the BEGIN event and returns an integer) */ \
421 /* with the proper reference capture for any */ \
422 /* TrackEventArgumentFunction in |__VA_ARGS__|. This is required so */ \
423 /* that the scoped event is exactly ONE line and can't escape the */ \
424 /* scope if used in a single line if statement. */ \
425 EventFinalizer(...) {} \
426 ~EventFinalizer() { \
427 if (force_always_trace || \
428 CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \
429 if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) { \
430 TRACE_EVENT_END(category); \
431 } else { \
432 SK_INTERNAL_ATRACE_ARGS_END(__VA_ARGS__); \
433 } \
434 } \
435 } \
436 \
437 EventFinalizer(const EventFinalizer&) = delete; \
438 EventFinalizer& operator=(const EventFinalizer&) = delete; \
439 \
440 EventFinalizer(EventFinalizer&&) = default; \
441 EventFinalizer& operator=(EventFinalizer&&) = delete; \
442 } finalizer; \
443 } SK_PERFETTO_UID(scoped_event) { \
444 [&]() { \
445 static_assert(!force_always_trace || \
446 ::skia_private::StrEndsWithAndLongerThan(category, ".always"), \
447 "[force_always_trace == true] requires [category] to end in '.always'"); \
448 if (force_always_trace || \
449 CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \
450 if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) { \
451 TRACE_EVENT_BEGIN(category, name, ##__VA_ARGS__); \
452 } else { \
453 SK_INTERNAL_ATRACE_ARGS_BEGIN(name, ##__VA_ARGS__); \
454 } \
455 } \
456 return 0; \
457 }() \
458 }
459
460 // Records an event with the current tracing backend if overall tracing is enabled, and Skia's
461 // "broad" tracing is enabled with SkAndroidFrameworkTraceUtil::setEnableTracing(true).
462 #define TRACE_EVENT_ATRACE_OR_PERFETTO(category, name, ...) \
463 TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
464 /* force_always_trace = */ false, category, name, ##__VA_ARGS__)
465
466 // Traces a formatted string if overall tracing is enabled, and Skia's "broad" tracing is enabled
467 // with SkAndroidFrameworkTraceUtil::setEnableTracing(true).
468 // No-op outside of Android framework builds.
469 // WARNING: this macro expands to a multi-line statement, and must not be used in a single line
470 // control statement!
471 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) \
472 char SK_PERFETTO_UID(skTraceStrBuf)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE]; \
473 if (SkAndroidFrameworkTraceUtil::getEnableTracing()) { \
474 snprintf(SK_PERFETTO_UID(skTraceStrBuf), SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE, \
475 fmt, ##__VA_ARGS__); \
476 } \
477 TRACE_EVENT0("skia.android", TRACE_STR_COPY(SK_PERFETTO_UID(skTraceStrBuf)))
478
479 // Traces a formatted string as long as overall tracing is enabled, even if Skia's "broad" tracing
480 // is disabled.
481 // No-op outside of Android framework builds.
482 // WARNING: this macro expands to a multi-line statement, and must not be used in a single line
483 // control statement!
484 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) \
485 char SK_PERFETTO_UID(skTraceStrBuf)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE]; \
486 snprintf(SK_PERFETTO_UID(skTraceStrBuf), SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE, \
487 fmt, ##__VA_ARGS__); \
488 TRACE_EVENT0_ALWAYS("skia.android", TRACE_STR_COPY(SK_PERFETTO_UID(skTraceStrBuf)))
489
490 // Records a pair of begin and end events called "name" (with 0-2 associated arguments) for the
491 // current scope as long as overall tracing is enabled, and Skia's "broad" tracing is enabled with
492 // SkAndroidFrameworkTraceUtil::setEnableTracing(true).
493 // Note that ATrace does not natively support trace arguments, so arguments are recorded as separate
494 // sub-events when ATrace is set as the current tracing backend. The Perfetto tracing backend
495 // associates any arguments with a single event / slice.
496 #define TRACE_EVENT0(category_group, name) \
497 TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name)
498 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
499 TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val)
500 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
501 TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
502
503 // Records a pair of begin and end events called "name" (with 0-2 associated arguments) for the
504 // current scope as long as overall tracing is enabled, even if Skia's "broad" tracing is disabled.
505 // Note that ATrace does not natively support trace arguments, so arguments are recorded as separate
506 // sub-events when ATrace is set as the current tracing backend. The Perfetto tracing backend
507 // associates any arguments with a single event / slice.
508 // Note: the ".always" suffix is appended to category_group in _ALWAYS trace event macro variants.
509 #define TRACE_EVENT0_ALWAYS(category_group, name) \
510 TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
511 /* force_always_trace = */ true, category_group ".always", name)
512 #define TRACE_EVENT1_ALWAYS(category_group, name, arg1_name, arg1_val) \
513 TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
514 /* force_always_trace = */ true, category_group ".always", name, arg1_name, arg1_val)
515 #define TRACE_EVENT2_ALWAYS(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
516 TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE(/* force_always_trace = */ true, \
517 category_group ".always", \
518 name, \
519 arg1_name, \
520 arg1_val, \
521 arg2_name, \
522 arg2_val)
523
524 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments.
525 // Note that ATrace does not support trace arguments, so they are only recorded when Perfetto is set
526 // as the current tracing backend.
527 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
528 do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name); } while(0)
529
530 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
531 do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val); } while(0)
532
533 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
534 arg2_name, arg2_val) \
535 do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val, \
536 arg2_name, arg2_val); } while(0)
537
538 // As above but appends the ".always" suffix
539 #define TRACE_EVENT_INSTANT0_ALWAYS(category_group, name, scope) \
540 do { TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
541 /* force_always_trace = */ true, category_group ".always", name); } while(0)
542
543 #define TRACE_EVENT_INSTANT1_ALWAYS(category_group, name, scope, arg1_name, arg1_val) \
544 do { TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
545 /* force_always_trace = */ true, category_group ".always", name, arg1_name, arg1_val); \
546 } while(0)
547
548 #define TRACE_EVENT_INSTANT2_ALWAYS(category_group, name, scope, arg1_name, arg1_val, \
549 arg2_name, arg2_val) \
550 do { TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE(/* force_always_trace = */ true, \
551 category_group, \
552 name, \
553 arg1_name, \
554 arg1_val, \
555 arg2_name, \
556 arg2_val); \
557 } while(0)
558
559 // Records the value of a counter called "name" immediately. Value
560 // must be representable as a 32 bit integer.
561 #define TRACE_COUNTER1(category_group, name, value) \
562 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \
563 if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) { \
564 TRACE_COUNTER(category_group, name, value); \
565 } else { \
566 ATRACE_INT(name, value); \
567 } \
568 }
569
570 // Records the values of a multi-parted counter called "name" immediately.
571 // In Chrome, this macro produces a stacked bar chart. Perfetto doesn't support
572 // that (related: b/242349575), so this just produces two separate counters.
573 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val) \
574 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \
575 if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) { \
576 TRACE_COUNTER(category_group, name "-" value1_name, value1_val); \
577 TRACE_COUNTER(category_group, name "-" value2_name, value2_val); \
578 } else { \
579 ATRACE_INT(name "-" value1_name, value1_val); \
580 ATRACE_INT(name "-" value2_name, value2_val); \
581 } \
582 }
583
584 // ATrace has no object tracking, and would require a legacy shim for Perfetto (which likely no-ops
585 // here). Further, these don't appear to currently be used outside of tests.
586 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
587 TRACE_EMPTY(category_group, name, id)
588 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
589 TRACE_EMPTY(category_group, name, id, snapshot)
590 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
591 TRACE_EMPTY(category_group, name, id)
592
593 // Macro to efficiently determine if a given category group is enabled. Only works with Perfetto.
594 // This is only used for some shader text logging that isn't supported in ATrace anyway.
595 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
596 if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing() && \
597 SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents)) { \
598 *ret = TRACE_EVENT_CATEGORY_ENABLED(category_group); \
599 } else { \
600 *ret = false; \
601 }
602
603 #else // Route through SkEventTracer (!SK_DISABLE_TRACING && !SK_ANDROID_FRAMEWORK_USE_PERFETTO)
604
605 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
606 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
607
608 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2
609 // associated arguments. If the category is not enabled, then this does nothing.
610 #define TRACE_EVENT0(category_group, name) \
611 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
612
613 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
614 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
615
616 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
617 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
618
619 #define TRACE_EVENT0_ALWAYS(category_group, name) \
620 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
621
622 #define TRACE_EVENT1_ALWAYS(category_group, name, arg1_name, arg1_val) \
623 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
624
625 #define TRACE_EVENT2_ALWAYS(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
626 INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
627
628 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the
629 // category is not enabled, then this does nothing.
630 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
631 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
632 TRACE_EVENT_FLAG_NONE | scope)
633
634 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
635 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
636 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val)
637
638 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
639 arg2_name, arg2_val) \
640 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
641 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \
642 arg2_name, arg2_val)
643
644 #define TRACE_EVENT_INSTANT0_ALWAYS(category_group, name, scope) \
645 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
646 TRACE_EVENT_FLAG_NONE | scope)
647
648 #define TRACE_EVENT_INSTANT1_ALWAYS(category_group, name, scope, arg1_name, arg1_val) \
649 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
650 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val)
651
652 #define TRACE_EVENT_INSTANT2_ALWAYS(category_group, name, scope, arg1_name, arg1_val, \
653 arg2_name, arg2_val) \
654 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
655 TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \
656 arg2_name, arg2_val)
657
658 // Records the value of a counter called "name" immediately. Value
659 // must be representable as a 32 bit integer.
660 #define TRACE_COUNTER1(category_group, name, value) \
661 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
662 TRACE_EVENT_FLAG_NONE, "value", \
663 static_cast<int>(value))
664
665 // Records the values of a multi-parted counter called "name" immediately.
666 // The UI will treat value1 and value2 as parts of a whole, displaying their
667 // values as a stacked-bar chart.
668 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
669 value2_name, value2_val) \
670 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
671 TRACE_EVENT_FLAG_NONE, value1_name, \
672 static_cast<int>(value1_val), value2_name, \
673 static_cast<int>(value2_val))
674
675 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
676 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
677 TRACE_EVENT_PHASE_ASYNC_BEGIN, category, name, id, TRACE_EVENT_FLAG_NONE)
678 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
679 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
680 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
681 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \
682 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
683 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
684
685 #define TRACE_EVENT_ASYNC_END0(category, name, id) \
686 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
687 category, name, id, TRACE_EVENT_FLAG_NONE)
688 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
689 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
690 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
691 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \
692 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
693 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
694
695 // Macros to track the life time and value of arbitrary client objects.
696 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
697 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
698 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name, id, \
699 TRACE_EVENT_FLAG_NONE)
700
701 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \
702 snapshot) \
703 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
704 TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name, \
705 id, TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
706
707 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
708 INTERNAL_TRACE_EVENT_ADD_WITH_ID( \
709 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name, id, \
710 TRACE_EVENT_FLAG_NONE)
711
712 // Macro to efficiently determine if a given category group is enabled.
713 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
714 do { \
715 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
716 if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
717 *ret = true; \
718 } else { \
719 *ret = false; \
720 } \
721 } while (0)
722
723 #endif
724
725 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
726 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0))
727 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0))
728 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1))
729 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2))
730 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3))
731 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4))
732 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5))
733 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6))
734 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7))
735 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8))
736 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9))
737 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
738
739 #define TRACE_EVENT_FLAG_SCOPE_MASK \
740 (static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \
741 TRACE_EVENT_FLAG_SCOPE_EXTRA))
742
743 // Type values for identifying types in the TraceValue union.
744 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
745 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
746 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
747 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
748 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
749 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
750 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
751 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
752
753 // Enum reflecting the scope of an INSTANT event. Must fit within TRACE_EVENT_FLAG_SCOPE_MASK.
754 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
755 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
756 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
757
758 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
759 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
760 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
761
762 #endif // SkTraceEventCommon_DEFINED