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 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 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93"; 42 #endif /* not lint */ 43 44 #include <sys/types.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/signal.h> 48 #include <sys/sysctl.h> 49 50 #include <err.h> 51 #include <stdio.h> 52 53 static int getstathz __P((void)); 54 55 main(argc, argv) 56 int argc; 57 char **argv; 58 { 59 extern int optind; 60 register int pid; 61 int ch, status, lflag; 62 struct timeval before, after; 63 struct rusage ru; 64 65 lflag = 0; 66 while ((ch = getopt(argc, argv, "l")) != EOF) 67 switch((char)ch) { 68 case 'l': 69 lflag = 1; 70 break; 71 case '?': 72 default: 73 fprintf(stderr, "usage: time [-l] command.\n"); 74 exit(1); 75 } 76 77 if (!(argc -= optind)) 78 exit(0); 79 argv += optind; 80 81 gettimeofday(&before, (struct timezone *)NULL); 82 switch(pid = vfork()) { 83 case -1: /* error */ 84 perror("time"); 85 exit(1); 86 /* NOTREACHED */ 87 case 0: /* child */ 88 execvp(*argv, argv); 89 perror(*argv); 90 _exit(1); 91 /* NOTREACHED */ 92 } 93 /* parent */ 94 (void)signal(SIGINT, SIG_IGN); 95 (void)signal(SIGQUIT, SIG_IGN); 96 while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */ 97 gettimeofday(&after, (struct timezone *)NULL); 98 if (status&0377) 99 fprintf(stderr, "Command terminated abnormally.\n"); 100 after.tv_sec -= before.tv_sec; 101 after.tv_usec -= before.tv_usec; 102 if (after.tv_usec < 0) 103 after.tv_sec--, after.tv_usec += 1000000; 104 fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000); 105 fprintf(stderr, "%9ld.%02ld user ", 106 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 107 fprintf(stderr, "%9ld.%02ld sys\n", 108 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 109 if (lflag) { 110 int hz = getstathz(); 111 u_long ticks; 112 113 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 114 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 115 116 /* 117 * If our round-off on the tick calculation still puts us at 0, 118 * then always assume at least one tick. 119 */ 120 if (ticks == 0) 121 ticks = 1; 122 123 fprintf(stderr, "%10ld %s\n", 124 ru.ru_maxrss, "maximum resident set size"); 125 fprintf(stderr, "%10ld %s\n", 126 ru.ru_ixrss / ticks, "average shared memory size"); 127 fprintf(stderr, "%10ld %s\n", 128 ru.ru_idrss / ticks, "average unshared data size"); 129 fprintf(stderr, "%10ld %s\n", 130 ru.ru_isrss / ticks, "average unshared stack size"); 131 fprintf(stderr, "%10ld %s\n", 132 ru.ru_minflt, "page reclaims"); 133 fprintf(stderr, "%10ld %s\n", 134 ru.ru_majflt, "page faults"); 135 fprintf(stderr, "%10ld %s\n", 136 ru.ru_nswap, "swaps"); 137 fprintf(stderr, "%10ld %s\n", 138 ru.ru_inblock, "block input operations"); 139 fprintf(stderr, "%10ld %s\n", 140 ru.ru_oublock, "block output operations"); 141 fprintf(stderr, "%10ld %s\n", 142 ru.ru_msgsnd, "messages sent"); 143 fprintf(stderr, "%10ld %s\n", 144 ru.ru_msgrcv, "messages received"); 145 fprintf(stderr, "%10ld %s\n", 146 ru.ru_nsignals, "signals received"); 147 fprintf(stderr, "%10ld %s\n", 148 ru.ru_nvcsw, "voluntary context switches"); 149 fprintf(stderr, "%10ld %s\n", 150 ru.ru_nivcsw, "involuntary context switches"); 151 } 152 exit (status>>8); 153 } 154 155 /* 156 * Return the frequency of the kernel's statistics clock. 157 */ 158 static int 159 getstathz() 160 { 161 struct clockinfo clockrate; 162 int mib[2]; 163 size_t size; 164 165 mib[0] = CTL_KERN; 166 mib[1] = KERN_CLOCKRATE; 167 size = sizeof clockrate; 168 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1) 169 err(1, "sysctl kern.clockrate"); 170 return clockrate.stathz; 171 } 172