Home | History | Annotate | Line # | Download | only in time
time.c revision 1.11
      1 /*	$NetBSD: time.c,v 1.11 1999/06/05 19:19:19 kleink 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
     45 #endif
     46 __RCSID("$NetBSD: time.c,v 1.11 1999/06/05 19:19:19 kleink Exp $");
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 #include <sys/resource.h>
     52 #include <sys/wait.h>
     53 #include <errno.h>
     54 #include <locale.h>
     55 #include <signal.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <unistd.h>
     59 
     60 
     61 int		main __P((int, char **));
     62 static void	usage __P((void));
     63 
     64 int
     65 main(argc, argv)
     66 	int argc;
     67 	char **argv;
     68 {
     69 	int pid;
     70 	int ch, status;
     71 	int lflag, portableflag;
     72 	const char *decpt;
     73 	const struct lconv *lconv;
     74 	struct timeval before, after;
     75 	struct rusage ru;
     76 
     77 #ifdef __GNUC__		/* XXX: borken gcc */
     78 	(void)&argv;
     79 #endif
     80 
     81 	(void)setlocale(LC_ALL, "");
     82 
     83 	lflag = portableflag = 0;
     84 	while ((ch = getopt(argc, argv, "lp")) != -1) {
     85 		switch (ch) {
     86 		case 'p':
     87 			portableflag = 1;
     88 			break;
     89 		case 'l':
     90 			lflag = 1;
     91 			break;
     92 		case '?':
     93 		default:
     94 			usage();
     95 		}
     96 	}
     97 	argc -= optind;
     98 	argv += optind;
     99 
    100 	if (argc < 1)
    101 		usage();
    102 
    103 	gettimeofday(&before, (struct timezone *)NULL);
    104 	switch(pid = vfork()) {
    105 	case -1:			/* error */
    106 		perror("vfork");
    107 		exit(EXIT_FAILURE);
    108 		/* NOTREACHED */
    109 	case 0:				/* child */
    110 		/* LINTED will return only on failure */
    111 		execvp(*argv, argv);
    112 		perror(*argv);
    113 		_exit((errno == ENOENT) ? 127 : 126);
    114 		/* NOTREACHED */
    115 	}
    116 
    117 	/* parent */
    118 	(void)signal(SIGINT, SIG_IGN);
    119 	(void)signal(SIGQUIT, SIG_IGN);
    120 	while (wait3(&status, 0, &ru) != pid);
    121 	gettimeofday(&after, (struct timezone *)NULL);
    122 	if (!WIFEXITED(status))
    123 		fprintf(stderr, "Command terminated abnormally.\n");
    124 	timersub(&after, &before, &after);
    125 
    126 	if ((lconv = localeconv()) == NULL ||
    127 	    (decpt = lconv->decimal_point) == NULL)
    128 		decpt = ".";
    129 
    130 	if (portableflag) {
    131 		fprintf (stderr, "real %9ld%s%02ld\n",
    132 			(long)after.tv_sec, decpt, (long)after.tv_usec/10000);
    133 		fprintf (stderr, "user %9ld%s%02ld\n",
    134 			(long)ru.ru_utime.tv_sec, decpt, (long)ru.ru_utime.tv_usec/10000);
    135 		fprintf (stderr, "sys  %9ld%s%02ld\n",
    136 			(long)ru.ru_stime.tv_sec, decpt, (long)ru.ru_stime.tv_usec/10000);
    137 	} else {
    138 
    139 		fprintf(stderr, "%9ld%s%02ld real ",
    140 			(long)after.tv_sec, decpt, (long)after.tv_usec/10000);
    141 		fprintf(stderr, "%9ld%s%02ld user ",
    142 			(long)ru.ru_utime.tv_sec, decpt, (long)ru.ru_utime.tv_usec/10000);
    143 		fprintf(stderr, "%9ld%s%02ld sys\n",
    144 			(long)ru.ru_stime.tv_sec, decpt, (long)ru.ru_stime.tv_usec/10000);
    145 	}
    146 
    147 	if (lflag) {
    148 		int hz = (int)sysconf(_SC_CLK_TCK);
    149 		long ticks;
    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, "page reclaims");
    164 		fprintf(stderr, "%10ld  %s\n",
    165 			ru.ru_majflt, "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 	exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
    185 	/* NOTREACHED */
    186 }
    187 
    188 static void
    189 usage()
    190 {
    191 
    192 	fprintf(stderr, "usage: time [-lp] utility [argument ...]\n");
    193 	exit(EXIT_FAILURE);
    194 }
    195