@@ -332,7 +332,7 @@ void SignalHandler::HandleProfilerSignal(int sig,
332
332
auto time_from = Now ();
333
333
old_handler (sig, info, context);
334
334
auto time_to = Now ();
335
- auto async_id = prof->GetAsyncId (isolate );
335
+ auto async_id = prof->GetAsyncId ();
336
336
prof->PushContext (time_from, time_to, cpu_time, async_id);
337
337
}
338
338
#else
@@ -384,20 +384,20 @@ void WallProfiler::CleanupHook(void* data) {
384
384
auto isolate = static_cast <Isolate*>(data);
385
385
auto prof = g_profilers.RemoveProfilerForIsolate (isolate);
386
386
if (prof) {
387
- prof->Cleanup (isolate );
387
+ prof->Cleanup ();
388
388
delete prof;
389
389
}
390
390
}
391
391
392
392
// This is only called when isolate is terminated without `beforeExit`
393
393
// notification.
394
- void WallProfiler::Cleanup (Isolate* isolate ) {
394
+ void WallProfiler::Cleanup () {
395
395
if (started_) {
396
396
cpuProfiler_->Stop (profileId_);
397
397
if (interceptSignal ()) {
398
398
SignalHandler::DecreaseUseCount ();
399
399
}
400
- Dispose (isolate, false );
400
+ Dispose (false );
401
401
}
402
402
}
403
403
@@ -411,18 +411,17 @@ ContextsByNode WallProfiler::GetContextsByNode(CpuProfile* profile,
411
411
return contextsByNode;
412
412
}
413
413
414
- auto isolate = Isolate::GetCurrent ();
415
- auto v8Context = isolate->GetCurrentContext ();
414
+ auto v8Context = isolate_->GetCurrentContext ();
416
415
auto contextIt = contexts.begin ();
417
416
418
417
// deltaIdx is the offset of the sample to process compared to current
419
418
// iteration index
420
419
int deltaIdx = 0 ;
421
420
422
- auto contextKey = String::NewFromUtf8Literal (isolate , " context" );
423
- auto timestampKey = String::NewFromUtf8Literal (isolate , " timestamp" );
424
- auto cpuTimeKey = String::NewFromUtf8Literal (isolate , " cpuTime" );
425
- auto asyncIdKey = String::NewFromUtf8Literal (isolate , " asyncId" );
421
+ auto contextKey = String::NewFromUtf8Literal (isolate_ , " context" );
422
+ auto timestampKey = String::NewFromUtf8Literal (isolate_ , " timestamp" );
423
+ auto cpuTimeKey = String::NewFromUtf8Literal (isolate_ , " cpuTime" );
424
+ auto asyncIdKey = String::NewFromUtf8Literal (isolate_ , " asyncId" );
426
425
auto V8toEpochOffset = GetV8ToEpochOffset ();
427
426
auto lastCpuTime = startCpuTime;
428
427
@@ -467,18 +466,18 @@ ContextsByNode WallProfiler::GetContextsByNode(CpuProfile* profile,
467
466
auto it = contextsByNode.find (sample);
468
467
Local<Array> array;
469
468
if (it == contextsByNode.end ()) {
470
- array = Array::New (isolate );
469
+ array = Array::New (isolate_ );
471
470
contextsByNode[sample] = {array, 1 };
472
471
} else {
473
472
array = it->second .contexts ;
474
473
++it->second .hitcount ;
475
474
}
476
475
// Conforms to TimeProfileNodeContext defined in v8-types.ts
477
- Local<Object> timedContext = Object::New (isolate );
476
+ Local<Object> timedContext = Object::New (isolate_ );
478
477
timedContext
479
478
->Set (v8Context,
480
479
timestampKey,
481
- BigInt::New (isolate , sampleTimestamp + V8toEpochOffset))
480
+ BigInt::New (isolate_ , sampleTimestamp + V8toEpochOffset))
482
481
.Check ();
483
482
auto * function_name = sample->GetFunctionNameStr ();
484
483
// If current sample is program, reports its cpu time to the next sample
@@ -488,7 +487,7 @@ ContextsByNode WallProfiler::GetContextsByNode(CpuProfile* profile,
488
487
->Set (
489
488
v8Context,
490
489
cpuTimeKey,
491
- Number::New (isolate , sampleContext.cpu_time - lastCpuTime))
490
+ Number::New (isolate_ , sampleContext.cpu_time - lastCpuTime))
492
491
.Check ();
493
492
lastCpuTime = sampleContext.cpu_time ;
494
493
}
@@ -499,14 +498,14 @@ ContextsByNode WallProfiler::GetContextsByNode(CpuProfile* profile,
499
498
timedContext
500
499
->Set (v8Context,
501
500
contextKey,
502
- sampleContext.context .get ()->Get (isolate ))
501
+ sampleContext.context .get ()->Get (isolate_ ))
503
502
.Check ();
504
503
}
505
504
if (collectAsyncId_) {
506
505
timedContext
507
506
->Set (v8Context,
508
507
asyncIdKey,
509
- Number::New (isolate , sampleContext.async_id ))
508
+ Number::New (isolate_ , sampleContext.async_id ))
510
509
.Check ();
511
510
}
512
511
}
@@ -527,7 +526,7 @@ void GCPrologueCallback(Isolate* isolate,
527
526
GCType type,
528
527
GCCallbackFlags flags,
529
528
void * data) {
530
- static_cast <WallProfiler*>(data)->OnGCStart (isolate );
529
+ static_cast <WallProfiler*>(data)->OnGCStart ();
531
530
}
532
531
533
532
void GCEpilogueCallback (Isolate* isolate,
@@ -565,39 +564,38 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
565
564
collectionMode_.store (CollectionMode::kNoCollect , std::memory_order_relaxed);
566
565
gcCount.store (0 , std::memory_order_relaxed);
567
566
568
- // TODO: bind to this isolate? Would fix the Dispose(nullptr) issue.
569
- auto isolate = v8::Isolate::GetCurrent ();
567
+ isolate_ = v8::Isolate::GetCurrent ();
570
568
v8::Local<v8::ArrayBuffer> buffer =
571
- v8::ArrayBuffer::New (isolate , sizeof (uint32_t ) * kFieldCount );
569
+ v8::ArrayBuffer::New (isolate_ , sizeof (uint32_t ) * kFieldCount );
572
570
573
571
v8::Local<v8::Uint32Array> jsArray =
574
572
v8::Uint32Array::New (buffer, 0 , kFieldCount );
575
573
fields_ = static_cast <uint32_t *>(buffer->GetBackingStore ()->Data ());
576
- jsArray_ = v8::Global<v8::Uint32Array>(isolate , jsArray);
574
+ jsArray_ = v8::Global<v8::Uint32Array>(isolate_ , jsArray);
577
575
std::fill (fields_, fields_ + kFieldCount , 0 );
578
576
579
577
if (collectAsyncId_) {
580
- isolate ->AddGCPrologueCallback (&GCPrologueCallback, this );
581
- isolate ->AddGCEpilogueCallback (&GCEpilogueCallback, this );
578
+ isolate_ ->AddGCPrologueCallback (&GCPrologueCallback, this );
579
+ isolate_ ->AddGCEpilogueCallback (&GCEpilogueCallback, this );
582
580
}
583
581
}
584
582
585
- void WallProfiler::Dispose (Isolate* isolate, bool removeFromMap) {
583
+ void WallProfiler::Dispose (bool removeFromMap) {
586
584
if (cpuProfiler_ != nullptr ) {
587
585
cpuProfiler_->Dispose ();
588
586
cpuProfiler_ = nullptr ;
589
587
590
588
if (removeFromMap) {
591
- g_profilers.RemoveProfiler (isolate , this );
589
+ g_profilers.RemoveProfiler (isolate_ , this );
592
590
}
593
591
594
592
if (collectAsyncId_) {
595
- isolate ->RemoveGCPrologueCallback (&GCPrologueCallback, this );
596
- isolate ->RemoveGCEpilogueCallback (&GCEpilogueCallback, this );
593
+ isolate_ ->RemoveGCPrologueCallback (&GCPrologueCallback, this );
594
+ isolate_ ->RemoveGCEpilogueCallback (&GCEpilogueCallback, this );
597
595
}
598
596
599
597
node::RemoveEnvironmentCleanupHook (
600
- isolate , &WallProfiler::CleanupHook, isolate );
598
+ isolate_ , &WallProfiler::CleanupHook, isolate_ );
601
599
}
602
600
}
603
601
@@ -738,8 +736,8 @@ Result WallProfiler::StartImpl() {
738
736
: CollectionMode::kNoCollect );
739
737
collectionMode_.store (collectionMode, std::memory_order_relaxed);
740
738
started_ = true ;
741
- auto isolate = Isolate::GetCurrent ();
742
- node::AddEnvironmentCleanupHook (isolate , &WallProfiler::CleanupHook, isolate );
739
+ node::AddEnvironmentCleanupHook (
740
+ isolate_ , &WallProfiler::CleanupHook, isolate_ );
743
741
return {};
744
742
}
745
743
@@ -786,8 +784,8 @@ v8::ProfilerId WallProfiler::StartInternal() {
786
784
// we wait for one signal before starting a new profile which should leave
787
785
// time to process in-flight tick samples.
788
786
if (detectV8Bug_ && !workaroundV8Bug_) {
789
- cpuProfiler_->CollectSample (v8::Isolate::GetCurrent () );
790
- cpuProfiler_->CollectSample (v8::Isolate::GetCurrent () );
787
+ cpuProfiler_->CollectSample (isolate_ );
788
+ cpuProfiler_->CollectSample (isolate_ );
791
789
}
792
790
793
791
return result.id ;
@@ -933,7 +931,7 @@ Result WallProfiler::StopImpl(bool restart, v8::Local<v8::Value>& profile) {
933
931
v8_profile->Delete ();
934
932
935
933
if (!restart) {
936
- Dispose (v8::Isolate::GetCurrent (), true );
934
+ Dispose (true );
937
935
} else if (workaroundV8Bug_) {
938
936
waitForSignal (callCount + 1 );
939
937
collectionMode_.store (withContexts_ ? CollectionMode::kCollectContexts
@@ -968,12 +966,11 @@ NAN_MODULE_INIT(WallProfiler::Init) {
968
966
Nan::New (" state" ).ToLocalChecked (),
969
967
SharedArrayGetter);
970
968
971
- PerIsolateData::For ( Isolate::GetCurrent ())
972
- ->WallProfilerConstructor ()
973
- . Reset ( Nan::GetFunction (tpl).ToLocalChecked ());
969
+ auto isolate = Isolate::GetCurrent ();
970
+ PerIsolateData::For (isolate) ->WallProfilerConstructor (). Reset (
971
+ Nan::GetFunction (tpl).ToLocalChecked ());
974
972
Nan::Set (target, className, Nan::GetFunction (tpl).ToLocalChecked ());
975
973
976
- auto isolate = v8::Isolate::GetCurrent ();
977
974
v8::PropertyAttribute ReadOnlyDontDelete =
978
975
static_cast <v8::PropertyAttribute>(ReadOnly | DontDelete);
979
976
@@ -992,28 +989,26 @@ NAN_MODULE_INIT(WallProfiler::Init) {
992
989
993
990
v8::CpuProfiler* WallProfiler::CreateV8CpuProfiler () {
994
991
if (cpuProfiler_ == nullptr ) {
995
- v8::Isolate* isolate = v8::Isolate::GetCurrent ();
996
-
997
- bool inserted = g_profilers.AddProfiler (isolate, this );
992
+ bool inserted = g_profilers.AddProfiler (isolate_, this );
998
993
999
994
if (!inserted) {
1000
995
// refuse to create a new profiler if one is already active
1001
996
return nullptr ;
1002
997
}
1003
- cpuProfiler_ = v8::CpuProfiler::New (isolate );
998
+ cpuProfiler_ = v8::CpuProfiler::New (isolate_ );
1004
999
cpuProfiler_->SetSamplingInterval (
1005
1000
std::chrono::microseconds (samplingPeriod_).count ());
1006
1001
}
1007
1002
return cpuProfiler_;
1008
1003
}
1009
1004
1010
- v8::Local<v8::Value> WallProfiler::GetContext (Isolate* isolate ) {
1005
+ v8::Local<v8::Value> WallProfiler::GetContext () {
1011
1006
auto context = *curContext_.load (std::memory_order_relaxed);
1012
- if (!context) return v8::Undefined (isolate );
1013
- return context->Get (isolate );
1007
+ if (!context) return v8::Undefined (isolate_ );
1008
+ return context->Get (isolate_ );
1014
1009
}
1015
1010
1016
- void WallProfiler::SetContext (Isolate* isolate, Local<Value> value) {
1011
+ void WallProfiler::SetContext (Local<Value> value) {
1017
1012
// Need to be careful here, because we might be interrupted by a
1018
1013
// signal handler that will make use of curContext_.
1019
1014
// Update of shared_ptr is not atomic, so instead we use a pointer
@@ -1024,7 +1019,7 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
1024
1019
? &context2_
1025
1020
: &context1_;
1026
1021
if (!value->IsNullOrUndefined ()) {
1027
- *newCurContext = std::make_shared<Global<Value>>(isolate , value);
1022
+ *newCurContext = std::make_shared<Global<Value>>(isolate_ , value);
1028
1023
} else {
1029
1024
newCurContext->reset ();
1030
1025
}
@@ -1034,17 +1029,17 @@ void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
1034
1029
1035
1030
NAN_GETTER (WallProfiler::GetContext) {
1036
1031
auto profiler = Nan::ObjectWrap::Unwrap<WallProfiler>(info.This ());
1037
- info.GetReturnValue ().Set (profiler->GetContext (info. GetIsolate () ));
1032
+ info.GetReturnValue ().Set (profiler->GetContext ());
1038
1033
}
1039
1034
1040
1035
NAN_SETTER (WallProfiler::SetContext) {
1041
1036
auto profiler = Nan::ObjectWrap::Unwrap<WallProfiler>(info.This ());
1042
- profiler->SetContext (info. GetIsolate (), value);
1037
+ profiler->SetContext (value);
1043
1038
}
1044
1039
1045
1040
NAN_GETTER (WallProfiler::SharedArrayGetter) {
1046
1041
auto profiler = Nan::ObjectWrap::Unwrap<WallProfiler>(info.This ());
1047
- info.GetReturnValue ().Set (profiler->jsArray_ . Get ( v8::Isolate::GetCurrent () ));
1042
+ info.GetReturnValue ().Set (profiler->GetSharedArray ( ));
1048
1043
}
1049
1044
1050
1045
NAN_METHOD (WallProfiler::V8ProfilerStuckEventLoopDetected) {
@@ -1071,7 +1066,7 @@ double GetAsyncIdNoGC(v8::Isolate* isolate) {
1071
1066
#endif
1072
1067
}
1073
1068
1074
- double WallProfiler::GetAsyncId (v8::Isolate* isolate ) {
1069
+ double WallProfiler::GetAsyncId () {
1075
1070
if (!collectAsyncId_) {
1076
1071
return -1 ;
1077
1072
}
@@ -1080,14 +1075,14 @@ double WallProfiler::GetAsyncId(v8::Isolate* isolate) {
1080
1075
if (curGcCount > 0 ) {
1081
1076
return gcAsyncId;
1082
1077
}
1083
- return GetAsyncIdNoGC (isolate );
1078
+ return GetAsyncIdNoGC (isolate_ );
1084
1079
}
1085
1080
1086
- void WallProfiler::OnGCStart (v8::Isolate* isolate ) {
1081
+ void WallProfiler::OnGCStart () {
1087
1082
auto curCount = gcCount.load (std::memory_order_relaxed);
1088
1083
std::atomic_signal_fence (std::memory_order_acquire);
1089
1084
if (curCount == 0 ) {
1090
- gcAsyncId = GetAsyncIdNoGC (isolate );
1085
+ gcAsyncId = GetAsyncIdNoGC (isolate_ );
1091
1086
}
1092
1087
gcCount.store (curCount + 1 , std::memory_order_relaxed);
1093
1088
std::atomic_signal_fence (std::memory_order_release);
0 commit comments