进程last-request-cpu的PHP-FPM池状态

标签 php c php-extension php-internals

我已经安装了 PHP 并启用了 FPM 功能,但我对 FPM 状态数据(例如进程 Last-request-cpu)感到不确定,下面是我的 php-fpm.conf 详细信息。

[www]
; Unix user/group of processes
user = www-data
group = www-data

; Chdir to this directory at the start.
chdir = /

; The address on which to accept FastCGI requests.
listen = /var/run/phpfpm/$pool_php5-fpm.sock

; Set listen(2) backlog. A value of '-1' means unlimited.
listen.backlog = -1

; Set permissions for unix socket.
listen.mode = 0666

; Pool configuration.
pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500

; The URI to view the FPM status page.
pm.status_path = /status

; The ping URI to call the monitoring page of FPM.
ping.path = /ping

; The access log file.
access.log = /var/log/phpfpm/$pool_php-fpm.access.log

; The access log format.
access.format = %R - %u %t "%m %r%Q%q" %s %f %{mili}d %{kilo}M %C%%

; The log file for slow requests.
slowlog = /var/log/phpfpm/$pool_php-fpm.log.slow

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
request_slowlog_timeout = 5

; Limits the extensions of the main script FPM will allow to parse.
security.limit_extensions = .php

我已启用 pm.status_path =/status 来查看 FPM 状态结果,如下所示:

<?xml version="1.0" ?>
<status>
<pool>www</pool>
<process-manager>dynamic</process-manager>
<start-time>1418352728</start-time>
<start-since>21936</start-since>
<accepted-conn>20</accepted-conn>
<listen-queue>0</listen-queue>
<max-listen-queue>0</max-listen-queue>
<listen-queue-len>0</listen-queue-len>
<idle-processes>3</idle-processes>
<active-processes>1</active-processes>
<total-processes>4</total-processes>
<max-active-processes>1</max-active-processes>
<max-children-reached>0</max-children-reached>
<slow-requests>0</slow-requests>
<processes>
<process>
    <pid>11</pid>
    <state>Idle</state>
    <start-time>1418352728</start-time>
    <start-since>21936</start-since>
    <requests>5</requests>
    <request-duration>5391</request-duration>
    <request-method>GET</request-method>
    <request-uri>/status?xml&amp;full</request-uri>
    <content-length>0</content-length>
    <user>-</user><script>-</script>
    <last-request-cpu>0.00</last-request-cpu>
    <last-request-memory>262144</last-request-memory>
</process>
<process>
    <pid>12</pid>
    <state>Idle</state>
    <start-time>1418352728</start-time>
    <start-since>21936</start-since>
    <requests>5</requests>
    <request-duration>3365</request-duration>
    <request-method>GET</request-method>
    <request-uri>/status?xml&amp;full</request-uri>
    <content-length>0</content-length>
    <user>-</user><script>-</script>
    <last-request-cpu>297.18</last-request-cpu>
    <last-request-memory>262144</last-request-memory>
</process>
</processes>
</status>

我不知道为什么元素last-request-cpu值297.18超过100,我想知道如何使用它作为监控信息.. 谢谢

最佳答案

该指标将表明上次请求中使用了 cpu time 总数的百分比。

CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to, for example, waiting for input/output (I/O) operations or entering low-power (idle) mode. The CPU time is measured in clock ticks or seconds.

因此,它不是按照本页其他地方建议的以毫秒为单位进行测量的。

您可以在

处查看实现

相关部分是这样的(为了可读性而重新格式化):

431    if (proc.cpu_duration.tv_sec == 0 && proc.cpu_duration.tv_usec == 0) {
432        cpu = 0.;
433    } else {
434        cpu = (proc.last_request_cpu.tms_utime 
                + proc.last_request_cpu.tms_stime 
                + proc.last_request_cpu.tms_cutime 
                + proc.last_request_cpu.tms_cstime) 
                / fpm_scoreboard_get_tick() 
                / (proc.cpu_duration.tv_sec 
                + proc.cpu_duration.tv_usec / 1000000.) 
                * 100.;
435    }

struct members for tms proc.last_request_cpu 定义为:

  • The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process.
  • The tms_stime structure member is the CPU time charged for execution by the system on behalf of the calling process.
  • The tms_cutime structure member is the sum of the tms_utime and tms_cutime times of the child processes.
  • The tms_cstime structure member is the sum of the tms_stime and tms_cstime times of the child processes.

所以这意味着我们正在将最后一个请求中所有可能的 cpu 时间相加。所有时间均以使用的时钟滴答数来衡量。

fpm_scoreboard_get_tick 函数将简单地返回每秒可能的 ticks 数量,例如how many instructions your computer can do at max per second per core.

struct members for the timeval proc.cpu_duration 定义为:

  • time_t tv_sec: This represents the number of whole seconds of elapsed time.
  • long int tv_usec: This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million.

这是耗时(以秒为单位),包括任何小数,例如类似于 2.456435663。

然后将该值乘以 100 以获得百分比值。

示例:

假设我们的最后一个请求在 5 秒内总共消耗了 350 个刻度。我们还假设每秒最大滴答数为 100。如果我们将这些数字代入上面的等式,我们会得到

 (350 / 100 / 5) * 100 = 70

这意味着最后一个请求使用了 70% 的可用 CPU 时间。

您获得高于 100% 的值的原因是因为每秒滴答数的值不受您拥有的核心数量的影响,而 proc.last_request_cpu 值将返回所有进程的滴答数,例如对数据库或其他数据源的访问可能发生在另一个进程中,但直接受 PHP 执行的代码影响。所以这里考虑到了这一点。

关于进程last-request-cpu的PHP-FPM池状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441075/

相关文章:

javascript - 通过BATCH调用PHP文件

c - C 中的变量定义

c - 是否可以将标准输入/输出设置为 C 中的文件?

php - 在 PHP 5.6.12 中启用 DOTNET COM 扩展

php - 我可以强制我的 PHP C 扩展在编译时链接 pthread 吗?

PHP 扩展 : why int var changes to 0?

PHP 5.3.0 NumberFormatter 替换

php - Yii 中的样式管理页面

c - 识别大写/小写字母C的功能

php - sql查询有时有效,有时无效