trace_event.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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.
  4. #ifndef BASE_TRACE_EVENT_TRACE_EVENT_H_
  5. #define BASE_TRACE_EVENT_TRACE_EVENT_H_
  6. // This header file defines implementation details of how the trace macros in
  7. // trace_event_common.h collect and store trace events. Anything not
  8. // implementation-specific should go in trace_event_common.h instead of here.
  9. #include <stddef.h>
  10. #include <stdint.h>
  11. #include <string>
  12. #include "base/atomicops.h"
  13. #include "base/debug/debugging_buildflags.h"
  14. #include "base/macros.h"
  15. #include "base/time/time.h"
  16. #include "base/time/time_override.h"
  17. #include "base/trace_event/builtin_categories.h"
  18. #include "base/trace_event/common/trace_event_common.h"
  19. #include "base/trace_event/heap_profiler.h"
  20. #include "base/trace_event/log_message.h"
  21. #include "base/trace_event/thread_instruction_count.h"
  22. #include "base/trace_event/trace_arguments.h"
  23. #include "base/trace_event/trace_category.h"
  24. #include "base/trace_event/trace_log.h"
  25. #include "build/build_config.h"
  26. // By default, const char* argument values are assumed to have long-lived scope
  27. // and will not be copied. Use this macro to force a const char* to be copied.
  28. #define TRACE_STR_COPY(str) ::base::trace_event::TraceStringWithCopy(str)
  29. // By default, trace IDs are eventually converted to a single 64-bit number. Use
  30. // this macro to add a scope string. For example,
  31. //
  32. // TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
  33. // "network", "ResourceLoad",
  34. // TRACE_ID_WITH_SCOPE("BlinkResourceID", resourceID));
  35. //
  36. // Also, it is possible to prepend the ID with another number, like the process
  37. // ID. This is useful in creating IDs that are unique among all processes. To do
  38. // that, pass two numbers after the scope string instead of one. For example,
  39. //
  40. // TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
  41. // "network", "ResourceLoad",
  42. // TRACE_ID_WITH_SCOPE("BlinkResourceID", pid, resourceID));
  43. #define TRACE_ID_WITH_SCOPE(scope, ...) \
  44. trace_event_internal::TraceID::WithScope(scope, ##__VA_ARGS__)
  45. // Use this for ids that are unique across processes. This allows different
  46. // processes to use the same id to refer to the same event.
  47. #define TRACE_ID_GLOBAL(id) trace_event_internal::TraceID::GlobalId(id)
  48. // Use this for ids that are unique within a single process. This allows
  49. // different processes to use the same id to refer to different events.
  50. #define TRACE_ID_LOCAL(id) trace_event_internal::TraceID::LocalId(id)
  51. #define TRACE_EVENT_API_CURRENT_THREAD_ID \
  52. static_cast<int>(base::PlatformThread::CurrentId())
  53. #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED_FOR_RECORDING_MODE() \
  54. UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
  55. (base::trace_event::TraceCategory::ENABLED_FOR_RECORDING | \
  56. base::trace_event::TraceCategory::ENABLED_FOR_ETW_EXPORT))
  57. #define INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED() \
  58. UNLIKELY(*INTERNAL_TRACE_EVENT_UID(category_group_enabled) & \
  59. (base::trace_event::TraceCategory::ENABLED_FOR_RECORDING | \
  60. base::trace_event::TraceCategory::ENABLED_FOR_ETW_EXPORT | \
  61. base::trace_event::TraceCategory::ENABLED_FOR_FILTERING))
  62. ////////////////////////////////////////////////////////////////////////////////
  63. // Implementation specific tracing API definitions.
  64. // Get a pointer to the enabled state of the given trace category. Only
  65. // long-lived literal strings should be given as the category group. The
  66. // returned pointer can be held permanently in a local static for example. If
  67. // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
  68. // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
  69. // between the load of the tracing state and the call to
  70. // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
  71. // for best performance when tracing is disabled.
  72. // const unsigned char*
  73. // TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
  74. #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
  75. base::trace_event::TraceLog::GetCategoryGroupEnabled
  76. // Get the number of times traces have been recorded. This is used to implement
  77. // the TRACE_EVENT_IS_NEW_TRACE facility.
  78. // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
  79. #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
  80. trace_event_internal::GetNumTracesRecorded
  81. // Add a trace event to the platform tracing system.
  82. // base::trace_event::TraceEventHandle TRACE_EVENT_API_ADD_TRACE_EVENT(
  83. // char phase,
  84. // const unsigned char* category_group_enabled,
  85. // const char* name,
  86. // const char* scope,
  87. // unsigned long long id,
  88. // base::trace_event::TraceArguments* args,
  89. // unsigned int flags)
  90. #define TRACE_EVENT_API_ADD_TRACE_EVENT trace_event_internal::AddTraceEvent
  91. // Add a trace event to the platform tracing system.
  92. // base::trace_event::TraceEventHandle
  93. // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_BIND_ID(
  94. // char phase,
  95. // const unsigned char* category_group_enabled,
  96. // const char* name,
  97. // const char* scope,
  98. // unsigned long long id,
  99. // unsigned long long bind_id,
  100. // base::trace_event::TraceArguments* args,
  101. // unsigned int flags)
  102. #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_BIND_ID \
  103. trace_event_internal::AddTraceEventWithBindId
  104. // Add a trace event to the platform tracing system overriding the pid.
  105. // The resulting event will have tid = pid == (process_id passed here).
  106. // base::trace_event::TraceEventHandle
  107. // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID(
  108. // char phase,
  109. // const unsigned char* category_group_enabled,
  110. // const char* name,
  111. // const char* scope,
  112. // unsigned long long id,
  113. // int process_id,
  114. // base::trace_event::TraceArguments* args,
  115. // unsigned int flags)
  116. #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_PROCESS_ID \
  117. trace_event_internal::AddTraceEventWithProcessId
  118. // Add a trace event to the platform tracing system.
  119. // base::trace_event::TraceEventHandle
  120. // TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
  121. // char phase,
  122. // const unsigned char* category_group_enabled,
  123. // const char* name,
  124. // const char* scope,
  125. // unsigned long long id,
  126. // int thread_id,
  127. // const TimeTicks& timestamp,
  128. // base::trace_event::TraceArguments* args,
  129. // unsigned int flags)
  130. #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
  131. trace_event_internal::AddTraceEventWithThreadIdAndTimestamp
  132. // Set the duration field of a COMPLETE trace event.
  133. // void TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(
  134. // const unsigned char* category_group_enabled,
  135. // const char* name,
  136. // base::trace_event::TraceEventHandle id)
  137. #define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
  138. trace_event_internal::UpdateTraceEventDuration
  139. // Adds a metadata event to the trace log. The |AppendValueAsTraceFormat| method
  140. // on the convertable value will be called at flush time.
  141. // TRACE_EVENT_API_ADD_METADATA_EVENT(
  142. // const unsigned char* category_group_enabled,
  143. // const char* event_name,
  144. // const char* arg_name,
  145. // std::unique_ptr<ConvertableToTraceFormat> arg_value)
  146. #define TRACE_EVENT_API_ADD_METADATA_EVENT \
  147. trace_event_internal::AddMetadataEvent
  148. // Defines atomic operations used internally by the tracing system.
  149. #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
  150. #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
  151. #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
  152. base::subtle::NoBarrier_Store(&(var), (value))
  153. // Defines visibility for classes in trace_event.h
  154. #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
  155. ////////////////////////////////////////////////////////////////////////////////
  156. // Implementation detail: trace event macros create temporary variables
  157. // to keep instrumentation overhead low. These macros give each temporary
  158. // variable a unique name based on the line number to prevent name collisions.
  159. #define INTERNAL_TRACE_EVENT_UID3(a,b) \
  160. trace_event_unique_##a##b
  161. #define INTERNAL_TRACE_EVENT_UID2(a,b) \
  162. INTERNAL_TRACE_EVENT_UID3(a,b)
  163. #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
  164. INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
  165. // Implementation detail: internal macro to create static category.
  166. // No barriers are needed, because this code is designed to operate safely
  167. // even when the unsigned char* points to garbage data (which may be the case
  168. // on processors without cache coherency).
  169. #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
  170. category_group, atomic, category_group_enabled) \
  171. category_group_enabled = reinterpret_cast<const unsigned char*>( \
  172. TRACE_EVENT_API_ATOMIC_LOAD(atomic)); \
  173. if (UNLIKELY(!category_group_enabled)) { \
  174. category_group_enabled = \
  175. TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
  176. TRACE_EVENT_API_ATOMIC_STORE( \
  177. atomic, reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
  178. category_group_enabled)); \
  179. }
  180. #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_MAYBE_AT_COMPILE_TIME( \
  181. category_group, k_category_group_enabled, category_group_enabled) \
  182. if (k_category_group_enabled) { \
  183. category_group_enabled = k_category_group_enabled; \
  184. } else { \
  185. static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
  186. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_CUSTOM_VARIABLES( \
  187. category_group, INTERNAL_TRACE_EVENT_UID(atomic), \
  188. category_group_enabled); \
  189. }
  190. #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
  191. static_assert( \
  192. base::trace_event::BuiltinCategories::IsAllowedCategory(category_group), \
  193. "Unknown tracing category is used. Please register your " \
  194. "category in base/trace_event/builtin_categories.h"); \
  195. constexpr const unsigned char* INTERNAL_TRACE_EVENT_UID( \
  196. k_category_group_enabled) = \
  197. base::trace_event::TraceLog::GetBuiltinCategoryEnabled(category_group); \
  198. const unsigned char* INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
  199. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO_MAYBE_AT_COMPILE_TIME( \
  200. category_group, INTERNAL_TRACE_EVENT_UID(k_category_group_enabled), \
  201. INTERNAL_TRACE_EVENT_UID(category_group_enabled));
  202. // Implementation detail: internal macro to return unoverridden
  203. // base::TimeTicks::Now(). This is important because in headless VirtualTime can
  204. // override base:TimeTicks::Now().
  205. #define INTERNAL_TRACE_TIME_TICKS_NOW() \
  206. base::subtle::TimeTicksNowIgnoringOverride()
  207. // Implementation detail: internal macro to return unoverridden
  208. // base::Time::Now(). This is important because in headless VirtualTime can
  209. // override base:TimeTicks::Now().
  210. #define INTERNAL_TRACE_TIME_NOW() base::subtle::TimeNowIgnoringOverride()
  211. // Implementation detail: internal macro to create static category and add
  212. // event if the category is enabled.
  213. #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
  214. do { \
  215. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  216. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  217. trace_event_internal::AddTraceEvent( \
  218. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  219. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  220. flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
  221. } \
  222. } while (0)
  223. // Implementation detail: internal macro to create static category and add begin
  224. // event if the category is enabled. Also adds the end event when the scope
  225. // ends.
  226. #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
  227. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  228. trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  229. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  230. base::trace_event::TraceEventHandle INTERNAL_TRACE_EVENT_UID(h) = \
  231. trace_event_internal::AddTraceEvent( \
  232. TRACE_EVENT_PHASE_COMPLETE, \
  233. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  234. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  235. TRACE_EVENT_FLAG_NONE, trace_event_internal::kNoId, \
  236. ##__VA_ARGS__); \
  237. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  238. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  239. INTERNAL_TRACE_EVENT_UID(h)); \
  240. }
  241. #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS(category_group, name, \
  242. flags, ...) \
  243. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  244. trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  245. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  246. base::trace_event::TraceEventHandle INTERNAL_TRACE_EVENT_UID(h) = \
  247. trace_event_internal::AddTraceEvent( \
  248. TRACE_EVENT_PHASE_COMPLETE, \
  249. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  250. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  251. flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
  252. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  253. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  254. INTERNAL_TRACE_EVENT_UID(h)); \
  255. }
  256. #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, \
  257. bind_id, flow_flags, ...) \
  258. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  259. trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  260. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  261. trace_event_internal::TraceID trace_event_bind_id((bind_id)); \
  262. unsigned int trace_event_flags = \
  263. flow_flags | trace_event_bind_id.id_flags(); \
  264. base::trace_event::TraceEventHandle INTERNAL_TRACE_EVENT_UID(h) = \
  265. trace_event_internal::AddTraceEvent( \
  266. TRACE_EVENT_PHASE_COMPLETE, \
  267. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  268. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  269. trace_event_flags, trace_event_bind_id.raw_id(), ##__VA_ARGS__); \
  270. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  271. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  272. INTERNAL_TRACE_EVENT_UID(h)); \
  273. }
  274. // Implementation detail: internal macro to create static category and add
  275. // event if the category is enabled.
  276. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
  277. flags, ...) \
  278. do { \
  279. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  280. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  281. trace_event_internal::TraceID trace_event_trace_id((id)); \
  282. unsigned int trace_event_flags = \
  283. flags | trace_event_trace_id.id_flags(); \
  284. trace_event_internal::AddTraceEvent( \
  285. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  286. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  287. trace_event_flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
  288. } \
  289. } while (0)
  290. // Implementation detail: internal macro to create static category and add
  291. // event if the category is enabled.
  292. #define INTERNAL_TRACE_EVENT_ADD_WITH_TIMESTAMP(phase, category_group, name, \
  293. timestamp, flags, ...) \
  294. do { \
  295. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  296. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  297. trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
  298. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  299. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  300. TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, \
  301. flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
  302. trace_event_internal::kNoId, ##__VA_ARGS__); \
  303. } \
  304. } while (0)
  305. // Implementation detail: internal macro to create static category and add
  306. // event if the category is enabled.
  307. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
  308. phase, category_group, name, id, thread_id, timestamp, flags, ...) \
  309. do { \
  310. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  311. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  312. trace_event_internal::TraceID trace_event_trace_id((id)); \
  313. unsigned int trace_event_flags = \
  314. flags | trace_event_trace_id.id_flags(); \
  315. trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
  316. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  317. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  318. thread_id, timestamp, \
  319. trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
  320. trace_event_internal::kNoId, ##__VA_ARGS__); \
  321. } \
  322. } while (0)
  323. // Implementation detail: internal macro to create static category and add
  324. // event if the category is enabled.
  325. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMPS( \
  326. phase, category_group, name, id, thread_id, timestamp, thread_timestamp, \
  327. flags) \
  328. do { \
  329. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  330. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  331. trace_event_internal::TraceID trace_event_trace_id((id)); \
  332. unsigned int trace_event_flags = \
  333. flags | trace_event_trace_id.id_flags(); \
  334. const unsigned char* uid_category_group_enabled = \
  335. INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
  336. trace_event_internal::AddTraceEventWithThreadIdAndTimestamps( \
  337. phase, uid_category_group_enabled, name, \
  338. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  339. thread_id, timestamp, thread_timestamp, \
  340. trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP); \
  341. } \
  342. } while (0)
  343. // Implementation detail: internal macro to create static category and add
  344. // metadata event if the category is enabled.
  345. #define INTERNAL_TRACE_EVENT_METADATA_ADD(category_group, name, ...) \
  346. do { \
  347. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  348. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  349. TRACE_EVENT_API_ADD_METADATA_EVENT( \
  350. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  351. ##__VA_ARGS__); \
  352. } \
  353. } while (0)
  354. #define INTERNAL_TRACE_LOG_MESSAGE(file, message, line) \
  355. TRACE_EVENT_INSTANT1( \
  356. "log", "LogMessage", \
  357. TRACE_EVENT_FLAG_TYPED_PROTO_ARGS | TRACE_EVENT_SCOPE_THREAD, "message", \
  358. std::make_unique<base::trace_event::LogMessage>(file, message, line))
  359. #if BUILDFLAG(ENABLE_LOCATION_SOURCE)
  360. // Implementation detail: internal macro to trace a task execution with the
  361. // location where it was posted from.
  362. //
  363. // This implementation is for when location sources are available.
  364. // TODO(ssid): The program counter of the current task should be added here.
  365. #define INTERNAL_TRACE_TASK_EXECUTION(run_function, task) \
  366. INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS( \
  367. "toplevel", run_function, TRACE_EVENT_FLAG_TYPED_PROTO_ARGS, "src_file", \
  368. (task).posted_from.file_name(), "src_func", \
  369. (task).posted_from.function_name()); \
  370. TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION INTERNAL_TRACE_EVENT_UID( \
  371. task_event)((task).posted_from.file_name()); \
  372. TRACE_HEAP_PROFILER_API_SCOPED_WITH_PROGRAM_COUNTER \
  373. INTERNAL_TRACE_EVENT_UID(task_pc_event)((task).posted_from.program_counter());
  374. #else
  375. // TODO(http://crbug.com760702) remove file name and just pass the program
  376. // counter to the heap profiler macro.
  377. // TODO(ssid): The program counter of the current task should be added here.
  378. #define INTERNAL_TRACE_TASK_EXECUTION(run_function, task) \
  379. INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS( \
  380. "toplevel", run_function, TRACE_EVENT_FLAG_TYPED_PROTO_ARGS, "src", \
  381. (task).posted_from.ToString()) \
  382. TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION INTERNAL_TRACE_EVENT_UID( \
  383. task_event)((task).posted_from.file_name()); \
  384. TRACE_HEAP_PROFILER_API_SCOPED_WITH_PROGRAM_COUNTER \
  385. INTERNAL_TRACE_EVENT_UID(task_pc_event)((task).posted_from.program_counter());
  386. #endif
  387. namespace trace_event_internal {
  388. // Specify these values when the corresponding argument of AddTraceEvent is not
  389. // used.
  390. const int kZeroNumArgs = 0;
  391. const std::nullptr_t kGlobalScope = nullptr;
  392. const unsigned long long kNoId = 0;
  393. // TraceID encapsulates an ID that can either be an integer or pointer.
  394. class BASE_EXPORT TraceID {
  395. public:
  396. // Can be combined with WithScope.
  397. class LocalId {
  398. public:
  399. explicit LocalId(const void* raw_id)
  400. : raw_id_(static_cast<unsigned long long>(
  401. reinterpret_cast<uintptr_t>(raw_id))) {}
  402. explicit LocalId(unsigned long long raw_id) : raw_id_(raw_id) {}
  403. unsigned long long raw_id() const { return raw_id_; }
  404. private:
  405. unsigned long long raw_id_;
  406. };
  407. // Can be combined with WithScope.
  408. class GlobalId {
  409. public:
  410. explicit GlobalId(unsigned long long raw_id) : raw_id_(raw_id) {}
  411. unsigned long long raw_id() const { return raw_id_; }
  412. private:
  413. unsigned long long raw_id_;
  414. };
  415. class WithScope {
  416. public:
  417. WithScope(const char* scope, unsigned long long raw_id)
  418. : scope_(scope), raw_id_(raw_id) {}
  419. WithScope(const char* scope, LocalId local_id)
  420. : scope_(scope), raw_id_(local_id.raw_id()) {
  421. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  422. }
  423. WithScope(const char* scope, GlobalId global_id)
  424. : scope_(scope), raw_id_(global_id.raw_id()) {
  425. id_flags_ = TRACE_EVENT_FLAG_HAS_GLOBAL_ID;
  426. }
  427. unsigned long long raw_id() const { return raw_id_; }
  428. const char* scope() const { return scope_; }
  429. unsigned int id_flags() const { return id_flags_; }
  430. private:
  431. const char* scope_ = nullptr;
  432. unsigned long long raw_id_;
  433. unsigned int id_flags_ = TRACE_EVENT_FLAG_HAS_ID;
  434. };
  435. TraceID(const void* raw_id) : raw_id_(static_cast<unsigned long long>(
  436. reinterpret_cast<uintptr_t>(raw_id))) {
  437. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  438. }
  439. TraceID(unsigned long long raw_id) : raw_id_(raw_id) {}
  440. TraceID(unsigned long raw_id) : raw_id_(raw_id) {}
  441. TraceID(unsigned int raw_id) : raw_id_(raw_id) {}
  442. TraceID(unsigned short raw_id) : raw_id_(raw_id) {}
  443. TraceID(unsigned char raw_id) : raw_id_(raw_id) {}
  444. TraceID(long long raw_id)
  445. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  446. TraceID(long raw_id)
  447. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  448. TraceID(int raw_id)
  449. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  450. TraceID(short raw_id)
  451. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  452. TraceID(signed char raw_id)
  453. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  454. TraceID(LocalId raw_id) : raw_id_(raw_id.raw_id()) {
  455. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  456. }
  457. TraceID(GlobalId raw_id) : raw_id_(raw_id.raw_id()) {
  458. id_flags_ = TRACE_EVENT_FLAG_HAS_GLOBAL_ID;
  459. }
  460. TraceID(WithScope scoped_id)
  461. : scope_(scoped_id.scope()),
  462. raw_id_(scoped_id.raw_id()),
  463. id_flags_(scoped_id.id_flags()) {}
  464. unsigned long long raw_id() const { return raw_id_; }
  465. const char* scope() const { return scope_; }
  466. unsigned int id_flags() const { return id_flags_; }
  467. private:
  468. const char* scope_ = nullptr;
  469. unsigned long long raw_id_;
  470. unsigned int id_flags_ = TRACE_EVENT_FLAG_HAS_ID;
  471. };
  472. // These functions all internally call
  473. // base::trace_event::TraceLog::GetInstance() then call the method with the same
  474. // name on it. This is used to reduce the generated machine code at each
  475. // TRACE_EVENTXXX macro call.
  476. base::trace_event::TraceEventHandle BASE_EXPORT
  477. AddTraceEvent(char phase,
  478. const unsigned char* category_group_enabled,
  479. const char* name,
  480. const char* scope,
  481. unsigned long long id,
  482. base::trace_event::TraceArguments* args,
  483. unsigned int flags);
  484. base::trace_event::TraceEventHandle BASE_EXPORT
  485. AddTraceEventWithBindId(char phase,
  486. const unsigned char* category_group_enabled,
  487. const char* name,
  488. const char* scope,
  489. unsigned long long id,
  490. unsigned long long bind_id,
  491. base::trace_event::TraceArguments* args,
  492. unsigned int flags);
  493. base::trace_event::TraceEventHandle BASE_EXPORT
  494. AddTraceEventWithProcessId(char phase,
  495. const unsigned char* category_group_enabled,
  496. const char* name,
  497. const char* scope,
  498. unsigned long long id,
  499. int process_id,
  500. base::trace_event::TraceArguments* args,
  501. unsigned int flags);
  502. base::trace_event::TraceEventHandle BASE_EXPORT
  503. AddTraceEventWithThreadIdAndTimestamp(
  504. char phase,
  505. const unsigned char* category_group_enabled,
  506. const char* name,
  507. const char* scope,
  508. unsigned long long id,
  509. int thread_id,
  510. const base::TimeTicks& timestamp,
  511. base::trace_event::TraceArguments* args,
  512. unsigned int flags);
  513. base::trace_event::TraceEventHandle BASE_EXPORT
  514. AddTraceEventWithThreadIdAndTimestamp(
  515. char phase,
  516. const unsigned char* category_group_enabled,
  517. const char* name,
  518. const char* scope,
  519. unsigned long long id,
  520. unsigned long long bind_id,
  521. int thread_id,
  522. const base::TimeTicks& timestamp,
  523. base::trace_event::TraceArguments* args,
  524. unsigned int flags);
  525. base::trace_event::TraceEventHandle BASE_EXPORT
  526. AddTraceEventWithThreadIdAndTimestamps(
  527. char phase,
  528. const unsigned char* category_group_enabled,
  529. const char* name,
  530. const char* scope,
  531. unsigned long long id,
  532. int thread_id,
  533. const base::TimeTicks& timestamp,
  534. const base::ThreadTicks& thread_timestamp,
  535. unsigned int flags);
  536. void BASE_EXPORT AddMetadataEvent(const unsigned char* category_group_enabled,
  537. const char* name,
  538. base::trace_event::TraceArguments* args,
  539. unsigned int flags);
  540. int BASE_EXPORT GetNumTracesRecorded();
  541. void BASE_EXPORT
  542. UpdateTraceEventDuration(const unsigned char* category_group_enabled,
  543. const char* name,
  544. base::trace_event::TraceEventHandle handle);
  545. void BASE_EXPORT UpdateTraceEventDurationExplicit(
  546. const unsigned char* category_group_enabled,
  547. const char* name,
  548. base::trace_event::TraceEventHandle handle,
  549. int thread_id,
  550. bool explicit_timestamps,
  551. const base::TimeTicks& now,
  552. const base::ThreadTicks& thread_now,
  553. base::trace_event::ThreadInstructionCount thread_instruction_now);
  554. // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
  555. // functions are defined here instead of in the macro, because the arg_values
  556. // could be temporary objects, such as std::string. In order to store
  557. // pointers to the internal c_str and pass through to the tracing API,
  558. // the arg_values must live throughout these procedures.
  559. template <class ARG1_TYPE>
  560. static inline base::trace_event::TraceEventHandle
  561. AddTraceEventWithThreadIdAndTimestamp(
  562. char phase,
  563. const unsigned char* category_group_enabled,
  564. const char* name,
  565. const char* scope,
  566. unsigned long long id,
  567. int thread_id,
  568. const base::TimeTicks& timestamp,
  569. unsigned int flags,
  570. unsigned long long bind_id,
  571. const char* arg1_name,
  572. ARG1_TYPE&& arg1_val) {
  573. base::trace_event::TraceArguments args(arg1_name,
  574. std::forward<ARG1_TYPE>(arg1_val));
  575. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  576. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  577. timestamp, &args, flags);
  578. }
  579. template <class ARG1_TYPE, class ARG2_TYPE>
  580. static inline base::trace_event::TraceEventHandle
  581. AddTraceEventWithThreadIdAndTimestamp(
  582. char phase,
  583. const unsigned char* category_group_enabled,
  584. const char* name,
  585. const char* scope,
  586. unsigned long long id,
  587. int thread_id,
  588. const base::TimeTicks& timestamp,
  589. unsigned int flags,
  590. unsigned long long bind_id,
  591. const char* arg1_name,
  592. ARG1_TYPE&& arg1_val,
  593. const char* arg2_name,
  594. ARG2_TYPE&& arg2_val) {
  595. base::trace_event::TraceArguments args(
  596. arg1_name, std::forward<ARG1_TYPE>(arg1_val), arg2_name,
  597. std::forward<ARG2_TYPE>(arg2_val));
  598. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  599. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  600. timestamp, &args, flags);
  601. }
  602. static inline base::trace_event::TraceEventHandle
  603. AddTraceEventWithThreadIdAndTimestamp(
  604. char phase,
  605. const unsigned char* category_group_enabled,
  606. const char* name,
  607. const char* scope,
  608. unsigned long long id,
  609. int thread_id,
  610. const base::TimeTicks& timestamp,
  611. unsigned int flags,
  612. unsigned long long bind_id) {
  613. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  614. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  615. timestamp, nullptr, flags);
  616. }
  617. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  618. char phase,
  619. const unsigned char* category_group_enabled,
  620. const char* name,
  621. const char* scope,
  622. unsigned long long id,
  623. unsigned int flags,
  624. unsigned long long bind_id) {
  625. const int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  626. const base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  627. return AddTraceEventWithThreadIdAndTimestamp(
  628. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  629. bind_id);
  630. }
  631. template <class ARG1_TYPE>
  632. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  633. char phase,
  634. const unsigned char* category_group_enabled,
  635. const char* name,
  636. const char* scope,
  637. unsigned long long id,
  638. unsigned int flags,
  639. unsigned long long bind_id,
  640. const char* arg1_name,
  641. ARG1_TYPE&& arg1_val) {
  642. int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  643. base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  644. return AddTraceEventWithThreadIdAndTimestamp(
  645. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  646. bind_id, arg1_name, std::forward<ARG1_TYPE>(arg1_val));
  647. }
  648. template <class ARG1_TYPE, class ARG2_TYPE>
  649. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  650. char phase,
  651. const unsigned char* category_group_enabled,
  652. const char* name,
  653. const char* scope,
  654. unsigned long long id,
  655. unsigned int flags,
  656. unsigned long long bind_id,
  657. const char* arg1_name,
  658. ARG1_TYPE&& arg1_val,
  659. const char* arg2_name,
  660. ARG2_TYPE&& arg2_val) {
  661. int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  662. base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  663. return AddTraceEventWithThreadIdAndTimestamp(
  664. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  665. bind_id, arg1_name, std::forward<ARG1_TYPE>(arg1_val), arg2_name,
  666. std::forward<ARG2_TYPE>(arg2_val));
  667. }
  668. template <class ARG1_TYPE>
  669. static void AddMetadataEvent(const unsigned char* category_group_enabled,
  670. const char* event_name,
  671. const char* arg_name,
  672. ARG1_TYPE&& arg_val) {
  673. base::trace_event::TraceArguments args(arg_name,
  674. std::forward<ARG1_TYPE>(arg_val));
  675. trace_event_internal::AddMetadataEvent(category_group_enabled, event_name,
  676. &args, TRACE_EVENT_FLAG_NONE);
  677. }
  678. // Used by TRACE_EVENTx macros. Do not use directly.
  679. class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
  680. public:
  681. ScopedTracer() = default;
  682. ~ScopedTracer() {
  683. if (category_group_enabled_ && *category_group_enabled_) {
  684. TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_,
  685. name_, event_handle_);
  686. }
  687. }
  688. void Initialize(const unsigned char* category_group_enabled,
  689. const char* name,
  690. base::trace_event::TraceEventHandle event_handle) {
  691. category_group_enabled_ = category_group_enabled;
  692. name_ = name;
  693. event_handle_ = event_handle;
  694. }
  695. private:
  696. // NOTE: Only initialize the first member to reduce generated code size,
  697. // since there is no point in initializing the other members if Initialize()
  698. // is never called.
  699. const unsigned char* category_group_enabled_ = nullptr;
  700. const char* name_;
  701. base::trace_event::TraceEventHandle event_handle_;
  702. };
  703. // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
  704. class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
  705. public:
  706. ScopedTraceBinaryEfficient(const char* category_group, const char* name);
  707. ~ScopedTraceBinaryEfficient();
  708. private:
  709. const unsigned char* category_group_enabled_;
  710. const char* name_;
  711. base::trace_event::TraceEventHandle event_handle_;
  712. };
  713. // This macro generates less code then TRACE_EVENT0 but is also
  714. // slower to execute when tracing is off. It should generally only be
  715. // used with code that is seldom executed or conditionally executed
  716. // when debugging.
  717. // For now the category_group must be "gpu".
  718. #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
  719. trace_event_internal::ScopedTraceBinaryEfficient \
  720. INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
  721. } // namespace trace_event_internal
  722. namespace base {
  723. namespace trace_event {
  724. template <typename IDType, const char* category>
  725. class TraceScopedTrackableObject {
  726. public:
  727. TraceScopedTrackableObject(const char* name, IDType id)
  728. : name_(name), id_(id) {
  729. TRACE_EVENT_OBJECT_CREATED_WITH_ID(category, name_, id_);
  730. }
  731. template <typename ArgType> void snapshot(ArgType snapshot) {
  732. TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category, name_, id_, snapshot);
  733. }
  734. ~TraceScopedTrackableObject() {
  735. TRACE_EVENT_OBJECT_DELETED_WITH_ID(category, name_, id_);
  736. }
  737. private:
  738. const char* name_;
  739. IDType id_;
  740. DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
  741. };
  742. } // namespace trace_event
  743. } // namespace base
  744. #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_