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

Conversation

Aditi-1400
Copy link
Contributor

Expose CppHeap data via cppgc::CppHeap::CollectStatistics() in the v8 module.
Fixes: #56533

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency. labels Feb 20, 2025
@Aditi-1400 Aditi-1400 force-pushed the issue-56533 branch 2 times, most recently from 0f5a16c to dac6b7a Compare February 20, 2025 14:10
Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

I think v8: add v8.getCppHeapStatistics() may be more intuitive as a commit message. Otherwise it might get interpreted as something like "exposing the cppgc::CppHeap::CollectStatistics() symbol" in the binary (for addons) / exposing the headers instead.

src/node_v8.cc Outdated

cppgc::HeapStatistics stats = isolate->GetCppHeap()->CollectStatistics(
static_cast<cppgc::HeapStatistics::DetailLevel>(
args[0].As<v8::Int32>()->Value()));
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add using v8::Int32; to the list of other using v8:... items at the top of the namespace and change this to just .As<Int32>()

Copy link
Member

Choose a reason for hiding this comment

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

Side note... we do this kind of static cast so often we should probably just wrap it in a utility macro.... but no need to do that here

Copy link
Member

@joyeecheung joyeecheung Feb 21, 2025

Choose a reason for hiding this comment

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

(About that side note, maybe it's even better to wrap them in methods with templated return types for a bit of type checking, the integer types and enums can get especially hairy because there are so many of variants of them, I think FromV8Value<T>() would be very cool)

Copy link

codecov bot commented Feb 20, 2025

Codecov Report

Attention: Patch coverage is 66.48045% with 60 lines in your changes missing coverage. Please review.

Project coverage is 90.34%. Comparing base (cbedcd1) to head (bee4733).
Report is 66 commits behind head on main.

Files with missing lines Patch % Lines
src/node_v8.cc 62.02% 49 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57146      +/-   ##
==========================================
+ Coverage   89.10%   90.34%   +1.23%     
==========================================
  Files         665      629      -36     
  Lines      193188   184551    -8637     
  Branches    37207    36041    -1166     
==========================================
- Hits       172137   166724    -5413     
+ Misses      13772    10957    -2815     
+ Partials     7279     6870     -409     
Files with missing lines Coverage Δ
lib/v8.js 99.35% <100.00%> (+0.02%) ⬆️
src/node_v8.cc 80.79% <62.02%> (-10.40%) ⬇️

... and 111 files with indirect coverage changes

lib/v8.js Outdated
};

function getCppHeapStatistics(type = 'detailed') {
validateOneOf(type, 'type', ['brief', 'detailed', undefined]);
Copy link
Member

Choose a reason for hiding this comment

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

The undefined here is unnecessary since you are specifying the default value in the argument (it will never come through as undefined)

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
validateOneOf(type, 'type', ['brief', 'detailed', undefined]);
validateOneOf(type, 'type', ['brief', 'detailed']);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops 🤦🏻‍♀️

Expose `CppHeap` data via
`cppgc::CppHeap::CollectStatistics()` in the v8 module.
Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Looks great! :)

FIXED_ONE_BYTE_STRING(isolate, "free_list_stats")};

Local<Value> name_value;
if (!ToV8Value(context, stats.space_stats[i].name.c_str(), isolate)
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
if (!ToV8Value(context, stats.space_stats[i].name.c_str(), isolate)
if (!ToV8Value(context, stats.space_stats[i].name, isolate)

.name is a std::string, so this should work fine here, right? It probably doesn't make a ton of difference in this method but if there's the option to stick to the strictly better (more performant/handles null bytes correctly) method, let's do it

@@ -498,6 +699,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GCProfiler::New);
registry->Register(GCProfiler::Start);
registry->Register(GCProfiler::Stop);
registry->Register(getCppHeapStatistics);
Copy link
Member

Choose a reason for hiding this comment

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

Just a tiny style suggestion, but as you can see here, the convention is mostly that the C++ functions are named in UpperCamelCase

Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

Almost there :)

Comment on lines +281 to +286
* `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.
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.

@@ -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

Comment on lines +289 to +290
[`cppgc::HeapStatistics`][] object. You can learn more about the properties of the object in
the [V8 documentation][`cppgc::HeapStatistics struct`].
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..)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose cppgc::CppHeap::CollectStatistics() in v8 module
5 participants