/src/node/deps/v8/include/v8-statistics.h
Line | Count | Source |
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 | | /** Holds the result of a memory measurement request. */ |
65 | | struct Result { |
66 | | /** |
67 | | * Two spans of equal length: the first includes each context for which |
68 | | * ShouldMeasure returned true and that was not garbage collected while |
69 | | * the memory measurement was in progress; the second includes the size |
70 | | * of the respective context. |
71 | | */ |
72 | | const MemorySpan<const Local<Context>>& contexts; |
73 | | const MemorySpan<const size_t>& sizes_in_bytes; |
74 | | |
75 | | /** |
76 | | * Total size of objects that were not attributed to any context (i.e. are |
77 | | * likely shared objects). |
78 | | */ |
79 | | size_t unattributed_size_in_bytes; |
80 | | |
81 | | /** Total size of generated code for Wasm (shared across contexts). */ |
82 | | size_t wasm_code_size_in_bytes; |
83 | | |
84 | | /** Total size of Wasm metadata (except code; shared across contexts). */ |
85 | | size_t wasm_metadata_size_in_bytes; |
86 | | }; |
87 | | |
88 | | /** |
89 | | * This function is called when memory measurement finishes. |
90 | | * |
91 | | * \param result the result of the measurement. |
92 | | */ |
93 | 0 | virtual void MeasurementComplete(Result result) {} |
94 | | |
95 | | /** |
96 | | * Returns a default delegate that resolves the given promise when |
97 | | * the memory measurement completes. |
98 | | * |
99 | | * \param isolate the current isolate |
100 | | * \param context the current context |
101 | | * \param promise_resolver the promise resolver that is given the |
102 | | * result of the memory measurement. |
103 | | * \param mode the detail level of the result. |
104 | | */ |
105 | | static std::unique_ptr<MeasureMemoryDelegate> Default( |
106 | | Isolate* isolate, Local<Context> context, |
107 | | Local<Promise::Resolver> promise_resolver, MeasureMemoryMode mode); |
108 | | }; |
109 | | |
110 | | /** |
111 | | * Collection of shared per-process V8 memory information. |
112 | | * |
113 | | * Instances of this class can be passed to |
114 | | * v8::V8::GetSharedMemoryStatistics to get shared memory statistics from V8. |
115 | | */ |
116 | | class V8_EXPORT SharedMemoryStatistics { |
117 | | public: |
118 | | SharedMemoryStatistics(); |
119 | 0 | size_t read_only_space_size() { return read_only_space_size_; } |
120 | 0 | size_t read_only_space_used_size() { return read_only_space_used_size_; } |
121 | 0 | size_t read_only_space_physical_size() { |
122 | 0 | return read_only_space_physical_size_; |
123 | 0 | } |
124 | | |
125 | | private: |
126 | | size_t read_only_space_size_; |
127 | | size_t read_only_space_used_size_; |
128 | | size_t read_only_space_physical_size_; |
129 | | |
130 | | friend class V8; |
131 | | friend class internal::ReadOnlyHeap; |
132 | | }; |
133 | | |
134 | | /** |
135 | | * Collection of V8 heap information. |
136 | | * |
137 | | * Instances of this class can be passed to v8::Isolate::GetHeapStatistics to |
138 | | * get heap statistics from V8. |
139 | | */ |
140 | | class V8_EXPORT HeapStatistics { |
141 | | public: |
142 | | HeapStatistics(); |
143 | 0 | size_t total_heap_size() { return total_heap_size_; } |
144 | 0 | size_t total_heap_size_executable() { return total_heap_size_executable_; } |
145 | 0 | size_t total_physical_size() { return total_physical_size_; } |
146 | 0 | size_t total_available_size() { return total_available_size_; } |
147 | 0 | size_t total_global_handles_size() { return total_global_handles_size_; } |
148 | 0 | size_t used_global_handles_size() { return used_global_handles_size_; } |
149 | 0 | size_t used_heap_size() { return used_heap_size_; } |
150 | 0 | size_t heap_size_limit() { return heap_size_limit_; } |
151 | 0 | size_t malloced_memory() { return malloced_memory_; } |
152 | 0 | size_t external_memory() { return external_memory_; } |
153 | 0 | size_t peak_malloced_memory() { return peak_malloced_memory_; } |
154 | 0 | size_t number_of_native_contexts() { return number_of_native_contexts_; } |
155 | 0 | size_t number_of_detached_contexts() { return number_of_detached_contexts_; } |
156 | | |
157 | | /** |
158 | | * Returns the total number of bytes allocated since the Isolate was created. |
159 | | * This includes all heap objects allocated in any space (new, old, code, |
160 | | * etc.). |
161 | | */ |
162 | 0 | uint64_t total_allocated_bytes() { return total_allocated_bytes_; } |
163 | | |
164 | | /** |
165 | | * Returns a 0/1 boolean, which signifies whether the V8 overwrite heap |
166 | | * garbage with a bit pattern. |
167 | | */ |
168 | 0 | size_t does_zap_garbage() { return does_zap_garbage_; } |
169 | | |
170 | | private: |
171 | | size_t total_heap_size_; |
172 | | size_t total_heap_size_executable_; |
173 | | size_t total_physical_size_; |
174 | | size_t total_available_size_; |
175 | | size_t used_heap_size_; |
176 | | size_t heap_size_limit_; |
177 | | size_t malloced_memory_; |
178 | | size_t external_memory_; |
179 | | size_t peak_malloced_memory_; |
180 | | bool does_zap_garbage_; |
181 | | size_t number_of_native_contexts_; |
182 | | size_t number_of_detached_contexts_; |
183 | | size_t total_global_handles_size_; |
184 | | size_t used_global_handles_size_; |
185 | | uint64_t total_allocated_bytes_; |
186 | | |
187 | | friend class V8; |
188 | | friend class Isolate; |
189 | | }; |
190 | | |
191 | | class V8_EXPORT HeapSpaceStatistics { |
192 | | public: |
193 | | HeapSpaceStatistics(); |
194 | 0 | const char* space_name() { return space_name_; } |
195 | 0 | size_t space_size() { return space_size_; } |
196 | 0 | size_t space_used_size() { return space_used_size_; } |
197 | 0 | size_t space_available_size() { return space_available_size_; } |
198 | 0 | size_t physical_space_size() { return physical_space_size_; } |
199 | | |
200 | | private: |
201 | | const char* space_name_; |
202 | | size_t space_size_; |
203 | | size_t space_used_size_; |
204 | | size_t space_available_size_; |
205 | | size_t physical_space_size_; |
206 | | |
207 | | friend class Isolate; |
208 | | }; |
209 | | |
210 | | class V8_EXPORT HeapObjectStatistics { |
211 | | public: |
212 | | HeapObjectStatistics(); |
213 | 0 | const char* object_type() { return object_type_; } |
214 | 0 | const char* object_sub_type() { return object_sub_type_; } |
215 | 0 | size_t object_count() { return object_count_; } |
216 | 0 | size_t object_size() { return object_size_; } |
217 | | |
218 | | private: |
219 | | const char* object_type_; |
220 | | const char* object_sub_type_; |
221 | | size_t object_count_; |
222 | | size_t object_size_; |
223 | | |
224 | | friend class Isolate; |
225 | | }; |
226 | | |
227 | | class V8_EXPORT HeapCodeStatistics { |
228 | | public: |
229 | | HeapCodeStatistics(); |
230 | 0 | size_t code_and_metadata_size() { return code_and_metadata_size_; } |
231 | 0 | size_t bytecode_and_metadata_size() { return bytecode_and_metadata_size_; } |
232 | 0 | size_t external_script_source_size() { return external_script_source_size_; } |
233 | 0 | size_t cpu_profiler_metadata_size() { return cpu_profiler_metadata_size_; } |
234 | | |
235 | | private: |
236 | | size_t code_and_metadata_size_; |
237 | | size_t bytecode_and_metadata_size_; |
238 | | size_t external_script_source_size_; |
239 | | size_t cpu_profiler_metadata_size_; |
240 | | |
241 | | friend class Isolate; |
242 | | }; |
243 | | |
244 | | } // namespace v8 |
245 | | |
246 | | #endif // INCLUDE_V8_STATISTICS_H_ |