xref: /csrg-svn/usr.bin/time/time.c (revision 32629)
112900Ssam #ifndef lint
2*32629Sbostic static	char *sccsid = "@(#)time.c	4.7 (Berkeley) 11/17/87";
312900Ssam #endif
41127Sbill 
59228Ssam /*
69228Ssam  * time
79228Ssam  */
81127Sbill #include <stdio.h>
91127Sbill #include <signal.h>
101127Sbill #include <sys/types.h>
1113594Swnj #include <sys/time.h>
1213594Swnj #include <sys/resource.h>
131127Sbill 
141127Sbill main(argc, argv)
159228Ssam 	int argc;
169228Ssam 	char **argv;
171127Sbill {
1832571Skarels 	int status, lflag = 0;
199228Ssam 	register int p;
209228Ssam 	struct timeval before, after;
219228Ssam 	struct rusage ru;
221127Sbill 
239228Ssam 	if (argc<=1)
241127Sbill 		exit(0);
2532571Skarels 	if (strcmp(argv[1], "-l") == 0) {
2632571Skarels 		lflag++;
2732571Skarels 		argc--;
2832571Skarels 		argv++;
2932571Skarels 	}
30*32629Sbostic 	gettimeofday(&before, (struct timezone *)NULL);
311127Sbill 	p = fork();
329228Ssam 	if (p < 0) {
339228Ssam 		perror("time");
341127Sbill 		exit(1);
351127Sbill 	}
369228Ssam 	if (p == 0) {
371127Sbill 		execvp(argv[1], &argv[1]);
389228Ssam 		perror(argv[1]);
391127Sbill 		exit(1);
401127Sbill 	}
41*32629Sbostic 	(void)signal(SIGINT, SIG_IGN);
42*32629Sbostic 	(void)signal(SIGQUIT, SIG_IGN);
439228Ssam 	while (wait3(&status, 0, &ru) != p)
449228Ssam 		;
45*32629Sbostic 	gettimeofday(&after, (struct timezone *)NULL);
469228Ssam 	if ((status&0377) != 0)
479228Ssam 		fprintf(stderr, "Command terminated abnormally.\n");
489228Ssam 	after.tv_sec -= before.tv_sec;
499228Ssam 	after.tv_usec -= before.tv_usec;
509228Ssam 	if (after.tv_usec < 0)
519228Ssam 		after.tv_sec--, after.tv_usec += 1000000;
529228Ssam 	printt("real", &after);
539228Ssam 	printt("user", &ru.ru_utime);
549228Ssam 	printt("sys ", &ru.ru_stime);
551370Sbill 	fprintf(stderr, "\n");
5632571Skarels 	if (lflag) {
5732571Skarels 		int hz = 100;			/* XXX */
5832571Skarels 		long ticks;
5932571Skarels 
6032571Skarels 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
6132571Skarels 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
62*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
6332571Skarels 			ru.ru_maxrss, "maximum resident set size");
64*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
6532571Skarels 			ru.ru_ixrss / ticks, "average shared memory size");
66*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
6732571Skarels 			ru.ru_idrss / ticks, "average unshared data size");
68*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
6932571Skarels 			ru.ru_isrss / ticks, "average unshared stack size");
70*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
7132571Skarels 			ru.ru_minflt, "page reclaims");
72*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
7332571Skarels 			ru.ru_majflt, "page faults");
74*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
7532571Skarels 			ru.ru_nswap, "swaps");
76*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
7732571Skarels 			ru.ru_inblock, "block input operations");
78*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
7932571Skarels 			ru.ru_oublock, "block output operations");
80*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8132571Skarels 			ru.ru_msgsnd, "messages sent");
82*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8332571Skarels 			ru.ru_msgrcv, "messages received");
84*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8532571Skarels 			ru.ru_nsignals, "signals received");
86*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8732571Skarels 			ru.ru_nvcsw, "voluntary context switches");
88*32629Sbostic 		fprintf(stderr, "%10ld  %s\n",
8932571Skarels 			ru.ru_nivcsw, "involuntary context switches");
9032571Skarels 	}
919228Ssam 	exit (status>>8);
921127Sbill }
931127Sbill 
949228Ssam printt(s, tv)
959228Ssam 	char *s;
969228Ssam 	struct timeval *tv;
971127Sbill {
981127Sbill 
99*32629Sbostic 	fprintf(stderr, "%9ld.%02ld %s ", tv->tv_sec, tv->tv_usec/10000, s);
1001127Sbill }
101