Coverage Report

Created: 2025-08-28 09:57

/src/node/deps/v8/include/v8-statistics.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2021 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 INCLUDE_V8_STATISTICS_H_
6
#define INCLUDE_V8_STATISTICS_H_
7
8
#include <stddef.h>
9
#include <stdint.h>
10
11
#include <memory>
12
#include <utility>
13
#include <vector>
14
15
#include "v8-local-handle.h"  // NOLINT(build/include_directory)
16
#include "v8-memory-span.h"   // NOLINT(build/include_directory)
17
#include "v8-promise.h"       // NOLINT(build/include_directory)
18
#include "v8config.h"         // NOLINT(build/include_directory)
19
20
namespace v8 {
21
22
class Context;
23
class Isolate;
24
25
namespace internal {
26
class ReadOnlyHeap;
27
}  // namespace internal
28
29
/**
30
 * Controls how the default MeasureMemoryDelegate reports the result of
31
 * the memory measurement to JS. With kSummary only the total size is reported.
32
 * With kDetailed the result includes the size of each native context.
33
 */
34
enum class MeasureMemoryMode { kSummary, kDetailed };
35
36
/**
37
 * Controls how promptly a memory measurement request is executed.
38
 * By default the measurement is folded with the next scheduled GC which may
39
 * happen after a while and is forced after some timeout.
40
 * The kEager mode starts incremental GC right away and is useful for testing.
41
 * The kLazy mode does not force GC.
42
 */
43
enum class MeasureMemoryExecution { kDefault, kEager, kLazy };
44
45
/**
46
 * The delegate is used in Isolate::MeasureMemory API.
47
 *
48
 * It specifies the contexts that need to be measured and gets called when
49
 * the measurement is completed to report the results.
50
 *
51
 * Both MeasurementComplete() callbacks will be invoked on completion.
52
 * Each implementation of this class should hence implement only one of them,
53
 * and leave the other empty.
54
 */
55
class V8_EXPORT MeasureMemoryDelegate {
56
 public:
57
  virtual ~MeasureMemoryDelegate() = default;
58
59
  /**
60
   * Returns true if the size of the given context needs to be measured.
61
   */
62
  virtual bool ShouldMeasure(Local<Context> context) = 0;
63
64
  /**
65
   * This function is called when memory measurement finishes.
66
   *
67
   * \param context_sizes_in_bytes a vector of (context, size) pairs that
68
   *   includes each context for which ShouldMeasure returned true and that
69
   *   was not garbage collected while the memory measurement was in progress.
70
   *
71
   * \param unattributed_size_in_bytes total size of objects that were not
72
   *   attributed to any context (i.e. are likely shared objects).
73
   */
74
  V8_DEPRECATED("Please use the version that takes a result struct")
75
  virtual void MeasurementComplete(
76
      const std::vector<std::pair<Local<Context>, size_t>>&
77
          context_sizes_in_bytes,
78
0
      size_t unattributed_size_in_bytes) {}
79
80
  /** Holds the result of a memory measurement request. */
81
  struct Result {
82
    /**
83
     * A vector of (context, size) pairs that includes each context for
84
     * which ShouldMeasure returned true and that was not garbage collected
85
     * while the memory measurement was in progress.
86
     */
87
    V8_DEPRECATED("Please use contexts and sizes_in_bytes")
88
    const std::vector<std::pair<Local<Context>, size_t>>&
89
        context_sizes_in_bytes;
90
91
    /**
92
     * Two spans of equal length: the first includes each context for which
93
     * ShouldMeasure returned true and that was not garbage collected while
94
     * the memory measurement was in progress; the second includes the size
95
     * of the respective context.
96
     */
97
    const MemorySpan<const Local<Context>>& contexts;
98
    const MemorySpan<const size_t>& sizes_in_bytes;
99
100
    /**
101
     * Total size of objects that were not attributed to any context (i.e. are
102
     * likely shared objects).
103
     */
104
    size_t unattributed_size_in_bytes;
105
106
    /** Total size of generated code for Wasm (shared across contexts). */
107
    size_t wasm_code_size_in_bytes;
108
109
    /** Total size of Wasm metadata (except code; shared across contexts). */
110
    size_t wasm_metadata_size_in_bytes;
111
  };
112
113
  /**
114
   * This function is called when memory measurement finishes.
115
   *
116
   * \param result the result of the measurement.
117
   */
118
0
  virtual void MeasurementComplete(Result result) {}
119
120
  /**
121
   * Returns a default delegate that resolves the given promise when
122
   * the memory measurement completes.
123
   *
124
   * \param isolate the current isolate
125
   * \param context the current context
126
   * \param promise_resolver the promise resolver that is given the
127
   *   result of the memory measurement.
128
   * \param mode the detail level of the result.
129
   */
130
  static std::unique_ptr<MeasureMemoryDelegate> Default(
131
      Isolate* isolate, Local<Context> context,
132
      Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode);
133
};
134
135
/**
136
 * Collection of shared per-process V8 memory information.
137
 *
138
 * Instances of this class can be passed to
139
 * v8::V8::GetSharedMemoryStatistics to get shared memory statistics from V8.
140
 */
141
class V8_EXPORT SharedMemoryStatistics {
142
 public:
143
  SharedMemoryStatistics();
144
0
  size_t read_only_space_size() { return read_only_space_size_; }
145
0
  size_t read_only_space_used_size() { return read_only_space_used_size_; }
146
0
  size_t read_only_space_physical_size() {
147
0
    return read_only_space_physical_size_;
148
0
  }
149
150
 private:
151
  size_t read_only_space_size_;
152
  size_t read_only_space_used_size_;
153
  size_t read_only_space_physical_size_;
154
155
  friend class V8;
156
  friend class internal::ReadOnlyHeap;
157
};
158
159
/**
160
 * Collection of V8 heap information.
161
 *
162
 * Instances of this class can be passed to v8::Isolate::GetHeapStatistics to
163
 * get heap statistics from V8.
164
 */
165
class V8_EXPORT HeapStatistics {
166
 public:
167
  HeapStatistics();
168
0
  size_t total_heap_size() { return total_heap_size_; }
169
0
  size_t total_heap_size_executable() { return total_heap_size_executable_; }
170
0
  size_t total_physical_size() { return total_physical_size_; }
171
0
  size_t total_available_size() { return total_available_size_; }
172
0
  size_t total_global_handles_size() { return total_global_handles_size_; }
173
0
  size_t used_global_handles_size() { return used_global_handles_size_; }
174
0
  size_t used_heap_size() { return used_heap_size_; }
175
0
  size_t heap_size_limit() { return heap_size_limit_; }
176
0
  size_t malloced_memory() { return malloced_memory_; }
177
0
  size_t external_memory() { return external_memory_; }
178
0
  size_t peak_malloced_memory() { return peak_malloced_memory_; }
179
0
  size_t number_of_native_contexts() { return number_of_native_contexts_; }
180
0
  size_t number_of_detached_contexts() { return number_of_detached_contexts_; }
181
182
  /**
183
   * Returns a 0/1 boolean, which signifies whether the V8 overwrite heap
184
   * garbage with a bit pattern.
185
   */
186
0
  size_t does_zap_garbage() { return does_zap_garbage_; }
187
188
 private:
189
  size_t total_heap_size_;
190
  size_t total_heap_size_executable_;
191
  size_t total_physical_size_;
192
  size_t total_available_size_;
193
  size_t used_heap_size_;
194
  size_t heap_size_limit_;
195
  size_t malloced_memory_;
196
  size_t external_memory_;
197
  size_t peak_malloced_memory_;
198
  bool does_zap_garbage_;
199
  size_t number_of_native_contexts_;
200
  size_t number_of_detached_contexts_;
201
  size_t total_global_handles_size_;
202
  size_t used_global_handles_size_;
203
204
  friend class V8;
205
  friend class Isolate;
206
};
207
208
class V8_EXPORT HeapSpaceStatistics {
209
 public:
210
  HeapSpaceStatistics();
211
0
  const char* space_name() { return space_name_; }
212
0
  size_t space_size() { return space_size_; }
213
0
  size_t space_used_size() { return space_used_size_; }
214
0
  size_t space_available_size() { return space_available_size_; }
215
0
  size_t physical_space_size() { return physical_space_size_; }
216
217
 private:
218
  const char* space_name_;
219
  size_t space_size_;
220
  size_t space_used_size_;
221
  size_t space_available_size_;
222
  size_t physical_space_size_;
223
224
  friend class Isolate;
225
};
226
227
class V8_EXPORT HeapObjectStatistics {
228
 public:
229
  HeapObjectStatistics();
230
0
  const char* object_type() { return object_type_; }
231
0
  const char* object_sub_type() { return object_sub_type_; }
232
0
  size_t object_count() { return object_count_; }
233
0
  size_t object_size() { return object_size_; }
234
235
 private:
236
  const char* object_type_;
237
  const char* object_sub_type_;
238
  size_t object_count_;
239
  size_t object_size_;
240
241
  friend class Isolate;
242
};
243
244
class V8_EXPORT HeapCodeStatistics {
245
 public:
246
  HeapCodeStatistics();
247
0
  size_t code_and_metadata_size() { return code_and_metadata_size_; }
248
0
  size_t bytecode_and_metadata_size() { return bytecode_and_metadata_size_; }
249
0
  size_t external_script_source_size() { return external_script_source_size_; }
250
0
  size_t cpu_profiler_metadata_size() { return cpu_profiler_metadata_size_; }
251
252
 private:
253
  size_t code_and_metadata_size_;
254
  size_t bytecode_and_metadata_size_;
255
  size_t external_script_source_size_;
256
  size_t cpu_profiler_metadata_size_;
257
258
  friend class Isolate;
259
};
260
261
}  // namespace v8
262
263
#endif  // INCLUDE_V8_STATISTICS_H_