Skip to content

Commit 2f8f767

Browse files
committed
src: allow non-Node.js TracingControllers
We do not need a Node.js-provided `v8::TracingController`, generally. Loosen that restriction in order to make it easier for embedders to provide their own subclass of `v8::TracingController`, or none at all. Refs: electron/electron@9c36576 Backport-PR-URL: #35241 PR-URL: #30467 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 9b84ee6 commit 2f8f767

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

src/api/environment.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env) {
485485
MultiIsolatePlatform* CreatePlatform(
486486
int thread_pool_size,
487487
node::tracing::TracingController* tracing_controller) {
488+
return CreatePlatform(
489+
thread_pool_size,
490+
static_cast<v8::TracingController*>(tracing_controller));
491+
}
492+
493+
MultiIsolatePlatform* CreatePlatform(
494+
int thread_pool_size,
495+
v8::TracingController* tracing_controller) {
488496
return MultiIsolatePlatform::Create(thread_pool_size, tracing_controller)
489497
.release();
490498
}
@@ -495,7 +503,7 @@ void FreePlatform(MultiIsolatePlatform* platform) {
495503

496504
std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create(
497505
int thread_pool_size,
498-
node::tracing::TracingController* tracing_controller) {
506+
v8::TracingController* tracing_controller) {
499507
return std::make_unique<NodePlatform>(thread_pool_size, tracing_controller);
500508
}
501509

src/node.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class NODE_EXTERN MultiIsolatePlatform : public v8::Platform {
311311

312312
static std::unique_ptr<MultiIsolatePlatform> Create(
313313
int thread_pool_size,
314-
node::tracing::TracingController* tracing_controller = nullptr);
314+
v8::TracingController* tracing_controller = nullptr);
315315
};
316316

317317
enum IsolateSettingsFlags {
@@ -466,9 +466,13 @@ NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env);
466466
NODE_EXTERN MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env);
467467

468468
// Legacy variants of MultiIsolatePlatform::Create().
469+
// TODO(addaleax): Deprecate in favour of the v8::TracingController variant.
469470
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
470471
int thread_pool_size,
471472
node::tracing::TracingController* tracing_controller);
473+
NODE_EXTERN MultiIsolatePlatform* CreatePlatform(
474+
int thread_pool_size,
475+
v8::TracingController* tracing_controller);
472476
NODE_EXTERN void FreePlatform(MultiIsolatePlatform* platform);
473477

474478
NODE_EXTERN void EmitBeforeExit(Environment* env);

src/node_platform.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ using v8::Isolate;
1313
using v8::Object;
1414
using v8::Platform;
1515
using v8::Task;
16-
using node::tracing::TracingController;
1716

1817
namespace {
1918

@@ -320,12 +319,16 @@ void PerIsolatePlatformData::DecreaseHandleCount() {
320319
}
321320

322321
NodePlatform::NodePlatform(int thread_pool_size,
323-
TracingController* tracing_controller) {
324-
if (tracing_controller) {
322+
v8::TracingController* tracing_controller) {
323+
if (tracing_controller != nullptr) {
325324
tracing_controller_ = tracing_controller;
326325
} else {
327-
tracing_controller_ = new TracingController();
326+
tracing_controller_ = new v8::TracingController();
328327
}
328+
// TODO(addaleax): It's a bit icky that we use global state here, but we can't
329+
// really do anything about it unless V8 starts exposing a way to access the
330+
// current v8::Platform instance.
331+
tracing::TraceEventHelper::SetTracingController(tracing_controller_);
329332
worker_thread_task_runner_ =
330333
std::make_shared<WorkerThreadsTaskRunner>(thread_pool_size);
331334
}
@@ -490,7 +493,7 @@ double NodePlatform::CurrentClockTimeMillis() {
490493
return SystemClockTimeMillis();
491494
}
492495

493-
TracingController* NodePlatform::GetTracingController() {
496+
v8::TracingController* NodePlatform::GetTracingController() {
494497
CHECK_NOT_NULL(tracing_controller_);
495498
return tracing_controller_;
496499
}

src/node_platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class WorkerThreadsTaskRunner {
137137
class NodePlatform : public MultiIsolatePlatform {
138138
public:
139139
NodePlatform(int thread_pool_size,
140-
node::tracing::TracingController* tracing_controller);
140+
v8::TracingController* tracing_controller);
141141
~NodePlatform() override = default;
142142

143143
void DrainTasks(v8::Isolate* isolate) override;
@@ -159,7 +159,7 @@ class NodePlatform : public MultiIsolatePlatform {
159159
bool IdleTasksEnabled(v8::Isolate* isolate) override;
160160
double MonotonicallyIncreasingTime() override;
161161
double CurrentClockTimeMillis() override;
162-
node::tracing::TracingController* GetTracingController() override;
162+
v8::TracingController* GetTracingController() override;
163163
bool FlushForegroundTasks(v8::Isolate* isolate) override;
164164

165165
void RegisterIsolate(v8::Isolate* isolate, uv_loop_t* loop) override;
@@ -179,7 +179,7 @@ class NodePlatform : public MultiIsolatePlatform {
179179
std::unordered_map<v8::Isolate*,
180180
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;
181181

182-
node::tracing::TracingController* tracing_controller_;
182+
v8::TracingController* tracing_controller_;
183183
std::shared_ptr<WorkerThreadsTaskRunner> worker_thread_task_runner_;
184184
};
185185

src/tracing/agent.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ void TracingController::AddMetadataEvent(
242242
TRACE_EVENT_FLAG_NONE,
243243
CurrentTimestampMicroseconds(),
244244
CurrentCpuTimestampMicroseconds());
245-
node::tracing::TraceEventHelper::GetAgent()->AddMetadataEvent(
246-
std::move(trace_event));
245+
Agent* node_agent = node::tracing::TraceEventHelper::GetAgent();
246+
if (node_agent != nullptr)
247+
node_agent->AddMetadataEvent(std::move(trace_event));
247248
}
248249

249250
} // namespace tracing

src/tracing/trace_event.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ namespace node {
44
namespace tracing {
55

66
Agent* g_agent = nullptr;
7+
v8::TracingController* g_controller = nullptr;
78

89
void TraceEventHelper::SetAgent(Agent* agent) {
910
g_agent = agent;
11+
g_controller = agent->GetTracingController();
1012
}
1113

1214
Agent* TraceEventHelper::GetAgent() {
1315
return g_agent;
1416
}
1517

16-
TracingController* TraceEventHelper::GetTracingController() {
17-
return g_agent->GetTracingController();
18+
v8::TracingController* TraceEventHelper::GetTracingController() {
19+
return g_controller;
20+
}
21+
22+
void TraceEventHelper::SetTracingController(v8::TracingController* controller) {
23+
g_controller = controller;
1824
}
1925

2026
} // namespace tracing

src/tracing/trace_event.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,9 @@ const uint64_t kNoId = 0;
314314
// Refs: https://github.com/nodejs/node/pull/28724
315315
class NODE_EXTERN TraceEventHelper {
316316
public:
317-
static TracingController* GetTracingController();
317+
static v8::TracingController* GetTracingController();
318+
static void SetTracingController(v8::TracingController* controller);
319+
318320
static Agent* GetAgent();
319321
static void SetAgent(Agent* agent);
320322

@@ -514,9 +516,10 @@ static V8_INLINE void AddMetadataEventImpl(
514516
arg_convertibles[1].reset(reinterpret_cast<v8::ConvertableToTraceFormat*>(
515517
static_cast<intptr_t>(arg_values[1])));
516518
}
517-
node::tracing::TracingController* controller =
518-
node::tracing::TraceEventHelper::GetTracingController();
519-
return controller->AddMetadataEvent(
519+
node::tracing::Agent* agent =
520+
node::tracing::TraceEventHelper::GetAgent();
521+
if (agent == nullptr) return;
522+
return agent->GetTracingController()->AddMetadataEvent(
520523
category_group_enabled, name, num_args, arg_names, arg_types, arg_values,
521524
arg_convertibles, flags);
522525
}

0 commit comments

Comments
 (0)