]> git.kernelconcepts.de Git - karo-tx-linux.git/commitdiff
ktime: Optimize ktime_divns for constant divisors
authorNicolas Pitre <nicolas.pitre@linaro.org>
Wed, 3 Dec 2014 19:43:06 +0000 (14:43 -0500)
committerJohn Stultz <john.stultz@linaro.org>
Sat, 24 Jan 2015 01:21:31 +0000 (17:21 -0800)
At least on ARM, do_div() is optimized to turn constant divisors into
an inline multiplication by the reciprocal value at compile time.
However this optimization is missed entirely whenever ktime_divns() is
used and the slow out-of-line division code is used all the time.

Let ktime_divns() use do_div() inline whenever the divisor is constant
and small enough.  This will make things like ktime_to_us() and
ktime_to_ms() much faster.

Cc: Arnd Bergmann <arnd.bergmann@linaro.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Nicolas Pitre <nico@linaro.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Nicolas Pitre <nico@linaro.org>
Signed-off-by: John Stultz <john.stultz@linaro.org>
include/linux/ktime.h
kernel/time/hrtimer.c

index c9d645ad98ff7ac9919f0fee00808cfd0e4963f7..411dd8bfe53906f2f171638a8898a5a4b2da9f1d 100644 (file)
@@ -166,7 +166,17 @@ static inline bool ktime_before(const ktime_t cmp1, const ktime_t cmp2)
 }
 
 #if BITS_PER_LONG < 64
-extern u64 ktime_divns(const ktime_t kt, s64 div);
+extern u64 __ktime_divns(const ktime_t kt, s64 div);
+static inline u64 ktime_divns(const ktime_t kt, s64 div)
+{
+       if (__builtin_constant_p(div) && !(div >> 32)) {
+               u64 ns = kt.tv64;
+               do_div(ns, div);
+               return ns;
+       } else {
+               return __ktime_divns(kt, div);
+       }
+}
 #else /* BITS_PER_LONG < 64 */
 # define ktime_divns(kt, div)          (u64)((kt).tv64 / (div))
 #endif
index 37e50aadd471195bebb3dc32c9ad026dbbcf2ab7..890535c41c2d08ac76e68c86655863be3b09869e 100644 (file)
@@ -266,7 +266,7 @@ lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
 /*
  * Divide a ktime value by a nanosecond value
  */
-u64 ktime_divns(const ktime_t kt, s64 div)
+u64 __ktime_divns(const ktime_t kt, s64 div)
 {
        u64 dclc;
        int sft = 0;
@@ -282,7 +282,7 @@ u64 ktime_divns(const ktime_t kt, s64 div)
 
        return dclc;
 }
-EXPORT_SYMBOL_GPL(ktime_divns);
+EXPORT_SYMBOL_GPL(__ktime_divns);
 #endif /* BITS_PER_LONG >= 64 */
 
 /*