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