date and time created 91/11/11 15:02:22 by bostic
[unix-history] / usr / src / usr.bin / time / time.c
CommitLineData
b1198826 1/*
4320bf82
KB
2 * Copyright (c) 1987, 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
cb956e54 5 * %sccs.include.redist.c%
b1198826 6 */
4320bf82
KB
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1987, 1988 The Regents of the University of California.\n\
11 All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
cb956e54 15static char sccsid[] = "@(#)time.c 4.9 (Berkeley) %G%";
4320bf82
KB
16#endif /* not lint */
17
63a84c73 18#include <sys/types.h>
7afd0a98
BJ
19#include <sys/time.h>
20#include <sys/resource.h>
4320bf82
KB
21#include <sys/signal.h>
22#include <stdio.h>
63a84c73
BJ
23
24main(argc, argv)
b1198826
SL
25 int argc;
26 char **argv;
63a84c73 27{
4320bf82
KB
28 extern int optind;
29 register int pid;
30 int ch, status, lflag;
b1198826
SL
31 struct timeval before, after;
32 struct rusage ru;
63a84c73 33
4320bf82
KB
34 lflag = 0;
35 while ((ch = getopt(argc, argv, "l")) != EOF)
36 switch((char)ch) {
37 case 'l':
38 lflag = 1;
39 break;
40 case '?':
41 default:
42 fprintf(stderr, "usage: time [-l] command.\n");
43 exit(1);
44 }
45
46 if (!(argc -= optind))
63a84c73 47 exit(0);
4320bf82
KB
48 argv += optind;
49
4caff17b 50 gettimeofday(&before, (struct timezone *)NULL);
4320bf82
KB
51 switch(pid = vfork()) {
52 case -1: /* error */
b1198826 53 perror("time");
63a84c73 54 exit(1);
4320bf82
KB
55 /* NOTREACHED */
56 case 0: /* child */
57 execvp(*argv, argv);
58 perror(*argv);
59 _exit(1);
60 /* NOTREACHED */
63a84c73 61 }
4320bf82 62 /* parent */
4caff17b
KB
63 (void)signal(SIGINT, SIG_IGN);
64 (void)signal(SIGQUIT, SIG_IGN);
4320bf82 65 while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */
4caff17b 66 gettimeofday(&after, (struct timezone *)NULL);
4320bf82 67 if (status&0377)
b1198826
SL
68 fprintf(stderr, "Command terminated abnormally.\n");
69 after.tv_sec -= before.tv_sec;
70 after.tv_usec -= before.tv_usec;
71 if (after.tv_usec < 0)
72 after.tv_sec--, after.tv_usec += 1000000;
4320bf82
KB
73 fprintf(stderr, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
74 fprintf(stderr, "%9ld.%02ld user ",
75 ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
76 fprintf(stderr, "%9ld.%02ld sys\n",
77 ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
e88e77d8
MK
78 if (lflag) {
79 int hz = 100; /* XXX */
80 long ticks;
81
82 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
83 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
4caff17b 84 fprintf(stderr, "%10ld %s\n",
e88e77d8 85 ru.ru_maxrss, "maximum resident set size");
4caff17b 86 fprintf(stderr, "%10ld %s\n",
e88e77d8 87 ru.ru_ixrss / ticks, "average shared memory size");
4caff17b 88 fprintf(stderr, "%10ld %s\n",
e88e77d8 89 ru.ru_idrss / ticks, "average unshared data size");
4caff17b 90 fprintf(stderr, "%10ld %s\n",
e88e77d8 91 ru.ru_isrss / ticks, "average unshared stack size");
4caff17b 92 fprintf(stderr, "%10ld %s\n",
e88e77d8 93 ru.ru_minflt, "page reclaims");
4caff17b 94 fprintf(stderr, "%10ld %s\n",
e88e77d8 95 ru.ru_majflt, "page faults");
4caff17b 96 fprintf(stderr, "%10ld %s\n",
e88e77d8 97 ru.ru_nswap, "swaps");
4caff17b 98 fprintf(stderr, "%10ld %s\n",
e88e77d8 99 ru.ru_inblock, "block input operations");
4caff17b 100 fprintf(stderr, "%10ld %s\n",
e88e77d8 101 ru.ru_oublock, "block output operations");
4caff17b 102 fprintf(stderr, "%10ld %s\n",
e88e77d8 103 ru.ru_msgsnd, "messages sent");
4caff17b 104 fprintf(stderr, "%10ld %s\n",
e88e77d8 105 ru.ru_msgrcv, "messages received");
4caff17b 106 fprintf(stderr, "%10ld %s\n",
e88e77d8 107 ru.ru_nsignals, "signals received");
4caff17b 108 fprintf(stderr, "%10ld %s\n",
e88e77d8 109 ru.ru_nvcsw, "voluntary context switches");
4caff17b 110 fprintf(stderr, "%10ld %s\n",
e88e77d8
MK
111 ru.ru_nivcsw, "involuntary context switches");
112 }
b1198826 113 exit (status>>8);
63a84c73 114}