src: refactor tracing code by addaleax · Pull Request #21867 · nodejs/node · GitHub
Skip to content
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: 2 additions & 3 deletions src/env-inl.h
4 changes: 2 additions & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ void InitThreadLocalOnce() {

Environment::Environment(IsolateData* isolate_data,
Local<Context> context,
tracing::Agent* tracing_agent)
tracing::AgentWriterHandle* tracing_agent_writer)
: isolate_(context->GetIsolate()),
isolate_data_(isolate_data),
tracing_agent_(tracing_agent),
tracing_agent_writer_(tracing_agent_writer),
immediate_info_(context->GetIsolate()),
tick_info_(context->GetIsolate()),
timer_base_(uv_now(isolate_data->event_loop())),
Expand Down
11 changes: 7 additions & 4 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "v8.h"
#include "node.h"
#include "node_http2_state.h"
#include "tracing/agent.h"

#include <list>
#include <stdint.h>
Expand All @@ -55,6 +54,10 @@ namespace performance {
class performance_state;
}

namespace tracing {
class AgentWriterHandle;
}

namespace worker {
class Worker;
}
Expand Down Expand Up @@ -584,7 +587,7 @@ class Environment {

Environment(IsolateData* isolate_data,
v8::Local<v8::Context> context,
tracing::Agent* tracing_agent);
tracing::AgentWriterHandle* tracing_agent_writer);
~Environment();

void Start(int argc,
Expand Down Expand Up @@ -622,7 +625,7 @@ class Environment {
inline bool profiler_idle_notifier_started() const;

inline v8::Isolate* isolate() const;
inline tracing::Agent* tracing_agent() const;
inline tracing::AgentWriterHandle* tracing_agent_writer() const;
inline uv_loop_t* event_loop() const;
inline uint32_t watched_providers() const;

Expand Down Expand Up @@ -876,7 +879,7 @@ class Environment {

v8::Isolate* const isolate_;
IsolateData* const isolate_data_;
tracing::Agent* const tracing_agent_;
tracing::AgentWriterHandle* const tracing_agent_writer_;
uv_timer_t timer_handle_;
uv_check_t immediate_check_handle_;
uv_idle_t immediate_idle_handle_;
Expand Down
11 changes: 5 additions & 6 deletions src/inspector/tracing_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class InspectorTraceWriter : public node::tracing::AsyncTraceWriter {
} // namespace

TracingAgent::TracingAgent(Environment* env)
: env_(env),
trace_writer_(
tracing::Agent::EmptyClientHandle()) {
: env_(env) {
}

TracingAgent::~TracingAgent() {
Expand All @@ -62,7 +60,7 @@ void TracingAgent::Wire(UberDispatcher* dispatcher) {

DispatchResponse TracingAgent::start(
std::unique_ptr<protocol::NodeTracing::TraceConfig> traceConfig) {
if (trace_writer_ != nullptr) {
if (!trace_writer_.empty()) {
return DispatchResponse::Error(
"Call NodeTracing::end to stop tracing before updating the config");
}
Expand All @@ -76,10 +74,11 @@ DispatchResponse TracingAgent::start(
if (categories_set.empty())
return DispatchResponse::Error("At least one category should be enabled");

trace_writer_ = env_->tracing_agent()->AddClient(
trace_writer_ = env_->tracing_agent_writer()->agent()->AddClient(
categories_set,
std::unique_ptr<InspectorTraceWriter>(
new InspectorTraceWriter(frontend_.get())));
new InspectorTraceWriter(frontend_.get())),
tracing::Agent::kIgnoreDefaultCategories);
return DispatchResponse::OK();
}

Expand Down
8 changes: 2 additions & 6 deletions src/inspector/tracing_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
#define SRC_INSPECTOR_TRACING_AGENT_H_

#include "node/inspector/protocol/NodeTracing.h"
#include "tracing/agent.h"
#include "v8.h"


namespace node {
class Environment;

namespace tracing {
class Agent;
} // namespace tracing

namespace inspector {
namespace protocol {

Expand All @@ -32,8 +29,7 @@ class TracingAgent : public NodeTracing::Backend {
void DisconnectTraceClient();

Environment* env_;
std::unique_ptr<std::pair<tracing::Agent*, int>,
void (*)(std::pair<tracing::Agent*, int>*)> trace_writer_;
tracing::AgentWriterHandle trace_writer_;
std::unique_ptr<NodeTracing::Frontend> frontend_;
};

Expand Down
31 changes: 21 additions & 10 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "req_wrap-inl.h"
#include "string_bytes.h"
#include "tracing/agent.h"
#include "tracing/node_trace_writer.h"
#include "util.h"
#include "uv.h"
#if NODE_USE_V8_PLATFORM
Expand Down Expand Up @@ -387,7 +388,7 @@ class NodeTraceStateObserver :
static struct {
#if NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {
tracing_agent_.reset(new tracing::Agent(trace_file_pattern));
tracing_agent_.reset(new tracing::Agent());
auto controller = tracing_agent_->GetTracingController();
controller->AddTraceStateObserver(new NodeTraceStateObserver(controller));
tracing::TraceEventHelper::SetTracingController(controller);
Expand All @@ -397,10 +398,10 @@ static struct {
}

void Dispose() {
tracing_agent_.reset(nullptr);
platform_->Shutdown();
delete platform_;
platform_ = nullptr;
tracing_agent_.reset(nullptr);
}

void DrainVMTasks(Isolate* isolate) {
Expand All @@ -427,23 +428,31 @@ static struct {
#endif // HAVE_INSPECTOR

void StartTracingAgent() {
tracing_agent_->Enable(trace_enabled_categories);
if (trace_enabled_categories.empty()) {
tracing_file_writer_ = tracing_agent_->DefaultHandle();
} else {
tracing_file_writer_ = tracing_agent_->AddClient(
ParseCommaSeparatedSet(trace_enabled_categories),
std::unique_ptr<tracing::AsyncTraceWriter>(
new tracing::NodeTraceWriter(trace_file_pattern)),
tracing::Agent::kUseDefaultCategories);
}
}

void StopTracingAgent() {
if (tracing_agent_)
tracing_agent_->Stop();
tracing_file_writer_.reset();
}

tracing::Agent* GetTracingAgent() const {
return tracing_agent_.get();
tracing::AgentWriterHandle* GetTracingAgentWriter() {
return &tracing_file_writer_;
}

NodePlatform* Platform() {
return platform_;
}

std::unique_ptr<tracing::Agent> tracing_agent_;
tracing::AgentWriterHandle tracing_file_writer_;
NodePlatform* platform_;
#else // !NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {}
Expand All @@ -464,7 +473,9 @@ static struct {
}
void StopTracingAgent() {}

tracing::Agent* GetTracingAgent() const { return nullptr; }
tracing::AgentWriterHandle* GetTracingAgentWriter() {
return nullptr;
}

NodePlatform* Platform() {
return nullptr;
Expand Down Expand Up @@ -3588,7 +3599,7 @@ Environment* CreateEnvironment(IsolateData* isolate_data,
HandleScope handle_scope(isolate);
Context::Scope context_scope(context);
auto env = new Environment(isolate_data, context,
v8_platform.GetTracingAgent());
v8_platform.GetTracingAgentWriter());
env->Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);
return env;
}
Expand Down Expand Up @@ -3647,7 +3658,7 @@ inline int Start(Isolate* isolate, IsolateData* isolate_data,
HandleScope handle_scope(isolate);
Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context);
Environment env(isolate_data, context, v8_platform.GetTracingAgent());
Environment env(isolate_data, context, v8_platform.GetTracingAgentWriter());
env.Start(argc, argv, exec_argc, exec_argv, v8_is_profiling);

const char* path = argc > 1 ? argv[1] : nullptr;
Expand Down
44 changes: 25 additions & 19 deletions src/node_trace_events.cc
Loading