[BACK]Return to time.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / time

Annotation of src/usr.bin/time/time.c, Revision 1.16

1.16    ! grunk       1: /*     $OpenBSD: time.c,v 1.15 2003/06/10 22:20:53 deraadt Exp $       */
1.1       deraadt     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.
1.14      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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
1.16    ! grunk      43: static char rcsid[] = "$OpenBSD: time.c,v 1.15 2003/06/10 22:20:53 deraadt Exp $";
1.1       deraadt    44: #endif /* not lint */
                     45:
1.7       art        46: #include <sys/param.h>
1.1       deraadt    47: #include <sys/time.h>
                     48: #include <sys/resource.h>
                     49: #include <sys/wait.h>
1.7       art        50: #include <sys/sysctl.h>
1.11      mpech      51:
                     52: #include <err.h>
                     53: #include <errno.h>
1.1       deraadt    54: #include <signal.h>
                     55: #include <stdio.h>
                     56: #include <stdlib.h>
                     57: #include <unistd.h>
                     58:
                     59: int lflag;
                     60: int portableflag;
                     61:
1.12      millert    62: __dead void usage(void);
1.11      mpech      63:
1.1       deraadt    64: int
1.15      deraadt    65: main(int argc, char *argv[])
1.1       deraadt    66: {
1.13      deraadt    67:        pid_t pid;
1.1       deraadt    68:        int ch, status;
                     69:        struct timeval before, after;
                     70:        struct rusage ru;
1.5       deraadt    71:        int exitonsig = 0;
1.1       deraadt    72:
1.11      mpech      73:
                     74:        while ((ch = getopt(argc, argv, "lp")) != -1) {
                     75:                switch(ch) {
                     76:                case 'l':
                     77:                        lflag = 1;
                     78:                        break;
1.1       deraadt    79:                case 'p':
                     80:                        portableflag = 1;
                     81:                        break;
                     82:                default:
1.11      mpech      83:                        usage();
                     84:                        /* NOTREACHED */
1.1       deraadt    85:                }
1.11      mpech      86:        }
1.1       deraadt    87:
1.11      mpech      88:        argc -= optind;
1.1       deraadt    89:        argv += optind;
1.16    ! grunk      90:
        !            91:        if (argc < 1)
1.11      mpech      92:                usage();
1.1       deraadt    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);
1.3       deraadt   110:        while (wait3(&status, 0, &ru) != pid)
                    111:                ;
1.1       deraadt   112:        gettimeofday(&after, (struct timezone *)NULL);
1.5       deraadt   113:        if (WIFSIGNALED(status))
                    114:                exitonsig = WTERMSIG(status);
1.1       deraadt   115:        if (!WIFEXITED(status))
                    116:                fprintf(stderr, "Command terminated abnormally.\n");
                    117:        timersub(&after, &before, &after);
                    118:
                    119:        if (portableflag) {
1.16    ! grunk     120:                fprintf(stderr, "real %9ld.%02ld\n",
1.1       deraadt   121:                        after.tv_sec, after.tv_usec/10000);
1.3       deraadt   122:                fprintf(stderr, "user %9ld.%02ld\n",
1.1       deraadt   123:                        ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
1.3       deraadt   124:                fprintf(stderr, "sys  %9ld.%02ld\n",
1.1       deraadt   125:                        ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
                    126:        } else {
                    127:
1.16    ! grunk     128:                fprintf(stderr, "%9ld.%02ld real ",
1.1       deraadt   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) {
1.7       art       137:                int hz;
1.1       deraadt   138:                long ticks;
1.7       art       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;
1.1       deraadt   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",
1.8       art       163:                        ru.ru_minflt, "minor page faults");
1.1       deraadt   164:                fprintf(stderr, "%10ld  %s\n",
1.8       art       165:                        ru.ru_majflt, "major page faults");
1.1       deraadt   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:
1.5       deraadt   184:        if (exitonsig) {
1.6       millert   185:                if (signal(exitonsig, SIG_DFL) == SIG_ERR)
1.5       deraadt   186:                        perror("signal");
                    187:                else
1.6       millert   188:                        kill(getpid(), exitonsig);
1.5       deraadt   189:        }
1.3       deraadt   190:        exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
1.11      mpech     191: }
                    192:
1.16    ! grunk     193: __dead void
1.15      deraadt   194: usage(void)
1.11      mpech     195: {
1.16    ! grunk     196:        extern char *__progname;
1.11      mpech     197:
                    198:        (void)fprintf(stderr, "usage: %s [-lp] command\n", __progname);
                    199:        exit(1);
1.1       deraadt   200: }