time.c revision 1.17 1 /* $NetBSD: time.c,v 1.17 2007/02/24 21:30:27 matt 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: time.c,v 1.17 2007/02/24 21:30:27 matt Exp $");
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sys/wait.h>
49 #include <errno.h>
50 #include <err.h>
51 #include <locale.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56
57 #include "ext.h"
58
59 int main(int, char **);
60 static void usage(void);
61 static void prl(long, const char *);
62 static void prtv(const char *, const char *, const struct timeval *,
63 const char *);
64
65 int
66 main(int argc, char ** volatile argv)
67 {
68 int pid;
69 int ch, status;
70 int volatile portableflag;
71 int volatile lflag;
72 int volatile cshflag;
73 const char *decpt;
74 const struct lconv *lconv;
75 struct timeval before, after;
76 struct rusage ru;
77
78 (void)setlocale(LC_ALL, "");
79
80 cshflag = lflag = portableflag = 0;
81 while ((ch = getopt(argc, argv, "clp")) != -1) {
82 switch (ch) {
83 case 'c':
84 cshflag = 1;
85 break;
86 case 'p':
87 portableflag = 1;
88 break;
89 case 'l':
90 lflag = 1;
91 break;
92 case '?':
93 default:
94 usage();
95 }
96 }
97 argc -= optind;
98 argv += optind;
99
100 if (argc < 1)
101 usage();
102
103 gettimeofday(&before, (struct timezone *)NULL);
104 switch(pid = vfork()) {
105 case -1: /* error */
106 err(EXIT_FAILURE, "Vfork failed");
107 /* NOTREACHED */
108 case 0: /* child */
109 /* LINTED will return only on failure */
110 execvp(*argv, argv);
111 err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
112 /* NOTREACHED */
113 }
114
115 /* parent */
116 (void)signal(SIGINT, SIG_IGN);
117 (void)signal(SIGQUIT, SIG_IGN);
118 if ((pid = wait4(pid, &status, 0, &ru)) == -1)
119 err(EXIT_FAILURE, "wait4 %d failed", pid);
120 (void)gettimeofday(&after, (struct timezone *)NULL);
121 if (!WIFEXITED(status))
122 warnx("Command terminated abnormally.");
123 timersub(&after, &before, &after);
124
125 if ((lconv = localeconv()) == NULL ||
126 (decpt = lconv->decimal_point) == NULL)
127 decpt = ".";
128
129 if (cshflag) {
130 static struct rusage null_ru;
131 before.tv_sec = 0;
132 before.tv_usec = 0;
133 prusage(stderr, &null_ru, &ru, &after, &before);
134 } else if (portableflag) {
135 prtv("real ", decpt, &after, "\n");
136 prtv("user ", decpt, &ru.ru_utime, "\n");
137 prtv("sys ", decpt, &ru.ru_stime, "\n");
138 } else {
139 prtv("", decpt, &after, " real ");
140 prtv("", decpt, &ru.ru_utime, " user ");
141 prtv("", decpt, &ru.ru_stime, " sys\n");
142 }
143
144 if (lflag) {
145 int hz = (int)sysconf(_SC_CLK_TCK);
146 unsigned long long ticks;
147 #define SCALE(x) (long)(ticks ? x / ticks : 0)
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 prl(ru.ru_maxrss, "maximum resident set size");
152 prl(SCALE(ru.ru_ixrss), "average shared memory size");
153 prl(SCALE(ru.ru_idrss), "average unshared data size");
154 prl(SCALE(ru.ru_isrss), "average unshared stack size");
155 prl(ru.ru_minflt, "page reclaims");
156 prl(ru.ru_majflt, "page faults");
157 prl(ru.ru_nswap, "swaps");
158 prl(ru.ru_inblock, "block input operations");
159 prl(ru.ru_oublock, "block output operations");
160 prl(ru.ru_msgsnd, "messages sent");
161 prl(ru.ru_msgrcv, "messages received");
162 prl(ru.ru_nsignals, "signals received");
163 prl(ru.ru_nvcsw, "voluntary context switches");
164 prl(ru.ru_nivcsw, "involuntary context switches");
165 }
166
167 return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
168 }
169
170 static void
171 usage()
172 {
173
174 (void)fprintf(stderr, "usage: %s [-clp] utility [argument ...]\n",
175 getprogname());
176 exit(EXIT_FAILURE);
177 }
178
179 static void
180 prl(long val, const char *expn)
181 {
182
183 (void)fprintf(stderr, "%10ld %s\n", val, expn);
184 }
185
186 static void
187 prtv(const char *pre, const char *decpt, const struct timeval *tv,
188 const char *post)
189 {
190
191 (void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt,
192 (long)tv->tv_usec / 10000, post);
193 }
194