22
33const  { 
44  ArrayPrototypeForEach, 
5+   Date, 
6+   DatePrototypeGetDate, 
7+   DatePrototypeGetFullYear, 
8+   DatePrototypeGetHours, 
9+   DatePrototypeGetMinutes, 
10+   DatePrototypeGetMonth, 
11+   DatePrototypeGetSeconds, 
512  NumberParseInt, 
613  ObjectDefineProperties, 
714  ObjectDefineProperty, 
815  ObjectGetOwnPropertyDescriptor, 
916  SafeMap, 
17+   String, 
1018  StringPrototypeStartsWith, 
1119  Symbol, 
12-   SymbolDispose, 
1320  SymbolAsyncDispose, 
21+   SymbolDispose, 
1422  globalThis, 
1523}  =  primordials ; 
1624
@@ -400,6 +408,7 @@ function initializeReportSignalHandlers() {
400408
401409function  initializeHeapSnapshotSignalHandlers ( )  { 
402410  const  signal  =  getOptionValue ( '--heapsnapshot-signal' ) ; 
411+   const  diagnosticDir  =  getOptionValue ( '--diagnostic-dir' ) ; 
403412
404413  if  ( ! signal ) 
405414    return ; 
@@ -408,7 +417,8 @@ function initializeHeapSnapshotSignalHandlers() {
408417  const  {  writeHeapSnapshot }  =  require ( 'v8' ) ; 
409418
410419  function  doWriteHeapSnapshot ( )  { 
411-     writeHeapSnapshot ( ) ; 
420+     const  heapSnapshotFilename  =  getHeapSnapshotFilename ( diagnosticDir ) ; 
421+     writeHeapSnapshot ( heapSnapshotFilename ) ; 
412422  } 
413423  process . on ( signal ,  doWriteHeapSnapshot ) ; 
414424
@@ -700,6 +710,31 @@ function markBootstrapComplete() {
700710  internalBinding ( 'performance' ) . markBootstrapComplete ( ) ; 
701711} 
702712
713+ // Sequence number for diagnostic filenames 
714+ let  sequenceNumOfheapSnapshot  =  0 ; 
715+ 
716+ // To generate the HeapSnapshotFilename while using custom diagnosticDir 
717+ function  getHeapSnapshotFilename ( diagnosticDir )  { 
718+   if  ( ! diagnosticDir )  return  undefined ; 
719+ 
720+   const  date  =  new  Date ( ) ; 
721+ 
722+   const  year  =  DatePrototypeGetFullYear ( date ) ; 
723+   const  month  =  String ( DatePrototypeGetMonth ( date )  +  1 ) . padStart ( 2 ,  '0' ) ; 
724+   const  day  =  String ( DatePrototypeGetDate ( date ) ) . padStart ( 2 ,  '0' ) ; 
725+   const  hours  =  String ( DatePrototypeGetHours ( date ) ) . padStart ( 2 ,  '0' ) ; 
726+   const  minutes  =  String ( DatePrototypeGetMinutes ( date ) ) . padStart ( 2 ,  '0' ) ; 
727+   const  seconds  =  String ( DatePrototypeGetSeconds ( date ) ) . padStart ( 2 ,  '0' ) ; 
728+ 
729+   const  dateString  =  `${ year } ${ month } ${ day }  ` ; 
730+   const  timeString  =  `${ hours } ${ minutes } ${ seconds }  ` ; 
731+   const  pid  =  process . pid ; 
732+   const  threadId  =  internalBinding ( 'worker' ) . threadId ; 
733+   const  fileSequence  =  ( ++ sequenceNumOfheapSnapshot ) . toString ( ) . padStart ( 3 ,  '0' ) ; 
734+ 
735+   return  `${ diagnosticDir }  /Heap.${ dateString }  .${ timeString }  .${ pid }  .${ threadId }  .${ fileSequence }  .heapsnapshot` ; 
736+ } 
737+ 
703738module . exports  =  { 
704739  setupUserModules, 
705740  prepareMainThreadExecution, 
0 commit comments