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