Chromium Code Reviews| 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" | |
|
jochen (gone - plz use gerrit)
2016/07/20 11:03:25
libplatform should not depend on v8.h
fmeawad
2016/07/26 09:08:09
Done.
| |
| 12 | |
| 13 namespace v8 { | |
| 14 namespace platform { | |
| 15 namespace tracing { | |
| 16 | |
| 17 class TraceObject { | |
|
jochen (gone - plz use gerrit)
2016/07/20 11:03:25
please add a ctor
fmeawad
2016/07/26 09:08:09
Done.
| |
| 18 public: | |
| 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 }; | |
|
jochen (gone - plz use gerrit)
2016/07/20 11:03:25
disallow copy/assign?
fmeawad
2016/07/26 09:08:09
Done.
| |
| 66 | |
| 67 class TraceWriter { | |
| 68 public: | |
| 69 virtual ~TraceWriter() {} | |
| 70 virtual void AppendTraceEvent(TraceObject* trace_event) = 0; | |
| 71 virtual void Flush() = 0; | |
| 72 | |
| 73 static TraceWriter* CreateJSONTraceWriter(std::ostream& stream); | |
| 74 }; | |
| 75 | |
| 76 class TraceBufferChunk { | |
| 77 public: | |
| 78 explicit TraceBufferChunk(uint32_t seq); | |
| 79 | |
| 80 void Reset(uint32_t new_seq); | |
| 81 bool IsFull() const { return next_free_ == kChunkSize; } | |
| 82 TraceObject* AddTraceEvent(size_t* event_index); | |
| 83 TraceObject* GetEventAt(size_t index) { return &chunk_[index]; } | |
| 84 | |
| 85 uint32_t seq() const { return seq_; } | |
| 86 size_t size() const { return next_free_; } | |
| 87 | |
| 88 static const size_t kChunkSize = 64; | |
| 89 | |
| 90 private: | |
| 91 size_t next_free_ = 0; | |
| 92 TraceObject chunk_[kChunkSize]; | |
| 93 uint32_t seq_; | |
| 94 }; | |
| 95 | |
| 96 class TraceBuffer { | |
| 97 public: | |
| 98 virtual ~TraceBuffer() {} | |
| 99 | |
| 100 virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0; | |
| 101 virtual TraceObject* GetEventByHandle(uint64_t handle) = 0; | |
| 102 virtual bool Flush() = 0; | |
| 103 | |
| 104 static const size_t kRingBufferChunks = 1024; | |
| 105 | |
| 106 static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks, | |
| 107 TraceWriter* trace_writer); | |
| 108 }; | |
| 109 | |
| 110 // Options determines how the trace buffer stores data. | |
| 111 enum TraceRecordMode { | |
| 112 // Record until the trace buffer is full. | |
| 113 RECORD_UNTIL_FULL, | |
| 114 | |
| 115 // Record until the user ends the trace. The trace buffer is a fixed size | |
| 116 // and we use it as a ring buffer during recording. | |
| 117 RECORD_CONTINUOUSLY, | |
| 118 | |
| 119 // Record until the trace buffer is full, but with a huge buffer size. | |
| 120 RECORD_AS_MUCH_AS_POSSIBLE, | |
| 121 | |
| 122 // Echo to console. Events are discarded. | |
| 123 ECHO_TO_CONSOLE, | |
| 124 }; | |
| 125 | |
| 126 class TraceConfig { | |
| 127 public: | |
| 128 typedef std::vector<std::string> StringList; | |
| 129 | |
| 130 static TraceConfig* CreateDefaultTraceConfig(); | |
| 131 // Create a trace config object using a given JSON format string. | |
| 132 static TraceConfig* CreateTraceConfigFromJSON(v8::Isolate* isolate, | |
| 133 const char* json_str); | |
| 134 | |
| 135 TraceRecordMode GetTraceRecordMode() const { return record_mode_; } | |
| 136 bool IsSamplingEnabled() const { return enable_sampling_; } | |
| 137 bool IsSystraceEnabled() const { return enable_systrace_; } | |
| 138 bool IsArgumentFilterEnabled() const { return enable_argument_filter_; } | |
| 139 | |
| 140 void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; } | |
| 141 void EnableSampling() { enable_sampling_ = true; } | |
| 142 void EnableSystrace() { enable_systrace_ = true; } | |
| 143 void EnableArgumentFilter() { enable_argument_filter_ = true; } | |
| 144 | |
| 145 bool IsCategoryGroupEnabled(const char* category_group) const; | |
| 146 | |
| 147 private: | |
| 148 friend class TraceConfigParser; | |
| 149 | |
| 150 TraceConfig() | |
| 151 : enable_sampling_(false), | |
| 152 enable_systrace_(false), | |
| 153 enable_argument_filter_(false) {} | |
| 154 | |
| 155 TraceRecordMode record_mode_; | |
| 156 bool enable_sampling_ : 1; | |
| 157 bool enable_systrace_ : 1; | |
| 158 bool enable_argument_filter_ : 1; | |
| 159 StringList included_categories_; | |
| 160 StringList excluded_categories_; | |
| 161 }; | |
| 162 | |
| 163 class TracingController { | |
| 164 public: | |
| 165 enum Mode { DISABLED = 0, RECORDING_MODE }; | |
| 166 | |
| 167 // The pointer returned from GetCategoryGroupEnabledInternal() points to a | |
| 168 // value with zero or more of the following bits. Used in this class only. | |
| 169 // The TRACE_EVENT macros should only use the value as a bool. | |
| 170 // These values must be in sync with macro values in TraceEvent.h in Blink. | |
| 171 enum CategoryGroupEnabledFlags { | |
| 172 // Category group enabled for the recording mode. | |
| 173 ENABLED_FOR_RECORDING = 1 << 0, | |
| 174 // Category group enabled by SetEventCallbackEnabled(). | |
| 175 ENABLED_FOR_EVENT_CALLBACK = 1 << 2, | |
| 176 // Category group enabled to export events to ETW. | |
| 177 ENABLED_FOR_ETW_EXPORT = 1 << 3 | |
| 178 }; | |
| 179 | |
| 180 void Initialize(TraceBuffer* trace_buffer); | |
| 181 const uint8_t* GetCategoryGroupEnabled(const char* category_group); | |
| 182 static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag); | |
| 183 uint64_t AddTraceEvent(char phase, const uint8_t* category_enabled_flag, | |
| 184 const char* name, const char* scope, uint64_t id, | |
| 185 uint64_t bind_id, int32_t num_args, | |
| 186 const char** arg_names, const uint8_t* arg_types, | |
| 187 const uint64_t* arg_values, unsigned int flags); | |
| 188 void UpdateTraceEventDuration(const uint8_t* category_enabled_flag, | |
| 189 const char* name, uint64_t handle); | |
| 190 | |
| 191 void StartTracing(TraceConfig* trace_config); | |
| 192 void StopTracing(); | |
| 193 | |
| 194 private: | |
| 195 const uint8_t* GetCategoryGroupEnabledInternal(const char* category_group); | |
| 196 void UpdateCategoryGroupEnabledFlag(size_t category_index); | |
| 197 void UpdateCategoryGroupEnabledFlags(); | |
| 198 | |
| 199 std::unique_ptr<TraceBuffer> trace_buffer_; | |
| 200 std::unique_ptr<TraceConfig> trace_config_; | |
| 201 Mode mode_ = DISABLED; | |
| 202 }; | |
| 203 | |
| 204 } // namespace tracing | |
| 205 } // namespace platform | |
| 206 } // namespace v8 | |
| 207 | |
| 208 #endif // V8_LIBPLATFORM_V8_TRACING_H_ | |
| OLD | NEW |