Home | History | Annotate | Line # | Download | only in time
time.c revision 1.16
      1  1.16  christos /*	$NetBSD: time.c,v 1.16 2006/12/18 15:17:38 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.9     lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\n\
     35   1.9     lukem 	The Regents of the University of California.  All rights reserved.\n");
     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.16  christos __RCSID("$NetBSD: time.c,v 1.16 2006/12/18 15:17:38 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.12  christos int		main(int, char **);
     58  1.12  christos static void	usage(void);
     59  1.12  christos static void	prl(long, const char *);
     60  1.12  christos static void	prtv(const char *, const char *, const struct timeval *,
     61  1.12  christos     const char *);
     62   1.9     lukem 
     63   1.4       jtc int
     64  1.16  christos main(int argc, char ** volatile argv)
     65   1.1       cgd {
     66   1.9     lukem 	int pid;
     67   1.3       jtc 	int ch, status;
     68  1.16  christos 	int volatile portableflag;
     69  1.16  christos 	int volatile lflag;
     70  1.11    kleink 	const char *decpt;
     71  1.11    kleink 	const struct lconv *lconv;
     72   1.1       cgd 	struct timeval before, after;
     73   1.1       cgd 	struct rusage ru;
     74   1.1       cgd 
     75  1.11    kleink 	(void)setlocale(LC_ALL, "");
     76  1.11    kleink 
     77  1.11    kleink 	lflag = portableflag = 0;
     78  1.11    kleink 	while ((ch = getopt(argc, argv, "lp")) != -1) {
     79  1.11    kleink 		switch (ch) {
     80   1.3       jtc 		case 'p':
     81   1.3       jtc 			portableflag = 1;
     82   1.3       jtc 			break;
     83   1.1       cgd 		case 'l':
     84   1.1       cgd 			lflag = 1;
     85   1.1       cgd 			break;
     86   1.1       cgd 		case '?':
     87   1.1       cgd 		default:
     88  1.11    kleink 			usage();
     89   1.1       cgd 		}
     90  1.11    kleink 	}
     91  1.11    kleink 	argc -= optind;
     92  1.11    kleink 	argv += optind;
     93   1.1       cgd 
     94  1.11    kleink 	if (argc < 1)
     95  1.11    kleink 		usage();
     96   1.1       cgd 
     97   1.1       cgd 	gettimeofday(&before, (struct timezone *)NULL);
     98   1.1       cgd 	switch(pid = vfork()) {
     99   1.1       cgd 	case -1:			/* error */
    100  1.12  christos 		err(EXIT_FAILURE, "Vfork failed");
    101   1.1       cgd 		/* NOTREACHED */
    102   1.1       cgd 	case 0:				/* child */
    103  1.11    kleink 		/* LINTED will return only on failure */
    104   1.1       cgd 		execvp(*argv, argv);
    105  1.12  christos 		err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
    106   1.1       cgd 		/* NOTREACHED */
    107   1.1       cgd 	}
    108   1.3       jtc 
    109   1.1       cgd 	/* parent */
    110   1.1       cgd 	(void)signal(SIGINT, SIG_IGN);
    111   1.1       cgd 	(void)signal(SIGQUIT, SIG_IGN);
    112  1.12  christos 	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
    113  1.12  christos 		err(EXIT_FAILURE, "wait4 %d failed", pid);
    114  1.12  christos 	(void)gettimeofday(&after, (struct timezone *)NULL);
    115   1.3       jtc 	if (!WIFEXITED(status))
    116  1.12  christos 		warnx("Command terminated abnormally.");
    117   1.6   mycroft 	timersub(&after, &before, &after);
    118   1.3       jtc 
    119  1.11    kleink 	if ((lconv = localeconv()) == NULL ||
    120  1.11    kleink 	    (decpt = lconv->decimal_point) == NULL)
    121  1.11    kleink 		decpt = ".";
    122  1.11    kleink 
    123   1.3       jtc 	if (portableflag) {
    124  1.12  christos 		prtv("real ", decpt, &after, "\n");
    125  1.12  christos 		prtv("user ", decpt, &ru.ru_utime, "\n");
    126  1.12  christos 		prtv("sys  ", decpt, &ru.ru_stime, "\n");
    127   1.3       jtc 	} else {
    128  1.12  christos 		prtv("", decpt, &after, " real ");
    129  1.12  christos 		prtv("", decpt, &ru.ru_utime, " user ");
    130  1.12  christos 		prtv("", decpt, &ru.ru_stime, " sys\n");
    131   1.3       jtc 	}
    132   1.3       jtc 
    133   1.1       cgd 	if (lflag) {
    134  1.11    kleink 		int hz = (int)sysconf(_SC_CLK_TCK);
    135  1.12  christos 		unsigned long long ticks;
    136  1.12  christos #define SCALE(x) (long)(ticks ? x / ticks : 0)
    137   1.1       cgd 
    138   1.1       cgd 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
    139  1.15    simonb 		    hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
    140  1.12  christos 		prl(ru.ru_maxrss, "maximum resident set size");
    141  1.12  christos 		prl(SCALE(ru.ru_ixrss), "average shared memory size");
    142  1.12  christos 		prl(SCALE(ru.ru_idrss), "average unshared data size");
    143  1.12  christos 		prl(SCALE(ru.ru_isrss), "average unshared stack size");
    144  1.12  christos 		prl(ru.ru_minflt, "page reclaims");
    145  1.12  christos 		prl(ru.ru_majflt, "page faults");
    146  1.12  christos 		prl(ru.ru_nswap, "swaps");
    147  1.12  christos 		prl(ru.ru_inblock, "block input operations");
    148  1.12  christos 		prl(ru.ru_oublock, "block output operations");
    149  1.12  christos 		prl(ru.ru_msgsnd, "messages sent");
    150  1.12  christos 		prl(ru.ru_msgrcv, "messages received");
    151  1.12  christos 		prl(ru.ru_nsignals, "signals received");
    152  1.12  christos 		prl(ru.ru_nvcsw, "voluntary context switches");
    153  1.12  christos 		prl(ru.ru_nivcsw, "involuntary context switches");
    154   1.1       cgd 	}
    155   1.3       jtc 
    156  1.12  christos 	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
    157  1.11    kleink }
    158  1.11    kleink 
    159  1.11    kleink static void
    160  1.11    kleink usage()
    161  1.11    kleink {
    162  1.15    simonb 
    163  1.14      jmmv 	(void)fprintf(stderr, "usage: %s [-lp] utility [argument ...]\n",
    164  1.12  christos 	    getprogname());
    165  1.12  christos 	exit(EXIT_FAILURE);
    166  1.12  christos }
    167  1.12  christos 
    168  1.12  christos static void
    169  1.12  christos prl(long val, const char *expn)
    170  1.12  christos {
    171  1.15    simonb 
    172  1.15    simonb 	(void)fprintf(stderr, "%10ld  %s\n", val, expn);
    173  1.12  christos }
    174  1.11    kleink 
    175  1.12  christos static void
    176  1.12  christos prtv(const char *pre, const char *decpt, const struct timeval *tv,
    177  1.12  christos     const char *post)
    178  1.12  christos {
    179  1.15    simonb 
    180  1.15    simonb 	(void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt,
    181  1.15    simonb 	    (long)tv->tv_usec / 10000, post);
    182   1.1       cgd }
    183