Skip to content

Commit 32873c5

Browse files
committed
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 9c2e67b commit 32873c5

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
@@ -289,11 +289,11 @@ static struct {
289289
#if NODE_USE_V8_PLATFORM
290290
void Initialize(int thread_pool_size) {
291291
tracing_agent_.reset(new tracing::Agent(trace_file_pattern));
292-
platform_ = new NodePlatform(thread_pool_size,
293-
tracing_agent_->GetTracingController());
292+
auto controller = tracing_agent_->GetTracingController();
293+
tracing::TraceEventHelper::SetTracingController(controller);
294+
StartTracingAgent();
295+
platform_ = new NodePlatform(thread_pool_size, controller);
294296
V8::InitializePlatform(platform_);
295-
tracing::TraceEventHelper::SetTracingController(
296-
tracing_agent_->GetTracingController());
297297
}
298298

299299
void Dispose() {
@@ -4406,8 +4406,6 @@ int Start(int argc, char** argv) {
44064406
#endif // HAVE_OPENSSL
44074407

44084408
v8_platform.Initialize(v8_thread_pool_size);
4409-
// Enable tracing when argv has --trace-events-enabled.
4410-
v8_platform.StartTracingAgent();
44114409
V8::Initialize();
44124410
performance::performance_v8_start = PERFORMANCE_NOW();
44134411
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)