Skip to content

Commit bd40cc6

Browse files
Matt LoringMylesBorins
Matt Loring
authored andcommitted
tracing: Update to use new Platform tracing apis
V8 modified the platform API to accept a tracing controller at platform creation time that is required to be present for the lifetime of the platform if tracing will every be enabled. This will simplify the implementation of a v8::Platform subclass for node. PR-URL: #14001 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 55eed60 commit bd40cc6

File tree

5 files changed

+34
-36
lines changed

5 files changed

+34
-36
lines changed

src/node.cc

+8-7
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,15 @@ node::DebugOptions debug_options;
251251
static struct {
252252
#if NODE_USE_V8_PLATFORM
253253
void Initialize(int thread_pool_size) {
254+
tracing_agent_ =
255+
trace_enabled ? new tracing::Agent() : nullptr;
254256
platform_ = v8::platform::CreateDefaultPlatform(
255-
thread_pool_size,
256-
v8::platform::IdleTaskSupport::kDisabled,
257-
v8::platform::InProcessStackDumping::kDisabled);
257+
thread_pool_size, v8::platform::IdleTaskSupport::kDisabled,
258+
v8::platform::InProcessStackDumping::kDisabled,
259+
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
258260
V8::InitializePlatform(platform_);
259-
tracing::TraceEventHelper::SetCurrentPlatform(platform_);
261+
tracing::TraceEventHelper::SetTracingController(
262+
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
260263
}
261264

262265
void PumpMessageLoop(Isolate* isolate) {
@@ -283,9 +286,7 @@ static struct {
283286
#endif // HAVE_INSPECTOR
284287

285288
void StartTracingAgent() {
286-
CHECK(tracing_agent_ == nullptr);
287-
tracing_agent_ = new tracing::Agent();
288-
tracing_agent_->Start(platform_, trace_enabled_categories);
289+
tracing_agent_->Start(trace_enabled_categories);
289290
}
290291

291292
void StopTracingAgent() {

src/tracing/agent.cc

+8-12
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,18 @@ namespace tracing {
1212
using v8::platform::tracing::TraceConfig;
1313
using std::string;
1414

15-
Agent::Agent() {}
16-
17-
void Agent::Start(v8::Platform* platform, const string& enabled_categories) {
18-
platform_ = platform;
19-
15+
Agent::Agent() {
2016
int err = uv_loop_init(&tracing_loop_);
2117
CHECK_EQ(err, 0);
2218

2319
NodeTraceWriter* trace_writer = new NodeTraceWriter(&tracing_loop_);
2420
TraceBuffer* trace_buffer = new NodeTraceBuffer(
2521
NodeTraceBuffer::kBufferChunks, trace_writer, &tracing_loop_);
26-
2722
tracing_controller_ = new TracingController();
23+
tracing_controller_->Initialize(trace_buffer);
24+
}
2825

26+
void Agent::Start(const string& enabled_categories) {
2927
TraceConfig* trace_config = new TraceConfig();
3028
if (!enabled_categories.empty()) {
3129
std::stringstream category_list(enabled_categories);
@@ -42,27 +40,25 @@ void Agent::Start(v8::Platform* platform, const string& enabled_categories) {
4240
// This thread should be created *after* async handles are created
4341
// (within NodeTraceWriter and NodeTraceBuffer constructors).
4442
// Otherwise the thread could shut down prematurely.
45-
err = uv_thread_create(&thread_, ThreadCb, this);
43+
int err = uv_thread_create(&thread_, ThreadCb, this);
4644
CHECK_EQ(err, 0);
4745

48-
tracing_controller_->Initialize(trace_buffer);
4946
tracing_controller_->StartTracing(trace_config);
50-
v8::platform::SetTracingController(platform, tracing_controller_);
47+
started_ = true;
5148
}
5249

5350
void Agent::Stop() {
54-
if (!IsStarted()) {
51+
if (!started_) {
5552
return;
5653
}
5754
// Perform final Flush on TraceBuffer. We don't want the tracing controller
5855
// to flush the buffer again on destruction of the V8::Platform.
5956
tracing_controller_->StopTracing();
6057
tracing_controller_->Initialize(nullptr);
61-
tracing_controller_ = nullptr;
58+
started_ = false;
6259

6360
// Thread should finish when the tracing loop is stopped.
6461
uv_thread_join(&thread_);
65-
v8::platform::SetTracingController(platform_, nullptr);
6662
}
6763

6864
// static

src/tracing/agent.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ namespace tracing {
1212
class Agent {
1313
public:
1414
Agent();
15-
void Start(v8::Platform* platform, const std::string& enabled_categories);
15+
void Start(const std::string& enabled_categories);
1616
void Stop();
1717

18+
TracingController* GetTracingController() { return tracing_controller_; }
19+
1820
private:
19-
bool IsStarted() { return platform_ != nullptr; }
2021
static void ThreadCb(void* arg);
2122

2223
uv_thread_t thread_;
2324
uv_loop_t tracing_loop_;
24-
v8::Platform* platform_ = nullptr;
25+
bool started_ = false;
2526
TracingController* tracing_controller_ = nullptr;
2627
};
2728

src/tracing/trace_event.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace node {
44
namespace tracing {
55

6-
v8::Platform* platform_ = nullptr;
6+
v8::TracingController* controller_ = nullptr;
77

8-
void TraceEventHelper::SetCurrentPlatform(v8::Platform* platform) {
9-
platform_ = platform;
8+
void TraceEventHelper::SetTracingController(v8::TracingController* controller) {
9+
controller_ = controller;
1010
}
1111

12-
v8::Platform* TraceEventHelper::GetCurrentPlatform() {
13-
return platform_;
12+
v8::TracingController* TraceEventHelper::GetTracingController() {
13+
return controller_;
1414
}
1515

1616
} // namespace tracing

src/tracing/trace_event.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ enum CategoryGroupEnabledFlags {
7070
// const uint8_t*
7171
// TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
7272
#define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
73-
node::tracing::TraceEventHelper::GetCurrentPlatform() \
73+
node::tracing::TraceEventHelper::GetTracingController() \
7474
->GetCategoryGroupEnabled
7575

7676
// Get the number of times traces have been recorded. This is used to implement
@@ -99,7 +99,7 @@ enum CategoryGroupEnabledFlags {
9999
// const char* name,
100100
// uint64_t id)
101101
#define TRACE_EVENT_API_UPDATE_TRACE_EVENT_DURATION \
102-
node::tracing::TraceEventHelper::GetCurrentPlatform() \
102+
node::tracing::TraceEventHelper::GetTracingController() \
103103
->UpdateTraceEventDuration
104104

105105
// Defines atomic operations used internally by the tracing system.
@@ -258,8 +258,8 @@ extern intptr_t kRuntimeCallStatsTracingEnabled;
258258

259259
class TraceEventHelper {
260260
public:
261-
static v8::Platform* GetCurrentPlatform();
262-
static void SetCurrentPlatform(v8::Platform* platform);
261+
static v8::TracingController* GetTracingController();
262+
static void SetTracingController(v8::TracingController* controller);
263263
};
264264

265265
// TraceID encapsulates an ID that can either be an integer or pointer. Pointers
@@ -406,11 +406,11 @@ static inline uint64_t AddTraceEventImpl(
406406
static_cast<intptr_t>(arg_values[1])));
407407
}
408408
// DCHECK(num_args <= 2);
409-
v8::Platform* platform =
410-
node::tracing::TraceEventHelper::GetCurrentPlatform();
411-
return platform->AddTraceEvent(phase, category_group_enabled, name, scope, id,
412-
bind_id, num_args, arg_names, arg_types,
413-
arg_values, arg_convertibles, flags);
409+
v8::TracingController* controller =
410+
node::tracing::TraceEventHelper::GetTracingController();
411+
return controller->AddTraceEvent(phase, category_group_enabled, name, scope, id,
412+
bind_id, num_args, arg_names, arg_types,
413+
arg_values, arg_convertibles, flags);
414414
}
415415

416416
// Define SetTraceValue for each allowed type. It stores the type and

0 commit comments

Comments
 (0)