Skip to contentSkip to conversation

cpuidle: poll_state: Add time limit to poll_idle()

PATCH In review Rafael J. Wysocki 4 participants 16 messages Mar 14, 2018 – Mar 27, 2018 2 messages not in the archive CPU idle time management framework

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cpuidle: poll_state: Add time limit to poll_idle()

From: Rafael J. Wysocki <[email protected]>

If poll_idle() is allowed to spin until need_resched() returns 'true', it may actually spin for a much longer time than expected by the idle governor, since set_tsk_need_resched() is not always called by the timer interrupt handler. If that happens, the CPU may spend much more time than anticipated in the "polling" state.

To prevent that from happening, limit the time of the spinning loop in poll_idle().

---

v2 -> v3: Use local_clock() for time measurements and drop the
counter, since that should be lightweight enough (as suggested by Peter).

---
 drivers/cpuidle/poll_state.c |   11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/cpuidle/poll_state.c +10 -1
Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================
@@ -6,15 +6,24 @@
 
 #include <linux/cpuidle.h>
 #include <linux/sched.h>
+#include <linux/sched/clock.h>
 #include <linux/sched/idle.h>
 
+#define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int index)
 {
+	u64 time_start = local_clock();
+
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
-		while (!need_resched())
+		while (!need_resched()) {
 			cpu_relax();
+
+			if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT)
+				break;
+		}
 	}
 	current_clr_polling();
 
#1 lore raw
Quoted 9 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If poll_idle() is allowed to spin until need_resched() returns 'true',
it may actually spin for a much longer time than expected by the idle governor, since set_tsk_need_resched() is not always called by the timer interrupt handler. If that happens, the CPU may spend much more time than anticipated in the "polling" state.

Quoted 5 lines

To prevent that from happening, limit the time of the spinning loop in poll_idle().

So ... about bisecting that other patch series...

It turned out I had this patch, which looks so obviously correct, as patch #1 in my series.

It also turned out that this patch is responsible for the entire 5-10% increase in CPU use for the memcache style workload.

I wonder if keeping an idle HT thread much busier than before slows down its sibling, or something like that.

Let me go test the nohz idle series by itself, without this patch.

Signature · 1 line
All Rights Reversed.
#2 lore raw
Quoted 21 lines · CET Rik van Riel
Quoted 9 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If poll_idle() is allowed to spin until need_resched() returns 'true',
it may actually spin for a much longer time than expected by the idle governor, since set_tsk_need_resched() is not always called by the timer interrupt handler. If that happens, the CPU may spend much more time than anticipated in the "polling" state.

Quoted 5 lines

To prevent that from happening, limit the time of the spinning loop in poll_idle().

So ... about bisecting that other patch series...

It turned out I had this patch, which looks so obviously correct, as patch #1 in my series.

Quoted 7 lines

It also turned out that this patch is responsible for the entire 5-10% increase in CPU use for the memcache style workload.

I wonder if keeping an idle HT thread much busier than before slows down its sibling, or something like that.

Uhm, sorry about this.

Does it improve if you do something like the below on top of it?

Quoted 2 lines

Let me go test the nohz idle series by itself, without this patch.

OK

---
 drivers/cpuidle/poll_state.c |    6 ++++++
 1 file changed, 6 insertions(+)
drivers/cpuidle/poll_state.c +6 -0
Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		1000
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int index)
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;
 
+			loop_count = 0;
 			if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT)
 				break;
 		}
#3 lore raw
Quoted 29 lines · Rafael J. Wysocki
Quoted 28 lines · CET Rik van Riel
Quoted 11 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If poll_idle() is allowed to spin until need_resched() returns 'true',
it may actually spin for a much longer time than expected by the idle
governor, since set_tsk_need_resched() is not always called by the
timer interrupt handler. If that happens, the CPU may spend much more time than anticipated in the "polling" state.

Quoted 6 lines

To prevent that from happening, limit the time of the spinning loop
in poll_idle().

So ... about bisecting that other patch series...

It turned out I had this patch, which looks so obviously correct, as patch #1 in my series.

It also turned out that this patch is responsible for the entire 5-10% increase in CPU use for the memcache style workload.

Quoted 5 lines
Quoted 3 lines

I wonder if keeping an idle HT thread much busier than before slows down its sibling, or something like that.

Uhm, sorry about this.

No worries, this is why we do patch reviews and tests in the first place.

Quoted 1 line

Does it improve if you do something like the below on top of it?

That was my next thing to try, after testing just the idle nohz series by itself :)

I'll push both into the test systems, and will get back to you when I have answers.

Quoted 40 lines
Quoted 2 lines

Let me go test the nohz idle series by itself, without this patch.

OK

---
 drivers/cpuidle/poll_state.c |    6 ++++++
 1 file changed, 6 insertions(+)

Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================

drivers/cpuidle/poll_state.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		1000
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int

index)

drivers/cpuidle/poll_state.c +5 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;
 
+			loop_count = 0;
 			if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
 				break;

}

Signature · 1 line
All Rights Reversed.
#4 lore raw
Quoted 39 lines · CET Rik van Riel
Quoted 29 lines · Rafael J. Wysocki
Quoted 28 lines · CET Rik van Riel
Quoted 11 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

If poll_idle() is allowed to spin until need_resched() returns 'true',
it may actually spin for a much longer time than expected by the idle
governor, since set_tsk_need_resched() is not always called by the
timer interrupt handler. If that happens, the CPU may spend much more time than anticipated in the "polling" state.

Quoted 6 lines

To prevent that from happening, limit the time of the spinning loop
in poll_idle().

So ... about bisecting that other patch series...

It turned out I had this patch, which looks so obviously correct, as patch #1 in my series.

It also turned out that this patch is responsible for the entire 5-10% increase in CPU use for the memcache style workload.

Quoted 5 lines
Quoted 3 lines

I wonder if keeping an idle HT thread much busier than before slows down its sibling, or something like that.

Uhm, sorry about this.

No worries, this is why we do patch reviews and tests in the first place.

Quoted 7 lines
Quoted 1 line

Does it improve if you do something like the below on top of it?

That was my next thing to try, after testing just the idle nohz series by itself :)

I'll push both into the test systems, and will get back to you when I have answers.

Thanks!

#5 lore raw
In reply to Rafael J. Wysocki
Quoted 26 lines · Rafael J. Wysocki

+++ linux-pm/drivers/cpuidle/poll_state.c

drivers/cpuidle/poll_state.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		1000
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int

index)

drivers/cpuidle/poll_state.c +5 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;
 
+			loop_count = 0;
 			if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
 				break;
Quoted 1 line

}

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I plan to try two more things:

1) Disable polling on SMT systems, with the idea that putting one thread to sleep with monitor/mwait in C1 will allow the other thread to run faster.

2) Insert more cpu_relax() calls into the main loop, so the CPU core spends more of its time in cpu_relax() and less time doing other things:

static int __cpuidle poll_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int index)
{
u64 time_start = local_clock();

  local_irq_enable();
  if (!current_set_polling_and_test()) {
    unsigned int loop_count = 0;
    while (!need_resched()) {
      cpu_relax();
      cpu_relax();
      cpu_relax();
      cpu_relax();
      cpu_relax();
      cpu_relax();
      cpu_relax();
      cpu_relax();
      if (loop_count++ < POLL_IDLE_COUNT)
        continue;

      loop_count = 0;
      if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT)
        break;
    }

}
current_clr_polling();

return index;
}

I will let you know how they perform.

Signature · 1 line
All Rights Reversed.
#6 lore raw
Quoted 38 lines · CEST Rik van Riel

--=-e8yLbs0aoH4SrxOskwwl
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Quoted 25 lines · Rafael J. Wysocki

=20
+++ linux-pm/drivers/cpuidle/poll_state.c

(quoted diff) +0 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>

=20
#define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16)
+#define POLL_IDLE_COUNT 1000
=20
static int __cpuidle poll_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int
index)

(quoted diff) +0 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp

=20

(quoted diff) +4 -0
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count =3D 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;

=20

(quoted diff) +1 -0
+			loop_count =3D 0;
 			if (local_clock() - time_start >

POLL_IDLE_TIME_LIMIT)

Review

Quoted 2 lines
(quoted diff) +0 -0
 				break;
 		}

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

Quoted 4 lines

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I see.

Quoted 6 lines

I plan to try two more things:

1) Disable polling on SMT systems, with the idea that putting one thread to sleep with monitor/mwait in C1 will allow the other thread to run faster.

Sounds plausible.

Quoted 4 lines

2) Insert more cpu_relax() calls into the main loop, so the CPU core spends more of its time in cpu_relax() and less time doing other things:

Well, maybe it's a matter of doing cpu_relax() between any other bits of significant computation in there:

---
 drivers/cpuidle/poll_state.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/cpuidle/poll_state.c +12 -1
Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		200
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int index)
@@ -18,11 +19,21 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
-
+			if (loop_count++ < POLL_IDLE_COUNT) {
+				cpu_relax();
+				continue;
+			}
+			cpu_relax();
+			loop_count = 0;
+			cpu_relax();
 			if (local_clock() - time_start > POLL_IDLE_TIME_LIMIT)
 				break;
+
+			cpu_relax();
 		}
 	}
 	current_clr_polling();
#7 lore raw
Quoted 55 lines · Rafael J. Wysocki
Quoted 38 lines · CEST Rik van Riel

--=-e8yLbs0aoH4SrxOskwwl
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Quoted 25 lines · Rafael J. Wysocki

=20
+++ linux-pm/drivers/cpuidle/poll_state.c

drivers/cpuidle/poll_state.c +0 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>

=20
#define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16)
+#define POLL_IDLE_COUNT 1000
=20
static int __cpuidle poll_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int
index)

(quoted diff) +0 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp

=20

drivers/cpuidle/poll_state.c +4 -0
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count =3D 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;

=20

(quoted diff) +1 -0
+			loop_count =3D 0;
 			if (local_clock() - time_start >

POLL_IDLE_TIME_LIMIT)

Review

Quoted 2 lines
(quoted diff) +0 -0
 				break;
 		}

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

Quoted 4 lines

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I see.

Quoted 6 lines

I plan to try two more things:

1) Disable polling on SMT systems, with the idea that putting one thread to sleep with monitor/mwait in C1 will allow the other thread to run faster.

Sounds plausible.

Quoted 8 lines
Quoted 4 lines

2) Insert more cpu_relax() calls into the main loop, so the CPU core spends more of its time in cpu_relax() and less time doing other things:

Well, maybe it's a matter of doing cpu_relax() between any other bits of
significant computation in there:

That sounds like a plausible thing to try.
Let me kick off a test with that variant, too.

Quoted 42 lines
 drivers/cpuidle/poll_state.c |   13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================

drivers/cpuidle/poll_state.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		200
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int

index)

drivers/cpuidle/poll_state.c +11 -1
@@ -18,11 +19,21 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
-
+			if (loop_count++ < POLL_IDLE_COUNT) {
+				cpu_relax();
+				continue;
+			}
+			cpu_relax();
+			loop_count = 0;
+			cpu_relax();
 			if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
 				break;
+
+			cpu_relax();
 		}
 	}

current_clr_polling();

Signature · 1 line
All Rights Reversed.

A message that is not in the archive (sent off-list or not yet received): 0DSBfDrt1Ffdw0DSCfOhiN

#9 lore raw
Quoted 40 lines · On 2018.03.25 14:25 Rik van Riel wrote:
Quoted 39 lines · Rafael J. Wysocki
Quoted 38 lines · CEST Rik van Riel

--=-e8yLbs0aoH4SrxOskwwl
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Quoted 26 lines · Rafael J. Wysocki

=20
+++ linux-pm/drivers/cpuidle/poll_state.c

drivers/cpuidle/poll_state.c +0 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>

=20
#define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16)
+#define POLL_IDLE_COUNT 1000
=20
static int __cpuidle poll_idle(struct cpuidle_device *dev,
struct cpuidle_driver *drv, int
index)

drivers/cpuidle/poll_state.c +5 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count =3D 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;
 
+			loop_count =3D 0;
 			if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
 				break;
Quoted 1 line

}

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I see.

I have no proof, but I do not see that as
the problem.

I think the issue is the overall exiting
and then re-entering idle state 0 much
more often, and the related overheads, where interrupts are disabled for short periods.

My jury rigged way of trying to create similar conditions seems to always have the ISR return with the need_resched() flag set, so there is no difference in idle state 0 entries per unit time between kernel 4.16-rc6 and one with the poll fixes added.

i.e. the difference between these numbers over some time:

cat /sys/devices/system/cpu/cpu*/cpuidle/state0/usage

Rik, I wonder if you see a difference with your real workflow?

... Doug

A message that is not in the archive (sent off-list or not yet received): 0LAnfFcYLFfdw0LApfPvKF

#11 lore raw
Quoted 3 lines · On 2018.03.25 23:00 Doug Smythies wrote:

On 2018.03.25 14:25 Rik van Riel wrote:

Quoted 1 line · Rafael J. Wysocki

On Sunday, March 25, 2018 10:15:52 PM CEST Rik van Riel wrote:

...[snip]...

Quoted 26 lines
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I see.

I have no proof, but I do not see that as
the problem.

I think the issue is the overall exiting
and then re-entering idle state 0 much
more often, and the related overheads, where interrupts are disabled for short periods.

My jury rigged way of trying to create similar conditions seems to always have the ISR return with the need_resched() flag set, so there is no difference in idle state 0 entries per unit time between kernel 4.16-rc6 and one with the poll fixes added.

Quoted 6 lines

i.e. the difference between these numbers over some time:

cat /sys/devices/system/cpu/cpu*/cpuidle/state0/usage

Rik, I wonder if you see a difference with your real workflow?

Using iperf, I was able to show a difference on my computer. Another computer was used as the server, and my test computer was the client. (the other way around didn't show a difference)

With Kernel 4.16-rc6 I got about ~2000 idle state 0 entries per minute and ~155 seconds residency. ~32 watts package power.

With the poll stuff included I got ~46000 idle state 0 entries per minute and ~53 seconds residency. ~20 watts package power.

... Doug

#12 lore raw
Quoted 26 lines · Doug Smythies
Quoted 3 lines · On 2018.03.25 23:00 Doug Smythies wrote:

On 2018.03.25 14:25 Rik van Riel wrote:

Quoted 1 line · Rafael J. Wysocki

On Sunday, March 25, 2018 10:15:52 PM CEST Rik van Riel wrote:

...[snip]...

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Quoted 9 lines

OK, I am still seeing a performance degradation with the above, though not throughout the entire workload.

It appears that making the idle loop do anything besides cpu_relax() for a significant amount of time slows things down.

I see.

I have no proof, but I do not see that as
the problem.

I think the issue is the overall exiting
and then re-entering idle state 0 much
more often, and the related overheads, where interrupts are disabled for short periods.

That is a very good point.

So far we have assumed that the performance impact was due to what happened in poll_idle(), but it very well may be due to how often poll_idle() is called and returns over a unit of time.

There may be workloads in which wakeups occur so often that idle states above 1 are (almost) never selected and in these workloads there will be increased overhead related to entering and exiting state 0 (polling) with the timeout. That is inevitable.

Rik, if your workload is one of these, you will see performance impact. However, you also should see a difference in power (see below).

Quoted 17 lines
Quoted 6 lines

My jury rigged way of trying to create similar conditions seems to always have the ISR return with the need_resched() flag set, so there is no difference in idle state 0 entries per unit time between kernel 4.16-rc6 and one with the poll fixes added.

Quoted 6 lines

i.e. the difference between these numbers over some time:

cat /sys/devices/system/cpu/cpu*/cpuidle/state0/usage

Rik, I wonder if you see a difference with your real workflow?

Using iperf, I was able to show a difference on my computer. Another computer was used as the server, and my test computer was the client. (the other way around didn't show a difference)

Quoted 5 lines

With Kernel 4.16-rc6 I got about ~2000 idle state 0 entries per minute and ~155 seconds residency. ~32 watts package power.

With the poll stuff included I got ~46000 idle state 0 entries per minute and ~53 seconds residency. ~20 watts package power.

OK, so that's 30%+ of a difference in power.

#13 lore raw
In reply to Rafael J. Wysocki
Quoted 10 lines · Rafael J. Wysocki
Quoted 7 lines · CEST Rik van Riel

I plan to try two more things:

1) Disable polling on SMT systems, with
the idea that putting one thread to
sleep with monitor/mwait in C1 will
allow the other thread to run faster.

Sounds plausible.

Plausible, but wrong. Tests showed that CPU use during the peak load of this test increased from about 71% to about 78% with this change, or just under 10% increase relative to the baseline.

Coincidentally, that is the same CPU use increase I have seen with the poll_idle() changes. Not sure if that means anything...

Quoted 8 lines
Quoted 4 lines

2) Insert more cpu_relax() calls into the
main loop, so the CPU core spends more
of its time in cpu_relax() and less
time doing other things:

Well, maybe it's a matter of doing cpu_relax() between any other bits of
significant computation in there:

I tried that, as well, and some other variations.

Every single change to poll_idle() that I tried seems to result in a 9-10% relative increase in CPU use during the peak load of the test.

During the busiest parts of the load, every CPU sees on the order of 20k context switches a second.

kind regards,

Rik

Signature · 1 line
All Rights Reversed.
#14 lore raw
Quoted 30 lines · Rik van Riel
Quoted 10 lines · Rafael J. Wysocki
Quoted 7 lines · CEST Rik van Riel

I plan to try two more things:

1) Disable polling on SMT systems, with
the idea that putting one thread to
sleep with monitor/mwait in C1 will
allow the other thread to run faster.

Sounds plausible.

Plausible, but wrong. Tests showed that CPU use during the peak load of this test increased from about 71% to about 78% with this change, or just under 10% increase relative to the baseline.

Coincidentally, that is the same CPU use increase I have seen with the poll_idle() changes. Not sure if that means anything...

Quoted 8 lines
Quoted 4 lines

2) Insert more cpu_relax() calls into the
main loop, so the CPU core spends more
of its time in cpu_relax() and less
time doing other things:

Well, maybe it's a matter of doing cpu_relax() between any other bits of
significant computation in there:

Quoted 8 lines

I tried that, as well, and some other variations.

Every single change to poll_idle() that I tried seems to result in a 9-10% relative increase in CPU use during the peak load of the test.

During the busiest parts of the load, every CPU sees on the order of 20k context switches a second.

Hmm. Basically, you are saying that

while (something)

    cpu_relax();

is measurably less overhead than

while (something) {
    cpu_relax();
    check something else;
    cpu_relax();
}

which honestly makes me wonder how this is possible at all.

Unless, that is, the Doug's theory actually matches the reality and the difference is related to the fact that the "something else" causes the loop to terminate after fewer steps.

Which in turn causes poll_idle() to return to the outer loop in do_idle() and *that* is enough overhead to explain what you are seeing.

So, it looks like after the change causing the poll_idle() loop to terminate in fewer steps (which is generally good for power and also for performance in some workloads) you are measuring the overhead of the outer loop in do_idle() instead of the overhead of the loop in poll_idle() itself.

With that in mind (and I'm going to assume that this is the case until I see convincing evidence to the contrary), it seems to me that your workload will be unhappy if *any* changes are made to poll_idle(), but we simply have to change it, because today it is kind of a design disaster.

#15 lore raw
Quoted 4 lines · Rafael J. Wysocki
Quoted 2 lines · Rik van Riel

On Sun, 2018-03-25 at 23:34 +0200, Rafael J. Wysocki wrote:

Quoted 8 lines
Quoted 8 lines

I tried that, as well, and some other variations.

Every single change to poll_idle() that I tried seems to result in a 9-10% relative increase in CPU use during the peak load of the test.

During the busiest parts of the load, every CPU sees on the order of 20k context switches a second.

... and I did something wrong in the tests :/

Quoted 7 lines

Hmm. Basically, you are saying that

while (something)

    cpu_relax();

is measurably less overhead than

Quoted 7 lines
while (something) {
    cpu_relax();
    check something else;
    cpu_relax();
}

which honestly makes me wonder how this is possible at all.

Part of the mystery is solved. Apparently the control kernel was not the one I thought it was, or the one that was printed in the output messages :(

I am re-running the tests now with a few variations, and seeing what works best.

My apologies for wasting everybody's time.

I should have valid results later tonight. Fingers crossed.

Signature · 1 line
All Rights Reversed.
#16 lore raw
In reply to Rafael J. Wysocki
Quoted 1 line · Rafael J. Wysocki

Does it improve if you do something like the below on top of it?

First off, apologies for doing my testing wrong. There was an error, which was cleverly hidden by scripts, and things were not running the way they should.

After running the tests right, I have seen some results. My own test patch of disabling polling, and always dropping into at least C1, resulted in a CPU use increase of 0.4%.

Your patch below, with a single cpu_relax(), and the local variable to not read the TSC too often, seems to give the best performance here, with a 0.2% reduction in CPU use.

Again, apologies for messing up the testing.

I will kick off proper tests for that other patch series right now.

Quoted 40 lines
Quoted 2 lines

Let me go test the nohz idle series by itself,
without this patch.

OK

---
 drivers/cpuidle/poll_state.c |    6 ++++++
 1 file changed, 6 insertions(+)

Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================

drivers/cpuidle/poll_state.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT	(TICK_NSEC / 16)
+#define POLL_IDLE_COUNT		1000
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
 			       struct cpuidle_driver *drv, int

index)

drivers/cpuidle/poll_state.c +5 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
 	local_irq_enable();
 	if (!current_set_polling_and_test()) {
+		unsigned int loop_count = 0;
+
 		while (!need_resched()) {
 			cpu_relax();
+			if (loop_count++ < POLL_IDLE_COUNT)
+				continue;
 
+			loop_count = 0;
 			if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
 				break;

}

Signature · 1 line
All Rights Reversed.
#17 lore raw
Quoted 8 lines · Rik van Riel
Quoted 1 line · Rafael J. Wysocki

Does it improve if you do something like the below on top of it?

First off, apologies for doing my testing wrong. There was an error, which was cleverly hidden by scripts, and things were not running the way they should.

No worries.

I'm glad that what you really see appears to match my understanding of things. :-)

Quoted 9 lines

After running the tests right, I have seen some results. My own test patch of disabling polling, and always dropping into at least C1, resulted in a CPU use increase of 0.4%.

Your patch below, with a single cpu_relax(), and the local variable to not read the TSC too often, seems to give the best performance here, with a 0.2% reduction in CPU use.

Cool.

Quoted 4 lines

Again, apologies for messing up the testing.

I will kick off proper tests for that other patch series right now.

Thanks!

Quoted 42 lines
Quoted 37 lines
Quoted 2 lines

Let me go test the nohz idle series by itself,
without this patch.

OK

---
 drivers/cpuidle/poll_state.c |    6 ++++++
 1 file changed, 6 insertions(+)

Index: linux-pm/drivers/cpuidle/poll_state.c
===================================================================

drivers/cpuidle/poll_state.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/sched/idle.h>
 
 #define POLL_IDLE_TIME_LIMIT (TICK_NSEC / 16)
+#define POLL_IDLE_COUNT              1000
 
 static int __cpuidle poll_idle(struct cpuidle_device *dev,
                             struct cpuidle_driver *drv, int

index)

drivers/cpuidle/poll_state.c +5 -0
@@ -18,9 +19,14 @@ static int __cpuidle poll_idle(struct cp
 
      local_irq_enable();
      if (!current_set_polling_and_test()) {
+             unsigned int loop_count = 0;
+
              while (!need_resched()) {
                      cpu_relax();
+                     if (loop_count++ < POLL_IDLE_COUNT)
+                             continue;
 
+                     loop_count = 0;
                      if (local_clock() - time_start >
 POLL_IDLE_TIME_LIMIT)
                              break;
Quoted 3 lines
              }

--
All Rights Reversed.