From fd2d81245cc4afe47c385e519574d834c8b8cc4b Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Tue, 27 Nov 2018 06:59:08 +0100 Subject: [PATCH] mem() refactoring parsing linux, code cleanup --- CHANGELOG.md | 1 + lib/memory.js | 58 +++++++++++++++++++++++++++++++----------------- lib/processes.js | 1 - 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b1097..5553d53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -100,6 +100,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.51.3 | 2018-11-27 | `mem()` refactoring parsing linux, code cleanup | | 3.51.2 | 2018-11-26 | `mem()` bugfix parsing `free` output linux | | 3.51.1 | 2018-11-26 | `processLoad()` bugfix windows | | 3.51.0 | 2018-11-25 | `processLoad()` added for windows | diff --git a/lib/memory.js b/lib/memory.js index 333c5af..28bc291 100644 --- a/lib/memory.js +++ b/lib/memory.js @@ -89,6 +89,20 @@ const OSX_RAM_manufacturers = { // // Reference: http://www.software-architect.net/blog/article/date/2015/06/12/-826c6e5052.html +// /procs/meminfo - sample (all in kB) +// +// MemTotal: 32806380 kB +// MemFree: 19220948 kB +// MemAvailable: 20851100 kB +// Buffers: 532892 kB +// Cached: 1935000 kB +// SwapCached: 0 kB +// Active: 11953672 kB +// Inactive: 1069288 kB +// SwapTotal: 16768892 kB +// SwapFree: 16768892 kB + + function mem(callback) { return new Promise((resolve) => { @@ -109,32 +123,36 @@ function mem(callback) { }; if (_linux) { - exec('export LC_ALL=C; free -b ; unset LC_ALL', function (error, stdout) { + exec('export LC_ALL=C; cat /proc/meminfo ; unset LC_ALL', function (error, stdout) { if (!error) { const lines = stdout.toString().split('\n'); - console.log(lines); - const parsedHeads = util.parseHead(lines[0], 100); - parsedHeads[0].from = 6; - let mem = lines[1]; - result.total = parseInt(mem.substring(parsedHeads[0].from - 1, parsedHeads[0].to), 10); - result.free = parseInt(mem.substring(parsedHeads[2].from - 1, parsedHeads[2].to), 10); + result.total = parseInt(util.getValue(lines, 'memtotal'), 10); + result.total = result.total ? result.total * 1024 : os.totalmem(); + result.free = parseInt(util.getValue(lines, 'memfree'), 10); + result.free = result.free ? result.free * 1024 : os.freemem(); result.used = result.total - result.free; - if (lines.length === 4) { // free (since free von procps-ng 3.3.10) - result.buffcache = parseInt(mem.substring(parsedHeads[4].from - 1, parsedHeads[4].to), 10); - result.available = parseInt(mem.substring(parsedHeads[5].from - 1, parsedHeads[5].to), 10); - mem = lines[2]; - } else { // free (older versions) - result.buffcache = parseInt(mem.substring(parsedHeads[4].from - 1, parsedHeads[4].to), 10) + parseInt(mem.substring(parsedHeads[5].from - 1, parsedHeads[5].to), 10); - result.available = result.free + result.buffcache; - mem = lines[3]; - } - result.active = result.total - result.free - result.buffcache; + let buffers = parseInt(util.getValue(lines, 'buffers'), 10); + buffers = buffers ? buffers * 1024 : 0; + let cached = parseInt(util.getValue(lines, 'cached'), 10); + cached = cached ? cached * 1024 : 0; + result.buffcache = buffers + cached; - result.swaptotal = parseInt(mem.substring(parsedHeads[0].from - 1, parsedHeads[0].to), 10); - result.swapfree = parseInt(mem.substring(parsedHeads[2].from - 1, parsedHeads[2].to), 10); - result.swapused = parseInt(mem.substring(parsedHeads[1].from - 1, parsedHeads[1].to), 10); + result.available = parseInt(util.getValue(lines, 'memavailable'), 10); + result.available = result.available ? result.available * 1024 : os.freemem(); + // result.active = result.total - result.free - result.buffcache; + + result.active = parseInt(util.getValue(lines, 'active'), 10); + result.active = result.active ? result.active * 1024 : 0; + result.buffcache = result.total - result.free - result.active; + + result.swaptotal = parseInt(util.getValue(lines, 'swaptotal'), 10); + result.swaptotal = result.swaptotal ? result.swaptotal * 1024 : 0; + result.swapfree = parseInt(util.getValue(lines, 'swapfree'), 10); + result.swapfree = result.swapfree ? result.swapfree * 1024 : 0; + result.swapused = parseInt(util.getValue(lines, 'swapcached'), 10); + result.swapused = result.swapused ? result.swapused * 1024 : 0; } if (callback) { callback(result); } resolve(result); diff --git a/lib/processes.js b/lib/processes.js index 6407832..8ff59f3 100644 --- a/lib/processes.js +++ b/lib/processes.js @@ -792,7 +792,6 @@ function processLoad(proc, callback) { exec(util.getWmic() + ' process get /value', util.execOptsWin, function (error, stdout) { if (!error) { let processSections = stdout.split(/\n\s*\n/); - let procs = []; let procStats = []; let list_new = {}; let allcpuu = 0;