trace_event_impl.h 6.1 KB

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