1 /* 2 * Copyright (c) 1987, 1988, 1993 3 * The Regents of the University of California. 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 static const char copyright[] = 36 "@(#) Copyright (c) 1987, 1988, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/time.h> 50 #include <sys/resource.h> 51 #include <sys/signal.h> 52 #include <sys/sysctl.h> 53 #include <stdlib.h> 54 #include <sys/wait.h> 55 56 #include <err.h> 57 #include <stdio.h> 58 #include <errno.h> 59 #include <locale.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <signal.h> 63 64 static int getstathz(void); 65 static void humantime(FILE *, long, long); 66 static void usage(void); 67 68 static char decimal_point; 69 70 int 71 main(int argc, char **argv) 72 { 73 int pid; 74 int aflag, ch, hflag, lflag, status, pflag; 75 struct timeval before, after; 76 struct rusage ru; 77 FILE *out = stderr; 78 char *ofn = NULL; 79 int exitonsig = 0; /* Die with same signal as child */ 80 81 (void) setlocale(LC_NUMERIC, ""); 82 decimal_point = localeconv()->decimal_point[0]; 83 84 aflag = hflag = lflag = pflag = 0; 85 while ((ch = getopt(argc, argv, "ahlo:p")) != -1) 86 switch((char)ch) { 87 case 'a': 88 aflag = 1; 89 break; 90 case 'h': 91 hflag = 1; 92 break; 93 case 'l': 94 lflag = 1; 95 break; 96 case 'o': 97 ofn = optarg; 98 break; 99 case 'p': 100 pflag = 1; 101 break; 102 case '?': 103 default: 104 usage(); 105 } 106 107 if (!(argc -= optind)) 108 exit(0); 109 argv += optind; 110 111 if (ofn) { 112 if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL) 113 err(1, "%s", ofn); 114 setvbuf(out, (char *)NULL, _IONBF, (size_t)0); 115 } 116 117 gettimeofday(&before, (struct timezone *)NULL); 118 switch(pid = fork()) { 119 case -1: /* error */ 120 err(1, "time"); 121 /* NOTREACHED */ 122 case 0: /* child */ 123 execvp(*argv, argv); 124 err(errno == ENOENT ? 127 : 126, "%s", *argv); 125 /* NOTREACHED */ 126 } 127 /* parent */ 128 (void)signal(SIGINT, SIG_IGN); 129 (void)signal(SIGQUIT, SIG_IGN); 130 while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */ 131 gettimeofday(&after, (struct timezone *)NULL); 132 if ( ! WIFEXITED(status)) 133 warnx("command terminated abnormally"); 134 if (WIFSIGNALED(status)) 135 exitonsig = WTERMSIG(status); 136 after.tv_sec -= before.tv_sec; 137 after.tv_usec -= before.tv_usec; 138 if (after.tv_usec < 0) 139 after.tv_sec--, after.tv_usec += 1000000; 140 if (pflag) { 141 /* POSIX wants output that must look like 142 "real %f\nuser %f\nsys %f\n" and requires 143 at least two digits after the radix. */ 144 fprintf(out, "real %ld%c%02ld\n", 145 after.tv_sec, decimal_point, 146 after.tv_usec/10000); 147 fprintf(out, "user %ld%c%02ld\n", 148 ru.ru_utime.tv_sec, decimal_point, 149 ru.ru_utime.tv_usec/10000); 150 fprintf(out, "sys %ld%c%02ld\n", 151 ru.ru_stime.tv_sec, decimal_point, 152 ru.ru_stime.tv_usec/10000); 153 } else if (hflag) { 154 humantime(out, after.tv_sec, after.tv_usec/10000); 155 fprintf(out, " real\t"); 156 humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 157 fprintf(out, " user\t"); 158 humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 159 fprintf(out, " sys\n"); 160 } else { 161 fprintf(out, "%9ld%c%02ld real ", 162 after.tv_sec, decimal_point, 163 after.tv_usec/10000); 164 fprintf(out, "%9ld%c%02ld user ", 165 ru.ru_utime.tv_sec, decimal_point, 166 ru.ru_utime.tv_usec/10000); 167 fprintf(out, "%9ld%c%02ld sys\n", 168 ru.ru_stime.tv_sec, decimal_point, 169 ru.ru_stime.tv_usec/10000); 170 } 171 if (lflag) { 172 int hz = getstathz(); 173 u_long ticks; 174 175 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 176 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 177 178 /* 179 * If our round-off on the tick calculation still puts us at 0, 180 * then always assume at least one tick. 181 */ 182 if (ticks == 0) 183 ticks = 1; 184 185 fprintf(out, "%10ld %s\n", 186 ru.ru_maxrss, "maximum resident set size"); 187 fprintf(out, "%10ld %s\n", 188 ru.ru_ixrss / ticks, "average shared memory size"); 189 fprintf(out, "%10ld %s\n", 190 ru.ru_idrss / ticks, "average unshared data size"); 191 fprintf(out, "%10ld %s\n", 192 ru.ru_isrss / ticks, "average unshared stack size"); 193 fprintf(out, "%10ld %s\n", 194 ru.ru_minflt, "page reclaims"); 195 fprintf(out, "%10ld %s\n", 196 ru.ru_majflt, "page faults"); 197 fprintf(out, "%10ld %s\n", 198 ru.ru_nswap, "swaps"); 199 fprintf(out, "%10ld %s\n", 200 ru.ru_inblock, "block input operations"); 201 fprintf(out, "%10ld %s\n", 202 ru.ru_oublock, "block output operations"); 203 fprintf(out, "%10ld %s\n", 204 ru.ru_msgsnd, "messages sent"); 205 fprintf(out, "%10ld %s\n", 206 ru.ru_msgrcv, "messages received"); 207 fprintf(out, "%10ld %s\n", 208 ru.ru_nsignals, "signals received"); 209 fprintf(out, "%10ld %s\n", 210 ru.ru_nvcsw, "voluntary context switches"); 211 fprintf(out, "%10ld %s\n", 212 ru.ru_nivcsw, "involuntary context switches"); 213 } 214 if (exitonsig) { 215 if (signal(exitonsig, SIG_DFL) == SIG_ERR) 216 perror("signal"); 217 else 218 kill(getpid(), exitonsig); 219 } 220 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 221 } 222 223 static void 224 usage(void) 225 { 226 fprintf(stderr, 227 "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n"); 228 exit(1); 229 } 230 231 /* 232 * Return the frequency of the kernel's statistics clock. 233 */ 234 static int 235 getstathz(void) 236 { 237 struct clockinfo clockrate; 238 int mib[2]; 239 size_t size; 240 241 mib[0] = CTL_KERN; 242 mib[1] = KERN_CLOCKRATE; 243 size = sizeof clockrate; 244 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 245 err(1, "sysctl kern.clockrate"); 246 return clockrate.stathz; 247 } 248 249 static void 250 humantime(FILE *out, long sec, long usec) 251 { 252 long days, hrs, mins; 253 254 days = sec / (60L * 60 * 24); 255 sec %= (60L * 60 * 24); 256 hrs = sec / (60L * 60); 257 sec %= (60L * 60); 258 mins = sec / 60; 259 sec %= 60; 260 261 fprintf(out, "\t"); 262 if (days) 263 fprintf(out, "%ldd", days); 264 if (hrs) 265 fprintf(out, "%ldh", hrs); 266 if (mins) 267 fprintf(out, "%ldm", mins); 268 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec); 269 } 270