public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86: fix cpufreq tsc scaling
@ 2009-05-26 22:48 Peter Zijlstra
  2009-05-26 22:52 ` Thomas Gleixner
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Zijlstra @ 2009-05-26 22:48 UTC (permalink / raw)
  To: Ingo Molnar, Thomas Gleixner, H. Peter Anvin; +Cc: lkml, john stultz

Subject: x86: fix cpufreq tsc scaling
From: Peter Zijlstra <a.p.zijlstra@chello.nl>
Date: Wed May 27 00:32:23 CEST 2009

For freqency dependent TSCs we only scale the cycles, we do not account
for the discrepancy in absolute value. Add a constant factor so that:

  c1 + f1*cyc == c2 + f2*cyc

LKML-Reference: <new-submission>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Chucked-on-by: Thomas Gleixner <tglx@linutronix.de>
---
 arch/x86/include/asm/timer.h |    6 +++++-
 arch/x86/kernel/tsc.c        |    8 ++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)

Index: linux-2.6/arch/x86/include/asm/timer.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/timer.h
+++ linux-2.6/arch/x86/include/asm/timer.h
@@ -45,12 +45,16 @@ extern int no_timer_check;
  */
 
 DECLARE_PER_CPU(unsigned long, cyc2ns);
+DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
 
 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
 
 static inline unsigned long long __cycles_2_ns(unsigned long long cyc)
 {
-	return cyc * per_cpu(cyc2ns, smp_processor_id()) >> CYC2NS_SCALE_FACTOR;
+	int cpu = smp_processor_id();
+	unsigned long long ns = per_cpu(cyc2ns_offset, cpu);
+	ns += cyc * per_cpu(cyc2ns, cpu) >> CYC2NS_SCALE_FACTOR;
+	return ns;
 }
 
 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
Index: linux-2.6/arch/x86/kernel/tsc.c
===================================================================
--- linux-2.6.orig/arch/x86/kernel/tsc.c
+++ linux-2.6/arch/x86/kernel/tsc.c
@@ -589,22 +589,26 @@ EXPORT_SYMBOL(recalibrate_cpu_khz);
  */
 
 DEFINE_PER_CPU(unsigned long, cyc2ns);
+DEFINE_PER_CPU(unsigned long long, cyc2ns_offset);
 
 static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
 {
-	unsigned long long tsc_now, ns_now;
+	unsigned long long tsc_now, ns_now, *offset;
 	unsigned long flags, *scale;
 
 	local_irq_save(flags);
 	sched_clock_idle_sleep_event();
 
 	scale = &per_cpu(cyc2ns, cpu);
+	offset = &per_cpu(cyc2ns_offset, cpu);
 
 	rdtscll(tsc_now);
 	ns_now = __cycles_2_ns(tsc_now);
 
-	if (cpu_khz)
+	if (cpu_khz) {
 		*scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
+		*offset = ns_now - (tsc_now * *scale >> CYC2NS_SCALE_FACTOR);
+	}
 
 	sched_clock_idle_wakeup_event(0);
 	local_irq_restore(flags);



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

* Re: [PATCH] x86: fix cpufreq tsc scaling
  2009-05-26 22:48 [PATCH] x86: fix cpufreq tsc scaling Peter Zijlstra
@ 2009-05-26 22:52 ` Thomas Gleixner
  2009-05-27  7:00   ` Peter Zijlstra
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Gleixner @ 2009-05-26 22:52 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Ingo Molnar, H. Peter Anvin, lkml, john stultz

On Wed, 27 May 2009, Peter Zijlstra wrote:
> Subject: x86: fix cpufreq tsc scaling
> From: Peter Zijlstra <a.p.zijlstra@chello.nl>
> Date: Wed May 27 00:32:23 CEST 2009
> 
> For freqency dependent TSCs we only scale the cycles, we do not account
> for the discrepancy in absolute value. Add a constant factor so that:
> 
>   c1 + f1*cyc == c2 + f2*cyc
> 
> LKML-Reference: <new-submission>
> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
> Chucked-on-by: Thomas Gleixner <tglx@linutronix.de>

Hey, you misspelled this. I said "Chuckled-on-by"

     tglx

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

* Re: [PATCH] x86: fix cpufreq tsc scaling
  2009-05-26 22:52 ` Thomas Gleixner
@ 2009-05-27  7:00   ` Peter Zijlstra
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2009-05-27  7:00 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: Ingo Molnar, H. Peter Anvin, lkml, john stultz

On Wed, 2009-05-27 at 00:52 +0200, Thomas Gleixner wrote:
> On Wed, 27 May 2009, Peter Zijlstra wrote:
> > Subject: x86: fix cpufreq tsc scaling
> > From: Peter Zijlstra <a.p.zijlstra@chello.nl>
> > Date: Wed May 27 00:32:23 CEST 2009
> > 
> > For freqency dependent TSCs we only scale the cycles, we do not account
> > for the discrepancy in absolute value. Add a constant factor so that:
> > 
> >   c1 + f1*cyc == c2 + f2*cyc
> > 
> > LKML-Reference: <new-submission>
> > Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
> > Chucked-on-by: Thomas Gleixner <tglx@linutronix.de>
> 
> Hey, you misspelled this. I said "Chuckled-on-by"

There is no hiding from your true feelings on the subject ;-)

But yeah, it appears I'm dyslectic after midnight..

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

end of thread, other threads:[~2009-05-27  7:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-26 22:48 [PATCH] x86: fix cpufreq tsc scaling Peter Zijlstra
2009-05-26 22:52 ` Thomas Gleixner
2009-05-27  7:00   ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox