【问题标题】:PHP5-FPM process consumes 100% of CPU when accessing a large PHP filePHP5-FPM 进程在访问大型 PHP 文件时消耗 100% 的 CPU
【发布时间】:2012-11-08 21:14:42
【问题描述】:

我有一个非常奇怪的问题。服务器上有一个 test.php 文件,它有大约 3k 行代码 (100 kB)。代码什么都不做,它定义了一个包含许多参数和函数的相当大的类,但该类永远不会被实例化或以任何方式使用。

我以每秒 100 个请求的速率访问 test.php,直到它被访问 1000 次。之后,我确保服务器平静并再次运行测试。前几次运行通常很好。然后以下运行有一些超时请求。下一次运行有更多的超时请求。然后如果我再做一次,几乎所有的请求都会失败,而top 显示几个 php5-fpm 正在做一些需要 100% CPU 的事情。服务器通常需要几分钟才能平静下来,在这种状态下,任何尝试执行 HTTP 请求都会导致 504 错误。

使用静态文件(例如 test.html)或仅具有 php 开闭标签的空 test2.php 进行相同测试,不会产生有趣的结果,一切运行顺利。

服务器是 Nginx AWS EC2 实例。到目前为止,我尝试使用配置但没有运气。以下是一些可能相关的参数。

root@ip-...:~# cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 23
model name  : Intel(R) Xeon(R) CPU           E5430  @ 2.66GHz
stepping    : 10
cpu MHz     : 2659.994
cache size  : 6144 KB
fdiv_bug    : no
hlt_bug     : no
f00f_bug    : no
coma_bug    : no
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
flags       : fpu tsc msr pae cx8 cmov pat pse36 clflush dts mmx fxsr sse sse2 ss ht pbe nx lm constant_tsc up arch_perfmon pebs bts aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm dca sse4_1 lahf_lm dts tpr_shadow vnmi flexpriority
bogomips    : 5319.98
clflush size    : 64
cache_alignment : 64
address sizes   : 38 bits physical, 48 bits virtual
power management:

root@ip-...:~# nproc
1

www.conf

pm.max_requests = 500
pm.max_children = 5
pm = dynamic

nginx.conf

worker_processes 4;
events {
    worker_connections 768;
}
http {
    gzip on;
    client_body_timeout 300;
    client_header_timeout 300;
    send_timeout 300;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
}

我尝试将其减少到 1 个 nginx worker + 1 个 php child 以及玩一些其他的东西,但这些东西似乎都没有任何区别。据我所知,内存永远不会被交换。任何关于其他可以调试或检查的想法,将不胜感激!

【问题讨论】:

  • apc 呢?这将缓存文件的操作码并跳过解析。
  • 哇,APC 确实有帮助!我无法猜到代码解析是如此的 CPU 密集型。

标签: performance nginx php


【解决方案1】:

使用apc。这会将解析后的 PHP 文件缓存为操作码,并跳过一遍又一遍的解析。

【讨论】:

  • 我有点怀疑将 php 代码转换为令牌是否占用了 100% 的 CPU。
  • 我同意你的观点,但请记住,他是在亚马逊上托管的,cpu 位于很远的地方,与普通机器相比性能更差。此外,标记 3k loc 类并为其创建内存结构可能是一个繁重的过程,尤其是在 100 个请求/秒的速率下。
猜你喜欢
  • 2012-11-23
  • 2010-10-12
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多