Skip to contentSkip to conversation

New timeofday proposal (v.A1)

RFC In review john stultz 8 participants 29 messages Dec 8, 2004 – Dec 9, 2004 1 message not in the archive

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

All,
Once again here is my time of day proposal and code. I've not been able
to work too much on it since my last release in September, and not much has changed in the proposal. However, since I'm doing another code release, I figured I'd re-send the design/justification summary for context.

New in this code release:
o Initial x86-64 port
o Re-worked the timesource structure as suggested by Christoph Lameter.

Any feedback or comments on this or the following code would be greatly appreciated.

thanks
-john

Proposal for an architecture independent time of day implementation.
-------------------------------------------------------------------
John Stultz ([email protected])
DRAFT
Tue Dec 7 15:38:58 PST 2004

Credits:
Keith Mannthey: Aided initial design.
Aided greatly to implementation details.
George Anzinger: Initial review and corrections. Ulrich Windl: Review and suggestions for clarity.

Many of the time of day related issues that cropped up in 2.5
development occurred where a fix or change was made to a number of architectures, but missed a few others. Currently every architecture has its own set of timekeeping functions that basically do the same thing, only using different (or frequently, not so different) types of hardware. As hardware has changed, many architectures have had to re-engineer their time system to handle multiple time and interrupt sources. With little common infrastructure, either each separate implementation has its own quirks and bugs, or we end up with a reasonable quantity of duplicated code. Additionally the lack of a clear time of day interface has led developers to use jiffies, HZ, and the raw xtime values to calculate the time of day themselves. This has lead to a number of troublesome bugs.

With the goal to simplify, streamline and consolidate the time-of-day
infrastructure, I propose the following common implementation across all arches. This will allow generic bugs to be fixed once, reduce code duplication, and with many architectures sharing the same time source, this allows drivers to be written once for multiple architectures. Additionally it will better delineate the lines between the timer subsystem and the time-of-day subsystem, opening the door for more flexible and better timekeeping.

Features of this design:
========================

o Splits time of day management from timer interrupts:
This is necessary for virtualization & tickless systems. It allows us
to no longer care how often clock_interrupt() is called. Queued, delayed or lost interrupts do not affect time keeping (within bounds - ie: the time source cannot overflow). This isolates HZ and jiffies to the timer subsystem (mostly), as they are frequently and incorrectly used to calculate time.
Additionally, it allows for dynamic tick interrupts / high-res ticks.
It avoids the need to interpolate between multiple shoddy time sources, and lets us be agnostic to where the periodic interrupts come from (cleans up i386 HPET interrupt code).

o Consolidates a large amount of code:
Allows for shared times source implementations, such as: i386, x86-64
and ia64 all use HPET, i386 and x86-64 both have ACPI PM timers, and i386 and ia64 both have cyclone counters. Time sources are just drivers!

o Generic algorithms which use time-source drivers chosen at runtime:
Drivers are just simple hw accessors functions with no internal state
needed. They can be loaded and changed while the system is running, like normal modules.

o More consistent and readable code:
Drop wall_to_monotonic & xtime in favor of a more simple system_time
and wall_time_offset variables. Where system_time is the monotonically increasing nanoseconds since boot time and wall_time_offset is the offset added to system_time to calculate time of day.

o Uses nanoseconds as the kernel's base time unit.
Rather then doing ugly manipulations to timevals or timespecs, this
simplifies math, and gives us plenty of room to grow (64bits of nanoseconds ~= 584 years).

o Clearly separates the NTP code from the time code:
Creates a clean and clear interface, keeping all the NTP related code
in a single place. Saves brains, normal people shouldn't have to think about the in kernel ntp machinery.

Brief Psudo-code to illustrate the design:
==========================================

Globals:
--------
offset_base: timesource cycle value at last call to timeofday_hook()
system_time: time in ns calculated at last call to timeofday_hook()
wall_offset: offset to monotonic_clock() to get current time of day

Functions:
----------
timeofday_hook()
now = timesource_read(); /* read the timesource */
ns = cyc2ns(now - offset_base); /* calc nsecs since last call */
ntp_ns = ntp_scale(ns); /* apply ntp scaling */
system_time += ntp_ns; /* add scaled value to system_time */
ntp_advance(ns); /* advance ntp state machine by ns */
offset_base = now; /* set new offset_base */

monotonic_clock()
now = timesource_read(); /* read the timesource */
ns = cyc2ns(now - offset_base); /* calculate nsecs since last hook */
ntp_ns = ntp_scale(ns); /* apply ntp scaling */
return system_time + ntp_ns; /* return system_time and scaled value
*/

settimeofday(desired)

	wall_offset = desired - monotonic_clock(); /* set wall offset */

gettimeofday()

	return wall_offset + monotonic_clock();	/* return current timeofday */

Points I'm glossing over for now:
====================================================

o Have to convert back to time_val for syscall interface

o ntp_scale(ns): scales ns by NTP scaling factor
- see ntp.c for details
- costly, but correct.

o ntp_advance(ns): advances NTP state machine by ns
- see ntp.c for details

o What is the cost of throwing around 64bit values for everything?
- Do we need an arch specific time structure that varies size
accordingly?

o Some arches (arm, for example) do not have high res timing hardware
- In this case we can have a "jiffies" timesource
- cyc2ns(x) = x*(NSEC_PER_SEC/HZ)
- doesn't work for tickless systems

o vsyscalls/userspace gettimeofday()
- Still done on a per-arch basis
- My earlier arch independent plan won't work
- Exporting the full NTP logic is going to be annoying

o suspend/resume
- not yet implemented, but shouldn't be hard
o cpufreq effects
- handled in the timesource driver

Anything else? What am I missing or just being ignorant of?

#1 lore raw

RFC 1/1 new timeofday core subsystem (v.A1)

All,
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 accsessor 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 archetecture specific hooks (i386 and x86-64 examples to follow), and it can be applied to a tree without affecting the code.

New in this version:
o Re-worked timesource structure from Christph Lameter's suggestions.

I look forward to your comments and feedback.

thanks
-john

linux-2.6.10-rc3_timeofday-core_A1.patch
========================================

drivers/Makefile +1 -0
diff -Nru a/drivers/Makefile b/drivers/Makefile
@@ -60,3 +60,4 @@
 obj-$(CONFIG_CPU_FREQ)		+= cpufreq/
 obj-$(CONFIG_MMC)		+= mmc/
 obj-y				+= firmware/
+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 +61 -0

61 lines not on this page · raw message

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

86 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>
@@ -525,6 +526,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
@@ -36,6 +36,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
@@ -219,6 +220,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;
@@ -401,6 +403,7 @@
 	do_gettimeofday(&txc->time);
 	return(result);
 }
+#endif
 
 asmlinkage long sys_adjtimex(struct timex __user *txc_p)
 {
kernel/timeofday.c new file +313 -0

313 lines not on this page · raw message

kernel/timer.c +4 -0
diff -Nru a/kernel/timer.c b/kernel/timer.c
@@ -568,6 +568,7 @@
 int tickadj = 500/HZ ? : 1;		/* microsecs */
 
 
+#ifndef CONFIG_NEWTOD
 /*
  * phase-lock loop variables
  */
@@ -798,6 +799,9 @@
 		}
 	} while (ticks);
 }
+#else /* CONFIG_NEWTOD */
+#define update_wall_time(x)
+#endif /* CONFIG_NEWTOD */
 
 static inline void do_process_times(struct task_struct *p,
 	unsigned long user, unsigned long system)
kernel/timesource.c new file +71 -0

71 lines not on this page · raw message

#2 lore raw

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

All,
This patch implements the minimal architecture specific hooks to enable
the new time of day subsystem code for i386 and x86-64. It applies on top of my linux-2.6.10-rc3_timeofday-core_A1 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.

The only new code is the sync_persistant_clock() function which is
mostly ripped out of do_timer_interrupt(). Pretty un-interesting.

New in this version:
o x86-64 hooks
o generic div_long_long_rem implementation (swiped from jiffies.h)

I look forward to your comments and feedback.

thanks
-john

linux-2.6.9-rc1_timeofday-arch_A1.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/time.c +38 -0
diff -Nru a/arch/i386/kernel/time.c b/arch/i386/kernel/time.c
@@ -67,6 +67,8 @@
 
 #include "io_ports.h"
 
+#include <linux/timeofday.h>
+
 extern spinlock_t i8259A_lock;
 int pit_latch_buggy;              /* extern */
 
@@ -87,6 +89,7 @@
 
 struct timer_opts *cur_timer = &timer_none;
 
+#ifndef CONFIG_NEWTOD
 /*
  * This version of gettimeofday has microsecond resolution
  * and better than microsecond precision on fast x86 machines with TSC.
@@ -169,6 +172,7 @@
 }
 
 EXPORT_SYMBOL(do_settimeofday);
+#endif
 
 static int set_rtc_mmss(unsigned long nowtime)
 {
@@ -194,11 +198,39 @@
  *		Note: This function is required to return accurate
  *		time even in the absence of multiple timer ticks.
  */
+#ifndef CONFIG_NEWTOD
 unsigned long long monotonic_clock(void)
 {
 	return cur_timer->monotonic_clock();
 }
 EXPORT_SYMBOL(monotonic_clock);
+#endif
+
+void sync_persistant_clock(struct timespec ts)
+{
+	/*
+	 * If we have an externally synchronized Linux clock, then update
+	 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
+	 * called as close as possible to 500 ms before the new second starts.
+	 */
+	if (ts.tv_sec > last_rtc_update + 660 &&
+	    (ts.tv_nsec / 1000)
+			>= USEC_AFTER - ((unsigned) TICK_SIZE) / 2 &&
+	    (ts.tv_nsec / 1000)
+			<= USEC_BEFORE + ((unsigned) TICK_SIZE) / 2) {
+		/* horrible...FIXME */
+		if (efi_enabled) {
+	 		if (efi_set_rtc_mmss(ts.tv_sec) == 0)
+				last_rtc_update = ts.tv_sec;
+			else
+				last_rtc_update = ts.tv_sec - 600;
+		} else if (set_rtc_mmss(ts.tv_sec) == 0)
+			last_rtc_update = ts.tv_sec;
+		else
+			last_rtc_update = ts.tv_sec - 600; /* do it again in 60 s */
+	}
+
+}
 
 #if defined(CONFIG_SMP) && defined(CONFIG_FRAME_POINTER)
 unsigned long profile_pc(struct pt_regs *regs)
@@ -238,6 +270,7 @@
 
 	do_timer_interrupt_hook(regs);
 
+#ifndef CONFIG_NEWTOD
 	/*
 	 * If we have an externally synchronized Linux clock, then update
 	 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
@@ -260,6 +293,7 @@
 		else
 			last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
 	}
+#endif
 
 #ifdef CONFIG_MCA
 	if( MCA_bus ) {
@@ -294,11 +328,15 @@
 	 */
 	write_seqlock(&xtime_lock);
 
+#ifndef CONFIG_NEWTOD
 	cur_timer->mark_offset();
+#endif
  
 	do_timer_interrupt(irq, NULL, regs);
 
 	write_sequnlock(&xtime_lock);
+
+	timeofday_interrupt_hook();
 	return IRQ_HANDLED;
 }
 
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 +30 -1
diff -Nru a/arch/x86_64/kernel/time.c b/arch/x86_64/kernel/time.c
@@ -35,6 +35,7 @@
 #include <asm/sections.h>
 #include <linux/cpufreq.h>
 #include <linux/hpet.h>
+#include <linux/timeofday.h>
 #ifdef CONFIG_X86_LOCAL_APIC
 #include <asm/apic.h>
 #endif
@@ -106,6 +107,7 @@
 
 unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;
 
+#ifndef CONFIG_NEWTOD
 /*
  * This version of gettimeofday() has microsecond resolution and better than
  * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
@@ -180,6 +182,7 @@
 }
 
 EXPORT_SYMBOL(do_settimeofday);
+#endif /* CONFIG_NEWTOD */
 
 unsigned long profile_pc(struct pt_regs *regs)
 {
@@ -281,6 +284,7 @@
 }
 
 
+#ifndef CONFIG_NEWTOD
 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
  *		Note: This function is required to return accurate
  *		time even in the absence of multiple timer ticks.
@@ -357,6 +361,26 @@
     }
 #endif
 }
+#endif /* CONFIG_NEWTOD */
+
+
+void sync_persistant_clock(struct timespec ts)
+{
+	static unsigned long rtc_update = 0;
+	/*
+	 * If we have an externally synchronized Linux clock, then update
+	 * CMOS clock accordingly every ~11 minutes. set_rtc_mmss() will
+	 * be called in the jiffy closest to exactly 500 ms before the
+	 * next second. If the update fails, we don't care, as it'll be
+	 * updated on the next turn, and the problem (time way off) isn't
+	 * likely to go away much sooner anyway.
+	 */
+	if (ts.tv_sec > rtc_update &&
+		abs(ts.tv_nsec - 500000000) <= tick_nsec / 2) {
+		set_rtc_mmss(xtime.tv_sec);
+		rtc_update = xtime.tv_sec + 660;
+	}
+}
 
 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
 {
@@ -373,6 +397,7 @@
 
 	write_seqlock(&xtime_lock);
 
+#ifndef CONFIG_NEWTOD
 	if (vxtime.hpet_address) {
 		offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
 		delay = hpet_readl(HPET_COUNTER) - offset;
@@ -422,6 +447,7 @@
 		handle_lost_ticks(lost, regs);
 		jiffies += lost;
 	}
+#endif /* CONFIG_NEWTOD */
 
 /*
  * Do the timer stuff.
@@ -445,6 +471,7 @@
 		smp_local_timer_interrupt(regs);
 #endif
 
+#ifndef CONFIG_NEWTOD
 /*
  * If we have an externally synchronized Linux clock, then update CMOS clock
  * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
@@ -458,9 +485,11 @@
 		set_rtc_mmss(xtime.tv_sec);
 		rtc_update = xtime.tv_sec + 660;
 	}
- 
+#endif /* CONFIG_NEWTOD */
+
 	write_sequnlock(&xtime_lock);
 
+	timeofday_interrupt_hook();
 	return IRQ_HANDLED;
 }
 
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 */
#3 lore raw

RFC 1/1 new timeofday timesources (v.A1)

All,
This patch implements most of the time sources for i386 and x86-64
(tsc, pit, cyclone, acpi-pm and hpet). It applies on top of my linux-2.6.10-rc3_timeofday-arch_A1 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 and the cpufreq code needs to be added to tsc.c. 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 x86-64 hpet code
o more i386 tsc cleanups

thanks
-john

linux-2.6.10_rc3_timeofday-timesources_A1.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-y				+= timers/
arch/i386/kernel/setup.c +2 -0
diff -Nru a/arch/i386/kernel/setup.c b/arch/i386/kernel/setup.c
@@ -48,6 +48,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>
 
@@ -1421,6 +1422,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
@@ -436,6 +436,7 @@
 
 void __init time_init(void)
 {
+#ifndef CONFIG_NEWTOD
 #ifdef CONFIG_HPET_TIMER
 	if (is_hpet_capable()) {
 		/*
@@ -453,6 +454,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/Makefile +2 -2
diff -Nru a/arch/i386/kernel/timers/Makefile b/arch/i386/kernel/timers/Makefile
@@ -4,6 +4,6 @@
 
 obj-y := timer.o timer_none.o timer_tsc.o timer_pit.o common.o
 
-obj-$(CONFIG_X86_CYCLONE_TIMER)	+= timer_cyclone.o
+#obj-$(CONFIG_X86_CYCLONE_TIMER)	+= timer_cyclone.o
 obj-$(CONFIG_HPET_TIMER)	+= timer_hpet.o
-obj-$(CONFIG_X86_PM_TIMER)	+= timer_pm.o
+#obj-$(CONFIG_X86_PM_TIMER)	+= timer_pm.o
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/timers/timer.c +2 -2
diff -Nru a/arch/i386/kernel/timers/timer.c b/arch/i386/kernel/timers/timer.c
@@ -14,13 +14,13 @@
 /* list of timers, ordered by preference, NULL terminated */
 static struct init_timer_opts* __initdata timers[] = {
 #ifdef CONFIG_X86_CYCLONE_TIMER
-	&timer_cyclone_init,
+//	&timer_cyclone_init,
 #endif
 #ifdef CONFIG_HPET_TIMER
 	&timer_hpet_init,
 #endif
 #ifdef CONFIG_X86_PM_TIMER
-	&timer_pmtmr_init,
+//	&timer_pmtmr_init,
 #endif
 	&timer_tsc_init,
 	&timer_pit_init,
arch/i386/kernel/tsc.c new file +39 -0
diff -Nru a/arch/i386/kernel/tsc.c b/arch/i386/kernel/tsc.c
@@ -0,0 +1,39 @@
+#include <linux/init.h>
+#include <linux/timex.h>
+#include <asm/tsc.h>
+#include "mach_timer.h"
+
+unsigned long cpu_freq_khz;
+
+void tsc_init(void)
+{
+	unsigned long long start, end;
+	unsigned long count;
+	u64 delta64;
+	int i;
+
+	/* repeat 3 times to make sure the cache is warm */
+	for(i=0; i < 3; i++) {
+		mach_prepare_counter();
+		rdtscll(start);
+		mach_countup(&count);
+		rdtscll(end);
+	}
+	delta64 = end - start;
+
+	/* cpu freq too fast */
+	if(delta64 > (1ULL<<32))
+		return;
+	/* cpu freq too slow */
+	if (delta64 <= CALIBRATE_TIME)
+		return;
+
+	delta64 *= 1000;
+	do_div(delta64,CALIBRATE_TIME);
+	cpu_freq_khz = (unsigned long)delta64;
+
+	cpu_khz = cpu_freq_khz;
+
+	printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000);
+
+}
drivers/timesource/Makefile +5 -0
diff -Nru a/drivers/timesource/Makefile b/drivers/timesource/Makefile
@@ -1 +1,6 @@
 obj-y += jiffies.o
+obj-$(CONFIG_X86) += i386_tsc.o
+#obj-$(CONFIG_X86) += i386_pit.o
+obj-$(CONFIG_X86_SUMMIT) += 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 +43 -0

43 lines not on this page · raw message

drivers/timesource/x86-64_hpet.c new file +44 -0

44 lines not on this page · raw message

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 +3 -0
diff -Nru a/include/linux/timeofday.h b/include/linux/timeofday.h
@@ -8,6 +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);
include/linux/timesource.h +1 -0
diff -Nru a/include/linux/timesource.h b/include/linux/timesource.h
@@ -12,6 +12,7 @@
 
 #include <linux/types.h>
 #include <linux/time.h>
+#include <asm/div64.h>
 
 /* struct timesource_t:
  *		Provides mostly state-free accessors to the underlying
#4 lore raw

Re: [RFC] new timeofday timesources (v.A1)

Quoted 6 lines · john stultz

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

Ack. That last one got sent using the wrong from address.

Please reply via [email protected].

sorry,
-john

#5 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

In reply to john stultz
Quoted 4 lines · john stultz
include/linux/timesource.h new file +4 -0
+struct timesource_t {
+	char* name;
+	int priority;
+	enum {

include/linux/timesource.h line 42

Quoted 10 lines
include/linux/timesource.h new file +10 -0
+		TIMESOURCE_FUNCTION,
+		TIMESOURCE_MMIO_32,
+		TIMESOURCE_MMIO_64
+	} type;
+	cycle_t (*read_fnct)(void);
+	void* ptr;
+	cycle_t mask;
+	u32 mult;
+	u32 shift;
+};

Maybe add TIMESOURCE_CPU or so? How can one specify a time source in a cpu register?

#6 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

Quoted 11 lines · Christoph Lameter
Quoted 9 lines · john stultz
include/linux/timesource.h new file +9 -0
+struct timesource_t {
+	char* name;
+	int priority;
+	enum {
+		TIMESOURCE_FUNCTION,
+		TIMESOURCE_MMIO_32,
+		TIMESOURCE_MMIO_64
+	} type;
+	cycle_t (*read_fnct)(void);
Quoted 8 lines

include/linux/timesource.h line 42

Quoted 5 lines
include/linux/timesource.h new file +5 -0
+	void* ptr;
+	cycle_t mask;
+	u32 mult;
+	u32 shift;
+};

Maybe add TIMESOURCE_CPU or so? How can one specify a time source in a cpu register?

Yea, for now its a function. I was thinking that get_cycles() could be used as an arch independent way to do this.

Good suggestion!
Thanks!
-john

#7 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

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 126

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,0);
+
+	/* apply the NTP scaling */
+	ns_offset = ntp_scale(ns_offset);

The call to ntp_scale will significantly impact clock retrieval performance. ntp_scale needs to be removed. Could you simply let the clock run with a scaling factor (just make sure the scaling factor is a bit slower than ntp time and then skip a few nanoseconds of time forward at the next correction?)

#8 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

In reply to john stultz

hoi :)

Quoted 1 line · john stultz

+struct timesource_t {

usually only typedefs end with _t

include/linux/timesource.h line 38

Quoted 9 lines
include/linux/timesource.h new file +9 -0
+	char* name;
+	int priority;
+	enum {
+		TIMESOURCE_FUNCTION,
+		TIMESOURCE_MMIO_32,
+		TIMESOURCE_MMIO_64
+	} type;
+	cycle_t (*read_fnct)(void);
+	void* ptr;

This could be made __iomem if it is intended to point to an IO memory region. Hmm, but then there is no ioread64, so I guess I'm wrong.

Quoted 5 lines
include/linux/timesource.h new file +4 -0
+	cycle_t mask;
+	u32 mult;
+	u32 shift;
+};
 

include/linux/timesource.h line 80

Quoted 10 lines
include/linux/timesource.h new file +10 -0
+static inline nsec_t cyc2ns(struct timesource_t* ts, cycle_t cycles, cycle_t* rem)
+{
+	u64 ret;
+	ret = (u64)cycles;
+	ret *= ts->mult;
+	ret >>= ts->shift;
+	if (rem) /* XXX we still need to do remainder math */
+		*rem = (cycle_t)0;
+	return (nsec_t)ret;
+}

well, the math is simple:

	if (rem) *rem = ret & (1 << ts->shift -1);
	ret >>= ts->shift;
Signature · 1 line
Martin Waitz

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

hoi :)

Quoted 8 lines · john stultz

Ah, actually, you missed something. The remainder you propose above is in units of cycles multiplied by mult. Thus to get it back to just cycles, we have to divide. So:

	ret *= ts->mult;
	if (rem)
		*rem = (ret & (1 << ts->shift -1))/mult;
	ret >>= ts->shift;

you are of course right

Quoted 1 line

Agreed?

well, divisions are always slow and we would loose precision again.

I have another suggestion: just keep the remainder in units of cycles*mult.

	ret *= ts->mult
	if (rem) {
		ret += *rem;
		*rem = ret & (1 << ts->shift -1);
	}
	ret >>= ts->shift

and remove the offset_base adjustion below.

Signature · 1 line
Martin Waitz
#11 lore raw

Re: [RFC] new timeofday core subsystem (v.A1)

In reply to john stultz

Hi!

drivers/timesource/Makefile line 1

Quoted 7 lines

I look forward to your comments and feedback.

diff -Nru a/drivers/timesource/Makefile b/drivers/timesource/Makefile

drivers/timesource/Makefile new file +1 -0
@@ -0,0 +1 @@
+obj-y += jiffies.o

Perhaps drivers/time would be better name? We do not have drivers/characterdevices, either...
Pavel

Signature · 1 line
64 bytes from 195.113.31.123: icmp_seq=28 ttl=51 time=448769.1 ms         
#12 lore raw
In reply to john stultz
Quoted 7 lines · john stultz

Features of this design:
========================

o Consolidates a large amount of code:
Allows for shared times source implementations, such as: i386, x86-64
and ia64 all use HPET, i386 and x86-64 both have ACPI PM timers, and i386 and ia64 both have cyclone counters. Time sources are just drivers!

What I would to see also included here is to provide a clean posix interface to these drivers. IMHO the current char interfaces to clock drivers should be removed. Look at SGI's mmtimer implementation in 2.6.10-rc3. We have modified the posix interface to allow clock drivers to export their timer values via CLOCK_SGI_CYCLE and we are also now able to schedule hardware interrupts via timer_* posix functions utilizing CLOCK_SGI_CYCLE that are then delivered as signals to an application. Timer chips usually include time sources as well as the ability to generate periodic or single shot
interrupts. There needs to be some way for clock drivers to cleanly interface with these. The API may be the posix subsystem but I do not like the quality of the code nor the current API for the clock drivers.

The API for user space to clocks already exists through the posix standard. I would suggest to work with that standard for a way to deal with clocks under Linux.

Quoted 7 lines

Brief Psudo-code to illustrate the design:
==========================================

monotonic_clock()
now = timesource_read(); /* read the timesource */
ns = cyc2ns(now - offset_base); /* calculate nsecs since last hook */
ntp_ns = ntp_scale(ns); /* apply ntp scaling */

These are not really functions right? timesource_read can be a direct memory read and the cyc2ns and ntp_scale can be reduced to some scaling factor?

Quoted 6 lines

Points I'm glossing over for now:
====================================================

o ntp_scale(ns): scales ns by NTP scaling factor
- see ntp.c for details
- costly, but correct.

Please do not call this function from monotonic_clock but provide some sort of scaling factor that is adjusted from time to time.

Quoted 3 lines

o What is the cost of throwing around 64bit values for everything?
- Do we need an arch specific time structure that varies size
accordingly?

I think 64 bit values are fine but then I work for a 64 bit company and this may just be the contextual predisposition.

Quoted 4 lines

o Some arches (arm, for example) do not have high res timing hardware
- In this case we can have a "jiffies" timesource
- cyc2ns(x) = x*(NSEC_PER_SEC/HZ)
- doesn't work for tickless systems

In that case maybe the "ticks" are the timesource and not really tick processing per se.
There could be a separation between "increment counter" and tick processing.

#13 lore raw
Quoted 10 lines · Christoph Lameter
Quoted 7 lines · john stultz

Features of this design:
========================

o Consolidates a large amount of code:
Allows for shared times source implementations, such as: i386, x86-64
and ia64 all use HPET, i386 and x86-64 both have ACPI PM timers, and i386 and ia64 both have cyclone counters. Time sources are just drivers!

Quoted 12 lines

What I would to see also included here is to provide a clean posix interface to these drivers. IMHO the current char interfaces to clock drivers should be removed. Look at SGI's mmtimer implementation in 2.6.10-rc3. We have modified the posix interface to allow clock drivers to export their timer values via CLOCK_SGI_CYCLE and we are also now able to schedule hardware interrupts via timer_* posix functions utilizing CLOCK_SGI_CYCLE that are then delivered as signals to an application. Timer chips usually include time sources as well as the ability to generate periodic or single shot
interrupts. There needs to be some way for clock drivers to cleanly interface with these. The API may be the posix subsystem but I do not like the quality of the code nor the current API for the clock drivers.

I'm not too familiar with the posix interfaces. I've been focused with in-kernel uses at the moment. I'll try to take a look at the mmtimer bits and see if I can better grasp your suggestion.

Quoted 7 lines

The API for user space to clocks already exists through the posix standard. I would suggest to work with that standard for a way to deal with clocks under Linux.

Quoted 3 lines

Brief Psudo-code to illustrate the design:
==========================================

Quoted 8 lines
Quoted 4 lines

monotonic_clock()
now = timesource_read(); /* read the timesource */
ns = cyc2ns(now - offset_base); /* calculate nsecs since last hook */
ntp_ns = ntp_scale(ns); /* apply ntp scaling */

These are not really functions right? timesource_read can be a direct memory read and the cyc2ns and ntp_scale can be reduced to some scaling factor?

Yep, timesource_read() and cyc2ns are static inlines which call the timesource read function, or just read the MMIO'ed address depending on the timesource type.

Quoted 9 lines
Quoted 6 lines

Points I'm glossing over for now:
====================================================

o ntp_scale(ns): scales ns by NTP scaling factor
- see ntp.c for details
- costly, but correct.

Please do not call this function from monotonic_clock but provide some sort of scaling factor that is adjusted from time to time.

You're going to have to expand on this. I had considered only NTP scaling the wall time, but for consistency it made more sense to me that we also apply NTP scaling to the monotonic clock. This avoids different notions of nanoseconds, one being the inaccurate unajusted system nanoseconds and the other being the accurately ntp scaled wall time nanoseconds. Trying to keep things sane.

Quoted 7 lines
Quoted 4 lines

o Some arches (arm, for example) do not have high res timing hardware
- In this case we can have a "jiffies" timesource
- cyc2ns(x) = x*(NSEC_PER_SEC/HZ)
- doesn't work for tickless systems

In that case maybe the "ticks" are the timesource and not really tick processing per se.

If I understand you, yes. In this case the timer interrupt counter (jiffies) is used as a free running counter.

Quoted 1 line

There could be a separation between "increment counter" and tick processing.

Could you expand on this?

thanks for the review!
-john

#14 lore raw
Quoted 10 lines · john stultz
Quoted 9 lines
Quoted 6 lines

Points I'm glossing over for now:
====================================================

o ntp_scale(ns): scales ns by NTP scaling factor
- see ntp.c for details
- costly, but correct.

Please do not call this function from monotonic_clock but provide some sort of scaling factor that is adjusted from time to time.

Quoted 6 lines

You're going to have to expand on this. I had considered only NTP scaling the wall time, but for consistency it made more sense to me that we also apply NTP scaling to the monotonic clock. This avoids different notions of nanoseconds, one being the inaccurate unajusted system nanoseconds and the other being the accurately ntp scaled wall time nanoseconds. Trying to keep things sane.

It certainly is more consistent but its a big performance hit if that scaling is done on every invocation of clock_gettime or gettimeofday().

With the improved scaling factor one should be able to come very close to ntp scaled time without invoking ntp_scale itself. Tick processing will then update time to be ntp scaled by fine tuning the scaling factor (with the bit shifting we can get very fine tuning) and eventually skip a few nanoseconds. Its basically some piece of interpolator logic in there so that the heavyweight calculations can just be done once in a while.

Quoted 9 lines
Quoted 2 lines

In that case maybe the "ticks" are the timesource and not really tick processing per se.

If I understand you, yes. In this case the timer interrupt counter (jiffies) is used as a free running counter.

Quoted 1 line

There could be a separation between "increment counter" and tick processing.

Could you expand on this?

The timesource is really a memory location incremented by "increment counter" and not part of tick processing. Logically these are seperate. "increment counter" could happen apart from tick processing. They are just conflated in the current tick implementation.

#15 lore raw
Quoted 22 lines · Christoph Lameter
Quoted 10 lines · john stultz
Quoted 9 lines
Quoted 6 lines

Points I'm glossing over for now:
====================================================

o ntp_scale(ns): scales ns by NTP scaling factor
- see ntp.c for details
- costly, but correct.

Please do not call this function from monotonic_clock but provide some sort of scaling factor that is adjusted from time to time.

Quoted 6 lines

You're going to have to expand on this. I had considered only NTP scaling the wall time, but for consistency it made more sense to me that we also apply NTP scaling to the monotonic clock. This avoids different notions of nanoseconds, one being the inaccurate unajusted system nanoseconds and the other being the accurately ntp scaled wall time nanoseconds. Trying to keep things sane.

It certainly is more consistent but its a big performance hit if that scaling is done on every invocation of clock_gettime or gettimeofday().

Quoted 6 lines

With the improved scaling factor one should be able to come very close to ntp scaled time without invoking ntp_scale itself. Tick processing will then update time to be ntp scaled by fine tuning the scaling factor (with the bit shifting we can get very fine tuning) and eventually skip a few nanoseconds. Its basically some piece of interpolator logic in there so that the heavyweight calculations can just be done once in a while.

No. I agree ntp_scale() is a performance concern. However I'm not sure how your suggestion of just slowing or tweaking the timesource mult/shift frequency values will allow us to implement the expected behavior of adjtimex(). We need to be able to implement the following adjustments within a single tick:

1. Adjust the frequency by 500ppm for 10usecs
2. After that adjust the frequency by 30ppm for the rest of the tick.

We can see how much of this can be fudged or generalized, but I'm hesitant to be too flippant about changing the NTP behavior for fear that the astronomers who so dearly care about leap seconds and minute time adjustments will "forget" to mention the asteroid heading towards my home. :)

I may have asked this before, but w/ 32 bit mult and shifts, how granular can these adjustments be?

Also additional complications arise when we have multiple things (like cpufreq) playing with the timesource frequency values as well.

thanks again for the thorough review!
-john

#16 lore raw
Quoted 7 lines · john stultz
Quoted 6 lines

With the improved scaling factor one should be able to come very close to ntp scaled time without invoking ntp_scale itself. Tick processing will then update time to be ntp scaled by fine tuning the scaling factor (with the bit shifting we can get very fine tuning) and eventually skip a few nanoseconds. Its basically some piece of interpolator logic in there so that the heavyweight calculations can just be done once in a while.

Quoted 8 lines

No. I agree ntp_scale() is a performance concern. However I'm not sure how your suggestion of just slowing or tweaking the timesource mult/shift frequency values will allow us to implement the expected behavior of adjtimex(). We need to be able to implement the following adjustments within a single tick:

1. Adjust the frequency by 500ppm for 10usecs
2. After that adjust the frequency by 30ppm for the rest of the tick.

Frequency adjustments just means an adjustment of the scaling factor. Am I missing something?

Quoted 5 lines

We can see how much of this can be fudged or generalized, but I'm hesitant to be too flippant about changing the NTP behavior for fear that the astronomers who so dearly care about leap seconds and minute time adjustments will "forget" to mention the asteroid heading towards my home. :)

I am not sure what NTP behavior needs to be fudged. Sorry about my limited NTP knowledge. Could you elaborate on what the problem is?

Quoted 3 lines

I may have asked this before, but w/ 32 bit mult and shifts, how granular can these adjustments be?

Yes. 128bit would be great for this. 64bit is fine though as far as I can see and allows granularity up to fractions of nanoseconds if applied between 1ms intervals.

Quoted 2 lines

Also additional complications arise when we have multiple things (like cpufreq) playing with the timesource frequency values as well.

I think these could all be taken into account by a scaling factor off a certain base established at a tick-like event that does the ntp scaling. The scaling between tick-like event needs to be just a scaling factor for performance reasons.

#17 lore raw
Quoted 43 lines · Christoph Lameter
Quoted 7 lines · john stultz
Quoted 6 lines

With the improved scaling factor one should be able to come very close to ntp scaled time without invoking ntp_scale itself. Tick processing will then update time to be ntp scaled by fine tuning the scaling factor (with the bit shifting we can get very fine tuning) and eventually skip a few nanoseconds. Its basically some piece of interpolator logic in there so that the heavyweight calculations can just be done once in a while.

Quoted 8 lines

No. I agree ntp_scale() is a performance concern. However I'm not sure how your suggestion of just slowing or tweaking the timesource mult/shift frequency values will allow us to implement the expected behavior of adjtimex(). We need to be able to implement the following adjustments within a single tick:

1. Adjust the frequency by 500ppm for 10usecs
2. After that adjust the frequency by 30ppm for the rest of the tick.

Frequency adjustments just means an adjustment of the scaling factor. Am I missing something?

Quoted 5 lines

We can see how much of this can be fudged or generalized, but I'm hesitant to be too flippant about changing the NTP behavior for fear that the astronomers who so dearly care about leap seconds and minute time adjustments will "forget" to mention the asteroid heading towards my home. :)

I am not sure what NTP behavior needs to be fudged. Sorry about my limited NTP knowledge. Could you elaborate on what the problem is?

Quoted 2 lines

I may have asked this before, but w/ 32 bit mult and shifts, how granular can these adjustments be?

Yes. 128bit would be great for this. 64bit is fine though as far as I can see and allows granularity up to fractions of nanoseconds if applied between 1ms intervals.

Quoted 8 lines
Quoted 2 lines

Also additional complications arise when we have multiple things (like cpufreq) playing with the timesource frequency values as well.

I think these could all be taken into account by a scaling factor off a certain base established at a tick-like event that does the ntp scaling. The scaling between tick-like event needs to be just a scaling factor for performance reasons.

Right. We seem to be doing ok now by just adjusting things at tick time and using the "normal" interpolation between ticks.

As for the math, the current code keeps a running "remainder" which is the amount of the correction that was finer than the clock resolution (i.e. less than a nano second) and rolls this in on the next tick. This gives resolution out to several bits to the right of the nano second. And I think this is all done with 32 bit math (if memory serves).

Quoted 2 lines
Signature · 2 lines
George Anzinger   [email protected]
High-res-timers:  http://sourceforge.net/projects/high-res-timers/
#18 lore raw
Quoted 8 lines · George Anzinger

Right. We seem to be doing ok now by just adjusting things at tick time and using the "normal" interpolation between ticks.

As for the math, the current code keeps a running "remainder" which is the amount of the correction that was finer than the clock resolution (i.e. less than a nano second) and rolls this in on the next tick. This gives resolution out to several bits to the right of the nano second. And I think this is all done with 32 bit math (if memory serves).

That is probably the i386 version with which I am not familiar. The time interpolator logic (IA64 and SPARC64) does fine with a scaled 64 bit factor without a remainder. The factor may be used to express fractions of nanoseconds.

#19 lore raw
In reply to Christoph Lameter
Quoted 20 lines · Christoph Lameter
Quoted 7 lines · john stultz
Quoted 6 lines

With the improved scaling factor one should be able to come very close to ntp scaled time without invoking ntp_scale itself. Tick processing will then update time to be ntp scaled by fine tuning the scaling factor (with the bit shifting we can get very fine tuning) and eventually skip a few nanoseconds. Its basically some piece of interpolator logic in there so that the heavyweight calculations can just be done once in a while.

Quoted 8 lines

No. I agree ntp_scale() is a performance concern. However I'm not sure how your suggestion of just slowing or tweaking the timesource mult/shift frequency values will allow us to implement the expected behavior of adjtimex(). We need to be able to implement the following adjustments within a single tick:

1. Adjust the frequency by 500ppm for 10usecs
2. After that adjust the frequency by 30ppm for the rest of the tick.

Frequency adjustments just means an adjustment of the scaling factor. Am I missing something?

Quoted 8 lines
Quoted 5 lines

We can see how much of this can be fudged or generalized, but I'm hesitant to be too flippant about changing the NTP behavior for fear that the astronomers who so dearly care about leap seconds and minute time adjustments will "forget" to mention the asteroid heading towards my home. :)

I am not sure what NTP behavior needs to be fudged. Sorry about my limited NTP knowledge. Could you elaborate on what the problem is?

Take a look at the adjtimex man page as well as the ntp.c file from the timeofday core patch. There are number of different types of adjustments that are made, possibly at the same time. Briefly, they are (to my understanding - I'm going off my notes from awhile ago):
o tick adjustments
how much time should pass in a _user_ tick
o frequency adjustments
long term adjustment to correct for constant drift),
o offset adjustments
additional ppm adjustment to correct for current offset from the ntp
server
o single shot offset adjustments
old style adjtime functionality

Tick, frequency and offset adjustments can be precalculated and summed to a single ppm adjustment. This is similar to the style of adjustment you propose directly onto the time source frequency values.

However, there is also this short term single shot adjustments. These adjustments are made by applying the MAX_SINGLESHOT_ADJ (500ppm) scaling for an amount of time (offset_len) which would compensate for the offset. This style is difficult because we cannot precompute it and apply it to an entire tick. Instead it needs to be applied for just a specific amount of time which may be only a fraction of a tick. When we start talking about systems with irregular tick frequency (via virtualization, or tickless systems) it becomes even more problematic.

If this can be fudged then it becomes less of an issue. Or at worse, we have to do two mult/shift operations on two "parts" of the time interval using different adjustments.

Its starting to look doable, but its not necessarily the simplest thing (for me at least). I'll put it on my list, but patches would be more then welcome.

thanks
-john

#20 lore raw
Quoted 18 lines · john stultz

Take a look at the adjtimex man page as well as the ntp.c file from the timeofday core patch. There are number of different types of adjustments that are made, possibly at the same time. Briefly, they are (to my understanding - I'm going off my notes from awhile ago):
o tick adjustments
how much time should pass in a _user_ tick
o frequency adjustments
long term adjustment to correct for constant drift),
o offset adjustments
additional ppm adjustment to correct for current offset from the ntp
server
o single shot offset adjustments
old style adjtime functionality

Tick, frequency and offset adjustments can be precalculated and summed to a single ppm adjustment. This is similar to the style of adjustment you propose directly onto the time source frequency values.

Quoted 8 lines

However, there is also this short term single shot adjustments. These adjustments are made by applying the MAX_SINGLESHOT_ADJ (500ppm) scaling for an amount of time (offset_len) which would compensate for the offset. This style is difficult because we cannot precompute it and apply it to an entire tick. Instead it needs to be applied for just a specific amount of time which may be only a fraction of a tick. When we start talking about systems with irregular tick frequency (via virtualization, or tickless systems) it becomes even more problematic.

We would need to schedule a special tick like event at a certain time but otherwise I do not see a problem. Is there a requirement that these "specific amounts of time" are less than 1 ms? The timer hardware (such as the RTC clock) can generate an event in <200ns that could be used to change the scaling. For a tickless system we would need to have such scheduled events anyways.

Quoted 3 lines

If this can be fudged then it becomes less of an issue. Or at worse, we have to do two mult/shift operations on two "parts" of the time interval using different adjustments.

That looks troublesome. Better avoid that.

Quoted 3 lines

Its starting to look doable, but its not necessarily the simplest thing (for me at least). I'll put it on my list, but patches would be more then welcome.

I am still suffering from my limited NTP knowlege but will see what I can do about this.

#21 lore raw
Quoted 10 lines · Christoph Lameter
Quoted 8 lines · john stultz

However, there is also this short term single shot adjustments. These adjustments are made by applying the MAX_SINGLESHOT_ADJ (500ppm) scaling for an amount of time (offset_len) which would compensate for the offset. This style is difficult because we cannot precompute it and apply it to an entire tick. Instead it needs to be applied for just a specific amount of time which may be only a fraction of a tick. When we start talking about systems with irregular tick frequency (via virtualization, or tickless systems) it becomes even more problematic.

Quoted 6 lines

We would need to schedule a special tick like event at a certain time but otherwise I do not see a problem. Is there a requirement that these "specific amounts of time" are less than 1 ms? The timer hardware (such as the RTC clock) can generate an event in <200ns that could be used to change the scaling. For a tickless system we would need to have such scheduled events anyways.

Eh, I'd like to not to be dependent on event accuracy/frequency. Lets see if we can do it w/o scheduling events.

Quoted 5 lines
Quoted 3 lines

If this can be fudged then it becomes less of an issue. Or at worse, we have to do two mult/shift operations on two "parts" of the time interval using different adjustments.

That looks troublesome. Better avoid that.

Well, its not *that* bad. Similar to the ntp_scale() function, it would look something like:

if (interval <= offset_len)

	return (interval * singleshot_mult)>>shift;
else {
	cycle_t v1,v2;
	v1 = (offset_len * singleshot_mult)>>shift;
	v2 = (interval-offset_len)*adjusted_mult)>>shift;
	return v1+v2;
}

Where:
singleshot_mult = original_mult + ntp_adj + ss_mult
and
adjusted_mult = original_mult + ntp_adj

Quoted 6 lines
Quoted 3 lines

Its starting to look doable, but its not necessarily the simplest thing (for me at least). I'll put it on my list, but patches would be more then welcome.

I am still suffering from my limited NTP knowlege but will see what I can do about this.

:) Any added NTP knowledge would be great to add to the pool.

-john

#22 lore raw
Quoted 12 lines · john stultz

Well, its not *that* bad. Similar to the ntp_scale() function, it would look something like:

if (interval <= offset_len)

	return (interval * singleshot_mult)>>shift;
else {
	cycle_t v1,v2;
	v1 = (offset_len * singleshot_mult)>>shift;
	v2 = (interval-offset_len)*adjusted_mult)>>shift;
	return v1+v2;
}
Quoted 6 lines

Where:
singleshot_mult = original_mult + ntp_adj + ss_mult
and
adjusted_mult = original_mult + ntp_adj

Yuck. Do we support this kind of thing today? Support for periods of a tick or so is not an issue right?

#23 lore raw
Quoted 21 lines · Christoph Lameter
Quoted 12 lines · john stultz

Well, its not *that* bad. Similar to the ntp_scale() function, it would look something like:

if (interval <= offset_len)

	return (interval * singleshot_mult)>>shift;
else {
	cycle_t v1,v2;
	v1 = (offset_len * singleshot_mult)>>shift;
	v2 = (interval-offset_len)*adjusted_mult)>>shift;
	return v1+v2;
}
Quoted 6 lines

Where:
singleshot_mult = original_mult + ntp_adj + ss_mult
and
adjusted_mult = original_mult + ntp_adj

Quoted 2 lines

Yuck. Do we support this kind of thing today? Support for periods of a tick or so is not an issue right?

Well, ok, you're right. I got my wires twisted and misspoke. Today we really don't, since NTP adjustments only occur on tick boundaries. So yes, singleshot adjustments are in multiples of ticks right now. But we do assume ticks arrive at regular periods. If they don't, well, then we apply it for only one ticks worth, but we've lost a tick so we're wrong anyway.

The code above, however, can handle non-regular interrupt intervals, which is something I think we should assume will occur.

thanks
-john

#24 lore raw
Quoted 9 lines · john stultz

Well, ok, you're right. I got my wires twisted and misspoke. Today we really don't, since NTP adjustments only occur on tick boundaries. So yes, singleshot adjustments are in multiples of ticks right now. But we do assume ticks arrive at regular periods. If they don't, well, then we apply it for only one ticks worth, but we've lost a tick so we're wrong anyway.

The code above, however, can handle non-regular interrupt intervals, which is something I think we should assume will occur.

Then we might also assume that we are beyond the technology of the eighties and that there is some way that hardware can give us an interrupt at a certain point in the future that will allow us to change the scaling factor? I think its safe to not do this two scale stuff. Too complex for the hot code path anyways.

#25 lore raw
In reply to john stultz

On 8 Dec 2004 at 15:36, john stultz wrote:

[...]

Quoted 6 lines

Take a look at the adjtimex man page as well as the ntp.c file from the timeofday core patch. There are number of different types of adjustments that are made, possibly at the same time. Briefly, they are (to my understanding - I'm going off my notes from awhile ago):
o tick adjustments
how much time should pass in a _user_ tick

tick adjustments are considered obsolete, because if a lcok implementation (or hardware) is severly broken, users should reject using that stuff. Meaning: tick adjustments are ment to be set once in the life of a computer system. No continuous tuning.

Quoted 2 lines

o frequency adjustments
long term adjustment to correct for constant drift),

Actually, you are compensating for a "tick problem" on a smaller scale (constant part), and variations caused by temperature, voltage, and others (variable part).

Quoted 10 lines

o offset adjustments
additional ppm adjustment to correct for current offset from the ntp
server
o single shot offset adjustments
old style adjtime functionality

Tick, frequency and offset adjustments can be precalculated and summed to a single ppm adjustment. This is similar to the style of adjustment you propose directly onto the time source frequency values.

Quoted 6 lines

However, there is also this short term single shot adjustments. These adjustments are made by applying the MAX_SINGLESHOT_ADJ (500ppm) scaling for an amount of time (offset_len) which would compensate for the offset. This style is difficult because we cannot precompute it and apply it to an entire tick. Instead it needs to be applied for just a specific amount of time which may be only a fraction of a tick. When we

Yes, that's the "precise" variant of implementing it. Poor implementations are just accurate to one tick.

Quoted 16 lines

start talking about systems with irregular tick frequency (via virtualization, or tickless systems) it becomes even more problematic.

If this can be fudged then it becomes less of an issue. Or at worse, we have to do two mult/shift operations on two "parts" of the time interval using different adjustments.

Its starting to look doable, but its not necessarily the simplest thing (for me at least). I'll put it on my list, but patches would be more then welcome.

thanks
-john

#26 lore raw
Quoted 38 lines · Ulrich Windl

On 8 Dec 2004 at 15:36, john stultz wrote:

[...]

Quoted 6 lines

Take a look at the adjtimex man page as well as the ntp.c file from the timeofday core patch. There are number of different types of adjustments that are made, possibly at the same time. Briefly, they are (to my understanding - I'm going off my notes from awhile ago):
o tick adjustments
how much time should pass in a _user_ tick

tick adjustments are considered obsolete, because if a lcok implementation (or hardware) is severly broken, users should reject using that stuff. Meaning: tick adjustments are ment to be set once in the life of a computer system. No continuous tuning.

Quoted 2 lines

o frequency adjustments
long term adjustment to correct for constant drift),

Actually, you are compensating for a "tick problem" on a smaller scale (constant part), and variations caused by temperature, voltage, and others (variable part).

Quoted 10 lines

o offset adjustments
additional ppm adjustment to correct for current offset from the ntp
server
o single shot offset adjustments
old style adjtime functionality

Tick, frequency and offset adjustments can be precalculated and summed to a single ppm adjustment. This is similar to the style of adjustment you propose directly onto the time source frequency values.

Quoted 6 lines

However, there is also this short term single shot adjustments. These adjustments are made by applying the MAX_SINGLESHOT_ADJ (500ppm) scaling for an amount of time (offset_len) which would compensate for the offset. This style is difficult because we cannot precompute it and apply it to an entire tick. Instead it needs to be applied for just a specific amount of time which may be only a fraction of a tick. When we

Quoted 2 lines

Yes, that's the "precise" variant of implementing it. Poor implementations are just accurate to one tick.

Thanks for your knowledgeable clarifications. Its good to know someone out there deeply understands this stuff more then at a "this is what the code is doing, and I have my own interpretation as to why" level. :)

thanks again,
-john

#27 lore raw
In reply to john stultz

On 8 Dec 2004 at 11:58, john stultz wrote:

[...]

Quoted 4 lines

behavior of adjtimex(). We need to be able to implement the following adjustments within a single tick:

1. Adjust the frequency by 500ppm for 10usecs

What do you mean by "for 10usecs"?

Quoted 1 line

2. After that adjust the frequency by 30ppm for the rest of the tick.

I'm not sure what you are taling about: plain old adjtime() or the NTP kernel interface?

[...]

Quoted 2 lines

I may have asked this before, but w/ 32 bit mult and shifts, how granular can these adjustments be?

Independent of any bits, the precision should be up to 1ns for reading and setting the clock, and as a consequence you might provide internal fractional nanoseconds (if you want to have a truly stable nanosecond clock model). If we get this right, there will be peace in this area until the wires in a PC are significantly shorter than 30cm (I think this is how far the light goes in 1ns). ;-)

Quoted 3 lines

Also additional complications arise when we have multiple things (like cpufreq) playing with the timesource frequency values as well.

I see a bug difference between precise time keeping and "playing" with timesources.

Regards,
Ulrich

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

Points I'm glossing over for now:
====================================================

o Some arches (arm, for example) do not have high res timing hardware

Just a note: The ARM architecture is rather a bunch of multiple subarchitectures sharing the same instruction set but with wildly different sets of peripherals. Many of those ARM subarchitectures have high res (sub microsec) timer capabilities.

Nicolas

#29 lore raw
Quoted 11 lines · Nicolas Pitre
Quoted 4 lines · john stultz

Points I'm glossing over for now:
====================================================

o Some arches (arm, for example) do not have high res timing hardware

Just a note: The ARM architecture is rather a bunch of multiple subarchitectures sharing the same instruction set but with wildly different sets of peripherals. Many of those ARM subarchitectures have high res (sub microsec) timer capabilities.

Ah, I must have missed that when I looked over that code. I'll drop (or at least qualify) the incorrect reference.

Thanks for the correction!
-john