@@ -531,6 +531,7 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
531
531
532
532
curContext_.store (&context1_, std::memory_order_relaxed);
533
533
collectionMode_.store (CollectionMode::kNoCollect , std::memory_order_relaxed);
534
+ gcCount.store (0 , std::memory_order_relaxed);
534
535
535
536
// TODO: bind to this isolate? Would fix the Dispose(nullptr) issue.
536
537
auto isolate = v8::Isolate::GetCurrent ();
@@ -543,7 +544,6 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
543
544
jsArray_ = v8::Global<v8::Uint32Array>(isolate, jsArray);
544
545
std::fill (fields_, fields_ + kFieldCount , 0 );
545
546
546
- gcCount = 0 ;
547
547
isolate->AddGCPrologueCallback (&GCPrologueCallback, this );
548
548
isolate->AddGCEpilogueCallback (&GCEpilogueCallback, this );
549
549
}
@@ -1044,6 +1044,42 @@ NAN_METHOD(WallProfiler::Dispose) {
1044
1044
delete profiler;
1045
1045
}
1046
1046
1047
+ int64_t GetAsyncIdNoGC (v8::Isolate* isolate) {
1048
+ return isolate->InContext ()
1049
+ ? static_cast <int64_t >(
1050
+ node::AsyncHooksGetExecutionAsyncId (isolate))
1051
+ : -1 ;
1052
+ }
1053
+
1054
+ int64_t WallProfiler::GetAsyncId (v8::Isolate* isolate) {
1055
+ auto curGcCount = gcCount.load (std::memory_order_relaxed);
1056
+ std::atomic_signal_fence (std::memory_order_acquire);
1057
+ if (curGcCount > 0 ) {
1058
+ return gcAsyncId;
1059
+ }
1060
+ return GetAsyncIdNoGC (isolate);
1061
+ }
1062
+
1063
+ void WallProfiler::OnGCStart (v8::Isolate* isolate) {
1064
+ auto curCount = gcCount.load (std::memory_order_relaxed);
1065
+ std::atomic_signal_fence (std::memory_order_acquire);
1066
+ if (curCount == 0 ) {
1067
+ gcAsyncId = GetAsyncIdNoGC (isolate);
1068
+ }
1069
+ gcCount.store (curCount + 1 , std::memory_order_relaxed);
1070
+ std::atomic_signal_fence (std::memory_order_release);
1071
+ }
1072
+
1073
+ void WallProfiler::OnGCEnd () {
1074
+ auto newCount = gcCount.load (std::memory_order_relaxed) - 1 ;
1075
+ std::atomic_signal_fence (std::memory_order_acquire);
1076
+ gcCount.store (newCount, std::memory_order_relaxed);
1077
+ std::atomic_signal_fence (std::memory_order_release);
1078
+ if (newCount == 0 ) {
1079
+ gcAsyncId = -1 ;
1080
+ }
1081
+ }
1082
+
1047
1083
void WallProfiler::PushContext (int64_t time_from,
1048
1084
int64_t time_to,
1049
1085
int64_t cpu_time,
0 commit comments