-/**
- * Takes the difference between two timespec values. end is assumed to
- * occur after start.
- */
-inline uint64_t timespecDiff(timespec end, timespec start) {
- if (end.tv_sec == start.tv_sec) {
- assert(end.tv_nsec >= start.tv_nsec);
- return end.tv_nsec - start.tv_nsec;
- }
- assert(end.tv_sec > start.tv_sec &&
- (uint64_t)(end.tv_sec - start.tv_sec) <
- std::numeric_limits<uint64_t>::max() / 1000000000UL);
- return (end.tv_sec - start.tv_sec) * 1000000000UL
- + end.tv_nsec - start.tv_nsec;
-}
-
-/**
- * Takes the difference between two sets of timespec values. The first
- * two come from a high-resolution clock whereas the other two come
- * from a low-resolution clock. The crux of the matter is that
- * high-res values may be bogus as documented in
- * http://linux.die.net/man/3/clock_gettime. The trouble is when the
- * running process migrates from one CPU to another, which is more
- * likely for long-running processes. Therefore we watch for high
- * differences between the two timings.
- *
- * This function is subject to further improvements.
- */
-inline uint64_t timespecDiff(timespec end, timespec start,
- timespec endCoarse, timespec startCoarse) {
- auto fine = timespecDiff(end, start);
- auto coarse = timespecDiff(endCoarse, startCoarse);
- if (coarse - fine >= 1000000) {
- // The fine time is in all likelihood bogus
- return coarse;
- }
- return fine;
-}
-