blob: bddc80f28855db53e02fcd85d9d02e93b1837fa4 [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>
Mark Grossd82b3512008-02-04 22:30:08 -080015#include <linux/pm_qos_params.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>
Stephen Hemminger57875362010-01-08 14:43:08 -080021#include <linux/math64.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040022
Arjan van de Ven69d25872009-09-21 17:04:08 -070023#define BUCKETS 12
Arjan van de Ven1f85f872010-05-24 14:32:59 -070024#define INTERVALS 8
Arjan van de Ven69d25872009-09-21 17:04:08 -070025#define RESOLUTION 1024
Arjan van de Ven1f85f872010-05-24 14:32:59 -070026#define DECAY 8
Arjan van de Ven69d25872009-09-21 17:04:08 -070027#define MAX_INTERESTING 50000
Arjan van de Ven1f85f872010-05-24 14:32:59 -070028#define STDDEV_THRESH 400
29
Arjan van de Ven69d25872009-09-21 17:04:08 -070030
31/*
32 * Concepts and ideas behind the menu governor
33 *
34 * For the menu governor, there are 3 decision factors for picking a C
35 * state:
36 * 1) Energy break even point
37 * 2) Performance impact
38 * 3) Latency tolerance (from pmqos infrastructure)
39 * These these three factors are treated independently.
40 *
41 * Energy break even point
42 * -----------------------
43 * C state entry and exit have an energy cost, and a certain amount of time in
44 * the C state is required to actually break even on this cost. CPUIDLE
45 * provides us this duration in the "target_residency" field. So all that we
46 * need is a good prediction of how long we'll be idle. Like the traditional
47 * menu governor, we start with the actual known "next timer event" time.
48 *
49 * Since there are other source of wakeups (interrupts for example) than
50 * the next timer event, this estimation is rather optimistic. To get a
51 * more realistic estimate, a correction factor is applied to the estimate,
52 * that is based on historic behavior. For example, if in the past the actual
53 * duration always was 50% of the next timer tick, the correction factor will
54 * be 0.5.
55 *
56 * menu uses a running average for this correction factor, however it uses a
57 * set of factors, not just a single factor. This stems from the realization
58 * that the ratio is dependent on the order of magnitude of the expected
59 * duration; if we expect 500 milliseconds of idle time the likelihood of
60 * getting an interrupt very early is much higher than if we expect 50 micro
61 * seconds of idle time. A second independent factor that has big impact on
62 * the actual factor is if there is (disk) IO outstanding or not.
63 * (as a special twist, we consider every sleep longer than 50 milliseconds
64 * as perfect; there are no power gains for sleeping longer than this)
65 *
66 * For these two reasons we keep an array of 12 independent factors, that gets
67 * indexed based on the magnitude of the expected duration as well as the
68 * "is IO outstanding" property.
69 *
Arjan van de Ven1f85f872010-05-24 14:32:59 -070070 * Repeatable-interval-detector
71 * ----------------------------
72 * There are some cases where "next timer" is a completely unusable predictor:
73 * Those cases where the interval is fixed, for example due to hardware
74 * interrupt mitigation, but also due to fixed transfer rate devices such as
75 * mice.
76 * For this, we use a different predictor: We track the duration of the last 8
77 * intervals and if the stand deviation of these 8 intervals is below a
78 * threshold value, we use the average of these intervals as prediction.
79 *
Arjan van de Ven69d25872009-09-21 17:04:08 -070080 * Limiting Performance Impact
81 * ---------------------------
82 * C states, especially those with large exit latencies, can have a real
Lucas De Marchi20e33412010-09-07 12:53:49 -040083 * noticeable impact on workloads, which is not acceptable for most sysadmins,
Arjan van de Ven69d25872009-09-21 17:04:08 -070084 * and in addition, less performance has a power price of its own.
85 *
86 * As a general rule of thumb, menu assumes that the following heuristic
87 * holds:
88 * The busier the system, the less impact of C states is acceptable
89 *
90 * This rule-of-thumb is implemented using a performance-multiplier:
91 * If the exit latency times the performance multiplier is longer than
92 * the predicted duration, the C state is not considered a candidate
93 * for selection due to a too high performance impact. So the higher
94 * this multiplier is, the longer we need to be idle to pick a deep C
95 * state, and thus the less likely a busy CPU will hit such a deep
96 * C state.
97 *
98 * Two factors are used in determing this multiplier:
99 * a value of 10 is added for each point of "per cpu load average" we have.
100 * a value of 5 points is added for each process that is waiting for
101 * IO on this CPU.
102 * (these values are experimentally determined)
103 *
104 * The load average factor gives a longer term (few seconds) input to the
105 * decision, while the iowait value gives a cpu local instantanious input.
106 * The iowait factor may look low, but realize that this is also already
107 * represented in the system load average.
108 *
109 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400110
111struct menu_device {
112 int last_state_idx;
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700113 int needs_update;
Len Brown4f86d3a2007-10-03 18:58:00 -0400114
115 unsigned int expected_us;
Richard Kennedy56e6943b42010-03-05 13:42:30 -0800116 u64 predicted_us;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700117 unsigned int exit_us;
118 unsigned int bucket;
119 u64 correction_factor[BUCKETS];
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700120 u32 intervals[INTERVALS];
121 int interval_ptr;
Len Brown4f86d3a2007-10-03 18:58:00 -0400122};
123
Arjan van de Ven69d25872009-09-21 17:04:08 -0700124
125#define LOAD_INT(x) ((x) >> FSHIFT)
126#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
127
Arjan van de Ven69d25872009-09-21 17:04:08 -0700128static inline int which_bucket(unsigned int duration)
129{
130 int bucket = 0;
131
132 /*
133 * We keep two groups of stats; one with no
134 * IO pending, one without.
135 * This allows us to calculate
136 * E(duration)|iowait
137 */
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200138 if (nr_iowait_cpu(smp_processor_id()))
Arjan van de Ven69d25872009-09-21 17:04:08 -0700139 bucket = BUCKETS/2;
140
141 if (duration < 10)
142 return bucket;
143 if (duration < 100)
144 return bucket + 1;
145 if (duration < 1000)
146 return bucket + 2;
147 if (duration < 10000)
148 return bucket + 3;
149 if (duration < 100000)
150 return bucket + 4;
151 return bucket + 5;
152}
153
154/*
155 * Return a multiplier for the exit latency that is intended
156 * to take performance requirements into account.
157 * The more performance critical we estimate the system
158 * to be, the higher this multiplier, and thus the higher
159 * the barrier to go to an expensive C state.
160 */
161static inline int performance_multiplier(void)
162{
163 int mult = 1;
164
Arjan van de Ven69d25872009-09-21 17:04:08 -0700165 /* for IO wait tasks (per cpu!) we add 5x each */
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200166 mult += 10 * nr_iowait_cpu(smp_processor_id());
Arjan van de Ven69d25872009-09-21 17:04:08 -0700167
168 return mult;
169}
170
Len Brown4f86d3a2007-10-03 18:58:00 -0400171static DEFINE_PER_CPU(struct menu_device, menu_devices);
172
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700173static void menu_update(struct cpuidle_device *dev);
174
Stephen Hemminger57875362010-01-08 14:43:08 -0800175/* This implements DIV_ROUND_CLOSEST but avoids 64 bit division */
176static u64 div_round64(u64 dividend, u32 divisor)
177{
178 return div_u64(dividend + (divisor / 2), divisor);
179}
180
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700181/*
182 * Try detecting repeating patterns by keeping track of the last 8
183 * intervals, and checking if the standard deviation of that set
184 * of points is below a threshold. If it is... then use the
185 * average of these 8 points as the estimated value.
186 */
187static void detect_repeating_patterns(struct menu_device *data)
188{
189 int i;
190 uint64_t avg = 0;
191 uint64_t stddev = 0; /* contains the square of the std deviation */
192
193 /* first calculate average and standard deviation of the past */
194 for (i = 0; i < INTERVALS; i++)
195 avg += data->intervals[i];
196 avg = avg / INTERVALS;
197
198 /* if the avg is beyond the known next tick, it's worthless */
199 if (avg > data->expected_us)
200 return;
201
202 for (i = 0; i < INTERVALS; i++)
203 stddev += (data->intervals[i] - avg) *
204 (data->intervals[i] - avg);
205
206 stddev = stddev / INTERVALS;
207
208 /*
209 * now.. if stddev is small.. then assume we have a
210 * repeating pattern and predict we keep doing this.
211 */
212
213 if (avg && stddev < STDDEV_THRESH)
214 data->predicted_us = avg;
215}
216
Len Brown4f86d3a2007-10-03 18:58:00 -0400217/**
218 * menu_select - selects the next idle state to enter
219 * @dev: the CPU
220 */
221static int menu_select(struct cpuidle_device *dev)
222{
223 struct menu_device *data = &__get_cpu_var(menu_devices);
Mark Grossed771342010-05-06 01:59:26 +0200224 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Ai Li71abbbf2010-08-09 17:20:13 -0700225 unsigned int power_usage = -1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400226 int i;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700227 int multiplier;
Tero Kristo74675712011-02-24 17:19:23 +0200228 struct timespec t;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700229
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700230 if (data->needs_update) {
231 menu_update(dev);
232 data->needs_update = 0;
233 }
234
Arjan van de Ven1c6fe032010-05-08 15:47:37 -0700235 data->last_state_idx = 0;
236 data->exit_us = 0;
237
venkatesh.pallipadi@intel.coma2bd9202008-07-30 19:21:42 -0700238 /* Special case when user has set very strict latency requirement */
Arjan van de Ven69d25872009-09-21 17:04:08 -0700239 if (unlikely(latency_req == 0))
venkatesh.pallipadi@intel.coma2bd9202008-07-30 19:21:42 -0700240 return 0;
venkatesh.pallipadi@intel.coma2bd9202008-07-30 19:21:42 -0700241
Arjan van de Ven69d25872009-09-21 17:04:08 -0700242 /* determine the expected residency time, round up */
Tero Kristo74675712011-02-24 17:19:23 +0200243 t = ktime_to_timespec(tick_nohz_get_sleep_length());
Len Brown4f86d3a2007-10-03 18:58:00 -0400244 data->expected_us =
Tero Kristo74675712011-02-24 17:19:23 +0200245 t.tv_sec * USEC_PER_SEC + t.tv_nsec / NSEC_PER_USEC;
Len Brown4f86d3a2007-10-03 18:58:00 -0400246
Arjan van de Ven69d25872009-09-21 17:04:08 -0700247
248 data->bucket = which_bucket(data->expected_us);
249
250 multiplier = performance_multiplier();
251
252 /*
253 * if the correction factor is 0 (eg first time init or cpu hotplug
254 * etc), we actually want to start out with a unity factor.
255 */
256 if (data->correction_factor[data->bucket] == 0)
257 data->correction_factor[data->bucket] = RESOLUTION * DECAY;
258
259 /* Make sure to round up for half microseconds */
Stephen Hemminger57875362010-01-08 14:43:08 -0800260 data->predicted_us = div_round64(data->expected_us * data->correction_factor[data->bucket],
261 RESOLUTION * DECAY);
Arjan van de Ven69d25872009-09-21 17:04:08 -0700262
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700263 detect_repeating_patterns(data);
264
Arjan van de Ven69d25872009-09-21 17:04:08 -0700265 /*
266 * We want to default to C1 (hlt), not to busy polling
267 * unless the timer is happening really really soon.
268 */
269 if (data->expected_us > 5)
270 data->last_state_idx = CPUIDLE_DRIVER_STATE_START;
271
Ai Li71abbbf2010-08-09 17:20:13 -0700272 /*
273 * Find the idle state with the lowest power while satisfying
274 * our constraints.
275 */
Arjan van de Ven69d25872009-09-21 17:04:08 -0700276 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400277 struct cpuidle_state *s = &dev->states[i];
278
Ai Li71abbbf2010-08-09 17:20:13 -0700279 if (s->flags & CPUIDLE_FLAG_IGNORE)
280 continue;
Len Brown4f86d3a2007-10-03 18:58:00 -0400281 if (s->target_residency > data->predicted_us)
Ai Li71abbbf2010-08-09 17:20:13 -0700282 continue;
venkatesh.pallipadi@intel.coma2bd9202008-07-30 19:21:42 -0700283 if (s->exit_latency > latency_req)
Ai Li71abbbf2010-08-09 17:20:13 -0700284 continue;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700285 if (s->exit_latency * multiplier > data->predicted_us)
Ai Li71abbbf2010-08-09 17:20:13 -0700286 continue;
287
288 if (s->power_usage < power_usage) {
289 power_usage = s->power_usage;
290 data->last_state_idx = i;
291 data->exit_us = s->exit_latency;
292 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400293 }
294
Arjan van de Ven69d25872009-09-21 17:04:08 -0700295 return data->last_state_idx;
Len Brown4f86d3a2007-10-03 18:58:00 -0400296}
297
298/**
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700299 * menu_reflect - records that data structures need update
Len Brown4f86d3a2007-10-03 18:58:00 -0400300 * @dev: the CPU
301 *
302 * NOTE: it's important to be fast here because this operation will add to
303 * the overall exit latency.
304 */
305static void menu_reflect(struct cpuidle_device *dev)
306{
307 struct menu_device *data = &__get_cpu_var(menu_devices);
Corrado Zoccolo672917d2009-09-21 17:04:09 -0700308 data->needs_update = 1;
309}
310
311/**
312 * menu_update - attempts to guess what happened after entry
313 * @dev: the CPU
314 */
315static void menu_update(struct cpuidle_device *dev)
316{
317 struct menu_device *data = &__get_cpu_var(menu_devices);
Len Brown4f86d3a2007-10-03 18:58:00 -0400318 int last_idx = data->last_state_idx;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700319 unsigned int last_idle_us = cpuidle_get_last_residency(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -0400320 struct cpuidle_state *target = &dev->states[last_idx];
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700321 unsigned int measured_us;
Arjan van de Ven69d25872009-09-21 17:04:08 -0700322 u64 new_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -0400323
324 /*
325 * Ugh, this idle state doesn't support residency measurements, so we
326 * are basically lost in the dark. As a compromise, assume we slept
Arjan van de Ven69d25872009-09-21 17:04:08 -0700327 * for the whole expected time.
Len Brown4f86d3a2007-10-03 18:58:00 -0400328 */
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700329 if (unlikely(!(target->flags & CPUIDLE_FLAG_TIME_VALID)))
Arjan van de Ven69d25872009-09-21 17:04:08 -0700330 last_idle_us = data->expected_us;
331
332
333 measured_us = last_idle_us;
Len Brown4f86d3a2007-10-03 18:58:00 -0400334
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700335 /*
Arjan van de Ven69d25872009-09-21 17:04:08 -0700336 * We correct for the exit latency; we are assuming here that the
337 * exit latency happens after the event that we're interested in.
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700338 */
Arjan van de Ven69d25872009-09-21 17:04:08 -0700339 if (measured_us > data->exit_us)
340 measured_us -= data->exit_us;
341
342
343 /* update our correction ratio */
344
345 new_factor = data->correction_factor[data->bucket]
346 * (DECAY - 1) / DECAY;
347
Arjan van de Ven1c6fe032010-05-08 15:47:37 -0700348 if (data->expected_us > 0 && measured_us < MAX_INTERESTING)
Arjan van de Ven69d25872009-09-21 17:04:08 -0700349 new_factor += RESOLUTION * measured_us / data->expected_us;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700350 else
Arjan van de Ven69d25872009-09-21 17:04:08 -0700351 /*
352 * we were idle so long that we count it as a perfect
353 * prediction
354 */
355 new_factor += RESOLUTION;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700356
Arjan van de Ven69d25872009-09-21 17:04:08 -0700357 /*
358 * We don't want 0 as factor; we always want at least
359 * a tiny bit of estimated time.
360 */
361 if (new_factor == 0)
362 new_factor = 1;
venkatesh.pallipadi@intel.com320eee72008-07-30 19:21:43 -0700363
Arjan van de Ven69d25872009-09-21 17:04:08 -0700364 data->correction_factor[data->bucket] = new_factor;
Arjan van de Ven1f85f872010-05-24 14:32:59 -0700365
366 /* update the repeating-pattern data */
367 data->intervals[data->interval_ptr++] = last_idle_us;
368 if (data->interval_ptr >= INTERVALS)
369 data->interval_ptr = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -0400370}
371
372/**
373 * menu_enable_device - scans a CPU's states and does setup
374 * @dev: the CPU
375 */
376static int menu_enable_device(struct cpuidle_device *dev)
377{
378 struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
379
380 memset(data, 0, sizeof(struct menu_device));
381
382 return 0;
383}
384
385static struct cpuidle_governor menu_governor = {
386 .name = "menu",
387 .rating = 20,
388 .enable = menu_enable_device,
389 .select = menu_select,
390 .reflect = menu_reflect,
391 .owner = THIS_MODULE,
392};
393
394/**
395 * init_menu - initializes the governor
396 */
397static int __init init_menu(void)
398{
399 return cpuidle_register_governor(&menu_governor);
400}
401
402/**
403 * exit_menu - exits the governor
404 */
405static void __exit exit_menu(void)
406{
407 cpuidle_unregister_governor(&menu_governor);
408}
409
410MODULE_LICENSE("GPL");
411module_init(init_menu);
412module_exit(exit_menu);