-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
537ec94
commit 50606e1
Showing
6 changed files
with
55 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,3 @@ language: node_js | |
node_js: | ||
- '10' | ||
- '8' | ||
- '6' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,60 @@ | ||
'use strict'; | ||
const util = require('util'); | ||
const path = require('path'); | ||
const childProcess = require('child_process'); | ||
const pify = require('pify'); | ||
|
||
const TEN_MEGABYTES = 1000 * 1000 * 10; | ||
const execFile = util.promisify(childProcess.execFile); | ||
|
||
function win() { | ||
const windows = async () => { | ||
// Source: https://github.com/MarkTiedemann/fastlist | ||
const bin = path.join(__dirname, 'fastlist.exe'); | ||
|
||
return pify(childProcess.execFile)(bin, {maxBuffer: TEN_MEGABYTES}) | ||
.then(stdout => | ||
stdout.trim() | ||
.split('\r\n') | ||
.map(line => line.split('\t')) | ||
.map(([name, pid, ppid]) => ({ | ||
name, | ||
pid: Number.parseInt(pid, 10), | ||
ppid: Number.parseInt(ppid, 10) | ||
})) | ||
); | ||
} | ||
|
||
function def(options = {}) { | ||
const ret = {}; | ||
const {stdout} = await execFile(bin, {maxBuffer: TEN_MEGABYTES}); | ||
|
||
return stdout | ||
.trim() | ||
.split('\r\n') | ||
.map(line => line.split('\t')) | ||
.map(([name, pid, ppid]) => ({ | ||
name, | ||
pid: Number.parseInt(pid, 10), | ||
ppid: Number.parseInt(ppid, 10) | ||
})); | ||
}; | ||
|
||
const main = async (options = {}) => { | ||
const flags = (options.all === false ? '' : 'a') + 'wwxo'; | ||
const ret = {}; | ||
|
||
return Promise.all(['comm', 'args', 'ppid', '%cpu', '%mem'].map(cmd => { | ||
return pify(childProcess.execFile)('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES}).then(stdout => { | ||
for (let line of stdout.trim().split('\n').slice(1)) { | ||
line = line.trim(); | ||
const [pid] = line.split(' ', 1); | ||
const val = line.slice(pid.length + 1).trim(); | ||
await Promise.all(['comm', 'args', 'ppid', '%cpu', '%mem'].map(async cmd => { | ||
const {stdout} = await execFile('ps', [flags, `pid,${cmd}`], {maxBuffer: TEN_MEGABYTES}); | ||
|
||
if (ret[pid] === undefined) { | ||
ret[pid] = {}; | ||
} | ||
for (let line of stdout.trim().split('\n').slice(1)) { | ||
line = line.trim(); | ||
const [pid] = line.split(' ', 1); | ||
const val = line.slice(pid.length + 1).trim(); | ||
|
||
ret[pid][cmd] = val; | ||
if (ret[pid] === undefined) { | ||
ret[pid] = {}; | ||
} | ||
}); | ||
})).then(() => { | ||
// Filter out inconsistencies as there might be race | ||
// issues due to differences in `ps` between the spawns | ||
// TODO: Use `Object.entries` when targeting Node.js 8 | ||
return Object.keys(ret).filter(x => ret[x].comm && ret[x].args && ret[x].ppid && ret[x]['%cpu'] && ret[x]['%mem']).map(x => { | ||
return { | ||
pid: Number.parseInt(x, 10), | ||
name: path.basename(ret[x].comm), | ||
cmd: ret[x].args, | ||
ppid: Number.parseInt(ret[x].ppid, 10), | ||
cpu: Number.parseFloat(ret[x]['%cpu']), | ||
memory: Number.parseFloat(ret[x]['%mem']) | ||
}; | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = process.platform === 'win32' ? win : def; | ||
|
||
ret[pid][cmd] = val; | ||
} | ||
})); | ||
|
||
// Filter out inconsistencies as there might be race | ||
// issues due to differences in `ps` between the spawns | ||
return Object.entries(ret) | ||
.filter(([, value]) => value.comm && value.args && value.ppid && value['%cpu'] && value['%mem']) | ||
.map(([key, value]) => ({ | ||
pid: Number.parseInt(key, 10), | ||
name: path.basename(value.comm), | ||
cmd: value.args, | ||
ppid: Number.parseInt(value.ppid, 10), | ||
cpu: Number.parseFloat(value['%cpu']), | ||
memory: Number.parseFloat(value['%mem']) | ||
})); | ||
}; | ||
|
||
module.exports = process.platform === 'win32' ? windows : main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters