Skip to content

Commit 5b796ea

Browse files
committed
Store profiling context in the CPED
1 parent 51fa650 commit 5b796ea

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

bindings/profilers/wall.cc

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void SignalHandler::HandleProfilerSignal(int sig,
323323
auto time_from = Now();
324324
old_handler(sig, info, context);
325325
auto time_to = Now();
326-
prof->PushContext(time_from, time_to, cpu_time);
326+
prof->PushContext(time_from, time_to, cpu_time, isolate);
327327
}
328328
#else
329329
class SignalHandler {
@@ -509,6 +509,8 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
509509
#endif
510510
jsArray_ = v8::Global<v8::Uint32Array>(isolate, jsArray);
511511
std::fill(fields_, fields_ + kFieldCount, 0);
512+
513+
cpedSymbol_ = v8::Global<v8::Symbol>(isolate, v8::Symbol::New(isolate));
512514
}
513515

514516
WallProfiler::~WallProfiler() {
@@ -953,23 +955,57 @@ v8::CpuProfiler* WallProfiler::CreateV8CpuProfiler() {
953955
}
954956

955957
v8::Local<v8::Value> WallProfiler::GetContext(Isolate* isolate) {
956-
auto context = GetContextPtr();
958+
auto context = GetContextPtr(isolate);
957959
if (!context) return v8::Undefined(isolate);
958960
return context->Get(isolate);
959961
}
960962

961963
void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
964+
auto cped = isolate->GetContinuationPreservedEmbedderData();
965+
// No Node AsyncContextFrame in this continuation yet
966+
if (!cped->IsObject()) return;
967+
968+
auto cpedObj = cped.As<Object>();
969+
auto localSymbol = cpedSymbol_.Get(isolate);
970+
auto profData = cpedObj->Get(localSymbol);
971+
972+
ContextPtr* contextPtr = nullptr;
973+
if (profData->IsNullOrUndefined()) {
974+
contextPtr = new ContextPtr();
975+
cpedObj->Set(localSymbol, External::New(isolate, contextPtr));
976+
977+
// Register a callback to delete contextPtr when the CPED object is GCed
978+
Persistent<Object>(isolate, cpedObj).SetWeak(contextPtr, [](const WeakCallbackInfo<ContextPtr>& data) {
979+
// Using SetSecondPassCallback as shared_ptr can trigger ~Global and any V8 API use needs to be in the second pass
980+
data.SetSecondPassCallback([](const WeakCallbackInfo<ContextPtr>& data) {
981+
delete data.GetParameter();
982+
});
983+
}, WeakCallbackType::kParameter);
984+
} else {
985+
contextPtr = reinterpret_cast<ContextPtr*>(profData->As<External>()->Value());
986+
}
987+
988+
// Signal-atomic update
962989
std::atomic_signal_fence(std::memory_order_release);
963990
std::atomic_store_explicit(
964-
&curContext_,
991+
contextPtr,
965992
value->IsNullOrUndefined()
966993
? std::shared_ptr<Global<Value>>()
967994
: std::make_shared<Global<Value>>(isolate, value),
968995
std::memory_order_relaxed);
969996
}
970997

971-
ContextPtr WallProfiler::GetContextPtr() {
972-
auto contextPtr = atomic_load_explicit(&curContext_, std::memory_order_relaxed);
998+
ContextPtr WallProfiler::GetContextPtr(Isolate* isolate) {
999+
auto cped = isolate->GetContinuationPreservedEmbedderData(); // will this fly in a signal handler?
1000+
if (!cped->IsObject()) return nullptr;
1001+
1002+
auto cpedObj = cped.As<Object>();
1003+
auto localSymbol = cpedSymbol_.Get(isolate); // will this fly in a signal handler?
1004+
auto profData = cpedObj->Get(localSymbol); // will this fly in a signal handler?
1005+
1006+
if (profData->IsNullOrUndefined()) return std::shared_ptr<Global<Value>>();
1007+
1008+
auto contextPtr = std::atomic_load_explicit(reinterpret_cast<ContextPtr*>(profData->As<External>()->Value()), std::memory_order_relaxed);
9731009
std::atomic_signal_fence(std::memory_order_acquire);
9741010
return contextPtr;
9751011
}
@@ -1001,12 +1037,14 @@ NAN_METHOD(WallProfiler::Dispose) {
10011037

10021038
void WallProfiler::PushContext(int64_t time_from,
10031039
int64_t time_to,
1004-
int64_t cpu_time) {
1040+
int64_t cpu_time,
1041+
Isolate* isolate) {
10051042
// Be careful this is called in a signal handler context therefore all
10061043
// operations must be async signal safe (in particular no allocations).
10071044
// Our ring buffer avoids allocations.
10081045
if (contexts_.size() < contexts_.capacity()) {
1009-
contexts_.push_back({GetContextPtr(), time_from, time_to, cpu_time});
1046+
HandleScope handle_scope(isolate); // will this fly in a signal handler?
1047+
contexts_.push_back({GetContextPtr(isolate), time_from, time_to, cpu_time});
10101048
std::atomic_fetch_add_explicit(
10111049
reinterpret_cast<std::atomic<uint32_t>*>(&fields_[kSampleCount]),
10121050
1U,

bindings/profilers/wall.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class WallProfiler : public Nan::ObjectWrap {
5151
// avoid heap allocation. Need to figure out the right move/copy semantics in
5252
// and out of the ring buffer.
5353

54-
ContextPtr curContext_;
54+
v8::Global<v8::Symbol> cpedSymbol_;
5555

5656
std::atomic<CollectionMode> collectionMode_;
5757
std::atomic<uint64_t> noCollectCallCount_;
@@ -95,7 +95,7 @@ class WallProfiler : public Nan::ObjectWrap {
9595
int64_t startCpuTime);
9696

9797
bool waitForSignal(uint64_t targetCallCount = 0);
98-
ContextPtr GetContextPtr();
98+
ContextPtr GetContextPtr(v8::Isolate* isolate);
9999

100100
public:
101101
/**
@@ -115,7 +115,7 @@ class WallProfiler : public Nan::ObjectWrap {
115115

116116
v8::Local<v8::Value> GetContext(v8::Isolate*);
117117
void SetContext(v8::Isolate*, v8::Local<v8::Value>);
118-
void PushContext(int64_t time_from, int64_t time_to, int64_t cpu_time);
118+
void PushContext(int64_t time_from, int64_t time_to, int64_t cpu_time, v8::Isolate* isolate);
119119
Result StartImpl();
120120
std::string StartInternal();
121121
Result StopImpl(bool restart, v8::Local<v8::Value>& profile);

0 commit comments

Comments
 (0)