Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixup! align timeOrigin in worker.performance.eventLoopUtilization
  • Loading branch information
legendecas committed Jul 12, 2022
commit a57b4923da8a3a9548afdfec42096ac619c70f41
53 changes: 40 additions & 13 deletions lib/internal/perf/event_loop_utilization.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
'use strict';

const nodeTiming = require('internal/perf/nodetiming');

const { now } = require('internal/perf/utils');
const {
constants: {
NODE_PERFORMANCE_MILESTONE_LOOP_START,
},
loopIdleTime,
milestones,
} = internalBinding('performance');

function eventLoopUtilization(util1, util2) {
const ls = nodeTiming.loopStart;
// Get the original milestone timestamps that calculated from the beginning
// of the process.
return internalEventLoopUtilization(
milestones[NODE_PERFORMANCE_MILESTONE_LOOP_START] / 1e6,
loopIdleTime(),
util1,
util2
);
}

if (ls <= 0) {
function internalEventLoopUtilization(loopStart, loopIdleTime, util1, util2) {
if (loopStart <= 0) {
return { idle: 0, active: 0, utilization: 0 };
}

Expand All @@ -17,17 +30,31 @@ function eventLoopUtilization(util1, util2) {
return { idle, active, utilization: active / (idle + active) };
}

const idle = nodeTiming.idleTime;
const active = now() - ls - idle;
// Using process.hrtime() to get the time from the beginning of the process,
// and offset it by the loopStart time (which is also calculated from the
// beginning of the process).
const now = process.hrtime();
const active = now[0] * 1e3 + now[1] / 1e6 - loopStart - loopIdleTime;

if (!util1) {
return { idle, active, utilization: active / (idle + active) };
return {
idle: loopIdleTime,
active,
utilization: active / (loopIdleTime + 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 };
const idleDelta = loopIdleTime - util1.idle;
const activeDelta = active - util1.active;
const utilization = activeDelta / (idleDelta + activeDelta);
return {
idle: idleDelta,
active: activeDelta,
utilization,
};
}

module.exports = eventLoopUtilization;
module.exports = {
internalEventLoopUtilization,
eventLoopUtilization,
};
2 changes: 1 addition & 1 deletion lib/internal/perf/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const {
filterBufferMapByNameAndType,
} = require('internal/perf/observe');

const eventLoopUtilization = require('internal/perf/event_loop_utilization');
const { eventLoopUtilization } = require('internal/perf/event_loop_utilization');
const nodeTiming = require('internal/perf/nodetiming');
const timerify = require('internal/perf/timerify');
const { customInspectSymbol: kInspect } = require('internal/util');
Expand Down
32 changes: 9 additions & 23 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const {
const EventEmitter = require('events');
const assert = require('internal/assert');
const path = require('path');
const { now } = require('internal/perf/utils');
const {
internalEventLoopUtilization
} = require('internal/perf/event_loop_utilization');

const errorCodes = require('internal/errors').codes;
const {
Expand Down Expand Up @@ -472,28 +474,12 @@ function eventLoopUtilization(util1, util2) {
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 = this[kHandle].loopIdleTime();

// Using performance.now() here is fine since it's always the time from
// the beginning of the process, and is why it needs to be offset by the
// loopStart time (which is also calculated from the beginning of the
// process).
const active = now() - this[kLoopStartTime] - 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 };
return internalEventLoopUtilization(
this[kLoopStartTime],
this[kHandle].loopIdleTime(),
util1,
util2
);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/node_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ void Worker::LoopStartTime(const FunctionCallbackInfo<Value>& args) {
double loop_start_time = w->env_->performance_state()->milestones[
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START];
CHECK_GE(loop_start_time, 0);
args.GetReturnValue().Set((loop_start_time - w->env_->time_origin()) / 1e6);
args.GetReturnValue().Set(loop_start_time / 1e6);
}

namespace {
Expand Down