summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSteve Beaver <sbeaver@netgate.com>2017-08-17 11:04:59 -0400
committerSteve Beaver <sbeaver@netgate.com>2017-08-17 11:05:33 -0400
commitacd2f3120c608317a6742a376156bdca4efb71da (patch)
tree07f2482088fdc4db2ae831a9a4ebeaf537c2c5ae /src
parentcc28e9b1d39eaf64e28702b963dde7a6132a4691 (diff)
downloadpfsense-acd2f3120c608317a6742a376156bdca4efb71da.zip
pfsense-acd2f3120c608317a6742a376156bdca4efb71da.tar.gz
Re-write CPU usage calculation to avoid sleep in AJAX call
Diffstat (limited to 'src')
-rw-r--r--src/usr/local/www/includes/functions.inc.php22
-rw-r--r--src/usr/local/www/widgets/widgets/system_information.widget.php67
2 files changed, 47 insertions, 42 deletions
diff --git a/src/usr/local/www/includes/functions.inc.php b/src/usr/local/www/includes/functions.inc.php
index 43723a3..664ea24 100644
--- a/src/usr/local/www/includes/functions.inc.php
+++ b/src/usr/local/www/includes/functions.inc.php
@@ -79,29 +79,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 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;
</div>
</div>
<?php $update_period = (!empty($config['widgets']['period'])) ? $config['widgets']['period'] : "10"; ?>
- <span id="cpumeter"><?=sprintf(gettext("Updating in %s seconds"), $update_period)?></span>
+ <span id="cpumeter"><?=sprintf(gettext("Retrieving CPU data %s"), "<i class=\"fa fa-gear fa-spin\"></i>")?></span>
</td>
</tr>
<?php
@@ -508,7 +508,8 @@ $rows_displayed = false;
//<![CDATA[
<?php if ($widget_first_instance): ?>
-var update_interval = "<?=$widgetperiod?>";
+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) {
OpenPOWER on IntegriCloud