v8  5.5.372(node7.10.1)
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 "v8-platform.h" // NOLINT(build/include)
14 
15 namespace v8 {
16 
17 namespace base {
18 class Mutex;
19 } // namespace base
20 
21 namespace platform {
22 namespace tracing {
23 
24 const int kTraceMaxNumArgs = 2;
25 
26 class TraceObject {
27  public:
28  union ArgValue {
29  bool as_bool;
30  uint64_t as_uint;
31  int64_t as_int;
32  double as_double;
33  const void* as_pointer;
34  const char* as_string;
35  };
36 
38  ~TraceObject();
39  void Initialize(
40  char phase, const uint8_t* category_enabled_flag, const char* name,
41  const char* scope, uint64_t id, uint64_t bind_id, int num_args,
42  const char** arg_names, const uint8_t* arg_types,
43  const uint64_t* arg_values,
45  unsigned int flags);
46  void UpdateDuration();
48  char phase, const uint8_t* category_enabled_flag, const char* name,
49  const char* scope, uint64_t id, uint64_t bind_id, int num_args,
50  const char** arg_names, const uint8_t* arg_types,
51  const uint64_t* arg_values,
53  unsigned int flags, int pid, int tid, int64_t ts, int64_t tts,
55 
56  int pid() const { return pid_; }
57  int tid() const { return tid_; }
58  char phase() const { return phase_; }
59  const uint8_t* category_enabled_flag() const {
60  return category_enabled_flag_;
61  }
62  const char* name() const { return name_; }
63  const char* scope() const { return scope_; }
64  uint64_t id() const { return id_; }
65  uint64_t bind_id() const { return bind_id_; }
66  int num_args() const { return num_args_; }
67  const char** arg_names() { return arg_names_; }
68  uint8_t* arg_types() { return arg_types_; }
69  ArgValue* arg_values() { return arg_values_; }
71  return arg_convertables_;
72  }
73  unsigned int flags() const { return flags_; }
74  int64_t ts() { return ts_; }
75  int64_t tts() { return tts_; }
76  uint64_t duration() { return duration_; }
78 
79  private:
80  int pid_;
81  int tid_;
82  char phase_;
83  const char* name_;
84  const char* scope_;
85  const uint8_t* category_enabled_flag_;
86  uint64_t id_;
87  uint64_t bind_id_;
88  int num_args_ = 0;
89  const char* arg_names_[kTraceMaxNumArgs];
90  uint8_t arg_types_[kTraceMaxNumArgs];
91  ArgValue arg_values_[kTraceMaxNumArgs];
94  char* parameter_copy_storage_ = nullptr;
95  unsigned int flags_;
96  int64_t ts_;
97  int64_t tts_;
98  uint64_t duration_;
99  uint64_t cpu_duration_;
100 
101  // Disallow copy and assign
102  TraceObject(const TraceObject&) = delete;
103  void operator=(const TraceObject&) = delete;
104 };
105 
106 class TraceWriter {
107  public:
109  virtual ~TraceWriter() {}
110  virtual void AppendTraceEvent(TraceObject* trace_event) = 0;
111  virtual void Flush() = 0;
112 
113  static TraceWriter* CreateJSONTraceWriter(std::ostream& stream);
114 
115  private:
116  // Disallow copy and assign
117  TraceWriter(const TraceWriter&) = delete;
118  void operator=(const TraceWriter&) = delete;
119 };
120 
122  public:
123  explicit TraceBufferChunk(uint32_t seq);
124 
125  void Reset(uint32_t new_seq);
126  bool IsFull() const { return next_free_ == kChunkSize; }
127  TraceObject* AddTraceEvent(size_t* event_index);
128  TraceObject* GetEventAt(size_t index) { return &chunk_[index]; }
129 
130  uint32_t seq() const { return seq_; }
131  size_t size() const { return next_free_; }
132 
133  static const size_t kChunkSize = 64;
134 
135  private:
136  size_t next_free_ = 0;
137  TraceObject chunk_[kChunkSize];
138  uint32_t seq_;
139 
140  // Disallow copy and assign
141  TraceBufferChunk(const TraceBufferChunk&) = delete;
142  void operator=(const TraceBufferChunk&) = delete;
143 };
144 
145 class TraceBuffer {
146  public:
148  virtual ~TraceBuffer() {}
149 
150  virtual TraceObject* AddTraceEvent(uint64_t* handle) = 0;
151  virtual TraceObject* GetEventByHandle(uint64_t handle) = 0;
152  virtual bool Flush() = 0;
153 
154  static const size_t kRingBufferChunks = 1024;
155 
156  static TraceBuffer* CreateTraceBufferRingBuffer(size_t max_chunks,
157  TraceWriter* trace_writer);
158 
159  private:
160  // Disallow copy and assign
161  TraceBuffer(const TraceBuffer&) = delete;
162  void operator=(const TraceBuffer&) = delete;
163 };
164 
165 // Options determines how the trace buffer stores data.
167  // Record until the trace buffer is full.
169 
170  // Record until the user ends the trace. The trace buffer is a fixed size
171  // and we use it as a ring buffer during recording.
173 
174  // Record until the trace buffer is full, but with a huge buffer size.
176 
177  // Echo to console. Events are discarded.
179 };
180 
181 class TraceConfig {
182  public:
183  typedef std::vector<std::string> StringList;
184 
186 
188  : enable_sampling_(false),
189  enable_systrace_(false),
190  enable_argument_filter_(false) {}
191  TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
192  bool IsSamplingEnabled() const { return enable_sampling_; }
193  bool IsSystraceEnabled() const { return enable_systrace_; }
194  bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
195 
196  void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; }
197  void EnableSampling() { enable_sampling_ = true; }
198  void EnableSystrace() { enable_systrace_ = true; }
199  void EnableArgumentFilter() { enable_argument_filter_ = true; }
200 
201  void AddIncludedCategory(const char* included_category);
202  void AddExcludedCategory(const char* excluded_category);
203 
204  bool IsCategoryGroupEnabled(const char* category_group) const;
205 
206  private:
207  TraceRecordMode record_mode_;
208  bool enable_sampling_ : 1;
209  bool enable_systrace_ : 1;
210  bool enable_argument_filter_ : 1;
211  StringList included_categories_;
212  StringList excluded_categories_;
213 
214  // Disallow copy and assign
215  TraceConfig(const TraceConfig&) = delete;
216  void operator=(const TraceConfig&) = delete;
217 };
218 
219 class TracingController {
220  public:
221  enum Mode { DISABLED = 0, RECORDING_MODE };
222 
223  // The pointer returned from GetCategoryGroupEnabledInternal() points to a
224  // value with zero or more of the following bits. Used in this class only.
225  // The TRACE_EVENT macros should only use the value as a bool.
226  // These values must be in sync with macro values in TraceEvent.h in Blink.
228  // Category group enabled for the recording mode.
230  // Category group enabled by SetEventCallbackEnabled().
232  // Category group enabled to export events to ETW.
234  };
235 
238  void Initialize(TraceBuffer* trace_buffer);
239  const uint8_t* GetCategoryGroupEnabled(const char* category_group);
240  static const char* GetCategoryGroupName(const uint8_t* category_enabled_flag);
241  uint64_t AddTraceEvent(
242  char phase, const uint8_t* category_enabled_flag, const char* name,
243  const char* scope, uint64_t id, uint64_t bind_id, int32_t num_args,
244  const char** arg_names, const uint8_t* arg_types,
245  const uint64_t* arg_values,
247  unsigned int flags);
248  void UpdateTraceEventDuration(const uint8_t* category_enabled_flag,
249  const char* name, uint64_t handle);
250 
251  void StartTracing(TraceConfig* trace_config);
252  void StopTracing();
253 
256 
257  private:
258  const uint8_t* GetCategoryGroupEnabledInternal(const char* category_group);
259  void UpdateCategoryGroupEnabledFlag(size_t category_index);
260  void UpdateCategoryGroupEnabledFlags();
261 
262  std::unique_ptr<TraceBuffer> trace_buffer_;
263  std::unique_ptr<TraceConfig> trace_config_;
264  std::unique_ptr<base::Mutex> mutex_;
265  std::unordered_set<Platform::TraceStateObserver*> observers_;
266  Mode mode_ = DISABLED;
267 
268  // Disallow copy and assign
269  TracingController(const TracingController&) = delete;
270  void operator=(const TracingController&) = delete;
271 };
272 
273 } // namespace tracing
274 } // namespace platform
275 } // namespace v8
276 
277 #endif // V8_LIBPLATFORM_V8_TRACING_H_
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)
TraceObject * GetEventAt(size_t index)
Definition: v8-tracing.h:128
bool IsCategoryGroupEnabled(const char *category_group) const
const uint8_t * category_enabled_flag() const
Definition: v8-tracing.h:59
void Initialize(TraceBuffer *trace_buffer)
void AddExcludedCategory(const char *excluded_category)
void UpdateTraceEventDuration(const uint8_t *category_enabled_flag, const char *name, uint64_t handle)
static const char * GetCategoryGroupName(const uint8_t *category_enabled_flag)
virtual void AppendTraceEvent(TraceObject *trace_event)=0
const uint8_t * GetCategoryGroupEnabled(const char *category_group)
Definition: libplatform.h:11
void SetTraceRecordMode(TraceRecordMode mode)
Definition: v8-tracing.h:196
virtual TraceObject * AddTraceEvent(uint64_t *handle)=0
void AddTraceStateObserver(Platform::TraceStateObserver *observer)
virtual TraceObject * GetEventByHandle(uint64_t handle)=0
const int kTraceMaxNumArgs
Definition: v8-tracing.h:24
void RemoveTraceStateObserver(Platform::TraceStateObserver *observer)
static const size_t kRingBufferChunks
Definition: v8-tracing.h:154
static TraceConfig * CreateDefaultTraceConfig()
std::vector< std::string > StringList
Definition: v8-tracing.h:183
void AddIncludedCategory(const char *included_category)
static TraceWriter * CreateJSONTraceWriter(std::ostream &stream)
void StartTracing(TraceConfig *trace_config)
TraceRecordMode GetTraceRecordMode() const
Definition: v8-tracing.h:191
TraceObject * AddTraceEvent(size_t *event_index)
const char * scope() const
Definition: v8-tracing.h:63
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)
const char * name() const
Definition: v8-tracing.h:62
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)