Skip to contentSkip to conversation

x86: fix cpufreq tsc scaling

PATCH In review Peter Zijlstra 2 participants 3 messages May 26, 2009 – May 27, 2009 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: fix cpufreq tsc scaling

Subject: x86: fix cpufreq tsc scaling
From: Peter Zijlstra <[email protected]>
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>

---
 arch/x86/include/asm/timer.h |    6 +++++-
 arch/x86/kernel/tsc.c        |    8 ++++++--
 2 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/timer.h +5 -1
Index: 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)
arch/x86/kernel/tsc.c +6 -2
Index: 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);
#1 lore raw
Quoted 4 lines · Peter Zijlstra

Subject: x86: fix cpufreq tsc scaling
From: Peter Zijlstra <[email protected]>
Date: Wed May 27 00:32:23 CEST 2009

Quoted 8 lines

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>

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

tglx

#2 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · Peter Zijlstra

Subject: x86: fix cpufreq tsc scaling
From: Peter Zijlstra <[email protected]>
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:

Quoted 7 lines
Quoted 5 lines

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

LKML-Reference: <new-submission>

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..