From c4f0e81dd0e185ccc2b3aff705f800fdd0cd0258 Mon Sep 17 00:00:00 2001 From: Ali Ijaz Sheikh Date: Wed, 16 May 2018 15:22:56 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/20823 Reviewed-By: Matteo Collina --- src/node.cc | 10 ++++------ src/node_platform.cc | 6 ++++-- test/cctest/node_test_fixture.h | 6 +++--- test/parallel/test-trace-events-metadata.js | 3 +++ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/node.cc b/src/node.cc index 1edb87bf84f..3f0fe28e98b 100644 --- a/src/node.cc +++ b/src/node.cc @@ -288,11 +288,11 @@ static struct { #if NODE_USE_V8_PLATFORM void Initialize(int thread_pool_size) { tracing_agent_.reset(new tracing::Agent(trace_file_pattern)); - platform_ = new NodePlatform(thread_pool_size, - tracing_agent_->GetTracingController()); + auto controller = tracing_agent_->GetTracingController(); + tracing::TraceEventHelper::SetTracingController(controller); + StartTracingAgent(); + platform_ = new NodePlatform(thread_pool_size, controller); V8::InitializePlatform(platform_); - tracing::TraceEventHelper::SetTracingController( - tracing_agent_->GetTracingController()); } void Dispose() { @@ -4420,8 +4420,6 @@ int Start(int argc, char** argv) { #endif // HAVE_OPENSSL v8_platform.Initialize(v8_thread_pool_size); - // Enable tracing when argv has --trace-events-enabled. - v8_platform.StartTracingAgent(); V8::Initialize(); performance::performance_v8_start = PERFORMANCE_NOW(); v8_initialized = true; diff --git a/src/node_platform.cc b/src/node_platform.cc index b8f1344727c..2885c72ed71 100644 --- a/src/node_platform.cc +++ b/src/node_platform.cc @@ -16,8 +16,10 @@ using v8::Platform; using v8::Task; using v8::TracingController; -static void BackgroundRunner(void* data) { - TaskQueue* background_tasks = static_cast*>(data); +static void BackgroundRunner(void *data) { + TRACE_EVENT_METADATA1("__metadata", "thread_name", "name", + "BackgroundTaskRunner"); + TaskQueue *background_tasks = static_cast *>(data); while (std::unique_ptr task = background_tasks->BlockingPop()) { task->Run(); background_tasks->NotifyOfCompletion(); diff --git a/test/cctest/node_test_fixture.h b/test/cctest/node_test_fixture.h index e0740a47096..f43cb56cd3a 100644 --- a/test/cctest/node_test_fixture.h +++ b/test/cctest/node_test_fixture.h @@ -64,12 +64,12 @@ class NodeTestFixture : public ::testing::Test { v8::Isolate* isolate_; static void SetUpTestCase() { - platform.reset(new node::NodePlatform(4, nullptr)); tracing_controller.reset(new v8::TracingController()); - allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); - params.array_buffer_allocator = allocator.get(); node::tracing::TraceEventHelper::SetTracingController( tracing_controller.get()); + platform.reset(new node::NodePlatform(4, nullptr)); + allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator()); + params.array_buffer_allocator = allocator.get(); CHECK_EQ(0, uv_loop_init(¤t_loop)); v8::V8::InitializePlatform(platform.get()); v8::V8::Initialize(); diff --git a/test/parallel/test-trace-events-metadata.js b/test/parallel/test-trace-events-metadata.js index f8fcdcfe5bb..eccec1ecf0f 100644 --- a/test/parallel/test-trace-events-metadata.js +++ b/test/parallel/test-trace-events-metadata.js @@ -22,5 +22,8 @@ proc.once('exit', common.mustCall(() => { assert(traces.some((trace) => trace.cat === '__metadata' && trace.name === 'thread_name' && trace.args.name === 'JavaScriptMainThread')); + assert(traces.some((trace) => + trace.cat === '__metadata' && trace.name === 'thread_name' && + trace.args.name === 'BackgroundTaskRunner')); })); }));