]>
git.saurik.com Git - apple/shell_cmds.git/blob - time/time.c
1 /* $NetBSD: time.c,v 1.9 1997/10/20 03:28:21 lukem Exp $ */
4 * Copyright (c) 1987, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
46 __RCSID("$NetBSD: time.c,v 1.9 1997/10/20 03:28:21 lukem Exp $");
49 #include <sys/types.h>
51 #include <sys/resource.h>
62 int main
__P((int, char **));
71 struct timeval before
, after
;
74 #ifdef __GNUC__ /* XXX: borken gcc */
78 while ((ch
= getopt(argc
, argv
, "lp")) != -1)
88 fprintf(stderr
, "usage: time [-lp] command.\n");
92 if (!(argc
-= optind
))
96 gettimeofday(&before
, (struct timezone
*)NULL
);
97 switch(pid
= vfork()) {
105 _exit((errno
== ENOENT
) ? 127 : 126);
110 (void)signal(SIGINT
, SIG_IGN
);
111 (void)signal(SIGQUIT
, SIG_IGN
);
112 while (wait3(&status
, 0, &ru
) != pid
);
113 gettimeofday(&after
, (struct timezone
*)NULL
);
114 if (!WIFEXITED(status
))
115 fprintf(stderr
, "Command terminated abnormally.\n");
116 timersub(&after
, &before
, &after
);
119 fprintf (stderr
, "real %9ld.%02ld\n",
120 (long)after
.tv_sec
, (long)after
.tv_usec
/10000);
121 fprintf (stderr
, "user %9ld.%02ld\n",
122 (long)ru
.ru_utime
.tv_sec
, (long)ru
.ru_utime
.tv_usec
/10000);
123 fprintf (stderr
, "sys %9ld.%02ld\n",
124 (long)ru
.ru_stime
.tv_sec
, (long)ru
.ru_stime
.tv_usec
/10000);
127 fprintf(stderr
, "%9ld.%02ld real ",
128 (long)after
.tv_sec
, (long)after
.tv_usec
/10000);
129 fprintf(stderr
, "%9ld.%02ld user ",
130 (long)ru
.ru_utime
.tv_sec
, (long)ru
.ru_utime
.tv_usec
/10000);
131 fprintf(stderr
, "%9ld.%02ld sys\n",
132 (long)ru
.ru_stime
.tv_sec
, (long)ru
.ru_stime
.tv_usec
/10000);
136 int hz
= 100; /* XXX */
139 ticks
= hz
* (ru
.ru_utime
.tv_sec
+ ru
.ru_stime
.tv_sec
) +
140 hz
* (ru
.ru_utime
.tv_usec
+ ru
.ru_stime
.tv_usec
) / 1000000;
142 fprintf(stderr
, "%10ld %s\n",
143 ru
.ru_maxrss
, "maximum resident set size");
144 fprintf(stderr
, "%10ld %s\n", ticks
? ru
.ru_ixrss
/ ticks
: 0,
145 "average shared memory size");
146 fprintf(stderr
, "%10ld %s\n", ticks
? ru
.ru_idrss
/ ticks
: 0,
147 "average unshared data size");
148 fprintf(stderr
, "%10ld %s\n", ticks
? ru
.ru_isrss
/ ticks
: 0,
149 "average unshared stack size");
150 fprintf(stderr
, "%10ld %s\n",
151 ru
.ru_minflt
, "page reclaims");
152 fprintf(stderr
, "%10ld %s\n",
153 ru
.ru_majflt
, "page faults");
154 fprintf(stderr
, "%10ld %s\n",
155 ru
.ru_nswap
, "swaps");
156 fprintf(stderr
, "%10ld %s\n",
157 ru
.ru_inblock
, "block input operations");
158 fprintf(stderr
, "%10ld %s\n",
159 ru
.ru_oublock
, "block output operations");
160 fprintf(stderr
, "%10ld %s\n",
161 ru
.ru_msgsnd
, "messages sent");
162 fprintf(stderr
, "%10ld %s\n",
163 ru
.ru_msgrcv
, "messages received");
164 fprintf(stderr
, "%10ld %s\n",
165 ru
.ru_nsignals
, "signals received");
166 fprintf(stderr
, "%10ld %s\n",
167 ru
.ru_nvcsw
, "voluntary context switches");
168 fprintf(stderr
, "%10ld %s\n",
169 ru
.ru_nivcsw
, "involuntary context switches");
172 exit (WIFEXITED(status
) ? WEXITSTATUS(status
) : EXIT_FAILURE
);