Skip to content

Commit 1766b8c

Browse files
kjinrvagg
authored andcommitted
trace_events: fix trace events JS API writing
The Trace Events JS API isn't functional if none of --trace-events-enabled or --trace-event-categories is passed as a CLI argument. This commit fixes that. In addition, we currently don't test the trace_events JS API in the casewhere no CLI args are provided. This commit adds that test. Fixes #24944 PR-URL: #24945 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]>
1 parent 143b844 commit 1766b8c

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/node_trace_events.cc

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
7474
CHECK_NOT_NULL(category_set);
7575
const auto& categories = category_set->GetCategories();
7676
if (!category_set->enabled_ && !categories.empty()) {
77+
// Starts the Tracing Agent if it wasn't started already (e.g. through
78+
// a command line flag.)
79+
StartTracingAgent();
7780
GetTracingAgentWriter()->Enable(categories);
7881
category_set->enabled_ = true;
7982
}

src/node_v8_platform-inl.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ struct V8Platform {
8585
tracing_agent_->GetTracingController();
8686
trace_state_observer_.reset(new NodeTraceStateObserver(controller));
8787
controller->AddTraceStateObserver(trace_state_observer_.get());
88-
StartTracingAgent();
88+
tracing_file_writer_ = tracing_agent_->DefaultHandle();
89+
// Only start the tracing agent if we enabled any tracing categories.
90+
if (!per_process::cli_options->trace_event_categories.empty()) {
91+
StartTracingAgent();
92+
}
8993
// Tracing must be initialized before platform threads are created.
9094
platform_ = new NodePlatform(thread_pool_size, controller);
9195
v8::V8::InitializePlatform(platform_);
@@ -111,9 +115,9 @@ struct V8Platform {
111115
}
112116

113117
inline void StartTracingAgent() {
114-
if (per_process::cli_options->trace_event_categories.empty()) {
115-
tracing_file_writer_ = tracing_agent_->DefaultHandle();
116-
} else {
118+
// Attach a new NodeTraceWriter only if this function hasn't been called
119+
// before.
120+
if (tracing_file_writer_.IsDefaultHandle()) {
117121
std::vector<std::string> categories =
118122
SplitString(per_process::cli_options->trace_event_categories, ',');
119123

@@ -163,6 +167,10 @@ namespace per_process {
163167
extern struct V8Platform v8_platform;
164168
}
165169

170+
inline void StartTracingAgent() {
171+
return per_process::v8_platform.StartTracingAgent();
172+
}
173+
166174
inline tracing::AgentWriterHandle* GetTracingAgentWriter() {
167175
return per_process::v8_platform.GetTracingAgentWriter();
168176
}

src/tracing/agent.h

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class AgentWriterHandle {
5959
inline void Enable(const std::set<std::string>& categories);
6060
inline void Disable(const std::set<std::string>& categories);
6161

62+
inline bool IsDefaultHandle();
63+
6264
inline Agent* agent() { return agent_; }
6365

6466
inline v8::TracingController* GetTracingController();
@@ -175,6 +177,10 @@ void AgentWriterHandle::Disable(const std::set<std::string>& categories) {
175177
if (agent_ != nullptr) agent_->Disable(id_, categories);
176178
}
177179

180+
bool AgentWriterHandle::IsDefaultHandle() {
181+
return agent_ != nullptr && id_ == Agent::kDefaultHandleId;
182+
}
183+
178184
inline v8::TracingController* AgentWriterHandle::GetTracingController() {
179185
return agent_ != nullptr ? agent_->GetTracingController() : nullptr;
180186
}

test/parallel/test-trace-events-api.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@ const {
1717
getEnabledCategories
1818
} = require('trace_events');
1919

20+
function getEnabledCategoriesFromCommandLine() {
21+
const indexOfCatFlag = process.execArgv.indexOf('--trace-event-categories');
22+
if (indexOfCatFlag === -1) {
23+
return undefined;
24+
} else {
25+
return process.execArgv[indexOfCatFlag + 1];
26+
}
27+
}
28+
2029
const isChild = process.argv[2] === 'child';
21-
const enabledCategories = isChild ? 'foo' : undefined;
30+
const enabledCategories = getEnabledCategoriesFromCommandLine();
2231

2332
assert.strictEqual(getEnabledCategories(), enabledCategories);
2433
[1, 'foo', true, false, null, undefined].forEach((i) => {
@@ -51,7 +60,9 @@ tracing.enable(); // purposefully enable twice to test calling twice
5160
assert.strictEqual(tracing.enabled, true);
5261

5362
assert.strictEqual(getEnabledCategories(),
54-
isChild ? 'foo,node.perf' : 'node.perf');
63+
[
64+
...[enabledCategories].filter((_) => !!_), 'node.perf'
65+
].join(','));
5566

5667
tracing.disable();
5768
assert.strictEqual(tracing.enabled, false);
@@ -106,7 +117,15 @@ if (isChild) {
106117
}
107118
}
108119

120+
testApiInChildProcess([], () => {
121+
testApiInChildProcess(['--trace-event-categories', 'foo']);
122+
});
123+
}
124+
125+
function testApiInChildProcess(execArgs, cb) {
109126
tmpdir.refresh();
127+
// Save the current directory so we can chdir back to it later
128+
const parentDir = process.cwd();
110129
process.chdir(tmpdir.path);
111130

112131
const expectedMarks = ['A', 'B'];
@@ -121,15 +140,14 @@ if (isChild) {
121140

122141
const proc = cp.fork(__filename,
123142
['child'],
124-
{ execArgv: [ '--expose-gc',
125-
'--trace-event-categories',
126-
'foo' ] });
143+
{ execArgv: [ '--expose-gc', ...execArgs ] });
127144

128145
proc.once('exit', common.mustCall(() => {
129146
const file = path.join(tmpdir.path, 'node_trace.1.log');
130147

131148
assert(fs.existsSync(file));
132149
fs.readFile(file, common.mustCall((err, data) => {
150+
assert.ifError(err);
133151
const traces = JSON.parse(data.toString()).traceEvents
134152
.filter((trace) => trace.cat !== '__metadata');
135153
assert.strictEqual(traces.length,
@@ -160,6 +178,8 @@ if (isChild) {
160178
assert.fail('Unexpected trace event phase');
161179
}
162180
});
181+
process.chdir(parentDir);
182+
cb && process.nextTick(cb);
163183
}));
164184
}));
165185
}

0 commit comments

Comments
 (0)