Commit | Line | Data |
---|---|---|
66f66453 WJ |
1 | /* |
2 | * Copyright (c) 1987, 1988 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. 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 | char copyright[] = | |
36 | "@(#) Copyright (c) 1987, 1988 The Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)time.c 4.9 (Berkeley) 6/1/90"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/types.h> | |
45 | #include <sys/time.h> | |
46 | #include <sys/resource.h> | |
47 | #include <sys/signal.h> | |
48 | #include <stdio.h> | |
49 | ||
50 | main(argc, argv) | |
51 | int argc; | |
52 | char **argv; | |
53 | { | |
54 | extern int optind; | |
55 | register int pid; | |
56 | int ch, status, lflag; | |
57 | struct timeval before, after; | |
58 | struct rusage ru; | |
59 | ||
60 | lflag = 0; | |
61 | while ((ch = getopt(argc, argv, "l")) != EOF) | |
62 | switch((char)ch) { | |
63 | case 'l': | |
64 | lflag = 1; | |
65 | break; | |
66 | case '?': | |
67 | default: | |
68 | fprintf(stderr, "usage: time [-l] command.\n"); | |
69 | exit(1); | |
70 | } | |
71 | ||
72 | if (!(argc -= optind)) | |
73 | exit(0); | |
74 | argv += optind; | |
75 | ||
76 | gettimeofday(&before, (struct timezone *)NULL); | |
77 | switch(pid = vfork()) { | |
78 | case -1: /* error */ | |
79 | perror("time"); | |
80 | exit(1); | |
81 | /* NOTREACHED */ | |
82 | case 0: /* child */ | |
83 | execvp(*argv, argv); | |
84 | perror(*argv); | |
85 | _exit(1); | |
86 | /* NOTREACHED */ | |
87 | } | |
88 | /* parent */ | |
89 | (void)signal(SIGINT, SIG_IGN); | |
90 | (void)signal(SIGQUIT, SIG_IGN); | |
91 | while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */ | |
92 | gettimeofday(&after, (struct timezone *)NULL); | |
93 | if (status&0377) | |
94 | fprintf(stderr, "Command terminated abnormally.\n"); | |
95 | after.tv_sec -= before.tv_sec; | |
96 | after.tv_usec -= before.tv_usec; | |
97 | if (after.tv_usec < 0) | |
98 | after.tv_sec--, after.tv_usec += 1000000; | |
99 | fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000); | |
100 | fprintf(stderr, "%9ld.%02ld user ", | |
101 | ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); | |
102 | fprintf(stderr, "%9ld.%02ld sys\n", | |
103 | ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); | |
104 | if (lflag) { | |
105 | int hz = 100; /* XXX */ | |
106 | long ticks; | |
107 | ||
108 | ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + | |
109 | hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; | |
110 | fprintf(stderr, "%10ld %s\n", | |
111 | ru.ru_maxrss, "maximum resident set size"); | |
112 | fprintf(stderr, "%10ld %s\n", | |
113 | ru.ru_ixrss / ticks, "average shared memory size"); | |
114 | fprintf(stderr, "%10ld %s\n", | |
115 | ru.ru_idrss / ticks, "average unshared data size"); | |
116 | fprintf(stderr, "%10ld %s\n", | |
117 | ru.ru_isrss / ticks, "average unshared stack size"); | |
118 | fprintf(stderr, "%10ld %s\n", | |
119 | ru.ru_minflt, "page reclaims"); | |
120 | fprintf(stderr, "%10ld %s\n", | |
121 | ru.ru_majflt, "page faults"); | |
122 | fprintf(stderr, "%10ld %s\n", | |
123 | ru.ru_nswap, "swaps"); | |
124 | fprintf(stderr, "%10ld %s\n", | |
125 | ru.ru_inblock, "block input operations"); | |
126 | fprintf(stderr, "%10ld %s\n", | |
127 | ru.ru_oublock, "block output operations"); | |
128 | fprintf(stderr, "%10ld %s\n", | |
129 | ru.ru_msgsnd, "messages sent"); | |
130 | fprintf(stderr, "%10ld %s\n", | |
131 | ru.ru_msgrcv, "messages received"); | |
132 | fprintf(stderr, "%10ld %s\n", | |
133 | ru.ru_nsignals, "signals received"); | |
134 | fprintf(stderr, "%10ld %s\n", | |
135 | ru.ru_nvcsw, "voluntary context switches"); | |
136 | fprintf(stderr, "%10ld %s\n", | |
137 | ru.ru_nivcsw, "involuntary context switches"); | |
138 | } | |
139 | exit (status>>8); | |
140 | } |