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