1 /* $OpenBSD: time.c,v 1.9 2001/07/12 05:17:23 deraadt Exp $ */ 2 /* $NetBSD: time.c,v 1.7 1995/06/27 00:34:00 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1987, 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1987, 1988, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 #if 0 45 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93"; 46 #endif 47 static char rcsid[] = "$OpenBSD: time.c,v 1.9 2001/07/12 05:17:23 deraadt Exp $"; 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 #include <sys/wait.h> 54 #include <sys/sysctl.h> 55 #include <signal.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <err.h> 59 #include <unistd.h> 60 #include <errno.h> 61 62 int lflag; 63 int portableflag; 64 65 int 66 main(argc, argv) 67 int argc; 68 char **argv; 69 { 70 extern int optind; 71 register int pid; 72 int ch, status; 73 struct timeval before, after; 74 struct rusage ru; 75 int exitonsig = 0; 76 77 lflag = 0; 78 while ((ch = getopt(argc, argv, "lp")) != -1) 79 switch((char)ch) { 80 case 'p': 81 portableflag = 1; 82 break; 83 case 'l': 84 lflag = 1; 85 break; 86 case '?': 87 default: 88 fprintf(stderr, "usage: time [-lp] command.\n"); 89 exit(1); 90 } 91 92 if (!(argc -= optind)) 93 exit(0); 94 argv += optind; 95 96 gettimeofday(&before, (struct timezone *)NULL); 97 switch(pid = vfork()) { 98 case -1: /* error */ 99 perror("time"); 100 exit(1); 101 /* NOTREACHED */ 102 case 0: /* child */ 103 execvp(*argv, argv); 104 perror(*argv); 105 _exit((errno == ENOENT) ? 127 : 126); 106 /* NOTREACHED */ 107 } 108 109 /* parent */ 110 (void)signal(SIGINT, SIG_IGN); 111 (void)signal(SIGQUIT, SIG_IGN); 112 while (wait3(&status, 0, &ru) != pid) 113 ; 114 gettimeofday(&after, (struct timezone *)NULL); 115 if (WIFSIGNALED(status)) 116 exitonsig = WTERMSIG(status); 117 if (!WIFEXITED(status)) 118 fprintf(stderr, "Command terminated abnormally.\n"); 119 timersub(&after, &before, &after); 120 121 if (portableflag) { 122 fprintf(stderr, "real %9ld.%02ld\n", 123 after.tv_sec, after.tv_usec/10000); 124 fprintf(stderr, "user %9ld.%02ld\n", 125 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 126 fprintf(stderr, "sys %9ld.%02ld\n", 127 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 128 } else { 129 130 fprintf(stderr, "%9ld.%02ld real ", 131 after.tv_sec, after.tv_usec/10000); 132 fprintf(stderr, "%9ld.%02ld user ", 133 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 134 fprintf(stderr, "%9ld.%02ld sys\n", 135 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 136 } 137 138 if (lflag) { 139 int hz; 140 long ticks; 141 int mib[2]; 142 struct clockinfo clkinfo; 143 size_t size; 144 145 mib[0] = CTL_KERN; 146 mib[1] = KERN_CLOCKRATE; 147 size = sizeof(clkinfo); 148 if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) 149 err(1, "sysctl"); 150 151 hz = clkinfo.hz; 152 153 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 154 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 155 156 fprintf(stderr, "%10ld %s\n", 157 ru.ru_maxrss, "maximum resident set size"); 158 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0, 159 "average shared memory size"); 160 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0, 161 "average unshared data size"); 162 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0, 163 "average unshared stack size"); 164 fprintf(stderr, "%10ld %s\n", 165 ru.ru_minflt, "minor page faults"); 166 fprintf(stderr, "%10ld %s\n", 167 ru.ru_majflt, "major page faults"); 168 fprintf(stderr, "%10ld %s\n", 169 ru.ru_nswap, "swaps"); 170 fprintf(stderr, "%10ld %s\n", 171 ru.ru_inblock, "block input operations"); 172 fprintf(stderr, "%10ld %s\n", 173 ru.ru_oublock, "block output operations"); 174 fprintf(stderr, "%10ld %s\n", 175 ru.ru_msgsnd, "messages sent"); 176 fprintf(stderr, "%10ld %s\n", 177 ru.ru_msgrcv, "messages received"); 178 fprintf(stderr, "%10ld %s\n", 179 ru.ru_nsignals, "signals received"); 180 fprintf(stderr, "%10ld %s\n", 181 ru.ru_nvcsw, "voluntary context switches"); 182 fprintf(stderr, "%10ld %s\n", 183 ru.ru_nivcsw, "involuntary context switches"); 184 } 185 186 if (exitonsig) { 187 if (signal(exitonsig, SIG_DFL) == SIG_ERR) 188 perror("signal"); 189 else 190 kill(getpid(), exitonsig); 191 } 192 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 193 } 194