High-res-timers part 1 (core) take 23
Subsystems (2)
PATCH 1/3 High-res-timers part 1 (core) take 23
Just in case you might like high res timers...
Now for 2.5.54-bk1
Changes since last time:
Update the kernel base.
Prior changes:
Added stub macros to include/linux/hrtime.h to handle the no high res case. This was causing undefines when building a kernel with CONFIG_HIGH_RES = n.
Removed the SMP optimization in timer.c (folks objected to eliminating unneeded spin locks in favor of letting the compiler do it).
-------
This patch supplies the core changes to implement high resolution timers. Mostly it changes the timer list from the multi stage hash (or cascade) list to a single stage hash list. This change makes it easy to configure the list size for those who are concerned with performance. It also eliminates the "time out" for the cascade operation every 512 jiffies, thus eliminating possibly long preemption times. On input from Stephen Hemminger<[email protected]> the configuration of the timer list size is no longer presented as a configure option. The code can still be change (one line) to use larger or smaller lists.
It also adds a sub jiffie word to the timer structure to allow timers to exist between jiffies. However, to support the sub jiffie timers, work needs to be done in the platform code for each arch. The platform work for the i386 arch follows in part 2. To prevent requests from nonexistent code for sub jiffies stuff, these parts of this patch are disabled with the IF_HIGH_RES() macro which depends on CONFIG_HIGH_RES_TIMERS which will be defined for each platform as they supply the needed code.
With this patch applied, the system should boot and run much as it does prior to the patch. This patch depends on the POSIX clocks & timers patch in that it assumes the changes that patch made to timer.c to remove timer_t. This dependency can be removed if needed.
This patch as well as the POSIX clocks & timers patch is available on the project site:
http://sourceforge.net/projects/high-res-timers/
For those who want a change log, this version is changed only to follow the changes in the posix patch, which, was changed to use the new syscall restart stuff.
The 3 parts to the high res timers are:
*core The core kernel (i.e. platform independent) changes
i386 The high-res changes for the i386 (x86) platform
hrposix The changes to the POSIX clocks & timers patch to
use high-res timers
Please apply.
Signature · 5 lines
George Anzinger [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml
include/linux/hrtime.h +155 -0
@@ -0,0 +1,155 @@ +#ifndef _HRTIME_H +#define _HRTIME_H + +/* +* This file is the glue to bring in the platform stuff. +* We make it all depend on the CONFIG option so all archs +* will work as long as the CONFIG is not set. Once an +* arch defines the CONFIG, it had better have the +* asm/hrtime.h file in place. +*/ + +/* +* This gets filled in at init time, either static or dynamic. +* Someday this will be what NTP fiddles with. +* Do we need the scale here? I don't think so, as long as we +* do percentage offsets for NTP. +*/ +struct timer_conversion_bits { +unsigned long _arch_to_usec; +unsigned long _arch_to_nsec; +unsigned long _usec_to_arch; +unsigned long _nsec_to_arch; +long _cycles_per_jiffies; +unsigned long _arch_to_latch; +}; +extern struct timer_conversion_bits timer_conversion_bits; +/* +* The following four values are not used for machines +* without a TSC. For machines with a TSC they +* are caculated at boot time. They are used to +* calculate "cycles" to jiffies or usec. Don't get +* confused into thinking they are simple multiples or +* divisors, however. +*/ +#define arch_to_usec timer_conversion_bits._arch_to_usec +#define arch_to_nsec timer_conversion_bits._arch_to_nsec +#define usec_to_arch timer_conversion_bits._usec_to_arch +#define nsec_to_arch timer_conversion_bits._nsec_to_arch +#define cycles_per_jiffies timer_conversion_bits._cycles_per_jiffies +#define arch_to_latch timer_conversion_bits._arch_to_latch + +#include <linux/config.h> +#ifdef CONFIG_HIGH_RES_TIMERS +#include <asm/hrtime.h> +/* +* The schedule_next_int function is to be defined by the "arch" code +* when an "arch" is implementing the high-res part of POSIX timers. +* The actual function will be called with the offset in "arch" (parm 2) +* defined sub_jiffie units from the reference jiffie boundry (parm 1)to +* the next required sub_jiffie timer interrupt. This value will be -1 +* if the next timer interrupt should be the next jiffie value. The +* "arch" code must determine how far out the interrupt is, based on +* current jiffie, sub_jiffie time and set up the hardware to interrupt +* at that time. It is possible that the time will already have passed, +* in which case the function should return true (no interrupt is +* needed), otherwise the return should be 0. The third parameter is the +* "always" flag which says that the code needs an interrupt, even if the +* time has passed. In this case a "close" in time should be used to +* generate the required interrupt. The sub_jiffie interrupt +* should just call do_timer(). If the interrupt code ususally does stuff +* each jiffie, a flag should be kept by the jiffies update code to +* indicate that a new jiffie has started. This flag is to keep this code +* from being executed on the sub jiffie interrupt. +*/ +#ifndef schedule_next_int +#define schedule_next_int (s,d,a) 0 +#undef CONFIG_HIGH_RES_TIMERS +#endif // schedule_next_int +/* +* The sub_jiffie() macro should return the current time offset from the latest +* jiffie. This will be in "arch" defined units and is used to determine if +* a timer has expired. Since no sub_expire value will be used if "arch" +* has not defined the high-res package, 0 will work well here. +* +* In addition, to save time if there is no high-res package (or it is not +* configured), we define the sub expression for the run_timer_list. +*/ + +#ifndef sub_jiffie +#undef CONFIG_HIGH_RES_TIMERS +#define sub_jiffie () 0 +#endif // sub_jiffie + +/* +* The high_res_test() macro should set up a test mode that will do a +* worst case timer interrupt. I.e. it may be that a call to +* schedule_next_int() could return -1 indicating that the time has +* already expired. This macro says to set it so that schedule_next_int() +* will always set up a timer interrupt. This is used during init to +* calculate the worst case loop time from timer set up to int to +* the signal code. + +* high_res_end_test() cancels the above state and allows the no +* interrupt return from schedule_next_int() +*/ +#ifndef high_res_test +#define high_res_test () +#define high_res_end_test () +#endif + +#define IF_HIGH_RES (a) a + +#else /* CONFIG_HIGH_RES_TIMERS */ +#define IF_HIGH_RES (a) +#define nsec_to_arch_cycles (a) 0 + +#define discipline_timer (cpu) +#define compute_latch (calibration_result) +#endif /* CONFIG_HIGH_RES_TIMERS */ + +/* +* Here is an SMP helping macro... +*/ +#ifdef CONFIG_SMP +#define IF_SMP (a) a +#else +#define IF_SMP (a) +#endif +/* +* These should have been defined in the platform hrtimers.h +* If not (or HIGH_RES_TIMERS not configured) define the default. +*/ +#ifndef update_jiffies +extern u64 jiffies_64; +#define update_jiffies () (*(u64 *)&jiffies_64)++ +#endif +#ifndef new_jiffie +#define new_jiffie () 0 +#endif +#ifndef schedule_next_int +#define schedule_next_int (a,b,c) +#endif +/* +* If we included a high-res file, we may have gotten a more efficient +* u64/u32, u64%u32 routine. The one in div64.h actually handles a +* u64 result, something we don't need, and, since it is more expensive +* arch porters are encouraged to implement the div_long_long_rem(). +* +* int div_long_long_rem(u64 dividend,int divisor,int* remainder) +* which returns dividend/divisor. +* +* Here we provide default code for those who, for what ever reason, +* have not provided the above. +*/ +#ifndef div_long_long_rem +#include <asm/div64.h> + +#define div_long_long_rem (dividend,divisor,remainder) ({ \ + u64 result = dividend; \ + *remainder = do_div(result,divisor); \ + result; }) + +#endif /* ifndef div_long_long_rem */ + +#endif /* _HRTIME_H */
include/linux/time.h +22 -9
@@ -1,7 +1,7 @@#ifndef _LINUX_TIME_H#define _LINUX_TIME_H -#include <asm/param.h> +#include <linux/param.h> #include <linux/types.h> #ifndef _STRUCT_TIMESPEC
@@ -53,6 +53,19 @@#define NSEC_PER_USEC (1000L)#endif +/* Parameters used to convert the timespec values */ +#ifndef USEC_PER_SEC +#define USEC_PER_SEC (1000000L) +#endif + +#ifndef NSEC_PER_SEC +#define NSEC_PER_SEC (1000000000L) +#endif + +#ifndef NSEC_PER_USEC +#define NSEC_PER_USEC (1000L) +#endif +static __inline__unsigned long timespec_to_jiffies (struct timespec *value) {
@@ -61,16 +74,16 @@if (sec >= (MAX_JIFFY_OFFSET /HZ ))return MAX_JIFFY_OFFSET ; - nsec +=1000000000L /HZ -1 ; - nsec /=1000000000L /HZ ; + nsec +=NSEC_PER_SEC /HZ -1 ; + nsec /=NSEC_PER_SEC /HZ ;return HZ * sec + nsec; }static __inline__void -jiffies_to_timespec (unsigned long jiffies,struct timespec *value) +jiffies_to_timespec (unsigned long _jiffies,struct timespec *value) { - value->tv_nsec = (jiffies %HZ ) * (1000000000L /HZ ); - value->tv_sec = jiffies /HZ ; + value->tv_nsec = (_jiffies %HZ ) * (NSEC_PER_SEC /HZ ); + value->tv_sec = _jiffies /HZ ; }/* Same for "timeval" */
@@ -171,9 +184,9 @@#define ITIMER_VIRTUAL 1#define ITIMER_PROF 2 -struct itimerspec { -struct timespec it_interval;/* timer period */ -struct timespec it_value;/* timer expiration */ +struct itimerspec { +struct timespec it_interval;/* timer period */ +struct timespec it_value;/* timer expiration */ };struct itimerval {
include/linux/timer.h +4 -0
@@ -18,6 +18,7 @@unsigned long data;struct tvec_t_base_s *base; +long sub_expires; };#define TIMER_MAGIC 0x4b87ad6e
@@ -29,6 +30,7 @@ .base = NULL, \ .magic = TIMER_MAGIC, \ .lock = SPIN_LOCK_UNLOCKED, \ + .sub_expires = 0, \ }/***
@@ -41,6 +43,7 @@static inline void init_timer (struct timer_list * timer) { timer->base =NULL ; + timer->sub_expires =0 ; timer->magic =TIMER_MAGIC ;spin_lock_init (&timer->lock); }
@@ -64,6 +67,7 @@extern void add_timer_on (struct timer_list *timer,int cpu);extern int del_timer (struct timer_list * timer);extern int mod_timer (struct timer_list *timer,unsigned long expires); +extern void update_real_wall_time (void );#if CONFIG_SMP extern int del_timer_sync (struct timer_list * timer);
kernel/ksyms.c +4 -0
@@ -56,6 +56,7 @@#include <linux/mount.h> #include <linux/ptrace.h> #include <asm/checksum.h> +#include <linux/hrtime.h> #if defined(CONFIG_PROC_FS )#include <linux/proc_fs.h>
@@ -484,6 +485,9 @@#endif EXPORT_SYMBOL (jiffies);EXPORT_SYMBOL (jiffies_64); +#ifdef CONFIG_HIGH_RES_TIMERS +EXPORT_SYMBOL (timer_conversion_bits); +#endif EXPORT_SYMBOL (xtime);EXPORT_SYMBOL (do_gettimeofday);EXPORT_SYMBOL (do_settimeofday);
kernel/timer.c +278 -153
575 lines not on this page · raw message
Quoted 2 lines · george anzinger
Just in case you might like high res timers...
Now for 2.5.54-bk1
high-resend timers?
Bill
Quoted 7 lines · William Lee Irwin III
Quoted 2 lines · george anzinger
Just in case you might like high res timers...
Now for 2.5.54-bk1high-resend timers?
Gentle rains wash away mountains :)
Signature · 5 lines
George Anzinger [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml
Hi George,
On Fri, 3 Jan 2003, george anzinger wrote:
| Just in case you might like high res timers... | | Now for 2.5.54-bk1
Using exactly 2.5.54-bk1 + the POSIX clocks/timers + HRT patches, when I run the HRT support test suite (do_test), I'm getting intermittent (but often) spinlock BUGs. I've done this both without and with HIGH_RES_TIMERS enabled (i.e., different kernels built in the same source tree).
Details are below.
Yes, I expect to be able to run the test suite on a kernel that doesn't have HIGH_RES_TIMERS enabled...and to see the tests fail.
| ------- | This patch supplies the core changes to implement high | resolution timers. Mostly it changes the timer list from | the multi stage hash (or cascade) list to a single stage | hash list. This change makes it easy to configure the list | size for those who are concerned with performance. It also | eliminates the "time out" for the cascade operation every | 512 jiffies, thus eliminating possibly long preemption | times. On input from Stephen Hemminger<[email protected]> | the configuration of the timer list size is no longer | presented as a configure option. The code can still be | change (one line) to use larger or smaller lists. | | With this patch applied, the system should boot and run much | as it does prior to the patch. This patch depends on the | POSIX clocks & timers patch in that it assumes the changes | that patch made to timer.c to remove timer_t. This | dependency can be removed if needed.
Couple of questions, please:
1. With HIGH_RES_TIMERS enabled, are all timers high-res timers, or does the API allow for some non-high-res timers (regular/normal) and some high-res-timers?
2. With HIGH_RES_TIMERS enabled and no apps trying to use high-res timers, should I be able to detect any additional system overhead due to the HIGH_RES_TIMERS code?
Thanks,
Signature · 56 lines
~Randy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.5.54-bk1 + POSIX/HRT patches, with HIGH_RES_TIMERS=n:
kernel BUG at include/asm/spinlock.h:123!
invalid operand: 0000
CPU: 1
EIP: 0060:[<c0130260>] Not tainted
EFLAGS: 00010086
EIP is at good_timespec+0x80/0x180
eax: 0000000e ebx: ee5c471c ecx: f63d7100 edx: c03e2dec
esi: 00000000 edi: ed50df9c ebp: ed50df8c esp: ed50df78
ds: 007b es: 007b ss: 0068
Process timer_test (pid: 1970, threadinfo=ed50c000 task=f5b85800)
Stack: c0369bc0 c0130244 ed50c000 ed50df9c 00000000 ed50dfbc c01308d6 00000000
ed50df9c 00000082 c0150e73 f7193ce4 40016000 00000032 ed50c000 4001526c
00000000 bfff9778 c0109537 00000000 00000000 bfff90a8 4001526c 00000000
Call Trace:
[<c0130244>] good_timespec+0x64/0x180
[<c01308d6>] sys_timer_settime+0x16/0x180
[<c0150e73>] sys_read+0x33/0x40
[<c0109537>] system_call+0x7/0xb
Code: 0f 0b 7b 00 64 98 36 c0 59 58 8d b6 00 00 00 00 f0 fe 4b 08
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.5.54-bk1 + POSIX/HRT patches, with HIGH_RES_TIMERS=y:
kernel BUG at include/asm/spinlock.h:123!
invalid operand: 0000
CPU: 0
EIP: 0060:[<c01307a0>] Not tainted
EFLAGS: 00010086
EIP is at good_timespec+0x80/0x180
eax: 0000000e ebx: f68c589c ecx: f6f96300 edx: c03e3aec
esi: 87000007 edi: f6919ed8 ebp: f6919ec8 esp: f6919eb4
ds: 007b es: 007b ss: 0068
Process 2timer_test (pid: 1940, threadinfo=f6918000 task=f6bc6b80)
Stack: c036a860 c0130784 00000023 f6919f30 00000001 f6919ee4 c01300b8 87000007
f6919ed8 00000086 00000023 f6919f30 f6919efc c0127117 f6919f30 f6919f30
f6bc7130 f6918000 f6919f1c c012829a f6bc7130 f6919f30 f6bc7130 f6919fc4
Call Trace:
[<c0130784>] good_timespec+0x64/0x180
[<c01300b8>] schedule_next_timer+0x18/0x50
[<c0127117>] __dequeue_signal+0x87/0xa0
[<c012829a>] notify_parent+0x7a/0x290
[<c01092cd>] handle_signal+0x5d/0xd0
[<c0222e5f>] copy_to_user+0x2f/0x40
[<c01285ea>] do_no_restart_syscall+0x10a/0x240
[<c0109582>] work_resched+0x13/0x15
Code: 0f 0b 7b 00 04 a5 36 c0 59 58 8d b6 00 00 00 00 f0 fe 4b 08
###Quoted 16 lines · Randy.Dunlap
Hi George,
On Fri, 3 Jan 2003, george anzinger wrote:
| Just in case you might like high res timers... | | Now for 2.5.54-bk1Using exactly 2.5.54-bk1 + the POSIX clocks/timers + HRT patches, when I run the HRT support test suite (
do_test), I'm getting intermittent (but often) spinlock BUGs. I've done this both without and withHIGH_RES_TIMERSenabled (i.e., different kernels built in the same source tree).
Details are below.
Quoted 2 lines
Yes, I expect to be able to run the test suite on a kernel that doesn't have
HIGH_RES_TIMERSenabled...and to see the tests fail.
Actually the tests should detect low res and adapt reasonably well. The one problem(?)is that the clock_nanosleep test finds the kernel timing bug introduced by not pushing the wall clock 1/HZ on a tick. (The current 2.5 kernel actually pushes time a bit more than this introducing a difference in the rate of time passing between the wall clock an the jiffies. This causes timers to expire early which is a big NO-NO, standard wise.)
This is most easily seen by doing:
time sleep 60
and noticing that it completes in less than 60 seconds, even with the scheduling overhead.
Quoted 20 lines
| ------- | This patch supplies the core changes to implement high | resolution timers. Mostly it changes the timer list from | the multi stage hash (or cascade) list to a single stage | hash list. This change makes it easy to configure the list | size for those who are concerned with performance. It also | eliminates the "time out" for the cascade operation every | 512 jiffies, thus eliminating possibly long preemption | times. On input from Stephen Hemminger<[email protected]> | the configuration of the timer list size is no longer | presented as a configure option. The code can still be | change (one line) to use larger or smaller lists. | | With this patch applied, the system should boot and run much | as it does prior to the patch. This patch depends on the | POSIX clocks & timers patch in that it assumes the changes | that patch made to timer.c to remove timer_t. This | dependency can be removed if needed.
Quoted 5 lines
Couple of questions, please:
1. With
HIGH_RES_TIMERSenabled, are all timers high-res timers, or does the API allow for some non-high-res timers (regular/normal) and some high-res-timers?
Actually you are asking a couple of questions here. First, the API (POSIX clocks & timers) is extended in the high res patch to add two new clocks:CLOCK_REALTIME_HRCLOCK_MONOTONIC_HR
Only calls using these clocks will generate the high-res overhead (usually an interrupt is required that is somewhere between two 1/HZ interrupts).
On the other hand, the timers list handling code was expanded to handle sub jiffie values (but only the above API can set these values) so there is a small (very small) addition in timer handling to at least test that the sub jiffie is zero.
Normal timers, i.e. setitimer, select, time_schedule, etc. all use the normal low res timers so nothing changes here.
Quoted 4 lines
2. With
HIGH_RES_TIMERSenabled and no apps trying to use high-res timers, should I be able to detect any additional system overhead due to theHIGH_RES_TIMERScode?
You would need a very sensitive tool to detect any additional overhead. Still, as I mentioned above, there is some.
Quoted 2 lines
Thanks,
And thank you for the bug report.
-g
Quoted 62 lines
--
~Randy~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.5.54-bk1 + POSIX/HRT patches, with HIGH_RES_TIMERS=n:kernel BUG at
include/asm/spinlock.h:123!invalid operand: 0000 CPU: 1 EIP: 0060:[<c0130260>] Not tainted EFLAGS: 00010086EIP is at
good_timespec+0x80/0x180
eax:0000000eebx:ee5c471cecx:f63d7100edx:c03e2dec
esi: 00000000 edi:ed50df9cebp:ed50df8cesp:ed50df78
ds: 007b es: 007b ss: 0068
Processtimer_test(pid: 1970, threadinfo=ed50c000 task=f5b85800)Stack: c0369bc0 c0130244 ed50c000 ed50df9c 00000000 ed50dfbc c01308d6 00000000 ed50df9c 00000082 c0150e73 f7193ce4 40016000 00000032 ed50c000 4001526c 00000000 bfff9778 c0109537 00000000 00000000 bfff90a8 4001526c 00000000 Call Trace: [<c0130244>] good_timespec+0x64/0x180 [<c01308d6>] sys_timer_settime+0x16/0x180 [<c0150e73>] sys_read+0x33/0x40 [<c0109537>] system_call+0x7/0xb Code: 0f 0b 7b 00 64 98 36 c0 59 58 8d b6 00 00 00 00 f0 fe 4b 08~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2.5.54-bk1 + POSIX/HRT patches, with HIGH_RES_TIMERS=y:kernel BUG at
include/asm/spinlock.h:123!invalid operand: 0000 CPU: 0 EIP: 0060:[<c01307a0>] Not tainted EFLAGS: 00010086EIP is at
good_timespec+0x80/0x180
eax:0000000eebx:f68c589cecx:f6f96300edx:c03e3aec
esi: 87000007 edi:f6919ed8ebp:f6919ec8esp:f6919eb4
ds: 007b es: 007b ss: 0068
Process 2timer_test (pid: 1940, threadinfo=f6918000 task=f6bc6b80)Stack: c036a860 c0130784 00000023 f6919f30 00000001 f6919ee4 c01300b8 87000007 f6919ed8 00000086 00000023 f6919f30 f6919efc c0127117 f6919f30 f6919f30 f6bc7130 f6918000 f6919f1c c012829a f6bc7130 f6919f30 f6bc7130 f6919fc4 Call Trace: [<c0130784>] good_timespec+0x64/0x180 [<c01300b8>] schedule_next_timer+0x18/0x50 [<c0127117>] __dequeue_signal+0x87/0xa0 [<c012829a>] notify_parent+0x7a/0x290 [<c01092cd>] handle_signal+0x5d/0xd0 [<c0222e5f>] copy_to_user+0x2f/0x40 [<c01285ea>] do_no_restart_syscall+0x10a/0x240 [<c0109582>] work_resched+0x13/0x15 Code: 0f 0b 7b 00 04 a5 36 c0 59 58 8d b6 00 00 00 00 f0 fe 4b 08###
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 5 lines
George Anzinger [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Preemption patch: http://www.kernel.org/pub/linux/kernel/people/rml