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