From owner-freebsd-smp@FreeBSD.ORG Mon Sep 24 16:29:34 2007 Return-Path: Delivered-To: freebsd-smp@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5F36E16A419 for ; Mon, 24 Sep 2007 16:29:34 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from speedfactory.net (mail6.speedfactory.net [66.23.216.219]) by mx1.freebsd.org (Postfix) with ESMTP id 4338A13C448 for ; Mon, 24 Sep 2007 16:29:34 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (unverified [66.23.211.162]) by speedfactory.net (SurgeMail 3.8p) with ESMTP id 211141650-1834499 for multiple; Mon, 24 Sep 2007 12:11:55 -0400 Received: from localhost.corp.yahoo.com (john@localhost [127.0.0.1]) (authenticated bits=0) by server.baldwin.cx (8.13.8/8.13.8) with ESMTP id l8OGD3hJ098542; Mon, 24 Sep 2007 12:13:07 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: "Attilio Rao" Date: Mon, 24 Sep 2007 11:52:41 -0400 User-Agent: KMail/1.9.6 References: <3bbf2fe10709221932i386f65b9h6f47ab4bee08c528@mail.gmail.com> In-Reply-To: <3bbf2fe10709221932i386f65b9h6f47ab4bee08c528@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200709241152.41660.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [127.0.0.1]); Mon, 24 Sep 2007 12:13:07 -0400 (EDT) X-Virus-Scanned: ClamAV 0.88.3/4378/Mon Sep 24 08:25:35 2007 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=4.2 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: freebsd-smp@freebsd.org, freebsd-arch@freebsd.org Subject: Re: rwlocks: poor performance with adaptive spinning X-BeenThere: freebsd-smp@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: FreeBSD SMP implementation group List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 24 Sep 2007 16:29:34 -0000 On Saturday 22 September 2007 10:32:06 pm Attilio Rao wrote: > Recently several people have reported problems of starvation with rwlocks. > In particular, users which tried to use rwlock on big SMP environment > (16+ CPUs) found them rather subjected to poor performances and to > starvation of waiters. > > Inspecting the code, something strange about adaptive spinning popped > up: basically, for rwlocks, adaptive spinning stubs seem to be > customed too down in the decisioning-loop. > The desposition of the stub will let the thread that would adaptively > spin, to set the respecitve (both read or write) waiters flag on, > which means that the owner of the lock will go down in the hard path > of locking functions and will performe a full wakeup even if the > waiters queues can result empty. This is a big penalty for adaptive > spinning which can make it completely useless. > In addiction to this, adaptive spinning only runs in the turnstile > spinlock path which is not ideal. > This patch ports the approach alredy used for adaptive spinning in sx > locks to rwlocks: > http://users.gufi.org/~rookie/works/patches/kern_rwlock.diff > > In sx it is unlikely to see big benefits because they are held for too > long times, but for rwlocks situation is rather different. > I would like to see if people can do benchmarks with this patch (maybe > in private environments?) as I'm not able to do them in short times. > > Adaptive spinning in rwlocks can be improved further with other tricks > (like adding a backoff counter, for example, or trying to spin with > the lock held in read mode too), but we first should be sure to start > with a solid base. I did this for mutexes and rwlocks over a year ago and Kris found it was slower in benchmarks. www.freebsd.org/~jhb/patches/lock_adapt.patch is the last thing I sent kris@ to test (it only has the mutex changes). This might be more optimal post-thread_lock since thread_lock seems to have heavily pessimized adaptive spinning because it now enqueues the thread and then dequeues it again before doing the adaptive spin. I liked the approach orginially because it simplifies the code a lot. A separate issue is that writers don't spin at all if a reader holds the lock, and I think one thing to test for that would be an adaptive spin with a static timeout. -- John Baldwin