Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose cppgc::CppHeap::CollectStatistics() #57146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,89 @@ buffers and external strings.
}
```

## `v8.getCppHeapStatistics(detailLevel)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## `v8.getCppHeapStatistics(detailLevel)`
## `v8.getCppHeapStatistics([detailLevel])`

We use the square brackets for optional arguments in the signature


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.
Comment on lines +281 to +286
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `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.
* `detailLevel` {string|undefined}: **Default:** `'detailed'`. Specifies the level of detail in the returned statistics.
Accepted values are:
* `'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.

I am not exactly sure what's the best way to document these enums, but we do have some precedents of this style in the docs.


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`].
Comment on lines +289 to +290
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[`cppgc::HeapStatistics`][] object. You can learn more about the properties of the object in
the [V8 documentation][`cppgc::HeapStatistics struct`].
[`cppgc::HeapStatistics`][] object. See the [V8 documentation][`cppgc::HeapStatistics struct`]
for more information about the properties of the object.

(I am fairly certain that we still try to avoid "you" as much as possible in the docs..)


```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])`

<!-- YAML
Expand Down Expand Up @@ -1304,12 +1387,14 @@ setTimeout(() => {
}, 1000);
```

[CppHeap]: https://v8docs.nodesource.com/node-22.4/d9/dc4/classv8_1_1_cpp_heap.html
[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
[Hook Callbacks]: #hook-callbacks
[V8]: https://developers.google.com/v8/
[`--heapsnapshot-near-heap-limit`]: cli.md#--heapsnapshot-near-heap-limitmax_count
[`AsyncLocalStorage`]: async_context.md#class-asynclocalstorage
[`Buffer`]: buffer.md
[`CollectStatistics()`]: https://v8docs.nodesource.com/node-22.4/d9/dc4/classv8_1_1_cpp_heap.html#a3a5d09567758e608fffde50eeabc2feb
[`DefaultDeserializer`]: #class-v8defaultdeserializer
[`DefaultSerializer`]: #class-v8defaultserializer
[`Deserializer`]: #class-v8deserializer
Expand All @@ -1323,6 +1408,8 @@ setTimeout(() => {
[`async_hooks`]: async_hooks.md
[`before` callback]: #beforepromise
[`buffer.constants.MAX_LENGTH`]: buffer.md#bufferconstantsmax_length
[`cppgc::HeapStatistics struct`]: https://v8docs.nodesource.com/node-22.4/df/d2f/structcppgc_1_1_heap_statistics.html
[`cppgc::HeapStatistics`]: https://v8docs.nodesource.com/node-22.4/d7/d51/heap-statistics_8h_source.html
[`deserializer._readHostObject()`]: #deserializer_readhostobject
[`deserializer.transferArrayBuffer()`]: #deserializertransferarraybufferid-arraybuffer
[`init` callback]: #initpromise-parent
Expand Down
22 changes: 21 additions & 1 deletion lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ const {
} = primordials;

const { Buffer } = require('buffer');
const { validateString, validateUint32 } = require('internal/validators');
const {
validateString,
validateUint32,
validateOneOf,
} = require('internal/validators');
const {
Serializer,
Deserializer,
Expand Down Expand Up @@ -145,6 +149,8 @@ const {
heapStatisticsBuffer,
heapCodeStatisticsBuffer,
heapSpaceStatisticsBuffer,
getCppHeapStatistics: _getCppHeapStatistics,
detailLevel,
} = binding;

const kNumberOfHeapSpaces = kHeapSpaces.length;
Expand Down Expand Up @@ -259,6 +265,19 @@ function setHeapSnapshotNearHeapLimit(limit) {
_setHeapSnapshotNearHeapLimit(limit);
}

const detailLevelDict = {
__proto__: null,
detailed: detailLevel.DETAILED,
brief: detailLevel.BRIEF,
};

function getCppHeapStatistics(type = 'detailed') {
validateOneOf(type, 'type', ['brief', 'detailed']);
const result = _getCppHeapStatistics(detailLevelDict[type]);
result.detail_level = type;
return result;
}

/* V8 serialization API */

/* JS methods for the base objects */
Expand Down Expand Up @@ -430,6 +449,7 @@ module.exports = {
getHeapStatistics,
getHeapSpaceStatistics,
getHeapCodeStatistics,
getCppHeapStatistics,
setFlagsFromString,
Serializer,
Deserializer,
Expand Down
Loading
Loading