8
8
ObjectGetOwnPropertyDescriptor,
9
9
SafeMap,
10
10
StringPrototypeStartsWith,
11
+ Date,
12
+ DatePrototypeGetFullYear,
13
+ DatePrototypeGetMonth,
14
+ DatePrototypeGetDate,
15
+ DatePrototypeGetHours,
16
+ DatePrototypeGetMinutes,
17
+ DatePrototypeGetSeconds,
18
+ String,
11
19
globalThis,
12
20
} = primordials ;
13
21
@@ -365,6 +373,7 @@ function initializeReportSignalHandlers() {
365
373
366
374
function initializeHeapSnapshotSignalHandlers ( ) {
367
375
const signal = getOptionValue ( '--heapsnapshot-signal' ) ;
376
+ const diagnosticDir = getOptionValue ( '--diagnostic-dir' ) ;
368
377
369
378
if ( ! signal )
370
379
return ;
@@ -373,7 +382,8 @@ function initializeHeapSnapshotSignalHandlers() {
373
382
const { writeHeapSnapshot } = require ( 'v8' ) ;
374
383
375
384
function doWriteHeapSnapshot ( ) {
376
- writeHeapSnapshot ( ) ;
385
+ const heapSnapshotFilename = getHeapSnapshotFilename ( diagnosticDir ) ;
386
+ writeHeapSnapshot ( heapSnapshotFilename ) ;
377
387
}
378
388
process . on ( signal , doWriteHeapSnapshot ) ;
379
389
@@ -650,6 +660,31 @@ function markBootstrapComplete() {
650
660
internalBinding ( 'performance' ) . markBootstrapComplete ( ) ;
651
661
}
652
662
663
+ // Sequence number for diagnostic filenames
664
+ let sequenceNumOfheapSnapshot = 0 ;
665
+
666
+ // To generate the HeapSnapshotFilename while using custom diagnosticDir
667
+ function getHeapSnapshotFilename ( diagnosticDir ) {
668
+ if ( ! diagnosticDir ) return undefined ;
669
+
670
+ const date = new Date ( ) ;
671
+
672
+ const year = DatePrototypeGetFullYear ( date ) ;
673
+ const month = String ( DatePrototypeGetMonth ( date ) + 1 ) . padStart ( 2 , '0' ) ;
674
+ const day = String ( DatePrototypeGetDate ( date ) ) . padStart ( 2 , '0' ) ;
675
+ const hours = String ( DatePrototypeGetHours ( date ) ) . padStart ( 2 , '0' ) ;
676
+ const minutes = String ( DatePrototypeGetMinutes ( date ) ) . padStart ( 2 , '0' ) ;
677
+ const seconds = String ( DatePrototypeGetSeconds ( date ) ) . padStart ( 2 , '0' ) ;
678
+
679
+ const dateString = `${ year } ${ month } ${ day } ` ;
680
+ const timeString = `${ hours } ${ minutes } ${ seconds } ` ;
681
+ const pid = process . pid ;
682
+ const threadId = internalBinding ( 'worker' ) . threadId ;
683
+ const fileSequence = ( ++ sequenceNumOfheapSnapshot ) . toString ( ) . padStart ( 3 , '0' ) ;
684
+
685
+ return `${ diagnosticDir } /Heap.${ dateString } .${ timeString } .${ pid } .${ threadId } .${ fileSequence } .heapsnapshot` ;
686
+ }
687
+
653
688
module . exports = {
654
689
setupUserModules,
655
690
prepareMainThreadExecution,
0 commit comments