time.c revision 1.9
1/*	$OpenBSD: time.c,v 1.9 2003/06/02 23:32:07 millert 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
35static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 5/31/93";
36#else
37static char rcsid[] = "$OpenBSD: time.c,v 1.9 2003/06/02 23:32:07 millert 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 */
50static void	pdeltat(struct timeval *, struct timeval *);
51
52void
53settimes()
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 */
67void
68/*ARGSUSED*/
69dotime(v, t)
70    Char **v;
71    struct command *t;
72{
73    struct timeval timedol;
74    struct rusage ru1, ruch;
75
76    (void) getrusage(RUSAGE_SELF, &ru1);
77    (void) getrusage(RUSAGE_CHILDREN, &ruch);
78    ruadd(&ru1, &ruch);
79    (void) gettimeofday(&timedol, NULL);
80    prusage(&ru0, &ru1, &timedol, &time0);
81}
82
83/*
84 * donice is only called when it on the line by itself or with a +- value
85 */
86void
87/*ARGSUSED*/
88donice(v, t)
89    Char **v;
90    struct command *t;
91{
92    register Char *cp;
93    int     nval = 0;
94
95    v++, cp = *v++;
96    if (cp == 0)
97	nval = 4;
98    else if (*v == 0 && any("+-", cp[0]))
99	nval = getn(cp);
100    (void) setpriority(PRIO_PROCESS, 0, nval);
101}
102
103void
104ruadd(ru, ru2)
105    register struct rusage *ru, *ru2;
106{
107    timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
108    timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
109    if (ru2->ru_maxrss > ru->ru_maxrss)
110	ru->ru_maxrss = ru2->ru_maxrss;
111
112    ru->ru_ixrss += ru2->ru_ixrss;
113    ru->ru_idrss += ru2->ru_idrss;
114    ru->ru_isrss += ru2->ru_isrss;
115    ru->ru_minflt += ru2->ru_minflt;
116    ru->ru_majflt += ru2->ru_majflt;
117    ru->ru_nswap += ru2->ru_nswap;
118    ru->ru_inblock += ru2->ru_inblock;
119    ru->ru_oublock += ru2->ru_oublock;
120    ru->ru_msgsnd += ru2->ru_msgsnd;
121    ru->ru_msgrcv += ru2->ru_msgrcv;
122    ru->ru_nsignals += ru2->ru_nsignals;
123    ru->ru_nvcsw += ru2->ru_nvcsw;
124    ru->ru_nivcsw += ru2->ru_nivcsw;
125}
126
127void
128prusage(r0, r1, e, b)
129    register struct rusage *r0, *r1;
130    struct timeval *e, *b;
131{
132    register time_t t =
133    (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
134    (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
135    (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
136    (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
137    register char *cp;
138    register long i;
139    register struct varent *vp = adrof(STRtime);
140
141    int     ms =
142    (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
143
144    cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
145
146    if (vp && vp->vec[0] && vp->vec[1])
147	cp = short2str(vp->vec[1]);
148
149    for (; *cp; cp++)
150	if (*cp != '%')
151	    (void) fputc(*cp, cshout);
152	else if (cp[1])
153	    switch (*++cp) {
154
155	    case 'U':		/* user CPU time used */
156		pdeltat(&r1->ru_utime, &r0->ru_utime);
157		break;
158
159	    case 'S':		/* system CPU time used */
160		pdeltat(&r1->ru_stime, &r0->ru_stime);
161		break;
162
163	    case 'E':		/* elapsed (wall-clock) time */
164		pcsecs((long) ms);
165		break;
166
167	    case 'P':		/* percent time spent running */
168		/* check if it did not run at all */
169		i = (ms == 0) ? 0 : ((long long)t * 1000 / ms);
170		/* nn.n% */
171		(void) fprintf(cshout, "%ld.%01ld%%", i / 10, i % 10);
172		break;
173
174	    case 'W':		/* number of swaps */
175		i = r1->ru_nswap - r0->ru_nswap;
176		(void) fprintf(cshout, "%ld", i);
177		break;
178
179	    case 'X':		/* (average) shared text size */
180		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
181			       (r1->ru_ixrss - r0->ru_ixrss) / t);
182		break;
183
184	    case 'D':		/* (average) unshared data size */
185		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
186			(r1->ru_idrss + r1->ru_isrss -
187			 (r0->ru_idrss + r0->ru_isrss)) / t);
188		break;
189
190	    case 'K':		/* (average) total data memory used  */
191		(void) fprintf(cshout, "%ld", t == 0 ? 0L :
192			((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
193			 (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t);
194		break;
195
196	    case 'M':		/* max. Resident Set Size */
197		(void) fprintf(cshout, "%ld", r1->ru_maxrss / 2L);
198		break;
199
200	    case 'F':		/* page faults */
201		(void) fprintf(cshout, "%ld", r1->ru_majflt - r0->ru_majflt);
202		break;
203
204	    case 'R':		/* page reclaims */
205		(void) fprintf(cshout, "%ld", r1->ru_minflt - r0->ru_minflt);
206		break;
207
208	    case 'I':		/* FS blocks in */
209		(void) fprintf(cshout, "%ld", r1->ru_inblock - r0->ru_inblock);
210		break;
211
212	    case 'O':		/* FS blocks out */
213		(void) fprintf(cshout, "%ld", r1->ru_oublock - r0->ru_oublock);
214		break;
215
216	    case 'r':		/* socket messages received */
217		(void) fprintf(cshout, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
218		break;
219
220	    case 's':		/* socket messages sent */
221		(void) fprintf(cshout, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
222		break;
223
224	    case 'k':		/* number of signals received */
225		(void) fprintf(cshout, "%ld", r1->ru_nsignals-r0->ru_nsignals);
226		break;
227
228	    case 'w':		/* num. voluntary context switches (waits) */
229		(void) fprintf(cshout, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
230		break;
231
232	    case 'c':		/* num. involuntary context switches */
233		(void) fprintf(cshout, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
234		break;
235	    }
236    (void) fputc('\n', cshout);
237}
238
239static void
240pdeltat(t1, t0)
241    struct timeval *t1, *t0;
242{
243    struct timeval td;
244
245    timersub(t1, t0, &td);
246    (void) fprintf(cshout, "%ld.%01ld", td.tv_sec, td.tv_usec / 100000);
247}
248
249#define  P2DIG(i) (void) fprintf(cshout, "%d%d", (i) / 10, (i) % 10)
250
251void
252psecs(l)
253    long    l;
254{
255    register int i;
256
257    i = l / 3600;
258    if (i) {
259	(void) fprintf(cshout, "%d:", i);
260	i = l % 3600;
261	P2DIG(i / 60);
262	goto minsec;
263    }
264    i = l;
265    (void) fprintf(cshout, "%d", i / 60);
266minsec:
267    i %= 60;
268    (void) fputc(':', cshout);
269    P2DIG(i);
270}
271
272void
273pcsecs(l)			/* PWP: print mm:ss.dd, l is in sec*100 */
274    long    l;
275{
276    register int i;
277
278    i = l / 360000;
279    if (i) {
280	(void) fprintf(cshout, "%d:", i);
281	i = (l % 360000) / 100;
282	P2DIG(i / 60);
283	goto minsec;
284    }
285    i = l / 100;
286    (void) fprintf(cshout, "%d", i / 60);
287minsec:
288    i %= 60;
289    (void) fputc(':', cshout);
290    P2DIG(i);
291    (void) fputc('.', cshout);
292    P2DIG((int) (l % 100));
293}
294