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