src/pi/ui/CPULoad.cpp
float CPULoad::get() {
	// Update value only once per second
	if(timer.elapsed() < 1.0)
		return prevLoad;

	// read current cpu times
	std::ifstream proc_stat("/proc/stat");
	proc_stat.ignore(5, ' '); // skip prefix

	int index = 0;
	size_t time = 0, total = 0, idle = 0;
	while(proc_stat >> time) {
		if(index == 3)
			idle = time;
		total += time;
		index++;
	}

	// compute the cpu load in percentage
	float idleDelta = idle - prevIdleTime;
	float totalDelta = total - prevTotalTime;
	float load = 100 * (1.0f - idleDelta/totalDelta);

	// Get the average load over the last 2 measures
	float smoothLoad = (load + prevLoad) / 2;

	// save values for the next measure
	prevIdleTime = idle;
	prevTotalTime = total;
	prevLoad = load;
	timer.reset();

	return smoothLoad;
}
/** Return the cpu load in percentage, updated no more than once per second **/
Return the cpu load in percentage, updated no more than once per second
Return the cpu load in percentage updated no more than once per second
float CPULoad::get() {
	if(timer.elapsed() < 1.0)
		return prevLoad;
	std::ifstream proc_stat("/proc/stat");
	proc_stat.ignore(5, ' '); 
	int index = 0;
	size_t time = 0, total = 0, idle = 0;
	while(proc_stat >> time) {
		if(index == 3)
			idle = time;
		total += time;
		index++;
	}
	float idleDelta = idle - prevIdleTime;
	float totalDelta = total - prevTotalTime;
	float load = 100 * (1.0f - idleDelta/totalDelta);
	float smoothLoad = (load + prevLoad) / 2;
	prevIdleTime = idle;
	prevTotalTime = total;
	prevLoad = load;
	timer.reset();
	return smoothLoad;
}
float CPULoad :: get ( ) { if ( timer . elapsed ( ) < 1.0 ) return prevLoad ; std :: ifstream proc_stat ( " " ) ; proc_stat . ignore ( 5 , ' ' ) ; int index = 0 ; size_t time = 0 , total = 0 , idle = 0 ; while ( proc_stat >> time ) { if ( index == 3 ) idle = time ; total += time ; index ++ ; } float idleDelta = idle - prevIdleTime ; float totalDelta = total - prevTotalTime ; float load = 100 * ( 1.0f - idleDelta / totalDelta ) ; float smoothLoad = ( load + prevLoad ) / 2 ; prevIdleTime = idle ; prevTotalTime = total ; prevLoad = load ; timer . reset ( ) ; return smoothLoad ; }
Return the cpu load in percentage, updated no more than once per second
Return the cpu load in percentage updated no more than once per second
// Update value only once per second // read current cpu times // skip prefix // compute the cpu load in percentage // Get the average load over the last 2 measures // save values for the next measure