8
8
ObjectGetOwnPropertyDescriptor,
9
9
SafeMap,
10
10
StringPrototypeStartsWith,
11
+ Date,
12
+ DatePrototypeToLocaleDateString,
13
+ DatePrototypeToTimeString,
11
14
globalThis,
12
15
} = primordials ;
13
16
@@ -365,6 +368,7 @@ function initializeReportSignalHandlers() {
365
368
366
369
function initializeHeapSnapshotSignalHandlers ( ) {
367
370
const signal = getOptionValue ( '--heapsnapshot-signal' ) ;
371
+ const diagnosticDir = getOptionValue ( '--diagnostic-dir' ) ;
368
372
369
373
if ( ! signal )
370
374
return ;
@@ -373,7 +377,8 @@ function initializeHeapSnapshotSignalHandlers() {
373
377
const { writeHeapSnapshot } = require ( 'v8' ) ;
374
378
375
379
function doWriteHeapSnapshot ( ) {
376
- writeHeapSnapshot ( ) ;
380
+ const heapSnapshotFilename = getHeapSnapshotFilename ( diagnosticDir ) ;
381
+ writeHeapSnapshot ( heapSnapshotFilename ) ;
377
382
}
378
383
process . on ( signal , doWriteHeapSnapshot ) ;
379
384
@@ -650,6 +655,22 @@ function markBootstrapComplete() {
650
655
internalBinding ( 'performance' ) . markBootstrapComplete ( ) ;
651
656
}
652
657
658
+ // Sequence number for diagnostic filenames
659
+ let sequenceNumOfheapSnapshot = 0 ;
660
+
661
+ function getHeapSnapshotFilename ( diagnosticDir ) {
662
+ if ( ! diagnosticDir ) return undefined ;
663
+
664
+ const date = new Date ( ) ;
665
+ const formattedDateString = DatePrototypeToLocaleDateString ( date ) . split ( '/' ) . reverse ( ) . join ( '' ) ;
666
+ const formattedTimeString = DatePrototypeToTimeString ( date ) . split ( ' ' ) [ 0 ] . replaceAll ( ':' , '' ) ;
667
+ const pid = process . pid ;
668
+ const threadId = internalBinding ( 'worker' ) . threadId ;
669
+ const fileSequence = ( ++ sequenceNumOfheapSnapshot ) . toString ( ) . padStart ( 3 , '0' ) ;
670
+
671
+ return `${ diagnosticDir } /Heap.${ formattedDateString } .${ formattedTimeString } .${ pid } .${ threadId } .${ fileSequence } .heapsnapshot` ;
672
+ }
673
+
653
674
module . exports = {
654
675
setupUserModules,
655
676
prepareMainThreadExecution,
0 commit comments