src: add JS start/stop methods for idle notifier
authorBen Noordhuis <info@bnoordhuis.nl>
Thu, 3 Oct 2013 09:03:46 +0000 (11:03 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Thu, 3 Oct 2013 14:50:54 +0000 (16:50 +0200)
The previous commit changes the profiler idle notifier so that it only
gets started when a --prof or --prof_lazy argument is specified on the
command line.

This commit adds two internal methods to the process object that allows
one to start and stop the idle notifier programmatically.

src/node.cc

index 5927867..b29fa35 100644 (file)
@@ -2294,6 +2294,40 @@ static void NeedImmediateCallbackSetter(
 }
 
 
+void SetIdle(uv_prepare_t* handle, int) {
+  Environment* env = Environment::from_idle_prepare_handle(handle);
+  env->isolate()->GetCpuProfiler()->SetIdle(true);
+}
+
+
+void ClearIdle(uv_check_t* handle, int) {
+  Environment* env = Environment::from_idle_check_handle(handle);
+  env->isolate()->GetCpuProfiler()->SetIdle(false);
+}
+
+
+void StartProfilerIdleNotifier(Environment* env) {
+  uv_prepare_start(env->idle_prepare_handle(), SetIdle);
+  uv_check_start(env->idle_check_handle(), ClearIdle);
+}
+
+
+void StopProfilerIdleNotifier(Environment* env) {
+  uv_prepare_stop(env->idle_prepare_handle());
+  uv_check_stop(env->idle_check_handle());
+}
+
+
+void StartProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
+  StartProfilerIdleNotifier(Environment::GetCurrent(args.GetIsolate()));
+}
+
+
+void StopProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
+  StopProfilerIdleNotifier(Environment::GetCurrent(args.GetIsolate()));
+}
+
+
 #define READONLY_PROPERTY(obj, str, var)                                      \
   do {                                                                        \
     obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly);            \
@@ -2465,6 +2499,12 @@ void SetupProcessObject(Environment* env,
                        DebugPortSetter);
 
   // define various internal methods
+  NODE_SET_METHOD(process,
+                  "_startProfilerIdleNotifier",
+                  StartProfilerIdleNotifier);
+  NODE_SET_METHOD(process,
+                  "_stopProfilerIdleNotifier",
+                  StopProfilerIdleNotifier);
   NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests);
   NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);
   NODE_SET_METHOD(process, "reallyExit", Exit);
@@ -3218,18 +3258,6 @@ void EmitExit(Environment* env) {
 }
 
 
-void SetIdle(uv_prepare_t* handle, int) {
-  Environment* env = Environment::from_idle_prepare_handle(handle);
-  env->isolate()->GetCpuProfiler()->SetIdle(true);
-}
-
-
-void ClearIdle(uv_check_t* handle, int) {
-  Environment* env = Environment::from_idle_check_handle(handle);
-  env->isolate()->GetCpuProfiler()->SetIdle(false);
-}
-
-
 Environment* CreateEnvironment(Isolate* isolate,
                                int argc,
                                const char* const* argv,
@@ -3246,15 +3274,6 @@ Environment* CreateEnvironment(Isolate* isolate,
       reinterpret_cast<uv_handle_t*>(env->immediate_check_handle()));
   uv_idle_init(env->event_loop(), env->immediate_idle_handle());
 
-  Local<FunctionTemplate> process_template = FunctionTemplate::New();
-  process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
-
-  Local<Object> process_object = process_template->GetFunction()->NewInstance();
-  env->set_process_object(process_object);
-
-  SetupProcessObject(env, argc, argv, exec_argc, exec_argv);
-  Load(env);
-
   // Inform V8's CPU profiler when we're idle.  The profiler is sampling-based
   // but not all samples are created equal; mark the wall clock time spent in
   // epoll_wait() and friends so profiling tools can filter it out.  The samples
@@ -3274,6 +3293,15 @@ Environment* CreateEnvironment(Isolate* isolate,
     uv_check_start(env->idle_check_handle(), ClearIdle);
   }
 
+  Local<FunctionTemplate> process_template = FunctionTemplate::New();
+  process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
+
+  Local<Object> process_object = process_template->GetFunction()->NewInstance();
+  env->set_process_object(process_object);
+
+  SetupProcessObject(env, argc, argv, exec_argc, exec_argv);
+  Load(env);
+
   return env;
 }