Skip to contentSkip to conversation

cpuidle: Time accounting fix

PATCH In review Venkatesh Pallipadi 3 participants 7 messages Sep 29, 2008 – Oct 16, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover cpuidle: Time accounting fix

As noted in PM minisummit notes here
http://lwn.net/Articles/292447/

Richard Woodruff <[email protected]> reported a bug
"the CPUIDLE bug that if target is avoided due to BM activity the original target state is still accounted the time."

Following provides a mechanism to fix the problem for all drivers and fixes the problem for acpi cpuidle driver.

#1 lore raw

PATCH 1/2 cpuidle: use last_state which can reflect the actual state entered

cpuidle accounts the idle time for the C-state it was trying to enter and not to the actual state that the driver eventually entered. The driver may select a different state than the one chosen by cpuidle due to constraints like bus-mastering, etc.

Change the time acounting code to look at the dev->last_state after returning from target_state->enter(). Driver can modify dev->last_state internally, inside the enter routine to reflect the actual C-state entered.

---
 drivers/cpuidle/cpuidle.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/cpuidle/cpuidle.c +4 -1
Index: tip/drivers/cpuidle/cpuidle.c
===================================================================
@@ -71,8 +71,11 @@ static void cpuidle_idle_call(void)
 	target_state = &dev->states[next_state];
 
 	/* enter the state and update stats */
-	dev->last_residency = target_state->enter(dev, target_state);
 	dev->last_state = target_state;
+	dev->last_residency = target_state->enter(dev, target_state);
+	if (dev->last_state)
+		target_state = dev->last_state;
+
 	target_state->time += (unsigned long long)dev->last_residency;
 	target_state->usage++;
 
#2 lore raw

Re: [patch 1/2] cpuidle: use last_state which can reflect the actual state entered

Quoted 22 lines · Venkatesh Pallipadi

cpuidle accounts the idle time for the C-state it was trying to enter and not to the actual state that the driver eventually entered. The driver may select a different state than the one chosen by cpuidle due to constraints like bus-mastering, etc.

Change the time acounting code to look at the dev->last_state after returning from target_state->enter(). Driver can modify dev->last_state internally, inside the enter routine to reflect the actual C-state entered.

---
 drivers/cpuidle/cpuidle.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: tip/drivers/cpuidle/cpuidle.c
===================================================================

drivers/cpuidle/cpuidle.c +0 -0
@@ -71,8 +71,11 @@ static void cpuidle_idle_call(void)
 	target_state = &dev->states[next_state];

drivers/cpuidle/cpuidle.c static void cpuidle_idle_call(void) · line 80

Quoted 10 lines
drivers/cpuidle/cpuidle.c +4 -1
 
 	/* enter the state and update stats */
-	dev->last_residency = target_state->enter(dev, target_state);
 	dev->last_state = target_state;
+	dev->last_residency = target_state->enter(dev, target_state);
+	if (dev->last_state)
+		target_state = dev->last_state;
+
 	target_state->time += (unsigned long long)dev->last_residency;
 	target_state->usage++;

Under what conditions would the enter hook set dev->last_sate to NULL? Having the check seems to indicate it's possilble.

A minor nit-pick... why not explicitly do the accounting using 'last_state' like below. While functionally the same as above, this makes it makes it more explicit when reading the code that the accounting is done using 'last_state' and not 'target_state.'

Kevin

drivers/cpuidle/cpuidle.c +3 -3
index 5ce07b5..c1294f5 100644
@@ -67,10 +67,10 @@ static void cpuidle_idle_call(void)
 	target_state = &dev->states[next_state];
 
 	/* enter the state and update stats */
-	dev->last_residency = target_state->enter(dev, target_state);
 	dev->last_state = target_state;
-	target_state->time += (unsigned long long)dev->last_residency;
-	target_state->usage++;
+	dev->last_residency = target_state->enter(dev, target_state);
+	dev->last_state->time += (unsigned long long)dev->last_residency;
+	dev->last_state->usage++;
 
 	/* give the governor an opportunity to reflect on the outcome */
 	if (cpuidle_curr_governor->reflect)
#3 lore raw

Re: [patch 1/2] cpuidle: use last_state which can reflect the actual state entered

Quoted 40 lines

-----Original Message-----
From: Kevin Hilman [mailto:[email protected]]
Sent: Wednesday, October 01, 2008 4:09 AM
To: Pallipadi, Venkatesh
Cc: [email protected]; [email protected]
Subject: Re: [linux-pm] [patch 1/2] cpuidle: use last_state which can reflect the actual state entered

Quoted 1 line · Venkatesh Pallipadi

cpuidle accounts the idle time for the C-state it was trying

to enter and

Quoted 1 line

not to the actual state that the driver eventually entered.

The driver may

Quoted 5 lines

select a different state than the one chosen by cpuidle due to constraints like bus-mastering, etc.

Change the time acounting code to look at the dev->last_state after returning from target_state->enter(). Driver can modify

dev->last_state

Quoted 5 lines

internally, inside the enter routine to reflect the actual C-state entered.

Quoted 7 lines
---
 drivers/cpuidle/cpuidle.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: tip/drivers/cpuidle/cpuidle.c
===================================================================
--- tip.orig/drivers/cpuidle/cpuidle.c 2008-09-23

13:52:59.000000000 -0700

Quoted 1 line

+++ tip/drivers/cpuidle/cpuidle.c 2008-09-23

14:22:43.000000000 -0700

Quoted 3 lines
drivers/cpuidle/cpuidle.c +0 -0
@@ -71,8 +71,11 @@ static void cpuidle_idle_call(void)
      target_state = &dev->states[next_state];
 
Quoted 12 lines

Review static void cpuidle_idle_call(void) · line 80

Quoted 9 lines
(quoted diff) +4 -1
      /* enter the state and update stats */
-     dev->last_residency = target_state->enter(dev, target_state);
      dev->last_state = target_state;
+     dev->last_residency = target_state->enter(dev, target_state);
+     if (dev->last_state)
+             target_state = dev->last_state;
+
      target_state->time += (unsigned long long)dev->last_residency;
      target_state->usage++;

Under what conditions would the enter hook set dev->last_sate to NULL? Having the check seems to indicate it's possilble.

Sorry about the delayed response.

Yes. I added the check after finding out that last_state can be possibly NULL. That happens when the governor changes while one core is in idle and also during CPU offline/online.

Quoted 5 lines

A minor nit-pick... why not explicitly do the accounting using 'last_state' like below. While functionally the same as above, this makes it makes it more explicit when reading the code that the accounting is done using 'last_state' and not 'target_state.'

Yes. It is cleaner. But, we still have to check for last_state being NULL.

Quoted 21 lines
drivers/cpuidle/cpuidle.c +3 -3
index 5ce07b5..c1294f5 100644
@@ -67,10 +67,10 @@ static void cpuidle_idle_call(void)
       target_state = &dev->states[next_state];
 
       /* enter the state and update stats */
-       dev->last_residency = target_state->enter(dev, target_state);
       dev->last_state = target_state;
-       target_state->time += (unsigned long long)dev->last_residency;
-       target_state->usage++;
+       dev->last_residency = target_state->enter(dev, target_state);
+       dev->last_state->time += (unsigned long
 long)dev->last_residency;
+       dev->last_state->usage++;
 
       /* give the governor an opportunity to reflect on the

outcome */

Quoted 2 lines
       if (cpuidle_curr_governor->reflect)

Thanks,
Venki

Tested-by
#4 lore raw

Re: [patch 1/2] cpuidle: use last_state which can reflect the actual state entered

Quoted 43 lines · Venkatesh
Quoted 30 lines
Quoted 1 line · Venkatesh Pallipadi

cpuidle accounts the idle time for the C-state it was trying

to enter and

Quoted 1 line

not to the actual state that the driver eventually entered.

The driver may

Quoted 5 lines

select a different state than the one chosen by cpuidle due to constraints like bus-mastering, etc.

Change the time acounting code to look at the dev->last_state after returning from target_state->enter(). Driver can modify

dev->last_state

Quoted 5 lines

internally, inside the enter routine to reflect the actual C-state entered.

Quoted 7 lines
---
 drivers/cpuidle/cpuidle.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: tip/drivers/cpuidle/cpuidle.c
===================================================================
--- tip.orig/drivers/cpuidle/cpuidle.c 2008-09-23

13:52:59.000000000 -0700

Quoted 1 line

+++ tip/drivers/cpuidle/cpuidle.c 2008-09-23

14:22:43.000000000 -0700

Quoted 3 lines
drivers/cpuidle/cpuidle.c +0 -0
@@ -71,8 +71,11 @@ static void cpuidle_idle_call(void)
      target_state = &dev->states[next_state];
 
Quoted 12 lines

Review static void cpuidle_idle_call(void) · line 80

Quoted 9 lines
(quoted diff) +4 -1
      /* enter the state and update stats */
-     dev->last_residency = target_state->enter(dev, target_state);
      dev->last_state = target_state;
+     dev->last_residency = target_state->enter(dev, target_state);
+     if (dev->last_state)
+             target_state = dev->last_state;
+
      target_state->time += (unsigned long long)dev->last_residency;
      target_state->usage++;

Under what conditions would the enter hook set dev->last_sate to NULL? Having the check seems to indicate it's possilble.

Quoted 5 lines

Sorry about the delayed response.

Yes. I added the check after finding out that last_state can be possibly NULL. That happens when the governor changes while one core is in idle and also during CPU offline/online.

OK, thanks for the clarification.

Quoted 7 lines
Quoted 5 lines

A minor nit-pick... why not explicitly do the accounting using 'last_state' like below. While functionally the same as above, this makes it makes it more explicit when reading the code that the accounting is done using 'last_state' and not 'target_state.'

Yes. It is cleaner. But, we still have to check for last_state being NULL.

Agreed.

I've switched to your version and tested this on OMAP3 and it's working well.

Quoted 26 lines
Quoted 21 lines
drivers/cpuidle/cpuidle.c +3 -3
index 5ce07b5..c1294f5 100644
@@ -67,10 +67,10 @@ static void cpuidle_idle_call(void)
       target_state = &dev->states[next_state];
 
       /* enter the state and update stats */
-       dev->last_residency = target_state->enter(dev, target_state);
       dev->last_state = target_state;
-       target_state->time += (unsigned long long)dev->last_residency;
-       target_state->usage++;
+       dev->last_residency = target_state->enter(dev, target_state);
+       dev->last_state->time += (unsigned long
 long)dev->last_residency;
+       dev->last_state->usage++;
 
       /* give the governor an opportunity to reflect on the

outcome */

Quoted 2 lines
       if (cpuidle_curr_governor->reflect)

Thanks,
Venki

reviewer
#5 lore raw

Re: [patch 1/2] cpuidle: use last_state which can reflect the actual state entered

In reply to Venkatesh Pallipadi

1,2 applied.

thanks,
-len

Quoted 36 lines · Venkatesh Pallipadi

cpuidle accounts the idle time for the C-state it was trying to enter and not to the actual state that the driver eventually entered. The driver may select a different state than the one chosen by cpuidle due to constraints like bus-mastering, etc.

Change the time acounting code to look at the dev->last_state after returning from target_state->enter(). Driver can modify dev->last_state internally, inside the enter routine to reflect the actual C-state entered.

---
 drivers/cpuidle/cpuidle.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Index: tip/drivers/cpuidle/cpuidle.c
===================================================================

drivers/cpuidle/cpuidle.c +4 -1
@@ -71,8 +71,11 @@ static void cpuidle_idle_call(void)
 	target_state = &dev->states[next_state];
 
 	/* enter the state and update stats */
-	dev->last_residency = target_state->enter(dev, target_state);
 	dev->last_state = target_state;
+	dev->last_residency = target_state->enter(dev, target_state);
+	if (dev->last_state)
+		target_state = dev->last_state;
+
 	target_state->time += (unsigned long long)dev->last_residency;
 	target_state->usage++;
 
Signature · 1 line
-- 
#6 lore raw

PATCH 2/2 cpuidle: update the last_state acpi cpuidle reflecting actual state entered

In reply to Venkatesh Pallipadi

reflect the actual state entered in dev->last_state, when actaul state entered is different from intended one.

---
 drivers/acpi/processor_idle.c |    1 +
 1 file changed, 1 insertion(+)
drivers/acpi/processor_idle.c +1 -0
Index: tip/drivers/acpi/processor_idle.c
===================================================================
@@ -1587,6 +1587,7 @@ static int acpi_idle_enter_bm(struct cpu
 
 	if (acpi_idle_bm_check()) {
 		if (dev->safe_state) {
+			dev->last_state = dev->safe_state;
 			return dev->safe_state->enter(dev, dev->safe_state);
 		} else {
 			local_irq_disable();