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