Skip to contentSkip to conversation

High Resolution Timers on IA64

PATCH [email protected] 1 participant 1 message Dec 18, 2003 Documentation

lore
Share & tools
lore
#0 lore raw

Hello,

Here is the first version of IA64 support for the High Resolution Timers Project.

Let me sum up briefly the project for those not familiar with it: The aim of the High Resolution Timers Project is to get rid of the current 1 jiffy resolution of the kernel for the timers (and sleeps) to reach a resolution closer to the one provided by the hardware. Grossly speaking, this feature is implemented by scheduling additional clock ticks exactly at the time the next high resolution timer is due to expire.
For instance, on our 1Ghz IA64 we measured timers with a resolution a bit smaller than 10 microseconds (while for now it's around 1 millisecond). Interestingly, on a x86 with the same frequency we obtained a resolution around 30 microseconds.

In order to try the patch you'll have to get a vanilla kernel 2.6.0-test5 and then apply in order those patches:
*official ia64 patch
*official HRT patch for 2.6.0-test5 (available at sourceforge.net/projects/high-res-timers, it doesn't apply cleanly, don't worry) *2.6.0-t5-hrt_syscalls.diff (it's available in the latest kernels, just correct some typos)
*hrtimers-2.6.0-test5-compil-20031213.patch (an update for the official HRT patch) *hrtimers-ia64-2.6.0-test5-20031218.patch (the real thing)
In the config you'll have to enable HRT and select ITC as the source clock. After the reboot you should get a HRT actived kernel :-)

To use the feature from the kernel (in a driver for instance) you just create timers normally and modify a new field "sub_jiffies". This field contains a value between 0 and arch_cycles_per_jiffy - 1.
On the userland side you access the high resolution timers through the new POSIX RT syscalls clock_*() and timer_*(), using the clock_id CLOCK_REALTIME_HR. As there isn't yet support for those syscalls in the glibc you have to link with a small special library. Get and apply hrtimers-support-2.5.62-1.0.patch (from sourceforge.net/projects/high-res-timers, don't worry about the old version number) and then apply hrtimers-support-ia64-2.6.0-t5-20031212.patch . Then you compile your code with -lposixtime .

Note about this release:
Please consider this code as alpha. It shouldn't do bad things on your machine but there is a known bug that causes some timers to still have a resolution around one jiffy, especially on CPUs other than the time-keeper. Any comment, reports of success or failure would be very welcome!

Thanks to Nick Pollitt for the original work on 2.4 and Alexandre Hessemann for the current support.
Eric Piel

PS: All the attached patches can also be found at http://pieleric.free.fr/hrt/ .

arch/i386/kernel/apic.c +1 -1
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/arch/i386/kernel/apic.c linux-2.6.0-test5-hrt/arch/i386/kernel/apic.c
@@ -1051,7 +1051,7 @@
 						per_cpu(prof_counter, cpu);
 		}
 
-		discipline_timer(cpu);
+		discipline_timer(cpu); //XXX this argument is very weird, the function expects a number of cycles elapsed!
 
 #ifdef CONFIG_SMP
 		update_process_times(user_mode(regs));
arch/i386/kernel/time.c +2 -2
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/arch/i386/kernel/time.c linux-2.6.0-test5-hrt/arch/i386/kernel/time.c
@@ -319,7 +319,7 @@
         /*
          * If time is already passed, just return saying so.
          */
-        if (sub_jiff_offset < 0)
+        if (sub_jiff_offset <= 0)
                 return 1;
 
 	__last_was_long = arch_cycles_per_jiffy == sub_jiffie_in;
@@ -332,7 +332,7 @@
 {
         start_PIT();
 }
-#endif /* CONFIG__APM */
+#endif /* CONFIG_APM */
 #endif /* CONFIG_HIGH_RES_TIMERS */
 
 
arch/i386/kernel/timers/hrtimer_pm.c +0 -1
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/arch/i386/kernel/timers/hrtimer_pm.c linux-2.6.0-test5-hrt/arch/i386/kernel/timers/hrtimer_pm.c
@@ -189,7 +189,6 @@
 #ifndef CONFIG_SMP
 			cpu_khz = cpufreq_scale(cpu_khz_ref, 
 						ref_freq, freq->new);
-		}
 #endif
 	}
 
arch/i386/kernel/timers/hrtimer_tsc.c +2 -2
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/arch/i386/kernel/timers/hrtimer_tsc.c linux-2.6.0-test5-hrt/arch/i386/kernel/timers/hrtimer_tsc.c
@@ -16,7 +16,7 @@
 extern int x86_udelay_tsc;
 extern spinlock_t i8253_lock;
 
-
+static int use_tsc;
 
 /* Cached *multiplier* to convert TSC counts to microseconds.
  * (see the equation below).
@@ -163,6 +163,7 @@
 #ifndef CONFIG_SMP
 		cpu_khz_ref = cpu_khz;
 #endif
+	}
 
 	if((val == CPUFREQ_PRECHANGE && (freq->old < freq->new)) ||
 	   (val == CPUFREQ_POSTCHANGE && (freq->old > freq->new))){
@@ -195,7 +196,6 @@
 						ref_freq, freq->new);
 #endif
 		}
-	}
 
 	return 0;
 }
include/asm-i386/hrtime.h +2 -6
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/include/asm-i386/hrtime.h linux-2.6.0-test5-hrt/include/asm-i386/hrtime.h
@@ -32,7 +32,7 @@
 #include <asm/io.h>
 #include <linux/sc_math.h>	/* scaling math routines */
 #include <asm/delay.h>
-#include <asm/smp.h>
+#include <linux/smp.h>
 #include <linux/timex.h>	/* for LATCH */
 #include <asm/percpu.h>
 /*
@@ -177,10 +177,6 @@
 
 extern int _schedule_next_int(unsigned long jiffie_f, long sub_jiffie_in);
 extern int _schedule_jiffies_int(unsigned long jiffie_f);
-extern unsigned int volatile latch_reload;
-
-EXTERN int jiffies_intr;
-
 
 /*
  * Now go ahead and include the clock specific file 586/386/acpi
@@ -352,7 +348,7 @@
 	spin_unlock(&i8253_lock);
 	return;
 }
-#endif				//  ! CONFIG_X86_LOCAL_APIC
+#endif				//  ! USE_APIC_TIMERS
 /*
  * Time out for a discussion.  Because the PIT and TSC (or the PIT and
  * pm timer) may drift WRT each other, we need a way to get the jiffie
include/asm-i386/hrtime-M586.h +2 -0
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/include/asm-i386/hrtime-M586.h linux-2.6.0-test5-hrt/include/asm-i386/hrtime-M586.h
@@ -38,6 +38,8 @@
 };
 #endif
 
+extern struct timer_opts hrtimer_tsc;
+
 extern inline unsigned long
 quick_get_cpuctr(void)
 {
include/asm-i386/hrtime-Macpi.h +1 -0
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/include/asm-i386/hrtime-Macpi.h linux-2.6.0-test5-hrt/include/asm-i386/hrtime-Macpi.h
@@ -38,6 +38,7 @@
 #define SIZE_MASK 0xffffff
 
 extern int acpi_pm_tmr_address;
+extern struct timer_opts hrtimer_pm;
 
 extern inline unsigned long
 quick_get_cpuctr(void)
include/asm-i386/mach-visws/do_timer.h +1 -1
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/include/asm-i386/mach-visws/do_timer.h linux-2.6.0-test5-hrt/include/asm-i386/mach-visws/do_timer.h
@@ -8,7 +8,7 @@
 	/* Clear the interrupt */
 	co_cpu_write(CO_CPU_STAT,co_cpu_read(CO_CPU_STAT) & ~CO_STAT_TIMEINTR);
 
-ifdef CONFIG_HIGH_RES_TIMERS
+#ifdef CONFIG_HIGH_RES_TIMERS
  
         { 
 		long arch_cycles = get_arch_cycles(jiffies);
include/linux/posix-timers.h +44 -0

54 lines not on this page · raw message

include/linux/sc_math.h +2 -2
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/include/linux/sc_math.h linux-2.6.0-test5-hrt/include/linux/sc_math.h
@@ -251,7 +251,7 @@
  */
 static inline long mpy_sc32(long a, long b)
 {
-        return (mpy1 * mpy2) >> 32);
+        return ((a * b) >> 32);
 }
 /*
  * X = (a/b)<<32 or more precisely x = (a<<32)/b
@@ -287,7 +287,7 @@
 /* x = (aa * bb) >> N */
 
 
-#define mpy_sc_n(N,aa,bb) ((aa) * (bb)) >> N)
+#define mpy_sc_n(N,aa,bb) (((aa) * (bb)) >> N)
 
 /* x = (aa << N / bb)  */
 #define div_sc_n(N,aa,bb) (SC_n((N), (aa)) / (bb))
kernel/timer.c +12 -14
diff -urN -X /home/eric/dontdiff -x '*.dg' linux-2.6.0-test5-hrt.orig/kernel/timer.c linux-2.6.0-test5-hrt/kernel/timer.c
@@ -98,7 +98,7 @@
  	 * must be cli-ed when calling this
  	 */
   	unsigned long expires = timer->expires;
- 	IF_HIGH_RES(int sub_expires = timer->sub_expires;)
+ 	IF_HIGH_RES(long sub_expires = timer->sub_expires;)
  		int indx;
  	struct list_head *pos,*list_start;
   
@@ -119,7 +119,7 @@
  	}
  			
  	indx =	expires & NEW_TVEC_MASK;
- 	if ((expires - base->timer_jiffies) <= NEW_TVEC_SIZE) {
+ 	if ((expires - base->timer_jiffies) < NEW_TVEC_SIZE) {
 #ifdef CONFIG_HIGH_RES_TIMERS
  		unsigned long jiffies_f;
   		/*
@@ -155,7 +155,8 @@
  		if ( expires == (jiffies_f = base->timer_jiffies) && 
  		     list_start->next == &timer->entry &&
  		     (base->running_timer == NULL)) {
- 			schedule_hr_timer_int(jiffies_f, sub_expires);
+ 			if (schedule_hr_timer_int(jiffies_f, sub_expires))
+				run_local_timers();
  		}
 #else
  		pos = (&base->tv[indx])->next;
@@ -433,8 +434,7 @@
 #else
 	while ((long)(jiffies - base->timer_jiffies) >= 0) {
 #endif
- 
-		struct list_head *head, *curr;
+		struct list_head *head;
 		head = base->tv + (base->timer_jiffies & NEW_TVEC_MASK);
 		/*
 		 * Note that we never move "head" but continue to
@@ -443,13 +443,12 @@
 		 * function call below.
 		 */
 repeat:
-		curr = head->next;
-		if (curr != head) {
+		if (!list_empty(head)) {
 			void (*fn)(unsigned long);
 			unsigned long data;
 			struct timer_list *timer;
 
-			timer = list_entry(curr, struct timer_list, entry);
+			timer = list_entry(head->next, struct timer_list, entry);
 #ifdef CONFIG_HIGH_RES_TIMERS
 			/*
 			 * This would be simpler if we never got behind
@@ -472,7 +471,7 @@
 				list_del(&timer->entry);
 				timer->base = NULL;
 				timer->entry.next = timer->entry.prev = NULL;
-				base->running_timer = timer;
+				set_running_timer(base, timer);
 				spin_unlock_irq(&base->lock);
 				fn(data);
 				spin_lock_irq(&base->lock);
@@ -483,7 +482,7 @@
 				/*
 				 * The new timer list is not always emptied
 				 * here as it contains:
-				 * a.) entries (list size)^N*jiffies out and
+				 * a.) entries (list size)*N jiffies out and
 				 * b.) entries that match in jiffies, but have
 				 *     sub_expire times further out than now.
 				 */
@@ -513,16 +512,15 @@
 	 * This allows zero time inserts as well as sub_jiffie values in
 	 * the current jiffie.
 	 */
-	--base->timer_jiffies;
 #ifdef CONFIG_HIGH_RES_TIMERS
-	if (!sub_jiff && schedule_jiffies_int(jiffies_f)) {
+	--base->timer_jiffies; //XXX needed when not HRT?
+	if ((sub_jiff == -1) && schedule_jiffies_int(jiffies_f)) {
 		spin_unlock_irq(&base->lock);
 		spin_lock_irq(&base->lock);
 		goto run_timer_list_again;
 	}
 #endif
-	base->running_timer = NULL;
-
+ 	set_running_timer(base, NULL);
 
 	spin_unlock_irq(&base->lock);
 }
include/asm-ia64/unistd.h renamed from include/asm-ia64/unistd.h.~1~ +11 -11
@@ -237,17 +237,17 @@
 #define __NR_epoll_wait			1245
 #define __NR_restart_syscall		1246
 #define __NR_semtimedop			1247
-#define __NR_sys_timer_create		1248
-#define __NR_sys_timer_settime		1249
-#define __NR_sys_timer_gettime		1250
-#define __NR_sys_timer_getoverrun	1251
-#define __NR_sys_timer_delete		1252
-#define __NR_sys_clock_settime		1253
-#define __NR_sys_clock_gettime		1254
-#define __NR_sys_clock_getres		1255
-#define __NR_sys_clock_nanosleep	1256
-#define __NR_sys_fstatfs64		1257
-#define __NR_sys_statfs64		1258
+#define __NR_timer_create		1248
+#define __NR_timer_settime		1249
+#define __NR_timer_gettime		1250
+#define __NR_timer_getoverrun		1251
+#define __NR_timer_delete		1252
+#define __NR_clock_settime		1253
+#define __NR_clock_gettime		1254
+#define __NR_clock_getres		1255
+#define __NR_clock_nanosleep		1256
+#define __NR_fstatfs64			1257
+#define __NR_statfs64			1258
 
 #ifdef __KERNEL__
 
Documentation/high-res-timers/lib/syscall_timer.c +64 -1

74 lines not on this page · raw message

arch/ia64/Kconfig +43 -0

49 lines not on this page · raw message

arch/ia64/kernel/time.c +77 -0

130 lines not on this page · raw message

include/asm-ia64/hrtime.h +93 -0

93 lines not on this page · raw message

include/asm-ia64/hrtime-itc.h +137 -0

137 lines not on this page · raw message

include/linux/sc_math.h +1 -1
diff -urwN -X patches/dontdiff -x Documentation 2.6.0-t5-hrt.orig/include/linux/sc_math.h 2.6.0-t5-hrt.clean/include/linux/sc_math.h
@@ -300,7 +300,7 @@
  * Warning, this will do an exception if X overflows.
  * Well, it would if done in asm, this code just truncates..
  */
-#define div_long_long_rem(a,b,c) div_ll_X_l_rem(a, b, c)
+//#define div_long_long_rem(a,b,c) div_ll_X_l_rem(a, b, c)
 
 /* x = divs / div; *rem = divs % div; */
 static inline unsigned long div_ll_X_l_rem(unsigned long divs, 
kernel/timer.c +5 -4
diff -urwN -X patches/dontdiff -x Documentation 2.6.0-t5-hrt.orig/kernel/timer.c 2.6.0-t5-hrt.clean/kernel/timer.c
@@ -425,11 +425,11 @@
  * folks using short timers causing then to loop forever...
  * If sub_jiffie_f > cycles_per_jiffies we will just clean out all
  * timers at jiffies_f and quit.  We get the rest on the next go round.
-	while ( unlikely(sub_jiffie_f >= cycles_per_jiffies)){
-		sub_jiffie_f -= cycles_per_jiffies;
+*/	while (unlikely(sub_jiffie_f >= arch_cycles_per_jiffy)) {
+		sub_jiffie_f -= arch_cycles_per_jiffy;
 		jiffies_f++;
 	}
-*/
+
 	while ((long)(jiffies_f - base->timer_jiffies) >= 0) {
 #else
 	while ((long)(jiffies - base->timer_jiffies) >= 0) {
@@ -513,8 +513,9 @@
 	 * the current jiffie.
 	 */
 #ifdef CONFIG_HIGH_RES_TIMERS
-	--base->timer_jiffies; //XXX needed when not HRT?
+	--base->timer_jiffies;
 	if ((sub_jiff == -1) && schedule_jiffies_int(jiffies_f)) {
+		//this can't work as long as we don't increase jiffies_f at the beginning
 		spin_unlock_irq(&base->lock);
 		spin_lock_irq(&base->lock);
 		goto run_timer_list_again;