Re: [PATCH 1/1] mutex: prevent optimistic spinning from spinning longer than neccessary (Repost)

Previous thread: [Regression, 2.6.36-rc1] ath9k resume problem on Acer Ferrari One by Rafael J. Wysocki on Wednesday, August 18, 2010 - 3:01 pm. (11 messages)

Next thread: [PATCH] Documentation: kernel-locking: mutex_trylock cannot be used in interrupt context by Stefan Richter on Wednesday, August 18, 2010 - 3:08 pm. (2 messages)
From: Tim Chen
Date: Wednesday, August 18, 2010 - 3:00 pm

I didn't get any feedback on this post sent a while back.  So I'm
reposting it to see if I can get some comments back this time.

There is a scalability issue for current implementation of optimistic
mutex spin in the kernel.  It is found on a 8 node 64 core Nehalem-EX
system (HT mode).

The intention of the optimistic mutex spin is to busy wait and spin on a 
mutex if the owner of the mutex is running, in the hope that the mutex 
will be released soon and be acquired, without the thread
trying to acquire mutex going to sleep.  However, 
when we have a large number of threads, contending for the mutex, we could 
have the mutex grabbed by other thread, and then another ……, and we will keep 
spinning, wasting cpu cycles and adding to the contention.  One
possible fix is to quit spinning and put the current thread on wait-list
if mutex lock switch to a new owner while we spin, indicating heavy
contention (see the patch included).   

I did some testing on a 8 socket Nehalem-EX system with a total of 64
cores. Using Ingo's test-mutex program that creates/delete files with
256 threads (http://lkml.org/lkml/2006/1/8/50) , I see the following
speed up after putting in the mutex spin fix:

./mutex-test V 256 10
                Ops/sec
2.6.34          62864
With fix        197200

Repeating the test with Aim7 fserver workload, again there is a speed up
with the fix:

                Jobs/min
2.6.34          91657
With fix        149325

To look at the impact on the distribution of mutex acquisition time, I
collected the mutex acquisition time on Aim7 fserver workload with some
instrumentation.  The average acquisition time is reduced by 48% and
number of contentions reduced by 32%.

                #contentions    Time to acquire mutex (cycles)
2.6.34          72973           44765791
With fix        49210           23067129 

The histogram of mutex acquisition time is listed below.  The
acquisition time is in 2^bin cycles.  We see that without the fix, the
acquisition ...
From: Ingo Molnar
Date: Thursday, August 19, 2010 - 4:05 am

These are some rather impressive speedups!

Have you tried to see what performance effects this change has on smaller 
boxes? Just to see what flip side (if any) this change has.

Thanks,

	Ingo
--

From: Tim Chen
Date: Thursday, August 19, 2010 - 3:24 pm

I've done similar experiments with 2.6.35 kernel on smaller boxes.  One is
on a dual-socket Westmere box (12 cores total, with HT). Another
experiment is on an old dual-socket Core 2 box (4 cores total, no HT)

On the 12-core Westmere box, I see a 250% increase for Ingo's mutex-test
program with my mutex patch but no significant difference in aim7's
fserver workload.

On the 4-core Core 2 box, I see the difference with the patch for both
mutex-test and aim7 fserver are negligible.

So far, it seems like the patch has not caused regression on smaller
systems.  We'll put it through more workloads to check.

Tim









--

From: Ingo Molnar
Date: Friday, August 20, 2010 - 6:19 am

Thanks! The performance results you've posted so far IMO more than justifies 
its inclusion.

	Ingo
--

From: Tim Chen
Date: Friday, August 20, 2010 - 9:54 am

Thanks to everyone for your review.  I've updated the patch to add
comments explaining the change per suggestion from Andrew.

Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
diff --git a/kernel/sched.c b/kernel/sched.c
index 41541d7..3747d04 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -3865,8 +3865,15 @@ int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
 		/*
 		 * Owner changed, break to re-assess state.
 		 */
-		if (lock->owner != owner)
+		if (lock->owner != owner) {
+			/* if lock has switched to a different owner,
+			 * we have heavy contention. Return 0 to
+			 * quit optimistic spinning and not contend further.
+			 */
+			if (lock->owner)
+				return 0;
 			break;
+		}
 
 		/*
 		 * Is that owner really running on that cpu?


--

Previous thread: [Regression, 2.6.36-rc1] ath9k resume problem on Acer Ferrari One by Rafael J. Wysocki on Wednesday, August 18, 2010 - 3:01 pm. (11 messages)

Next thread: [PATCH] Documentation: kernel-locking: mutex_trylock cannot be used in interrupt context by Stefan Richter on Wednesday, August 18, 2010 - 3:08 pm. (2 messages)