v8  9.0.257(node16.0.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.h" // NOLINT(build/include_directory)
9 
10 namespace v8 {
11 namespace metrics {
12 
18 };
19 
21  int64_t bytes_before = -1;
22  int64_t bytes_after = -1;
23  int64_t bytes_freed = -1;
24 };
25 
45 };
46 
50 };
51 
55 };
56 
63 };
64 
66  bool async = false;
67  bool streamed = false;
68  bool success = false;
70  size_t function_count = 0;
72 };
73 
75  bool async = false;
76  bool streamed = false;
77  bool cached = false;
78  bool deserialized = false;
79  bool lazy = false;
80  bool success = false;
81  size_t code_size_in_bytes = 0;
84 };
85 
87  bool async = false;
88  bool success = false;
91 };
92 
94  bool lazy = false;
95  size_t code_size_in_bytes = 0;
97 };
98 
100  size_t count = 0;
101 };
102 
103 #define V8_MAIN_THREAD_METRICS_EVENTS(V)
104  V(GarbageCollectionFullCycle)
105  V(GarbageCollectionFullMainThreadIncrementalMark)
106  V(GarbageCollectionFullMainThreadIncrementalSweep)
107  V(GarbageCollectionYoungCycle)
108  V(WasmModuleDecoded)
109  V(WasmModuleCompiled)
110  V(WasmModuleInstantiated)
111  V(WasmModuleTieredUp)
112 
113 #define V8_THREAD_SAFE_METRICS_EVENTS(V) V(WasmModulesPerIsolate)
114 
115 /**
116  * This class serves as a base class for recording event-based metrics in V8.
117  * There a two kinds of metrics, those which are expected to be thread-safe and
118  * whose implementation is required to fulfill this requirement and those whose
119  * implementation does not have that requirement and only needs to be
120  * executable on the main thread. If such an event is triggered from a
121  * background thread, it will be delayed and executed by the foreground task
122  * runner.
123  *
124  * The thread-safe events are listed in the V8_THREAD_SAFE_METRICS_EVENTS
125  * macro above while the main thread event are listed in
126  * V8_MAIN_THREAD_METRICS_EVENTS above. For the former, a virtual method
127  * AddMainThreadEvent(const E& event, v8::Context::Token token) will be
128  * generated and for the latter AddThreadSafeEvent(const E& event).
129  *
130  * Thread-safe events are not allowed to access the context and therefore do
131  * not carry a context ID with them. These IDs can be generated using
132  * Recorder::GetContextId() and the ID will be valid throughout the lifetime
133  * of the isolate. It is not guaranteed that the ID will still resolve to
134  * a valid context using Recorder::GetContext() at the time the metric is
135  * recorded. In this case, an empty handle will be returned.
136  *
137  * The embedder is expected to call v8::Isolate::SetMetricsRecorder()
138  * providing its implementation and have the virtual methods overwritten
139  * for the events it cares about.
140  */
142  public:
143  // A unique identifier for a context in this Isolate.
144  // It is guaranteed to not be reused throughout the lifetime of the Isolate.
145  class ContextId {
146  public:
147  ContextId() : id_(kEmptyId) {}
148 
149  bool IsEmpty() const { return id_ == kEmptyId; }
150  static const ContextId Empty() { return ContextId{kEmptyId}; }
151 
152  bool operator==(const ContextId& other) const { return id_ == other.id_; }
153  bool operator!=(const ContextId& other) const { return id_ != other.id_; }
154 
155  private:
156  friend class ::v8::Context;
157  friend class ::v8::internal::Isolate;
158 
159  explicit ContextId(uintptr_t id) : id_(id) {}
160 
161  static constexpr uintptr_t kEmptyId = 0;
162  uintptr_t id_;
163  };
164 
165  virtual ~Recorder() = default;
166 
167 #define ADD_MAIN_THREAD_EVENT(E)
168  virtual void AddMainThreadEvent(const E& event, ContextId context_id) {}
170 #undef ADD_MAIN_THREAD_EVENT
171 
172 #define ADD_THREAD_SAFE_EVENT(E)
173  virtual void AddThreadSafeEvent(const E& event) {}
175 #undef ADD_THREAD_SAFE_EVENT
176 
177  virtual void NotifyIsolateDisposal() {}
178 
179  // Return the context with the given id or an empty handle if the context
180  // was already garbage collected.
181  static MaybeLocal<Context> GetContext(Isolate* isolate, ContextId id);
182  // Return the unique id corresponding to the given context.
183  static ContextId GetContextId(Local<Context> context);
184 };
185 
186 } // namespace metrics
187 } // namespace v8
188 
189 #endif // V8_METRICS_H_
V8_THREAD_SAFE_METRICS_EVENTS
#define V8_THREAD_SAFE_METRICS_EVENTS(V)
Definition: v8-metrics.h:113
v8::metrics::GarbageCollectionFullCycle::main_thread_atomic
GarbageCollectionPhases main_thread_atomic
Definition: v8-metrics.h:31
v8::metrics::GarbageCollectionFullCycle::main_thread_incremental_cpp
GarbageCollectionPhases main_thread_incremental_cpp
Definition: v8-metrics.h:34
v8::metrics::GarbageCollectionFullMainThreadIncrementalMark::cpp_wall_clock_duration_in_us
int64_t cpp_wall_clock_duration_in_us
Definition: v8-metrics.h:49
v8::metrics::WasmModuleDecoded::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:71
v8::metrics::GarbageCollectionFullCycle::main_thread_efficiency_cpp_in_bytes_per_us
double main_thread_efficiency_cpp_in_bytes_per_us
Definition: v8-metrics.h:44
v8::metrics::Recorder::ContextId::operator!=
bool operator!=(const ContextId &other) const
Definition: v8-metrics.h:153
v8::metrics::WasmModuleCompiled::lazy
bool lazy
Definition: v8-metrics.h:79
v8::metrics::GarbageCollectionFullMainThreadIncrementalMark::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:48
v8::metrics::Recorder::GetContextId
static ContextId GetContextId(Local< Context > context)
v8::metrics::Recorder::NotifyIsolateDisposal
virtual void NotifyIsolateDisposal()
Definition: v8-metrics.h:177
v8::metrics::GarbageCollectionYoungCycle::total_wall_clock_duration_in_us
int64_t total_wall_clock_duration_in_us
Definition: v8-metrics.h:58
v8::metrics::WasmModuleCompiled::liftoff_bailout_count
size_t liftoff_bailout_count
Definition: v8-metrics.h:82
v8::metrics::Recorder::ContextId
Definition: v8-metrics.h:145
v8::metrics::GarbageCollectionSizes::bytes_freed
int64_t bytes_freed
Definition: v8-metrics.h:23
v8::metrics::GarbageCollectionFullMainThreadIncrementalSweep::cpp_wall_clock_duration_in_us
int64_t cpp_wall_clock_duration_in_us
Definition: v8-metrics.h:54
v8::metrics::GarbageCollectionFullMainThreadIncrementalSweep
Definition: v8-metrics.h:52
v8::metrics::GarbageCollectionFullCycle::collection_rate_cpp_in_percent
double collection_rate_cpp_in_percent
Definition: v8-metrics.h:40
v8::metrics::GarbageCollectionPhases::mark_wall_clock_duration_in_us
int64_t mark_wall_clock_duration_in_us
Definition: v8-metrics.h:15
v8::metrics::WasmModuleInstantiated
Definition: v8-metrics.h:86
v8::metrics::GarbageCollectionFullCycle::total_cpp
GarbageCollectionPhases total_cpp
Definition: v8-metrics.h:28
v8::metrics::GarbageCollectionFullCycle::collection_rate_in_percent
double collection_rate_in_percent
Definition: v8-metrics.h:39
v8::metrics::Recorder::ContextId::IsEmpty
bool IsEmpty() const
Definition: v8-metrics.h:149
v8::metrics::GarbageCollectionFullCycle::main_thread_atomic_cpp
GarbageCollectionPhases main_thread_atomic_cpp
Definition: v8-metrics.h:32
v8::metrics::GarbageCollectionSizes::bytes_after
int64_t bytes_after
Definition: v8-metrics.h:22
v8::metrics::GarbageCollectionFullCycle::efficiency_cpp_in_bytes_per_us
double efficiency_cpp_in_bytes_per_us
Definition: v8-metrics.h:42
v8::metrics::Recorder::ContextId::operator==
bool operator==(const ContextId &other) const
Definition: v8-metrics.h:152
v8::metrics::GarbageCollectionYoungCycle::efficiency_in_bytes_per_us
double efficiency_in_bytes_per_us
Definition: v8-metrics.h:61
v8::metrics::GarbageCollectionYoungCycle::main_thread_efficiency_in_bytes_per_us
double main_thread_efficiency_in_bytes_per_us
Definition: v8-metrics.h:62
v8::metrics::GarbageCollectionFullCycle::main_thread_cpp
GarbageCollectionPhases main_thread_cpp
Definition: v8-metrics.h:30
v8::metrics::WasmModuleCompiled::deserialized
bool deserialized
Definition: v8-metrics.h:78
v8::metrics::GarbageCollectionYoungCycle
Definition: v8-metrics.h:57
v8::metrics::WasmModuleTieredUp
Definition: v8-metrics.h:93
v8::metrics::GarbageCollectionFullCycle::main_thread
GarbageCollectionPhases main_thread
Definition: v8-metrics.h:29
v8::metrics::Recorder::~Recorder
virtual ~Recorder()=default
v8::metrics::WasmModuleInstantiated::success
bool success
Definition: v8-metrics.h:88
v8::metrics::GarbageCollectionFullCycle::efficiency_in_bytes_per_us
double efficiency_in_bytes_per_us
Definition: v8-metrics.h:41
v8::metrics::Recorder::ContextId::Empty
static const ContextId Empty()
Definition: v8-metrics.h:150
v8::metrics::GarbageCollectionSizes
Definition: v8-metrics.h:20
v8::metrics::GarbageCollectionFullCycle::memory
GarbageCollectionSizes memory
Definition: v8-metrics.h:37
V8_MAIN_THREAD_METRICS_EVENTS
#define V8_MAIN_THREAD_METRICS_EVENTS(V)
Definition: v8-metrics.h:103
v8::V8::MaybeLocal
friend class MaybeLocal
Definition: v8.h:10136
v8::metrics::WasmModuleTieredUp::lazy
bool lazy
Definition: v8-metrics.h:94
v8::metrics::WasmModuleDecoded
Definition: v8-metrics.h:65
v8::metrics::GarbageCollectionSizes::bytes_before
int64_t bytes_before
Definition: v8-metrics.h:21
v8::metrics::GarbageCollectionFullCycle
Definition: v8-metrics.h:26
v8::metrics::WasmModuleCompiled::streamed
bool streamed
Definition: v8-metrics.h:76
v8::metrics::WasmModulesPerIsolate
Definition: v8-metrics.h:99
V8_EXPORT
#define V8_EXPORT
Definition: v8config.h:512
v8::metrics::GarbageCollectionPhases::sweep_wall_clock_duration_in_us
int64_t sweep_wall_clock_duration_in_us
Definition: v8-metrics.h:16
v8::metrics::WasmModuleCompiled::success
bool success
Definition: v8-metrics.h:80
v8::internal
Definition: v8-cppgc.h:26
v8::metrics::GarbageCollectionFullCycle::main_thread_efficiency_in_bytes_per_us
double main_thread_efficiency_in_bytes_per_us
Definition: v8-metrics.h:43
v8::Context
Definition: v8.h:10561
v8::Isolate
Definition: v8.h:8450
v8::metrics::WasmModuleTieredUp::code_size_in_bytes
size_t code_size_in_bytes
Definition: v8-metrics.h:95
v8::metrics::GarbageCollectionFullCycle::memory_cpp
GarbageCollectionSizes memory_cpp
Definition: v8-metrics.h:38
v8::metrics::WasmModuleDecoded::async
bool async
Definition: v8-metrics.h:66
v8::metrics::GarbageCollectionPhases::weak_wall_clock_duration_in_us
int64_t weak_wall_clock_duration_in_us
Definition: v8-metrics.h:17
v8::metrics::WasmModuleCompiled
Definition: v8-metrics.h:74
v8
Definition: libplatform.h:15
v8::metrics::GarbageCollectionPhases::compact_wall_clock_duration_in_us
int64_t compact_wall_clock_duration_in_us
Definition: v8-metrics.h:14
v8::metrics::WasmModuleTieredUp::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:96
v8::metrics::GarbageCollectionFullCycle::total
GarbageCollectionPhases total
Definition: v8-metrics.h:27
v8::metrics::WasmModuleCompiled::async
bool async
Definition: v8-metrics.h:75
v8::metrics::WasmModuleDecoded::success
bool success
Definition: v8-metrics.h:68
v8::metrics::WasmModuleDecoded::module_size_in_bytes
size_t module_size_in_bytes
Definition: v8-metrics.h:69
v8::metrics::WasmModuleDecoded::function_count
size_t function_count
Definition: v8-metrics.h:70
v8::metrics::GarbageCollectionFullCycle::objects
GarbageCollectionSizes objects
Definition: v8-metrics.h:35
v8::metrics::WasmModuleInstantiated::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:90
v8::metrics::WasmModuleInstantiated::imported_function_count
size_t imported_function_count
Definition: v8-metrics.h:89
v8::V8::Local
friend class Local
Definition: v8.h:10134
v8::metrics::WasmModuleCompiled::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:83
v8::metrics::Recorder::ContextId::ContextId
ContextId()
Definition: v8-metrics.h:147
v8::metrics::Recorder
Definition: v8-metrics.h:141
v8::metrics::WasmModuleInstantiated::async
bool async
Definition: v8-metrics.h:87
v8::metrics::Recorder::GetContext
static MaybeLocal< Context > GetContext(Isolate *isolate, ContextId id)
v8::metrics::GarbageCollectionPhases
Definition: v8-metrics.h:13
v8::metrics::GarbageCollectionYoungCycle::collection_rate_in_percent
double collection_rate_in_percent
Definition: v8-metrics.h:60
v8::metrics::WasmModuleCompiled::code_size_in_bytes
size_t code_size_in_bytes
Definition: v8-metrics.h:81
v8::metrics
Definition: v8-metrics.h:11
v8::metrics::WasmModuleCompiled::cached
bool cached
Definition: v8-metrics.h:77
v8::metrics::WasmModulesPerIsolate::count
size_t count
Definition: v8-metrics.h:100
v8::metrics::GarbageCollectionFullMainThreadIncrementalSweep::wall_clock_duration_in_us
int64_t wall_clock_duration_in_us
Definition: v8-metrics.h:53
v8::metrics::GarbageCollectionFullCycle::objects_cpp
GarbageCollectionSizes objects_cpp
Definition: v8-metrics.h:36
v8::metrics::WasmModuleDecoded::streamed
bool streamed
Definition: v8-metrics.h:67
v8::metrics::GarbageCollectionYoungCycle::main_thread_wall_clock_duration_in_us
int64_t main_thread_wall_clock_duration_in_us
Definition: v8-metrics.h:59
v8::metrics::GarbageCollectionFullCycle::main_thread_incremental
GarbageCollectionPhases main_thread_incremental
Definition: v8-metrics.h:33
v8::metrics::GarbageCollectionFullMainThreadIncrementalMark
Definition: v8-metrics.h:47