From 1188f0711f48f9f56b9fd574b4d34ab1d51c6abd Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 25 May 2022 10:04:37 -0700 Subject: [PATCH] Linux CPU utilization improvement, #4039 --- agents/modules_meshcmd/sysinfo.js | 35 ++++++++++++++++++------------ agents/modules_meshcore/sysinfo.js | 35 ++++++++++++++++++------------ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/agents/modules_meshcmd/sysinfo.js b/agents/modules_meshcmd/sysinfo.js index bb157ba3..02c355d9 100644 --- a/agents/modules_meshcmd/sysinfo.js +++ b/agents/modules_meshcmd/sysinfo.js @@ -106,32 +106,39 @@ function windows_memUtilization() return (ret); } -function linux_cpuUtilization() -{ +var cpuLastIdle = []; +var cpuLastSum = []; +function linux_cpuUtilization() { var ret = { cpus: [] }; var info = require('fs').readFileSync('/proc/stat'); var lines = info.toString().split('\n'); var columns; var x, y; - var sum, idle, utilization; - for (var i in lines) - { + var cpuNo = 0; + var currSum, currIdle, utilization; + for (var i in lines) { columns = lines[i].split(' '); if (!columns[0].startsWith('cpu')) { break; } - x = 0, sum = 0; + x = 0, currSum = 0; while (columns[++x] == ''); - for (y = x; y < columns.length; ++y) { sum += parseInt(columns[y]); } - idle = parseInt(columns[3 + x]); - utilization = (100 - ((idle / sum) * 100)); //.toFixed(2); - if (!ret.total) - { + for (y = x; y < columns.length; ++y) { currSum += parseInt(columns[y]); } + currIdle = parseInt(columns[3 + x]); + + var diffIdle = currIdle - cpuLastIdle[cpuNo]; + var diffSum = currSum - cpuLastSum[cpuNo]; + + utilization = (100 - ((diffIdle / diffSum) * 100)); + + cpuLastSum[cpuNo] = currSum; + cpuLastIdle[cpuNo] = currIdle; + + if (!ret.total) { ret.total = utilization; - } - else - { + } else { ret.cpus.push(utilization); } + ++cpuNo; } var p = new promise(function (res, rej) { this._res = res; this._rej = rej; }); diff --git a/agents/modules_meshcore/sysinfo.js b/agents/modules_meshcore/sysinfo.js index bb157ba3..02c355d9 100644 --- a/agents/modules_meshcore/sysinfo.js +++ b/agents/modules_meshcore/sysinfo.js @@ -106,32 +106,39 @@ function windows_memUtilization() return (ret); } -function linux_cpuUtilization() -{ +var cpuLastIdle = []; +var cpuLastSum = []; +function linux_cpuUtilization() { var ret = { cpus: [] }; var info = require('fs').readFileSync('/proc/stat'); var lines = info.toString().split('\n'); var columns; var x, y; - var sum, idle, utilization; - for (var i in lines) - { + var cpuNo = 0; + var currSum, currIdle, utilization; + for (var i in lines) { columns = lines[i].split(' '); if (!columns[0].startsWith('cpu')) { break; } - x = 0, sum = 0; + x = 0, currSum = 0; while (columns[++x] == ''); - for (y = x; y < columns.length; ++y) { sum += parseInt(columns[y]); } - idle = parseInt(columns[3 + x]); - utilization = (100 - ((idle / sum) * 100)); //.toFixed(2); - if (!ret.total) - { + for (y = x; y < columns.length; ++y) { currSum += parseInt(columns[y]); } + currIdle = parseInt(columns[3 + x]); + + var diffIdle = currIdle - cpuLastIdle[cpuNo]; + var diffSum = currSum - cpuLastSum[cpuNo]; + + utilization = (100 - ((diffIdle / diffSum) * 100)); + + cpuLastSum[cpuNo] = currSum; + cpuLastIdle[cpuNo] = currIdle; + + if (!ret.total) { ret.total = utilization; - } - else - { + } else { ret.cpus.push(utilization); } + ++cpuNo; } var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });