'real'、'user' 和 'sys' 在 time(1) 的输出中是什么意思? [英] What do 'real', 'user' and 'sys' mean in the output of time(1)?

查看:22
本文介绍了'real'、'user' 和 'sys' 在 time(1) 的输出中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ time foo
real        0m0.003s
user        0m0.000s
sys         0m0.004s
$

时间输出中的real"、user"和sys"是什么意思?

What do 'real', 'user' and 'sys' mean in the output of time?

在对我的应用进行基准测试时,哪个有意义?

Which one is meaningful when benchmarking my app?

推荐答案

Real、User 和 Sys 进程时间统计

其中一件与另一件不同.Real 指实际经过的时间;用户和系统指的是仅由进程使用的 CPU 时间.

One of these things is not like the other. Real refers to actual elapsed time; User and Sys refer to CPU time used only by the process.

  • Real 是挂钟时间 - 从通话开始到结束的时间.这是所有经过的时间,包括其他进程使用的时间片和进程阻塞的时间(例如,如果它正在等待 I/O 完成).

  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

User 是进程在用户模式代码(内核外)中花费的 CPU 时间.这只是用于执行进程的实际 CPU 时间.其他进程和进程花费的阻塞时间不计入此数字.

User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

Sys 是进程内内核花费的 CPU 时间量.这意味着在内核中执行系统调用所花费的 CPU 时间,而不是仍在用户空间中运行的库代码.与用户"一样,这只是进程使用的 CPU 时间.有关内核模式(也称为主管"模式)和系统调用机制的简要说明,请参见下文.

Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.

User+Sys 会告诉你你的进程实际使用了多少 CPU 时间.请注意,这是跨所有 CPU 的,因此如果进程有多个线程(并且此进程在具有多个处理器的计算机上运行),它可能会超过 Real 报告的挂钟时间(其中通常发生).请注意,在输出中,这些数字包括所有子进程(及其后代)的 UserSys 时间以及它们可以被收集的时间,例如通过 wait(2)waitpid(2),尽管底层系统调用分别返回进程及其子进程的统计信息.

User+Sys will tell you how much actual CPU time your process used. Note that this is across all CPUs, so if the process has multiple threads (and this process is running on a computer with more than one processor) it could potentially exceed the wall clock time reported by Real (which usually occurs). Note that in the output these figures include the User and Sys time of all child processes (and their descendants) as well when they could have been collected, e.g. by wait(2) or waitpid(2), although the underlying system calls return the statistics for the process and its children separately.

time(1)

time 报告的统计数据是从各种系统调用中收集的.'User' 和 'Sys' 来自 等待(2)(POSIX) 或 times (2) (POSIX),具体取决于特定系统.'Real' 是根据从 gettimeofday (2) 调用.根据系统版本的不同,还可以按时间收集各种其他统计信息,例如上下文切换的次数.

The statistics reported by time are gathered from various system calls. 'User' and 'Sys' come from wait (2) (POSIX) or times (2) (POSIX), depending on the particular system. 'Real' is calculated from a start and end time gathered from the gettimeofday (2) call. Depending on the version of the system, various other statistics such as the number of context switches may also be gathered by time.

在多处理器机器上,多线程进程或进程分叉子进程的运行时间可能小于总 CPU 时间 - 因为不同的线程或进程可能并行运行.此外,报告的时间统计数据来自不同的来源,因此为非常短的运行任务记录的时间可能会出现四舍五入错误,如原始发布者给出的示例所示.

On a multi-processor machine, a multi-threaded process or a process forking children could have an elapsed time smaller than the total CPU time - as different threads or processes may run in parallel. Also, the time statistics reported come from different origins, so times recorded for very short running tasks may be subject to rounding errors, as the example given by the original poster shows.

内核模式与用户模式的简要介绍

在 Unix 或任何受保护的内存操作系统上,'Kernel' 或 'Supervisor' 模式是指 CPU 可以运行的特权模式.某些可能影响安全性或稳定性的特权操作只能在 CPU 在此模式下运行时进行;这些操作不适用于应用程序代码.此类操作的一个示例可能是操纵 MMU 以访问另一个地址空间过程.通常,用户模式 代码不能这样做(有充分的理由),尽管它可以请求 共享内存 来自内核,可以被多个读取或写入过程.在这种情况下,共享内存是通过安全机制从内核显式请求的,两个进程都必须显式附加到它才能使用它.

On Unix, or any protected-memory operating system, 'Kernel' or 'Supervisor' mode refers to a privileged mode that the CPU can operate in. Certain privileged actions that could affect security or stability can only be done when the CPU is operating in this mode; these actions are not available to application code. An example of such an action might be manipulation of the MMU to gain access to the address space of another process. Normally, user-mode code cannot do this (with good reason), although it can request shared memory from the kernel, which could be read or written by more than one process. In this case, the shared memory is explicitly requested from the kernel through a secure mechanism and both processes have to explicitly attach to it in order to use it.

特权模式通常被称为内核"模式,因为内核是由运行在这种模式下的 CPU 执行的.为了切换到内核模式,您必须发出特定指令(通常称为 trap) 将 CPU 切换为以内核模式运行并从跳转表中保存的特定位置运行代码.出于安全原因,您无法切换到内核模式并执行任意代码 - 陷阱通过地址表进行管理,除非 CPU 以管理员模式运行,否则无法写入该地址表.您使用明确的陷阱编号进行陷阱,并在跳转表中查找地址;内核具有有限数量的受控入口点.

The privileged mode is usually referred to as 'kernel' mode because the kernel is executed by the CPU running in this mode. In order to switch to kernel mode you have to issue a specific instruction (often called a trap) that switches the CPU to running in kernel mode and runs code from a specific location held in a jump table. For security reasons, you cannot switch to kernel mode and execute arbitrary code - the traps are managed through a table of addresses that cannot be written to unless the CPU is running in supervisor mode. You trap with an explicit trap number and the address is looked up in the jump table; the kernel has a finite number of controlled entry points.

C 库中的系统"调用(特别是手册页第 2 节中描述的那些)具有用户模式组件,这是您从 C 程序中实际调用的内容.在幕后,它们可能会向内核发出一个或多个系统调用来执行特定的服务,例如 I/O,但它们仍然有代码在用户模式下运行.如果需要,也很可能从任何用户空间代码直接向内核模式发出陷阱,尽管您可能需要编写一段汇编语言来为调用正确设置寄存器.

The 'system' calls in the C library (particularly those described in Section 2 of the man pages) have a user-mode component, which is what you actually call from your C program. Behind the scenes, they may issue one or more system calls to the kernel to do specific services such as I/O, but they still also have code running in user-mode. It is also quite possible to directly issue a trap to kernel mode from any user space code if desired, although you may need to write a snippet of assembly language to set up the registers correctly for the call.

有关系统"的更多信息

您的代码在用户模式下无法执行某些操作 - 例如分配内存或访问硬件(HDD、网络等).这些都在内核的监督下,只有它自己才能做到.mallocfread/fwrite 等一些操作将调用这些内核函数,然后将其计为系统"时间.不幸的是,这并不像对 malloc 的每次调用都将计入 'sys' 时间"那么简单.对 malloc 的调用将自己进行一些处理(仍计入用户"时间),然后在此过程中可能会调用内核中的函数(计入系统"时间).从内核调用返回后,'user' 将有更多时间,然后 malloc 将返回到您的代码.至于切换什么时候发生,有多少是用在内核模式的……你说不准.这取决于库的实现.此外,其他看似无害的函数也可能在后台使用 malloc 之类的,然后在 'sys' 中再次有一段时间.

There are things that your code cannot do from user mode - things like allocating memory or accessing hardware (HDD, network, etc.). These are under the supervision of the kernel, and it alone can do them. Some operations like malloc orfread/fwrite will invoke these kernel functions and that then will count as 'sys' time. Unfortunately it's not as simple as "every call to malloc will be counted in 'sys' time". The call to malloc will do some processing of its own (still counted in 'user' time) and then somewhere along the way it may call the function in kernel (counted in 'sys' time). After returning from the kernel call, there will be some more time in 'user' and then malloc will return to your code. As for when the switch happens, and how much of it is spent in kernel mode... you cannot say. It depends on the implementation of the library. Also, other seemingly innocent functions might also use malloc and the like in the background, which will again have some time in 'sys' then.

这篇关于'real'、'user' 和 'sys' 在 time(1) 的输出中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆