trace_event.h 49 KB

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