xref: /linux/drivers/cpuidle/governors/menu.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * menu.c - the menu idle governor
4  *
5  * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
6  * Copyright (C) 2009 Intel Corporation
7  * Author:
8  *        Arjan van de Ven <arjan@linux.intel.com>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/cpuidle.h>
13 #include <linux/time.h>
14 #include <linux/ktime.h>
15 #include <linux/hrtimer.h>
16 #include <linux/tick.h>
17 #include <linux/sched/stat.h>
18 #include <linux/math64.h>
19 
20 #include "gov.h"
21 
22 #define BUCKETS 6
23 #define INTERVAL_SHIFT 3
24 #define INTERVALS (1UL << INTERVAL_SHIFT)
25 #define RESOLUTION 1024
26 #define DECAY 8
27 #define MAX_INTERESTING (50000 * NSEC_PER_USEC)
28 
29 /*
30  * Concepts and ideas behind the menu governor
31  *
32  * For the menu governor, there are 2 decision factors for picking a C
33  * state:
34  * 1) Energy break even point
35  * 2) Latency tolerance (from pmqos infrastructure)
36  * These two factors are treated independently.
37  *
38  * Energy break even point
39  * -----------------------
40  * C state entry and exit have an energy cost, and a certain amount of time in
41  * the  C state is required to actually break even on this cost. CPUIDLE
42  * provides us this duration in the "target_residency" field. So all that we
43  * need is a good prediction of how long we'll be idle. Like the traditional
44  * menu governor, we take the actual known "next timer event" time.
45  *
46  * Since there are other source of wakeups (interrupts for example) than
47  * the next timer event, this estimation is rather optimistic. To get a
48  * more realistic estimate, a correction factor is applied to the estimate,
49  * that is based on historic behavior. For example, if in the past the actual
50  * duration always was 50% of the next timer tick, the correction factor will
51  * be 0.5.
52  *
53  * menu uses a running average for this correction factor, but it uses a set of
54  * factors, not just a single factor. This stems from the realization that the
55  * ratio is dependent on the order of magnitude of the expected duration; if we
56  * expect 500 milliseconds of idle time the likelihood of getting an interrupt
57  * very early is much higher than if we expect 50 micro seconds of idle time.
58  * For this reason, menu keeps an array of 6 independent factors, that gets
59  * indexed based on the magnitude of the expected duration.
60  *
61  * Repeatable-interval-detector
62  * ----------------------------
63  * There are some cases where "next timer" is a completely unusable predictor:
64  * Those cases where the interval is fixed, for example due to hardware
65  * interrupt mitigation, but also due to fixed transfer rate devices like mice.
66  * For this, we use a different predictor: We track the duration of the last 8
67  * intervals and use them to estimate the duration of the next one.
68  */
69 
70 struct menu_device {
71 	int             needs_update;
72 	int             tick_wakeup;
73 
74 	u64		next_timer_ns;
75 	unsigned int	bucket;
76 	unsigned int	correction_factor[BUCKETS];
77 	unsigned int	intervals[INTERVALS];
78 	int		interval_ptr;
79 };
80 
81 static inline int which_bucket(u64 duration_ns)
82 {
83 	int bucket = 0;
84 
85 	if (duration_ns < 10ULL * NSEC_PER_USEC)
86 		return bucket;
87 	if (duration_ns < 100ULL * NSEC_PER_USEC)
88 		return bucket + 1;
89 	if (duration_ns < 1000ULL * NSEC_PER_USEC)
90 		return bucket + 2;
91 	if (duration_ns < 10000ULL * NSEC_PER_USEC)
92 		return bucket + 3;
93 	if (duration_ns < 100000ULL * NSEC_PER_USEC)
94 		return bucket + 4;
95 	return bucket + 5;
96 }
97 
98 static DEFINE_PER_CPU(struct menu_device, menu_devices);
99 
100 static void menu_update_intervals(struct menu_device *data, unsigned int interval_us)
101 {
102 	/* Update the repeating-pattern data. */
103 	data->intervals[data->interval_ptr++] = interval_us;
104 	if (data->interval_ptr >= INTERVALS)
105 		data->interval_ptr = 0;
106 }
107 
108 static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev);
109 
110 /*
111  * Try detecting repeating patterns by keeping track of the last 8
112  * intervals, and checking if the standard deviation of that set
113  * of points is below a threshold. If it is... then use the
114  * average of these 8 points as the estimated value.
115  */
116 static unsigned int get_typical_interval(struct menu_device *data)
117 {
118 	s64 value, min_thresh = -1, max_thresh = UINT_MAX;
119 	unsigned int max, min, divisor;
120 	u64 avg, variance, avg_sq;
121 	int i;
122 
123 again:
124 	/* Compute the average and variance of past intervals. */
125 	max = 0;
126 	min = UINT_MAX;
127 	avg = 0;
128 	variance = 0;
129 	divisor = 0;
130 	for (i = 0; i < INTERVALS; i++) {
131 		value = data->intervals[i];
132 		/*
133 		 * Discard the samples outside the interval between the min and
134 		 * max thresholds.
135 		 */
136 		if (value <= min_thresh || value >= max_thresh)
137 			continue;
138 
139 		divisor++;
140 
141 		avg += value;
142 		variance += value * value;
143 
144 		if (value > max)
145 			max = value;
146 
147 		if (value < min)
148 			min = value;
149 	}
150 
151 	if (!max)
152 		return UINT_MAX;
153 
154 	if (divisor == INTERVALS) {
155 		avg >>= INTERVAL_SHIFT;
156 		variance >>= INTERVAL_SHIFT;
157 	} else {
158 		do_div(avg, divisor);
159 		do_div(variance, divisor);
160 	}
161 
162 	avg_sq = avg * avg;
163 	variance -= avg_sq;
164 
165 	/*
166 	 * The typical interval is obtained when standard deviation is
167 	 * small (stddev <= 20 us, variance <= 400 us^2) or standard
168 	 * deviation is small compared to the average interval (avg >
169 	 * 6*stddev, avg^2 > 36*variance). The average is smaller than
170 	 * UINT_MAX aka U32_MAX, so computing its square does not
171 	 * overflow a u64. We simply reject this candidate average if
172 	 * the standard deviation is greater than 715 s (which is
173 	 * rather unlikely).
174 	 *
175 	 * Use this result only if there is no timer to wake us up sooner.
176 	 */
177 	if (likely(variance <= U64_MAX/36)) {
178 		if ((avg_sq > variance * 36 && divisor * 4 >= INTERVALS * 3) ||
179 		    variance <= 400)
180 			return avg;
181 	}
182 
183 	/*
184 	 * If there are outliers, discard them by setting thresholds to exclude
185 	 * data points at a large enough distance from the average, then
186 	 * calculate the average and standard deviation again. Once we get
187 	 * down to the last 3/4 of our samples, stop excluding samples.
188 	 *
189 	 * This can deal with workloads that have long pauses interspersed
190 	 * with sporadic activity with a bunch of short pauses.
191 	 *
192 	 * However, if the number of remaining samples is too small to exclude
193 	 * any more outliers, allow the deepest available idle state to be
194 	 * selected because there are systems where the time spent by CPUs in
195 	 * deep idle states is correlated to the maximum frequency the CPUs
196 	 * can get to.  On those systems, shallow idle states should be avoided
197 	 * unless there is a clear indication that the given CPU is most likley
198 	 * going to be woken up shortly.
199 	 */
200 	if (divisor * 4 <= INTERVALS * 3)
201 		return UINT_MAX;
202 
203 	/* Update the thresholds for the next round. */
204 	if (avg - min > max - avg)
205 		min_thresh = min;
206 	else
207 		max_thresh = max;
208 
209 	goto again;
210 }
211 
212 /**
213  * menu_select - selects the next idle state to enter
214  * @drv: cpuidle driver containing state data
215  * @dev: the CPU
216  * @stop_tick: indication on whether or not to stop the tick
217  */
218 static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
219 		       bool *stop_tick)
220 {
221 	struct menu_device *data = this_cpu_ptr(&menu_devices);
222 	s64 latency_req = cpuidle_governor_latency_req(dev->cpu);
223 	u64 predicted_ns;
224 	ktime_t delta, delta_tick;
225 	int i, idx;
226 
227 	if (data->needs_update) {
228 		menu_update(drv, dev);
229 		data->needs_update = 0;
230 	} else if (!dev->last_residency_ns) {
231 		/*
232 		 * This happens when the driver rejects the previously selected
233 		 * idle state and returns an error, so update the recent
234 		 * intervals table to prevent invalid information from being
235 		 * used going forward.
236 		 */
237 		menu_update_intervals(data, UINT_MAX);
238 	}
239 
240 	/* Find the shortest expected idle interval. */
241 	predicted_ns = get_typical_interval(data) * NSEC_PER_USEC;
242 	if (predicted_ns > RESIDENCY_THRESHOLD_NS) {
243 		unsigned int timer_us;
244 
245 		/* Determine the time till the closest timer. */
246 		delta = tick_nohz_get_sleep_length(&delta_tick);
247 		if (unlikely(delta < 0)) {
248 			delta = 0;
249 			delta_tick = 0;
250 		}
251 
252 		data->next_timer_ns = delta;
253 		data->bucket = which_bucket(data->next_timer_ns);
254 
255 		/* Round up the result for half microseconds. */
256 		timer_us = div_u64((RESOLUTION * DECAY * NSEC_PER_USEC) / 2 +
257 					data->next_timer_ns *
258 						data->correction_factor[data->bucket],
259 				   RESOLUTION * DECAY * NSEC_PER_USEC);
260 		/* Use the lowest expected idle interval to pick the idle state. */
261 		predicted_ns = min((u64)timer_us * NSEC_PER_USEC, predicted_ns);
262 	} else {
263 		/*
264 		 * Because the next timer event is not going to be determined
265 		 * in this case, assume that without the tick the closest timer
266 		 * will be in distant future and that the closest tick will occur
267 		 * after 1/2 of the tick period.
268 		 */
269 		data->next_timer_ns = KTIME_MAX;
270 		delta_tick = TICK_NSEC / 2;
271 		data->bucket = BUCKETS - 1;
272 	}
273 
274 	if (unlikely(drv->state_count <= 1 || latency_req == 0) ||
275 	    ((data->next_timer_ns < drv->states[1].target_residency_ns ||
276 	      latency_req < drv->states[1].exit_latency_ns) &&
277 	     !dev->states_usage[0].disable)) {
278 		/*
279 		 * In this case state[0] will be used no matter what, so return
280 		 * it right away and keep the tick running if state[0] is a
281 		 * polling one.
282 		 */
283 		*stop_tick = !(drv->states[0].flags & CPUIDLE_FLAG_POLLING);
284 		return 0;
285 	}
286 
287 	/*
288 	 * If the tick is already stopped, the cost of possible short idle
289 	 * duration misprediction is much higher, because the CPU may be stuck
290 	 * in a shallow idle state for a long time as a result of it.  In that
291 	 * case, say we might mispredict and use the known time till the closest
292 	 * timer event for the idle state selection.
293 	 */
294 	if (tick_nohz_tick_stopped() && predicted_ns < TICK_NSEC)
295 		predicted_ns = data->next_timer_ns;
296 
297 	/*
298 	 * Find the idle state with the lowest power while satisfying
299 	 * our constraints.
300 	 */
301 	idx = -1;
302 	for (i = 0; i < drv->state_count; i++) {
303 		struct cpuidle_state *s = &drv->states[i];
304 
305 		if (dev->states_usage[i].disable)
306 			continue;
307 
308 		if (idx == -1)
309 			idx = i; /* first enabled state */
310 
311 		if (s->exit_latency_ns > latency_req)
312 			break;
313 
314 		if (s->target_residency_ns <= predicted_ns) {
315 			idx = i;
316 			continue;
317 		}
318 
319 		/*
320 		 * Use a physical idle state instead of busy polling so long as
321 		 * its target residency is below the residency threshold, its
322 		 * exit latency is not greater than the predicted idle duration,
323 		 * and the next timer doesn't expire soon.
324 		 */
325 		if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
326 		    s->target_residency_ns < RESIDENCY_THRESHOLD_NS &&
327 		    s->target_residency_ns <= data->next_timer_ns &&
328 		    s->exit_latency_ns <= predicted_ns) {
329 			predicted_ns = s->target_residency_ns;
330 			idx = i;
331 			break;
332 		}
333 
334 		if (predicted_ns < TICK_NSEC)
335 			break;
336 
337 		if (!tick_nohz_tick_stopped()) {
338 			/*
339 			 * If the state selected so far is shallow, waking up
340 			 * early won't hurt, so retain the tick in that case and
341 			 * let the governor run again in the next iteration of
342 			 * the idle loop.
343 			 */
344 			predicted_ns = drv->states[idx].target_residency_ns;
345 			break;
346 		}
347 
348 		/*
349 		 * If the state selected so far is shallow and this state's
350 		 * target residency matches the time till the closest timer
351 		 * event, select this one to avoid getting stuck in the shallow
352 		 * one for too long.
353 		 */
354 		if (drv->states[idx].target_residency_ns < TICK_NSEC &&
355 		    s->target_residency_ns <= delta_tick)
356 			idx = i;
357 
358 		return idx;
359 	}
360 
361 	if (idx == -1)
362 		idx = 0; /* No states enabled. Must use 0. */
363 
364 	/*
365 	 * Don't stop the tick if the selected state is a polling one or if the
366 	 * expected idle duration is shorter than the tick period length.
367 	 */
368 	if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) ||
369 	     predicted_ns < TICK_NSEC) && !tick_nohz_tick_stopped()) {
370 		*stop_tick = false;
371 
372 		if (idx > 0 && drv->states[idx].target_residency_ns > delta_tick) {
373 			/*
374 			 * The tick is not going to be stopped and the target
375 			 * residency of the state to be returned is not within
376 			 * the time until the next timer event including the
377 			 * tick, so try to correct that.
378 			 */
379 			for (i = idx - 1; i >= 0; i--) {
380 				if (dev->states_usage[i].disable)
381 					continue;
382 
383 				idx = i;
384 				if (drv->states[i].target_residency_ns <= delta_tick)
385 					break;
386 			}
387 		}
388 	}
389 
390 	return idx;
391 }
392 
393 /**
394  * menu_reflect - records that data structures need update
395  * @dev: the CPU
396  * @index: the index of actual entered state
397  *
398  * NOTE: it's important to be fast here because this operation will add to
399  *       the overall exit latency.
400  */
401 static void menu_reflect(struct cpuidle_device *dev, int index)
402 {
403 	struct menu_device *data = this_cpu_ptr(&menu_devices);
404 
405 	dev->last_state_idx = index;
406 	data->needs_update = 1;
407 	data->tick_wakeup = tick_nohz_idle_got_tick();
408 }
409 
410 /**
411  * menu_update - attempts to guess what happened after entry
412  * @drv: cpuidle driver containing state data
413  * @dev: the CPU
414  */
415 static void menu_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
416 {
417 	struct menu_device *data = this_cpu_ptr(&menu_devices);
418 	int last_idx = dev->last_state_idx;
419 	struct cpuidle_state *target = &drv->states[last_idx];
420 	u64 measured_ns;
421 	unsigned int new_factor;
422 
423 	/*
424 	 * Try to figure out how much time passed between entry to low
425 	 * power state and occurrence of the wakeup event.
426 	 *
427 	 * If the entered idle state didn't support residency measurements,
428 	 * we use them anyway if they are short, and if long,
429 	 * truncate to the whole expected time.
430 	 *
431 	 * Any measured amount of time will include the exit latency.
432 	 * Since we are interested in when the wakeup begun, not when it
433 	 * was completed, we must subtract the exit latency. However, if
434 	 * the measured amount of time is less than the exit latency,
435 	 * assume the state was never reached and the exit latency is 0.
436 	 */
437 
438 	if (data->tick_wakeup && data->next_timer_ns > TICK_NSEC) {
439 		/*
440 		 * The nohz code said that there wouldn't be any events within
441 		 * the tick boundary (if the tick was stopped), but the idle
442 		 * duration predictor had a differing opinion.  Since the CPU
443 		 * was woken up by a tick (that wasn't stopped after all), the
444 		 * predictor was not quite right, so assume that the CPU could
445 		 * have been idle long (but not forever) to help the idle
446 		 * duration predictor do a better job next time.
447 		 */
448 		measured_ns = 9 * MAX_INTERESTING / 10;
449 	} else if ((drv->states[last_idx].flags & CPUIDLE_FLAG_POLLING) &&
450 		   dev->poll_time_limit) {
451 		/*
452 		 * The CPU exited the "polling" state due to a time limit, so
453 		 * the idle duration prediction leading to the selection of that
454 		 * state was inaccurate.  If a better prediction had been made,
455 		 * the CPU might have been woken up from idle by the next timer.
456 		 * Assume that to be the case.
457 		 */
458 		measured_ns = data->next_timer_ns;
459 	} else {
460 		/* measured value */
461 		measured_ns = dev->last_residency_ns;
462 
463 		/* Deduct exit latency */
464 		if (measured_ns > 2 * target->exit_latency_ns)
465 			measured_ns -= target->exit_latency_ns;
466 		else
467 			measured_ns /= 2;
468 	}
469 
470 	/* Make sure our coefficients do not exceed unity */
471 	if (measured_ns > data->next_timer_ns)
472 		measured_ns = data->next_timer_ns;
473 
474 	/* Update our correction ratio */
475 	new_factor = data->correction_factor[data->bucket];
476 	new_factor -= new_factor / DECAY;
477 
478 	if (data->next_timer_ns > 0 && measured_ns < MAX_INTERESTING)
479 		new_factor += div64_u64(RESOLUTION * measured_ns,
480 					data->next_timer_ns);
481 	else
482 		/*
483 		 * we were idle so long that we count it as a perfect
484 		 * prediction
485 		 */
486 		new_factor += RESOLUTION;
487 
488 	/*
489 	 * We don't want 0 as factor; we always want at least
490 	 * a tiny bit of estimated time. Fortunately, due to rounding,
491 	 * new_factor will stay nonzero regardless of measured_us values
492 	 * and the compiler can eliminate this test as long as DECAY > 1.
493 	 */
494 	if (DECAY == 1 && unlikely(new_factor == 0))
495 		new_factor = 1;
496 
497 	data->correction_factor[data->bucket] = new_factor;
498 
499 	menu_update_intervals(data, ktime_to_us(measured_ns));
500 }
501 
502 /**
503  * menu_enable_device - scans a CPU's states and does setup
504  * @drv: cpuidle driver
505  * @dev: the CPU
506  */
507 static int menu_enable_device(struct cpuidle_driver *drv,
508 				struct cpuidle_device *dev)
509 {
510 	struct menu_device *data = &per_cpu(menu_devices, dev->cpu);
511 	int i;
512 
513 	memset(data, 0, sizeof(struct menu_device));
514 
515 	/*
516 	 * if the correction factor is 0 (eg first time init or cpu hotplug
517 	 * etc), we actually want to start out with a unity factor.
518 	 */
519 	for(i = 0; i < BUCKETS; i++)
520 		data->correction_factor[i] = RESOLUTION * DECAY;
521 
522 	return 0;
523 }
524 
525 static struct cpuidle_governor menu_governor = {
526 	.name =		"menu",
527 	.rating =	20,
528 	.enable =	menu_enable_device,
529 	.select =	menu_select,
530 	.reflect =	menu_reflect,
531 };
532 
533 /**
534  * init_menu - initializes the governor
535  */
536 static int __init init_menu(void)
537 {
538 	return cpuidle_register_governor(&menu_governor);
539 }
540 
541 postcore_initcall(init_menu);
542