◐ Shell
clean mode source ↗

cli: support `${pid}` placeholder in --cpu-prof-name · nodejs/node@d5640ca

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(