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