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 // This header file defines the set of trace_event macros without specifying
8 // how the events actually get collected and stored. If you need to expose trace
9 // events to some other universe, you can copy-and-paste this file as well as
10 // trace_event.h, modifying the macros contained there as necessary for the
11 // target platform. The end result is that multiple libraries can funnel events
12 // through to a shared trace event collector.
13 
14 // IMPORTANT: To avoid conflicts, if you need to modify this file for a library,
15 // land your change in base/ first, and then copy-and-paste it.
16 
17 // Trace events are for tracking application performance and resource usage.
18 // Macros are provided to track:
19 //    Begin and end of function calls
20 //    Counters
21 //
22 // Events are issued against categories. Whereas LOG's
23 // categories are statically defined, TRACE categories are created
24 // implicitly with a string. For example:
25 //   TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent",
26 //                        TRACE_EVENT_SCOPE_THREAD)
27 //
28 // It is often the case that one trace may belong in multiple categories at the
29 // same time. The first argument to the trace can be a comma-separated list of
30 // categories, forming a category group, like:
31 //
32 // TRACE_EVENT_INSTANT0("input,views", "OnMouseOver", TRACE_EVENT_SCOPE_THREAD)
33 //
34 // We can enable/disable tracing of OnMouseOver by enabling/disabling either
35 // category.
36 //
37 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
38 //   TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
39 //   doSomethingCostly()
40 //   TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
41 // Note: our tools can't always determine the correct BEGIN/END pairs unless
42 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you
43 // need them to be in separate scopes.
44 //
45 // A common use case is to trace entire function scopes. This
46 // issues a trace BEGIN and END automatically:
47 //   void doSomethingCostly() {
48 //     TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
49 //     ...
50 //   }
51 //
52 // Additional parameters can be associated with an event:
53 //   void doSomethingCostly2(int howMuch) {
54 //     TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
55 //         "howMuch", howMuch);
56 //     ...
57 //   }
58 //
59 // The trace system will automatically add to this information the
60 // current process id, thread id, and a timestamp in microseconds.
61 //
62 // To trace an asynchronous procedure such as an IPC send/receive, use
63 // ASYNC_BEGIN and ASYNC_END:
64 //   [single threaded sender code]
65 //     static int send_count = 0;
66 //     ++send_count;
67 //     TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
68 //     Send(new MyMessage(send_count));
69 //   [receive code]
70 //     void OnMyMessage(send_count) {
71 //       TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
72 //     }
73 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
74 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process.
75 // Pointers can be used for the ID parameter, and they will be mangled
76 // internally so that the same pointer on two different processes will not
77 // match. For example:
78 //   class MyTracedClass {
79 //    public:
80 //     MyTracedClass() {
81 //       TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
82 //     }
83 //     ~MyTracedClass() {
84 //       TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
85 //     }
86 //   }
87 //
88 // Trace event also supports counters, which is a way to track a quantity
89 // as it varies over time. Counters are created with the following macro:
90 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
91 //
92 // Counters are process-specific. The macro itself can be issued from any
93 // thread, however.
94 //
95 // Sometimes, you want to track two counters at once. You can do this with two
96 // counter macros:
97 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
98 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
99 // Or you can do it with a combined macro:
100 //   TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
101 //       "bytesPinned", g_myCounterValue[0],
102 //       "bytesAllocated", g_myCounterValue[1]);
103 // This indicates to the tracing UI that these counters should be displayed
104 // in a single graph, as a summed area chart.
105 //
106 // Since counters are in a global namespace, you may want to disambiguate with a
107 // unique ID, by using the TRACE_COUNTER_ID* variations.
108 //
109 // By default, trace collection is compiled in, but turned off at runtime.
110 // Collecting trace data is the responsibility of the embedding
111 // application. In Chrome's case, navigating to about:tracing will turn on
112 // tracing and display data collected across all active processes.
113 //
114 //
115 // Memory scoping note:
116 // Tracing copies the pointers, not the string content, of the strings passed
117 // in for category_group, name, and arg_names.  Thus, the following code will
118 // cause problems:
119 //     char* str = strdup("importantName");
120 //     TRACE_EVENT_INSTANT0("SUBSYSTEM", str);  // BAD!
121 //     free(str);                   // Trace system now has dangling pointer
122 //
123 // To avoid this issue with the |name| and |arg_name| parameters, use the
124 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
125 // Notes: The category must always be in a long-lived char* (i.e. static const).
126 //        The |arg_values|, when used, are always deep copied with the _COPY
127 //        macros.
128 //
129 // When are string argument values copied:
130 // const char* arg_values are only referenced by default:
131 //     TRACE_EVENT1("category", "name",
132 //                  "arg1", "literal string is only referenced");
133 // Use TRACE_STR_COPY to force copying of a const char*:
134 //     TRACE_EVENT1("category", "name",
135 //                  "arg1", TRACE_STR_COPY("string will be copied"));
136 // std::string arg_values are always copied:
137 //     TRACE_EVENT1("category", "name",
138 //                  "arg1", std::string("string will be copied"));
139 //
140 //
141 // Convertable notes:
142 // Converting a large data type to a string can be costly. To help with this,
143 // the trace framework provides an interface ConvertableToTraceFormat. If you
144 // inherit from it and implement the AppendAsTraceFormat method the trace
145 // framework will call back to your object to convert a trace output time. This
146 // means, if the category for the event is disabled, the conversion will not
147 // happen.
148 //
149 //   class MyData : public base::trace_event::ConvertableToTraceFormat {
150 //    public:
151 //     MyData() {}
152 //     void AppendAsTraceFormat(std::string* out) const override {
153 //       out->append("{\"foo\":1}");
154 //     }
155 //    private:
156 //     ~MyData() override {}
157 //     DISALLOW_COPY_AND_ASSIGN(MyData);
158 //   };
159 //
160 //   TRACE_EVENT1("foo", "bar", "data",
161 //                scoped_refptr<ConvertableToTraceFormat>(new MyData()));
162 //
163 // The trace framework will take ownership if the passed pointer and it will
164 // be free'd when the trace buffer is flushed.
165 //
166 // Note, we only do the conversion when the buffer is flushed, so the provided
167 // data object should not be modified after it's passed to the trace framework.
168 //
169 //
170 // Thread Safety:
171 // A thread safe singleton and mutex are used for thread safety. Category
172 // enabled flags are used to limit the performance impact when the system
173 // is not enabled.
174 //
175 // TRACE_EVENT macros first cache a pointer to a category. The categories are
176 // statically allocated and safe at all times, even after exit. Fetching a
177 // category is protected by the TraceLog::lock_. Multiple threads initializing
178 // the static variable is safe, as they will be serialized by the lock and
179 // multiple calls will return the same pointer to the category.
180 //
181 // Then the category_group_enabled flag is checked. This is a unsigned char, and
182 // not intended to be multithread safe. It optimizes access to AddTraceEvent
183 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
184 // cause some threads to incorrectly call or skip calling AddTraceEvent near
185 // the time of the system being enabled or disabled. This is acceptable as
186 // we tolerate some data loss while the system is being enabled/disabled and
187 // because AddTraceEvent is threadsafe internally and checks the enabled state
188 // again under lock.
189 //
190 // Without the use of these static category pointers and enabled flags all
191 // trace points would carry a significant performance cost of acquiring a lock
192 // and resolving the category.
193 
194 #if defined(TRACE_EVENT0)
195 #error "Another copy of this file has already been included."
196 #endif
197 
198 // This will mark the trace event as disabled by default. The user will need
199 // to explicitly enable the event.
200 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
201 
202 // Records a pair of begin and end events called "name" for the current
203 // scope, with 0, 1 or 2 associated arguments. If the category is not
204 // enabled, then this does nothing.
205 // - category and name strings must have application lifetime (statics or
206 //   literals). They may not include " chars.
207 #define TRACE_EVENT0(category_group, name)    \
208   INTERNAL_TRACE_MEMORY(category_group, name) \
209   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
210 #define TRACE_EVENT_WITH_FLOW0(category_group, name, bind_id, flow_flags)  \
211   INTERNAL_TRACE_MEMORY(category_group, name)                              \
212   INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, bind_id, \
213                                             flow_flags)
214 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
215   INTERNAL_TRACE_MEMORY(category_group, name)                   \
216   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
217 #define TRACE_EVENT_WITH_FLOW1(category_group, name, bind_id, flow_flags,  \
218                                arg1_name, arg1_val)                        \
219   INTERNAL_TRACE_MEMORY(category_group, name)                              \
220   INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, bind_id, \
221                                             flow_flags, arg1_name, arg1_val)
222 #define TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name,   \
223                      arg2_val)                                               \
224   INTERNAL_TRACE_MEMORY(category_group, name)                                \
225   INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val, \
226                                   arg2_name, arg2_val)
227 #define TRACE_EVENT_WITH_FLOW2(category_group, name, bind_id, flow_flags,    \
228                                arg1_name, arg1_val, arg2_name, arg2_val)     \
229   INTERNAL_TRACE_MEMORY(category_group, name)                                \
230   INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, bind_id,   \
231                                             flow_flags, arg1_name, arg1_val, \
232                                             arg2_name, arg2_val)
233 
234 // Records events like TRACE_EVENT2 but uses |memory_tag| for memory tracing.
235 // Use this where |name| is too generic to accurately aggregate allocations.
236 #define TRACE_EVENT_WITH_MEMORY_TAG2(category, name, memory_tag, arg1_name, \
237                                      arg1_val, arg2_name, arg2_val)         \
238   INTERNAL_TRACE_MEMORY(category, memory_tag)                               \
239   INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val,      \
240                                   arg2_name, arg2_val)
241 
242 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
243 // included in official builds.
244 
245 #if OFFICIAL_BUILD
246 #undef TRACING_IS_OFFICIAL_BUILD
247 #define TRACING_IS_OFFICIAL_BUILD 1
248 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
249 #define TRACING_IS_OFFICIAL_BUILD 0
250 #endif
251 
252 #if TRACING_IS_OFFICIAL_BUILD
253 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
254 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
255   (void)0
256 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
257                                arg2_name, arg2_val)                       \
258   (void)0
259 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
260 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, \
261                                        arg1_val)                               \
262   (void)0
263 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, \
264                                        arg1_val, arg2_name, arg2_val)          \
265   (void)0
266 #else
267 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
268   TRACE_EVENT0(category_group, name)
269 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
270   TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
271 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
272                                arg2_name, arg2_val)                       \
273   TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
274 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
275   TRACE_EVENT_INSTANT0(category_group, name, scope)
276 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, \
277                                        arg1_val)                               \
278   TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
279 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, \
280                                        arg1_val, arg2_name, arg2_val)          \
281   TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val,       \
282                        arg2_name, arg2_val)
283 #endif
284 
285 // Records a single event called "name" immediately, with 0, 1 or 2
286 // associated arguments. If the category is not enabled, then this
287 // does nothing.
288 // - category and name strings must have application lifetime (statics or
289 //   literals). They may not include " chars.
290 #define TRACE_EVENT_INSTANT0(category_group, name, scope)                   \
291   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
292                            TRACE_EVENT_FLAG_NONE | scope)
293 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
294   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name,    \
295                            TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val)
296 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
297                              arg2_name, arg2_val)                              \
298   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name,    \
299                            TRACE_EVENT_FLAG_NONE | scope, arg1_name, arg1_val, \
300                            arg2_name, arg2_val)
301 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope)              \
302   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
303                            TRACE_EVENT_FLAG_COPY | scope)
304 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, arg1_name,   \
305                                   arg1_val)                                 \
306   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name, \
307                            TRACE_EVENT_FLAG_COPY | scope, arg1_name, arg1_val)
308 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, arg1_name,      \
309                                   arg1_val, arg2_name, arg2_val)               \
310   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, category_group, name,    \
311                            TRACE_EVENT_FLAG_COPY | scope, arg1_name, arg1_val, \
312                            arg2_name, arg2_val)
313 
314 // Syntactic sugars for the sampling tracing in the main thread.
315 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
316   TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
317 #define TRACE_EVENT_GET_SAMPLING_STATE() \
318   TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
319 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
320   TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
321 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(categoryAndName) \
322   TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(0, categoryAndName)
323 
324 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
325 // associated arguments. If the category is not enabled, then this
326 // does nothing.
327 // - category and name strings must have application lifetime (statics or
328 //   literals). They may not include " chars.
329 #define TRACE_EVENT_BEGIN0(category_group, name)                          \
330   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
331                            TRACE_EVENT_FLAG_NONE)
332 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val)     \
333   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
334                            TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
335 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val,     \
336                            arg2_name, arg2_val)                           \
337   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
338                            TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val,    \
339                            arg2_name, arg2_val)
340 #define TRACE_EVENT_COPY_BEGIN0(category_group, name)                     \
341   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name, \
342                            TRACE_EVENT_FLAG_COPY)
343 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
344   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name,  \
345                            TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
346 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
347                                 arg2_name, arg2_val)                       \
348   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, category_group, name,  \
349                            TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val,     \
350                            arg2_name, arg2_val)
351 
352 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
353 // - |id| is used to match the _BEGIN event with the _END event.
354 //   Events are considered to match if their category_group, name and id values
355 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
356 //   If it's a pointer, the bits will be xored with a hash of the process ID so
357 //   that the same pointer on two different processes will not collide.
358 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, name, id, \
359                                                      thread_id, timestamp)     \
360   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
361       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id,      \
362       timestamp, TRACE_EVENT_FLAG_NONE)
363 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(                \
364     category_group, name, id, thread_id, timestamp)                       \
365   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                     \
366       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
367       timestamp, TRACE_EVENT_FLAG_COPY)
368 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP1(                \
369     category_group, name, id, thread_id, timestamp, arg1_name, arg1_val)  \
370   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                     \
371       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
372       timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
373 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP2(                \
374     category_group, name, id, thread_id, timestamp, arg1_name, arg1_val,  \
375     arg2_name, arg2_val)                                                  \
376   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                     \
377       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
378       timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name,   \
379       arg2_val)
380 
381 // Records a single END event for "name" immediately. If the category
382 // is not enabled, then this does nothing.
383 // - category and name strings must have application lifetime (statics or
384 //   literals). They may not include " chars.
385 #define TRACE_EVENT_END0(category_group, name)                          \
386   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
387                            TRACE_EVENT_FLAG_NONE)
388 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val)     \
389   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
390                            TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
391 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, arg2_name, \
392                          arg2_val)                                             \
393   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name,        \
394                            TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val,         \
395                            arg2_name, arg2_val)
396 #define TRACE_EVENT_COPY_END0(category_group, name)                     \
397   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name, \
398                            TRACE_EVENT_FLAG_COPY)
399 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
400   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name,  \
401                            TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
402 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
403                               arg2_name, arg2_val)                       \
404   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, category_group, name,  \
405                            TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val,   \
406                            arg2_name, arg2_val)
407 
408 #define TRACE_EVENT_MARK(category_group, name)                           \
409   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_MARK, category_group, name, \
410                            TRACE_EVENT_FLAG_NONE)
411 
412 #define TRACE_EVENT_MARK_WITH_TIMESTAMP(category_group, name, timestamp)   \
413   INTERNAL_TRACE_EVENT_ADD_WITH_TIMESTAMP(TRACE_EVENT_PHASE_MARK,          \
414                                           category_group, name, timestamp, \
415                                           TRACE_EVENT_FLAG_NONE)
416 
417 #define TRACE_EVENT_COPY_MARK(category_group, name)                      \
418   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_MARK, category_group, name, \
419                            TRACE_EVENT_FLAG_COPY)
420 
421 #define TRACE_EVENT_COPY_MARK_WITH_TIMESTAMP(category_group, name, timestamp) \
422   INTERNAL_TRACE_EVENT_ADD_WITH_TIMESTAMP(TRACE_EVENT_PHASE_MARK,             \
423                                           category_group, name, timestamp,    \
424                                           TRACE_EVENT_FLAG_COPY)
425 
426 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
427 // - |id| is used to match the _BEGIN event with the _END event.
428 //   Events are considered to match if their category_group, name and id values
429 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
430 //   If it's a pointer, the bits will be xored with a hash of the process ID so
431 //   that the same pointer on two different processes will not collide.
432 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, name, id, \
433                                                    thread_id, timestamp)     \
434   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                        \
435       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id,      \
436       timestamp, TRACE_EVENT_FLAG_NONE)
437 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0(                \
438     category_group, name, id, thread_id, timestamp)                     \
439   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                   \
440       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
441       timestamp, TRACE_EVENT_FLAG_COPY)
442 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP1(                 \
443     category_group, name, id, thread_id, timestamp, arg1_name, arg1_val) \
444   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                    \
445       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id,  \
446       timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
447 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP2(                 \
448     category_group, name, id, thread_id, timestamp, arg1_name, arg1_val, \
449     arg2_name, arg2_val)                                                 \
450   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                    \
451       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id,  \
452       timestamp, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name,  \
453       arg2_val)
454 
455 // Records the value of a counter called "name" immediately. Value
456 // must be representable as a 32 bit integer.
457 // - category and name strings must have application lifetime (statics or
458 //   literals). They may not include " chars.
459 #define TRACE_COUNTER1(category_group, name, value)                         \
460   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
461                            TRACE_EVENT_FLAG_NONE, "value",                  \
462                            static_cast<int>(value))
463 #define TRACE_COPY_COUNTER1(category_group, name, value)                    \
464   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
465                            TRACE_EVENT_FLAG_COPY, "value",                  \
466                            static_cast<int>(value))
467 
468 // Records the values of a multi-parted counter called "name" immediately.
469 // The UI will treat value1 and value2 as parts of a whole, displaying their
470 // values as a stacked-bar chart.
471 // - category and name strings must have application lifetime (statics or
472 //   literals). They may not include " chars.
473 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val,       \
474                        value2_name, value2_val)                             \
475   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
476                            TRACE_EVENT_FLAG_NONE, value1_name,              \
477                            static_cast<int>(value1_val), value2_name,       \
478                            static_cast<int>(value2_val))
479 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val,  \
480                             value2_name, value2_val)                        \
481   INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, category_group, name, \
482                            TRACE_EVENT_FLAG_COPY, value1_name,              \
483                            static_cast<int>(value1_val), value2_name,       \
484                            static_cast<int>(value2_val))
485 
486 // Records the value of a counter called "name" immediately. Value
487 // must be representable as a 32 bit integer.
488 // - category and name strings must have application lifetime (statics or
489 //   literals). They may not include " chars.
490 // - |id| is used to disambiguate counters with the same name. It must either
491 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
492 //   will be xored with a hash of the process ID so that the same pointer on
493 //   two different processes will not collide.
494 #define TRACE_COUNTER_ID1(category_group, name, id, value)                    \
495   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
496                                    name, id, TRACE_EVENT_FLAG_NONE, "value",  \
497                                    static_cast<int>(value))
498 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value)               \
499   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
500                                    name, id, TRACE_EVENT_FLAG_COPY, "value",  \
501                                    static_cast<int>(value))
502 
503 // Records the values of a multi-parted counter called "name" immediately.
504 // The UI will treat value1 and value2 as parts of a whole, displaying their
505 // values as a stacked-bar chart.
506 // - category and name strings must have application lifetime (statics or
507 //   literals). They may not include " chars.
508 // - |id| is used to disambiguate counters with the same name. It must either
509 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
510 //   will be xored with a hash of the process ID so that the same pointer on
511 //   two different processes will not collide.
512 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val,  \
513                           value2_name, value2_val)                            \
514   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
515                                    name, id, TRACE_EVENT_FLAG_NONE,           \
516                                    value1_name, static_cast<int>(value1_val), \
517                                    value2_name, static_cast<int>(value2_val))
518 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name,         \
519                                value1_val, value2_name, value2_val)           \
520   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, category_group, \
521                                    name, id, TRACE_EVENT_FLAG_COPY,           \
522                                    value1_name, static_cast<int>(value1_val), \
523                                    value2_name, static_cast<int>(value2_val))
524 
525 // TRACE_EVENT_SAMPLE_* events are injected by the sampling profiler.
526 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP0(category_group, name,       \
527                                                    thread_id, timestamp)       \
528   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
529       TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
530       TRACE_EVENT_FLAG_NONE)
531 
532 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP1(                            \
533     category_group, name, thread_id, timestamp, arg1_name, arg1_val)           \
534   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
535       TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
536       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
537 
538 #define TRACE_EVENT_SAMPLE_WITH_TID_AND_TIMESTAMP2(category_group, name,       \
539                                                    thread_id, timestamp,       \
540                                                    arg1_name, arg1_val,        \
541                                                    arg2_name, arg2_val)        \
542   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
543       TRACE_EVENT_PHASE_SAMPLE, category_group, name, 0, thread_id, timestamp, \
544       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
545 
546 // ASYNC_STEP_* APIs should be only used by legacy code. New code should
547 // consider using NESTABLE_ASYNC_* APIs to describe substeps within an async
548 // event.
549 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
550 // associated arguments. If the category is not enabled, then this
551 // does nothing.
552 // - category and name strings must have application lifetime (statics or
553 //   literals). They may not include " chars.
554 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
555 //   events are considered to match if their category_group, name and id values
556 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
557 //   If it's a pointer, the bits will be xored with a hash of the process ID so
558 //   that the same pointer on two different processes will not collide.
559 //
560 // An asynchronous operation can consist of multiple phases. The first phase is
561 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
562 // ASYNC_STEP_INTO or ASYNC_STEP_PAST macros. The ASYNC_STEP_INTO macro will
563 // annotate the block following the call. The ASYNC_STEP_PAST macro will
564 // annotate the block prior to the call. Note that any particular event must use
565 // only STEP_INTO or STEP_PAST macros; they can not mix and match. When the
566 // operation completes, call ASYNC_END.
567 //
568 // An ASYNC trace typically occurs on a single thread (if not, they will only be
569 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
570 // operation must use the same |name| and |id|. Each step can have its own
571 // args.
572 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id)        \
573   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
574                                    category_group, name, id,      \
575                                    TRACE_EVENT_FLAG_NONE)
576 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
577                                  arg1_val)                            \
578   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN,     \
579                                    category_group, name, id,          \
580                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
581 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
582                                  arg1_val, arg2_name, arg2_val)       \
583   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                   \
584       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id,        \
585       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
586 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id)   \
587   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
588                                    category_group, name, id,      \
589                                    TRACE_EVENT_FLAG_COPY)
590 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
591                                       arg1_val)                            \
592   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN,          \
593                                    category_group, name, id,               \
594                                    TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
595 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
596                                       arg1_val, arg2_name, arg2_val)       \
597   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                        \
598       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id,             \
599       TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
600 
601 // Similar to TRACE_EVENT_ASYNC_BEGINx but with a custom |at| timestamp
602 // provided.
603 #define TRACE_EVENT_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, name, id, \
604                                                 timestamp)                \
605   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                     \
606       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id,            \
607       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_NONE)
608 #define TRACE_EVENT_COPY_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, name, id, \
609                                                      timestamp)                \
610   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
611       TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id,                 \
612       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_COPY)
613 
614 // Records a single ASYNC_STEP_INTO event for |step| immediately. If the
615 // category is not enabled, then this does nothing. The |name| and |id| must
616 // match the ASYNC_BEGIN event above. The |step| param identifies this step
617 // within the async event. This should be called at the beginning of the next
618 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
619 // ASYNC_STEP_PAST events.
620 #define TRACE_EVENT_ASYNC_STEP_INTO0(category_group, name, id, step)  \
621   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_INTO, \
622                                    category_group, name, id,          \
623                                    TRACE_EVENT_FLAG_NONE, "step", step)
624 #define TRACE_EVENT_ASYNC_STEP_INTO1(category_group, name, id, step, \
625                                      arg1_name, arg1_val)            \
626   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
627       TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id,   \
628       TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
629 
630 // Similar to TRACE_EVENT_ASYNC_STEP_INTOx but with a custom |at| timestamp
631 // provided.
632 #define TRACE_EVENT_ASYNC_STEP_INTO_WITH_TIMESTAMP0(category_group, name, id, \
633                                                     step, timestamp)          \
634   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                         \
635       TRACE_EVENT_PHASE_ASYNC_STEP_INTO, category_group, name, id,            \
636       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_NONE,    \
637       "step", step)
638 
639 // Records a single ASYNC_STEP_PAST event for |step| immediately. If the
640 // category is not enabled, then this does nothing. The |name| and |id| must
641 // match the ASYNC_BEGIN event above. The |step| param identifies this step
642 // within the async event. This should be called at the beginning of the next
643 // phase of an asynchronous operation. The ASYNC_BEGIN event must not have any
644 // ASYNC_STEP_INTO events.
645 #define TRACE_EVENT_ASYNC_STEP_PAST0(category_group, name, id, step)  \
646   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP_PAST, \
647                                    category_group, name, id,          \
648                                    TRACE_EVENT_FLAG_NONE, "step", step)
649 #define TRACE_EVENT_ASYNC_STEP_PAST1(category_group, name, id, step, \
650                                      arg1_name, arg1_val)            \
651   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
652       TRACE_EVENT_PHASE_ASYNC_STEP_PAST, category_group, name, id,   \
653       TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
654 
655 // Records a single ASYNC_END event for "name" immediately. If the category
656 // is not enabled, then this does nothing.
657 #define TRACE_EVENT_ASYNC_END0(category_group, name, id)        \
658   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
659                                    category_group, name, id,    \
660                                    TRACE_EVENT_FLAG_NONE)
661 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
662   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END,               \
663                                    category_group, name, id,                  \
664                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
665 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
666                                arg2_name, arg2_val)                           \
667   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                           \
668       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id,                  \
669       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
670 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id)   \
671   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
672                                    category_group, name, id,    \
673                                    TRACE_EVENT_FLAG_COPY)
674 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
675                                     arg1_val)                            \
676   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END,          \
677                                    category_group, name, id,             \
678                                    TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
679 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
680                                     arg1_val, arg2_name, arg2_val)       \
681   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                      \
682       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id,             \
683       TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
684 
685 // Similar to TRACE_EVENT_ASYNC_ENDx but with a custom |at| timestamp provided.
686 #define TRACE_EVENT_ASYNC_END_WITH_TIMESTAMP0(category_group, name, id, \
687                                               timestamp)                \
688   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                   \
689       TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id,            \
690       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_NONE)
691 
692 // NESTABLE_ASYNC_* APIs are used to describe an async operation, which can
693 // be nested within a NESTABLE_ASYNC event and/or have inner NESTABLE_ASYNC
694 // events.
695 // - category and name strings must have application lifetime (statics or
696 //   literals). They may not include " chars.
697 // - A pair of NESTABLE_ASYNC_BEGIN event and NESTABLE_ASYNC_END event is
698 //   considered as a match if their category_group, name and id all match.
699 // - |id| must either be a pointer or an integer value up to 64 bits.
700 //   If it's a pointer, the bits will be xored with a hash of the process ID so
701 //   that the same pointer on two different processes will not collide.
702 // - |id| is used to match a child NESTABLE_ASYNC event with its parent
703 //   NESTABLE_ASYNC event. Therefore, events in the same nested event tree must
704 //   be logged using the same id and category_group.
705 //
706 // Unmatched NESTABLE_ASYNC_END event will be parsed as an event that starts
707 // at the first NESTABLE_ASYNC event of that id, and unmatched
708 // NESTABLE_ASYNC_BEGIN event will be parsed as an event that ends at the last
709 // NESTABLE_ASYNC event of that id. Corresponding warning messages for
710 // unmatched events will be shown in the analysis view.
711 
712 // Records a single NESTABLE_ASYNC_BEGIN event called "name" immediately, with
713 // 0, 1 or 2 associated arguments. If the category is not enabled, then this
714 // does nothing.
715 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_group, name, id)        \
716   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, \
717                                    category_group, name, id,               \
718                                    TRACE_EVENT_FLAG_NONE)
719 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
720                                           arg1_val)                            \
721   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN,     \
722                                    category_group, name, id,                   \
723                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
724 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
725                                           arg1_val, arg2_name, arg2_val)       \
726   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                            \
727       TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id,        \
728       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
729 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 0
730 // or 2 associated arguments. If the category is not enabled, then this does
731 // nothing.
732 #define TRACE_EVENT_NESTABLE_ASYNC_END0(category_group, name, id)        \
733   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, \
734                                    category_group, name, id,             \
735                                    TRACE_EVENT_FLAG_NONE)
736 // Records a single NESTABLE_ASYNC_END event called "name" immediately, with 1
737 // associated argument. If the category is not enabled, then this does nothing.
738 #define TRACE_EVENT_NESTABLE_ASYNC_END1(category_group, name, id, arg1_name, \
739                                         arg1_val)                            \
740   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_END,     \
741                                    category_group, name, id,                 \
742                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
743 #define TRACE_EVENT_NESTABLE_ASYNC_END2(category_group, name, id, arg1_name, \
744                                         arg1_val, arg2_name, arg2_val)       \
745   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                          \
746       TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id,        \
747       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
748 
749 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
750 // with none, one or two associated argument. If the category is not enabled,
751 // then this does nothing.
752 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT0(category_group, name, id)        \
753   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
754                                    category_group, name, id,                 \
755                                    TRACE_EVENT_FLAG_NONE)
756 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT1(category_group, name, id,        \
757                                             arg1_name, arg1_val)             \
758   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, \
759                                    category_group, name, id,                 \
760                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
761 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(                              \
762     category_group, name, id, arg1_name, arg1_val, arg2_name, arg2_val)   \
763   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                       \
764       TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, category_group, name, id, \
765       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
766 
767 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TTS2(                       \
768     category_group, name, id, arg1_name, arg1_val, arg2_name, arg2_val)        \
769   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                            \
770       TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id,        \
771       TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
772       arg2_name, arg2_val)
773 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TTS2(                         \
774     category_group, name, id, arg1_name, arg1_val, arg2_name, arg2_val)        \
775   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                            \
776       TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id,          \
777       TRACE_EVENT_FLAG_ASYNC_TTS | TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
778       arg2_name, arg2_val)
779 
780 // Similar to TRACE_EVENT_NESTABLE_ASYNC_{BEGIN,END}x but with a custom
781 // |timestamp| provided.
782 #define TRACE_EVENT_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(category_group, name, \
783                                                          id, timestamp)        \
784   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                          \
785       TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id,        \
786       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_NONE)
787 
788 #define TRACE_EVENT_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(category_group, name, \
789                                                        id, timestamp)        \
790   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                        \
791       TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id,        \
792       TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, TRACE_EVENT_FLAG_NONE)
793 
794 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_BEGIN_WITH_TIMESTAMP0(          \
795     category_group, name, id, timestamp)                                \
796   INTERNAL_TRACE_EVENT_ADD_WITH_ID_AND_TIMESTAMP(                       \
797       TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN, category_group, name, id, \
798       timestamp, TRACE_EVENT_FLAG_COPY)
799 #define TRACE_EVENT_COPY_NESTABLE_ASYNC_END_WITH_TIMESTAMP0(          \
800     category_group, name, id, timestamp)                              \
801   INTERNAL_TRACE_EVENT_ADD_WITH_ID_AND_TIMESTAMP(                     \
802       TRACE_EVENT_PHASE_NESTABLE_ASYNC_END, category_group, name, id, \
803       timestamp, TRACE_EVENT_FLAG_COPY)
804 
805 // Records a single NESTABLE_ASYNC_INSTANT event called "name" immediately,
806 // with 2 associated arguments. If the category is not enabled, then this
807 // does nothing.
808 #define TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(                              \
809     category_group, name, id, arg1_name, arg1_val, arg2_name, arg2_val)   \
810   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                       \
811       TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT, category_group, name, id, \
812       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
813 
814 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
815 // associated arguments. If the category is not enabled, then this
816 // does nothing.
817 // - category and name strings must have application lifetime (statics or
818 //   literals). They may not include " chars.
819 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
820 //   events are considered to match if their category_group, name and id values
821 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
822 //   If it's a pointer, the bits will be xored with a hash of the process ID so
823 //   that the same pointer on two different processes will not collide.
824 // FLOW events are different from ASYNC events in how they are drawn by the
825 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
826 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
827 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
828 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
829 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
830 // macros. When the operation completes, call FLOW_END. An async operation can
831 // span threads and processes, but all events in that operation must use the
832 // same |name| and |id|. Each event can have its own args.
833 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id)        \
834   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
835                                    category_group, name, id,     \
836                                    TRACE_EVENT_FLAG_NONE)
837 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
838   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN,               \
839                                    category_group, name, id,                   \
840                                    TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
841 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
842                                 arg2_name, arg2_val)                           \
843   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                            \
844       TRACE_EVENT_PHASE_FLOW_BEGIN, category_group, name, id,                  \
845       TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, arg2_name, arg2_val)
846 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id)   \
847   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
848                                    category_group, name, id,     \
849                                    TRACE_EVENT_FLAG_COPY)
850 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
851                                      arg1_val)                            \
852   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN,          \
853                                    category_group, name, id,              \
854                                    TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
855 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
856                                      arg1_val, arg2_name, arg2_val)       \
857   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                       \
858       TRACE_EVENT_PHASE_FLOW_BEGIN, category_group, name, id,             \
859       TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, arg2_name, arg2_val)
860 
861 // Records a single FLOW_STEP event for |step| immediately. If the category
862 // is not enabled, then this does nothing. The |name| and |id| must match the
863 // FLOW_BEGIN event above. The |step| param identifies this step within the
864 // async event. This should be called at the beginning of the next phase of an
865 // asynchronous operation.
866 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step)  \
867   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
868                                    category_group, name, id,    \
869                                    TRACE_EVENT_FLAG_NONE, "step", step)
870 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, arg1_name, \
871                                arg1_val)                                  \
872   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                       \
873       TRACE_EVENT_PHASE_FLOW_STEP, category_group, name, id,              \
874       TRACE_EVENT_FLAG_NONE, "step", step, arg1_name, arg1_val)
875 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
876   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP,     \
877                                    category_group, name, id,        \
878                                    TRACE_EVENT_FLAG_COPY, "step", step)
879 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, arg1_name, \
880                                     arg1_val)                                  \
881   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                            \
882       TRACE_EVENT_PHASE_FLOW_STEP, category_group, name, id,                   \
883       TRACE_EVENT_FLAG_COPY, "step", step, arg1_name, arg1_val)
884 
885 // Records a single FLOW_END event for "name" immediately. If the category
886 // is not enabled, then this does nothing.
887 #define TRACE_EVENT_FLOW_END0(category_group, name, id)                        \
888   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
889                                    name, id, TRACE_EVENT_FLAG_NONE)
890 #define TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(category_group, name, id)      \
891   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
892                                    name, id,                                   \
893                                    TRACE_EVENT_FLAG_BIND_TO_ENCLOSING)
894 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val)   \
895   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
896                                    name, id, TRACE_EVENT_FLAG_NONE, arg1_name, \
897                                    arg1_val)
898 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val,   \
899                               arg2_name, arg2_val)                             \
900   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
901                                    name, id, TRACE_EVENT_FLAG_NONE, arg1_name, \
902                                    arg1_val, arg2_name, arg2_val)
903 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id)                   \
904   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
905                                    name, id, TRACE_EVENT_FLAG_COPY)
906 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name,        \
907                                    arg1_val)                                   \
908   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
909                                    name, id, TRACE_EVENT_FLAG_COPY, arg1_name, \
910                                    arg1_val)
911 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name,        \
912                                    arg1_val, arg2_name, arg2_val)              \
913   INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, category_group, \
914                                    name, id, TRACE_EVENT_FLAG_COPY, arg1_name, \
915                                    arg1_val, arg2_name, arg2_val)
916 
917 // Macros to track the life time and value of arbitrary client objects.
918 // See also TraceTrackableObject.
919 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
920   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
921       TRACE_EVENT_PHASE_CREATE_OBJECT, category_group, name,         \
922       TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
923 
924 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, \
925                                             snapshot)                 \
926   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                   \
927       TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name,        \
928       TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
929 
930 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID_AND_TIMESTAMP(                    \
931     category_group, name, id, timestamp, snapshot)                            \
932   INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(                         \
933       TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, category_group, name,                \
934       TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, \
935       TRACE_EVENT_FLAG_NONE, "snapshot", snapshot)
936 
937 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
938   INTERNAL_TRACE_EVENT_ADD_WITH_ID(                                  \
939       TRACE_EVENT_PHASE_DELETE_OBJECT, category_group, name,         \
940       TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
941 
942 // Macro to efficiently determine if a given category group is enabled.
943 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret)             \
944   do {                                                                      \
945     INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group);                 \
946     if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE()) { \
947       *ret = true;                                                          \
948     } else {                                                                \
949       *ret = false;                                                         \
950     }                                                                       \
951   } while (0)
952 
953 // Macro to explicitly warm up a given category group. This could be useful in
954 // cases where we want to initialize a category group before any trace events
955 // for that category group is reported. For example, to have a category group
956 // always show up in the "record categories" list for manually selecting
957 // settings in about://tracing.
958 #define TRACE_EVENT_WARMUP_CATEGORY(category_group) \
959   INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group)
960 
961 // Macro to efficiently determine, through polling, if a new trace has begun.
962 #define TRACE_EVENT_IS_NEW_TRACE(ret)                                      \
963   do {                                                                     \
964     static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0;          \
965     int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED();   \
966     if (num_traces_recorded != -1 &&                                       \
967         num_traces_recorded !=                                             \
968             INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) {               \
969       INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = num_traces_recorded; \
970       *ret = true;                                                         \
971     } else {                                                               \
972       *ret = false;                                                        \
973     }                                                                      \
974   } while (0)
975 
976 // Notes regarding the following definitions:
977 // New values can be added and propagated to third party libraries, but existing
978 // definitions must never be changed, because third party libraries may use old
979 // definitions.
980 
981 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
982 #define TRACE_EVENT_PHASE_BEGIN ('B')
983 #define TRACE_EVENT_PHASE_END ('E')
984 #define TRACE_EVENT_PHASE_COMPLETE ('X')
985 #define TRACE_EVENT_PHASE_INSTANT ('I')
986 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
987 #define TRACE_EVENT_PHASE_ASYNC_STEP_INTO ('T')
988 #define TRACE_EVENT_PHASE_ASYNC_STEP_PAST ('p')
989 #define TRACE_EVENT_PHASE_ASYNC_END ('F')
990 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_BEGIN ('b')
991 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_END ('e')
992 #define TRACE_EVENT_PHASE_NESTABLE_ASYNC_INSTANT ('n')
993 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
994 #define TRACE_EVENT_PHASE_FLOW_STEP ('t')
995 #define TRACE_EVENT_PHASE_FLOW_END ('f')
996 #define TRACE_EVENT_PHASE_METADATA ('M')
997 #define TRACE_EVENT_PHASE_COUNTER ('C')
998 #define TRACE_EVENT_PHASE_SAMPLE ('P')
999 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
1000 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
1001 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
1002 #define TRACE_EVENT_PHASE_MEMORY_DUMP ('v')
1003 #define TRACE_EVENT_PHASE_MARK ('R')
1004 
1005 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
1006 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned int>(0))
1007 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned int>(1 << 0))
1008 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned int>(1 << 1))
1009 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned int>(1 << 2))
1010 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned int>(1 << 3))
1011 #define TRACE_EVENT_FLAG_SCOPE_EXTRA (static_cast<unsigned int>(1 << 4))
1012 #define TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP (static_cast<unsigned int>(1 << 5))
1013 #define TRACE_EVENT_FLAG_ASYNC_TTS (static_cast<unsigned int>(1 << 6))
1014 #define TRACE_EVENT_FLAG_BIND_TO_ENCLOSING (static_cast<unsigned int>(1 << 7))
1015 #define TRACE_EVENT_FLAG_FLOW_IN (static_cast<unsigned int>(1 << 8))
1016 #define TRACE_EVENT_FLAG_FLOW_OUT (static_cast<unsigned int>(1 << 9))
1017 #define TRACE_EVENT_FLAG_HAS_CONTEXT_ID (static_cast<unsigned int>(1 << 10))
1018 
1019 #define TRACE_EVENT_FLAG_SCOPE_MASK                          \
1020   (static_cast<unsigned int>(TRACE_EVENT_FLAG_SCOPE_OFFSET | \
1021                              TRACE_EVENT_FLAG_SCOPE_EXTRA))
1022 
1023 // Type values for identifying types in the TraceValue union.
1024 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1))
1025 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2))
1026 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3))
1027 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4))
1028 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5))
1029 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6))
1030 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7))
1031 #define TRACE_VALUE_TYPE_CONVERTABLE (static_cast<unsigned char>(8))
1032 
1033 // Enum reflecting the scope of an INSTANT event. Must fit within
1034 // TRACE_EVENT_FLAG_SCOPE_MASK.
1035 #define TRACE_EVENT_SCOPE_GLOBAL (static_cast<unsigned char>(0 << 3))
1036 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
1037 #define TRACE_EVENT_SCOPE_THREAD (static_cast<unsigned char>(2 << 3))
1038 
1039 #define TRACE_EVENT_SCOPE_NAME_GLOBAL ('g')
1040 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
1041 #define TRACE_EVENT_SCOPE_NAME_THREAD ('t')
1042 #endif  // SkTraceEventCommon_DEFINED
1043