sys_getloadavg()
In this article, we will focus on the PHP sys_getloadavg() function. We will provide you with an overview of the function, how it works, and examples of its
This article covers the PHP sys_getloadavg() function, including an overview, how it works, and usage examples.
Introduction to the sys_getloadavg() function
The sys_getloadavg() function is a built-in PHP function that retrieves the system load average. Note that it is only available on Unix-like systems (Linux, macOS) and will not work on Windows, where it returns false. It can be used to monitor system performance and optimize resource allocation.
The function takes no arguments and returns an array containing three floating-point numbers — the 1, 5, and 15 minute load averages of the system:
sys_getloadavg(): arrayWhat "load average" actually means
The load average is the average number of processes that are either running on the CPU or waiting for it (or for disk I/O) over a given window. The three values let you see a trend:
$load[0]— load over the last 1 minute (most recent, most volatile).$load[1]— load over the last 5 minutes.$load[2]— load over the last 15 minutes (smoothest, best for trends).
A rough rule of thumb: divide the load by the number of CPU cores. A 1-minute load of 4.0 on a 4-core machine means the CPU is roughly fully utilized; the same value on a 1-core machine means it is heavily overloaded. This is why you almost always normalize the load against the core count before acting on it.
How to use the sys_getloadavg() function
Using the sys_getloadavg() function is straightforward. It returns an array of load averages, which should be checked for length before accessing specific indices. Here is an example:
How to use the sys_getloadavg() function
In this example, we call sys_getloadavg() and assign the returned array to $load. We verify the array contains at least three elements before outputting the 1, 5, and 15 minute load averages.
Normalizing load against CPU cores
Because a raw load number is meaningless without knowing how many cores the machine has, a practical health check divides the load by the core count and compares it to a threshold. You can read the core count from /proc/cpuinfo on Linux (or nproc):
<?php
function cpuCoreCount(): int
{
// Linux: count processor entries in /proc/cpuinfo
if (is_readable('/proc/cpuinfo')) {
$cpuinfo = file_get_contents('/proc/cpuinfo');
return max(1, substr_count($cpuinfo, 'processor'));
}
// Fallback for other systems
return (int) (shell_exec('nproc') ?: 1);
}
$load = sys_getloadavg();
$cores = cpuCoreCount();
$perCore = $load[0] / $cores;
if ($perCore > 1.0) {
echo "WARNING: system is overloaded (" . round($perCore, 2) . " per core)\n";
} else {
echo "OK: load per core is " . round($perCore, 2) . "\n";
}
?>Here $load[0] / $cores converts the absolute load into a per-core figure: a value above 1.0 means there are more runnable processes than cores, so work is queuing.
Performance considerations
The sys_getloadavg() function is a useful tool for monitoring system performance. However, it reads directly from the OS kernel and is not computationally expensive. In high-traffic web applications, it is still best practice to avoid calling it on every request to minimize unnecessary overhead. Use it for periodic monitoring or diagnostic checks rather than in tight loops or performance-critical sections.
Conclusion
In conclusion, sys_getloadavg() provides a quick way to retrieve system load averages on Unix-like systems. By normalizing the result against the number of CPU cores and checking the returned array, you can effectively monitor system performance.
For other diagnostic and timing helpers, see microtime() for high-resolution timing, time() for the current Unix timestamp, and syslog() for sending the resulting alerts to the system logger. To inspect the returned array itself, count() is used in the examples above.