xref: /netbsd-src/usr.bin/time/time.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1987, 1988 The Regents of the University of California.
3  * 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 char copyright[] =
36 "@(#) Copyright (c) 1987, 1988 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)time.c	4.9 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: time.c,v 1.4 1993/08/27 22:30:47 jtc Exp $";
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sys/signal.h>
49 #include <sys/wait.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <errno.h>
54 
55 int lflag;
56 int portableflag;
57 
58 int
59 main(argc, argv)
60 	int argc;
61 	char **argv;
62 {
63 	extern int optind;
64 	register int pid;
65 	int ch, status;
66 	struct timeval before, after;
67 	struct rusage ru;
68 
69 	lflag = 0;
70 	while ((ch = getopt(argc, argv, "lp")) != EOF)
71 		switch((char)ch) {
72 		case 'p':
73 			portableflag = 1;
74 			break;
75 		case 'l':
76 			lflag = 1;
77 			break;
78 		case '?':
79 		default:
80 			fprintf(stderr, "usage: time [-lp] command.\n");
81 			exit(1);
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 		perror("time");
92 		exit(1);
93 		/* NOTREACHED */
94 	case 0:				/* child */
95 		execvp(*argv, argv);
96 		perror(*argv);
97 		_exit((errno == ENOENT) ? 127 : 126);
98 		/* NOTREACHED */
99 	}
100 
101 	/* parent */
102 	(void)signal(SIGINT, SIG_IGN);
103 	(void)signal(SIGQUIT, SIG_IGN);
104 	while (wait3(&status, 0, &ru) != pid);
105 	gettimeofday(&after, (struct timezone *)NULL);
106 	if (!WIFEXITED(status))
107 		fprintf(stderr, "Command terminated abnormally.\n");
108 	after.tv_sec -= before.tv_sec;
109 	after.tv_usec -= before.tv_usec;
110 	if (after.tv_usec < 0)
111 		after.tv_sec--, after.tv_usec += 1000000;
112 
113 	if (portableflag) {
114 		fprintf (stderr, "real %9ld.%02ld\n",
115 			after.tv_sec, after.tv_usec/10000);
116 		fprintf (stderr, "user %9ld.%02ld\n",
117 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
118 		fprintf (stderr, "sys  %9ld.%02ld\n",
119 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
120 	} else {
121 
122 		fprintf(stderr, "%9ld.%02ld real ",
123 			after.tv_sec, after.tv_usec/10000);
124 		fprintf(stderr, "%9ld.%02ld user ",
125 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
126 		fprintf(stderr, "%9ld.%02ld sys\n",
127 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
128 	}
129 
130 	if (lflag) {
131 		int hz = 100;			/* XXX */
132 		long ticks;
133 
134 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
135 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
136 
137 		fprintf(stderr, "%10ld  %s\n",
138 			ru.ru_maxrss, "maximum resident set size");
139 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_ixrss / ticks : 0,
140 			"average shared memory size");
141 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_idrss / ticks : 0,
142 			"average unshared data size");
143 		fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_isrss / ticks : 0,
144 			"average unshared stack size");
145 		fprintf(stderr, "%10ld  %s\n",
146 			ru.ru_minflt, "page reclaims");
147 		fprintf(stderr, "%10ld  %s\n",
148 			ru.ru_majflt, "page faults");
149 		fprintf(stderr, "%10ld  %s\n",
150 			ru.ru_nswap, "swaps");
151 		fprintf(stderr, "%10ld  %s\n",
152 			ru.ru_inblock, "block input operations");
153 		fprintf(stderr, "%10ld  %s\n",
154 			ru.ru_oublock, "block output operations");
155 		fprintf(stderr, "%10ld  %s\n",
156 			ru.ru_msgsnd, "messages sent");
157 		fprintf(stderr, "%10ld  %s\n",
158 			ru.ru_msgrcv, "messages received");
159 		fprintf(stderr, "%10ld  %s\n",
160 			ru.ru_nsignals, "signals received");
161 		fprintf(stderr, "%10ld  %s\n",
162 			ru.ru_nvcsw, "voluntary context switches");
163 		fprintf(stderr, "%10ld  %s\n",
164 			ru.ru_nivcsw, "involuntary context switches");
165 	}
166 
167 	exit (WEXITSTATUS(status));
168 }
169