diff options
author | anton <anton@cvs.openbsd.org> | 2017-07-22 09:37:22 +0000 |
---|---|---|
committer | anton <anton@cvs.openbsd.org> | 2017-07-22 09:37:22 +0000 |
commit | bd2709dd1500f77d1f9d41d0c8cdd51781768680 (patch) | |
tree | cd0e0724ca87b6bf1004cd7d6679d3e2420f9c02 /bin/ksh/c_sh.c | |
parent | 8a5d19608c201eda68d49d1d5a117be465dc2a33 (diff) |
Use monotonic clock for the time command in csh and ksh.
From Scott Cheloha
ok tb@
Diffstat (limited to 'bin/ksh/c_sh.c')
-rw-r--r-- | bin/ksh/c_sh.c | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/bin/ksh/c_sh.c b/bin/ksh/c_sh.c index 4e40c464ecb..6c54839cdcc 100644 --- a/bin/ksh/c_sh.c +++ b/bin/ksh/c_sh.c @@ -1,4 +1,4 @@ -/* $OpenBSD: c_sh.c,v 1.59 2016/03/04 15:11:06 deraadt Exp $ */ +/* $OpenBSD: c_sh.c,v 1.60 2017/07/22 09:37:21 anton Exp $ */ /* * built-in Bourne commands @@ -18,7 +18,8 @@ #include "sh.h" -static void p_time(struct shf *, int, struct timeval *, int, char *, char *); +static void p_tv(struct shf *, int, struct timeval *, int, char *, char *); +static void p_ts(struct shf *, int, struct timespec *, int, char *, char *); /* :, false and true */ int @@ -670,7 +671,7 @@ c_unset(char **wp) } static void -p_time(struct shf *shf, int posix, struct timeval *tv, int width, char *prefix, +p_tv(struct shf *shf, int posix, struct timeval *tv, int width, char *prefix, char *suffix) { if (posix) @@ -683,18 +684,34 @@ p_time(struct shf *shf, int posix, struct timeval *tv, int width, char *prefix, tv->tv_usec / 10000, suffix); } +static void +p_ts(struct shf *shf, int posix, struct timespec *ts, int width, char *prefix, + char *suffix) +{ + if (posix) + shf_fprintf(shf, "%s%*lld.%02ld%s", prefix ? prefix : "", + width, (long long)ts->tv_sec, ts->tv_nsec / 10000000, + suffix); + else + shf_fprintf(shf, "%s%*lldm%02lld.%02lds%s", prefix ? prefix : "", + width, (long long)ts->tv_sec / 60, + (long long)ts->tv_sec % 60, + ts->tv_nsec / 10000000, suffix); +} + + int c_times(char **wp) { struct rusage usage; (void) getrusage(RUSAGE_SELF, &usage); - p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); - p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); + p_tv(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); + p_tv(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); (void) getrusage(RUSAGE_CHILDREN, &usage); - p_time(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); - p_time(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); + p_tv(shl_stdout, 0, &usage.ru_utime, 0, NULL, " "); + p_tv(shl_stdout, 0, &usage.ru_stime, 0, NULL, "\n"); return 0; } @@ -710,11 +727,12 @@ timex(struct op *t, int f, volatile int *xerrok) #define TF_POSIX BIT(2) /* report in posix format */ int rv = 0; struct rusage ru0, ru1, cru0, cru1; - struct timeval usrtime, systime, tv0, tv1; + struct timeval usrtime, systime; + struct timespec ts0, ts1, ts2; int tf = 0; extern struct timeval j_usrtime, j_systime; /* computed by j_wait */ - gettimeofday(&tv0, NULL); + clock_gettime(CLOCK_MONOTONIC, &ts0); getrusage(RUSAGE_SELF, &ru0); getrusage(RUSAGE_CHILDREN, &cru0); if (t->left) { @@ -731,7 +749,7 @@ timex(struct op *t, int f, volatile int *xerrok) rv = execute(t->left, f | XTIME, xerrok); if (t->left->type == TCOM) tf |= t->left->str[0]; - gettimeofday(&tv1, NULL); + clock_gettime(CLOCK_MONOTONIC, &ts1); getrusage(RUSAGE_SELF, &ru1); getrusage(RUSAGE_CHILDREN, &cru1); } else @@ -749,20 +767,20 @@ timex(struct op *t, int f, volatile int *xerrok) } if (!(tf & TF_NOREAL)) { - timersub(&tv1, &tv0, &tv1); + timespecsub(&ts1, &ts0, &ts2); if (tf & TF_POSIX) - p_time(shl_out, 1, &tv1, 5, "real ", "\n"); + p_ts(shl_out, 1, &ts2, 5, "real ", "\n"); else - p_time(shl_out, 0, &tv1, 5, NULL, " real "); + p_ts(shl_out, 0, &ts2, 5, NULL, " real "); } if (tf & TF_POSIX) - p_time(shl_out, 1, &usrtime, 5, "user ", "\n"); + p_tv(shl_out, 1, &usrtime, 5, "user ", "\n"); else - p_time(shl_out, 0, &usrtime, 5, NULL, " user "); + p_tv(shl_out, 0, &usrtime, 5, NULL, " user "); if (tf & TF_POSIX) - p_time(shl_out, 1, &systime, 5, "sys ", "\n"); + p_tv(shl_out, 1, &systime, 5, "sys ", "\n"); else - p_time(shl_out, 0, &systime, 5, NULL, " system\n"); + p_tv(shl_out, 0, &systime, 5, NULL, " system\n"); shf_flush(shl_out); return rv; |