Skip to content

Commit

Permalink
process: add custom directory in heapsnapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJithil committed May 4, 2023
1 parent 9e5e2f1 commit 98cdeb0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ObjectGetOwnPropertyDescriptor,
SafeMap,
StringPrototypeStartsWith,
DateNow,
globalThis,
} = primordials;

Expand Down Expand Up @@ -365,6 +366,7 @@ function initializeReportSignalHandlers() {

function initializeHeapSnapshotSignalHandlers() {
const signal = getOptionValue('--heapsnapshot-signal');
const diagnosticDir = getOptionValue('--diagnostic-dir');

if (!signal)
return;
Expand All @@ -373,7 +375,8 @@ function initializeHeapSnapshotSignalHandlers() {
const { writeHeapSnapshot } = require('v8');

function doWriteHeapSnapshot() {
writeHeapSnapshot();
const heapSnapshotFilename = getHeapSnapshotFilename(diagnosticDir);
writeHeapSnapshot(heapSnapshotFilename);
}
process.on(signal, doWriteHeapSnapshot);

Expand Down Expand Up @@ -650,6 +653,11 @@ function markBootstrapComplete() {
internalBinding('performance').markBootstrapComplete();
}

function getHeapSnapshotFilename(diagnosticDir) {
if (!diagnosticDir) return undefined;
return `${diagnosticDir}/Heap.${DateNow()}.heapsnapshot`;
}

module.exports = {
setupUserModules,
prepareMainThreadExecution,
Expand Down
41 changes: 41 additions & 0 deletions test/sequential/test-heapdump-flag-custom-dir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const common = require('../common');

if (common.isWindows)
common.skip('test not supported on Windows');

const assert = require('assert');

if (process.argv[2] === 'child') {
const fs = require('fs');

assert.strictEqual(process.listenerCount('SIGUSR2'), 1);
process.kill(process.pid, 'SIGUSR2');
process.kill(process.pid, 'SIGUSR2');

// Asynchronously wait for the snapshot. Use an async loop to be a bit more
// robust in case platform or machine differences throw off the timing.
(function validate() {
const files = fs.readdirSync(process.cwd());

if (files.length === 0)
return setImmediate(validate);

assert.strictEqual(files.length, 2);

for (let i = 0; i < files.length; i++) {
assert.match(files[i], /^Heap\..+\.heapsnapshot$/);
JSON.parse(fs.readFileSync(files[i]));
}
})();
} else {
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();
const args = ['--heapsnapshot-signal', 'SIGUSR2', '--diagnostic-dir', tmpdir.path, __filename, 'child'];
const child = spawnSync(process.execPath, args, { cwd: tmpdir.path });

assert.strictEqual(child.status, 0);
assert.strictEqual(child.signal, null);
}

0 comments on commit 98cdeb0

Please sign in to comment.