仓酷云
标题:
来一发办理PHP-CGI 历程 CPU 100%
[打印本页]
作者:
飘灵儿
时间:
2015-1-14 20:27
标题:
来一发办理PHP-CGI 历程 CPU 100%
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!
偶然候,运转Nginx、PHP-CGI(php-fpm)Web办事的Linux办事器,俄然体系负载上升,利用top下令检察,良多php-cgi历程CPU利用率靠近100%。厥后,我经由过程跟踪发明,这类情形的呈现,跟PHP的file_get_contents()函数有着亲切的干系。
们晓得,在php.ini中,有一个参数max_execution_time能够设置PHP剧本的最年夜实行工夫,可是,在php-cgi(php-fpm)中,该参数不会起效。真正可以把持PHP剧本最年夜实行工夫的是php-fpm.conf设置文件中的以下参数:
[color=#333333!important]
1
[color=#317cc5!important]
2
3
[color=#317cc5!important]
4
[color=#004ed0!important]
The
[color=#004ed0!important]
timeout
[color=#006fe0!important]
[color=#333333!important]
(
[color=#800080!important]
in
[color=#006fe0!important]
seconds
[color=#333333!important]
)
[color=#006fe0!important]
[color=#800080!important]
for
[color=#006fe0!important]
serving
[color=#006fe0!important]
a
[color=#006fe0!important]
[color=#004ed0!important]
single
[color=#004ed0!important]
request
[color=#004ed0!important]
after
[color=#004ed0!important]
which
[color=#004ed0!important]
the
[color=#004ed0!important]
worker
[color=#004ed0!important]
process
[color=#004ed0!important]
will
[color=#004ed0!important]
be
[color=#004ed0!important]
terminated
[color=#004ed0!important]
Should
[color=#004ed0!important]
be
[color=#004ed0!important]
used
when
[color=#006fe0!important]
[color=#008000!important]
max_execution_time
[color=#006fe0!important]
[color=#004ed0!important]
ini
[color=#004ed0!important]
option
[color=#004ed0!important]
does
[color=#800080!important]
not
[color=#006fe0!important]
[color=#004ed0!important]
stop
[color=#004ed0!important]
script
[color=#004ed0!important]
execution
[color=#800080!important]
for
[color=#006fe0!important]
[color=#004ed0!important]
some
reason
[color=#006fe0!important]
[color=#008000!important]
0s
[color=#006fe0!important]
means
[color=#006fe0!important]
[color=#008000!important]
off
[color=#006fe0!important]
[color=#006fe0!important]
<
[color=#004ed0!important]
value
[color=#002d7a!important]
name
[color=#006fe0!important]
=
[color=#008000!important]
"request_terminate_timeout"
[color=#006fe0!important]
>
[color=#ce0000!important]
0s
[color=#006fe0!important]
<
[color=#006fe0!important]
/
value
[color=#006fe0!important]
>
默许值为0秒,也就是说,PHP剧本会一向实行下往。如许,当一切的php-cgi历程都卡在file_get_contents()函数时,这台Nginx+PHP的WebServer已没法再处置新的PHP哀求了,Nginx将给用户前往“502BadGateway”。修正该参数,设置一个PHP剧本最年夜实行工夫是需要的,可是,治本不治标。比方改成<valuename="request_terminate_timeout">30s</value>,假如产生file_get_contents()猎取网页内容较慢的情形,这就意味着150个php-cgi历程,每秒钟只能处置5个哀求,WebServer一样很难制止“502BadGateway”。
要做到完全办理,只能让PHP步伐员们改失落间接利用file_get_contents("http://example.com/")的习气,而是略微修正一下,加个超不时间,用以下体例来完成HTTPGET哀求。如果以为贫苦,能够自即将以下代码封装成一个函数。
[color=#333333!important]
1
[color=#317cc5!important]
2
3
[color=#317cc5!important]
4
5
[color=#317cc5!important]
6
7
[color=#317cc5!important]
8
9
[color=#ff0000!important]
<?php
[color=#006fe0!important]
[color=#002d7a!important]
$ctx
[color=#006fe0!important]
[color=#006fe0!important]
=
[color=#006fe0!important]
[color=#004ed0!important]
stream_context_create
[color=#333333!important]
(
[color=#800080!important]
array
[color=#333333!important]
(
[color=#006fe0!important]
[color=#006fe0!important]
[color=#008000!important]
http
[color=#006fe0!important]
[color=#006fe0!important]
=
[color=#006fe0!important]
>
[color=#006fe0!important]
[color=#800080!important]
array
[color=#333333!important]
(
[color=#006fe0!important]
[color=#006fe0!important]
[color=#008000!important]
timeout
[color=#006fe0!important]
[color=#006fe0!important]
=
[color=#006fe0!important]
>
[color=#006fe0!important]
[color=#ce0000!important]
1
[color=#006fe0!important]
[color=#ff8000!important]
//设置一个超不时间,单元为秒
[color=#006fe0!important]
[color=#333333!important]
)
[color=#006fe0!important]
[color=#006fe0!important]
[color=#333333!important]
)
[color=#006fe0!important]
[color=#333333!important]
)
[color=#333333!important]
;
[color=#006fe0!important]
[color=#004ed0!important]
file_get_contents
[color=#333333!important]
(
[color=#008000!important]
"http://example.com/"
[color=#333333!important]
,
[color=#006fe0!important]
[color=#ce0000!important]
0
[color=#333333!important]
,
[color=#006fe0!important]
[color=#002d7a!important]
$ctx
[color=#333333!important]
)
[color=#333333!important]
;
[color=#006fe0!important]
[color=#ff0000!important]
?>
固然,招致php-cgi历程CPU100%的缘故原由不但有这一种,那末,怎样断定是file_get_contents()函数招致的呢?
起首,利用top下令检察CPU利用率较高的php-cgi历程。
top-10:34:18up724days,21:01,3users,loadaverage:17.86,11.16,7.69
Tasks:561total,15running,546sleeping,0stopped,0zombie
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:8100996ktotal,4320108kused,3780888kfree,772572kbuffers
Swap:8193108ktotal,50776kused,8142332kfree,412088kcached
PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND
10747www180360m22m12mR100.60.30:02.60php-cgi
10709www160359m28m17mR96.80.40:11.34php-cgi
10745www180360m24m14mR94.80.30:39.51php-cgi
10707www180360m25m14mS77.40.30:33.48php-cgi
10782www200360m26m15mR75.50.30:10.93php-cgi
10708www250360m22m12mR69.70.30:45.16php-cgi
10683www250362m28m15mR54.20.40:32.65php-cgi
10711www250360m25m15mR52.20.30:44.25php-cgi
10688www250359m25m15mR38.70.30:10.44php-cgi
10719www250360m26m16mR7.70.30:40.59php-cgi
找个中一个CPU100%的php-cgi历程的PID,用以下下令跟踪一下:
strace-p10747
假如屏幕显现:
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
select(7,[6],[6],[],{15,0})=1(out[6],left{15,0})
poll([{fd=6,events=POLLIN}],1,0)=0(Timeout)
那末,就能够断定是file_get_contents()招致的成绩了。
欢迎大家来到仓酷云论坛!
作者:
莫相离
时间:
2015-1-16 18:07
标题:
来一发办理PHP-CGI 历程 CPU 100%
现在的linux操作系统如redhat,难点,红旗等,都是用这么一个内核,加上其它的用程序(包括X)构成的。
作者:
若天明
时间:
2015-1-25 22:32
老实说,第一个程序是在C中编译好的,调试好了才在Linux下运行,感觉用vi比较麻烦,因为有错了不能调试,只是提示错误。
作者:
山那边是海
时间:
2015-2-4 15:59
随着IT从业人员越来越多,理论上会有更多的人使用Linux,可以肯定,Linux在以后这多时间不会消失。
作者:
分手快乐
时间:
2015-2-10 04:25
硬盘安装及光盘安装,清楚了解安装Linux应注意的有关问题,如安装Linux应在最后一个分区内,至少分二个分区。
作者:
再见西城
时间:
2015-2-28 21:44
其实当你安装了一个完整的Linux系统后其中已经包含了一个强大的帮助,只是可能你还没有发现和使用它们的技巧。
作者:
兰色精灵
时间:
2015-3-10 13:04
Linux的成功就在于用最少的资源最短的时间实现了所有功能,这也是符合人类进化的,相信以后节能问题会日益突出。
作者:
再现理想
时间:
2015-3-17 11:10
写学习日记,这是学习历程的见证,同时我坚持认为是增强学习信念的法宝。
作者:
飘灵儿
时间:
2015-3-24 11:48
Linux的成功就在于用最少的资源最短的时间实现了所有功能,这也是符合人类进化的,相信以后节能问题会日益突出。
欢迎光临 仓酷云 (http://ckuyun.com/)
Powered by Discuz! X3.2