diff mbox series

[5/6] cpuidle: menu: Avoid computations for very close timers

Message ID 2753018.aK9edJ7Dvu@aspire.rjw.lan (mailing list archive)
State Mainlined
Delegated to: Rafael Wysocki
Headers show
Series cpuidle: menu: Fixes, optimizations and cleanups | expand

Commit Message

Rafael J. Wysocki Oct. 2, 2018, 9:46 p.m. UTC
From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

If the next timer event (with the tick excluded) is closer than the
target residency of the second state or the PM QoS latency constraint
is below its exit latency, state[0] will be used regardless of any
other factors, so skip the computations in menu_select() then and
return 0 straight away from it.

Still, do that after the bucket has been determined to avoid
disturbing the wakeup statistics in general.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/cpuidle/governors/menu.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Daniel Lezcano Oct. 4, 2018, 3:50 p.m. UTC | #1
On Tue, Oct 02, 2018 at 11:46:28PM +0200, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> If the next timer event (with the tick excluded) is closer than the
> target residency of the second state or the PM QoS latency constraint
> is below its exit latency, state[0] will be used regardless of any
> other factors, so skip the computations in menu_select() then and
> return 0 straight away from it.
> 
> Still, do that after the bucket has been determined to avoid
> disturbing the wakeup statistics in general.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> ---
>  drivers/cpuidle/governors/menu.c |   12 ++++++++++++
>  1 file changed, 12 insertions(+)
> 
> Index: linux-pm/drivers/cpuidle/governors/menu.c
> ===================================================================
> --- linux-pm.orig/drivers/cpuidle/governors/menu.c
> +++ linux-pm/drivers/cpuidle/governors/menu.c
> @@ -309,6 +309,18 @@ static int menu_select(struct cpuidle_dr
>  	get_iowait_load(&nr_iowaiters, &cpu_load);
>  	data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
>  
> +	if (unlikely(drv->state_count <= 1) ||

I'm not sure this test is necessary.

If state_count == 0, we don't have to select anything as the cpuidle can't register because of:

static int __cpuidle_register_driver(struct cpuidle_driver *drv)
{
        int ret;

        if (!drv || !drv->state_count)
                return -EINVAL;

	[ ... ]
}

If state_count == 1, then there is nothing to select, it is always the state 0.

> +	    ((data->next_timer_us < drv->states[1].target_residency ||
> +	      latency_req < drv->states[1].exit_latency) &&
> +	     !drv->states[0].disabled && !dev->states_usage[0].disable)) {
> +		/*
> +		 * In this case state[0] will be used no matter what, so return
> +		 * it right away and keep the tick running.
> +		 */
> +		*stop_tick = false;
> +		return 0;
> +	}
> +
>  	/*
>  	 * Force the result of multiplication to be 64 bits even if both
>  	 * operands are 32 bits.
>
Rafael J. Wysocki Oct. 4, 2018, 5:11 p.m. UTC | #2
On Thu, Oct 4, 2018 at 5:50 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
>
> On Tue, Oct 02, 2018 at 11:46:28PM +0200, Rafael J. Wysocki wrote:
> > From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> >
> > If the next timer event (with the tick excluded) is closer than the
> > target residency of the second state or the PM QoS latency constraint
> > is below its exit latency, state[0] will be used regardless of any
> > other factors, so skip the computations in menu_select() then and
> > return 0 straight away from it.
> >
> > Still, do that after the bucket has been determined to avoid
> > disturbing the wakeup statistics in general.
> >
> > Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> > ---
> >  drivers/cpuidle/governors/menu.c |   12 ++++++++++++
> >  1 file changed, 12 insertions(+)
> >
> > Index: linux-pm/drivers/cpuidle/governors/menu.c
> > ===================================================================
> > --- linux-pm.orig/drivers/cpuidle/governors/menu.c
> > +++ linux-pm/drivers/cpuidle/governors/menu.c
> > @@ -309,6 +309,18 @@ static int menu_select(struct cpuidle_dr
> >       get_iowait_load(&nr_iowaiters, &cpu_load);
> >       data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
> >
> > +     if (unlikely(drv->state_count <= 1) ||
>
> I'm not sure this test is necessary.

Yes, it is IMO.

Strictly speaking it prevents state[1] from being accessed if the
count is not 2 at least.

> If state_count == 0, we don't have to select anything as the cpuidle can't register because of:
>
> static int __cpuidle_register_driver(struct cpuidle_driver *drv)
> {
>         int ret;
>
>         if (!drv || !drv->state_count)
>                 return -EINVAL;
>
>         [ ... ]
> }
>
> If state_count == 1, then there is nothing to select, it is always the state 0.

Which is why it is better to simply return 0 right away in this case. :-)

I guess I could compare state_count just to 1, but <= 1 works too.
diff mbox series

Patch

Index: linux-pm/drivers/cpuidle/governors/menu.c
===================================================================
--- linux-pm.orig/drivers/cpuidle/governors/menu.c
+++ linux-pm/drivers/cpuidle/governors/menu.c
@@ -309,6 +309,18 @@  static int menu_select(struct cpuidle_dr
 	get_iowait_load(&nr_iowaiters, &cpu_load);
 	data->bucket = which_bucket(data->next_timer_us, nr_iowaiters);
 
+	if (unlikely(drv->state_count <= 1) ||
+	    ((data->next_timer_us < drv->states[1].target_residency ||
+	      latency_req < drv->states[1].exit_latency) &&
+	     !drv->states[0].disabled && !dev->states_usage[0].disable)) {
+		/*
+		 * In this case state[0] will be used no matter what, so return
+		 * it right away and keep the tick running.
+		 */
+		*stop_tick = false;
+		return 0;
+	}
+
 	/*
 	 * Force the result of multiplication to be 64 bits even if both
 	 * operands are 32 bits.