【问题标题】:PHP-FPM / FastCGI + exit() causing CPU spikesPHP-FPM / FastCGI + exit() 导致 CPU 峰值
【发布时间】:2012-02-15 17:25:35
【问题描述】:

在 Cherokee 1.2.101 的 FastCGI 中运行 Archlinux / php-fpm 5.3.9 的某些服务器上,我遇到了间歇性问题。我正在使用一个缓存插件,它使用如下逻辑构建和提供静态缓存文件:

$cache_file = md5($host . $uri) . '.cache';
if( file_exists($cache_file) ) {
  $cache_file_contents = file_get_contents($cache_file)
  exit( $cache_file_contents );
}
// else build/save the $cache_file

一些进程最终会挂在exit() 调用上的 php-fpm 缓慢日志中。那时负载达到峰值,100% 的 CPU 使用率(几乎)完全用于网络服务器,PHP 页面开始返回 500 - Internal Server 错误。有时服务器会自行恢复,其他的我需要重新启动 php-fpm 和 cherokee。

  • 我已将 PHP-FPM 的 FastCGI 设置配置为执行

  • 尽管这是一个 VPS,但我暂时排除了文件系统上的 IO 等待,因为缓存文件应该已经加载。我无法在使用vmstat进行测试时捕捉到它

  • 我将pm.max_requests 设置为500,但想知道exit() 调用是否会干扰进程循环。

  • php-fpm 日志显示很多WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)。这似乎是 php-fpm 调节池中子进程数量的正常部分

我们将不胜感激任何有关故障排除的提示。以下是我发现引发了一些危险信号的 3 件事:

http://www.php.net/manual/en/function.exit.php#96930

https://serverfault.com/questions/84962/php-via-fastcgi-terminated-by-calling-exit#85008

Errors when calling exit() function for fastCGI?

【问题讨论】:

  • 对于故障排除,您可以尝试使用 strace 来查看该特定进程在做什么:strace -p PID

标签: fastcgi php cherokee


【解决方案1】:

这可能与将输出传递给服务器有关(跟踪 I/O 也是如此)。您可以通过让您的网络服务器为静态缓存文件提供服务来避免 FPM。除此之外,我建议你使用这个 PHP 块来减少内存/I/O:

if (file_exists($cache_file))
{
    readfile($cache_file)
    exit;
}

readfile

如果你不想使用exit(我个人从未遇到过在 PHP 中将它与 FastCGI 一起使用的问题),你应该清理你的代码,这样就不需要使用 exit,例如您可以return 或查看您的代码流为什么需要使用 exit 并消除问题。

【讨论】:

    【解决方案2】:

    我最终使用了 http://www.php.net/manual/en/function.exit.php 的 cmets 中引用的 Pythonic 异常包装方法

    在主index.php中

    class SystemExit extends Exception {}
    
    try{ 
        /* Resume loading web-app */
    }
    catch (SystemExit $e) {}
    

    在问题的缓存逻辑中,替换exit( $cache_file_contents );

    while (@ob_end_flush());
    flush();
    echo $cache_file_contents;
    throw new SystemExit();
    

    这缓解了显示挂在exit() 上的 php-fpm 慢日志。我不完全相信它解决了根本问题,但它已经清理了日志文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 2016-05-01
      • 2013-11-30
      相关资源
      最近更新 更多