OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project 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 |
| 5 #ifndef V8_LIBPLATFORM_V8_TRACING_H_ |
| 6 #define V8_LIBPLATFORM_V8_TRACING_H_ |
| 7 |
| 8 #include <fstream> |
| 9 #include <memory> |
| 10 #include <vector> |
| 11 |
| 12 namespace v8 { |
| 13 namespace platform { |
| 14 namespace tracing { |
| 15 |
| 16 class TraceObject { |
| 17 public: |
| 18 TraceObject() {} |
| 19 void Initialize(char phase, const uint8_t* category_enabled_flag, |
| 20 const char* name, const char* scope, uint64_t id, |
| 21 uint64_t bind_id, int num_args, const char** arg_names, |
| 22 const uint8_t* arg_types, const uint64_t* arg_values, |
| 23 unsigned int flags); |
| 24 void UpdateDuration(); |
| 25 void InitializeForTesting(char phase, const uint8_t* category_enabled_flag, |
| 26 const char* name, const char* scope, uint64_t id, |
| 27 uint64_t bind_id, int num_args, |
| 28 const char** arg_names, const uint8_t* arg_types, |
| 29 const uint64_t* arg_values, unsigned int flags, |
| 30 int pid, int tid, int64_t ts, int64_t tts, |
| 31 uint64_t duration, uint64_t cpu_duration); |
| 32 |
| 33 int pid() const { return pid_; } |
| 34 int tid() const { return tid_; } |
| 35 char phase() const { return phase_; } |
| 36 const uint8_t* category_enabled_flag() const { |
| 37 return category_enabled_flag_; |
| 38 } |
| 39 const char* name() const { return name_; } |
| 40 const char* scope() const { return scope_; } |
| 41 uint64_t id() const { return id_; } |
| 42 uint64_t bind_id() const { return bind_id_; } |
| 43 unsigned int flags() const { return flags_; } |
| 44 int64_t ts() { return ts_; } |
| 45 int64_t tts() { return tts_; } |
| 46 uint64_t duration() { return duration_; } |
| 47 uint64_t cpu_duration() { return cpu_duration_; } |
| 48 |
| 49 private: |
| 50 int pid_; |
| 51 int tid_; |
| 52 char phase_; |
| 53 const char* name_; |
| 54 const char* scope_; |
| 55 const uint8_t* category_enabled_flag_; |
| 56 uint64_t id_; |
| 57 uint64_t bind_id_; |
| 58 int num_args_; |
| 59 unsigned int flags_; |
| 60 int64_t ts_; |
| 61 int64_t tts_; |
| 62 uint64_t duration_; |
| 63 uint64_t cpu_duration_; |
| 64 // TODO(fmeawad): Add args support. |
| 65 |
| 66 // Disallow copy and assign |
| 67 TraceObject(const TraceObject&) = delete; |
| 68 void operator=(const TraceObject&) = delete; |
| 69 }; |
| 70 |
| 71 class TraceWriter { |
| 72 public: |
| 73 TraceWriter() {} |
| 74 virtual ~TraceWriter() {} |
| 75 virtual void AppendTraceEvent(TraceObject* trace_event) = 0; |
| 76 virtual void Flush() = 0; |
| 77 |
| 78 static TraceWriter* CreateJSONTraceWriter(std::ostream& stream); |
| 79 |
| 80 private: |
| 81 // Disallow copy and assign |
| 82 TraceWriter(const TraceWriter&) = delete; |
| 83 void operator=(const TraceWriter&) = delete; |
| 84 }; |
| 85 |
| 86 class TraceBufferChunk { |
| 87 public: |
| 88 explicit TraceBufferChunk(uint32_t seq); |
| 89 |
| 90 void Reset(uint32_t new_seq); |
| 91 bool IsFull() const { return next_free_ == kChunkSize; } |
| 92 TraceObject* AddTraceEvent(size_t* event_index); |
| 93 TraceObject* GetEventAt(size_t index) { return &chunk_[index]; } |
| 94 |
| 95 uint32_t seq() const { return seq_; } |
| 96 size_t size() const { return next_free_; } |
| 97 |
| 98 static const size_t kChunkSize = 64; |
| 99 |
| 100 private: |
| 101 size_t next_free_ = 0; |
| 102 TraceObject chunk_[kChunkSize]; |
| 103 uint32_t seq_; |
| 104 |
| 105 // Disallow copy and assign |
| 106 TraceBufferChunk(const TraceBufferChunk&) = delete; |
| 107 void operator=(const TraceBufferChunk&) = delete; |
| 108 }; |
| 109 |
| 110 class TraceBuffer { |
| 111 public: |
| 112 TraceBuffer() {} |
| 113 virtual ~TraceBuffer() {} |
| 114 |
| 115 virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0; |
| 116 virtual TraceObject* GetEventByHandle(uint64_t handle) = 0; |
| 117 virtual bool Flush() = 0; |
| 118 |
| 119 static const size_t kRingBufferChunks = 1024; |
| 120 |
| 121 static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks, |
| 122 TraceWriter* trace_writer); |
| 123 |
| 124 private: |
| 125 // Disallow copy and assign |
| 126 TraceBuffer(const TraceBuffer&) = delete; |
| 127 void operator=(const TraceBuffer&) = delete; |
| 128 }; |
| 129 |
| 130 // Options determines how the trace buffer stores data. |
| 131 enum TraceRecordMode { |
| 132 // Record until the trace buffer is full. |
| 133 RECORD_UNTIL_FULL, |
| 134 |
| 135 // Record until the user ends the trace. The trace buffer is a fixed size |
| 136 // and we use it as a ring buffer during recording. |
| 137 RECORD_CONTINUOUSLY, |
| 138 |
| 139 // Record until the trace buffer is full, but with a huge buffer size. |
| 140 RECORD_AS_MUCH_AS_POSSIBLE, |
| 141 |
| 142 // Echo to console. Events are discarded. |
| 143 ECHO_TO_CONSOLE, |
| 144 }; |
| 145 |
| 146 class TraceConfig { |
| 147 public: |
| 148 typedef std::vector<std::string> StringList; |
| 149 |
| 150 TraceConfig() |
| 151 : enable_sampling_(false), |
| 152 enable_systrace_(false), |
| 153 enable_argument_filter_(false) {} |
| 154 TraceRecordMode GetTraceRecordMode() const { return record_mode_; } |
| 155 bool IsSamplingEnabled() const { return enable_sampling_; } |
| 156 bool IsSystraceEnabled() const { return enable_systrace_; } |
| 157 bool IsArgumentFilterEnabled() const { return enable_argument_filter_; } |
| 158 |
| 159 void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; } |
| 160 void EnableSampling() { enable_sampling_ = true; } |
| 161 void EnableSystrace() { enable_systrace_ = true; } |
| 162 void EnableArgumentFilter() { enable_argument_filter_ = true; } |
| 163 |
| 164 void AddIncludedCategory(const char* included_category); |
| 165 void AddExcludedCategory(const char* excluded_category); |
| 166 |
| 167 bool IsCategoryGroupEnabled(const char* category_group) const; |
| 168 |
| 169 private: |
| 170 TraceRecordMode record_mode_; |
| 171 bool enable_sampling_ : 1; |
| 172 bool enable_systrace_ : 1; |
| 173 bool enable_argument_filter_ : 1; |
| 174 StringList included_categories_; |
| 175 StringList excluded_categories_; |
| 176 |
| 177 // Disallow copy and assign |
| 178 TraceConfig(const TraceConfig&) = delete; |
| 179 void operator=(const TraceConfig&) = delete; |
| 180 }; |
| 181 |
| 182 class TracingController { |
| 183 public: |
| 184 enum Mode { DISABLED = 0, RECORDING_MODE }; |
| 185 |
| 186 // The pointer returned from GetCategoryGroupEnabledInternal() points to a |
| 187 // value with zero or more of the following bits. Used in this class only. |
| 188 // The TRACE_EVENT macros should only use the value as a bool. |
| 189 // These values must be in sync with macro values in TraceEvent.h in Blink. |
| 190 enum CategoryGroupEnabledFlags { |
| 191 // Category group enabled for the recording mode. |
| 192 ENABLED_FOR_RECORDING = 1 << 0, |
| 193 // Category group enabled by SetEventCallbackEnabled(). |
| 194 ENABLED_FOR_EVENT_CALLBACK = 1 << 2, |
| 195 // Category group enabled to export events to ETW. |
| 196 ENABLED_FOR_ETW_EXPORT = 1 << 3 |
| 197 }; |
| 198 |
| 199 TracingController() {} |
| 200 void Initialize(TraceBuffer* trace_buffer); |
| 201 const uint8_t* GetCategoryGroupEnabled(const char* category_group); |
| 202 static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag); |
| 203 uint64_t AddTraceEvent(char phase, const uint8_t* category_enabled_flag, |
| 204 const char* name, const char* scope, uint64_t id, |
| 205 uint64_t bind_id, int32_t num_args, |
| 206 const char** arg_names, const uint8_t* arg_types, |
| 207 const uint64_t* arg_values, unsigned int flags); |
| 208 void UpdateTraceEventDuration(const uint8_t* category_enabled_flag, |
| 209 const char* name, uint64_t handle); |
| 210 |
| 211 void StartTracing(TraceConfig* trace_config); |
| 212 void StopTracing(); |
| 213 |
| 214 private: |
| 215 const uint8_t* GetCategoryGroupEnabledInternal(const char* category_group); |
| 216 void UpdateCategoryGroupEnabledFlag(size_t category_index); |
| 217 void UpdateCategoryGroupEnabledFlags(); |
| 218 |
| 219 std::unique_ptr<TraceBuffer> trace_buffer_; |
| 220 std::unique_ptr<TraceConfig> trace_config_; |
| 221 Mode mode_ = DISABLED; |
| 222 |
| 223 // Disallow copy and assign |
| 224 TracingController(const TracingController&) = delete; |
| 225 void operator=(const TracingController&) = delete; |
| 226 }; |
| 227 |
| 228 } // namespace tracing |
| 229 } // namespace platform |
| 230 } // namespace v8 |
| 231 |
| 232 #endif // V8_LIBPLATFORM_V8_TRACING_H_ |
OLD | NEW |