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

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

问题描述

$ 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?

推荐答案

真实,用户和系统进程时间统计

其中之一与另一件事不一样.实际是指实际经过的时间; User和Sys是指仅由进程使用的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.

  • 实数是挂钟时间-从通话开始到结束的时间.这是所有经过的时间,包括其他进程使用的时间片以及该进程花费的时间被阻塞(例如,如果它在等待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).

用户是进程内用户模式代码(在内核外部)花费的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)

Origins of the statistics reported by time (1)

time报告的统计信息是从各种系统调用中收集的. 用户"和系统"来自 ( POSIX )或 gettimeofday (2) 致电.根据系统版本的不同,time可能还会收集各种其他统计信息,例如上下文切换数.

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或任何受保护的内存操作系统上,内核"或管理程序" 模式是指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.

有关"sys"的更多信息

有些代码在用户模式下无法执行-诸如分配内存或访问硬件(HDD,网络等)之类的操作.这些都是在内核的监督下完成的,仅靠内核就可以做到.诸如mallocfread/fwrite之类的某些操作将调用这些内核函数,然后将其计为"sys"时间.不幸的是,这并不像对malloc的每次调用都以'sys'时间计入"那样简单.对malloc的调用将自己进行一些处理(仍以用户"时间计),然后沿其调用内核中函数的方式进行某处处理(以"sys"时间计).从内核调用返回后,用户"中将有更多时间,然后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.

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

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