Skip to content

Commit f39064b

Browse files
committed
add params as options object
1 parent 5861bc1 commit f39064b

File tree

8 files changed

+160
-168
lines changed

8 files changed

+160
-168
lines changed

doc/api/v8.md

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,41 +1788,23 @@ const profile = handle.stop();
17881788
console.log(profile);
17891789
```
17901790
1791-
## `v8.heapProfilerConstants`
1791+
## `v8.startHeapProfile([options])`
17921792
17931793
<!-- YAML
17941794
added: REPLACEME
17951795
-->
17961796
1797-
* {Object}
1798-
1799-
A frozen object containing constants used with [`v8.startHeapProfile()`][].
1800-
1801-
The following constants are available:
1802-
1803-
| Constant | Description |
1804-
| ------------------------------------------------ | --------------------------------------------------- |
1805-
| `SAMPLING_NO_FLAGS` | No flags. |
1806-
| `SAMPLING_FORCE_GC` | Force garbage collection before taking the profile. |
1807-
| `SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MAJOR_GC` | Include objects collected by major GC. |
1808-
| `SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MINOR_GC` | Include objects collected by minor GC. |
1809-
1810-
These constants can be combined using bitwise OR to pass as the `flags`
1811-
parameter.
1812-
1813-
## `v8.startHeapProfile([sampleInterval[, stackDepth[, flags]]])`
1814-
1815-
<!-- YAML
1816-
added: REPLACEME
1817-
-->
1818-
1819-
* `sampleInterval` {number} The average sampling interval in bytes.
1820-
**Default:** `524288` (512 KiB).
1821-
* `stackDepth` {integer} The maximum stack depth for samples.
1822-
**Default:** `16`.
1823-
* `flags` {integer} Flags to control sampling behavior. Use constants from
1824-
[`v8.heapProfilerConstants`][]. Multiple flags can be combined with bitwise
1825-
OR. **Default:** `v8.heapProfilerConstants.SAMPLING_NO_FLAGS`.
1797+
* `options` {Object}
1798+
* `sampleInterval` {number} The average sampling interval in bytes.
1799+
**Default:** `524288` (512 KiB).
1800+
* `stackDepth` {integer} The maximum stack depth for samples.
1801+
**Default:** `16`.
1802+
* `forceGC` {boolean} Force garbage collection before taking the profile.
1803+
**Default:** `false`.
1804+
* `includeObjectsCollectedByMajorGC` {boolean} Include objects collected
1805+
by major GC. **Default:** `false`.
1806+
* `includeObjectsCollectedByMinorGC` {boolean} Include objects collected
1807+
by minor GC. **Default:** `false`.
18261808
* Returns: {SyncHeapProfileHandle}
18271809
18281810
Starting a heap profile then return a `SyncHeapProfileHandle` object.
@@ -1848,28 +1830,26 @@ With custom parameters:
18481830
18491831
```cjs
18501832
const v8 = require('node:v8');
1851-
const { heapProfilerConstants } = v8;
1852-
1853-
const handle = v8.startHeapProfile(
1854-
1024,
1855-
8,
1856-
heapProfilerConstants.SAMPLING_FORCE_GC |
1857-
heapProfilerConstants.SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MAJOR_GC,
1858-
);
1833+
1834+
const handle = v8.startHeapProfile({
1835+
sampleInterval: 1024,
1836+
stackDepth: 8,
1837+
forceGC: true,
1838+
includeObjectsCollectedByMajorGC: true,
1839+
});
18591840
const profile = handle.stop();
18601841
console.log(profile);
18611842
```
18621843
18631844
```mjs
18641845
import v8 from 'node:v8';
1865-
const { heapProfilerConstants } = v8;
1866-
1867-
const handle = v8.startHeapProfile(
1868-
1024,
1869-
8,
1870-
heapProfilerConstants.SAMPLING_FORCE_GC |
1871-
heapProfilerConstants.SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MAJOR_GC,
1872-
);
1846+
1847+
const handle = v8.startHeapProfile({
1848+
sampleInterval: 1024,
1849+
stackDepth: 8,
1850+
forceGC: true,
1851+
includeObjectsCollectedByMajorGC: true,
1852+
});
18731853
const profile = handle.stop();
18741854
console.log(profile);
18751855
```
@@ -1908,8 +1888,6 @@ console.log(profile);
19081888
[`serializer.transferArrayBuffer()`]: #serializertransferarraybufferid-arraybuffer
19091889
[`serializer.writeRawBytes()`]: #serializerwriterawbytesbuffer
19101890
[`settled` callback]: #settledpromise
1911-
[`v8.heapProfilerConstants`]: #v8heapprofilerconstants
1912-
[`v8.startHeapProfile()`]: #v8startheapprofilesampleinterval-stackdepth-flags
19131891
[`v8.stopCoverage()`]: #v8stopcoverage
19141892
[`v8.takeCoverage()`]: #v8takecoverage
19151893
[`vm.Script`]: vm.md#new-vmscriptcode-options

doc/api/worker_threads.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,21 +2005,25 @@ w.on('online', async () => {
20052005
});
20062006
```
20072007
2008-
### `worker.startHeapProfile([sampleInterval[, stackDepth[, flags]]])`
2008+
### `worker.startHeapProfile([options])`
20092009
20102010
<!-- YAML
20112011
added:
20122012
- v24.9.0
20132013
- v22.20.0
20142014
-->
20152015
2016-
* `sampleInterval` {number} The average sampling interval in bytes.
2017-
**Default:** `524288` (512 KiB).
2018-
* `stackDepth` {integer} The maximum stack depth for samples.
2019-
**Default:** `16`.
2020-
* `flags` {integer} Flags to control sampling behavior. Use constants from
2021-
[`v8.heapProfilerConstants`][]. Multiple flags can be combined with bitwise
2022-
OR. **Default:** `v8.heapProfilerConstants.SAMPLING_NO_FLAGS`.
2016+
* `options` {Object}
2017+
* `sampleInterval` {number} The average sampling interval in bytes.
2018+
**Default:** `524288` (512 KiB).
2019+
* `stackDepth` {integer} The maximum stack depth for samples.
2020+
**Default:** `16`.
2021+
* `forceGC` {boolean} Force garbage collection before taking the profile.
2022+
**Default:** `false`.
2023+
* `includeObjectsCollectedByMajorGC` {boolean} Include objects collected
2024+
by major GC. **Default:** `false`.
2025+
* `includeObjectsCollectedByMinorGC` {boolean} Include objects collected
2026+
by minor GC. **Default:** `false`.
20232027
* Returns: {Promise}
20242028
20252029
Starting a Heap profile then return a Promise that fulfills with an error
@@ -2303,7 +2307,6 @@ thread spawned will spawn another until the application crashes.
23032307
[`trace_events`]: tracing.md
23042308
[`v8.getHeapSnapshot()`]: v8.md#v8getheapsnapshotoptions
23052309
[`v8.getHeapStatistics()`]: v8.md#v8getheapstatistics
2306-
[`v8.heapProfilerConstants`]: v8.md#v8heapprofilerconstants
23072310
[`vm`]: vm.md
23082311
[`worker.SHARE_ENV`]: #worker_threadsshare_env
23092312
[`worker.on('message')`]: #event-message_1

lib/internal/v8/heap_profile.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22

33
const {
4-
ObjectFreeze,
5-
} = primordials;
6-
7-
const {
4+
validateBoolean,
85
validateInteger,
96
validateInt32,
7+
validateObject,
108
} = require('internal/validators');
119

1210
const {
@@ -16,34 +14,36 @@ const {
1614
kSamplingIncludeObjectsCollectedByMinorGC,
1715
} = internalBinding('v8');
1816

19-
const heapProfilerConstants = {
20-
__proto__: null,
21-
SAMPLING_NO_FLAGS: kSamplingNoFlags,
22-
SAMPLING_FORCE_GC: kSamplingForceGC,
23-
SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MAJOR_GC:
24-
kSamplingIncludeObjectsCollectedByMajorGC,
25-
SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MINOR_GC:
26-
kSamplingIncludeObjectsCollectedByMinorGC,
27-
};
28-
ObjectFreeze(heapProfilerConstants);
17+
function normalizeHeapProfileOptions(options = {}) {
18+
validateObject(options, 'options');
19+
const {
20+
sampleInterval = 512 * 1024,
21+
stackDepth = 16,
22+
forceGC = false,
23+
includeObjectsCollectedByMajorGC = false,
24+
includeObjectsCollectedByMinorGC = false,
25+
} = options;
26+
27+
validateInteger(sampleInterval, 'options.sampleInterval', 1);
28+
validateInt32(stackDepth, 'options.stackDepth', 0);
29+
validateBoolean(forceGC, 'options.forceGC');
30+
validateBoolean(includeObjectsCollectedByMajorGC,
31+
'options.includeObjectsCollectedByMajorGC');
32+
validateBoolean(includeObjectsCollectedByMinorGC,
33+
'options.includeObjectsCollectedByMinorGC');
2934

30-
const kMaxSamplingFlags =
31-
heapProfilerConstants.SAMPLING_FORCE_GC |
32-
heapProfilerConstants.SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MAJOR_GC |
33-
heapProfilerConstants.SAMPLING_INCLUDE_OBJECTS_COLLECTED_BY_MINOR_GC;
35+
let flags = kSamplingNoFlags;
36+
if (forceGC) flags |= kSamplingForceGC;
37+
if (includeObjectsCollectedByMajorGC) {
38+
flags |= kSamplingIncludeObjectsCollectedByMajorGC;
39+
}
40+
if (includeObjectsCollectedByMinorGC) {
41+
flags |= kSamplingIncludeObjectsCollectedByMinorGC;
42+
}
3443

35-
function normalizeHeapProfileOptions(
36-
sampleInterval = 512 * 1024,
37-
stackDepth = 16,
38-
flags = heapProfilerConstants.SAMPLING_NO_FLAGS,
39-
) {
40-
validateInteger(sampleInterval, 'sampleInterval', 1);
41-
validateInt32(stackDepth, 'stackDepth', 0);
42-
validateInt32(flags, 'flags', 0, kMaxSamplingFlags);
4344
return { sampleInterval, stackDepth, flags };
4445
}
4546

4647
module.exports = {
47-
heapProfilerConstants,
4848
normalizeHeapProfileOptions,
4949
};

lib/internal/worker.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,18 @@ class Worker extends EventEmitter {
590590
});
591591
}
592592

593-
startHeapProfile(sampleInterval, stackDepth, flags) {
594-
({ sampleInterval, stackDepth, flags } =
595-
normalizeHeapProfileOptions(sampleInterval, stackDepth, flags));
593+
/**
594+
* @param {object} [options]
595+
* @param {number} [options.sampleInterval]
596+
* @param {number} [options.stackDepth]
597+
* @param {boolean} [options.forceGC]
598+
* @param {boolean} [options.includeObjectsCollectedByMajorGC]
599+
* @param {boolean} [options.includeObjectsCollectedByMinorGC]
600+
* @returns {Promise}
601+
*/
602+
startHeapProfile(options) {
603+
const { sampleInterval, stackDepth, flags } =
604+
normalizeHeapProfileOptions(options);
596605
const startTaker = this[kHandle]?.startHeapProfile(
597606
sampleInterval, stackDepth, flags);
598607
return new Promise((resolve, reject) => {

lib/v8.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const {
2626
Int32Array,
2727
Int8Array,
2828
JSONParse,
29-
ObjectDefineProperty,
3029
ObjectPrototypeToString,
3130
SymbolDispose,
3231
Uint16Array,
@@ -52,7 +51,6 @@ const {
5251
namespace: startupSnapshot,
5352
} = require('internal/v8/startup_snapshot');
5453
const {
55-
heapProfilerConstants,
5654
normalizeHeapProfileOptions,
5755
} = require('internal/v8/heap_profile');
5856

@@ -225,14 +223,17 @@ function startCpuProfile() {
225223

226224
/**
227225
* Starting Heap Profile.
228-
* @param {number} [sampleInterval]
229-
* @param {number} [stackDepth]
230-
* @param {number} [flags]
226+
* @param {object} [options]
227+
* @param {number} [options.sampleInterval]
228+
* @param {number} [options.stackDepth]
229+
* @param {boolean} [options.forceGC]
230+
* @param {boolean} [options.includeObjectsCollectedByMajorGC]
231+
* @param {boolean} [options.includeObjectsCollectedByMinorGC]
231232
* @returns {SyncHeapProfileHandle}
232233
*/
233-
function startHeapProfile(sampleInterval, stackDepth, flags) {
234-
({ sampleInterval, stackDepth, flags } =
235-
normalizeHeapProfileOptions(sampleInterval, stackDepth, flags));
234+
function startHeapProfile(options) {
235+
const { sampleInterval, stackDepth, flags } =
236+
normalizeHeapProfileOptions(options);
236237
_startHeapProfile(sampleInterval, stackDepth, flags);
237238
return new SyncHeapProfileHandle();
238239
}
@@ -557,10 +558,3 @@ module.exports = {
557558
startCpuProfile,
558559
startHeapProfile,
559560
};
560-
561-
ObjectDefineProperty(module.exports, 'heapProfilerConstants', {
562-
__proto__: null,
563-
configurable: false,
564-
enumerable: true,
565-
value: heapProfilerConstants,
566-
});

src/util.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ v8::Maybe<int> GetValidFileMode(Environment* env,
816816
return v8::Just(mode);
817817
}
818818

819-
static void buildHeapProfileNode(Isolate* isolate,
819+
static void BuildHeapProfileNode(Isolate* isolate,
820820
const AllocationProfile::Node* node,
821821
JSONWriter* writer) {
822822
size_t selfSize = 0;
@@ -838,7 +838,7 @@ static void buildHeapProfileNode(Isolate* isolate,
838838
writer->json_arraystart("children");
839839
for (const auto* child : node->children) {
840840
writer->json_start();
841-
buildHeapProfileNode(isolate, child, writer);
841+
BuildHeapProfileNode(isolate, child, writer);
842842
writer->json_end();
843843
}
844844
writer->json_arrayend();
@@ -866,7 +866,7 @@ bool SerializeHeapProfile(Isolate* isolate, std::ostringstream& out_stream) {
866866
writer.json_arrayend();
867867

868868
writer.json_objectstart("head");
869-
buildHeapProfileNode(isolate, profile->GetRootNode(), &writer);
869+
BuildHeapProfileNode(isolate, profile->GetRootNode(), &writer);
870870
writer.json_objectend();
871871

872872
writer.json_end();

0 commit comments

Comments
 (0)