Linux-time
下面是引用别人的blog:
用途说明
time命令常用于测量一个命令的运行时间,注意不是用来显示和修改系统时间的(这是date命令干的事情)。但是今天我通过查看time命令的手册页,发现它能做的不仅仅是测量运行时间,还可以测量内存、I/O等的使用情况,手册页上的说法是time a simple command or give resource usage,其中time一词我认为它应该是测量或测定的意思,并不单指时间。一个程序在运行时使用的系统资源通常包括CPU、Memory和I/O等,其中CPU资源的统计包括实际使用时间(real time)、用户态使用时间(the process spent in user mode)、内核态使用时间(the process spent in kernel mode)。但是简单的使用time命令并不能得到内存和I/O的统计数据,请看后文慢慢道来。
常用参数
time命令最常用的使用方式就是在其后面直接跟上命令和参数:
time <command> [<arguments...>]
在命令执行完成之后就会打印出CPU的使用情况:
real 0m5.064s <== 实际使用时间(real time)
user 0m0.020s <== 用户态使用时间(the process spent in user mode)
sys 0m0.040s <== 内核态使用时间(the process spent in kernel mode)
time命令跟上-p参数可以只打印时间数值(秒数),不打印单位。
使用示例
示例一
统计运行时间
[root@web186 root]# time find . -name "mysql.sh"
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
real 0m14.837s
user 0m0.030s
sys 0m0.120s
示例三 解决time命令输出信息的重定向问题
time命令的输出信息是打印在标准错误输出上的, 我们通过一个简单的尝试来验证一下。
[root@web186 root]# time find . -name "mysql.sh" >1.txt
real 0m0.081s
user 0m0.060s
sys 0m0.020s
[root@web186 root]# time find . -name "mysql.sh" 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
real 0m0.068s
user 0m0.040s
sys 0m0.030s
通过上面的尝试,发现无法将time的输出信息重定向到文件里面,为什么?因为time是shell的关键字,shell做了特殊处理,它会把time命令后面的命令行作为一个整体来进行处理,在重定向时,实际上是针对后面的命令来的,time命令本身的输出并不会被重定向的。那现在怎么办呢?网上提供了两种解决方法【2,3】,我们一一尝试一下。
第一种解决方法,就是将time命令和将要执行的命令行放到一个shell代码块中,也就是一对大括号中,要注意空格和分号的使用。
[root@web186 root]# {time find . -name "mysql.sh"} 2>2.txt
好像成功了。慢,看一下对不对。
[root@web186 root]# cat 2.txt
-bash: {time: command not found
原来bash把 {time 作为一个整体来处理了,前后都加上空格试试。
[root@web186 root]# { time find . -name "mysql.sh" } 2>2.txt
> Ctrl+C
这次Bash认为命令都没有输入完成,少了分号。因为Bash认为后面的 } 是find命令的参数。
[root@web186 root]# { time find . -name "mysql.sh"; } 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
[root@web186 root]# cat 2.txt
real 0m0.068s
user 0m0.030s
sys 0m0.040s
第一种方式的尝试成功了,总结起来就是 { time command-line; } 2>file 注意分隔符的使用。
另外一种方式就是使用子Shell的方式,如下所示:
[root@web186 root]# (time find . -name "mysql.sh") 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
[root@web186 root]# cat 2.txt
real 0m0.083s
user 0m0.040s
sys 0m0.020s
[root@web186 root]#
第二种方式的尝试也成功了,总结起来就是 (time command-line) 2>file 这里time紧贴着小括号(也可以的,命令行结束也不必带分号。当然最好还是用第一种方式,毕竟启动一个子shell是要多占些资源的。
Linux中time命令,我们经常用来计算某个程序的运行耗时,用户态cpu耗时,系统态cpu耗时。
例如:
$ time foo real 0m0.003suser 0m0.000ssys 0m0.004s$那么这三个时间都具体代表什么意思呢?
[1] real : 表示foo程序整个的运行耗时,可以理解为foo运行开始时刻你看了一下手表,foo运行结束时,你又看了一下手表,两次时间的差值就是本次real 代表的值
举个极端的例子如下:可以看到real time恰好为2秒。
# time sleep 2
real 0m2.003s
user 0m0.000s
sys 0m0.000s
[2] user 0m0.000s:这个时间代表的是foo运行在用户态的cpu时间,什么意思?
首先,我来讲一下用户态和核心态:
核心态(Kernel Mode):
在内核态,代码拥有完全的,不受任何限制的访问底层硬件的能力。可以执行任意的CPU指令,访问任意的内存地址。内核态通常情况下,都是为那些最底层的,由操作系统提供的,可信可靠的代码来运行的。内核态的代码崩溃将是灾难性的,它会影响到整个系统。
—– In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC.
用户态(User Mode):
在用户态,代码不具备直接访问硬件或者访问内存的能力,而必须借助操作系统提供的可靠的,底层的APIs来访问硬件或者内存。由于这种隔离带来的保护作用,用户态的代码崩溃(Crash),系统是可以恢复的。我们大多数的代码都是运行在用户态的。
—– In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode must delegate to system APIs to access hardware or memory. Due to the protection afforded by this sort of isolation, crashes in user mode are always recoverable. Most of the code running on your computer will execute in user mode.
为什么要区分Kernel Mode 和 User Mode:
隔离保护,使得系统更稳定。
好,讲完用户态和核心态之后,我们来看user time,说过了,这个指的是程序foo运行在用户态的cpu时间,cpu时间不是墙上的钟走过的时间,而是指CPU工作时间。
[3] sys 0m0.004s : 这个时间代表的是foo运行在核心态的cpu时间。
好,讲完上面的这些,我们来看看这三个的关系,这三者之间没有严格的关系,常见的误区有:
误区一: real_time = user_time + sys_time
我们错误的理解为,real time 就等于 user time + sys time,这是不对的,real time是时钟走过的时间,user time 是程序在用户态的cpu时间,sys time 为程序在核心态的cpu时间。
利用这三者,我们可以计算程序运行期间的cpu利用率如下:
%cpu_usage = (user_time + sys_time)/real_time * 100%
如:
# time sleep 2
real 0m2.003s
user 0m0.000s
sys 0m0.000s
cpu利用率为0,因为本身就是这样的,sleep 了2秒,时钟走过了2秒,但是cpu时间都为0,所以利用率为0
误区二:real_time > user_time + sys_time
一般来说,上面是成立的,上面的情况在单cpu的情况下,往往都是对的。
但是在多核cpu情况下,而且代码写的确实很漂亮,能把多核cpu都利用起来,那么这时候上面的关系就不成立了,例如可能出现下面的情况,请不要惊奇。
real 1m47.363s
user 2m41.318s
sys 0m4.013s