Project

General

Profile

« Previous | Next » 

Revision acd2f312

Added by Steve Beaver almost 8 years ago

Re-write CPU usage calculation to avoid sleep in AJAX call

View differences:

src/usr/local/www/includes/functions.inc.php
79 79
	return $uptimestr;
80 80
}
81 81

  
82
/* Calculates non-idle CPU time and returns as a percentage */
82
// Returns the current total ticks and user ticks. The dashboard widget calculates the load from that
83 83
function cpu_usage() {
84
	$duration = 1;
84

  
85 85
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
86 86
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
87
	sleep($duration);
88
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
89

  
90
	$totalStart = array_sum($cpuTicks);
91
	$totalEnd = array_sum($cpuTicks2);
92

  
93
	// Something wrapped ?!?!
94
	if ($totalEnd <= $totalStart) {
95
		return 0;
96
	}
97

  
98
	// Calculate total cycles used
99
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
100

  
101
	// Calculate the percentage used
102
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
103 87

  
104
	return $cpuUsage;
88
	return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
105 89
}
106 90

  
107 91
function get_pfstate($percent=false) {
src/usr/local/www/widgets/widgets/system_information.widget.php
389 389
					</div>
390 390
				</div>
391 391
				<?php $update_period = (!empty($config['widgets']['period'])) ? $config['widgets']['period'] : "10"; ?>
392
				<span id="cpumeter"><?=sprintf(gettext("Updating in %s seconds"), $update_period)?></span>
392
				<span id="cpumeter"><?=sprintf(gettext("Retrieving CPU data %s"), "<i class=\"fa fa-gear fa-spin\"></i>")?></span>
393 393
			</td>
394 394
		</tr>
395 395
<?php
......
508 508
//<![CDATA[
509 509
<?php if ($widget_first_instance): ?>
510 510

  
511
var update_interval = "<?=$widgetperiod?>";
511
var lastTotal = 0;
512
var lastUsed = 0;
512 513

  
513 514
function setProgress(barName, percent) {
514 515
	$('[id="' + barName + '"]').css('width', percent + '%').attr('aria-valuenow', percent);
......
523 524
			return false;
524 525
	}))
525 526

  
526
	updateUptime(values[2]);
527
	updateDateTime(values[5]);
528
	updateCPU(values[0]);
529
	updateMemory(values[1]);
530
	updateState(values[3]);
531
	updateTemp(values[4]);
532
	updateCpuFreq(values[6]);
533
	updateLoadAverage(values[7]);
534
	updateMbuf(values[8]);
535
	updateMbufMeter(values[9]);
536
	updateStateMeter(values[10]);
527
	if (lastTotal === 0) {
528
		lastTotal = values[0];
529
		lastUsed = values[1];
530
	} else {
531
		updateCPU(values[0], values[1]);
532
	}
533

  
534
	updateUptime(values[3]);
535
	updateDateTime(values[6]);
536
	updateMemory(values[2]);
537
	updateState(values[4]);
538
	updateTemp(values[5]);
539
	updateCpuFreq(values[7]);
540
	updateLoadAverage(values[8]);
541
	updateMbuf(values[9]);
542
	updateMbufMeter(values[10]);
543
	updateStateMeter(values[11]);
537 544
}
538 545

  
539 546
function updateMemory(x) {
......
560 567
	}
561 568
}
562 569

  
563
function updateCPU(x) {
570
function updateCPU(total, used) {
564 571

  
565
	if ($('#cpumeter')) {
566
		$('[id="cpumeter"]').html(x + '%');
567
	}
568
	if ($('#cpuPB')) {
569
		setProgress('cpuPB', parseInt(x));
570
	}
572
	// Just in case it wraps
573
	if ((lastTotal <= total) && (lastUsed <= used)) {
574
		// Calculate the total ticks and the used ticks sine the last time it was checked
575
		var d_total = total - lastTotal;
576
		var d_used = used - lastUsed;
577

  
578
		// Convert to percent
579
		var x = Math.trunc( ((d_total - d_used)/d_total) * 100);
571 580

  
572
	/* Load CPU Graph widget if enabled */
573
	if (widgetActive('cpu_graphs')) {
574
		GraphValue(graph[0], x);
581
		if ($('#cpumeter')) {
582
			$('[id="cpumeter"]').html(x + '%');
583
		}
584

  
585
		if ($('#cpuPB')) {
586
			setProgress('cpuPB', parseInt(x));
587
		}
588

  
589
		/* Load CPU Graph widget if enabled */
590
		if (widgetActive('cpu_graphs')) {
591
			GraphValue(graph[0], x);
592
		}
575 593
	}
594

  
595
	lastTotal = total;
596
	lastUsed = used;
576 597
}
577 598

  
578 599
function updateTemp(x) {

Also available in: Unified diff