📄 ch39_03.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 39] 39.3 The csh time variable </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:50:11Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch39_01.htm"TITLE="39. Time and Performance"><LINKREL="prev"HREF="ch39_02.htm"TITLE="39.2 Timing Programs "><LINKREL="next"HREF="ch39_04.htm"TITLE="39.4 Average Command Runtimes with runtime "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch39_02.htm"TITLE="39.2 Timing Programs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 39.2 Timing Programs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 39<BR>Time and Performance</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch39_04.htm"TITLE="39.4 Average Command Runtimes with runtime "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 39.4 Average Command Runtimes with runtime "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-1230">39.3 The csh time variable </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43578"></A><ACLASS="indexterm"NAME="AUTOID-43580"></A>The C shell's variable named <EMCLASS="emphasis">time</EM> controls the built-in <EMCLASS="emphasis">csh</EM><SPANCLASS="link"><EMCLASS="emphasis">time</EM> command (<ACLASS="linkend"HREF="ch39_02.htm"TITLE="Timing Programs ">39.2</A>)</SPAN>.It lets you run <EMCLASS="emphasis">time</EM> by default on commands that takemore than a certain number of CPU seconds, and it lets you control theformat of <EMCLASS="emphasis">time</EM>'s output.</P><PCLASS="para">We'll start with the simple stuff. On virtually any UNIX system,you can use the <EMCLASS="emphasis">time</EM><SPANCLASS="link">shell variable (<ACLASS="linkend"HREF="ch06_08.htm"TITLE="Shell Variables ">6.8</A>)</SPAN>to run <EMCLASS="emphasis">time</EM> automaticallywhen commands take more than a set amount of CPU time. Decidewhat your threshhold is (i.e., the point at which you want <EMCLASS="emphasis">time</EM>to run automatically), in CPU seconds.Then set the <EMCLASS="emphasis">time</EM> shellvariable to thisnumber.For example, if you want to run <EMCLASS="emphasis">time</EM>automatically on programs that require more than 10 CPU seconds, givethe command:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch43_13.htm"TITLE="43.13 The Text Formatters nroff, troff, ditroff, ... ">nroff</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">% <CODECLASS="userinput"><B>set time=10</B></CODE>% <CODECLASS="userinput"><B>ls</B></CODE>file1.ms file2.ms file3.ms% <CODECLASS="userinput"><B>nroff -ms *.ms | lpr</B></CODE>4.3u 9.8s 0:23 60% 0+200k 106+103io 143pf+0w</PRE></TD></TR></TABLE></P><PCLASS="para">The <EMCLASS="emphasis">ls</EM> command didn't generate a <EMCLASS="emphasis">time</EM> report because itran in well under 10 seconds.The <EMCLASS="emphasis">nroff</EM> command took about14.1 CPU seconds, so it did generate a report.</P><PCLASS="para">Why would you want to do this? It lets you monitor theperformance of long jobs automatically without being bothered bystatistics for the small jobs.</P><PCLASS="para">On many C shells, you can also use the <EMCLASS="emphasis">time</EM> variable to customizethe timing report.Sometimes this is useful; the standard reportgives you a lot of information, but it's pretty ugly. For somereason, this feature often goes undocumented.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43609"></A>To customize a timing report, give a command like this:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set time=(</B></CODE><CODECLASS="replaceable"><I>threshold </I></CODE><CODECLASS="userinput"><B>"</B></CODE><CODECLASS="replaceable"><I>format-string</I></CODE><CODECLASS="userinput"><B>")</B></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">Note that you have to give a threshold, whether you want one or not.If you don't want execution times reported automatically, set<EMCLASS="emphasis">threshold</EM> to some large number.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43621"></A><ACLASS="indexterm"NAME="AUTOID-43624"></A>The format string can be any combination of text and tags. Eachtag causes <EMCLASS="emphasis">time</EM> to insert particular statistics.The validtags seem to vary some from system-to-system (and are undocumentedsome places, so you may not be able to tell).We've used two sources: a version for 4.1BSD written byMark Wittenberg and one supplied with Solaris 2.4.Where the two are different, Mark's is labeled <BCLASS="emphasis.bold">A></B> and Sun'sis <BCLASS="emphasis.bold">B></B>.</P><DLCLASS="variablelist"><DTCLASS="term">%D</DT><DDCLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">A></B> Average kilobytes of resident data+stack pages.</P><PCLASS="para"><BCLASS="emphasis.bold">B></B> Average amount of unshared data space used,in kilobytes. This excludes any "shared data." Shared memory is arelatively new feature; many programs don't use it.</P></DD><DTCLASS="term">%E</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43641"></A>The elapsed time required to execute the program. This is the amountof time you'd measure if you sat with a stopwatch and waited for theprogram to finish; it's often called "wall clock" time.</P></DD><DTCLASS="term">%F</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43647"></A>The number of page faults; i.e., the number of times UNIX had to bringa page of virtual memory in from disk.A large number of page faults may mean that your program is taking anunnecessarily long time to run, and you can fix the problem by buyingmore memory. </P></DD><DTCLASS="term">%I</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43654"></A>The number of block input operations. This is the number of times theprogram needed to read data from disk.</P></DD><DTCLASS="term">%K</DT><DDCLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">A></B> Average kilobytes of resident text+data+stack pages.</P><PCLASS="para"><BCLASS="emphasis.bold">B></B> Average amount of unshared stack space used,in kilobytes.</P></DD><DTCLASS="term">%M</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43667"></A>The maximum amount of real memory (physical memory) used by theprogram during execution, in kilobytes.(On 17 October 1986, Daniel V. Klein reported on Usenet <EMCLASS="emphasis">net.unix</EM> thatthe amount %M gives is really just <EMCLASS="emphasis">half</EM> the maximum.The number does seem to be smaller than %K sometimes, so Daniel isprobably right.Don't you love undocumented features?)</P></DD><DTCLASS="term">%O</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43675"></A>The number of block output operations.</P></DD><DTCLASS="term">%P</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43681"></A>The program's total CPU time, as a percentage of elapsed time. Ifyou're the only user on the system and the program does little I/O,this should be close to 100%. It will decrease as the program's I/Orequirements and the system's overall load increase.</P></DD><DTCLASS="term">%S</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43688"></A>CPU system time; the number of seconds the CPU spent in the "system" state onbehalf of your program - i.e., how much time the system spent executingsystem calls on behalf of your program.</P></DD><DTCLASS="term">%U</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43694"></A>CPU user time; the number of seconds the CPU spent in the "user" state onbehalf of your program - i.e., how much time the system spent executingyour program itself.</P></DD><DTCLASS="term">%W</DT><DDCLASS="listitem"><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-43700"></A>The number of "swaps"; the number of times the system needed to moveyour whole program to disk in order to free memory. If this isnon-zero, your system needs more memory.</P></DD><DTCLASS="term">%X</DT><DDCLASS="listitem"><PCLASS="para"><BCLASS="emphasis.bold">A></B> Average kilobytes of resident text pages.</P><PCLASS="para"><BCLASS="emphasis.bold">B></B> The average amount of shared memory that your program required, inkilobytes.<ACLASS="indexterm"NAME="AUTOID-43709"></A></P></DD></DL><PCLASS="para">For example, let's say that we want time statistics for programs thatrequire more than 10 seconds of CPU time, and that we want toreport the system time, the user time, and the elapsed time.</P><PCLASS="para">Despite the huge number of statistics you can get, these are all thatyou really care about, unless you're a performance expert. To do so,we'll set the <EMCLASS="emphasis">time</EM> variable like this (you can also set it inyour<SPANCLASS="link"><EMCLASS="emphasis">.cshrc</EM> file (<ACLASS="linkend"HREF="ch02_02.htm"TITLE="Shell Setup Files-Which, Where, and Why ">2.2</A>)</SPAN>):</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>set time=(10 "System time: %S User time: %U Elapsed time: %E")</B></CODE>% <CODECLASS="userinput"><B>nroff -man * > /dev/null</B></CODE>System time: 0.3 User time: 41.2 Elapsed time: 0:43</PRE></BLOCKQUOTE></P><PCLASS="para">This report is much clearer than the mess you get by default. Itshows clearly that the <EMCLASS="emphasis">nroff</EM> command required 0.3 seconds ofsystem-state CPU time, 41.2 seconds of user-state CPU time, and atotal elapsed time of 43 seconds. </P><BLOCKQUOTECLASS="note"><PCLASS="para"><STRONG>NOTE:</STRONG> I have seen a note somewhere saying thatmany of <EMCLASS="emphasis">time</EM>'s more obscure statistics weren't reportedcorrectly. By "obscure statistics," I mean page faults, averageamount of unshared stack space, and the like. You can trust the user and system CPU time, the elapsed time, and other basicstatistics, but if you really care about the fancy statistics, beware.I seriously doubt that any vendor has fixed these problems.</P></BLOCKQUOTE><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">ML</SPAN>, <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch39_02.htm"TITLE="39.2 Timing Programs "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 39.2 Timing Programs "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch39_04.htm"TITLE="39.4 Average Command Runtimes with runtime "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 39.4 Average Command Runtimes with runtime "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">39.2 Timing Programs </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">39.4 Average Command Runtimes with runtime </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -