1 /* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
13 #include <minix/minlib.h>
16 /* -DNEW prints time to 0.01 sec. */
27 int main(int argc
, char **argv
);
28 void print_time(clock_t t
);
29 void twin(int n
, char *p
);
37 struct tms pre_buf
, post_buf
;
40 int start_time
, end_time
;
41 u64_t start_tsc
, end_tsc
, spent_tsc
;
45 if (argc
== 1) exit(0);
47 while((c
=getopt(argc
, argv
, "C")) != EOF
) {
53 fprintf(stderr
, "usage: time [-C] <command>\n");
64 /* Get real time at start of run. */
65 start_time
= times(&dummy
);
66 read_tsc_64(&start_tsc
);
69 if ((pid
= fork()) < 0) {
70 std_err("Cannot fork\n");
73 if (pid
== 0) execute();
75 /* Parent is the time program. Disable interrupts and wait. */
76 signal(SIGINT
, SIG_IGN
);
77 signal(SIGQUIT
, SIG_IGN
);
81 } while (wait(&status
) != pid
);
82 read_tsc_64(&end_tsc
);
83 spent_tsc
= end_tsc
- start_tsc
;
84 end_time
= times(&dummy
);
85 real_time
= (end_time
- start_time
);
87 if ((status
& 0377) != 0) std_err("Command terminated abnormally.\n");
91 fprintf(stderr
, "%qd tsc ", spent_tsc
);
93 /* Print results. -DNEW enables time on one line to 0.01 sec */
96 print_time(real_time
);
98 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
100 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
103 print_time(real_time
);
105 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
107 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
110 return((status
& 0377) ? -1 : (status
>> 8));
116 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
118 int hours
, minutes
, seconds
, hundredths
, i
;
121 system_hz
= (u32_t
) sysconf(_SC_CLK_TCK
);
124 for (i
= 0; i
< 8; i
++) a
[i
] = ' ';
125 hours
= (int) (t
/ ((clock_t) 3600 * system_hz
));
126 t
-= (clock_t) hours
* 3600 * system_hz
;
127 minutes
= (int) (t
/ ((clock_t) 60 * system_hz
));
128 t
-= (clock_t) minutes
* 60 * system_hz
;
129 seconds
= (int) (t
/ system_hz
);
130 t
-= (clock_t) seconds
* system_hz
;
131 hundredths
= (int) (t
* 100 / system_hz
);
137 if (minutes
|| digit_seen
) {
138 twin(minutes
, &a
[3]);
141 if (seconds
|| digit_seen
)
142 twin(seconds
, &a
[6]);
145 a
[9] = hundredths
/ 10 + '0';
146 #ifdef HUNDREDTHS /* tenths used to be enough */
147 a
[10] = hundredths
% 10 + '0';
159 if (digit_seen
== 0 && c1
== '0') c1
= ' ';
162 if (n
> 0) digit_seen
= 1;
168 std_err("Cannot execute ");