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

n-api: adds function to adjust external memory #14310

Closed
wants to merge 4 commits into from

Conversation

chris--young
Copy link

Added a simple wrapper around v8::Isolate::AdjustAmountOfExternalAllocatedMemory

One of the requests from @mhdawson's original issue was for this to act as a no-op on unsupported vms. I am a little unsure of the best way to handle that. As far as I can tell Chakra is the only unsupported vm we need to do account for and figure the easiest thing to do would be for me to open a second pr against node-charkracore where napi_adjust_external_memory() always returns -1 (or something along those lines). But I'm worried that will cause a conflict when merging the two repos.

Fixes: #13928

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

n-api

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to the Node-API. labels Jul 16, 2017
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.

Two comments, otherwise LGTM – thanks!

doc/api/n-api.md Outdated
@@ -3155,6 +3155,21 @@ support it:
* If the function is not available, provide an alternate implementation
that does not use the function.

### napi_adjust_external_memory
<!-- YAML
added: v8.0.0
Copy link
Member

Choose a reason for hiding this comment

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

Can you use added: REPLACEME here?

@@ -58,3 +58,6 @@ test_general.wrap(x, 25);
assert.throws(function() {
test_general.wrap(x, 'Blah');
}, Error);

// test napi_adjust_external_memory
assert.ok(typeof test_general.testAdjustExternalMemory() === 'number');
Copy link
Member

Choose a reason for hiding this comment

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

Can you use assert.strictEqual for this kind of check?

doc/api/n-api.md Outdated
-->
```C
NAPI_EXTERN int64_t napi_adjust_external_memory(napi_env env,
int64_t change_in_bytes);
Copy link
Member

Choose a reason for hiding this comment

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

I think the parameters should be aligned here, that probably means the first one will have to be on a new line. I'm surprised the linter did not complain about this.

Copy link
Member

Choose a reason for hiding this comment

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

We don't run the C++ linter against Markdown files yet.

doc/api/n-api.md Outdated
@@ -3170,6 +3185,7 @@ support it:
[Working with JavaScript Values]: #n_api_working_with_javascript_values
[Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations

[`napi_adjust_external_memory`]: #n_api_napi_adjust_external_memory
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this unless there is a reference to it.

src/node_api.h Outdated
@@ -512,6 +512,10 @@ NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
// version management
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);

// Garbage collection
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about the comment. Does adjusting the external memory trigger a gc ?

Copy link
Author

Choose a reason for hiding this comment

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

No, I don't think it triggers a gc but it is related to gc and I wanted to avoid any confusion with the "version management" comment above. I can remove this line if you think that would be best.

Copy link
Member

Choose a reason for hiding this comment

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

It controls the operations of the GC, so I think the heading is appropriate.

Copy link
Member

@bnoordhuis bnoordhuis Jul 18, 2017

Choose a reason for hiding this comment

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

Needs more detail though. Many people will wonder the same thing as @mhdawson when it's just one of many input signals that influence the frequency of GC runs.

@mhdawson
Copy link
Member

mhdawson commented Jul 18, 2017

Thanks for picking this up. In terms of the Chakra implementation I think we (the N-API team) agreed to let the Microsoft team figure out how to implement new functions like this. Left a few comments.

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

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

LGTM besides the doc nit.

doc/api/n-api.md Outdated
@@ -3155,6 +3155,21 @@ support it:
* If the function is not available, provide an alternate implementation
that does not use the function.

### napi_adjust_external_memory
Copy link
Member

Choose a reason for hiding this comment

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

I think you would need a "Garbage Collection" heading here as well, so as to not categorize it under "Version Management".

src/node_api.h Outdated
@@ -512,6 +512,10 @@ NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
// version management
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);

// Garbage collection
NAPI_EXTERN int64_t napi_adjust_external_memory(napi_env env,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think napi_status napi_adjust_external_memory(napi_env env, int64_t change_in_bytes, int64_t* result) would be more consistent with the rest of the API, since almost everything else returns a napi_status.

kept alive by JavaScript objects.
- `[out] result`: The adjusted value

Returns `napi_ok` if the API succeeded.
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is missing a description of what the method actually does.

Copy link
Author

Choose a reason for hiding this comment

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

Added a description below

@@ -58,3 +58,6 @@ test_general.wrap(x, 25);
assert.throws(function() {
test_general.wrap(x, 'Blah');
}, Error);

// test napi_adjust_external_memory
assert.notStrictEqual(test_general.testAdjustExternalMemory(), -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add some tests for invalid values.

Copy link
Author

Choose a reason for hiding this comment

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

yea

@mhdawson
Copy link
Member

@chris--young you still working on this ?

@chris--young
Copy link
Author

@mhdawson yea ill wrap it up this weekend

Copy link
Contributor

@digitalinfinity digitalinfinity left a comment

Choose a reason for hiding this comment

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

LGTM (although agree with @cjihrig that we should document it more precisely). For the node-chakracore side of the house, we welcome PRs 😄 - if you ping me on the ChakraCore gitter channel, happy to help guide you to make that change.

@mhdawson
Copy link
Member

@chris--young thanks for your help on this :)

@chris--young
Copy link
Author

@cjihrig please review again

I looked into testing for invalid values, but it seems like there isn't much to be done there. V8 handles the change gracefully even if you exceed the external allocation limit or pass in 0. Any other bad values should be prevented by the compiler.

@chris--young
Copy link
Author

@digitalinfinity i'd definitely like to help out on the node-chakracore side too. I'll follow up with you after my changes here have been fully approved.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -58,3 +58,6 @@ test_general.wrap(x, 25);
assert.throws(function() {
test_general.wrap(x, 'Blah');
}, Error);

// test napi_adjust_external_memory
assert.strictEqual(typeof test_general.testAdjustExternalMemory(), 'number');
Copy link
Member

Choose a reason for hiding this comment

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

can we also test that the number is >0. I believe that we probably can't tell what the number will be, but I think we can depend on it being >0 ?

@mhdawson
Copy link
Member

Chris Young added 4 commits August 25, 2017 11:43
Added a simple wrapper around v8::Isolate::AdjustAmountOfExternalAllocatedMemory

Fixes: nodejs#13928
Improved documentation based on pull request feedback

Fixes: nodejs#13928
Changed napi_adjust_external_memory() signature to be more
consistent with the rest of the API

Fixes: nodejs#13928
@BridgeAR
Copy link
Member

I guess this is ready to land?

@benjamingr
Copy link
Member

Landed in c77e6d3

@benjamingr benjamingr closed this Aug 30, 2017
benjamingr pushed a commit that referenced this pull request Aug 30, 2017
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: #14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: #13928
@BridgeAR
Copy link
Member

@BridgeAR
Copy link
Member

@benjamingr that was timing 😆

cjihrig pushed a commit to cjihrig/node that referenced this pull request Aug 31, 2017
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: nodejs#14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: nodejs#13928
addaleax pushed a commit to addaleax/ayo that referenced this pull request Sep 5, 2017
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: nodejs/node#14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: nodejs/node#13928
MylesBorins pushed a commit that referenced this pull request Sep 10, 2017
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: #14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: #13928
@MylesBorins MylesBorins mentioned this pull request Sep 10, 2017
MylesBorins pushed a commit that referenced this pull request Sep 12, 2017
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: #14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: #13928
gabrielschulhof pushed a commit to gabrielschulhof/node that referenced this pull request Apr 10, 2018
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

PR-URL: nodejs#14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: nodejs#13928
MylesBorins pushed a commit that referenced this pull request Apr 16, 2018
Added a wrapper around
v8::Isolate::AdjustAmountOfExternalAllocatedMemory

Backport-PR-URL: #19447
PR-URL: #14310
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Fixes: #13928
@MylesBorins MylesBorins mentioned this pull request Apr 16, 2018
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++. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

N-API: expose function to suggest GC extends external memory