Commit 0e96d5ad authored by Tuukka Tikkanen's avatar Tuukka Tikkanen Committed by Rafael J. Wysocki

cpuidle: Fix variable domains in get_typical_interval()

The menu governor uses a static function get_typical_interval() to
try to detect a repeating pattern of wakeups. The previous interval
durations are stored as an array of unsigned ints, but the arithmetic
in the function is performed exclusively as 64 bit values, even when
the value stored in a variable is known not to exceed unsigned int,
which may be smaller and more efficient on some platforms.

This patch changes the types of varibles used to store some
intermediates, the maximum and and the cutoff threshold to unsigned
ints. Average and standard deviation are still treated as 64 bit values,
even when the values are known to be within the domain of unsigned int,
to avoid casts to ensure correct integer promotion for arithmetic
operations.
Signed-off-by: default avatarTuukka Tikkanen <tuukka.tikkanen@linaro.org>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 939e33b7
...@@ -200,18 +200,19 @@ static u64 div_round64(u64 dividend, u32 divisor) ...@@ -200,18 +200,19 @@ static u64 div_round64(u64 dividend, u32 divisor)
static void get_typical_interval(struct menu_device *data) static void get_typical_interval(struct menu_device *data)
{ {
int i, divisor; int i, divisor;
uint64_t max, avg, stddev; unsigned int max, thresh;
int64_t thresh = LLONG_MAX; /* Discard outliers above this value. */ uint64_t avg, stddev;
thresh = UINT_MAX; /* Discard outliers above this value */
again: again:
/* first calculate average and standard deviation of the past */ /* First calculate the average of past intervals */
max = 0; max = 0;
avg = 0; avg = 0;
divisor = 0; divisor = 0;
stddev = 0;
for (i = 0; i < INTERVALS; i++) { for (i = 0; i < INTERVALS; i++) {
int64_t value = data->intervals[i]; unsigned int value = data->intervals[i];
if (value <= thresh) { if (value <= thresh) {
avg += value; avg += value;
divisor++; divisor++;
...@@ -221,8 +222,10 @@ static void get_typical_interval(struct menu_device *data) ...@@ -221,8 +222,10 @@ static void get_typical_interval(struct menu_device *data)
} }
do_div(avg, divisor); do_div(avg, divisor);
/* Then try to determine standard deviation */
stddev = 0;
for (i = 0; i < INTERVALS; i++) { for (i = 0; i < INTERVALS; i++) {
int64_t 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; stddev += diff * diff;
......
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