Skip to contentSkip to conversation

Patches to reduce delay in arch/kernel/time.c

RFC Nigel Cunningham 2 participants 7 messages Jan 8, 2005

lore
Share & tools
lore
#0 lore raw

Hi all.

Over the past couple of months there's been a fair bit of discussion regarding clock drift after suspending, big pauses during suspending and resuming and so on. I believe this set of three patches, applied on top of David's patch (now in Linus' tree) may address the remaining issues. Comments please!

Patch 1: Replace multiple calls to get_cmos_time in both suspend and resume routines with a single call. Since get_cmos_time waits for the start of the next second before returning the result, This reduces the delay for suspending and resuming by one second in each case.
Patch 2: Make sleep start an unsigned long. This appears to address the occasional 1hr 10min error seen by myself and John
(http://lkml.org/lkml/2004/12/15/290). Better solution? Patch 3: Implement a new __get_cmos_time() function which doesn't delay until the start of the new second before returning. Use it in suspending (feel free to correct me, but I don't think having the
exact start of the second is necessary here).

Regards,

Nigel

Signature · 5 lines
Nigel Cunningham
Software Engineer, Canberra, Australia
http://www.cyclades.com

Ph: +61 (2) 6292 8028      Mob: +61 (417) 100 574
#1 lore raw

Patch 3/3: Reduce number of get_cmos_time_calls.

Create new __get_cmos_time patch, which doesn't wait for the start of a new second before returning. Adjust timer_suspend to use this as we don't appear to need the exact start of a second when suspending.

arch/i386/kernel/time.c +1 -1
diff -ruNp 913-old/arch/i386/kernel/time.c 913-new/arch/i386/kernel/time.c
@@ -327,7 +327,7 @@ static int timer_suspend(struct sys_devi
 	/*
 	 * Estimate time zone so that set_time can update the clock
 	 */
-	long cmos_time = get_cmos_time();
+	long cmos_time = __get_cmos_time();
 	
 	clock_cmos_diff = -cmos_time;
 	clock_cmos_diff += get_seconds();
arch/x86_64/kernel/time.c +50 -34

108 lines not on this page · raw message

include/asm-i386/mach-default/mach_time.h +31 -15

65 lines not on this page · raw message

#2 lore raw

Re: Patch 3/3: Reduce number of get_cmos_time_calls.

Hi!

Quoted 3 lines

Create new __get_cmos_time patch, which doesn't wait for the start of a new second before returning. Adjust timer_suspend to use this as we don't appear to need the exact start of a second when suspending.

Basically nice cleanup. I do not know if this does not mean up-to second error in clock for each suspend/resume?

arch/x86_64/kernel/time.c unsigned long long sched_clock(void) · line 503

Quoted 8 lines
arch/x86_64/kernel/time.c +2 -0
@@ -499,11 +499,56 @@ unsigned long long sched_clock(void)
 	return cycles_2_ns(a);
 }
 
+unsigned long __get_cmos_time(void)
+{

Missing static?

arch/x86_64/kernel/time.c unsigned long long sched_clock(void) · line 514

Quoted 10 lines
arch/x86_64/kernel/time.c +10 -0
+
+	/*
+	 * Do we need the spinlock in here too?
+	 *
+	 * If we're called directly (not via get_cmos_time),
+	 * we're in the middle of a sysdev suspend/resume
+	 * and interrupts are disabled, so this 
+	 * should be safe without any locking.
+	 * 				-- NC
+	 */

I'd say "Caller is responsible for locking"... and explain this in caller. Also do not sign comments.

Quoted 10 lines
arch/x86_64/kernel/time.c +10 -0
+	do {
+		sec = CMOS_READ(RTC_SECONDS);
+		min = CMOS_READ(RTC_MINUTES);
+		hour = CMOS_READ(RTC_HOURS);
+		day = CMOS_READ(RTC_DAY_OF_MONTH);
+		mon = CMOS_READ(RTC_MONTH);
+		year = CMOS_READ(RTC_YEAR);
+	} while (sec != CMOS_READ(RTC_SECONDS));
+
+	/*

arch/x86_64/kernel/time.c unsigned long long sched_clock(void) · line 535

Quoted 10 lines
arch/x86_64/kernel/time.c +10 -0
+	 * We know that x86-64 always uses BCD format, no need to check the config
+	 * register.
+	 */
+
+	    BCD_TO_BIN(sec);
+	    BCD_TO_BIN(min);
+	    BCD_TO_BIN(hour);
+	    BCD_TO_BIN(day);
+	    BCD_TO_BIN(mon);
+	    BCD_TO_BIN(year);

Whitespace damage?
Pavel

Signature · 2 lines
People were complaining that M$ turns users into beta-testers...
...jr ghea gurz vagb qrirybcref, naq gurl frrz gb yvxr vg gung jnl!
#3 lore raw

Patch 2/3: Reduce number of get_cmos_time_calls.

In reply to Nigel Cunningham

Change sleep_start from signed to unsigned long. This appears to address an issue with the clock being occasionally off by around 1 hr 10 minutes.

arch/i386/kernel/time.c +2 -1
diff -ruNp 912-old/arch/i386/kernel/time.c 912-new/arch/i386/kernel/time.c
@@ -319,7 +319,8 @@ unsigned long get_cmos_time(void)
 	return retval;
 }
 
-static long clock_cmos_diff, sleep_start;
+static long clock_cmos_diff;
+static unsigned long sleep_start;
 
 static int timer_suspend(struct sys_device *dev, u32 state)
 {
#4 lore raw

Re: Patch 2/3: Reduce number of get_cmos_time_calls.

Hi!

Quoted 3 lines

Change sleep_start from signed to unsigned long. This appears to address an issue with the clock being occasionally off by around 1 hr 10 minutes.

sleep_start is in seconds, right? I do not see how it could explain 1hr10 difference... Please do not apply this.

OTOH try this code (in userspace, run as root). It did strange thigs to my time, and it is unrelated to swsusp. (But it does similar weird things with interrupts).

pavel@amd:~$ cat misc/latency.c
void
main(void)
{
        int i;
        iopl(3);
        while (1) {
                asm volatile("cli");
                //              for (i=0; i<20000000; i++)
                for (i=0; i<1000000000; i++)
                        asm volatile("");
                asm volatile("sti");
                sleep(1);
        }
}
pavel@amd:~$

Pavel

Signature · 2 lines
People were complaining that M$ turns users into beta-testers...
...jr ghea gurz vagb qrirybcref, naq gurl frrz gb yvxr vg gung jnl!
#5 lore raw

Patch 1/3: Reduce number of get_cmos_time_calls.

In reply to Nigel Cunningham

Reduce the number of calls to get_cmos_time. Since get_cmos_time waits for the start of a new second, two consecutive calls add one full second to the time to suspend/resume.

arch/i386/kernel/time.c +8 -4
diff -ruNp 911-old/arch/i386/kernel/time.c 911-new/arch/i386/kernel/time.c
@@ -326,9 +326,11 @@ static int timer_suspend(struct sys_devi
 	/*
 	 * Estimate time zone so that set_time can update the clock
 	 */
-	clock_cmos_diff = -get_cmos_time();
+	long cmos_time = get_cmos_time();
+	
+	clock_cmos_diff = -cmos_time;
 	clock_cmos_diff += get_seconds();
-	sleep_start = get_cmos_time();
+	sleep_start = cmos_time;
 	return 0;
 }
 
@@ -337,13 +339,15 @@ static int timer_resume(struct sys_devic
 	unsigned long flags;
 	unsigned long sec;
 	unsigned long sleep_length;
+	unsigned long cmos_time;
 
 #ifdef CONFIG_HPET_TIMER
 	if (is_hpet_enabled())
 		hpet_reenable();
 #endif
-	sec = get_cmos_time() + clock_cmos_diff;
-	sleep_length = (get_cmos_time() - sleep_start) * HZ;
+	cmos_time = get_cmos_time();
+	sec = cmos_time + clock_cmos_diff;
+	sleep_length = (cmos_time - sleep_start) * HZ;
 	write_seqlock_irqsave(&xtime_lock, flags);
 	xtime.tv_sec = sec;
 	xtime.tv_nsec = 0;
#6 lore raw

Re: Patch 1/3: Reduce number of get_cmos_time_calls.

Hi!

Quoted 3 lines

Reduce the number of calls to get_cmos_time. Since get_cmos_time waits for the start of a new second, two consecutive calls add one full second to the time to suspend/resume.

Ack. (You probably want to send it to akpm...?). I did not realize that get_cmos_time() is so costly.
Pavel

Signature · 2 lines
People were complaining that M$ turns users into beta-testers...
...jr ghea gurz vagb qrirybcref, naq gurl frrz gb yvxr vg gung jnl!