Skip to contentSkip to conversation

cpuidle: enter_state: Don't needlessly calculate diff time

PATCH Superseded Fieah Lim 2 participants 4 messages Sep 10, 2018 CPU idle time management framework

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fieah Lim

author
#0 lore raw

PATCH v2 1/1 cpuidle: enter_state: Don't needlessly calculate diff time

Currently we use ktime_us_delta() to calculate last residency and state usage unconditionally, it makes no sense to do this calculation when we fails to enter any idle state.
It can be optimize by moving the calculation after entered_state >= 0

While at it, merge those comment blocks into one and remove a space between type casting of diff.

This patch has no functional changes.

---
Changes from v1:
- Rewrite changelog as Rafael J. Wysocki suggested

---
 drivers/cpuidle/cpuidle.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +8 -9
index 6df894d65d9e..5f6b2c9b6555 100644
@@ -247,21 +247,20 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
 	if (!cpuidle_state_is_coupled(drv, index))
 		local_irq_enable();
 
-	diff = ktime_us_delta(time_end, time_start);
-	if (diff > INT_MAX)
-		diff = INT_MAX;
-
-	dev->last_residency = (int) diff;
+	dev->last_residency = 0;
 
 	if (entered_state >= 0) {
-		/* Update cpuidle counters */
-		/* This can be moved to within driver enter routine
+		/* Update cpuidle counters
+		 * This can be moved to within driver enter routine,
 		 * but that results in multiple copies of same code.
 		 */
+		diff = ktime_us_delta(time_end, time_start);
+		if (diff > INT_MAX)
+			diff = INT_MAX;
+
+		dev->last_residency = (int)diff;
 		dev->states_usage[entered_state].time += dev->last_residency;
 		dev->states_usage[entered_state].usage++;
-	} else {
-		dev->last_residency = 0;
 	}
 
 	return entered_state;
Signature · 1 line
2.18.0
#1 lore raw
Quoted 24 lines · Fieah Lim

Currently we use ktime_us_delta() to calculate last residency and state usage unconditionally, it makes no sense to do this calculation when we fails to enter any idle state.
It can be optimize by moving the calculation after entered_state >= 0

While at it, merge those comment blocks into one and remove a space between type casting of diff.

This patch has no functional changes.

---
Changes from v1:
- Rewrite changelog as Rafael J. Wysocki suggested

---
 drivers/cpuidle/cpuidle.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +0 -0
index 6df894d65d9e..5f6b2c9b6555 100644

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 250

Quoted 9 lines
drivers/cpuidle/cpuidle.c +1 -5
@@ -247,21 +247,20 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
        if (!cpuidle_state_is_coupled(drv, index))
                local_irq_enable();
 
-       diff = ktime_us_delta(time_end, time_start);
-       if (diff > INT_MAX)
-               diff = INT_MAX;
-
-       dev->last_residency = (int) diff;
+       dev->last_residency = 0;

You don't need this line if you retain the "else" branch below, do you?

Quoted 23 lines
drivers/cpuidle/cpuidle.c +7 -5
        if (entered_state >= 0) {
-               /* Update cpuidle counters */
-               /* This can be moved to within driver enter routine
+               /* Update cpuidle counters
+                * This can be moved to within driver enter routine,
                 * but that results in multiple copies of same code.
                 */
+               diff = ktime_us_delta(time_end, time_start);
+               if (diff > INT_MAX)
+                       diff = INT_MAX;
+
+               dev->last_residency = (int)diff;
                dev->states_usage[entered_state].time += dev->last_residency;
                dev->states_usage[entered_state].usage++;
-       } else {
-               dev->last_residency = 0;
        }
 
        return entered_state;
--

2.18.0

author
#2 lore raw
Quoted 28 lines · Rafael J. Wysocki
Quoted 27 lines · Fieah Lim

Currently we use ktime_us_delta() to calculate last residency and state usage unconditionally, it makes no sense to do this calculation when we fails to enter any idle state.
It can be optimize by moving the calculation after entered_state >= 0

While at it, merge those comment blocks into one and remove a space between type casting of diff.

This patch has no functional changes.

---
Changes from v1:
- Rewrite changelog as Rafael J. Wysocki suggested

---
 drivers/cpuidle/cpuidle.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +0 -0
index 6df894d65d9e..5f6b2c9b6555 100644
@@ -247,21 +247,20 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
        if (!cpuidle_state_is_coupled(drv, index))
                local_irq_enable();
 
Quoted 9 lines

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 250

Quoted 6 lines
drivers/cpuidle/cpuidle.c +1 -5
-       diff = ktime_us_delta(time_end, time_start);
-       if (diff > INT_MAX)
-               diff = INT_MAX;
-
-       dev->last_residency = (int) diff;
+       dev->last_residency = 0;

You don't need this line if you retain the "else" branch below, do you?

You are right, should I make a v3?

Please bear with me if anything wrong in this reply.
I'm not sure if I doing this right. This is my first reply to a mailing list. Thank you.

Quoted 23 lines
Quoted 23 lines
drivers/cpuidle/cpuidle.c +7 -5
        if (entered_state >= 0) {
-               /* Update cpuidle counters */
-               /* This can be moved to within driver enter routine
+               /* Update cpuidle counters
+                * This can be moved to within driver enter routine,
                 * but that results in multiple copies of same code.
                 */
+               diff = ktime_us_delta(time_end, time_start);
+               if (diff > INT_MAX)
+                       diff = INT_MAX;
+
+               dev->last_residency = (int)diff;
                dev->states_usage[entered_state].time += dev->last_residency;
                dev->states_usage[entered_state].usage++;
-       } else {
-               dev->last_residency = 0;
        }
 
        return entered_state;
--

2.18.0

#3 lore raw
Quoted 39 lines · Fieah Lim
Quoted 28 lines · Rafael J. Wysocki
Quoted 27 lines · Fieah Lim

Currently we use ktime_us_delta() to calculate last residency and state usage unconditionally, it makes no sense to do this calculation when we fails to enter any idle state.
It can be optimize by moving the calculation after entered_state >= 0

While at it, merge those comment blocks into one and remove a space between type casting of diff.

This patch has no functional changes.

---
Changes from v1:
- Rewrite changelog as Rafael J. Wysocki suggested

---
 drivers/cpuidle/cpuidle.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
drivers/cpuidle/cpuidle.c +0 -0
index 6df894d65d9e..5f6b2c9b6555 100644
@@ -247,21 +247,20 @@ int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
        if (!cpuidle_state_is_coupled(drv, index))
                local_irq_enable();
 
Quoted 9 lines

drivers/cpuidle/cpuidle.c int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, · line 250

Quoted 6 lines
drivers/cpuidle/cpuidle.c +1 -5
-       diff = ktime_us_delta(time_end, time_start);
-       if (diff > INT_MAX)
-               diff = INT_MAX;
-
-       dev->last_residency = (int) diff;
+       dev->last_residency = 0;

You don't need this line if you retain the "else" branch below, do you?

Quoted 1 line

You are right, should I make a v3?

Yes, please.