2005-04-21 16:53:53 +02:00
|
|
|
/* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
|
|
|
|
|
|
|
|
#define NEW 1
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/times.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* -DNEW prints time to 0.01 sec. */
|
|
|
|
#ifdef NEW
|
|
|
|
#define HUNDREDTHS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char **args;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
int digit_seen;
|
|
|
|
char a[] = " . \0";
|
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
int main(int argc, char **argv);
|
|
|
|
void print_time(clock_t t);
|
|
|
|
void twin(int n, char *p);
|
|
|
|
void execute(void);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
int main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
2011-09-21 21:16:34 +02:00
|
|
|
int cycles = 0;
|
2005-04-21 16:53:53 +02:00
|
|
|
struct tms pre_buf, post_buf;
|
|
|
|
int status, pid;
|
2005-05-20 12:06:33 +02:00
|
|
|
struct tms dummy;
|
|
|
|
int start_time, end_time;
|
2011-09-21 21:16:34 +02:00
|
|
|
u64_t start_tsc, end_tsc, spent_tsc;
|
2005-04-21 16:53:53 +02:00
|
|
|
clock_t real_time;
|
2011-09-21 21:16:34 +02:00
|
|
|
int c;
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
if (argc == 1) exit(0);
|
|
|
|
|
2011-09-21 21:16:34 +02:00
|
|
|
while((c=getopt(argc, argv, "C")) != EOF) {
|
|
|
|
switch(c) {
|
|
|
|
case 'C':
|
|
|
|
cycles = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "usage: time [-C] <command>\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
args = &argv[0];
|
|
|
|
name = argv[0];
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Get real time at start of run. */
|
2005-05-20 12:06:33 +02:00
|
|
|
start_time = times(&dummy);
|
2011-09-21 21:16:34 +02:00
|
|
|
read_tsc_64(&start_tsc);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
/* Fork off child. */
|
|
|
|
if ((pid = fork()) < 0) {
|
|
|
|
std_err("Cannot fork\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (pid == 0) execute();
|
|
|
|
|
|
|
|
/* Parent is the time program. Disable interrupts and wait. */
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
|
|
|
|
do {
|
|
|
|
times(&pre_buf);
|
|
|
|
} while (wait(&status) != pid);
|
2011-09-21 21:16:34 +02:00
|
|
|
read_tsc_64(&end_tsc);
|
2012-09-24 21:05:01 +02:00
|
|
|
spent_tsc = end_tsc - start_tsc;
|
2005-05-20 12:06:33 +02:00
|
|
|
end_time = times(&dummy);
|
|
|
|
real_time = (end_time - start_time);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
|
|
|
|
times(&post_buf);
|
|
|
|
|
2011-09-21 21:16:34 +02:00
|
|
|
if(cycles) {
|
|
|
|
fprintf(stderr, "%qd tsc ", spent_tsc);
|
|
|
|
}
|
2005-04-21 16:53:53 +02:00
|
|
|
/* Print results. -DNEW enables time on one line to 0.01 sec */
|
|
|
|
#ifndef NEW
|
|
|
|
std_err("real ");
|
|
|
|
print_time(real_time);
|
|
|
|
std_err("\nuser ");
|
|
|
|
print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
|
|
|
|
std_err("\nsys ");
|
|
|
|
print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
|
|
|
|
std_err("\n");
|
|
|
|
#else
|
|
|
|
print_time(real_time);
|
|
|
|
std_err(" real");
|
|
|
|
print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
|
|
|
|
std_err(" user");
|
|
|
|
print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
|
|
|
|
std_err(" sys\n");
|
|
|
|
#endif
|
|
|
|
return((status & 0377) ? -1 : (status >> 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_time(t)
|
|
|
|
register clock_t t;
|
|
|
|
{
|
|
|
|
/* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
|
|
|
|
|
|
|
|
int hours, minutes, seconds, hundredths, i;
|
2009-08-30 18:06:59 +02:00
|
|
|
u32_t system_hz;
|
|
|
|
|
2010-09-14 23:31:56 +02:00
|
|
|
system_hz = (u32_t) sysconf(_SC_CLK_TCK);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
digit_seen = 0;
|
|
|
|
for (i = 0; i < 8; i++) a[i] = ' ';
|
2009-08-30 18:06:59 +02:00
|
|
|
hours = (int) (t / ((clock_t) 3600 * system_hz));
|
|
|
|
t -= (clock_t) hours * 3600 * system_hz;
|
|
|
|
minutes = (int) (t / ((clock_t) 60 * system_hz));
|
|
|
|
t -= (clock_t) minutes * 60 * system_hz;
|
|
|
|
seconds = (int) (t / system_hz);
|
|
|
|
t -= (clock_t) seconds * system_hz;
|
|
|
|
hundredths = (int) (t * 100 / system_hz);
|
2005-04-21 16:53:53 +02:00
|
|
|
|
|
|
|
if (hours) {
|
|
|
|
twin(hours, &a[0]);
|
|
|
|
a[2] = ':';
|
|
|
|
}
|
|
|
|
if (minutes || digit_seen) {
|
|
|
|
twin(minutes, &a[3]);
|
|
|
|
a[5] = ':';
|
|
|
|
}
|
|
|
|
if (seconds || digit_seen)
|
|
|
|
twin(seconds, &a[6]);
|
|
|
|
else
|
|
|
|
a[7] = '0';
|
|
|
|
a[9] = hundredths / 10 + '0';
|
|
|
|
#ifdef HUNDREDTHS /* tenths used to be enough */
|
|
|
|
a[10] = hundredths % 10 + '0';
|
|
|
|
#endif
|
|
|
|
std_err(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void twin(n, p)
|
|
|
|
int n;
|
|
|
|
char *p;
|
|
|
|
{
|
|
|
|
char c1, c2;
|
|
|
|
c1 = (n / 10) + '0';
|
|
|
|
c2 = (n % 10) + '0';
|
|
|
|
if (digit_seen == 0 && c1 == '0') c1 = ' ';
|
|
|
|
*p++ = c1;
|
|
|
|
*p++ = c2;
|
|
|
|
if (n > 0) digit_seen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void execute()
|
|
|
|
{
|
|
|
|
execvp(name, args);
|
|
|
|
std_err("Cannot execute ");
|
|
|
|
std_err(name);
|
|
|
|
std_err("\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|