cli: support `${pid}` placeholder in --cpu-prof-name · nodejs/node@e592d73 · GitHub
Skip to content

Commit e592d73

Browse files
haramjaduh95
authored andcommitted
cli: support ${pid} placeholder in --cpu-prof-name
PR-URL: #59072 Fixes: #57418 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent cba0de1 commit e592d73

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

doc/api/cli.md

Lines changed: 6 additions & 3 deletions

src/inspector_profiler.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "node_file.h"
99
#include "node_internals.h"
1010
#include "util-inl.h"
11+
#include "uv.h"
1112
#include "v8-inspector.h"
1213

1314
#include <cinttypes>
@@ -465,6 +466,27 @@ static void EndStartedProfilers(Environment* env) {
465466
}
466467
}
467468

469+
static std::string ReplacePlaceholders(const std::string& pattern) {
470+
std::string result = pattern;
471+
472+
static const std::unordered_map<std::string, std::function<std::string()>>
473+
kPlaceholderMap = {
474+
{"${pid}", []() { return std::to_string(uv_os_getpid()); }},
475+
// TODO(haramj): Add more placeholders as needed.
476+
};
477+
478+
for (const auto& [placeholder, getter] : kPlaceholderMap) {
479+
size_t pos = 0;
480+
while ((pos = result.find(placeholder, pos)) != std::string::npos) {
481+
const std::string value = getter();
482+
result.replace(pos, placeholder.length(), value);
483+
pos += value.length();
484+
}
485+
}
486+
487+
return result;
488+
}
489+
468490
void StartProfilers(Environment* env) {
469491
AtExit(env, [](void* env) {
470492
EndStartedProfilers(static_cast<Environment*>(env));
@@ -486,7 +508,9 @@ void StartProfilers(Environment* env) {
486508
DiagnosticFilename filename(env, "CPU", "cpuprofile");
487509
env->set_cpu_prof_name(*filename);
488510
} else {
489-
env->set_cpu_prof_name(env->options()->cpu_prof_name);
511+
std::string resolved_name =
512+
ReplacePlaceholders(env->options()->cpu_prof_name);
513+
env->set_cpu_prof_name(resolved_name);
490514
}
491515
CHECK_NULL(env->cpu_profiler_connection());
492516
env->set_cpu_profiler_connection(

test/sequential/test-cpu-prof-name.js

Lines changed: 35 additions & 0 deletions

0 commit comments

Comments
 (0)