Skip to content

Commit 1cfbdfb

Browse files
committed
Allow use of CPED to store sampling context
1 parent a694680 commit 1cfbdfb

File tree

3 files changed

+169
-39
lines changed

3 files changed

+169
-39
lines changed

bindings/profilers/wall.cc

Lines changed: 125 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ using namespace v8;
5858

5959
namespace dd {
6060

61+
using ContextPtr = std::shared_ptr<Global<Value>>;
62+
6163
// Maximum number of rounds in the GetV8ToEpochOffset
6264
static constexpr int MAX_EPOCH_OFFSET_ATTEMPTS = 20;
6365

@@ -318,8 +320,7 @@ void SignalHandler::HandleProfilerSignal(int sig,
318320
auto time_from = Now();
319321
old_handler(sig, info, context);
320322
auto time_to = Now();
321-
auto async_id = prof->GetAsyncId(isolate);
322-
prof->PushContext(time_from, time_to, cpu_time, async_id);
323+
prof->PushContext(time_from, time_to, cpu_time, isolate);
323324
}
324325
#else
325326
class SignalHandler {
@@ -513,8 +514,10 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
513514
bool withContexts,
514515
bool workaroundV8Bug,
515516
bool collectCpuTime,
516-
bool isMainThread)
517+
bool isMainThread,
518+
bool useCPED)
517519
: samplingPeriod_(samplingPeriod),
520+
useCPED_(useCPED),
518521
includeLines_(includeLines),
519522
withContexts_(withContexts),
520523
isMainThread_(isMainThread) {
@@ -529,7 +532,6 @@ WallProfiler::WallProfiler(std::chrono::microseconds samplingPeriod,
529532
contexts_.reserve(duration * 2 / samplingPeriod);
530533
}
531534

532-
curContext_.store(&context1_, std::memory_order_relaxed);
533535
collectionMode_.store(CollectionMode::kNoCollect, std::memory_order_relaxed);
534536
gcCount.store(0, std::memory_order_relaxed);
535537

@@ -676,13 +678,21 @@ NAN_METHOD(WallProfiler::New) {
676678
"Include line option is not compatible with contexts.");
677679
}
678680

681+
auto useCPEDValue =
682+
Nan::Get(arg, Nan::New<v8::String>("useCPED").ToLocalChecked());
683+
if (useCPEDValue.IsEmpty() || !useCPEDValue.ToLocalChecked()->IsBoolean()) {
684+
return Nan::ThrowTypeError("useCPED must be a boolean.");
685+
}
686+
bool useCPED = useCPEDValue.ToLocalChecked().As<v8::Boolean>()->Value();
687+
679688
WallProfiler* obj = new WallProfiler(interval,
680689
duration,
681690
lineNumbers,
682691
withContexts,
683692
workaroundV8Bug,
684693
collectCpuTime,
685-
isMainThread);
694+
isMainThread,
695+
useCPED);
686696
obj->Wrap(info.This());
687697
info.GetReturnValue().Set(info.This());
688698
} else {
@@ -995,28 +1005,109 @@ v8::CpuProfiler* WallProfiler::CreateV8CpuProfiler() {
9951005
}
9961006

9971007
v8::Local<v8::Value> WallProfiler::GetContext(Isolate* isolate) {
998-
auto context = *curContext_.load(std::memory_order_relaxed);
1008+
auto context = GetContextPtr(isolate);
9991009
if (!context) return v8::Undefined(isolate);
10001010
return context->Get(isolate);
10011011
}
10021012

1013+
class PersistentContextPtr : AtomicContextPtr {
1014+
Persistent<Object> per;
1015+
1016+
void BindLifecycleTo(Isolate* isolate, Local<Object>& obj) {
1017+
// Register a callback to delete this object when the object is GCed
1018+
per.Reset(isolate, obj);
1019+
per.SetWeak(
1020+
this,
1021+
[](const WeakCallbackInfo<PersistentContextPtr>& data) {
1022+
auto& per = data.GetParameter()->per;
1023+
if (!per.IsEmpty()) {
1024+
per.ClearWeak();
1025+
per.Reset();
1026+
}
1027+
// Using SetSecondPassCallback as shared_ptr can trigger ~Global and
1028+
// any V8 API use needs to be in the second pass
1029+
data.SetSecondPassCallback(
1030+
[](const WeakCallbackInfo<PersistentContextPtr>& data) {
1031+
delete data.GetParameter();
1032+
});
1033+
},
1034+
WeakCallbackType::kParameter);
1035+
}
1036+
1037+
friend class WallProfiler;
1038+
};
1039+
10031040
void WallProfiler::SetContext(Isolate* isolate, Local<Value> value) {
1004-
// Need to be careful here, because we might be interrupted by a
1005-
// signal handler that will make use of curContext_.
1006-
// Update of shared_ptr is not atomic, so instead we use a pointer
1007-
// (curContext_) that points on two shared_ptr (context1_ and context2_),
1008-
// update the shared_ptr that is not currently in use and then atomically
1009-
// update curContext_.
1010-
auto newCurContext = curContext_.load(std::memory_order_relaxed) == &context1_
1011-
? &context2_
1012-
: &context1_;
1013-
if (!value->IsNullOrUndefined()) {
1014-
*newCurContext = std::make_shared<Global<Value>>(isolate, value);
1041+
if (!useCPED_) {
1042+
curContext_.Set(isolate, value);
1043+
return;
1044+
}
1045+
1046+
auto cped = isolate->GetContinuationPreservedEmbedderData();
1047+
// No Node AsyncContextFrame in this continuation yet
1048+
if (!cped->IsObject()) return;
1049+
1050+
auto cpedObj = cped.As<Object>();
1051+
auto localSymbol = cpedSymbol_.Get(isolate);
1052+
auto v8Ctx = isolate->GetCurrentContext();
1053+
auto maybeProfData = cpedObj->Get(v8Ctx, localSymbol);
1054+
if (maybeProfData.IsEmpty()) return;
1055+
auto profData = maybeProfData.ToLocalChecked();
1056+
1057+
PersistentContextPtr* contextPtr = nullptr;
1058+
if (profData->IsUndefined()) {
1059+
contextPtr = new PersistentContextPtr();
1060+
1061+
auto maybeSetResult =
1062+
cpedObj->Set(v8Ctx, localSymbol, External::New(isolate, contextPtr));
1063+
if (maybeSetResult.IsNothing()) {
1064+
delete contextPtr;
1065+
return;
1066+
}
1067+
contextPtr->BindLifecycleTo(isolate, cpedObj);
10151068
} else {
1016-
newCurContext->reset();
1069+
contextPtr =
1070+
static_cast<PersistentContextPtr*>(profData.As<External>()->Value());
10171071
}
1018-
std::atomic_signal_fence(std::memory_order_release);
1019-
curContext_.store(newCurContext, std::memory_order_relaxed);
1072+
1073+
contextPtr->Set(isolate, value);
1074+
}
1075+
1076+
ContextPtr WallProfiler::GetContextPtrSignalSafe(Isolate* isolate) {
1077+
if (!useCPED_) {
1078+
// Not strictly necessary but we can avoid HandleScope creation for this
1079+
// case.
1080+
return curContext_.Get();
1081+
}
1082+
1083+
if (gcCount > 0) {
1084+
return gcContext;
1085+
} else if (isolate->InContext()) {
1086+
auto handleScope = HandleScope(isolate);
1087+
return GetContextPtr(isolate);
1088+
}
1089+
// not in a V8 Context
1090+
return std::shared_ptr<Global<Value>>();
1091+
}
1092+
1093+
ContextPtr WallProfiler::GetContextPtr(Isolate* isolate) {
1094+
if (!useCPED_) {
1095+
return curContext_.Get();
1096+
}
1097+
1098+
auto cped = isolate->GetContinuationPreservedEmbedderData();
1099+
if (!cped->IsObject()) return std::shared_ptr<Global<Value>>();
1100+
1101+
auto cpedObj = cped.As<Object>();
1102+
auto localSymbol = cpedSymbol_.Get(isolate);
1103+
auto maybeProfData = cpedObj->Get(isolate->GetCurrentContext(), localSymbol);
1104+
if (maybeProfData.IsEmpty()) return std::shared_ptr<Global<Value>>();
1105+
auto profData = maybeProfData.ToLocalChecked();
1106+
1107+
if (profData->IsUndefined()) return std::shared_ptr<Global<Value>>();
1108+
1109+
return static_cast<PersistentContextPtr*>(profData.As<External>()->Value())
1110+
->Get();
10201111
}
10211112

10221113
NAN_GETTER(WallProfiler::GetContext) {
@@ -1065,7 +1156,10 @@ void WallProfiler::OnGCStart(v8::Isolate* isolate) {
10651156
std::atomic_signal_fence(std::memory_order_acquire);
10661157
if (curCount == 0) {
10671158
gcAsyncId = GetAsyncIdNoGC(isolate);
1068-
}
1159+
if (useCPED_) {
1160+
gcContext = GetContextPtrSignalSafe(isolate);
1161+
}
1162+
}
10691163
gcCount.store(curCount + 1, std::memory_order_relaxed);
10701164
std::atomic_signal_fence(std::memory_order_release);
10711165
}
@@ -1077,20 +1171,25 @@ void WallProfiler::OnGCEnd() {
10771171
std::atomic_signal_fence(std::memory_order_release);
10781172
if (newCount == 0) {
10791173
gcAsyncId = -1;
1080-
}
1174+
if (useCPED_) {
1175+
gcContext.reset();
1176+
}
1177+
}
10811178
}
10821179

10831180
void WallProfiler::PushContext(int64_t time_from,
10841181
int64_t time_to,
10851182
int64_t cpu_time,
1086-
int64_t async_id) {
1183+
Isolate* isolate) {
10871184
// Be careful this is called in a signal handler context therefore all
10881185
// operations must be async signal safe (in particular no allocations).
10891186
// Our ring buffer avoids allocations.
1090-
auto context = curContext_.load(std::memory_order_relaxed);
1091-
std::atomic_signal_fence(std::memory_order_acquire);
10921187
if (contexts_.size() < contexts_.capacity()) {
1093-
contexts_.push_back({*context, time_from, time_to, cpu_time, async_id});
1188+
contexts_.push_back({GetContextPtrSignalSafe(isolate),
1189+
time_from,
1190+
time_to,
1191+
cpu_time,
1192+
GetAsyncId(isolate)});
10941193
std::atomic_fetch_add_explicit(
10951194
reinterpret_cast<std::atomic<uint32_t>*>(&fields_[kSampleCount]),
10961195
1U,

bindings/profilers/wall.hh

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,53 @@ struct Result {
3737
std::string msg;
3838
};
3939

40+
using ContextPtr = std::shared_ptr<v8::Global<v8::Value>>;
41+
42+
class AtomicContextPtr {
43+
ContextPtr ptr1;
44+
ContextPtr ptr2;
45+
std::atomic<ContextPtr*> currentPtr = &ptr1;
46+
47+
void Set(v8::Isolate* isolate, v8::Local<v8::Value> value) {
48+
auto newPtr =
49+
currentPtr.load(std::memory_order_relaxed) == &ptr1 ? &ptr2 : &ptr1;
50+
if (!value->IsNullOrUndefined()) {
51+
*newPtr = std::make_shared<v8::Global<v8::Value>>(isolate, value);
52+
} else {
53+
newPtr->reset();
54+
}
55+
std::atomic_signal_fence(std::memory_order_release);
56+
currentPtr.store(newPtr, std::memory_order_relaxed);
57+
}
58+
59+
ContextPtr Get() {
60+
auto ptr = currentPtr.load(std::memory_order_relaxed);
61+
std::atomic_signal_fence(std::memory_order_acquire);
62+
return ptr ? *ptr : std::shared_ptr<v8::Global<v8::Value>>();
63+
}
64+
65+
friend class WallProfiler;
66+
};
67+
4068
class WallProfiler : public Nan::ObjectWrap {
4169
public:
4270
enum class CollectionMode { kNoCollect, kPassThrough, kCollectContexts };
4371

4472
private:
4573
enum Fields { kSampleCount, kFieldCount };
4674

47-
using ContextPtr = std::shared_ptr<v8::Global<v8::Value>>;
48-
4975
std::chrono::microseconds samplingPeriod_{0};
5076
v8::CpuProfiler* cpuProfiler_ = nullptr;
51-
// TODO: Investigate use of v8::Persistent instead of shared_ptr<Global> to
52-
// avoid heap allocation. Need to figure out the right move/copy semantics in
53-
// and out of the ring buffer.
5477

55-
// We're using a pair of shared pointers and an atomic pointer-to-current as
56-
// a way to ensure signal safety on update.
57-
ContextPtr context1_;
58-
ContextPtr context2_;
59-
std::atomic<ContextPtr*> curContext_;
78+
bool useCPED_ = false;
79+
// If we aren't using the CPED, we use a single context ptr stored here.
80+
AtomicContextPtr curContext_;
81+
// Otherwise we'll use a private symbol to store the context in CPED objects.
82+
v8::Global<v8::Symbol> cpedSymbol_;
6083

6184
std::atomic<int> gcCount = 0;
6285
int64_t gcAsyncId;
86+
ContextPtr gcContext;
6387

6488
std::atomic<CollectionMode> collectionMode_;
6589
std::atomic<uint64_t> noCollectCallCount_;
@@ -105,6 +129,8 @@ class WallProfiler : public Nan::ObjectWrap {
105129
GENERAL_REGS_ONLY;
106130

107131
bool waitForSignal(uint64_t targetCallCount = 0);
132+
ContextPtr GetContextPtr(v8::Isolate* isolate);
133+
ContextPtr GetContextPtrSignalSafe(v8::Isolate* isolate);
108134

109135
public:
110136
/**
@@ -113,21 +139,26 @@ class WallProfiler : public Nan::ObjectWrap {
113139
* parameter is informative; it is up to the caller to call the Stop method
114140
* every period. The parameter is used to preallocate data structures that
115141
* should not be reallocated in async signal safe code.
142+
* @param useCPED whether to use the V8 ContinuationPreservingEmbedderData
143+
* to store the current sampling context. It can be used if AsyncLocalStorage
144+
* uses the AsyncContextFrame implementation (experimental in Node 23, default
145+
* in Node 24.)
116146
*/
117147
explicit WallProfiler(std::chrono::microseconds samplingPeriod,
118148
std::chrono::microseconds duration,
119149
bool includeLines,
120150
bool withContexts,
121151
bool workaroundV8bug,
122152
bool collectCpuTime,
123-
bool isMainThread);
153+
bool isMainThread,
154+
bool useCPED);
124155

125156
v8::Local<v8::Value> GetContext(v8::Isolate*);
126157
void SetContext(v8::Isolate*, v8::Local<v8::Value>);
127158
void PushContext(int64_t time_from,
128159
int64_t time_to,
129160
int64_t cpu_time,
130-
int64_t async_id);
161+
v8::Isolate* isolate);
131162
Result StartImpl();
132163
std::string StartInternal();
133164
Result StopImpl(bool restart,

ts/src/time-profiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function start(options: TimeProfilerOptions = {}) {
9191
throw new Error('Wall profiler is already started');
9292
}
9393

94-
gProfiler = new TimeProfiler({...options, isMainThread});
94+
gProfiler = new TimeProfiler({...options, isMainThread, useCPED: false});
9595
gSourceMapper = options.sourceMapper;
9696
gIntervalMicros = options.intervalMicros!;
9797
gV8ProfilerStuckEventLoopDetected = 0;

0 commit comments

Comments
 (0)