xref: /original-bsd/usr.bin/time/time.c (revision ad93c43e)
1 #ifndef lint
2 static	char *sccsid = "@(#)time.c	4.7 (Berkeley) 11/17/87";
3 #endif
4 
5 /*
6  * time
7  */
8 #include <stdio.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13 
14 main(argc, argv)
15 	int argc;
16 	char **argv;
17 {
18 	int status, lflag = 0;
19 	register int p;
20 	struct timeval before, after;
21 	struct rusage ru;
22 
23 	if (argc<=1)
24 		exit(0);
25 	if (strcmp(argv[1], "-l") == 0) {
26 		lflag++;
27 		argc--;
28 		argv++;
29 	}
30 	gettimeofday(&before, (struct timezone *)NULL);
31 	p = fork();
32 	if (p < 0) {
33 		perror("time");
34 		exit(1);
35 	}
36 	if (p == 0) {
37 		execvp(argv[1], &argv[1]);
38 		perror(argv[1]);
39 		exit(1);
40 	}
41 	(void)signal(SIGINT, SIG_IGN);
42 	(void)signal(SIGQUIT, SIG_IGN);
43 	while (wait3(&status, 0, &ru) != p)
44 		;
45 	gettimeofday(&after, (struct timezone *)NULL);
46 	if ((status&0377) != 0)
47 		fprintf(stderr, "Command terminated abnormally.\n");
48 	after.tv_sec -= before.tv_sec;
49 	after.tv_usec -= before.tv_usec;
50 	if (after.tv_usec < 0)
51 		after.tv_sec--, after.tv_usec += 1000000;
52 	printt("real", &after);
53 	printt("user", &ru.ru_utime);
54 	printt("sys ", &ru.ru_stime);
55 	fprintf(stderr, "\n");
56 	if (lflag) {
57 		int hz = 100;			/* XXX */
58 		long ticks;
59 
60 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
61 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
62 		fprintf(stderr, "%10ld  %s\n",
63 			ru.ru_maxrss, "maximum resident set size");
64 		fprintf(stderr, "%10ld  %s\n",
65 			ru.ru_ixrss / ticks, "average shared memory size");
66 		fprintf(stderr, "%10ld  %s\n",
67 			ru.ru_idrss / ticks, "average unshared data size");
68 		fprintf(stderr, "%10ld  %s\n",
69 			ru.ru_isrss / ticks, "average unshared stack size");
70 		fprintf(stderr, "%10ld  %s\n",
71 			ru.ru_minflt, "page reclaims");
72 		fprintf(stderr, "%10ld  %s\n",
73 			ru.ru_majflt, "page faults");
74 		fprintf(stderr, "%10ld  %s\n",
75 			ru.ru_nswap, "swaps");
76 		fprintf(stderr, "%10ld  %s\n",
77 			ru.ru_inblock, "block input operations");
78 		fprintf(stderr, "%10ld  %s\n",
79 			ru.ru_oublock, "block output operations");
80 		fprintf(stderr, "%10ld  %s\n",
81 			ru.ru_msgsnd, "messages sent");
82 		fprintf(stderr, "%10ld  %s\n",
83 			ru.ru_msgrcv, "messages received");
84 		fprintf(stderr, "%10ld  %s\n",
85 			ru.ru_nsignals, "signals received");
86 		fprintf(stderr, "%10ld  %s\n",
87 			ru.ru_nvcsw, "voluntary context switches");
88 		fprintf(stderr, "%10ld  %s\n",
89 			ru.ru_nivcsw, "involuntary context switches");
90 	}
91 	exit (status>>8);
92 }
93 
94 printt(s, tv)
95 	char *s;
96 	struct timeval *tv;
97 {
98 
99 	fprintf(stderr, "%9ld.%02ld %s ", tv->tv_sec, tv->tv_usec/10000, s);
100 }
101