Introduce CLOCK_BOOTTIME (v2)
Subsystems (2)
cover Introduce CLOCK_BOOTTIME (v2)
After some discussions with Jamie Lokier about some of the drawbacks of CLOCK_MONOTONIC not incrementing during suspend (see http://www.spinics.net/lists/linux-fsdevel/msg40272.html),
I wanted to see if we couldn't provide a new clockid that would allow applications that wanted to be aware of time passing during suspend without having to deal with the inconsistencies of CLOCK_REALTIME caused by calls to settimeofday.
So this patchset introduces CLOCK_BOOTTIME, which is identical to CLOCK_MONOTONIC, but includes any time spent in suspend.
This second version of the patch uses a array table for the clock_id->hrtimer_base convesion instead of the switch.
Thomas: I've not been able to do much comparision at the asm level for ppc and arm (while I have cross tools for compiling, I don't have objdump for those arches on my build box) between this and the previous switch code. But on x86, size breakdown looks like:
Original:
text data bss dec hex filename
15517512 1899836 891200 18308548 1175dc4 vmlinux
Switch:
text data bss dec hex filename
15517716 1899804 891200 18308720 1175e70 vmlinux
Table:
text data bss dec hex filename
15517873 1899868 891200 18308941 1175f4d vmlinux
So that's not as great, but remvoing the WARN_ON added in hrtimer_clockid_to_base drops it down quite a bit:
Table-nowarn:
text data bss dec hex filename
15517617 1899804 891200 18308621 1175e0d vmlinux
Looking at the asm, the change from the original is limited (as expected)to: hrtimer_get_res, hrtimer_init, __hrtimer_start_range_ns, where the difference is a few extra movs, and and hrtimers_init, where there's the extra table initialization work.
If you have any tips for more specific comprisions please let me know. Diffing objdump output doesn't always make it super clear as to what changed.
Jamie: On platforms that don't implement read_persistent_clock, your issue would still be present, but fixing that is on my list. Other then that issue, does this seem to address your concern?
Arve: I believe CLOCK_BOOTTIME would be sufficient for what Android is using as elapsedRealtime() or ANDROID_ALARM_ELAPSED_REALTIME. If not please let me know why.
thanks
-john
John Stultz (2):
[RFC] hrtimers: extend hrtimer base code to handle more then 2
clockids
[RFC] hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix
interface
include/linux/hrtimer.h | 8 ++++- include/linux/time.h | 4 ++ kernel/hrtimer.c | 63 +++++++++++++++++++++++++++-------- kernel/posix-timers.c | 16 ++++++++- kernel/time/timekeeping.c | 79 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 152 insertions(+), 18 deletions(-)
Signature · 1 line
1.7.3.2.146.gca209
RFC PATCH 1/2 hrtimers: extend hrtimer base code to handle more then 2 clockids
The hrtimer code is written mainly with CLOCK_REALTIME and CLOCK_MONOTONIC in mind. These are clockids 0 and 1 resepctively. However, if we are to introduce any new hrtimer bases, using new clockids, we have to skip the cputimers (clockids 2,3) as well as other clockids that may not impelement timers.
This patch adds a little bit of indirection between the clockid and the base, so that we can extend the base by one when we add a new clockid at number 7 or so.
--- include/linux/hrtimer.h | 6 ++++- kernel/hrtimer.c | 48 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 14 deletions(-)
include/linux/hrtimer.h +5 -1
@@ -149,7 +149,11 @@ struct hrtimer_clock_base {#endif }; -#define HRTIMER_MAX_CLOCK_BASES 2 +enum hrtimer_base_type { +HRTIMER_BASE_REALTIME , +HRTIMER_BASE_MONOTONIC , +HRTIMER_MAX_CLOCK_BASES , +};/* * struct hrtimer_cpu_base - the per cpu clock bases
kernel/hrtimer.c +35 -13
113 lines not on this page · raw message
Signature · 1 line
1.7.3.2.146.gca209
RFC PATCH 2/2 hrtimers: Add CLOCK_BOOTTIME clockid, hrtimerbase and posix interface
In reply to John StultzCLOCK_MONOTONIC stops while the system is in suspend. This is because to applications system suspend is invisible. However, there is a growing set of applications that are wanting to be suspend-aware, but do not want to deal with the complicatoins of CLOCK_REALTIME (which might jump around if settimeofday is called).
For these applications, I propose a new clockid: CLOCK_BOOTTIME. CLOCK_BOOTTIME is idential to CLOCK_MONOTONIC, except it also includes any time spent in suspend.
This patch adds the new CLOCK_BOOTTIME clockid, as well as the infrastructure needed to support hrtimers against it, and the wiring to expose it out via the posix interface.
--- include/linux/hrtimer.h | 2 + include/linux/time.h | 4 ++ kernel/hrtimer.c | 15 +++++++- kernel/posix-timers.c | 16 ++++++++- kernel/time/timekeeping.c | 79 ++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 112 insertions(+), 4 deletions(-)
include/linux/hrtimer.h +2 -0
@@ -152,6 +152,7 @@ struct hrtimer_clock_base {enum hrtimer_base_type {HRTIMER_BASE_REALTIME ,HRTIMER_BASE_MONOTONIC , +HRTIMER_BASE_BOOTTIME ,HRTIMER_MAX_CLOCK_BASES , };
@@ -313,6 +314,7 @@ static inline int hrtimer_is_hres_active(struct hrtimer *timer)extern ktime_t ktime_get (void );extern ktime_t ktime_get_real (void ); +extern ktime_t ktime_get_boottime (void ); DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
include/linux/time.h +4 -0
@@ -126,6 +126,8 @@ unsigned long get_seconds(void);struct timespec current_kernel_time (void );struct timespec __current_kernel_time (void );/* does not take xtime_lock */ struct timespec __get_wall_to_monotonic (void );/* does not take xtime_lock */ +extern struct timespec __get_sleep_time (void );/* does not take xtime_lock */ +struct timespec get_monotonic_coarse (void );#define CURRENT_TIME (current_kernel_time())
@@ -160,6 +162,7 @@ extern void getnstimeofday(struct timespec *tv);extern void getrawmonotonic (struct timespec *ts);extern void getboottime (struct timespec *ts);extern void monotonic_to_bootbased (struct timespec *ts); +extern void get_monotonic_boottime (struct timespec *ts);extern struct timespec timespec_trunc (struct timespec t,unsigned gran);extern int timekeeping_valid_for_hres (void );
@@ -290,6 +293,7 @@ struct itimerval {#define CLOCK_MONOTONIC_RAW 4#define CLOCK_REALTIME_COARSE 5#define CLOCK_MONOTONIC_COARSE 6 +#define CLOCK_BOOTTIME 7/* * The IDs of various hardware clocks:
kernel/hrtimer.c +13 -2
@@ -73,6 +73,11 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = .get_time = &ktime_get, .resolution = KTIME_LOW_RES, }, + { + .index = CLOCK_BOOTTIME, + .get_time = &ktime_get_boottime, + .resolution = KTIME_LOW_RES, + }, } };
@@ -93,21 +98,26 @@ static inline int hrtimer_clockid_to_base(clockid_t clock_id)*/ static void hrtimer_get_softirq_time (struct hrtimer_cpu_base *base) { -ktime_t xtim, tomono; -struct timespec xts, tom; +ktime_t xtim, tomono, sleep; +struct timespec xts, tom, slp;unsigned long seq;do { seq =read_seqbegin (&xtime_lock); xts =__current_kernel_time (); tom =__get_wall_to_monotonic (); + slp =__get_sleep_time (); }while (read_seqretry (&xtime_lock, seq)); xtim =timespec_to_ktime (xts); tomono =timespec_to_ktime (tom); + sleep =timespec_to_ktime (slp); base->clock_base[HRTIMER_BASE_REALTIME ].softirq_time = xtim; base->clock_base[HRTIMER_BASE_MONOTONIC ].softirq_time =ktime_add (xtim, tomono); + base->clock_base[HRTIMER_BASE_BOOTTIME ].softirq_time = +ktime_add (base->clock_base[HRTIMER_BASE_MONOTONIC ].softirq_time, + sleep); }/*
@@ -1727,6 +1737,7 @@ void __init hrtimers_init(void) hrtimer_clock_to_base_table[i] =-1 ; hrtimer_clock_to_base_table[CLOCK_REALTIME ] =HRTIMER_BASE_REALTIME ; hrtimer_clock_to_base_table[CLOCK_MONOTONIC ] =HRTIMER_BASE_MONOTONIC ; + hrtimer_clock_to_base_table[CLOCK_BOOTTIME ] =HRTIMER_BASE_BOOTTIME ;hrtimer_cpu_notify (&hrtimers_nb, (unsigned long )CPU_UP_PREPARE , (void *)(long )smp_processor_id ());
kernel/posix-timers.c +15 -1
@@ -240,7 +240,7 @@ static int posix_ktime_get_ts(clockid_t which_clock, struct timespec *tp) }/* -* Get monotonic time for posix timers +* Get monotonic-raw time for posix timers */ static int posix_get_monotonic_raw (clockid_t which_clock,struct timespec *tp) {
@@ -267,6 +267,14 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp *tp =ktime_to_timespec (KTIME_LOW_RES );return 0 ; } + +static int posix_get_boottime (const clockid_t which_clock,struct timespec *tp) +{ +get_monotonic_boottime (tp); +return 0 ; +} + +/* * Initialize everything, well, just everything in Posix clocks/timers ;) */
@@ -301,12 +309,18 @@ static __init int init_posix_timers(void) .timer_create = no_timer_create, .nsleep = no_nsleep, }; +struct k_clock clock_boottime = { + .clock_getres = hrtimer_get_res, + .clock_get = posix_get_boottime, + .clock_set = do_posix_clock_nosettime, + };register_posix_clock (CLOCK_REALTIME , &clock_realtime);register_posix_clock (CLOCK_MONOTONIC , &clock_monotonic);register_posix_clock (CLOCK_MONOTONIC_RAW , &clock_monotonic_raw);register_posix_clock (CLOCK_REALTIME_COARSE , &clock_realtime_coarse);register_posix_clock (CLOCK_MONOTONIC_COARSE , &clock_monotonic_coarse); +register_posix_clock (CLOCK_BOOTTIME , &clock_boottime); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, SLAB_PANIC,
kernel/time/timekeeping.c +78 -1
91 lines not on this page · raw message
Signature · 1 line
1.7.3.2.146.gca209