Home > Backend Development > PHP Tutorial > PHP code to get CPU usage

PHP code to get CPU usage

WBOY
Release: 2016-07-25 08:57:03
Original
3828 people have browsed it
Share some php codes to obtain the CPU usage in the current system. Friends in need can use it as a reference.

In PHP, you can use getrusage() to get the CPU usage. This method is only applicable to Linux systems. Example:

<?php
//获取cpu使用情况
print_r(getrusage());  
/* 输出 
Array 
( 
[ru_oublock] => 0 
[ru_inblock] => 0 
[ru_msgsnd] => 2 
[ru_msgrcv] => 3 
[ru_maxrss] => 12692 
[ru_ixrss] => 764 
[ru_idrss] => 3864 
[ru_minflt] => 94 
[ru_majflt] => 0 
[ru_nsignals] => 1 
[ru_nvcsw] => 67 
[ru_nivcsw] => 4 
[ru_nswap] => 0 
[ru_utime.tv_usec] => 0 
[ru_utime.tv_sec] => 0 
[ru_stime.tv_usec] => 6269 
[ru_stime.tv_sec] => 0 
) 
*/
Copy after login

Explanation: ru_oublock: block output operation ru_inblock: block input operation ru_msgsnd: message sent ru_msgrcv: message received ru_maxrss: Maximum resident set size ru_ixrss: total shared memory size ru_idrss: All non-shared memory size ru_minflt: page recycling ru_majflt: page invalid ru_nsignals: received signals ru_nvcsw: active context switch ru_nivcsw: Passive context switch ru_nswap: swap area ru_utime.tv_usec: User mode time (microseconds) ru_utime.tv_sec: User mode time (seconds) ru_stime.tv_usec: System kernel time (microseconds) ru_stime.tv_sec: System kernel time? (seconds) Sometimes you need to check how much CPU a script consumes, and you need to look at the values ​​of "user mode time" and "system kernel time". The seconds and microseconds parts are provided separately. You can divide the microsecond value by 1 million and add it to the seconds value to get the number of seconds with a decimal part. Example:

<?php
// sleep for 3 seconds (non-busy)  
sleep(3);  
$data = getrusage();  
echo “User time: “.  
($data['ru_utime.tv_sec'] +  
$data['ru_utime.tv_usec'] / 1000000);  
echo “System time: “.  
($data['ru_stime.tv_sec'] +  
$data['ru_stime.tv_usec'] / 1000000);  
/* 输出 
User time: 0.011552 
System time: 0 
*/
Copy after login

sleep does not occupy system time, example:

<?php
// loop 10 million times (busy)  
for($i=0;$i<10000000;$i++) {  
}  
$data = getrusage();  
echo “User time: “.  
($data['ru_utime.tv_sec'] +  
$data['ru_utime.tv_usec'] / 1000000);  
echo “System time: “.  
($data['ru_stime.tv_sec'] +  
$data['ru_stime.tv_usec'] / 1000000);  
/* 输出 
User time: 1.424592 
System time: 0.004204 
*/
Copy after login

The execution of the above code consumes approximately 14 seconds of CPU time, almost all of which is user time because there are no system calls. System time is the time the CPU spends executing kernel instructions on system calls. Example:

<?php
$start = microtime(true);  
// keep calling microtime for about 3 seconds  
while(microtime(true) – $start < 3) {  
}  
$data = getrusage();  
echo “User time: “.  
($data['ru_utime.tv_sec'] +  
$data['ru_utime.tv_usec'] / 1000000);  
echo “System time: “.  
($data['ru_stime.tv_sec'] +  
$data['ru_stime.tv_usec'] / 1000000);  
/* prints 
User time: 1.088171 
System time: 1.675315 
*/
Copy after login

The above example shows higher level CPU consumption. Articles you may be interested in: Solution to the problem of cpu surge caused by php's file_get_contents php code to get CPU usage Use proc/loadavg in php to monitor the average load of the CPU php code to record server load, memory, cpu status Example of php page caching (reducing the burden on cpu and mysql) An example of php code to obtain cpu and memory usage php implementation code to obtain Linux server CPU, memory, hard disk usage php code to obtain the computer’s unique identification information (cpu, network card, MAC address) The relationship between PHP-CGI process CPU 100% and file_get_contents function Solution to the php program randomly recording mysql rand() causing CPU 100%



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template