New issue
Advanced search Search tips

Issue 767929 link

Starred by 3 users

Issue metadata

Status: Available
Owner: ----
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Mac
Pri: 3
Type: Bug



Sign in to add a comment

Consider switching to _PTHREAD_MUTEX_POLICY_FIRSTFIT on OSX for faster locks.

Project Member Reported by dalecur...@chromium.org, Sep 22 2017

Issue description

Provides a significant speedup to lock performance: 0m3.448s vs 0m47.917s for the 8 thread counting test below.

Unfortunately seems to fail for a few things currently, https://codereview.chromium.org/2887263002 and notes below suggest it may not work for condition variables. ISTR many of the failures being in places the Fuschia team has spent some time deflaking recently, so some may have been real issues. Will resubmit that CL and post test failures here.

Original internal discussion:

Simple test which uses 8 threads to increment a counter 1000000 times (clang -O2 -lpthread)

Linux (Z620): $ time thread-test
Sum 8000000

real	0m1.012s
user	0m1.404s
sys	0m6.004s

macOS (2012 Pro): $ time thread-test
Sum 8000000

real    0m47.917s
user    0m3.477s
sys     0m46.397s

I'm wondering if the issue is even further exacerbated on Mac Pro devices with higher core counts...

I know Apple wrote this some time ago:

https://webkit.org/blog/6161/locking-in-webkit/

I seem to recall private discussions centering around us being unable to reproduce this; though may be remembering incorrectly. I'm wondering if they wrote that since they all work on Mac Pros...

Also to offset any concerns that the number of cores in the Z620 help, the timing using taskset for limiting to four cores is faster on Linux:

$ time taskset 0x11 ./thread-test
Sum 8000000

real	0m0.803s
user	0m0.776s
sys	0m0.796s


Someone pointed out:

https://blog.mozilla.org/nfroyd/2017/03/29/on-mutex-performance-part-1/

Which suggests setting _PTHREAD_MUTEX_POLICY_FIRSTFIT, this does indeed speed the code up significantly:

$ time ./thread-test 
Sum 8000000

real	0m3.448s
user	0m1.204s
sys	0m25.009s

https://bugzilla.mozilla.org/show_bug.cgi?id=1353787

Suggests that this can't be used with ConditionVariables in a reliable manner.
 
test.cc
645 bytes View Download
https://chromium-review.googlesource.com/c/chromium/src/+/1170164 is a "modern" retry. Will post failure messages here.
> Simple test which uses 8 threads to increment a counter 1000000 times (clang -O2 -lpthread)

But we don't want to use that benchmark to choose lock policies, given issues like  crbug.com/870054  - gmail hangs (ranging from 2-10 s) caused by Windows using unfair locks. Anytime we have a lock which is held most of the time and is being contended for we probably have a design flaw. The lock on the process heap is probably the closest thing to this and usually the threads have other work to do as well.

If testing with unfair locks finds some race conditions, however, then that would be very useful.

Components: Internals>TaskScheduler
Agreed, it's definitely a toy example that we shouldn't base a final decision on.

Ideally we'd land the macOS change as a Finch experiment and see what happens to metrics like hangs, crashiness, performance stats, etc. However my last attempt couldn't make it past the CQ, we'll see how c#1 does and can go from there.

+TaskScheduler folks since they might find this interesting.
Looks like mozilla went with some sort of adaptive approach:

https://hg.mozilla.org/integration/mozilla-inbound/rev/ef231f04c4ec
https://chromium-review.googlesource.com/c/chromium/src/+/1170184 is a CQ try for PTHREAD_MUTEX_ADAPTIVE_NP

Will send it to the perf bots if it passes CQ.
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/16c9f0a0640000
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/17e08cf8640000
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/138bd530640000
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/148da7f8640000
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/14e0a780640000
📍 Job complete. See results below.
https://pinpoint-dot-chromeperf.appspot.com/job/16c8b19c640000

Sign in to add a comment