From b6dc889691d58e9da47c0130bdc9f447be7e2354 Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Wed, 22 Nov 2023 21:39:40 +0100 Subject: [PATCH 1/2] Add compatibility with ps from BusyBox --- lib/ps.js | 26 +++++++++++++------------- test/ps.js | 40 ++-------------------------------------- 2 files changed, 15 insertions(+), 51 deletions(-) diff --git a/lib/ps.js b/lib/ps.js index 6ac922f..5471efd 100644 --- a/lib/ps.js +++ b/lib/ps.js @@ -35,27 +35,16 @@ function parseTime (timestr, centisec) { * @param {Function} done(err, stat) */ function ps (pids, options, done) { - const pArg = pids.join(',') - let args = ['-o', 'etime,pid,ppid,pcpu,rss,time', '-p', pArg] + let args = ['-o', 'etime,pid,ppid,pcpu,rss,time'] if (PLATFORM === 'aix' || PLATFORM === 'os400') { - args = ['-o', 'etime,pid,ppid,pcpu,rssize,time', '-p', pArg] + args = ['-o', 'etime,pid,ppid,pcpu,rssize,time'] } bin('ps', args, function (err, stdout, code) { if (err) { - if (PLATFORM === 'os390' && /no matching processes found/.test(err)) { - err = new Error('No matching pid found') - err.code = 'ENOENT' - } - return done(err) } - if (code === 1) { - const error = new Error('No matching pid found') - error.code = 'ENOENT' - return done(error) - } if (code !== 0) { return done(new Error('pidusage ps command exited with code ' + code)) } @@ -102,6 +91,11 @@ function ps (pids, options, done) { } const pid = parseInt(line[1], 10) + + if (!pids.includes(pid)) { + continue + } + let hst = history.get(pid, options.maxage) if (hst === undefined) hst = {} @@ -128,6 +122,12 @@ function ps (pids, options, done) { history.set(pid, statistics[pid], options.maxage) } + if (Object.keys(statistics).length === 0) { + const error = new Error('No matching pid found') + error.code = 'ENOENT' + return done(error) + } + done(null, statistics) }) } diff --git a/test/ps.js b/test/ps.js index edbf477..c35b602 100644 --- a/test/ps.js +++ b/test/ps.js @@ -45,7 +45,7 @@ test('should parse ps output on Darwin', async t => { const ps = require('../lib/ps') - const result = await pify(ps)([348932], {}) + const result = await pify(ps)([430, 7166], {}) t.deepEqual(result, { 430: { cpu: (93784070 / 319853000) * 100, @@ -56,24 +56,6 @@ test('should parse ps output on Darwin', async t => { elapsed: (2 * 86400 + 40 * 3600 + 50 * 60 + 53 * 1) * 1000, timestamp: 864000000 }, - 432: { - cpu: (90123100 / 147053000) * 100, - memory: 2364 * 1024, - ppid: 430, - pid: 432, - ctime: (1 * 86400 + 1 * 3600 + 2 * 60 + 3 * 1) * 1000 + (10 * 10), - elapsed: (40 * 3600 + 50 * 60 + 53 * 1) * 1000, - timestamp: 864000000 - }, - 727: { - cpu: (867260 / 6650000) * 100, - memory: 348932 * 1024, - ppid: 1, - pid: 727, - ctime: (14 * 60 + 27 * 1) * 1000 + (10 * 26), - elapsed: (1 * 3600 + 50 * 60 + 50 * 1) * 1000, - timestamp: 864000000 - }, 7166: { cpu: (20 / 20000) * 100, memory: 3756 * 1024, @@ -110,7 +92,7 @@ test('should parse ps output on *nix', async t => { // // const ps = require('../lib/ps') // - // const result = await pify(ps)([11678], {}) + // const result = await pify(ps)([430, 7166], {}) // t.deepEqual(result, { // 430: { // cpu: 3.0, @@ -121,24 +103,6 @@ test('should parse ps output on *nix', async t => { // elapsed: (2 * 86400 + 40 * 3600 + 50 * 60 + 53 * 1) * 1000, // timestamp: 864000000 // }, - // 432: { - // cpu: 0.0, - // memory: 2364 * 1024, - // ppid: 430, - // pid: 432, - // ctime: (1 * 86400 + 1 * 3600 + 2 * 60 + 3 * 1) * 1000, - // elapsed: (40 * 3600 + 50 * 60 + 53 * 1) * 1000, - // timestamp: 864000000 - // }, - // 727: { - // cpu: 10.0, - // memory: 348932 * 1024, - // ppid: 1, - // pid: 727, - // ctime: (14 * 60 + 27 * 1) * 1000, - // elapsed: (1 * 3600 + 50 * 60 + 50 * 1) * 1000, - // timestamp: 864000000 - // }, // 7166: { // cpu: 0.1, // memory: 3756 * 1024, From 602d016fc97ad2ae3cc9fdbb95d10d355c0306e7 Mon Sep 17 00:00:00 2001 From: Pascal Jufer Date: Wed, 22 Nov 2023 21:50:21 +0100 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff447fd..55fca8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 3.0.2 + +- add compatibility with ps from busybox + ### 3.0.1 - removed dynamic requires to allow for bundling #154