* [PATCH v3 0/2] cpuidle: governors: teo: Wakeup events classification change and refinement
@ 2026-01-29 20:46 Rafael J. Wysocki
2026-01-29 20:49 ` [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events Rafael J. Wysocki
2026-01-29 20:51 ` [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup Rafael J. Wysocki
0 siblings, 2 replies; 6+ messages in thread
From: Rafael J. Wysocki @ 2026-01-29 20:46 UTC (permalink / raw)
To: Linux PM; +Cc: LKML, Christian Loehle, Doug Smythies
Hi All,
This is a new version of
https://lore.kernel.org/linux-pm/12831359.O9o76ZdvQC@rafael.j.wysocki/
updating the first patch. The second one is unchanged.
Patch [1/2] changes the criteria used for classifying wakeup events as hits
or intercepts to (hopefully) make the classification work better, especially
for large state bins.
Patch [2/2] refines the idle state lookup based on intercepts to first
consider the state with the maximum intercepts metric, so that state is
always taken into consideration.
Please see the individual patch changelogs for details.
Thanks!
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events
2026-01-29 20:46 [PATCH v3 0/2] cpuidle: governors: teo: Wakeup events classification change and refinement Rafael J. Wysocki
@ 2026-01-29 20:49 ` Rafael J. Wysocki
2026-01-30 10:34 ` Christian Loehle
2026-01-29 20:51 ` [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup Rafael J. Wysocki
1 sibling, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2026-01-29 20:49 UTC (permalink / raw)
To: Linux PM; +Cc: LKML, Christian Loehle, Doug Smythies
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
If differences between target residency values of adjacent idle states
of a given CPU are relatively large, the corresponding idle state bins
used by the teo governors are large either and the rule by which hits
are distinguished from intercepts is inaccurate.
Namely, by that rule, a wakeup event is classified as a hit if the
sleep length (the time till the closest timer other than the tick)
and the measured idle duration, adjusted for the entered idle state
exit latency, fall into the same idle state bin. However, if that bin
is large enough, the actual difference between the sleep length and
the measured idle duration may be significant. It may in fact be
significantly greater than the analogous difference for an event where
the sleep length and the measured idle duration fall into different
bins.
For this reason, amend the rule in question with a check that will only
allow a wakeup event to be counted as a hit if the sleep length is less
than the "raw" measured idle duration (which means that the wakeup
appears to have occurred after the anticipated timer event). Otherwise,
the event will be counted as an intercept.
Also update the documentation part explaining the difference between
"hits" and "intercepts" to take the above change into account.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
v2 -> v3:
* Use 1/2 of the entered state exit latency instead of a constant
margin in the inequality driving the classification (Christian).
* Update code comments and the changelog.
v1.1 -> v2: No changes
v1 -> v1.1
* Drop the change in teo_select() along with the corresponding
part of the changelog (after receiving testing feedback from
Christian)
---
drivers/cpuidle/governors/teo.c | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)
--- a/drivers/cpuidle/governors/teo.c
+++ b/drivers/cpuidle/governors/teo.c
@@ -48,12 +48,11 @@
* in accordance with what happened last time.
*
* The "hits" metric reflects the relative frequency of situations in which the
- * sleep length and the idle duration measured after CPU wakeup fall into the
- * same bin (that is, the CPU appears to wake up "on time" relative to the sleep
- * length). In turn, the "intercepts" metric reflects the relative frequency of
- * non-timer wakeup events for which the measured idle duration falls into a bin
- * that corresponds to an idle state shallower than the one whose bin is fallen
- * into by the sleep length (these events are also referred to as "intercepts"
+ * sleep length and the idle duration measured after CPU wakeup are close enough
+ * (that is, the CPU appears to wake up "on time" relative to the sleep length).
+ * In turn, the "intercepts" metric reflects the relative frequency of non-timer
+ * wakeup events for which the measured idle duration is significantly different
+ * from the sleep length (these events are also referred to as "intercepts"
* below).
*
* The governor also counts "intercepts" with the measured idle duration below
@@ -167,6 +166,7 @@ static void teo_decay(unsigned int *metr
*/
static void teo_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
{
+ s64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
struct teo_cpu *cpu_data = this_cpu_ptr(&teo_cpus);
int i, idx_timer = 0, idx_duration = 0;
s64 target_residency_ns, measured_ns;
@@ -182,8 +182,6 @@ static void teo_update(struct cpuidle_dr
*/
measured_ns = S64_MAX;
} else {
- s64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
-
measured_ns = dev->last_residency_ns;
/*
* The delay between the wakeup and the first instruction
@@ -253,12 +251,17 @@ static void teo_update(struct cpuidle_dr
}
/*
- * If the measured idle duration falls into the same bin as the sleep
- * length, this is a "hit", so update the "hits" metric for that bin.
+ * If the measured idle duration (adjusted for the entered state exit
+ * latency) falls into the same bin as the sleep length and the latter
+ * is less than the "raw" measured idle duration (so the wakeup appears
+ * to have occurred after the anticipated timer event), this is a "hit",
+ * so update the "hits" metric for that bin.
+ *
* Otherwise, update the "intercepts" metric for the bin fallen into by
* the measured idle duration.
*/
- if (idx_timer == idx_duration) {
+ if (idx_timer == idx_duration &&
+ cpu_data->sleep_length_ns - measured_ns < lat_ns / 2) {
cpu_data->state_bins[idx_timer].hits += PULSE;
} else {
cpu_data->state_bins[idx_duration].intercepts += PULSE;
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup
2026-01-29 20:46 [PATCH v3 0/2] cpuidle: governors: teo: Wakeup events classification change and refinement Rafael J. Wysocki
2026-01-29 20:49 ` [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events Rafael J. Wysocki
@ 2026-01-29 20:51 ` Rafael J. Wysocki
2026-01-30 11:04 ` Christian Loehle
1 sibling, 1 reply; 6+ messages in thread
From: Rafael J. Wysocki @ 2026-01-29 20:51 UTC (permalink / raw)
To: Linux PM; +Cc: LKML, Christian Loehle, Doug Smythies
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
There are cases in which decisions made by the teo governor are
arguably overly conservative.
For instance, suppose that there are 4 idle states and the values of
the intercepts metric for the first 3 of them are 400, 250, and 251,
respectively. If the total sum computed in teo_update() is 1000, the
governor will select idle state 1 (provided that all idle states are
enabled and the scheduler tick has not been stopped) although arguably
idle state 0 would be a better choice because the likelihood of getting
an idle duration below the target residency of idle state 1 is greater
than the likelihood of getting an idle duration between the target
residency of idle state 1 and the target residency of idle state 2.
To address this, refine the candidate idle state lookup based on
intercepts to start at the state with the maximum intercepts metric,
below the deepest enabled one, to avoid the cases in which the search
may stop before reaching that state.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
v2 -> v3: No changes
v1 -> v2:
* Multiple fixes related to the handling of cases in which some states
are disabled.
* Fixes in new comments (there was some confusion in those comments
regarding the direction of idle states table traversal).
* Fixed typos in new comments.
---
drivers/cpuidle/governors/teo.c | 50 ++++++++++++++++++++++++++++++++++------
1 file changed, 43 insertions(+), 7 deletions(-)
--- a/drivers/cpuidle/governors/teo.c
+++ b/drivers/cpuidle/governors/teo.c
@@ -74,12 +74,17 @@
* than the candidate one (it represents the cases in which the CPU was
* likely woken up by a non-timer wakeup source).
*
+ * Also find the idle state with the maximum intercepts metric (if there are
+ * multiple states with the maximum intercetps metric, choose the one with
+ * the highest index).
+ *
* 2. If the second sum computed in step 1 is greater than a half of the sum of
* both metrics for the candidate state bin and all subsequent bins (if any),
* a shallower idle state is likely to be more suitable, so look for it.
*
* - Traverse the enabled idle states shallower than the candidate one in the
- * descending order.
+ * descending order, starting at the state with the maximum intercepts
+ * metric found in step 1.
*
* - For each of them compute the sum of the "intercepts" metrics over all
* of the idle states between it and the candidate one (including the
@@ -308,8 +313,10 @@ static int teo_select(struct cpuidle_dri
ktime_t delta_tick = TICK_NSEC / 2;
unsigned int idx_intercept_sum = 0;
unsigned int intercept_sum = 0;
+ unsigned int intercept_max = 0;
unsigned int idx_hit_sum = 0;
unsigned int hit_sum = 0;
+ int intercept_max_idx = -1;
int constraint_idx = 0;
int idx0 = 0, idx = -1;
s64 duration_ns;
@@ -340,17 +347,32 @@ static int teo_select(struct cpuidle_dri
if (!dev->states_usage[0].disable)
idx = 0;
- /* Compute the sums of metrics for early wakeup pattern detection. */
+ /*
+ * Compute the sums of metrics for early wakeup pattern detection and
+ * look for the state bin with the maximum intercepts metric below the
+ * deepest enabled one (if there are multiple states with the maximum
+ * intercepts metric, choose the one with the highest index).
+ */
for (i = 1; i < drv->state_count; i++) {
struct teo_bin *prev_bin = &cpu_data->state_bins[i-1];
+ unsigned int prev_intercepts = prev_bin->intercepts;
struct cpuidle_state *s = &drv->states[i];
/*
* Update the sums of idle state metrics for all of the states
* shallower than the current one.
*/
- intercept_sum += prev_bin->intercepts;
hit_sum += prev_bin->hits;
+ intercept_sum += prev_intercepts;
+ /*
+ * Check if this is the bin with the maximum number of
+ * intercepts so far and in that case update the index of
+ * the state with the maximum intercetps metric.
+ */
+ if (prev_intercepts >= intercept_max) {
+ intercept_max = prev_intercepts;
+ intercept_max_idx = i - 1;
+ }
if (dev->states_usage[i].disable)
continue;
@@ -414,9 +436,22 @@ static int teo_select(struct cpuidle_dri
}
/*
- * Look for the deepest idle state whose target residency had
- * not exceeded the idle duration in over a half of the relevant
- * cases in the past.
+ * If the minimum state index is greater than or equal to the
+ * index of the state with the maximum intercepts metric and
+ * the corresponding state is enabled, there is no need to look
+ * at the deeper states.
+ */
+ if (min_idx >= intercept_max_idx &&
+ !dev->states_usage[min_idx].disable) {
+ idx = min_idx;
+ goto constraint;
+ }
+
+ /*
+ * Look for the deepest enabled idle state, at most as deep as
+ * the one with the maximum intercetps metric, whose target
+ * residency had not been greater than the idle duration in over
+ * a half of the relevant cases in the past.
*
* Take the possible duration limitation present if the tick
* has been stopped already into account.
@@ -428,7 +463,8 @@ static int teo_select(struct cpuidle_dri
continue;
idx = i;
- if (2 * intercept_sum > idx_intercept_sum)
+ if (2 * intercept_sum > idx_intercept_sum &&
+ i <= intercept_max_idx)
break;
}
}
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events
2026-01-29 20:49 ` [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events Rafael J. Wysocki
@ 2026-01-30 10:34 ` Christian Loehle
0 siblings, 0 replies; 6+ messages in thread
From: Christian Loehle @ 2026-01-30 10:34 UTC (permalink / raw)
To: Rafael J. Wysocki, Linux PM; +Cc: LKML, Doug Smythies
On 1/29/26 20:49, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>
> If differences between target residency values of adjacent idle states
> of a given CPU are relatively large, the corresponding idle state bins
> used by the teo governors are large either and the rule by which hits
> are distinguished from intercepts is inaccurate.
>
> Namely, by that rule, a wakeup event is classified as a hit if the
> sleep length (the time till the closest timer other than the tick)
> and the measured idle duration, adjusted for the entered idle state
> exit latency, fall into the same idle state bin. However, if that bin
> is large enough, the actual difference between the sleep length and
> the measured idle duration may be significant. It may in fact be
> significantly greater than the analogous difference for an event where
> the sleep length and the measured idle duration fall into different
> bins.
>
> For this reason, amend the rule in question with a check that will only
> allow a wakeup event to be counted as a hit if the sleep length is less
> than the "raw" measured idle duration (which means that the wakeup
> appears to have occurred after the anticipated timer event). Otherwise,
> the event will be counted as an intercept.
>
> Also update the documentation part explaining the difference between
> "hits" and "intercepts" to take the above change into account.
>
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Reviewed-by: Christian Loehle <christian.loehle@arm.com>
> ---
>
> v2 -> v3:
> * Use 1/2 of the entered state exit latency instead of a constant
> margin in the inequality driving the classification (Christian).
> * Update code comments and the changelog.
>
> v1.1 -> v2: No changes
>
> v1 -> v1.1
> * Drop the change in teo_select() along with the corresponding
> part of the changelog (after receiving testing feedback from
> Christian)
>
> ---
> drivers/cpuidle/governors/teo.c | 25 ++++++++++++++-----------
> 1 file changed, 14 insertions(+), 11 deletions(-)
>
> --- a/drivers/cpuidle/governors/teo.c
> +++ b/drivers/cpuidle/governors/teo.c
> @@ -48,12 +48,11 @@
> * in accordance with what happened last time.
> *
> * The "hits" metric reflects the relative frequency of situations in which the
> - * sleep length and the idle duration measured after CPU wakeup fall into the
> - * same bin (that is, the CPU appears to wake up "on time" relative to the sleep
> - * length). In turn, the "intercepts" metric reflects the relative frequency of
> - * non-timer wakeup events for which the measured idle duration falls into a bin
> - * that corresponds to an idle state shallower than the one whose bin is fallen
> - * into by the sleep length (these events are also referred to as "intercepts"
> + * sleep length and the idle duration measured after CPU wakeup are close enough
> + * (that is, the CPU appears to wake up "on time" relative to the sleep length).
> + * In turn, the "intercepts" metric reflects the relative frequency of non-timer
> + * wakeup events for which the measured idle duration is significantly different
> + * from the sleep length (these events are also referred to as "intercepts"
> * below).
> *
> * The governor also counts "intercepts" with the measured idle duration below
> @@ -167,6 +166,7 @@ static void teo_decay(unsigned int *metr
> */
> static void teo_update(struct cpuidle_driver *drv, struct cpuidle_device *dev)
> {
> + s64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
> struct teo_cpu *cpu_data = this_cpu_ptr(&teo_cpus);
> int i, idx_timer = 0, idx_duration = 0;
> s64 target_residency_ns, measured_ns;
> @@ -182,8 +182,6 @@ static void teo_update(struct cpuidle_dr
> */
> measured_ns = S64_MAX;
> } else {
> - s64 lat_ns = drv->states[dev->last_state_idx].exit_latency_ns;
> -
> measured_ns = dev->last_residency_ns;
> /*
> * The delay between the wakeup and the first instruction
> @@ -253,12 +251,17 @@ static void teo_update(struct cpuidle_dr
> }
>
> /*
> - * If the measured idle duration falls into the same bin as the sleep
> - * length, this is a "hit", so update the "hits" metric for that bin.
> + * If the measured idle duration (adjusted for the entered state exit
> + * latency) falls into the same bin as the sleep length and the latter
> + * is less than the "raw" measured idle duration (so the wakeup appears
> + * to have occurred after the anticipated timer event), this is a "hit",
> + * so update the "hits" metric for that bin.
> + *
> * Otherwise, update the "intercepts" metric for the bin fallen into by
> * the measured idle duration.
> */
> - if (idx_timer == idx_duration) {
> + if (idx_timer == idx_duration &&
> + cpu_data->sleep_length_ns - measured_ns < lat_ns / 2) {
> cpu_data->state_bins[idx_timer].hits += PULSE;
> } else {
> cpu_data->state_bins[idx_duration].intercepts += PULSE;
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup
2026-01-29 20:51 ` [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup Rafael J. Wysocki
@ 2026-01-30 11:04 ` Christian Loehle
2026-01-30 17:58 ` Rafael J. Wysocki
0 siblings, 1 reply; 6+ messages in thread
From: Christian Loehle @ 2026-01-30 11:04 UTC (permalink / raw)
To: Rafael J. Wysocki, Linux PM; +Cc: LKML, Doug Smythies
On 1/29/26 20:51, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
>
> There are cases in which decisions made by the teo governor are
> arguably overly conservative.
>
> For instance, suppose that there are 4 idle states and the values of
> the intercepts metric for the first 3 of them are 400, 250, and 251,
> respectively. If the total sum computed in teo_update() is 1000, the
> governor will select idle state 1 (provided that all idle states are
> enabled and the scheduler tick has not been stopped) although arguably
> idle state 0 would be a better choice because the likelihood of getting
> an idle duration below the target residency of idle state 1 is greater
> than the likelihood of getting an idle duration between the target
> residency of idle state 1 and the target residency of idle state 2.
FWIW the algorithm would be self-correcting long-term here because
selecting state1 would then no longer accumulate intercepts for >state1
and soon enough it will select state0.
Anyway I think this is an optimisation nonetheless.
>
> To address this, refine the candidate idle state lookup based on
> intercepts to start at the state with the maximum intercepts metric,
> below the deepest enabled one, to avoid the cases in which the search
> may stop before reaching that state.
>
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>
> v2 -> v3: No changes
>
> v1 -> v2:
> * Multiple fixes related to the handling of cases in which some states
> are disabled.
> * Fixes in new comments (there was some confusion in those comments
> regarding the direction of idle states table traversal).
> * Fixed typos in new comments.
>
> ---
> drivers/cpuidle/governors/teo.c | 50 ++++++++++++++++++++++++++++++++++------
> 1 file changed, 43 insertions(+), 7 deletions(-)
>
> --- a/drivers/cpuidle/governors/teo.c
> +++ b/drivers/cpuidle/governors/teo.c
> @@ -74,12 +74,17 @@
> * than the candidate one (it represents the cases in which the CPU was
> * likely woken up by a non-timer wakeup source).
> *
> + * Also find the idle state with the maximum intercepts metric (if there are
> + * multiple states with the maximum intercetps metric, choose the one with
s/intercetps/intercepts
> + * the highest index).
> + *
> * 2. If the second sum computed in step 1 is greater than a half of the sum of
> * both metrics for the candidate state bin and all subsequent bins (if any),
> * a shallower idle state is likely to be more suitable, so look for it.
> *
> * - Traverse the enabled idle states shallower than the candidate one in the
> - * descending order.
> + * descending order, starting at the state with the maximum intercepts
> + * metric found in step 1.
> *
> * - For each of them compute the sum of the "intercepts" metrics over all
> * of the idle states between it and the candidate one (including the
> @@ -308,8 +313,10 @@ static int teo_select(struct cpuidle_dri
> ktime_t delta_tick = TICK_NSEC / 2;
> unsigned int idx_intercept_sum = 0;
> unsigned int intercept_sum = 0;
> + unsigned int intercept_max = 0;
> unsigned int idx_hit_sum = 0;
> unsigned int hit_sum = 0;
> + int intercept_max_idx = -1;
> int constraint_idx = 0;
> int idx0 = 0, idx = -1;
> s64 duration_ns;
> @@ -340,17 +347,32 @@ static int teo_select(struct cpuidle_dri
> if (!dev->states_usage[0].disable)
> idx = 0;
>
> - /* Compute the sums of metrics for early wakeup pattern detection. */
> + /*
> + * Compute the sums of metrics for early wakeup pattern detection and
> + * look for the state bin with the maximum intercepts metric below the
> + * deepest enabled one (if there are multiple states with the maximum
> + * intercepts metric, choose the one with the highest index).
> + */
> for (i = 1; i < drv->state_count; i++) {
> struct teo_bin *prev_bin = &cpu_data->state_bins[i-1];
> + unsigned int prev_intercepts = prev_bin->intercepts;
> struct cpuidle_state *s = &drv->states[i];
>
> /*
> * Update the sums of idle state metrics for all of the states
> * shallower than the current one.
> */
> - intercept_sum += prev_bin->intercepts;
> hit_sum += prev_bin->hits;
> + intercept_sum += prev_intercepts;
> + /*
> + * Check if this is the bin with the maximum number of
> + * intercepts so far and in that case update the index of
> + * the state with the maximum intercetps metric.
s/intercetps/intercepts
> + */
> + if (prev_intercepts >= intercept_max) {
> + intercept_max = prev_intercepts;
> + intercept_max_idx = i - 1;
> + }
>
> if (dev->states_usage[i].disable)
> continue;
> @@ -414,9 +436,22 @@ static int teo_select(struct cpuidle_dri
> }
>
> /*
> - * Look for the deepest idle state whose target residency had
> - * not exceeded the idle duration in over a half of the relevant
> - * cases in the past.
> + * If the minimum state index is greater than or equal to the
> + * index of the state with the maximum intercepts metric and
> + * the corresponding state is enabled, there is no need to look
> + * at the deeper states.
> + */
> + if (min_idx >= intercept_max_idx &&
> + !dev->states_usage[min_idx].disable) {
> + idx = min_idx;
> + goto constraint;
> + }
> +
> + /*
> + * Look for the deepest enabled idle state, at most as deep as
> + * the one with the maximum intercetps metric, whose target
s/intercetps/intercepts
> + * residency had not been greater than the idle duration in over
> + * a half of the relevant cases in the past.
> *
> * Take the possible duration limitation present if the tick
> * has been stopped already into account.
> @@ -428,7 +463,8 @@ static int teo_select(struct cpuidle_dri
> continue;
>
> idx = i;
> - if (2 * intercept_sum > idx_intercept_sum)
> + if (2 * intercept_sum > idx_intercept_sum &&
> + i <= intercept_max_idx)
Ah right, we don't wanna miss the intercept 'peak' here (and we're iterating idle
states in descending order).
All good then!
Reviewed-by: Christian Loehle <christian.loehle@arm.com>
> break;
> }
> }
>
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup
2026-01-30 11:04 ` Christian Loehle
@ 2026-01-30 17:58 ` Rafael J. Wysocki
0 siblings, 0 replies; 6+ messages in thread
From: Rafael J. Wysocki @ 2026-01-30 17:58 UTC (permalink / raw)
To: Christian Loehle; +Cc: Rafael J. Wysocki, Linux PM, LKML, Doug Smythies
On Fri, Jan 30, 2026 at 12:04 PM Christian Loehle
<christian.loehle@arm.com> wrote:
>
> On 1/29/26 20:51, Rafael J. Wysocki wrote:
> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >
> > There are cases in which decisions made by the teo governor are
> > arguably overly conservative.
> >
> > For instance, suppose that there are 4 idle states and the values of
> > the intercepts metric for the first 3 of them are 400, 250, and 251,
> > respectively. If the total sum computed in teo_update() is 1000, the
> > governor will select idle state 1 (provided that all idle states are
> > enabled and the scheduler tick has not been stopped) although arguably
> > idle state 0 would be a better choice because the likelihood of getting
> > an idle duration below the target residency of idle state 1 is greater
> > than the likelihood of getting an idle duration between the target
> > residency of idle state 1 and the target residency of idle state 2.
>
> FWIW the algorithm would be self-correcting long-term here because
> selecting state1 would then no longer accumulate intercepts for >state1
> and soon enough it will select state0.
> Anyway I think this is an optimisation nonetheless.
>
> >
> > To address this, refine the candidate idle state lookup based on
> > intercepts to start at the state with the maximum intercepts metric,
> > below the deepest enabled one, to avoid the cases in which the search
> > may stop before reaching that state.
> >
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
> >
> > v2 -> v3: No changes
> >
> > v1 -> v2:
> > * Multiple fixes related to the handling of cases in which some states
> > are disabled.
> > * Fixes in new comments (there was some confusion in those comments
> > regarding the direction of idle states table traversal).
> > * Fixed typos in new comments.
> >
> > ---
> > drivers/cpuidle/governors/teo.c | 50 ++++++++++++++++++++++++++++++++++------
> > 1 file changed, 43 insertions(+), 7 deletions(-)
> >
> > --- a/drivers/cpuidle/governors/teo.c
> > +++ b/drivers/cpuidle/governors/teo.c
> > @@ -74,12 +74,17 @@
> > * than the candidate one (it represents the cases in which the CPU was
> > * likely woken up by a non-timer wakeup source).
> > *
> > + * Also find the idle state with the maximum intercepts metric (if there are
> > + * multiple states with the maximum intercetps metric, choose the one with
>
> s/intercetps/intercepts
>
> > + * the highest index).
> > + *
> > * 2. If the second sum computed in step 1 is greater than a half of the sum of
> > * both metrics for the candidate state bin and all subsequent bins (if any),
> > * a shallower idle state is likely to be more suitable, so look for it.
> > *
> > * - Traverse the enabled idle states shallower than the candidate one in the
> > - * descending order.
> > + * descending order, starting at the state with the maximum intercepts
> > + * metric found in step 1.
> > *
> > * - For each of them compute the sum of the "intercepts" metrics over all
> > * of the idle states between it and the candidate one (including the
> > @@ -308,8 +313,10 @@ static int teo_select(struct cpuidle_dri
> > ktime_t delta_tick = TICK_NSEC / 2;
> > unsigned int idx_intercept_sum = 0;
> > unsigned int intercept_sum = 0;
> > + unsigned int intercept_max = 0;
> > unsigned int idx_hit_sum = 0;
> > unsigned int hit_sum = 0;
> > + int intercept_max_idx = -1;
> > int constraint_idx = 0;
> > int idx0 = 0, idx = -1;
> > s64 duration_ns;
> > @@ -340,17 +347,32 @@ static int teo_select(struct cpuidle_dri
> > if (!dev->states_usage[0].disable)
> > idx = 0;
> >
> > - /* Compute the sums of metrics for early wakeup pattern detection. */
> > + /*
> > + * Compute the sums of metrics for early wakeup pattern detection and
> > + * look for the state bin with the maximum intercepts metric below the
> > + * deepest enabled one (if there are multiple states with the maximum
> > + * intercepts metric, choose the one with the highest index).
> > + */
> > for (i = 1; i < drv->state_count; i++) {
> > struct teo_bin *prev_bin = &cpu_data->state_bins[i-1];
> > + unsigned int prev_intercepts = prev_bin->intercepts;
> > struct cpuidle_state *s = &drv->states[i];
> >
> > /*
> > * Update the sums of idle state metrics for all of the states
> > * shallower than the current one.
> > */
> > - intercept_sum += prev_bin->intercepts;
> > hit_sum += prev_bin->hits;
> > + intercept_sum += prev_intercepts;
> > + /*
> > + * Check if this is the bin with the maximum number of
> > + * intercepts so far and in that case update the index of
> > + * the state with the maximum intercetps metric.
>
> s/intercetps/intercepts
>
> > + */
> > + if (prev_intercepts >= intercept_max) {
> > + intercept_max = prev_intercepts;
> > + intercept_max_idx = i - 1;
> > + }
> >
> > if (dev->states_usage[i].disable)
> > continue;
> > @@ -414,9 +436,22 @@ static int teo_select(struct cpuidle_dri
> > }
> >
> > /*
> > - * Look for the deepest idle state whose target residency had
> > - * not exceeded the idle duration in over a half of the relevant
> > - * cases in the past.
> > + * If the minimum state index is greater than or equal to the
> > + * index of the state with the maximum intercepts metric and
> > + * the corresponding state is enabled, there is no need to look
> > + * at the deeper states.
> > + */
> > + if (min_idx >= intercept_max_idx &&
> > + !dev->states_usage[min_idx].disable) {
> > + idx = min_idx;
> > + goto constraint;
> > + }
> > +
> > + /*
> > + * Look for the deepest enabled idle state, at most as deep as
> > + * the one with the maximum intercetps metric, whose target
>
> s/intercetps/intercepts
Yeah, I've noticed this and the above, but thanks for pointing them out!
I'll fix them when applying the patch.
> > + * residency had not been greater than the idle duration in over
> > + * a half of the relevant cases in the past.
> > *
> > * Take the possible duration limitation present if the tick
> > * has been stopped already into account.
> > @@ -428,7 +463,8 @@ static int teo_select(struct cpuidle_dri
> > continue;
> >
> > idx = i;
> > - if (2 * intercept_sum > idx_intercept_sum)
> > + if (2 * intercept_sum > idx_intercept_sum &&
> > + i <= intercept_max_idx)
>
>
> Ah right, we don't wanna miss the intercept 'peak' here (and we're iterating idle
> states in descending order).
> All good then!
> Reviewed-by: Christian Loehle <christian.loehle@arm.com>
Thanks!
> > break;
> > }
> > }
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-01-30 17:58 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-29 20:46 [PATCH v3 0/2] cpuidle: governors: teo: Wakeup events classification change and refinement Rafael J. Wysocki
2026-01-29 20:49 ` [PATCH v3 1/2] cpuidle: governors: teo: Adjust the classification of wakeup events Rafael J. Wysocki
2026-01-30 10:34 ` Christian Loehle
2026-01-29 20:51 ` [PATCH v3 2/2] cpuidle: governors: teo: Refine intercepts-based idle state lookup Rafael J. Wysocki
2026-01-30 11:04 ` Christian Loehle
2026-01-30 17:58 ` Rafael J. Wysocki
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®