Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 9566fe8

Browse files
committed
src: only start idle notifier when profiling
The previous commit adds a notifier that tells the V8 profiler when node.js is idle, i.e. when it's about to start sleeping in the platform's equivalent of epoll_wait(). This commit adds a heuristic that only starts the notifier when the V8 profiler is started from the command line.
1 parent 57231d5 commit 9566fe8

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

src/node.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ static const char* eval_string = NULL;
131131
static bool use_debug_agent = false;
132132
static bool debug_wait_connect = false;
133133
static int debug_port = 5858;
134+
static bool v8_is_profiling = false;
134135

135136
// used by C++ modules as well
136137
bool no_deprecation = false;
@@ -3076,6 +3077,17 @@ void Init(int* argc,
30763077
const char** v8_argv;
30773078
ParseArgs(argc, argv, exec_argc, exec_argv, &v8_argc, &v8_argv);
30783079

3080+
// TODO(bnoordhuis) Intercept --prof arguments and start the CPU profiler
3081+
// manually? That would give us a little more control over its runtime
3082+
// behavior but it could also interfere with the user's intentions in ways
3083+
// we fail to anticipate. Dillema.
3084+
for (int i = 1; i < v8_argc; ++i) {
3085+
if (strncmp(v8_argv[i], "--prof", sizeof("--prof") - 1) == 0) {
3086+
v8_is_profiling = true;
3087+
break;
3088+
}
3089+
}
3090+
30793091
// The const_cast doesn't violate conceptual const-ness. V8 doesn't modify
30803092
// the argv array or the elements it points to.
30813093
V8::SetFlagsFromCommandLine(&v8_argc, const_cast<char**>(v8_argv), true);
@@ -3247,8 +3259,6 @@ Environment* CreateEnvironment(Isolate* isolate,
32473259
// but not all samples are created equal; mark the wall clock time spent in
32483260
// epoll_wait() and friends so profiling tools can filter it out. The samples
32493261
// still end up in v8.log but with state=IDLE rather than state=EXTERNAL.
3250-
// TODO(bnoordhuis): Only start when profiling. OTOH, the performance impact
3251-
// is probably negligible.
32523262
// TODO(bnoordhuis) Depends on a libuv implementation detail that we should
32533263
// probably fortify in the API contract, namely that the last started prepare
32543264
// or check watcher runs first. It's not 100% foolproof; if an add-on starts
@@ -3257,11 +3267,13 @@ Environment* CreateEnvironment(Isolate* isolate,
32573267
uv_prepare_init(env->event_loop(), env->idle_prepare_handle());
32583268
uv_prepare_start(env->idle_prepare_handle(), SetIdle);
32593269
uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_prepare_handle()));
3260-
3261-
uv_check_init(env->event_loop(), env->idle_check_handle());
3262-
uv_check_start(env->idle_check_handle(), ClearIdle);
32633270
uv_unref(reinterpret_cast<uv_handle_t*>(env->idle_check_handle()));
32643271

3272+
if (v8_is_profiling) {
3273+
uv_check_init(env->event_loop(), env->idle_check_handle());
3274+
uv_check_start(env->idle_check_handle(), ClearIdle);
3275+
}
3276+
32653277
return env;
32663278
}
32673279

0 commit comments

Comments
 (0)