* [RFC 0/3] Improve fallback LPJ calculation
@ 2010-09-10 9:54 Phil Carmody
2010-09-10 9:54 ` [PATCH 1/3] calibrate: extract fall-back calculation into own helper Phil Carmody
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Phil Carmody @ 2010-09-10 9:54 UTC (permalink / raw)
To: mingo, gregkh; +Cc: linux-kernel, travis, akataria
The motivation for patch 2/3 is that currently our OMAP calibrates
itself using the trial-and-error binary chop fallback that some other
architectures no longer need to perform. This is a lengthy process,
taking 0.2s in an environment where boot time is of great interest.
The patch has two optimisations. Firstly, it replaces the initial
repeated-doubling to find the relevant power of 2 with a tight loop
that just does as much as it can in a jiffy. Secondly, it doesn't
binary chop over an entire power of 2 range, it choses a much smaller
range based on how much it squeezed in, and failed to squeeze in,
during the first stage. Both are significant optimisations, and
bring our calibration down from 23 jiffies to 6, and, in the process,
arrive at a more accurate lpj value.
The 'bands' and 'sub-logarithmic' growth may look over-engineered,
but they cost a small level of in inaccuracy of the initial guess
(for all architectures) in order to avoid the very large inaccuracies
that appeared during testing (on x86_64 architectures, and presumably
others with less metronomic operation). Note that due to the existence
of the TSC and other timers, the x86_64 doesn't use this fallback
routine, but I wanted to code defensively, able to cope with all kinds
of processor behaviours.
1/3 is simply cosmetic to prepare for 2/3.
3/3 is simply to assist testing and not intended for integration.
I would appreciate it if people with exotic or unusual architectures
could help test this.
Cheers,
Phil
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/3] calibrate: extract fall-back calculation into own helper
2010-09-10 9:54 [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
@ 2010-09-10 9:54 ` Phil Carmody
2010-09-10 9:54 ` [PATCH 2/3] calibrate: home in on correct lpj bounds more quickly Phil Carmody
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Phil Carmody @ 2010-09-10 9:54 UTC (permalink / raw)
To: mingo, gregkh; +Cc: linux-kernel, travis, akataria
From: Carmody Phil.2 (EXT-Ixonos/Helsinki) <ext-phil.2.carmody@nokia.com>
So that it can be modified more clinically.
Signed-off-by: Phil Carmody <ext-phil.2.carmody@nokia.com>
---
init/calibrate.c | 73 +++++++++++++++++++++++++++++------------------------
1 files changed, 40 insertions(+), 33 deletions(-)
diff --git a/init/calibrate.c b/init/calibrate.c
index 6eb48e5..f7042ff 100644
--- a/init/calibrate.c
+++ b/init/calibrate.c
@@ -119,10 +119,47 @@ static unsigned long __cpuinit calibrate_delay_direct(void) {return 0;}
*/
#define LPS_PREC 8
-void __cpuinit calibrate_delay(void)
+static unsigned long __cpuinit calibrate_delay_converge(void)
{
- unsigned long ticks, loopbit;
+ unsigned long lpj, ticks, loopbit;
int lps_precision = LPS_PREC;
+
+ lpj = (1<<12);
+ while ((lpj <<= 1) != 0) {
+ /* wait for "start of" clock tick */
+ ticks = jiffies;
+ while (ticks == jiffies)
+ /* nothing */;
+ /* Go .. */
+ ticks = jiffies;
+ __delay(lpj);
+ ticks = jiffies - ticks;
+ if (ticks)
+ break;
+ }
+
+ /*
+ * Do a binary approximation to get lpj set to
+ * equal one clock (up to lps_precision bits)
+ */
+ lpj >>= 1;
+ loopbit = lpj;
+ while (lps_precision-- && (loopbit >>= 1)) {
+ lpj |= loopbit;
+ ticks = jiffies;
+ while (ticks == jiffies)
+ /* nothing */;
+ ticks = jiffies;
+ __delay(lpj);
+ if (jiffies != ticks) /* longer than 1 tick */
+ lpj &= ~loopbit;
+ }
+
+ return lpj;
+}
+
+void __cpuinit calibrate_delay(void)
+{
static bool printed;
if (preset_lpj) {
@@ -139,39 +176,9 @@ void __cpuinit calibrate_delay(void)
pr_info("Calibrating delay using timer "
"specific routine.. ");
} else {
- loops_per_jiffy = (1<<12);
-
if (!printed)
pr_info("Calibrating delay loop... ");
- while ((loops_per_jiffy <<= 1) != 0) {
- /* wait for "start of" clock tick */
- ticks = jiffies;
- while (ticks == jiffies)
- /* nothing */;
- /* Go .. */
- ticks = jiffies;
- __delay(loops_per_jiffy);
- ticks = jiffies - ticks;
- if (ticks)
- break;
- }
-
- /*
- * Do a binary approximation to get loops_per_jiffy set to
- * equal one clock (up to lps_precision bits)
- */
- loops_per_jiffy >>= 1;
- loopbit = loops_per_jiffy;
- while (lps_precision-- && (loopbit >>= 1)) {
- loops_per_jiffy |= loopbit;
- ticks = jiffies;
- while (ticks == jiffies)
- /* nothing */;
- ticks = jiffies;
- __delay(loops_per_jiffy);
- if (jiffies != ticks) /* longer than 1 tick */
- loops_per_jiffy &= ~loopbit;
- }
+ loops_per_jiffy = calibrate_delay_converge();
}
if (!printed)
pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
--
1.7.2.rc1.37.gf8c40
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 2/3] calibrate: home in on correct lpj bounds more quickly
2010-09-10 9:54 [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
2010-09-10 9:54 ` [PATCH 1/3] calibrate: extract fall-back calculation into own helper Phil Carmody
@ 2010-09-10 9:54 ` Phil Carmody
2010-09-10 9:54 ` [PATCH 3/3] DO NOT INTEGRATE: test-only timing Phil Carmody
2010-09-22 9:22 ` [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
3 siblings, 0 replies; 5+ messages in thread
From: Phil Carmody @ 2010-09-10 9:54 UTC (permalink / raw)
To: mingo, gregkh; +Cc: linux-kernel, travis, akataria
Binary chop with a jiffy-resync on each step to find an upper bound
is slow, so just race in a tight-ish loop to find an underestimate.
If done with lots of individual steps, sometimes several hundreds
of loops would be required, which would impose a significant overhead,
and make the initial estimate very low. By taking slowly increasing
steps there will be less overhead.
E.g. an x86_64 2.67GHz could have fitted in 613 individual small
delays, but in reality should have been able to fit in a single
delay 649 times longer, so underestimated by 36 steps. To reach
the equivalent of 649 small delays with the accellerating scheme
now requires about 130 steps, or <1/4th of the overhead.
As now we have a better initial estimate we can now binary chop
over a smaller range. With the loop overhead in the initial estimate
kept low, and the step sizes moderate, we won't have under-estimated
by much, so chose as tight a range as we can.
Signed-off-by: Phil Carmody <ext-phil.2.carmody@nokia.com>
---
init/calibrate.c | 57 ++++++++++++++++++++++++++++++++---------------------
1 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/init/calibrate.c b/init/calibrate.c
index f7042ff..7315c9a 100644
--- a/init/calibrate.c
+++ b/init/calibrate.c
@@ -110,8 +110,8 @@ static unsigned long __cpuinit calibrate_delay_direct(void) {return 0;}
/*
* This is the number of bits of precision for the loops_per_jiffy. Each
- * bit takes on average 1.5/HZ seconds. This (like the original) is a little
- * better than 1%
+ * time we refine our estimate after the first takes 1.5/HZ seconds, so try
+ * to start with a good estimate.
* For the boot cpu we can skip the delay calibration and assign it a value
* calculated based on the timer frequency.
* For the rest of the CPUs we cannot assume that the timer frequency is same as
@@ -121,38 +121,49 @@ static unsigned long __cpuinit calibrate_delay_direct(void) {return 0;}
static unsigned long __cpuinit calibrate_delay_converge(void)
{
- unsigned long lpj, ticks, loopbit;
- int lps_precision = LPS_PREC;
+ /* First stage - slowly accelerate to find initial bounds */
+ unsigned long lpj, ticks, loopadd, chop_limit;
+ int trials = 0, band = 0, trial_in_band = 0;
lpj = (1<<12);
- while ((lpj <<= 1) != 0) {
- /* wait for "start of" clock tick */
- ticks = jiffies;
- while (ticks == jiffies)
- /* nothing */;
- /* Go .. */
- ticks = jiffies;
- __delay(lpj);
- ticks = jiffies - ticks;
- if (ticks)
- break;
- }
+
+ /* wait for "start of" clock tick */
+ ticks = jiffies;
+ while (ticks == jiffies)
+ ; /* nothing */
+ /* Go .. */
+ ticks = jiffies;
+ do {
+ if (++trial_in_band == (1<<band)) {
+ ++band;
+ trial_in_band = 0;
+ }
+ __delay(lpj * band);
+ trials += band;
+ } while (ticks == jiffies);
+ /*
+ * We overshot, so retreat to a clear underestimate. Then estimate
+ * the largest likely undershoot. This defines our chop bounds.
+ */
+ trials -= band;
+ loopadd = lpj * band;
+ lpj *= trials;
+ chop_limit = lpj >> (LPS_PREC + 1);
/*
* Do a binary approximation to get lpj set to
- * equal one clock (up to lps_precision bits)
+ * equal one clock (up to LPS_PREC bits)
*/
- lpj >>= 1;
- loopbit = lpj;
- while (lps_precision-- && (loopbit >>= 1)) {
- lpj |= loopbit;
+ while (loopadd > chop_limit) {
+ lpj += loopadd;
ticks = jiffies;
while (ticks == jiffies)
- /* nothing */;
+ ; /* nothing */
ticks = jiffies;
__delay(lpj);
if (jiffies != ticks) /* longer than 1 tick */
- lpj &= ~loopbit;
+ lpj -= loopadd;
+ loopadd >>= 1;
}
return lpj;
--
1.7.2.rc1.37.gf8c40
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 3/3] DO NOT INTEGRATE: test-only timing
2010-09-10 9:54 [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
2010-09-10 9:54 ` [PATCH 1/3] calibrate: extract fall-back calculation into own helper Phil Carmody
2010-09-10 9:54 ` [PATCH 2/3] calibrate: home in on correct lpj bounds more quickly Phil Carmody
@ 2010-09-10 9:54 ` Phil Carmody
2010-09-22 9:22 ` [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
3 siblings, 0 replies; 5+ messages in thread
From: Phil Carmody @ 2010-09-10 9:54 UTC (permalink / raw)
To: mingo, gregkh; +Cc: linux-kernel, travis, akataria
Always run both the old and the converging method to see
what they would say, even if we have more accurate values.
Signed-off-by: Phil Carmody <ext-phil.2.carmody@nokia.com>
---
init/calibrate.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 70 insertions(+), 4 deletions(-)
diff --git a/init/calibrate.c b/init/calibrate.c
index 7315c9a..b6d592e 100644
--- a/init/calibrate.c
+++ b/init/calibrate.c
@@ -146,6 +146,8 @@ static unsigned long __cpuinit calibrate_delay_converge(void)
* the largest likely undershoot. This defines our chop bounds.
*/
trials -= band;
+ pr_info("lpj underestimate = %lu * %i = %lu, band = %i\n",
+ lpj, trials, trials * lpj, band);
loopadd = lpj * band;
lpj *= trials;
chop_limit = lpj >> (LPS_PREC + 1);
@@ -169,32 +171,96 @@ static unsigned long __cpuinit calibrate_delay_converge(void)
return lpj;
}
+/* Old version for timing purposes only */
+static unsigned long __cpuinit calibrate_delay_converge_slowly(void)
+{
+ unsigned long lpj, ticks, loopbit;
+ int lps_precision = LPS_PREC;
+
+ lpj = (1<<12);
+ while ((lpj <<= 1) != 0) {
+ /* wait for "start of" clock tick */
+ ticks = jiffies;
+ while (ticks == jiffies)
+ /* nothing */;
+ /* Go .. */
+ ticks = jiffies;
+ __delay(lpj);
+ ticks = jiffies - ticks;
+ if (ticks)
+ break;
+ }
+
+ /*
+ * Do a binary approximation to get lpj set to
+ * equal one clock (up to lps_precision bits)
+ */
+ lpj >>= 1;
+ loopbit = lpj;
+ while (lps_precision-- && (loopbit >>= 1)) {
+ lpj |= loopbit;
+ ticks = jiffies;
+ while (ticks == jiffies)
+ /* nothing */;
+ ticks = jiffies;
+ __delay(lpj);
+ if (jiffies != ticks) /* longer than 1 tick */
+ lpj &= ~loopbit;
+ }
+
+ return lpj;
+}
+
+static void finally_print_bogomips(unsigned long lpj, unsigned int ticks)
+{
+ /* This calculation is bogus */
+ pr_cont("%lu.%02lu BogoMIPS (lpj=%lu) took %u jiffies\n",
+ lpj/(500000/HZ), (lpj/(5000/HZ)) % 100, lpj, ticks);
+}
+
void __cpuinit calibrate_delay(void)
{
+ enum methods { DUNNO, PRESET, FINE, DIRECT, CONVERGE };
static bool printed;
+ int method_used = DUNNO;
+ unsigned long ojiffs = jiffies;
if (preset_lpj) {
+ method_used = PRESET;
loops_per_jiffy = preset_lpj;
if (!printed)
pr_info("Calibrating delay loop (skipped) "
"preset value.. ");
} else if ((!printed) && lpj_fine) {
+ method_used = FINE;
loops_per_jiffy = lpj_fine;
pr_info("Calibrating delay loop (skipped), "
"value calculated using timer frequency.. ");
} else if ((loops_per_jiffy = calibrate_delay_direct()) != 0) {
+ method_used = DIRECT;
if (!printed)
pr_info("Calibrating delay using timer "
"specific routine.. ");
} else {
+ method_used = CONVERGE;
if (!printed)
pr_info("Calibrating delay loop... ");
loops_per_jiffy = calibrate_delay_converge();
}
- if (!printed)
- pr_cont("%lu.%02lu BogoMIPS (lpj=%lu)\n",
- loops_per_jiffy/(500000/HZ),
- (loops_per_jiffy/(5000/HZ)) % 100, loops_per_jiffy);
+ if (!printed) {
+ unsigned long tmp_lpj;
+ finally_print_bogomips(loops_per_jiffy, jiffies-ojiffs);
+ if (method_used != CONVERGE) {
+ ojiffs = jiffies;
+ pr_info("New-style converging method... ");
+ tmp_lpj = calibrate_delay_converge();
+ finally_print_bogomips(tmp_lpj, jiffies-ojiffs);
+ }
+ ojiffs = jiffies;
+ pr_info("Old-style converging method... ");
+ tmp_lpj = calibrate_delay_converge_slowly();
+ finally_print_bogomips(tmp_lpj, jiffies-ojiffs);
+ }
printed = true;
}
--
1.7.2.rc1.37.gf8c40
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [RFC 0/3] Improve fallback LPJ calculation
2010-09-10 9:54 [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
` (2 preceding siblings ...)
2010-09-10 9:54 ` [PATCH 3/3] DO NOT INTEGRATE: test-only timing Phil Carmody
@ 2010-09-22 9:22 ` Phil Carmody
3 siblings, 0 replies; 5+ messages in thread
From: Phil Carmody @ 2010-09-22 9:22 UTC (permalink / raw)
To: mingo, gregkh; +Cc: linux-kernel, travis, akataria, tony, linux-omap
Adding Tony and Linux-OMAP cc:, as this patchset was written specifically
with the OMAP in mind (thread rooted at http://lkml.org/lkml/2010/9/10/104 ).
Any comments would be appreciated.
Phil
On 10/09/10 11:54 +0200, Carmody Phil.2 (EXT-Ixonos/Helsinki) wrote:
>
> The motivation for patch 2/3 is that currently our OMAP calibrates
> itself using the trial-and-error binary chop fallback that some other
> architectures no longer need to perform. This is a lengthy process,
> taking 0.2s in an environment where boot time is of great interest.
>
> The patch has two optimisations. Firstly, it replaces the initial
> repeated-doubling to find the relevant power of 2 with a tight loop
> that just does as much as it can in a jiffy. Secondly, it doesn't
> binary chop over an entire power of 2 range, it choses a much smaller
> range based on how much it squeezed in, and failed to squeeze in,
> during the first stage. Both are significant optimisations, and
> bring our calibration down from 23 jiffies to 6, and, in the process,
> arrive at a more accurate lpj value.
>
> The 'bands' and 'sub-logarithmic' growth may look over-engineered,
> but they cost a small level of in inaccuracy of the initial guess
> (for all architectures) in order to avoid the very large inaccuracies
> that appeared during testing (on x86_64 architectures, and presumably
> others with less metronomic operation). Note that due to the existence
> of the TSC and other timers, the x86_64 doesn't use this fallback
> routine, but I wanted to code defensively, able to cope with all kinds
> of processor behaviours.
>
> 1/3 is simply cosmetic to prepare for 2/3.
> 3/3 is simply to assist testing and not intended for integration.
>
> I would appreciate it if people with exotic or unusual architectures
> could help test this.
>
> Cheers,
> Phil
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-09-22 9:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-10 9:54 [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
2010-09-10 9:54 ` [PATCH 1/3] calibrate: extract fall-back calculation into own helper Phil Carmody
2010-09-10 9:54 ` [PATCH 2/3] calibrate: home in on correct lpj bounds more quickly Phil Carmody
2010-09-10 9:54 ` [PATCH 3/3] DO NOT INTEGRATE: test-only timing Phil Carmody
2010-09-22 9:22 ` [RFC 0/3] Improve fallback LPJ calculation Phil Carmody
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®