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 "$Id: time.c,v 1.10 1998/07/28 10:08:16 des Exp $"; 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 <string.h> 59 #include <unistd.h> 60 #include <signal.h> 61 62 static int getstathz __P((void)); 63 static void usage __P((void)); 64 65 int 66 main(argc, argv) 67 int argc; 68 char **argv; 69 { 70 extern char *optarg; 71 extern int optind; 72 73 register int pid; 74 int aflag, ch, lflag, status; 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 aflag = lflag = 0; 82 while ((ch = getopt(argc, argv, "alo:")) != -1) 83 switch((char)ch) { 84 case 'a': 85 aflag = 1; 86 break; 87 case 'l': 88 lflag = 1; 89 break; 90 case 'o': 91 ofn = optarg; 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 = vfork()) { 110 case -1: /* error */ 111 err(1, "time"); 112 /* NOTREACHED */ 113 case 0: /* child */ 114 execvp(*argv, argv); 115 warn("%s", *argv); 116 _exit(1); 117 /* NOTREACHED */ 118 } 119 /* parent */ 120 (void)signal(SIGINT, SIG_IGN); 121 (void)signal(SIGQUIT, SIG_IGN); 122 while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */ 123 gettimeofday(&after, (struct timezone *)NULL); 124 if ( ! WIFEXITED(status)) 125 warnx("command terminated abnormally"); 126 if (WIFSIGNALED(status)) 127 exitonsig = WTERMSIG(status); 128 after.tv_sec -= before.tv_sec; 129 after.tv_usec -= before.tv_usec; 130 if (after.tv_usec < 0) 131 after.tv_sec--, after.tv_usec += 1000000; 132 fprintf(out, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000); 133 fprintf(out, "%9ld.%02ld user ", 134 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 135 fprintf(out, "%9ld.%02ld sys\n", 136 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 137 if (lflag) { 138 int hz = getstathz(); 139 u_long ticks; 140 141 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 142 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 143 144 /* 145 * If our round-off on the tick calculation still puts us at 0, 146 * then always assume at least one tick. 147 */ 148 if (ticks == 0) 149 ticks = 1; 150 151 fprintf(out, "%10ld %s\n", 152 ru.ru_maxrss, "maximum resident set size"); 153 fprintf(out, "%10ld %s\n", 154 ru.ru_ixrss / ticks, "average shared memory size"); 155 fprintf(out, "%10ld %s\n", 156 ru.ru_idrss / ticks, "average unshared data size"); 157 fprintf(out, "%10ld %s\n", 158 ru.ru_isrss / ticks, "average unshared stack size"); 159 fprintf(out, "%10ld %s\n", 160 ru.ru_minflt, "page reclaims"); 161 fprintf(out, "%10ld %s\n", 162 ru.ru_majflt, "page faults"); 163 fprintf(out, "%10ld %s\n", 164 ru.ru_nswap, "swaps"); 165 fprintf(out, "%10ld %s\n", 166 ru.ru_inblock, "block input operations"); 167 fprintf(out, "%10ld %s\n", 168 ru.ru_oublock, "block output operations"); 169 fprintf(out, "%10ld %s\n", 170 ru.ru_msgsnd, "messages sent"); 171 fprintf(out, "%10ld %s\n", 172 ru.ru_msgrcv, "messages received"); 173 fprintf(out, "%10ld %s\n", 174 ru.ru_nsignals, "signals received"); 175 fprintf(out, "%10ld %s\n", 176 ru.ru_nvcsw, "voluntary context switches"); 177 fprintf(out, "%10ld %s\n", 178 ru.ru_nivcsw, "involuntary context switches"); 179 } 180 if (exitonsig) { 181 if (signal(exitonsig, SIG_DFL) < 0) 182 perror("signal"); 183 else 184 kill(getpid(), exitonsig); 185 } 186 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 187 } 188 189 static void 190 usage() 191 { 192 fprintf(stderr, "usage: time [-al] [-o file] command\n"); 193 exit(1); 194 } 195 196 /* 197 * Return the frequency of the kernel's statistics clock. 198 */ 199 static int 200 getstathz() 201 { 202 struct clockinfo clockrate; 203 int mib[2]; 204 size_t size; 205 206 mib[0] = CTL_KERN; 207 mib[1] = KERN_CLOCKRATE; 208 size = sizeof clockrate; 209 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 210 err(1, "sysctl kern.clockrate"); 211 return clockrate.stathz; 212 } 213