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