From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8244 Path: news.gmane.org!not-for-mail From: Alexander Monakov Newsgroups: gmane.linux.lib.musl.general Subject: Re: New optimized normal-type mutex? Date: Thu, 30 Jul 2015 12:36:19 +0300 (MSK) Message-ID: References: <20150521234402.GA25373@brightrain.aerifal.cx> <8c49d81e.dNq.dMV.21.hNiSfA@mailjet.com> <1438207875.10742.3.camel@inria.fr> <20150729233054.GZ16376@brightrain.aerifal.cx> <1438213760.10742.5.camel@inria.fr> <20150730001014.GA16376@brightrain.aerifal.cx> <1438243654.10742.9.camel@inria.fr> <1438247427.10742.13.camel@inria.fr> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Trace: ger.gmane.org 1438249009 31606 80.91.229.3 (30 Jul 2015 09:36:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 30 Jul 2015 09:36:49 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8257-gllmg-musl=m.gmane.org@lists.openwall.com Thu Jul 30 11:36:34 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1ZKkGO-0001xF-Uf for gllmg-musl@m.gmane.org; Thu, 30 Jul 2015 11:36:33 +0200 Original-Received: (qmail 32483 invoked by uid 550); 30 Jul 2015 09:36:31 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 32463 invoked from network); 30 Jul 2015 09:36:30 -0000 In-Reply-To: <1438247427.10742.13.camel@inria.fr> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) Xref: news.gmane.org gmane.linux.lib.musl.general:8244 Archived-At: On Thu, 30 Jul 2015, Jens Gustedt wrote: > Am Donnerstag, den 30.07.2015, 10:07 +0200 schrieb Jens Gustedt: > > Am Mittwoch, den 29.07.2015, 20:10 -0400 schrieb Rich Felker: > > > On Thu, Jul 30, 2015 at 01:49:20AM +0200, Jens Gustedt wrote: > > > > Hm, could you be more specific about where this hurts? > > > > > > > > In the code I have there is > > > > > > > > for (;val & lockbit;) { > > > > __syscall(SYS_futex, loc, FUTEX_WAIT, val, 0); > > > > val = atomic_load_explicit(loc, memory_order_consume); > > > > } > > > > > > > > so this should be robust against spurious wakeups, no? > > > > > > The problem is that futex_wait returns immediately with EAGAIN if > > > *loc!=val, which happens very often if *loc is incremented or > > > otherwise changed on each arriving waiter. > > > > Yes, sure, it may change. Whether or not this is often may depend, I > > don't think we can easily make a quantitative statement, here. > > > > In the case of atomics the critical section is extremely short, and > > the count, if it varies so much, should have a bit stabilized during > > the spinlock phase before coming to the futex part. That futex part is > > really only a last resort for the rare case that the thread that is > > holding the lock has been descheduled in the middle. > > > > My current test case is having X threads hammer on one single > > location, X being up to some hundred. On my 2x2 hyperthreaded CPU for > > a reasonable number of threads (X = 16 or 32) I have an overall > > performance improvement of 30%, say, when using my version of the lock > > instead of the original musl one. The point of inversion where the > > original musl lock is better is at about 200 threads. > > > > I'll see how I can get hold on occurrence statistics of the different > > phases without being too intrusive (which would change the > > scheduling). > > So I tested briefly varying the number of threads from 2 up to 2048. > > Out of the loop iterations on the slow path, less than 0.1 % try to go > into futex wait, and out of these about 20 % come back with EGAIN. That sounds like your testcase simulates a load where you'd be better off with a spinlock in the first place, no? Have you tried simulating a load that does some non-trivial work between lock/unlock, making a spinlock a poor fit? Alexander