Skip to contentSkip to conversation

new timeofday core subsystem (v. A2)

RFC PATCH In review john stultz 8 participants 35 messages Jan 24, 2005 – Feb 2, 2005

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 new timeofday core subsystem (v. A2)

All,
Here is a new release of my time of day proposal, which include ppc64
support as well as suspend/resume and cpufreq hooks. For basic summary of my ideas, you can follow this link: http://lwn.net/Articles/100665/

This patch implements the architecture independent portion of the time
of day subsystem. Included is timeofday.c (which includes all the time of day management and accessor functions), ntp.c (which includes the ntp scaling code, leapsecond processing, and ntp kernel state machine code), timesource.c (for timesource specific management functions), interface definition .h files, the example jiffies timesource (lowest common denominator time source, mainly for use as example code) and minimal hooks into arch independent code.

The patch does not function without minimal architecture specific hooks (i386, x86-64, and ppc64 examples to follow), and it can be applied to a tree without affecting the code.

New in this version:
o TIMESOURCE_CYCLES type
o __iomem labes and proper mmio access
o write_seqlock_irq -> writeseqlock_irqsave
o arch generic interface for for get_cmos_time() equivalents
o suspend/resume hooks for sleep/hibernate (lightly tested)
o timesource adjust_callback hook

I look forward to your comments and feedback.

thanks
-john

linux-2.6.11-rc2_timeofday-core_A2.patch
========================================

drivers/Makefile +1 -0
diff -Nru a/drivers/Makefile b/drivers/Makefile
@@ -63,3 +63,4 @@
 obj-$(CONFIG_INFINIBAND)	+= infiniband/
 obj-y				+= firmware/
 obj-$(CONFIG_CRYPTO)		+= crypto/
+obj-$(CONFIG_NEWTOD)		+= timesource/
drivers/timesource/Makefile new file +1 -0
diff -Nru a/drivers/timesource/Makefile b/drivers/timesource/Makefile
@@ -0,0 +1 @@
+obj-y += jiffies.o
drivers/timesource/jiffies.c new file +45 -0

45 lines not on this page · raw message

include/linux/ntp.h new file +22 -0
diff -Nru a/include/linux/ntp.h b/include/linux/ntp.h
@@ -0,0 +1,22 @@
+/*	linux/include/linux/ntp.h
+ *
+ *	Copyright (C) 2003, 2004 IBM, John Stultz ([email protected])
+ *
+ *	This file contains time of day helper functions
+ */
+
+#ifndef _LINUX_NTP_H
+#define _LINUX_NTP_H
+#include <linux/types.h>
+#include <linux/time.h>
+#include <linux/timex.h>
+
+/* timeofday interfaces */
+nsec_t ntp_scale(nsec_t value);
+void ntp_advance(nsec_t value);
+int ntp_adjtimex(struct timex*);
+int ntp_leapsecond(struct timespec now);
+void ntp_clear(void);
+int get_ntp_status(void);
+
+#endif
include/linux/time.h +4 -0
diff -Nru a/include/linux/time.h b/include/linux/time.h
@@ -27,6 +27,10 @@
 
 #ifdef __KERNEL__
 
+/* timeofday base types */
+typedef u64 nsec_t;
+typedef u64 cycle_t;
+
 /* Parameters used to convert the timespec values */
 #ifndef USEC_PER_SEC
 #define USEC_PER_SEC (1000000L)
include/linux/timeofday.h new file +67 -0

67 lines not on this page · raw message

include/linux/timesource.h new file +101 -0

101 lines not on this page · raw message

init/main.c +2 -0
diff -Nru a/init/main.c b/init/main.c
@@ -46,6 +46,7 @@
 #include <linux/rmap.h>
 #include <linux/mempolicy.h>
 #include <linux/key.h>
+#include <linux/timeofday.h>
 
 #include <asm/io.h>
 #include <asm/bugs.h>
@@ -463,6 +464,7 @@
 	pidhash_init();
 	init_timers();
 	softirq_init();
+	timeofday_init();
 	time_init();
 
 	/*
kernel/Makefile +1 -0
diff -Nru a/kernel/Makefile b/kernel/Makefile
@@ -9,6 +9,7 @@
 	    rcupdate.o intermodule.o extable.o params.o posix-timers.o \
 	    kthread.o wait.o kfifo.o sys_ni.o
 
+obj-$(CONFIG_NEWTOD) += timeofday.o timesource.o ntp.o
 obj-$(CONFIG_FUTEX) += futex.o
 obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
 obj-$(CONFIG_SMP) += cpu.o spinlock.o
kernel/ntp.c new file +554 -0

554 lines not on this page · raw message

kernel/time.c +3 -0
diff -Nru a/kernel/time.c b/kernel/time.c
@@ -37,6 +37,7 @@
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
+#include <linux/timeofday.h>
 
 /* 
  * The timezone where the local system is located.  Used as a default by some
@@ -218,6 +219,7 @@
 /* adjtimex mainly allows reading (and writing, if superuser) of
  * kernel time-keeping variables. used by xntpd.
  */
+#ifndef CONFIG_NEWTOD
 int do_adjtimex(struct timex *txc)
 {
         long ltemp, mtemp, save_adjust;
@@ -400,6 +402,7 @@
 	do_gettimeofday(&txc->time);
 	return(result);
 }
+#endif
 
 asmlinkage long sys_adjtimex(struct timex __user *txc_p)
 {
kernel/timeofday.c new file +436 -0

436 lines not on this page · raw message

kernel/timer.c +4 -0
diff -Nru a/kernel/timer.c b/kernel/timer.c
@@ -575,6 +575,7 @@
 int tickadj = 500/HZ ? : 1;		/* microsecs */
 
 
+#ifndef CONFIG_NEWTOD
 /*
  * phase-lock loop variables
  */
@@ -805,6 +806,9 @@
 		}
 	} while (ticks);
 }
+#else /* CONFIG_NEWTOD */
+#define update_wall_time(x)
+#endif /* CONFIG_NEWTOD */
 
 /*
  * Called from the timer interrupt handler to charge one tick to the current 
kernel/timesource.c new file +71 -0

71 lines not on this page · raw message

#1 lore raw

RFC PATCH 1/1 new timeofday arch specific hooks (v. A2)

All,
This patch implements the minimal architecture specific hooks to enable
the new time of day subsystem code for i386, x86-64, and ppc64. It applies on top of my linux-2.6.11-rc1_timeofday-core_A2 patch and with this patch applied, you can test the new time of day subsystem.

Basically it adds the call to timeofday_interrupt_hook() and cuts alot
of code out of the build via #ifdefs. I know, I know, #ifdefs' are ugly and bad, and the final patch will just remove the old code. For now this allows us to be flexible and easily switch between the two implementations with a single define. Also it makes the patch a bit easier to read.

New in this version:
o more i386 cleanups
o added arch specific read_persistent_clock interface
o ppc64 hooks!

I look forward to your comments and feedback.

thanks
-john

linux-2.6.11-rc2_timeofday-arch_A2.patch
=======================================

arch/i386/Kconfig +4 -0
diff -Nru a/arch/i386/Kconfig b/arch/i386/Kconfig
@@ -14,6 +14,10 @@
 	  486, 586, Pentiums, and various instruction-set-compatible chips by
 	  AMD, Cyrix, and others.
 
+config NEWTOD
+	bool
+	default y
+
 config MMU
 	bool
 	default y
arch/i386/kernel/Makefile +1 -1
diff -Nru a/arch/i386/kernel/Makefile b/arch/i386/kernel/Makefile
@@ -10,7 +10,7 @@
 		doublefault.o quirks.o
 
 obj-y				+= cpu/
-obj-y				+= timers/
+obj-$(!CONFIG_NEWTOD)		+= timers/
 obj-$(CONFIG_ACPI_BOOT)		+= acpi/
 obj-$(CONFIG_X86_BIOS_REBOOT)	+= reboot.o
 obj-$(CONFIG_MCA)		+= mca.o
arch/i386/kernel/apm.c +3 -0
diff -Nru a/arch/i386/kernel/apm.c b/arch/i386/kernel/apm.c
@@ -224,6 +224,7 @@
 #include <linux/smp_lock.h>
 #include <linux/dmi.h>
 #include <linux/suspend.h>
+#include <linux/timeofday.h>
 
 #include <asm/system.h>
 #include <asm/uaccess.h>
@@ -1204,6 +1205,7 @@
 	device_suspend(PMSG_SUSPEND);
 	device_power_down(PMSG_SUSPEND);
 
+	timeofday_suspend_hook();
 	/* serialize with the timer interrupt */
 	write_seqlock_irq(&xtime_lock);
 
@@ -1231,6 +1233,7 @@
 	spin_unlock(&i8253_lock);
 	write_sequnlock_irq(&xtime_lock);
 
+	timeofday_resume_hook();
 	if (err == APM_NO_ERROR)
 		err = APM_SUCCESS;
 	if (err != APM_SUCCESS)
arch/i386/kernel/i8259.c +42 -0

48 lines not on this page · raw message

arch/i386/kernel/time.c +63 -0

127 lines not on this page · raw message

arch/i386/lib/delay.c +19 -0
diff -Nru a/arch/i386/lib/delay.c b/arch/i386/lib/delay.c
@@ -23,10 +23,29 @@
 
 extern struct timer_opts* timer;
 
+#ifndef CONFIG_NEWTOD
 void __delay(unsigned long loops)
 {
 	cur_timer->delay(loops);
 }
+#else
+#include <linux/timeofday.h>
+/* XXX - For now just use a simple loop delay
+ *  This has cpufreq issues, but so did the old method.
+ */
+void __delay(unsigned long loops)
+{
+	int d0;
+	__asm__ __volatile__(
+		"\tjmp 1f\n"
+		".align 16\n"
+		"1:\tjmp 2f\n"
+		".align 16\n"
+		"2:\tdecl %0\n\tjns 2b"
+		:"=&a" (d0)
+		:"0" (loops));
+}
+#endif
 
 inline void __const_udelay(unsigned long xloops)
 {
arch/ppc64/Kconfig +4 -0
diff -Nru a/arch/ppc64/Kconfig b/arch/ppc64/Kconfig
@@ -10,6 +10,10 @@
 	bool
 	default y
 
+config NEWTOD
+	bool
+	default y
+
 config UID16
 	bool
 
arch/ppc64/kernel/sys_ppc32.c +2 -0
diff -Nru a/arch/ppc64/kernel/sys_ppc32.c b/arch/ppc64/kernel/sys_ppc32.c
@@ -322,8 +322,10 @@
 
 	ret = do_adjtimex(&txc);
 
+#ifndef CONFIG_NEWTOD
 	/* adjust the conversion of TB to time of day to track adjtimex */
 	ppc_adjtimex();
+#endif
 
 	if(put_user(txc.modes, &utp->modes) ||
 	   __put_user(txc.offset, &utp->offset) ||
arch/ppc64/kernel/time.c +61 -0

139 lines not on this page · raw message

arch/x86_64/Kconfig +4 -0
diff -Nru a/arch/x86_64/Kconfig b/arch/x86_64/Kconfig
@@ -24,6 +24,10 @@
 	bool
 	default y
 
+config NEWTOD
+	bool
+	default y
+
 config MMU
 	bool
 	default y
arch/x86_64/kernel/time.c +52 -1

127 lines not on this page · raw message

arch/x86_64/kernel/vsyscall.c +5 -1
diff -Nru a/arch/x86_64/kernel/vsyscall.c b/arch/x86_64/kernel/vsyscall.c
@@ -171,8 +171,12 @@
 	BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime));
 	BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)));
 	map_vsyscall();
+/* XXX - disable vsyscall gettimeofday for now */
+#ifndef CONFIG_NEWTOD
 	sysctl_vsyscall = 1; 
-
+#else
+	sysctl_vsyscall = 0;
+#endif
 	return 0;
 }
 
include/asm-generic/div64.h +9 -0
diff -Nru a/include/asm-generic/div64.h b/include/asm-generic/div64.h
@@ -55,4 +55,13 @@
 
 #endif /* BITS_PER_LONG */
 
+#ifndef div_long_long_rem
+#define div_long_long_rem(dividend,divisor,remainder) \
+({							\
+	u64 result = dividend;				\
+	*remainder = do_div(result,divisor);		\
+	result;						\
+})
+#endif
+
 #endif /* _ASM_GENERIC_DIV64_H */
include/asm-i386/timer.h +8 -8
diff -Nru a/include/asm-i386/timer.h b/include/asm-i386/timer.h
@@ -2,6 +2,13 @@
 #define _ASMi386_TIMER_H
 #include <linux/init.h>
 
+#define TICK_SIZE (tick_nsec / 1000)
+void setup_pit_timer(void);
+/* Modifiers for buggy PIT handling */
+extern int pit_latch_buggy;
+extern int timer_ack;
+
+#ifndef CONFIG_NEWTOD
 /**
  * struct timer_ops - used to define a timer source
  *
@@ -29,18 +36,10 @@
 	struct timer_opts *opts;
 };
 
-#define TICK_SIZE (tick_nsec / 1000)
-
 extern struct timer_opts* __init select_timer(void);
 extern void clock_fallback(void);
-void setup_pit_timer(void);
-
-/* Modifiers for buggy PIT handling */
-
-extern int pit_latch_buggy;
 
 extern struct timer_opts *cur_timer;
-extern int timer_ack;
 
 /* list of externed timers */
 extern struct timer_opts timer_none;
@@ -60,5 +59,6 @@
 
 #ifdef CONFIG_X86_PM_TIMER
 extern struct init_timer_opts timer_pmtmr_init;
+#endif
 #endif
 #endif
#2 lore raw

RFC PATCH 1/1 new timeofday arch specific timesources (v. A2)

All,
This patch implements most of the time sources for i386, x86-64, and
ppc64 (tsc, pit, cyclone, acpi-pm, hpet and timebase). It applies on top of my linux-2.6.11-rc2_timeofday-arch_A2 patch. It provides real timesources (opposed to the example jiffies timesource) that can be used for more realistic testing.

This patch is the shabbiest of the three. It needs to be broken up, and
cleaned. The i386_pit.c is broken. Also, acpi_pm and hpet need to be made generic so they can be shared between i386 and x86-64. But for now it will get you going so you can test and play with the core code.

New in this release:
o ppc64_timebase code
o move tsc code to TIMESOURCE_CYCLES
o add tsc_update_callback for cpufreq changes (untested)
o move hpet code to TIMESOURCE_MMIO_32

thanks
-john

linux-2.6.11-rc2_timeofday-timesources_A2.patch
===================================================

arch/i386/kernel/Makefile +1 -1
diff -Nru a/arch/i386/kernel/Makefile b/arch/i386/kernel/Makefile
@@ -7,7 +7,7 @@
 obj-y	:= process.o semaphore.o signal.o entry.o traps.o irq.o vm86.o \
 		ptrace.o time.o ioport.o ldt.o setup.o i8259.o sys_i386.o \
 		pci-dma.o i386_ksyms.o i387.o dmi_scan.o bootflag.o \
-		doublefault.o quirks.o
+		doublefault.o quirks.o tsc.o
 
 obj-y				+= cpu/
 obj-$(!CONFIG_NEWTOD)		+= timers/
arch/i386/kernel/setup.c +2 -0
diff -Nru a/arch/i386/kernel/setup.c b/arch/i386/kernel/setup.c
@@ -49,6 +49,7 @@
 #include <asm/io_apic.h>
 #include <asm/ist.h>
 #include <asm/io.h>
+#include <asm/tsc.h>
 #include "setup_arch_pre.h"
 #include <bios_ebda.h>
 
@@ -1439,6 +1440,7 @@
 	conswitchp = &dummy_con;
 #endif
 #endif
+	tsc_init();
 }
 
 #include "setup_arch_post.h"
arch/i386/kernel/time.c +2 -0
diff -Nru a/arch/i386/kernel/time.c b/arch/i386/kernel/time.c
@@ -461,6 +461,7 @@
 
 void __init time_init(void)
 {
+#ifndef CONFIG_NEWTOD
 #ifdef CONFIG_HPET_TIMER
 	if (is_hpet_capable()) {
 		/*
@@ -478,6 +479,7 @@
 
 	cur_timer = select_timer();
 	printk(KERN_INFO "Using %s for high-res timesource\n",cur_timer->name);
+#endif
 
 	time_init_hook();
 }
arch/i386/kernel/timers/common.c +0 -2
diff -Nru a/arch/i386/kernel/timers/common.c b/arch/i386/kernel/timers/common.c
@@ -22,8 +22,6 @@
  * device.
  */
 
-#define CALIBRATE_TIME	(5 * 1000020/HZ)
-
 unsigned long __init calibrate_tsc(void)
 {
 	mach_prepare_counter();
arch/i386/kernel/tsc.c new file +111 -0

111 lines not on this page · raw message

drivers/timesource/Makefile +6 -0
diff -Nru a/drivers/timesource/Makefile b/drivers/timesource/Makefile
@@ -1 +1,7 @@
 obj-y += jiffies.o
+obj-$(CONFIG_X86) += i386_tsc.o
+obj-$(CONFIG_PPC64) += ppc64_timebase.o
+#obj-$(CONFIG_X86) += i386_pit.o
+obj-$(CONFIG_X86_CYCLONE_TIMER) += cyclone.o
+obj-$(CONFIG_X86_PM_TIMER) += acpi_pm.o
+obj-$(CONFIG_X86_64) += x86-64_hpet.o
drivers/timesource/acpi_pm.c new file +113 -0

113 lines not on this page · raw message

drivers/timesource/cyclone.c new file +154 -0

154 lines not on this page · raw message

drivers/timesource/i386_pit.c new file +100 -0

100 lines not on this page · raw message

drivers/timesource/i386_tsc.c new file +55 -0

55 lines not on this page · raw message

drivers/timesource/ppc64_timebase.c new file +37 -0
diff -Nru a/drivers/timesource/ppc64_timebase.c b/drivers/timesource/ppc64_timebase.c
@@ -0,0 +1,37 @@
+#include <linux/timesource.h>
+#include <asm/time.h>
+
+static cycle_t timebase_read(void)
+{
+	return (cycle_t)get_tb();
+}
+
+struct timesource_t timesource_timebase = {
+	.name = "timebase",
+	.priority = 200,
+	.type = TIMESOURCE_FUNCTION,
+	.read_fnct = timebase_read,
+	.mask = (cycle_t)-1UL,
+	.mult = 0,
+	.shift = 22,
+};
+
+
+/* XXX - this should be calculated or properly externed! */
+extern unsigned long tb_to_ns_scale;
+extern unsigned long tb_to_ns_shift;
+extern unsigned long tb_ticks_per_sec;
+
+static int init_timebase_timesource(void)
+{
+	unsigned long long x;
+	x = (NSEC_PER_SEC/HZ);
+	x = x << timesource_timebase.shift;
+	do_div(x, (tb_ticks_per_sec/1000));
+	timesource_timebase.mult = (u32)x;
+
+	register_timesource(&timesource_timebase);
+	return 0;
+}
+
+module_init(init_timebase_timesource);
drivers/timesource/x86-64_hpet.c new file +40 -0
diff -Nru a/drivers/timesource/x86-64_hpet.c b/drivers/timesource/x86-64_hpet.c
@@ -0,0 +1,40 @@
+#include <linux/timesource.h>
+#include <linux/hpet.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <asm/io.h>
+#include <asm/hpet.h>
+
+#define HPET_MASK (~0L)
+#define HPET_SHIFT 32
+
+struct timesource_t timesource_hpet = {
+	.name = "hpet",
+	.priority = 300,
+	.type = TIMESOURCE_MMIO_32,
+	.mmio_ptr = NULL,
+	.mask = (cycle_t)HPET_MASK,
+	.mult = 0, /* set below */
+	.shift = HPET_SHIFT,
+};
+
+static int init_hpet_timesource(void)
+{
+	unsigned long hpet_period, hpet_hz;
+
+	if (!vxtime.hpet_address)
+		return -ENODEV;
+
+	/* calculate the hpet address */
+	timesource_hpet.mmio_ptr =
+		(void __iomem*)fix_to_virt(FIX_HPET_BASE) + HPET_COUNTER;
+
+	/* calculate and set the timesource multiplier */
+	hpet_period = hpet_readl(HPET_PERIOD);
+	hpet_hz = (1000000000000000L + hpet_period / 2) / hpet_period;
+	timesource_hpet.mult = (1000000L << HPET_SHIFT) / hpet_hz;
+
+	register_timesource(&timesource_hpet);
+	return 0;
+}
+module_init(init_hpet_timesource);
include/asm-i386/mach-default/mach_timer.h +5 -1
diff -Nru a/include/asm-i386/mach-default/mach_timer.h b/include/asm-i386/mach-default/mach_timer.h
@@ -14,8 +14,12 @@
  */
 #ifndef _MACH_TIMER_H
 #define _MACH_TIMER_H
+#include <linux/jiffies.h>
+#include <asm/io.h>
 
-#define CALIBRATE_LATCH	(5 * LATCH)
+#define CALIBRATE_ITERATION 50
+#define CALIBRATE_LATCH	(CALIBRATE_ITERATION * LATCH)
+#define CALIBRATE_TIME	(CALIBRATE_ITERATION * 1000020/HZ)
 
 static inline void mach_prepare_counter(void)
 {
include/asm-i386/tsc.h new file +6 -0
diff -Nru a/include/asm-i386/tsc.h b/include/asm-i386/tsc.h
@@ -0,0 +1,6 @@
+#ifndef _ASM_I386_TSC_H
+#define _ASM_I386_TSC_H
+extern unsigned long cpu_freq_khz;
+void tsc_init(void);
+
+#endif
include/linux/timeofday.h +2 -0
diff -Nru a/include/linux/timeofday.h b/include/linux/timeofday.h
@@ -8,7 +8,9 @@
 #define _LINUX_TIMEOFDAY_H
 #include <linux/types.h>
 #include <linux/time.h>
+#include <linux/timex.h>
 #include <asm/div64.h>
+
 
 #ifdef CONFIG_NEWTOD
 nsec_t get_lowres_timestamp(void);
kernel/sched.c +5 -0
diff -Nru a/kernel/sched.c b/kernel/sched.c
@@ -174,6 +174,11 @@
 #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran)	\
 				< (long long) (sd)->cache_hot_time)
 
+
+#ifdef CONFIG_NEWTOD
+#define sched_clock() 0
+#endif
+
 /*
  * These are the runqueue data structures:
  */
#3 lore raw

Re: [RFC][PATCH] new timeofday arch specific timesources (v. A2)

drivers/timesource/cyclone.c line 28

Quoted 5 lines · john stultz
drivers/timesource/cyclone.c new file +5 -0
+/* helper macro to atomically read both cyclone counter registers */
+#define read_cyclone_counter(low,high) \
+	do{ \
+		high = cyclone_timer[1]; low = cyclone_timer[0]; \
+	} while (high != cyclone_timer[1]);

This is only necessary on 32 bit platforms. On ia64 an atomic read would do the job. Maybe that logic needs to go into the custom defined readq for 32 bit? Then you could avoid repeating the code for drivers that read 64 bit clocks on 32bit processors.

#4 lore raw

Re: [RFC][PATCH] new timeofday arch specific timesources (v. A2)

Quoted 12 lines · Christoph Lameter

drivers/timesource/cyclone.c line 28

Quoted 5 lines · john stultz
drivers/timesource/cyclone.c new file +5 -0
+/* helper macro to atomically read both cyclone counter registers */
+#define read_cyclone_counter(low,high) \
+	do{ \
+		high = cyclone_timer[1]; low = cyclone_timer[0]; \
+	} while (high != cyclone_timer[1]);

This is only necessary on 32 bit platforms. On ia64 an atomic read would do the job. Maybe that logic needs to go into the custom defined readq for 32 bit? Then you could avoid repeating the code for drivers that read 64 bit clocks on 32bit processors.

Yea, I still need to convert the cyclone timesource to an TIMESOURCE_MMIO_64. Hopefully I'll get to that in the next release.

thanks again for the review and feedback!
-john

#5 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

In reply to john stultz
Quoted 6 lines · john stultz

All,
This patch implements the minimal architecture specific hooks to enable
the new time of day subsystem code for i386, x86-64, and ppc64. It applies on top of my linux-2.6.11-rc1_timeofday-core_A2 patch and with this patch applied, you can test the new time of day subsystem.

Quoted 6 lines

Basically it adds the call to timeofday_interrupt_hook() and cuts alot
of code out of the build via #ifdefs. I know, I know, #ifdefs' are ugly and bad, and the final patch will just remove the old code. For now this allows us to be flexible and easily switch between the two implementations with a single define. Also it makes the patch a bit easier to read.

I haven't seen your other patch. Do you mean that with this patch, ppc64 stops using it's own gettimeofday implementation based on the CPU hardware timebase ?

There are reasons why I plan to keep that. First, our implementation is very efficient. It allows a timeofday computation without locks or barriers thanks to carefully hand crafted data dependencies in the operation. Second, we have an ABI issue here. For historical reasons, we have this "systemcfg" data structure that can be mmap'ed to userland, and which contains copy of some of the ppc64 internal time keeping infos. Some userland stuff use it to implement a fully userland gettimeofday (again, without barrier nor locks). This is done at least by IBM's JVM. My still-to-be-merged vDSO patch will also use this for the userland implementation of gettimeofday syscall itself.

Ben.

#6 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 14 lines · Benjamin Herrenschmidt
Quoted 6 lines · john stultz

All,
This patch implements the minimal architecture specific hooks to enable
the new time of day subsystem code for i386, x86-64, and ppc64. It applies on top of my linux-2.6.11-rc1_timeofday-core_A2 patch and with this patch applied, you can test the new time of day subsystem.

Quoted 6 lines

Basically it adds the call to timeofday_interrupt_hook() and cuts alot
of code out of the build via #ifdefs. I know, I know, #ifdefs' are ugly and bad, and the final patch will just remove the old code. For now this allows us to be flexible and easily switch between the two implementations with a single define. Also it makes the patch a bit easier to read.

Quoted 3 lines

I haven't seen your other patch. Do you mean that with this patch, ppc64 stops using it's own gettimeofday implementation based on the CPU hardware timebase ?

Not quite. It still uses the hardware timebase, but we use a common infrastructure to calculate time. I believe you'll find the common code similar to the current ppc64 time code, as it seemed to be one of the better timeofday implementations (oh the joy of sane hardware time devices).

Quoted 4 lines

There are reasons why I plan to keep that. First, our implementation is very efficient. It allows a timeofday computation without locks or barriers thanks to carefully hand crafted data dependencies in the operation.

The performance is a concern, and right now there are issues (ntp_scale being the top of the list) however I hope they can be resolved. Looking at ppc64's do_gettimeofday() vs this implementation there we do have more overhead, but maybe you could suggest how we can avoid some of it?

Quoted 7 lines

Second, we have an ABI issue here. For historical reasons, we have this "systemcfg" data structure that can be mmap'ed to userland, and which contains copy of some of the ppc64 internal time keeping infos. Some userland stuff use it to implement a fully userland gettimeofday (again, without barrier nor locks). This is done at least by IBM's JVM. My still-to-be-merged vDSO patch will also use this for the userland implementation of gettimeofday syscall itself.

I still want to support vsyscall gettimeofday, although it does have to be done on an arch-by-arch basis. It's likely the systemcfg data structure can still be generated and exported. I'll look into it and see what can be done.

thanks
-john

#7 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 4 lines · john stultz

The performance is a concern, and right now there are issues (ntp_scale being the top of the list) however I hope they can be resolved. Looking at ppc64's do_gettimeofday() vs this implementation there we do have more overhead, but maybe you could suggest how we can avoid some of it?

I would suggest reclaculating the scale factor and offset for ntp adjustement regulary from the timer tick or so, not on each gettimeofday call.

Also, I have some updates to the ppc64 implementation where I regulary update the pre-scale offset into the post-scale one so that the timebase-prescale substraction always gives a 32 bits number. I do that so my fast userland gettimeofday can be implemented more easily and more efficiently for 32 bits processes. I yet have to check how I can hook those things into your new scheme.

Quoted 4 lines

I still want to support vsyscall gettimeofday, although it does have to be done on an arch-by-arch basis. It's likely the systemcfg data structure can still be generated and exported. I'll look into it and see what can be done.

Well, since it only contains the prescale and postscale offsets and the scaling value, it only needs to be updated when they change, so a hook here would be fine.

Ben.

#8 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 10 lines · Benjamin Herrenschmidt
Quoted 4 lines · john stultz

The performance is a concern, and right now there are issues (ntp_scale being the top of the list) however I hope they can be resolved. Looking at ppc64's do_gettimeofday() vs this implementation there we do have more overhead, but maybe you could suggest how we can avoid some of it?

I would suggest reclaculating the scale factor and offset for ntp adjustement regulary from the timer tick or so, not on each gettimeofday call.

Agreed. I'll get something like this done for the next release.

Quoted 6 lines

Also, I have some updates to the ppc64 implementation where I regulary update the pre-scale offset into the post-scale one so that the timebase-prescale substraction always gives a 32 bits number. I do that so my fast userland gettimeofday can be implemented more easily and more efficiently for 32 bits processes. I yet have to check how I can hook those things into your new scheme.

Hmm. In my code, I move the interval delta (similar to your pre-scale offset) to system_time (seems to be equivalent to the post-scale) at each call to timeofday_interrupt_hook(). So while 64 bits are normally used, you could probably get away doing the interval delta calculations in 32bits if your timesource frequency isn't too large. This would only be done in the arch-specific 32bit vsyscall code, right?

Quoted 8 lines
Quoted 4 lines

I still want to support vsyscall gettimeofday, although it does have to be done on an arch-by-arch basis. It's likely the systemcfg data structure can still be generated and exported. I'll look into it and see what can be done.

Well, since it only contains the prescale and postscale offsets and the scaling value, it only needs to be updated when they change, so a hook here would be fine.

Great, thats what I was hoping.

thanks
-john

#9 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 7 lines · john stultz

Agreed. I'll get something like this done for the next release.

Quoted 3 lines

Well, since it only contains the prescale and postscale offsets and the scaling value, it only needs to be updated when they change, so a hook here would be fine.

Great, thats what I was hoping.

I just hope that the implementation of one arch does not become a standard without sufficient reflection. Could we first get an explanation of the rationale of the offsets? From my viewpoint of the ia64 implementation I have some difficulty understanding why such complicated things as prescale and postscale are necessary in gettimeday and why the simple formula that we use in gettimeofday is not sufficient?

Frankly, the direction that the design of the new time subsystem is taking is bothering me. Work on this on our part would just improve the situation from drastically worse performance to somewhat worse. So far I have not seen a benefit of moving away from the existing code base. For the project to make sense it needs at least to be evident that the design of the solution would lead to better timer performance in the long run. Conceptually that seems so far not to be possible.

I'd love simplication of the timer subsystem through the use of nanosecond offsets. However, the POSIX api always has extra fields for seconds and nanoseconds and converting back and forth between the internal representation in 64bit nanoseconds and the POSIX structures may be another performance penalty since it involves divisions and remainder processing.

What I think is a priority need is some subsystem that manages time sources effectively (including the ability of the ntp code to scale the appropriately) and does that in an arch independent way so that all the code can be consolidated. Extract the best existing solutions and work from there.

#10 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 6 lines · Christoph Lameter

I just hope that the implementation of one arch does not become a standard without sufficient reflection. Could we first get an explanation of the rationale of the offsets? From my viewpoint of the ia64 implementation I have some difficulty understanding why such complicated things as prescale and postscale are necessary in gettimeday and why the simple formula that we use in gettimeofday is not sufficient?

What is complicated here ? The formula, at least as we do on ppc64, is simply:

time = (hw_value - prescale offset) / scale + post scale offset

Please, don't tell me that a substraction bothers you for performances, and this first offset, while it could maybe be "folded" into the second one, is actually handy, especially since it allows you to keep the (hw_value - prescale offset) in a 32 bits number if your HW timebase isn't too fast.

Now, for the details, on ppc, we calculate time in what we call "xsecs" which is 2^20 xsec/sec, so not exactly micro or nanoseconds, but that's also for simplifying calculations, we may want the generic code to just fallback on to ns.

Quoted 7 lines

Frankly, the direction that the design of the new time subsystem is taking is bothering me. Work on this on our part would just improve the situation from drastically worse performance to somewhat worse. So far I have not seen a benefit of moving away from the existing code base. For the project to make sense it needs at least to be evident that the design of the solution would lead to better timer performance in the long run. Conceptually that seems so far not to be possible.

The main problem with performances in the new code is the fact that it does the ntp correction on every call. John is aware that it is a problem and will fix that.

Quoted 7 lines

I'd love simplication of the timer subsystem through the use of nanosecond offsets. However, the POSIX api always has extra fields for seconds and nanoseconds and converting back and forth between the internal representation in 64bit nanoseconds and the POSIX structures may be another performance penalty since it involves divisions and remainder processing.

Quoted 5 lines

What I think is a priority need is some subsystem that manages time sources effectively (including the ability of the ntp code to scale the appropriately) and does that in an arch independent way so that all the code can be consolidated. Extract the best existing solutions and work from there.

Which is what John is trying to do, so help instead of criticizing :)

Ben.

#11 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · Christoph Lameter

I just hope that the implementation of one arch does not become a standard without sufficient reflection. Could we first get an explanation of the rationale of the offsets? From my viewpoint of the ia64 implementation I have some difficulty understanding why such complicated things as prescale and postscale are necessary in gettimeday and why the simple formula that we use in gettimeofday is not sufficient?

Quoted 4 lines

What is complicated here ? The formula, at least as we do on ppc64, is simply:

time = (hw_value - prescale offset) / scale + post scale offset

Yes that is basically what we do on ia64 but we use different terminology.

time = ns_at_last_tick + (hw_value - last_tick_hw_value) * scale >> shift

Quoted 7 lines
Quoted 5 lines

What I think is a priority need is some subsystem that manages time sources effectively (including the ability of the ntp code to scale the appropriately) and does that in an arch independent way so that all the code can be consolidated. Extract the best existing solutions and work from there.

Which is what John is trying to do, so help instead of criticizing :)

I sure hope that we will be doing that. But so far this has been a new implementation instead otherwise ntp_scale would not be in the gettimeofday function.

#12 lore raw

Re: [RFC][PATCH] new timeofday arch specific hooks (v. A2)

In reply to john stultz
Quoted 6 lines · john stultz

Hmm. In my code, I move the interval delta (similar to your pre-scale offset) to system_time (seems to be equivalent to the post-scale) at each call to timeofday_interrupt_hook(). So while 64 bits are normally used, you could probably get away doing the interval delta calculations in 32bits if your timesource frequency isn't too large. This would only be done in the arch-specific 32bit vsyscall code, right?

Yes. Looks ok so far, but I need to make sure by looking at the code. I'll let you know.

Quoted 13 lines
Quoted 8 lines
Quoted 4 lines

I still want to support vsyscall gettimeofday, although it does have to be done on an arch-by-arch basis. It's likely the systemcfg data structure can still be generated and exported. I'll look into it and see what can be done.

Well, since it only contains the prescale and postscale offsets and the scaling value, it only needs to be updated when they change, so a hook here would be fine.

Great, thats what I was hoping.

thanks
-john

Signature · 1 line
Benjamin Herrenschmidt <[email protected]>
#13 lore raw
In reply to john stultz
Quoted 11 lines · john stultz
kernel/timeofday.c new file +11 -0
+/* __monotonic_clock():
+ *		private function, must hold system_time_lock lock when being
+ *		called. Returns the monotonically increasing number of
+ *		nanoseconds	since the system booted (adjusted by NTP scaling)
+ */
+static nsec_t __monotonic_clock(void)
+{
+	nsec_t ret, ns_offset;
+	cycle_t now, delta;
+
+	/* read timesource */

kernel/timeofday.c line 138

Quoted 10 lines
kernel/timeofday.c new file +10 -0
+	now = read_timesource(timesource);
+
+	/* calculate the delta since the last clock_interrupt */
+	delta = (now - offset_base) & timesource->mask;
+
+	/* convert to nanoseconds */
+	ns_offset = cyc2ns(timesource, delta, NULL);
+
+	/* apply the NTP scaling */
+	ns_offset = ntp_scale(ns_offset);

The monotonic clock is the time base for the gettime and gettimeofday functions. This means ntp_scale() is called every time that the kernel or an application access time.

As pointed out before this will dramatically worsen the performance compared to the current code base.

ntp_scale() also will make it difficult to implement optimized arch specific version of function for timer access.

The fastcalls would have to be disabled on ia64 to make this work. Its likely extremely difficult to implement a fastcall if it involves ntp_scale().

#14 lore raw
Quoted 14 lines · Christoph Lameter

kernel/timeofday.c line 138

Quoted 5 lines · john stultz
kernel/timeofday.c new file +5 -0
+	/* convert to nanoseconds */
+	ns_offset = cyc2ns(timesource, delta, NULL);
+
+	/* apply the NTP scaling */
+	ns_offset = ntp_scale(ns_offset);

The monotonic clock is the time base for the gettime and gettimeofday functions. This means ntp_scale() is called every time that the kernel or an application access time.

As pointed out before this will dramatically worsen the performance compared to the current code base.

Quoted 6 lines

ntp_scale() also will make it difficult to implement optimized arch specific version of function for timer access.

The fastcalls would have to be disabled on ia64 to make this work. Its likely extremely difficult to implement a fastcall if it involves ntp_scale().

Yep, performance is a big concern. Re-working ntp_scale() is still on my TODO list. I just didn't get to it in this release.

Patches are always welcome. :)

thanks for the feedback!
-john

#15 lore raw
Quoted 2 lines · john stultz

Yep, performance is a big concern. Re-working ntp_scale() is still on my TODO list. I just didn't get to it in this release.

This is a hopeless endeavor if you look at the function. Throw ntp_scale out and calculate a scaling factor during the ticks. At tick time then you may forward the clock a few ns in order to correct it otherwise monkey around with the scaling factor.

#16 lore raw
Quoted 8 lines · Christoph Lameter
Quoted 2 lines · john stultz

Yep, performance is a big concern. Re-working ntp_scale() is still on my TODO list. I just didn't get to it in this release.

This is a hopeless endeavor if you look at the function. Throw ntp_scale out and calculate a scaling factor during the ticks. At tick time then you may forward the clock a few ns in order to correct it otherwise monkey around with the scaling factor.

We talked about this last time. I do intend to re-work ntp_scale() so its not a function call, much as you describe above.

hopelessly endeavoring,
-john

#17 lore raw
Quoted 4 lines · john stultz

We talked about this last time. I do intend to re-work ntp_scale() so its not a function call, much as you describe above.

hopelessly endeavoring,

hehe.... But seriously: The easiest approach may be to modify the time sources to allow a fine tuning of the scaling factor. That way ntp_scale may be moved into tick processing where it would adjust the scaling of the time sources up or downward. Thus no ntp_scale in the monotonic clock processing anymore.

Monotonic clocks could be calculated

monotime = ns_at_last_tick + (time_source_cycles_since_tick * current_scaling_factor) >> shift_factor.

This would also be easy to implement in asm if necessary.

tick processing could then increment or decrement the current scaling factor to minimize the error between ticks. It could also add nanoseconds to ns_at_last_tick to correct the clock forward.

With the appropiate shift_factor one should be able to fine tune time much more accurately than ntp_scale would do. Over time the necessary corrections could be minimized to just adding a few ns once in a while.

#18 lore raw
Quoted 5 lines · Christoph Lameter
Quoted 3 lines · john stultz

We talked about this last time. I do intend to re-work ntp_scale() so its not a function call, much as you describe above.

Quoted 7 lines
Quoted 1 line

hopelessly endeavoring,

hehe.... But seriously: The easiest approach may be to modify the time sources to allow a fine tuning of the scaling factor. That way ntp_scale may be moved into tick processing where it would adjust the scaling of the time sources up or downward. Thus no ntp_scale in the monotonic clock processing anymore.

It depends what you want to have between ticks: If your ticks are too wide, the clock will do a little jump forward at the start of a new tick; if they are too narrow, the clock will jump back a bit at the start of a new tick (assuming tick interpolation and tick generation are correlated. (The old kernel code uses a constant to scale the timer's register to a tick. However if the time is too fast or slow, the interpolation will also be). Those being blessed with a GPS or better clock will be able to demonstrate the quality of the code as well as the tuning possibilities against frequency errors.

Quoted 11 lines

Monotonic clocks could be calculated

monotime = ns_at_last_tick + (time_source_cycles_since_tick * current_scaling_factor) >> shift_factor.

This would also be easy to implement in asm if necessary.

tick processing could then increment or decrement the current scaling factor to minimize the error between ticks. It could also add nanoseconds to ns_at_last_tick to correct the clock forward.

Is that what corresponds to "adjust_nanoscale()" in my PPSkit?

Quoted 5 lines

With the appropiate shift_factor one should be able to fine tune time much more accurately than ntp_scale would do. Over time the necessary corrections could be minimized to just adding a few ns once in a while.

Regards,
Ulrich

#19 lore raw
In reply to Christoph Lameter
Quoted 10 lines

Monotonic clocks could be calculated

monotime = ns_at_last_tick + (time_source_cycles_since_tick * current_scaling_factor) >> shift_factor.

This would also be easy to implement in asm if necessary.

tick processing could then increment or decrement the current scaling factor to minimize the error between ticks. It could also add nanoseconds to ns_at_last_tick to correct the clock forward.

I'd think that adding nanoseconds to ns_at_last_tick is not a good idea. It might minimize the error shortly after the tick, but not the total error average over the whole tick period. And it introduces clock jumps. Tiny, but unnecessary.

Just as you say,

Quoted 3 lines

With the appropiate shift_factor one should be able to fine tune time much more accurately than ntp_scale would do. Over time the necessary corrections could be minimized to just adding a few ns once in a while.

finetuning the scaling factor should be enough.

Tim

#20 lore raw
In reply to Christoph Lameter
Quoted 19 lines · Christoph Lameter
Quoted 17 lines · john stultz
kernel/timeofday.c new file +17 -0
+/* __monotonic_clock():
+ *		private function, must hold system_time_lock lock when being
+ *		called. Returns the monotonically increasing number of
+ *		nanoseconds	since the system booted (adjusted by NTP scaling)
+ */
+static nsec_t __monotonic_clock(void)
+{
+	nsec_t ret, ns_offset;
+	cycle_t now, delta;
+
+	/* read timesource */
+	now = read_timesource(timesource);
+
+	/* calculate the delta since the last clock_interrupt */
+	delta = (now - offset_base) & timesource->mask;
+
+	/* convert to nanoseconds */
Quoted 8 lines

kernel/timeofday.c line 138

Quoted 4 lines
kernel/timeofday.c new file +4 -0
+	ns_offset = cyc2ns(timesource, delta, NULL);
+
+	/* apply the NTP scaling */
+	ns_offset = ntp_scale(ns_offset);

The monotonic clock is the time base for the gettime and gettimeofday functions. This means ntp_scale() is called every time that the kernel or an application access time.

It depends on what you want: There is little sense in implementing a nanosecond clock model with NTP when the result is wrong by several microseconds IMHO. You don't know what the time is used for, so just get the best you can. Thos only wanting some crude time, could be happy with the jiffies (or their equivalent), right?

Regards,
Ulrich

Quoted 11 lines

As pointed out before this will dramatically worsen the performance compared to the current code base.

ntp_scale() also will make it difficult to implement optimized arch specific version of function for timer access.

The fastcalls would have to be disabled on ia64 to make this work. Its likely extremely difficult to implement a fastcall if it involves ntp_scale().

#21 lore raw
In reply to john stultz
Quoted 4 lines · john stultz

All,
Here is a new release of my time of day proposal, which include ppc64
support as well as suspend/resume and cpufreq hooks. For basic summary of my ideas, you can follow this link: http://lwn.net/Articles/100665/

[...]
How do vsyscalls (running gettimeofday in user space) fit into your architecture? I don't see any provision for this.

Also on x86-64 we plan to keep the cycle time base per CPU, that will likely require some more changes to your architecture too.

-Andi

#22 lore raw
Quoted 9 lines · Andi Kleen
Quoted 4 lines · john stultz

All,
Here is a new release of my time of day proposal, which include ppc64
support as well as suspend/resume and cpufreq hooks. For basic summary of my ideas, you can follow this link: http://lwn.net/Articles/100665/

[...]
How do vsyscalls (running gettimeofday in user space) fit into your architecture? I don't see any provision for this.

Yea, I had some earlier ideas for it, although they were misconceived. My plan at the moment is to do it similarly to how x86-64 and my i386 patch did it, but still have it on an arch-per-arch basis.

Quoted 2 lines

Also on x86-64 we plan to keep the cycle time base per CPU, that will likely require some more changes to your architecture too.

I like to hear more details, if you can discuss it. Its interesting, because I don't quite see how you'd be able to do this.

thanks
-john

#23 lore raw
In reply to john stultz

Minor spelling fix, and a question.

Quoted 5 lines · john stultz

linux-2.6.11-rc2_timeofday-core_A2.patch
========================================
diff -Nru a/drivers/Makefile b/drivers/Makefile

drivers/Makefile +0 -0

...

Quoted 1 line

+ * all systems. It has the same course resolution as

should be "coarse"

Do you replace get_cmos_time() - it doesn't look like it.

You use it in your patch here...

Quoted 3 lines

diff -Nru a/arch/i386/kernel/time.c b/arch/i386/kernel/time.c

arch/i386/kernel/time.c +0 -0

...

arch/i386/kernel/time.c

Quoted 6 lines
arch/i386/kernel/time.c +6 -0
+/* arch specific timeofday hooks */
+nsec_t read_persistent_clock(void)
+{
+	return (nsec_t)get_cmos_time() * NSEC_PER_SEC;
+}
+

I didn't scan for all uses of read_persistent_clock, but in my experience get_cmos_time() has a latency of up to 1 second on x86 because it synchronizes with the rollover of the RTC seconds.

This comment in timeofday.c:timeofday_suspend_hook worries me:

arch/i386/kernel/time.c

Quoted 7 lines
arch/i386/kernel/time.c +7 -0
+	/* First off, save suspend start time
+	 * then quickly read the time source.
+	 * These two calls hopefully occur quickly
+	 * because the difference will accumulate as
+	 * time drift on resume.
+	 */
+	suspend_start = read_persistent_clock();

Do you know if the sync problem is an issue here?

=============================
Tim Bird
Architecture Group Chair, CE Linux Forum
Senior Staff Engineer, Sony Electronics
=============================

#24 lore raw
Quoted 10 lines · Tim Bird

Minor spelling fix, and a question.

Quoted 5 lines · john stultz

linux-2.6.11-rc2_timeofday-core_A2.patch
========================================
diff -Nru a/drivers/Makefile b/drivers/Makefile

drivers/Makefile +0 -0

...

Quoted 2 lines
Quoted 1 line

+ * all systems. It has the same course resolution as

should be "coarse"

Good catch, I'm a terrible speller.

Quoted 1 line

Do you replace get_cmos_time() - it doesn't look like it.

Nope, its still used on i386 and x86-64, however I had to create an arch independent abstraction for set/read_persistent_clock().

Quoted 7 lines

You use it in your patch here...

Quoted 3 lines

diff -Nru a/arch/i386/kernel/time.c b/arch/i386/kernel/time.c

arch/i386/kernel/time.c +0 -0

...

Quoted 11 lines

arch/i386/kernel/time.c

Quoted 6 lines
arch/i386/kernel/time.c +6 -0
+/* arch specific timeofday hooks */
+nsec_t read_persistent_clock(void)
+{
+	return (nsec_t)get_cmos_time() * NSEC_PER_SEC;
+}
+

I didn't scan for all uses of read_persistent_clock, but in my experience get_cmos_time() has a latency of up to 1 second on x86 because it synchronizes with the rollover of the RTC seconds.

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Quoted 12 lines

This comment in timeofday.c:timeofday_suspend_hook worries me:

arch/i386/kernel/time.c

Quoted 7 lines
arch/i386/kernel/time.c +7 -0
+	/* First off, save suspend start time
+	 * then quickly read the time source.
+	 * These two calls hopefully occur quickly
+	 * because the difference will accumulate as
+	 * time drift on resume.
+	 */
+	suspend_start = read_persistent_clock();

Do you know if the sync problem is an issue here?

I don't believe so. The full context of the code is this:

	/* First off, save suspend start time
	 * then quickly read the time source.
	 * These two calls hopefully occur quickly
	 * because the difference will accumulate as
	 * time drift on resume.
	 */
	suspend_start = read_persistent_clock();
	now = read_timesource(timesource);

Since we call read_persistent_clock(), it should return right as the second changes, thus we will be marking the new second as closely as possible with the timesource value. If the order was reversed, I think it would be a concern.

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

thanks so much for the code review!
-john

#25 lore raw

Hi John and Tim.

Quoted 8 lines · john stultz
Quoted 4 lines

I didn't scan for all uses of read_persistent_clock, but in my experience get_cmos_time() has a latency of up to 1 second on x86 because it synchronizes with the rollover of the RTC seconds.

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Tim and I talked about this at the recent CELF conference. I have a concern in that suspend-to-disk calls the suspend methods and then (after the atomic copy) the resume methods. Since the copy usually takes < 1s, and the suspend and resume methods both make two calls to get_coms_time, that's an average of 1.5s per suspend call and 1.5s per resume call - but if the copy does take next to no time (as normal), it's really 1.5s + 2s = 3.5s average just for getting the time. I believe Tim has similar issues in code he is working on. It's a concern if your battery is running out and you're trying to hibernate!

[...]

Quoted 4 lines

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

My question is, "Is there a way we can get sub-second resolution without waiting for the start of a new second four times in a row?" I'm sure there must be.

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
#26 lore raw
Quoted 12 lines · Nigel Cunningham

Hi John and Tim.

Quoted 8 lines · john stultz
Quoted 4 lines

I didn't scan for all uses of read_persistent_clock, but in my experience get_cmos_time() has a latency of up to 1 second on x86 because it synchronizes with the rollover of the RTC seconds.

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Quoted 9 lines

Tim and I talked about this at the recent CELF conference. I have a concern in that suspend-to-disk calls the suspend methods and then (after the atomic copy) the resume methods. Since the copy usually takes < 1s, and the suspend and resume methods both make two calls to get_coms_time, that's an average of 1.5s per suspend call and 1.5s per resume call - but if the copy does take next to no time (as normal), it's really 1.5s + 2s = 3.5s average just for getting the time. I believe Tim has similar issues in code he is working on. It's a concern if your battery is running out and you're trying to hibernate!

Well, counting the atomic copy in the "3.5s average just for getting the time" doesn't quite seem fair, but I think I understand. Its interesting, I wasn't aware of the suspend/copy/resume process that occurs for suspend-to-disk. The thing I don't quite get is why are the resume methods called before we really suspend to disk?

Quoted 8 lines
Quoted 4 lines

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

My question is, "Is there a way we can get sub-second resolution without waiting for the start of a new second four times in a row?" I'm sure there must be.

Well, I'm not sure what else we could use for the persistent clock, but I'd be happy to change the read/set_persistent_clock function to use it.

thanks
-john

#27 lore raw

Hi.

Quoted 23 lines · john stultz
Quoted 12 lines · Nigel Cunningham

Hi John and Tim.

Quoted 8 lines · john stultz
Quoted 4 lines

I didn't scan for all uses of read_persistent_clock, but in my experience get_cmos_time() has a latency of up to 1 second on x86 because it synchronizes with the rollover of the RTC seconds.

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Quoted 9 lines

Tim and I talked about this at the recent CELF conference. I have a concern in that suspend-to-disk calls the suspend methods and then (after the atomic copy) the resume methods. Since the copy usually takes < 1s, and the suspend and resume methods both make two calls to get_coms_time, that's an average of 1.5s per suspend call and 1.5s per resume call - but if the copy does take next to no time (as normal), it's really 1.5s + 2s = 3.5s average just for getting the time. I believe Tim has similar issues in code he is working on. It's a concern if your battery is running out and you're trying to hibernate!

Quoted 2 lines

Well, counting the atomic copy in the "3.5s average just for getting the time" doesn't quite seem fair, but I think I understand. Its

You're right. Maybe we could say 3s, accounting .5s of that 3.5s average delay as being for the atomic copy.

Quoted 3 lines

interesting, I wasn't aware of the suspend/copy/resume process that occurs for suspend-to-disk. The thing I don't quite get is why are the resume methods called before we really suspend to disk?

We call the suspend and resume methods because the suspend is supposed to achieve atomicity, and the resume is necessary for us to be able to write the image. (Remember that these calls are invoked as part of the drivers_suspend and drivers_resume code). Until recently the sysdev_suspend and resume methods weren't called and things did still work, but that was an omission and we did then run into time issues.

Quoted 11 lines
Quoted 8 lines
Quoted 4 lines

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

My question is, "Is there a way we can get sub-second resolution without waiting for the start of a new second four times in a row?" I'm sure there must be.

Well, I'm not sure what else we could use for the persistent clock, but I'd be happy to change the read/set_persistent_clock function to use it.

Is it possible to still use the persistent clock, but do the math for the portions of seconds?

By the way, Tim, I hope I didn't misunderstand anything, and that these _are_ the same issues you had!

Regards,

Nigel

Quoted 2 lines

thanks
-john

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

Ph: +61 (2) 6292 8028      Mob: +61 (417) 100 574
#28 lore raw
Quoted 7 lines · Nigel Cunningham

Hi.

Quoted 3 lines · john stultz

interesting, I wasn't aware of the suspend/copy/resume process that occurs for suspend-to-disk. The thing I don't quite get is why are the resume methods called before we really suspend to disk?

Quoted 6 lines

We call the suspend and resume methods because the suspend is supposed to achieve atomicity, and the resume is necessary for us to be able to write the image. (Remember that these calls are invoked as part of the drivers_suspend and drivers_resume code). Until recently the sysdev_suspend and resume methods weren't called and things did still work, but that was an omission and we did then run into time issues.

Ah! Ok, thanks for the summary.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

My question is, "Is there a way we can get sub-second resolution without waiting for the start of a new second four times in a row?" I'm sure there must be.

Quoted 5 lines
Quoted 2 lines

Well, I'm not sure what else we could use for the persistent clock, but I'd be happy to change the read/set_persistent_clock function to use it.

Is it possible to still use the persistent clock, but do the math for the portions of seconds?

I'm not sure what you mean? Given the patch Tim just sent, it seems the issue is the CMOS only gives us second resolution, so we try to increase our accuracy by aligning the reads so we return when the second changes. We can avoid the read-alignment which speeds things up, but introduces up to a second worth of drift. If that's ok, then the trade off is worth it.

Alternative persistent clocks like the efi clock might provide better resolution and could then avoid this issue. Although I don't know for sure.

thanks
-john

#29 lore raw

Hi.

Quoted 8 lines · john stultz
Quoted 6 lines

We call the suspend and resume methods because the suspend is supposed to achieve atomicity, and the resume is necessary for us to be able to write the image. (Remember that these calls are invoked as part of the drivers_suspend and drivers_resume code). Until recently the sysdev_suspend and resume methods weren't called and things did still work, but that was an omission and we did then run into time issues.

Ah! Ok, thanks for the summary.

No problem.

Quoted 22 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

I've only lightly tested the suspend code, but on my system I didn't see very much drift appear. Regardless, it should be better then what the current suspend/resume code does, which doesn't keep any sub-second resolution across suspend.

My question is, "Is there a way we can get sub-second resolution without waiting for the start of a new second four times in a row?" I'm sure there must be.

Quoted 5 lines
Quoted 2 lines

Well, I'm not sure what else we could use for the persistent clock, but I'd be happy to change the read/set_persistent_clock function to use it.

Is it possible to still use the persistent clock, but do the math for the portions of seconds?

I'm not sure what you mean? Given the patch Tim just sent, it seems the issue is the CMOS only gives us second resolution, so we try to increase our accuracy by aligning the reads so we return when the second changes. We can avoid the read-alignment which speeds things up, but introduces up to a second worth of drift. If that's ok, then the trade off is worth it.

Quoted 3 lines

Alternative persistent clocks like the efi clock might provide better resolution and could then avoid this issue. Although I don't know for sure.

Ah. Okay. I hadn't looked that closely so that I realised the CMOS only gives the accuracy we're using. Humble apologies. So then, I agree: it would be best if we can move to something with greater precision and make mileage from it. Is that an option on all x86 machines though? I guess cmos is the lowest common denominator :<

Nigel

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

Ph: +61 (2) 6292 8028      Mob: +61 (417) 100 574
#30 lore raw
In reply to john stultz
Quoted 3 lines · john stultz

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Sorry - I should have given more context. I am worried about suspend and resume times. An extra (up-to-a) second delay on suspend it pretty painful for CE devices. (See my SIG for my other hat in the forum.)

Quoted 6 lines

Since we call read_persistent_clock(), it should return right as the second changes, thus we will be marking the new second as closely as possible with the timesource value. If the order was reversed, I think it would be a concern.

It sounds like for your code, this synchronization is a valuable. For many CE products, the synchronization is not needed. I have a patch that removes the synchronization for i386 and ppc, but I haven't submitted it because I didn't want to mess up non-boot-context callers of get_cmos_time which have valid synchronization needs.

As you can see below, the patch is pretty braindead.
I was wondering if this conflicted with your new timer system or not.

diffstat:

 arch/ppc/kernel/time.c                    |   10 ++++++++--
 include/asm-i386/mach-default/mach_time.h |    6 +++++-
 init/Kconfig                              |   27 +++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 3 deletions(-)

-----------------------

arch/ppc/kernel/time.c +8 -2
diff -pruN -X /home/tbird/dontdiff linux-2.6.10.orig/arch/ppc/kernel/time.c linux-2.6.10/arch/ppc/kernel/time.c
@@ -291,8 +291,12 @@ EXPORT_SYMBOL(do_settimeofday);
 /* This function is only called on the boot processor */
 void __init time_init(void)
 {
-	time_t sec, old_sec;
-	unsigned old_stamp, stamp, elapsed;
+	time_t sec;
+	unsigned stamp;
+#ifndef CONFIG_RTC_NO_SYNC
+	time_t old_sec;
+	unsigned old_stamp, elapsed;
+#endif
 
         if (ppc_md.time_init != NULL)
                 time_offset = ppc_md.time_init();
@@ -317,6 +321,7 @@ void __init time_init(void)
 	stamp = get_native_tbl();
 	if (ppc_md.get_rtc_time) {
 		sec = ppc_md.get_rtc_time();
+#ifndef CONFIG_RTC_NO_SYNC
 		elapsed = 0;
 		do {
 			old_stamp = stamp;
@@ -329,6 +334,7 @@ void __init time_init(void)
 		} while ( sec == old_sec && elapsed < 2*HZ*tb_ticks_per_jiffy);
 		if (sec==old_sec)
 			printk("Warning: real time clock seems stuck!\n");
+#endif
 		xtime.tv_sec = sec;
 		xtime.tv_nsec = 0;
 		/* No update now, we just read the time from the RTC ! */
include/asm-i386/mach-default/mach_time.h +5 -1
diff -pruN -X /home/tbird/dontdiff linux-2.6.10.orig/include/asm-i386/mach-default/mach_time.h linux-2.6.10/include/asm-i386/mach-default/mach_time.h
@@ -89,6 +89,7 @@ static inline unsigned long mach_get_cmo
 	 * RTC registers show the second which has precisely just started.
 	 * Let's hope other operating systems interpret the RTC the same way.
 	 */
+#ifndef CONFIG_RTC_NO_SYNC_ON_READ
 	/* read RTC exactly on falling edge of update flag */
 	for (i = 0 ; i < 1000000 ; i++)	/* may take up to 1 second... */
 		if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
@@ -96,7 +97,10 @@ static inline unsigned long mach_get_cmo
 	for (i = 0 ; i < 1000000 ; i++)	/* must try at least 2.228 ms */
 		if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
 			break;
-	do { /* Isn't this overkill ? UIP above should guarantee consistency */
+/* The following is probably overkill because
+ * UIP above should guarantee consistency */
+#endif
+	do {
 		sec = CMOS_READ(RTC_SECONDS);
 		min = CMOS_READ(RTC_MINUTES);
 		hour = CMOS_READ(RTC_HOURS);
init/Kconfig +27 -0
diff -pruN -X /home/tbird/dontdiff linux-2.6.10.orig/init/Kconfig linux-2.6.10/init/Kconfig
@@ -248,6 +248,33 @@ config IKCONFIG_PROC
 	  This option enables access to the kernel configuration file
 	  through /proc/config.gz.
 
+menuconfig FASTBOOT
+	bool "Fast boot options"
+	help
+	  Say Y here to select among various options that can decrease
+	  kernel boot time.  These options may involve providing
+	  hardcoded values for some parameters that the kernel usually
+	  determines automatically.
+
+	  This option is useful primarily on embedded systems.
+
+	  If unsure, say N.
+
+config RTC_NO_SYNC
+	bool "Disable synch on read of Real Time Clock" if FASTBOOT
+	default n
+	help
+	  The Real Time Clock is read aligned by default. That means a
+	  series of reads of the RTC are done until it's verified that
+	  the RTC's state has just changed.  If you enable this feature,
+	  this synchronization will not be performed.  The result is that
+	  the machine will boot up to 1 second faster.
+
+	  A drawback is that, with this option enabled, your system
+	  clock may drift from the correct value over the course
+	  of several boot cycles (under certain circumstances).
+
+	  If unsure, say N.
 
 menuconfig EMBEDDED
 	bool "Configure standard kernel features (for small systems)"
#31 lore raw
Quoted 9 lines · Tim Bird
Quoted 3 lines · john stultz

I believe you're right. Although we don't call read_persistent_clock() very frequently, nor do we call it in ways we don't already call get_cmos_time(). So I'm not sure exactly what the concern is.

Sorry - I should have given more context. I am worried about suspend and resume times. An extra (up-to-a) second delay on suspend it pretty painful for CE devices. (See my SIG for my other hat in the forum.)

Ok, Nigel clarified it pretty well. Thanks.

Quoted 8 lines
Quoted 6 lines

Since we call read_persistent_clock(), it should return right as the second changes, thus we will be marking the new second as closely as possible with the timesource value. If the order was reversed, I think it would be a concern.

It sounds like for your code, this synchronization is a valuable.

Well, it just affects how much time error we gain on suspend/resume. We can't be perfect (well, unless our active timesource is persistent clock), and the comment points that we're just trying to minimize the error.

Quoted 5 lines

For many CE products, the synchronization is not needed. I have a patch that removes the synchronization for i386 and ppc, but I haven't submitted it because I didn't want to mess up non-boot-context callers of get_cmos_time which have valid synchronization needs.

Interesting patch. Indeed, the trade off is just how quickly you want to boot vs how much drift you gain each suspend/resume cycle. Assuming all of the clocks are good, your patch could introduce up to 2 seconds of drift each suspend/resume cycle.

Quoted 3 lines

As you can see below, the patch is pretty braindead.
I was wondering if this conflicted with your new timer system or not.

Not really. The issue is present with or without my code.

thanks
-john

#32 lore raw
Quoted 4 lines · john stultz

Interesting patch. Indeed, the trade off is just how quickly you want to boot vs how much drift you gain each suspend/resume cycle. Assuming all of the clocks are good, your patch could introduce up to 2 seconds of drift each suspend/resume cycle.

If we're not writing to the RTC on suspend, then I believe the drift is capped. For some consumer products, 2 seconds of drift is OK.

Nigel, does the RTC get written to, or just read, on suspend?

Also, I'm worried about the clock appearing to run backwards over a suspend. Unless a suspend/resume cycle took less than 1 second, I don't think this could happen. Is that right?

=============================
Tim Bird
Architecture Group Chair, CE Linux Forum
Senior Staff Engineer, Sony Electronics
=============================

#33 lore raw
Quoted 10 lines · Tim Bird
Quoted 4 lines · john stultz

Interesting patch. Indeed, the trade off is just how quickly you want to boot vs how much drift you gain each suspend/resume cycle. Assuming all of the clocks are good, your patch could introduce up to 2 seconds of drift each suspend/resume cycle.

If we're not writing to the RTC on suspend, then I believe the drift is capped. For some consumer products, 2 seconds of drift is OK.

Nigel, does the RTC get written to, or just read, on suspend?

I'll let Nigel respond, but I don't believe so. The time code only writes out to the CMOS every X-minutes if we're synced w/ the NTP server.

Quoted 3 lines

Also, I'm worried about the clock appearing to run backwards over a suspend. Unless a suspend/resume cycle took less than 1 second, I don't think this could happen. Is that right?

Well (with my code, the existing code might be slightly different), on suspend we read the persistent clock and we accumulate all the time that has passed on the timesource. Then on resume we read the persistent clock, the delta between persistent clock reads (which cannot be negative unless the CMOS runs backwards) is added to the system time and a new time interval is started from the current value of the timesource.

So, unless something tweaks the CMOS between reads, or the hardware has problems, then time should not go backwards.

thanks
-john

#34 lore raw

Hi.

Quoted 7 lines · john stultz
Quoted 6 lines · Tim Bird
Quoted 4 lines · john stultz

Interesting patch. Indeed, the trade off is just how quickly you want to boot vs how much drift you gain each suspend/resume cycle. Assuming all of the clocks are good, your patch could introduce up to 2 seconds of drift each suspend/resume cycle.

Quoted 8 lines
Quoted 4 lines

If we're not writing to the RTC on suspend, then I believe the drift is capped. For some consumer products, 2 seconds of drift is OK.

Nigel, does the RTC get written to, or just read, on suspend?

I'll let Nigel respond, but I don't believe so. The time code only writes out to the CMOS every X-minutes if we're synced w/ the NTP server.

Yes, just read.

Quoted 12 lines
Quoted 3 lines

Also, I'm worried about the clock appearing to run backwards over a suspend. Unless a suspend/resume cycle took less than 1 second, I don't think this could happen. Is that right?

Well (with my code, the existing code might be slightly different), on suspend we read the persistent clock and we accumulate all the time that has passed on the timesource. Then on resume we read the persistent clock, the delta between persistent clock reads (which cannot be negative unless the CMOS runs backwards) is added to the system time and a new time interval is started from the current value of the timesource.

Quoted 2 lines

So, unless something tweaks the CMOS between reads, or the hardware has problems, then time should not go backwards.

Sounds good.

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