最新消息:

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

CPU admin 5081浏览 0评论

一、 现象描述:

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 %idle
12:01:01 AM all 0.39 26.67 1.84 30.15 0.00 40.95
12:02:03 AM all 0.30 24.07 2.74 30.10 0.00 42.80
12:03:01 AM all 0.27 23.61 1.65 34.48 0.00 40.00
12:04:02 AM all 0.26 23.37 1.96 34.87 0.00 39.54
12:05:04 AM all 0.31 24.08 3.74 36.66 0.00 35.21
12:06:02 AM all 0.43 21.96 2.09 34.93 0.00 40.59
12:07:16 AM all 0.25 23.66 2.07 59.63 0.00 14.38
12:08:02 AM all 0.33 30.21 2.01 36.88 0.00 30.57

二、服务器硬/软件:

Product Name: PowerEdge R410
CPU:Intel(R) Xeon(R) CPU E5606 @ 2.13GHz
Memory:24GB 4*6
OS:Red Hat Enterprise Linux Server release 6.3 (Santiago)
Web server:nginx version: nginx/1.4.4
CGI 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/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily <strong>## 日志切割定时任务在此</strong>
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很高的原因详解

您必须 登录 才能发表评论!