CakeFest 2024: The Official CakePHP Conference

getrusage

(PHP 4, PHP 5, PHP 7, PHP 8)

getrusage获取当前资源使用状况

说明

getrusage(int $mode = 0): array|false

这是 getrusage(2) 的接口。它返回了调用自系统的数据。

参数

mode

如果 mode 是 1,getrusage 会使用 RUSAGE_CHILDREN 来调用。

返回值

返回了一个包含系统返回数据的关联数组。所以条目均可通过文档中字段的名称来访问。失败时返回 false

更新日志

版本 说明
7.0.0 此函数现在开始支持 Windows。

示例

示例 #1 getrusage() 示例

<?php
$dat
= getrusage();
echo
$dat["ru_oublock"]; // 块输出操作数
echo $dat["ru_inblock"]; // 块输入操作数
echo $dat["ru_msgsnd"]; // 发送的 IPC 消息数
echo $dat["ru_msgrcv"]; // 接收的 IPC 消息数
echo $dat["ru_maxrss"]; // maximum resident set size
echo $dat["ru_ixrss"]; // 整数类型的共享内存大小
echo $dat["ru_idrss"]; // 整数类型的非共享内存大小
echo $dat["ru_minflt"]; // 页面回收次数(软分页错误)
echo $dat["ru_majflt"]; // 页面错误次数(硬分页错误)
echo $dat["ru_nsignals"]; // 接收到的信号数
echo $dat["ru_nvcsw"]; // number of voluntary context switches
echo $dat["ru_nivcsw"]; // number of involuntary context switches
echo $dat["ru_nswap"]; // 交换次数
echo $dat["ru_utime.tv_usec"]; // 用户使用时间(微秒)
echo $dat["ru_utime.tv_sec"]; // 用户使用时间(秒)
echo $dat["ru_stime.tv_usec"]; // 系统使用时间(微秒)
echo $dat["ru_stime.tv_sec"]; // 系统使用时间(秒)
?>

注释

注意:

在 Windows 上 getrusage() 仅会返回以下类型:

  • "ru_stime.tv_sec"
  • "ru_stime.tv_usec"
  • "ru_utime.tv_sec"
  • "ru_utime.tv_usec"
  • "ru_majflt"(仅当 modeRUSAGE_SELF
  • "ru_maxrss"(仅当 modeRUSAGE_SELF

如果使用设置 mode1RUSAGE_CHILDREN)的情况下调用 getrusage(),则会收集线程的资源使用情况(意味着在内部使用 RUSAGE_THREAD 调用此函数)。

注意:

在 BeOS 2000,仅会返回以下类型:

  • "ru_stime.tv_sec"
  • "ru_stime.tv_usec"
  • "ru_utime.tv_sec"
  • "ru_utime.tv_usec"

参见

  • 系统上 getrusage(2) 的 man 页面

add a note

User Contributed Notes 3 notes

up
10
jlh at gmx dot ch
7 years ago
Note that this function returns rusage of the current process. In a web environment where you have long running apache processes that serve several requests with PHP executions, this will return cumulative timings and is therefore not suitable for telling how much user time your used. The best you could do is to call getrusage() once at the beginning and once at the end and calculate the difference.
up
4
Anonymous
5 years ago
up
4
Domas Mituzas
16 years ago
getrusage() reports kernel counters that are updated only once application loses context and a switch to kernel space happens. For example on modern Linux server kernels that would mean that getrusage() calls would return information rounded at 10ms, desktop kernels - at 1ms.

getrusage() isn't usable for micro-measurements at all - and getmicrotime(true) might be much more valuable resource.
To Top