src: abstract tracing agent for both legacy and perfetto · nodejs/node@dbb3126 · GitHub
Skip to content

Commit dbb3126

Browse files
legendecasrichardlau
authored andcommitted
src: abstract tracing agent for both legacy and perfetto
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net> PR-URL: #64053 Refs: nodejs/diagnostics#654 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fe5260c commit dbb3126

18 files changed

Lines changed: 603 additions & 526 deletions

node.gyp

Lines changed: 2 additions & 0 deletions

src/env.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ using v8::SnapshotCreator;
7171
using v8::StackTrace;
7272
using v8::String;
7373
using v8::Symbol;
74-
using v8::TracingController;
7574
using v8::TryCatch;
7675
using v8::Uint32;
7776
using v8::Undefined;
@@ -894,10 +893,9 @@ Environment::Environment(IsolateData* isolate_data,
894893
inspector_agent_ = std::make_unique<inspector::Agent>(this);
895894
#endif
896895

897-
if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter()) {
896+
if (tracing::Agent* agent = tracing::Agent::GetInstance()) {
898897
trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this);
899-
if (TracingController* tracing_controller = writer->GetTracingController())
900-
tracing_controller->AddTraceStateObserver(trace_state_observer_.get());
898+
agent->AddTraceStateObserver(trace_state_observer_.get());
901899
}
902900

903901
destroy_async_id_list_.reserve(512);
@@ -1064,10 +1062,8 @@ Environment::~Environment() {
10641062
principal_realm_.reset();
10651063

10661064
if (trace_state_observer_) {
1067-
tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
1068-
CHECK_NOT_NULL(writer);
1069-
if (TracingController* tracing_controller = writer->GetTracingController())
1070-
tracing_controller->RemoveTraceStateObserver(trace_state_observer_.get());
1065+
if (tracing::Agent* agent = tracing::Agent::GetInstance())
1066+
agent->RemoveTraceStateObserver(trace_state_observer_.get());
10711067
}
10721068

10731069
TRACE_EVENT_NESTABLE_ASYNC_END0(

src/inspector/tracing_agent.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "main_thread_interface.h"
44
#include "node_internals.h"
55
#include "node_v8_platform-inl.h"
6+
#include "tracing/agent_legacy.h"
67
#include "v8.h"
78

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

165-
tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
166-
if (writer != nullptr) {
166+
auto* agent =
167+
static_cast<tracing::LegacyTracingAgent*>(tracing::Agent::GetInstance());
168+
if (agent != nullptr) {
167169
trace_writer_ =
168-
writer->agent()->AddClient(categories_set,
169-
std::make_unique<InspectorTraceWriter>(
170-
frontend_object_id_, main_thread_),
171-
tracing::Agent::kIgnoreDefaultCategories);
170+
agent->AddClient(categories_set,
171+
std::make_unique<InspectorTraceWriter>(
172+
frontend_object_id_, main_thread_),
173+
tracing::LegacyTracingAgent::kIgnoreDefaultCategories);
172174
}
173175
return DispatchResponse::Success();
174176
}

src/node_trace_events.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void NodeCategorySet::New(const FunctionCallbackInfo<Value>& args) {
7373
if (!*val) return;
7474
categories.emplace(*val);
7575
}
76-
CHECK_NOT_NULL(GetTracingAgentWriter());
76+
CHECK_NOT_NULL(tracing::Agent::GetInstance());
7777
new NodeCategorySet(env, args.This(), std::move(categories));
7878
}
7979

@@ -85,8 +85,10 @@ void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
8585
if (!category_set->enabled_ && !categories.empty()) {
8686
// Starts the Tracing Agent if it wasn't started already (e.g. through
8787
// a command line flag.)
88-
StartTracingAgent();
89-
GetTracingAgentWriter()->Enable(categories);
88+
auto* agent = tracing::Agent::GetInstance();
89+
agent->StartTracing(per_process::cli_options->trace_event_categories);
90+
tracing::AgentWriterHandle* writer = agent->GetDefaultWriterHandle();
91+
writer->Enable(categories);
9092
category_set->enabled_ = true;
9193
}
9294
}
@@ -97,15 +99,17 @@ void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
9799
CHECK_NOT_NULL(category_set);
98100
const auto& categories = category_set->GetCategories();
99101
if (category_set->enabled_ && !categories.empty()) {
100-
GetTracingAgentWriter()->Disable(categories);
102+
auto* agent = tracing::Agent::GetInstance();
103+
tracing::AgentWriterHandle* writer = agent->GetDefaultWriterHandle();
104+
writer->Disable(categories);
101105
category_set->enabled_ = false;
102106
}
103107
}
104108

105109
void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
106110
Environment* env = Environment::GetCurrent(args);
107111
std::string categories =
108-
GetTracingAgentWriter()->agent()->GetEnabledCategories();
112+
tracing::Agent::GetInstance()->GetEnabledCategories();
109113
Local<Value> ret;
110114
if (!categories.empty() &&
111115
ToV8Value(env->context(), categories, env->isolate()).ToLocal(&ret)) {

src/node_v8_platform-inl.h

Lines changed: 12 additions & 101 deletions

0 commit comments

Comments
 (0)