Process cpu % and memory use of a PID
Ideas from https://github.com/arunoda/node-usage/ but with no C-bindings
var pusage = require('pidusage')
pusage.stat(process.pid, function(err, stat) {
expect(err).to.be.null
expect(stat).to.be.an('object')
expect(stat).to.have.property('cpu')
expect(stat).to.have.property('memory')
console.log('Pcpu: %s', stat.cpu)
console.log('Mem: %s', stat.memory) //those are bytes
})
// Unmonitor process
pusage.unmonitor(18902);
A check on the os.platform is done to determine the method to use.
We use /proc/{pid}/stat in addition to the the PAGE_SIZE and the CLK_TCK direclty from getconf() command. Uptime comes from proc/uptime file because it's more accurate than the nodejs os.uptime().
/!\ As stated in #17, memory will increase when using pidusage.stat in an interval because of readFile. Use --expose-gc and release the garbage collector to avoid such leaking.
We use a fallback with the ps -o pcpu,rss -p PID command to get the same informations.
AIX is tricky because I have no AIX test environement, at the moment we use: ps -o pcpu,rssize -p PID but /proc results should be more accurate! If you're familiar with the AIX environment and know how to get the same results as we've got with Linux systems, please help.
#4
Windows is really tricky, atm it uses the wmic.exe, feel free to share ideas on how to improve this.
More specifically, thanks to @crystaldust we replaced wmic PROCESS by wmic path Win32_PerfFormattedData_PerfProc_Process to get more accurated data (PR, commit).
MIT