trace_event_impl.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_IMPL_H_
  5. #define BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/atomicops.h"
  11. #include "base/base_export.h"
  12. #include "base/callback.h"
  13. #include "base/macros.h"
  14. #include "base/observer_list.h"
  15. #include "base/single_thread_task_runner.h"
  16. #include "base/strings/string_util.h"
  17. #include "base/synchronization/condition_variable.h"
  18. #include "base/synchronization/lock.h"
  19. #include "base/threading/thread_local.h"
  20. #include "base/trace_event/common/trace_event_common.h"
  21. #include "base/trace_event/thread_instruction_count.h"
  22. #include "base/trace_event/trace_arguments.h"
  23. #include "base/trace_event/trace_event_memory_overhead.h"
  24. #include "build/build_config.h"
  25. namespace base {
  26. namespace trace_event {
  27. typedef base::RepeatingCallback<bool(const char* arg_name)>
  28. ArgumentNameFilterPredicate;
  29. typedef base::RepeatingCallback<bool(const char* category_group_name,
  30. const char* event_name,
  31. ArgumentNameFilterPredicate*)>
  32. ArgumentFilterPredicate;
  33. typedef base::RepeatingCallback<bool(const std::string& metadata_name)>
  34. MetadataFilterPredicate;
  35. struct TraceEventHandle {
  36. uint32_t chunk_seq;
  37. // These numbers of bits must be kept consistent with
  38. // TraceBufferChunk::kMaxTrunkIndex and
  39. // TraceBufferChunk::kTraceBufferChunkSize (in trace_buffer.h).
  40. unsigned chunk_index : 26;
  41. unsigned event_index : 6;
  42. };
  43. class BASE_EXPORT TraceEvent {
  44. public:
  45. // TODO(898794): Remove once all users have been updated.
  46. using TraceValue = base::trace_event::TraceValue;
  47. TraceEvent();
  48. TraceEvent(int thread_id,
  49. TimeTicks timestamp,
  50. ThreadTicks thread_timestamp,
  51. ThreadInstructionCount thread_instruction_count,
  52. char phase,
  53. const unsigned char* category_group_enabled,
  54. const char* name,
  55. const char* scope,
  56. unsigned long long id,
  57. unsigned long long bind_id,
  58. TraceArguments* args,
  59. unsigned int flags);
  60. ~TraceEvent();
  61. // Allow move operations.
  62. TraceEvent(TraceEvent&&) noexcept;
  63. TraceEvent& operator=(TraceEvent&&) noexcept;
  64. // Reset instance to empty state.
  65. void Reset();
  66. // Reset instance to new state. This is equivalent but slightly more
  67. // efficient than doing a move assignment, since it avoids creating
  68. // temporary copies. I.e. compare these two statements:
  69. //
  70. // event = TraceEvent(thread_id, ....); // Create and destroy temporary.
  71. // event.Reset(thread_id, ...); // Direct re-initialization.
  72. //
  73. void Reset(int thread_id,
  74. TimeTicks timestamp,
  75. ThreadTicks thread_timestamp,
  76. ThreadInstructionCount thread_instruction_count,
  77. char phase,
  78. const unsigned char* category_group_enabled,
  79. const char* name,
  80. const char* scope,
  81. unsigned long long id,
  82. unsigned long long bind_id,
  83. TraceArguments* args,
  84. unsigned int flags);
  85. void UpdateDuration(const TimeTicks& now,
  86. const ThreadTicks& thread_now,
  87. ThreadInstructionCount thread_instruction_now);
  88. void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
  89. // Serialize event data to JSON
  90. void AppendAsJSON(
  91. std::string* out,
  92. const ArgumentFilterPredicate& argument_filter_predicate) const;
  93. void AppendPrettyPrinted(std::ostringstream* out) const;
  94. TimeTicks timestamp() const { return timestamp_; }
  95. ThreadTicks thread_timestamp() const { return thread_timestamp_; }
  96. ThreadInstructionCount thread_instruction_count() const {
  97. return thread_instruction_count_;
  98. }
  99. char phase() const { return phase_; }
  100. int thread_id() const { return thread_id_; }
  101. int process_id() const { return process_id_; }
  102. TimeDelta duration() const { return duration_; }
  103. TimeDelta thread_duration() const { return thread_duration_; }
  104. ThreadInstructionDelta thread_instruction_delta() const {
  105. return thread_instruction_delta_;
  106. }
  107. const char* scope() const { return scope_; }
  108. unsigned long long id() const { return id_; }
  109. unsigned int flags() const { return flags_; }
  110. unsigned long long bind_id() const { return bind_id_; }
  111. // Exposed for unittesting:
  112. const StringStorage& parameter_copy_storage() const {
  113. return parameter_copy_storage_;
  114. }
  115. const unsigned char* category_group_enabled() const {
  116. return category_group_enabled_;
  117. }
  118. const char* name() const { return name_; }
  119. size_t arg_size() const { return args_.size(); }
  120. unsigned char arg_type(size_t index) const { return args_.types()[index]; }
  121. const char* arg_name(size_t index) const { return args_.names()[index]; }
  122. const TraceValue& arg_value(size_t index) const {
  123. return args_.values()[index];
  124. }
  125. ConvertableToTraceFormat* arg_convertible_value(size_t index) {
  126. return (arg_type(index) == TRACE_VALUE_TYPE_CONVERTABLE)
  127. ? arg_value(index).as_convertable
  128. : nullptr;
  129. }
  130. #if defined(OS_ANDROID)
  131. void SendToATrace();
  132. #endif
  133. private:
  134. void InitArgs(TraceArguments* args);
  135. // Note: these are ordered by size (largest first) for optimal packing.
  136. TimeTicks timestamp_ = TimeTicks();
  137. ThreadTicks thread_timestamp_ = ThreadTicks();
  138. TimeDelta duration_ = TimeDelta::FromInternalValue(-1);
  139. TimeDelta thread_duration_ = TimeDelta();
  140. ThreadInstructionCount thread_instruction_count_ = ThreadInstructionCount();
  141. ThreadInstructionDelta thread_instruction_delta_ = ThreadInstructionDelta();
  142. // scope_ and id_ can be used to store phase-specific data.
  143. // The following should be default-initialized to the expression
  144. // trace_event_internal::kGlobalScope, which is nullptr, but its definition
  145. // cannot be included here due to cyclical header dependencies.
  146. // The equivalence is checked with a static_assert() in trace_event_impl.cc.
  147. const char* scope_ = nullptr;
  148. unsigned long long id_ = 0u;
  149. const unsigned char* category_group_enabled_ = nullptr;
  150. const char* name_ = nullptr;
  151. StringStorage parameter_copy_storage_;
  152. TraceArguments args_;
  153. // Depending on TRACE_EVENT_FLAG_HAS_PROCESS_ID the event will have either:
  154. // tid: thread_id_, pid: current_process_id (default case).
  155. // tid: -1, pid: process_id_ (when flags_ & TRACE_EVENT_FLAG_HAS_PROCESS_ID).
  156. union {
  157. int thread_id_ = 0;
  158. int process_id_;
  159. };
  160. unsigned int flags_ = 0;
  161. unsigned long long bind_id_ = 0;
  162. char phase_ = TRACE_EVENT_PHASE_BEGIN;
  163. DISALLOW_COPY_AND_ASSIGN(TraceEvent);
  164. };
  165. } // namespace trace_event
  166. } // namespace base
  167. #endif // BASE_TRACE_EVENT_TRACE_EVENT_IMPL_H_