v8  10.1.124 (node 18.2.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 <stddef.h>
9 #include <stdint.h>
10 
11 #include <vector>
12 
13 #include "v8-internal.h" // NOLINT(build/include_directory)
14 #include "v8-local-handle.h" // NOLINT(build/include_directory)
15 
16 namespace v8 {
17 
18 class Context;
19 class Isolate;
20 
21 namespace metrics {
22 
29 };
30 
32  int64_t bytes_before = -1;
33  int64_t bytes_after = -1;
34  int64_t bytes_freed = -1;
35 };
36 
38  int reason = -1;
57 };
58 
62 };
63 
66 };
67 
71 };
72 
75 };
76 
78  int reason = -1;
84 };
85 
87  bool async = false;
88  bool streamed = false;
89  bool success = false;
91  size_t function_count = 0;
93  int64_t cpu_duration_in_us = -1;
94 };
95 
97  bool async = false;
98  bool streamed = false;
99  bool cached = false;
100  bool deserialized = false;
101  bool lazy = false;
102  bool success = false;
103  size_t code_size_in_bytes = 0;
106  int64_t cpu_duration_in_us = -1;
107 };
108 
110  bool async = false;
111  bool success = false;
114 };
115 
117  bool lazy = false;
118  size_t code_size_in_bytes = 0;
120  int64_t cpu_duration_in_us = -1;
121 };
122 
124  size_t count = 0;
125 };
126 
127 #define V8_MAIN_THREAD_METRICS_EVENTS(V)
128  V(GarbageCollectionFullCycle)
129  V(GarbageCollectionFullMainThreadIncrementalMark)
130  V(GarbageCollectionFullMainThreadBatchedIncrementalMark)
131  V(GarbageCollectionFullMainThreadIncrementalSweep)
132  V(GarbageCollectionFullMainThreadBatchedIncrementalSweep)
133  V(GarbageCollectionYoungCycle)
134  V(WasmModuleDecoded)
135  V(WasmModuleCompiled)
136  V(WasmModuleInstantiated)
137  V(WasmModuleTieredUp)
138 
139 #define V8_THREAD_SAFE_METRICS_EVENTS(V) V(WasmModulesPerIsolate)
140 
141 /**
142  * This class serves as a base class for recording event-based metrics in V8.
143  * There a two kinds of metrics, those which are expected to be thread-safe and
144  * whose implementation is required to fulfill this requirement and those whose
145  * implementation does not have that requirement and only needs to be
146  * executable on the main thread. If such an event is triggered from a
147  * background thread, it will be delayed and executed by the foreground task
148  * runner.
149  *
150  * The thread-safe events are listed in the V8_THREAD_SAFE_METRICS_EVENTS
151  * macro above while the main thread event are listed in
152  * V8_MAIN_THREAD_METRICS_EVENTS above. For the former, a virtual method
153  * AddMainThreadEvent(const E& event, v8::Context::Token token) will be
154  * generated and for the latter AddThreadSafeEvent(const E& event).
155  *
156  * Thread-safe events are not allowed to access the context and therefore do
157  * not carry a context ID with them. These IDs can be generated using
158  * Recorder::GetContextId() and the ID will be valid throughout the lifetime
159  * of the isolate. It is not guaranteed that the ID will still resolve to
160  * a valid context using Recorder::GetContext() at the time the metric is
161  * recorded. In this case, an empty handle will be returned.
162  *
163  * The embedder is expected to call v8::Isolate::SetMetricsRecorder()
164  * providing its implementation and have the virtual methods overwritten
165  * for the events it cares about.
166  */
168  public:
169  // A unique identifier for a context in this Isolate.
170  // It is guaranteed to not be reused throughout the lifetime of the Isolate.
171  class ContextId {
172  public:
173  ContextId() : id_(kEmptyId) {}
174 
175  bool IsEmpty() const { return id_ == kEmptyId; }
176  static const ContextId Empty() { return ContextId{kEmptyId}; }
177 
178  bool operator==(const ContextId& other) const { return id_ == other.id_; }
179  bool operator!=(const ContextId& other) const { return id_ != other.id_; }
180 
181  private:
182  friend class ::v8::Context;
183  friend class ::v8::internal::Isolate;
184 
185  explicit ContextId(uintptr_t id) : id_(id) {}
186 
187  static constexpr uintptr_t kEmptyId = 0;
188  uintptr_t id_;
189  };
190 
191  virtual ~Recorder() = default;
192 
193 #define ADD_MAIN_THREAD_EVENT(E)
194  virtual void AddMainThreadEvent(const E& event, ContextId context_id) {}
196 #undef ADD_MAIN_THREAD_EVENT
197 
198 #define ADD_THREAD_SAFE_EVENT(E)
199  virtual void AddThreadSafeEvent(const E& event) {}
201 #undef ADD_THREAD_SAFE_EVENT
202 
203  virtual void NotifyIsolateDisposal() {}
204 
205  // Return the context with the given id or an empty handle if the context
206  // was already garbage collected.
208  // Return the unique id corresponding to the given context.
210 };
211 
212 /**
213  * Experimental API intended for the LongTasks UKM (crbug.com/1173527).
214  * The Reset() method should be called at the start of a potential
215  * long task. The Get() method returns durations of V8 work that
216  * happened during the task.
217  *
218  * This API is experimental and may be removed/changed in the future.
219  */
221  /**
222  * Resets durations of V8 work for the new task.
223  */
224  V8_INLINE static void Reset(Isolate* isolate) {
226  }
227 
228  /**
229  * Returns durations of V8 work that happened since the last Reset().
230  */
231  static LongTaskStats Get(Isolate* isolate);
232 
236  // Only collected with --slow-histograms
237  int64_t v8_execute_us = 0;
238 };
239 
240 } // namespace metrics
241 } // namespace v8
242 
243 #endif // V8_METRICS_H_