Skip to content

Commit c787bb8

Browse files
joyeecheungtargos
authored andcommitted
src: refactor profile initialization
- Process and store --cpu-prof-dir and --cpu-prof-name during Environment creation - Start profilers in one `profiler::StartProfilers()` PR-URL: #27475 Refs: #27421 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 600048b commit c787bb8

File tree

6 files changed

+62
-74
lines changed

6 files changed

+62
-74
lines changed

src/env-inl.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -673,16 +673,16 @@ Environment::cpu_profiler_connection() {
673673
return cpu_profiler_connection_.get();
674674
}
675675

676-
inline void Environment::set_cpu_profile_path(const std::string& path) {
677-
cpu_profile_path_ = path;
676+
inline void Environment::set_cpu_prof_name(const std::string& name) {
677+
cpu_prof_name_ = name;
678678
}
679679

680-
inline const std::string& Environment::cpu_profile_path() const {
681-
return cpu_profile_path_;
680+
inline const std::string& Environment::cpu_prof_name() const {
681+
return cpu_prof_name_;
682682
}
683683

684-
inline void Environment::set_cpu_prof_dir(const std::string& path) {
685-
cpu_prof_dir_ = path;
684+
inline void Environment::set_cpu_prof_dir(const std::string& dir) {
685+
cpu_prof_dir_ = dir;
686686
}
687687

688688
inline const std::string& Environment::cpu_prof_dir() const {

src/env.cc

-14
Original file line numberDiff line numberDiff line change
@@ -907,20 +907,6 @@ void Environment::stop_sub_worker_contexts() {
907907

908908
#if HAVE_INSPECTOR
909909

910-
void Environment::InitializeCPUProfDir(const std::string& dir) {
911-
if (!dir.empty()) {
912-
cpu_prof_dir_ = dir;
913-
return;
914-
}
915-
char cwd[CWD_BUFSIZE];
916-
size_t size = CWD_BUFSIZE;
917-
int err = uv_cwd(cwd, &size);
918-
// TODO(joyeecheung): fallback to exec path / argv[0]
919-
CHECK_EQ(err, 0);
920-
CHECK_GT(size, 0);
921-
cpu_prof_dir_ = cwd;
922-
}
923-
924910
#endif // HAVE_INSPECTOR
925911

926912
void MemoryTracker::TrackField(const char* edge_name,

src/env.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,11 @@ class Environment : public MemoryRetainer {
11401140
std::unique_ptr<profiler::V8CpuProfilerConnection> connection);
11411141
profiler::V8CpuProfilerConnection* cpu_profiler_connection();
11421142

1143-
inline void set_cpu_profile_path(const std::string& path);
1144-
inline const std::string& cpu_profile_path() const;
1143+
inline void set_cpu_prof_name(const std::string& name);
1144+
inline const std::string& cpu_prof_name() const;
11451145

1146-
inline void set_cpu_prof_dir(const std::string& path);
1146+
inline void set_cpu_prof_dir(const std::string& dir);
11471147
inline const std::string& cpu_prof_dir() const;
1148-
1149-
void InitializeCPUProfDir(const std::string& dir);
11501148
#endif // HAVE_INSPECTOR
11511149

11521150
private:
@@ -1184,7 +1182,7 @@ class Environment : public MemoryRetainer {
11841182
std::unique_ptr<profiler::V8CpuProfilerConnection> cpu_profiler_connection_;
11851183
std::string coverage_directory_;
11861184
std::string cpu_prof_dir_;
1187-
std::string cpu_profile_path_;
1185+
std::string cpu_prof_name_;
11881186
#endif // HAVE_INSPECTOR
11891187

11901188
std::shared_ptr<EnvironmentOptions> options_;

src/inspector_profiler.cc

+50-32
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,26 @@ void V8CpuProfilerConnection::OnMessage(
209209
}
210210

211211
void V8CpuProfilerConnection::WriteCpuProfile(Local<String> message) {
212-
const std::string& path = env()->cpu_profile_path();
213-
CHECK(!path.empty());
214-
std::string directory = path.substr(0, path.find_last_of(kPathSeparator));
215-
if (directory != path) {
216-
uv_fs_t req;
217-
int ret = fs::MKDirpSync(nullptr, &req, directory, 0777, nullptr);
218-
uv_fs_req_cleanup(&req);
219-
if (ret < 0 && ret != UV_EEXIST) {
220-
char err_buf[128];
221-
uv_err_name_r(ret, err_buf, sizeof(err_buf));
222-
fprintf(stderr,
223-
"%s: Failed to create cpu profile directory %s\n",
224-
err_buf,
225-
directory.c_str());
226-
return;
227-
}
212+
const std::string& filename = env()->cpu_prof_name();
213+
const std::string& directory = env()->cpu_prof_dir();
214+
CHECK(!filename.empty());
215+
CHECK(!directory.empty());
216+
uv_fs_t req;
217+
int ret = fs::MKDirpSync(nullptr, &req, directory, 0777, nullptr);
218+
uv_fs_req_cleanup(&req);
219+
if (ret < 0 && ret != UV_EEXIST) {
220+
char err_buf[128];
221+
uv_err_name_r(ret, err_buf, sizeof(err_buf));
222+
fprintf(stderr,
223+
"%s: Failed to create cpu profile directory %s\n",
224+
err_buf,
225+
directory.c_str());
226+
return;
228227
}
229228
MaybeLocal<String> result = GetResult(message);
229+
std::string target = directory + kPathSeparator + filename;
230230
if (!result.IsEmpty()) {
231-
WriteResult(path.c_str(), result.ToLocalChecked());
231+
WriteResult(target.c_str(), result.ToLocalChecked());
232232
}
233233
}
234234

@@ -312,24 +312,42 @@ void EndStartedProfilers(Environment* env) {
312312
}
313313
}
314314

315-
void StartCoverageCollection(Environment* env) {
316-
CHECK_NULL(env->coverage_connection());
317-
env->set_coverage_connection(std::make_unique<V8CoverageConnection>(env));
318-
env->coverage_connection()->Start();
315+
std::string GetCwd() {
316+
char cwd[CWD_BUFSIZE];
317+
size_t size = CWD_BUFSIZE;
318+
int err = uv_cwd(cwd, &size);
319+
// This can fail if the cwd is deleted.
320+
// TODO(joyeecheung): store this in the Environment during Environment
321+
// creation and fallback to exec_path and argv0, then we no longer need
322+
// SetCoverageDirectory().
323+
CHECK_EQ(err, 0);
324+
CHECK_GT(size, 0);
325+
return cwd;
319326
}
320327

321-
void StartCpuProfiling(Environment* env, const std::string& profile_name) {
322-
std::string path = env->cpu_prof_dir() + std::string(kPathSeparator);
323-
if (profile_name.empty()) {
324-
DiagnosticFilename filename(env, "CPU", "cpuprofile");
325-
path += *filename;
326-
} else {
327-
path += profile_name;
328+
void StartProfilers(Environment* env) {
329+
Isolate* isolate = env->isolate();
330+
Local<String> coverage_str = env->env_vars()->Get(
331+
isolate, FIXED_ONE_BYTE_STRING(isolate, "NODE_V8_COVERAGE"));
332+
if (!coverage_str.IsEmpty() && coverage_str->Length() > 0) {
333+
CHECK_NULL(env->coverage_connection());
334+
env->set_coverage_connection(std::make_unique<V8CoverageConnection>(env));
335+
env->coverage_connection()->Start();
336+
}
337+
if (env->options()->cpu_prof) {
338+
const std::string& dir = env->options()->cpu_prof_dir;
339+
env->set_cpu_prof_dir(dir.empty() ? GetCwd() : dir);
340+
if (env->options()->cpu_prof_name.empty()) {
341+
DiagnosticFilename filename(env, "CPU", "cpuprofile");
342+
env->set_cpu_prof_name(*filename);
343+
} else {
344+
env->set_cpu_prof_name(env->options()->cpu_prof_name);
345+
}
346+
CHECK_NULL(env->cpu_profiler_connection());
347+
env->set_cpu_profiler_connection(
348+
std::make_unique<V8CpuProfilerConnection>(env));
349+
env->cpu_profiler_connection()->Start();
328350
}
329-
env->set_cpu_profile_path(std::move(path));
330-
env->set_cpu_profiler_connection(
331-
std::make_unique<V8CpuProfilerConnection>(env));
332-
env->cpu_profiler_connection()->Start();
333351
}
334352

335353
static void SetCoverageDirectory(const FunctionCallbackInfo<Value>& args) {

src/node.cc

+1-14
Original file line numberDiff line numberDiff line change
@@ -227,21 +227,8 @@ MaybeLocal<Value> RunBootstrapping(Environment* env) {
227227
Isolate* isolate = env->isolate();
228228
Local<Context> context = env->context();
229229

230-
Local<String> coverage_str = env->env_vars()->Get(
231-
isolate, FIXED_ONE_BYTE_STRING(isolate, "NODE_V8_COVERAGE"));
232-
if (!coverage_str.IsEmpty() && coverage_str->Length() > 0) {
233230
#if HAVE_INSPECTOR
234-
profiler::StartCoverageCollection(env);
235-
#else
236-
fprintf(stderr, "NODE_V8_COVERAGE cannot be used without inspector\n");
237-
#endif // HAVE_INSPECTOR
238-
}
239-
240-
#if HAVE_INSPECTOR
241-
if (env->options()->cpu_prof) {
242-
env->InitializeCPUProfDir(env->options()->cpu_prof_dir);
243-
profiler::StartCpuProfiling(env, env->options()->cpu_prof_name);
244-
}
231+
profiler::StartProfilers(env);
245232
#endif // HAVE_INSPECTOR
246233

247234
// Add a reference to the global object

src/node_internals.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
324324

325325
#if HAVE_INSPECTOR
326326
namespace profiler {
327-
void StartCoverageCollection(Environment* env);
328-
void StartCpuProfiling(Environment* env, const std::string& profile_name);
327+
void StartProfilers(Environment* env);
329328
void EndStartedProfilers(Environment* env);
330329
}
331330
#endif // HAVE_INSPECTOR

0 commit comments

Comments
 (0)