Introduce CLOCK_REALTIME_COARSE
Subsystems (2)
RFC PATCH 1/1 Introduce CLOCK_REALTIME_COARSE
Hey all,
After talking with some application writers who want very fast, but not
fine-grained timestamps, I decided to try to implement a new clock_ids to clock_gettime(): CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE which returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like the acpi_pm clocksource), and we can even use the vdso clock_gettime() method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
This isn't a new idea, I know Ingo pushed a patch (see commit 5899a0f044f3c80e9f7262ec5bc7164773a4c28e) a little while ago that made the vsyscall gettimeofday() return coarse grained time when the vsyscall64 sysctrl was set to 2. However this affects all applications on a system.
With this method, applications can choose the proper speed/granularity trade-off for themselves.
This is a first pass on this implementation, and while I did test it, the box I tested it with did not have a glibc new enough to utilize the vdso clock_gettime(), so there may still be issues there. I'll find a newer box for testing shortly.
Any thoughts or feedback will be appreciated!
thanks
-john
--- arch/x86/vdso/vclock_gettime.c | 39 ++++++++++++++++++++++++++++++++++++--- include/linux/time.h | 3 +++ kernel/posix-timers.c | 33 +++++++++++++++++++++++++++++++++ kernel/time/timekeeping.c | 19 +++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-)
arch/x86/vdso/vclock_gettime.c +36 -3
@@ -83,14 +83,47 @@ notrace static noinline int do_monotonic(struct timespec *ts)return 0 ; } +notrace static noinline int do_realtime_coarse(struct timespec *ts) +{ +unsigned long seq; +do { + seq =read_seqbegin (>od->lock); + ts->tv_sec = gtod->wall_time_sec; + ts->tv_nsec = gtod->wall_time_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +return 0 ; +} + +notrace static noinline int do_monotonic_coarse(struct timespec *ts) +{ +unsigned long seq, ns, secs; +do { + seq =read_seqbegin (>od->lock); + secs = gtod->wall_time_sec; + ns = gtod->wall_time_nsec; + secs += gtod->wall_to_monotonic.tv_sec; + ns += gtod->wall_to_monotonic.tv_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +vset_normalized_timespec (ts, secs, ns); +return 0 ; +} + notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) { -if (likely (gtod->sysctl_enabled && gtod->clock.vread)) +if (likely (gtod->sysctl_enabled))switch (clock) {case CLOCK_REALTIME : -return do_realtime (ts); +if (likely (gtod->clock.vread)) +return do_realtime (ts); +break ;case CLOCK_MONOTONIC : -return do_monotonic (ts); +if (likely (gtod->clock.vread)) +return do_monotonic (ts); +break ; +case CLOCK_REALTIME_COARSE : +return do_realtime_coarse (ts); +case CLOCK_MONOTONIC_COARSE : +return do_monotonic_coarse (ts); }return vdso_fallback_gettime (clock, ts); }
include/linux/time.h +3 -0
@@ -108,6 +108,7 @@ void timekeeping_init(void);unsigned long get_seconds (void );struct timespec current_kernel_time (void ); +struct timespec get_monotonic_coarse (void );#define CURRENT_TIME (current_kernel_time())#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
@@ -225,6 +226,8 @@ struct itimerval {#define CLOCK_PROCESS_CPUTIME_ID 2#define CLOCK_THREAD_CPUTIME_ID 3#define CLOCK_MONOTONIC_RAW 4 +#define CLOCK_REALTIME_COARSE 5 +#define CLOCK_MONOTONIC_COARSE 6/* * The IDs of various hardware clocks:
kernel/posix-timers.c +33 -0
@@ -236,6 +236,25 @@ static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)return 0 ; } + +static int posix_get_realtime_coarse (clockid_t which_clock,struct timespec *tp) +{ + *tp =current_kernel_time (); +return 0 ; +} + +static int posix_get_monotonic_coarse (clockid_t which_clock, +struct timespec *tp) +{ + *tp =get_monotonic_coarse (); +return 0 ; +} + +int posix_get_coarse_res (const clockid_t which_clock,struct timespec *tp) +{ + *tp =ktime_to_timespec (KTIME_LOW_RES ); +return 0 ; +}/* * Initialize everything, well, just everything in Posix clocks/timers ;) */
@@ -255,10 +274,24 @@ static __init int init_posix_timers(void) .clock_set = do_posix_clock_nosettime, .timer_create = no_timer_create, }; +struct k_clock clock_realtime_coarse = { + .clock_getres = hrtimer_get_res, + .clock_get = posix_get_realtime_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + }; +struct k_clock clock_monotonic_coarse = { + .clock_getres = hrtimer_get_res, + .clock_get = posix_get_monotonic_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + };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); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, SLAB_PANIC,
kernel/time/timekeeping.c +19 -0
@@ -602,3 +602,22 @@ struct timespec current_kernel_time(void)return now; }EXPORT_SYMBOL (current_kernel_time); + +struct timespec get_monotonic_coarse (void ) +{ +struct timespec now, mono; +unsigned long seq; + +do { + seq =read_seqbegin (&xtime_lock); + + now = xtime_cache; + mono = wall_to_monotonic; + }while (read_seqretry (&xtime_lock, seq)); + +set_normalized_timespec (&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); +return now; +} +EXPORT_SYMBOL (get_monotonic_coarse); +
Quoted 22 lines · john stultz
kernel/posix-timers.c +18
-0+static int posix_get_realtime_coarse (clockid_t which_clock,struct timespec *tp) +{ + *tp =current_kernel_time (); +return 0 ; +} + +static int posix_get_monotonic_coarse (clockid_t which_clock, +struct timespec *tp) +{ + *tp =get_monotonic_coarse (); +return 0 ; +} + +int posix_get_coarse_res (const clockid_t which_clock,struct timespec *tp) +{ + *tp =ktime_to_timespec (KTIME_LOW_RES ); +return 0 ; +}/* * Initialize everything, well, just everything in Posix clocks/timers ;) */
kernel/posix-timers.c static __init int init_posix_timers(void) · line 278
Quoted 5 lines
kernel/posix-timers.c +2
-0@@ -255,10 +274,24 @@ static __init int init_posix_timers(void) .clock_set = do_posix_clock_nosettime, .timer_create = no_timer_create, }; +struct k_clock clock_realtime_coarse = { + .clock_getres = hrtimer_get_res,
shouldn't that be posix_get_coarse_res ?
kernel/posix-timers.c static __init int init_posix_timers(void) · line 284
Quoted 6 lines
kernel/posix-timers.c +6
-0+ .clock_get = posix_get_realtime_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + }; +struct k_clock clock_monotonic_coarse = { + .clock_getres = hrtimer_get_res,
ditto
kernel/posix-timers.c static __init int init_posix_timers(void) · line 288
Quoted 4 lines
kernel/posix-timers.c +4
-0+ .clock_get = posix_get_monotonic_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + };
Looks good otherwise.
tglx
Quoted 22 lines · Thomas Gleixner
Quoted 21 lines · john stultz
(quoted diff) +18
-0+static int posix_get_realtime_coarse(clockid_t which_clock, struct timespec *tp) +{ + *tp = current_kernel_time(); + return 0; +} + +static int posix_get_monotonic_coarse(clockid_t which_clock, + struct timespec *tp) +{ + *tp = get_monotonic_coarse(); + return 0; +} + +int posix_get_coarse_res(const clockid_t which_clock, struct timespec *tp) +{ + *tp = ktime_to_timespec(KTIME_LOW_RES); + return 0; +} /* * Initialize everything, well, just everything in Posix clocks/timers ;) */
Quoted 8 lines
Review static __init int init_posix_timers(void) · line 278
Quoted 6 lines
(quoted diff) +2
-0@@ -255,10 +274,24 @@ static __init int init_posix_timers(void) .clock_set = do_posix_clock_nosettime, .timer_create = no_timer_create, }; + struct k_clock clock_realtime_coarse = { + .clock_getres = hrtimer_get_res,shouldn't that be
posix_get_coarse_res?
Doh! Thanks for pointing that out.
Fixed patch below.
-john
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new clock_ids to clock_gettime(): CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE which returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like the acpi_pm clocksource), and we can even use the vdso clock_gettime() method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
This isn't a new idea, I know Ingo pushed a patch (see commit 5899a0f044f3c80e9f7262ec5bc7164773a4c28e) a little while ago that made the vsyscall gettimeofday() return coarse grained time when the vsyscall64 sysctrl was set to 2. However this affects all applications on a system.
With this method, applications can choose the proper speed/granularity trade-off for themselves.
This is a first pass on this implementation, and while I did test it, the box I tested it with did not have a glibc new enough to utilize the vdso clock_gettime(), so there may still be issues there. I'll find a newer box for testing shortly.
Any thoughts or feedback will be appreciated!
thanks
-john
---
arch/x86/vdso/vclock_gettime.c +36 -3
@@ -83,14 +83,47 @@ notrace static noinline int do_monotonic(struct timespec *ts)return 0 ; } +notrace static noinline int do_realtime_coarse(struct timespec *ts) +{ +unsigned long seq; +do { + seq =read_seqbegin (>od->lock); + ts->tv_sec = gtod->wall_time_sec; + ts->tv_nsec = gtod->wall_time_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +return 0 ; +} + +notrace static noinline int do_monotonic_coarse(struct timespec *ts) +{ +unsigned long seq, ns, secs; +do { + seq =read_seqbegin (>od->lock); + secs = gtod->wall_time_sec; + ns = gtod->wall_time_nsec; + secs += gtod->wall_to_monotonic.tv_sec; + ns += gtod->wall_to_monotonic.tv_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +vset_normalized_timespec (ts, secs, ns); +return 0 ; +} + notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) { -if (likely (gtod->sysctl_enabled && gtod->clock.vread)) +if (likely (gtod->sysctl_enabled))switch (clock) {case CLOCK_REALTIME : -return do_realtime (ts); +if (likely (gtod->clock.vread)) +return do_realtime (ts); +break ;case CLOCK_MONOTONIC : -return do_monotonic (ts); +if (likely (gtod->clock.vread)) +return do_monotonic (ts); +break ; +case CLOCK_REALTIME_COARSE : +return do_realtime_coarse (ts); +case CLOCK_MONOTONIC_COARSE : +return do_monotonic_coarse (ts); }return vdso_fallback_gettime (clock, ts); }
include/linux/time.h +3 -0
@@ -108,6 +108,7 @@ void timekeeping_init(void);unsigned long get_seconds (void );struct timespec current_kernel_time (void ); +struct timespec get_monotonic_coarse (void );#define CURRENT_TIME (current_kernel_time())#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
@@ -225,6 +226,8 @@ struct itimerval {#define CLOCK_PROCESS_CPUTIME_ID 2#define CLOCK_THREAD_CPUTIME_ID 3#define CLOCK_MONOTONIC_RAW 4 +#define CLOCK_REALTIME_COARSE 5 +#define CLOCK_MONOTONIC_COARSE 6/* * The IDs of various hardware clocks:
kernel/posix-timers.c +33 -0
@@ -236,6 +236,25 @@ static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)return 0 ; } + +static int posix_get_realtime_coarse (clockid_t which_clock,struct timespec *tp) +{ + *tp =current_kernel_time (); +return 0 ; +} + +static int posix_get_monotonic_coarse (clockid_t which_clock, +struct timespec *tp) +{ + *tp =get_monotonic_coarse (); +return 0 ; +} + +int posix_get_coarse_res (const clockid_t which_clock,struct timespec *tp) +{ + *tp =ktime_to_timespec (KTIME_LOW_RES ); +return 0 ; +}/* * Initialize everything, well, just everything in Posix clocks/timers ;) */
@@ -255,10 +274,24 @@ static __init int init_posix_timers(void) .clock_set = do_posix_clock_nosettime, .timer_create = no_timer_create, }; +struct k_clock clock_realtime_coarse = { + .clock_getres = posix_get_coarse_res, + .clock_get = posix_get_realtime_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + }; +struct k_clock clock_monotonic_coarse = { + .clock_getres = posix_get_coarse_res, + .clock_get = posix_get_monotonic_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + };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); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, SLAB_PANIC,
kernel/time/timekeeping.c +19 -0
@@ -602,3 +602,22 @@ struct timespec current_kernel_time(void)return now; }EXPORT_SYMBOL (current_kernel_time); + +struct timespec get_monotonic_coarse (void ) +{ +struct timespec now, mono; +unsigned long seq; + +do { + seq =read_seqbegin (&xtime_lock); + + now = xtime_cache; + mono = wall_to_monotonic; + }while (read_seqretry (&xtime_lock, seq)); + +set_normalized_timespec (&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); +return now; +} +EXPORT_SYMBOL (get_monotonic_coarse); +
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
kernel/time/timekeeping.c struct timespec current_kernel_time(void) · line 623
Quoted 8 lines
me_lock, seq));kernel/time/timekeeping.c +7
-0+ +set_normalized_timespec (&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); +return now; +} +EXPORT_SYMBOL (get_monotonic_coarse); +
why does this need to be exported ?
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 13 lines · Arjan van de Ven
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
Quoted 3 lines
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
On the hardware side to make this happen we'd need a platform that has:
- cheap, high-res, cross-cpu synced, clocksource
- cheap, high-res, clockevents
Maybe power64, sparc64 and s390x qualify, but certainly nothing on x86 does.
Furthermore, on the software side we'd need a few modifications, such as doing lazy accounting for things like u/s-time which currently rely on the tick and moving the load-balancing into a hrtimer.
Also, even with the above done, we'd probably want to tinker with the clockevent/hrtimer code and possibly use a second per-cpu hardware timer for the scheduler, since doing the whole hrtimer rb-tree dance for every context switch is simply way too expensive.
But even with all that manged, there's still other bits that rely on the tick -- RCU being one of the more interesting ones.
Quoted 30 lines · Peter Zijlstra
Quoted 13 lines · Arjan van de Ven
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.Quoted 3 lines
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
On the hardware side to make this happen we'd need a platform that has:
- cheap, high-res, cross-cpu synced, clocksource
- cheap, high-res, clockeventsMaybe power64, sparc64 and s390x qualify, but certainly nothing on x86 does.
Furthermore, on the software side we'd need a few modifications, such as doing lazy accounting for things like u/s-time which currently rely on the tick and moving the load-balancing into a hrtimer.
Quoted 7 lines
Also, even with the above done, we'd probably want to tinker with the clockevent/hrtimer code and possibly use a second per-cpu hardware timer for the scheduler, since doing the whole hrtimer rb-tree dance for every context switch is simply way too expensive.
But even with all that manged, there's still other bits that rely on the tick -- RCU being one of the more interesting ones.
On alternative to the tick is to inform RCU of each transition to/from userspace, so that RCU would treat user-mode execution as it currently does dyntick-idle state. If there is -never- to be any scheduling-clock interrupts, then RCU would need to also know about transitions to/from the idle loop -- which happens automatically if CONFIG_NO_HZ, of course.
But I expect that there would be some additional excitement elsewhere... And given the large number of transitions to/from userspace, getting all of them noted in the RCU case might be non-trivial as well.
Thanx, Paul
Quoted 18 lines · On Mon, 20 Jul 2009 13:17:02 +0200 Peter Zijlstra <[email protected]> wrote:
Quoted 13 lines · Arjan van de Ven
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.Quoted 3 lines
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
Quoted 8 lines
On the hardware side to make this happen we'd need a platform that has:
- cheap, high-res, cross-cpu synced, clocksource
- cheap, high-res, clockeventsMaybe power64, sparc64 and s390x qualify, but certainly nothing on x86 does.
the x86 on my desk disagrees.
Quoted 3 lines
Furthermore, on the software side we'd need a few modifications, such as doing lazy accounting for things like u/s-time which currently rely on the tick and moving the load-balancing into a hrtimer.
I thought the load balancer no longer runs as a timer.. but I could well be wrong.
Quoted 7 lines
Also, even with the above done, we'd probably want to tinker with the clockevent/hrtimer code and possibly use a second per-cpu hardware timer for the scheduler, since doing the whole hrtimer rb-tree dance for every context switch is simply way too expensive.
But even with all that manged, there's still other bits that rely on the tick -- RCU being one of the more interesting ones.
we need to at least keep our options open to go there, since even the early measurements (iirc from Andrea 5 years ago) of the 1 KHz time show that it has a real performance impact, as much as 1%. While we may not need to switch over RIGHT NOW, adding more dependencies on this timer is just not a good idea...
Signature · 3 lines
Arjan van de Ven Intel Open Source Technology Centre For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 6 lines · Arjan van de Ven
Quoted 2 lines
Maybe power64, sparc64 and s390x qualify, but certainly nothing on x86 does.
the x86 on my desk disagrees.
From what I know even nehalem doesn't have fully synced tscs when your
machine is large enough, and the timers are still a tad expensive.
Maybe your desktop is next-gen? 't would be nice to finally have an x86 that has usable clock and timer hardware.
I'm sure tglx would be pleasantly surprised :-)
Quoted 6 lines
Quoted 3 lines
Furthermore, on the software side we'd need a few modifications, such as doing lazy accounting for things like u/s-time which currently rely on the tick and moving the load-balancing into a hrtimer.
I thought the load balancer no longer runs as a timer.. but I could well be wrong.
It doesn't but it does need wakeup kicks, which are currently done from the tick.
And I'm not at all disagreeing that we want the tick gone, I'm just pointing out there's some challenges ahead still ;-)
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Arjan van de Ven
Quoted 2 lines
Maybe power64, sparc64 and s390x qualify, but certainly nothing on x86 does.
the x86 on my desk disagrees.
From what I know even nehalem doesn't have fully synced tscs when your
machine is large enough, and the timers are still a tad expensive.
All single-socket Nehalem boards have synced TSCs, and some multi-socket Nehalem boards have synced TSCs, notably Intel's. I don't know if you can safely rely on the latter, though.
Quoted 2 lines
Maybe your desktop is next-gen? 't would be nice to finally have an x86 that has usable clock and timer hardware.
x86 CPUs with the ARAT feature ("always running APIC timer") will let you use the local APIC even in deep C-states. I don't know about the overhead of the local APIC.
- Josh Triplett
Quoted 13 lines · Arjan van de Ven
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
Quoted 3 lines
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
So it does require some sort of periodic interval. But the granularity is probably flexible, although I'm not sure it would be of much use if the granularity gets to be lower then 100hz.
While being 100% tickless, even when non-idle would be nice, there will be some need for timekeeping events to prevent clocksource counters from wrapping, and to do accurate NTP steering.
Even so, the value we're exporting in this patch is the xtime_cache, which is updated every tick. This is currently used in file timestamping, so if we go 100% tickless, we'll have to change the file timestamping to use the actual CLOCK_REALTIME clock_id, which requires a possibly slow hardware read and would likely hurt fs performance.
So this patch doesn't so much tie us to having a tick or periodic event any more the the fs timestamping does.
Quoted 10 lines
kernel/time/timekeeping.c struct timespec current_kernel_time(void) · line 623
Quoted 8 lines
me_lock, seq));kernel/time/timekeeping.c +7
-0+ +set_normalized_timespec (&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); +return now; +} +EXPORT_SYMBOL (get_monotonic_coarse); +why does this need to be exported ?
Doesn't. Thanks for noticing!
thanks
-john
Quoted 15 lines · john stultz
Quoted 13 lines · Arjan van de Ven
Review
Quoted 8 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
me_lock, seq));(quoted diff) +7
-0+ + set_normalized_timespec(&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); + return now; +} +EXPORT_SYMBOL(get_monotonic_coarse); +why does this need to be exported ?
Quoted 1 line
Doesn't. Thanks for noticing!
Here's an updated version of the patch that fixes the following:
1) Remove EXPORT_SYMBOL for get_monotonic_coarse
2) The x86_64 vsyscall was using xtime instead of xtime_cache for the calculations. This would cause the resolution to be actually NTP_INTERVAL_FREQ (possibly 2/sec) instead of HZ.
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new clock_ids to clock_gettime(): CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE which returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like the acpi_pm clocksource), and we can even use the vdso clock_gettime() method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
This isn't a new idea, I know Ingo pushed a patch (see commit 5899a0f044f3c80e9f7262ec5bc7164773a4c28e) a little while ago that made the vsyscall gettimeofday() return coarse grained time when the vsyscall64 sysctrl was set to 2. However this affects all applications on a system.
With this method, applications can choose the proper speed/granularity trade-off for themselves.
Any thoughts or feedback will be appreciated!
thanks
-john
arch/x86/include/asm/vgtod.h +1 -0
@@ -21,6 +21,7 @@ struct vsyscall_gtod_data {u32 shift; } clock;struct timespec wall_to_monotonic; +struct timespec wall_time_coarse; }; extern struct vsyscall_gtod_data __vsyscall_gtod_data __section_vsyscall_gtod_data;
arch/x86/kernel/vsyscall_64.c +1 -0
@@ -87,6 +87,7 @@ void update_vsyscall(struct timespec *wall_time, struct clocksource *clock) vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec; vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec; vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic; + vsyscall_gtod_data.wall_time_coarse =__current_kernel_time ();write_sequnlock_irqrestore (&vsyscall_gtod_data.lock, flags); }
arch/x86/vdso/vclock_gettime.c +36 -3
@@ -86,14 +86,47 @@ notrace static noinline int do_monotonic(struct timespec *ts)return 0 ; } +notrace static noinline int do_realtime_coarse(struct timespec *ts) +{ +unsigned long seq; +do { + seq =read_seqbegin (>od->lock); + ts->tv_sec = gtod->wall_time_coarse.tv_sec; + ts->tv_nsec = gtod->wall_time_coarse.tv_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +return 0 ; +} + +notrace static noinline int do_monotonic_coarse(struct timespec *ts) +{ +unsigned long seq, ns, secs; +do { + seq =read_seqbegin (>od->lock); + secs = gtod->wall_time_coarse.tv_sec; + ns = gtod->wall_time_coarse.tv_nsec; + secs += gtod->wall_to_monotonic.tv_sec; + ns += gtod->wall_to_monotonic.tv_nsec; + }while (unlikely (read_seqretry (>od->lock, seq))); +vset_normalized_timespec (ts, secs, ns); +return 0 ; +} + notrace int __vdso_clock_gettime(clockid_t clock, struct timespec *ts) { -if (likely (gtod->sysctl_enabled && gtod->clock.vread)) +if (likely (gtod->sysctl_enabled))switch (clock) {case CLOCK_REALTIME : -return do_realtime (ts); +if (likely (gtod->clock.vread)) +return do_realtime (ts); +break ;case CLOCK_MONOTONIC : -return do_monotonic (ts); +if (likely (gtod->clock.vread)) +return do_monotonic (ts); +break ; +case CLOCK_REALTIME_COARSE : +return do_realtime_coarse (ts); +case CLOCK_MONOTONIC_COARSE : +return do_monotonic_coarse (ts); }return vdso_fallback_gettime (clock, ts); }
include/linux/time.h +4 -0
@@ -109,6 +109,8 @@ extern int timekeeping_suspended;unsigned long get_seconds (void );struct timespec current_kernel_time (void ); +struct timespec __current_kernel_time (void );/* does not hold xtime_lock */ +struct timespec get_monotonic_coarse (void );#define CURRENT_TIME (current_kernel_time())#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
@@ -241,6 +243,8 @@ struct itimerval {#define CLOCK_PROCESS_CPUTIME_ID 2#define CLOCK_THREAD_CPUTIME_ID 3#define CLOCK_MONOTONIC_RAW 4 +#define CLOCK_REALTIME_COARSE 5 +#define CLOCK_MONOTONIC_COARSE 6/* * The IDs of various hardware clocks:
kernel/posix-timers.c +33 -0
@@ -236,6 +236,25 @@ static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec *tp)return 0 ; } + +static int posix_get_realtime_coarse (clockid_t which_clock,struct timespec *tp) +{ + *tp =current_kernel_time (); +return 0 ; +} + +static int posix_get_monotonic_coarse (clockid_t which_clock, +struct timespec *tp) +{ + *tp =get_monotonic_coarse (); +return 0 ; +} + +int posix_get_coarse_res (const clockid_t which_clock,struct timespec *tp) +{ + *tp =ktime_to_timespec (KTIME_LOW_RES ); +return 0 ; +}/* * Initialize everything, well, just everything in Posix clocks/timers ;) */
@@ -255,10 +274,24 @@ static __init int init_posix_timers(void) .clock_set = do_posix_clock_nosettime, .timer_create = no_timer_create, }; +struct k_clock clock_realtime_coarse = { + .clock_getres = posix_get_coarse_res, + .clock_get = posix_get_realtime_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + }; +struct k_clock clock_monotonic_coarse = { + .clock_getres = posix_get_coarse_res, + .clock_get = posix_get_monotonic_coarse, + .clock_set = do_posix_clock_nosettime, + .timer_create = no_timer_create, + };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); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, SLAB_PANIC,
kernel/time/timekeeping.c +21 -0
@@ -603,6 +603,10 @@ unsigned long get_seconds(void) }EXPORT_SYMBOL (get_seconds); +struct timespec __current_kernel_time (void ) +{ +return xtime_cache; +}struct timespec current_kernel_time (void ) {
@@ -618,3 +622,20 @@ struct timespec current_kernel_time(void)return now; }EXPORT_SYMBOL (current_kernel_time); + +struct timespec get_monotonic_coarse (void ) +{ +struct timespec now, mono; +unsigned long seq; + +do { + seq =read_seqbegin (&xtime_lock); + + now = xtime_cache; + mono = wall_to_monotonic; + }while (read_seqretry (&xtime_lock, seq)); + +set_normalized_timespec (&now, now.tv_sec + mono.tv_sec, + now.tv_nsec + mono.tv_nsec); +return now; +}
Quoted 25 lines · john stultz
Quoted 12 lines · Arjan van de Ven
Quoted 9 lines · On Sat, 18 Jul 2009 15:09:38 -0700 john stultz <[email protected]> wrote:
After talking with some application writers who want very fast, but not fine-grained timestamps, I decided to try to implement a new
clock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.Quoted 3 lines
Does this tie us to having a tick? I still have hope that we can get rid of the tick even when apps are running .... since with CFS we don't really need the tick for the scheduler anymore for example....
So it does require some sort of periodic interval. But the granularity is probably flexible, although I'm not sure it would be of much use if the granularity gets to be lower then 100hz.
While being 100% tickless, even when non-idle would be nice, there will be some need for timekeeping events to prevent clocksource counters from wrapping, and to do accurate NTP steering.
Quoted 8 lines
Even so, the value we're exporting in this patch is the
xtime_cache, which is updated every tick. This is currently used in file timestamping, so if we go 100% tickless, we'll have to change the file timestamping to use the actualCLOCK_REALTIMEclock_id, which requires a possibly slow hardware read and would likely hurt fs performance.So this patch doesn't so much tie us to having a tick or periodic event any more the the fs timestamping does.
Hmm. I think that we can have our cake and eat it too, if the machine has a hardware timer that can be turned on and off very cheaply. Just (heh) turn off the tick when an entire tick interval elapses without an access to the cached time.
This is a win if we frequently have two or more consecutive tick intervals without a clock read, it does nothing (except probably a touch of bookkeeping overhead) when someone reads the cached time during each tick interval, and it's a loss (due to excessive reprogramming of the timer) when the cache is read on alternating intervals. Of course, if the cached time is read (several times, anyway) every tick, then having a tick is a good thing because it avoids time source reads.
Getting this to work from the a vsyscall would be tricky. We could have a userspace-readable flag indicating both what time it is and whether the value is accurate and has already been requested this interval (use some sentinal value for the not-requested case, at the cost of a tiny chance the vsyscall doesn't work) and punt to the kernel if this is the first access in any interval.
--Andy
Quoted 12 lines · john stultz
Hey all,
After talking with some application writers who want very
fast, but not fine-grained timestamps, I decided to try to implement a newclock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
Quoted 5 lines
This isn't a new idea, I know Ingo pushed a patch (see commit
5899a0f044f3c80e9f7262ec5bc7164773a4c28e) a little while ago that made the vsyscallgettimeofday()return coarse grained time when the vsyscall64 sysctrl was set to 2. However this affects all applications on a system.
Note, that patch is an -rt commit, right? I.e. not yet upstream.
Quoted 9 lines
With this method, applications can choose the proper speed/granularity trade-off for themselves.
This is a first pass on this implementation, and while I did test it, the box I tested it with did not have a glibc new enough to utilize the vdso
clock_gettime(), so there may still be issues there. I'll find a newer box for testing shortly.Any thoughts or feedback will be appreciated!
Looks good. I think we should offer both methods: your patch as an unconditional 'coarse time' approximator always available everywhere, plus the vsyscall redirector as well from -rt, to allow admins/users to tweak in a global way on apps that cannot be changed.
Ingo
Quoted 14 lines · Ingo Molnar
Quoted 12 lines · john stultz
Hey all,
After talking with some application writers who want very
fast, but not fine-grained timestamps, I decided to try to implement a newclock_idstoclock_gettime():CLOCK_REALTIME_COARSEandCLOCK_MONOTONIC_COARSEwhich returns the time at the last tick. This is very fast as we don't have to access any hardware (which can be very painful if you're using something like theacpi_pmclocksource), and we can even use the vdsoclock_gettime()method to avoid the syscall. The only trade off is you only get low-res tick grained time resolution.
Quoted 7 lines
Quoted 5 lines
This isn't a new idea, I know Ingo pushed a patch (see commit
5899a0f044f3c80e9f7262ec5bc7164773a4c28e) a little while ago that made the vsyscallgettimeofday()return coarse grained time when the vsyscall64 sysctrl was set to 2. However this affects all applications on a system.Note, that patch is an -rt commit, right? I.e. not yet upstream.
Oh yikes.. Somehow I cloned off of the wrong tree. Yes, you're right, I was looking at the -rt git tree, your patch has not gone upstream yet.
My patch does still apply to mainline with offsets.
Quoted 8 lines
Quoted 8 lines
With this method, applications can choose the proper speed/granularity trade-off for themselves.
This is a first pass on this implementation, and while I did test it, the box I tested it with did not have a glibc new enough to utilize the vdso
clock_gettime(), so there may still be issues there. I'll find a newer box for testing shortly.
Quoted 7 lines
Quoted 1 line
Any thoughts or feedback will be appreciated!
Looks good. I think we should offer both methods: your patch as an unconditional 'coarse time' approximator always available everywhere, plus the vsyscall redirector as well from -rt, to allow admins/users to tweak in a global way on apps that cannot be changed.
I was thinking for users of apps that cannot be changed, an LD_PRELOAD redirector that changed the clock_id to a _COARSE might be a little cleaner then whole-sale forcing all of user-land to be low-res. But I'm not terribly opinionated on it.
-john
Quoted 2 lines · john stultz
- if (
likely(gtod->sysctl_enabled && gtod->clock.vread))
+ if (likely(gtod->sysctl_enabled))
This irks me. If the sysctl is enabled and the codepath is getting used often enough that we care about performance, branch prediction should do the right thing without compiler hints. On the other hand, if the sysctl is disabled, and the compiler is telling the cpu to ignore its branch predictor, it'll hurt. I don't think we should be wrapping (un)likely annotations around configuration options, unless we're biasing against debug conditions where we definitely don't care about performance. The patch is certainly no worse than the existing code, but while we have the hood up, it might be nice to remove the annotation, unless we're sure that it does no harm, and does some good.
-- Chris
Quoted 5 lines · On Sat, 18 Jul 2009 23:00:55 -0400 Chris Snook <[email protected]> wrote:
Quoted 2 lines · john stultz
- if (
likely(gtod->sysctl_enabled && gtod->clock.vread))
+ if (likely(gtod->sysctl_enabled))
Quoted 11 lines
This irks me. If the sysctl is enabled and the codepath is getting used often enough that we care about performance, branch prediction should do the right thing without compiler hints. On the other hand, if the sysctl is disabled, and the compiler is telling the cpu to ignore its branch predictor, it'll hurt. I don't think we should be wrapping (un)likely annotations around configuration options, unless we're biasing against debug conditions where we definitely don't care about performance. The patch is certainly no worse than the existing code, but while we have the hood up, it might be nice to remove the annotation, unless we're sure that it does no harm, and does some good.
it's on x86.. likely/unlikely don't impact the CPU (since there are no "ignore the branch predictor" hints), only the code placement.....
(and that's probably a good thing; CPU branch predictors are pretty good, I'd not be surprised if they're at least as good as the programmers who think how they code is used)
--
Arjan van de Ven Intel Open Source Technology Centre
For development, discussion and tips for power savings, visit http://www.lesswatts.org
Quoted 2 lines · Arjan van de Ven
it's on x86.. likely/unlikely don't impact the CPU (since there are no "ignore the branch predictor" hints), only the code placement.....
But code placement does affect branch prediction (e.g. the Pentium 4 static predictor assumes backward branches are taken, recent AMD processors prefer you to arrange code such that conditional branches are not taken, etc.)
Signature · 1 line
Nicholas Miell <[email protected]>