summaryrefslogtreecommitdiff
path: root/usr.bin/time
diff options
context:
space:
mode:
authorAlan Somers <asomers@FreeBSD.org>2018-01-24 17:12:34 +0000
committerAlan Somers <asomers@FreeBSD.org>2018-01-24 17:12:34 +0000
commitcb938d95e249d122a0f3146c9d03deb143ca4450 (patch)
tree98dd25ba4e2b6074a8fb2e7fc9573b41e8b7ee46 /usr.bin/time
parent28f3d8b2c2999bbf1ba18565576395127b0c226e (diff)
downloadsrc-test-cb938d95e249d122a0f3146c9d03deb143ca4450.tar.gz
src-test-cb938d95e249d122a0f3146c9d03deb143ca4450.zip
time(1): use clock_gettime(2) instead of gettimeofday(2)
This is a prerequisite to adding support for the monotonic clock Reviewed by: ken, imp MFC after: 3 weeks Sponsored by: Spectra Logic Corp Differential Revision: https://reviews.freebsd.org/D14030
Notes
Notes: svn path=/head/; revision=328343
Diffstat (limited to 'usr.bin/time')
-rw-r--r--usr.bin/time/time.c38
1 files changed, 21 insertions, 17 deletions
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c
index f8421365c919f..02c67ad199140 100644
--- a/usr.bin/time/time.c
+++ b/usr.bin/time/time.c
@@ -58,18 +58,19 @@ static const char rcsid[] =
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
static int getstathz(void);
static void humantime(FILE *, long, long);
-static void showtime(FILE *, struct timeval *, struct timeval *,
+static void showtime(FILE *, struct timespec *, struct timespec *,
struct rusage *);
static void siginfo(int);
static void usage(void);
static sig_atomic_t siginfo_recvd;
static char decimal_point;
-static struct timeval before_tv;
+static struct timespec before_ts;
static int hflag, pflag;
int
@@ -80,7 +81,7 @@ main(int argc, char **argv)
pid_t pid;
struct rlimit rl;
struct rusage ru;
- struct timeval after;
+ struct timespec after;
char *ofn = NULL;
FILE *out = stderr;
@@ -120,7 +121,8 @@ main(int argc, char **argv)
setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
}
- (void)gettimeofday(&before_tv, NULL);
+ if (clock_gettime(CLOCK_REALTIME, &before_ts))
+ err(1, "clock_gettime");
switch(pid = fork()) {
case -1: /* error */
err(1, "time");
@@ -139,16 +141,18 @@ main(int argc, char **argv)
while (wait4(pid, &status, 0, &ru) != pid) {
if (siginfo_recvd) {
siginfo_recvd = 0;
- (void)gettimeofday(&after, NULL);
+ if (clock_gettime(CLOCK_REALTIME, &after))
+ err(1, "clock_gettime");
getrusage(RUSAGE_CHILDREN, &ru);
- showtime(stdout, &before_tv, &after, &ru);
+ showtime(stdout, &before_ts, &after, &ru);
}
}
- (void)gettimeofday(&after, NULL);
+ if (clock_gettime(CLOCK_REALTIME, &after))
+ err(1, "clock_gettime");
if ( ! WIFEXITED(status))
warnx("command terminated abnormally");
exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
- showtime(out, &before_tv, &after, &ru);
+ showtime(out, &before_ts, &after, &ru);
if (lflag) {
int hz = getstathz();
u_long ticks;
@@ -237,7 +241,7 @@ getstathz(void)
}
static void
-humantime(FILE *out, long sec, long usec)
+humantime(FILE *out, long sec, long centisec)
{
long days, hrs, mins;
@@ -255,18 +259,18 @@ humantime(FILE *out, long sec, long usec)
fprintf(out, "%ldh", hrs);
if (mins)
fprintf(out, "%ldm", mins);
- fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
+ fprintf(out, "%ld%c%02lds", sec, decimal_point, centisec);
}
static void
-showtime(FILE *out, struct timeval *before, struct timeval *after,
+showtime(FILE *out, struct timespec *before, struct timespec *after,
struct rusage *ru)
{
after->tv_sec -= before->tv_sec;
- after->tv_usec -= before->tv_usec;
- if (after->tv_usec < 0)
- after->tv_sec--, after->tv_usec += 1000000;
+ after->tv_nsec -= before->tv_nsec;
+ if (after->tv_nsec < 0)
+ after->tv_sec--, after->tv_nsec += 1000000000;
if (pflag) {
/* POSIX wants output that must look like
@@ -274,7 +278,7 @@ showtime(FILE *out, struct timeval *before, struct timeval *after,
at least two digits after the radix. */
fprintf(out, "real %jd%c%02ld\n",
(intmax_t)after->tv_sec, decimal_point,
- after->tv_usec/10000);
+ after->tv_nsec/10000000);
fprintf(out, "user %jd%c%02ld\n",
(intmax_t)ru->ru_utime.tv_sec, decimal_point,
ru->ru_utime.tv_usec/10000);
@@ -282,7 +286,7 @@ showtime(FILE *out, struct timeval *before, struct timeval *after,
(intmax_t)ru->ru_stime.tv_sec, decimal_point,
ru->ru_stime.tv_usec/10000);
} else if (hflag) {
- humantime(out, after->tv_sec, after->tv_usec/10000);
+ humantime(out, after->tv_sec, after->tv_nsec/10000000);
fprintf(out, " real\t");
humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
fprintf(out, " user\t");
@@ -291,7 +295,7 @@ showtime(FILE *out, struct timeval *before, struct timeval *after,
} else {
fprintf(out, "%9jd%c%02ld real ",
(intmax_t)after->tv_sec, decimal_point,
- after->tv_usec/10000);
+ after->tv_nsec/10000000);
fprintf(out, "%9jd%c%02ld user ",
(intmax_t)ru->ru_utime.tv_sec, decimal_point,
ru->ru_utime.tv_usec/10000);