Commit 7024b18c authored by Rasmus Villemoes's avatar Rasmus Villemoes Committed by Rafael J. Wysocki

cpuidle: menu: avoid expensive square root computation

Computing the integer square root is a rather expensive operation, at
least compared to doing a 64x64 -> 64 multiply (avg*avg) and, on 64
bit platforms, doing an extra comparison to a constant (variance <=
U64_MAX/36).

On 64 bit platforms, this does mean that we add a restriction on the
range of the variance where we end up using the estimate (since
previously the stddev <= ULONG_MAX was a tautology), but on the other
hand, we extend the range quite substantially on 32 bit platforms - in
both cases, we now allow standard deviations up to 715 seconds, which
is for example guaranteed if all observations are less than 1430
seconds.
Signed-off-by: default avatarRasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 18558cae
...@@ -200,7 +200,7 @@ static void get_typical_interval(struct menu_device *data) ...@@ -200,7 +200,7 @@ static void get_typical_interval(struct menu_device *data)
{ {
int i, divisor; int i, divisor;
unsigned int max, thresh; unsigned int max, thresh;
uint64_t avg, stddev; uint64_t avg, variance;
thresh = UINT_MAX; /* Discard outliers above this value */ thresh = UINT_MAX; /* Discard outliers above this value */
...@@ -224,36 +224,35 @@ static void get_typical_interval(struct menu_device *data) ...@@ -224,36 +224,35 @@ static void get_typical_interval(struct menu_device *data)
else else
do_div(avg, divisor); do_div(avg, divisor);
/* Then try to determine standard deviation */ /* Then try to determine variance */
stddev = 0; variance = 0;
for (i = 0; i < INTERVALS; i++) { for (i = 0; i < INTERVALS; i++) {
unsigned int value = data->intervals[i]; unsigned int value = data->intervals[i];
if (value <= thresh) { if (value <= thresh) {
int64_t diff = value - avg; int64_t diff = value - avg;
stddev += diff * diff; variance += diff * diff;
} }
} }
if (divisor == INTERVALS) if (divisor == INTERVALS)
stddev >>= INTERVAL_SHIFT; variance >>= INTERVAL_SHIFT;
else else
do_div(stddev, divisor); do_div(variance, divisor);
/* /*
* The typical interval is obtained when standard deviation is small * The typical interval is obtained when standard deviation is
* or standard deviation is small compared to the average interval. * small (stddev <= 20 us, variance <= 400 us^2) or standard
* * deviation is small compared to the average interval (avg >
* int_sqrt() formal parameter type is unsigned long. When the * 6*stddev, avg^2 > 36*variance). The average is smaller than
* greatest difference to an outlier exceeds ~65 ms * sqrt(divisor) * UINT_MAX aka U32_MAX, so computing its square does not
* the resulting squared standard deviation exceeds the input domain * overflow a u64. We simply reject this candidate average if
* of int_sqrt on platforms where unsigned long is 32 bits in size. * the standard deviation is greater than 715 s (which is
* In such case reject the candidate average. * rather unlikely).
* *
* Use this result only if there is no timer to wake us up sooner. * Use this result only if there is no timer to wake us up sooner.
*/ */
if (likely(stddev <= ULONG_MAX)) { if (likely(variance <= U64_MAX/36)) {
stddev = int_sqrt(stddev); if (((avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3)) || variance <= 400) {
|| stddev <= 20) {
if (data->next_timer_us > avg) if (data->next_timer_us > avg)
data->predicted_us = avg; data->predicted_us = avg;
return; return;
......
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