1 /** $MirOS: src/usr.bin/time/time.c,v 1.2 2005/03/13 18:33:46 tg Exp $ */
2 /* $OpenBSD: time.c,v 1.15 2003/06/10 22:20:53 deraadt Exp $ */
3 /* $NetBSD: time.c,v 1.7 1995/06/27 00:34:00 jtc Exp $ */
4
5 /*
6 * Copyright (c) 1987, 1988, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. 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 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 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
43 #include <sys/wait.h>
44 #include <sys/sysctl.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 __SCCSID("@(#)time.c 8.1 (Berkeley) 6/6/93");
54 __RCSID("$MirOS: src/usr.bin/time/time.c,v 1.2 2005/03/13 18:33:46 tg Exp $");
55
56 int lflag;
57 int portableflag;
58
59 __dead void usage(void);
60
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 pid_t pid;
65 int ch, status;
66 struct timeval before, after;
67 struct rusage ru;
68 int exitonsig = 0;
69
70
71 while ((ch = getopt(argc, argv, "lp")) != -1) {
72 switch(ch) {
73 case 'l':
74 lflag = 1;
75 break;
76 case 'p':
77 portableflag = 1;
78 break;
79 case '?':
80 default:
81 usage();
82 /* NOTREACHED */
83 }
84 }
85
86 argc -= optind;
87 argv += optind;
88
89 if (argc < 1)
90 usage();
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 ;
110 gettimeofday(&after, (struct timezone *)NULL);
111 if (WIFSIGNALED(status))
112 exitonsig = WTERMSIG(status);
113 if (!WIFEXITED(status))
114 fprintf(stderr, "Command terminated abnormally.\n");
115 timersub(&after, &before, &after);
116
117 if (portableflag) {
118 fprintf(stderr, "real %9lld.%02ld\n",
119 (int64_t)after.tv_sec, after.tv_usec/10000);
120 fprintf(stderr, "user %9lld.%02ld\n",
121 (int64_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
122 fprintf(stderr, "sys %9lld.%02ld\n",
123 (int64_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
124 } else {
125
126 fprintf(stderr, "%9lld.%02ld real ",
127 (int64_t)after.tv_sec, after.tv_usec/10000);
128 fprintf(stderr, "%9lld.%02ld user ",
129 (int64_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
130 fprintf(stderr, "%9lld.%02ld sys\n",
131 (int64_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
132 }
133
134 if (lflag) {
135 int hz;
136 long ticks;
137 int mib[2];
138 struct clockinfo clkinfo;
139 size_t size;
140
141 mib[0] = CTL_KERN;
142 mib[1] = KERN_CLOCKRATE;
143 size = sizeof(clkinfo);
144 if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0)
145 err(1, "sysctl");
146
147 hz = clkinfo.hz;
148
149 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
150 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
151
152 fprintf(stderr, "%10ld %s\n",
153 ru.ru_maxrss, "maximum resident set size");
154 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0,
155 "average shared memory size");
156 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0,
157 "average unshared data size");
158 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0,
159 "average unshared stack size");
160 fprintf(stderr, "%10ld %s\n",
161 ru.ru_minflt, "minor page faults");
162 fprintf(stderr, "%10ld %s\n",
163 ru.ru_majflt, "major page faults");
164 fprintf(stderr, "%10ld %s\n",
165 ru.ru_nswap, "swaps");
166 fprintf(stderr, "%10ld %s\n",
167 ru.ru_inblock, "block input operations");
168 fprintf(stderr, "%10ld %s\n",
169 ru.ru_oublock, "block output operations");
170 fprintf(stderr, "%10ld %s\n",
171 ru.ru_msgsnd, "messages sent");
172 fprintf(stderr, "%10ld %s\n",
173 ru.ru_msgrcv, "messages received");
174 fprintf(stderr, "%10ld %s\n",
175 ru.ru_nsignals, "signals received");
176 fprintf(stderr, "%10ld %s\n",
177 ru.ru_nvcsw, "voluntary context switches");
178 fprintf(stderr, "%10ld %s\n",
179 ru.ru_nivcsw, "involuntary context switches");
180 }
181
182 if (exitonsig) {
183 if (signal(exitonsig, SIG_DFL) == SIG_ERR)
184 perror("signal");
185 else
186 kill(getpid(), exitonsig);
187 }
188 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
189 }
190
191 __dead void
usage(void)192 usage(void)
193 {
194 extern char *__progname;
195
196 (void)fprintf(stderr, "usage: %s [-lp] command\n", __progname);
197 exit(1);
198 }
199