sched: avoid unnecessary overflow in sched_clock
PATCH 1/1 sched: avoid unnecessary overflow in sched_clock
In hundreds of days, the __cycles_2_ns calculation in sched_clock has an overflow. cyc * per_cpu(cyc2ns, cpu) exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
arch/x86/include/asm/timer.h +22 -1
@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);
@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }
PATCH 1/1 sched: avoid unnecessary overflow in sched_clock
(Added the missing signed-off-by line)
In hundreds of days, the __cycles_2_ns calculation in sched_clock has an overflow. cyc * per_cpu(cyc2ns, cpu) exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
arch/x86/include/asm/timer.h +22 -1
@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);
@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }
Quoted 11 lines · Salman Qazi
(Added the missing signed-off-by line)
In hundreds of days, the
__cycles_2_nscalculation insched_clockhas an overflow. cyc *per_cpu(cyc2ns, cpu)exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
Signed-off-by Salman Qazi <[email protected]>
Quoted 48 lines
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)arch/x86/include/asm/timer.h +22
-1@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }
Paul Turner
Reviewed-byQuoted 8 lines · john stultz
Quoted 7 lines · Salman Qazi
(Added the missing signed-off-by line)
In hundreds of days, the
__cycles_2_nscalculation insched_clockhas an overflow. cyc *per_cpu(cyc2ns, cpu)exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
Quoted 7 lines
Quoted 4 lines
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
Signed-off-by Salman Qazi<[email protected]>Acked-by John Stultz<[email protected]>
Quoted 51 lines
Quoted 49 lines
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)arch/x86/include/asm/timer.h +22
-1@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu)>>CYC2NS_SCALE_FACTOR ; + quot = (cyc>>CYC2NS_SCALE_FACTOR ); + rem = cyc& ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu))>>CYC2NS_SCALE_FACTOR );return ns; }
Quoted 8 lines · john stultz
Quoted 7 lines · Salman Qazi
(Added the missing signed-off-by line)
In hundreds of days, the
__cycles_2_nscalculation insched_clockhas an overflow. cyc *per_cpu(cyc2ns, cpu)exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
Quoted 6 lines
Quoted 4 lines
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
Signed-off-by Salman Qazi <[email protected]>Acked-by John Stultz <[email protected]>
This wants a stable tag, no?
-Mike
Quoted 55 lines
Quoted 38 lines
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)arch/x86/include/asm/timer.h +18
-0@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;arch/x86/include/asm/timer.h DECLARE_PER_CPU(unsigned long long, cyc2ns_offset); · line 70
Quoted 10 lines
arch/x86/include/asm/timer.h +4
-1int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }--
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/
Quoted 12 lines · Mike Galbraith
Quoted 11 lines · john stultz
Quoted 10 lines · Salman Qazi
(Added the missing signed-off-by line)
In hundreds of days, the
__cycles_2_nscalculation insched_clockhas an overflow. cyc *per_cpu(cyc2ns, cpu)exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Salman Qazi<[email protected]>Acked-by John Stultz<[email protected]>This wants a stable tag, no?
-Mike
Probably a good idea -- This especially sucks rocks in the sched_clock_stable==1 case; resulting in it coming straight back out of sched_clock_cpu() and trashing rq->clock.
- Paul
Quoted 57 lines
Quoted 55 lines
Quoted 38 lines
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)arch/x86/include/asm/timer.h +18
-0@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;arch/x86/include/asm/timer.h DECLARE_PER_CPU(unsigned long long, cyc2ns_offset); · line 70
Quoted 10 lines
arch/x86/include/asm/timer.h +4
-1int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu)>>CYC2NS_SCALE_FACTOR ; + quot = (cyc>>CYC2NS_SCALE_FACTOR ); + rem = cyc& ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu))>>CYC2NS_SCALE_FACTOR );return ns; }--
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/
Quoted 3 lines · Paul Turner
Probably a good idea -- This especially sucks rocks in the sched_clock_stable==1 case; resulting in it coming straight back out of
sched_clock_cpu()and trashingrq->clock.
There's another problem there in that TSC will wrap too soon and things will blow up in your face that way.
PATCH 1/1 sched: avoid unnecessary overflow in sched_clock
(Added Reviewed-by/Acked-by tags)
In hundreds of days, the __cycles_2_ns calculation in sched_clock has an overflow. cyc * per_cpu(cyc2ns, cpu) exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
arch/x86/include/asm/timer.h +22 -1
@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);
@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }
[tip:sched/urgent] sched, x86: Avoid unnecessary overflow in sched_clock
In reply to Salman QaziCommit-ID: 4cecf6d401a01d054afc1e5f605bcbfe553cb9b9 Gitweb: http://git.kernel.org/tip/4cecf6d401a01d054afc1e5f605bcbfe553cb9b9 Author: Salman Qazi <[email protected]> AuthorDate: Tue, 15 Nov 2011 14:12:06 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 16 Nov 2011 19:51:25 +0100
sched, x86: Avoid unnecessary overflow in sched_clock
(Added the missing signed-off-by line)
In hundreds of days, the __cycles_2_ns calculation in sched_clock has an overflow. cyc * per_cpu(cyc2ns, cpu) exceeds 64 bits, causing the final value to become zero. We can solve this without losing any precision.
We can decompose TSC into quotient and remainder of division by the scale factor, and then use this to convert TSC into nanoseconds.
--- arch/x86/include/asm/timer.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
arch/x86/include/asm/timer.h +22 -1
@@ -32,6 +32,22 @@ extern int no_timer_check;* ([email protected]) * * [email protected] "math is hard, lets go shopping!" +* +* In: +* +* ns = cycles * cyc2ns_scale / SC +* +* Although we may still have enough bits to store the value of ns, +* in some cases, we may not have enough bits to store cycles * cyc2ns_scale, +* leading to an incorrect result. +* +* To avoid this, we can decompose 'cycles' into quotient and remainder +* of division by SC. Then, +* +* ns = (quot * SC + rem) * cyc2ns_scale / SC +* = quot * cyc2ns_scale + (rem * cyc2ns_scale) / SC +* +* - [email protected] */ DECLARE_PER_CPU(unsigned long, cyc2ns);
@@ -41,9 +57,14 @@ DECLARE_PER_CPU(unsigned long long, cyc2ns_offset);static inline unsigned long long __cycles_2_ns (unsigned long long cyc) { +unsigned long long quot; +unsigned long long rem;int cpu =smp_processor_id ();unsigned long long ns =per_cpu (cyc2ns_offset, cpu); - ns += cyc *per_cpu (cyc2ns, cpu) >>CYC2NS_SCALE_FACTOR ; + quot = (cyc >>CYC2NS_SCALE_FACTOR ); + rem = cyc & ((1ULL <<CYC2NS_SCALE_FACTOR ) -1 ); + ns += quot *per_cpu (cyc2ns, cpu) + + ((rem *per_cpu (cyc2ns, cpu)) >>CYC2NS_SCALE_FACTOR );return ns; }