linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roman Zippel <zippel@linux-m68k.org>
To: john stultz <johnstul@us.ibm.com>
Cc: Valdis.Kletnieks@vt.edu, Andrew Morton <akpm@osdl.org>,
	linux-kernel@vger.kernel.org, Uwe Bugla <uwe.bugla@gmx.de>,
	James Bottomley <James.Bottomley@SteelEye.com>
Subject: [PATCH] adjust clock for lost ticks
Date: Sat, 8 Jul 2006 22:02:31 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.64.0607082151400.12900@scrub.home> (raw)
In-Reply-To: <1152223506.24656.173.camel@cog.beaverton.ibm.com>


A large number of lost ticks can cause an overadjustment of the clock.
To compensate for this we look at the current error and the larger the
error already is the more careful we are at adjusting the error.
As small extra fix reset the error when the clock is set.

Signed-off-by: Roman Zippel <zippel@linux-m68k.org>

---
 kernel/timer.c |   85 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 47 insertions(+), 38 deletions(-)

Index: linux-2.6-mm/kernel/timer.c
===================================================================
--- linux-2.6-mm.orig/kernel/timer.c	2006-07-07 02:51:57.000000000 +0200
+++ linux-2.6-mm/kernel/timer.c	2006-07-08 19:10:52.000000000 +0200
@@ -891,6 +891,7 @@ int do_settimeofday(struct timespec *tv)
 	set_normalized_timespec(&xtime, sec, nsec);
 	set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
 
+	clock->error = 0;
 	ntp_clear();
 
 	write_sequnlock_irqrestore(&xtime_lock, flags);
@@ -1008,52 +1009,52 @@ static int __init timekeeping_init_devic
 device_initcall(timekeeping_init_device);
 
 /*
- * If the error is already larger, we look ahead another tick,
+ * If the error is already larger, we look ahead even further
  * to compensate for late or lost adjustments.
  */
-static __always_inline int clocksource_bigadjust(int sign, s64 error, s64 *interval, s64 *offset)
+static __always_inline int clocksource_bigadjust(s64 error, s64 *interval, s64 *offset)
 {
-	int adj;
+	s64 tick_error, i;
+	u32 look_ahead, adj;
+	s32 error2, mult;
 
 	/*
-	 * As soon as the machine is synchronized to the external time
-	 * source this should be the common case.
+	 * Use the current error value to determine how much to look ahead.
+	 * The larger the error the slower we adjust for it to avoid problems
+	 * with losing too many ticks, otherwise we would overadjust and
+	 * produce an even larger error.  The smaller the adjustment the
+	 * faster we try to adjust for it, as lost ticks can do less harm
+	 * here.  This is tuned so that an error of about 1 msec is adusted
+	 * within about 1 sec (or 2^20 nsec in 2^SHIFT_HZ ticks).
 	 */
-	error >>= 2;
-	if (likely(sign > 0 ? error <= *interval : error >= *interval))
-		return sign;
+	error2 = clock->error >> (TICK_LENGTH_SHIFT + 22 - 2 * SHIFT_HZ);
+	error2 = abs(error2);
+	for (look_ahead = 0; error2 > 0; look_ahead++)
+		error2 >>= 2;
 
 	/*
-	 * An extra look ahead dampens the effect of the current error,
-	 * which can grow quite large with continously late updates, as
-	 * it would dominate the adjustment value and can lead to
-	 * oscillation.
+	 * Now calculate the error in (1 << look_ahead) ticks, but first
+	 * remove the single look ahead already included in the error.
 	 */
-	error += current_tick_length() >> (TICK_LENGTH_SHIFT - clock->shift + 1);
-	error -= clock->xtime_interval >> 1;
-
-	adj = 0;
-	while (1) {
-		error >>= 1;
-		if (sign > 0 ? error <= *interval : error >= *interval)
-			break;
-		adj++;
+	tick_error = current_tick_length() >> (TICK_LENGTH_SHIFT - clock->shift + 1);
+	tick_error -= clock->xtime_interval >> 1;
+	error = ((error - tick_error) >> look_ahead) + tick_error;
+
+	/* Finally calculate the adjustment shift value.  */
+	i = *interval;
+	mult = 1;
+	if (error < 0) {
+		error = -error;
+		*interval = -*interval;
+		*offset = -*offset;
+		mult = -1;
 	}
-
-	/*
-	 * Add the current adjustments to the error and take the offset
-	 * into account, the latter can cause the error to be hardly
-	 * reduced at the next tick. Check the error again if there's
-	 * room for another adjustment, thus further reducing the error
-	 * which otherwise had to be corrected at the next update.
-	 */
-	error = (error << 1) - *interval + *offset;
-	if (sign > 0 ? error > *interval : error < *interval)
-		adj++;
+	for (adj = 0; error > i; adj++)
+		error >>= 1;
 
 	*interval <<= adj;
 	*offset <<= adj;
-	return sign << adj;
+	return mult << adj;
 }
 
 /*
@@ -1068,11 +1069,19 @@ static void clocksource_adjust(struct cl
 
 	error = clock->error >> (TICK_LENGTH_SHIFT - clock->shift - 1);
 	if (error > interval) {
-		adj = clocksource_bigadjust(1, error, &interval, &offset);
+		error >>= 2;
+		if (likely(error <= interval))
+			adj = 1;
+		else
+			adj = clocksource_bigadjust(error, &interval, &offset);
 	} else if (error < -interval) {
-		interval = -interval;
-		offset = -offset;
-		adj = clocksource_bigadjust(-1, error, &interval, &offset);
+		error >>= 2;
+		if (likely(error >= -interval)) {
+			adj = -1;
+			interval = -interval;
+			offset = -offset;
+		} else
+			adj = clocksource_bigadjust(error, &interval, &offset);
 	} else
 		return;
 
@@ -1129,7 +1138,7 @@ static void update_wall_time(void)
 	clocksource_adjust(clock, offset);
 
 	/* store full nanoseconds into xtime */
-	xtime.tv_nsec = clock->xtime_nsec >> clock->shift;
+	xtime.tv_nsec = (s64)clock->xtime_nsec >> clock->shift;
 	clock->xtime_nsec -= (s64)xtime.tv_nsec << clock->shift;
 
 	/* check to see if there is a new clocksource to use */

  parent reply	other threads:[~2006-07-08 20:02 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-24 13:19 2.6.17-mm2 Andrew Morton
2006-06-24 15:53 ` 2.6.17-mm2 Rafael J. Wysocki
2006-06-24 17:20   ` 2.6.17-mm2 Dave Jones
2006-06-24 21:34     ` 2.6.17-mm2 Andrew Morton
2006-06-25  8:51       ` 2.6.17-mm2 Rafael J. Wysocki
2006-06-25 10:22         ` 2.6.17-mm2 Andrew Morton
2006-06-25 15:16           ` 2.6.17-mm2 Andrew Morton
2006-06-25 18:23             ` 2.6.17-mm2 Sam Ravnborg
2006-06-25 18:40               ` 2.6.17-mm2 Andrew Morton
2006-06-25 21:21                 ` 2.6.17-mm2 Sam Ravnborg
2006-06-30  7:38             ` 2.6.17-mm2 Randy.Dunlap
2006-07-02 10:11               ` 2.6.17-mm2 Russell King
2006-07-02 18:42                 ` 2.6.17-mm2 Randy.Dunlap
2006-07-02 18:47                   ` 2.6.17-mm2 Arjan van de Ven
2006-07-02 18:47                   ` 2.6.17-mm2 Sam Ravnborg
2006-07-03  5:50                 ` 2.6.17-mm2 Randy.Dunlap
2006-07-03 13:49                   ` 2.6.17-mm2 Russell King
2006-06-25 19:19           ` 2.6.17-mm2 Rafael J. Wysocki
2006-06-26 20:13           ` 2.6.17-mm2 Chandra Seetharaman
2006-06-24 19:41 ` 2.6.17-mm2 Dominik Karall
2006-06-24 21:43   ` 2.6.17-mm2 Andrew Morton
2006-06-25  6:06 ` 2.6.17-mm2 Reuben Farrelly
2006-06-25  9:37   ` 2.6.17-mm2 Barry K. Nathan
2006-06-25 10:29     ` 2.6.17-mm2 Reuben Farrelly
2006-06-25 11:19 ` 2.6.17-mm2 Michal Piotrowski
2006-06-25 11:40   ` 2.6.17-mm2 Andrew Morton
2006-06-25 12:18     ` 2.6.17-mm2 Michal Piotrowski
2006-06-25 16:25 ` 2.6.17-mm2 (NULL pointer dereference) Dominik Karall
2006-06-25 17:18   ` Andrew Morton
2006-06-25 18:11     ` Dominik Karall
2006-06-25 16:47 ` 2.6.17-mm2: no QLA3YYY_NAPI help text Adrian Bunk
2006-06-25 19:32 ` 2.6.17-mm2: BLK_CPQ_CISS_DA=m error Adrian Bunk
2006-06-26  0:41   ` Vivek Goyal
2006-06-25 23:13 ` [-mm patch] make drivers/scsi/pata_it821x.c:it821x_passthru_dev_select() static Adrian Bunk
2006-06-25 23:27   ` Alan Cox
2006-06-27  1:03   ` Jeff Garzik
2006-06-25 23:13 ` [-mm patch] fs/cifs/cifsproto.h: remove #ifdef around small_smb_init_no_tc() prototype Adrian Bunk
2006-06-26  4:05   ` Steven French
2006-06-26 15:17 ` [-mm patch] drivers/scsi/arcmsr/: cleanups Adrian Bunk
2006-06-26 20:27 ` [-mm patch] drivers/md/raid5.c: remove an unused variable Adrian Bunk
2006-06-26 21:41 ` 2.6.17-mm2 hrtimer code wedges at boot? Valdis.Kletnieks
2006-06-26 22:50   ` Valdis.Kletnieks
2006-06-26 23:02   ` john stultz
2006-06-26 23:27   ` Thomas Gleixner
2006-06-27  2:12     ` Valdis.Kletnieks
2006-06-27  5:54       ` Thomas Gleixner
2006-06-27 10:16   ` Roman Zippel
2006-06-27 16:43     ` Valdis.Kletnieks
2006-06-27 17:10       ` Roman Zippel
2006-06-27 17:23         ` Roman Zippel
2006-06-27 19:07           ` Valdis.Kletnieks
2006-06-28  0:07             ` john stultz
2006-06-28 10:35               ` Roman Zippel
2006-06-28 11:44                 ` Roman Zippel
2006-06-29 23:07                   ` Valdis.Kletnieks
2006-06-30 19:26                     ` john stultz
2006-06-30 21:04                       ` Valdis.Kletnieks
2006-07-03  1:13                         ` Roman Zippel
2006-07-03  1:56                           ` Daniel Walker
2006-07-03  2:20                             ` Valdis.Kletnieks
2006-07-03 20:08                               ` john stultz
2006-07-03 19:59                             ` john stultz
2006-07-04 22:21                               ` Valdis.Kletnieks
2006-07-05  4:29                           ` Valdis.Kletnieks
2006-07-06  0:37                             ` Roman Zippel
2006-07-06  0:56                               ` john stultz
2006-07-06  6:38                               ` Valdis.Kletnieks
2006-07-06  0:51                             ` john stultz
2006-07-06  1:12                               ` john stultz
2006-07-06  5:43                                 ` john stultz
2006-07-06 20:33                               ` Roman Zippel
2006-07-06 22:05                                 ` john stultz
2006-07-07 23:16                                   ` Roman Zippel
2006-07-08 20:02                                   ` Roman Zippel [this message]
2006-07-09 21:25                                     ` [PATCH] adjust clock for lost ticks john stultz
2006-06-28 23:41                 ` 2.6.17-mm2 hrtimer code wedges at boot? john stultz
2006-06-29 11:24                   ` Roman Zippel
2006-06-28 16:54 ` [-mm patch] include/asm-i386/acpi.h should #include <asm/processor.h> Adrian Bunk
2006-06-28 16:54 ` [-mm patch] fix sgivwfb compile Adrian Bunk
2006-06-28 16:54 ` [-mm patch] arch/i386/mach-visws/setup.c: remove dummy function calls Adrian Bunk

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=Pine.LNX.4.64.0607082151400.12900@scrub.home \
    --to=zippel@linux-m68k.org \
    --cc=James.Bottomley@SteelEye.com \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=akpm@osdl.org \
    --cc=johnstul@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=uwe.bugla@gmx.de \
    /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;
as well as URLs for NNTP newsgroup(s).