@@ -474,11 +474,13 @@ std::shared_ptr<ContextsByNode> WallProfiler::GetContextsByNode(
474
474
sampleContext.context .get ()->Get (isolate))
475
475
.Check ();
476
476
}
477
- timedContext
478
- ->Set (v8Context,
479
- asyncIdKey,
480
- NewNumberFromInt64 (isolate, sampleContext.async_id ))
481
- .Check ();
477
+ if (collectAsyncId_) {
478
+ timedContext
479
+ ->Set (v8Context,
480
+ asyncIdKey,
481
+ NewNumberFromInt64 (isolate, sampleContext.async_id ))
482
+ .Check ();
483
+ }
482
484
}
483
485
}
484
486
array->Set (v8Context, array->Length (), timedContext).Check ();
@@ -513,6 +515,7 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
513
515
bool withContexts,
514
516
bool workaroundV8Bug,
515
517
bool collectCpuTime,
518
+ bool collectAsyncId,
516
519
bool isMainThread)
517
520
: samplingPeriod_(samplingPeriod),
518
521
includeLines_ (includeLines),
@@ -524,6 +527,7 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
524
527
// event just after triggers the issue.
525
528
workaroundV8Bug_ = workaroundV8Bug && DD_WALL_USE_SIGPROF && detectV8Bug_;
526
529
collectCpuTime_ = collectCpuTime && withContexts;
530
+ collectAsyncId_ = collectAsyncId && withContexts;
527
531
528
532
if (withContexts_) {
529
533
contexts_.reserve (duration * 2 / samplingPeriod);
@@ -544,8 +548,10 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
544
548
jsArray_ = v8::Global<v8::Uint32Array>(isolate, jsArray);
545
549
std::fill (fields_, fields_ + kFieldCount , 0 );
546
550
547
- isolate->AddGCPrologueCallback (&GCPrologueCallback, this );
548
- isolate->AddGCEpilogueCallback (&GCEpilogueCallback, this );
551
+ if (collectAsyncId_) {
552
+ isolate->AddGCPrologueCallback (&GCPrologueCallback, this );
553
+ isolate->AddGCEpilogueCallback (&GCEpilogueCallback, this );
554
+ }
549
555
}
550
556
551
557
WallProfiler::~WallProfiler () {
@@ -559,13 +565,21 @@ void WallProfiler::Dispose(Isolate* isolate) {
559
565
560
566
g_profilers.RemoveProfiler (isolate, this );
561
567
562
- if (isolate != nullptr ) {
568
+ if (isolate != nullptr && collectAsyncId_ ) {
563
569
isolate->RemoveGCPrologueCallback (&GCPrologueCallback, this );
564
570
isolate->RemoveGCEpilogueCallback (&GCEpilogueCallback, this );
565
571
}
566
572
}
567
573
}
568
574
575
+ #define DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (name ) \
576
+ auto name##Value = \
577
+ Nan::Get (arg, Nan::New<v8::String>(#name).ToLocalChecked()); \
578
+ if (name##Value.IsEmpty() || !name##Value.ToLocalChecked()->IsBoolean ()) { \
579
+ return Nan::ThrowTypeError (#name " must be a boolean." ); \
580
+ } \
581
+ bool name = name##Value.ToLocalChecked().As<v8::Boolean>()->Value ();
582
+
569
583
NAN_METHOD (WallProfiler::New) {
570
584
if (info.Length () != 1 || !info[0 ]->IsObject ()) {
571
585
return Nan::ThrowTypeError (" WallProfiler must have one object argument." );
@@ -604,50 +618,12 @@ NAN_METHOD(WallProfiler::New) {
604
618
return Nan::ThrowTypeError (" Duration must not be less than sample rate." );
605
619
}
606
620
607
- auto lineNumbersValue =
608
- Nan::Get (arg, Nan::New<v8::String>(" lineNumbers" ).ToLocalChecked ());
609
- if (lineNumbersValue.IsEmpty () ||
610
- !lineNumbersValue.ToLocalChecked ()->IsBoolean ()) {
611
- return Nan::ThrowTypeError (" lineNumbers must be a boolean." );
612
- }
613
- bool lineNumbers =
614
- lineNumbersValue.ToLocalChecked ().As <v8::Boolean>()->Value ();
615
-
616
- auto withContextsValue =
617
- Nan::Get (arg, Nan::New<v8::String>(" withContexts" ).ToLocalChecked ());
618
- if (withContextsValue.IsEmpty () ||
619
- !withContextsValue.ToLocalChecked ()->IsBoolean ()) {
620
- return Nan::ThrowTypeError (" withContext must be a boolean." );
621
- }
622
- bool withContexts =
623
- withContextsValue.ToLocalChecked ().As <v8::Boolean>()->Value ();
624
-
625
- auto workaroundV8BugValue =
626
- Nan::Get (arg, Nan::New<v8::String>(" workaroundV8Bug" ).ToLocalChecked ());
627
- if (workaroundV8BugValue.IsEmpty () ||
628
- !workaroundV8BugValue.ToLocalChecked ()->IsBoolean ()) {
629
- return Nan::ThrowTypeError (" workaroundV8Bug must be a boolean." );
630
- }
631
- bool workaroundV8Bug =
632
- workaroundV8BugValue.ToLocalChecked ().As <v8::Boolean>()->Value ();
633
-
634
- auto collectCpuTimeValue =
635
- Nan::Get (arg, Nan::New<v8::String>(" collectCpuTime" ).ToLocalChecked ());
636
- if (collectCpuTimeValue.IsEmpty () ||
637
- !collectCpuTimeValue.ToLocalChecked ()->IsBoolean ()) {
638
- return Nan::ThrowTypeError (" collectCpuTime must be a boolean." );
639
- }
640
- bool collectCpuTime =
641
- collectCpuTimeValue.ToLocalChecked ().As <v8::Boolean>()->Value ();
642
-
643
- auto isMainThreadValue =
644
- Nan::Get (arg, Nan::New<v8::String>(" isMainThread" ).ToLocalChecked ());
645
- if (isMainThreadValue.IsEmpty () ||
646
- !isMainThreadValue.ToLocalChecked ()->IsBoolean ()) {
647
- return Nan::ThrowTypeError (" isMainThread must be a boolean." );
648
- }
649
- bool isMainThread =
650
- isMainThreadValue.ToLocalChecked ().As <v8::Boolean>()->Value ();
621
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (lineNumbers);
622
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (withContexts);
623
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (workaroundV8Bug);
624
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (collectCpuTime);
625
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (collectAsyncId);
626
+ DD_WALL_PROFILER_GET_BOOLEAN_CONFIG (isMainThread);
651
627
652
628
if (withContexts && !DD_WALL_USE_SIGPROF) {
653
629
return Nan::ThrowTypeError (" Contexts are not supported." );
@@ -657,6 +633,10 @@ NAN_METHOD(WallProfiler::New) {
657
633
return Nan::ThrowTypeError (" Cpu time collection requires contexts." );
658
634
}
659
635
636
+ if (collectAsyncId && !withContexts) {
637
+ return Nan::ThrowTypeError (" Async ID collection requires contexts." );
638
+ }
639
+
660
640
if (lineNumbers && withContexts) {
661
641
// Currently custom contexts are not compatible with caller line
662
642
// information, because it's not possible to associate context with line
@@ -682,6 +662,7 @@ NAN_METHOD(WallProfiler::New) {
682
662
withContexts,
683
663
workaroundV8Bug,
684
664
collectCpuTime,
665
+ collectAsyncId,
685
666
isMainThread);
686
667
obj->Wrap (info.This ());
687
668
info.GetReturnValue ().Set (info.This ());
@@ -693,6 +674,8 @@ NAN_METHOD(WallProfiler::New) {
693
674
}
694
675
}
695
676
677
+ #undef DD_WALL_PROFILER_GET_BOOLEAN_CONFIG
678
+
696
679
NAN_METHOD (WallProfiler::Start) {
697
680
WallProfiler* wallProfiler =
698
681
Nan::ObjectWrap::Unwrap<WallProfiler>(info.Holder ());
@@ -1052,6 +1035,9 @@ int64_t GetAsyncIdNoGC(v8::Isolate* isolate) {
1052
1035
}
1053
1036
1054
1037
int64_t WallProfiler::GetAsyncId (v8::Isolate* isolate) {
1038
+ if (!collectAsyncId_) {
1039
+ return -1 ;
1040
+ }
1055
1041
auto curGcCount = gcCount.load (std::memory_order_relaxed);
1056
1042
std::atomic_signal_fence (std::memory_order_acquire);
1057
1043
if (curGcCount > 0 ) {
0 commit comments