Home | History | Annotate | Line # | Download | only in time
time.c revision 1.21.2.1
      1 /*	$NetBSD: time.c,v 1.21.2.1 2011/11/10 14:31:54 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
     41 #endif
     42 __RCSID("$NetBSD: time.c,v 1.21.2.1 2011/11/10 14:31:54 yamt Exp $");
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/time.h>
     47 #include <sys/resource.h>
     48 #include <sys/wait.h>
     49 #include <errno.h>
     50 #include <err.h>
     51 #include <locale.h>
     52 #include <signal.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <unistd.h>
     56 
     57 #include "ext.h"
     58 
     59 __dead static void	usage(void);
     60 static void	prl(long, const char *);
     61 static void	prts(const char *, const char *, const struct timespec *,
     62     const char *);
     63 static void	prtv(const char *, const char *, const struct timeval *,
     64     const char *);
     65 
     66 int
     67 main(int argc, char ** volatile argv)
     68 {
     69 	int pid;
     70 	int ch, status;
     71 	int volatile portableflag;
     72 	int volatile lflag;
     73 	int volatile cshflag;
     74 	const char *decpt;
     75 	const struct lconv *lconv;
     76 	struct timespec before, after;
     77 	struct rusage ru;
     78 
     79 	(void)setlocale(LC_ALL, "");
     80 
     81 	cshflag = lflag = portableflag = 0;
     82 	while ((ch = getopt(argc, argv, "clp")) != -1) {
     83 		switch (ch) {
     84 		case 'c':
     85 			cshflag = 1;
     86 			portableflag = 0;
     87 			lflag = 0;
     88 			break;
     89 		case 'p':
     90 			portableflag = 1;
     91 			cshflag = 0;
     92 			lflag = 0;
     93 			break;
     94 		case 'l':
     95 			lflag = 1;
     96 			portableflag = 0;
     97 			cshflag = 0;
     98 			break;
     99 		case '?':
    100 		default:
    101 			usage();
    102 		}
    103 	}
    104 	argc -= optind;
    105 	argv += optind;
    106 
    107 	if (argc < 1)
    108 		usage();
    109 
    110 	(void)clock_gettime(CLOCK_MONOTONIC, &before);
    111 	switch(pid = vfork()) {
    112 	case -1:			/* error */
    113 		err(EXIT_FAILURE, "Vfork failed");
    114 		/* NOTREACHED */
    115 	case 0:				/* child */
    116 		/* LINTED will return only on failure */
    117 		execvp(*argv, argv);
    118 		err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
    119 		/* NOTREACHED */
    120 	}
    121 
    122 	/* parent */
    123 	(void)signal(SIGINT, SIG_IGN);
    124 	(void)signal(SIGQUIT, SIG_IGN);
    125 	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
    126 		err(EXIT_FAILURE, "wait4 %d failed", pid);
    127 	(void)clock_gettime(CLOCK_MONOTONIC, &after);
    128 	if (!WIFEXITED(status))
    129 		warnx("Command terminated abnormally.");
    130 	timespecsub(&after, &before, &after);
    131 
    132 	if ((lconv = localeconv()) == NULL ||
    133 	    (decpt = lconv->decimal_point) == NULL)
    134 		decpt = ".";
    135 
    136 	if (cshflag) {
    137 		static struct rusage null_ru;
    138 		before.tv_sec = 0;
    139 		before.tv_nsec = 0;
    140 		prusage(stderr, &null_ru, &ru, &after, &before);
    141 	} else if (portableflag) {
    142 		prts("real ", decpt, &after, "\n");
    143 		prtv("user ", decpt, &ru.ru_utime, "\n");
    144 		prtv("sys  ", decpt, &ru.ru_stime, "\n");
    145 	} else {
    146 		prts("", decpt, &after, " real ");
    147 		prtv("", decpt, &ru.ru_utime, " user ");
    148 		prtv("", decpt, &ru.ru_stime, " sys\n");
    149 	}
    150 
    151 	if (lflag) {
    152 		int hz = (int)sysconf(_SC_CLK_TCK);
    153 		unsigned long long ticks;
    154 #define SCALE(x) (long)(ticks ? x / ticks : 0)
    155 
    156 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
    157 		    hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
    158 		prl(ru.ru_maxrss, "maximum resident set size");
    159 		prl(SCALE(ru.ru_ixrss), "average shared memory size");
    160 		prl(SCALE(ru.ru_idrss), "average unshared data size");
    161 		prl(SCALE(ru.ru_isrss), "average unshared stack size");
    162 		prl(ru.ru_minflt, "page reclaims");
    163 		prl(ru.ru_majflt, "page faults");
    164 		prl(ru.ru_nswap, "swaps");
    165 		prl(ru.ru_inblock, "block input operations");
    166 		prl(ru.ru_oublock, "block output operations");
    167 		prl(ru.ru_msgsnd, "messages sent");
    168 		prl(ru.ru_msgrcv, "messages received");
    169 		prl(ru.ru_nsignals, "signals received");
    170 		prl(ru.ru_nvcsw, "voluntary context switches");
    171 		prl(ru.ru_nivcsw, "involuntary context switches");
    172 	}
    173 
    174 	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
    175 }
    176 
    177 static void
    178 usage(void)
    179 {
    180 
    181 	(void)fprintf(stderr, "Usage: %s [-clp] utility [argument ...]\n",
    182 	    getprogname());
    183 	exit(EXIT_FAILURE);
    184 }
    185 
    186 static void
    187 prl(long val, const char *expn)
    188 {
    189 
    190 	(void)fprintf(stderr, "%10ld  %s\n", val, expn);
    191 }
    192 
    193 static void
    194 prts(const char *pre, const char *decpt, const struct timespec *ts,
    195     const char *post)
    196 {
    197 
    198 	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)ts->tv_sec,
    199 	    decpt, (long)ts->tv_nsec / 10000000, post);
    200 }
    201 
    202 static void
    203 prtv(const char *pre, const char *decpt, const struct timeval *tv,
    204     const char *post)
    205 {
    206 
    207 	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)tv->tv_sec,
    208 	    decpt, (long)tv->tv_usec / 10000, post);
    209 }
    210