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/resource.h> 50 #include <sys/signal.h> 51 #include <sys/sysctl.h> 52 #include <sys/time.h> 53 #include <sys/wait.h> 54 55 #include <err.h> 56 #include <errno.h> 57 #include <locale.h> 58 #include <signal.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 static int getstathz(void); 65 static void humantime(FILE *, long, long); 66 static void showtime(FILE *, struct timeval *, struct timeval *, 67 struct rusage *); 68 static void siginfo(int); 69 static void usage(void); 70 71 static char decimal_point; 72 static struct timeval before; 73 static int hflag, pflag; 74 75 int 76 main(int argc, char **argv) 77 { 78 int aflag, ch, lflag, status; 79 int exitonsig; 80 pid_t pid; 81 struct rlimit rl; 82 struct rusage ru; 83 struct timeval after; 84 char *ofn = NULL; 85 FILE *out = stderr; 86 87 (void) setlocale(LC_NUMERIC, ""); 88 decimal_point = localeconv()->decimal_point[0]; 89 90 aflag = hflag = lflag = pflag = 0; 91 while ((ch = getopt(argc, argv, "ahlo:p")) != -1) 92 switch((char)ch) { 93 case 'a': 94 aflag = 1; 95 break; 96 case 'h': 97 hflag = 1; 98 break; 99 case 'l': 100 lflag = 1; 101 break; 102 case 'o': 103 ofn = optarg; 104 break; 105 case 'p': 106 pflag = 1; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 113 if (!(argc -= optind)) 114 exit(0); 115 argv += optind; 116 117 if (ofn) { 118 if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL) 119 err(1, "%s", ofn); 120 setvbuf(out, (char *)NULL, _IONBF, (size_t)0); 121 } 122 123 gettimeofday(&before, (struct timezone *)NULL); 124 switch(pid = fork()) { 125 case -1: /* error */ 126 err(1, "time"); 127 /* NOTREACHED */ 128 case 0: /* child */ 129 if (ofn) 130 fclose(out); 131 execvp(*argv, argv); 132 err(errno == ENOENT ? 127 : 126, "%s", *argv); 133 /* NOTREACHED */ 134 } 135 /* parent */ 136 (void)signal(SIGINT, SIG_IGN); 137 (void)signal(SIGQUIT, SIG_IGN); 138 (void)signal(SIGINFO, siginfo); 139 while (wait4(pid, &status, 0, &ru) != pid); 140 gettimeofday(&after, (struct timezone *)NULL); 141 if ( ! WIFEXITED(status)) 142 warnx("command terminated abnormally"); 143 exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0; 144 showtime(out, &before, &after, &ru); 145 if (lflag) { 146 int hz = getstathz(); 147 u_long ticks; 148 149 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 150 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 151 152 /* 153 * If our round-off on the tick calculation still puts us at 0, 154 * then always assume at least one tick. 155 */ 156 if (ticks == 0) 157 ticks = 1; 158 159 fprintf(out, "%10ld %s\n", 160 ru.ru_maxrss, "maximum resident set size"); 161 fprintf(out, "%10ld %s\n", 162 ru.ru_ixrss / ticks, "average shared memory size"); 163 fprintf(out, "%10ld %s\n", 164 ru.ru_idrss / ticks, "average unshared data size"); 165 fprintf(out, "%10ld %s\n", 166 ru.ru_isrss / ticks, "average unshared stack size"); 167 fprintf(out, "%10ld %s\n", 168 ru.ru_minflt, "page reclaims"); 169 fprintf(out, "%10ld %s\n", 170 ru.ru_majflt, "page faults"); 171 fprintf(out, "%10ld %s\n", 172 ru.ru_nswap, "swaps"); 173 fprintf(out, "%10ld %s\n", 174 ru.ru_inblock, "block input operations"); 175 fprintf(out, "%10ld %s\n", 176 ru.ru_oublock, "block output operations"); 177 fprintf(out, "%10ld %s\n", 178 ru.ru_msgsnd, "messages sent"); 179 fprintf(out, "%10ld %s\n", 180 ru.ru_msgrcv, "messages received"); 181 fprintf(out, "%10ld %s\n", 182 ru.ru_nsignals, "signals received"); 183 fprintf(out, "%10ld %s\n", 184 ru.ru_nvcsw, "voluntary context switches"); 185 fprintf(out, "%10ld %s\n", 186 ru.ru_nivcsw, "involuntary context switches"); 187 } 188 /* 189 * If the child has exited on a signal, exit on the same 190 * signal, too, in order to reproduce the child's exit status. 191 * However, avoid actually dumping core from the current process. 192 */ 193 if (exitonsig) { 194 if (signal(exitonsig, SIG_DFL) == SIG_ERR) 195 warn("signal"); 196 else { 197 rl.rlim_max = rl.rlim_cur = 0; 198 if (setrlimit(RLIMIT_CORE, &rl) == -1) 199 warn("setrlimit"); 200 kill(getpid(), exitonsig); 201 } 202 } 203 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 204 } 205 206 static void 207 usage(void) 208 { 209 fprintf(stderr, 210 "usage: time [-al] [-h | -p] [-o file] utility [argument ...]\n"); 211 exit(1); 212 } 213 214 /* 215 * Return the frequency of the kernel's statistics clock. 216 */ 217 static int 218 getstathz(void) 219 { 220 int mib[2]; 221 size_t size; 222 struct clockinfo clockrate; 223 224 mib[0] = CTL_KERN; 225 mib[1] = KERN_CLOCKRATE; 226 size = sizeof clockrate; 227 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 228 err(1, "sysctl kern.clockrate"); 229 return clockrate.stathz; 230 } 231 232 static void 233 humantime(FILE *out, long sec, long usec) 234 { 235 long days, hrs, mins; 236 237 days = sec / (60L * 60 * 24); 238 sec %= (60L * 60 * 24); 239 hrs = sec / (60L * 60); 240 sec %= (60L * 60); 241 mins = sec / 60; 242 sec %= 60; 243 244 fprintf(out, "\t"); 245 if (days) 246 fprintf(out, "%ldd", days); 247 if (hrs) 248 fprintf(out, "%ldh", hrs); 249 if (mins) 250 fprintf(out, "%ldm", mins); 251 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec); 252 } 253 254 static void 255 showtime(FILE *out, struct timeval *before, struct timeval *after, 256 struct rusage *ru) 257 { 258 259 after->tv_sec -= before->tv_sec; 260 after->tv_usec -= before->tv_usec; 261 if (after->tv_usec < 0) 262 after->tv_sec--, after->tv_usec += 1000000; 263 264 if (pflag) { 265 /* POSIX wants output that must look like 266 "real %f\nuser %f\nsys %f\n" and requires 267 at least two digits after the radix. */ 268 fprintf(out, "real %ld%c%02ld\n", 269 after->tv_sec, decimal_point, 270 after->tv_usec/10000); 271 fprintf(out, "user %ld%c%02ld\n", 272 ru->ru_utime.tv_sec, decimal_point, 273 ru->ru_utime.tv_usec/10000); 274 fprintf(out, "sys %ld%c%02ld\n", 275 ru->ru_stime.tv_sec, decimal_point, 276 ru->ru_stime.tv_usec/10000); 277 } else if (hflag) { 278 humantime(out, after->tv_sec, after->tv_usec/10000); 279 fprintf(out, " real\t"); 280 humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000); 281 fprintf(out, " user\t"); 282 humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000); 283 fprintf(out, " sys\n"); 284 } else { 285 fprintf(out, "%9ld%c%02ld real ", 286 after->tv_sec, decimal_point, 287 after->tv_usec/10000); 288 fprintf(out, "%9ld%c%02ld user ", 289 ru->ru_utime.tv_sec, decimal_point, 290 ru->ru_utime.tv_usec/10000); 291 fprintf(out, "%9ld%c%02ld sys\n", 292 ru->ru_stime.tv_sec, decimal_point, 293 ru->ru_stime.tv_usec/10000); 294 } 295 } 296 297 static void 298 siginfo(int sig __unused) 299 { 300 struct timeval after; 301 struct rusage ru; 302 303 gettimeofday(&after, (struct timezone *)NULL); 304 getrusage(RUSAGE_CHILDREN, &ru); 305 showtime(stdout, &before, &after, &ru); 306 } 307