in governors/menu.c [169:257]
static unsigned int get_typical_interval(struct menu_device *data,
unsigned int predicted_us)
{
int i, divisor;
unsigned int min, max, thresh, avg;
uint64_t sum, variance;
thresh = INT_MAX; /* Discard outliers above this value */
again:
/* First calculate the average of past intervals */
min = UINT_MAX;
max = 0;
sum = 0;
divisor = 0;
for (i = 0; i < INTERVALS; i++) {
unsigned int value = data->intervals[i];
if (value <= thresh) {
sum += value;
divisor++;
if (value > max)
max = value;
if (value < min)
min = value;
}
}
/*
* If the result of the computation is going to be discarded anyway,
* avoid the computation altogether.
*/
if (min >= predicted_us)
return UINT_MAX;
if (divisor == INTERVALS)
avg = sum >> INTERVAL_SHIFT;
else
avg = div_u64(sum, divisor);
/* Then try to determine variance */
variance = 0;
for (i = 0; i < INTERVALS; i++) {
unsigned int value = data->intervals[i];
if (value <= thresh) {
int64_t diff = (int64_t)value - avg;
variance += diff * diff;
}
}
if (divisor == INTERVALS)
variance >>= INTERVAL_SHIFT;
else
do_div(variance, divisor);
/*
* The typical interval is obtained when standard deviation is
* small (stddev <= 20 us, variance <= 400 us^2) or standard
* deviation is small compared to the average interval (avg >
* 6*stddev, avg^2 > 36*variance). The average is smaller than
* UINT_MAX aka U32_MAX, so computing its square does not
* overflow a u64. We simply reject this candidate average if
* the standard deviation is greater than 715 s (which is
* rather unlikely).
*
* Use this result only if there is no timer to wake us up sooner.
*/
if (likely(variance <= U64_MAX/36)) {
if ((((u64)avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
|| variance <= 400) {
return avg;
}
}
/*
* If we have outliers to the upside in our distribution, discard
* those by setting the threshold to exclude these outliers, then
* calculate the average and standard deviation again. Once we get
* down to the bottom 3/4 of our samples, stop excluding samples.
*
* This can deal with workloads that have long pauses interspersed
* with sporadic activity with a bunch of short pauses.
*/
if ((divisor * 4) <= INTERVALS * 3)
return UINT_MAX;
thresh = max - 1;
goto again;
}