Skip to content

Commit

Permalink
cast pid as integer
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 31, 2017
1 parent e3660d8 commit b70eca1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var stats = {

if(this.cpu !== null) {
fs.readFile('/proc/uptime', 'utf8', function(err, uptime) {
if(err)
if(err) {
return done(err, null)
}

if(uptime === undefined) {
console.error("[pidusage] We couldn't find uptime from /proc/uptime")
Expand All @@ -28,24 +29,26 @@ var stats = {
})
} else {
helpers.cpu(function(err, cpu) {
if(err)
if(err) {
return done(err, null)
}

self.cpu = cpu
return self.proc_calc(pid, options, done)
})
}
},
proc_calc: function(pid, options, done) {
pid = parseInt(pid, 10)
var history = this.history[pid] ? this.history[pid] : {}
var cpu = this.cpu
var self = this

//Arguments to path.join must be strings
fs.readFile(p.join('/proc', ''+pid, 'stat'), 'utf8', function(err, infos) {

if(err)
if(err) {
return done(err, null)
}

//https://github.com/arunoda/node-usage/commit/a6ca74ecb8dd452c3c00ed2bde93294d7bb75aa8
//preventing process space in name by removing values before last ) (pid (name) ...)
Expand Down Expand Up @@ -94,7 +97,7 @@ var stats = {
* on solaris 11 can't figure out a way to do this properly so...
*/
ps: function(pid, options, done) {

pid = parseInt(pid, 10)
var cmd = 'ps -o pcpu,rss -p '

if(os.platform() == 'aix')
Expand All @@ -117,6 +120,7 @@ var stats = {
* This is really in a beta stage
*/
win: function(pid, options, done) {
pid = parseInt(pid, 10)
var history = this.history[pid] ? this.history[pid] : {}
// http://social.msdn.microsoft.com/Forums/en-US/469ec6b7-4727-4773-9dc7-6e3de40e87b8/cpu-usage-in-for-each-active-process-how-is-this-best-determined-and-implemented-in-an?forum=csharplanguage
var args = 'PROCESS ' + pid + ' get workingsetsize,usermodetime,kernelmodetime'
Expand Down

0 comments on commit b70eca1

Please sign in to comment.