From ead4d7a75aed5357bdab32dc7054774543d39298 Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Tue, 15 Jul 2025 00:00:02 +0900 Subject: [PATCH 1/5] cli: support `${pid}` placeholder in --cpu-prof-name --- doc/api/cli.md | 10 ++++++---- src/inspector_profiler.cc | 13 ++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 74863327c73746..1a2288fb2bfc5e 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -540,14 +540,16 @@ $ ls *.cpuprofile CPU.20190409.202950.15293.0.0.cpuprofile ``` -If `--cpu-prof-name` is specified, the provided value will be used as-is; patterns such as -`${hhmmss}` or `${pid}` are not supported. +If `--cpu-prof-name` is specified, the provided value is used as a template +for the file name. Some placeholders are supported and will be substituted +at runtime: + +- `${pid}` — the current process ID ```console $ node --cpu-prof --cpu-prof-name 'CPU.${pid}.cpuprofile' index.js $ ls *.cpuprofile -'CPU.${pid}.cpuprofile' -``` +CPU.15293.cpuprofile ### `--cpu-prof-dir` diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index f09dd04ccd7f6e..d275b41d958a2f 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -9,6 +9,8 @@ #include "node_internals.h" #include "util-inl.h" #include "v8-inspector.h" +#include +#include #include #include @@ -465,6 +467,14 @@ static void EndStartedProfilers(Environment* env) { } } +std::string ReplacePlaceholders(const std::string& pattern) { + std::string result = pattern; + std::string pid_str = std::to_string(getpid()); + result = std::regex_replace(result, std::regex("\\$\\{pid\\}"), pid_str); + // TODO: Add more placeholders as needed. + return result; +} + void StartProfilers(Environment* env) { AtExit(env, [](void* env) { EndStartedProfilers(static_cast(env)); @@ -486,7 +496,8 @@ void StartProfilers(Environment* env) { DiagnosticFilename filename(env, "CPU", "cpuprofile"); env->set_cpu_prof_name(*filename); } else { - env->set_cpu_prof_name(env->options()->cpu_prof_name); + std::string resolved_name = ReplacePlaceholders(env->options()->cpu_prof_name); + env->set_cpu_prof_name(resolved_name); } CHECK_NULL(env->cpu_profiler_connection()); env->set_cpu_profiler_connection( From 9916afdc8c2386fc7d622894a3c77882ca0d7925 Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Tue, 15 Jul 2025 01:35:06 +0900 Subject: [PATCH 2/5] replaced and includes with "uv.h" for consistency --- doc/api/cli.md | 4 ++-- src/inspector_profiler.cc | 25 ++++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 1a2288fb2bfc5e..10ed548f9501ab 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -544,7 +544,7 @@ If `--cpu-prof-name` is specified, the provided value is used as a template for the file name. Some placeholders are supported and will be substituted at runtime: -- `${pid}` — the current process ID +* `${pid}` — the current process ID ```console $ node --cpu-prof --cpu-prof-name 'CPU.${pid}.cpuprofile' index.js @@ -661,7 +661,7 @@ For example, the following script will not emit ```mjs import sys from 'node:sys'; -``` +```` ```cjs const sys = require('node:sys'); diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index d275b41d958a2f..d629975a36998f 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -1,4 +1,5 @@ #include "inspector_profiler.h" +#include "uv.h" #include "base_object-inl.h" #include "debug_utils-inl.h" #include "diagnosticfilename-inl.h" @@ -9,8 +10,6 @@ #include "node_internals.h" #include "util-inl.h" #include "v8-inspector.h" -#include -#include #include #include @@ -467,11 +466,22 @@ static void EndStartedProfilers(Environment* env) { } } -std::string ReplacePlaceholders(const std::string& pattern) { +static std::string ReplacePlaceholders(const std::string& pattern) { std::string result = pattern; - std::string pid_str = std::to_string(getpid()); - result = std::regex_replace(result, std::regex("\\$\\{pid\\}"), pid_str); - // TODO: Add more placeholders as needed. + + static const std::unordered_map> kPlaceholderMap = { + { "${pid}", []() { return std::to_string(uv_os_getpid()); } }, + // TODO(haramj): Add more placeholders as needed. + }; + + for (const auto& [placeholder, getter] : kPlaceholderMap) { + size_t pos = 0; + while ((pos = result.find(placeholder, pos)) != std::string::npos) { + result.replace(pos, placeholder.length(), getter()); + pos += getter().length(); + } + } + return result; } @@ -496,7 +506,8 @@ void StartProfilers(Environment* env) { DiagnosticFilename filename(env, "CPU", "cpuprofile"); env->set_cpu_prof_name(*filename); } else { - std::string resolved_name = ReplacePlaceholders(env->options()->cpu_prof_name); + std::string resolved_name = + ReplacePlaceholders(env->options()->cpu_prof_name); env->set_cpu_prof_name(resolved_name); } CHECK_NULL(env->cpu_profiler_connection()); From b366703b4ebc52be0e0fbda3162cb3ce96f372c4 Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Tue, 15 Jul 2025 01:39:32 +0900 Subject: [PATCH 3/5] doc: fix broken link reference for --diagnostic-dir option --- doc/api/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 10ed548f9501ab..e5f780721e0092 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3980,7 +3980,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 [`--allow-worker`]: #--allow-worker [`--build-snapshot`]: #--build-snapshot [`--cpu-prof-dir`]: #--cpu-prof-dir -[`--diagnostic-dir`]: #--diagnostic-dirdirectory +[`--diagnostic-dir`]: #--diagnostic-dir [`--disable-sigusr1`]: #--disable-sigusr1 [`--env-file-if-exists`]: #--env-file-if-existsconfig [`--env-file`]: #--env-fileconfig From 323e9535f4ef978208d1dd3647aceae88713c835 Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Tue, 15 Jul 2025 08:52:27 +0900 Subject: [PATCH 4/5] Revert "doc: fix broken link reference for --diagnostic-dir option" This reverts commit 3b529fcc570546ebb1ee8a804f5a434a12dc86b1. --- doc/api/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index e5f780721e0092..10ed548f9501ab 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -3980,7 +3980,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12 [`--allow-worker`]: #--allow-worker [`--build-snapshot`]: #--build-snapshot [`--cpu-prof-dir`]: #--cpu-prof-dir -[`--diagnostic-dir`]: #--diagnostic-dir +[`--diagnostic-dir`]: #--diagnostic-dirdirectory [`--disable-sigusr1`]: #--disable-sigusr1 [`--env-file-if-exists`]: #--env-file-if-existsconfig [`--env-file`]: #--env-fileconfig From 89d051b3579a0f3ac84cbf816d3467a9532a6765 Mon Sep 17 00:00:00 2001 From: haramjeong <04harams77@gmail.com> Date: Tue, 15 Jul 2025 08:56:46 +0900 Subject: [PATCH 5/5] src: make format-cpp-build --- src/inspector_profiler.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/inspector_profiler.cc b/src/inspector_profiler.cc index d629975a36998f..f73491342de0b5 100644 --- a/src/inspector_profiler.cc +++ b/src/inspector_profiler.cc @@ -1,5 +1,4 @@ #include "inspector_profiler.h" -#include "uv.h" #include "base_object-inl.h" #include "debug_utils-inl.h" #include "diagnosticfilename-inl.h" @@ -9,6 +8,7 @@ #include "node_file.h" #include "node_internals.h" #include "util-inl.h" +#include "uv.h" #include "v8-inspector.h" #include @@ -469,10 +469,11 @@ static void EndStartedProfilers(Environment* env) { static std::string ReplacePlaceholders(const std::string& pattern) { std::string result = pattern; - static const std::unordered_map> kPlaceholderMap = { - { "${pid}", []() { return std::to_string(uv_os_getpid()); } }, - // TODO(haramj): Add more placeholders as needed. - }; + static const std::unordered_map> + kPlaceholderMap = { + {"${pid}", []() { return std::to_string(uv_os_getpid()); }}, + // TODO(haramj): Add more placeholders as needed. + }; for (const auto& [placeholder, getter] : kPlaceholderMap) { size_t pos = 0;