Commit 84599f8a authored by Peter Zijlstra's avatar Peter Zijlstra Committed by Ingo Molnar

sched, x86: Fix cpufreq + sched_clock() TSC scaling

For freqency dependent TSCs we only scale the cycles, we do not account
for the discrepancy in absolute value.

Our current formula is: time = cycles * mult

(where mult is a function of the cpu-speed on variable tsc machines)

Suppose our current cycle count is 10, and we have a multiplier of 5,
then our time value would end up being 50.

Now cpufreq comes along and changes the multiplier to say 3 or 7,
which would result in our time being resp. 30 or 70.

That means that we can observe random jumps in the time value due to
frequency changes in both fwd and bwd direction.

So what this patch does is change the formula to:

  time = cycles * frequency + offset

And we calculate offset so that time_before == time_after, thereby
ridding us of these jumps in time.

[ Impact: fix/reduce sched_clock() jumps across frequency changing events ]
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <new-submission>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
Chucked-on-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 03347e25
...@@ -45,12 +45,16 @@ extern int no_timer_check; ...@@ -45,12 +45,16 @@ extern int no_timer_check;
*/ */
DECLARE_PER_CPU(unsigned long, cyc2ns); DECLARE_PER_CPU(unsigned long, cyc2ns);
DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);
#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */ #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
static inline unsigned long long __cycles_2_ns(unsigned long long cyc) 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) static inline unsigned long long cycles_2_ns(unsigned long long cyc)
......
...@@ -589,22 +589,26 @@ EXPORT_SYMBOL(recalibrate_cpu_khz); ...@@ -589,22 +589,26 @@ EXPORT_SYMBOL(recalibrate_cpu_khz);
*/ */
DEFINE_PER_CPU(unsigned long, cyc2ns); 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) 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; unsigned long flags, *scale;
local_irq_save(flags); local_irq_save(flags);
sched_clock_idle_sleep_event(); sched_clock_idle_sleep_event();
scale = &per_cpu(cyc2ns, cpu); scale = &per_cpu(cyc2ns, cpu);
offset = &per_cpu(cyc2ns_offset, cpu);
rdtscll(tsc_now); rdtscll(tsc_now);
ns_now = __cycles_2_ns(tsc_now); ns_now = __cycles_2_ns(tsc_now);
if (cpu_khz) if (cpu_khz) {
*scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/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); sched_clock_idle_wakeup_event(0);
local_irq_restore(flags); local_irq_restore(flags);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment