mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-01-13 16:03:20 -05:00
Linux CPU utilization improvement, #4039
This commit is contained in:
parent
4e9daa4286
commit
1188f0711f
@ -106,32 +106,39 @@ function windows_memUtilization()
|
|||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
function linux_cpuUtilization()
|
var cpuLastIdle = [];
|
||||||
{
|
var cpuLastSum = [];
|
||||||
|
function linux_cpuUtilization() {
|
||||||
var ret = { cpus: [] };
|
var ret = { cpus: [] };
|
||||||
var info = require('fs').readFileSync('/proc/stat');
|
var info = require('fs').readFileSync('/proc/stat');
|
||||||
var lines = info.toString().split('\n');
|
var lines = info.toString().split('\n');
|
||||||
var columns;
|
var columns;
|
||||||
var x, y;
|
var x, y;
|
||||||
var sum, idle, utilization;
|
var cpuNo = 0;
|
||||||
for (var i in lines)
|
var currSum, currIdle, utilization;
|
||||||
{
|
for (var i in lines) {
|
||||||
columns = lines[i].split(' ');
|
columns = lines[i].split(' ');
|
||||||
if (!columns[0].startsWith('cpu')) { break; }
|
if (!columns[0].startsWith('cpu')) { break; }
|
||||||
|
|
||||||
x = 0, sum = 0;
|
x = 0, currSum = 0;
|
||||||
while (columns[++x] == '');
|
while (columns[++x] == '');
|
||||||
for (y = x; y < columns.length; ++y) { sum += parseInt(columns[y]); }
|
for (y = x; y < columns.length; ++y) { currSum += parseInt(columns[y]); }
|
||||||
idle = parseInt(columns[3 + x]);
|
currIdle = parseInt(columns[3 + x]);
|
||||||
utilization = (100 - ((idle / sum) * 100)); //.toFixed(2);
|
|
||||||
if (!ret.total)
|
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;
|
ret.total = utilization;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
ret.cpus.push(utilization);
|
ret.cpus.push(utilization);
|
||||||
}
|
}
|
||||||
|
++cpuNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||||
|
@ -106,32 +106,39 @@ function windows_memUtilization()
|
|||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
function linux_cpuUtilization()
|
var cpuLastIdle = [];
|
||||||
{
|
var cpuLastSum = [];
|
||||||
|
function linux_cpuUtilization() {
|
||||||
var ret = { cpus: [] };
|
var ret = { cpus: [] };
|
||||||
var info = require('fs').readFileSync('/proc/stat');
|
var info = require('fs').readFileSync('/proc/stat');
|
||||||
var lines = info.toString().split('\n');
|
var lines = info.toString().split('\n');
|
||||||
var columns;
|
var columns;
|
||||||
var x, y;
|
var x, y;
|
||||||
var sum, idle, utilization;
|
var cpuNo = 0;
|
||||||
for (var i in lines)
|
var currSum, currIdle, utilization;
|
||||||
{
|
for (var i in lines) {
|
||||||
columns = lines[i].split(' ');
|
columns = lines[i].split(' ');
|
||||||
if (!columns[0].startsWith('cpu')) { break; }
|
if (!columns[0].startsWith('cpu')) { break; }
|
||||||
|
|
||||||
x = 0, sum = 0;
|
x = 0, currSum = 0;
|
||||||
while (columns[++x] == '');
|
while (columns[++x] == '');
|
||||||
for (y = x; y < columns.length; ++y) { sum += parseInt(columns[y]); }
|
for (y = x; y < columns.length; ++y) { currSum += parseInt(columns[y]); }
|
||||||
idle = parseInt(columns[3 + x]);
|
currIdle = parseInt(columns[3 + x]);
|
||||||
utilization = (100 - ((idle / sum) * 100)); //.toFixed(2);
|
|
||||||
if (!ret.total)
|
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;
|
ret.total = utilization;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
ret.cpus.push(utilization);
|
ret.cpus.push(utilization);
|
||||||
}
|
}
|
||||||
|
++cpuNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
||||||
|
Loading…
Reference in New Issue
Block a user