1*e5ab0163Smlelstv /* $NetBSD: time.c,v 1.23 2020/10/17 08:46:02 mlelstv Exp $ */
249f0ad86Scgd
361f28255Scgd /*-
4cee2bad8Smycroft * Copyright (c) 1980, 1991, 1993
5cee2bad8Smycroft * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15b5b29542Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
328ea378c6Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3449f0ad86Scgd #if 0
3549f0ad86Scgd static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 5/31/93";
3649f0ad86Scgd #else
37*e5ab0163Smlelstv __RCSID("$NetBSD: time.c,v 1.23 2020/10/17 08:46:02 mlelstv Exp $");
3849f0ad86Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd
41271dc0c3Smatt #ifndef NOT_CSH
4261f28255Scgd #include <sys/types.h>
4361f28255Scgd #include <stdarg.h>
4461f28255Scgd #include "csh.h"
4561f28255Scgd #include "extern.h"
46271dc0c3Smatt #endif
47bc4c3c57Schristos #include <util.h>
4861f28255Scgd
4961f28255Scgd /*
5061f28255Scgd * C Shell - routines handling process timing and niceing
5161f28255Scgd */
52ccc205ceSsimonb static void pdeltat(FILE *, int, struct timeval *, struct timeval *);
53271dc0c3Smatt static void pcsecs(FILE *, long);
5461f28255Scgd
55271dc0c3Smatt #ifndef NOT_CSH
5661f28255Scgd void
settimes(void)57b771e65bSwiz settimes(void)
5861f28255Scgd {
5961f28255Scgd struct rusage ruch;
6061f28255Scgd
61c4753f0fSchristos (void)clock_gettime(CLOCK_MONOTONIC, &time0);
6261f28255Scgd (void)getrusage(RUSAGE_SELF, &ru0);
6361f28255Scgd (void)getrusage(RUSAGE_CHILDREN, &ruch);
6461f28255Scgd ruadd(&ru0, &ruch);
6561f28255Scgd }
6661f28255Scgd
6761f28255Scgd /*
6861f28255Scgd * dotime is only called if it is truly a builtin function and not a
6961f28255Scgd * prefix to another command
7061f28255Scgd */
7161f28255Scgd void
72cee2bad8Smycroft /*ARGSUSED*/
dotime(Char ** v,struct command * t)73b771e65bSwiz dotime(Char **v, struct command *t)
7461f28255Scgd {
7561f28255Scgd struct rusage ru1, ruch;
76c4753f0fSchristos struct timespec timedol;
7761f28255Scgd
7861f28255Scgd (void)getrusage(RUSAGE_SELF, &ru1);
7961f28255Scgd (void)getrusage(RUSAGE_CHILDREN, &ruch);
8061f28255Scgd ruadd(&ru1, &ruch);
81c4753f0fSchristos (void)clock_gettime(CLOCK_MONOTONIC, &timedol);
82271dc0c3Smatt prusage(cshout, &ru0, &ru1, &timedol, &time0);
8361f28255Scgd }
8461f28255Scgd
8561f28255Scgd /*
8661f28255Scgd * donice is only called when it on the line by itself or with a +- value
8761f28255Scgd */
8861f28255Scgd void
89cee2bad8Smycroft /*ARGSUSED*/
donice(Char ** v,struct command * t)90b771e65bSwiz donice(Char **v, struct command *t)
9161f28255Scgd {
9276adbe2bStls Char *cp;
93b771e65bSwiz int nval;
9461f28255Scgd
95b771e65bSwiz nval = 0;
96b771e65bSwiz v++;
97b771e65bSwiz cp = *v++;
9861f28255Scgd if (cp == 0)
9961f28255Scgd nval = 4;
10061f28255Scgd else if (*v == 0 && any("+-", cp[0]))
10161f28255Scgd nval = getn(cp);
10261f28255Scgd (void)setpriority(PRIO_PROCESS, 0, nval);
10361f28255Scgd }
10461f28255Scgd
10561f28255Scgd void
ruadd(struct rusage * ru,struct rusage * ru2)106b771e65bSwiz ruadd(struct rusage *ru, struct rusage *ru2)
10761f28255Scgd {
108ffe9a4ccSmycroft timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
109ffe9a4ccSmycroft timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
11061f28255Scgd if (ru2->ru_maxrss > ru->ru_maxrss)
11161f28255Scgd ru->ru_maxrss = ru2->ru_maxrss;
11261f28255Scgd
11361f28255Scgd ru->ru_ixrss += ru2->ru_ixrss;
11461f28255Scgd ru->ru_idrss += ru2->ru_idrss;
11561f28255Scgd ru->ru_isrss += ru2->ru_isrss;
11661f28255Scgd ru->ru_minflt += ru2->ru_minflt;
11761f28255Scgd ru->ru_majflt += ru2->ru_majflt;
11861f28255Scgd ru->ru_nswap += ru2->ru_nswap;
11961f28255Scgd ru->ru_inblock += ru2->ru_inblock;
12061f28255Scgd ru->ru_oublock += ru2->ru_oublock;
12161f28255Scgd ru->ru_msgsnd += ru2->ru_msgsnd;
12261f28255Scgd ru->ru_msgrcv += ru2->ru_msgrcv;
12361f28255Scgd ru->ru_nsignals += ru2->ru_nsignals;
12461f28255Scgd ru->ru_nvcsw += ru2->ru_nvcsw;
12561f28255Scgd ru->ru_nivcsw += ru2->ru_nivcsw;
12661f28255Scgd }
12761f28255Scgd
12861f28255Scgd void
prusage(FILE * fp,struct rusage * r0,struct rusage * r1,struct timespec * e,struct timespec * b)129c4753f0fSchristos prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timespec *e,
130c4753f0fSchristos struct timespec *b)
13161f28255Scgd {
132b771e65bSwiz struct varent *vp;
1336310b596Schristos const char *cp;
134397b1102Schristos
135397b1102Schristos vp = adrof(STRtime);
136397b1102Schristos
137397b1102Schristos if (vp && vp->vec[0] && vp->vec[1])
138397b1102Schristos cp = short2str(vp->vec[1]);
139397b1102Schristos else
140397b1102Schristos cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
141ccc205ceSsimonb prusage1(fp, cp, 1, r0, r1, e, b);
142397b1102Schristos }
143397b1102Schristos #endif
144397b1102Schristos
145397b1102Schristos void
prusage1(FILE * fp,const char * cp,int prec,struct rusage * r0,struct rusage * r1,struct timespec * e,struct timespec * b)146ccc205ceSsimonb prusage1(FILE *fp, const char *cp, int prec,
147ccc205ceSsimonb struct rusage *r0, struct rusage *r1,
148397b1102Schristos struct timespec *e, struct timespec *b)
149397b1102Schristos {
150b771e65bSwiz long i;
151b771e65bSwiz time_t t;
15237e39248Schristos time_t ms;
153b771e65bSwiz
154c4753f0fSchristos ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
155b771e65bSwiz t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
15661f28255Scgd (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
15761f28255Scgd (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
15861f28255Scgd (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;
15961f28255Scgd
16061f28255Scgd for (; *cp; cp++)
16161f28255Scgd if (*cp != '%')
162271dc0c3Smatt (void) fputc(*cp, fp);
16361f28255Scgd else if (cp[1])
16461f28255Scgd switch (*++cp) {
165b771e65bSwiz case 'D': /* (average) unshared data size */
166271dc0c3Smatt (void)fprintf(fp, "%ld", t == 0 ? 0L :
1678392979dSdholland (long)((r1->ru_idrss + r1->ru_isrss -
1688392979dSdholland (r0->ru_idrss + r0->ru_isrss)) / t));
16961f28255Scgd break;
17061f28255Scgd case 'E': /* elapsed (wall-clock) time */
171271dc0c3Smatt pcsecs(fp, (long) ms);
17261f28255Scgd break;
173b771e65bSwiz case 'F': /* page faults */
174271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_majflt - r0->ru_majflt);
175b771e65bSwiz break;
176b771e65bSwiz case 'I': /* FS blocks in */
177271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_inblock - r0->ru_inblock);
178b771e65bSwiz break;
179b771e65bSwiz case 'K': /* (average) total data memory used */
180271dc0c3Smatt (void)fprintf(fp, "%ld", t == 0 ? 0L :
1818392979dSdholland (long)(((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
1828392979dSdholland (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t));
183b771e65bSwiz break;
184b771e65bSwiz case 'M': /* max. Resident Set Size */
185*e5ab0163Smlelstv (void)fprintf(fp, "%ld", r1->ru_maxrss);
186b771e65bSwiz break;
187b771e65bSwiz case 'O': /* FS blocks out */
188271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_oublock - r0->ru_oublock);
189b771e65bSwiz break;
19061f28255Scgd case 'P': /* percent time spent running */
19161f28255Scgd /* check if it did not run at all */
192000297a9Sfair if (ms == 0) {
193271dc0c3Smatt (void)fputs("0.0%", fp);
194000297a9Sfair } else {
195bc4c3c57Schristos char pb[32];
19637e39248Schristos (void)fputs(strpct(pb, sizeof(pb),
19737e39248Schristos (uintmax_t)t, (uintmax_t)ms, 1), fp);
198bc4c3c57Schristos (void)fputc('%', fp);
199000297a9Sfair }
20061f28255Scgd break;
201b771e65bSwiz case 'R': /* page reclaims */
202271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_minflt - r0->ru_minflt);
203b771e65bSwiz break;
204b771e65bSwiz case 'S': /* system CPU time used */
205ccc205ceSsimonb pdeltat(fp, prec, &r1->ru_stime, &r0->ru_stime);
206b771e65bSwiz break;
207b771e65bSwiz case 'U': /* user CPU time used */
208ccc205ceSsimonb pdeltat(fp, prec, &r1->ru_utime, &r0->ru_utime);
209b771e65bSwiz break;
21061f28255Scgd case 'W': /* number of swaps */
21161f28255Scgd i = r1->ru_nswap - r0->ru_nswap;
212271dc0c3Smatt (void)fprintf(fp, "%ld", i);
21361f28255Scgd break;
21461f28255Scgd case 'X': /* (average) shared text size */
215271dc0c3Smatt (void)fprintf(fp, "%ld", t == 0 ? 0L :
2168392979dSdholland (long)((r1->ru_ixrss - r0->ru_ixrss) / t));
21761f28255Scgd break;
218b771e65bSwiz case 'c': /* num. involuntary context switches */
219271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
22061f28255Scgd break;
2210a600be8Swiz case 'k': /* number of signals received */
222271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_nsignals-r0->ru_nsignals);
22361f28255Scgd break;
224b771e65bSwiz case 'r': /* socket messages received */
225271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
226b771e65bSwiz break;
227b771e65bSwiz case 's': /* socket messages sent */
228271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
229b771e65bSwiz break;
23061f28255Scgd case 'w': /* num. voluntary context switches (waits) */
231271dc0c3Smatt (void)fprintf(fp, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
23261f28255Scgd break;
23361f28255Scgd }
234271dc0c3Smatt (void)fputc('\n', fp);
23561f28255Scgd }
23661f28255Scgd
23761f28255Scgd static void
pdeltat(FILE * fp,int prec,struct timeval * t1,struct timeval * t0)238ccc205ceSsimonb pdeltat(FILE *fp, int prec, struct timeval *t1, struct timeval *t0)
23961f28255Scgd {
24061f28255Scgd struct timeval td;
24161f28255Scgd
242ffe9a4ccSmycroft timersub(t1, t0, &td);
243ccc205ceSsimonb (void)fprintf(fp, "%ld.%0*ld", (long)td.tv_sec,
244ccc205ceSsimonb prec, (long)(td.tv_usec / 100000));
24561f28255Scgd }
24661f28255Scgd
24737e39248Schristos #define P2DIG(fp, i) (void)fprintf(fp, "%ld%ld", (i) / 10, (i) % 10)
248cee2bad8Smycroft
249271dc0c3Smatt #ifndef NOT_CSH
250cee2bad8Smycroft void
psecs(long l)251b771e65bSwiz psecs(long l)
252cee2bad8Smycroft {
25337e39248Schristos long i;
254cee2bad8Smycroft
255cee2bad8Smycroft i = l / 3600;
256cee2bad8Smycroft if (i) {
25737e39248Schristos (void)fprintf(cshout, "%ld:", i);
258cee2bad8Smycroft i = l % 3600;
259271dc0c3Smatt P2DIG(cshout, i / 60);
260cee2bad8Smycroft goto minsec;
261cee2bad8Smycroft }
262cee2bad8Smycroft i = l;
26337e39248Schristos (void)fprintf(cshout, "%ld", i / 60);
264cee2bad8Smycroft minsec:
265cee2bad8Smycroft i %= 60;
266cee2bad8Smycroft (void)fputc(':', cshout);
267271dc0c3Smatt P2DIG(cshout, i);
268cee2bad8Smycroft }
269271dc0c3Smatt #endif
270cee2bad8Smycroft
271271dc0c3Smatt static void
pcsecs(FILE * fp,long l)272271dc0c3Smatt pcsecs(FILE *fp, long l) /* PWP: print mm:ss.dd, l is in sec*100 */
273cee2bad8Smycroft {
27437e39248Schristos long i;
275cee2bad8Smycroft
276cee2bad8Smycroft i = l / 360000;
277cee2bad8Smycroft if (i) {
27837e39248Schristos (void)fprintf(fp, "%ld:", i);
279cee2bad8Smycroft i = (l % 360000) / 100;
280271dc0c3Smatt P2DIG(fp, i / 60);
281cee2bad8Smycroft goto minsec;
282cee2bad8Smycroft }
283cee2bad8Smycroft i = l / 100;
28437e39248Schristos (void)fprintf(fp, "%ld", i / 60);
285cee2bad8Smycroft minsec:
286cee2bad8Smycroft i %= 60;
287271dc0c3Smatt (void)fputc(':', fp);
288271dc0c3Smatt P2DIG(fp, i);
289271dc0c3Smatt (void)fputc('.', fp);
29037e39248Schristos P2DIG(fp, (l % 100));
291cee2bad8Smycroft }
292