1 // Copyright (c) 2013 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 
5 // Trace events are for tracking application performance and resource usage.
6 // Macros are provided to track:
7 //    Begin and end of function calls
8 //    Counters
9 //
10 // Events are issued against categories. Whereas LOG's
11 // categories are statically defined, TRACE categories are created
12 // implicitly with a string. For example:
13 //   TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent")
14 //
15 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
16 //   TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
17 //   doSomethingCostly()
18 //   TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
19 // Note: our tools can't always determine the correct BEGIN/END pairs unless
20 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you need them
21 // to be in separate scopes.
22 //
23 // A common use case is to trace entire function scopes. This
24 // issues a trace BEGIN and END automatically:
25 //   void doSomethingCostly() {
26 //     TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
27 //     ...
28 //   }
29 //
30 // Additional parameters can be associated with an event:
31 //   void doSomethingCostly2(int howMuch) {
32 //     TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
33 //         "howMuch", howMuch);
34 //     ...
35 //   }
36 //
37 // The trace system will automatically add to this information the
38 // current process id, thread id, and a timestamp in microseconds.
39 //
40 // To trace an asynchronous procedure such as an IPC send/receive, use ASYNC_BEGIN and
41 // ASYNC_END:
42 //   [single threaded sender code]
43 //     static int send_count = 0;
44 //     ++send_count;
45 //     TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
46 //     Send(new MyMessage(send_count));
47 //   [receive code]
48 //     void OnMyMessage(send_count) {
49 //       TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
50 //     }
51 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
52 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process. Pointers can
53 // be used for the ID parameter, and they will be mangled internally so that
54 // the same pointer on two different processes will not match. For example:
55 //   class MyTracedClass {
56 //    public:
57 //     MyTracedClass() {
58 //       TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
59 //     }
60 //     ~MyTracedClass() {
61 //       TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
62 //     }
63 //   }
64 //
65 // Trace event also supports counters, which is a way to track a quantity
66 // as it varies over time. Counters are created with the following macro:
67 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
68 //
69 // Counters are process-specific. The macro itself can be issued from any
70 // thread, however.
71 //
72 // Sometimes, you want to track two counters at once. You can do this with two
73 // counter macros:
74 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
75 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
76 // Or you can do it with a combined macro:
77 //   TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
78 //       "bytesPinned", g_myCounterValue[0],
79 //       "bytesAllocated", g_myCounterValue[1]);
80 // This indicates to the tracing UI that these counters should be displayed
81 // in a single graph, as a summed area chart.
82 //
83 // Since counters are in a global namespace, you may want to disembiguate with a
84 // unique ID, by using the TRACE_COUNTER_ID* variations.
85 //
86 // By default, trace collection is compiled in, but turned off at runtime.
87 // Collecting trace data is the responsibility of the embedding
88 // application. In Chrome's case, navigating to about:tracing will turn on
89 // tracing and display data collected across all active processes.
90 //
91 //
92 // Memory scoping note:
93 // Tracing copies the pointers, not the string content, of the strings passed
94 // in for category, name, and arg_names. Thus, the following code will
95 // cause problems:
96 //     char* str = strdup("impprtantName");
97 //     TRACE_EVENT_INSTANT0("SUBSYSTEM", str);  // BAD!
98 //     free(str);                   // Trace system now has dangling pointer
99 //
100 // To avoid this issue with the |name| and |arg_name| parameters, use the
101 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
102 // Notes: The category must always be in a long-lived char* (i.e. static const).
103 //        The |arg_values|, when used, are always deep copied with the _COPY
104 //        macros.
105 //
106 // When are string argument values copied:
107 // const char* arg_values are only referenced by default:
108 //     TRACE_EVENT1("category", "name",
109 //                  "arg1", "literal string is only referenced");
110 // Use TRACE_STR_COPY to force copying of a const char*:
111 //     TRACE_EVENT1("category", "name",
112 //                  "arg1", TRACE_STR_COPY("string will be copied"));
113 // std::string arg_values are always copied:
114 //     TRACE_EVENT1("category", "name",
115 //                  "arg1", std::string("string will be copied"));
116 //
117 //
118 // Thread Safety:
119 // A thread safe singleton and mutex are used for thread safety. Category
120 // enabled flags are used to limit the performance impact when the system
121 // is not enabled.
122 //
123 // TRACE_EVENT macros first cache a pointer to a category. The categories are
124 // statically allocated and safe at all times, even after exit. Fetching a
125 // category is protected by the TraceLog::lock_. Multiple threads initializing
126 // the static variable is safe, as they will be serialized by the lock and
127 // multiple calls will return the same pointer to the category.
128 //
129 // Then the category_enabled flag is checked. This is a unsigned char, and
130 // not intended to be multithread safe. It optimizes access to addTraceEvent
131 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
132 // cause some threads to incorrectly call or skip calling addTraceEvent near
133 // the time of the system being enabled or disabled. This is acceptable as
134 // we tolerate some data loss while the system is being enabled/disabled and
135 // because addTraceEvent is threadsafe internally and checks the enabled state
136 // again under lock.
137 //
138 // Without the use of these static category pointers and enabled flags all
139 // trace points would carry a significant performance cost of aquiring a lock
140 // and resolving the category.
141 
142 #ifndef COMMON_TRACE_EVENT_H_
143 #define COMMON_TRACE_EVENT_H_
144 
145 #include <string>
146 
147 #include "common/event_tracer.h"
148 
149 // By default, const char* argument values are assumed to have long-lived scope
150 // and will not be copied. Use this macro to force a const char* to be copied.
151 #define TRACE_STR_COPY(str) \
152     WebCore::TraceEvent::TraceStringWithCopy(str)
153 
154 // Records a pair of begin and end events called "name" for the current
155 // scope, with 0, 1 or 2 associated arguments. If the category is not
156 // enabled, then this does nothing.
157 // - category and name strings must have application lifetime (statics or
158 //   literals). They may not include " chars.
159 #define TRACE_EVENT0(category, name) \
160     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name)
161 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \
162     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val)
163 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
164     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \
165         arg2_name, arg2_val)
166 
167 // Records a single event called "name" immediately, with 0, 1 or 2
168 // associated arguments. If the category is not enabled, then this
169 // does nothing.
170 // - category and name strings must have application lifetime (statics or
171 //   literals). They may not include " chars.
172 #define TRACE_EVENT_INSTANT0(category, name) \
173     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
174         category, name, TRACE_EVENT_FLAG_NONE)
175 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
176     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
177         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
178 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
179         arg2_name, arg2_val) \
180     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
181         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
182         arg2_name, arg2_val)
183 #define TRACE_EVENT_COPY_INSTANT0(category, name) \
184     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
185         category, name, TRACE_EVENT_FLAG_COPY)
186 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
187     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
188         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
189 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
190         arg2_name, arg2_val) \
191     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
192         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
193         arg2_name, arg2_val)
194 
195 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
196 // associated arguments. If the category is not enabled, then this
197 // does nothing.
198 // - category and name strings must have application lifetime (statics or
199 //   literals). They may not include " chars.
200 #define TRACE_EVENT_BEGIN0(category, name) \
201     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
202         category, name, TRACE_EVENT_FLAG_NONE)
203 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
204     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
205         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
206 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
207         arg2_name, arg2_val) \
208     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
209         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
210         arg2_name, arg2_val)
211 #define TRACE_EVENT_COPY_BEGIN0(category, name) \
212     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
213         category, name, TRACE_EVENT_FLAG_COPY)
214 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \
215     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
216         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
217 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
218         arg2_name, arg2_val) \
219     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
220         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
221         arg2_name, arg2_val)
222 
223 // Records a single END event for "name" immediately. If the category
224 // is not enabled, then this does nothing.
225 // - category and name strings must have application lifetime (statics or
226 //   literals). They may not include " chars.
227 #define TRACE_EVENT_END0(category, name) \
228     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
229         category, name, TRACE_EVENT_FLAG_NONE)
230 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
231     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
232         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
233 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
234         arg2_name, arg2_val) \
235     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
236         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
237         arg2_name, arg2_val)
238 #define TRACE_EVENT_COPY_END0(category, name) \
239     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
240         category, name, TRACE_EVENT_FLAG_COPY)
241 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \
242     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
243         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
244 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
245         arg2_name, arg2_val) \
246     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
247         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
248         arg2_name, arg2_val)
249 
250 // Records the value of a counter called "name" immediately. Value
251 // must be representable as a 32 bit integer.
252 // - category and name strings must have application lifetime (statics or
253 //   literals). They may not include " chars.
254 #define TRACE_COUNTER1(category, name, value) \
255     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
256         category, name, TRACE_EVENT_FLAG_NONE, \
257         "value", static_cast<int>(value))
258 #define TRACE_COPY_COUNTER1(category, name, value) \
259     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
260         category, name, TRACE_EVENT_FLAG_COPY, \
261         "value", static_cast<int>(value))
262 
263 // Records the values of a multi-parted counter called "name" immediately.
264 // The UI will treat value1 and value2 as parts of a whole, displaying their
265 // values as a stacked-bar chart.
266 // - category and name strings must have application lifetime (statics or
267 //   literals). They may not include " chars.
268 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \
269         value2_name, value2_val) \
270     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
271         category, name, TRACE_EVENT_FLAG_NONE, \
272         value1_name, static_cast<int>(value1_val), \
273         value2_name, static_cast<int>(value2_val))
274 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
275         value2_name, value2_val) \
276     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
277         category, name, TRACE_EVENT_FLAG_COPY, \
278         value1_name, static_cast<int>(value1_val), \
279         value2_name, static_cast<int>(value2_val))
280 
281 // Records the value of a counter called "name" immediately. Value
282 // must be representable as a 32 bit integer.
283 // - category and name strings must have application lifetime (statics or
284 //   literals). They may not include " chars.
285 // - |id| is used to disambiguate counters with the same name. It must either
286 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
287 //   will be xored with a hash of the process ID so that the same pointer on
288 //   two different processes will not collide.
289 #define TRACE_COUNTER_ID1(category, name, id, value) \
290     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
291         category, name, id, TRACE_EVENT_FLAG_NONE, \
292         "value", static_cast<int>(value))
293 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \
294     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
295         category, name, id, TRACE_EVENT_FLAG_COPY, \
296         "value", static_cast<int>(value))
297 
298 // Records the values of a multi-parted counter called "name" immediately.
299 // The UI will treat value1 and value2 as parts of a whole, displaying their
300 // values as a stacked-bar chart.
301 // - category and name strings must have application lifetime (statics or
302 //   literals). They may not include " chars.
303 // - |id| is used to disambiguate counters with the same name. It must either
304 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
305 //   will be xored with a hash of the process ID so that the same pointer on
306 //   two different processes will not collide.
307 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
308         value2_name, value2_val) \
309     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
310         category, name, id, TRACE_EVENT_FLAG_NONE, \
311         value1_name, static_cast<int>(value1_val), \
312         value2_name, static_cast<int>(value2_val))
313 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
314         value2_name, value2_val) \
315     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
316         category, name, id, TRACE_EVENT_FLAG_COPY, \
317         value1_name, static_cast<int>(value1_val), \
318         value2_name, static_cast<int>(value2_val))
319 
320 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
321 // associated arguments. If the category is not enabled, then this
322 // does nothing.
323 // - category and name strings must have application lifetime (statics or
324 //   literals). They may not include " chars.
325 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
326 //   events are considered to match if their category, name and id values all
327 //   match. |id| must either be a pointer or an integer value up to 64 bits. If
328 //   it's a pointer, the bits will be xored with a hash of the process ID so
329 //   that the same pointer on two different processes will not collide.
330 // An asynchronous operation can consist of multiple phases. The first phase is
331 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
332 // ASYNC_STEP_BEGIN macros. When the operation completes, call ASYNC_END.
333 // An async operation can span threads and processes, but all events in that
334 // operation must use the same |name| and |id|. Each event can have its own
335 // args.
336 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
337     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
338         category, name, id, TRACE_EVENT_FLAG_NONE)
339 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
340     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
341         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
342 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
343         arg2_name, arg2_val) \
344     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
345         category, name, id, TRACE_EVENT_FLAG_NONE, \
346         arg1_name, arg1_val, arg2_name, arg2_val)
347 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \
348     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
349         category, name, id, TRACE_EVENT_FLAG_COPY)
350 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
351     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
352         category, name, id, TRACE_EVENT_FLAG_COPY, \
353         arg1_name, arg1_val)
354 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
355         arg2_name, arg2_val) \
356     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
357         category, name, id, TRACE_EVENT_FLAG_COPY, \
358         arg1_name, arg1_val, arg2_name, arg2_val)
359 
360 // Records a single ASYNC_STEP event for |step| immediately. If the category
361 // is not enabled, then this does nothing. The |name| and |id| must match the
362 // ASYNC_BEGIN event above. The |step| param identifies this step within the
363 // async event. This should be called at the beginning of the next phase of an
364 // asynchronous operation.
365 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \
366     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
367         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
368 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
369                                       arg1_name, arg1_val) \
370     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
371         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
372         arg1_name, arg1_val)
373 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \
374     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
375         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
376 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
377         arg1_name, arg1_val) \
378     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
379         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
380         arg1_name, arg1_val)
381 
382 // Records a single ASYNC_END event for "name" immediately. If the category
383 // is not enabled, then this does nothing.
384 #define TRACE_EVENT_ASYNC_END0(category, name, id) \
385     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
386         category, name, id, TRACE_EVENT_FLAG_NONE)
387 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
388     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
389         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
390 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
391         arg2_name, arg2_val) \
392     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
393         category, name, id, TRACE_EVENT_FLAG_NONE, \
394         arg1_name, arg1_val, arg2_name, arg2_val)
395 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \
396     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
397         category, name, id, TRACE_EVENT_FLAG_COPY)
398 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
399     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
400         category, name, id, TRACE_EVENT_FLAG_COPY, \
401         arg1_name, arg1_val)
402 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
403         arg2_name, arg2_val) \
404     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
405         category, name, id, TRACE_EVENT_FLAG_COPY, \
406         arg1_name, arg1_val, arg2_name, arg2_val)
407 
408 // Creates a scope of a sampling state with the given category and name (both must
409 // be constant strings). These states are intended for a sampling profiler.
410 // Implementation note: we store category and name together because we don't
411 // want the inconsistency/expense of storing two pointers.
412 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
413 // thread from others.
414 //
415 // {  // The sampling state is set within this scope.
416 //    TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
417 //    ...;
418 // }
419 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, name) \
420     TraceEvent::SamplingStateScope<bucket_number> traceEventSamplingScope(category "\0" name);
421 
422 // Returns a current sampling state of the given bucket.
423 // The format of the returned string is "category\0name".
424 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
425     TraceEvent::SamplingStateScope<bucket_number>::current()
426 
427 // Sets a current sampling state of the given bucket.
428 // |category| and |name| have to be constant strings.
429 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(bucket_number, category, name) \
430     TraceEvent::SamplingStateScope<bucket_number>::set(category "\0" name)
431 
432 // Sets a current sampling state of the given bucket.
433 // |categoryAndName| doesn't need to be a constant string.
434 // The format of the string is "category\0name".
435 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(bucket_number, categoryAndName) \
436     TraceEvent::SamplingStateScope<bucket_number>::set(categoryAndName)
437 
438 // Syntactic sugars for the sampling tracing in the main thread.
439 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
440     TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
441 #define TRACE_EVENT_GET_SAMPLING_STATE() \
442     TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
443 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
444     TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
445 #define TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(categoryAndName) \
446     TRACE_EVENT_SET_NONCONST_SAMPLING_STATE_FOR_BUCKET(0, categoryAndName)
447 
448 ////////////////////////////////////////////////////////////////////////////////
449 // Implementation specific tracing API definitions.
450 
451 // Get a pointer to the enabled state of the given trace category. Only
452 // long-lived literal strings should be given as the category name. The returned
453 // pointer can be held permanently in a local static for example. If the
454 // unsigned char is non-zero, tracing is enabled. If tracing is enabled,
455 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
456 // between the load of the tracing state and the call to
457 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
458 // for best performance when tracing is disabled.
459 // const unsigned char*
460 //     TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
461 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
462     angle::GetTraceCategoryEnabledFlag
463 
464 // Add a trace event to the platform tracing system.
465 // void TRACE_EVENT_API_ADD_TRACE_EVENT(
466 //                    char phase,
467 //                    const unsigned char* category_enabled,
468 //                    const char* name,
469 //                    unsigned long long id,
470 //                    int num_args,
471 //                    const char** arg_names,
472 //                    const unsigned char* arg_types,
473 //                    const unsigned long long* arg_values,
474 //                    unsigned char flags)
475 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
476     angle::AddTraceEvent
477 
478 ////////////////////////////////////////////////////////////////////////////////
479 
480 // Implementation detail: trace event macros create temporary variables
481 // to keep instrumentation overhead low. These macros give each temporary
482 // variable a unique name based on the line number to prevent name collissions.
483 #define INTERNAL_TRACE_EVENT_UID3(a, b) \
484     trace_event_unique_##a##b
485 #define INTERNAL_TRACE_EVENT_UID2(a, b) \
486     INTERNAL_TRACE_EVENT_UID3(a, b)
487 #define INTERNALTRACEEVENTUID(name_prefix) \
488     INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
489 
490 // Implementation detail: internal macro to create static category.
491 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
492     static const unsigned char* INTERNALTRACEEVENTUID(catstatic) = 0; \
493     if (!INTERNALTRACEEVENTUID(catstatic)) \
494       INTERNALTRACEEVENTUID(catstatic) = \
495           TRACE_EVENT_API_GET_CATEGORY_ENABLED(category);
496 
497 // Implementation detail: internal macro to create static category and add
498 // event if the category is enabled.
499 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
500     do { \
501         INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
502         if (*INTERNALTRACEEVENTUID(catstatic)) { \
503             gl::TraceEvent::addTraceEvent( \
504                 phase, INTERNALTRACEEVENTUID(catstatic), name, \
505                 gl::TraceEvent::noEventId, flags, ##__VA_ARGS__); \
506         } \
507     } while (0)
508 
509 // Implementation detail: internal macro to create static category and add begin
510 // event if the category is enabled. Also adds the end event when the scope
511 // ends.
512 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \
513     INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
514     gl::TraceEvent::TraceEndOnScopeClose \
515         INTERNALTRACEEVENTUID(profileScope); \
516     if (*INTERNALTRACEEVENTUID(catstatic)) { \
517       gl::TraceEvent::addTraceEvent( \
518           TRACE_EVENT_PHASE_BEGIN, \
519           INTERNALTRACEEVENTUID(catstatic), \
520           name, gl::TraceEvent::noEventId, \
521           TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
522       INTERNALTRACEEVENTUID(profileScope).initialize( \
523           INTERNALTRACEEVENTUID(catstatic), name); \
524     }
525 
526 // Implementation detail: internal macro to create static category and add
527 // event if the category is enabled.
528 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \
529                                          ...) \
530     do { \
531         INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
532         if (*INTERNALTRACEEVENTUID(catstatic)) { \
533             unsigned char traceEventFlags = flags | TRACE_EVENT_FLAG_HAS_ID; \
534             gl::TraceEvent::TraceID traceEventTraceID( \
535                 id, &traceEventFlags); \
536             gl::TraceEvent::addTraceEvent( \
537                 phase, INTERNALTRACEEVENTUID(catstatic), \
538                 name, traceEventTraceID.data(), traceEventFlags, \
539                 ##__VA_ARGS__); \
540         } \
541     } while (0)
542 
543 // Notes regarding the following definitions:
544 // New values can be added and propagated to third party libraries, but existing
545 // definitions must never be changed, because third party libraries may use old
546 // definitions.
547 
548 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
549 #define TRACE_EVENT_PHASE_BEGIN    ('B')
550 #define TRACE_EVENT_PHASE_END      ('E')
551 #define TRACE_EVENT_PHASE_INSTANT  ('I')
552 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
553 #define TRACE_EVENT_PHASE_ASYNC_STEP  ('T')
554 #define TRACE_EVENT_PHASE_ASYNC_END   ('F')
555 #define TRACE_EVENT_PHASE_METADATA ('M')
556 #define TRACE_EVENT_PHASE_COUNTER  ('C')
557 #define TRACE_EVENT_PHASE_SAMPLE  ('P')
558 
559 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
560 #define TRACE_EVENT_FLAG_NONE        (static_cast<unsigned char>(0))
561 #define TRACE_EVENT_FLAG_COPY        (static_cast<unsigned char>(1 << 0))
562 #define TRACE_EVENT_FLAG_HAS_ID      (static_cast<unsigned char>(1 << 1))
563 #define TRACE_EVENT_FLAG_MANGLE_ID   (static_cast<unsigned char>(1 << 2))
564 
565 // Type values for identifying types in the TraceValue union.
566 #define TRACE_VALUE_TYPE_BOOL         (static_cast<unsigned char>(1))
567 #define TRACE_VALUE_TYPE_UINT         (static_cast<unsigned char>(2))
568 #define TRACE_VALUE_TYPE_INT          (static_cast<unsigned char>(3))
569 #define TRACE_VALUE_TYPE_DOUBLE       (static_cast<unsigned char>(4))
570 #define TRACE_VALUE_TYPE_POINTER      (static_cast<unsigned char>(5))
571 #define TRACE_VALUE_TYPE_STRING       (static_cast<unsigned char>(6))
572 #define TRACE_VALUE_TYPE_COPY_STRING  (static_cast<unsigned char>(7))
573 
574 
575 namespace gl {
576 
577 namespace TraceEvent {
578 
579 // Specify these values when the corresponding argument of addTraceEvent is not
580 // used.
581 const int zeroNumArgs = 0;
582 const unsigned long long noEventId = 0;
583 
584 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
585 // are mangled with the Process ID so that they are unlikely to collide when the
586 // same pointer is used on different processes.
587 class TraceID {
588 public:
TraceID(const void * id,unsigned char * flags)589     explicit TraceID(const void* id, unsigned char* flags) :
590         m_data(static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(id)))
591     {
592         *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
593     }
TraceID(unsigned long long id,unsigned char * flags)594     explicit TraceID(unsigned long long id, unsigned char* flags) : m_data(id) { (void)flags; }
TraceID(unsigned long id,unsigned char * flags)595     explicit TraceID(unsigned long id, unsigned char* flags) : m_data(id) { (void)flags; }
TraceID(unsigned int id,unsigned char * flags)596     explicit TraceID(unsigned int id, unsigned char* flags) : m_data(id) { (void)flags; }
TraceID(unsigned short id,unsigned char * flags)597     explicit TraceID(unsigned short id, unsigned char* flags) : m_data(id) { (void)flags; }
TraceID(unsigned char id,unsigned char * flags)598     explicit TraceID(unsigned char id, unsigned char* flags) : m_data(id) { (void)flags; }
TraceID(long long id,unsigned char * flags)599     explicit TraceID(long long id, unsigned char* flags) :
600         m_data(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(long id,unsigned char * flags)601     explicit TraceID(long id, unsigned char* flags) :
602         m_data(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(int id,unsigned char * flags)603     explicit TraceID(int id, unsigned char* flags) :
604         m_data(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(short id,unsigned char * flags)605     explicit TraceID(short id, unsigned char* flags) :
606         m_data(static_cast<unsigned long long>(id)) { (void)flags; }
TraceID(signed char id,unsigned char * flags)607     explicit TraceID(signed char id, unsigned char* flags) :
608         m_data(static_cast<unsigned long long>(id)) { (void)flags; }
609 
data()610     unsigned long long data() const { return m_data; }
611 
612 private:
613     unsigned long long m_data;
614 };
615 
616 // Simple union to store various types as unsigned long long.
617 union TraceValueUnion {
618     bool m_bool;
619     unsigned long long m_uint;
620     long long m_int;
621     double m_double;
622     const void* m_pointer;
623     const char* m_string;
624 };
625 
626 // Simple container for const char* that should be copied instead of retained.
627 class TraceStringWithCopy {
628 public:
TraceStringWithCopy(const char * str)629     explicit TraceStringWithCopy(const char* str) : m_str(str) { }
630     operator const char* () const { return m_str; }
631 private:
632     const char* m_str;
633 };
634 
635 // Define setTraceValue for each allowed type. It stores the type and
636 // value in the return arguments. This allows this API to avoid declaring any
637 // structures so that it is portable to third_party libraries.
638 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
639                                          union_member, \
640                                          value_type_id) \
641     static inline void setTraceValue(actual_type arg, \
642                                      unsigned char* type, \
643                                      unsigned long long* value) { \
644         TraceValueUnion typeValue; \
645         typeValue.union_member = arg; \
646         *type = value_type_id; \
647         *value = typeValue.m_uint; \
648     }
649 // Simpler form for int types that can be safely casted.
650 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
651                                              value_type_id) \
652     static inline void setTraceValue(actual_type arg, \
653                                      unsigned char* type, \
654                                      unsigned long long* value) { \
655         *type = value_type_id; \
656         *value = static_cast<unsigned long long>(arg); \
657     }
658 
INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long,TRACE_VALUE_TYPE_UINT)659 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
660 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
661 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
662 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
663 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
664 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
665 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
666 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
667 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, m_bool, TRACE_VALUE_TYPE_BOOL)
668 INTERNAL_DECLARE_SET_TRACE_VALUE(double, m_double, TRACE_VALUE_TYPE_DOUBLE)
669 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, m_pointer,
670                                  TRACE_VALUE_TYPE_POINTER)
671 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, m_string,
672                                  TRACE_VALUE_TYPE_STRING)
673 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, m_string,
674                                  TRACE_VALUE_TYPE_COPY_STRING)
675 
676 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
677 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
678 
679 static inline void setTraceValue(const std::string& arg,
680                                  unsigned char* type,
681                                  unsigned long long* value) {
682     TraceValueUnion typeValue;
683     typeValue.m_string = arg.data();
684     *type = TRACE_VALUE_TYPE_COPY_STRING;
685     *value = typeValue.m_uint;
686 }
687 
688 // These addTraceEvent template functions are defined here instead of in the
689 // macro, because the arg values could be temporary string objects. In order to
690 // store pointers to the internal c_str and pass through to the tracing API, the
691 // arg values must live throughout these procedures.
692 
addTraceEvent(char phase,const unsigned char * categoryEnabled,const char * name,unsigned long long id,unsigned char flags)693 static inline angle::TraceEventHandle addTraceEvent(char phase,
694                                                     const unsigned char *categoryEnabled,
695                                                     const char *name,
696                                                     unsigned long long id,
697                                                     unsigned char flags)
698 {
699     return TRACE_EVENT_API_ADD_TRACE_EVENT(
700         phase, categoryEnabled, name, id,
701         zeroNumArgs, 0, 0, 0,
702         flags);
703 }
704 
705 template <class ARG1_TYPE>
addTraceEvent(char phase,const unsigned char * categoryEnabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1Name,const ARG1_TYPE & arg1Val)706 static inline angle::TraceEventHandle addTraceEvent(char phase,
707                                                     const unsigned char *categoryEnabled,
708                                                     const char *name,
709                                                     unsigned long long id,
710                                                     unsigned char flags,
711                                                     const char *arg1Name,
712                                                     const ARG1_TYPE &arg1Val)
713 {
714     const int numArgs = 1;
715     unsigned char argTypes[1];
716     unsigned long long argValues[1];
717     setTraceValue(arg1Val, &argTypes[0], &argValues[0]);
718     return TRACE_EVENT_API_ADD_TRACE_EVENT(
719         phase, categoryEnabled, name, id,
720         numArgs, &arg1Name, argTypes, argValues,
721         flags);
722 }
723 
724 template <class ARG1_TYPE, class ARG2_TYPE>
addTraceEvent(char phase,const unsigned char * categoryEnabled,const char * name,unsigned long long id,unsigned char flags,const char * arg1Name,const ARG1_TYPE & arg1Val,const char * arg2Name,const ARG2_TYPE & arg2Val)725 static inline angle::TraceEventHandle addTraceEvent(char phase,
726                                                     const unsigned char *categoryEnabled,
727                                                     const char *name,
728                                                     unsigned long long id,
729                                                     unsigned char flags,
730                                                     const char *arg1Name,
731                                                     const ARG1_TYPE &arg1Val,
732                                                     const char *arg2Name,
733                                                     const ARG2_TYPE &arg2Val)
734 {
735     const int numArgs = 2;
736     const char* argNames[2] = { arg1Name, arg2Name };
737     unsigned char argTypes[2];
738     unsigned long long argValues[2];
739     setTraceValue(arg1Val, &argTypes[0], &argValues[0]);
740     setTraceValue(arg2Val, &argTypes[1], &argValues[1]);
741     return TRACE_EVENT_API_ADD_TRACE_EVENT(
742         phase, categoryEnabled, name, id,
743         numArgs, argNames, argTypes, argValues,
744         flags);
745 }
746 
747 // Used by TRACE_EVENTx macro. Do not use directly.
748 class TraceEndOnScopeClose {
749 public:
750     // Note: members of m_data intentionally left uninitialized. See initialize.
TraceEndOnScopeClose()751     TraceEndOnScopeClose() : m_pdata(0) { }
~TraceEndOnScopeClose()752     ~TraceEndOnScopeClose()
753     {
754         if (m_pdata)
755             addEventIfEnabled();
756     }
757 
initialize(const unsigned char * categoryEnabled,const char * name)758     void initialize(const unsigned char* categoryEnabled,
759                     const char* name)
760     {
761         m_data.categoryEnabled = categoryEnabled;
762         m_data.name = name;
763         m_pdata = &m_data;
764     }
765 
766 private:
767     // Add the end event if the category is still enabled.
addEventIfEnabled()768     void addEventIfEnabled()
769     {
770         // Only called when m_pdata is non-null.
771         if (*m_pdata->categoryEnabled) {
772             TRACE_EVENT_API_ADD_TRACE_EVENT(
773                 TRACE_EVENT_PHASE_END,
774                 m_pdata->categoryEnabled,
775                 m_pdata->name, noEventId,
776                 zeroNumArgs, 0, 0, 0,
777                 TRACE_EVENT_FLAG_NONE);
778         }
779     }
780 
781     // This Data struct workaround is to avoid initializing all the members
782     // in Data during construction of this object, since this object is always
783     // constructed, even when tracing is disabled. If the members of Data were
784     // members of this class instead, compiler warnings occur about potential
785     // uninitialized accesses.
786     struct Data {
787         const unsigned char* categoryEnabled;
788         const char* name;
789     };
790     Data* m_pdata;
791     Data m_data;
792 };
793 
794 } // namespace TraceEvent
795 
796 } // namespace gl
797 
798 #endif
799