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

[v12.x backport] v8: implement v8.takeCoverage() and v8.stopCoverage() #36352

Merged
merged 2 commits into from
Mar 17, 2021
Merged
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
28 changes: 28 additions & 0 deletions doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,32 @@ v8.setFlagsFromString('--trace_gc');
setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
```

## `v8.takeCoverage()`

<!-- YAML
added: REPLACEME
-->

The `v8.takeCoverage()` method allows the user to write the coverage started by
[`NODE_V8_COVERAGE`][] to disk on demand. This method can be invoked multiple
times during the lifetime of the process, each time the execution counter will
be reset and a new coverage report will be written to the directory specified
by [`NODE_V8_COVERAGE`][].

When the process is about to exit, one last coverage will still be written to
disk, unless [`v8.stopCoverage()`][] is invoked before the process exits.

## `v8.stopCoverage()`

<!-- YAML
added: REPLACEME
-->

The `v8.stopCoverage()` method allows the user to stop the coverage collection
started by [`NODE_V8_COVERAGE`][], so that V8 can release the execution count
records and optimize code. This can be used in conjunction with
`v8.takeCoverage()` if the user wants to collect the coverage on demand.

## `v8.writeHeapSnapshot([filename])`
<!-- YAML
added: v11.13.0
Expand Down Expand Up @@ -509,6 +535,7 @@ A subclass of [`Deserializer`][] corresponding to the format written by
[`Deserializer`]: #v8_class_v8_deserializer
[`Error`]: errors.html#errors_class_error
[`GetHeapSpaceStatistics`]: https://v8docs.nodesource.com/node-13.2/d5/dda/classv8_1_1_isolate.html#ac673576f24fdc7a33378f8f57e1d13a4
[`NODE_V8_COVERAGE`]: cli.md#cli_node_v8_coverage_dir
[`Serializer`]: #v8_class_v8_serializer
[`deserializer._readHostObject()`]: #v8_deserializer_readhostobject
[`deserializer.transferArrayBuffer()`]: #v8_deserializer_transferarraybuffer_id_arraybuffer
Expand All @@ -518,6 +545,7 @@ A subclass of [`Deserializer`][] corresponding to the format written by
[`serializer.releaseBuffer()`]: #v8_serializer_releasebuffer
[`serializer.transferArrayBuffer()`]: #v8_serializer_transferarraybuffer_id_arraybuffer
[`serializer.writeRawBytes()`]: #v8_serializer_writerawbytes_buffer
[`v8.stopCoverage()`]: #v8_v8_stopcoverage
[`vm.Script`]: vm.html#vm_new_vm_script_code_options
[HTML structured clone algorithm]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
[V8]: https://developers.google.com/v8/
Expand Down
8 changes: 8 additions & 0 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const {
Serializer: _Serializer,
Deserializer: _Deserializer
} = internalBinding('serdes');

let profiler = {};
if (internalBinding('config').hasInspector) {
profiler = internalBinding('profiler');
}

const assert = require('internal/assert');
const { copy } = internalBinding('buffer');
const { inspect } = require('internal/util/inspect');
Expand Down Expand Up @@ -286,6 +292,8 @@ module.exports = {
DefaultSerializer,
DefaultDeserializer,
deserialize,
takeCoverage: profiler.takeCoverage,
stopCoverage: profiler.stopCoverage,
serialize,
writeHeapSnapshot,
};
Loading