real time,user time,sys time

程序运行时间的几个衡量标准包括:real time,user time和sys time,即时钟时间、用户CPU时间、系统CPU时间

通过time命令便可获得这些事件,例如

xxl@xxl-pc:~/apue/real_user_sys time$ time sleep 2

real	0m2.004s
user	0m0.000s
sys	0m0.000s
表明运行sleep这个程序总共花了2.004s,用户CPU时间和系统CPU时间都是0

下面来看一下这几个时间的定义:

real time 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 time 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 time is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system callswithin 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.


对应的中文翻译是:

real time 是墙上时间(wall clock time),也就是进程从开始到结束所用的实际时间。这个时间包括其他进程使用的时间片和进程阻塞的时间(比如等待I/O完成)。
user time指进程执行用户态代码(核心之外)所使用的时间。这是执行此进程所消耗的实际CPU时间,其他进程和此进程阻塞的时间并不包括在内。
sys time指进程在内核态消耗的CPU时间,即在内核执行系统调用所使用的CPU时间。


按道理说,进程从开始到结束所经过的时间(real time)应该比进程所消耗的用户时间和系统时间之和(user time + sys time)大,因为real time还算上了进程阻塞的时间,这段时间该进程不占用CPU,但是看下面的例子(摘自CSDN论坛的帖子):

jmf@jmf:~/MyProgram/9_26$ time -p man gcc >>gg
real 1.81
user 2.54
sys 0.12

出现了real < user+sys的情况,这是为什么?解释如下:

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 it could potentially exceed the wall clock time reported byReal. Note that in the output these figures include the User andSys time of all child processes as well, although the underlying system calls return the statistics for the process and its children separately.

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

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.


多处理器的系统上,一个进程如果有 多个线程或者有 多个子进程可能导致real time比CPU time(user + sys time)要小,这是因为不同的线程或进程可以并行执行。换句话说,这里的并行是指真正的并行,不是在单CPU上的多线程并发,如果系统只有一个CPU(单核),那么即使多线程,也不会使得real < user + sys。

举个例子,一个纯计算任务,没有系统调用(即没有耗费sys time,只有user time),采用单线程需要执行的时间为8s。那么在一个四核的机器上,采用并行算法,用4个核一起算,理想上如果完全并行,加速比为4,2s内就能完成,那么从开始到结束,real time是2s。user time呢?应该是2s * 4 = 8s. 所以此时会出现real < sys + user的情况。

同样,对于多CPU的机器上的多线程程序,也会有这样的结果。例子如下,不考虑线程安全之类的问题。

//real_less_than_user.cc
#include 
long long  cnt = 0;
void *ThreadFun(void *)
{
	for (long long i = 0; i < 1000000000; i++)
	{
		cnt++;
	}
}

int main()
{
	pthread_t tid;
	pthread_create(&tid, NULL, ThreadFun, NULL);
	ThreadFun(0);
	pthread_join(tid, NULL);
	
	return 0;
}

运行的时间信息为:

xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m18.102s
user	0m33.434s
sys	0m0.280s
可见,多线程程序的user time大于real time。由于涉及到系统调用等原因,sys time也有一点点时间开销。


而对于单线程的程序,如下:

//real_more_than_user.cc
long long  cnt = 0;
void *ThreadFun(void *)
{
	for (long long i = 0; i < 1000000000; i++)
	{
		cnt++;
	}
}

int main()
{
	ThreadFun(0);
	return 0;
}
运行的时间信息为:
xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m5.995s
user	0m5.944s
sys	0m0.040s
real > user + sys


但是,有时候也会出现如下情况:

xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m5.990s
user	0m5.944s
sys	0m0.048s
此时 rea < user + sys,在单线程的情况下也会出现这种情况,不解,还请高人解释



参考:

http://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

你可能感兴趣的:(Linux)