1 /* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
13 #include <minix/minlib.h>
14 // #include <minix/u64.h>
17 /* -DNEW prints time to 0.01 sec. */
28 int main(int argc
, char **argv
);
29 void print_time(clock_t t
);
30 void twin(int n
, char *p
);
38 struct tms pre_buf
, post_buf
;
41 struct timeval start_time
, end_time
;
44 int start_time
, end_time
;
46 u64_t start_tsc
, end_tsc
, spent_tsc
;
50 if (argc
== 1) exit(0);
52 while((c
=getopt(argc
, argv
, "C")) != EOF
) {
58 fprintf(stderr
, "usage: time [-C] <command>\n");
69 /* Get real time at start of run. */
71 (void) sysutime(UTIME_TIMEOFDAY
, &start_time
);
73 start_time
= times(&dummy
);
75 read_tsc_64(&start_tsc
);
78 if ((pid
= fork()) < 0) {
79 std_err("Cannot fork\n");
82 if (pid
== 0) execute();
84 /* Parent is the time program. Disable interrupts and wait. */
85 signal(SIGINT
, SIG_IGN
);
86 signal(SIGQUIT
, SIG_IGN
);
90 } while (wait(&status
) != pid
);
91 read_tsc_64(&end_tsc
);
92 spent_tsc
= end_tsc
- start_tsc
;
94 (void) sysutime(UTIME_TIMEOFDAY
, &end_time
);
95 real_time
= (end_time
.tv_sec
- start_time
.tv_sec
) * CLOCKS_PER_SEC
96 + (end_time
.tv_usec
- start_time
.tv_usec
) * CLOCKS_PER_SEC
/ 1000000;
98 end_time
= times(&dummy
);
99 real_time
= (end_time
- start_time
);
102 if ((status
& 0377) != 0) std_err("Command terminated abnormally.\n");
106 fprintf(stderr
, "%qd tsc ", spent_tsc
);
108 /* Print results. -DNEW enables time on one line to 0.01 sec */
111 print_time(real_time
);
113 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
115 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
118 print_time(real_time
);
120 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
122 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
125 return((status
& 0377) ? -1 : (status
>> 8));
131 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
133 int hours
, minutes
, seconds
, hundredths
, i
;
136 system_hz
= (u32_t
) sysconf(_SC_CLK_TCK
);
139 for (i
= 0; i
< 8; i
++) a
[i
] = ' ';
140 hours
= (int) (t
/ ((clock_t) 3600 * system_hz
));
141 t
-= (clock_t) hours
* 3600 * system_hz
;
142 minutes
= (int) (t
/ ((clock_t) 60 * system_hz
));
143 t
-= (clock_t) minutes
* 60 * system_hz
;
144 seconds
= (int) (t
/ system_hz
);
145 t
-= (clock_t) seconds
* system_hz
;
146 hundredths
= (int) (t
* 100 / system_hz
);
152 if (minutes
|| digit_seen
) {
153 twin(minutes
, &a
[3]);
156 if (seconds
|| digit_seen
)
157 twin(seconds
, &a
[6]);
160 a
[9] = hundredths
/ 10 + '0';
161 #ifdef HUNDREDTHS /* tenths used to be enough */
162 a
[10] = hundredths
% 10 + '0';
174 if (digit_seen
== 0 && c1
== '0') c1
= ' ';
177 if (n
> 0) digit_seen
= 1;
183 std_err("Cannot execute ");