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