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 <atomic> 9 #include <fstream> 10 #include <memory> 11 #include <unordered_set> 12 #include <vector> 13 14 #include "libplatform/libplatform-export.h" 15 #include "v8-platform.h" // NOLINT(build/include_directory) 16 17 namespace perfetto { 18 namespace trace_processor { 19 class TraceProcessorStorage; 20 } 21 class TracingSession; 22 } 23 24 namespace v8 { 25 26 namespace base { 27 class Mutex; 28 } // namespace base 29 30 namespace platform { 31 namespace tracing { 32 33 class TraceEventListener; 34 35 const int kTraceMaxNumArgs = 2; 36 37 class V8_PLATFORM_EXPORT TraceObject { 38 public: 39 union ArgValue { 40 V8_DEPRECATED("use as_uint ? true : false") bool as_bool; 41 uint64_t as_uint; 42 int64_t as_int; 43 double as_double; 44 const void* as_pointer; 45 const char* as_string; 46 }; 47 48 TraceObject() = default; 49 ~TraceObject(); 50 void Initialize( 51 char phase, const uint8_t* category_enabled_flag, const char* name, 52 const char* scope, uint64_t id, uint64_t bind_id, int num_args, 53 const char** arg_names, const uint8_t* arg_types, 54 const uint64_t* arg_values, 55 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables, 56 unsigned int flags, int64_t timestamp, int64_t cpu_timestamp); 57 void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp); 58 void InitializeForTesting( 59 char phase, const uint8_t* category_enabled_flag, const char* name, 60 const char* scope, uint64_t id, uint64_t bind_id, int num_args, 61 const char** arg_names, const uint8_t* arg_types, 62 const uint64_t* arg_values, 63 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables, 64 unsigned int flags, int pid, int tid, int64_t ts, int64_t tts, 65 uint64_t duration, uint64_t cpu_duration); 66 pid()67 int pid() const { return pid_; } tid()68 int tid() const { return tid_; } phase()69 char phase() const { return phase_; } category_enabled_flag()70 const uint8_t* category_enabled_flag() const { 71 return category_enabled_flag_; 72 } name()73 const char* name() const { return name_; } scope()74 const char* scope() const { return scope_; } id()75 uint64_t id() const { return id_; } bind_id()76 uint64_t bind_id() const { return bind_id_; } num_args()77 int num_args() const { return num_args_; } arg_names()78 const char** arg_names() { return arg_names_; } arg_types()79 uint8_t* arg_types() { return arg_types_; } arg_values()80 ArgValue* arg_values() { return arg_values_; } arg_convertables()81 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables() { 82 return arg_convertables_; 83 } flags()84 unsigned int flags() const { return flags_; } ts()85 int64_t ts() { return ts_; } tts()86 int64_t tts() { return tts_; } duration()87 uint64_t duration() { return duration_; } cpu_duration()88 uint64_t cpu_duration() { return cpu_duration_; } 89 90 private: 91 int pid_; 92 int tid_; 93 char phase_; 94 const char* name_; 95 const char* scope_; 96 const uint8_t* category_enabled_flag_; 97 uint64_t id_; 98 uint64_t bind_id_; 99 int num_args_ = 0; 100 const char* arg_names_[kTraceMaxNumArgs]; 101 uint8_t arg_types_[kTraceMaxNumArgs]; 102 ArgValue arg_values_[kTraceMaxNumArgs]; 103 std::unique_ptr<v8::ConvertableToTraceFormat> 104 arg_convertables_[kTraceMaxNumArgs]; 105 char* parameter_copy_storage_ = nullptr; 106 unsigned int flags_; 107 int64_t ts_; 108 int64_t tts_; 109 uint64_t duration_; 110 uint64_t cpu_duration_; 111 112 // Disallow copy and assign 113 TraceObject(const TraceObject&) = delete; 114 void operator=(const TraceObject&) = delete; 115 }; 116 117 class V8_PLATFORM_EXPORT TraceWriter { 118 public: 119 TraceWriter() = default; 120 virtual ~TraceWriter() = default; 121 virtual void AppendTraceEvent(TraceObject* trace_event) = 0; 122 virtual void Flush() = 0; 123 124 static TraceWriter* CreateJSONTraceWriter(std::ostream& stream); 125 static TraceWriter* CreateJSONTraceWriter(std::ostream& stream, 126 const std::string& tag); 127 128 private: 129 // Disallow copy and assign 130 TraceWriter(const TraceWriter&) = delete; 131 void operator=(const TraceWriter&) = delete; 132 }; 133 134 class V8_PLATFORM_EXPORT TraceBufferChunk { 135 public: 136 explicit TraceBufferChunk(uint32_t seq); 137 138 void Reset(uint32_t new_seq); IsFull()139 bool IsFull() const { return next_free_ == kChunkSize; } 140 TraceObject* AddTraceEvent(size_t* event_index); GetEventAt(size_t index)141 TraceObject* GetEventAt(size_t index) { return &chunk_[index]; } 142 seq()143 uint32_t seq() const { return seq_; } size()144 size_t size() const { return next_free_; } 145 146 static const size_t kChunkSize = 64; 147 148 private: 149 size_t next_free_ = 0; 150 TraceObject chunk_[kChunkSize]; 151 uint32_t seq_; 152 153 // Disallow copy and assign 154 TraceBufferChunk(const TraceBufferChunk&) = delete; 155 void operator=(const TraceBufferChunk&) = delete; 156 }; 157 158 class V8_PLATFORM_EXPORT TraceBuffer { 159 public: 160 TraceBuffer() = default; 161 virtual ~TraceBuffer() = default; 162 163 virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0; 164 virtual TraceObject* GetEventByHandle(uint64_t handle) = 0; 165 virtual bool Flush() = 0; 166 167 static const size_t kRingBufferChunks = 1024; 168 169 static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks, 170 TraceWriter* trace_writer); 171 172 private: 173 // Disallow copy and assign 174 TraceBuffer(const TraceBuffer&) = delete; 175 void operator=(const TraceBuffer&) = delete; 176 }; 177 178 // Options determines how the trace buffer stores data. 179 enum TraceRecordMode { 180 // Record until the trace buffer is full. 181 RECORD_UNTIL_FULL, 182 183 // Record until the user ends the trace. The trace buffer is a fixed size 184 // and we use it as a ring buffer during recording. 185 RECORD_CONTINUOUSLY, 186 187 // Record until the trace buffer is full, but with a huge buffer size. 188 RECORD_AS_MUCH_AS_POSSIBLE, 189 190 // Echo to console. Events are discarded. 191 ECHO_TO_CONSOLE, 192 }; 193 194 class V8_PLATFORM_EXPORT TraceConfig { 195 public: 196 typedef std::vector<std::string> StringList; 197 198 static TraceConfig* CreateDefaultTraceConfig(); 199 TraceConfig()200 TraceConfig() : enable_systrace_(false), enable_argument_filter_(false) {} GetTraceRecordMode()201 TraceRecordMode GetTraceRecordMode() const { return record_mode_; } GetEnabledCategories()202 const StringList& GetEnabledCategories() const { 203 return included_categories_; 204 } IsSystraceEnabled()205 bool IsSystraceEnabled() const { return enable_systrace_; } IsArgumentFilterEnabled()206 bool IsArgumentFilterEnabled() const { return enable_argument_filter_; } 207 SetTraceRecordMode(TraceRecordMode mode)208 void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; } EnableSystrace()209 void EnableSystrace() { enable_systrace_ = true; } EnableArgumentFilter()210 void EnableArgumentFilter() { enable_argument_filter_ = true; } 211 212 void AddIncludedCategory(const char* included_category); 213 214 bool IsCategoryGroupEnabled(const char* category_group) const; 215 216 private: 217 TraceRecordMode record_mode_; 218 bool enable_systrace_ : 1; 219 bool enable_argument_filter_ : 1; 220 StringList included_categories_; 221 222 // Disallow copy and assign 223 TraceConfig(const TraceConfig&) = delete; 224 void operator=(const TraceConfig&) = delete; 225 }; 226 227 #if defined(_MSC_VER) 228 #define V8_PLATFORM_NON_EXPORTED_BASE(code) \ 229 __pragma(warning(suppress : 4275)) code 230 #else 231 #define V8_PLATFORM_NON_EXPORTED_BASE(code) code 232 #endif // defined(_MSC_VER) 233 234 class V8_PLATFORM_EXPORT TracingController V8_PLATFORM_NON_EXPORTED_BASE(v8::TracingController)235 : public V8_PLATFORM_NON_EXPORTED_BASE(v8::TracingController) { 236 public: 237 TracingController(); 238 ~TracingController() override; 239 240 #if defined(V8_USE_PERFETTO) 241 // Must be called before StartTracing() if V8_USE_PERFETTO is true. Provides 242 // the output stream for the JSON trace data. 243 void InitializeForPerfetto(std::ostream* output_stream); 244 // Provide an optional listener for testing that will receive trace events. 245 // Must be called before StartTracing(). 246 void SetTraceEventListenerForTesting(TraceEventListener* listener); 247 #else // defined(V8_USE_PERFETTO) 248 // The pointer returned from GetCategoryGroupEnabled() points to a value with 249 // zero or more of the following bits. Used in this class only. The 250 // TRACE_EVENT macros should only use the value as a bool. These values must 251 // be in sync with macro values in TraceEvent.h in Blink. 252 enum CategoryGroupEnabledFlags { 253 // Category group enabled for the recording mode. 254 ENABLED_FOR_RECORDING = 1 << 0, 255 // Category group enabled by SetEventCallbackEnabled(). 256 ENABLED_FOR_EVENT_CALLBACK = 1 << 2, 257 // Category group enabled to export events to ETW. 258 ENABLED_FOR_ETW_EXPORT = 1 << 3 259 }; 260 261 // Takes ownership of |trace_buffer|. 262 void Initialize(TraceBuffer* trace_buffer); 263 264 // v8::TracingController implementation. 265 const uint8_t* GetCategoryGroupEnabled(const char* category_group) override; 266 uint64_t AddTraceEvent( 267 char phase, const uint8_t* category_enabled_flag, const char* name, 268 const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args, 269 const char** arg_names, const uint8_t* arg_types, 270 const uint64_t* arg_values, 271 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables, 272 unsigned int flags) override; 273 uint64_t AddTraceEventWithTimestamp( 274 char phase, const uint8_t* category_enabled_flag, const char* name, 275 const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args, 276 const char** arg_names, const uint8_t* arg_types, 277 const uint64_t* arg_values, 278 std::unique_ptr<v8::ConvertableToTraceFormat>* arg_convertables, 279 unsigned int flags, int64_t timestamp) override; 280 void UpdateTraceEventDuration(const uint8_t* category_enabled_flag, 281 const char* name, uint64_t handle) override; 282 283 static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag); 284 #endif // !defined(V8_USE_PERFETTO) 285 286 void AddTraceStateObserver( 287 v8::TracingController::TraceStateObserver* observer) override; 288 void RemoveTraceStateObserver( 289 v8::TracingController::TraceStateObserver* observer) override; 290 291 void StartTracing(TraceConfig* trace_config); 292 void StopTracing(); 293 294 protected: 295 #if !defined(V8_USE_PERFETTO) 296 virtual int64_t CurrentTimestampMicroseconds(); 297 virtual int64_t CurrentCpuTimestampMicroseconds(); 298 #endif // !defined(V8_USE_PERFETTO) 299 300 private: 301 #if !defined(V8_USE_PERFETTO) 302 void UpdateCategoryGroupEnabledFlag(size_t category_index); 303 void UpdateCategoryGroupEnabledFlags(); 304 #endif // !defined(V8_USE_PERFETTO) 305 306 std::unique_ptr<base::Mutex> mutex_; 307 std::unique_ptr<TraceConfig> trace_config_; 308 std::atomic_bool recording_{false}; 309 std::unordered_set<v8::TracingController::TraceStateObserver*> observers_; 310 311 #if defined(V8_USE_PERFETTO) 312 std::ostream* output_stream_ = nullptr; 313 std::unique_ptr<perfetto::trace_processor::TraceProcessorStorage> 314 trace_processor_; 315 TraceEventListener* listener_for_testing_ = nullptr; 316 std::unique_ptr<perfetto::TracingSession> tracing_session_; 317 #else // !defined(V8_USE_PERFETTO) 318 std::unique_ptr<TraceBuffer> trace_buffer_; 319 #endif // !defined(V8_USE_PERFETTO) 320 321 // Disallow copy and assign 322 TracingController(const TracingController&) = delete; 323 void operator=(const TracingController&) = delete; 324 }; 325 326 #undef V8_PLATFORM_NON_EXPORTED_BASE 327 328 } // namespace tracing 329 } // namespace platform 330 } // namespace v8 331 332 #endif // V8_LIBPLATFORM_V8_TRACING_H_ 333