1 /* $OpenBSD: time.c,v 1.16 2006/05/17 13:31:22 grunk 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.16 2006/05/17 13:31:22 grunk 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 default: 83 usage(); 84 /* NOTREACHED */ 85 } 86 } 87 88 argc -= optind; 89 argv += optind; 90 91 if (argc < 1) 92 usage(); 93 94 gettimeofday(&before, (struct timezone *)NULL); 95 switch(pid = vfork()) { 96 case -1: /* error */ 97 perror("time"); 98 exit(1); 99 /* NOTREACHED */ 100 case 0: /* child */ 101 execvp(*argv, argv); 102 perror(*argv); 103 _exit((errno == ENOENT) ? 127 : 126); 104 /* NOTREACHED */ 105 } 106 107 /* parent */ 108 (void)signal(SIGINT, SIG_IGN); 109 (void)signal(SIGQUIT, SIG_IGN); 110 while (wait3(&status, 0, &ru) != pid) 111 ; 112 gettimeofday(&after, (struct timezone *)NULL); 113 if (WIFSIGNALED(status)) 114 exitonsig = WTERMSIG(status); 115 if (!WIFEXITED(status)) 116 fprintf(stderr, "Command terminated abnormally.\n"); 117 timersub(&after, &before, &after); 118 119 if (portableflag) { 120 fprintf(stderr, "real %9ld.%02ld\n", 121 after.tv_sec, after.tv_usec/10000); 122 fprintf(stderr, "user %9ld.%02ld\n", 123 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 124 fprintf(stderr, "sys %9ld.%02ld\n", 125 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 126 } else { 127 128 fprintf(stderr, "%9ld.%02ld real ", 129 after.tv_sec, after.tv_usec/10000); 130 fprintf(stderr, "%9ld.%02ld user ", 131 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 132 fprintf(stderr, "%9ld.%02ld sys\n", 133 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 134 } 135 136 if (lflag) { 137 int hz; 138 long ticks; 139 int mib[2]; 140 struct clockinfo clkinfo; 141 size_t size; 142 143 mib[0] = CTL_KERN; 144 mib[1] = KERN_CLOCKRATE; 145 size = sizeof(clkinfo); 146 if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) 147 err(1, "sysctl"); 148 149 hz = clkinfo.hz; 150 151 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 152 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 153 154 fprintf(stderr, "%10ld %s\n", 155 ru.ru_maxrss, "maximum resident set size"); 156 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0, 157 "average shared memory size"); 158 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0, 159 "average unshared data size"); 160 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0, 161 "average unshared stack size"); 162 fprintf(stderr, "%10ld %s\n", 163 ru.ru_minflt, "minor page faults"); 164 fprintf(stderr, "%10ld %s\n", 165 ru.ru_majflt, "major page faults"); 166 fprintf(stderr, "%10ld %s\n", 167 ru.ru_nswap, "swaps"); 168 fprintf(stderr, "%10ld %s\n", 169 ru.ru_inblock, "block input operations"); 170 fprintf(stderr, "%10ld %s\n", 171 ru.ru_oublock, "block output operations"); 172 fprintf(stderr, "%10ld %s\n", 173 ru.ru_msgsnd, "messages sent"); 174 fprintf(stderr, "%10ld %s\n", 175 ru.ru_msgrcv, "messages received"); 176 fprintf(stderr, "%10ld %s\n", 177 ru.ru_nsignals, "signals received"); 178 fprintf(stderr, "%10ld %s\n", 179 ru.ru_nvcsw, "voluntary context switches"); 180 fprintf(stderr, "%10ld %s\n", 181 ru.ru_nivcsw, "involuntary context switches"); 182 } 183 184 if (exitonsig) { 185 if (signal(exitonsig, SIG_DFL) == SIG_ERR) 186 perror("signal"); 187 else 188 kill(getpid(), exitonsig); 189 } 190 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 191 } 192 193 __dead void 194 usage(void) 195 { 196 extern char *__progname; 197 198 (void)fprintf(stderr, "usage: %s [-lp] command\n", __progname); 199 exit(1); 200 } 201