downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

time_nanosleep> <sleep
[edit] Last updated: Fri, 17 May 2013

view this page in

sys_getloadavg

(PHP 5 >= 5.1.3)

sys_getloadavgOrtalama sistem yükünü döndürür

Açıklama

array sys_getloadavg ( void )

Son 1., 5. ve 15. dakikada ölçülen ortalama sistem yükünü (sistemde çalışmakta olan süreç sayısını) gösteren üç değer döndürür.

Dönen Değerler

Son 1., 5. ve 15. dakikada alınan örnekleri içeren bir dizi döndürür.

Örnekler

Örnek 1 - sys_getloadavg() örneği

<?php
$load 
sys_getloadavg();
if (
$load[0] > 80) {
    
header('HTTP/1.1 503 Too busy, try again later');
    die(
'Sunucu çok meşgul. Lütfen daha sonra tekrar deneyin.');
}
?>

Notlar

Bilginize: Bu işlev Windows sistemlerinde çalışmaz.



time_nanosleep> <sleep
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes sys_getloadavg - [7 notes]
up
2
vladvarna at gmail dot com
10 months ago
You can emulate loadavg using this. Can also be used to get iowait

<?php
  
function ProcStats()
   {   
      
$fp=fopen("/proc/stat","r");
       if(
false===$fp)
               return
false;
      
$a=explode(' ',fgets($fp));
      
array_shift($a); //get rid of 'cpu'
      
while(!$a[0])
          
array_shift($a); //get rid of ' '
      
var_dump($a);
      
fclose($fp);
       return
$a;
   }

  
$a=ProcStats();
  
sleep(5);
  
$b=ProcStats();

  
  
$total=array_sum($b)-array_sum($a);
  
  
$loadavg = round(100* (($b[0]+$b[1]+$b[2]) - ($a[0]+$a[1]+$a[2])) / $total, 2); // user+nice+system
  
$iowait= round(100* ($b[4] - $a[4])/$total,2);

?>
up
2
scott at corelevel dot com
6 years ago
I was having a problem with a large script I need to run - was a loop through about 50,000 records and downloading several pictures for a bunch of them, and updating the database.

the problem came as I started getting visitors to my site, the server would get behind, run out of memory, iowait skyrockets, mysql slows down... was a total downhill spiral.

Use this to fix it.

$load = sys_getloadavg();
$sleep=5;
$maxload=2;
if ($load[0] > $maxload) {
       sleep($sleep);
    echo "Busy server - sleep $sleep seconds<br>";
}

I have to play with the load and the sleep number to find what worked for my script, but now my server does not bog at all.
up
2
tom pittlik
7 years ago
The code below mimics the output of sys_getloadavg(). You may have to tweak the way the substring is captured for different distros.

<?

function sys_getloadavg_hack()
{
    $str = substr(strrchr(shell_exec("uptime"),":"),1);
    $avs = array_map("trim",explode(",",$str));

    return $avs;
}

print_r(sys_getloadavg_hack());

// Array
// (
//     [0] => 6.24
//     [1] => 4.92
//     [2] => 3.99
// )

?>

This function is a neat way of running low priority or non-essential cron jobs on a busy server - if the load is high, don't continue with the task (and try again in a few minutes time).
up
1
rick at rctonline dot nl
1 year ago
Here is another one that also works on windows. Note that this method is not fast, so be careful in the number of calls to this function.

<?php
function get_server_load() {
   
        if (
stristr(PHP_OS, 'win')) {
       
           
$wmi = new COM("Winmgmts://");
           
$server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor");
           
           
$cpu_num = 0;
           
$load_total = 0;
           
            foreach(
$server as $cpu){
               
$cpu_num++;
               
$load_total += $cpu->loadpercentage;
            }
           
           
$load = round($load_total/$cpu_num);
           
        } else {
       
           
$sys_load = sys_getloadavg();
           
$load = $sys_load[0];
       
        }
       
        return (int)
$load;
   
    }
?>
up
1
Anonymous
3 years ago
To get just current load avg, you can do :

<?php
$output
= shell_exec('cat /proc/loadavg');
$loadavg = substr($output,0,strpos($output," "));
?>
up
1
surfchen at gmail dot com
6 years ago
the codes below will provide this function for order versions of PHP.
if (!function_exists('sys_getloadavg')) {
    function sys_getloadavg()
    {
        $loadavg_file = '/proc/loadavg';
        if (file_exists($loadavg_file)) {
            return explode(chr(32),file_get_contents($loadavg_file));
        }
        return array(0,0,0);
    }
}
up
1
828586 at gmail dot com
2 years ago
equivalent for windows
<?php
ob_start
();
passthru('typeperf -sc 1 "\processor(_total)\% processor time"',$status);
$content = ob_get_contents();
ob_end_clean();
if (
$status === 0) {
    if (
preg_match("/\,\"([0-9]+\.[0-9]+)\"/",$content,$load)) {
        if (
$load[1] > get_config('busy_error')) {
           
header('HTTP/1.1 503 Too busy, try again later');
            die(
'Server too busy. Please try again later.');
        }
    }
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites