Skip to content

Commit 85212bb

Browse files
AndreasMadsenapapirovski
authored andcommitted
trace_events: add file pattern cli option
Allow the user to specify the filepath for the trace_events log file using a template string. PR-URL: #18480 Reviewed-By: Ali Ijaz Sheikh <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 523d44a commit 85212bb

File tree

10 files changed

+98
-10
lines changed

10 files changed

+98
-10
lines changed

doc/api/cli.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,14 @@ added: v7.7.0
235235
A comma separated list of categories that should be traced when trace event
236236
tracing is enabled using `--trace-events-enabled`.
237237

238+
### `--trace-event-file-pattern`
239+
<!-- YAML
240+
added: REPLACEME
241+
-->
242+
243+
Template string specifying the filepath for the trace event data, it
244+
supports `${rotation}` and `${pid}`.
245+
238246
### `--zero-fill-buffers`
239247
<!-- YAML
240248
added: v6.0.0
@@ -464,6 +472,7 @@ Node options that are allowed are:
464472
- `--trace-deprecation`
465473
- `--trace-events-categories`
466474
- `--trace-events-enabled`
475+
- `--trace-event-file-pattern`
467476
- `--trace-sync-io`
468477
- `--trace-warnings`
469478
- `--track-heap-objects`

doc/api/tracing.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ Running Node.js with tracing enabled will produce log files that can be opened
3333
in the [`chrome://tracing`](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
3434
tab of Chrome.
3535

36+
The logging file is by default called `node_trace.${rotation}.log`, where
37+
`${rotation}` is an incrementing log-rotation id. The filepath pattern can
38+
be specified with `--trace-event-file-pattern` that accepts a template
39+
string that supports `${rotation}` and `${pid}`. For example:
40+
41+
```txt
42+
node --trace-events-enabled --trace-event-file-pattern '${pid}-${rotation}.log' server.js
43+
```
44+
3645
Starting with Node 10.0.0, the tracing system uses the same time source as the
3746
one used by `process.hrtime()` however the trace-event timestamps are expressed
3847
in microseconds, unlike `process.hrtime()` which returns nanoseconds.

doc/node.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ Enable the collection of trace event tracing information.
156156
A comma-separated list of categories that should be traced when trace event tracing is enabled using
157157
.Fl -trace-events-enabled .
158158
.
159+
.It Fl -trace-event-file-pattern Ar pattern
160+
Template string specifying the filepath for the trace event data, it
161+
supports \fB${rotation}\fR and \fB${pid}\fR.
162+
.
159163
.It Fl -zero-fill-buffers
160164
Automatically zero-fills all newly allocated Buffer and SlowBuffer instances.
161165
.

src/node.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ static node_module* modlist_linked;
192192
static node_module* modlist_addon;
193193
static bool trace_enabled = false;
194194
static std::string trace_enabled_categories; // NOLINT(runtime/string)
195+
static std::string trace_file_pattern = // NOLINT(runtime/string)
196+
"node_trace.${rotation}.log";
195197
static bool abort_on_uncaught_exception = false;
196198

197199
// Bit flag used to track security reverts (see node_revert.h)
@@ -275,7 +277,7 @@ static struct {
275277
#if NODE_USE_V8_PLATFORM
276278
void Initialize(int thread_pool_size) {
277279
if (trace_enabled) {
278-
tracing_agent_.reset(new tracing::Agent());
280+
tracing_agent_.reset(new tracing::Agent(trace_file_pattern));
279281
platform_ = new NodePlatform(thread_pool_size,
280282
tracing_agent_->GetTracingController());
281283
V8::InitializePlatform(platform_);
@@ -3422,6 +3424,10 @@ static void PrintHelp() {
34223424
" --trace-events-enabled track trace events\n"
34233425
" --trace-event-categories comma separated list of trace event\n"
34243426
" categories to record\n"
3427+
" --trace-event-file-pattern Template string specifying the\n"
3428+
" filepath for the trace-events data, it\n"
3429+
" supports ${rotation} and ${pid}\n"
3430+
" log-rotation id. %%2$u is the pid.\n"
34253431
" --track-heap-objects track heap object allocations for heap "
34263432
"snapshots\n"
34273433
" --prof-process process v8 profiler output generated\n"
@@ -3550,6 +3556,7 @@ static void CheckIfAllowedInEnv(const char* exe, bool is_env,
35503556
"--no-force-async-hooks-checks",
35513557
"--trace-events-enabled",
35523558
"--trace-event-categories",
3559+
"--trace-event-file-pattern",
35533560
"--track-heap-objects",
35543561
"--zero-fill-buffers",
35553562
"--v8-pool-size",
@@ -3701,6 +3708,14 @@ static void ParseArgs(int* argc,
37013708
}
37023709
args_consumed += 1;
37033710
trace_enabled_categories = categories;
3711+
} else if (strcmp(arg, "--trace-event-file-pattern") == 0) {
3712+
const char* file_pattern = argv[index + 1];
3713+
if (file_pattern == nullptr) {
3714+
fprintf(stderr, "%s: %s requires an argument\n", argv[0], arg);
3715+
exit(9);
3716+
}
3717+
args_consumed += 1;
3718+
trace_file_pattern = file_pattern;
37043719
} else if (strcmp(arg, "--track-heap-objects") == 0) {
37053720
track_heap_objects = true;
37063721
} else if (strcmp(arg, "--throw-deprecation") == 0) {

src/tracing/agent.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ namespace tracing {
1313
using v8::platform::tracing::TraceConfig;
1414
using std::string;
1515

16-
Agent::Agent() {
16+
Agent::Agent(const std::string& log_file_pattern) {
1717
int err = uv_loop_init(&tracing_loop_);
1818
CHECK_EQ(err, 0);
1919

20-
NodeTraceWriter* trace_writer = new NodeTraceWriter(&tracing_loop_);
20+
NodeTraceWriter* trace_writer = new NodeTraceWriter(
21+
log_file_pattern, &tracing_loop_);
2122
TraceBuffer* trace_buffer = new NodeTraceBuffer(
2223
NodeTraceBuffer::kBufferChunks, trace_writer, &tracing_loop_);
2324
tracing_controller_ = new TracingController();

src/tracing/agent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TracingController : public v8::platform::tracing::TracingController {
1919

2020
class Agent {
2121
public:
22-
Agent();
22+
explicit Agent(const std::string& log_file_pattern);
2323
void Start(const std::string& enabled_categories);
2424
void Stop();
2525

src/tracing/node_trace_writer.cc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
namespace node {
99
namespace tracing {
1010

11-
NodeTraceWriter::NodeTraceWriter(uv_loop_t* tracing_loop)
12-
: tracing_loop_(tracing_loop) {
11+
NodeTraceWriter::NodeTraceWriter(const std::string& log_file_pattern,
12+
uv_loop_t* tracing_loop)
13+
: tracing_loop_(tracing_loop), log_file_pattern_(log_file_pattern) {
1314
flush_signal_.data = this;
1415
int err = uv_async_init(tracing_loop_, &flush_signal_, FlushSignalCb);
1516
CHECK_EQ(err, 0);
@@ -54,12 +55,27 @@ NodeTraceWriter::~NodeTraceWriter() {
5455
}
5556
}
5657

58+
void replace_substring(std::string* target,
59+
const std::string& search,
60+
const std::string& insert) {
61+
size_t pos = target->find(search);
62+
for (; pos != std::string::npos; pos = target->find(search, pos)) {
63+
target->replace(pos, search.size(), insert);
64+
pos += insert.size();
65+
}
66+
}
67+
5768
void NodeTraceWriter::OpenNewFileForStreaming() {
5869
++file_num_;
5970
uv_fs_t req;
60-
std::ostringstream log_file;
61-
log_file << "node_trace." << file_num_ << ".log";
62-
fd_ = uv_fs_open(tracing_loop_, &req, log_file.str().c_str(),
71+
72+
// Evaluate a JS-style template string, it accepts the values ${pid} and
73+
// ${rotation}
74+
std::string filepath(log_file_pattern_);
75+
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
76+
replace_substring(&filepath, "${rotation}", std::to_string(file_num_));
77+
78+
fd_ = uv_fs_open(tracing_loop_, &req, filepath.c_str(),
6379
O_CREAT | O_WRONLY | O_TRUNC, 0644, nullptr);
6480
CHECK_NE(fd_, -1);
6581
uv_fs_req_cleanup(&req);

src/tracing/node_trace_writer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ using v8::platform::tracing::TraceWriter;
1616

1717
class NodeTraceWriter : public TraceWriter {
1818
public:
19-
explicit NodeTraceWriter(uv_loop_t* tracing_loop);
19+
explicit NodeTraceWriter(const std::string& log_file_pattern,
20+
uv_loop_t* tracing_loop);
2021
~NodeTraceWriter();
2122

2223
void AppendTraceEvent(TraceObject* trace_event) override;
@@ -62,6 +63,7 @@ class NodeTraceWriter : public TraceWriter {
6263
int highest_request_id_completed_ = 0;
6364
int total_traces_ = 0;
6465
int file_num_ = 0;
66+
const std::string& log_file_pattern_;
6567
std::ostringstream stream_;
6668
TraceWriter* json_trace_writer_ = nullptr;
6769
bool exited_ = false;

test/parallel/test-cli-node-options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ expect('--throw-deprecation', 'B\n');
2525
expect('--zero-fill-buffers', 'B\n');
2626
expect('--v8-pool-size=10', 'B\n');
2727
expect('--trace-event-categories node', 'B\n');
28+
// eslint-disable-next-line no-template-curly-in-string
29+
expect('--trace-event-file-pattern {pid}-${rotation}.trace_events', 'B\n');
2830

2931
if (!common.isWindows) {
3032
expect('--perf-basic-prof', 'B\n');
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const assert = require('assert');
5+
const cp = require('child_process');
6+
const fs = require('fs');
7+
8+
tmpdir.refresh();
9+
process.chdir(tmpdir.path);
10+
11+
const CODE =
12+
'setTimeout(() => { for (var i = 0; i < 100000; i++) { "test" + i } }, 1)';
13+
14+
const proc = cp.spawn(process.execPath, [
15+
'--trace-events-enabled',
16+
'--trace-event-file-pattern',
17+
// eslint-disable-next-line no-template-curly-in-string
18+
'${pid}-${rotation}-${pid}-${rotation}.tracing.log',
19+
'-e', CODE
20+
]);
21+
22+
proc.once('exit', common.mustCall(() => {
23+
const expectedFilename = `${proc.pid}-1-${proc.pid}-1.tracing.log`;
24+
25+
assert(common.fileExists(expectedFilename));
26+
fs.readFile(expectedFilename, common.mustCall((err, data) => {
27+
const traces = JSON.parse(data.toString()).traceEvents;
28+
assert(traces.length > 0);
29+
}));
30+
}));

0 commit comments

Comments
 (0)