v8 14.1.146 (node 25.0.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
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 <optional>
12#include <vector>
13
14#include "v8-internal.h" // NOLINT(build/include_directory)
15#include "v8-isolate.h" // NOLINT(build/include_directory)
16#include "v8-local-handle.h" // NOLINT(build/include_directory)
17#include "v8config.h" // NOLINT(build/include_directory)
18
19namespace v8 {
20
21class Context;
22class Isolate;
23
24namespace metrics {
25
32};
33
35 int64_t bytes_before = -1;
36 int64_t bytes_after = -1;
37 int64_t bytes_freed = -1;
38};
39
41 int64_t bytes_baseline = -1;
42 int64_t bytes_limit = -1;
43 int64_t bytes_current = -1;
44 int64_t bytes_max = -1;
45};
46
48 int reason = -1;
50 // The priority of the isolate during the GC cycle. A nullopt value denotes a
51 // mixed priority cycle, meaning the Isolate's priority was changed while the
52 // cycle was in progress.
53 std::optional<v8::Isolate::Priority> priority = std::nullopt;
54 bool reduce_memory = false;
55 bool is_loading = false;
83};
84
88};
89
93};
94
95template <typename EventType>
98};
99
100using GarbageCollectionFullMainThreadBatchedIncrementalMark =
103using GarbageCollectionFullMainThreadBatchedIncrementalSweep =
106
108 int reason = -1;
109 // The priority of the isolate during the GC cycle. A nullopt value denotes a
110 // mixed priority cycle, meaning the Isolate's priority was changed while the
111 // cycle was in progress.
112 std::optional<v8::Isolate::Priority> priority = std::nullopt;
118#if defined(CPPGC_YOUNG_GENERATION)
122 double collection_rate_cpp_in_percent = -1.0;
123 double efficiency_cpp_in_bytes_per_us = -1.0;
125#endif // defined(CPPGC_YOUNG_GENERATION)
126};
127
129 WasmModuleDecoded() = default;
130 WasmModuleDecoded(bool async, bool streamed, bool success,
131 size_t module_size_in_bytes, size_t function_count,
132 int64_t wall_clock_duration_in_us)
133 : async(async),
134 streamed(streamed),
135 success(success),
138 wall_clock_duration_in_us(wall_clock_duration_in_us) {}
139
140 bool async = false;
141 bool streamed = false;
142 bool success = false;
146};
147
150
151 WasmModuleCompiled(bool async, bool streamed, bool cached, bool deserialized,
152 bool lazy, bool success, size_t code_size_in_bytes,
153 size_t liftoff_bailout_count,
154 int64_t wall_clock_duration_in_us)
155 : async(async),
156 streamed(streamed),
157 cached(cached),
158 deserialized(deserialized),
159 lazy(lazy),
160 success(success),
163 wall_clock_duration_in_us(wall_clock_duration_in_us) {}
164
165 bool async = false;
166 bool streamed = false;
167 bool cached = false;
168 bool deserialized = false;
169 bool lazy = false;
170 bool success = false;
174};
175
177 bool async = false;
178 bool success = false;
181};
182
185};
186
187/**
188 * This class serves as a base class for recording event-based metrics in V8.
189 * There a two kinds of metrics, those which are expected to be thread-safe and
190 * whose implementation is required to fulfill this requirement and those whose
191 * implementation does not have that requirement and only needs to be
192 * executable on the main thread. If such an event is triggered from a
193 * background thread, it will be delayed and executed by the foreground task
194 * runner.
195 *
196 * The embedder is expected to call v8::Isolate::SetMetricsRecorder()
197 * providing its implementation and have the virtual methods overwritten
198 * for the events it cares about.
199 */
201 public:
202 // A unique identifier for a context in this Isolate.
203 // It is guaranteed to not be reused throughout the lifetime of the Isolate.
204 class ContextId {
205 public:
206 ContextId() : id_(kEmptyId) {}
207
208 bool IsEmpty() const { return id_ == kEmptyId; }
209 static const ContextId Empty() { return ContextId{kEmptyId}; }
210
211 bool operator==(const ContextId& other) const { return id_ == other.id_; }
212 bool operator!=(const ContextId& other) const { return id_ != other.id_; }
213
214 private:
215 friend class ::v8::Context;
216 friend class ::v8::internal::Isolate;
217
218 explicit ContextId(uintptr_t id) : id_(id) {}
219
220 static constexpr uintptr_t kEmptyId = 0;
221 uintptr_t id_;
222 };
223
224 virtual ~Recorder() = default;
225
226 // Main thread events. Those are only triggered on the main thread, and hence
227 // can access the context.
228#define ADD_MAIN_THREAD_EVENT(E)
229 virtual void AddMainThreadEvent(const E&, ContextId) {}
232 ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadBatchedIncrementalMark)
234 ADD_MAIN_THREAD_EVENT(GarbageCollectionFullMainThreadBatchedIncrementalSweep)
239#undef ADD_MAIN_THREAD_EVENT
240
241 // Thread-safe events are not allowed to access the context and therefore do
242 // not carry a context ID with them. These IDs can be generated using
243 // Recorder::GetContextId() and the ID will be valid throughout the lifetime
244 // of the isolate. It is not guaranteed that the ID will still resolve to
245 // a valid context using Recorder::GetContext() at the time the metric is
246 // recorded. In this case, an empty handle will be returned.
247#define ADD_THREAD_SAFE_EVENT(E)
248 virtual void AddThreadSafeEvent(const E&) {}
250#undef ADD_THREAD_SAFE_EVENT
251
252 virtual void NotifyIsolateDisposal() {}
253
254 // Return the context with the given id or an empty handle if the context
255 // was already garbage collected.
257 // Return the unique id corresponding to the given context.
259};
260
261/**
262 * Experimental API intended for the LongTasks UKM (crbug.com/1173527).
263 * The Reset() method should be called at the start of a potential
264 * long task. The Get() method returns durations of V8 work that
265 * happened during the task.
266 *
267 * This API is experimental and may be removed/changed in the future.
268 */
270 /**
271 * Resets durations of V8 work for the new task.
272 */
273 V8_INLINE static void Reset(Isolate* isolate) {
275 }
276
277 /**
278 * Returns durations of V8 work that happened since the last Reset().
279 */
280 static LongTaskStats Get(Isolate* isolate);
281
285 // Only collected with --slow-histograms
286 int64_t v8_execute_us = 0;
287};
288
289} // namespace metrics
290} // namespace v8
291
292#endif // V8_METRICS_H_
friend class Local
friend class MaybeLocal
static V8_INLINE void IncrementLongTasksStatsCounter(v8::Isolate *isolate)
static const ContextId Empty()
Definition v8-metrics.h:209
bool operator!=(const ContextId &other) const
Definition v8-metrics.h:212
bool operator==(const ContextId &other) const
Definition v8-metrics.h:211
virtual ~Recorder()=default
static MaybeLocal< Context > GetContext(Isolate *isolate, ContextId id)
static ContextId GetContextId(Local< Context > context)
virtual void NotifyIsolateDisposal()
Definition v8-metrics.h:252
int64_t incremental_marking_start_stop_wall_clock_duration_in_us
Definition v8-metrics.h:81
GarbageCollectionPhases main_thread_incremental
Definition v8-metrics.h:62
GarbageCollectionPhases main_thread_atomic_cpp
Definition v8-metrics.h:61
GarbageCollectionLimits global_consumed
Definition v8-metrics.h:69
GarbageCollectionPhases main_thread
Definition v8-metrics.h:58
GarbageCollectionPhases main_thread_incremental_cpp
Definition v8-metrics.h:63
GarbageCollectionPhases main_thread_cpp
Definition v8-metrics.h:59
GarbageCollectionSizes memory_cpp
Definition v8-metrics.h:67
GarbageCollectionPhases total_cpp
Definition v8-metrics.h:57
GarbageCollectionPhases main_thread_atomic
Definition v8-metrics.h:60
GarbageCollectionSizes objects_cpp
Definition v8-metrics.h:65
GarbageCollectionLimits old_generation_consumed
Definition v8-metrics.h:68
std::optional< v8::Isolate::Priority > priority
Definition v8-metrics.h:53
std::optional< v8::Isolate::Priority > priority
Definition v8-metrics.h:112
int64_t gc_full_incremental_wall_clock_duration_us
Definition v8-metrics.h:283
int64_t gc_young_wall_clock_duration_us
Definition v8-metrics.h:284
static V8_INLINE void Reset(Isolate *isolate)
Definition v8-metrics.h:273
int64_t gc_full_atomic_wall_clock_duration_us
Definition v8-metrics.h:282
static LongTaskStats Get(Isolate *isolate)
WasmModuleCompiled(bool async, bool streamed, bool cached, bool deserialized, bool lazy, bool success, size_t code_size_in_bytes, size_t liftoff_bailout_count, int64_t wall_clock_duration_in_us)
Definition v8-metrics.h:151
WasmModuleDecoded(bool async, bool streamed, bool success, size_t module_size_in_bytes, size_t function_count, int64_t wall_clock_duration_in_us)
Definition v8-metrics.h:130
#define ADD_MAIN_THREAD_EVENT(E)
Definition v8-metrics.h:228
#define ADD_THREAD_SAFE_EVENT(E)
Definition v8-metrics.h:247
#define V8_EXPORT
Definition v8config.h:860
#define V8_INLINE
Definition v8config.h:513