GeezerVison is showing github.com — v8: add v8.getCppHeapStatistics() method · nodejs/node@673a424
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/v8.md +90Lines changed: 90 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -271,6 +271,92 @@ buffers and external strings. | 271 | 271 | } | 272 | 272 | ``` | 273 | 273 |
| | 274 | +## `v8.getCppHeapStatistics([detailLevel])` | | 275 | + | | 276 | +Retrieves [CppHeap][] statistics regarding memory consumption and | | 277 | +utilization using the V8 [`CollectStatistics()`][] function which | | 278 | +may change from one V8 version to the | | 279 | +next. | | 280 | + | | 281 | +* `detailLevel` {string|undefined}: **Default:** `'detailed'`. | | 282 | + Specifies the level of detail in the returned statistics. | | 283 | + Accepted values are: | | 284 | + * `'brief'`: Brief statistics contain only the top-level | | 285 | + allocated and used | | 286 | + memory statistics for the entire heap. | | 287 | + * `'detailed'`: Detailed statistics also contain a break | | 288 | + down per space and page, as well as freelist statistics | | 289 | + and object type histograms. | | 290 | + | | 291 | +It returns an object with a structure similar to the | | 292 | +[`cppgc::HeapStatistics`][] object. See the [V8 documentation][`cppgc::HeapStatistics struct`] | | 293 | +for more information about the properties of the object. | | 294 | + | | 295 | +```js | | 296 | +// Detailed | | 297 | +({ | | 298 | + committed_size_bytes: 131072, | | 299 | + resident_size_bytes: 131072, | | 300 | + used_size_bytes: 152, | | 301 | + space_statistics: [ | | 302 | + { | | 303 | + name: 'NormalPageSpace0', | | 304 | + committed_size_bytes: 0, | | 305 | + resident_size_bytes: 0, | | 306 | + used_size_bytes: 0, | | 307 | + page_stats: [{}], | | 308 | + free_list_stats: {}, | | 309 | + }, | | 310 | + { | | 311 | + name: 'NormalPageSpace1', | | 312 | + committed_size_bytes: 131072, | | 313 | + resident_size_bytes: 131072, | | 314 | + used_size_bytes: 152, | | 315 | + page_stats: [{}], | | 316 | + free_list_stats: {}, | | 317 | + }, | | 318 | + { | | 319 | + name: 'NormalPageSpace2', | | 320 | + committed_size_bytes: 0, | | 321 | + resident_size_bytes: 0, | | 322 | + used_size_bytes: 0, | | 323 | + page_stats: [{}], | | 324 | + free_list_stats: {}, | | 325 | + }, | | 326 | + { | | 327 | + name: 'NormalPageSpace3', | | 328 | + committed_size_bytes: 0, | | 329 | + resident_size_bytes: 0, | | 330 | + used_size_bytes: 0, | | 331 | + page_stats: [{}], | | 332 | + free_list_stats: {}, | | 333 | + }, | | 334 | + { | | 335 | + name: 'LargePageSpace', | | 336 | + committed_size_bytes: 0, | | 337 | + resident_size_bytes: 0, | | 338 | + used_size_bytes: 0, | | 339 | + page_stats: [{}], | | 340 | + free_list_stats: {}, | | 341 | + }, | | 342 | + ], | | 343 | + type_names: [], | | 344 | + detail_level: 'detailed', | | 345 | +}); | | 346 | +``` | | 347 | + | | 348 | +```js | | 349 | +// Brief | | 350 | +({ | | 351 | + committed_size_bytes: 131072, | | 352 | + resident_size_bytes: 131072, | | 353 | + used_size_bytes: 128864, | | 354 | + space_statistics: [], | | 355 | + type_names: [], | | 356 | + detail_level: 'brief', | | 357 | +}); | | 358 | +``` | | 359 | + | 274 | 360 | ## `v8.queryObjects(ctor[, options])` | 275 | 361 |
| 276 | 362 | <!-- YAML | @@ -1341,12 +1427,14 @@ writeString('hello'); | 1341 | 1427 | writeString('你好'); | 1342 | 1428 | ``` | 1343 | 1429 |
| | 1430 | +[CppHeap]: https://v8docs.nodesource.com/node-22.4/d9/dc4/classv8_1_1_cpp_heap.html | 1344 | 1431 | [HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm | 1345 | 1432 | [Hook Callbacks]: #hook-callbacks | 1346 | 1433 | [V8]: https://developers.google.com/v8/ | 1347 | 1434 | [`--heapsnapshot-near-heap-limit`]: cli.md#--heapsnapshot-near-heap-limitmax_count | 1348 | 1435 | [`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage | 1349 | 1436 | [`Buffer`]: buffer.md | | 1437 | +[`CollectStatistics()`]: https://v8docs.nodesource.com/node-22.4/d9/dc4/classv8_1_1_cpp_heap.html#a3a5d09567758e608fffde50eeabc2feb | 1350 | 1438 | [`DefaultDeserializer`]: #class-v8defaultdeserializer | 1351 | 1439 | [`DefaultSerializer`]: #class-v8defaultserializer | 1352 | 1440 | [`Deserializer`]: #class-v8deserializer | @@ -1360,6 +1448,8 @@ writeString('你好'); | 1360 | 1448 | [`async_hooks`]: async_hooks.md | 1361 | 1449 | [`before` callback]: #beforepromise | 1362 | 1450 | [`buffer.constants.MAX_LENGTH`]: buffer.md#bufferconstantsmax_length | | 1451 | +[`cppgc::HeapStatistics struct`]: https://v8docs.nodesource.com/node-22.4/df/d2f/structcppgc_1_1_heap_statistics.html | | 1452 | +[`cppgc::HeapStatistics`]: https://v8docs.nodesource.com/node-22.4/d7/d51/heap-statistics_8h_source.html | 1363 | 1453 | [`deserializer._readHostObject()`]: #deserializer_readhostobject | 1364 | 1454 | [`deserializer.transferArrayBuffer()`]: #deserializertransferarraybufferid-arraybuffer | 1365 | 1455 | [`init` callback]: #initpromise-parent | |
Collapse file Copy file name to clipboardExpand all lines: lib/v8.js +21-1Lines changed: 21 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,11 @@ const { | 37 | 37 | } = primordials; | 38 | 38 |
| 39 | 39 | const { Buffer } = require('buffer'); | 40 | | -const { validateString, validateUint32 } = require('internal/validators'); | | 40 | +const { | | 41 | + validateString, | | 42 | + validateUint32, | | 43 | + validateOneOf, | | 44 | +} = require('internal/validators'); | 41 | 45 | const { | 42 | 46 | Serializer, | 43 | 47 | Deserializer, | @@ -146,6 +150,8 @@ const { | 146 | 150 | heapStatisticsBuffer, | 147 | 151 | heapCodeStatisticsBuffer, | 148 | 152 | heapSpaceStatisticsBuffer, | | 153 | + getCppHeapStatistics: _getCppHeapStatistics, | | 154 | + detailLevel, | 149 | 155 | } = binding; | 150 | 156 |
| 151 | 157 | const kNumberOfHeapSpaces = kHeapSpaces.length; | @@ -271,6 +277,19 @@ function setHeapSnapshotNearHeapLimit(limit) { | 271 | 277 | _setHeapSnapshotNearHeapLimit(limit); | 272 | 278 | } | 273 | 279 |
| | 280 | +const detailLevelDict = { | | 281 | + __proto__: null, | | 282 | + detailed: detailLevel.DETAILED, | | 283 | + brief: detailLevel.BRIEF, | | 284 | +}; | | 285 | + | | 286 | +function getCppHeapStatistics(type = 'detailed') { | | 287 | + validateOneOf(type, 'type', ['brief', 'detailed']); | | 288 | + const result = _getCppHeapStatistics(detailLevelDict[type]); | | 289 | + result.detail_level = type; | | 290 | + return result; | | 291 | +} | | 292 | + | 274 | 293 | /* V8 serialization API */ | 275 | 294 |
| 276 | 295 | /* JS methods for the base objects */ | @@ -442,6 +461,7 @@ module.exports = { | 442 | 461 | getHeapStatistics, | 443 | 462 | getHeapSpaceStatistics, | 444 | 463 | getHeapCodeStatistics, | | 464 | + getCppHeapStatistics, | 445 | 465 | setFlagsFromString, | 446 | 466 | Serializer, | 447 | 467 | Deserializer, | |
|