php-cgi process CPU 100% vs. file_get_contents functions _php Tutorial

Source: Internet
Author: User
Tags stop script high cpu usage
Sometimes, the Linux server running Nginx, php-cgi (PHP-FPM) Web services, suddenly the system load up, use the top command to view, many php-cgi process CPU utilization is close to 100%. Later, I tracked the discovery that this kind of situation is closely related to PHP's file_get_contents () function.

In large and medium-sized Web sites, API interface calls based on the HTTP protocol are commonplace. PHP programmers like to use the simple and convenient file_get_contents ("http://example.com/") function to get the return content of a URL, but if http://example.com/the site responds slowly, file _get_contents () will always be stuck there and will not time out.

We know that in php.ini, there is a parameter max_execution_time can set the maximum execution time for PHP scripts, but in php-cgi (PHP-FPM), this parameter will not work. The real ability to control the maximum execution time for PHP scripts is the following parameters in the php-fpm.conf configuration file:
The timeout (in seconds) for serving a single request after which the worker process would be is terminated should be used whe n ' max_execution_time ' ini option does not stop script execution for some reason ' 0s ' means ' off ' 0s default value is 0 seconds, that is, PHP script will continue to do so. This way, when all the php-cgi processes are stuck in the file_get_contents () function, the nginx+php WebServer can no longer process the new PHP request, and Nginx will return "502 bad Gateway" to the user. To modify this parameter, it is necessary to set the maximum execution time for a PHP script, but the symptom is not a cure. For example, change into 30s , if file_get_contents () gets a slower page content, which means that 150 php-cgi processes can handle only 5 requests per second, WebServer is also difficult to avoid "502 bad Gateway".

To do a thorough solution, only PHP programmers can get rid of the habit of directly using file_get_contents ("http://example.com/"), but slightly modified, add a time-out, in the following way to implement an HTTP GET request. If you feel trouble, you can encapsulate the following code as a function yourself.
Array (' timeout ' = 1//Set a time-out, in seconds)); File_get_contents ("http://example.com/", 0, $ctx);?> of course, the cause of the php-cgi process CPU 100% is not the only reason, then, how to determine the file_get_contents ( ) function?

First, use the top command to see the php-cgi process with high CPU usage.

Top-10:34:18 up 724 days, 21:01, 3 users, Load average:17.86, 11.16, 7.69
tasks:561 Total, running, 546 sleeping, 0 stopped, 0 zombie
Cpu (s): 5.9%us, 4.2%sy, 0.0%ni, 89.4%id, 0.2%wa, 0.0%hi, 0.2%si, 0.0%st
mem:8100996k Total, 4320108k used, 3780888k free, 772572k buffers
swap:8193108k Total, 50776k used, 8142332k free, 412088k cached

PID USER PR NI VIRT RES SHR S%cpu%MEM time+ COMMAND
10747 www 0 360m 22m 12m R 100.6 0.3 0:02.60 php-cgi
10709 www 0 359m 28m 17m R 96.8 0.4 0:11.34 php-cgi
10745 www 0 360m 24m 14m R 94.8 0.3 0:39.51 php-cgi
10707 www 0 360m 25m 14m S 77.4 0.3 0:33.48 php-cgi
10782 www 0 360m 26m 15m R 75.5 0.3 0:10.93 php-cgi
10708 www 0 360m 22m 12m R 69.7 0.3 0:45.16 php-cgi
10683 www 0 362m 28m 15m R 54.2 0.4 0:32.65 php-cgi
10711 www 0 360m 25m 15m R 52.2 0.3 0:44.25 php-cgi
10688 www 0 359m 25m 15m R 38.7 0.3 0:10.44 php-cgi
10719 www 0 360m 26m 16m R 7.7 0.3 0:40.59 php-cgi

Find the PID of one of the CPU 100% php-cgi processes to follow the command:

Strace-p 10747

If the screen displays:

Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)

Then, you can be sure that file_get_contents () is causing the problem.

http://www.bkjia.com/PHPjc/363903.html www.bkjia.com true http://www.bkjia.com/PHPjc/363903.html techarticle Sometimes, the Linux server running Nginx, php-cgi (PHP-FPM) Web services, suddenly the system load up, use the top command to view, many php-cgi process CPU utilization is close to 100%. Later ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.