• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EVENT0_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_EVENT0_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 #define SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE 256
155 
156 class SkAndroidFrameworkTraceUtil {
157 public:
158     SkAndroidFrameworkTraceUtil() = delete;
159 
160     // Controls whether broad tracing is enabled. Warning: not thread-safe!
161     //
162     // Some key trace events may still be recorded when this is disabled, if a relevant tracing
163     // session is active.
164     //
165     // ATrace is used by default, but can be replaced with Perfetto by calling
166     // setUsePerfettoTrackEvents(true)
setEnableTracing(bool enableAndroidTracing)167     static void setEnableTracing(bool enableAndroidTracing) {
168         gEnableAndroidTracing = enableAndroidTracing;
169     }
170 
171     // Controls whether tracing uses Perfetto instead of ATrace. Warning: not thread-safe!
172     //
173     // Returns true if Skia was built with Perfetto, false otherwise.
setUsePerfettoTrackEvents(bool usePerfettoTrackEvents)174     static bool setUsePerfettoTrackEvents(bool usePerfettoTrackEvents) {
175 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
176         // Ensure Perfetto is initialized if it wasn't already the preferred tracing backend.
177         if (!gUsePerfettoTrackEvents && usePerfettoTrackEvents) {
178             initPerfetto();
179         }
180         gUsePerfettoTrackEvents = usePerfettoTrackEvents;
181         return true;
182 #else // !SK_ANDROID_FRAMEWORK_USE_PERFETTO
183         // Note: please reach out to skia-android@google.com if you encounter this unexpectedly.
184         SkDebugf("Tracing Skia with Perfetto is not supported in this environment (host build?)");
185         return false;
186 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
187     }
188 
getEnableTracing()189     static bool getEnableTracing() {
190         return gEnableAndroidTracing;
191     }
192 
getUsePerfettoTrackEvents()193     static bool getUsePerfettoTrackEvents() {
194         return gUsePerfettoTrackEvents;
195     }
196 
197 private:
198     static bool gEnableAndroidTracing;
199     static bool gUsePerfettoTrackEvents;
200 
201 #ifdef SK_ANDROID_FRAMEWORK_USE_PERFETTO
202     // Initializes tracing systems, and establishes a connection to the 'traced' daemon.
203     //
204     // Can be called multiple times.
initPerfetto()205     static void initPerfetto() {
206         ::perfetto::TracingInitArgs perfettoArgs;
207         perfettoArgs.backends |= perfetto::kSystemBackend;
208         ::perfetto::Tracing::Initialize(perfettoArgs);
209         ::skia::TrackEvent::Register();
210     }
211 #endif // SK_ANDROID_FRAMEWORK_USE_PERFETTO
212 };
213 #endif // SK_BUILD_FOR_ANDROID_FRAMEWORK
214 
215 #ifdef SK_DEBUG
216 static void skprintf_like_noop(const char format[], ...) SK_PRINTF_LIKE(1, 2);
skprintf_like_noop(const char format[],...)217 static inline void skprintf_like_noop(const char format[], ...) {}
sk_noop(...)218 static inline void sk_noop(...) {}
219 #define TRACE_EMPTY(...) do { sk_noop(__VA_ARGS__); } while (0)
220 #define TRACE_EMPTY_FMT(fmt, ...) do { skprintf_like_noop(fmt, ##__VA_ARGS__); } while (0)
221 #else
222 #define TRACE_EMPTY(...) do {} while (0)
223 #define TRACE_EMPTY_FMT(fmt, ...) do {} while (0)
224 #endif
225 
226 #if defined(SK_DISABLE_TRACING) || \
227         (defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) && !defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO))
228 
229     #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
230     #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
231     #define TRACE_EVENT0(cg, n) TRACE_EMPTY(cg, n)
232     #define TRACE_EVENT0_ALWAYS(cg, n) TRACE_EMPTY(cg, n)
233     #define TRACE_EVENT1(cg, n, a1n, a1v) TRACE_EMPTY(cg, n, a1n, a1v)
234     #define TRACE_EVENT2(cg, n, a1n, a1v, a2n, a2v) TRACE_EMPTY(cg, n, a1n, a1v, a2n, a2v)
235     #define TRACE_EVENT_INSTANT0(cg, n, scope) TRACE_EMPTY(cg, n, scope)
236     #define TRACE_EVENT_INSTANT1(cg, n, scope, a1n, a1v) TRACE_EMPTY(cg, n, scope, a1n, a1v)
237     #define TRACE_EVENT_INSTANT2(cg, n, scope, a1n, a1v, a2n, a2v)  \
238         TRACE_EMPTY(cg, n, scope, a1n, a1v, a2n, a2v)
239     #define TRACE_COUNTER1(cg, n, value) TRACE_EMPTY(cg, n, value)
240     #define TRACE_COUNTER2(cg, n, v1n, v1v, v2n, v2v) TRACE_EMPTY(cg, n, v1n, v1v, v2n, v2v)
241 
242 #elif defined(SK_ANDROID_FRAMEWORK_USE_PERFETTO)
243 
244 namespace skia_private {
245     // ATrace can't accept ::perfetto::DynamicString or ::perfetto::StaticString, so any trace event
246     // names that were wrapped in TRACE_STR_COPY or TRACE_STR_STATIC need to be unboxed back to
247     // char* before being passed to ATrace.
UnboxPerfettoString(const::perfetto::DynamicString & str)248     inline const char* UnboxPerfettoString(const ::perfetto::DynamicString& str) {
249         return str.value;
250     }
UnboxPerfettoString(const::perfetto::StaticString & str)251     inline const char* UnboxPerfettoString(const ::perfetto::StaticString& str) {
252         return str.value;
253     }
UnboxPerfettoString(const char * str)254     inline const char* UnboxPerfettoString(const char* str) {
255         return str;
256     }
257 
StrEndsWithAndLongerThan(const char * str,const char * suffix)258     constexpr bool StrEndsWithAndLongerThan(const char* str, const char* suffix) {
259         auto strView = std::basic_string_view(str);
260         auto suffixView = std::basic_string_view(suffix);
261         // string_view::ends_with isn't available until C++20
262         return strView.size() > suffixView.size() &&
263                 strView.compare(strView.size() - suffixView.size(),
264                                 std::string_view::npos, suffixView) == 0;
265     }
266 }
267 
268 // Generate a unique variable name with a given prefix.
269 // The indirection in this multi-level macro lets __LINE__ expand at the right time/place to get
270 // prefix123 instead of prefix__LINE__.
271 #define SK_PERFETTO_INTERNAL_CONCAT2(a, b) a##b
272 #define SK_PERFETTO_INTERNAL_CONCAT(a, b) SK_PERFETTO_INTERNAL_CONCAT2(a, b)
273 #define SK_PERFETTO_UID(prefix) SK_PERFETTO_INTERNAL_CONCAT(prefix, __LINE__)
274 
275 // Assuming there is an active tracing session, this call will create a trace event if tracing is
276 // enabled (with SkAndroidFrameworkTraceUtil::setEnableTracing(true)) or if force_always_trace is
277 // true. The event goes through ATrace by default, but can be routed to Perfetto instead by calling
278 // SkAndroidFrameworkTraceUtil::setUsePerfettoTrackEvents(true).
279 //
280 // If force_always_trace = true, then the caller *must* append the ".always" suffix to the provided
281 // category. This allows Perfetto tracing sessions to optionally filter to just the "skia.always"
282 // category tag. This requirement is enforced at compile time.
283 #define TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE(force_always_trace, category, name, ...)       \
284     struct SK_PERFETTO_UID(ScopedEvent) {                                                       \
285         struct EventFinalizer {                                                                 \
286             /* The ... parameter slot is an implementation detail. It allows the */             \
287             /* anonymous struct to use aggregate initialization to invoke the    */             \
288             /* lambda (which emits the BEGIN event and returns an integer)       */             \
289             /* with the proper reference capture for any                         */             \
290             /* TrackEventArgumentFunction in |__VA_ARGS__|. This is required so  */             \
291             /* that the scoped event is exactly ONE line and can't escape the    */             \
292             /* scope if used in a single line if statement.                      */             \
293             EventFinalizer(...) {}                                                              \
294             ~EventFinalizer() {                                                                 \
295                 if (force_always_trace ||                                                       \
296                         CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) {         \
297                     if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) {             \
298                         TRACE_EVENT_END(category);                                              \
299                     } else {                                                                    \
300                         ATRACE_END();                                                           \
301                     }                                                                           \
302                 }                                                                               \
303             }                                                                                   \
304                                                                                                 \
305             EventFinalizer(const EventFinalizer&) = delete;                                     \
306             EventFinalizer& operator=(const EventFinalizer&) = delete;                          \
307                                                                                                 \
308             EventFinalizer(EventFinalizer&&) = default;                                         \
309             EventFinalizer& operator=(EventFinalizer&&) = delete;                               \
310         } finalizer;                                                                            \
311     } SK_PERFETTO_UID(scoped_event) {                                                           \
312         [&]() {                                                                                 \
313             static_assert(!force_always_trace ||                                                \
314                         ::skia_private::StrEndsWithAndLongerThan(category, ".always"),         \
315                     "[force_always_trace == true] requires [category] to end in '.always'");    \
316             if (force_always_trace ||                                                           \
317                     CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) {             \
318                 if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) {                 \
319                     TRACE_EVENT_BEGIN(category, name, ##__VA_ARGS__);                           \
320                 } else {                                                                        \
321                     ATRACE_BEGIN(::skia_private::UnboxPerfettoString(name));                   \
322                 }                                                                               \
323             }                                                                                   \
324             return 0;                                                                           \
325         }()                                                                                     \
326     }
327 
328 // Records an event with the current tracing backend, if overall Skia tracing is also enabled.
329 #define TRACE_EVENT_ATRACE_OR_PERFETTO(category, name, ...)                     \
330     TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE(                                   \
331             /* force_always_trace = */ false, category, name, ##__VA_ARGS__)
332 
333 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...)                                                  \
334     char SK_PERFETTO_UID(skTraceStrBuf)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE];           \
335     if (SkAndroidFrameworkTraceUtil::getEnableTracing()) {                                  \
336         snprintf(SK_PERFETTO_UID(skTraceStrBuf), SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE,   \
337                  fmt, ##__VA_ARGS__);                                                       \
338     }                                                                                       \
339     TRACE_EVENT0("skia.android", TRACE_STR_COPY(SK_PERFETTO_UID(skTraceStrBuf)))
340 
341 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...)                                           \
342     char SK_PERFETTO_UID(skTraceStrBuf)[SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE];           \
343     snprintf(SK_PERFETTO_UID(skTraceStrBuf), SK_ANDROID_FRAMEWORK_ATRACE_BUFFER_SIZE,       \
344              fmt, ##__VA_ARGS__);                                                           \
345     TRACE_EVENT0_ALWAYS("skia.android", TRACE_STR_COPY(SK_PERFETTO_UID(skTraceStrBuf)))
346 
347 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2
348 // associated arguments. Note that ATrace does not support trace arguments, so they are only
349 // recorded when Perfetto is set as the current tracing backend.
350 #define TRACE_EVENT0(category_group, name) \
351     TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name)
352 // Note: ".always" suffix appended to category_group in TRACE_EVENT0_ALWAYS.
353 #define TRACE_EVENT0_ALWAYS(category_group, name) TRACE_EVENT_ATRACE_OR_PERFETTO_FORCEABLE( \
354         /* force_always_trace = */ true, category_group ".always", name)
355 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
356     TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val)
357 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
358     TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
359 
360 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments.
361 // Note that ATrace does not support trace arguments, so they are only recorded when Perfetto is set
362 // as the current tracing backend.
363 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
364     do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name); } while(0)
365 
366 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
367     do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val); } while(0)
368 
369 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val,      \
370                              arg2_name, arg2_val)                                   \
371     do { TRACE_EVENT_ATRACE_OR_PERFETTO(category_group, name, arg1_name, arg1_val,  \
372                                         arg2_name, arg2_val); } while(0)
373 
374 // Records the value of a counter called "name" immediately. Value
375 // must be representable as a 32 bit integer.
376 #define TRACE_COUNTER1(category_group, name, value)                     \
377     if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) { \
378         if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) { \
379             TRACE_COUNTER(category_group, name, value);                 \
380         } else {                                                        \
381             ATRACE_INT(name, value);                                    \
382         }                                                               \
383     }
384 
385 // Records the values of a multi-parted counter called "name" immediately.
386 // In Chrome, this macro produces a stacked bar chart. Perfetto doesn't support
387 // that (related: b/242349575), so this just produces two separate counters.
388 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, value2_name, value2_val)  \
389     if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing())) {                         \
390         if (SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents()) {                         \
391             TRACE_COUNTER(category_group, name "-" value1_name, value1_val);                    \
392             TRACE_COUNTER(category_group, name "-" value2_name, value2_val);                    \
393         } else {                                                                                \
394             ATRACE_INT(name "-" value1_name, value1_val);                                       \
395             ATRACE_INT(name "-" value2_name, value2_val);                                       \
396         }                                                                                       \
397     }
398 
399 // ATrace has no object tracking, and would require a legacy shim for Perfetto (which likely no-ops
400 // here). Further, these don't appear to currently be used outside of tests.
401 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
402     TRACE_EMPTY(category_group, name, id)
403 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
404     TRACE_EMPTY(category_group, name, id, snapshot)
405 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
406     TRACE_EMPTY(category_group, name, id)
407 
408 // Macro to efficiently determine if a given category group is enabled. Only works with Perfetto.
409 // This is only used for some shader text logging that isn't supported in ATrace anyway.
410 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret)                     \
411     if (CC_UNLIKELY(SkAndroidFrameworkTraceUtil::getEnableTracing() &&              \
412                     SkAndroidFrameworkTraceUtil::getUsePerfettoTrackEvents)) {      \
413         *ret = TRACE_EVENT_CATEGORY_ENABLED(category_group);                        \
414     } else {                                                                        \
415         *ret = false;                                                               \
416     }
417 
418 #else // Route through SkEventTracer (!SK_DISABLE_TRACING && !SK_ANDROID_FRAMEWORK_USE_PERFETTO)
419 
420 #define ATRACE_ANDROID_FRAMEWORK(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
421 #define ATRACE_ANDROID_FRAMEWORK_ALWAYS(fmt, ...) TRACE_EMPTY_FMT(fmt, ##__VA_ARGS__)
422 
423 // Records a pair of begin and end events called "name" for the current scope, with 0, 1 or 2
424 // associated arguments. If the category is not enabled, then this does nothing.
425 #define TRACE_EVENT0(category_group, name) \
426   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
427 
428 #define TRACE_EVENT0_ALWAYS(category_group, name) \
429   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
430 
431 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
432   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
433 
434 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
435   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
436 
437 // Records a single event called "name" immediately, with 0, 1 or 2 associated arguments. If the
438 // category is not enabled, then this does nothing.
439 #define TRACE_EVENT_INSTANT0(category_group, name, scope)                   \
440   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
441                            TRACE_EVENT_FLAG_NONE | scope)
442 
443 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
444   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name,    \
445                            TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val)
446 
447 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
448                              arg2_name, arg2_val)                              \
449   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name,    \
450                            TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \
451                            arg2_name, arg2_val)
452 
453 // Records the value of a counter called "name" immediately. Value
454 // must be representable as a 32 bit integer.
455 #define TRACE_COUNTER1(category_group, name, value)                         \
456   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
457                            TRACE_EVENT_FLAG_NONE, "value",                  \
458                            static_cast<int>(value))
459 
460 // Records the values of a multi-parted counter called "name" immediately.
461 // The UI will treat value1 and value2 as parts of a whole, displaying their
462 // values as a stacked-bar chart.
463 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val,       \
464                        value2_name, value2_val)                             \
465   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
466                            TRACE_EVENT_FLAG_NONE, value1_name,              \
467                            static_cast<int>(value1_val), value2_name,       \
468                            static_cast<int>(value2_val))
469 
470 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id)                                           \
471     INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                                          \
472         TRACE_EVENT_PHASE_ASYNC_BEGIN, category, name, id, TRACE_EVENT_FLAG_NONE)
473 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val)                      \
474     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN,                            \
475         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
476 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \
477     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN,                            \
478         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
479 
480 #define TRACE_EVENT_ASYNC_END0(category, name, id)                                           \
481     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END,                            \
482         category, name, id, TRACE_EVENT_FLAG_NONE)
483 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val)                      \
484     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END,                            \
485         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
486 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, arg2_name, arg2_val) \
487     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END,                            \
488         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
489 
490 // Macros to track the life time and value of arbitrary client objects.
491 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
492   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
493       TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name, id,     \
494       TRACE_EVENT_FLAG_NONE)
495 
496 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \
497                                             snapshot)                 \
498   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                   \
499       TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name,        \
500       id, TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
501 
502 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
503   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
504       TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name, id,     \
505       TRACE_EVENT_FLAG_NONE)
506 
507 // Macro to efficiently determine if a given category group is enabled.
508 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret)             \
509   do {                                                                      \
510     INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group);                 \
511     if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
512       *ret = true;                                                          \
513     } else {                                                                \
514       *ret = false;                                                         \
515     }                                                                       \
516   } while (0)
517 
518 #endif
519 
520 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
521 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0))
522 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0))
523 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1))
524 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2))
525 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3))
526 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4))
527 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5))
528 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6))
529 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7))
530 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8))
531 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9))
532 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
533 
534 #define TRACE_EVENT_FLAG_SCOPE_MASK                          \
535   (static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \
536                              TRACE_EVENT_FLAG_SCOPE_EXTRA))
537 
538 // Type values for identifying types in the TraceValue union.
539 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
540 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
541 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
542 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
543 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
544 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
545 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
546 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
547 
548 // Enum reflecting the scope of an INSTANT event. Must fit within TRACE_EVENT_FLAG_SCOPE_MASK.
549 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
550 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
551 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
552 
553 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
554 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
555 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
556 
557 #endif  // SkTraceEventCommon_DEFINED
558