From 422f53d348122f786c8e35d19b855bfa3461532f Mon Sep 17 00:00:00 2001 From: Oleksandr Chekhovskyi Date: Fri, 28 Aug 2015 13:54:45 +0200 Subject: [PATCH] Enable idle notifier in node.js when profiling Now idle time is marked as "(idle)" instead of just "(program)". --- v8-profiler.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/v8-profiler.js b/v8-profiler.js index 63e6552..e4c2a39 100644 --- a/v8-profiler.js +++ b/v8-profiler.js @@ -64,6 +64,7 @@ CpuProfile.prototype.getHeader = function() { } var startTime, endTime; +var activeProfiles = []; var profiler = { /*HEAP PROFILER API*/ @@ -111,16 +112,37 @@ var profiler = { get profiles() { return binding.cpu.profiles; }, startProfiling: function(name, recsamples) { + if (activeProfiles.length == 0 && typeof process._startProfilerIdleNotifier == "function") + process._startProfilerIdleNotifier(); + + name = name || ""; + + if (activeProfiles.indexOf(name) < 0) + activeProfiles.push(name) + startTime = Date.now(); binding.cpu.startProfiling(name, recsamples); }, stopProfiling: function(name) { + var index = activeProfiles.indexOf(name); + if (name && index < 0) + return; + var profile = binding.cpu.stopProfiling(name); endTime = Date.now(); profile.__proto__ = CpuProfile.prototype; if (!profile.startTime) profile.startTime = startTime; if (!profile.endTime) profile.endTime = endTime; + + if (name) + activeProfiles.splice(index, 1); + else + activeProfiles.length = activeProfiles.length - 1; + + if (activeProfiles.length == 0 && typeof process._stopProfilerIdleNotifier == "function") + process._stopProfilerIdleNotifier(); + return profile; },