@@ -366,6 +366,27 @@ static int64_t GetV8ToEpochOffset() {
366
366
return V8toEpochOffset;
367
367
}
368
368
369
+ void WallProfiler::CleanupHook (void * data) {
370
+ auto isolate = static_cast <Isolate*>(data);
371
+ auto prof = g_profilers.GetProfiler (isolate);
372
+ if (prof) {
373
+ prof->Cleanup (isolate);
374
+ delete prof;
375
+ }
376
+ }
377
+
378
+ // This is only called when isolate is terminated without `beforeExit`
379
+ // notification.
380
+ void WallProfiler::Cleanup (Isolate* isolate) {
381
+ if (started_) {
382
+ cpuProfiler_->Stop (profileId_);
383
+ if (interceptSignal ()) {
384
+ SignalHandler::DecreaseUseCount ();
385
+ }
386
+ Dispose (isolate);
387
+ }
388
+ }
389
+
369
390
ContextsByNode WallProfiler::GetContextsByNode (CpuProfile* profile,
370
391
ContextBuffer& contexts,
371
392
int64_t startCpuTime) {
@@ -562,6 +583,9 @@ void WallProfiler::Dispose(Isolate* isolate) {
562
583
isolate->RemoveGCPrologueCallback (&GCPrologueCallback, this );
563
584
isolate->RemoveGCEpilogueCallback (&GCEpilogueCallback, this );
564
585
}
586
+
587
+ node::RemoveEnvironmentCleanupHook (
588
+ isolate, &WallProfiler::CleanupHook, isolate);
565
589
}
566
590
}
567
591
@@ -702,17 +726,19 @@ Result WallProfiler::StartImpl() {
702
726
: CollectionMode::kNoCollect );
703
727
collectionMode_.store (collectionMode, std::memory_order_relaxed);
704
728
started_ = true ;
729
+ auto isolate = Isolate::GetCurrent ();
730
+ node::AddEnvironmentCleanupHook (isolate, &WallProfiler::CleanupHook, isolate);
705
731
return {};
706
732
}
707
733
708
- std::string WallProfiler::StartInternal () {
734
+ v8::ProfilerId WallProfiler::StartInternal () {
709
735
// Reuse the same names for the profiles because strings used for profile
710
736
// names are not released until v8::CpuProfiler object is destroyed.
711
737
// https://github.com/nodejs/node/blob/b53c51995380b1f8d642297d848cab6010d2909c/deps/v8/src/profiler/profile-generator.h#L516
712
738
char buf[128 ];
713
739
snprintf (buf, sizeof (buf), " pprof-%" PRId64, (profileIdx_++) % 2 );
714
740
v8::Local<v8::String> title = Nan::New<String>(buf).ToLocalChecked ();
715
- cpuProfiler_->StartProfiling (
741
+ auto result = cpuProfiler_->Start (
716
742
title,
717
743
includeLines_ ? CpuProfilingMode::kCallerLineNumbers
718
744
: CpuProfilingMode::kLeafNodeLineNumbers ,
@@ -752,7 +778,7 @@ std::string WallProfiler::StartInternal() {
752
778
cpuProfiler_->CollectSample (v8::Isolate::GetCurrent ());
753
779
}
754
780
755
- return buf ;
781
+ return result. id ;
756
782
}
757
783
758
784
NAN_METHOD (WallProfiler::Stop) {
@@ -837,12 +863,11 @@ Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
837
863
std::atomic_signal_fence (std::memory_order_acquire);
838
864
}
839
865
840
- if (withContexts_ || workaroundV8Bug_ ) {
866
+ if (interceptSignal () ) {
841
867
SignalHandler::DecreaseUseCount ();
842
868
}
843
869
844
- auto v8_profile = cpuProfiler_->StopProfiling (
845
- Nan::New<String>(oldProfileId).ToLocalChecked ());
870
+ auto v8_profile = cpuProfiler_->Stop (oldProfileId);
846
871
847
872
ContextBuffer contexts;
848
873
if (withContexts_) {
@@ -1016,6 +1041,7 @@ NAN_METHOD(WallProfiler::V8ProfilerStuckEventLoopDetected) {
1016
1041
}
1017
1042
1018
1043
NAN_METHOD (WallProfiler::Dispose) {
1044
+ // Profiler should already be stopped when this is called.
1019
1045
auto profiler = Nan::ObjectWrap::Unwrap<WallProfiler>(info.This ());
1020
1046
delete profiler;
1021
1047
}
0 commit comments