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