xref: /original-bsd/bin/csh/time.c (revision 5d3a6356)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley Software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 static char *sccsid = "@(#)time.c	5.6 (Berkeley) 05/19/88";
9 #endif
10 
11 #include "sh.h"
12 
13 /*
14  * C Shell - routines handling process timing and niceing
15  */
16 
17 settimes()
18 {
19 	struct rusage ruch;
20 
21 	(void) gettimeofday(&time0, (struct timezone *)0);
22 	(void) getrusage(RUSAGE_SELF, &ru0);
23 	(void) getrusage(RUSAGE_CHILDREN, &ruch);
24 	ruadd(&ru0, &ruch);
25 }
26 
27 /*
28  * dotime is only called if it is truly a builtin function and not a
29  * prefix to another command
30  */
31 dotime()
32 {
33 	struct timeval timedol;
34 	struct rusage ru1, ruch;
35 
36 	(void) getrusage(RUSAGE_SELF, &ru1);
37 	(void) getrusage(RUSAGE_CHILDREN, &ruch);
38 	ruadd(&ru1, &ruch);
39 	(void) gettimeofday(&timedol, (struct timezone *)0);
40 	prusage(&ru0, &ru1, &timedol, &time0);
41 }
42 
43 /*
44  * donice is only called when it on the line by itself or with a +- value
45  */
46 donice(v)
47 	register char **v;
48 {
49 	register char *cp;
50 	int nval;
51 
52 	v++, cp = *v++;
53 	if (cp == 0)
54 		nval = 4;
55 	else if (*v == 0 && any(cp[0], "+-"))
56 		nval = getn(cp);
57 	(void) setpriority(PRIO_PROCESS, 0, nval);
58 }
59 
60 ruadd(ru, ru2)
61 	register struct rusage *ru, *ru2;
62 {
63 	register long *lp, *lp2;
64 	register int cnt;
65 
66 	tvadd(&ru->ru_utime, &ru2->ru_utime);
67 	tvadd(&ru->ru_stime, &ru2->ru_stime);
68 	if (ru2->ru_maxrss > ru->ru_maxrss)
69 		ru->ru_maxrss = ru2->ru_maxrss;
70 	cnt = &ru->ru_last - &ru->ru_first + 1;
71 	lp = &ru->ru_first; lp2 = &ru2->ru_first;
72 	do
73 		*lp++ += *lp2++;
74 	while (--cnt > 0);
75 }
76 
77 prusage(r0, r1, e, b)
78 	register struct rusage *r0, *r1;
79 	struct timeval *e, *b;
80 {
81 	register time_t t =
82 	    (r1->ru_utime.tv_sec-r0->ru_utime.tv_sec)*100+
83 	    (r1->ru_utime.tv_usec-r0->ru_utime.tv_usec)/10000+
84 	    (r1->ru_stime.tv_sec-r0->ru_stime.tv_sec)*100+
85 	    (r1->ru_stime.tv_usec-r0->ru_stime.tv_usec)/10000;
86 	register char *cp;
87 	register long i;
88 	register struct varent *vp = adrof("time");
89 	long ms =
90 	    (e->tv_sec-b->tv_sec)*100 + (e->tv_usec-b->tv_usec)/10000;
91 
92 	cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
93 	if (vp && vp->vec[0] && vp->vec[1])
94 		cp = vp->vec[1];
95 	for (; *cp; cp++)
96 	if (*cp != '%')
97 		cshputchar(*cp);
98 	else if (cp[1]) switch(*++cp) {
99 
100 	case 'U':
101 		pdeltat(&r1->ru_utime, &r0->ru_utime);
102 		break;
103 
104 	case 'S':
105 		pdeltat(&r1->ru_stime, &r0->ru_stime);
106 		break;
107 
108 	case 'E':
109 		psecs(ms / 100);
110 		break;
111 
112 	case 'P':
113 		printf("%d%%", (int) (t*100 / ((ms ? ms : 1))));
114 		break;
115 
116 	case 'W':
117 		i = r1->ru_nswap - r0->ru_nswap;
118 		printf("%ld", i);
119 		break;
120 
121 	case 'X':
122 		printf("%ld", t == 0 ? 0L : (r1->ru_ixrss-r0->ru_ixrss)/t);
123 		break;
124 
125 	case 'D':
126 		printf("%ld", t == 0 ? 0L :
127 		    (r1->ru_idrss+r1->ru_isrss-(r0->ru_idrss+r0->ru_isrss))/t);
128 		break;
129 
130 	case 'K':
131 		printf("%ld", t == 0 ? 0L :
132 		    ((r1->ru_ixrss+r1->ru_isrss+r1->ru_idrss) -
133 		    (r0->ru_ixrss+r0->ru_idrss+r0->ru_isrss))/t);
134 		break;
135 
136 	case 'M':
137 		printf("%ld", r1->ru_maxrss/2);
138 		break;
139 
140 	case 'F':
141 		printf("%ld", r1->ru_majflt-r0->ru_majflt);
142 		break;
143 
144 	case 'R':
145 		printf("%ld", r1->ru_minflt-r0->ru_minflt);
146 		break;
147 
148 	case 'I':
149 		printf("%ld", r1->ru_inblock-r0->ru_inblock);
150 		break;
151 
152 	case 'O':
153 		printf("%ld", r1->ru_oublock-r0->ru_oublock);
154 		break;
155 	}
156 	cshputchar('\n');
157 }
158 
159 pdeltat(t1, t0)
160 	struct timeval *t1, *t0;
161 {
162 	struct timeval td;
163 
164 	tvsub(&td, t1, t0);
165 	printf("%d.%01d", td.tv_sec, td.tv_usec/100000);
166 }
167 
168 tvadd(tsum, t0)
169 	struct timeval *tsum, *t0;
170 {
171 
172 	tsum->tv_sec += t0->tv_sec;
173 	tsum->tv_usec += t0->tv_usec;
174 	if (tsum->tv_usec > 1000000)
175 		tsum->tv_sec++, tsum->tv_usec -= 1000000;
176 }
177 
178 tvsub(tdiff, t1, t0)
179 	struct timeval *tdiff, *t1, *t0;
180 {
181 
182 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
183 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
184 	if (tdiff->tv_usec < 0)
185 		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
186 }
187