From owner-freebsd-current@freebsd.org Thu Nov 5 19:26:28 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 3629BA26D5C for ; Thu, 5 Nov 2015 19:26:28 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wm0-x232.google.com (mail-wm0-x232.google.com [IPv6:2a00:1450:400c:c09::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C089B1FBB; Thu, 5 Nov 2015 19:26:27 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by wmeg8 with SMTP id g8so23428505wme.0; Thu, 05 Nov 2015 11:26:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=vR9CV+HNZwADRQslsDiIcchOm7kwx+gEEtH24k0WzAo=; b=WWye/7vTX3vBRcB8SpMQlrizDCy+q+kSADbJs/ATztVqWK8mYILey3Gi4L/tE26OYK vOaNBNKAikmpZiqlMChkxZTVVnYPA1UK+MpE+26QkKmpXBtSYYt5n0O0p/xa4maw5nnX GsZ/uor6ezSC2gcSQauMnevljlN+ihofVK+i+sjGK/mAAkNmf7QDxIRTh2INX083IJmn 20jY98VmGIsTSHjLs4XQEJqvI+VJdFUcU4C7rB7yLw3i6EmrkOwkQa+1+G4ibvkee6ds dgk+e0bXpekFRJh5mGx9/mGbfv5d7GitMiBi+81qGNIOB0enV995GQK5cSy5/SE7fUXf hMJA== X-Received: by 10.28.23.211 with SMTP id 202mr5515844wmx.81.1446751586341; Thu, 05 Nov 2015 11:26:26 -0800 (PST) Received: from dft-labs.eu (n1x0n-1-pt.tunnel.tserv5.lon1.ipv6.he.net. [2001:470:1f08:1f7::2]) by smtp.gmail.com with ESMTPSA id l81sm9928810wmb.2.2015.11.05.11.26.25 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 05 Nov 2015 11:26:25 -0800 (PST) Date: Thu, 5 Nov 2015 20:26:23 +0100 From: Mateusz Guzik To: John Baldwin Cc: freebsd-current@freebsd.org, Konstantin Belousov Subject: Re: [PATCH] microoptimize by trying to avoid locking a locked mutex Message-ID: <20151105192623.GB27709@dft-labs.eu> Mail-Followup-To: Mateusz Guzik , John Baldwin , freebsd-current@freebsd.org, Konstantin Belousov References: <20151104233218.GA27709@dft-labs.eu> <20151105142628.GJ2257@kib.kiev.ua> <13871467.CBcqGMncpJ@ralph.baldwin.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <13871467.CBcqGMncpJ@ralph.baldwin.cx> User-Agent: Mutt/1.5.21 (2010-09-15) 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: Thu, 05 Nov 2015 19:26:28 -0000 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. -- Mateusz Guzik