From acd2f3120c608317a6742a376156bdca4efb71da Mon Sep 17 00:00:00 2001 From: Steve Beaver Date: Thu, 17 Aug 2017 11:04:59 -0400 Subject: Re-write CPU usage calculation to avoid sleep in AJAX call --- .../widgets/widgets/system_information.widget.php | 67 ++++++++++++++-------- 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'src/usr/local/www/widgets/widgets') diff --git a/src/usr/local/www/widgets/widgets/system_information.widget.php b/src/usr/local/www/widgets/widgets/system_information.widget.php index 8249ebd..ae38c56 100644 --- a/src/usr/local/www/widgets/widgets/system_information.widget.php +++ b/src/usr/local/www/widgets/widgets/system_information.widget.php @@ -389,7 +389,7 @@ $rows_displayed = false; - + ")?> -var update_interval = ""; +var lastTotal = 0; +var lastUsed = 0; function setProgress(barName, percent) { $('[id="' + barName + '"]').css('width', percent + '%').attr('aria-valuenow', percent); @@ -523,17 +524,23 @@ function stats(x) { return false; })) - updateUptime(values[2]); - updateDateTime(values[5]); - updateCPU(values[0]); - updateMemory(values[1]); - updateState(values[3]); - updateTemp(values[4]); - updateCpuFreq(values[6]); - updateLoadAverage(values[7]); - updateMbuf(values[8]); - updateMbufMeter(values[9]); - updateStateMeter(values[10]); + if (lastTotal === 0) { + lastTotal = values[0]; + lastUsed = values[1]; + } else { + updateCPU(values[0], values[1]); + } + + updateUptime(values[3]); + updateDateTime(values[6]); + updateMemory(values[2]); + updateState(values[4]); + updateTemp(values[5]); + updateCpuFreq(values[7]); + updateLoadAverage(values[8]); + updateMbuf(values[9]); + updateMbufMeter(values[10]); + updateStateMeter(values[11]); } function updateMemory(x) { @@ -560,19 +567,33 @@ function updateMbufMeter(x) { } } -function updateCPU(x) { +function updateCPU(total, used) { - if ($('#cpumeter')) { - $('[id="cpumeter"]').html(x + '%'); - } - if ($('#cpuPB')) { - setProgress('cpuPB', parseInt(x)); - } + // Just in case it wraps + if ((lastTotal <= total) && (lastUsed <= used)) { + // Calculate the total ticks and the used ticks sine the last time it was checked + var d_total = total - lastTotal; + var d_used = used - lastUsed; + + // Convert to percent + var x = Math.trunc( ((d_total - d_used)/d_total) * 100); - /* Load CPU Graph widget if enabled */ - if (widgetActive('cpu_graphs')) { - GraphValue(graph[0], x); + if ($('#cpumeter')) { + $('[id="cpumeter"]').html(x + '%'); + } + + if ($('#cpuPB')) { + setProgress('cpuPB', parseInt(x)); + } + + /* Load CPU Graph widget if enabled */ + if (widgetActive('cpu_graphs')) { + GraphValue(graph[0], x); + } } + + lastTotal = total; + lastUsed = used; } function updateTemp(x) { -- cgit v1.1