Skip to content

Commit 360d3d7

Browse files
legendecasaduh95
authored andcommitted
src: fix perfetto build on GetTraceFilePath
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net> PR-URL: #64721 Fixes: nodejs/diagnostics#654 Refs: #64565 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 4c781bb commit 360d3d7

6 files changed

Lines changed: 37 additions & 39 deletions

File tree

src/node_trace_events.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "node_v8_platform-inl.h"
88
#include "permission/permission.h"
99
#include "tracing/agent.h"
10-
#include "tracing/node_trace_writer.h"
10+
#include "tracing/trace_event_helper.h"
1111
#include "util-inl.h"
1212

1313
#include <set>
@@ -90,7 +90,7 @@ void NodeCategorySet::Enable(const FunctionCallbackInfo<Value>& args) {
9090
THROW_IF_INSUFFICIENT_PERMISSIONS(
9191
category_set->env(),
9292
permission::PermissionScope::kFileSystemWrite,
93-
tracing::NodeTraceWriter::GetFilePath(
93+
tracing::GetTraceFilePath(
9494
per_process::cli_options->trace_event_file_pattern, 1));
9595
auto* agent = tracing::Agent::GetInstance();
9696
agent->StartTracing(per_process::cli_options->trace_event_categories);

src/tracing/agent_perfetto.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "env-inl.h"
77
#include "node_options.h"
88
#include "trace_event.h"
9+
#include "trace_event_helper.h"
910

1011
#include "trace_event_perfetto.h"
1112

@@ -21,16 +22,6 @@ constexpr uint64_t kReadPeriodMs = 5000;
2122
// trace file grows without bound.
2223
constexpr uint64_t kMaxFileSizeBytes = 64 * 1024 * 1024; // 64 MiB
2324

24-
void replace_substring(std::string* target,
25-
std::string_view search,
26-
std::string_view insert) {
27-
size_t pos = target->find(search);
28-
for (; pos != std::string::npos; pos = target->find(search, pos)) {
29-
target->replace(pos, search.size(), insert);
30-
pos += insert.size();
31-
}
32-
}
33-
3425
std::set<std::string> flatten(
3526
const std::unordered_map<int, std::multiset<std::string>>& map) {
3627
std::set<std::string> result;
@@ -104,9 +95,7 @@ class SimpleWriter : public TraceWriter {
10495
++file_num_;
10596
uv_fs_t req;
10697

107-
std::string filepath(log_file_pattern_);
108-
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
109-
replace_substring(&filepath, "${rotation}", std::to_string(file_num_));
98+
std::string filepath = GetTraceFilePath(log_file_pattern_, file_num_);
11099

111100
if (fd_ >= 0) {
112101
uv_fs_close(loop_, &req, fd_, nullptr);

src/tracing/node_trace_writer.cc

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "tracing/node_trace_writer.h"
2+
#include "tracing/trace_event_helper.h"
23

34
#include "util-inl.h"
45

@@ -8,27 +9,9 @@
89
namespace node {
910
namespace tracing {
1011

11-
void replace_substring(std::string* target,
12-
const std::string& search,
13-
const std::string& insert) {
14-
size_t pos = target->find(search);
15-
for (; pos != std::string::npos; pos = target->find(search, pos)) {
16-
target->replace(pos, search.size(), insert);
17-
pos += insert.size();
18-
}
19-
}
20-
2112
NodeTraceWriter::NodeTraceWriter(const std::string& log_file_pattern)
2213
: log_file_pattern_(log_file_pattern) {}
2314

24-
std::string NodeTraceWriter::GetFilePath(const std::string& log_file_pattern,
25-
int file_num) {
26-
std::string filepath(log_file_pattern);
27-
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
28-
replace_substring(&filepath, "${rotation}", std::to_string(file_num));
29-
return filepath;
30-
}
31-
3215
void NodeTraceWriter::InitializeOnThread(uv_loop_t* loop) {
3316
CHECK_NULL(tracing_loop_);
3417
tracing_loop_ = loop;
@@ -82,9 +65,7 @@ void NodeTraceWriter::OpenNewFileForStreaming() {
8265
++file_num_;
8366
uv_fs_t req;
8467

85-
// Evaluate a JS-style template string, it accepts the values ${pid} and
86-
// ${rotation}
87-
std::string filepath(GetFilePath(log_file_pattern_, file_num_));
68+
std::string filepath = GetTraceFilePath(log_file_pattern_, file_num_);
8869

8970
if (fd_ != -1) {
9071
CHECK_EQ(uv_fs_close(nullptr, &req, fd_, nullptr), 0);

src/tracing/node_trace_writer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ class NodeTraceWriter : public AsyncTraceWriter {
2121
explicit NodeTraceWriter(const std::string& log_file_pattern);
2222
~NodeTraceWriter() override;
2323

24-
static std::string GetFilePath(const std::string& log_file_pattern,
25-
int file_num);
26-
2724
void InitializeOnThread(uv_loop_t* loop) override;
2825
void AppendTraceEvent(TraceObject* trace_event) override;
2926
void Flush(bool blocking) override;

src/tracing/trace_event_helper.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include "tracing/trace_event_helper.h"
22
#include "node.h"
3+
#include "uv.h"
4+
5+
#include <string>
6+
#include <string_view>
37

48
namespace node {
59
namespace tracing {
@@ -14,6 +18,26 @@ void TraceEventHelper::SetTracingController(v8::TracingController* controller) {
1418
g_controller = controller;
1519
}
1620

21+
namespace {
22+
void replace_substring(std::string* target,
23+
std::string_view search,
24+
std::string_view insert) {
25+
size_t pos = target->find(search);
26+
for (; pos != std::string::npos; pos = target->find(search, pos)) {
27+
target->replace(pos, search.size(), insert);
28+
pos += insert.size();
29+
}
30+
}
31+
} // namespace
32+
33+
std::string GetTraceFilePath(std::string_view log_file_pattern, int file_num) {
34+
// Evaluate a JS-style template string that accepts ${pid} and ${rotation}.
35+
std::string filepath(log_file_pattern);
36+
replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid()));
37+
replace_substring(&filepath, "${rotation}", std::to_string(file_num));
38+
return filepath;
39+
}
40+
1741
} // namespace tracing
1842

1943
v8::TracingController* GetTracingController() {

src/tracing/trace_event_helper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55

66
#include "v8-platform.h"
77

8+
#include <string>
9+
#include <string_view>
10+
811
namespace node::tracing {
912

13+
// Expands a trace log file pattern into a concrete path, substituting ${pid}
14+
// and ${rotation}.
15+
std::string GetTraceFilePath(std::string_view log_file_pattern, int file_num);
16+
1017
class TraceEventHelper {
1118
public:
1219
static v8::TracingController* GetTracingController();

0 commit comments

Comments
 (0)