Ruby on Rails 进程卡在 CPU 100% 处

Ruby on Rails 进程卡在 CPU 100% 处

环境:Ubuntu 10.04 LTS、Passenger、Nginx 1.0.6、MySQL、Ruby 1.9.2、Rails 3.1

经过一段时间后,服务器中 CPU 占用率达到 100% 的进程数量逐渐增多

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND 
 2393 avitus    20   0  496m 381m 1392 R  100  9.4  25:10.74 Rack: /home/web ...

对任意一个卡住的 PID 运行 strace 都会得到以下结果:

Process 2393 attached with 3 threads - interrupt to quit
[pid  2396] futex(0x8ca80e4, FUTEX_WAIT_PRIVATE, 2, NULL <unfinished ...>
[pid  2394] restart_syscall(<... resuming interrupted call ...>) = -1 ETIMEDOUT (Connection timed out)
[pid  2394] gettimeofday({1322590778, 346573}, NULL) = 0
[pid  2394] futex(0x821db60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  2394] clock_gettime(CLOCK_REALTIME, {1322590778, 346885177}) = 0
[pid  2394] futex(0x821db84, FUTEX_WAIT_PRIVATE, 33872659, {0, 9687823}) = -1 ETIMEDOUT (Connection timed out)
[pid  2394] gettimeofday({1322590778, 356921}, NULL) = 0
[pid  2394] futex(0x821db60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  2394] clock_gettime(CLOCK_REALTIME, {1322590778, 357196244}) = 0
[pid  2394] futex(0x821db84, FUTEX_WAIT_PRIVATE, 33872661, {0, 9724756}) = -1 ETIMEDOUT (Connection timed out)
[pid  2394] gettimeofday({1322590778, 367240}, NULL) = 0
[pid  2394] futex(0x821db60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  2394] clock_gettime(CLOCK_REALTIME, {1322590778, 367459723}) = 0
[pid  2394] futex(0x821db84, FUTEX_WAIT_PRIVATE, 33872663, {0, 9780277}) = -1 ETIMEDOUT (Connection timed out)
[pid  2394] gettimeofday({1322590778, 377586}, NULL) = 0
[pid  2394] futex(0x821db60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  2394] clock_gettime(CLOCK_REALTIME, {1322590778, 377807840}) = 0
[pid  2394] futex(0x821db84, FUTEX_WAIT_PRIVATE, 33872665, {0, 9778160}) = -1 ETIMEDOUT (Connection timed out)
[pid  2394] gettimeofday({1322590778, 387932}, NULL) = 0
[pid  2394] futex(0x821db60, FUTEX_WAKE_PRIVATE, 1) = 0
[pid  2394] clock_gettime(CLOCK_REALTIME, {1322590778, 388162450}) = 0
[pid  2394] futex(0x821db84, FUTEX_WAIT_PRIVATE, 33872667, {0, 9769550}) = -1 ETIMEDOUT (Connection timed out)

包括 strace 的 'c' 标志将产生:

Process 2393 attached with 3 threads - interrupt to quit 
Process 2393 detached Process 2394 detached 
Process 2396 detached 
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 94.97    0.003172           2      1489       744 futex
  3.74    0.000125           0       745           clock_gettime
  1.29    0.000043           0       745           gettimeofday
  0.00    0.000000           0         1         1 restart_syscall
------ ----------- ----------- --------- --------- ----------------
100.00    0.003340                  2980       745 total

我可以用 kill -9 来终止卡住的进程,应用程序和服务器似乎可以顺利运行。我已经想不出如何继续调试了,所以如果有人对原因或其他调查途径有任何建议,我将非常高兴。

答案1

尝试在 Passenger 中将 Passenger_spawn_method 设置为 conservative。我在使用 Mongo 时遇到了这个问题,并遇到了:

http://code.google.com/p/phusion-passenger/issues/detail?id=684

和:

https://github.com/rails/rails/issues/1339

我不知道为什么它不起作用,但如果您还没有找到解决方案的话,希望它能够帮助您。

答案2

这种特殊行为(每 20 毫秒检查一次 futex,然后检查时间)似乎是空闲 Ruby 进程的正常行为:

http://www.ruby-forum.com/topic/192255

答案3

尝试在你的服务器上运行以下命令

sudo date -s "`date`"

来源:http://www.redmine.org/boards/2/topics/31731

相关内容