From owner-freebsd-current@freebsd.org Fri Nov 6 01:16:31 2015 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id DC32AA2639B for ; Fri, 6 Nov 2015 01:16:30 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B0A5718AD; Fri, 6 Nov 2015 01:16:30 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (c-73-231-226-104.hsd1.ca.comcast.net [73.231.226.104]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id 02644B9BA; Thu, 5 Nov 2015 20:16:29 -0500 (EST) From: John Baldwin To: Ian Lepore Cc: Adrian Chadd , Mateusz Guzik , freebsd-current , Konstantin Belousov Subject: Re: [PATCH] microoptimize by trying to avoid locking a locked mutex Date: Thu, 05 Nov 2015 16:41:16 -0800 Message-ID: <3595970.jWQn08DLd8@ralph.baldwin.cx> User-Agent: KMail/4.14.3 (FreeBSD/10.2-STABLE; KDE/4.14.3; amd64; ; ) In-Reply-To: <1446766522.91534.412.camel@freebsd.org> References: <20151104233218.GA27709@dft-labs.eu> <1563180.x0Z3Ou4xid@ralph.baldwin.cx> <1446766522.91534.412.camel@freebsd.org> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Thu, 05 Nov 2015 20:16:29 -0500 (EST) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Nov 2015 01:16:31 -0000 On Thursday, November 05, 2015 04:35:22 PM Ian Lepore wrote: > On Thu, 2015-11-05 at 14:19 -0800, John Baldwin wrote: > > On Thursday, November 05, 2015 01:45:19 PM Adrian Chadd wrote: > > > On 5 November 2015 at 11:26, Mateusz Guzik > > > wrote: > > > > On Thu, Nov 05, 2015 at 11:04:13AM -0800, John Baldwin wrote: > > > > > On Thursday, November 05, 2015 04:26:28 PM Konstantin Belousov > > > > > wrote: > > > > > > On Thu, Nov 05, 2015 at 12:32:18AM +0100, Mateusz Guzik > > > > > > wrote: > > > > > > > mtx_lock will unconditionally try to grab the lock and if > > > > > > > that fails, > > > > > > > will call __mtx_lock_sleep which will immediately try to do > > > > > > > the same > > > > > > > atomic op again. > > > > > > > > > > > > > > So, the obvious microoptimization is to check the state in > > > > > > > __mtx_lock_sleep and avoid the operation if the lock is not > > > > > > > free. > > > > > > > > > > > > > > This gives me ~40% speedup in a microbenchmark of 40 find > > > > > > > processes > > > > > > > traversing tmpfs and contending on mount mtx (only used as > > > > > > > an easy > > > > > > > benchmark, I have WIP patches to get rid of it). > > > > > > > > > > > > > > Second part of the patch is optional and just checks the > > > > > > > state of the > > > > > > > lock prior to doing any atomic operations, but it gives a > > > > > > > very modest > > > > > > > speed up when applied on top of the __mtx_lock_sleep > > > > > > > change. As such, > > > > > > > I'm not going to defend this part. > > > > > > Shouldn't the same consideration applied to all spinning > > > > > > loops, i.e. > > > > > > also to the spin/thread mutexes, and to the spinning parts of > > > > > > sx and > > > > > > lockmgr ? > > > > > > > > > > I agree. I think both changes are good and worth doing in our > > > > > other > > > > > primitives. > > > > > > > > > > > > > I glanced over e.g. rw_rlock and it did not have the issue, now > > > > that I > > > > see _sx_xlock_hard it wuld indeed use fixing. > > > > > > > > Expect a patch in few h for all primitives I'll find. I'll stress > > > > test > > > > the kernel, but it is unlikely I'll do microbenchmarks for > > > > remaining > > > > primitives. > > > > > > Is this stuff you're proposing still valid for non-x86 platforms? > > > > Yes. It just does a read before trying the atomic compare and swap > > and > > falls through to the hard case as if the atomic op failed if the > > result > > of the read would result in a compare failure. > > > > The atomic ops include barriers, the new pre-read of the variable > doesn't. Will that cause problems, especially for code inside a loop > where the compiler may decide to shuffle things around? I do not believe so. Eventually you have to go through a barrier to break out of the loop. > I suspect the performance gain will be biggest on the platforms where > atomic ops are expensive (I gather from various code comments that's > the case on x86). Yes, and where you have contention. :-/ -- John Baldwin