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

src: slim down node_v8_platform-inl.h #46926

Closed
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
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
'src/node_url.cc',
'src/node_util.cc',
'src/node_v8.cc',
'src/node_v8_platform.cc',
'src/node_wasi.cc',
'src/node_wasm_web_api.cc',
'src/node_watchdog.cc',
Expand Down
119 changes: 18 additions & 101 deletions src/node_v8_platform-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,17 @@ namespace node {
class NodeTraceStateObserver
: public v8::TracingController::TraceStateObserver {
public:
inline void OnTraceEnabled() override {
std::string title = GetProcessTitle("");
if (!title.empty()) {
// Only emit the metadata event if the title can be retrieved
// successfully. Ignore it otherwise.
TRACE_EVENT_METADATA1(
"__metadata", "process_name", "name", TRACE_STR_COPY(title.c_str()));
}
TRACE_EVENT_METADATA1("__metadata",
"version",
"node",
per_process::metadata.versions.node.c_str());
TRACE_EVENT_METADATA1(
"__metadata", "thread_name", "name", "JavaScriptMainThread");

auto trace_process = tracing::TracedValue::Create();
trace_process->BeginDictionary("versions");

#define V(key) \
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());

NODE_VERSIONS_KEYS(V)
#undef V

trace_process->EndDictionary();

trace_process->SetString("arch", per_process::metadata.arch.c_str());
trace_process->SetString("platform",
per_process::metadata.platform.c_str());

trace_process->BeginDictionary("release");
trace_process->SetString("name",
per_process::metadata.release.name.c_str());
#if NODE_VERSION_IS_LTS
trace_process->SetString("lts", per_process::metadata.release.lts.c_str());
#endif
trace_process->EndDictionary();
TRACE_EVENT_METADATA1(
"__metadata", "node", "process", std::move(trace_process));

// This only runs the first time tracing is enabled
controller_->RemoveTraceStateObserver(this);
}
void OnTraceEnabled() override;

inline void OnTraceDisabled() override {
// Do nothing here. This should never be called because the
// observer removes itself when OnTraceEnabled() is called.
UNREACHABLE();
}
};

explicit NodeTraceStateObserver(v8::TracingController* controller)
: controller_(controller) {}

~NodeTraceStateObserver() override = default;

private:
Expand All @@ -84,71 +43,25 @@ struct V8Platform {
bool initialized_ = false;

#if NODE_USE_V8_PLATFORM
inline void Initialize(int thread_pool_size) {
CHECK(!initialized_);
initialized_ = true;
tracing_agent_ = std::make_unique<tracing::Agent>();
node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get());
node::tracing::TracingController* controller =
tracing_agent_->GetTracingController();
trace_state_observer_ =
std::make_unique<NodeTraceStateObserver>(controller);
controller->AddTraceStateObserver(trace_state_observer_.get());
tracing_file_writer_ = tracing_agent_->DefaultHandle();
// Only start the tracing agent if we enabled any tracing categories.
if (!per_process::cli_options->trace_event_categories.empty()) {
StartTracingAgent();
}
// Tracing must be initialized before platform threads are created.
platform_ = new NodePlatform(thread_pool_size, controller);
v8::V8::InitializePlatform(platform_);
}
// Make sure V8Platform don not call into Libuv threadpool,
// see DefaultProcessExitHandlerInternal in environment.cc
inline void Dispose() {
if (!initialized_)
return;
initialized_ = false;
node::tracing::TraceEventHelper::SetAgent(nullptr);
StopTracingAgent();
platform_->Shutdown();
delete platform_;
platform_ = nullptr;
// Destroy tracing after the platform (and platform threads) have been
// stopped.
tracing_agent_.reset(nullptr);
// The observer remove itself in OnTraceEnabled
trace_state_observer_.reset(nullptr);
}
void Initialize(int thread_pool_size);
void Dispose();
void StartTracingAgent();

inline void DrainVMTasks(v8::Isolate* isolate) {
platform_->DrainTasks(isolate);
}

inline void StartTracingAgent() {
// Attach a new NodeTraceWriter only if this function hasn't been called
// before.
if (tracing_file_writer_.IsDefaultHandle()) {
std::vector<std::string> categories =
SplitString(per_process::cli_options->trace_event_categories, ',');

tracing_file_writer_ = tracing_agent_->AddClient(
std::set<std::string>(std::make_move_iterator(categories.begin()),
std::make_move_iterator(categories.end())),
std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)),
tracing::Agent::kUseDefaultCategories);
}
inline void StopTracingAgent() {
tracing_file_writer_.reset();
}

inline void StopTracingAgent() { tracing_file_writer_.reset(); }

inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
return &tracing_file_writer_;
}

inline NodePlatform* Platform() { return platform_; }
inline NodePlatform* Platform() {
return platform_;
}

std::unique_ptr<NodeTraceStateObserver> trace_state_observer_;
std::unique_ptr<tracing::Agent> tracing_agent_;
Expand All @@ -165,11 +78,15 @@ struct V8Platform {
"so event tracing is not available.\n");
}
}
inline void StopTracingAgent() {}
inline NodePlatform* Platform() {
return nullptr;
}

inline tracing::AgentWriterHandle* GetTracingAgentWriter() { return nullptr; }
inline void StopTracingAgent() {}

inline NodePlatform* Platform() { return nullptr; }
inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
return nullptr;
}
#endif // !NODE_USE_V8_PLATFORM
};

Expand Down
112 changes: 112 additions & 0 deletions src/node_v8_platform.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "node_v8_platform-inl.h"

#include <memory>

#include "env-inl.h"
#include "node.h"
#include "node_metadata.h"
#include "node_options.h"
#include "node_platform.h"
#include "tracing/node_trace_writer.h"
#include "tracing/trace_event.h"
#include "tracing/traced_value.h"
#include "util.h"

namespace node {

void NodeTraceStateObserver::OnTraceEnabled() {
std::string title = GetProcessTitle("");
if (!title.empty()) {
// Only emit the metadata event if the title can be retrieved
// successfully. Ignore it otherwise.
TRACE_EVENT_METADATA1(
"__metadata", "process_name", "name", TRACE_STR_COPY(title.c_str()));
}
TRACE_EVENT_METADATA1("__metadata",
"version",
"node",
per_process::metadata.versions.node.c_str());
TRACE_EVENT_METADATA1(
"__metadata", "thread_name", "name", "JavaScriptMainThread");

auto trace_process = tracing::TracedValue::Create();
trace_process->BeginDictionary("versions");

#define V(key) \
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());

NODE_VERSIONS_KEYS(V)
#undef V

trace_process->EndDictionary();

trace_process->SetString("arch", per_process::metadata.arch.c_str());
trace_process->SetString("platform", per_process::metadata.platform.c_str());

trace_process->BeginDictionary("release");
trace_process->SetString("name", per_process::metadata.release.name.c_str());
#if NODE_VERSION_IS_LTS
trace_process->SetString("lts", per_process::metadata.release.lts.c_str());
#endif
trace_process->EndDictionary();
TRACE_EVENT_METADATA1(
"__metadata", "node", "process", std::move(trace_process));

// This only runs the first time tracing is enabled
controller_->RemoveTraceStateObserver(this);
}

#if NODE_USE_V8_PLATFORM
void V8Platform::Initialize(int thread_pool_size) {
CHECK(!initialized_);
initialized_ = true;
tracing_agent_ = std::make_unique<tracing::Agent>();
node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get());
node::tracing::TracingController* controller =
tracing_agent_->GetTracingController();
trace_state_observer_ = std::make_unique<NodeTraceStateObserver>(controller);
controller->AddTraceStateObserver(trace_state_observer_.get());
tracing_file_writer_ = tracing_agent_->DefaultHandle();
// Only start the tracing agent if we enabled any tracing categories.
if (!per_process::cli_options->trace_event_categories.empty()) {
StartTracingAgent();
}
// Tracing must be initialized before platform threads are created.
platform_ = new NodePlatform(thread_pool_size, controller);
v8::V8::InitializePlatform(platform_);
}
// Make sure V8Platform don not call into Libuv threadpool,
// see DefaultProcessExitHandlerInternal in environment.cc
void V8Platform::Dispose() {
if (!initialized_) return;
initialized_ = false;
node::tracing::TraceEventHelper::SetAgent(nullptr);
StopTracingAgent();
platform_->Shutdown();
delete platform_;
platform_ = nullptr;
// Destroy tracing after the platform (and platform threads) have been
// stopped.
tracing_agent_.reset(nullptr);
// The observer remove itself in OnTraceEnabled
trace_state_observer_.reset(nullptr);
}

void V8Platform::StartTracingAgent() {
// Attach a new NodeTraceWriter only if this function hasn't been called
// before.
if (tracing_file_writer_.IsDefaultHandle()) {
std::vector<std::string> categories =
SplitString(per_process::cli_options->trace_event_categories, ',');

tracing_file_writer_ = tracing_agent_->AddClient(
std::set<std::string>(std::make_move_iterator(categories.begin()),
std::make_move_iterator(categories.end())),
std::unique_ptr<tracing::AsyncTraceWriter>(new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)),
tracing::Agent::kUseDefaultCategories);
}
}
#endif // !NODE_USE_V8_PLATFORM

} // namespace node