Dynamic Tick: Enabling longer sleep times on 32-bit
Subsystems (3)
cover Dynamic Tick: Enabling longer sleep times on 32-bit
From: Jon Hunter <[email protected]>
This is a resend of the patch series shown here:
http://www.spinics.net/lists/kernel/msg891029.html
This patch series has been updated based on the feedback received and rebased against the current kernel.
This patch series ensures that the wrapping of the clocksource will not be missed if the kernel sleeps for longer periods and allows 32-bit machines to sleep for longer than 2.15 seconds.
Jon Hunter (2):
Dynamic Tick: Prevent clocksource wrapping during idle Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15
seconds
include/linux/clockchips.h | 6 ++-- include/linux/clocksource.h | 2 + include/linux/time.h | 1 + kernel/hrtimer.c | 2 +- kernel/time/clockevents.c | 10 ++++---- kernel/time/clocksource.c | 47 +++++++++++++++++++++++++++++++++++ kernel/time/tick-oneshot.c | 2 +- kernel/time/tick-sched.c | 57 ++++++++++++++++++++++++++++++++---------- kernel/time/timekeeping.c | 11 ++++++++ kernel/time/timer_list.c | 4 +- 10 files changed, 116 insertions(+), 26 deletions(-)
PATCH 1/2 Dynamic Tick: Prevent clocksource wrapping during idle
From: Jon Hunter <[email protected]>
The dynamic tick allows the kernel to sleep for periods longer than a single tick. This patch prevents that the kernel from sleeping for a period longer than the maximum time that the current clocksource can count. This ensures that the kernel will not lose track of time. This patch adds a function called "clocksource_max_deferment()" that calculates the maximum time the kernel can sleep for a given clocksource and function called "timekeeping_max_deferment()" that returns maximum time the kernel can sleep for the current clocksource.
--- include/linux/clocksource.h | 2 + include/linux/time.h | 1 + kernel/time/clocksource.c | 47 +++++++++++++++++++++++++++++++++++ kernel/time/tick-sched.c | 57 ++++++++++++++++++++++++++++++++---------- kernel/time/timekeeping.c | 11 ++++++++ 5 files changed, 104 insertions(+), 14 deletions(-)
include/linux/clocksource.h +2 -0
@@ -151,6 +151,7 @@ extern u64 timecounter_cyc2time(struct timecounter *tc,* @mult: cycle to nanosecond multiplier (adjusted by NTP) * @mult_orig: cycle to nanosecond multiplier (unadjusted by NTP) * @shift: cycle to nanosecond divisor (power of two) +* @max_idle_ns: max idle time permitted by the clocksource (nsecs) * @flags: flags describing special properties * @vread: vsyscall based read * @resume: resume function for the clocksource, if necessary
@@ -171,6 +172,7 @@ struct clocksource {u32 mult;u32 mult_orig;u32 shift; +s64 max_idle_ns;unsigned long flags;cycle_t (*vread)(void);void (*resume)(void );
include/linux/time.h +1 -0
@@ -145,6 +145,7 @@ extern void monotonic_to_bootbased(struct timespec *ts);extern struct timespec timespec_trunc (struct timespec t,unsigned gran);extern int timekeeping_valid_for_hres (void ); +extern s64 timekeeping_max_deferment (void );extern void update_wall_time (void );extern void update_xtime_cache (u64 nsec);
kernel/time/clocksource.c +47 -0
59 lines not on this page · raw message
kernel/time/tick-sched.c +43 -14
88 lines not on this page · raw message
kernel/time/timekeeping.c +11 -0
@@ -278,6 +278,17 @@ int timekeeping_valid_for_hres(void) }/** +* timekeeping_max_deferment - Returns max time the clocksource can be deferred +* +* IMPORTANT: Caller must observe xtime_lock via read_seqbegin/read_seqretry +* to ensure that the clocksource does not change! +*/ +s64 timekeeping_max_deferment (void ) +{ +return clock->max_idle_ns; +} + +/** * read_persistent_clock - Return time in seconds from the persistent clock. * * Weak dummy function for arches that do not yet support it.
Signature · 1 line
1.6.0.4
PATCH 2/2 Dynamic Tick: Allow 32-bit machines to sleep for more than 2.15 seconds
From: Jon Hunter <[email protected]>
In the dynamic tick code, "max_delta_ns" (member of the "clock_event_device" structure) represents the maximum sleep time that can occur between timer events in nanoseconds.
The variable, "max_delta_ns", is defined as an unsigned long which is a 32-bit integer for 32-bit machines and a 64-bit integer for 64-bit machines (if -m64 option is used for gcc). The value of max_delta_ns is set by calling the function "clockevent_delta2ns()" which returns a maximum value of LONG_MAX. For a 32-bit machine LONG_MAX is equal to 0x7fffffff and in nanoseconds this equates to ~2.15 seconds. Hence, the maximum sleep time for a 32-bit machine is ~2.15 seconds, where as for a 64-bit machine it will be many years.
This patch changes the type of max_delta_ns to be "unsigned long long" instead of "unsigned long" so that this variable is a 64-bit type for both 32-bit and 64-bit machines. It also changes the maximum value returned by clockevent_delta2ns() to LLONG_MAX. Hence this allows a 32-bit machine to sleep for longer than ~2.15 seconds. Please note that this patch also changes "min_delta_ns" to be "unsigned long long" too and although this is probably unnecessary, it makes the patch simpler.
--- include/linux/clockchips.h | 6 +++--- kernel/hrtimer.c | 2 +- kernel/time/clockevents.c | 10 +++++----- kernel/time/tick-oneshot.c | 2 +- kernel/time/timer_list.c | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-)
include/linux/clockchips.h +3 -3
@@ -77,8 +77,8 @@ enum clock_event_nofitiers {struct clock_event_device {const char *name;unsigned int features; -unsigned long max_delta_ns; -unsigned long min_delta_ns; +unsigned long long max_delta_ns; +unsigned long long min_delta_ns;unsigned long mult;int shift;int rating;
@@ -116,7 +116,7 @@ static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec, }/* Clock event layer functions */ -extern unsigned long clockevent_delta2ns (unsigned long latch, +extern unsigned long long clockevent_delta2ns (unsigned long latch,struct clock_event_device *evt);extern void clockevents_register_device (struct clock_event_device *dev);
kernel/hrtimer.c +1 -1
@@ -1255,7 +1255,7 @@ hrtimer_interrupt_hanging(struct clock_event_device *dev, force_clock_reprogram =1 ; dev->min_delta_ns = (unsigned long )try_time.tv64 *3 ;printk (KERN_WARNING "hrtimer: interrupt too slow, " -"forcing clock min delta to %lu ns\n" , dev->min_delta_ns); +"forcing clock min delta to %llu ns\n" , dev->min_delta_ns); }/* * High resolution timer interrupt
kernel/time/clockevents.c +5 -5
@@ -37,10 +37,10 @@ static DEFINE_SPINLOCK(clockevents_lock);* * Math helper, returns latch value converted to nanoseconds (bound checked) */ -unsigned long clockevent_delta2ns (unsigned long latch, +unsigned long long clockevent_delta2ns (unsigned long latch,struct clock_event_device *evt) { -u64 clc = ((u64 ) latch << evt->shift); +unsigned long long clc = ((unsigned long long ) latch << evt->shift);if (unlikely (!evt->mult)) { evt->mult =1 ;
@@ -50,10 +50,10 @@ unsigned long clockevent_delta2ns(unsigned long latch,do_div (clc, evt->mult);if (clc <1000 ) clc =1000 ; -if (clc >LONG_MAX ) - clc =LONG_MAX ; +if (clc >LLONG_MAX ) + clc =LLONG_MAX ; -return (unsigned long ) clc; +return clc; }EXPORT_SYMBOL_GPL (clockevent_delta2ns);
kernel/time/tick-oneshot.c +1 -1
@@ -50,7 +50,7 @@ int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires, dev->min_delta_ns += dev->min_delta_ns >>1 ;printk (KERN_WARNING -"CE: %s increasing min_delta_ns to %lu nsec\n" , +"CE: %s increasing min_delta_ns to %llu nsec\n" , dev->name ? dev->name :"?" , dev->min_delta_ns <<1 );
kernel/time/timer_list.c +2 -2
@@ -204,8 +204,8 @@ print_tickdevice(struct seq_file *m, struct tick_device *td, int cpu)return ; }SEQ_printf (m,"%s\n" , dev->name); -SEQ_printf (m," max_delta_ns: %lu\n" , dev->max_delta_ns); -SEQ_printf (m," min_delta_ns: %lu\n" , dev->min_delta_ns); +SEQ_printf (m," max_delta_ns: %llu\n" , dev->max_delta_ns); +SEQ_printf (m," min_delta_ns: %llu\n" , dev->min_delta_ns);SEQ_printf (m," mult: %lu\n" , dev->mult);SEQ_printf (m," shift: %d\n" , dev->shift);SEQ_printf (m," mode: %d\n" , dev->mode);
Signature · 1 line
1.6.0.4