time.c revision 1.16 1 /* $NetBSD: time.c,v 1.16 2006/12/18 15:17:38 christos 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.16 2006/12/18 15:17:38 christos 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 int main(int, char **);
58 static void usage(void);
59 static void prl(long, const char *);
60 static void prtv(const char *, const char *, const struct timeval *,
61 const char *);
62
63 int
64 main(int argc, char ** volatile argv)
65 {
66 int pid;
67 int ch, status;
68 int volatile portableflag;
69 int volatile lflag;
70 const char *decpt;
71 const struct lconv *lconv;
72 struct timeval before, after;
73 struct rusage ru;
74
75 (void)setlocale(LC_ALL, "");
76
77 lflag = portableflag = 0;
78 while ((ch = getopt(argc, argv, "lp")) != -1) {
79 switch (ch) {
80 case 'p':
81 portableflag = 1;
82 break;
83 case 'l':
84 lflag = 1;
85 break;
86 case '?':
87 default:
88 usage();
89 }
90 }
91 argc -= optind;
92 argv += optind;
93
94 if (argc < 1)
95 usage();
96
97 gettimeofday(&before, (struct timezone *)NULL);
98 switch(pid = vfork()) {
99 case -1: /* error */
100 err(EXIT_FAILURE, "Vfork failed");
101 /* NOTREACHED */
102 case 0: /* child */
103 /* LINTED will return only on failure */
104 execvp(*argv, argv);
105 err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
106 /* NOTREACHED */
107 }
108
109 /* parent */
110 (void)signal(SIGINT, SIG_IGN);
111 (void)signal(SIGQUIT, SIG_IGN);
112 if ((pid = wait4(pid, &status, 0, &ru)) == -1)
113 err(EXIT_FAILURE, "wait4 %d failed", pid);
114 (void)gettimeofday(&after, (struct timezone *)NULL);
115 if (!WIFEXITED(status))
116 warnx("Command terminated abnormally.");
117 timersub(&after, &before, &after);
118
119 if ((lconv = localeconv()) == NULL ||
120 (decpt = lconv->decimal_point) == NULL)
121 decpt = ".";
122
123 if (portableflag) {
124 prtv("real ", decpt, &after, "\n");
125 prtv("user ", decpt, &ru.ru_utime, "\n");
126 prtv("sys ", decpt, &ru.ru_stime, "\n");
127 } else {
128 prtv("", decpt, &after, " real ");
129 prtv("", decpt, &ru.ru_utime, " user ");
130 prtv("", decpt, &ru.ru_stime, " sys\n");
131 }
132
133 if (lflag) {
134 int hz = (int)sysconf(_SC_CLK_TCK);
135 unsigned long long ticks;
136 #define SCALE(x) (long)(ticks ? x / ticks : 0)
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 prl(ru.ru_maxrss, "maximum resident set size");
141 prl(SCALE(ru.ru_ixrss), "average shared memory size");
142 prl(SCALE(ru.ru_idrss), "average unshared data size");
143 prl(SCALE(ru.ru_isrss), "average unshared stack size");
144 prl(ru.ru_minflt, "page reclaims");
145 prl(ru.ru_majflt, "page faults");
146 prl(ru.ru_nswap, "swaps");
147 prl(ru.ru_inblock, "block input operations");
148 prl(ru.ru_oublock, "block output operations");
149 prl(ru.ru_msgsnd, "messages sent");
150 prl(ru.ru_msgrcv, "messages received");
151 prl(ru.ru_nsignals, "signals received");
152 prl(ru.ru_nvcsw, "voluntary context switches");
153 prl(ru.ru_nivcsw, "involuntary context switches");
154 }
155
156 return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
157 }
158
159 static void
160 usage()
161 {
162
163 (void)fprintf(stderr, "usage: %s [-lp] utility [argument ...]\n",
164 getprogname());
165 exit(EXIT_FAILURE);
166 }
167
168 static void
169 prl(long val, const char *expn)
170 {
171
172 (void)fprintf(stderr, "%10ld %s\n", val, expn);
173 }
174
175 static void
176 prtv(const char *pre, const char *decpt, const struct timeval *tv,
177 const char *post)
178 {
179
180 (void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt,
181 (long)tv->tv_usec / 10000, post);
182 }
183