linux-rt-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Round the calculated scale factor in set_cyc2ns_scale()
@ 2012-12-05 14:16 Bernd Faust
  2012-12-19 18:28 ` John Stultz
  0 siblings, 1 reply; 2+ messages in thread
From: Bernd Faust @ 2012-12-05 14:16 UTC (permalink / raw)
  To: berndfaust, johnstul, linux-kernel, linux-rt-users

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.

Signed-off-by: Bernd Faust <berndfaust@gmail.com>
---
 arch/x86/kernel/tsc.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index cfa5d4f..8ed0857 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -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));
 	}
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Round the calculated scale factor in set_cyc2ns_scale()
  2012-12-05 14:16 [PATCH] Round the calculated scale factor in set_cyc2ns_scale() Bernd Faust
@ 2012-12-19 18:28 ` John Stultz
  0 siblings, 0 replies; 2+ messages in thread
From: John Stultz @ 2012-12-19 18:28 UTC (permalink / raw)
  To: Bernd Faust; +Cc: linux-kernel, linux-rt-users

On 12/05/2012 06:16 AM, Bernd Faust wrote:
> 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.
>
> Signed-off-by: Bernd Faust <berndfaust@gmail.com>

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-12-19 18:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-05 14:16 [PATCH] Round the calculated scale factor in set_cyc2ns_scale() Bernd Faust
2012-12-19 18:28 ` John Stultz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).