88 ObjectGetOwnPropertyDescriptor,
99 SafeMap,
1010 StringPrototypeStartsWith,
11+ Date,
12+ DatePrototypeGetFullYear,
13+ DatePrototypeGetMonth,
14+ DatePrototypeGetDate,
15+ DatePrototypeGetHours,
16+ DatePrototypeGetMinutes,
17+ DatePrototypeGetSeconds,
18+ String,
1119 globalThis,
1220} = primordials ;
1321
@@ -365,6 +373,7 @@ function initializeReportSignalHandlers() {
365373
366374function initializeHeapSnapshotSignalHandlers ( ) {
367375 const signal = getOptionValue ( '--heapsnapshot-signal' ) ;
376+ const diagnosticDir = getOptionValue ( '--diagnostic-dir' ) ;
368377
369378 if ( ! signal )
370379 return ;
@@ -373,7 +382,8 @@ function initializeHeapSnapshotSignalHandlers() {
373382 const { writeHeapSnapshot } = require ( 'v8' ) ;
374383
375384 function doWriteHeapSnapshot ( ) {
376- writeHeapSnapshot ( ) ;
385+ const heapSnapshotFilename = getHeapSnapshotFilename ( diagnosticDir ) ;
386+ writeHeapSnapshot ( heapSnapshotFilename ) ;
377387 }
378388 process . on ( signal , doWriteHeapSnapshot ) ;
379389
@@ -650,6 +660,31 @@ function markBootstrapComplete() {
650660 internalBinding ( 'performance' ) . markBootstrapComplete ( ) ;
651661}
652662
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+
653688module . exports = {
654689 setupUserModules,
655690 prepareMainThreadExecution,
0 commit comments