xref: /csrg-svn/usr.bin/time/time.c (revision 62311)
135476Sbostic /*
2*62311Sbostic  * Copyright (c) 1987, 1988, 1993
3*62311Sbostic  *	The Regents of the University of California.  All rights reserved.
435476Sbostic  *
542770Sbostic  * %sccs.include.redist.c%
635476Sbostic  */
735476Sbostic 
812900Ssam #ifndef lint
9*62311Sbostic static char copyright[] =
10*62311Sbostic "@(#) Copyright (c) 1987, 1988, 1993\n\
11*62311Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235476Sbostic #endif /* not lint */
131127Sbill 
1435476Sbostic #ifndef lint
15*62311Sbostic static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 06/06/93";
1635476Sbostic #endif /* not lint */
1735476Sbostic 
181127Sbill #include <sys/types.h>
1913594Swnj #include <sys/time.h>
2013594Swnj #include <sys/resource.h>
2135476Sbostic #include <sys/signal.h>
2235476Sbostic #include <stdio.h>
231127Sbill 
main(argc,argv)241127Sbill main(argc, argv)
259228Ssam 	int argc;
269228Ssam 	char **argv;
271127Sbill {
2835476Sbostic 	extern int optind;
2935476Sbostic 	register int pid;
3035476Sbostic 	int ch, status, lflag;
319228Ssam 	struct timeval before, after;
329228Ssam 	struct rusage ru;
331127Sbill 
3435476Sbostic 	lflag = 0;
3535476Sbostic 	while ((ch = getopt(argc, argv, "l")) != EOF)
3635476Sbostic 		switch((char)ch) {
3735476Sbostic 		case 'l':
3835476Sbostic 			lflag = 1;
3935476Sbostic 			break;
4035476Sbostic 		case '?':
4135476Sbostic 		default:
4235476Sbostic 			fprintf(stderr, "usage: time [-l] command.\n");
4335476Sbostic 			exit(1);
4435476Sbostic 		}
4535476Sbostic 
4635476Sbostic 	if (!(argc -= optind))
471127Sbill 		exit(0);
4835476Sbostic 	argv += optind;
4935476Sbostic 
5032629Sbostic 	gettimeofday(&before, (struct timezone *)NULL);
5135476Sbostic 	switch(pid = vfork()) {
5235476Sbostic 	case -1:			/* error */
539228Ssam 		perror("time");
541127Sbill 		exit(1);
5535476Sbostic 		/* NOTREACHED */
5635476Sbostic 	case 0:				/* child */
5735476Sbostic 		execvp(*argv, argv);
5835476Sbostic 		perror(*argv);
5935476Sbostic 		_exit(1);
6035476Sbostic 		/* NOTREACHED */
611127Sbill 	}
6235476Sbostic 	/* parent */
6332629Sbostic 	(void)signal(SIGINT, SIG_IGN);
6432629Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
6535476Sbostic 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
6632629Sbostic 	gettimeofday(&after, (struct timezone *)NULL);
6735476Sbostic 	if (status&0377)
689228Ssam 		fprintf(stderr, "Command terminated abnormally.\n");
699228Ssam 	after.tv_sec -= before.tv_sec;
709228Ssam 	after.tv_usec -= before.tv_usec;
719228Ssam 	if (after.tv_usec < 0)
729228Ssam 		after.tv_sec--, after.tv_usec += 1000000;
7335476Sbostic 	fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
7435476Sbostic 	fprintf(stderr, "%9ld.%02ld user ",
7535476Sbostic 	    ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
7635476Sbostic 	fprintf(stderr, "%9ld.%02ld sys\n",
7735476Sbostic 	    ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
7832571Skarels 	if (lflag) {
7932571Skarels 		int hz = 100;			/* XXX */
8032571Skarels 		long ticks;
8132571Skarels 
8232571Skarels 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
8332571Skarels 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
8432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8532571Skarels 			ru.ru_maxrss, "maximum resident set size");
8632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8732571Skarels 			ru.ru_ixrss / ticks, "average shared memory size");
8832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8932571Skarels 			ru.ru_idrss / ticks, "average unshared data size");
9032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9132571Skarels 			ru.ru_isrss / ticks, "average unshared stack size");
9232629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9332571Skarels 			ru.ru_minflt, "page reclaims");
9432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9532571Skarels 			ru.ru_majflt, "page faults");
9632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9732571Skarels 			ru.ru_nswap, "swaps");
9832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9932571Skarels 			ru.ru_inblock, "block input operations");
10032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10132571Skarels 			ru.ru_oublock, "block output operations");
10232629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10332571Skarels 			ru.ru_msgsnd, "messages sent");
10432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10532571Skarels 			ru.ru_msgrcv, "messages received");
10632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10732571Skarels 			ru.ru_nsignals, "signals received");
10832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10932571Skarels 			ru.ru_nvcsw, "voluntary context switches");
11032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11132571Skarels 			ru.ru_nivcsw, "involuntary context switches");
11232571Skarels 	}
1139228Ssam 	exit (status>>8);
1141127Sbill }
115