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