From 174fe65f6146a9af35502a6564b7b47b7f9abe52 Mon Sep 17 00:00:00 2001 From: simonepri Date: Sun, 3 Jun 2018 20:55:52 +0200 Subject: [PATCH 1/4] Fix spacing --- lib/procfile.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/procfile.js b/lib/procfile.js index 1da16e0..a7e0a3b 100644 --- a/lib/procfile.js +++ b/lib/procfile.js @@ -15,11 +15,10 @@ function readProcFile (pid, options, done) { if (err.code === 'ENOENT') { err.message = 'No maching pid found' } - return done(err, null) } - var date = Date.now() + // https://github.com/arunoda/node-usage/commit/a6ca74ecb8dd452c3c00ed2bde93294d7bb75aa8 // preventing process space in name by removing values before last ) (pid (name) ...) var index = infos.lastIndexOf(')') @@ -38,8 +37,11 @@ function readProcFile (pid, options, done) { uptime: cpu.uptime } - // http://stackoverflow.com/questions/16726779/total-cpu-usage-of-an-application-from-proc-pid-stat/16736599#16736599 + var memory = stat.rss * cpu.pageSize + + // https://stackoverflow.com/a/16736599/3921589 var childrens = options.childrens ? stat.cutime + stat.cstime : 0 + // process usage since last call in seconds var total = (stat.stime - (hst.stime || 0) + stat.utime - (hst.utime || 0) + childrens) / cpu.clockTick // time elapsed between calls in seconds var seconds = Math.abs(hst.uptime !== undefined ? stat.uptime - hst.uptime : stat.start - stat.uptime) @@ -48,7 +50,6 @@ function readProcFile (pid, options, done) { history.set(stat, options.maxage) var cpuPercent = Math.min(Math.round((total / seconds) * 100000) / 1000, 100.0) - var memory = stat.rss * cpu.pageSize return done(null, { cpu: cpuPercent, From 88972d8cd38d4137b70261a830af22283b69c57c Mon Sep 17 00:00:00 2001 From: simonepri Date: Sun, 3 Jun 2018 21:10:28 +0200 Subject: [PATCH 2/4] Fix cpu percentage formula --- README.md | 9 ++++----- lib/procfile.js | 23 +++++++++++------------ lib/ps.js | 7 +++++-- lib/wmic.js | 8 ++++---- test/ps.js | 16 ++++++++++++---- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d5268ce..1cdf475 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ setInterval(function () { pidusage(process.pid, function (err, stats) { console.log(stats) // => { - // cpu: 10.0, // percentage (it may happen to be greater than 100%) + // cpu: 10.0, // percentage (0-100%) // memory: 357306368, // bytes // ppid: 312, // PPID // pid: 727, // PID @@ -41,7 +41,7 @@ pidusage([727, 1234], function (err, stats) { console.log(stats) // => { // 727: { - // cpu: 10.0, // percentage + // cpu: 10.0, // percentage (0-100%) // memory: 357306368, // bytes // ppid: 312, // PPID // pid: 727, // PID @@ -50,7 +50,7 @@ pidusage([727, 1234], function (err, stats) { // timestamp: 864000000 // ms since epoch // }, // 1234: { - // cpu: 0.1, // percentage + // cpu: 0.1, // percentage (0-100%) // memory: 3846144, // bytes // ppid: 727, // PPID // pid: 1234, // PID @@ -65,7 +65,7 @@ pidusage([727, 1234], function (err, stats) { const stats = await pidusage(process.pid) console.log(stats) // => { -// cpu: 10.0, // percentage (it may happen to be greater than 100%) +// cpu: 10.0, // percentage (0-100%) // memory: 357306368, // bytes // ppid: 312, // PPID // pid: 727, // PID @@ -135,4 +135,3 @@ This project is licensed under the MIT License - see the [LICENSE][license] file [node:cpuUsage]: https://nodejs.org/api/process.html#process_process_cpuusage_previousvalue [node:memUsage]: https://nodejs.org/api/process.html#process_process_memoryusage - diff --git a/lib/procfile.js b/lib/procfile.js index a7e0a3b..8ba4386 100644 --- a/lib/procfile.js +++ b/lib/procfile.js @@ -3,7 +3,7 @@ var path = require('path') var updateCpu = require('./helpers/cpu') var parallel = require('./helpers/parallel') var history = require('./history') -var cpu = null +var cpuInfo = null function readProcFile (pid, options, done) { var hst = history.get(pid, options.maxage) @@ -18,7 +18,7 @@ function readProcFile (pid, options, done) { return done(err, null) } var date = Date.now() - + // https://github.com/arunoda/node-usage/commit/a6ca74ecb8dd452c3c00ed2bde93294d7bb75aa8 // preventing process space in name by removing values before last ) (pid (name) ...) var index = infos.lastIndexOf(')') @@ -32,29 +32,28 @@ function readProcFile (pid, options, done) { stime: parseFloat(infos[12]), cutime: parseFloat(infos[13]), cstime: parseFloat(infos[14]), - start: parseFloat(infos[19]) / cpu.clockTick, + start: parseFloat(infos[19]) / cpuInfo.clockTick, rss: parseFloat(infos[21]), - uptime: cpu.uptime + uptime: cpuInfo.uptime } - var memory = stat.rss * cpu.pageSize + var memory = stat.rss * cpuInfo.pageSize // https://stackoverflow.com/a/16736599/3921589 var childrens = options.childrens ? stat.cutime + stat.cstime : 0 // process usage since last call in seconds - var total = (stat.stime - (hst.stime || 0) + stat.utime - (hst.utime || 0) + childrens) / cpu.clockTick + var total = (stat.stime - (hst.stime || 0) + stat.utime - (hst.utime || 0) + childrens) / cpuInfo.clockTick // time elapsed between calls in seconds var seconds = Math.abs(hst.uptime !== undefined ? stat.uptime - hst.uptime : stat.start - stat.uptime) - if (seconds === 0) seconds = 1 // we sure can't divide through 0 + var cpu = seconds > 0 ? (total / seconds) * 100 : 0 history.set(stat, options.maxage) - var cpuPercent = Math.min(Math.round((total / seconds) * 100000) / 1000, 100.0) return done(null, { - cpu: cpuPercent, + cpu: Math.min(cpu, 100.0), memory: memory, - ctime: (stat.utime + stat.stime) / cpu.clockTick, + ctime: (stat.utime + stat.stime) / cpuInfo.clockTick, elapsed: date - (stat.start * 1000), timestamp: stat.start * 1000, // start date pid: pid, @@ -64,10 +63,10 @@ function readProcFile (pid, options, done) { } function procfile (pid, options, done) { - updateCpu(cpu, function (err, result) { + updateCpu(cpuInfo, function (err, result) { if (err) return done(err) - cpu = result + cpuInfo = result var fns = {} pid.forEach(function (id, i) { diff --git a/lib/ps.js b/lib/ps.js index fa7891b..b16a1b3 100644 --- a/lib/ps.js +++ b/lib/ps.js @@ -4,6 +4,7 @@ var os = require('os') var bin = require('./bin') var PLATFORM = os.platform() +var cpus = os.cpus().length function parseTime (timestr, fraction) { var time = 0 @@ -56,6 +57,7 @@ function ps (pids, options, done) { // ELAPSED: format is [[dd-]hh:]mm:ss // RSS: is counted as blocks of 1024 bytes // TIME: format is [[dd-]hh:]mm:ss + // %CPU: goes from 0 to vcore * 100 // // Refs: http://www.manpages.info/linux/ps.1.html // NB: The columns are returned in the order given inside the -o option @@ -70,6 +72,7 @@ function ps (pids, options, done) { // ELAPSED: format is [[dd-]hh:]mm:ss // RSS: is counted as blocks of 1024 bytes // TIME: format is [[dd-]hh:]ss:mm.pp (pp is the percentage of a minute) + // %CPU: goes from 0 to vcore * 100 // // Refs: https://ss64.com/osx/ps.html // NB: The columns are returned in the order given inside the -o option @@ -92,13 +95,13 @@ function ps (pids, options, done) { var pid = parseInt(line[1], 10) var ppid = parseInt(line[2], 10) - var cpu = parseFloat(line[3].replace(',', '.'), 10) + var cpu = parseFloat(line[3].replace(',', '.'), 10) / cpus var memory = parseInt(line[4], 10) * 1024 var etime = parseTime(line[0]) var ctime = parseTime(line[5], true) statistics[pid] = { - cpu: Math.min(Math.round(cpu * 1000) / 1000, 100.0), + cpu: Math.min(cpu, 100.0), memory: memory, ppid: ppid, pid: pid, diff --git a/lib/wmic.js b/lib/wmic.js index 0d75038..53aea46 100644 --- a/lib/wmic.js +++ b/lib/wmic.js @@ -99,15 +99,15 @@ function wmic (pids, options, done) { } // process usage since last call - var total = (kerneltime + usertime - hst.ctime) - // time elapsed between calls + var total = (kerneltime + usertime - hst.ctime) / 1000 + // time elapsed between calls in seconds var seconds = uptime - hst.uptime - var cpu = seconds > 0 ? (total / (seconds * 1000)) * 100 : 0 + var cpu = seconds > 0 ? (total / seconds) * 100 : 0 history.set(pid, {ctime: usertime + kerneltime, uptime: uptime}, options.maxage) statistics[pid] = { - cpu: Math.min(Math.round(cpu * 1000) / 1000, 100.0), + cpu: Math.min(cpu, 100.0), memory: memory, ppid: ppid, pid: pid, diff --git a/test/ps.js b/test/ps.js index 090b7a0..67afb92 100644 --- a/test/ps.js +++ b/test/ps.js @@ -38,8 +38,12 @@ test('should parse ps output on Darwin', async t => { spawn: () => mocks.spawn(stdout, '', null, 0, null) }) mockery.registerMock('os', { - EOL: os.EOL, platform: () => 'darwin', type: () => 'type', release: () => 'release'} - ) + EOL: os.EOL, + platform: () => 'darwin', + type: () => 'type', + release: () => 'release', + cpus: () => [os.cpus()[0]] + }) const ps = require('../lib/ps') @@ -99,8 +103,12 @@ test('should parse ps output on *nix', async t => { spawn: () => mocks.spawn(stdout, '', null, 0, null) }) mockery.registerMock('os', { - EOL: os.EOL, platform: () => 'linux', type: () => 'type', release: () => 'release'} - ) + EOL: os.EOL, + platform: () => 'linux', + type: () => 'type', + release: () => 'release', + cpus: () => [os.cpus()[0]] + }) const ps = require('../lib/ps') From 39fff7456408b380f4f90cc9123554e06d004cdb Mon Sep 17 00:00:00 2001 From: simonepri Date: Mon, 4 Jun 2018 09:50:17 +0200 Subject: [PATCH 3/4] Fix lint script --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 926211a..f353031 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "node": ">=4" }, "scripts": { - "lint": "standard index.js lib/**/*.js test/**/*.js", - "test": "npm run lint && nyc ava -m \"!*benchmark*\"", + "lint": "standard", + "test": "standard &&nyc ava -m \"!*benchmark*\"", "alpine": "docker run -v $(pwd):/var/pidusage pidusage:latest npm test", "coverage": "codecov", "bench": "ava -m \"*benchmark*\"" From aa013f69ad05f29c03cae53d9e82dbfb28ffd8b1 Mon Sep 17 00:00:00 2001 From: simonepri Date: Mon, 4 Jun 2018 09:52:22 +0200 Subject: [PATCH 4/4] Fix codestyle --- lib/procfile.js | 1 - test/integration.js | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/procfile.js b/lib/procfile.js index 8ba4386..408670e 100644 --- a/lib/procfile.js +++ b/lib/procfile.js @@ -49,7 +49,6 @@ function readProcFile (pid, options, done) { history.set(stat, options.maxage) - return done(null, { cpu: Math.min(cpu, 100.0), memory: memory, diff --git a/test/integration.js b/test/integration.js index c3c95b7..ac3bc86 100644 --- a/test/integration.js +++ b/test/integration.js @@ -43,8 +43,9 @@ test('should work with an array of pids', async t => { child.stderr.on('data', d => reject(d.toString())) child.on('error', reject) child.on('exit', reject) - }) - , 'script not executed') + }), + 'script not executed' + ) const pids = [ppid, pid] let result