The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Andi Kleen <ak@suse.de>
To: "Guillaume Chazarain" <guichaz@gmail.com>
Cc: "Ingo Molnar" <mingo@elte.hu>, linux-kernel@vger.kernel.org
Subject: Re: [1/3] Bugfix: Don't use the TSC in sched_clock if unstable
Date: Mon, 5 Mar 2007 00:28:24 +0100	[thread overview]
Message-ID: <200703050028.25209.ak@suse.de> (raw)
In-Reply-To: <3d8471ca0703041033v689b51dfuba0b09fc979d7698@mail.gmail.com>

On Sunday 04 March 2007 19:33, Guillaume Chazarain wrote:
> 2007/3/4, Andi Kleen <ak@suse.de>:
> 
> > On what hardware?
> 
> Pentium M 798 MHz -> 2GHz

An older one (Banias) I assume? (please post /proc/cpuinfo) 

Assuming that:

> > And how many frequency transitions do you have per second?
> 
> 10 in a kernel compile exhibiting audio skips.
> 
> Anyway, the "Clocksource tsc unstable (delta = -263211549 ns)" line

That's because the new clocksource code doesn't correctly update
its state on cpufrequency scaling. That's another problem, but independent
of this. sched_clock doesn't rely on the clock source state for this,
but uses its own conversion functions.

> in the dmesg I attached to the previous mail confirms IMHO that my
> TSC is too unstable for scheduling purpose.

No it shouldn't be.  Must be a bug somewhere.

Your CPU doesn't have p state invariant TSC, but we should be able 
to generate nano seconds from TSC using the known frequency at any point 
of time using the frequency updated in arch/i386/kernel/tsc.c:time_cpufreq_notifier(). 

Can you run with this debug patch and send me the output? If it stops
logging before you see the problem increase MAX in the patch.

-Andi

Index: linux/arch/i386/kernel/tsc.c
===================================================================
--- linux.orig/arch/i386/kernel/tsc.c
+++ linux/arch/i386/kernel/tsc.c
@@ -85,9 +85,20 @@ static unsigned long cyc2ns_scale __read
 
 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
 
+enum {
+	MAX = 500,
+}; 
+
+static int cnt;
+
 static inline void set_cyc2ns_scale(unsigned long cpu_khz)
 {
-	cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
+	long new = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
+	if (cnt < MAX) {
+		printk("cyc2ns_scale %lx -> %lx\n", cyc2ns_scale, new); 
+		cnt++;
+	}
+	cyc2ns_scale = new;
 }
 
 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
@@ -101,6 +112,8 @@ static inline unsigned long long cycles_
 unsigned long long sched_clock(void)
 {
 	unsigned long long this_offset;
+	static u64 last_offset;
+	static long last_conv;
 
 	if (unlikely(custom_sched_clock))
 		return (*custom_sched_clock)();
@@ -116,7 +129,17 @@ unsigned long long sched_clock(void)
 	rdtscll(this_offset);
 
 	/* return the value in ns */
-	return cycles_2_ns(this_offset);
+	this_offset = cycles_2_ns(this_offset);
+
+	if (cnt < MAX && this_offset <= last_offset) {
+		printk("sched_clock backward %llx(%lx)->%llx(%lx)\n",
+			last_offset, last_conv, this_offset, cyc2ns_scale);
+		cnt++;
+	} 
+	last_offset = this_offset;
+	last_conv = cyc2ns_scale;
+
+	return this_offset;
 }
 
 static unsigned long calculate_cpu_khz(void)


  reply	other threads:[~2007-03-04 23:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-03 21:41 [1/3] Bugfix: Don't use the TSC in sched_clock if unstable Guillaume Chazarain
2007-03-04 14:37 ` Andi Kleen
     [not found]   ` <3d8471ca0703040741h4eff73a3wd67d648d52497e34@mail.gmail.com>
2007-03-04 17:25     ` Andi Kleen
2007-03-04 18:33       ` Guillaume Chazarain
2007-03-04 23:28         ` Andi Kleen [this message]
2007-03-05  0:15           ` Guillaume Chazarain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200703050028.25209.ak@suse.de \
    --to=ak@suse.de \
    --cc=guichaz@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox