Home | History | Annotate | Line # | Download | only in csh
time.c revision 1.12
      1 /* $NetBSD: time.c,v 1.12 2001/09/14 14:04:01 wiz 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.12 2001/09/14 14:04:01 wiz Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 
     47 #if __STDC__
     48 # include <stdarg.h>
     49 #else
     50 # include <varargs.h>
     51 #endif
     52 
     53 #include "csh.h"
     54 #include "extern.h"
     55 
     56 /*
     57  * C Shell - routines handling process timing and niceing
     58  */
     59 static void pdeltat(struct timeval *, struct timeval *);
     60 extern char *strpct(u_long num, u_long denom, u_int digits);
     61 
     62 void
     63 settimes(void)
     64 {
     65     struct rusage ruch;
     66 
     67     (void)gettimeofday(&time0, NULL);
     68     (void)getrusage(RUSAGE_SELF, &ru0);
     69     (void)getrusage(RUSAGE_CHILDREN, &ruch);
     70     ruadd(&ru0, &ruch);
     71 }
     72 
     73 /*
     74  * dotime is only called if it is truly a builtin function and not a
     75  * prefix to another command
     76  */
     77 void
     78 /*ARGSUSED*/
     79 dotime(Char **v, struct command *t)
     80 {
     81     struct rusage ru1, ruch;
     82     struct timeval timedol;
     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(Char **v, struct command *t)
     97 {
     98     Char *cp;
     99     int nval;
    100 
    101     nval = 0;
    102     v++;
    103     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(struct rusage *ru, struct rusage *ru2)
    113 {
    114     timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
    115     timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
    116     if (ru2->ru_maxrss > ru->ru_maxrss)
    117 	ru->ru_maxrss = ru2->ru_maxrss;
    118 
    119     ru->ru_ixrss += ru2->ru_ixrss;
    120     ru->ru_idrss += ru2->ru_idrss;
    121     ru->ru_isrss += ru2->ru_isrss;
    122     ru->ru_minflt += ru2->ru_minflt;
    123     ru->ru_majflt += ru2->ru_majflt;
    124     ru->ru_nswap += ru2->ru_nswap;
    125     ru->ru_inblock += ru2->ru_inblock;
    126     ru->ru_oublock += ru2->ru_oublock;
    127     ru->ru_msgsnd += ru2->ru_msgsnd;
    128     ru->ru_msgrcv += ru2->ru_msgrcv;
    129     ru->ru_nsignals += ru2->ru_nsignals;
    130     ru->ru_nvcsw += ru2->ru_nvcsw;
    131     ru->ru_nivcsw += ru2->ru_nivcsw;
    132 }
    133 
    134 void
    135 prusage(struct rusage *r0, struct rusage *r1, struct timeval *e,
    136         struct timeval *b)
    137 {
    138     struct varent *vp;
    139     char *cp;
    140     long i;
    141     time_t t;
    142     int ms;
    143 
    144     cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
    145     ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
    146     t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
    147         (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
    148         (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
    149         (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
    150     vp = adrof(STRtime);
    151 
    152     if (vp && vp->vec[0] && vp->vec[1])
    153 	cp = short2str(vp->vec[1]);
    154 
    155     for (; *cp; cp++)
    156 	if (*cp != '%')
    157 	    (void) fputc(*cp, cshout);
    158 	else if (cp[1])
    159 	    switch (*++cp) {
    160 	    case 'D':		/* (average) unshared data size */
    161 		(void)fprintf(cshout, "%ld", t == 0 ? 0L :
    162 			(r1->ru_idrss + r1->ru_isrss -
    163 			 (r0->ru_idrss + r0->ru_isrss)) / t);
    164 		break;
    165 	    case 'E':		/* elapsed (wall-clock) time */
    166 		pcsecs((long) ms);
    167 		break;
    168 	    case 'F':		/* page faults */
    169 		(void)fprintf(cshout, "%ld", r1->ru_majflt - r0->ru_majflt);
    170 		break;
    171 	    case 'I':		/* FS blocks in */
    172 		(void)fprintf(cshout, "%ld", r1->ru_inblock - r0->ru_inblock);
    173 		break;
    174 	    case 'K':		/* (average) total data memory used  */
    175 		(void)fprintf(cshout, "%ld", t == 0 ? 0L :
    176 			((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
    177 			 (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t);
    178 		break;
    179 	    case 'M':		/* max. Resident Set Size */
    180 		(void)fprintf(cshout, "%ld", r1->ru_maxrss / 2L);
    181 		break;
    182 	    case 'O':		/* FS blocks out */
    183 		(void)fprintf(cshout, "%ld", r1->ru_oublock - r0->ru_oublock);
    184 		break;
    185 	    case 'P':		/* percent time spent running */
    186 		/* check if it did not run at all */
    187 		if (ms == 0) {
    188 			(void)fputs("0.0%", cshout);
    189 		} else {
    190 			(void)fputs(strpct((ulong)t, (ulong)ms, 1), cshout);
    191 		}
    192 		break;
    193 	    case 'R':		/* page reclaims */
    194 		(void)fprintf(cshout, "%ld", r1->ru_minflt - r0->ru_minflt);
    195 		break;
    196 	    case 'S':		/* system CPU time used */
    197 		pdeltat(&r1->ru_stime, &r0->ru_stime);
    198 		break;
    199 	    case 'U':		/* user CPU time used */
    200 		pdeltat(&r1->ru_utime, &r0->ru_utime);
    201 		break;
    202 	    case 'W':		/* number of swaps */
    203 		i = r1->ru_nswap - r0->ru_nswap;
    204 		(void)fprintf(cshout, "%ld", i);
    205 		break;
    206 	    case 'X':		/* (average) shared text size */
    207 		(void)fprintf(cshout, "%ld", t == 0 ? 0L :
    208 			       (r1->ru_ixrss - r0->ru_ixrss) / t);
    209 		break;
    210 	    case 'c':		/* num. involuntary context switches */
    211 		(void)fprintf(cshout, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
    212 		break;
    213 	    case 'k':		/* number of signals received */
    214 		(void)fprintf(cshout, "%ld", r1->ru_nsignals-r0->ru_nsignals);
    215 		break;
    216 	    case 'r':		/* socket messages received */
    217 		(void)fprintf(cshout, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
    218 		break;
    219 	    case 's':		/* socket messages sent */
    220 		(void)fprintf(cshout, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
    221 		break;
    222 	    case 'w':		/* num. voluntary context switches (waits) */
    223 		(void)fprintf(cshout, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
    224 		break;
    225 	    }
    226     (void)fputc('\n', cshout);
    227 }
    228 
    229 static void
    230 pdeltat(struct timeval *t1, struct timeval *t0)
    231 {
    232     struct timeval td;
    233 
    234     timersub(t1, t0, &td);
    235     (void)fprintf(cshout, "%ld.%01ld", (long)td.tv_sec,
    236 	(long)(td.tv_usec / 100000));
    237 }
    238 
    239 #define  P2DIG(i) (void)fprintf(cshout, "%d%d", (i) / 10, (i) % 10)
    240 
    241 void
    242 psecs(long l)
    243 {
    244     int i;
    245 
    246     i = l / 3600;
    247     if (i) {
    248 	(void)fprintf(cshout, "%d:", i);
    249 	i = l % 3600;
    250 	P2DIG(i / 60);
    251 	goto minsec;
    252     }
    253     i = l;
    254     (void)fprintf(cshout, "%d", i / 60);
    255 minsec:
    256     i %= 60;
    257     (void)fputc(':', cshout);
    258     P2DIG(i);
    259 }
    260 
    261 void
    262 pcsecs(long l)			/* PWP: print mm:ss.dd, l is in sec*100 */
    263 {
    264     int i;
    265 
    266     i = l / 360000;
    267     if (i) {
    268 	(void)fprintf(cshout, "%d:", i);
    269 	i = (l % 360000) / 100;
    270 	P2DIG(i / 60);
    271 	goto minsec;
    272     }
    273     i = l / 100;
    274     (void)fprintf(cshout, "%d", i / 60);
    275 minsec:
    276     i %= 60;
    277     (void)fputc(':', cshout);
    278     P2DIG(i);
    279     (void)fputc('.', cshout);
    280     P2DIG((int) (l % 100));
    281 }
    282