Home | History | Annotate | Line # | Download | only in csh
time.c revision 1.5
      1 /*-
      2  * Copyright (c) 1980, 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)time.c	8.1 (Berkeley) 5/31/93";*/
     36 static char *rcsid = "$Id: time.c,v 1.5 1994/09/21 00:11:20 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 #if __STDC__
     41 # include <stdarg.h>
     42 #else
     43 # include <varargs.h>
     44 #endif
     45 
     46 #include "csh.h"
     47 #include "extern.h"
     48 
     49 /*
     50  * C Shell - routines handling process timing and niceing
     51  */
     52 static void	pdeltat __P((struct timeval *, struct timeval *));
     53 
     54 void
     55 settimes()
     56 {
     57     struct rusage ruch;
     58 
     59     (void) gettimeofday(&time0, NULL);
     60     (void) getrusage(RUSAGE_SELF, &ru0);
     61     (void) getrusage(RUSAGE_CHILDREN, &ruch);
     62     ruadd(&ru0, &ruch);
     63 }
     64 
     65 /*
     66  * dotime is only called if it is truly a builtin function and not a
     67  * prefix to another command
     68  */
     69 void
     70 /*ARGSUSED*/
     71 dotime(v, t)
     72     Char **v;
     73     struct command *t;
     74 {
     75     struct timeval timedol;
     76     struct rusage ru1, ruch;
     77 
     78     (void) getrusage(RUSAGE_SELF, &ru1);
     79     (void) getrusage(RUSAGE_CHILDREN, &ruch);
     80     ruadd(&ru1, &ruch);
     81     (void) gettimeofday(&timedol, NULL);
     82     prusage(&ru0, &ru1, &timedol, &time0);
     83 }
     84 
     85 /*
     86  * donice is only called when it on the line by itself or with a +- value
     87  */
     88 void
     89 /*ARGSUSED*/
     90 donice(v, t)
     91     Char **v;
     92     struct command *t;
     93 {
     94     register Char *cp;
     95     int     nval = 0;
     96 
     97     v++, cp = *v++;
     98     if (cp == 0)
     99 	nval = 4;
    100     else if (*v == 0 && any("+-", cp[0]))
    101 	nval = getn(cp);
    102     (void) setpriority(PRIO_PROCESS, 0, nval);
    103 }
    104 
    105 void
    106 ruadd(ru, ru2)
    107     register struct rusage *ru, *ru2;
    108 {
    109     tvadd(&ru->ru_utime, &ru2->ru_utime);
    110     tvadd(&ru->ru_stime, &ru2->ru_stime);
    111     if (ru2->ru_maxrss > ru->ru_maxrss)
    112 	ru->ru_maxrss = ru2->ru_maxrss;
    113 
    114     ru->ru_ixrss += ru2->ru_ixrss;
    115     ru->ru_idrss += ru2->ru_idrss;
    116     ru->ru_isrss += ru2->ru_isrss;
    117     ru->ru_minflt += ru2->ru_minflt;
    118     ru->ru_majflt += ru2->ru_majflt;
    119     ru->ru_nswap += ru2->ru_nswap;
    120     ru->ru_inblock += ru2->ru_inblock;
    121     ru->ru_oublock += ru2->ru_oublock;
    122     ru->ru_msgsnd += ru2->ru_msgsnd;
    123     ru->ru_msgrcv += ru2->ru_msgrcv;
    124     ru->ru_nsignals += ru2->ru_nsignals;
    125     ru->ru_nvcsw += ru2->ru_nvcsw;
    126     ru->ru_nivcsw += ru2->ru_nivcsw;
    127 }
    128 
    129 void
    130 prusage(r0, r1, e, b)
    131     register struct rusage *r0, *r1;
    132     struct timeval *e, *b;
    133 {
    134     register time_t t =
    135     (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
    136     (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
    137     (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
    138     (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
    139     register char *cp;
    140     register long i;
    141     register struct varent *vp = adrof(STRtime);
    142 
    143     int     ms =
    144     (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
    145 
    146     cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
    147 
    148     if (vp && vp->vec[0] && vp->vec[1])
    149 	cp = short2str(vp->vec[1]);
    150 
    151     for (; *cp; cp++)
    152 	if (*cp != '%')
    153 	    (void) fputc(*cp, cshout);
    154 	else if (cp[1])
    155 	    switch (*++cp) {
    156 
    157 	    case 'U':		/* user CPU time used */
    158 		pdeltat(&r1->ru_utime, &r0->ru_utime);
    159 		break;
    160 
    161 	    case 'S':		/* system CPU time used */
    162 		pdeltat(&r1->ru_stime, &r0->ru_stime);
    163 		break;
    164 
    165 	    case 'E':		/* elapsed (wall-clock) time */
    166 		pcsecs((long) ms);
    167 		break;
    168 
    169 	    case 'P':		/* percent time spent running */
    170 		/* check if it did not run at all */
    171 		i = (ms == 0) ? 0 : (t * 1000 / ms);
    172 		/* nn.n% */
    173 		(void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10);
    174 		break;
    175 
    176 	    case 'W':		/* number of swaps */
    177 		i = r1->ru_nswap - r0->ru_nswap;
    178 		(void) fprintf(cshout, "%ld", i);
    179 		break;
    180 
    181 	    case 'X':		/* (average) shared text size */
    182 		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
    183 			       (r1->ru_ixrss - r0->ru_ixrss) / t);
    184 		break;
    185 
    186 	    case 'D':		/* (average) unshared data size */
    187 		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
    188 			(r1->ru_idrss + r1->ru_isrss -
    189 			 (r0->ru_idrss + r0->ru_isrss)) / t);
    190 		break;
    191 
    192 	    case 'K':		/* (average) total data memory used  */
    193 		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
    194 			((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
    195 			 (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t);
    196 		break;
    197 
    198 	    case 'M':		/* max. Resident Set Size */
    199 		(void) fprintf(cshout, "%ld", r1->ru_maxrss / 2L);
    200 		break;
    201 
    202 	    case 'F':		/* page faults */
    203 		(void) fprintf(cshout, "%ld", r1->ru_majflt - r0->ru_majflt);
    204 		break;
    205 
    206 	    case 'R':		/* page reclaims */
    207 		(void) fprintf(cshout, "%ld", r1->ru_minflt - r0->ru_minflt);
    208 		break;
    209 
    210 	    case 'I':		/* FS blocks in */
    211 		(void) fprintf(cshout, "%ld", r1->ru_inblock - r0->ru_inblock);
    212 		break;
    213 
    214 	    case 'O':		/* FS blocks out */
    215 		(void) fprintf(cshout, "%ld", r1->ru_oublock - r0->ru_oublock);
    216 		break;
    217 
    218 	    case 'r':		/* socket messages recieved */
    219 		(void) fprintf(cshout, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
    220 		break;
    221 
    222 	    case 's':		/* socket messages sent */
    223 		(void) fprintf(cshout, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
    224 		break;
    225 
    226 	    case 'k':		/* number of signals recieved */
    227 		(void) fprintf(cshout, "%ld", r1->ru_nsignals-r0->ru_nsignals);
    228 		break;
    229 
    230 	    case 'w':		/* num. voluntary context switches (waits) */
    231 		(void) fprintf(cshout, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
    232 		break;
    233 
    234 	    case 'c':		/* num. involuntary context switches */
    235 		(void) fprintf(cshout, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
    236 		break;
    237 	    }
    238     (void) fputc('\n', cshout);
    239 }
    240 
    241 static void
    242 pdeltat(t1, t0)
    243     struct timeval *t1, *t0;
    244 {
    245     struct timeval td;
    246 
    247     tvsub(&td, t1, t0);
    248     (void) fprintf(cshout, "%d.%01d", td.tv_sec, td.tv_usec / 100000);
    249 }
    250 
    251 void
    252 tvadd(tsum, t0)
    253     struct timeval *tsum, *t0;
    254 {
    255 
    256     tsum->tv_sec += t0->tv_sec;
    257     tsum->tv_usec += t0->tv_usec;
    258     if (tsum->tv_usec > 1000000)
    259 	tsum->tv_sec++, tsum->tv_usec -= 1000000;
    260 }
    261 
    262 void
    263 tvsub(tdiff, t1, t0)
    264     struct timeval *tdiff, *t1, *t0;
    265 {
    266 
    267     tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
    268     tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
    269     if (tdiff->tv_usec < 0)
    270 	tdiff->tv_sec--, tdiff->tv_usec += 1000000;
    271 }
    272 
    273 #define  P2DIG(i) (void) fprintf(cshout, "%d%d", (i) / 10, (i) % 10)
    274 
    275 void
    276 psecs(l)
    277     long    l;
    278 {
    279     register int i;
    280 
    281     i = l / 3600;
    282     if (i) {
    283 	(void) fprintf(cshout, "%d:", i);
    284 	i = l % 3600;
    285 	P2DIG(i / 60);
    286 	goto minsec;
    287     }
    288     i = l;
    289     (void) fprintf(cshout, "%d", i / 60);
    290 minsec:
    291     i %= 60;
    292     (void) fputc(':', cshout);
    293     P2DIG(i);
    294 }
    295 
    296 void
    297 pcsecs(l)			/* PWP: print mm:ss.dd, l is in sec*100 */
    298     long    l;
    299 {
    300     register int i;
    301 
    302     i = l / 360000;
    303     if (i) {
    304 	(void) fprintf(cshout, "%d:", i);
    305 	i = (l % 360000) / 100;
    306 	P2DIG(i / 60);
    307 	goto minsec;
    308     }
    309     i = l / 100;
    310     (void) fprintf(cshout, "%d", i / 60);
    311 minsec:
    312     i %= 60;
    313     (void) fputc(':', cshout);
    314     P2DIG(i);
    315     (void) fputc('.', cshout);
    316     P2DIG((int) (l % 100));
    317 }
    318