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