You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

886 lines
42 KiB

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