Home | History | Annotate | Download | only in interface
      1 // Copyright (c) 2012 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 under third_party_mods/chromium or at:
      4 // http://src.chromium.org/svn/trunk/src/LICENSE
      5 
      6 #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_
      7 #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_
      8 
      9 #include <string>
     10 
     11 #include "webrtc/system_wrappers/interface/event_tracer.h"
     12 
     13 #if defined(TRACE_EVENT0)
     14 #error "Another copy of trace_event.h has already been included."
     15 #endif
     16 
     17 // Extracted from Chromium's src/base/debug/trace_event.h.
     18 
     19 // This header is designed to give you trace_event macros without specifying
     20 // how the events actually get collected and stored. If you need to expose trace
     21 // event to some other universe, you can copy-and-paste this file,
     22 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for
     23 // the target platform. The end result is that multiple libraries can funnel
     24 // events through to a shared trace event collector.
     25 
     26 // Trace events are for tracking application performance and resource usage.
     27 // Macros are provided to track:
     28 //    Begin and end of function calls
     29 //    Counters
     30 //
     31 // Events are issued against categories. Whereas LOG's
     32 // categories are statically defined, TRACE categories are created
     33 // implicitly with a string. For example:
     34 //   TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent")
     35 //
     36 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
     37 //   TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
     38 //   doSomethingCostly()
     39 //   TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
     40 // Note: our tools can't always determine the correct BEGIN/END pairs unless
     41 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you
     42 // need them to be in separate scopes.
     43 //
     44 // A common use case is to trace entire function scopes. This
     45 // issues a trace BEGIN and END automatically:
     46 //   void doSomethingCostly() {
     47 //     TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
     48 //     ...
     49 //   }
     50 //
     51 // Additional parameters can be associated with an event:
     52 //   void doSomethingCostly2(int howMuch) {
     53 //     TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
     54 //         "howMuch", howMuch);
     55 //     ...
     56 //   }
     57 //
     58 // The trace system will automatically add to this information the
     59 // current process id, thread id, and a timestamp in microseconds.
     60 //
     61 // To trace an asynchronous procedure such as an IPC send/receive, use
     62 // ASYNC_BEGIN and ASYNC_END:
     63 //   [single threaded sender code]
     64 //     static int send_count = 0;
     65 //     ++send_count;
     66 //     TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
     67 //     Send(new MyMessage(send_count));
     68 //   [receive code]
     69 //     void OnMyMessage(send_count) {
     70 //       TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
     71 //     }
     72 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
     73 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process.
     74 // Pointers can be used for the ID parameter, and they will be mangled
     75 // internally so that the same pointer on two different processes will not
     76 // match. For example:
     77 //   class MyTracedClass {
     78 //    public:
     79 //     MyTracedClass() {
     80 //       TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
     81 //     }
     82 //     ~MyTracedClass() {
     83 //       TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
     84 //     }
     85 //   }
     86 //
     87 // Trace event also supports counters, which is a way to track a quantity
     88 // as it varies over time. Counters are created with the following macro:
     89 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
     90 //
     91 // Counters are process-specific. The macro itself can be issued from any
     92 // thread, however.
     93 //
     94 // Sometimes, you want to track two counters at once. You can do this with two
     95 // counter macros:
     96 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
     97 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
     98 // Or you can do it with a combined macro:
     99 //   TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
    100 //       "bytesPinned", g_myCounterValue[0],
    101 //       "bytesAllocated", g_myCounterValue[1]);
    102 // This indicates to the tracing UI that these counters should be displayed
    103 // in a single graph, as a summed area chart.
    104 //
    105 // Since counters are in a global namespace, you may want to disembiguate with a
    106 // unique ID, by using the TRACE_COUNTER_ID* variations.
    107 //
    108 // By default, trace collection is compiled in, but turned off at runtime.
    109 // Collecting trace data is the responsibility of the embedding
    110 // application. In Chrome's case, navigating to about:tracing will turn on
    111 // tracing and display data collected across all active processes.
    112 //
    113 //
    114 // Memory scoping note:
    115 // Tracing copies the pointers, not the string content, of the strings passed
    116 // in for category, name, and arg_names.  Thus, the following code will
    117 // cause problems:
    118 //     char* str = strdup("impprtantName");
    119 //     TRACE_EVENT_INSTANT0("SUBSYSTEM", str);  // BAD!
    120 //     free(str);                   // Trace system now has dangling pointer
    121 //
    122 // To avoid this issue with the |name| and |arg_name| parameters, use the
    123 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
    124 // Notes: The category must always be in a long-lived char* (i.e. static const).
    125 //        The |arg_values|, when used, are always deep copied with the _COPY
    126 //        macros.
    127 //
    128 // When are string argument values copied:
    129 // const char* arg_values are only referenced by default:
    130 //     TRACE_EVENT1("category", "name",
    131 //                  "arg1", "literal string is only referenced");
    132 // Use TRACE_STR_COPY to force copying of a const char*:
    133 //     TRACE_EVENT1("category", "name",
    134 //                  "arg1", TRACE_STR_COPY("string will be copied"));
    135 // std::string arg_values are always copied:
    136 //     TRACE_EVENT1("category", "name",
    137 //                  "arg1", std::string("string will be copied"));
    138 //
    139 //
    140 // Thread Safety:
    141 // Thread safety is provided by methods defined in event_tracer.h. See the file
    142 // for details.
    143 
    144 
    145 // By default, const char* argument values are assumed to have long-lived scope
    146 // and will not be copied. Use this macro to force a const char* to be copied.
    147 #define TRACE_STR_COPY(str) \
    148     webrtc::trace_event_internal::TraceStringWithCopy(str)
    149 
    150 // By default, uint64 ID argument values are not mangled with the Process ID in
    151 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
    152 #define TRACE_ID_MANGLE(id) \
    153     webrtc::trace_event_internal::TraceID::ForceMangle(id)
    154 
    155 // Records a pair of begin and end events called "name" for the current
    156 // scope, with 0, 1 or 2 associated arguments. If the category is not
    157 // enabled, then this does nothing.
    158 // - category and name strings must have application lifetime (statics or
    159 //   literals). They may not include " chars.
    160 #define TRACE_EVENT0(category, name) \
    161     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name)
    162 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \
    163     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val)
    164 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \
    165     INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \
    166         arg2_name, arg2_val)
    167 
    168 // Same as TRACE_EVENT except that they are not included in official builds.
    169 #ifdef OFFICIAL_BUILD
    170 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0
    171 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0
    172 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \
    173                                arg2_name, arg2_val) (void)0
    174 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0
    175 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
    176     (void)0
    177 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
    178                                        arg2_name, arg2_val) (void)0
    179 #else
    180 #define UNSHIPPED_TRACE_EVENT0(category, name) \
    181     TRACE_EVENT0(category, name)
    182 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) \
    183     TRACE_EVENT1(category, name, arg1_name, arg1_val)
    184 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \
    185                                arg2_name, arg2_val) \
    186     TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val)
    187 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) \
    188     TRACE_EVENT_INSTANT0(category, name)
    189 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
    190     TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val)
    191 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
    192                                        arg2_name, arg2_val) \
    193     TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
    194                          arg2_name, arg2_val)
    195 #endif
    196 
    197 // Records a single event called "name" immediately, with 0, 1 or 2
    198 // associated arguments. If the category is not enabled, then this
    199 // does nothing.
    200 // - category and name strings must have application lifetime (statics or
    201 //   literals). They may not include " chars.
    202 #define TRACE_EVENT_INSTANT0(category, name) \
    203     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    204         category, name, TRACE_EVENT_FLAG_NONE)
    205 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \
    206     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    207         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    208 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \
    209         arg2_name, arg2_val) \
    210     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    211         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
    212         arg2_name, arg2_val)
    213 #define TRACE_EVENT_COPY_INSTANT0(category, name) \
    214     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    215         category, name, TRACE_EVENT_FLAG_COPY)
    216 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \
    217     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    218         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
    219 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \
    220         arg2_name, arg2_val) \
    221     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    222         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
    223         arg2_name, arg2_val)
    224 
    225 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
    226 // associated arguments. If the category is not enabled, then this
    227 // does nothing.
    228 // - category and name strings must have application lifetime (statics or
    229 //   literals). They may not include " chars.
    230 #define TRACE_EVENT_BEGIN0(category, name) \
    231     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    232         category, name, TRACE_EVENT_FLAG_NONE)
    233 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \
    234     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    235         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    236 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \
    237         arg2_name, arg2_val) \
    238     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    239         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
    240         arg2_name, arg2_val)
    241 #define TRACE_EVENT_COPY_BEGIN0(category, name) \
    242     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    243         category, name, TRACE_EVENT_FLAG_COPY)
    244 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \
    245     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    246         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
    247 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \
    248         arg2_name, arg2_val) \
    249     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    250         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
    251         arg2_name, arg2_val)
    252 
    253 // Records a single END event for "name" immediately. If the category
    254 // is not enabled, then this does nothing.
    255 // - category and name strings must have application lifetime (statics or
    256 //   literals). They may not include " chars.
    257 #define TRACE_EVENT_END0(category, name) \
    258     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    259         category, name, TRACE_EVENT_FLAG_NONE)
    260 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \
    261     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    262         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    263 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \
    264         arg2_name, arg2_val) \
    265     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    266         category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
    267         arg2_name, arg2_val)
    268 #define TRACE_EVENT_COPY_END0(category, name) \
    269     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    270         category, name, TRACE_EVENT_FLAG_COPY)
    271 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \
    272     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    273         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
    274 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \
    275         arg2_name, arg2_val) \
    276     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    277         category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
    278         arg2_name, arg2_val)
    279 
    280 // Records the value of a counter called "name" immediately. Value
    281 // must be representable as a 32 bit integer.
    282 // - category and name strings must have application lifetime (statics or
    283 //   literals). They may not include " chars.
    284 #define TRACE_COUNTER1(category, name, value) \
    285     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    286         category, name, TRACE_EVENT_FLAG_NONE, \
    287         "value", static_cast<int>(value))
    288 #define TRACE_COPY_COUNTER1(category, name, value) \
    289     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    290         category, name, TRACE_EVENT_FLAG_COPY, \
    291         "value", static_cast<int>(value))
    292 
    293 // Records the values of a multi-parted counter called "name" immediately.
    294 // The UI will treat value1 and value2 as parts of a whole, displaying their
    295 // values as a stacked-bar chart.
    296 // - category and name strings must have application lifetime (statics or
    297 //   literals). They may not include " chars.
    298 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \
    299         value2_name, value2_val) \
    300     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    301         category, name, TRACE_EVENT_FLAG_NONE, \
    302         value1_name, static_cast<int>(value1_val), \
    303         value2_name, static_cast<int>(value2_val))
    304 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \
    305         value2_name, value2_val) \
    306     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    307         category, name, TRACE_EVENT_FLAG_COPY, \
    308         value1_name, static_cast<int>(value1_val), \
    309         value2_name, static_cast<int>(value2_val))
    310 
    311 // Records the value of a counter called "name" immediately. Value
    312 // must be representable as a 32 bit integer.
    313 // - category and name strings must have application lifetime (statics or
    314 //   literals). They may not include " chars.
    315 // - |id| is used to disambiguate counters with the same name. It must either
    316 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
    317 //   will be xored with a hash of the process ID so that the same pointer on
    318 //   two different processes will not collide.
    319 #define TRACE_COUNTER_ID1(category, name, id, value) \
    320     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    321         category, name, id, TRACE_EVENT_FLAG_NONE, \
    322         "value", static_cast<int>(value))
    323 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \
    324     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    325         category, name, id, TRACE_EVENT_FLAG_COPY, \
    326         "value", static_cast<int>(value))
    327 
    328 // Records the values of a multi-parted counter called "name" immediately.
    329 // The UI will treat value1 and value2 as parts of a whole, displaying their
    330 // values as a stacked-bar chart.
    331 // - category and name strings must have application lifetime (statics or
    332 //   literals). They may not include " chars.
    333 // - |id| is used to disambiguate counters with the same name. It must either
    334 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
    335 //   will be xored with a hash of the process ID so that the same pointer on
    336 //   two different processes will not collide.
    337 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \
    338         value2_name, value2_val) \
    339     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    340         category, name, id, TRACE_EVENT_FLAG_NONE, \
    341         value1_name, static_cast<int>(value1_val), \
    342         value2_name, static_cast<int>(value2_val))
    343 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \
    344         value2_name, value2_val) \
    345     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    346         category, name, id, TRACE_EVENT_FLAG_COPY, \
    347         value1_name, static_cast<int>(value1_val), \
    348         value2_name, static_cast<int>(value2_val))
    349 
    350 
    351 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
    352 // associated arguments. If the category is not enabled, then this
    353 // does nothing.
    354 // - category and name strings must have application lifetime (statics or
    355 //   literals). They may not include " chars.
    356 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
    357 //   events are considered to match if their category, name and id values all
    358 //   match. |id| must either be a pointer or an integer value up to 64 bits. If
    359 //   it's a pointer, the bits will be xored with a hash of the process ID so
    360 //   that the same pointer on two different processes will not collide.
    361 // An asynchronous operation can consist of multiple phases. The first phase is
    362 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
    363 // ASYNC_STEP macros. When the operation completes, call ASYNC_END.
    364 // An ASYNC trace typically occur on a single thread (if not, they will only be
    365 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
    366 // operation must use the same |name| and |id|. Each event can have its own
    367 // args.
    368 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \
    369     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    370         category, name, id, TRACE_EVENT_FLAG_NONE)
    371 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
    372     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    373         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    374 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
    375         arg2_name, arg2_val) \
    376     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    377         category, name, id, TRACE_EVENT_FLAG_NONE, \
    378         arg1_name, arg1_val, arg2_name, arg2_val)
    379 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \
    380     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    381         category, name, id, TRACE_EVENT_FLAG_COPY)
    382 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \
    383     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    384         category, name, id, TRACE_EVENT_FLAG_COPY, \
    385         arg1_name, arg1_val)
    386 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \
    387         arg2_name, arg2_val) \
    388     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    389         category, name, id, TRACE_EVENT_FLAG_COPY, \
    390         arg1_name, arg1_val, arg2_name, arg2_val)
    391 
    392 // Records a single ASYNC_STEP event for |step| immediately. If the category
    393 // is not enabled, then this does nothing. The |name| and |id| must match the
    394 // ASYNC_BEGIN event above. The |step| param identifies this step within the
    395 // async event. This should be called at the beginning of the next phase of an
    396 // asynchronous operation.
    397 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \
    398     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    399         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
    400 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \
    401                                       arg1_name, arg1_val) \
    402     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    403         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
    404         arg1_name, arg1_val)
    405 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \
    406     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    407         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
    408 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \
    409         arg1_name, arg1_val) \
    410     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    411         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
    412         arg1_name, arg1_val)
    413 
    414 // Records a single ASYNC_END event for "name" immediately. If the category
    415 // is not enabled, then this does nothing.
    416 #define TRACE_EVENT_ASYNC_END0(category, name, id) \
    417     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    418         category, name, id, TRACE_EVENT_FLAG_NONE)
    419 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
    420     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    421         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    422 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
    423         arg2_name, arg2_val) \
    424     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    425         category, name, id, TRACE_EVENT_FLAG_NONE, \
    426         arg1_name, arg1_val, arg2_name, arg2_val)
    427 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \
    428     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    429         category, name, id, TRACE_EVENT_FLAG_COPY)
    430 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \
    431     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    432         category, name, id, TRACE_EVENT_FLAG_COPY, \
    433         arg1_name, arg1_val)
    434 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \
    435         arg2_name, arg2_val) \
    436     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    437         category, name, id, TRACE_EVENT_FLAG_COPY, \
    438         arg1_name, arg1_val, arg2_name, arg2_val)
    439 
    440 
    441 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
    442 // associated arguments. If the category is not enabled, then this
    443 // does nothing.
    444 // - category and name strings must have application lifetime (statics or
    445 //   literals). They may not include " chars.
    446 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
    447 //   events are considered to match if their category, name and id values all
    448 //   match. |id| must either be a pointer or an integer value up to 64 bits. If
    449 //   it's a pointer, the bits will be xored with a hash of the process ID so
    450 //   that the same pointer on two different processes will not collide.
    451 // FLOW events are different from ASYNC events in how they are drawn by the
    452 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
    453 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
    454 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
    455 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
    456 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
    457 // macros. When the operation completes, call FLOW_END. An async operation can
    458 // span threads and processes, but all events in that operation must use the
    459 // same |name| and |id|. Each event can have its own args.
    460 #define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \
    461     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    462         category, name, id, TRACE_EVENT_FLAG_NONE)
    463 #define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
    464     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    465         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    466 #define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
    467         arg2_name, arg2_val) \
    468     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    469         category, name, id, TRACE_EVENT_FLAG_NONE, \
    470         arg1_name, arg1_val, arg2_name, arg2_val)
    471 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \
    472     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    473         category, name, id, TRACE_EVENT_FLAG_COPY)
    474 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \
    475     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    476         category, name, id, TRACE_EVENT_FLAG_COPY, \
    477         arg1_name, arg1_val)
    478 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \
    479         arg2_name, arg2_val) \
    480     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    481         category, name, id, TRACE_EVENT_FLAG_COPY, \
    482         arg1_name, arg1_val, arg2_name, arg2_val)
    483 
    484 // Records a single FLOW_STEP event for |step| immediately. If the category
    485 // is not enabled, then this does nothing. The |name| and |id| must match the
    486 // FLOW_BEGIN event above. The |step| param identifies this step within the
    487 // async event. This should be called at the beginning of the next phase of an
    488 // asynchronous operation.
    489 #define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \
    490     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    491         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
    492 #define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \
    493         arg1_name, arg1_val) \
    494     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    495         category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
    496         arg1_name, arg1_val)
    497 #define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \
    498     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    499         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
    500 #define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \
    501         arg1_name, arg1_val) \
    502     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    503         category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
    504         arg1_name, arg1_val)
    505 
    506 // Records a single FLOW_END event for "name" immediately. If the category
    507 // is not enabled, then this does nothing.
    508 #define TRACE_EVENT_FLOW_END0(category, name, id) \
    509     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    510         category, name, id, TRACE_EVENT_FLAG_NONE)
    511 #define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \
    512     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    513         category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    514 #define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \
    515         arg2_name, arg2_val) \
    516     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    517         category, name, id, TRACE_EVENT_FLAG_NONE, \
    518         arg1_name, arg1_val, arg2_name, arg2_val)
    519 #define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \
    520     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    521         category, name, id, TRACE_EVENT_FLAG_COPY)
    522 #define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \
    523     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    524         category, name, id, TRACE_EVENT_FLAG_COPY, \
    525         arg1_name, arg1_val)
    526 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \
    527         arg2_name, arg2_val) \
    528     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    529         category, name, id, TRACE_EVENT_FLAG_COPY, \
    530         arg1_name, arg1_val, arg2_name, arg2_val)
    531 
    532 
    533 ////////////////////////////////////////////////////////////////////////////////
    534 // Implementation specific tracing API definitions.
    535 
    536 // Get a pointer to the enabled state of the given trace category. Only
    537 // long-lived literal strings should be given as the category name. The returned
    538 // pointer can be held permanently in a local static for example. If the
    539 // unsigned char is non-zero, tracing is enabled. If tracing is enabled,
    540 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
    541 // between the load of the tracing state and the call to
    542 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
    543 // for best performance when tracing is disabled.
    544 // const unsigned char*
    545 //     TRACE_EVENT_API_GET_CATEGORY_ENABLED(const char* category_name)
    546 #define TRACE_EVENT_API_GET_CATEGORY_ENABLED \
    547     webrtc::EventTracer::GetCategoryEnabled
    548 
    549 // Add a trace event to the platform tracing system.
    550 // void TRACE_EVENT_API_ADD_TRACE_EVENT(
    551 //                    char phase,
    552 //                    const unsigned char* category_enabled,
    553 //                    const char* name,
    554 //                    unsigned long long id,
    555 //                    int num_args,
    556 //                    const char** arg_names,
    557 //                    const unsigned char* arg_types,
    558 //                    const unsigned long long* arg_values,
    559 //                    unsigned char flags)
    560 #define TRACE_EVENT_API_ADD_TRACE_EVENT webrtc::EventTracer::AddTraceEvent
    561 
    562 ////////////////////////////////////////////////////////////////////////////////
    563 
    564 // Implementation detail: trace event macros create temporary variables
    565 // to keep instrumentation overhead low. These macros give each temporary
    566 // variable a unique name based on the line number to prevent name collissions.
    567 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
    568     trace_event_unique_##a##b
    569 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
    570     INTERNAL_TRACE_EVENT_UID3(a,b)
    571 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
    572     INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
    573 
    574 // Implementation detail: internal macro to create static category.
    575 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \
    576     static const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = 0; \
    577     if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    578       INTERNAL_TRACE_EVENT_UID(catstatic) = \
    579           TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \
    580     }
    581 
    582 // Implementation detail: internal macro to create static category and add
    583 // event if the category is enabled.
    584 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \
    585     do { \
    586       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
    587       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    588         webrtc::trace_event_internal::AddTraceEvent(          \
    589             phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
    590             webrtc::trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
    591       } \
    592     } while (0)
    593 
    594 // Implementation detail: internal macro to create static category and add begin
    595 // event if the category is enabled. Also adds the end event when the scope
    596 // ends.
    597 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \
    598     INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
    599     webrtc::trace_event_internal::TraceEndOnScopeClose  \
    600         INTERNAL_TRACE_EVENT_UID(profileScope); \
    601     if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    602       webrtc::trace_event_internal::AddTraceEvent(      \
    603           TRACE_EVENT_PHASE_BEGIN, \
    604           INTERNAL_TRACE_EVENT_UID(catstatic), \
    605           name, webrtc::trace_event_internal::kNoEventId,       \
    606           TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
    607       INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
    608           INTERNAL_TRACE_EVENT_UID(catstatic), name); \
    609     }
    610 
    611 // Implementation detail: internal macro to create static category and add
    612 // event if the category is enabled.
    613 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \
    614                                          ...) \
    615     do { \
    616       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \
    617       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    618         unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
    619         webrtc::trace_event_internal::TraceID trace_event_trace_id( \
    620             id, &trace_event_flags); \
    621         webrtc::trace_event_internal::AddTraceEvent( \
    622             phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
    623             name, trace_event_trace_id.data(), trace_event_flags, \
    624             ##__VA_ARGS__); \
    625       } \
    626     } while (0)
    627 
    628 // Notes regarding the following definitions:
    629 // New values can be added and propagated to third party libraries, but existing
    630 // definitions must never be changed, because third party libraries may use old
    631 // definitions.
    632 
    633 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
    634 #define TRACE_EVENT_PHASE_BEGIN    ('B')
    635 #define TRACE_EVENT_PHASE_END      ('E')
    636 #define TRACE_EVENT_PHASE_INSTANT  ('I')
    637 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
    638 #define TRACE_EVENT_PHASE_ASYNC_STEP  ('T')
    639 #define TRACE_EVENT_PHASE_ASYNC_END   ('F')
    640 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
    641 #define TRACE_EVENT_PHASE_FLOW_STEP  ('t')
    642 #define TRACE_EVENT_PHASE_FLOW_END   ('f')
    643 #define TRACE_EVENT_PHASE_METADATA ('M')
    644 #define TRACE_EVENT_PHASE_COUNTER  ('C')
    645 
    646 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
    647 #define TRACE_EVENT_FLAG_NONE        (static_cast<unsigned char>(0))
    648 #define TRACE_EVENT_FLAG_COPY        (static_cast<unsigned char>(1 << 0))
    649 #define TRACE_EVENT_FLAG_HAS_ID      (static_cast<unsigned char>(1 << 1))
    650 #define TRACE_EVENT_FLAG_MANGLE_ID   (static_cast<unsigned char>(1 << 2))
    651 
    652 // Type values for identifying types in the TraceValue union.
    653 #define TRACE_VALUE_TYPE_BOOL         (static_cast<unsigned char>(1))
    654 #define TRACE_VALUE_TYPE_UINT         (static_cast<unsigned char>(2))
    655 #define TRACE_VALUE_TYPE_INT          (static_cast<unsigned char>(3))
    656 #define TRACE_VALUE_TYPE_DOUBLE       (static_cast<unsigned char>(4))
    657 #define TRACE_VALUE_TYPE_POINTER      (static_cast<unsigned char>(5))
    658 #define TRACE_VALUE_TYPE_STRING       (static_cast<unsigned char>(6))
    659 #define TRACE_VALUE_TYPE_COPY_STRING  (static_cast<unsigned char>(7))
    660 
    661 namespace webrtc {
    662 namespace trace_event_internal {
    663 
    664 // Specify these values when the corresponding argument of AddTraceEvent is not
    665 // used.
    666 const int kZeroNumArgs = 0;
    667 const unsigned long long kNoEventId = 0;
    668 
    669 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
    670 // are mangled with the Process ID so that they are unlikely to collide when the
    671 // same pointer is used on different processes.
    672 class TraceID {
    673  public:
    674   class ForceMangle {
    675     public:
    676      explicit ForceMangle(unsigned long long id) : data_(id) {}
    677      explicit ForceMangle(unsigned long id) : data_(id) {}
    678      explicit ForceMangle(unsigned int id) : data_(id) {}
    679      explicit ForceMangle(unsigned short id) : data_(id) {}
    680      explicit ForceMangle(unsigned char id) : data_(id) {}
    681      explicit ForceMangle(long long id)
    682          : data_(static_cast<unsigned long long>(id)) {}
    683      explicit ForceMangle(long id)
    684          : data_(static_cast<unsigned long long>(id)) {}
    685      explicit ForceMangle(int id)
    686          : data_(static_cast<unsigned long long>(id)) {}
    687      explicit ForceMangle(short id)
    688          : data_(static_cast<unsigned long long>(id)) {}
    689      explicit ForceMangle(signed char id)
    690          : data_(static_cast<unsigned long long>(id)) {}
    691 
    692      unsigned long long data() const { return data_; }
    693 
    694     private:
    695      unsigned long long data_;
    696   };
    697 
    698   explicit TraceID(const void* id, unsigned char* flags)
    699       : data_(static_cast<unsigned long long>(
    700               reinterpret_cast<unsigned long>(id))) {
    701     *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
    702   }
    703   explicit TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
    704     *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
    705   }
    706   explicit TraceID(unsigned long long id, unsigned char* flags)
    707       : data_(id) { (void)flags; }
    708   explicit TraceID(unsigned long id, unsigned char* flags)
    709       : data_(id) { (void)flags; }
    710   explicit TraceID(unsigned int id, unsigned char* flags)
    711       : data_(id) { (void)flags; }
    712   explicit TraceID(unsigned short id, unsigned char* flags)
    713       : data_(id) { (void)flags; }
    714   explicit TraceID(unsigned char id, unsigned char* flags)
    715       : data_(id) { (void)flags; }
    716   explicit TraceID(long long id, unsigned char* flags)
    717       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
    718   explicit TraceID(long id, unsigned char* flags)
    719       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
    720   explicit TraceID(int id, unsigned char* flags)
    721       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
    722   explicit TraceID(short id, unsigned char* flags)
    723       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
    724   explicit TraceID(signed char id, unsigned char* flags)
    725       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
    726 
    727   unsigned long long data() const { return data_; }
    728 
    729  private:
    730   unsigned long long data_;
    731 };
    732 
    733 // Simple union to store various types as unsigned long long.
    734 union TraceValueUnion {
    735   bool as_bool;
    736   unsigned long long as_uint;
    737   long long as_int;
    738   double as_double;
    739   const void* as_pointer;
    740   const char* as_string;
    741 };
    742 
    743 // Simple container for const char* that should be copied instead of retained.
    744 class TraceStringWithCopy {
    745  public:
    746   explicit TraceStringWithCopy(const char* str) : str_(str) {}
    747   operator const char* () const { return str_; }
    748  private:
    749   const char* str_;
    750 };
    751 
    752 // Define SetTraceValue for each allowed type. It stores the type and
    753 // value in the return arguments. This allows this API to avoid declaring any
    754 // structures so that it is portable to third_party libraries.
    755 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
    756                                          union_member, \
    757                                          value_type_id) \
    758     static inline void SetTraceValue(actual_type arg, \
    759                                      unsigned char* type, \
    760                                      unsigned long long* value) { \
    761       TraceValueUnion type_value; \
    762       type_value.union_member = arg; \
    763       *type = value_type_id; \
    764       *value = type_value.as_uint; \
    765     }
    766 // Simpler form for int types that can be safely casted.
    767 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
    768                                              value_type_id) \
    769     static inline void SetTraceValue(actual_type arg, \
    770                                      unsigned char* type, \
    771                                      unsigned long long* value) { \
    772       *type = value_type_id; \
    773       *value = static_cast<unsigned long long>(arg); \
    774     }
    775 
    776 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
    777 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
    778 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
    779 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
    780 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
    781 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
    782 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
    783 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
    784 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
    785 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
    786 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
    787 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
    788 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
    789                                  TRACE_VALUE_TYPE_POINTER)
    790 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
    791                                  TRACE_VALUE_TYPE_STRING)
    792 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
    793                                  TRACE_VALUE_TYPE_COPY_STRING)
    794 
    795 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
    796 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
    797 
    798 // std::string version of SetTraceValue so that trace arguments can be strings.
    799 static inline void SetTraceValue(const std::string& arg,
    800                                  unsigned char* type,
    801                                  unsigned long long* value) {
    802   TraceValueUnion type_value;
    803   type_value.as_string = arg.c_str();
    804   *type = TRACE_VALUE_TYPE_COPY_STRING;
    805   *value = type_value.as_uint;
    806 }
    807 
    808 // These AddTraceEvent template functions are defined here instead of in the
    809 // macro, because the arg_values could be temporary objects, such as
    810 // std::string. In order to store pointers to the internal c_str and pass
    811 // through to the tracing API, the arg_values must live throughout
    812 // these procedures.
    813 
    814 static inline void AddTraceEvent(char phase,
    815                                 const unsigned char* category_enabled,
    816                                 const char* name,
    817                                 unsigned long long id,
    818                                 unsigned char flags) {
    819   TRACE_EVENT_API_ADD_TRACE_EVENT(
    820       phase, category_enabled, name, id,
    821       kZeroNumArgs, NULL, NULL, NULL,
    822       flags);
    823 }
    824 
    825 template<class ARG1_TYPE>
    826 static inline void AddTraceEvent(char phase,
    827                                 const unsigned char* category_enabled,
    828                                 const char* name,
    829                                 unsigned long long id,
    830                                 unsigned char flags,
    831                                 const char* arg1_name,
    832                                 const ARG1_TYPE& arg1_val) {
    833   const int num_args = 1;
    834   unsigned char arg_types[1];
    835   unsigned long long arg_values[1];
    836   SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
    837   TRACE_EVENT_API_ADD_TRACE_EVENT(
    838       phase, category_enabled, name, id,
    839       num_args, &arg1_name, arg_types, arg_values,
    840       flags);
    841 }
    842 
    843 template<class ARG1_TYPE, class ARG2_TYPE>
    844 static inline void AddTraceEvent(char phase,
    845                                 const unsigned char* category_enabled,
    846                                 const char* name,
    847                                 unsigned long long id,
    848                                 unsigned char flags,
    849                                 const char* arg1_name,
    850                                 const ARG1_TYPE& arg1_val,
    851                                 const char* arg2_name,
    852                                 const ARG2_TYPE& arg2_val) {
    853   const int num_args = 2;
    854   const char* arg_names[2] = { arg1_name, arg2_name };
    855   unsigned char arg_types[2];
    856   unsigned long long arg_values[2];
    857   SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
    858   SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
    859   TRACE_EVENT_API_ADD_TRACE_EVENT(
    860       phase, category_enabled, name, id,
    861       num_args, arg_names, arg_types, arg_values,
    862       flags);
    863 }
    864 
    865 // Used by TRACE_EVENTx macro. Do not use directly.
    866 class TraceEndOnScopeClose {
    867  public:
    868   // Note: members of data_ intentionally left uninitialized. See Initialize.
    869   TraceEndOnScopeClose() : p_data_(NULL) {}
    870   ~TraceEndOnScopeClose() {
    871     if (p_data_)
    872       AddEventIfEnabled();
    873   }
    874 
    875   void Initialize(const unsigned char* category_enabled,
    876                   const char* name) {
    877     data_.category_enabled = category_enabled;
    878     data_.name = name;
    879     p_data_ = &data_;
    880   }
    881 
    882  private:
    883   // Add the end event if the category is still enabled.
    884   void AddEventIfEnabled() {
    885     // Only called when p_data_ is non-null.
    886     if (*p_data_->category_enabled) {
    887       TRACE_EVENT_API_ADD_TRACE_EVENT(
    888           TRACE_EVENT_PHASE_END,
    889           p_data_->category_enabled,
    890           p_data_->name, kNoEventId,
    891           kZeroNumArgs, NULL, NULL, NULL,
    892           TRACE_EVENT_FLAG_NONE);
    893     }
    894   }
    895 
    896   // This Data struct workaround is to avoid initializing all the members
    897   // in Data during construction of this object, since this object is always
    898   // constructed, even when tracing is disabled. If the members of Data were
    899   // members of this class instead, compiler warnings occur about potential
    900   // uninitialized accesses.
    901   struct Data {
    902     const unsigned char* category_enabled;
    903     const char* name;
    904   };
    905   Data* p_data_;
    906   Data data_;
    907 };
    908 
    909 }  // namespace trace_event_internal
    910 }  // namespace webrtc
    911 
    912 #endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TRACE_EVENT_H_
    913