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