soyuka / pidusage

Cross-platform process cpu % and memory usage of a PID
MIT License
512 stars 55 forks source link

Its not telling accurate ram and CPU usage #18

Closed muhammadfaizan closed 8 years ago

muhammadfaizan commented 8 years ago

I was trying out this on my ubuntu server, the top command showed me cpu usage of 40% while pidusage of same process showed 0% cpu usage, can you tell me where i am wrong, and is there a fix? Also the RAM consumption was above 90MB and it showed 0.63MB

soyuka commented 8 years ago

Those numbers don't feel right. Can you give me a way to reproduce this? Please use ps instead of top to do comparisons with pidusage.

soyuka commented 8 years ago

Just tested this myself and I got similar results between ps -o pcpu,rss -p "$pid" and pidusage (on debian, ubuntu would take the same path). Please provide a reproducible pattern.

muhammadfaizan commented 8 years ago

yes I used ps as well, i am on ubuntu micro instance of Ec2.

It is happening in ubuntu only, although it works well with Darwin, locally.

soyuka commented 8 years ago

Can you give me a way to reproduce please?

On Darwin this uses ps internally so they would not be any differences.

muhammadfaizan commented 8 years ago

I am using the following code. this will reproduce the result for you.

'use strict';
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const process = exec('phantomjs --webdriver 4444');
var pusage = require('pidusage');

const KB = 1024;
const MB = 1024 * KB;
const GB = 1024 * MB;

setInterval(function() {
  pusage.stat(process .pid, function(err, stat) {
    if (err) {
      console.log(err);
    }
    console.log(`${process .pid} : Pcpu : ${stat.cpu}`);
    console.log(`${process .pid} :Mem : ${stat.memory / MB} MB`); 

    }
  });
}, 5000);
soyuka commented 8 years ago

I can't spam phantomjs, how 're you increasing cpu usage?

I might have an idea though is that ps computes child processes.