Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: sys_getloadavg - Manual
[go: Go Back, main page]

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

search for in the

time_nanosleep" width="11" height="7"/> <sleep
Last updated: Sun, 25 Nov 2007

view this page in

sys_getloadavg

(PHP 5 >= 5.1.3)

sys_getloadavg — システムの平均負荷を取得する

説明

array sys_getloadavg ( void )

過去 1、5、15 分間のシステムの平均負荷 (システムの実行キューの中のプロセス数) を表す三つの値を返します。

返り値

(過去 1、5、15 分間の) 三つの値を array で返します。

Example#1 sys_getloadavg() の例

<?php
$load 
sys_getloadavg();
if (
$load[0] > 80) {
    
header('HTTP/1.1 503 Too busy, try again later');
    die(
'Server too busy. Please try again later.');
}
?>

注意

注意: この関数は Windows 環境にはまだ実装されていません。



time_nanosleep" width="11" height="7"/> <sleep
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
sys_getloadavg
scott at corelevel dot com
28-Nov-2006 07:47
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.
surfchen at gmail dot com
07-Jul-2006 01:22
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);
    }
}
tom pittlik
03-Mar-2006 07:36
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).

time_nanosleep" width="11" height="7"/> <sleep
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites