linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Waiman Long <waiman.long@hp.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, x86@kernel.org,
	linux-kernel@vger.kernel.org,
	Steven Rostedt <rostedt@goodmis.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	Michel Lespinasse <walken@google.com>,
	Andi Kleen <andi@firstfloor.org>, Rik van Riel <riel@redhat.com>,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Raghavendra K T <raghavendra.kt@linux.vnet.ibm.com>,
	George Spelvin <linux@horizon.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	Daniel J Blueman <daniel@numascale.com>,
	Alexander Fyodorov <halcy@yandex.ru>,
	Aswin Chandramouleeswaran <aswin@hp.com>,
	Scott J Norton <scott.norton@hp.com>,
	Thavatchai Makphaibulchoke <thavatchai.makpahibulchoke@hp.com>
Subject: Re: [PATCH v3 1/2] qspinlock: Introducing a 4-byte queue spinlock implementation
Date: Fri, 31 Jan 2014 14:24:33 -0500	[thread overview]
Message-ID: <52EBF871.5020603@hp.com> (raw)
In-Reply-To: <20140131150832.GG4941@twins.programming.kicks-ass.net>

On 01/31/2014 10:08 AM, Peter Zijlstra wrote:
> On Tue, Jan 28, 2014 at 01:19:10PM -0500, Waiman Long wrote:
>> For single-thread performance (no contention), a 256K lock/unlock
>> loop was run on a 2.4Ghz Westmere x86-64 CPU.  The following table
>> shows the average time (in ns) for a single lock/unlock sequence
>> (including the looping and timing overhead):
>>
>>    Lock Type			Time (ns)
>>    ---------			---------
>>    Ticket spinlock		  14.1
>>    Queue spinlock (Normal)	   8.8*
> What CONFIG_NR_CPUS ?

I was testing on a RHEL6.4 system which has a CONFIG_NR_CPUS of 4096.

>
> Because for CONFIG_NR_CPUS<  128 (or 256 if you got !PARAVIRT), the fast
> path code should be:
>
> ticket:
>
>    mov $0x100,eax
>    lock xadd %ax,(%rbx)
>    cmp %al,%ah
>    jne ...
>
> although my GCC is being silly and writes:
>
>    mov $0x100,eax
>    lock xadd %ax,(%rbx)
>    movzbl %ah,%edx
>    cmp %al,%dl
>    jne ...
>
> Which seems rather like a waste of a perfectly good cycle.
>
> With a bigger NR_CPUS you do indeed need more ops:
>
>    mov $0x10000,%edx
>    lock xadd %edx,(%rbx)
>    mov %edx,%ecx
>    shr $0x10,%ecx
>    cmp %dx,%cx
>    jne ...
>
>
> Whereas for the straight cmpxchg() you'd get something relatively simple
> like:
>
>    mov %edx,%eax
>    lock cmpxchg %ecx,(%rbx)
>    cmp %edx,%eax
>    jne ...

I believe the speeds of the lock functions are about the same. However, 
qspinlock has a much simpler unlock function which probably account of 
most of the speed gain.

> Anyway, as soon as you get some (light) contention you're going to tank
> because you have to pull in extra cachelines, which is sad.

Light contention is the only case where the qspinlock may not perform as 
good as the ticket spinlock. I know this is the most common case. 
However, I would argue that the slowdown, if any, will not be really 
noticeable. This is what I will try to find out.


> I suppose we could from the ticket code more and optimize the
> uncontended path, but that'll make the contended path more expensive
> again, although probably not as bad as hitting a new cacheline.

I don't get what you are trying to say.

Right now, I am using only bit 0 as a lock bit. I can use  bit 4, for 
instance, as a pending locker bit and spin until bit 0 is clear. So if 
there is only 1 other task spinning, it won't need to  fetch another 
cacheline. However, it will slow down the uncontended path as I can't 
assign a 0 byte to free the lock. I have to use an atomic subtraction or 
clear bit instead.

-Longman

  reply	other threads:[~2014-01-31 19:24 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-28 18:19 [PATCH v3 0/2] qspinlock: Introducing a 4-byte queue spinlock Waiman Long
2014-01-28 18:19 ` [PATCH v3 1/2] qspinlock: Introducing a 4-byte queue spinlock implementation Waiman Long
2014-01-28 18:19   ` Waiman Long
2014-01-29  0:20   ` Andi Kleen
2014-01-29  0:20     ` Andi Kleen
2014-01-29  2:57     ` George Spelvin
2014-01-29 17:57     ` Waiman Long
2014-01-29 17:57       ` Waiman Long
2014-01-30 17:43   ` Rik van Riel
2014-01-30 19:00   ` Tim Chen
2014-01-30 19:28     ` Peter Zijlstra
2014-01-30 22:27       ` Tim Chen
2014-01-31 18:26       ` Waiman Long
2014-01-31 18:26         ` Waiman Long
2014-01-31 19:14         ` George Spelvin
2014-01-31 19:28           ` Waiman Long
2014-01-31 19:45         ` Peter Zijlstra
2014-01-31 18:16     ` Waiman Long
2014-01-30 19:35   ` Peter Zijlstra
2014-01-31 18:28     ` Waiman Long
2014-01-31 15:08   ` Peter Zijlstra
2014-01-31 19:24     ` Waiman Long [this message]
2014-01-31 19:24       ` Waiman Long
2014-01-31 19:51       ` Peter Zijlstra
2014-02-03 11:40       ` Peter Zijlstra
2014-02-06  3:10         ` Waiman Long
2014-02-07 18:17           ` Paul E. McKenney
2014-02-03  8:51   ` Raghavendra K T
2014-01-28 18:19 ` [PATCH v3 2/2] qspinlock, x86: Enable x86-64 to use queue spinlock Waiman Long
2014-01-30 17:45   ` Rik van Riel
2014-01-30  8:55 ` [PATCH v3 0/2] qspinlock: Introducing a 4-byte " Raghavendra K T
2014-01-30 15:38   ` Waiman Long
2014-01-30 15:38     ` Waiman Long
2014-01-30 18:49     ` Raghavendra K T
2014-02-03  8:51       ` Raghavendra K T
2014-02-03  8:51         ` Raghavendra K T
2014-02-06  3:09         ` Waiman Long

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52EBF871.5020603@hp.com \
    --to=waiman.long@hp.com \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=arnd@arndb.de \
    --cc=aswin@hp.com \
    --cc=daniel@numascale.com \
    --cc=halcy@yandex.ru \
    --cc=hpa@zytor.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@horizon.com \
    --cc=mingo@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=raghavendra.kt@linux.vnet.ibm.com \
    --cc=riel@redhat.com \
    --cc=rostedt@goodmis.org \
    --cc=scott.norton@hp.com \
    --cc=tglx@linutronix.de \
    --cc=thavatchai.makpahibulchoke@hp.com \
    --cc=tim.c.chen@linux.intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=walken@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).