@@ -209,26 +209,26 @@ void V8CpuProfilerConnection::OnMessage(
209
209
}
210
210
211
211
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 ;
228
227
}
229
228
MaybeLocal<String> result = GetResult (message);
229
+ std::string target = directory + kPathSeparator + filename;
230
230
if (!result.IsEmpty ()) {
231
- WriteResult (path .c_str (), result.ToLocalChecked ());
231
+ WriteResult (target .c_str (), result.ToLocalChecked ());
232
232
}
233
233
}
234
234
@@ -312,24 +312,42 @@ void EndStartedProfilers(Environment* env) {
312
312
}
313
313
}
314
314
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;
319
326
}
320
327
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 ();
328
350
}
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 ();
333
351
}
334
352
335
353
static void SetCoverageDirectory (const FunctionCallbackInfo<Value>& args) {
0 commit comments