Commit 5b59eadf authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] MSEC_TO_JIFFIES consolidation

From: Ingo Molnar <mingo@elte.hu>

We have various different implementations of MSEC[S]_TO_JIFFIES and
JIFFIES_TO_MSEC[S].  We recently had a compile-time clash in USB.

Fix all that up.

- The SCTP version was very inefficient.  Hopefully this version is accurate
  enough.

- Optimise for the HZ=100 and HZ=1000 cases

- This version does round-up, so sleep(9 milliseconds) works OK on 100HZ.

- We still have lots of jiffies_to_msec and msec_to_jiffies implementations.

From: William Lee Irwin III <wli@holomorphy.com>

  Optimize the cases where HZ is a divisor of 1000 or vice-versa in
  JIFFIES_TO_MSECS() and MSECS_TO_JIFFIES() by allowing the nonvanishing(!)
  integral ratios to appear as a parenthesized expressions eligible for
  constant folding optimizations.

From: me

  Use typesafe inlines for the jiffies-to-millisecond conversion functions.

  This means that milliseconds officially takes the type `unsigned int'.
  All current callers seem to be OK with that.

  Drivers need to be fixed up to use this instead of their private versions.
parent 1b104df1
......@@ -5,8 +5,6 @@
# define HZ 1000 /* Internal kernel timer frequency */
# define USER_HZ 100 /* .. some user interfaces are in "ticks" */
# define CLOCKS_PER_SEC (USER_HZ) /* like times() */
# define JIFFIES_TO_MSEC(x) (x)
# define MSEC_TO_JIFFIES(x) (x)
#endif
#ifndef HZ
......
......@@ -177,6 +177,37 @@ struct timezone {
(SH_DIV((MAX_JIFFY_OFFSET >> SEC_JIFFIE_SC) * TICK_NSEC, NSEC_PER_SEC, 1) - 1)
#endif
/*
* Convert jiffies to milliseconds and back.
*
* Avoid unnecessary multiplications/divisions in the
* two most common HZ cases:
*/
static inline unsigned int jiffies_to_msecs(unsigned long j)
{
#if HZ <= 1000 && !(1000 % HZ)
return (1000 / HZ) * j;
#elif HZ > 1000 && !(HZ % 1000)
return (j + (HZ / 1000) - 1)/(HZ / 1000);
#else
return (j * 1000) / HZ;
#endif
}
static inline unsigned long msecs_to_jiffies(unsigned int m)
{
#if HZ <= 1000 && !(1000 % HZ)
return (m + (1000 / HZ) - 1) / (1000 / HZ);
#elif HZ > 1000 && !(HZ % 1000)
return m * (HZ / 1000);
#else
return (m * HZ + 999) / 1000;
#endif
}
#define JIFFIES_TO_MSECS(j) jiffies_to_msecs(j)
#define MSECS_TO_JIFFIES(m) msecs_to_jiffies(m)
/*
* The TICK_NSEC - 1 rounds up the value to the next resolution. Note
* that a remainder subtract here would not do the right thing as the
......
......@@ -83,8 +83,6 @@ if(!(expr)) do { \
#define MESSAGE(args...) printk(KERN_INFO args)
#define ERROR(args...) printk(KERN_ERR args)
#define MSECS_TO_JIFFIES(ms) (((ms)*HZ+999)/1000)
/*
* Magic numbers used by Linux-IrDA. Random numbers which must be unique to
* give the best protection
......
......@@ -116,11 +116,6 @@
#define SCTP_STATIC static
#endif
#define MSECS_TO_JIFFIES(msec) \
(((msec / 1000) * HZ) + ((msec % 1000) * HZ) / 1000)
#define JIFFIES_TO_MSECS(jiff) \
(((jiff / HZ) * 1000) + ((jiff % HZ) * 1000) / HZ)
/*
* Function declarations.
*/
......
......@@ -75,13 +75,6 @@
#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
#ifndef JIFFIES_TO_MSEC
# define JIFFIES_TO_MSEC(x) ((x) * 1000 / HZ)
#endif
#ifndef MSEC_TO_JIFFIES
# define MSEC_TO_JIFFIES(x) ((x) * HZ / 1000)
#endif
/*
* These are the 'tuning knobs' of the scheduler:
*
......@@ -1880,7 +1873,7 @@ static void rebalance_tick(int this_cpu, runqueue_t *this_rq,
interval *= sd->busy_factor;
/* scale ms to jiffies */
interval = MSEC_TO_JIFFIES(interval);
interval = MSECS_TO_JIFFIES(interval);
if (unlikely(!interval))
interval = 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment