Project

General

Profile

« Previous | Next » 

Revision 73b5c257

Added by Steve Beaver almost 8 years ago

Revise CPU load display to eliminate sleep in AJAX call

(cherry picked from commit c4ec696bf9eae3f8dbbad30b2e0804b9bf7de4b6)

View differences:

src/usr/local/www/includes/functions.inc.php
114 114
	return $uptimestr;
115 115
}
116 116

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

  
120 120
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
121 121
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
122
	sleep($duration);
123
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
124

  
125
	$totalStart = array_sum($cpuTicks);
126
	$totalEnd = array_sum($cpuTicks2);
127

  
128
	// Something wrapped ?!?!
129
	if ($totalEnd <= $totalStart) {
130
		return 0;
131
	}
132

  
133
	// Calculate total cycles used
134
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
135

  
136
	// Calculate the percentage used
137
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
138 122

  
139
	return $cpuUsage;
123
	return array_sum($cpuTicks) . "|" . $cpuTicks['idle'];
140 124
}
141 125

  
142 126
function get_pfstate($percent=false) {
src/usr/local/www/widgets/widgets/system_information.widget.php
452 452
					<div id="cpuPB" class="progress-bar progress-bar-striped" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
453 453
					</div>
454 454
				</div>
455

  
456
				<span id="cpumeter"></span>
455
				<span id="cpumeter"><?=sprintf(gettext("Retrieving CPU data %s"), "<i class=\"fa fa-gear fa-spin\"></i>")?></span>
457 456
			</td>
458 457
		</tr>
459 458
<?php
......
573 572
	set_widget_checkbox_events("#widget-<?=$widgetname?>_panel-footer [id^=show]", "showallsysinfoitems");
574 573
});
575 574

  
575
var lastTotal = 0;
576
var lastUsed = 0;
577

  
576 578
function setProgress(barName, percent) {
577 579
	$('#' + barName).css('width', percent + '%').attr('aria-valuenow', percent);
578 580
}
......
586 588
			return false;
587 589
	}))
588 590

  
589
	updateUptime(values[2]);
590
	updateDateTime(values[5]);
591
	updateCPU(values[0]);
592
	updateMemory(values[1]);
593
	updateState(values[3]);
594
	updateTemp(values[4]);
595
	updateInterfaceStats(values[6]);
596
	updateInterfaces(values[7]);
597
	updateCpuFreq(values[8]);
598
	updateLoadAverage(values[9]);
599
	updateMbuf(values[10]);
600
	updateMbufMeter(values[11]);
601
	updateStateMeter(values[12]);
591
	if (lastTotal === 0) {
592
		lastTotal = values[0];
593
		lastUsed = values[1];
594
	} else {
595
		updateCPU(values[0], values[1]);
596
	}
597

  
598
	updateUptime(values[3]);
599
	updateDateTime(values[6]);
600
	updateMemory(values[2]);
601
	updateState(values[4]);
602
	updateTemp(values[5]);
603
	updateCpuFreq(values[7]);
604
	updateLoadAverage(values[8]);
605
	updateMbuf(values[9]);
606
	updateMbufMeter(values[10]);
607
	updateStateMeter(values[11]);
602 608
}
603 609

  
604 610
function updateMemory(x) {
......
625 631
	}
626 632
}
627 633

  
628
function updateCPU(x) {
634
function updateCPU(total, used) {
635
	if ((lastTotal <= total) && (lastUsed <= used)) { // Just in case it wraps
636
		// Calculate the total ticks and the used ticks sine the last time it was checked
637
		var d_total = total - lastTotal;
638
		var d_used = used - lastUsed;
629 639

  
630
	if ($('#cpumeter')) {
631
		$("#cpumeter").html(x + '%');
632
	}
633
	if ($('#cpuPB')) {
634
		setProgress('cpuPB', parseInt(x));
635
	}
640
		// Convert to percent
641
		var x = Math.trunc( ((d_total - d_used)/d_total) * 100);
636 642

  
637
	/* Load CPU Graph widget if enabled */
638
	if (widgetActive('cpu_graphs')) {
639
		GraphValue(graph[0], x);
643
		if ($('#cpumeter')) {
644
			$('[id="cpumeter"]').html(x + '%');
645
		}
646

  
647
		if ($('#cpuPB')) {
648
			setProgress('cpuPB', parseInt(x));
649
		}
650

  
651
		/* Load CPU Graph widget if enabled */
652
		if (widgetActive('cpu_graphs')) {
653
			GraphValue(graph[0], x);
654
		}
640 655
	}
656

  
657
	// Update the saved "last" values
658
	lastTotal = total;
659
	lastUsed = used;
641 660
}
642 661

  
643 662
function updateTemp(x) {

Also available in: Unified diff