xref: /freebsd/usr.bin/time/time.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
1 /*
2  * Copyright (c) 1987, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1988, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45 	"$Id$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/signal.h>
52 #include <sys/sysctl.h>
53 #include <stdlib.h>
54 #include <sys/wait.h>
55 
56 #include <err.h>
57 #include <stdio.h>
58 #include <unistd.h>
59 
60 static int getstathz __P((void));
61 static void usage __P((void));
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char **argv;
67 {
68 	register int pid;
69 	int ch, status, lflag;
70 	struct timeval before, after;
71 	struct rusage ru;
72 
73 	lflag = 0;
74 	while ((ch = getopt(argc, argv, "l")) != -1)
75 		switch((char)ch) {
76 		case 'l':
77 			lflag = 1;
78 			break;
79 		case '?':
80 		default:
81 			usage();
82 		}
83 
84 	if (!(argc -= optind))
85 		exit(0);
86 	argv += optind;
87 
88 	gettimeofday(&before, (struct timezone *)NULL);
89 	switch(pid = vfork()) {
90 	case -1:			/* error */
91 		err(1, "time");
92 		/* NOTREACHED */
93 	case 0:				/* child */
94 		execvp(*argv, argv);
95 		warn("%s", *argv);
96 		_exit(1);
97 		/* NOTREACHED */
98 	}
99 	/* parent */
100 	(void)signal(SIGINT, SIG_IGN);
101 	(void)signal(SIGQUIT, SIG_IGN);
102 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
103 	gettimeofday(&after, (struct timezone *)NULL);
104 	if (status&0377)
105 		warnx("command terminated abnormally");
106 	after.tv_sec -= before.tv_sec;
107 	after.tv_usec -= before.tv_usec;
108 	if (after.tv_usec < 0)
109 		after.tv_sec--, after.tv_usec += 1000000;
110 	fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
111 	fprintf(stderr, "%9ld.%02ld user ",
112 	    ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
113 	fprintf(stderr, "%9ld.%02ld sys\n",
114 	    ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
115 	if (lflag) {
116 		int hz = getstathz();
117 		u_long ticks;
118 
119 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
120 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
121 
122 		/*
123 		 * If our round-off on the tick calculation still puts us at 0,
124 		 * then always assume at least one tick.
125 		 */
126 		if (ticks == 0)
127 			ticks = 1;
128 
129 		fprintf(stderr, "%10ld  %s\n",
130 			ru.ru_maxrss, "maximum resident set size");
131 		fprintf(stderr, "%10ld  %s\n",
132 			ru.ru_ixrss / ticks, "average shared memory size");
133 		fprintf(stderr, "%10ld  %s\n",
134 			ru.ru_idrss / ticks, "average unshared data size");
135 		fprintf(stderr, "%10ld  %s\n",
136 			ru.ru_isrss / ticks, "average unshared stack size");
137 		fprintf(stderr, "%10ld  %s\n",
138 			ru.ru_minflt, "page reclaims");
139 		fprintf(stderr, "%10ld  %s\n",
140 			ru.ru_majflt, "page faults");
141 		fprintf(stderr, "%10ld  %s\n",
142 			ru.ru_nswap, "swaps");
143 		fprintf(stderr, "%10ld  %s\n",
144 			ru.ru_inblock, "block input operations");
145 		fprintf(stderr, "%10ld  %s\n",
146 			ru.ru_oublock, "block output operations");
147 		fprintf(stderr, "%10ld  %s\n",
148 			ru.ru_msgsnd, "messages sent");
149 		fprintf(stderr, "%10ld  %s\n",
150 			ru.ru_msgrcv, "messages received");
151 		fprintf(stderr, "%10ld  %s\n",
152 			ru.ru_nsignals, "signals received");
153 		fprintf(stderr, "%10ld  %s\n",
154 			ru.ru_nvcsw, "voluntary context switches");
155 		fprintf(stderr, "%10ld  %s\n",
156 			ru.ru_nivcsw, "involuntary context switches");
157 	}
158 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
159 }
160 
161 static void
162 usage()
163 {
164 	fprintf(stderr, "usage: time [-l] command\n");
165 	exit(1);
166 }
167 
168 /*
169  * Return the frequency of the kernel's statistics clock.
170  */
171 static int
172 getstathz()
173 {
174 	struct clockinfo clockrate;
175 	int mib[2];
176 	size_t size;
177 
178 	mib[0] = CTL_KERN;
179 	mib[1] = KERN_CLOCKRATE;
180 	size = sizeof clockrate;
181 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
182 		err(1, "sysctl kern.clockrate");
183 	return clockrate.stathz;
184 }
185