blob: 72bc6e63584ccf8ae26da9fd9bb0aa826c1e7ec7 [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
2 * menu.c - the menu idle governor
3 *
4 * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
Arjan van de Ven69d25872009-09-21 17:04:08 -07005 * Copyright (C) 2009 Intel Corporation
6 * Author:
7 * Arjan van de Ven <arjan@linux.intel.com>
Len Brown4f86d3a2007-10-03 18:58:00 -04008 *
Arjan van de Ven69d25872009-09-21 17:04:08 -07009 * This code is licenced under the GPL version 2 as described
10 * in the COPYING file that acompanies the Linux Kernel.
Len Brown4f86d3a2007-10-03 18:58:00 -040011 */
12
13#include <linux/kernel.h>
14#include <linux/cpuidle.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020015#include <linux/pm_qos.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040016#include <linux/time.h>
17#include <linux/ktime.h>
18#include <linux/hrtimer.h>
19#include <linux/tick.h>
Arjan van de Ven69d25872009-09-21 17:04:08 -070020#include <linux/sched.h>
Ingo Molnarbadaff82017-02-08 08:45:17 +010021#include <linux/sched/loadavg.h>
Stephen Hemminger57875362010-01-08 14:43:08 -080022#include <linux/math64.h>
Paul Gortmaker884b17e2011-08-29 17:52:39 -040023#include <linux/module.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040024
Tuukka Tikkanendecd51b2013-08-14 19:02:40 +030025/*
26 * Please note when changing the tuning values:
27 * If (MAX_INTERESTING-1) * RESOLUTION > UINT_MAX, the result of
28 * a scaling operation multiplication may overflow on 32 bit platforms.
29 * In that case, #define RESOLUTION as ULL to get 64 bit result:
30 * #define RESOLUTION 1024ULL
31 *
32 * The default values do not overflow.
33 */
Arjan van de Ven69d25872009-09-21 17:04:08 -070034#define BUCKETS 12
Mel Gormanae779302014-08-06 14:19:18 +010035#define INTERVAL_SHIFT 3
36#define INTERVALS (1UL << INTERVAL_SHIFT)
Arjan van de Ven69d25872009-09-21 17:04:08 -070037#define RESOLUTION 1024
Arjan van de Ven1f85f872010-05-24 14:32:59 -070038#define DECAY 8
Arjan van de Ven69d25872009-09-21 17:04:08 -070039#define MAX_INTERESTING 50000
Arjan van de Ven1f85f872010-05-24 14:32:59 -070040
Arjan van de Ven69d25872009-09-21 17:04:08 -070041
42/*
43 * Concepts and ideas behind the menu governor
44 *
45 * For the menu governor, there are 3 decision factors for picking a C
46 * state:
47 * 1) Energy break even point
48 * 2) Performance impact
49 * 3) Latency tolerance (from pmqos infrastructure)
50 * These these three factors are treated independently.
51 *
52 * Energy break even point
53 * -----------------------
54 * C state entry and exit have an energy cost, and a certain amount of time in
55 * the C state is required to actually break even on this cost. CPUIDLE
56 * provides us this duration in the "target_residency" field. So all that we
57 * need is a good prediction of how long we'll be idle. Like the traditional
58 * menu governor, we start with the actual known "next timer event" time.
59 *
60 * Since there are other source of wakeups (interrupts for example) than
61 * the next timer event, this estimation is rather optimistic. To get a
62 * more realistic estimate, a correction factor is applied to the estimate,
63 * that is based on historic behavior. For example, if in the past the actual
64 * duration always was 50% of the next timer tick, the correction factor will
65 * be 0.5.
66 *
67 * menu uses a running average for this correction factor, however it uses a
68 * set of factors, not just a single factor. This stems from the realization
69 * that the ratio is dependent on the order of magnitude of the expected
70 * duration; if we expect 500 milliseconds of idle time the likelihood of
71 * getting an interrupt very early is much higher than if we expect 50 micro
72 * seconds of idle time. A second independent factor that has big impact on
73 * the actual factor is if there is (disk) IO outstanding or not.
74 * (as a special twist, we consider every sleep longer than 50 milliseconds
75 * as perfect; there are no power gains for sleeping longer than this)
76 *
77 * For these two reasons we keep an array of 12 independent factors, that gets
78 * indexed based on the magnitude of the expected duration as well as the
79 * "is IO outstanding" property.
80 *
Arjan van de Ven1f85f872010-05-24 14:32:59 -070081 * Repeatable-interval-detector
82 * ----------------------------
83 * There are some cases where "next timer" is a completely unusable predictor:
84 * Those cases where the interval is fixed, for example due to hardware
85 * interrupt mitigation, but also due to fixed transfer rate devices such as
86 * mice.
87 * For this, we use a different predictor: We track the duration of the last 8
88 * intervals and if the stand deviation of these 8 intervals is below a
89 * threshold value, we use the average of these intervals as prediction.
90 *
Arjan van de Ven69d25872009-09-21 17:04:08 -070091 * Limiting Performance Impact
92 * ---------------------------
93 * C states, especially those with large exit latencies, can have a real
Lucas De Marchi20e33412010-09-07 12:53:49 -040094 * noticeable impact on workloads, which is not acceptable for most sysadmins,
Arjan van de Ven69d25872009-09-21 17:04:08 -070095 * and in addition, less performance has a power price of its own.
96 *
97 * As a general rule of thumb, menu assumes that the following heuristic
98 * holds:
99 * The busier the system, the less impact of C states is acceptable
100 *
101 * This rule-of-thumb is implemented using a performance-multiplier:
102 * If the exit latency times the performance multiplier is longer than
103 * the predicted duration, the C state is not considered a candidate
104 * for selection due to a too high performance impact. So the higher
105 * this multiplier is, the longer we need to be idle to pick a deep C
106 * state, and thus the less likely a busy CPU will hit such a deep
107 * C state.
108 *
109 * Two factors are used in determing this multiplier:
110 * a value of 10 is added for each point of "per cpu load average" we have.
111 * a value of 5 points is added for each process that is waiting for
112 * IO on this CPU.
113 * (these values are experimentally determined)
114 *
115 * The load average factor gives a longer term (few seconds) input to the
116 * decision, while the iowait value gives a cpu local instantanious input.
117 * The iowait factor may look low, but realize that this is also already
118 * represented in the system load average.
119 *
120 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400121
122struct menu_device {
123 int last_state_idx;
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700124 int needs_update;
Len Brown4f86d3a2007-10-03 18:58:00 -0400125
tuukka.tikkanen@linaro.org5dc2f5a2014-02-24 08:29:31 +0200126 unsigned int next_timer_us;
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300127 unsigned int predicted_us;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700128 unsigned int bucket;
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300129 unsigned int correction_factor[BUCKETS];
Tuukka Tikkanen939e33b2013-08-14 19:02:38 +0300130 unsigned int intervals[INTERVALS];
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700131 int interval_ptr;
Len Brown4f86d3a2007-10-03 18:58:00 -0400132};
133
Mel Gorman372ba8c2014-08-06 14:19:21 +0100134static inline int get_loadavg(unsigned long load)
Arjan van de Ven69d25872009-09-21 17:04:08 -0700135{
Mel Gorman372ba8c2014-08-06 14:19:21 +0100136 return LOAD_INT(load) * 10 + LOAD_FRAC(load) / 10;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700137}
138
Mel Gorman64b4ca52014-08-06 14:19:20 +0100139static inline int which_bucket(unsigned int duration, unsigned long nr_iowaiters)
Arjan van de Ven69d25872009-09-21 17:04:08 -0700140{
141 int bucket = 0;
142
143 /*
144 * We keep two groups of stats; one with no
145 * IO pending, one without.
146 * This allows us to calculate
147 * E(duration)|iowait
148 */
Mel Gorman64b4ca52014-08-06 14:19:20 +0100149 if (nr_iowaiters)
Arjan van de Ven69d25872009-09-21 17:04:08 -0700150 bucket = BUCKETS/2;
151
152 if (duration < 10)
153 return bucket;
154 if (duration < 100)
155 return bucket + 1;
156 if (duration < 1000)
157 return bucket + 2;
158 if (duration < 10000)
159 return bucket + 3;
160 if (duration < 100000)
161 return bucket + 4;
162 return bucket + 5;
163}
164
165/*
166 * Return a multiplier for the exit latency that is intended
167 * to take performance requirements into account.
168 * The more performance critical we estimate the system
169 * to be, the higher this multiplier, and thus the higher
170 * the barrier to go to an expensive C state.
171 */
Mel Gorman372ba8c2014-08-06 14:19:21 +0100172static inline int performance_multiplier(unsigned long nr_iowaiters, unsigned long load)
Arjan van de Ven69d25872009-09-21 17:04:08 -0700173{
174 int mult = 1;
175
176 /* for higher loadavg, we are more reluctant */
177
Colin Cross81863d62011-09-19 16:42:44 -0700178 /*
179 * this doesn't work as intended - it is almost always 0, but can
180 * sometimes, depending on workload, spike very high into the hundreds
181 * even when the average cpu load is under 10%.
182 */
183 /* mult += 2 * get_loadavg(); */
Arjan van de Ven69d25872009-09-21 17:04:08 -0700184
185 /* for IO wait tasks (per cpu!) we add 5x each */
Mel Gorman64b4ca52014-08-06 14:19:20 +0100186 mult += 10 * nr_iowaiters;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700187
188 return mult;
189}
190
Len Brown4f86d3a2007-10-03 18:58:00 -0400191static DEFINE_PER_CPU(struct menu_device, menu_devices);
192
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530193static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev);
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700194
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700195/*
196 * Try detecting repeating patterns by keeping track of the last 8
197 * intervals, and checking if the standard deviation of that set
198 * of points is below a threshold. If it is... then use the
199 * average of these 8 points as the estimated value.
200 */
Rik van Riele132b9b2016-03-16 12:14:00 -0400201static unsigned int get_typical_interval(struct menu_device *data)
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700202{
Tuukka Tikkanen4cd46bc2013-08-14 19:02:37 +0300203 int i, divisor;
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100204 unsigned int max, thresh, avg;
205 uint64_t sum, variance;
Tuukka Tikkanen0e96d5a2013-08-14 19:02:39 +0300206
207 thresh = UINT_MAX; /* Discard outliers above this value */
Youquan Songc96ca4f2012-10-26 12:27:07 +0200208
209again:
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700210
Tuukka Tikkanen0e96d5a2013-08-14 19:02:39 +0300211 /* First calculate the average of past intervals */
Tuukka Tikkanen4cd46bc2013-08-14 19:02:37 +0300212 max = 0;
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100213 sum = 0;
Tuukka Tikkanen4cd46bc2013-08-14 19:02:37 +0300214 divisor = 0;
Youquan Songc96ca4f2012-10-26 12:27:07 +0200215 for (i = 0; i < INTERVALS; i++) {
Tuukka Tikkanen0e96d5a2013-08-14 19:02:39 +0300216 unsigned int value = data->intervals[i];
Youquan Songc96ca4f2012-10-26 12:27:07 +0200217 if (value <= thresh) {
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100218 sum += value;
Youquan Songc96ca4f2012-10-26 12:27:07 +0200219 divisor++;
220 if (value > max)
221 max = value;
222 }
223 }
Mel Gormanae779302014-08-06 14:19:18 +0100224 if (divisor == INTERVALS)
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100225 avg = sum >> INTERVAL_SHIFT;
Mel Gormanae779302014-08-06 14:19:18 +0100226 else
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100227 avg = div_u64(sum, divisor);
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700228
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100229 /* Then try to determine variance */
230 variance = 0;
Youquan Songc96ca4f2012-10-26 12:27:07 +0200231 for (i = 0; i < INTERVALS; i++) {
Tuukka Tikkanen0e96d5a2013-08-14 19:02:39 +0300232 unsigned int value = data->intervals[i];
Youquan Songc96ca4f2012-10-26 12:27:07 +0200233 if (value <= thresh) {
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100234 int64_t diff = (int64_t)value - avg;
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100235 variance += diff * diff;
Youquan Songc96ca4f2012-10-26 12:27:07 +0200236 }
237 }
Mel Gormanae779302014-08-06 14:19:18 +0100238 if (divisor == INTERVALS)
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100239 variance >>= INTERVAL_SHIFT;
Mel Gormanae779302014-08-06 14:19:18 +0100240 else
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100241 do_div(variance, divisor);
Mel Gormanae779302014-08-06 14:19:18 +0100242
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700243 /*
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100244 * The typical interval is obtained when standard deviation is
245 * small (stddev <= 20 us, variance <= 400 us^2) or standard
246 * deviation is small compared to the average interval (avg >
247 * 6*stddev, avg^2 > 36*variance). The average is smaller than
248 * UINT_MAX aka U32_MAX, so computing its square does not
249 * overflow a u64. We simply reject this candidate average if
250 * the standard deviation is greater than 715 s (which is
251 * rather unlikely).
Tuukka Tikkanen0d6a7ff2013-08-14 19:02:36 +0300252 *
Tuukka Tikkanen330647a2013-08-14 19:02:34 +0300253 * Use this result only if there is no timer to wake us up sooner.
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700254 */
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100255 if (likely(variance <= U64_MAX/36)) {
Rasmus Villemoes3b996692016-02-16 20:19:19 +0100256 if ((((u64)avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
Rasmus Villemoes7024b182016-02-16 20:19:18 +0100257 || variance <= 400) {
Rik van Riele132b9b2016-03-16 12:14:00 -0400258 return avg;
Tuukka Tikkanen0d6a7ff2013-08-14 19:02:36 +0300259 }
Youquan Song69a37be2012-10-26 12:26:41 +0200260 }
Tuukka Tikkanen017099e2013-08-14 19:02:35 +0300261
262 /*
263 * If we have outliers to the upside in our distribution, discard
264 * those by setting the threshold to exclude these outliers, then
265 * calculate the average and standard deviation again. Once we get
266 * down to the bottom 3/4 of our samples, stop excluding samples.
267 *
268 * This can deal with workloads that have long pauses interspersed
269 * with sporadic activity with a bunch of short pauses.
270 */
271 if ((divisor * 4) <= INTERVALS * 3)
Rik van Riele132b9b2016-03-16 12:14:00 -0400272 return UINT_MAX;
Tuukka Tikkanen017099e2013-08-14 19:02:35 +0300273
274 thresh = max - 1;
275 goto again;
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700276}
277
Len Brown4f86d3a2007-10-03 18:58:00 -0400278/**
279 * menu_select - selects the next idle state to enter
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530280 * @drv: cpuidle driver containing state data
Len Brown4f86d3a2007-10-03 18:58:00 -0400281 * @dev: the CPU
282 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530283static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400284{
Christoph Lameter229b6862014-08-17 12:30:30 -0500285 struct menu_device *data = this_cpu_ptr(&menu_devices);
Mark Grossed771342010-05-06 01:59:26 +0200286 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Len Brown4f86d3a2007-10-03 18:58:00 -0400287 int i;
tuukka.tikkanen@linaro.org96e95182014-02-24 08:29:35 +0200288 unsigned int interactivity_req;
Rik van Riele132b9b2016-03-16 12:14:00 -0400289 unsigned int expected_interval;
Mel Gorman372ba8c2014-08-06 14:19:21 +0100290 unsigned long nr_iowaiters, cpu_load;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700291
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700292 if (data->needs_update) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530293 menu_update(drv, dev);
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700294 data->needs_update = 0;
295 }
296
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700297 /* Special case when user has set very strict latency requirement */
Arjan van de Ven69d25872009-09-21 17:04:08 -0700298 if (unlikely(latency_req == 0))
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700299 return 0;
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700300
Arjan van de Ven69d25872009-09-21 17:04:08 -0700301 /* determine the expected residency time, round up */
Mel Gorman107d4f42014-08-06 14:19:19 +0100302 data->next_timer_us = ktime_to_us(tick_nohz_get_sleep_length());
Len Brown4f86d3a2007-10-03 18:58:00 -0400303
Mel Gorman372ba8c2014-08-06 14:19:21 +0100304 get_iowait_load(&nr_iowaiters, &cpu_load);
Mel Gorman64b4ca52014-08-06 14:19:20 +0100305 data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
Arjan van de Ven69d25872009-09-21 17:04:08 -0700306
Arjan van de Ven69d25872009-09-21 17:04:08 -0700307 /*
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300308 * Force the result of multiplication to be 64 bits even if both
309 * operands are 32 bits.
310 * Make sure to round up for half microseconds.
311 */
Javi Merinoee3c86f2015-04-16 12:43:51 -0700312 data->predicted_us = DIV_ROUND_CLOSEST_ULL((uint64_t)data->next_timer_us *
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300313 data->correction_factor[data->bucket],
Stephen Hemminger57875362010-01-08 14:43:08 -0800314 RESOLUTION * DECAY);
Arjan van de Ven69d25872009-09-21 17:04:08 -0700315
Rik van Riele132b9b2016-03-16 12:14:00 -0400316 expected_interval = get_typical_interval(data);
317 expected_interval = min(expected_interval, data->next_timer_us);
tuukka.tikkanen@linaro.org96e95182014-02-24 08:29:35 +0200318
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100319 if (CPUIDLE_DRIVER_STATE_START > 0) {
Rafael J. Wysocki0c313cb2016-03-20 01:33:35 +0100320 struct cpuidle_state *s = &drv->states[CPUIDLE_DRIVER_STATE_START];
321 unsigned int polling_threshold;
322
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100323 /*
324 * We want to default to C1 (hlt), not to busy polling
Rik van Riele132b9b2016-03-16 12:14:00 -0400325 * unless the timer is happening really really soon, or
326 * C1's exit latency exceeds the user configured limit.
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100327 */
Rafael J. Wysocki0c313cb2016-03-20 01:33:35 +0100328 polling_threshold = max_t(unsigned int, 20, s->target_residency);
329 if (data->next_timer_us > polling_threshold &&
330 latency_req > s->exit_latency && !s->disabled &&
Rik van Riele132b9b2016-03-16 12:14:00 -0400331 !dev->states_usage[CPUIDLE_DRIVER_STATE_START].disable)
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100332 data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
Rafael J. Wysocki0c313cb2016-03-20 01:33:35 +0100333 else
334 data->last_state_idx = CPUIDLE_DRIVER_STATE_START - 1;
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100335 } else {
Arjan van de Ven69d25872009-09-21 17:04:08 -0700336 data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
Rafael J. Wysocki9c4b2862016-01-14 23:24:22 +0100337 }
Arjan van de Ven69d25872009-09-21 17:04:08 -0700338
Ai Li71abbbf2010-08-09 17:20:13 -0700339 /*
Rik van Riele132b9b2016-03-16 12:14:00 -0400340 * Use the lowest expected idle interval to pick the idle state.
341 */
342 data->predicted_us = min(data->predicted_us, expected_interval);
343
344 /*
345 * Use the performance multiplier and the user-configurable
346 * latency_req to determine the maximum exit latency.
347 */
348 interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load);
349 if (latency_req > interactivity_req)
350 latency_req = interactivity_req;
351
352 /*
Ai Li71abbbf2010-08-09 17:20:13 -0700353 * Find the idle state with the lowest power while satisfying
354 * our constraints.
355 */
Rafael J. Wysocki5bb17292016-01-16 00:56:34 +0100356 for (i = data->last_state_idx + 1; i < drv->state_count; i++) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530357 struct cpuidle_state *s = &drv->states[i];
ShuoX Liudc7fd272012-07-03 19:05:31 +0200358 struct cpuidle_state_usage *su = &dev->states_usage[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400359
Rafael J. Wysockicbc9ef02012-07-03 19:07:42 +0200360 if (s->disabled || su->disable)
ShuoX Liu3a533962012-03-28 15:19:11 -0700361 continue;
Rafael J. Wysocki14851912013-07-27 01:41:34 +0200362 if (s->target_residency > data->predicted_us)
Ai Li71abbbf2010-08-09 17:20:13 -0700363 continue;
venkatesh.pallipadi@intel.coma2bd92022008-07-30 19:21:42 -0700364 if (s->exit_latency > latency_req)
Ai Li71abbbf2010-08-09 17:20:13 -0700365 continue;
Ai Li71abbbf2010-08-09 17:20:13 -0700366
Daniel Lezcano8aef33a2013-01-15 14:18:04 +0100367 data->last_state_idx = i;
Len Brown4f86d3a2007-10-03 18:58:00 -0400368 }
369
Arjan van de Ven69d25872009-09-21 17:04:08 -0700370 return data->last_state_idx;
Len Brown4f86d3a2007-10-03 18:58:00 -0400371}
372
373/**
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700374 * menu_reflect - records that data structures need update
Len Brown4f86d3a2007-10-03 18:58:00 -0400375 * @dev: the CPU
Deepthi Dharware978aa72011-10-28 16:20:09 +0530376 * @index: the index of actual entered state
Len Brown4f86d3a2007-10-03 18:58:00 -0400377 *
378 * NOTE: it's important to be fast here because this operation will add to
379 * the overall exit latency.
380 */
Deepthi Dharware978aa72011-10-28 16:20:09 +0530381static void menu_reflect(struct cpuidle_device *dev, int index)
Len Brown4f86d3a2007-10-03 18:58:00 -0400382{
Christoph Lameter229b6862014-08-17 12:30:30 -0500383 struct menu_device *data = this_cpu_ptr(&menu_devices);
Rafael J. Wysockia802ea92015-05-04 22:53:28 +0200384
Deepthi Dharware978aa72011-10-28 16:20:09 +0530385 data->last_state_idx = index;
Rafael J. Wysockia802ea92015-05-04 22:53:28 +0200386 data->needs_update = 1;
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700387}
388
389/**
390 * menu_update - attempts to guess what happened after entry
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530391 * @drv: cpuidle driver containing state data
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700392 * @dev: the CPU
393 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530394static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700395{
Christoph Lameter229b6862014-08-17 12:30:30 -0500396 struct menu_device *data = this_cpu_ptr(&menu_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400397 int last_idx = data->last_state_idx;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530398 struct cpuidle_state *target = &drv->states[last_idx];
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700399 unsigned int measured_us;
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300400 unsigned int new_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -0400401
402 /*
tuukka.tikkanen@linaro.org61c66d62014-02-24 08:29:34 +0200403 * Try to figure out how much time passed between entry to low
404 * power state and occurrence of the wakeup event.
405 *
406 * If the entered idle state didn't support residency measurements,
Len Brown4108b3d2014-12-16 01:52:06 -0500407 * we use them anyway if they are short, and if long,
408 * truncate to the whole expected time.
tuukka.tikkanen@linaro.org61c66d62014-02-24 08:29:34 +0200409 *
410 * Any measured amount of time will include the exit latency.
411 * Since we are interested in when the wakeup begun, not when it
Antonio Ospite2fba5372014-06-04 14:03:45 +0200412 * was completed, we must subtract the exit latency. However, if
tuukka.tikkanen@linaro.org61c66d62014-02-24 08:29:34 +0200413 * the measured amount of time is less than the exit latency,
414 * assume the state was never reached and the exit latency is 0.
Len Brown4f86d3a2007-10-03 18:58:00 -0400415 */
Len Brown4108b3d2014-12-16 01:52:06 -0500416
417 /* measured value */
418 measured_us = cpuidle_get_last_residency(dev);
419
420 /* Deduct exit latency */
Rik van Rielefddfd92015-11-03 17:34:19 -0500421 if (measured_us > 2 * target->exit_latency)
Len Brown4108b3d2014-12-16 01:52:06 -0500422 measured_us -= target->exit_latency;
Rik van Rielefddfd92015-11-03 17:34:19 -0500423 else
424 measured_us /= 2;
Len Brown4108b3d2014-12-16 01:52:06 -0500425
426 /* Make sure our coefficients do not exceed unity */
427 if (measured_us > data->next_timer_us)
tuukka.tikkanen@linaro.org7ac26432014-02-24 08:29:33 +0200428 measured_us = data->next_timer_us;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700429
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300430 /* Update our correction ratio */
431 new_factor = data->correction_factor[data->bucket];
432 new_factor -= new_factor / DECAY;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700433
tuukka.tikkanen@linaro.org5dc2f5a2014-02-24 08:29:31 +0200434 if (data->next_timer_us > 0 && measured_us < MAX_INTERESTING)
435 new_factor += RESOLUTION * measured_us / data->next_timer_us;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700436 else
Arjan van de Ven69d25872009-09-21 17:04:08 -0700437 /*
438 * we were idle so long that we count it as a perfect
439 * prediction
440 */
441 new_factor += RESOLUTION;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700442
Arjan van de Ven69d25872009-09-21 17:04:08 -0700443 /*
444 * We don't want 0 as factor; we always want at least
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300445 * a tiny bit of estimated time. Fortunately, due to rounding,
446 * new_factor will stay nonzero regardless of measured_us values
447 * and the compiler can eliminate this test as long as DECAY > 1.
Arjan van de Ven69d25872009-09-21 17:04:08 -0700448 */
Tuukka Tikkanen51f245b2013-08-14 19:02:41 +0300449 if (DECAY == 1 && unlikely(new_factor == 0))
Arjan van de Ven69d25872009-09-21 17:04:08 -0700450 new_factor = 1;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700451
Arjan van de Ven69d25872009-09-21 17:04:08 -0700452 data->correction_factor[data->bucket] = new_factor;
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700453
454 /* update the repeating-pattern data */
tuukka.tikkanen@linaro.org61c66d62014-02-24 08:29:34 +0200455 data->intervals[data->interval_ptr++] = measured_us;
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700456 if (data->interval_ptr >= INTERVALS)
457 data->interval_ptr = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400458}
459
460/**
461 * menu_enable_device - scans a CPU's states and does setup
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530462 * @drv: cpuidle driver
Len Brown4f86d3a2007-10-03 18:58:00 -0400463 * @dev: the CPU
464 */
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530465static int menu_enable_device(struct cpuidle_driver *drv,
466 struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400467{
468 struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
Chander Kashyapbed4d592014-04-22 18:08:04 +0530469 int i;
Len Brown4f86d3a2007-10-03 18:58:00 -0400470
471 memset(data, 0, sizeof(struct menu_device));
472
Chander Kashyapbed4d592014-04-22 18:08:04 +0530473 /*
474 * if the correction factor is 0 (eg first time init or cpu hotplug
475 * etc), we actually want to start out with a unity factor.
476 */
477 for(i = 0; i < BUCKETS; i++)
478 data->correction_factor[i] = RESOLUTION * DECAY;
479
Len Brown4f86d3a2007-10-03 18:58:00 -0400480 return 0;
481}
482
483static struct cpuidle_governor menu_governor = {
484 .name = "menu",
485 .rating = 20,
486 .enable = menu_enable_device,
487 .select = menu_select,
488 .reflect = menu_reflect,
489 .owner = THIS_MODULE,
490};
491
492/**
493 * init_menu - initializes the governor
494 */
495static int __init init_menu(void)
496{
497 return cpuidle_register_governor(&menu_governor);
498}
499
Daniel Lezcano137b9442013-06-12 15:08:48 +0200500postcore_initcall(init_menu);