From bee47339fb9edc53293d07f86e5a3a8850074ea8 Mon Sep 17 00:00:00 2001 From: Aditi Date: Mon, 17 Feb 2025 19:29:24 +0530 Subject: [PATCH] v8: add v8.getCppHeapStatistics() method Expose `CppHeap` data via `cppgc::CppHeap::CollectStatistics()` in the v8 module. --- doc/api/v8.md | 87 ++++++++++++ lib/v8.js | 22 ++- src/node_v8.cc | 202 ++++++++++++++++++++++++++++ test/parallel/test-cppheap-stats.js | 125 +++++++++++++++++ 4 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-cppheap-stats.js diff --git a/doc/api/v8.md b/doc/api/v8.md index 670283e17f5d80..2992e075a1d064 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -271,6 +271,89 @@ buffers and external strings. } ``` +## `v8.getCppHeapStatistics(detailLevel)` + +Retrieves [CppHeap][] regarding memory consumption and +utilization statistics using the V8 +[`CollectStatistics()`][] function which may change from one V8 version to the +next. + +* `detailLevel` (`'brief'`|`'detailed`): Specifies the level of detail in the + returned statistics. + * `brief`: Brief statistics contain only the top-level allocated and used + memory statistics for the entire heap. + * `detailed`: Detailed statistics also contain a break down per space and + page, as well as freelist statistics and object type histograms. + +It returns an object with a structure similar to the +[`cppgc::HeapStatistics`][] object. You can learn more about the properties of the object in +the [V8 documentation][`cppgc::HeapStatistics struct`]. + +```js +// Detailed +({ + committed_size_bytes: 131072, + resident_size_bytes: 131072, + used_size_bytes: 152, + space_statistics: [ + { + name: 'NormalPageSpace0', + committed_size_bytes: 0, + resident_size_bytes: 0, + used_size_bytes: 0, + page_stats: [{}], + free_list_stats: {}, + }, + { + name: 'NormalPageSpace1', + committed_size_bytes: 131072, + resident_size_bytes: 131072, + used_size_bytes: 152, + page_stats: [{}], + free_list_stats: {}, + }, + { + name: 'NormalPageSpace2', + committed_size_bytes: 0, + resident_size_bytes: 0, + used_size_bytes: 0, + page_stats: [{}], + free_list_stats: {}, + }, + { + name: 'NormalPageSpace3', + committed_size_bytes: 0, + resident_size_bytes: 0, + used_size_bytes: 0, + page_stats: [{}], + free_list_stats: {}, + }, + { + name: 'LargePageSpace', + committed_size_bytes: 0, + resident_size_bytes: 0, + used_size_bytes: 0, + page_stats: [{}], + free_list_stats: {}, + }, + ], + type_names: [], + detail_level: 'detailed', +}); +``` + +```js +// Brief +({ + committed_size_bytes: 131072, + resident_size_bytes: 131072, + used_size_bytes: 128864, + space_statistics: [], + type_names: [], + detail_level: 'brief', +}); +``` + ## `v8.queryObjects(ctor[, options])`