note 126283 added to function.sys-getloadavg
| From: | ChrisWheeler at main dot php dot net | Date: | Wed, 28 Jul 2021 22:28:45 +0000 |
| Subject: | note 126283 added to function.sys-getloadavg | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216674@lists.php.net to get a copy of this message | ||
It is important to note that sys_getloadavg() does not return a percentage, and the numbers that it
returns are completely meaningless if you do not know the number of CPU cores. To get the number as
a percentage you need to divide the value by the number of CPU cores in the system. (eg: if the
value is 0.5 and you have two cores, then you are using 25% of the CPU on average). Here is a simple
function to get the values as a percent:
<?php
function percentloadavg(){
$cpu_count = 1;
if(is_file('/proc/cpuinfo')) {
$cpuinfo = file_get_contents('/proc/cpuinfo');
preg_match_all('/^processor/m', $cpuinfo, $matches);
$cpu_count = count($matches[0]);
}
$sys_getloadavg = sys_getloadavg();
$sys_getloadavg[0] = $sys_getloadavg[0] / $cpu_count;
$sys_getloadavg[1] = $sys_getloadavg[1] / $cpu_count;
$sys_getloadavg[2] = $sys_getloadavg[2] / $cpu_count;
return $sys_getloadavg;
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 98.188.247.34)
----
Manual Page -- https://php.net/manual/en/function.sys-getloadavg.php
Edit -- https://main.php.net/note/edit/126283
Del: integrated -- https://main.php.net/note/delete/126283/integrated
Del: useless -- https://main.php.net/note/delete/126283/useless
Del: bad code -- https://main.php.net/note/delete/126283/bad+code
Del: spam -- https://main.php.net/note/delete/126283/spam
Del: non-english -- https://main.php.net/note/delete/126283/non-english
Del: in docs -- https://main.php.net/note/delete/126283/in+docs
Del: other reasons-- https://main.php.net/note/delete/126283
Reject -- https://main.php.net/note/reject/126283
Search -- https://main.php.net/manage/user-notes.php