Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.x backport] backport 23233, 22938 #23398

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,18 @@ static struct {
controller->AddTraceStateObserver(new NodeTraceStateObserver(controller));
tracing::TraceEventHelper::SetTracingController(controller);
StartTracingAgent();
// Tracing must be initialized before platform threads are created.
platform_ = new NodePlatform(thread_pool_size, controller);
V8::InitializePlatform(platform_);
}

void Dispose() {
tracing_agent_.reset(nullptr);
platform_->Shutdown();
delete platform_;
platform_ = nullptr;
// Destroy tracing after the platform (and platform threads) have been
// stopped.
tracing_agent_.reset(nullptr);
}

void DrainVMTasks(Isolate* isolate) {
Expand Down
43 changes: 37 additions & 6 deletions src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,28 @@ using v8::Platform;
using v8::Task;
using v8::TracingController;

struct PlatformWorkerData {
TaskQueue<Task>* task_queue;
Mutex* platform_workers_mutex;
ConditionVariable* platform_workers_ready;
int* pending_platform_workers;
int id;
};

static void BackgroundRunner(void* data) {
std::unique_ptr<PlatformWorkerData>
worker_data(static_cast<PlatformWorkerData*>(data));
TRACE_EVENT_METADATA1("__metadata", "thread_name", "name",
"BackgroundTaskRunner");
TaskQueue<Task> *background_tasks = static_cast<TaskQueue<Task> *>(data);

// Notify the main thread that the platform worker is ready.
{
Mutex::ScopedLock lock(*worker_data->platform_workers_mutex);
(*worker_data->pending_platform_workers)--;
worker_data->platform_workers_ready->Signal(lock);
}

TaskQueue<Task>* background_tasks = worker_data->task_queue;
while (std::unique_ptr<Task> task = background_tasks->BlockingPop()) {
task->Run();
background_tasks->NotifyOfCompletion();
Expand Down Expand Up @@ -144,15 +162,29 @@ class BackgroundTaskRunner::DelayedTaskScheduler {
};

BackgroundTaskRunner::BackgroundTaskRunner(int thread_pool_size) {
Mutex::ScopedLock lock(platform_workers_mutex_);
pending_platform_workers_ = thread_pool_size;

delayed_task_scheduler_.reset(
new DelayedTaskScheduler(&background_tasks_));
threads_.push_back(delayed_task_scheduler_->Start());

for (int i = 0; i < thread_pool_size; i++) {
PlatformWorkerData* worker_data = new PlatformWorkerData{
&background_tasks_, &platform_workers_mutex_,
&platform_workers_ready_, &pending_platform_workers_, i
};
std::unique_ptr<uv_thread_t> t { new uv_thread_t() };
if (uv_thread_create(t.get(), BackgroundRunner, &background_tasks_) != 0)
if (uv_thread_create(t.get(), BackgroundRunner, worker_data) != 0)
break;
threads_.push_back(std::move(t));
}

// Wait for platform workers to initialize before continuing with the
// bootstrap.
while (pending_platform_workers_ > 0) {
platform_workers_ready_.Wait(lock);
}
}

void BackgroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
Expand Down Expand Up @@ -248,10 +280,9 @@ int PerIsolatePlatformData::unref() {
NodePlatform::NodePlatform(int thread_pool_size,
TracingController* tracing_controller) {
if (tracing_controller) {
tracing_controller_.reset(tracing_controller);
tracing_controller_ = tracing_controller;
} else {
TracingController* controller = new TracingController();
tracing_controller_.reset(controller);
tracing_controller_ = new TracingController();
}
background_task_runner_ =
std::make_shared<BackgroundTaskRunner>(thread_pool_size);
Expand Down Expand Up @@ -425,7 +456,7 @@ double NodePlatform::CurrentClockTimeMillis() {
}

TracingController* NodePlatform::GetTracingController() {
return tracing_controller_.get();
return tracing_controller_;
}

template <class T>
Expand Down
8 changes: 7 additions & 1 deletion src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,18 @@ class BackgroundTaskRunner : public v8::TaskRunner {
void Shutdown();

size_t NumberOfAvailableBackgroundThreads() const;

private:
TaskQueue<v8::Task> background_tasks_;

class DelayedTaskScheduler;
std::unique_ptr<DelayedTaskScheduler> delayed_task_scheduler_;

std::vector<std::unique_ptr<uv_thread_t>> threads_;

Mutex platform_workers_mutex_;
ConditionVariable platform_workers_ready_;
int pending_platform_workers_;
};

class NodePlatform : public MultiIsolatePlatform {
Expand Down Expand Up @@ -156,7 +161,8 @@ class NodePlatform : public MultiIsolatePlatform {
std::unordered_map<v8::Isolate*,
std::shared_ptr<PerIsolatePlatformData>> per_isolate_;

std::unique_ptr<v8::TracingController> tracing_controller_;

v8::TracingController* tracing_controller_;
std::shared_ptr<BackgroundTaskRunner> background_task_runner_;
};

Expand Down
11 changes: 5 additions & 6 deletions src/tracing/agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ using v8::platform::tracing::TraceConfig;
using v8::platform::tracing::TraceWriter;
using std::string;

Agent::Agent() {
tracing_controller_ = new TracingController();
Agent::Agent() : tracing_controller_(new TracingController()) {
tracing_controller_->Initialize(nullptr);

CHECK_EQ(uv_loop_init(&tracing_loop_), 0);
Expand Down Expand Up @@ -117,7 +116,7 @@ AgentWriterHandle Agent::AddClient(
use_categories = &categories_with_default;
}

ScopedSuspendTracing suspend(tracing_controller_, this);
ScopedSuspendTracing suspend(tracing_controller_.get(), this);
int id = next_writer_id_++;
AsyncTraceWriter* raw = writer.get();
writers_[id] = std::move(writer);
Expand Down Expand Up @@ -157,7 +156,7 @@ void Agent::Disconnect(int client) {
Mutex::ScopedLock lock(initialize_writer_mutex_);
to_be_initialized_.erase(writers_[client].get());
}
ScopedSuspendTracing suspend(tracing_controller_, this);
ScopedSuspendTracing suspend(tracing_controller_.get(), this);
writers_.erase(client);
categories_.erase(client);
}
Expand All @@ -166,13 +165,13 @@ void Agent::Enable(int id, const std::set<std::string>& categories) {
if (categories.empty())
return;

ScopedSuspendTracing suspend(tracing_controller_, this,
ScopedSuspendTracing suspend(tracing_controller_.get(), this,
id != kDefaultHandleId);
categories_[id].insert(categories.begin(), categories.end());
}

void Agent::Disable(int id, const std::set<std::string>& categories) {
ScopedSuspendTracing suspend(tracing_controller_, this,
ScopedSuspendTracing suspend(tracing_controller_.get(), this,
id != kDefaultHandleId);
std::multiset<std::string>& writer_categories = categories_[id];
for (const std::string& category : categories) {
Expand Down
6 changes: 4 additions & 2 deletions src/tracing/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class Agent {
Agent();
~Agent();

TracingController* GetTracingController() { return tracing_controller_; }
TracingController* GetTracingController() {
return tracing_controller_.get();
}

enum UseDefaultCategoryMode {
kUseDefaultCategories,
Expand Down Expand Up @@ -119,7 +121,7 @@ class Agent {
// These maps store the original arguments to AddClient(), by id.
std::unordered_map<int, std::multiset<std::string>> categories_;
std::unordered_map<int, std::unique_ptr<AsyncTraceWriter>> writers_;
TracingController* tracing_controller_ = nullptr;
std::unique_ptr<TracingController> tracing_controller_;

// Variables related to initializing per-event-loop properties of individual
// writers, such as libuv handles.
Expand Down