Skip to contentSkip to conversation

Round the calculated scale factor in set_cyc2ns_scale()

PATCH In review Bernd Faust 2 participants 2 messages Dec 5, 2012 – Dec 19, 2012 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Round the calculated scale factor in set_cyc2ns_scale()

During some experiments with an external clock (in a FPGA), we saw that the TSC clock drifted approx. 2.5ms per second.

This drift was caused by the current way of calculating the scale. In our case cpu_khz had a value of 3292725. This resulted in a scale value of 310. But when doing the calculation by hand it shows that the actual value is 310.9886188491, so a value of 311 would be more precise.

With this change the value is rounded.

---
 arch/x86/kernel/tsc.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/tsc.c +2 -1
index cfa5d4f..8ed0857 100644
@@ -617,7 +617,8 @@ static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
 	ns_now = __cycles_2_ns(tsc_now);
 
 	if (cpu_khz) {
-		*scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
+		*scale = ((NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR) +
+				cpu_khz / 2) / cpu_khz;
 		*offset = ns_now - mult_frac(tsc_now, *scale,
 					     (1UL << CYC2NS_SCALE_FACTOR));
 	}
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 11 lines · Bernd Faust

During some experiments with an external clock (in a FPGA), we saw that the TSC clock drifted approx. 2.5ms per second.

This drift was caused by the current way of calculating the scale. In our case cpu_khz had a value of 3292725. This resulted in a scale value of 310. But when doing the calculation by hand it shows that the actual value is 310.9886188491, so a value of 311 would be more precise.

With this change the value is rounded.

Thanks for sending this in! I've queued it, but because I was slow here, it will have to wait for 3.9.

Sorry for the delay, and thanks again!
-john