From 5b76de2ae98192ab609a7070a19f01eb63b3ab49 Mon Sep 17 00:00:00 2001 From: polikeiji Date: Mon, 3 May 2021 13:00:48 +0900 Subject: [PATCH 1/3] [fix] Fix that RSS is never returned on Windows --- index.js | 15 ++++++++------- test/test.js | 9 +++++++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 6225d04..4c0316e 100755 --- a/index.js +++ b/index.js @@ -72,8 +72,14 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { } // Convert RSS to number of bytes - columns[4] = parseInt(columns[4], 10); - if (process.platform !== 'win32') { + if (process.platform == 'win32') { + // For Windows, WMIC.exe never returns any value for "Status" and it causes "WorkingSetSize" is set at columns[3]. + // See: https://docs.microsoft.com/ja-jp/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN + columns[4] = parseInt(columns[3], 10); + columns[3] = null; + } + else { + columns[4] = parseInt(columns[4], 10); columns[4] *= 1024; } @@ -122,19 +128,14 @@ function normalizeHeader(str) { switch (str) { case 'Name': return 'COMMAND'; - break; case 'ParentProcessId': return 'PPID'; - break; case 'ProcessId': return 'PID'; - break; case 'Status': return 'STAT'; - break; case 'WorkingSetSize': return 'RSS'; - break; default: throw new Error('Unknown process listing header: ' + str); } diff --git a/test/test.js b/test/test.js index 0ebb5f4..30b5e4c 100644 --- a/test/test.js +++ b/test/test.js @@ -9,6 +9,9 @@ var red = chalk.red, green = chalk.green, cyan = chalk.cyan; +var isWin = process.platform === 'win32'; +var isNumGreaterThanZero = (n) => !isNaN(parseInt(n, 10)) && n > 0; + var scripts = { parent: path.join(__dirname, 'exec', 'parent.js'), child: path.join(__dirname, 'exec', 'child.js') @@ -65,9 +68,11 @@ test(cyan('Includes itself it includeRoot is true'), function (t) { }; t.equal(current.PID, '' + process.pid, green('✓ Current PID is valid')); - t.equal(current.COMMAND, 'node', green('✓ Current COMM is node')); + t.equal(current.COMMAND, isWin ? 'node.exe' : 'node', green('✓ Current COMM is node')); + t.equal(isNumGreaterThanZero(current.RSS), true, green('✓ Current RSS is valid')); t.notEqual(other.PID, '' + process.pid, green('✓ Current PID is valid')); - t.equal(other.COMMAND, 'ps', green('✓ Current COMM is ps')); + t.equal(other.COMMAND, isWin ? 'WMIC.exe' : 'ps', green('✓ Current COMM is ps')); + t.equal(isNumGreaterThanZero(other.RSS), true, green('✓ Other RSS is valid')); t.end(); }); }); From ccfeeb72f6cdba81136998aabab28bbabb1fc133 Mon Sep 17 00:00:00 2001 From: polikeiji Date: Mon, 3 May 2021 15:18:19 +0900 Subject: [PATCH 2/3] [fix] Fix that RSS is never returned on Windows --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4c0316e..a530871 100755 --- a/index.js +++ b/index.js @@ -73,7 +73,7 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { // Convert RSS to number of bytes if (process.platform == 'win32') { - // For Windows, WMIC.exe never returns any value for "Status" and it causes "WorkingSetSize" is set at columns[3]. + // For Windows, WMIC.exe never returns any value for "Status" and it causes "WorkingSetSize" is set at columns[3] // See: https://docs.microsoft.com/ja-jp/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN columns[4] = parseInt(columns[3], 10); columns[3] = null; From 7046081f836bf40ba491d7ae676c8c44d6b988b9 Mon Sep 17 00:00:00 2001 From: polikeiji Date: Mon, 3 May 2021 23:52:26 +0900 Subject: [PATCH 3/3] [refactor] Omitted 'Status' in the WMIC call --- index.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a530871..aeed808 100755 --- a/index.js +++ b/index.js @@ -48,7 +48,7 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { var processLister; if (process.platform === 'win32') { // See also: https://github.com/nodejs/node-v0.x-archive/issues/2318 - processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,Status,WorkingSetSize']); + processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,WorkingSetSize']); } else { processLister = spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm,rss']); } @@ -73,10 +73,7 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { // Convert RSS to number of bytes if (process.platform == 'win32') { - // For Windows, WMIC.exe never returns any value for "Status" and it causes "WorkingSetSize" is set at columns[3] - // See: https://docs.microsoft.com/ja-jp/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN - columns[4] = parseInt(columns[3], 10); - columns[3] = null; + columns[3] = parseInt(columns[3], 10); } else { columns[4] = parseInt(columns[4], 10); @@ -90,6 +87,13 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) { row[h.shift()] = h.length ? columns.shift() : columns.join(' '); } + // For Windows, WMIC.exe never returns any value for "Status" which used to get value corresponding to "STAT" + // See: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN + // So just set "null" for the backward compatibility. + if (process.platform == 'win32') { + row['STAT'] = null; + } + return cb(null, row); }), es.writeArray(function (err, ps) {