From c4ec696bf9eae3f8dbbad30b2e0804b9bf7de4b6 Mon Sep 17 00:00:00 2001 From: Steve Beaver Date: Thu, 17 Aug 2017 19:03:35 -0400 Subject: Revise CPU load display to eliminate sleep in AJAX call --- src/usr/local/www/includes/functions.inc.php | 22 +------ .../widgets/widgets/system_information.widget.php | 69 ++++++++++++++-------- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/usr/local/www/includes/functions.inc.php b/src/usr/local/www/includes/functions.inc.php index 23012bd..b53d2b4 100644 --- a/src/usr/local/www/includes/functions.inc.php +++ b/src/usr/local/www/includes/functions.inc.php @@ -114,29 +114,13 @@ function get_uptime() { return $uptimestr; } -/* Calculates non-idle CPU time and returns as a percentage */ +// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that function cpu_usage() { - $duration = 1; + $diff = array('user', 'nice', 'sys', 'intr', 'idle'); $cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); - sleep($duration); - $cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); - - $totalStart = array_sum($cpuTicks); - $totalEnd = array_sum($cpuTicks2); - - // Something wrapped ?!?! - if ($totalEnd <= $totalStart) { - return 0; - } - - // Calculate total cycles used - $totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']); - - // Calculate the percentage used - $cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart))); - return $cpuUsage; + return array_sum($cpuTicks) . "|" . $cpuTicks['idle']; } function get_pfstate($percent=false) { 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 000f33f..d32f144 100644 --- a/src/usr/local/www/widgets/widgets/system_information.widget.php +++ b/src/usr/local/www/widgets/widgets/system_information.widget.php @@ -452,8 +452,7 @@ $rows_displayed = false;
- - + ")?> _panel-footer [id^=show]", "showallsysinfoitems"); }); +var lastTotal = 0; +var lastUsed = 0; + function setProgress(barName, percent) { $('#' + barName).css('width', percent + '%').attr('aria-valuenow', percent); } @@ -586,19 +588,23 @@ function stats(x) { return false; })) - updateUptime(values[2]); - updateDateTime(values[5]); - updateCPU(values[0]); - updateMemory(values[1]); - updateState(values[3]); - updateTemp(values[4]); - updateInterfaceStats(values[6]); - updateInterfaces(values[7]); - updateCpuFreq(values[8]); - updateLoadAverage(values[9]); - updateMbuf(values[10]); - updateMbufMeter(values[11]); - updateStateMeter(values[12]); + 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) { @@ -625,19 +631,32 @@ function updateMbufMeter(x) { } } -function updateCPU(x) { +function updateCPU(total, used) { + if ((lastTotal <= total) && (lastUsed <= used)) { // Just in case it wraps + // 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; - if ($('#cpumeter')) { - $("#cpumeter").html(x + '%'); - } - if ($('#cpuPB')) { - setProgress('cpuPB', parseInt(x)); - } + // 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); + } } + + // Update the saved "last" values + lastTotal = total; + lastUsed = used; } function updateTemp(x) { -- cgit v1.1