Home | History | Annotate | Line # | Download | only in time
time.c revision 1.22
      1  1.22  christos /*	$NetBSD: time.c,v 1.22 2011/11/09 19:10:10 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.22  christos __RCSID("$NetBSD: time.c,v 1.22 2011/11/09 19:10:10 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.22  christos static void	prts(const char *, const char *, const struct timespec *,
     62  1.22  christos     const char *);
     63  1.12  christos static void	prtv(const char *, const char *, const struct timeval *,
     64  1.12  christos     const char *);
     65   1.9     lukem 
     66   1.4       jtc int
     67  1.16  christos main(int argc, char ** volatile argv)
     68   1.1       cgd {
     69   1.9     lukem 	int pid;
     70   1.3       jtc 	int ch, status;
     71  1.16  christos 	int volatile portableflag;
     72  1.16  christos 	int volatile lflag;
     73  1.17      matt 	int volatile cshflag;
     74  1.11    kleink 	const char *decpt;
     75  1.11    kleink 	const struct lconv *lconv;
     76  1.22  christos 	struct timespec before, after;
     77   1.1       cgd 	struct rusage ru;
     78   1.1       cgd 
     79  1.11    kleink 	(void)setlocale(LC_ALL, "");
     80  1.11    kleink 
     81  1.17      matt 	cshflag = lflag = portableflag = 0;
     82  1.17      matt 	while ((ch = getopt(argc, argv, "clp")) != -1) {
     83  1.11    kleink 		switch (ch) {
     84  1.17      matt 		case 'c':
     85  1.17      matt 			cshflag = 1;
     86  1.18      matt 			portableflag = 0;
     87  1.18      matt 			lflag = 0;
     88  1.17      matt 			break;
     89   1.3       jtc 		case 'p':
     90   1.3       jtc 			portableflag = 1;
     91  1.18      matt 			cshflag = 0;
     92  1.18      matt 			lflag = 0;
     93   1.3       jtc 			break;
     94   1.1       cgd 		case 'l':
     95   1.1       cgd 			lflag = 1;
     96  1.18      matt 			portableflag = 0;
     97  1.18      matt 			cshflag = 0;
     98   1.1       cgd 			break;
     99   1.1       cgd 		case '?':
    100   1.1       cgd 		default:
    101  1.11    kleink 			usage();
    102   1.1       cgd 		}
    103  1.11    kleink 	}
    104  1.11    kleink 	argc -= optind;
    105  1.11    kleink 	argv += optind;
    106   1.1       cgd 
    107  1.11    kleink 	if (argc < 1)
    108  1.11    kleink 		usage();
    109   1.1       cgd 
    110  1.22  christos 	(void)clock_gettime(CLOCK_MONOTONIC, &before);
    111   1.1       cgd 	switch(pid = vfork()) {
    112   1.1       cgd 	case -1:			/* error */
    113  1.12  christos 		err(EXIT_FAILURE, "Vfork failed");
    114   1.1       cgd 		/* NOTREACHED */
    115   1.1       cgd 	case 0:				/* child */
    116  1.11    kleink 		/* LINTED will return only on failure */
    117   1.1       cgd 		execvp(*argv, argv);
    118  1.12  christos 		err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
    119   1.1       cgd 		/* NOTREACHED */
    120   1.1       cgd 	}
    121   1.3       jtc 
    122   1.1       cgd 	/* parent */
    123   1.1       cgd 	(void)signal(SIGINT, SIG_IGN);
    124   1.1       cgd 	(void)signal(SIGQUIT, SIG_IGN);
    125  1.12  christos 	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
    126  1.12  christos 		err(EXIT_FAILURE, "wait4 %d failed", pid);
    127  1.22  christos 	(void)clock_gettime(CLOCK_MONOTONIC, &after);
    128   1.3       jtc 	if (!WIFEXITED(status))
    129  1.12  christos 		warnx("Command terminated abnormally.");
    130  1.22  christos 	timespecsub(&after, &before, &after);
    131   1.3       jtc 
    132  1.11    kleink 	if ((lconv = localeconv()) == NULL ||
    133  1.11    kleink 	    (decpt = lconv->decimal_point) == NULL)
    134  1.11    kleink 		decpt = ".";
    135  1.11    kleink 
    136  1.17      matt 	if (cshflag) {
    137  1.17      matt 		static struct rusage null_ru;
    138  1.17      matt 		before.tv_sec = 0;
    139  1.22  christos 		before.tv_nsec = 0;
    140  1.17      matt 		prusage(stderr, &null_ru, &ru, &after, &before);
    141  1.17      matt 	} else if (portableflag) {
    142  1.22  christos 		prts("real ", decpt, &after, "\n");
    143  1.12  christos 		prtv("user ", decpt, &ru.ru_utime, "\n");
    144  1.12  christos 		prtv("sys  ", decpt, &ru.ru_stime, "\n");
    145   1.3       jtc 	} else {
    146  1.22  christos 		prts("", decpt, &after, " real ");
    147  1.12  christos 		prtv("", decpt, &ru.ru_utime, " user ");
    148  1.12  christos 		prtv("", decpt, &ru.ru_stime, " sys\n");
    149   1.3       jtc 	}
    150   1.3       jtc 
    151   1.1       cgd 	if (lflag) {
    152  1.11    kleink 		int hz = (int)sysconf(_SC_CLK_TCK);
    153  1.12  christos 		unsigned long long ticks;
    154  1.12  christos #define SCALE(x) (long)(ticks ? x / ticks : 0)
    155   1.1       cgd 
    156   1.1       cgd 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
    157  1.15    simonb 		    hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
    158  1.12  christos 		prl(ru.ru_maxrss, "maximum resident set size");
    159  1.12  christos 		prl(SCALE(ru.ru_ixrss), "average shared memory size");
    160  1.12  christos 		prl(SCALE(ru.ru_idrss), "average unshared data size");
    161  1.12  christos 		prl(SCALE(ru.ru_isrss), "average unshared stack size");
    162  1.12  christos 		prl(ru.ru_minflt, "page reclaims");
    163  1.12  christos 		prl(ru.ru_majflt, "page faults");
    164  1.12  christos 		prl(ru.ru_nswap, "swaps");
    165  1.12  christos 		prl(ru.ru_inblock, "block input operations");
    166  1.12  christos 		prl(ru.ru_oublock, "block output operations");
    167  1.12  christos 		prl(ru.ru_msgsnd, "messages sent");
    168  1.12  christos 		prl(ru.ru_msgrcv, "messages received");
    169  1.12  christos 		prl(ru.ru_nsignals, "signals received");
    170  1.12  christos 		prl(ru.ru_nvcsw, "voluntary context switches");
    171  1.12  christos 		prl(ru.ru_nivcsw, "involuntary context switches");
    172   1.1       cgd 	}
    173   1.3       jtc 
    174  1.12  christos 	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
    175  1.11    kleink }
    176  1.11    kleink 
    177  1.11    kleink static void
    178  1.20  christos usage(void)
    179  1.11    kleink {
    180  1.15    simonb 
    181  1.22  christos 	(void)fprintf(stderr, "Usage: %s [-clp] utility [argument ...]\n",
    182  1.12  christos 	    getprogname());
    183  1.12  christos 	exit(EXIT_FAILURE);
    184  1.12  christos }
    185  1.12  christos 
    186  1.12  christos static void
    187  1.12  christos prl(long val, const char *expn)
    188  1.12  christos {
    189  1.15    simonb 
    190  1.15    simonb 	(void)fprintf(stderr, "%10ld  %s\n", val, expn);
    191  1.12  christos }
    192  1.11    kleink 
    193  1.12  christos static void
    194  1.22  christos prts(const char *pre, const char *decpt, const struct timespec *ts,
    195  1.22  christos     const char *post)
    196  1.22  christos {
    197  1.22  christos 
    198  1.22  christos 	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)ts->tv_sec,
    199  1.22  christos 	    decpt, (long)ts->tv_nsec / 10000000, post);
    200  1.22  christos }
    201  1.22  christos 
    202  1.22  christos static void
    203  1.12  christos prtv(const char *pre, const char *decpt, const struct timeval *tv,
    204  1.12  christos     const char *post)
    205  1.12  christos {
    206  1.15    simonb 
    207  1.22  christos 	(void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)tv->tv_sec,
    208  1.22  christos 	    decpt, (long)tv->tv_usec / 10000, post);
    209   1.1       cgd }
    210