Skip to content

Commit 32c3006

Browse files
ofrobotsMylesBorins
authored andcommitted
src: trace_events: background thread events
V8 uses a thread pool provided by the host to schedule background tasks for concurrent GC and compiation. Emit trace events to identify the background threads. Ensure that the tracing infrastructure is started before the thread pool is initialized. PR-URL: #20823 Reviewed-By: Matteo Collina <[email protected]>
1 parent 014a2d1 commit 32c3006

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

src/node.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,11 @@ static struct {
288288
#if NODE_USE_V8_PLATFORM
289289
void Initialize(int thread_pool_size) {
290290
tracing_agent_.reset(new tracing::Agent(trace_file_pattern));
291-
platform_ = new NodePlatform(thread_pool_size,
292-
tracing_agent_->GetTracingController());
291+
auto controller = tracing_agent_->GetTracingController();
292+
tracing::TraceEventHelper::SetTracingController(controller);
293+
StartTracingAgent();
294+
platform_ = new NodePlatform(thread_pool_size, controller);
293295
V8::InitializePlatform(platform_);
294-
tracing::TraceEventHelper::SetTracingController(
295-
tracing_agent_->GetTracingController());
296296
}
297297

298298
void Dispose() {
@@ -4420,8 +4420,6 @@ int Start(int argc, char** argv) {
44204420
#endif // HAVE_OPENSSL
44214421

44224422
v8_platform.Initialize(v8_thread_pool_size);
4423-
// Enable tracing when argv has --trace-events-enabled.
4424-
v8_platform.StartTracingAgent();
44254423
V8::Initialize();
44264424
performance::performance_v8_start = PERFORMANCE_NOW();
44274425
v8_initialized = true;

src/node_platform.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ using v8::Platform;
1616
using v8::Task;
1717
using v8::TracingController;
1818

19-
static void BackgroundRunner(void* data) {
20-
TaskQueue<Task>* background_tasks = static_cast<TaskQueue<Task>*>(data);
19+
static void BackgroundRunner(void *data) {
20+
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
21+
"BackgroundTaskRunner");
22+
TaskQueue<Task> *background_tasks = static_cast<TaskQueue<Task> *>(data);
2123
while (std::unique_ptr<Task> task = background_tasks->BlockingPop()) {
2224
task->Run();
2325
background_tasks->NotifyOfCompletion();

test/cctest/node_test_fixture.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ class NodeTestFixture : public ::testing::Test {
6464
v8::Isolate* isolate_;
6565

6666
static void SetUpTestCase() {
67-
platform.reset(new node::NodePlatform(4, nullptr));
6867
tracing_controller.reset(new v8::TracingController());
69-
allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
70-
params.array_buffer_allocator = allocator.get();
7168
node::tracing::TraceEventHelper::SetTracingController(
7269
tracing_controller.get());
70+
platform.reset(new node::NodePlatform(4, nullptr));
71+
allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
72+
params.array_buffer_allocator = allocator.get();
7373
CHECK_EQ(0, uv_loop_init(&current_loop));
7474
v8::V8::InitializePlatform(platform.get());
7575
v8::V8::Initialize();

test/parallel/test-trace-events-metadata.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ proc.once('exit', common.mustCall(() => {
2222
assert(traces.some((trace) =>
2323
trace.cat === '__metadata' && trace.name === 'thread_name' &&
2424
trace.args.name === 'JavaScriptMainThread'));
25+
assert(traces.some((trace) =>
26+
trace.cat === '__metadata' && trace.name === 'thread_name' &&
27+
trace.args.name === 'BackgroundTaskRunner'));
2528
}));
2629
}));

0 commit comments

Comments
 (0)