Lines Matching +full:idle +full:- +full:state

1 // SPDX-License-Identifier: GPL-2.0-only
3 * menu.c - the menu idle governor
5 * Copyright (C) 2006-2007 Adam Belay <abelay@novell.com>
33 * state:
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
43 * need is a good prediction of how long we'll be idle. Like the traditional
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.
61 * Repeatable-interval-detector
62 * ----------------------------
110 s64 value, min_thresh = -1, max_thresh = UINT_MAX; in get_typical_interval()
123 value = data->intervals[i]; in get_typical_interval()
155 variance -= avg_sq; in get_typical_interval()
199 if (avg - min > max - avg) in get_typical_interval()
208 * menu_select - selects the next idle state to enter
209 * @drv: cpuidle driver containing state data
217 s64 latency_req = cpuidle_governor_latency_req(dev->cpu); in menu_select()
222 if (data->needs_update) { in menu_select()
224 data->needs_update = 0; in menu_select()
227 /* Find the shortest expected idle interval. */ in menu_select()
239 data->next_timer_ns = delta; in menu_select()
240 data->bucket = which_bucket(data->next_timer_ns); in menu_select()
244 data->next_timer_ns * in menu_select()
245 data->correction_factor[data->bucket], in menu_select()
247 /* Use the lowest expected idle interval to pick the idle state. */ in menu_select()
256 data->next_timer_ns = KTIME_MAX; in menu_select()
258 data->bucket = BUCKETS - 1; in menu_select()
261 if (unlikely(drv->state_count <= 1 || latency_req == 0) || in menu_select()
262 ((data->next_timer_ns < drv->states[1].target_residency_ns || in menu_select()
263 latency_req < drv->states[1].exit_latency_ns) && in menu_select()
264 !dev->states_usage[0].disable)) { in menu_select()
266 * In this case state[0] will be used no matter what, so return in menu_select()
267 * it right away and keep the tick running if state[0] is a in menu_select()
270 *stop_tick = !(drv->states[0].flags & CPUIDLE_FLAG_POLLING); in menu_select()
277 * idle duration misprediction is much higher, because the CPU in menu_select()
278 * may be stuck in a shallow idle state for a long time as a in menu_select()
280 * the known time till the closest timer event for the idle in menu_select()
281 * state selection. in menu_select()
284 predicted_ns = data->next_timer_ns; in menu_select()
290 * Find the idle state with the lowest power while satisfying in menu_select()
293 idx = -1; in menu_select()
294 for (i = 0; i < drv->state_count; i++) { in menu_select()
295 struct cpuidle_state *s = &drv->states[i]; in menu_select()
297 if (dev->states_usage[i].disable) in menu_select()
300 if (idx == -1) in menu_select()
301 idx = i; /* first enabled state */ in menu_select()
303 if (s->target_residency_ns > predicted_ns) { in menu_select()
305 * Use a physical idle state, not busy polling, unless in menu_select()
308 if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) && in menu_select()
309 s->exit_latency_ns <= latency_req && in menu_select()
310 s->target_residency_ns <= data->next_timer_ns) { in menu_select()
311 predicted_ns = s->target_residency_ns; in menu_select()
320 * If the state selected so far is shallow, in menu_select()
325 predicted_ns = drv->states[idx].target_residency_ns; in menu_select()
330 * If the state selected so far is shallow and this in menu_select()
331 * state's target residency matches the time till the in menu_select()
335 if (drv->states[idx].target_residency_ns < TICK_NSEC && in menu_select()
336 s->target_residency_ns <= delta_tick) in menu_select()
341 if (s->exit_latency_ns > latency_req) in menu_select()
347 if (idx == -1) in menu_select()
351 * Don't stop the tick if the selected state is a polling one or if the in menu_select()
352 * expected idle duration is shorter than the tick period length. in menu_select()
354 if (((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) || in menu_select()
358 if (idx > 0 && drv->states[idx].target_residency_ns > delta_tick) { in menu_select()
361 * residency of the state to be returned is not within in menu_select()
365 for (i = idx - 1; i >= 0; i--) { in menu_select()
366 if (dev->states_usage[i].disable) in menu_select()
370 if (drv->states[i].target_residency_ns <= delta_tick) in menu_select()
380 * menu_reflect - records that data structures need update
382 * @index: the index of actual entered state
391 dev->last_state_idx = index; in menu_reflect()
392 data->needs_update = 1; in menu_reflect()
393 data->tick_wakeup = tick_nohz_idle_got_tick(); in menu_reflect()
397 * menu_update - attempts to guess what happened after entry
398 * @drv: cpuidle driver containing state data
404 int last_idx = dev->last_state_idx; in menu_update()
405 struct cpuidle_state *target = &drv->states[last_idx]; in menu_update()
411 * power state and occurrence of the wakeup event. in menu_update()
413 * If the entered idle state didn't support residency measurements, in menu_update()
421 * assume the state was never reached and the exit latency is 0. in menu_update()
424 if (data->tick_wakeup && data->next_timer_ns > TICK_NSEC) { in menu_update()
427 * the tick boundary (if the tick was stopped), but the idle in menu_update()
431 * have been idle long (but not forever) to help the idle in menu_update()
435 } else if ((drv->states[last_idx].flags & CPUIDLE_FLAG_POLLING) && in menu_update()
436 dev->poll_time_limit) { in menu_update()
438 * The CPU exited the "polling" state due to a time limit, so in menu_update()
439 * the idle duration prediction leading to the selection of that in menu_update()
440 * state was inaccurate. If a better prediction had been made, in menu_update()
441 * the CPU might have been woken up from idle by the next timer. in menu_update()
444 measured_ns = data->next_timer_ns; in menu_update()
447 measured_ns = dev->last_residency_ns; in menu_update()
450 if (measured_ns > 2 * target->exit_latency_ns) in menu_update()
451 measured_ns -= target->exit_latency_ns; in menu_update()
457 if (measured_ns > data->next_timer_ns) in menu_update()
458 measured_ns = data->next_timer_ns; in menu_update()
461 new_factor = data->correction_factor[data->bucket]; in menu_update()
462 new_factor -= new_factor / DECAY; in menu_update()
464 if (data->next_timer_ns > 0 && measured_ns < MAX_INTERESTING) in menu_update()
466 data->next_timer_ns); in menu_update()
469 * we were idle so long that we count it as a perfect in menu_update()
483 data->correction_factor[data->bucket] = new_factor; in menu_update()
485 /* update the repeating-pattern data */ in menu_update()
486 data->intervals[data->interval_ptr++] = ktime_to_us(measured_ns); in menu_update()
487 if (data->interval_ptr >= INTERVALS) in menu_update()
488 data->interval_ptr = 0; in menu_update()
492 * menu_enable_device - scans a CPU's states and does setup
499 struct menu_device *data = &per_cpu(menu_devices, dev->cpu); in menu_enable_device()
509 data->correction_factor[i] = RESOLUTION * DECAY; in menu_enable_device()
523 * init_menu - initializes the governor