v8  6.7.288(node10.6.0)
V8 is Google's open source JavaScript engine
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 <fstream>
9 #include <memory>
10 #include <unordered_set>
11 #include <vector>
12 
13 #include "libplatform/libplatform-export.h"
14 #include "v8-platform.h" // NOLINT(build/include)
15 
16 namespace v8 {
17 
18 namespace base {
19 class Mutex;
20 } // namespace base
21 
22 namespace platform {
23 namespace tracing {
24 
25 const int kTraceMaxNumArgs = 2;
26 
28  public:
29  union ArgValue {
30  bool as_bool;
31  uint64_t as_uint;
32  int64_t as_int;
33  double as_double;
34  const void* as_pointer;
35  const char* as_string;
36  };
37 
39  ~TraceObject();
40  void Initialize(
41  char phase, const uint8_t* category_enabled_flag, const char* name,
42  const char* scope, uint64_t id, uint64_t bind_id, int num_args,
43  const char** arg_names, const uint8_t* arg_types,
44  const uint64_t* arg_values,
47  void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp);
49  char phase, const uint8_t* category_enabled_flag, const char* name,
50  const char* scope, uint64_t id, uint64_t bind_id, int num_args,
51  const char** arg_names, const uint8_t* arg_types,
52  const uint64_t* arg_values,
54  unsigned int flags, int pid, int tid, int64_t ts, int64_t tts,
56 
57  int pid() const { return pid_; }
58  int tid() const { return tid_; }
59  char phase() const { return phase_; }
60  const uint8_t* category_enabled_flag() const {
61  return category_enabled_flag_;
62  }
63  const char* name() const { return name_; }
64  const char* scope() const { return scope_; }
65  uint64_t id() const { return id_; }
66  uint64_t bind_id() const { return bind_id_; }
67  int num_args() const { return num_args_; }
68  const char** arg_names() { return arg_names_; }
69  uint8_t* arg_types() { return arg_types_; }
70  ArgValue* arg_values() { return arg_values_; }
72  return arg_convertables_;
73  }
74  unsigned int flags() const { return flags_; }
75  int64_t ts() { return ts_; }
76  int64_t tts() { return tts_; }
77  uint64_t duration() { return duration_; }
79 
80  private:
81  int pid_;
82  int tid_;
83  char phase_;
84  const char* name_;
85  const char* scope_;
86  const uint8_t* category_enabled_flag_;
87  uint64_t id_;
88  uint64_t bind_id_;
89  int num_args_ = 0;
90  const char* arg_names_[kTraceMaxNumArgs];
91  uint8_t arg_types_[kTraceMaxNumArgs];
92  ArgValue arg_values_[kTraceMaxNumArgs];
95  char* parameter_copy_storage_ = nullptr;
96  unsigned int flags_;
97  int64_t ts_;
98  int64_t tts_;
99  uint64_t duration_;
100  uint64_t cpu_duration_;
101 
102  // Disallow copy and assign
103  TraceObject(const TraceObject&) = delete;
104  void operator=(const TraceObject&) = delete;
105 };
106 
108  public:
110  virtual ~TraceWriter() {}
111  virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
112  virtual void Flush() = 0;
113 
114  static TraceWriter* CreateJSONTraceWriter(std::ostream& stream);
115 
116  private:
117  // Disallow copy and assign
118  TraceWriter(const TraceWriter&) = delete;
119  void operator=(const TraceWriter&) = delete;
120 };
121 
123  public:
124  explicit TraceBufferChunk(uint32_t seq);
125 
126  void Reset(uint32_t new_seq);
127  bool IsFull() const { return next_free_ == kChunkSize; }
128  TraceObject* AddTraceEvent(size_t* event_index);
129  TraceObject* GetEventAt(size_t index) { return &chunk_[index]; }
130 
131  uint32_t seq() const { return seq_; }
132  size_t size() const { return next_free_; }
133 
134  static const size_t kChunkSize = 64;
135 
136  private:
137  size_t next_free_ = 0;
138  TraceObject chunk_[kChunkSize];
139  uint32_t seq_;
140 
141  // Disallow copy and assign
142  TraceBufferChunk(const TraceBufferChunk&) = delete;
143  void operator=(const TraceBufferChunk&) = delete;
144 };
145 
147  public:
149  virtual ~TraceBuffer() {}
150 
151  virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0;
152  virtual TraceObject* GetEventByHandle(uint64_t handle) = 0;
153  virtual bool Flush() = 0;
154 
155  static const size_t kRingBufferChunks = 1024;
156 
157  static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks,
158  TraceWriter* trace_writer);
159 
160  private:
161  // Disallow copy and assign
162  TraceBuffer(const TraceBuffer&) = delete;
163  void operator=(const TraceBuffer&) = delete;
164 };
165 
166 // Options determines how the trace buffer stores data.
168  // Record until the trace buffer is full.
170 
171  // Record until the user ends the trace. The trace buffer is a fixed size
172  // and we use it as a ring buffer during recording.
174 
175  // Record until the trace buffer is full, but with a huge buffer size.
177 
178  // Echo to console. Events are discarded.
180 };
181 
183  public:
184  typedef std::vector<std::string> StringList;
185 
187 
188  TraceConfig() : enable_systrace_(false), enable_argument_filter_(false) {}
189  TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
190  bool IsSystraceEnabled() const { return enable_systrace_; }
191  bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
192 
193  void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; }
194  void EnableSystrace() { enable_systrace_ = true; }
195  void EnableArgumentFilter() { enable_argument_filter_ = true; }
196 
197  void AddIncludedCategory(const char* included_category);
198 
199  bool IsCategoryGroupEnabled(const char* category_group) const;
200 
201  private:
202  TraceRecordMode record_mode_;
203  bool enable_systrace_ : 1;
204  bool enable_argument_filter_ : 1;
205  StringList included_categories_;
206 
207  // Disallow copy and assign
208  TraceConfig(const TraceConfig&) = delete;
209  void operator=(const TraceConfig&) = delete;
210 };
211 
212 #if defined(_MSC_VER)
213 #define V8_PLATFORM_NON_EXPORTED_BASE(code)
214  __pragma(warning(suppress : 4275)) code
215 #else
216 #define V8_PLATFORM_NON_EXPORTED_BASE(code) code
217 #endif // defined(_MSC_VER)
218 
219 class V8_PLATFORM_EXPORT TracingController
221  public:
222  enum Mode { DISABLED = 0, RECORDING_MODE };
223 
224  // The pointer returned from GetCategoryGroupEnabledInternal() points to a
225  // value with zero or more of the following bits. Used in this class only.
226  // The TRACE_EVENT macros should only use the value as a bool.
227  // These values must be in sync with macro values in TraceEvent.h in Blink.
229  // Category group enabled for the recording mode.
231  // Category group enabled by SetEventCallbackEnabled().
233  // Category group enabled to export events to ETW.
235  };
236 
238  ~TracingController() override;
239  void Initialize(TraceBuffer* trace_buffer);
240 
241  // v8::TracingController implementation.
242  const uint8_t* GetCategoryGroupEnabled(const char* category_group) override;
243  uint64_t AddTraceEvent(
244  char phase, const uint8_t* category_enabled_flag, const char* name,
245  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
246  const char** arg_names, const uint8_t* arg_types,
247  const uint64_t* arg_values,
249  unsigned int flags) override;
251  char phase, const uint8_t* category_enabled_flag, const char* name,
252  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
253  const char** arg_names, const uint8_t* arg_types,
254  const uint64_t* arg_values,
256  unsigned int flags, int64_t timestamp) override;
257  void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
258  const char* name, uint64_t handle) override;
260  v8::TracingController::TraceStateObserver* observer) override;
262  v8::TracingController::TraceStateObserver* observer) override;
263 
264  void StartTracing(TraceConfig* trace_config);
265  void StopTracing();
266 
267  static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
268 
269  protected:
270  virtual int64_t CurrentTimestampMicroseconds();
271  virtual int64_t CurrentCpuTimestampMicroseconds();
272 
273  private:
274  const uint8_t* GetCategoryGroupEnabledInternal(const char* category_group);
275  void UpdateCategoryGroupEnabledFlag(size_t category_index);
276  void UpdateCategoryGroupEnabledFlags();
277 
278  std::unique_ptr<TraceBuffer> trace_buffer_;
279  std::unique_ptr<TraceConfig> trace_config_;
280  std::unique_ptr<base::Mutex> mutex_;
281  std::unordered_set<v8::TracingController::TraceStateObserver*> observers_;
282  Mode mode_ = DISABLED;
283 
284  // Disallow copy and assign
285  TracingController(const TracingController&) = delete;
286  void operator=(const TracingController&) = delete;
287 };
288 
289 #undef V8_PLATFORM_NON_EXPORTED_BASE
290 
291 } // namespace tracing
292 } // namespace platform
293 } // namespace v8
294 
295 #endif // V8_LIBPLATFORM_V8_TRACING_H_
void UpdateDuration(int64_t timestamp, int64_t cpu_timestamp)
TraceObject * GetEventAt(size_t index)
Definition: v8-tracing.h:129
bool IsCategoryGroupEnabled(const char *category_group) const
const uint8_t * category_enabled_flag() const
Definition: v8-tracing.h:60
void AddTraceStateObserver(v8::TracingController::TraceStateObserver *observer) override
void Initialize(TraceBuffer *trace_buffer)
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)
#define V8_PLATFORM_EXPORT
static const char * GetCategoryGroupName(const uint8_t *category_enabled_flag)
void UpdateTraceEventDuration(const uint8_t *category_enabled_flag, const char *name, uint64_t handle) override
virtual void AppendTraceEvent(TraceObject *trace_event)=0
void SetTraceRecordMode(TraceRecordMode mode)
Definition: v8-tracing.h:193
virtual TraceObject * AddTraceEvent(uint64_t *handle)=0
virtual TraceObject * GetEventByHandle(uint64_t handle)=0
const int kTraceMaxNumArgs
Definition: v8-tracing.h:25
static const size_t kRingBufferChunks
Definition: v8-tracing.h:155
static TraceConfig * CreateDefaultTraceConfig()
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
std::vector< std::string > StringList
Definition: v8-tracing.h:184
void AddIncludedCategory(const char *included_category)
static TraceWriter * CreateJSONTraceWriter(std::ostream &stream)
void StartTracing(TraceConfig *trace_config)
#define V8_PLATFORM_NON_EXPORTED_BASE(code)
Definition: v8-tracing.h:216
constexpr uint32_t CurrentValueSerializerFormatVersion()
TraceRecordMode GetTraceRecordMode() const
Definition: v8-tracing.h:189
const uint8_t * GetCategoryGroupEnabled(const char *category_group) override
TraceObject * AddTraceEvent(size_t *event_index)
const char * scope() const
Definition: v8-tracing.h:64
virtual int64_t CurrentCpuTimestampMicroseconds()
void RemoveTraceStateObserver(v8::TracingController::TraceStateObserver *observer) 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
const char * name() const
Definition: v8-tracing.h:63
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)
static TraceBuffer * CreateTraceBufferRingBuffer(size_t max_chunks, TraceWriter *trace_writer)