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