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