diff mbox

[20/21] cpuidle: don't calculate time-diff if entered_state == 0

Message ID acd1cc837f969a50582d2b25a73c5e247f40f843.1379779777.git.viresh.kumar@linaro.org
State New
Headers show

Commit Message

Viresh Kumar Sept. 22, 2013, 1:21 a.m. UTC
If entered_state == 0, we don't need to set dev->last_residency to 'diff' as we
will be setting it to zero without using its new value.

And so move calculation of diff also inside the "if" statement.

Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/cpuidle/cpuidle.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

Comments

Daniel Lezcano Sept. 25, 2013, 10:38 p.m. UTC | #1
On 09/22/2013 03:21 AM, Viresh Kumar wrote:
> If entered_state == 0, we don't need to set dev->last_residency to 'diff' as we
> will be setting it to zero without using its new value.

I don't get it, can you elaborate. We can be a long time in this state
(eg. if the prediction is false).

> And so move calculation of diff also inside the "if" statement.
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
> ---
>  drivers/cpuidle/cpuidle.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c
> index bf80236..cb81689 100644
> --- a/drivers/cpuidle/cpuidle.c
> +++ b/drivers/cpuidle/cpuidle.c
> @@ -77,23 +77,22 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
>  
>  	struct cpuidle_state *target_state = &drv->states[index];
>  	ktime_t time_start, time_end;
> -	s64 diff;
>  
>  	time_start = ktime_get();
>  
>  	entered_state = target_state->enter(dev, drv, index);
>  
> -	time_end = ktime_get();
> +	if (entered_state >= 0) {
> +		s64 diff;
>  
> -	local_irq_enable();
> +		time_end = ktime_get();
> +		diff = ktime_to_us(ktime_sub(time_end, time_start));
>  
> -	diff = ktime_to_us(ktime_sub(time_end, time_start));
> -	if (diff > INT_MAX)
> -		diff = INT_MAX;
> +		if (diff > INT_MAX)
> +			diff = INT_MAX;
>  
> -	dev->last_residency = (int) diff;
> +		dev->last_residency = (int) diff;
>  
> -	if (entered_state >= 0) {
>  		/* Update cpuidle counters */
>  		/* This can be moved to within driver enter routine
>  		 * but that results in multiple copies of same code.
> @@ -104,6 +103,8 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
>  		dev->last_residency = 0;
>  	}
>  
> +	local_irq_enable();
> +
>  	return entered_state;
>  }
>  
>
Viresh Kumar Sept. 26, 2013, 6:24 a.m. UTC | #2
On 26 September 2013 04:08, Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
> On 09/22/2013 03:21 AM, Viresh Kumar wrote:
>> If entered_state == 0, we don't need to set dev->last_residency to 'diff' as we
>> will be setting it to zero without using its new value.
>
> I don't get it, can you elaborate.

Sure.. We have following code in cpuidle_enter_state():

---------
        diff = ktime_to_us(ktime_sub(time_end, time_start));
        if (diff > INT_MAX)
                diff = INT_MAX;

        dev->last_residency = (int) diff;

        if (entered_state >= 0) {
                /* Update cpuidle counters */
                /* This can be moved to within driver enter routine
                 * but that results in multiple copies of same code.
                 */
                dev->states_usage[entered_state].time += dev->last_residency;
                dev->states_usage[entered_state].usage++;
        } else {
                dev->last_residency = 0;
        }
-------

We are setting last_residency to 0 when (entered_state < 0) and aren't using
the value of "diff". So, we can actually skip calculations of time_end, diff and
last_residency when (entered_state < 0).. Makes sense?


> We can be a long time in this state
> (eg. if the prediction is false).

Okay.. I didn't get it :)
Daniel Lezcano Sept. 26, 2013, 8:25 a.m. UTC | #3
On 09/26/2013 08:24 AM, Viresh Kumar wrote:
> On 26 September 2013 04:08, Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
>> On 09/22/2013 03:21 AM, Viresh Kumar wrote:
>>> If entered_state == 0, we don't need to set dev->last_residency to 'diff' as we
>>> will be setting it to zero without using its new value.
>>
>> I don't get it, can you elaborate.
>
> Sure.. We have following code in cpuidle_enter_state():
>
> ---------
>          diff = ktime_to_us(ktime_sub(time_end, time_start));
>          if (diff > INT_MAX)
>                  diff = INT_MAX;
>
>          dev->last_residency = (int) diff;
>
>          if (entered_state >= 0) {
>                  /* Update cpuidle counters */
>                  /* This can be moved to within driver enter routine
>                   * but that results in multiple copies of same code.
>                   */
>                  dev->states_usage[entered_state].time += dev->last_residency;
>                  dev->states_usage[entered_state].usage++;
>          } else {
>                  dev->last_residency = 0;
>          }
> -------
>
> We are setting last_residency to 0 when (entered_state < 0) and aren't using
> the value of "diff". So, we can actually skip calculations of time_end, diff and
> last_residency when (entered_state < 0).. Makes sense?

Yes I agree, but why are you saying "If entered_state == 0, we don't 
need to ..." ?

>> We can be a long time in this state
>> (eg. if the prediction is false).
>
> Okay.. I didn't get it :)
>
Viresh Kumar Sept. 26, 2013, 8:28 a.m. UTC | #4
On 26 September 2013 13:55, Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
> Yes I agree, but why are you saying "If entered_state == 0, we don't need to
> ..." ?

Ahh.. that's a mistake.. s/==/< :)
Daniel Lezcano Sept. 26, 2013, 8:33 a.m. UTC | #5
On 09/26/2013 10:28 AM, Viresh Kumar wrote:
> On 26 September 2013 13:55, Daniel Lezcano <daniel.lezcano@linaro.org> wrote:
>> Yes I agree, but why are you saying "If entered_state == 0, we don't need to
>> ..." ?
>
> Ahh.. that's a mistake.. s/==/< :)

Ah ok.
diff mbox

Patch

diff --git a/drivers/cpuidle/cpuidle.c b/drivers/cpuidle/cpuidle.c
index bf80236..cb81689 100644
--- a/drivers/cpuidle/cpuidle.c
+++ b/drivers/cpuidle/cpuidle.c
@@ -77,23 +77,22 @@  int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 
 	struct cpuidle_state *target_state = &drv->states[index];
 	ktime_t time_start, time_end;
-	s64 diff;
 
 	time_start = ktime_get();
 
 	entered_state = target_state->enter(dev, drv, index);
 
-	time_end = ktime_get();
+	if (entered_state >= 0) {
+		s64 diff;
 
-	local_irq_enable();
+		time_end = ktime_get();
+		diff = ktime_to_us(ktime_sub(time_end, time_start));
 
-	diff = ktime_to_us(ktime_sub(time_end, time_start));
-	if (diff > INT_MAX)
-		diff = INT_MAX;
+		if (diff > INT_MAX)
+			diff = INT_MAX;
 
-	dev->last_residency = (int) diff;
+		dev->last_residency = (int) diff;
 
-	if (entered_state >= 0) {
 		/* Update cpuidle counters */
 		/* This can be moved to within driver enter routine
 		 * but that results in multiple copies of same code.
@@ -104,6 +103,8 @@  int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 		dev->last_residency = 0;
 	}
 
+	local_irq_enable();
+
 	return entered_state;
 }