PHP Classes

CPU Load Calculator: Retrieve the CPU load level under Linux

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in Bookmark in
  Info   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2006-05-15 (9 years ago) RSS 2.0 feedStarStarStarStar 73%Total: 4,275 This week: 2All time: 645 This week: 691Down
Version License PHP version Categories  
cpuload 1.0GNU General Publi...3System information, Unix, Performance...
Description Author  

This class can be used to retrieve the CPU load level on a system running Linux.

The class retrieves the CPU load information from /proc/stat and returns the user, nice and system mode loads, the CPU load percentage, and the idle load percentage.

Innovation Award  
PHP Programming Innovation award nominee
June 2006
Number 7
The CPU load is a measure of how busy an operating system is.

If a system is too busy and you need to execute an heavy task that may not be prioritary, it may be better to hold on the task for a while until the system becomes less busy.

This class is capable of retrieving the current CPU load, and so it can be useful to make task scheduling decisions that depend on how busy the operating system is on the moment.

Manuel Lemos
Picture of Helmut Daschnigrum
Name: Helmut Daschnigrum <contact>
Classes: 6 packages by
Country: Canada Canada
Age: ???
All time rank: 1027 in Canada Canada
Week rank: 224 Down4 in Canada Canada Up
Innovation award
Innovation award
Nominee: 1x

class_CPULoad.php - CPU Load Class
Copyright 2001-2002, Steve Blinch (BlitzAffe Online,

Latest version available at:


  This script obtains CPU load percentage information from /proc/stat, allowing
  the server to display User, Nice, System, Idle, and CPU load percentages to


	The most important concept to note when using the CPULoad class is that the
	CPU load can only be calculated over a period of time.  For example, the
	script needs to check the current CPU load, wait for a second or two, then
	check the CPU load again to get an accurate reading.
	There are two ways to do this:
	1. When the script is called, it can pause for one or two seconds to get an
	accurate reading.  While this works, it tends to be cumbersome on busier
	sites, since the page takes one or two seconds longer to load.
	2. The script can save the CPU data to a file every time it runs, then
	refer back to it the next time it is executed.  This requires no delay in
	processing.  The only drawback is that the CPU load is calculated over the
	time period between page loads.  For example, if the page hasn't been
	visited for an hour, the next visitor will see the CPU load average for
	the last hour (rather than the current CPU load).
	Generally number 2 is the best method.

	The most commonly used functions of the CPULoad class are:
		This function corresponds to method number 1 above.  The CPULoad class
		checks the CPU load, waits for 1 second, then re-checks the CPU load.
		It then calculates the CPU load over that 1 second period.
		If you pass a value for $interval, this specifies the number of seconds
		to wait between checks.  Higher values make for more accurate readings,
		but will delay your script's execution.

		This function corresponds to method number 2 above.  The CPULoad class
		saves a small file in /tmp/ every time it is called, and uses it to
		remember the CPU load information.  It then calculates the average
		CPU load since the last time it was called.
		If you pass a value for $fastest_interval, this specifies the minimum
		number of seconds (between page loads) that must elapse before the CPU
		load is recalculated.  Generally, 4 is good for accurate values.
		This function outputs the calculated CPU load information.
	If you want access to the raw CPU load percentage information, the following
	variables are available:
		CPULoad->load["user"] - contains the User Mode load percentage
		CPULoad->load["nice"] - contains the Low Priority User Mode load percentage
		CPULoad->load["system"] - contains the System Mode load percentage
		CPULoad->load["idle"] - contains the Idle load percentage
		CPULoad->load["cpu"] - contains overall CPU load
		CPULoad->cached - contains the number of seconds remaining until the CPU
		load is recalculated (if using CPULoad::get_load()).

If these descriptions sound confusing, just try out the sample script included;
it's really simple to use.
  Files folder image Files  
File Role Description
Plain text file class_CPULoad.php Class Main class file
Accessible without login Plain text file LICENSE Lic. License terms
Accessible without login Plain text file README Doc. Documentation
Accessible without login Plain text file showload.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 0%Total:4,275All time:645
 This week:2This week:691Down
User Ratings User Comments (1)  
 All time