[原创]time命令解析_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2868 | 回复: 0   主题: [原创]time命令解析        下一篇 
wulcan
版主
等级:中校
经验:1795
发帖:124
精华:0
注册:2014-3-19
状态:离线
发送短消息息给wulcan 加好友    发送短消息息给wulcan 发消息
发表于: IP:您无权察看 2015-7-24 19:32:59 | [全部帖] [楼主帖] 楼主

在测试程序的时候,会经常使用time命令来确认程序运行的时间。
特别是在编译内核或者测试机器性能的时候,time必不可少的工具。
但是在FreeBSD上,time有点奇怪,因为man time上面说可以有几个参数,比如-l和-a参数,但是我输入这几个参数后,就会得“参数未找到”的错误。长期以来一直以为man没有跟上命令的更新,但经历了很多版本,结果都是一样,FreeBSD不会这么粗心的!

今天又仔细查看了一下man time,发现time命令本身就有两个:一个是外部命令,一个是内部命令。
对于FreeBSD来说,外部命令在/usr/bin/time,内部命令集成在csh里面。
这个问题可能很多人可能没注意到。

先看外部的time命令,这个命令还是比较简单的:

 time [-al] [-h -p] [-o file] utility [argument ...]


-a : 追加模式,跟-o相配合使用,会在结果文件中追加而不是覆盖。
-l : 把rusage结构一起输出,也就相当于输出详细信息
-h : 人可读的模式,其实也就是那几个时间放一行
-p : 机读模式,每行一个项目,跟-h相互斥


例:

 root@TaoYuan:~ # /usr/bin/time -l php test.php
1250000025000001
2.21 real 2.21 user 0.00 sys
8044 maximum resident set size
2661 average shared memory size
112 average unshared data size
128 average unshared stack size
402 page reclaims
0 page faults
0 swaps
0 block input operations
0 block output operations
0 messages sent
0 messages received
0 signals received
3 voluntary context switches
24 involuntary context switches


这个输出还是比较详细的。

再来看默认的time,也就是builtin的time,这个要man csh了:

……
time [command]
Executes command (which must be a simple command, not an alias,
a pipeline, a command list or a parenthesized command list) and
prints a time summary as described under the time variable. If
necessary, an extra shell is created to print the time statis-
tic when the command completes. Without command, prints a time
summary for the current shell and its children.


    注意上面的提示:time必须是简单的命令,不能是alias、管道、命令列表、或父类化的命令列表。经过测试,/usr/bin/time也要遵循这个规则,复杂的命令有时得不到想要的结果。

……
time If set to a number, then the time builtin (q.v.) executes auto-
matically after each command which takes more than that many
CPU seconds. If there is a second word, it is used as a format
string for the output of the time builtin. (u) The following
sequences may be used in the format string:
%U The time the process spent in user mode in cpu seconds.
%S The time the process spent in kernel mode in cpu seconds.
%E The elapsed (wall clock) time in seconds.
%P The CPU percentage computed as (%U + %S) / %E.
%W Number of times the process was swapped.
%X The average amount in (shared) text space used in Kbytes.
%D The average amount in (unshared) data/stack space used in
Kbytes.
%K The total space used (%X + %D) in Kbytes.
%M The maximum memory the process had in use at any time in
Kbytes.
%F The number of major page faults (page needed to be brought
from disk).
%R The number of minor page faults.
%I The number of input operations.
%O The number of output operations.
%r The number of socket messages received.
%s The number of socket messages sent.
%k The number of signals received.
%w The number of voluntary context switches (waits).
%c The number of involuntary context switches.
Only the first four sequences are supported on systems without
BSD resource limit functions. The default time format is `%Uu
%Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww' for systems that support
resource usage reporting and `%Uu %Ss %E %P' for systems that
do not.
Under Sequent's DYNIX/ptx, %X, %D, %K, %r and %s are not avail-
able, but the following additional sequences are:
%Y The number of system calls performed.
%Z The number of pages which are zero-filled on demand.
%i The number of times a process's resident set size was
increased by the kernel.
%d The number of times a process's resident set size was
decreased by the kernel.
%l The number of read system calls performed.
%m The number of write system calls performed.
%p The number of reads from raw disk devices.
%q The number of writes to raw disk devices.
and the default time format is `%Uu %Ss %E %P %I+%Oio
%Fpf+%Ww'. Note that the CPU percentage can be higher than
100% on multi-processors.
……


    这段文字很长,不再细致的翻译,大致意思是:使用time变量来指定输出的格式,并且要指定一个时间,单位为秒,所有运行时CPU占用时间超过这个数值的命令,都会在最后自动附加time的结果。默认的输出格式为“%Uu  %Ss  %E    %P  %X+%Dk  %I+%Oio %Fpf+%Ww”,也就是我们平时见到的格式,这儿顺便把这几个的意思解释一下:

root@TaoYuan:~ # time php test.php
1250000025000001
2.214u 0.000s 0:02.21 100.0% 3372+305k 0+0io 0pf+0w


第二行是输出的结果,test.php的内容是让一个几十亿的大数累计,是很耗CPU的一个计算。
第三行:

2.214u: 用户态时间为2.214秒


0.000s: 内核时间为0秒
0:02.21: 这个是总的时间,为上面两项之和
100%: CPU占用率
3372+305K: text占用的内存为3372k,数据/堆栈占用305k
0+0IO: 0输入,0输出

0pf+0w: 0 page fault,0 swap


然后设置一下time变量,来看一下神奇的效果

root@TaoYuan:~ # set time = ( 1 "%U %S %P %r %s" )
root@TaoYuan:~ # php test.php
1250000025000001
2.211 0.000 100.0% 0 0
root@TaoYuan:~ # set time = ( 2 "%U %S %P %r %s" )
root@TaoYuan:~ # php test.php
1250000025000001
2.213 0.000 100.0% 0 0
root@TaoYuan:~ # set time = ( 3 "%U %S %P %r %s" )
root@TaoYuan:~ # php test.php
1250000025000001


第一个命令:设置显示上限为1秒,所有CPU占用超过1s的都显示time结果,time的格式为:用户态时间,内核态时间,CPU占用率,网络接收,网络发送
第二个命令运行test.php,由于test.php运行时间为2.2秒,所以就会在下方自动显示时间,时间格式为所设置的格式。
第三个命令,把CPU占用上限设置为2秒,同样能显示。
第五个命令,把CPU占用上限设置为3秒,再运行同样的程序,就不会显示time值。

如果想开机就有这种效果,把time写进~/.cshrc里面就可以了:

 set autorehash
set time = ( 1 "%U %S %P %r %s" )
set mail = (/var/mail/$USER)


自动显示time,这个功能还是挺不错的,特别是在编译内核和大型软件的时候,就不需要再特别地输入time了。



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论