19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1987, 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 3580c486a4SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1987, 1988, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 4180c486a4SPhilippe Charnier #if 0 429b50d902SRodney W. Grimes static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93"; 4380c486a4SPhilippe Charnier #endif 4480c486a4SPhilippe Charnier static const char rcsid[] = 45c3aac50fSPeter Wemm "$FreeBSD$"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/types.h> 499b50d902SRodney W. Grimes #include <sys/time.h> 509b50d902SRodney W. Grimes #include <sys/resource.h> 519b50d902SRodney W. Grimes #include <sys/signal.h> 52f0850246SJohn Polstra #include <sys/sysctl.h> 53bc2c47dfSJordan K. Hubbard #include <stdlib.h> 54bc2c47dfSJordan K. Hubbard #include <sys/wait.h> 55f0850246SJohn Polstra 56f0850246SJohn Polstra #include <err.h> 579b50d902SRodney W. Grimes #include <stdio.h> 58844338c2SOllivier Robert #include <errno.h> 59e1fc0c16SAndrey A. Chernov #include <locale.h> 6003a22248SPoul-Henning Kamp #include <string.h> 61fabfd133SDag-Erling Smørgrav #include <unistd.h> 629a4902a9SMartin Cracauer #include <signal.h> 639b50d902SRodney W. Grimes 643f330d7dSWarner Losh static int getstathz(void); 653f330d7dSWarner Losh static void humantime(FILE *, long, long); 663f330d7dSWarner Losh static void usage(void); 67f0850246SJohn Polstra 68e1fc0c16SAndrey A. Chernov static char decimal_point; 69e1fc0c16SAndrey A. Chernov 7080c486a4SPhilippe Charnier int 719b50d902SRodney W. Grimes main(argc, argv) 729b50d902SRodney W. Grimes int argc; 739b50d902SRodney W. Grimes char **argv; 749b50d902SRodney W. Grimes { 759b50d902SRodney W. Grimes register int pid; 76ea257bd5SDavid E. O'Brien int aflag, ch, hflag, lflag, status, pflag; 779b50d902SRodney W. Grimes struct timeval before, after; 789b50d902SRodney W. Grimes struct rusage ru; 79fabfd133SDag-Erling Smørgrav FILE *out = stderr; 80fabfd133SDag-Erling Smørgrav char *ofn = NULL; 819a4902a9SMartin Cracauer int exitonsig = 0; /* Die with same signal as child */ 829b50d902SRodney W. Grimes 83e1fc0c16SAndrey A. Chernov (void) setlocale(LC_NUMERIC, ""); 84e1fc0c16SAndrey A. Chernov decimal_point = localeconv()->decimal_point[0]; 85e1fc0c16SAndrey A. Chernov 86ea257bd5SDavid E. O'Brien aflag = hflag = lflag = pflag = 0; 87ea257bd5SDavid E. O'Brien while ((ch = getopt(argc, argv, "ahlo:p")) != -1) 889b50d902SRodney W. Grimes switch((char)ch) { 8903a22248SPoul-Henning Kamp case 'a': 90fabfd133SDag-Erling Smørgrav aflag = 1; 9103a22248SPoul-Henning Kamp break; 92ea257bd5SDavid E. O'Brien case 'h': 93ea257bd5SDavid E. O'Brien hflag = 1; 94ea257bd5SDavid E. O'Brien break; 959b50d902SRodney W. Grimes case 'l': 969b50d902SRodney W. Grimes lflag = 1; 979b50d902SRodney W. Grimes break; 989b81ca85SDag-Erling Smørgrav case 'o': 999b81ca85SDag-Erling Smørgrav ofn = optarg; 1009b81ca85SDag-Erling Smørgrav break; 101844338c2SOllivier Robert case 'p': 102844338c2SOllivier Robert pflag = 1; 103844338c2SOllivier Robert break; 1049b50d902SRodney W. Grimes case '?': 1059b50d902SRodney W. Grimes default: 10680c486a4SPhilippe Charnier usage(); 1079b50d902SRodney W. Grimes } 1089b50d902SRodney W. Grimes 1099b50d902SRodney W. Grimes if (!(argc -= optind)) 1109b50d902SRodney W. Grimes exit(0); 1119b50d902SRodney W. Grimes argv += optind; 1129b50d902SRodney W. Grimes 113fabfd133SDag-Erling Smørgrav if (ofn) { 114fabfd133SDag-Erling Smørgrav if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL) 1159b81ca85SDag-Erling Smørgrav err(1, "%s", ofn); 1169b81ca85SDag-Erling Smørgrav setvbuf(out, (char *)NULL, _IONBF, (size_t)0); 117fabfd133SDag-Erling Smørgrav } 11803a22248SPoul-Henning Kamp 1199b50d902SRodney W. Grimes gettimeofday(&before, (struct timezone *)NULL); 1201fd98d7dSDag-Erling Smørgrav switch(pid = fork()) { 1219b50d902SRodney W. Grimes case -1: /* error */ 12280c486a4SPhilippe Charnier err(1, "time"); 1239b50d902SRodney W. Grimes /* NOTREACHED */ 1249b50d902SRodney W. Grimes case 0: /* child */ 125844338c2SOllivier Robert errno = 0; 1269b50d902SRodney W. Grimes execvp(*argv, argv); 12780c486a4SPhilippe Charnier warn("%s", *argv); 128844338c2SOllivier Robert if (errno == ENOENT) 129844338c2SOllivier Robert _exit(127); /* POSIX: utility could not be found */ 130844338c2SOllivier Robert else 131844338c2SOllivier Robert _exit(126); /* POSIX: utility could not be invoked */ 1329b50d902SRodney W. Grimes /* NOTREACHED */ 1339b50d902SRodney W. Grimes } 1349b50d902SRodney W. Grimes /* parent */ 1359b50d902SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1369b50d902SRodney W. Grimes (void)signal(SIGQUIT, SIG_IGN); 1379b50d902SRodney W. Grimes while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */ 1389b50d902SRodney W. Grimes gettimeofday(&after, (struct timezone *)NULL); 1399a4902a9SMartin Cracauer if ( ! WIFEXITED(status)) 14080c486a4SPhilippe Charnier warnx("command terminated abnormally"); 1419a4902a9SMartin Cracauer if (WIFSIGNALED(status)) 1429a4902a9SMartin Cracauer exitonsig = WTERMSIG(status); 1439b50d902SRodney W. Grimes after.tv_sec -= before.tv_sec; 1449b50d902SRodney W. Grimes after.tv_usec -= before.tv_usec; 1459b50d902SRodney W. Grimes if (after.tv_usec < 0) 1469b50d902SRodney W. Grimes after.tv_sec--, after.tv_usec += 1000000; 147844338c2SOllivier Robert if (pflag) { 148844338c2SOllivier Robert /* POSIX wants output that must look like 149844338c2SOllivier Robert "real %f\nuser %f\nsys %f\n" and requires 150844338c2SOllivier Robert at least two digits after the radix. */ 151e1fc0c16SAndrey A. Chernov fprintf(out, "real %ld%c%02ld\n", 152e1fc0c16SAndrey A. Chernov after.tv_sec, decimal_point, 153e1fc0c16SAndrey A. Chernov after.tv_usec/10000); 154e1fc0c16SAndrey A. Chernov fprintf(out, "user %ld%c%02ld\n", 155e1fc0c16SAndrey A. Chernov ru.ru_utime.tv_sec, decimal_point, 156e1fc0c16SAndrey A. Chernov ru.ru_utime.tv_usec/10000); 157e1fc0c16SAndrey A. Chernov fprintf(out, "sys %ld%c%02ld\n", 158e1fc0c16SAndrey A. Chernov ru.ru_stime.tv_sec, decimal_point, 159e1fc0c16SAndrey A. Chernov ru.ru_stime.tv_usec/10000); 160ea257bd5SDavid E. O'Brien } else if (hflag) { 161ea257bd5SDavid E. O'Brien humantime(out, after.tv_sec, after.tv_usec/10000); 162a07101abSDavid E. O'Brien fprintf(out, " real\t"); 163ea257bd5SDavid E. O'Brien humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 164a07101abSDavid E. O'Brien fprintf(out, " user\t"); 165ea257bd5SDavid E. O'Brien humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 166ea257bd5SDavid E. O'Brien fprintf(out, " sys\n"); 167844338c2SOllivier Robert } else { 168e1fc0c16SAndrey A. Chernov fprintf(out, "%9ld%c%02ld real ", 169e1fc0c16SAndrey A. Chernov after.tv_sec, decimal_point, 170e1fc0c16SAndrey A. Chernov after.tv_usec/10000); 171e1fc0c16SAndrey A. Chernov fprintf(out, "%9ld%c%02ld user ", 172e1fc0c16SAndrey A. Chernov ru.ru_utime.tv_sec, decimal_point, 173e1fc0c16SAndrey A. Chernov ru.ru_utime.tv_usec/10000); 174e1fc0c16SAndrey A. Chernov fprintf(out, "%9ld%c%02ld sys\n", 175e1fc0c16SAndrey A. Chernov ru.ru_stime.tv_sec, decimal_point, 176e1fc0c16SAndrey A. Chernov ru.ru_stime.tv_usec/10000); 177844338c2SOllivier Robert } 1789b50d902SRodney W. Grimes if (lflag) { 179f0850246SJohn Polstra int hz = getstathz(); 1809e5fd22bSPaul Traina u_long ticks; 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 1839b50d902SRodney W. Grimes hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 1849e5fd22bSPaul Traina 1859e5fd22bSPaul Traina /* 1869e5fd22bSPaul Traina * If our round-off on the tick calculation still puts us at 0, 1879e5fd22bSPaul Traina * then always assume at least one tick. 1889e5fd22bSPaul Traina */ 1899e5fd22bSPaul Traina if (ticks == 0) 1909e5fd22bSPaul Traina ticks = 1; 1919e5fd22bSPaul Traina 19203a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 1939b50d902SRodney W. Grimes ru.ru_maxrss, "maximum resident set size"); 19403a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 1959b50d902SRodney W. Grimes ru.ru_ixrss / ticks, "average shared memory size"); 19603a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 1979b50d902SRodney W. Grimes ru.ru_idrss / ticks, "average unshared data size"); 19803a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 1999b50d902SRodney W. Grimes ru.ru_isrss / ticks, "average unshared stack size"); 20003a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2019b50d902SRodney W. Grimes ru.ru_minflt, "page reclaims"); 20203a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2039b50d902SRodney W. Grimes ru.ru_majflt, "page faults"); 20403a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2059b50d902SRodney W. Grimes ru.ru_nswap, "swaps"); 20603a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2079b50d902SRodney W. Grimes ru.ru_inblock, "block input operations"); 20803a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2099b50d902SRodney W. Grimes ru.ru_oublock, "block output operations"); 21003a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2119b50d902SRodney W. Grimes ru.ru_msgsnd, "messages sent"); 21203a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2139b50d902SRodney W. Grimes ru.ru_msgrcv, "messages received"); 21403a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2159b50d902SRodney W. Grimes ru.ru_nsignals, "signals received"); 21603a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2179b50d902SRodney W. Grimes ru.ru_nvcsw, "voluntary context switches"); 21803a22248SPoul-Henning Kamp fprintf(out, "%10ld %s\n", 2199b50d902SRodney W. Grimes ru.ru_nivcsw, "involuntary context switches"); 2209b50d902SRodney W. Grimes } 2219a4902a9SMartin Cracauer if (exitonsig) { 2220050672aSDavid Malone if (signal(exitonsig, SIG_DFL) == SIG_ERR) 2239a4902a9SMartin Cracauer perror("signal"); 2249a4902a9SMartin Cracauer else 2259a4902a9SMartin Cracauer kill(getpid(), exitonsig); 2269a4902a9SMartin Cracauer } 227bc2c47dfSJordan K. Hubbard exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 2289b50d902SRodney W. Grimes } 229f0850246SJohn Polstra 23080c486a4SPhilippe Charnier static void 23180c486a4SPhilippe Charnier usage() 23280c486a4SPhilippe Charnier { 233ea257bd5SDavid E. O'Brien fprintf(stderr, "usage: time [-al] [-h|-p] [-o file] command\n"); 2349b81ca85SDag-Erling Smørgrav exit(1); 23580c486a4SPhilippe Charnier } 23680c486a4SPhilippe Charnier 237f0850246SJohn Polstra /* 238f0850246SJohn Polstra * Return the frequency of the kernel's statistics clock. 239f0850246SJohn Polstra */ 240f0850246SJohn Polstra static int 241f0850246SJohn Polstra getstathz() 242f0850246SJohn Polstra { 243f0850246SJohn Polstra struct clockinfo clockrate; 244f0850246SJohn Polstra int mib[2]; 245f0850246SJohn Polstra size_t size; 246f0850246SJohn Polstra 247f0850246SJohn Polstra mib[0] = CTL_KERN; 248f0850246SJohn Polstra mib[1] = KERN_CLOCKRATE; 249f0850246SJohn Polstra size = sizeof clockrate; 250f0850246SJohn Polstra if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 251f0850246SJohn Polstra err(1, "sysctl kern.clockrate"); 252f0850246SJohn Polstra return clockrate.stathz; 253f0850246SJohn Polstra } 254ea257bd5SDavid E. O'Brien 255ea257bd5SDavid E. O'Brien static void 256ea257bd5SDavid E. O'Brien humantime(out, sec, usec) 257ea257bd5SDavid E. O'Brien FILE *out; 258ea257bd5SDavid E. O'Brien long sec; 259ea257bd5SDavid E. O'Brien long usec; 260ea257bd5SDavid E. O'Brien { 261ea257bd5SDavid E. O'Brien long days, hrs, mins; 262ea257bd5SDavid E. O'Brien 263ea257bd5SDavid E. O'Brien days = sec / (60L * 60 * 24); 264ea257bd5SDavid E. O'Brien sec %= (60L * 60 * 24); 265ea257bd5SDavid E. O'Brien hrs = sec / (60L * 60); 266ea257bd5SDavid E. O'Brien sec %= (60L * 60); 267ea257bd5SDavid E. O'Brien mins = sec / 60; 268ea257bd5SDavid E. O'Brien sec %= 60; 269ea257bd5SDavid E. O'Brien 270ea257bd5SDavid E. O'Brien fprintf(out, "\t"); 271ea257bd5SDavid E. O'Brien if (days) 272ea257bd5SDavid E. O'Brien fprintf(out, "%ldd", days); 273ea257bd5SDavid E. O'Brien if (hrs) 274ea257bd5SDavid E. O'Brien fprintf(out, "%ldh", hrs); 275ea257bd5SDavid E. O'Brien if (mins) 276ea257bd5SDavid E. O'Brien fprintf(out, "%ldm", mins); 277e1fc0c16SAndrey A. Chernov fprintf(out, "%ld%c%02lds", sec, decimal_point, usec); 278ea257bd5SDavid E. O'Brien } 279