Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using process.cpuUsage to get percentage #283

Closed
nickstanish opened this issue Sep 7, 2016 · 4 comments
Closed

Using process.cpuUsage to get percentage #283

nickstanish opened this issue Sep 7, 2016 · 4 comments

Comments

@nickstanish
Copy link

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.

@Trott
Copy link
Member

Trott commented Sep 17, 2016

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.

@SpeedyCraftah
Copy link

Sad

@Trott
Copy link
Member

Trott commented May 31, 2019

Sad

I don't know, seems like they got a pretty good response in nodejs/node#8728.

@Trott
Copy link
Member

Trott commented May 31, 2019

(Locking this issue. For legit help questions, feel free to open a new issue.)

@nodejs nodejs locked as resolved and limited conversation to collaborators May 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants