]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/cpuidle/governors/menu.c
PM / QoS: Fix device resume latency framework
[mirror_ubuntu-bionic-kernel.git] / drivers / cpuidle / governors / menu.c
CommitLineData
4f86d3a8
LB
1/*
2 * menu.c - the menu idle governor
3 *
4 * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
69d25870
AV
5 * Copyright (C) 2009 Intel Corporation
6 * Author:
7 * Arjan van de Ven <arjan@linux.intel.com>
4f86d3a8 8 *
69d25870
AV
9 * This code is licenced under the GPL version 2 as described
10 * in the COPYING file that acompanies the Linux Kernel.
4f86d3a8
LB
11 */
12
13#include <linux/kernel.h>
14#include <linux/cpuidle.h>
e8db0be1 15#include <linux/pm_qos.h>
4f86d3a8
LB
16#include <linux/time.h>
17#include <linux/ktime.h>
18#include <linux/hrtimer.h>
19#include <linux/tick.h>
69d25870 20#include <linux/sched.h>
4f17722c 21#include <linux/sched/loadavg.h>
03441a34 22#include <linux/sched/stat.h>
5787536e 23#include <linux/math64.h>
9908859a 24#include <linux/cpu.h>
4f86d3a8 25
decd51bb
TT
26/*
27 * Please note when changing the tuning values:
28 * If (MAX_INTERESTING-1) * RESOLUTION > UINT_MAX, the result of
29 * a scaling operation multiplication may overflow on 32 bit platforms.
30 * In that case, #define RESOLUTION as ULL to get 64 bit result:
31 * #define RESOLUTION 1024ULL
32 *
33 * The default values do not overflow.
34 */
69d25870 35#define BUCKETS 12
ae779300
MG
36#define INTERVAL_SHIFT 3
37#define INTERVALS (1UL << INTERVAL_SHIFT)
69d25870 38#define RESOLUTION 1024
1f85f87d 39#define DECAY 8
69d25870 40#define MAX_INTERESTING 50000
1f85f87d 41
69d25870
AV
42
43/*
44 * Concepts and ideas behind the menu governor
45 *
46 * For the menu governor, there are 3 decision factors for picking a C
47 * state:
48 * 1) Energy break even point
49 * 2) Performance impact
50 * 3) Latency tolerance (from pmqos infrastructure)
51 * These these three factors are treated independently.
52 *
53 * Energy break even point
54 * -----------------------
55 * C state entry and exit have an energy cost, and a certain amount of time in
56 * the C state is required to actually break even on this cost. CPUIDLE
57 * provides us this duration in the "target_residency" field. So all that we
58 * need is a good prediction of how long we'll be idle. Like the traditional
59 * menu governor, we start with the actual known "next timer event" time.
60 *
61 * Since there are other source of wakeups (interrupts for example) than
62 * the next timer event, this estimation is rather optimistic. To get a
63 * more realistic estimate, a correction factor is applied to the estimate,
64 * that is based on historic behavior. For example, if in the past the actual
65 * duration always was 50% of the next timer tick, the correction factor will
66 * be 0.5.
67 *
68 * menu uses a running average for this correction factor, however it uses a
69 * set of factors, not just a single factor. This stems from the realization
70 * that the ratio is dependent on the order of magnitude of the expected
71 * duration; if we expect 500 milliseconds of idle time the likelihood of
72 * getting an interrupt very early is much higher than if we expect 50 micro
73 * seconds of idle time. A second independent factor that has big impact on
74 * the actual factor is if there is (disk) IO outstanding or not.
75 * (as a special twist, we consider every sleep longer than 50 milliseconds
76 * as perfect; there are no power gains for sleeping longer than this)
77 *
78 * For these two reasons we keep an array of 12 independent factors, that gets
79 * indexed based on the magnitude of the expected duration as well as the
80 * "is IO outstanding" property.
81 *
1f85f87d
AV
82 * Repeatable-interval-detector
83 * ----------------------------
84 * There are some cases where "next timer" is a completely unusable predictor:
85 * Those cases where the interval is fixed, for example due to hardware
86 * interrupt mitigation, but also due to fixed transfer rate devices such as
87 * mice.
88 * For this, we use a different predictor: We track the duration of the last 8
89 * intervals and if the stand deviation of these 8 intervals is below a
90 * threshold value, we use the average of these intervals as prediction.
91 *
69d25870
AV
92 * Limiting Performance Impact
93 * ---------------------------
94 * C states, especially those with large exit latencies, can have a real
20e3341b 95 * noticeable impact on workloads, which is not acceptable for most sysadmins,
69d25870
AV
96 * and in addition, less performance has a power price of its own.
97 *
98 * As a general rule of thumb, menu assumes that the following heuristic
99 * holds:
100 * The busier the system, the less impact of C states is acceptable
101 *
102 * This rule-of-thumb is implemented using a performance-multiplier:
103 * If the exit latency times the performance multiplier is longer than
104 * the predicted duration, the C state is not considered a candidate
105 * for selection due to a too high performance impact. So the higher
106 * this multiplier is, the longer we need to be idle to pick a deep C
107 * state, and thus the less likely a busy CPU will hit such a deep
108 * C state.
109 *
110 * Two factors are used in determing this multiplier:
111 * a value of 10 is added for each point of "per cpu load average" we have.
112 * a value of 5 points is added for each process that is waiting for
113 * IO on this CPU.
114 * (these values are experimentally determined)
115 *
116 * The load average factor gives a longer term (few seconds) input to the
117 * decision, while the iowait value gives a cpu local instantanious input.
118 * The iowait factor may look low, but realize that this is also already
119 * represented in the system load average.
120 *
121 */
4f86d3a8
LB
122
123struct menu_device {
124 int last_state_idx;
672917dc 125 int needs_update;
4f86d3a8 126
5dc2f5a3 127 unsigned int next_timer_us;
51f245b8 128 unsigned int predicted_us;
69d25870 129 unsigned int bucket;
51f245b8 130 unsigned int correction_factor[BUCKETS];
939e33b7 131 unsigned int intervals[INTERVALS];
1f85f87d 132 int interval_ptr;
4f86d3a8
LB
133};
134
69d25870
AV
135
136#define LOAD_INT(x) ((x) >> FSHIFT)
137#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
138
372ba8cb 139static inline int get_loadavg(unsigned long load)
69d25870 140{
372ba8cb 141 return LOAD_INT(load) * 10 + LOAD_FRAC(load) / 10;
69d25870
AV
142}
143
64b4ca5c 144static inline int which_bucket(unsigned int duration, unsigned long nr_iowaiters)
69d25870
AV
145{
146 int bucket = 0;
147
148 /*
149 * We keep two groups of stats; one with no
150 * IO pending, one without.
151 * This allows us to calculate
152 * E(duration)|iowait
153 */
64b4ca5c 154 if (nr_iowaiters)
69d25870
AV
155 bucket = BUCKETS/2;
156
157 if (duration < 10)
158 return bucket;
159 if (duration < 100)
160 return bucket + 1;
161 if (duration < 1000)
162 return bucket + 2;
163 if (duration < 10000)
164 return bucket + 3;
165 if (duration < 100000)
166 return bucket + 4;
167 return bucket + 5;
168}
169
170/*
171 * Return a multiplier for the exit latency that is intended
172 * to take performance requirements into account.
173 * The more performance critical we estimate the system
174 * to be, the higher this multiplier, and thus the higher
175 * the barrier to go to an expensive C state.
176 */
372ba8cb 177static inline int performance_multiplier(unsigned long nr_iowaiters, unsigned long load)
69d25870
AV
178{
179 int mult = 1;
180
181 /* for higher loadavg, we are more reluctant */
182
372ba8cb 183 mult += 2 * get_loadavg(load);
69d25870
AV
184
185 /* for IO wait tasks (per cpu!) we add 5x each */
64b4ca5c 186 mult += 10 * nr_iowaiters;
69d25870
AV
187
188 return mult;
189}
190
4f86d3a8
LB
191static DEFINE_PER_CPU(struct menu_device, menu_devices);
192
46bcfad7 193static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev);
672917dc 194
1f85f87d
AV
195/*
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 */
e132b9b3 201static unsigned int get_typical_interval(struct menu_device *data)
1f85f87d 202{
4cd46bca 203 int i, divisor;
3b99669b
RV
204 unsigned int max, thresh, avg;
205 uint64_t sum, variance;
0e96d5ad
TT
206
207 thresh = UINT_MAX; /* Discard outliers above this value */
1f85f87d 208
c96ca4fb 209again:
1f85f87d 210
0e96d5ad 211 /* First calculate the average of past intervals */
4cd46bca 212 max = 0;
3b99669b 213 sum = 0;
4cd46bca 214 divisor = 0;
c96ca4fb 215 for (i = 0; i < INTERVALS; i++) {
0e96d5ad 216 unsigned int value = data->intervals[i];
c96ca4fb 217 if (value <= thresh) {
3b99669b 218 sum += value;
c96ca4fb
YS
219 divisor++;
220 if (value > max)
221 max = value;
222 }
223 }
ae779300 224 if (divisor == INTERVALS)
3b99669b 225 avg = sum >> INTERVAL_SHIFT;
ae779300 226 else
3b99669b 227 avg = div_u64(sum, divisor);
c96ca4fb 228
7024b18c
RV
229 /* Then try to determine variance */
230 variance = 0;
c96ca4fb 231 for (i = 0; i < INTERVALS; i++) {
0e96d5ad 232 unsigned int value = data->intervals[i];
c96ca4fb 233 if (value <= thresh) {
3b99669b 234 int64_t diff = (int64_t)value - avg;
7024b18c 235 variance += diff * diff;
c96ca4fb
YS
236 }
237 }
ae779300 238 if (divisor == INTERVALS)
7024b18c 239 variance >>= INTERVAL_SHIFT;
ae779300 240 else
7024b18c 241 do_div(variance, divisor);
ae779300 242
1f85f87d 243 /*
7024b18c
RV
244 * 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).
0d6a7ffa 252 *
330647a9 253 * Use this result only if there is no timer to wake us up sooner.
1f85f87d 254 */
7024b18c 255 if (likely(variance <= U64_MAX/36)) {
3b99669b 256 if ((((u64)avg*avg > variance*36) && (divisor * 4 >= INTERVALS * 3))
7024b18c 257 || variance <= 400) {
e132b9b3 258 return avg;
0d6a7ffa 259 }
69a37bea 260 }
017099e2
TT
261
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)
e132b9b3 272 return UINT_MAX;
017099e2
TT
273
274 thresh = max - 1;
275 goto again;
1f85f87d
AV
276}
277
4f86d3a8
LB
278/**
279 * menu_select - selects the next idle state to enter
46bcfad7 280 * @drv: cpuidle driver containing state data
4f86d3a8
LB
281 * @dev: the CPU
282 */
46bcfad7 283static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev)
4f86d3a8 284{
229b6863 285 struct menu_device *data = this_cpu_ptr(&menu_devices);
9908859a 286 struct device *device = get_cpu_device(dev->cpu);
ed77134b 287 int latency_req = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
4f86d3a8 288 int i;
3ed09c94
NP
289 int first_idx;
290 int idx;
96e95182 291 unsigned int interactivity_req;
e132b9b3 292 unsigned int expected_interval;
372ba8cb 293 unsigned long nr_iowaiters, cpu_load;
6dbf5cea 294 int resume_latency = dev_pm_qos_raw_read_value(device);
69d25870 295
672917dc 296 if (data->needs_update) {
46bcfad7 297 menu_update(drv, dev);
672917dc
CZ
298 data->needs_update = 0;
299 }
300
0759e80b
RW
301 if (resume_latency < latency_req &&
302 resume_latency != PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
9908859a
AS
303 latency_req = resume_latency;
304
a2bd9202 305 /* Special case when user has set very strict latency requirement */
69d25870 306 if (unlikely(latency_req == 0))
a2bd9202 307 return 0;
a2bd9202 308
69d25870 309 /* determine the expected residency time, round up */
107d4f46 310 data->next_timer_us = ktime_to_us(tick_nohz_get_sleep_length());
69d25870 311
372ba8cb 312 get_iowait_load(&nr_iowaiters, &cpu_load);
64b4ca5c 313 data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
69d25870 314
51f245b8
TT
315 /*
316 * Force the result of multiplication to be 64 bits even if both
317 * operands are 32 bits.
318 * Make sure to round up for half microseconds.
319 */
ee3c86f3 320 data->predicted_us = DIV_ROUND_CLOSEST_ULL((uint64_t)data->next_timer_us *
51f245b8 321 data->correction_factor[data->bucket],
5787536e 322 RESOLUTION * DECAY);
69d25870 323
e132b9b3
RR
324 expected_interval = get_typical_interval(data);
325 expected_interval = min(expected_interval, data->next_timer_us);
96e95182 326
dc2251bf
RW
327 first_idx = 0;
328 if (drv->states[0].flags & CPUIDLE_FLAG_POLLING) {
329 struct cpuidle_state *s = &drv->states[1];
0c313cb2
RW
330 unsigned int polling_threshold;
331
9c4b2867
RW
332 /*
333 * We want to default to C1 (hlt), not to busy polling
e132b9b3
RR
334 * unless the timer is happening really really soon, or
335 * C1's exit latency exceeds the user configured limit.
9c4b2867 336 */
0c313cb2
RW
337 polling_threshold = max_t(unsigned int, 20, s->target_residency);
338 if (data->next_timer_us > polling_threshold &&
339 latency_req > s->exit_latency && !s->disabled &&
dc2251bf
RW
340 !dev->states_usage[1].disable)
341 first_idx = 1;
9c4b2867 342 }
4f86d3a8 343
e132b9b3
RR
344 /*
345 * Use the lowest expected idle interval to pick the idle state.
346 */
347 data->predicted_us = min(data->predicted_us, expected_interval);
348
349 /*
350 * Use the performance multiplier and the user-configurable
351 * latency_req to determine the maximum exit latency.
352 */
353 interactivity_req = data->predicted_us / performance_multiplier(nr_iowaiters, cpu_load);
354 if (latency_req > interactivity_req)
355 latency_req = interactivity_req;
356
71abbbf8
AL
357 /*
358 * Find the idle state with the lowest power while satisfying
359 * our constraints.
360 */
3ed09c94
NP
361 idx = -1;
362 for (i = first_idx; i < drv->state_count; i++) {
46bcfad7 363 struct cpuidle_state *s = &drv->states[i];
dc7fd275 364 struct cpuidle_state_usage *su = &dev->states_usage[i];
4f86d3a8 365
cbc9ef02 366 if (s->disabled || su->disable)
3a53396b 367 continue;
3ed09c94
NP
368 if (idx == -1)
369 idx = i; /* first enabled state */
14851912 370 if (s->target_residency > data->predicted_us)
8e37e1a2 371 break;
a2bd9202 372 if (s->exit_latency > latency_req)
8e37e1a2 373 break;
71abbbf8 374
3ed09c94 375 idx = i;
4f86d3a8
LB
376 }
377
3ed09c94
NP
378 if (idx == -1)
379 idx = 0; /* No states enabled. Must use 0. */
380
381 data->last_state_idx = idx;
382
69d25870 383 return data->last_state_idx;
4f86d3a8
LB
384}
385
386/**
672917dc 387 * menu_reflect - records that data structures need update
4f86d3a8 388 * @dev: the CPU
e978aa7d 389 * @index: the index of actual entered state
4f86d3a8
LB
390 *
391 * NOTE: it's important to be fast here because this operation will add to
392 * the overall exit latency.
393 */
e978aa7d 394static void menu_reflect(struct cpuidle_device *dev, int index)
672917dc 395{
229b6863 396 struct menu_device *data = this_cpu_ptr(&menu_devices);
a802ea96 397
e978aa7d 398 data->last_state_idx = index;
a802ea96 399 data->needs_update = 1;
672917dc
CZ
400}
401
402/**
403 * menu_update - attempts to guess what happened after entry
46bcfad7 404 * @drv: cpuidle driver containing state data
672917dc
CZ
405 * @dev: the CPU
406 */
46bcfad7 407static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
4f86d3a8 408{
229b6863 409 struct menu_device *data = this_cpu_ptr(&menu_devices);
4f86d3a8 410 int last_idx = data->last_state_idx;
46bcfad7 411 struct cpuidle_state *target = &drv->states[last_idx];
320eee77 412 unsigned int measured_us;
51f245b8 413 unsigned int new_factor;
4f86d3a8
LB
414
415 /*
61c66d6e 416 * Try to figure out how much time passed between entry to low
417 * power state and occurrence of the wakeup event.
418 *
419 * If the entered idle state didn't support residency measurements,
4108b3d9
LB
420 * we use them anyway if they are short, and if long,
421 * truncate to the whole expected time.
61c66d6e 422 *
423 * Any measured amount of time will include the exit latency.
424 * Since we are interested in when the wakeup begun, not when it
2fba5376 425 * was completed, we must subtract the exit latency. However, if
61c66d6e 426 * the measured amount of time is less than the exit latency,
427 * assume the state was never reached and the exit latency is 0.
4f86d3a8 428 */
69d25870 429
4108b3d9
LB
430 /* measured value */
431 measured_us = cpuidle_get_last_residency(dev);
4f86d3a8 432
4108b3d9 433 /* Deduct exit latency */
efddfd90 434 if (measured_us > 2 * target->exit_latency)
4108b3d9 435 measured_us -= target->exit_latency;
efddfd90
RR
436 else
437 measured_us /= 2;
69d25870 438
4108b3d9
LB
439 /* Make sure our coefficients do not exceed unity */
440 if (measured_us > data->next_timer_us)
441 measured_us = data->next_timer_us;
69d25870 442
51f245b8
TT
443 /* Update our correction ratio */
444 new_factor = data->correction_factor[data->bucket];
445 new_factor -= new_factor / DECAY;
69d25870 446
5dc2f5a3 447 if (data->next_timer_us > 0 && measured_us < MAX_INTERESTING)
448 new_factor += RESOLUTION * measured_us / data->next_timer_us;
320eee77 449 else
69d25870
AV
450 /*
451 * we were idle so long that we count it as a perfect
452 * prediction
453 */
454 new_factor += RESOLUTION;
320eee77 455
69d25870
AV
456 /*
457 * We don't want 0 as factor; we always want at least
51f245b8
TT
458 * a tiny bit of estimated time. Fortunately, due to rounding,
459 * new_factor will stay nonzero regardless of measured_us values
460 * and the compiler can eliminate this test as long as DECAY > 1.
69d25870 461 */
51f245b8 462 if (DECAY == 1 && unlikely(new_factor == 0))
69d25870 463 new_factor = 1;
320eee77 464
69d25870 465 data->correction_factor[data->bucket] = new_factor;
1f85f87d
AV
466
467 /* update the repeating-pattern data */
61c66d6e 468 data->intervals[data->interval_ptr++] = measured_us;
1f85f87d
AV
469 if (data->interval_ptr >= INTERVALS)
470 data->interval_ptr = 0;
4f86d3a8
LB
471}
472
473/**
474 * menu_enable_device - scans a CPU's states and does setup
46bcfad7 475 * @drv: cpuidle driver
4f86d3a8
LB
476 * @dev: the CPU
477 */
46bcfad7
DD
478static int menu_enable_device(struct cpuidle_driver *drv,
479 struct cpuidle_device *dev)
4f86d3a8
LB
480{
481 struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
bed4d597 482 int i;
4f86d3a8
LB
483
484 memset(data, 0, sizeof(struct menu_device));
485
bed4d597
CK
486 /*
487 * if the correction factor is 0 (eg first time init or cpu hotplug
488 * etc), we actually want to start out with a unity factor.
489 */
490 for(i = 0; i < BUCKETS; i++)
491 data->correction_factor[i] = RESOLUTION * DECAY;
492
4f86d3a8
LB
493 return 0;
494}
495
496static struct cpuidle_governor menu_governor = {
497 .name = "menu",
498 .rating = 20,
499 .enable = menu_enable_device,
500 .select = menu_select,
501 .reflect = menu_reflect,
4f86d3a8
LB
502};
503
504/**
505 * init_menu - initializes the governor
506 */
507static int __init init_menu(void)
508{
509 return cpuidle_register_governor(&menu_governor);
510}
511
137b944e 512postcore_initcall(init_menu);