mem() refactoring parsing linux, code cleanup

This commit is contained in:
Sebastian Hildebrandt 2018-11-27 06:59:08 +01:00
parent ba14dab53a
commit fd2d81245c
3 changed files with 39 additions and 21 deletions

View File

@ -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 |

View File

@ -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);

View File

@ -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;