v8  9.4.146 (node 16.13.0)
V8 is Google's open source JavaScript engine
v8-metrics.h
Go to the documentation of this file.
1 // Copyright 2020 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_METRICS_H_
6 #define V8_METRICS_H_
7 
8 #include "v8-internal.h" // NOLINT(build/include_directory)
9 #include "v8.h" // NOLINT(build/include_directory)
10 
11 namespace v8 {
12 namespace metrics {
13 
19 };
20 
22  int64_t bytes_before = -1;
23  int64_t bytes_after = -1;
24  int64_t bytes_freed = -1;
25 };
26 
46 };
47 
51 };
52 
55 };
56 
60 };
61 
64 };
65 
72 };
73 
75  bool async = false;
76  bool streamed = false;
77  bool success = false;
79  size_t function_count = 0;
81  int64_t cpu_duration_in_us = -1;
82 };
83 
85  bool async = false;
86  bool streamed = false;
87  bool cached = false;
88  bool deserialized = false;
89  bool lazy = false;
90  bool success = false;
91  size_t code_size_in_bytes = 0;
94  int64_t cpu_duration_in_us = -1;
95 };
96 
98  bool async = false;
99  bool success = false;
102 };
103 
105  bool lazy = false;
106  size_t code_size_in_bytes = 0;
108  int64_t cpu_duration_in_us = -1;
109 };
110 
112  size_t count = 0;
113 };
114 
115 #define V8_MAIN_THREAD_METRICS_EVENTS(V)
116  V(GarbageCollectionFullCycle)
117  V(GarbageCollectionFullMainThreadIncrementalMark)
118  V(GarbageCollectionFullMainThreadBatchedIncrementalMark)
119  V(GarbageCollectionFullMainThreadIncrementalSweep)
120  V(GarbageCollectionFullMainThreadBatchedIncrementalSweep)
121  V(GarbageCollectionYoungCycle)
122  V(WasmModuleDecoded)
123  V(WasmModuleCompiled)
124  V(WasmModuleInstantiated)
125  V(WasmModuleTieredUp)
126 
127 #define V8_THREAD_SAFE_METRICS_EVENTS(V) V(WasmModulesPerIsolate)
128 
129 /**
130  * This class serves as a base class for recording event-based metrics in V8.
131  * There a two kinds of metrics, those which are expected to be thread-safe and
132  * whose implementation is required to fulfill this requirement and those whose
133  * implementation does not have that requirement and only needs to be
134  * executable on the main thread. If such an event is triggered from a
135  * background thread, it will be delayed and executed by the foreground task
136  * runner.
137  *
138  * The thread-safe events are listed in the V8_THREAD_SAFE_METRICS_EVENTS
139  * macro above while the main thread event are listed in
140  * V8_MAIN_THREAD_METRICS_EVENTS above. For the former, a virtual method
141  * AddMainThreadEvent(const E& event, v8::Context::Token token) will be
142  * generated and for the latter AddThreadSafeEvent(const E& event).
143  *
144  * Thread-safe events are not allowed to access the context and therefore do
145  * not carry a context ID with them. These IDs can be generated using
146  * Recorder::GetContextId() and the ID will be valid throughout the lifetime
147  * of the isolate. It is not guaranteed that the ID will still resolve to
148  * a valid context using Recorder::GetContext() at the time the metric is
149  * recorded. In this case, an empty handle will be returned.
150  *
151  * The embedder is expected to call v8::Isolate::SetMetricsRecorder()
152  * providing its implementation and have the virtual methods overwritten
153  * for the events it cares about.
154  */
156  public:
157  // A unique identifier for a context in this Isolate.
158  // It is guaranteed to not be reused throughout the lifetime of the Isolate.
159  class ContextId {
160  public:
161  ContextId() : id_(kEmptyId) {}
162 
163  bool IsEmpty() const { return id_ == kEmptyId; }
164  static const ContextId Empty() { return ContextId{kEmptyId}; }
165 
166  bool operator==(const ContextId& other) const { return id_ == other.id_; }
167  bool operator!=(const ContextId& other) const { return id_ != other.id_; }
168 
169  private:
170  friend class ::v8::Context;
171  friend class ::v8::internal::Isolate;
172 
173  explicit ContextId(uintptr_t id) : id_(id) {}
174 
175  static constexpr uintptr_t kEmptyId = 0;
176  uintptr_t id_;
177  };
178 
179  virtual ~Recorder() = default;
180 
181 #define ADD_MAIN_THREAD_EVENT(E)
182  virtual void AddMainThreadEvent(const E& event, ContextId context_id) {}
184 #undef ADD_MAIN_THREAD_EVENT
185 
186 #define ADD_THREAD_SAFE_EVENT(E)
187  virtual void AddThreadSafeEvent(const E& event) {}
189 #undef ADD_THREAD_SAFE_EVENT
190 
191  virtual void NotifyIsolateDisposal() {}
192 
193  // Return the context with the given id or an empty handle if the context
194  // was already garbage collected.
196  // Return the unique id corresponding to the given context.
198 };
199 
200 /**
201  * Experimental API intended for the LongTasks UKM (crbug.com/1173527).
202  * The Reset() method should be called at the start of a potential
203  * long task. The Get() method returns durations of V8 work that
204  * happened during the task.
205  *
206  * This API is experimental and may be removed/changed in the future.
207  */
209  /**
210  * Resets durations of V8 work for the new task.
211  */
212  V8_INLINE static void Reset(Isolate* isolate) {
214  }
215 
216  /**
217  * Returns durations of V8 work that happened since the last Reset().
218  */
219  static LongTaskStats Get(Isolate* isolate);
220 
224 };
225 
226 } // namespace metrics
227 } // namespace v8
228 
229 #endif // V8_METRICS_H_