您好,欢迎访问一九零五行业门户网

一个php进程cpu %nice很高的原因详解

一、 现象描述: 1、 cpu的%user、%sys占用的cpu不高,但%nice占用了大量的cpu资源,最高占用cpu的60%以上;2、 ps -elf中pri为90,ni为10,top看到pr值为30,ni 10; 问题: 为什么从ps中看到的priority值和top中的不同? top中的pr是:the priority of the
一、 现象描述:
1、 cpu的%user、%sys占用的cpu不高,但%nice占用了大量的cpu资源,最高占用cpu的60%以上;2、 ps -elf中pri为90,ni为10,top看到pr值为30,ni 10;
问题:为什么从ps中看到的priority值和top中的不同?
top中的pr是:the priority of the task(取值范围:-20最高,19最低)
ps中的pri是:realtime priority(根据 nice( ) 和setpriority( ) 计算,static priority 取值1-99,dynamic priority还没有看懂)
3、sar信息收集
cpu:linux 2.6.32-279.el6.x86_64 (web2.17173ops.com) 05/09/2014 _x86_64_ (8 cpu)12:00:04 am cpu %user %nice %system %iowait %steal %idle12:01:01 am all 0.39 26.67 1.84 30.15 0.00 40.9512:02:03 am all 0.30 24.07 2.74 30.10 0.00 42.8012:03:01 am all 0.27 23.61 1.65 34.48 0.00 40.0012:04:02 am all 0.26 23.37 1.96 34.87 0.00 39.5412:05:04 am all 0.31 24.08 3.74 36.66 0.00 35.2112:06:02 am all 0.43 21.96 2.09 34.93 0.00 40.5912:07:16 am all 0.25 23.66 2.07 59.63 0.00 14.3812:08:02 am all 0.33 30.21 2.01 36.88 0.00 30.57
二、服务器硬/软件:
product name: poweredge r410cpu:intel(r) xeon(r) cpu e5606 @ 2.13ghzmemory:24gb 4*6os:red hat enterprise linux server release 6.3 (santiago)web server:nginx version: nginx/1.4.4cgi server:php-5.3.21
三、 判断问题:
1、 nice资源一般是用户端控制的行为产生;
2、 除非程序中有大量的使用sleep,或者是调用了nice等函数,对自定义了优先级别,但一般程序不会这么变态;
四、 查找问题过程:
1、习惯性认为是程序开发的问题,在代码中不断查找类似sleep、nice的字眼,浪费了一些时间;
2、ps -elf | grep master,发现php-fpm启动时间不一致,有的是03:57,有的是4:03,如果是crontab定义的,时间肯定不会这么不一致;
3、查找/etc/crontab,文件里面没有定义任何定时任务;
4、rhel6的定时任务被anacrontab接管,anacrontab是crontab的补充,内容如下:(问题也就出在下面红色+备注的那行)
# cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron# see anacron(8) and anacrontab(5) for details.shell=/bin/shpath=/sbin:/bin:/usr/sbin:/usr/binmailto=root# the maximal random delay added to the base delay of the jobsrandom_delay=45# the jobs will be started during the following hours onlystart_hours_range=3-22#period in days delay in minutes job-identifier command1 5 cron.daily nice run-parts /etc/cron.daily ## 日志切割定时任务在此7 25 cron.weekly nice run-parts /etc/cron.weekly@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
五、 思考、优化:
1、不建议在服务器上使用anacrontab,继续使用crontab;(rhel5不存在此问题);
2、其实anacrontab使用nice指令也不会出现问题,主要是和php-fpm启动脚本中的restart配合导致这种情况的发生;
3、php-fpm使用reload或者是kill -usr2,nice都不会生效,会按原pid的优先等级。因为手工启动php-fpm时我们肯定不会带上nice。
原文地址:一个php进程cpu %nice很高的原因详解, 感谢原作者分享。
其它类似信息

推荐信息