PHP Classes

CPU Load Calculator: Retrieve the CPU load level under Linux

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 69%Total: 4,399 All time: 649 This week: 54Up
Version License PHP version Categories
cpuload 1.0.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: 1037 in Canada Canada
Week rank: 215 Down4 in Canada Canada Up
Innovation award
Innovation award
Nominee: 1x

Details

class_CPULoad.php - CPU Load Class Copyright 2001-2002, Steve Blinch (BlitzAffe Online, http://code.blitzaffe.com) Latest version available at: http://code.blitzaffe.com Contact: steve@blitzaffe.com DESCRIPTION This script obtains CPU load percentage information from /proc/stat, allowing the server to display User, Nice, System, Idle, and CPU load percentages to users. USAGE 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: CPULoad::sample_load($interval=1) 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. CPULoad::get_load($fastest_sample=4) 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. CPULoad::print_load() 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 (4)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:4,399
This week:0
All time:649
This week:54Up
User Ratings User Comments (1)
 All time
Utility:92%StarStarStarStarStar
Consistency:80%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:69%StarStarStarStar
Rank:314