Skip to contentSkip to conversation

sched: Reduce runqueue lock contention -v6

PATCH In review Peter Zijlstra 10 participants 152 messages Apr 5, 2011 – Jun 8, 2011 Page 2 of 2

Subsystems (14)
Arm (32-bit) x86 PowerPC MIPS +10 more subsystems
lore
Share & tools
lore
#146 lore raw

[tip:sched/locking] sched: Move the second half of ttwu() to the remote cpu

In reply to Peter Zijlstra
Commit-ID:  317f394160e9beb97d19a84c39b7e5eb3d7815a8
Gitweb:     http://git.kernel.org/tip/317f394160e9beb97d19a84c39b7e5eb3d7815a8
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Tue, 5 Apr 2011 17:23:58 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 14 Apr 2011 08:52:41 +0200

sched: Move the second half of ttwu() to the remote cpu

Now that we've removed the rq->lock requirement from the first part of ttwu() and can compute placement without holding any rq->lock, ensure we execute the second half of ttwu() on the actual cpu we want the task to run on.

This avoids having to take rq->lock and doing the task enqueue remotely, saving lots on cacheline transfers.

As measured using: http://oss.oracle.com/~mason/sembench.c

  $ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance > $i; done
  $ echo 4096 32000 64 128 > /proc/sys/kernel/sem
  $ ./sembench -t 2048 -w 1900 -o 0

unpatched: run time 30 seconds 647278 worker burns per second
patched: run time 30 seconds 816715 worker burns per second

---
 include/linux/sched.h   |    3 +-
 init/Kconfig            |    5 ++++
 kernel/sched.c          |   56 +++++++++++++++++++++++++++++++++++++++++++++++
 kernel/sched_features.h |    6 +++++
 4 files changed, 69 insertions(+), 1 deletions(-)
include/linux/sched.h +2 -1
index 25c5031..e09dafa 100644
@@ -1203,6 +1203,7 @@ struct task_struct {
 	int lock_depth;		/* BKL lock depth */
 
 #ifdef CONFIG_SMP
+	struct task_struct *wake_entry;
 	int on_cpu;
 #endif
 	int on_rq;
@@ -2192,7 +2193,7 @@ extern void set_task_comm(struct task_struct *tsk, char *from);
 extern char *get_task_comm(char *to, struct task_struct *tsk);
 
 #ifdef CONFIG_SMP
-static inline void scheduler_ipi(void) { }
+void scheduler_ipi(void);
 extern unsigned long wait_task_inactive(struct task_struct *, long match_state);
 #else
 static inline void scheduler_ipi(void) { }
init/Kconfig +5 -0
index 56240e7..32745bf 100644
@@ -827,6 +827,11 @@ config SCHED_AUTOGROUP
 	  desktop applications.  Task group autogeneration is currently based
 	  upon task session.
 
+config SCHED_TTWU_QUEUE
+	bool
+	depends on !SPARC32
+	default y
+
 config MM_OWNER
 	bool
 
kernel/sched.c +56 -0

78 lines not on this page · raw message

kernel/sched_features.h +6 -0
index 68e69ac..be40f73 100644
@@ -64,3 +64,9 @@ SCHED_FEAT(OWNER_SPIN, 1)
  * Decrement CPU power based on irq activity
  */
 SCHED_FEAT(NONIRQ_POWER, 1)
+
+/*
+ * Queue remote wakeups on the target CPU and process them
+ * using the scheduler IPI. Reduces rq->lock contention/bounces.
+ */
+SCHED_FEAT(TTWU_QUEUE, 1)
#147 lore raw

PATCH 21/21 sched: Remove need_migrate_task()

In reply to Peter Zijlstra

Oleg noticed that need_migrate_task() doesn't need the ->on_cpu check now that ttwu() doesn't do remote enqueues for !->on_rq && ->on_cpu, so remove the helper and replace the single instance with a direct ->on_rq test.

---
 kernel/sched.c |   17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
kernel/sched.c +1 -16
Index: linux-2.6/kernel/sched.c
===================================================================
@@ -2139,21 +2139,6 @@ struct migration_arg {
 static int migration_cpu_stop(void *data);
 
 /*
- * The task's runqueue lock must be held.
- * Returns true if you have to wait for migration thread.
- */
-static bool need_migrate_task(struct task_struct *p)
-{
-	/*
-	 * If the task is not on a runqueue (and not running), then
-	 * the next wake-up will properly place the task.
-	 */
-	bool running = p->on_rq || p->on_cpu;
-	smp_rmb(); /* finish_lock_switch() */
-	return running;
-}
-
-/*
  * wait_task_inactive - wait for a thread to unschedule.
  *
  * If @match_state is nonzero, it's the @p->state value just checked and
@@ -5734,7 +5719,7 @@ int set_cpus_allowed_ptr(struct task_str
 		goto out;
 
 	dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
-	if (need_migrate_task(p)) {
+	if (p->on_rq) {
 		struct migration_arg arg = { p, dest_cpu };
 		/* Need help from migration thread: drop lock and wait. */
 		task_rq_unlock(rq, p, &flags);
#148 lore raw

[tip:sched/locking] sched: Remove need_migrate_task()

Commit-ID:  bd8e7dded88a3e1c085c333f19ff31387616f71a
Gitweb:     http://git.kernel.org/tip/bd8e7dded88a3e1c085c333f19ff31387616f71a
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Tue, 5 Apr 2011 17:23:59 +0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 14 Apr 2011 08:52:41 +0200

sched: Remove need_migrate_task()

Oleg noticed that need_migrate_task() doesn't need the ->on_cpu check now that ttwu() doesn't do remote enqueues for !->on_rq && ->on_cpu, so remove the helper and replace the single instance with a direct ->on_rq test.

---
 kernel/sched.c |   17 +----------------
 1 files changed, 1 insertions(+), 16 deletions(-)
kernel/sched.c +1 -16
index 9e3ede1..cd597c7 100644
@@ -2199,21 +2199,6 @@ struct migration_arg {
 static int migration_cpu_stop(void *data);
 
 /*
- * The task's runqueue lock must be held.
- * Returns true if you have to wait for migration thread.
- */
-static bool need_migrate_task(struct task_struct *p)
-{
-	/*
-	 * If the task is not on a runqueue (and not running), then
-	 * the next wake-up will properly place the task.
-	 */
-	bool running = p->on_rq || p->on_cpu;
-	smp_rmb(); /* finish_lock_switch() */
-	return running;
-}
-
-/*
  * wait_task_inactive - wait for a thread to unschedule.
  *
  * If @match_state is nonzero, it's the @p->state value just checked and
@@ -5985,7 +5970,7 @@ int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
 		goto out;
 
 	dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
-	if (need_migrate_task(p)) {
+	if (p->on_rq) {
 		struct migration_arg arg = { p, dest_cpu };
 		/* Need help from migration thread: drop lock and wait. */
 		task_rq_unlock(rq, p, &flags);
#149 lore raw
In reply to Peter Zijlstra
Quoted 3 lines · Peter Zijlstra
unpatched: run time 30 seconds 647278 worker burns per second
patched:   run time 30 seconds 816715 worker burns per second 

Obviously bigger is better :-), the above means 26% more wakeups processed in the 30 seconds.

#150 lore raw
In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra

This patch series aims to optimize remote wakeups by moving most of the work of the wakeup to the remote cpu and avoid bouncing runqueue data structures where possible.

As measured by sembench (which basically creates a wakeup storm) on my dual-socket westmere:

Quoted 8 lines
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance > $i; done
$ echo 4096 32000 64 128 > /proc/sys/kernel/sem
$ ./sembench -t 2048 -w 1900 -o 0
unpatched: run time 30 seconds 647278 worker burns per second
patched:   run time 30 seconds 816715 worker burns per second

I've queued this series for .40.

Full diffstat per request

---
 arch/alpha/kernel/smp.c             |    3 +-
 arch/arm/kernel/smp.c               |    5 +-
 arch/blackfin/mach-common/smp.c     |    3 +
 arch/cris/arch-v32/kernel/smp.c     |   13 +-
 arch/ia64/kernel/irq_ia64.c         |    2 +
 arch/ia64/xen/irq_xen.c             |   10 +-
 arch/m32r/kernel/smp.c              |    4 +-
 arch/mips/cavium-octeon/smp.c       |    2 +
 arch/mips/kernel/smtc.c             |    2 +-
 arch/mips/mti-malta/malta-int.c     |    2 +
 arch/mips/pmc-sierra/yosemite/smp.c |    4 +
 arch/mips/sgi-ip27/ip27-irq.c       |    2 +
 arch/mips/sibyte/bcm1480/smp.c      |    7 +-
 arch/mips/sibyte/sb1250/smp.c       |    7 +-
 arch/mn10300/kernel/smp.c           |    5 +-
 arch/parisc/kernel/smp.c            |    5 +-
 arch/powerpc/kernel/smp.c           |    4 +-
 arch/s390/kernel/smp.c              |    6 +-
 arch/sh/kernel/smp.c                |    2 +
 arch/sparc/kernel/smp_32.c          |    4 +-
 arch/sparc/kernel/smp_64.c          |    1 +
 arch/tile/kernel/smp.c              |    6 +-
 arch/um/kernel/smp.c                |    2 +-
 arch/x86/kernel/smp.c               |    5 +-
 arch/x86/xen/smp.c                  |    5 +-
 include/linux/mutex.h               |    2 +-
 include/linux/sched.h               |   23 +-
 init/Kconfig                        |    5 +
 kernel/mutex-debug.c                |    2 +-
 kernel/mutex-debug.h                |    2 +-
 kernel/mutex.c                      |    2 +-
 kernel/mutex.h                      |    2 +-
 kernel/sched.c                      |  622 +++++++++++++++++++----------------
 kernel/sched_debug.c                |    2 +-
 kernel/sched_fair.c                 |   23 ++-
 kernel/sched_features.h             |    6 +
 kernel/sched_idletask.c             |    2 +-
 kernel/sched_rt.c                   |   54 ++--
 kernel/sched_stoptask.c             |    5 +-
 39 files changed, 483 insertions(+), 380 deletions(-)
#151 lore raw
In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra

This patch series aims to optimize remote wakeups by moving most of the work of the wakeup to the remote cpu and avoid bouncing runqueue data structures where possible.

As measured by sembench (which basically creates a wakeup storm) on my dual-socket westmere:

Quoted 8 lines
$ for i in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor ; do echo performance>  $i; done
$ echo 4096 32000 64 128>  /proc/sys/kernel/sem
$ ./sembench -t 2048 -w 1900 -o 0
unpatched: run time 30 seconds 647278 worker burns per second
patched:   run time 30 seconds 816715 worker burns per second

I've queued this series for .40.

Here are the results of running sembench on a 128 cpu box. In all of the below cases, I had to use the kernel parameter idle=mwait to eliminate spinlock contention in clockevents_notify() in the idle loop. I'll try to track down what can be done about that later.

I took Peter's patches from the tip/sched/locking tree. I got similar results directly from that branch, but separated them out to try to isolate some irregular behavior that mostly went away when I added idle=mwait. Since that branch was on top of 2.6.39-rc3, I used that as a base.

The other patchset in play is Chris Mason's semtimedop optimization patches. By themselves, I didn't see an improvement with Chris' patches, but in conjunction with Peter's, they gave the best results. When combining the patches, I removed Chris' batched wakeup patch, since it conflicted with Peter's patchset and really isn't needed any more.

(It's been a while since Chris posted these. They are in the "unbreakable" git tree,
http://oss.oracle.com/git/?p=linux-2.6-unbreakable.git;a=summary ,
and ported easily to mainline. I can repost them.)

I used Chris's latest sembench, http://oss.oracle.com/~mason/sembench.c
and the command "./sembench -t 2048 -w 1900 -o 0". I got similar burns-per-second numbers when cranking up the parameters to "./sembench -t 16384 -w 15000 -o 0".

2.6.38:

2048 threads, waking 1900 at a time
using ipc sem operations
main thread burns: 6549
worker burn count total 12443100 min 6068 max 6105 avg 6075 run time 30 seconds 414770 worker burns per second

2.6.39-rc3:

worker burn count total 11876900 min 5791 max 5805 avg 5799 run time 30 seconds 395896 worker burns per second

2.6.39-rc3 + mason's semtimedop patches:

worker burn count total 9988300 min 4868 max 4896 avg 4877 run time 30 seconds 332943 worker burns per second

2.6.39-rc3 + mason's patches (no batch wakeup patch):

worker burn count total 9743200 min 4750 max 4786 avg 4757 run time 30 seconds 324773 worker burns per second

2.6.39-rc3 + peterz's patches:

worker burn count total 14430500 min 7038 max 7060 avg 7046 run time 30 seconds 481016 worker burns per second

2.6.39-rc3 + mason's patches + peterz's patches:

worker burn count total 15072700 min 7348 max 7381 avg 7359 run time 30 seconds 502423 worker burns per second