trace_event.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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 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, h); \
  239. }
  240. #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS(category_group, name, \
  241. flags, ...) \
  242. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  243. trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  244. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  245. base::trace_event::TraceEventHandle h = \
  246. trace_event_internal::AddTraceEvent( \
  247. TRACE_EVENT_PHASE_COMPLETE, \
  248. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  249. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  250. flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
  251. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  252. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
  253. }
  254. #define INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLOW(category_group, name, \
  255. bind_id, flow_flags, ...) \
  256. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  257. trace_event_internal::ScopedTracer INTERNAL_TRACE_EVENT_UID(tracer); \
  258. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  259. trace_event_internal::TraceID trace_event_bind_id((bind_id)); \
  260. unsigned int trace_event_flags = \
  261. flow_flags | trace_event_bind_id.id_flags(); \
  262. base::trace_event::TraceEventHandle h = \
  263. trace_event_internal::AddTraceEvent( \
  264. TRACE_EVENT_PHASE_COMPLETE, \
  265. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  266. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  267. trace_event_flags, trace_event_bind_id.raw_id(), ##__VA_ARGS__); \
  268. INTERNAL_TRACE_EVENT_UID(tracer).Initialize( \
  269. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, h); \
  270. }
  271. // Implementation detail: internal macro to create static category and add
  272. // event if the category is enabled.
  273. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
  274. flags, ...) \
  275. do { \
  276. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  277. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  278. trace_event_internal::TraceID trace_event_trace_id((id)); \
  279. unsigned int trace_event_flags = \
  280. flags | trace_event_trace_id.id_flags(); \
  281. trace_event_internal::AddTraceEvent( \
  282. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  283. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  284. trace_event_flags, trace_event_internal::kNoId, ##__VA_ARGS__); \
  285. } \
  286. } while (0)
  287. // Implementation detail: internal macro to create static category and add
  288. // event if the category is enabled.
  289. #define INTERNAL_TRACE_EVENT_ADD_WITH_TIMESTAMP(phase, category_group, name, \
  290. timestamp, flags, ...) \
  291. do { \
  292. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  293. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  294. trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
  295. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  296. trace_event_internal::kGlobalScope, trace_event_internal::kNoId, \
  297. TRACE_EVENT_API_CURRENT_THREAD_ID, timestamp, \
  298. flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
  299. trace_event_internal::kNoId, ##__VA_ARGS__); \
  300. } \
  301. } while (0)
  302. // Implementation detail: internal macro to create static category and add
  303. // event if the category is enabled.
  304. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
  305. phase, category_group, name, id, thread_id, timestamp, flags, ...) \
  306. do { \
  307. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  308. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  309. trace_event_internal::TraceID trace_event_trace_id((id)); \
  310. unsigned int trace_event_flags = \
  311. flags | trace_event_trace_id.id_flags(); \
  312. trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
  313. phase, INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  314. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  315. thread_id, timestamp, \
  316. trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP, \
  317. trace_event_internal::kNoId, ##__VA_ARGS__); \
  318. } \
  319. } while (0)
  320. // Implementation detail: internal macro to create static category and add
  321. // event if the category is enabled.
  322. #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMPS( \
  323. phase, category_group, name, id, thread_id, timestamp, thread_timestamp, \
  324. flags) \
  325. do { \
  326. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  327. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  328. trace_event_internal::TraceID trace_event_trace_id((id)); \
  329. unsigned int trace_event_flags = \
  330. flags | trace_event_trace_id.id_flags(); \
  331. const unsigned char* uid_category_group_enabled = \
  332. INTERNAL_TRACE_EVENT_UID(category_group_enabled); \
  333. trace_event_internal::AddTraceEventWithThreadIdAndTimestamps( \
  334. phase, uid_category_group_enabled, name, \
  335. trace_event_trace_id.scope(), trace_event_trace_id.raw_id(), \
  336. thread_id, timestamp, thread_timestamp, \
  337. trace_event_flags | TRACE_EVENT_FLAG_EXPLICIT_TIMESTAMP); \
  338. } \
  339. } while (0)
  340. // Implementation detail: internal macro to create static category and add
  341. // metadata event if the category is enabled.
  342. #define INTERNAL_TRACE_EVENT_METADATA_ADD(category_group, name, ...) \
  343. do { \
  344. INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
  345. if (INTERNAL_TRACE_EVENT_CATEGORY_GROUP_ENABLED()) { \
  346. TRACE_EVENT_API_ADD_METADATA_EVENT( \
  347. INTERNAL_TRACE_EVENT_UID(category_group_enabled), name, \
  348. ##__VA_ARGS__); \
  349. } \
  350. } while (0)
  351. #define INTERNAL_TRACE_LOG_MESSAGE(file, message, line) \
  352. TRACE_EVENT_INSTANT1( \
  353. "log", "LogMessage", \
  354. TRACE_EVENT_FLAG_TYPED_PROTO_ARGS | TRACE_EVENT_SCOPE_THREAD, "message", \
  355. std::make_unique<base::trace_event::LogMessage>(file, message, line))
  356. #if BUILDFLAG(ENABLE_LOCATION_SOURCE)
  357. // Implementation detail: internal macro to trace a task execution with the
  358. // location where it was posted from.
  359. //
  360. // This implementation is for when location sources are available.
  361. // TODO(ssid): The program counter of the current task should be added here.
  362. #define INTERNAL_TRACE_TASK_EXECUTION(run_function, task) \
  363. INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS( \
  364. "toplevel", run_function, TRACE_EVENT_FLAG_TYPED_PROTO_ARGS, "src_file", \
  365. (task).posted_from.file_name(), "src_func", \
  366. (task).posted_from.function_name()); \
  367. TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION INTERNAL_TRACE_EVENT_UID( \
  368. task_event)((task).posted_from.file_name()); \
  369. TRACE_HEAP_PROFILER_API_SCOPED_WITH_PROGRAM_COUNTER \
  370. INTERNAL_TRACE_EVENT_UID(task_pc_event)((task).posted_from.program_counter());
  371. #else
  372. // TODO(http://crbug.com760702) remove file name and just pass the program
  373. // counter to the heap profiler macro.
  374. // TODO(ssid): The program counter of the current task should be added here.
  375. #define INTERNAL_TRACE_TASK_EXECUTION(run_function, task) \
  376. INTERNAL_TRACE_EVENT_ADD_SCOPED_WITH_FLAGS( \
  377. "toplevel", run_function, TRACE_EVENT_FLAG_TYPED_PROTO_ARGS, "src", \
  378. (task).posted_from.ToString()) \
  379. TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION INTERNAL_TRACE_EVENT_UID( \
  380. task_event)((task).posted_from.file_name()); \
  381. TRACE_HEAP_PROFILER_API_SCOPED_WITH_PROGRAM_COUNTER \
  382. INTERNAL_TRACE_EVENT_UID(task_pc_event)((task).posted_from.program_counter());
  383. #endif
  384. namespace trace_event_internal {
  385. // Specify these values when the corresponding argument of AddTraceEvent is not
  386. // used.
  387. const int kZeroNumArgs = 0;
  388. const std::nullptr_t kGlobalScope = nullptr;
  389. const unsigned long long kNoId = 0;
  390. // TraceID encapsulates an ID that can either be an integer or pointer.
  391. class BASE_EXPORT TraceID {
  392. public:
  393. // Can be combined with WithScope.
  394. class LocalId {
  395. public:
  396. explicit LocalId(const void* raw_id)
  397. : raw_id_(static_cast<unsigned long long>(
  398. reinterpret_cast<uintptr_t>(raw_id))) {}
  399. explicit LocalId(unsigned long long raw_id) : raw_id_(raw_id) {}
  400. unsigned long long raw_id() const { return raw_id_; }
  401. private:
  402. unsigned long long raw_id_;
  403. };
  404. // Can be combined with WithScope.
  405. class GlobalId {
  406. public:
  407. explicit GlobalId(unsigned long long raw_id) : raw_id_(raw_id) {}
  408. unsigned long long raw_id() const { return raw_id_; }
  409. private:
  410. unsigned long long raw_id_;
  411. };
  412. class WithScope {
  413. public:
  414. WithScope(const char* scope, unsigned long long raw_id)
  415. : scope_(scope), raw_id_(raw_id) {}
  416. WithScope(const char* scope, LocalId local_id)
  417. : scope_(scope), raw_id_(local_id.raw_id()) {
  418. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  419. }
  420. WithScope(const char* scope, GlobalId global_id)
  421. : scope_(scope), raw_id_(global_id.raw_id()) {
  422. id_flags_ = TRACE_EVENT_FLAG_HAS_GLOBAL_ID;
  423. }
  424. unsigned long long raw_id() const { return raw_id_; }
  425. const char* scope() const { return scope_; }
  426. unsigned int id_flags() const { return id_flags_; }
  427. private:
  428. const char* scope_ = nullptr;
  429. unsigned long long raw_id_;
  430. unsigned int id_flags_ = TRACE_EVENT_FLAG_HAS_ID;
  431. };
  432. TraceID(const void* raw_id) : raw_id_(static_cast<unsigned long long>(
  433. reinterpret_cast<uintptr_t>(raw_id))) {
  434. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  435. }
  436. TraceID(unsigned long long raw_id) : raw_id_(raw_id) {}
  437. TraceID(unsigned long raw_id) : raw_id_(raw_id) {}
  438. TraceID(unsigned int raw_id) : raw_id_(raw_id) {}
  439. TraceID(unsigned short raw_id) : raw_id_(raw_id) {}
  440. TraceID(unsigned char raw_id) : raw_id_(raw_id) {}
  441. TraceID(long long raw_id)
  442. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  443. TraceID(long raw_id)
  444. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  445. TraceID(int raw_id)
  446. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  447. TraceID(short raw_id)
  448. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  449. TraceID(signed char raw_id)
  450. : raw_id_(static_cast<unsigned long long>(raw_id)) {}
  451. TraceID(LocalId raw_id) : raw_id_(raw_id.raw_id()) {
  452. id_flags_ = TRACE_EVENT_FLAG_HAS_LOCAL_ID;
  453. }
  454. TraceID(GlobalId raw_id) : raw_id_(raw_id.raw_id()) {
  455. id_flags_ = TRACE_EVENT_FLAG_HAS_GLOBAL_ID;
  456. }
  457. TraceID(WithScope scoped_id)
  458. : scope_(scoped_id.scope()),
  459. raw_id_(scoped_id.raw_id()),
  460. id_flags_(scoped_id.id_flags()) {}
  461. unsigned long long raw_id() const { return raw_id_; }
  462. const char* scope() const { return scope_; }
  463. unsigned int id_flags() const { return id_flags_; }
  464. private:
  465. const char* scope_ = nullptr;
  466. unsigned long long raw_id_;
  467. unsigned int id_flags_ = TRACE_EVENT_FLAG_HAS_ID;
  468. };
  469. // These functions all internally call
  470. // base::trace_event::TraceLog::GetInstance() then call the method with the same
  471. // name on it. This is used to reduce the generated machine code at each
  472. // TRACE_EVENTXXX macro call.
  473. base::trace_event::TraceEventHandle BASE_EXPORT
  474. AddTraceEvent(char phase,
  475. const unsigned char* category_group_enabled,
  476. const char* name,
  477. const char* scope,
  478. unsigned long long id,
  479. base::trace_event::TraceArguments* args,
  480. unsigned int flags);
  481. base::trace_event::TraceEventHandle BASE_EXPORT
  482. AddTraceEventWithBindId(char phase,
  483. const unsigned char* category_group_enabled,
  484. const char* name,
  485. const char* scope,
  486. unsigned long long id,
  487. unsigned long long bind_id,
  488. base::trace_event::TraceArguments* args,
  489. unsigned int flags);
  490. base::trace_event::TraceEventHandle BASE_EXPORT
  491. AddTraceEventWithProcessId(char phase,
  492. const unsigned char* category_group_enabled,
  493. const char* name,
  494. const char* scope,
  495. unsigned long long id,
  496. int process_id,
  497. base::trace_event::TraceArguments* args,
  498. unsigned int flags);
  499. base::trace_event::TraceEventHandle BASE_EXPORT
  500. AddTraceEventWithThreadIdAndTimestamp(
  501. char phase,
  502. const unsigned char* category_group_enabled,
  503. const char* name,
  504. const char* scope,
  505. unsigned long long id,
  506. int thread_id,
  507. const base::TimeTicks& timestamp,
  508. base::trace_event::TraceArguments* args,
  509. unsigned int flags);
  510. base::trace_event::TraceEventHandle BASE_EXPORT
  511. AddTraceEventWithThreadIdAndTimestamp(
  512. char phase,
  513. const unsigned char* category_group_enabled,
  514. const char* name,
  515. const char* scope,
  516. unsigned long long id,
  517. unsigned long long bind_id,
  518. int thread_id,
  519. const base::TimeTicks& timestamp,
  520. base::trace_event::TraceArguments* args,
  521. unsigned int flags);
  522. base::trace_event::TraceEventHandle BASE_EXPORT
  523. AddTraceEventWithThreadIdAndTimestamps(
  524. char phase,
  525. const unsigned char* category_group_enabled,
  526. const char* name,
  527. const char* scope,
  528. unsigned long long id,
  529. int thread_id,
  530. const base::TimeTicks& timestamp,
  531. const base::ThreadTicks& thread_timestamp,
  532. unsigned int flags);
  533. void BASE_EXPORT AddMetadataEvent(const unsigned char* category_group_enabled,
  534. const char* name,
  535. base::trace_event::TraceArguments* args,
  536. unsigned int flags);
  537. int BASE_EXPORT GetNumTracesRecorded();
  538. void BASE_EXPORT
  539. UpdateTraceEventDuration(const unsigned char* category_group_enabled,
  540. const char* name,
  541. base::trace_event::TraceEventHandle handle);
  542. void BASE_EXPORT UpdateTraceEventDurationExplicit(
  543. const unsigned char* category_group_enabled,
  544. const char* name,
  545. base::trace_event::TraceEventHandle handle,
  546. int thread_id,
  547. bool explicit_timestamps,
  548. const base::TimeTicks& now,
  549. const base::ThreadTicks& thread_now,
  550. base::trace_event::ThreadInstructionCount thread_instruction_now);
  551. // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
  552. // functions are defined here instead of in the macro, because the arg_values
  553. // could be temporary objects, such as std::string. In order to store
  554. // pointers to the internal c_str and pass through to the tracing API,
  555. // the arg_values must live throughout these procedures.
  556. template <class ARG1_TYPE>
  557. static inline base::trace_event::TraceEventHandle
  558. AddTraceEventWithThreadIdAndTimestamp(
  559. char phase,
  560. const unsigned char* category_group_enabled,
  561. const char* name,
  562. const char* scope,
  563. unsigned long long id,
  564. int thread_id,
  565. const base::TimeTicks& timestamp,
  566. unsigned int flags,
  567. unsigned long long bind_id,
  568. const char* arg1_name,
  569. ARG1_TYPE&& arg1_val) {
  570. base::trace_event::TraceArguments args(arg1_name,
  571. std::forward<ARG1_TYPE>(arg1_val));
  572. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  573. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  574. timestamp, &args, flags);
  575. }
  576. template <class ARG1_TYPE, class ARG2_TYPE>
  577. static inline base::trace_event::TraceEventHandle
  578. AddTraceEventWithThreadIdAndTimestamp(
  579. char phase,
  580. const unsigned char* category_group_enabled,
  581. const char* name,
  582. const char* scope,
  583. unsigned long long id,
  584. int thread_id,
  585. const base::TimeTicks& timestamp,
  586. unsigned int flags,
  587. unsigned long long bind_id,
  588. const char* arg1_name,
  589. ARG1_TYPE&& arg1_val,
  590. const char* arg2_name,
  591. ARG2_TYPE&& arg2_val) {
  592. base::trace_event::TraceArguments args(
  593. arg1_name, std::forward<ARG1_TYPE>(arg1_val), arg2_name,
  594. std::forward<ARG2_TYPE>(arg2_val));
  595. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  596. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  597. timestamp, &args, flags);
  598. }
  599. static inline base::trace_event::TraceEventHandle
  600. AddTraceEventWithThreadIdAndTimestamp(
  601. char phase,
  602. const unsigned char* category_group_enabled,
  603. const char* name,
  604. const char* scope,
  605. unsigned long long id,
  606. int thread_id,
  607. const base::TimeTicks& timestamp,
  608. unsigned int flags,
  609. unsigned long long bind_id) {
  610. return TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
  611. phase, category_group_enabled, name, scope, id, bind_id, thread_id,
  612. timestamp, nullptr, flags);
  613. }
  614. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  615. char phase,
  616. const unsigned char* category_group_enabled,
  617. const char* name,
  618. const char* scope,
  619. unsigned long long id,
  620. unsigned int flags,
  621. unsigned long long bind_id) {
  622. const int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  623. const base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  624. return AddTraceEventWithThreadIdAndTimestamp(
  625. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  626. bind_id);
  627. }
  628. template <class ARG1_TYPE>
  629. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  630. char phase,
  631. const unsigned char* category_group_enabled,
  632. const char* name,
  633. const char* scope,
  634. unsigned long long id,
  635. unsigned int flags,
  636. unsigned long long bind_id,
  637. const char* arg1_name,
  638. ARG1_TYPE&& arg1_val) {
  639. int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  640. base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  641. return AddTraceEventWithThreadIdAndTimestamp(
  642. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  643. bind_id, arg1_name, std::forward<ARG1_TYPE>(arg1_val));
  644. }
  645. template <class ARG1_TYPE, class ARG2_TYPE>
  646. static inline base::trace_event::TraceEventHandle AddTraceEvent(
  647. char phase,
  648. const unsigned char* category_group_enabled,
  649. const char* name,
  650. const char* scope,
  651. unsigned long long id,
  652. unsigned int flags,
  653. unsigned long long bind_id,
  654. const char* arg1_name,
  655. ARG1_TYPE&& arg1_val,
  656. const char* arg2_name,
  657. ARG2_TYPE&& arg2_val) {
  658. int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
  659. base::TimeTicks now = TRACE_TIME_TICKS_NOW();
  660. return AddTraceEventWithThreadIdAndTimestamp(
  661. phase, category_group_enabled, name, scope, id, thread_id, now, flags,
  662. bind_id, arg1_name, std::forward<ARG1_TYPE>(arg1_val), arg2_name,
  663. std::forward<ARG2_TYPE>(arg2_val));
  664. }
  665. template <class ARG1_TYPE>
  666. static void AddMetadataEvent(const unsigned char* category_group_enabled,
  667. const char* event_name,
  668. const char* arg_name,
  669. ARG1_TYPE&& arg_val) {
  670. base::trace_event::TraceArguments args(arg_name,
  671. std::forward<ARG1_TYPE>(arg_val));
  672. trace_event_internal::AddMetadataEvent(category_group_enabled, event_name,
  673. &args, TRACE_EVENT_FLAG_NONE);
  674. }
  675. // Used by TRACE_EVENTx macros. Do not use directly.
  676. class TRACE_EVENT_API_CLASS_EXPORT ScopedTracer {
  677. public:
  678. ScopedTracer() = default;
  679. ~ScopedTracer() {
  680. if (category_group_enabled_ && *category_group_enabled_) {
  681. TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION(category_group_enabled_,
  682. name_, event_handle_);
  683. }
  684. }
  685. void Initialize(const unsigned char* category_group_enabled,
  686. const char* name,
  687. base::trace_event::TraceEventHandle event_handle) {
  688. category_group_enabled_ = category_group_enabled;
  689. name_ = name;
  690. event_handle_ = event_handle;
  691. }
  692. private:
  693. // NOTE: Only initialize the first member to reduce generated code size,
  694. // since there is no point in initializing the other members if Initialize()
  695. // is never called.
  696. const unsigned char* category_group_enabled_ = nullptr;
  697. const char* name_;
  698. base::trace_event::TraceEventHandle event_handle_;
  699. };
  700. // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
  701. class TRACE_EVENT_API_CLASS_EXPORT ScopedTraceBinaryEfficient {
  702. public:
  703. ScopedTraceBinaryEfficient(const char* category_group, const char* name);
  704. ~ScopedTraceBinaryEfficient();
  705. private:
  706. const unsigned char* category_group_enabled_;
  707. const char* name_;
  708. base::trace_event::TraceEventHandle event_handle_;
  709. };
  710. // This macro generates less code then TRACE_EVENT0 but is also
  711. // slower to execute when tracing is off. It should generally only be
  712. // used with code that is seldom executed or conditionally executed
  713. // when debugging.
  714. // For now the category_group must be "gpu".
  715. #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
  716. trace_event_internal::ScopedTraceBinaryEfficient \
  717. INTERNAL_TRACE_EVENT_UID(scoped_trace)(category_group, name);
  718. } // namespace trace_event_internal
  719. namespace base {
  720. namespace trace_event {
  721. template <typename IDType, const char* category>
  722. class TraceScopedTrackableObject {
  723. public:
  724. TraceScopedTrackableObject(const char* name, IDType id)
  725. : name_(name), id_(id) {
  726. TRACE_EVENT_OBJECT_CREATED_WITH_ID(category, name_, id_);
  727. }
  728. template <typename ArgType> void snapshot(ArgType snapshot) {
  729. TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category, name_, id_, snapshot);
  730. }
  731. ~TraceScopedTrackableObject() {
  732. TRACE_EVENT_OBJECT_DELETED_WITH_ID(category, name_, id_);
  733. }
  734. private:
  735. const char* name_;
  736. IDType id_;
  737. DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
  738. };
  739. } // namespace trace_event
  740. } // namespace base
  741. #endif // BASE_TRACE_EVENT_TRACE_EVENT_H_