perf_hooks: add idleTime and event loop util

Use uv_metrics_idle_time() to return a high resolution millisecond timer
of the amount of time the event loop has been idle since it was
initialized.

Include performance.eventLoopUtilization() API to handle the math of
calculating the idle and active times. This has been added to prevent
accidental miscalculations of the event loop utilization. Such as not
taking into consideration offsetting nodeTiming.loopStart or timing
differences when being called from a Worker thread.

PR-URL: https://github.com/nodejs/node/pull/34938
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Adrian Estrada <edsadr@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
This commit is contained in:
Trevor Norris 2020-08-25 13:36:37 -06:00 committed by Rich Trott
parent 47f4080db4
commit 589b2a1244
8 changed files with 279 additions and 2 deletions

View file

@ -25,7 +25,8 @@ const {
timerify,
constants,
installGarbageCollectionTracking,
removeGarbageCollectionTracking
removeGarbageCollectionTracking,
loopIdleTime,
} = internalBinding('performance');
const {
@ -205,6 +206,10 @@ class PerformanceNodeTiming extends PerformanceEntry {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE);
}
get idleTime() {
return loopIdleTime();
}
[kInspect]() {
return {
name: 'node',
@ -448,10 +453,37 @@ class Performance {
return ret;
}
eventLoopUtilization(util1, util2) {
const ls = nodeTiming.loopStart;
if (ls <= 0) {
return { idle: 0, active: 0, utilization: 0 };
}
if (util2) {
const idle = util1.idle - util2.idle;
const active = util1.active - util2.active;
return { idle, active, utilization: active / (idle + active) };
}
const idle = nodeTiming.idleTime;
const active = performance.now() - ls - idle;
if (!util1) {
return { idle, active, utilization: active / (idle + active) };
}
const idle_delta = idle - util1.idle;
const active_delta = active - util1.active;
const utilization = active_delta / (idle_delta + active_delta);
return { idle: idle_delta, active: active_delta, utilization };
}
[kInspect]() {
return {
nodeTiming: this.nodeTiming,
timeOrigin: this.timeOrigin
timeOrigin: this.timeOrigin,
idleTime: this.idleTime,
};
}
}