summaryrefslogtreecommitdiff
path: root/usr.bin/time/time.c
blob: 1d43d9f09b42623f2520170af447d3d2763e049b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*	$NetBSD: time.c,v 1.23 2017/07/15 14:34:08 christos Exp $	*/

/*
 * Copyright (c) 1987, 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <bsd/sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <errno.h>
#include <err.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <bsd/stdlib.h>
#include <unistd.h>
#include <time.h>

#include "strpct.h"

static void	usage(void);
static void	prl(long, const char *);
static void	prts(const char *, const char *, const struct timespec *,
    const char *);
static void	prtv(const char *, const char *, const struct timeval *,
    const char *);
void		prusage1(FILE *, const char *fmt, struct rusage *, struct rusage *,
    struct timespec *, struct timespec *);
static void	pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0);
static void	pcsecs(FILE *fp, long l);

int
main(int argc, char ** volatile argv)
{
	int pid;
	int ch, status;
	int volatile portableflag;
	int volatile lflag;
	const char *decpt;
	const char *fmt;
	const struct lconv *lconv;
	struct timespec before, after;
	struct rusage ru;

	(void)setlocale(LC_ALL, "");

	lflag = portableflag = 0;
	fmt = NULL;
	while ((ch = getopt(argc, argv, "cf:lp")) != -1) {
		switch (ch) {
		case 'f':
			fmt = optarg;
			portableflag = 0;
			lflag = 0;
			break;
		case 'c':
			fmt = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
			portableflag = 0;
			lflag = 0;
			break;
		case 'p':
			portableflag = 1;
			fmt = NULL;
			lflag = 0;
			break;
		case 'l':
			lflag = 1;
			portableflag = 0;
			fmt = NULL;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1)
		usage();

	(void)clock_gettime(CLOCK_MONOTONIC, &before);
	switch(pid = vfork()) {
	case -1:			/* error */
		err(EXIT_FAILURE, "Vfork failed");
		/* NOTREACHED */
	case 0:				/* child */
		/* LINTED will return only on failure */
		execvp(*argv, argv);
		err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
		/* NOTREACHED */
	}

	/* parent */
	(void)signal(SIGINT, SIG_IGN);
	(void)signal(SIGQUIT, SIG_IGN);
	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
		err(EXIT_FAILURE, "wait4 %d failed", pid);
	(void)clock_gettime(CLOCK_MONOTONIC, &after);
	if (!WIFEXITED(status))
		warnx("Command terminated abnormally.");
	timespecsub(&after, &before, &after);

	if ((lconv = localeconv()) == NULL ||
	    (decpt = lconv->decimal_point) == NULL)
		decpt = ".";

	if (fmt) {
		static struct rusage null_ru;
		before.tv_sec = 0;
		before.tv_nsec = 0;
		prusage1(stderr, fmt, &null_ru, &ru, &after, &before);
	} else if (portableflag) {
		prts("real ", decpt, &after, "\n");
		prtv("user ", decpt, &ru.ru_utime, "\n");
		prtv("sys  ", decpt, &ru.ru_stime, "\n");
	} else {
		prts("", decpt, &after, " real ");
		prtv("", decpt, &ru.ru_utime, " user ");
		prtv("", decpt, &ru.ru_stime, " sys\n");
	}

	if (lflag) {
		int hz = (int)sysconf(_SC_CLK_TCK);
		unsigned long long ticks;
#define SCALE(x) (long)(ticks ? x / ticks : 0)

		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
		    hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
		prl(ru.ru_maxrss, "maximum resident set size");
		prl(SCALE(ru.ru_ixrss), "average shared memory size");
		prl(SCALE(ru.ru_idrss), "average unshared data size");
		prl(SCALE(ru.ru_isrss), "average unshared stack size");
		prl(ru.ru_minflt, "page reclaims");
		prl(ru.ru_majflt, "page faults");
		prl(ru.ru_nswap, "swaps");
		prl(ru.ru_inblock, "block input operations");
		prl(ru.ru_oublock, "block output operations");
		prl(ru.ru_msgsnd, "messages sent");
		prl(ru.ru_msgrcv, "messages received");
		prl(ru.ru_nsignals, "signals received");
		prl(ru.ru_nvcsw, "voluntary context switches");
		prl(ru.ru_nivcsw, "involuntary context switches");
	}

	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
}

static void
usage(void)
{

	(void)fprintf(stderr, "Usage: %s [-clp] [-f <fmt>] utility [argument ...]\n",
	    getprogname());
	exit(EXIT_FAILURE);
}

static void
prl(long val, const char *expn)
{

	(void)fprintf(stderr, "%10ld  %s\n", val, expn);
}

static void
prts(const char *pre, const char *decpt, const struct timespec *ts,
    const char *post)
{

	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)ts->tv_sec,
	    decpt, (long)ts->tv_nsec / 10000000, post);
}

static void
prtv(const char *pre, const char *decpt, const struct timeval *tv,
    const char *post)
{

	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)tv->tv_sec,
	    decpt, (long)tv->tv_usec / 10000, post);
}

void
prusage1(FILE *fp, const char *cp, struct rusage *r0, struct rusage *r1,
    struct timespec *e, struct timespec *b)
{
    long i;
    time_t t;
    time_t ms;

    ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
    t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
        (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
        (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
        (r1->ru_stime.tv_usec - r0->ru_stime.tv_usec) / 10000;

    for (; *cp; cp++)
        if (*cp != '%')
            (void) fputc(*cp, fp);
        else if (cp[1])
            switch (*++cp) {
            case 'D':           /* (average) unshared data size */
                (void)fprintf(fp, "%ld", t == 0 ? 0L :
                        (long)((r1->ru_idrss + r1->ru_isrss -
                         (r0->ru_idrss + r0->ru_isrss)) / t));
                break;
            case 'E':           /* elapsed (wall-clock) time */
                pcsecs(fp, (long) ms);
                break;
            case 'F':           /* page faults */
                (void)fprintf(fp, "%ld", r1->ru_majflt - r0->ru_majflt);
                break;
            case 'I':           /* FS blocks in */
                (void)fprintf(fp, "%ld", r1->ru_inblock - r0->ru_inblock);
                break;
            case 'K':           /* (average) total data memory used  */
                (void)fprintf(fp, "%ld", t == 0 ? 0L :
                        (long)(((r1->ru_ixrss + r1->ru_isrss + r1->ru_idrss) -
                         (r0->ru_ixrss + r0->ru_idrss + r0->ru_isrss)) / t));
                break;
            case 'M':           /* max. Resident Set Size */
                (void)fprintf(fp, "%ld", r1->ru_maxrss / 2L);
                break;
            case 'O':           /* FS blocks out */
                (void)fprintf(fp, "%ld", r1->ru_oublock - r0->ru_oublock);
                break;
            case 'P':           /* percent time spent running */
                /* check if it did not run at all */
                if (ms == 0) {
                        (void)fputs("0.0%", fp);
                } else {
                        char pb[32];
                        (void)fputs(strpct(pb, sizeof(pb),
                            (uintmax_t)t, (uintmax_t)ms, 1), fp);
                        (void)fputc('%', fp);
                }
                break;
            case 'R':           /* page reclaims */
                (void)fprintf(fp, "%ld", r1->ru_minflt - r0->ru_minflt);
                break;
            case 'S':           /* system CPU time used */
                pdeltat(fp, &r1->ru_stime, &r0->ru_stime);
		break;
            case 'U':           /* user CPU time used */
                pdeltat(fp, &r1->ru_utime, &r0->ru_utime);
                break;
            case 'W':           /* number of swaps */
                i = r1->ru_nswap - r0->ru_nswap;
                (void)fprintf(fp, "%ld", i);
                break;
            case 'X':           /* (average) shared text size */
                (void)fprintf(fp, "%ld", t == 0 ? 0L :
                               (long)((r1->ru_ixrss - r0->ru_ixrss) / t));
                break;
            case 'c':           /* num. involuntary context switches */
                (void)fprintf(fp, "%ld", r1->ru_nivcsw - r0->ru_nivcsw);
                break;
            case 'k':           /* number of signals received */
                (void)fprintf(fp, "%ld", r1->ru_nsignals-r0->ru_nsignals);
                break;
            case 'r':           /* socket messages received */
                (void)fprintf(fp, "%ld", r1->ru_msgrcv - r0->ru_msgrcv);
                break;
            case 's':           /* socket messages sent */
                (void)fprintf(fp, "%ld", r1->ru_msgsnd - r0->ru_msgsnd);
                break;
            case 'w':           /* num. voluntary context switches (waits) */
                (void)fprintf(fp, "%ld", r1->ru_nvcsw - r0->ru_nvcsw);
                break;
            }
    (void)fputc('\n', fp);
}

static void
pdeltat(FILE *fp, struct timeval *t1, struct timeval *t0)
{
    struct timeval td;

    timersub(t1, t0, &td);
    (void)fprintf(fp, "%ld.%01ld", (long)td.tv_sec,
        (long)(td.tv_usec / 100000));
}

#define  P2DIG(fp, i) (void)fprintf(fp, "%ld%ld", (i) / 10, (i) % 10)

static void
pcsecs(FILE *fp, long l)        /* PWP: print mm:ss.dd, l is in sec*100 */
{
    long i;

    i = l / 360000;
    if (i) {
        (void)fprintf(fp, "%ld:", i);
        i = (l % 360000) / 100;
        P2DIG(fp, i / 60);
        goto minsec;
    }
    i = l / 100;
    (void)fprintf(fp, "%ld", i / 60);
minsec:
    i %= 60;
    (void)fputc(':', fp);
    P2DIG(fp, i);
    (void)fputc('.', fp);
    P2DIG(fp, (l % 100));
}