X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftime%2Ftime.c;h=166832d8a961d56b8ebcf9ebd4b58c62d1b1e4ed;hb=HEAD;hp=5e36bf7b48dec210ac3f9341b9caee53b7164acb;hpb=74257cee33ae3033f961d5f22a0313b8cb1b18d4;p=ccan diff --git a/ccan/time/time.c b/ccan/time/time.c index 5e36bf7b..98107922 100644 --- a/ccan/time/time.c +++ b/ccan/time/time.c @@ -1,108 +1,138 @@ /* Licensed under BSD-MIT - see LICENSE file for details */ #include #include -#include +#include -struct timeval time_now(void) +#if !HAVE_CLOCK_GETTIME +#include + +struct timeabs time_now(void) { struct timeval now; + struct timeabs ret; gettimeofday(&now, NULL); - return now; + ret.ts.tv_sec = now.tv_sec; + ret.ts.tv_nsec = now.tv_usec * 1000; + return TIMEABS_CHECK(ret); } - -bool time_greater(struct timeval a, struct timeval b) +#else +#include +struct timeabs time_now(void) { - if (a.tv_sec > b.tv_sec) - return true; - else if (a.tv_sec < b.tv_sec) - return false; - - return a.tv_usec > b.tv_usec; + struct timeabs ret; + clock_gettime(CLOCK_REALTIME, &ret.ts); + return TIMEABS_CHECK(ret); } +#endif /* HAVE_CLOCK_GETTIME */ -bool time_less(struct timeval a, struct timeval b) +struct timemono time_mono(void) { - if (a.tv_sec < b.tv_sec) - return true; - else if (a.tv_sec > b.tv_sec) - return false; - - return a.tv_usec < b.tv_usec; + struct timemono ret; +#if TIME_HAVE_MONOTONIC + clock_gettime(CLOCK_MONOTONIC, &ret.ts); +#else /* Best we can do */ + ret.ts = time_now().ts; +#endif /* !HAVE_TIME_MONOTONIC */ + return TIMEMONO_CHECK(ret); } -bool time_eq(struct timeval a, struct timeval b) +struct timerel time_divide(struct timerel t, unsigned long div) { - return a.tv_sec == b.tv_sec && a.tv_usec == b.tv_usec; + struct timerel res; + uint64_t rem, ns; + + /* Dividing seconds is simple. */ + res.ts.tv_sec = TIMEREL_CHECK(t).ts.tv_sec / div; + rem = t.ts.tv_sec % div; + + /* If we can't fit remainder * 1,000,000,000 in 64 bits? */ +#if 0 /* ilog is great, but we use fp for multiply anyway. */ + bits = ilog64(rem); + if (bits + 30 >= 64) { + /* Reduce accuracy slightly */ + rem >>= (bits - (64 - 30)); + div >>= (bits - (64 - 30)); + } +#endif + if (rem & ~(((uint64_t)1 << 30) - 1)) { + /* FIXME: fp is cheating! */ + double nsec = rem * 1000000000.0 + t.ts.tv_nsec; + res.ts.tv_nsec = nsec / div; + } else { + ns = rem * 1000000000 + t.ts.tv_nsec; + res.ts.tv_nsec = ns / div; + } + return TIMEREL_CHECK(res); } -struct timeval time_sub(struct timeval recent, struct timeval old) +struct timerel time_multiply(struct timerel t, unsigned long mult) { - struct timeval diff; + struct timerel res; - diff.tv_sec = recent.tv_sec - old.tv_sec; - if (old.tv_usec > recent.tv_usec) { - diff.tv_sec--; - diff.tv_usec = 1000000 + recent.tv_usec - old.tv_usec; - } else - diff.tv_usec = recent.tv_usec - old.tv_usec; + /* Are we going to overflow if we multiply nsec? */ + if (mult & ~((1UL << 30) - 1)) { + /* FIXME: fp is cheating! */ + double nsec = (double)t.ts.tv_nsec * mult; - assert(diff.tv_sec >= 0); - return diff; -} + res.ts.tv_sec = nsec / 1000000000.0; + res.ts.tv_nsec = nsec - (res.ts.tv_sec * 1000000000.0); + } else { + uint64_t nsec = t.ts.tv_nsec * mult; -struct timeval time_add(struct timeval a, struct timeval b) -{ - struct timeval sum; - - sum.tv_sec = a.tv_sec + b.tv_sec; - sum.tv_usec = a.tv_usec + b.tv_usec; - if (sum.tv_usec > 1000000) { - sum.tv_sec++; - sum.tv_usec -= 1000000; + res.ts.tv_nsec = nsec % 1000000000; + res.ts.tv_sec = nsec / 1000000000; } - return sum; -} - -struct timeval time_divide(struct timeval t, unsigned long div) -{ - return time_from_usec(time_to_usec(t) / div); -} - -struct timeval time_multiply(struct timeval t, unsigned long mult) -{ - return time_from_usec(time_to_usec(t) * mult); + res.ts.tv_sec += TIMEREL_CHECK(t).ts.tv_sec * mult; + return TIMEREL_CHECK(res); } -uint64_t time_to_msec(struct timeval t) +struct timespec time_check_(struct timespec t, const char *abortstr) { - uint64_t msec; - - msec = t.tv_usec / 1000 + (uint64_t)t.tv_sec * 1000; - return msec; + if (t.tv_sec < 0 || t.tv_nsec >= 1000000000) { + if (abortstr) { + fprintf(stderr, "%s: malformed time %li.%09li\n", + abortstr, + (long)t.tv_sec, (long)t.tv_nsec); + abort(); + } else { + struct timespec old = t; + + if (t.tv_nsec >= 1000000000) { + t.tv_sec += t.tv_nsec / 1000000000; + t.tv_nsec %= 1000000000; + } + if (t.tv_sec < 0) + t.tv_sec = 0; + + fprintf(stderr, "WARNING: malformed time" + " %li seconds %li ns converted to %li.%09li.\n", + (long)old.tv_sec, (long)old.tv_nsec, + (long)t.tv_sec, (long)t.tv_nsec); + } + } + return t; } -uint64_t time_to_usec(struct timeval t) +struct timerel timerel_check(struct timerel t, const char *abortstr) { - uint64_t usec; + struct timerel ret; - usec = t.tv_usec + (uint64_t)t.tv_sec * 1000000; - return usec; + ret.ts = time_check_(t.ts, abortstr); + return ret; } -struct timeval time_from_msec(uint64_t msec) +struct timeabs timeabs_check(struct timeabs t, const char *abortstr) { - struct timeval t; + struct timeabs ret; - t.tv_usec = (msec % 1000) * 1000; - t.tv_sec = msec / 1000; - return t; + ret.ts = time_check_(t.ts, abortstr); + return ret; } -struct timeval time_from_usec(uint64_t usec) +struct timemono timemono_check(struct timemono t, const char *abortstr) { - struct timeval t; + struct timemono ret; - t.tv_usec = usec % 1000000; - t.tv_sec = usec / 1000000; - return t; + ret.ts = time_check_(t.ts, abortstr); + return ret; }