All of lore.kernel.org
 help / color / mirror / Atom feed
From: Waiman Long <waiman.long@hp.com>
To: paulmck@linux.vnet.ibm.com
Cc: linux-kernel@vger.kernel.org, mingo@elte.hu,
	laijs@cn.fujitsu.com, dipankar@in.ibm.com,
	akpm@linux-foundation.org, mathieu.desnoyers@efficios.com,
	josh@joshtriplett.org, niv@us.ibm.com, tglx@linutronix.de,
	peterz@infradead.org, rostedt@goodmis.org,
	Valdis.Kletnieks@vt.edu, dhowells@redhat.com,
	edumazet@google.com, darren@dvhart.com, fweisbec@gmail.com,
	sbw@mit.edu, torvalds@linux-foundation.org,
	Davidlohr Bueso <davidlohr.bueso@hp.com>
Subject: Re: [PATCH RFC ticketlock] Auto-queued ticketlock
Date: Tue, 11 Jun 2013 14:41:59 -0400	[thread overview]
Message-ID: <51B76F77.6020004@hp.com> (raw)
In-Reply-To: <20130611163607.GG5146@linux.vnet.ibm.com>

On 06/11/2013 12:36 PM, Paul E. McKenney wrote:
>
>> I am a bit concern about the size of the head queue table itself.
>> RHEL6, for example, had defined CONFIG_NR_CPUS to be 4096 which mean
>> a table size of 256. Maybe it is better to dynamically allocate the
>> table at init time depending on the actual number of CPUs in the
>> system.
> But if your kernel is built for 4096 CPUs, the 32*256=8192 bytes of memory
> is way down in the noise.  Systems that care about that small an amount
> of memory probably have a small enough number of CPUs that they can just
> turn off queueing at build time using CONFIG_TICKET_LOCK_QUEUED=n, right?

My concern is more about the latency on the table scan than the actual 
memory that was used.

>
>>> +/*
>>> + * Return a pointer to the queue header associated with the specified lock,
>>> + * or return NULL if there is no queue for the lock or if the lock's queue
>>> + * is in transition.
>>> + */
>>> +static struct tkt_q_head *tkt_q_find_head(arch_spinlock_t *asp)
>>> +{
>>> +	int i;
>>> +	int start;
>>> +
>>> +	start = i = tkt_q_hash(asp);
>>> +	do
>>> +		if (tkt_q_heads[i].ref == asp)
>>> +			return&tkt_q_heads[i];
>>> +	while ((i = tkt_q_next_slot(i)) != start);
>>> +	return NULL;
>>> +}
>> With a table size of 256 and you have to scan the whole table to
>> find the right head queue. This can be a significant overhead. I
>> will suggest setting a limiting of how many entries it scans before
>> it aborts rather than checking the whole table.
> But it will scan 256 entries only if there are 256 other locks in queued
> mode, which is -very- unlikely, even given 4096 CPUs.  That said, if you
> show me that this results in a real latency problem on a real system,
> I would be happy to provide a way to limit the search.

Looking at the code more carefully, the chance of actually scanning 256 
entries is very small. However, I now have some concern on the way you 
set up the initial queue.

+/*
+ * Start handling a period of high contention by finding a queue to associate
+ * with this lock.  Returns true if successful (in which case we hold the
+ * lock) and false otherwise (in which case we do -not- hold the lock).
+ */
+bool tkt_q_start_contend(arch_spinlock_t *asp, struct __raw_tickets inc)
+{
+	int i;
+	int start;
+
+	/* Hash the lock address to find a starting point. */
+	start = i = tkt_q_hash(asp);
+
+	/*
+	 * Each pass through the following loop attempts to associate
+	 * the lock with the corresponding queue.
+	 */
+	do {
+		/*
+		 * Use 0x1 to mark the queue in use, but also avoiding
+		 * any spinners trying to use it before we get it all
+		 * initialized.
+		 */
+		if (cmpxchg(&tkt_q_heads[i].ref,
+			    NULL,
+			    (arch_spinlock_t *)0x1) == NULL) {
+
+			/* Succeeded, now go initialize it. */
+			return tkt_q_init_contend(i, asp, inc);
+		}
+
+		/* If someone beat us to it, go spin on their queue. */
+		if (ACCESS_ONCE(asp->tickets.head)&  0x1)
+			return tkt_q_do_spin(asp, inc);
+	} while ((i = tkt_q_next_slot(i)) != start);
+
+	/* All the queues are in use, revert to spinning on the ticket lock. */
+	return false;
+}
+

Unconditional cmpxchg() can be a source of high contention by itself. 
Considering that 16 threads may be doing cmpxchg() more or less 
simultaneously on the same cache line, it can cause a lot of contention. 
It will be better if you check to see if tkt_q_heads[i] is NULL first 
before doing cmpxchg.

Another point is that the 16 threads maybe setting up the queues in 
consecutive slots in the head table. This is both a source of contention 
and a waste of effort. One possible solution is to add one more field 
(set to cpuid + 1, for example) to indicate that that setup is being 
done with asp set to the target lock address immediately. We will need 
to use cmpxchg128() for 64-bit machine, though. Another solution is to 
have only that thread with ticket number that is a fixed distance from 
head (e.g. 16*2) to do the queue setup while the rest wait until the 
setup is done before spinning on the queue.

As my colleague Davidlohr had reported there are more regressions than 
performance improvement in the AIM7 benchmark. I believe that queue 
setup contention is likely a source of performance regression.

Regards,
Longman

  parent reply	other threads:[~2013-06-11 18:42 UTC|newest]

Thread overview: 96+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-09 19:36 [PATCH RFC ticketlock] Auto-queued ticketlock Paul E. McKenney
2013-06-10 20:47 ` Steven Rostedt
2013-06-10 20:57   ` Paul E. McKenney
2013-06-10 21:01   ` Thomas Gleixner
2013-06-10 21:15     ` Paul E. McKenney
2013-06-10 21:08 ` Steven Rostedt
2013-06-10 21:30   ` Paul E. McKenney
2013-06-10 21:35 ` Eric Dumazet
2013-06-10 21:54   ` Paul E. McKenney
2013-06-10 23:02 ` Steven Rostedt
2013-06-11  0:22   ` Paul E. McKenney
2013-06-11  0:44 ` Steven Rostedt
2013-06-11  0:51   ` Linus Torvalds
2013-06-11  7:53     ` Lai Jiangshan
2013-06-11 10:14       ` Paul E. McKenney
2013-06-11 15:22         ` Steven Rostedt
2013-06-11 16:45           ` Paul E. McKenney
2013-06-11 10:06     ` Paul E. McKenney
2013-06-11 17:53     ` Davidlohr Bueso
2013-06-11 18:05       ` Paul E. McKenney
2013-06-11 18:10       ` Steven Rostedt
2013-06-11 18:14         ` Davidlohr Bueso
2013-06-11 18:46         ` Paul E. McKenney
2013-06-12 17:50         ` Davidlohr Bueso
2013-06-12 18:15           ` Linus Torvalds
2013-06-12 20:03             ` Davidlohr Bueso
2013-06-12 20:26               ` Linus Torvalds
2013-06-12 20:40                 ` Davidlohr Bueso
2013-06-12 21:06                 ` Raymond Jennings
2013-06-12 23:32                 ` Al Viro
2013-06-13  0:01                   ` Linus Torvalds
2013-06-13  0:20                     ` Al Viro
2013-06-13  0:38                       ` Linus Torvalds
2013-06-13  0:49                         ` Al Viro
2013-06-13  0:59                           ` Linus Torvalds
2013-06-14 15:00                             ` Waiman Long
2013-06-14 15:37                               ` Linus Torvalds
2013-06-14 18:17                                 ` Waiman Long
2013-06-15  1:26                                   ` Benjamin Herrenschmidt
2013-06-15  3:36                                     ` Waiman Long
2013-06-12 20:37               ` Linus Torvalds
2013-06-12 18:18           ` Steven Rostedt
2013-06-11  9:56   ` Paul E. McKenney
2013-06-11 15:00     ` Paul E. McKenney
2013-06-11  1:04 ` Steven Rostedt
2013-06-11  9:52   ` Paul E. McKenney
2013-06-11 14:48 ` Lai Jiangshan
2013-06-11 15:10   ` Lai Jiangshan
2013-06-11 16:48     ` Paul E. McKenney
2013-06-11 17:17       ` Linus Torvalds
2013-06-11 17:30         ` Paul E. McKenney
2013-06-11 16:21   ` Paul E. McKenney
2013-06-11 15:57 ` Waiman Long
2013-06-11 16:20   ` Steven Rostedt
2013-06-11 16:43     ` Paul E. McKenney
2013-06-11 17:13       ` Steven Rostedt
2013-06-11 17:43         ` Paul E. McKenney
2013-06-11 17:35     ` Waiman Long
2013-06-11 16:36   ` Paul E. McKenney
2013-06-11 17:01     ` Steven Rostedt
2013-06-11 17:16       ` Paul E. McKenney
2013-06-11 18:41     ` Waiman Long [this message]
2013-06-11 18:54       ` Davidlohr Bueso
2013-06-11 19:49       ` Paul E. McKenney
2013-06-11 20:09         ` Steven Rostedt
2013-06-11 20:32           ` Paul E. McKenney
2013-06-11 20:53             ` Steven Rostedt
2013-06-11 20:25         ` Jason Low
2013-06-11 20:36           ` Paul E. McKenney
2013-06-11 20:56         ` Steven Rostedt
2013-06-11 21:09           ` Paul E. McKenney
2013-06-12  1:19         ` Lai Jiangshan
2013-06-12  1:58           ` Steven Rostedt
2013-06-12 10:12             ` Paul E. McKenney
2013-06-12 11:06             ` Lai Jiangshan
2013-06-12 14:21               ` Paul E. McKenney
2013-06-12 14:15         ` Lai Jiangshan
2013-06-12 14:44           ` Paul E. McKenney
2013-06-11 17:02 ` [PATCH RFC ticketlock] v2 " Paul E. McKenney
2013-06-11 17:35   ` Linus Torvalds
2013-06-11 17:49     ` Paul E. McKenney
2013-06-11 17:36   ` Steven Rostedt
2013-06-11 17:52     ` Paul E. McKenney
2013-06-12 15:40   ` [PATCH RFC ticketlock] v3 " Paul E. McKenney
2013-06-12 16:13     ` Lai Jiangshan
2013-06-12 16:59       ` Paul E. McKenney
2013-06-13  2:55     ` Lai Jiangshan
2013-06-13 15:22       ` Paul E. McKenney
2013-06-13 23:25         ` Lai Jiangshan
2013-06-13 23:57           ` Paul E. McKenney
2013-06-14  1:28             ` Lai Jiangshan
2013-06-14 23:49               ` Paul E. McKenney
2013-06-14  7:12             ` Lai Jiangshan
2013-06-14 23:46               ` Paul E. McKenney
     [not found]     ` <CAC4Lta3dpTDc19rXLVQkZrxbu8AJL+Foc6ocAktUAozCpk2-Mg@mail.gmail.com>
2013-07-01  9:19       ` Raghavendra KT
2013-07-02  5:56         ` Paul E. McKenney

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=51B76F77.6020004@hp.com \
    --to=waiman.long@hp.com \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=akpm@linux-foundation.org \
    --cc=darren@dvhart.com \
    --cc=davidlohr.bueso@hp.com \
    --cc=dhowells@redhat.com \
    --cc=dipankar@in.ibm.com \
    --cc=edumazet@google.com \
    --cc=fweisbec@gmail.com \
    --cc=josh@joshtriplett.org \
    --cc=laijs@cn.fujitsu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mingo@elte.hu \
    --cc=niv@us.ibm.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sbw@mit.edu \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.