public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* x86: tsc: make TSC calibration more immune to interrupts
@ 2011-04-20 18:52 Kasper Pedersen
  2011-04-20 19:15 ` john stultz
  0 siblings, 1 reply; 11+ messages in thread
From: Kasper Pedersen @ 2011-04-20 18:52 UTC (permalink / raw)
  To: linux-kernel
  Cc: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86, John Stultz,
	Peter Zijlstra, Suresh Siddha, Kasper Pedersen

When a SMI or plain interrupt occurs during the delayed part
of TSC calibration, and the SMI/irq handler is good and fast
so that is does not exceed SMI_TRESHOLD, tsc_khz can be a bit
off (10-30ppm).

We should not depend on interrupts being longer than 50000
clocks, so always do the 5 tries, and use the best sample we
get.
This should work always for any four periodic or rate-limited
interrupt sources. If we get 5 interrupts with 500ns gaps in
a row, behaviour should be as without this patch.

This costs us 20-100 microseconds in startup time, as
tsc_read_refs is called 8 times.

measurements:
On a 700MHz P3 I see t2-t1=~22000, and 31ppm error.
A Core2 is similar: http://n1.taur.dk/tscdeviat.png
(while mostly t2-t1=~1000, in about 1 of 3000 tests
I see t2-t1=~20000 for both machines.)
vmware ESX4 has t2-t1=~8000 and up.

Signed-off-by: Kasper Pedersen <kernel@kasperkp.dk>
---
 arch/x86/kernel/tsc.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index ffe5755..e916f99 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -117,7 +117,7 @@ static int __init tsc_setup(char *str)
 
 __setup("tsc=", tsc_setup);
 
-#define MAX_RETRIES     5
+#define BESTOF_SAMPLES      5
 #define SMI_TRESHOLD    50000
 
 /*
@@ -125,19 +125,29 @@ __setup("tsc=", tsc_setup);
  */
 static u64 tsc_read_refs(u64 *p, int hpet)
 {
-	u64 t1, t2;
+	u64 t1, t2, tp, best_uncertainty, uncertainty, best_t2;
 	int i;
 
-	for (i = 0; i < MAX_RETRIES; i++) {
+	best_uncertainty = SMI_TRESHOLD;
+	best_t2 = 0;
+	for (i = 0; i < BESTOF_SAMPLES; i++) {
 		t1 = get_cycles();
 		if (hpet)
-			*p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
+			tp = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
 		else
-			*p = acpi_pm_read_early();
+			tp = acpi_pm_read_early();
 		t2 = get_cycles();
-		if ((t2 - t1) < SMI_TRESHOLD)
-			return t2;
+		uncertainty = t2 - t1;
+		if (uncertainty < best_uncertainty) {
+			best_uncertainty = uncertainty;
+			best_t2 = t2;
+			*p = tp;
+		}
 	}
+	if (best_uncertainty < SMI_TRESHOLD)
+		return best_t2;
+
+	*p = tp;
 	return ULLONG_MAX;
 }
 


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

end of thread, other threads:[~2011-04-23  1:38 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-20 18:52 x86: tsc: make TSC calibration more immune to interrupts Kasper Pedersen
2011-04-20 19:15 ` john stultz
2011-04-20 19:44   ` Kasper Pedersen
2011-04-20 20:28     ` john stultz
2011-04-20 21:22       ` x86: tsc: v2 " Kasper Pedersen
2011-04-20 22:39         ` Josh Triplett
2011-04-21  2:19           ` john stultz
2011-04-21  4:32             ` Josh Triplett
2011-04-21 19:46           ` Kasper Pedersen
2011-04-23  1:38             ` john stultz
2011-04-21 19:52           ` x86: tsc: v3 " Kasper Pedersen

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