[PATCH] Fine tune the time conversion to eliminate conversion errors.

From: George Anzinger
Date: Tue Jan 27 2004 - 04:33:11 EST


Andrew,

The time conversion code is erroring on the side of a bit too small. The attached patch forces any error to be on the high side. The current code will convert 1 nanosecond to zero jiffies (standard says that should be 1). It also is around 1 nanosecond late on each roll to the next jiffie.

I have done some error checks with this patch applied and get the following errors in PPB ( Parts Per Billion):

HZ nano sec conversion microsecond conversion
1000 315 45
1024 227 40
100 28 317

In all cases the error is on the high side, which means that the final shift will, most likely, eliminate the error bits.

--
George Anzinger george@xxxxxxxxxx
High-res-timers: http://sourceforge.net/projects/high-res-timers/
Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml
--- linux-2.6.1-org/include/linux/time.h 2003-08-11 13:48:55.000000000 -0700
+++ linux/include/linux/time.h 2004-01-27 00:29:12.000000000 -0800
@@ -148,14 +148,14 @@
#endif
#define NSEC_JIFFIE_SC (SEC_JIFFIE_SC + 29)
#define USEC_JIFFIE_SC (SEC_JIFFIE_SC + 19)
-#define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC))\
- / (u64)TICK_NSEC))
+#define SEC_CONVERSION ((unsigned long)((((u64)NSEC_PER_SEC << SEC_JIFFIE_SC) +\
+ TICK_NSEC -1) / (u64)TICK_NSEC))

-#define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC))\
- / (u64)TICK_NSEC))
+#define NSEC_CONVERSION ((unsigned long)((((u64)1 << NSEC_JIFFIE_SC) +\
+ TICK_NSEC -1) / (u64)TICK_NSEC))
#define USEC_CONVERSION \
- ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC)) \
- / (u64)TICK_NSEC))
+ ((unsigned long)((((u64)NSEC_PER_USEC << USEC_JIFFIE_SC) +\
+ TICK_NSEC -1) / (u64)TICK_NSEC))
/*
* USEC_ROUND is used in the timeval to jiffie conversion. See there
* for more details. It is the scaled resolution rounding value. Note