1 /* $OpenBSD: time.c,v 1.18 2009/10/27 23:59:44 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 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/resource.h> 36 #include <sys/wait.h> 37 #include <sys/sysctl.h> 38 39 #include <err.h> 40 #include <errno.h> 41 #include <signal.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 int lflag; 47 int portableflag; 48 49 __dead void usage(void); 50 51 int 52 main(int argc, char *argv[]) 53 { 54 pid_t pid; 55 int ch, status; 56 struct timeval before, after; 57 struct rusage ru; 58 int exitonsig = 0; 59 60 61 while ((ch = getopt(argc, argv, "lp")) != -1) { 62 switch(ch) { 63 case 'l': 64 lflag = 1; 65 break; 66 case 'p': 67 portableflag = 1; 68 break; 69 default: 70 usage(); 71 /* NOTREACHED */ 72 } 73 } 74 75 argc -= optind; 76 argv += optind; 77 78 if (argc < 1) 79 usage(); 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((errno == ENOENT) ? 127 : 126); 91 /* NOTREACHED */ 92 } 93 94 /* parent */ 95 (void)signal(SIGINT, SIG_IGN); 96 (void)signal(SIGQUIT, SIG_IGN); 97 while (wait3(&status, 0, &ru) != pid) 98 ; 99 gettimeofday(&after, (struct timezone *)NULL); 100 if (WIFSIGNALED(status)) 101 exitonsig = WTERMSIG(status); 102 if (!WIFEXITED(status)) 103 fprintf(stderr, "Command terminated abnormally.\n"); 104 timersub(&after, &before, &after); 105 106 if (portableflag) { 107 fprintf(stderr, "real %9ld.%02ld\n", 108 after.tv_sec, after.tv_usec/10000); 109 fprintf(stderr, "user %9ld.%02ld\n", 110 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 111 fprintf(stderr, "sys %9ld.%02ld\n", 112 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 113 } else { 114 115 fprintf(stderr, "%9ld.%02ld real ", 116 after.tv_sec, after.tv_usec/10000); 117 fprintf(stderr, "%9ld.%02ld user ", 118 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); 119 fprintf(stderr, "%9ld.%02ld sys\n", 120 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); 121 } 122 123 if (lflag) { 124 int hz; 125 long ticks; 126 int mib[2]; 127 struct clockinfo clkinfo; 128 size_t size; 129 130 mib[0] = CTL_KERN; 131 mib[1] = KERN_CLOCKRATE; 132 size = sizeof(clkinfo); 133 if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0) 134 err(1, "sysctl"); 135 136 hz = clkinfo.hz; 137 138 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) + 139 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000; 140 141 fprintf(stderr, "%10ld %s\n", 142 ru.ru_maxrss, "maximum resident set size"); 143 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0, 144 "average shared memory size"); 145 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0, 146 "average unshared data size"); 147 fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0, 148 "average unshared stack size"); 149 fprintf(stderr, "%10ld %s\n", 150 ru.ru_minflt, "minor page faults"); 151 fprintf(stderr, "%10ld %s\n", 152 ru.ru_majflt, "major page faults"); 153 fprintf(stderr, "%10ld %s\n", 154 ru.ru_nswap, "swaps"); 155 fprintf(stderr, "%10ld %s\n", 156 ru.ru_inblock, "block input operations"); 157 fprintf(stderr, "%10ld %s\n", 158 ru.ru_oublock, "block output operations"); 159 fprintf(stderr, "%10ld %s\n", 160 ru.ru_msgsnd, "messages sent"); 161 fprintf(stderr, "%10ld %s\n", 162 ru.ru_msgrcv, "messages received"); 163 fprintf(stderr, "%10ld %s\n", 164 ru.ru_nsignals, "signals received"); 165 fprintf(stderr, "%10ld %s\n", 166 ru.ru_nvcsw, "voluntary context switches"); 167 fprintf(stderr, "%10ld %s\n", 168 ru.ru_nivcsw, "involuntary context switches"); 169 } 170 171 if (exitonsig) { 172 if (signal(exitonsig, SIG_DFL) == SIG_ERR) 173 perror("signal"); 174 else 175 kill(getpid(), exitonsig); 176 } 177 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE); 178 } 179 180 __dead void 181 usage(void) 182 { 183 extern char *__progname; 184 185 (void)fprintf(stderr, "usage: %s [-lp] utility [argument ...]\n", 186 __progname); 187 exit(1); 188 } 189