You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to find the process' percentage of cpu usage of the system and the values that I'm getting are way off from what system process managers report: e.g. top/htop and activity monitor
First approach: adding user and system time and dividing by elapsed time - as seen here nodejs/node#6157 (comment)
This example does not work due to passing invalid arguments to secNSec2ms
Fixing those issues and creating a loop so that I can compare it to other monitors ends up with this:
setInterval(function () {
var startTime = process.hrtime()
var startUsage = process.cpuUsage()
// spin the CPU for 500 milliseconds
var now = Date.now()
while (Date.now() - now < 500);
var elapTime = process.hrtime(startTime)
var elapUsage = process.cpuUsage(startUsage)
var elapTimeMS = hrtimeToMS(elapTime)
var elapUserMS = elapUsage.user / 1000; // microseconds to milliseconds
var elapSystMS = elapUsage.system / 1000;
var cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1) + '%'
console.log('elapsed time ms: ', elapTimeMS)
console.log('elapsed user ms: ', elapUserMS)
console.log('elapsed system ms:', elapSystMS)
console.log('cpu percent: ', cpuPercent, '\n')
}, 1000);
function hrtimeToMS (hrtime) {
return hrtime[0] * 1000 + hrtime[1] / 1000000
}
Now on my macbook pro (2.9 GHz Intel Core i5 - dual core but each core has 2 hyperthreads so it is effectively 4 cores, and os.cpus() would report 4 cpus), this consistently gives about 99.7%.
Changing this line to account for number of cores var cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1) + '%' gives 24.9 pretty consistently. However htop shows usage for that process at around 32% and activity monitor ranges from 26-45%. Which makes me think that a lot is not accounted for.
If I save the values so that the sleeping time is included and rotated:
const os = require('os');
const NUMBER_OF_CPUS = os.cpus().length;
let startTime = process.hrtime()
let startUsage = process.cpuUsage()
setInterval(() => {
// spin the CPU for 500 milliseconds
var now = Date.now()
while (Date.now() - now < 500);
const newTime = process.hrtime();
const newUsage = process.cpuUsage();
const elapTime = process.hrtime(startTime)
const elapUsage = process.cpuUsage(startUsage)
startUsage = newUsage;
startTime = newTime;
const elapTimeMS = hrtimeToMS(elapTime)
const elapUserMS = elapUsage.user / 1000; // microseconds to milliseconds
const elapSystMS = elapUsage.system / 1000;
const cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS / NUMBER_OF_CPUS).toFixed(1) + '%'
console.log('elapsed time ms: ', elapTimeMS)
console.log('elapsed user ms: ', elapUserMS)
console.log('elapsed system ms:', elapSystMS)
console.log('cpu percent: ', cpuPercent, '\n')
}, 1000);
function hrtimeToMS (hrtime) {
return hrtime[0] * 1000 + hrtime[1] / 1000000
}
then the percentage is consistently 8.3%, but htop sees about 35% and activity monitor ranges from 25-45 again.
I tried another method in an express app where I stored previous values of process.cpuUsage() and os.cpus() and set a timer for every 1000ms where I would compute the percentage and update the previous values. This involved adding all of the times from os.cpus() (which are millis) and taking the difference from the previous values to get the total cpu time in that period (and would account for number of cores). Then I used that instead of hrtime, but these results seemed to be significantly larger. Monitors showed about 0.1 to 0.2% but the node app's reported usage ranged from 1 to 45%.
Is measuring elapsed time with hrtime the right way to do this natively?
From searching the web, it looks like a lot of libraries are just reading in process tables or executing shell commands to get the percentage.
The text was updated successfully, but these errors were encountered:
Sorry no one has responded in 10 days. You might consider opening an issue for this in the main repo.
Or you might have better luck trying to ask this question in the #Node.js IRC channel or on StackOverflow.
I'm trying to find the process' percentage of cpu usage of the system and the values that I'm getting are way off from what system process managers report: e.g. top/htop and activity monitor
First approach: adding user and system time and dividing by elapsed time - as seen here nodejs/node#6157 (comment)
This example does not work due to passing invalid arguments to
secNSec2ms
Fixing those issues and creating a loop so that I can compare it to other monitors ends up with this:
Now on my macbook pro (2.9 GHz Intel Core i5 - dual core but each core has 2 hyperthreads so it is effectively 4 cores, and
os.cpus()
would report 4 cpus), this consistently gives about 99.7%.Changing this line to account for number of cores
var cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1) + '%'
gives 24.9 pretty consistently. However htop shows usage for that process at around 32% and activity monitor ranges from 26-45%. Which makes me think that a lot is not accounted for.If I save the values so that the sleeping time is included and rotated:
then the percentage is consistently 8.3%, but htop sees about 35% and activity monitor ranges from 25-45 again.
I tried another method in an express app where I stored previous values of
process.cpuUsage()
andos.cpus()
and set a timer for every 1000ms where I would compute the percentage and update the previous values. This involved adding all of the times fromos.cpus()
(which are millis) and taking the difference from the previous values to get the total cpu time in that period (and would account for number of cores). Then I used that instead of hrtime, but these results seemed to be significantly larger. Monitors showed about 0.1 to 0.2% but the node app's reported usage ranged from 1 to 45%.Is measuring elapsed time with
hrtime
the right way to do this natively?From searching the web, it looks like a lot of libraries are just reading in process tables or executing shell commands to get the percentage.
The text was updated successfully, but these errors were encountered: