[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.25

1.25    ! schwarze    1: /*     $OpenBSD: time.c,v 1.24 2017/07/22 17:01:09 schwarze 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: #include <sys/time.h>
                     34: #include <sys/resource.h>
                     35: #include <sys/wait.h>
1.7       art        36: #include <sys/sysctl.h>
1.11      mpech      37:
                     38: #include <err.h>
                     39: #include <errno.h>
1.1       deraadt    40: #include <signal.h>
                     41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <unistd.h>
                     44:
1.12      millert    45: __dead void usage(void);
1.11      mpech      46:
1.1       deraadt    47: int
1.15      deraadt    48: main(int argc, char *argv[])
1.1       deraadt    49: {
1.24      schwarze   50:        struct rusage ru;
                     51:        struct timespec before, after, during;
                     52:        int ch, exitonsig, lflag, portableflag, status;
1.13      deraadt    53:        pid_t pid;
1.1       deraadt    54:
1.21      deraadt    55:        if (pledge("stdio proc exec", NULL) == -1)
                     56:                err(1, "pledge");
1.24      schwarze   57:
                     58:        exitonsig = lflag = portableflag = 0;
1.11      mpech      59:
                     60:        while ((ch = getopt(argc, argv, "lp")) != -1) {
                     61:                switch(ch) {
                     62:                case 'l':
                     63:                        lflag = 1;
                     64:                        break;
1.1       deraadt    65:                case 'p':
                     66:                        portableflag = 1;
                     67:                        break;
                     68:                default:
1.11      mpech      69:                        usage();
1.1       deraadt    70:                }
1.11      mpech      71:        }
1.1       deraadt    72:
1.11      mpech      73:        argc -= optind;
1.1       deraadt    74:        argv += optind;
1.16      grunk      75:
                     76:        if (argc < 1)
1.11      mpech      77:                usage();
1.1       deraadt    78:
1.22      tedu       79:        clock_gettime(CLOCK_MONOTONIC, &before);
1.1       deraadt    80:        switch(pid = vfork()) {
                     81:        case -1:                        /* error */
1.25    ! schwarze   82:                warn("fork");
        !            83:                return 1;
1.1       deraadt    84:        case 0:                         /* child */
                     85:                execvp(*argv, argv);
1.25    ! schwarze   86:                warn("%s", *argv);
1.1       deraadt    87:                _exit((errno == ENOENT) ? 127 : 126);
                     88:        }
                     89:
                     90:        /* parent */
1.23      schwarze   91:        signal(SIGINT, SIG_IGN);
                     92:        signal(SIGQUIT, SIG_IGN);
1.3       deraadt    93:        while (wait3(&status, 0, &ru) != pid)
                     94:                ;
1.22      tedu       95:        clock_gettime(CLOCK_MONOTONIC, &after);
1.5       deraadt    96:        if (WIFSIGNALED(status))
                     97:                exitonsig = WTERMSIG(status);
1.1       deraadt    98:        if (!WIFEXITED(status))
                     99:                fprintf(stderr, "Command terminated abnormally.\n");
1.22      tedu      100:        timespecsub(&after, &before, &during);
1.1       deraadt   101:
                    102:        if (portableflag) {
1.19      deraadt   103:                fprintf(stderr, "real %9lld.%02ld\n",
1.22      tedu      104:                        (long long)during.tv_sec, during.tv_nsec/10000000);
1.19      deraadt   105:                fprintf(stderr, "user %9lld.%02ld\n",
                    106:                        (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
                    107:                fprintf(stderr, "sys  %9lld.%02ld\n",
                    108:                        (long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
1.1       deraadt   109:        } else {
1.19      deraadt   110:                fprintf(stderr, "%9lld.%02ld real ",
1.22      tedu      111:                        (long long)during.tv_sec, during.tv_nsec/10000000);
1.19      deraadt   112:                fprintf(stderr, "%9lld.%02ld user ",
                    113:                        (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
                    114:                fprintf(stderr, "%9lld.%02ld sys\n",
                    115:                        (long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
1.1       deraadt   116:        }
                    117:
                    118:        if (lflag) {
1.7       art       119:                int hz;
1.1       deraadt   120:                long ticks;
1.7       art       121:                int mib[2];
                    122:                struct clockinfo clkinfo;
                    123:                size_t size;
                    124:
                    125:                mib[0] = CTL_KERN;
                    126:                mib[1] = KERN_CLOCKRATE;
                    127:                size = sizeof(clkinfo);
                    128:                if (sysctl(mib, 2, &clkinfo, &size, NULL, 0) < 0)
                    129:                        err(1, "sysctl");
                    130:
                    131:                hz = clkinfo.hz;
1.1       deraadt   132:
                    133:                ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
                    134:                     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
                    135:
                    136:                fprintf(stderr, "%10ld  %s\n",
                    137:                        ru.ru_maxrss, "maximum resident set size");
                    138:                fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_ixrss / ticks : 0,
                    139:                        "average shared memory size");
                    140:                fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_idrss / ticks : 0,
                    141:                        "average unshared data size");
                    142:                fprintf(stderr, "%10ld  %s\n", ticks ? ru.ru_isrss / ticks : 0,
                    143:                        "average unshared stack size");
                    144:                fprintf(stderr, "%10ld  %s\n",
1.8       art       145:                        ru.ru_minflt, "minor page faults");
1.1       deraadt   146:                fprintf(stderr, "%10ld  %s\n",
1.8       art       147:                        ru.ru_majflt, "major page faults");
1.1       deraadt   148:                fprintf(stderr, "%10ld  %s\n",
                    149:                        ru.ru_nswap, "swaps");
                    150:                fprintf(stderr, "%10ld  %s\n",
                    151:                        ru.ru_inblock, "block input operations");
                    152:                fprintf(stderr, "%10ld  %s\n",
                    153:                        ru.ru_oublock, "block output operations");
                    154:                fprintf(stderr, "%10ld  %s\n",
                    155:                        ru.ru_msgsnd, "messages sent");
                    156:                fprintf(stderr, "%10ld  %s\n",
                    157:                        ru.ru_msgrcv, "messages received");
                    158:                fprintf(stderr, "%10ld  %s\n",
                    159:                        ru.ru_nsignals, "signals received");
                    160:                fprintf(stderr, "%10ld  %s\n",
                    161:                        ru.ru_nvcsw, "voluntary context switches");
                    162:                fprintf(stderr, "%10ld  %s\n",
                    163:                        ru.ru_nivcsw, "involuntary context switches");
                    164:        }
                    165:
1.5       deraadt   166:        if (exitonsig) {
1.6       millert   167:                if (signal(exitonsig, SIG_DFL) == SIG_ERR)
1.25    ! schwarze  168:                        return 128 + exitonsig;
1.5       deraadt   169:                else
1.25    ! schwarze  170:                        raise(exitonsig);
1.5       deraadt   171:        }
1.25    ! schwarze  172:        return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
1.11      mpech     173: }
                    174:
1.16      grunk     175: __dead void
1.15      deraadt   176: usage(void)
1.11      mpech     177: {
1.23      schwarze  178:        fprintf(stderr, "usage: %s [-lp] utility [argument ...]\n",
                    179:            getprogname());
1.11      mpech     180:        exit(1);
1.1       deraadt   181: }