xref: /openbsd-src/usr.bin/time/time.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: time.c,v 1.21 2015/10/10 14:49:23 deraadt Exp $	*/
2 /*	$NetBSD: time.c,v 1.7 1995/06/27 00:34:00 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1987, 1988, 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/time.h>
34 #include <sys/resource.h>
35 #include <sys/wait.h>
36 #include <sys/sysctl.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 
45 int lflag;
46 int portableflag;
47 
48 __dead void usage(void);
49 
50 int
51 main(int argc, char *argv[])
52 {
53 	pid_t pid;
54 	int ch, status;
55 	struct timeval before, after;
56 	struct rusage ru;
57 	int exitonsig = 0;
58 
59 	if (pledge("stdio proc exec", NULL) == -1)
60 		err(1, "pledge");
61 
62 	while ((ch = getopt(argc, argv, "lp")) != -1) {
63 		switch(ch) {
64 		case 'l':
65 			lflag = 1;
66 			break;
67 		case 'p':
68 			portableflag = 1;
69 			break;
70 		default:
71 			usage();
72 			/* NOTREACHED */
73 		}
74 	}
75 
76 	argc -= optind;
77 	argv += optind;
78 
79 	if (argc < 1)
80 		usage();
81 
82 	gettimeofday(&before, (struct timezone *)NULL);
83 	switch(pid = vfork()) {
84 	case -1:			/* error */
85 		perror("time");
86 		exit(1);
87 		/* NOTREACHED */
88 	case 0:				/* child */
89 		execvp(*argv, argv);
90 		perror(*argv);
91 		_exit((errno == ENOENT) ? 127 : 126);
92 		/* NOTREACHED */
93 	}
94 
95 	/* parent */
96 	(void)signal(SIGINT, SIG_IGN);
97 	(void)signal(SIGQUIT, SIG_IGN);
98 	while (wait3(&status, 0, &ru) != pid)
99 		;
100 	gettimeofday(&after, (struct timezone *)NULL);
101 	if (WIFSIGNALED(status))
102 		exitonsig = WTERMSIG(status);
103 	if (!WIFEXITED(status))
104 		fprintf(stderr, "Command terminated abnormally.\n");
105 	timersub(&after, &before, &after);
106 
107 	if (portableflag) {
108 		fprintf(stderr, "real %9lld.%02ld\n",
109 			(long long)after.tv_sec, after.tv_usec/10000);
110 		fprintf(stderr, "user %9lld.%02ld\n",
111 			(long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
112 		fprintf(stderr, "sys  %9lld.%02ld\n",
113 			(long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
114 	} else {
115 
116 		fprintf(stderr, "%9lld.%02ld real ",
117 			(long long)after.tv_sec, after.tv_usec/10000);
118 		fprintf(stderr, "%9lld.%02ld user ",
119 			(long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
120 		fprintf(stderr, "%9lld.%02ld sys\n",
121 			(long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
122 	}
123 
124 	if (lflag) {
125 		int hz;
126 		long ticks;
127 		int mib[2];
128 		struct clockinfo clkinfo;
129 		size_t size;
130 
131 		mib[0] = CTL_KERN;
132 		mib[1] = KERN_CLOCKRATE;
133 		size = sizeof(clkinfo);
134 		if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0)
135 			err(1, "sysctl");
136 
137 		hz = clkinfo.hz;
138 
139 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
140 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
141 
142 		fprintf(stderr, "%10ld  %s\n",
143 			ru.ru_maxrss, "maximum resident set size");
144 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_ixrss / ticks : 0,
145 			"average shared memory size");
146 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_idrss / ticks : 0,
147 			"average unshared data size");
148 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_isrss / ticks : 0,
149 			"average unshared stack size");
150 		fprintf(stderr, "%10ld  %s\n",
151 			ru.ru_minflt, "minor page faults");
152 		fprintf(stderr, "%10ld  %s\n",
153 			ru.ru_majflt, "major page faults");
154 		fprintf(stderr, "%10ld  %s\n",
155 			ru.ru_nswap, "swaps");
156 		fprintf(stderr, "%10ld  %s\n",
157 			ru.ru_inblock, "block input operations");
158 		fprintf(stderr, "%10ld  %s\n",
159 			ru.ru_oublock, "block output operations");
160 		fprintf(stderr, "%10ld  %s\n",
161 			ru.ru_msgsnd, "messages sent");
162 		fprintf(stderr, "%10ld  %s\n",
163 			ru.ru_msgrcv, "messages received");
164 		fprintf(stderr, "%10ld  %s\n",
165 			ru.ru_nsignals, "signals received");
166 		fprintf(stderr, "%10ld  %s\n",
167 			ru.ru_nvcsw, "voluntary context switches");
168 		fprintf(stderr, "%10ld  %s\n",
169 			ru.ru_nivcsw, "involuntary context switches");
170 	}
171 
172 	if (exitonsig) {
173 		if (signal(exitonsig, SIG_DFL) == SIG_ERR)
174 			perror("signal");
175 		else
176 			kill(getpid(), exitonsig);
177 	}
178 	exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
179 }
180 
181 __dead void
182 usage(void)
183 {
184 	extern char *__progname;
185 
186 	(void)fprintf(stderr, "usage: %s [-lp] utility [argument ...]\n",
187 	    __progname);
188 	exit(1);
189 }
190