optimized ktime_get[_ts] for GENERIC_TIME=y
PATCH 1/1 optimized ktime_get[_ts] for GENERIC_TIME=y
From: Martin Schwidefsky <[email protected]>
The generic ktime_get function defined in kernel/hrtimer.c is suboptimial for GENERIC_TIME=y:
0) | ktime_get() {
0) | ktime_get_ts() {
0) | getnstimeofday() {
0) | read_tod_clock() {
0) 0.601 us | }
0) 1.938 us | }
0) | set_normalized_timespec() {
0) 0.602 us | }
0) 4.375 us | }
0) 5.523 us | }Overall there are two read_seqbegin/read_seqretry loops and a lot of unnecessary struct timespec calculations. ktime_get returns a nano second value which is the sum of xtime, wall_to_monotonic and the nano second delta from the clock source.
ktime_get can be optimized for GENERIC_TIME=y. The new version only calls clocksource_read:
0) | ktime_get() {
0) | read_tod_clock() {
0) 0.610 us | }
0) 1.977 us | }It uses a single read_seqbegin/readseqretry loop and just adds everthing to a nano second value.
ktime_get_ts is optimized in a similar fashion.
--- kernel/hrtimer.c | 4 ++ kernel/time/timekeeping.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
kernel/hrtimer.c +4 -0
@@ -46,6 +46,7 @@#include <asm/uaccess.h> +#ifndef CONFIG_GENERIC_TIME /** * ktime_get - get the monotonic time in ktime_t format *
@@ -60,6 +61,7 @@ ktime_t ktime_get(void) return timespec_to_ktime(now); }EXPORT_SYMBOL_GPL (ktime_get); +#endif /** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -104,6 +106,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, } }; +#ifndef CONFIG_GENERIC_TIME /** * ktime_get_ts - get the monotonic clock in timespec format * @ts: pointer to timespec variable
@@ -128,6 +131,7 @@ void ktime_get_ts(struct timespec *ts) ts->tv_nsec + tomono.tv_nsec); }EXPORT_SYMBOL_GPL (ktime_get_ts); +#endif /* * Get the coarse grained time at the softirq based on xtime and
kernel/time/timekeeping.c +63 -0
69 lines not on this page · raw message
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
Quoted 85 lines · Martin Schwidefsky
From: Martin Schwidefsky <[email protected]>
The generic
ktime_getfunction defined inkernel/hrtimer.cis suboptimial for GENERIC_TIME=y:0) | ktime_get() { 0) | ktime_get_ts() { 0) | getnstimeofday() { 0) | read_tod_clock() { 0) 0.601 us | } 0) 1.938 us | } 0) | set_normalized_timespec() { 0) 0.602 us | } 0) 4.375 us | } 0) 5.523 us | }Overall there are two read_seqbegin/read_seqretry loops and a lot of unnecessary struct timespec calculations.
ktime_getreturns a nano second value which is the sum of xtime,wall_to_monotonicand the nano second delta from the clock source.
ktime_getcan be optimized for GENERIC_TIME=y. The new version only callsclocksource_read:0) | ktime_get() { 0) | read_tod_clock() { 0) 0.610 us | } 0) 1.977 us | }It uses a single read_seqbegin/readseqretry loop and just adds everthing to a nano second value.
ktime_get_tsis optimized in a similar fashion.Cc Ingo Molnar <[email protected]>Cc Thomas Gleixner <[email protected]>Cc john stultz <[email protected]>Signed-off-by Martin Schwidefsky <[email protected]>--- kernel/hrtimer.c | 4 ++ kernel/time/timekeeping.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)diff -urpN
linux-2.5/kernel/hrtimer.clinux-2.5-patched/kernel/hrtimer.ckernel/hrtimer.c +4
-0@@ -46,6 +46,7 @@#include <asm/uaccess.h> +#ifndef CONFIG_GENERIC_TIME /** * ktime_get - get the monotonic time in ktime_t format * @@ -60,6 +61,7 @@ ktime_t ktime_get(void) return timespec_to_ktime(now); }EXPORT_SYMBOL_GPL (ktime_get); +#endif /** * ktime_get_real - get the real (wall-) time in ktime_t format @@ -104,6 +106,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, } }; +#ifndef CONFIG_GENERIC_TIME /** * ktime_get_ts - get the monotonic clock in timespec format * @ts: pointer to timespec variable @@ -128,6 +131,7 @@ void ktime_get_ts(struct timespec *ts) ts->tv_nsec + tomono.tv_nsec); }EXPORT_SYMBOL_GPL (ktime_get_ts); +#endif /* * Get the coarse grained time at the softirq based on xtime and diff -urpN
linux-2.5/kernel/time/timekeeping.clinux-2.5-patched/kernel/time/timekeeping.ckernel/time/timekeeping.c +0
-0@@ -118,6 +118,69 @@ void getnstimeofday(struct timespec *ts)EXPORT_SYMBOL (getnstimeofday);
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 129
Quoted 10 lines
kernel/time/timekeeping.c +9
-0+ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock);
minor nit : read_seqbegin() returns an "unsigned int", not an "unsigned long"
But apparently, most callers store the value into an unsigned long... oh well...
Quoted 7 lines · On Mon, 06 Jul 2009 16:07:22 +0200 Eric Dumazet <[email protected]> wrote:
Quoted 7 lines
diff -urpN
linux-2.5/kernel/time/timekeeping.clinux-2.5-patched/kernel/time/timekeeping.ckernel/time/timekeeping.c +0
-0@@ -118,6 +118,69 @@ void getnstimeofday(struct timespec *ts)EXPORT_SYMBOL (getnstimeofday);
Quoted 11 lines
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 129
Quoted 9 lines
kernel/time/timekeeping.c +9
-0+ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock);minor nit :
read_seqbegin()returns an "unsigned int", not an "unsigned long"
Hmm, just cut-copy-pasted the seq-loop. Seems like a job for the janitor .. But I will update my patch.
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
Quoted 32 lines · Martin Schwidefsky
From: Martin Schwidefsky <[email protected]>
The generic
ktime_getfunction defined inkernel/hrtimer.cis suboptimial for GENERIC_TIME=y:0) | ktime_get() { 0) | ktime_get_ts() { 0) | getnstimeofday() { 0) | read_tod_clock() { 0) 0.601 us | } 0) 1.938 us | } 0) | set_normalized_timespec() { 0) 0.602 us | } 0) 4.375 us | } 0) 5.523 us | }Overall there are two read_seqbegin/read_seqretry loops and a lot of unnecessary struct timespec calculations.
ktime_getreturns a nano second value which is the sum of xtime,wall_to_monotonicand the nano second delta from the clock source.
ktime_getcan be optimized for GENERIC_TIME=y. The new version only callsclocksource_read:0) | ktime_get() { 0) | read_tod_clock() { 0) 0.610 us | } 0) 1.977 us | }It uses a single read_seqbegin/readseqretry loop and just adds everthing to a nano second value.
Quoted 6 lines
ktime_get_tsis optimized in a similar fashion.Cc Ingo Molnar <[email protected]>Cc Thomas Gleixner <[email protected]>Cc john stultz <[email protected]>Signed-off-by Martin Schwidefsky <[email protected]>
Looks good to me.
Quoted 116 lines
--- kernel/hrtimer.c | 4 ++ kernel/time/timekeeping.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)diff -urpN
linux-2.5/kernel/hrtimer.clinux-2.5-patched/kernel/hrtimer.ckernel/hrtimer.c +4
-0@@ -46,6 +46,7 @@#include <asm/uaccess.h> +#ifndef CONFIG_GENERIC_TIME /** * ktime_get - get the monotonic time in ktime_t format * @@ -60,6 +61,7 @@ ktime_t ktime_get(void) return timespec_to_ktime(now); }EXPORT_SYMBOL_GPL (ktime_get); +#endif /** * ktime_get_real - get the real (wall-) time in ktime_t format @@ -104,6 +106,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, } }; +#ifndef CONFIG_GENERIC_TIME /** * ktime_get_ts - get the monotonic clock in timespec format * @ts: pointer to timespec variable @@ -128,6 +131,7 @@ void ktime_get_ts(struct timespec *ts) ts->tv_nsec + tomono.tv_nsec); }EXPORT_SYMBOL_GPL (ktime_get_ts); +#endif /* * Get the coarse grained time at the softirq based on xtime and diff -urpN
linux-2.5/kernel/time/timekeeping.clinux-2.5-patched/kernel/time/timekeeping.ckernel/time/timekeeping.c +63
-0@@ -118,6 +118,69 @@ void getnstimeofday(struct timespec *ts)EXPORT_SYMBOL (getnstimeofday); +ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + +do { + seq =read_seqbegin (&xtime_lock); + time.tv_sec = xtime.tv_sec + wall_to_monotonic.tv_sec; + time.tv_nsec = xtime.tv_nsec + wall_to_monotonic.tv_nsec; + +/* read clocksource: */ + cycle_now =clocksource_read (clock); + +/* calculate the delta since the last update_wall_time: */ + cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + +/* convert to nanoseconds: */ + nsecs =cyc2ns (clock, cycle_delta); + + }while (read_seqretry (&xtime_lock, seq)); + nsecs += time.tv_sec *1000000000ULL + time.tv_nsec; +return (ktime_t ) { .tv64 = nsecs }; +} +EXPORT_SYMBOL_GPL (ktime_get); + +/** +* ktime_get_ts - get the monotonic clock in timespec format +* @ts: pointer to timespec variable +* +* The function calculates the monotonic clock from the realtime +* clock and the wall_to_monotonic offset and stores the result +* in normalized timespec format in the variable pointed to by @ts. +*/ +void ktime_get_ts (struct timespec *ts) +{ +cycle_t cycle_now, cycle_delta; +struct timespec tomono; +unsigned long seq; +s64 nsecs; + +do { + seq =read_seqbegin (&xtime_lock); + *ts = xtime; + tomono = wall_to_monotonic; + +/* read clocksource: */ + cycle_now =clocksource_read (clock); + +/* calculate the delta since the last update_wall_time: */ + cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + +/* convert to nanoseconds: */ + nsecs =cyc2ns (clock, cycle_delta); + + }while (read_seqretry (&xtime_lock, seq)); + +set_normalized_timespec (ts, ts->tv_sec + tomono.tv_sec, + ts->tv_nsec + tomono.tv_nsec + nsecs); +} +EXPORT_SYMBOL_GPL (ktime_get_ts); +/** * do_gettimeofday - Returns the time of day in a timeval * @tv: pointer to the timeval to be set
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 131
Quoted 11 lines · Martin Schwidefsky
kernel/time/timekeeping.c +11
-0+ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock); + time.tv_sec = xtime.tv_sec + wall_to_monotonic.tv_sec; + time.tv_nsec = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
That's actually a violation of the timespec semantics. tv_nsec can end up greater than (10^9 - 1). Please use separate sec and nsec variables.
secs = xtime.tv_sec + wall_to_monotonic.tv_sec; nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 140
Quoted 8 lines
kernel/time/timekeeping.c +8
-0+/* read clocksource: */ + cycle_now =clocksource_read (clock); + +/* calculate the delta since the last update_wall_time: */ + cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + +/* convert to nanoseconds: */ + nsecs =cyc2ns (clock, cycle_delta);
So that needs to be changed to:
nsecs += cyc2ns(clock, cycle_delta);
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 144
Quoted 4 lines
kernel/time/timekeeping.c +4
-0+ + }while (read_seqretry (&xtime_lock, seq)); + nsecs += time.tv_sec *1000000000ULL + time.tv_nsec; +return (ktime_t ) { .tv64 = nsecs };
This will break all 32bit architectures which do not have CONFIG_KTIME_SCALAR set.
With the above changes applied:
return ktime_add_ns(ktime_set(secs, 0), nsecs);
will do the trick. Might need some comments though :)
Thanks,
tglx
Quoted 15 lines · On Mon, 6 Jul 2009 22:31:39 +0200 (CEST) Thomas Gleixner <[email protected]>…
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 131
Quoted 11 lines · Martin Schwidefsky
kernel/time/timekeeping.c +11
-0+ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock); + time.tv_sec = xtime.tv_sec + wall_to_monotonic.tv_sec; + time.tv_nsec = xtime.tv_nsec + wall_to_monotonic.tv_nsec;That's actually a violation of the timespec semantics.
tv_nseccan end up greater than (10^9 - 1). Please use separate sec and nsec variables.
Well the tv_sec/tv_nsec fields of a timespec are long values. But its no problem to switch to local variables.
Quoted 14 lines
secs = xtime.tv_sec + wall_to_monotonic.tv_sec; nsecs = xtime.tv_nsec + wall_to_monotonic.tv_nsec;kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 140
Quoted 8 lines
kernel/time/timekeeping.c +8
-0+/* read clocksource: */ + cycle_now =clocksource_read (clock); + +/* calculate the delta since the last update_wall_time: */ + cycle_delta = (cycle_now - clock->cycle_last) & clock->mask; + +/* convert to nanoseconds: */ + nsecs =cyc2ns (clock, cycle_delta);So that needs to be changed to:
Quoted 1 line
nsecs += cyc2ns(clock, cycle_delta);
Ok.
Quoted 7 lines
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 144
Quoted 4 lines
kernel/time/timekeeping.c +4
-0+ + }while (read_seqretry (&xtime_lock, seq)); + nsecs += time.tv_sec *1000000000ULL + time.tv_nsec; +return (ktime_t ) { .tv64 = nsecs };This will break all 32bit architectures which do not have
CONFIG_KTIME_SCALARset.
Yep, that is a bug.
Quoted 5 lines
With the above changes applied:
return ktime_add_ns(ktime_set(secs, 0), nsecs);will do the trick. Might need some comments though :)
Ok, will update the patch. Thanks for the review.
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
Martin,
Quoted 19 lines · Martin Schwidefsky
Quoted 15 lines · On Mon, 6 Jul 2009 22:31:39 +0200 (CEST) Thomas Gleixner <[email protected]>…
kernel/time/timekeeping.c void getnstimeofday(struct timespec *ts) · line 131
Quoted 11 lines · Martin Schwidefsky
kernel/time/timekeeping.c +11
-0+ktime_t ktime_get (void ) +{ +cycle_t cycle_now, cycle_delta; +struct timespec time; +unsigned long seq; +s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock); + time.tv_sec = xtime.tv_sec + wall_to_monotonic.tv_sec; + time.tv_nsec = xtime.tv_nsec + wall_to_monotonic.tv_nsec;That's actually a violation of the timespec semantics.
tv_nseccan end up greater than (10^9 - 1). Please use separate sec and nsec variables.
Quoted 2 lines
Well the tv_sec/tv_nsec fields of a timespec are long values. But its no problem to switch to local variables.
Right. I'm not worried about an overflow of the variable. I was about to suggest using timespec_to_ktime() to fix the !SCALAR problem when I noticed that the timespec is possibly not normalized. Not using a timespec makes the code more obvious I think.
Thanks,
tglx
Martin Schwidefsky
Acked-by john stultzQuoted 22 lines · On Tue, 7 Jul 2009 10:08:56 +0200 (CEST) Thomas Gleixner <[email protected]>…
Martin,
Quoted 19 lines · Martin Schwidefsky
Quoted 15 lines · On Mon, 6 Jul 2009 22:31:39 +0200 (CEST) Thomas Gleixner <[email protected]>…
Review
Quoted 11 lines · Martin Schwidefsky
(quoted diff) +11
-0+ktime_t ktime_get(void) +{ + cycle_t cycle_now, cycle_delta; + struct timespec time; + unsigned long seq; + s64 nsecs; + + do { + seq = read_seqbegin(&xtime_lock); + time.tv_sec = xtime.tv_sec + wall_to_monotonic.tv_sec; + time.tv_nsec = xtime.tv_nsec + wall_to_monotonic.tv_nsec;That's actually a violation of the timespec semantics.
tv_nseccan end up greater than (10^9 - 1). Please use separate sec and nsec variables.
Quoted 7 lines
Quoted 2 lines
Well the tv_sec/tv_nsec fields of a timespec are long values. But its no problem to switch to local variables.
Right. I'm not worried about an overflow of the variable. I was about to suggest using
timespec_to_ktime()to fix the !SCALAR problem when I noticed that the timespec is possibly not normalized. Not using a timespec makes the code more obvious I think.
That makes sense. New patch:
--
Subject: [PATCH] optimized ktime_get[_ts] for GENERIC_TIME=y
From: Martin Schwidefsky <[email protected]>
The generic ktime_get function defined in kernel/hrtimer.c is suboptimial for GENERIC_TIME=y:
0) | ktime_get() {
0) | ktime_get_ts() {
0) | getnstimeofday() {
0) | read_tod_clock() {
0) 0.601 us | }
0) 1.938 us | }
0) | set_normalized_timespec() {
0) 0.602 us | }
0) 4.375 us | }
0) 5.523 us | }Overall there are two read_seqbegin/read_seqretry loops and a lot of unnecessary struct timespec calculations. ktime_get returns a nano second value which is the sum of xtime, wall_to_monotonic and the nano second delta from the clock source.
ktime_get can be optimized for GENERIC_TIME=y. The new version only calls clocksource_read:
0) | ktime_get() {
0) | read_tod_clock() {
0) 0.610 us | }
0) 1.977 us | }It uses a single read_seqbegin/readseqretry loop and just adds everthing to a nano second value.
ktime_get_ts is optimized in a similar fashion.
---
kernel/hrtimer.c | 4 ++ kernel/time/timekeeping.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+)
kernel/hrtimer.c +4 -0
@@ -48,6 +48,7 @@#include <asm/uaccess.h> +#ifndef CONFIG_GENERIC_TIME /** * ktime_get - get the monotonic time in ktime_t format *
@@ -62,6 +63,7 @@ ktime_t ktime_get(void) return timespec_to_ktime(now); }EXPORT_SYMBOL_GPL (ktime_get); +#endif /** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -106,6 +108,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, } }; +#ifndef CONFIG_GENERIC_TIME /** * ktime_get_ts - get the monotonic clock in timespec format * @ts: pointer to timespec variable
@@ -130,6 +133,7 @@ void ktime_get_ts(struct timespec *ts) ts->tv_nsec + tomono.tv_nsec); }EXPORT_SYMBOL_GPL (ktime_get_ts); +#endif /* * Get the coarse grained time at the softirq based on xtime and
kernel/time/timekeeping.c +65 -0
71 lines not on this page · raw message
Signature · 4 lines
blue skies, Martin. "Reality continues to ruin my life." - Calvin.
[tip:timers/core] timekeeping: optimized ktime_get[_ts] for GENERIC_TIME=y
Commit-ID: 951ed4d36b77ba9fe1ea08fc3c59d8bb6c9bda32 Gitweb: http://git.kernel.org/tip/951ed4d36b77ba9fe1ea08fc3c59d8bb6c9bda32 Author: Martin Schwidefsky <[email protected]> AuthorDate: Tue, 7 Jul 2009 11:27:28 +0200 Committer: Thomas Gleixner <[email protected]> CommitDate: Tue, 7 Jul 2009 12:47:33 +0200
timekeeping: optimized ktime_get[_ts] for GENERIC_TIME=y
The generic ktime_get function defined in kernel/hrtimer.c is suboptimial for GENERIC_TIME=y:
0) | ktime_get() {
0) | ktime_get_ts() {
0) | getnstimeofday() {
0) | read_tod_clock() {
0) 0.601 us | }
0) 1.938 us | }
0) | set_normalized_timespec() {
0) 0.602 us | }
0) 4.375 us | }
0) 5.523 us | }Overall there are two read_seqbegin/read_seqretry loops and a lot of unnecessary struct timespec calculations. ktime_get returns a nano second value which is the sum of xtime, wall_to_monotonic and the nano second delta from the clock source.
ktime_get can be optimized for GENERIC_TIME=y. The new version only calls clocksource_read:
0) | ktime_get() {
0) | read_tod_clock() {
0) 0.610 us | }
0) 1.977 us | }It uses a single read_seqbegin/readseqretry loop and just adds everthing to a nano second value.
ktime_get_ts is optimized in a similar fashion.
[ tglx: added WARN_ON(timekeeping_suspended) as in getnstimeofday() ]
LKML-Reference: <20090707112728.3005244d@skybase>
--- kernel/hrtimer.c | 4 ++ kernel/time/timekeeping.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 0 deletions(-)
kernel/hrtimer.c +4 -0
@@ -48,6 +48,7 @@#include <asm/uaccess.h> +#ifndef CONFIG_GENERIC_TIME /** * ktime_get - get the monotonic time in ktime_t format *
@@ -62,6 +63,7 @@ ktime_t ktime_get(void) return timespec_to_ktime(now); }EXPORT_SYMBOL_GPL (ktime_get); +#endif /** * ktime_get_real - get the real (wall-) time in ktime_t format
@@ -106,6 +108,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) = } }; +#ifndef CONFIG_GENERIC_TIME /** * ktime_get_ts - get the monotonic clock in timespec format * @ts: pointer to timespec variable
@@ -130,6 +133,7 @@ void ktime_get_ts(struct timespec *ts) ts->tv_nsec + tomono.tv_nsec); }EXPORT_SYMBOL_GPL (ktime_get_ts); +#endif /* * Get the coarse grained time at the softirq based on xtime and
kernel/time/timekeeping.c +69 -0
75 lines not on this page · raw message