Skip to content
Open
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
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
'src/timers.cc',
'src/timer_wrap.cc',
'src/tracing/agent.cc',
'src/tracing/agent_legacy.cc',
'src/tracing/node_trace_buffer.cc',
'src/tracing/node_trace_writer.cc',
'src/tracing/trace_event.cc',
Expand Down Expand Up @@ -338,6 +339,7 @@
'src/tcp_wrap.h',
'src/timers.h',
'src/tracing/agent.h',
'src/tracing/agent_legacy.h',
'src/tracing/node_trace_buffer.h',
'src/tracing/node_trace_writer.h',
'src/tracing/trace_event.h',
Expand Down
12 changes: 4 additions & 8 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ using v8::SnapshotCreator;
using v8::StackTrace;
using v8::String;
using v8::Symbol;
using v8::TracingController;
using v8::TryCatch;
using v8::Uint32;
using v8::Undefined;
Expand Down Expand Up @@ -894,10 +893,9 @@ Environment::Environment(IsolateData* isolate_data,
inspector_agent_ = std::make_unique<inspector::Agent>(this);
#endif

if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter()) {
if (tracing::Agent* agent = tracing::Agent::GetInstance()) {
trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this);
if (TracingController* tracing_controller = writer->GetTracingController())
tracing_controller->AddTraceStateObserver(trace_state_observer_.get());
agent->AddTraceStateObserver(trace_state_observer_.get());
}

destroy_async_id_list_.reserve(512);
Expand Down Expand Up @@ -1064,10 +1062,8 @@ Environment::~Environment() {
principal_realm_.reset();

if (trace_state_observer_) {
tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
CHECK_NOT_NULL(writer);
if (TracingController* tracing_controller = writer->GetTracingController())
tracing_controller->RemoveTraceStateObserver(trace_state_observer_.get());
if (tracing::Agent* agent = tracing::Agent::GetInstance())
agent->RemoveTraceStateObserver(trace_state_observer_.get());
}

TRACE_EVENT_NESTABLE_ASYNC_END0(
Expand Down
14 changes: 8 additions & 6 deletions src/inspector/tracing_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "main_thread_interface.h"
#include "node_internals.h"
#include "node_v8_platform-inl.h"
#include "tracing/agent_legacy.h"
#include "v8.h"

#include <set>
Expand Down Expand Up @@ -162,13 +163,14 @@ DispatchResponse TracingAgent::start(
return DispatchResponse::InvalidRequest(
"At least one category should be enabled");

tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
if (writer != nullptr) {
auto* agent =
static_cast<tracing::LegacyTracingAgent*>(tracing::Agent::GetInstance());
if (agent != nullptr) {
trace_writer_ =
writer->agent()->AddClient(categories_set,
std::make_unique<InspectorTraceWriter>(
frontend_object_id_, main_thread_),
tracing::Agent::kIgnoreDefaultCategories);
agent->AddClient(categories_set,
std::make_unique<InspectorTraceWriter>(
frontend_object_id_, main_thread_),
tracing::LegacyTracingAgent::kIgnoreDefaultCategories);
}
return DispatchResponse::Success();
}
Expand Down
14 changes: 9 additions & 5 deletions src/node_trace_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
if (!*val) return;
categories.emplace(*val);
}
CHECK_NOT_NULL(GetTracingAgentWriter());
CHECK_NOT_NULL(tracing::Agent::GetInstance());
new NodeCategorySet(env, args.This(), std::move(categories));
}

Expand All @@ -85,8 +85,10 @@ void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
if (!category_set->enabled_ && !categories.empty()) {
// Starts the Tracing Agent if it wasn't started already (e.g. through
// a command line flag.)
StartTracingAgent();
GetTracingAgentWriter()->Enable(categories);
auto* agent = tracing::Agent::GetInstance();
agent->StartTracing(per_process::cli_options->trace_event_categories);
tracing::AgentWriterHandle* writer = agent->GetDefaultWriterHandle();
writer->Enable(categories);
category_set->enabled_ = true;
}
}
Expand All @@ -97,15 +99,17 @@ void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
CHECK_NOT_NULL(category_set);
const auto& categories = category_set->GetCategories();
if (category_set->enabled_ && !categories.empty()) {
GetTracingAgentWriter()->Disable(categories);
auto* agent = tracing::Agent::GetInstance();
tracing::AgentWriterHandle* writer = agent->GetDefaultWriterHandle();
writer->Disable(categories);
category_set->enabled_ = false;
}
}

void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::string categories =
GetTracingAgentWriter()->agent()->GetEnabledCategories();
tracing::Agent::GetInstance()->GetEnabledCategories();
Local<Value> ret;
if (!categories.empty() &&
ToV8Value(env->context(), categories, env->isolate()).ToLocal(&ret)) {
Expand Down
113 changes: 12 additions & 101 deletions src/node_v8_platform-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,30 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <memory>
#include <string_view>

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

namespace node {

// Ensures that __metadata trace events are only emitted
// when tracing is enabled.
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");

tracing::ProcessMeta trace_process;
TRACE_EVENT_METADATA1("__metadata",
"node",
"process",
tracing::CastTracedValue(trace_process));
// This only runs the first time tracing is enabled
controller_->RemoveTraceStateObserver(this);
}

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:
v8::TracingController* controller_;
};

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();
tracing_agent_ = tracing::Agent::CreateDefault();
// 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);
platform_ = new NodePlatform(thread_pool_size,
tracing_agent_->GetTracingController());
v8::V8::InitializePlatform(platform_);
}
// Make sure V8Platform don not call into Libuv threadpool,
Expand All @@ -90,58 +36,33 @@ struct V8Platform {
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);
}

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

inline void StartTracingAgent() {
constexpr auto convert_to_set =
[](auto& categories) -> std::set<std::string> {
std::set<std::string> out;
for (const auto& s : categories) {
out.emplace(std::string(s.data(), s.size()));
}
return out;
};
// Attach a new NodeTraceWriter only if this function hasn't been called
// before.
if (tracing_file_writer_.IsDefaultHandle()) {
using std::operator""sv;
auto categories = std::views::split(
per_process::cli_options->trace_event_categories, ","sv);

tracing_file_writer_ = tracing_agent_->AddClient(
convert_to_set(categories),
std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter(
per_process::cli_options->trace_event_file_pattern)),
tracing::Agent::kUseDefaultCategories);
}
if (!initialized_) return;
tracing_agent_->StartTracing(
per_process::cli_options->trace_event_categories);
}

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

inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
return &tracing_file_writer_;
inline void StopTracingAgent() {
if (!initialized_) return;
tracing_agent_->StopTracing();
}

inline NodePlatform* Platform() { return platform_; }

std::unique_ptr<NodeTraceStateObserver> trace_state_observer_;
std::unique_ptr<tracing::Agent> tracing_agent_;
tracing::AgentWriterHandle tracing_file_writer_;
std::unique_ptr<tracing::Agent, tracing::Agent::Deleter> tracing_agent_;
NodePlatform* platform_;
#else // !NODE_USE_V8_PLATFORM
inline void Initialize(int thread_pool_size) {}
Expand All @@ -156,8 +77,6 @@ struct V8Platform {
}
inline void StopTracingAgent() {}

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

inline NodePlatform* Platform() { return nullptr; }
#endif // !NODE_USE_V8_PLATFORM
};
Expand All @@ -166,14 +85,6 @@ namespace per_process {
extern struct V8Platform v8_platform;
}

inline void StartTracingAgent() {
return per_process::v8_platform.StartTracingAgent();
}

inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
return per_process::v8_platform.GetTracingAgentWriter();
}

inline void DisposePlatform() {
per_process::v8_platform.Dispose();
}
Expand Down
Loading
Loading