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