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