xref: /csrg-svn/usr.bin/time/time.c (revision 35476)
1*35476Sbostic /*
2*35476Sbostic  * Copyright (c) 1987, 1988 The Regents of the University of California.
3*35476Sbostic  * All rights reserved.
4*35476Sbostic  *
5*35476Sbostic  * Redistribution and use in source and binary forms are permitted
6*35476Sbostic  * provided that the above copyright notice and this paragraph are
7*35476Sbostic  * duplicated in all such forms and that any documentation,
8*35476Sbostic  * advertising materials, and other materials related to such
9*35476Sbostic  * distribution and use acknowledge that the software was developed
10*35476Sbostic  * by the University of California, Berkeley.  The name of the
11*35476Sbostic  * University may not be used to endorse or promote products derived
12*35476Sbostic  * from this software without specific prior written permission.
13*35476Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35476Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35476Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*35476Sbostic  */
17*35476Sbostic 
1812900Ssam #ifndef lint
19*35476Sbostic char copyright[] =
20*35476Sbostic "@(#) Copyright (c) 1987, 1988 The Regents of the University of California.\n\
21*35476Sbostic  All rights reserved.\n";
22*35476Sbostic #endif /* not lint */
231127Sbill 
24*35476Sbostic #ifndef lint
25*35476Sbostic static char sccsid[] = "@(#)time.c	4.8 (Berkeley) 09/09/88";
26*35476Sbostic #endif /* not lint */
27*35476Sbostic 
281127Sbill #include <sys/types.h>
2913594Swnj #include <sys/time.h>
3013594Swnj #include <sys/resource.h>
31*35476Sbostic #include <sys/signal.h>
32*35476Sbostic #include <stdio.h>
331127Sbill 
341127Sbill main(argc, argv)
359228Ssam 	int argc;
369228Ssam 	char **argv;
371127Sbill {
38*35476Sbostic 	extern int optind;
39*35476Sbostic 	register int pid;
40*35476Sbostic 	int ch, status, lflag;
419228Ssam 	struct timeval before, after;
429228Ssam 	struct rusage ru;
431127Sbill 
44*35476Sbostic 	lflag = 0;
45*35476Sbostic 	while ((ch = getopt(argc, argv, "l")) != EOF)
46*35476Sbostic 		switch((char)ch) {
47*35476Sbostic 		case 'l':
48*35476Sbostic 			lflag = 1;
49*35476Sbostic 			break;
50*35476Sbostic 		case '?':
51*35476Sbostic 		default:
52*35476Sbostic 			fprintf(stderr, "usage: time [-l] command.\n");
53*35476Sbostic 			exit(1);
54*35476Sbostic 		}
55*35476Sbostic 
56*35476Sbostic 	if (!(argc -= optind))
571127Sbill 		exit(0);
58*35476Sbostic 	argv += optind;
59*35476Sbostic 
6032629Sbostic 	gettimeofday(&before, (struct timezone *)NULL);
61*35476Sbostic 	switch(pid = vfork()) {
62*35476Sbostic 	case -1:			/* error */
639228Ssam 		perror("time");
641127Sbill 		exit(1);
65*35476Sbostic 		/* NOTREACHED */
66*35476Sbostic 	case 0:				/* child */
67*35476Sbostic 		execvp(*argv, argv);
68*35476Sbostic 		perror(*argv);
69*35476Sbostic 		_exit(1);
70*35476Sbostic 		/* NOTREACHED */
711127Sbill 	}
72*35476Sbostic 	/* parent */
7332629Sbostic 	(void)signal(SIGINT, SIG_IGN);
7432629Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
75*35476Sbostic 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
7632629Sbostic 	gettimeofday(&after, (struct timezone *)NULL);
77*35476Sbostic 	if (status&0377)
789228Ssam 		fprintf(stderr, "Command terminated abnormally.\n");
799228Ssam 	after.tv_sec -= before.tv_sec;
809228Ssam 	after.tv_usec -= before.tv_usec;
819228Ssam 	if (after.tv_usec < 0)
829228Ssam 		after.tv_sec--, after.tv_usec += 1000000;
83*35476Sbostic 	fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
84*35476Sbostic 	fprintf(stderr, "%9ld.%02ld user ",
85*35476Sbostic 	    ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
86*35476Sbostic 	fprintf(stderr, "%9ld.%02ld sys\n",
87*35476Sbostic 	    ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
8832571Skarels 	if (lflag) {
8932571Skarels 		int hz = 100;			/* XXX */
9032571Skarels 		long ticks;
9132571Skarels 
9232571Skarels 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
9332571Skarels 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
9432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9532571Skarels 			ru.ru_maxrss, "maximum resident set size");
9632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9732571Skarels 			ru.ru_ixrss / ticks, "average shared memory size");
9832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
9932571Skarels 			ru.ru_idrss / ticks, "average unshared data size");
10032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10132571Skarels 			ru.ru_isrss / ticks, "average unshared stack size");
10232629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10332571Skarels 			ru.ru_minflt, "page reclaims");
10432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10532571Skarels 			ru.ru_majflt, "page faults");
10632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10732571Skarels 			ru.ru_nswap, "swaps");
10832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
10932571Skarels 			ru.ru_inblock, "block input operations");
11032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11132571Skarels 			ru.ru_oublock, "block output operations");
11232629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11332571Skarels 			ru.ru_msgsnd, "messages sent");
11432629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11532571Skarels 			ru.ru_msgrcv, "messages received");
11632629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11732571Skarels 			ru.ru_nsignals, "signals received");
11832629Sbostic 		fprintf(stderr, "%10ld  %s\n",
11932571Skarels 			ru.ru_nvcsw, "voluntary context switches");
12032629Sbostic 		fprintf(stderr, "%10ld  %s\n",
12132571Skarels 			ru.ru_nivcsw, "involuntary context switches");
12232571Skarels 	}
1239228Ssam 	exit (status>>8);
1241127Sbill }
125