svn commit: r254380 - in head/sys: kern sys

Colin Percival cperciva at FreeBSD.org
Thu Aug 15 20:19:18 UTC 2013


Author: cperciva
Date: Thu Aug 15 20:19:17 2013
New Revision: 254380
URL: http://svnweb.freebsd.org/changeset/base/254380

Log:
  Change the queue of locks in kern_rangelock.c from holding lock requests in
  the order that they arrive, to holding
  (a) granted write lock requests, followed by
  (b) granted read lock requests, followed by
  (c) ungranted requests, in order of arrival.
  
  This changes the stopping condition for iterating through granted locks to
  see if a new request can be granted: When considering a read lock request,
  we can stop iterating as soon as we see a read lock request, since anything
  after that point is either a granted read lock request or a request which
  has not yet been granted.  (For write lock requests, we must still compare
  against all granted lock requests.)
  
  For workloads with R parallel reads and W parallel writes, this improves
  the time spent from O((R+W)^2) to O(W*(R+W)); i.e., heavy parallel-read
  workloads become significantly more scalable.
  
  No statistically significant change in buildworld time has been measured,
  but synthetic tests of parallel 'dd > /dev/null' and 'openssl enc >/dev/null'
  with the input file cached yield dramatic (up to 10x) improvement with high
  (up to 128 processes) levels of parallelism.
  
  Reviewed by:	kib

Modified:
  head/sys/kern/kern_rangelock.c
  head/sys/sys/rangelock.h

Modified: head/sys/kern/kern_rangelock.c
==============================================================================
--- head/sys/kern/kern_rangelock.c	Thu Aug 15 20:03:22 2013	(r254379)
+++ head/sys/kern/kern_rangelock.c	Thu Aug 15 20:19:17 2013	(r254380)
@@ -84,20 +84,14 @@ rangelock_destroy(struct rangelock *lock
 }
 
 /*
- * Verifies the supplied rl_q_entries for compatibility.  Returns true
- * if the rangelock queue entries are not compatible, false if they are.
- *
  * Two entries are compatible if their ranges do not overlap, or both
  * entries are for read.
  */
 static int
-rangelock_incompatible(const struct rl_q_entry *e1,
+ranges_overlap(const struct rl_q_entry *e1,
     const struct rl_q_entry *e2)
 {
 
-	if ((e1->rl_q_flags & RL_LOCK_TYPE_MASK) == RL_LOCK_READ &&
-	    (e2->rl_q_flags & RL_LOCK_TYPE_MASK) == RL_LOCK_READ)
-		return (0);
 	if (e1->rl_q_start < e2->rl_q_end && e1->rl_q_end > e2->rl_q_start)
 		return (1);
 	return (0);
@@ -109,30 +103,38 @@ rangelock_incompatible(const struct rl_q
 static void
 rangelock_calc_block(struct rangelock *lock)
 {
-	struct rl_q_entry *entry, *entry1, *whead;
+	struct rl_q_entry *entry, *nextentry, *entry1;
 
-	if (lock->rl_currdep == TAILQ_FIRST(&lock->rl_waiters) &&
-	    lock->rl_currdep != NULL)
-		lock->rl_currdep = TAILQ_NEXT(lock->rl_currdep, rl_q_link);
-	for (entry = lock->rl_currdep; entry != NULL;
-	     entry = TAILQ_NEXT(entry, rl_q_link)) {
-		TAILQ_FOREACH(entry1, &lock->rl_waiters, rl_q_link) {
-			if (rangelock_incompatible(entry, entry1))
-				goto out;
-			if (entry1 == entry)
-				break;
+	for (entry = lock->rl_currdep; entry != NULL; entry = nextentry) {
+		nextentry = TAILQ_NEXT(entry, rl_q_link);
+		if (entry->rl_q_flags & RL_LOCK_READ) {
+			/* Reads must not overlap with granted writes. */
+			for (entry1 = TAILQ_FIRST(&lock->rl_waiters);
+			    !(entry1->rl_q_flags & RL_LOCK_READ);
+			    entry1 = TAILQ_NEXT(entry1, rl_q_link)) {
+				if (ranges_overlap(entry, entry1))
+					goto out;
+			}
+		} else {
+			/* Write must not overlap with any granted locks. */
+			for (entry1 = TAILQ_FIRST(&lock->rl_waiters);
+			    entry1 != entry;
+			    entry1 = TAILQ_NEXT(entry1, rl_q_link)) {
+				if (ranges_overlap(entry, entry1))
+					goto out;
+			}
+
+			/* Move grantable write locks to the front. */
+			TAILQ_REMOVE(&lock->rl_waiters, entry, rl_q_link);
+			TAILQ_INSERT_HEAD(&lock->rl_waiters, entry, rl_q_link);
 		}
+
+		/* Grant this lock. */
+		entry->rl_q_flags |= RL_LOCK_GRANTED;
+		wakeup(entry);
 	}
 out:
 	lock->rl_currdep = entry;
-	TAILQ_FOREACH(whead, &lock->rl_waiters, rl_q_link) {
-		if (whead == lock->rl_currdep)
-			break;
-		if (!(whead->rl_q_flags & RL_LOCK_GRANTED)) {
-			whead->rl_q_flags |= RL_LOCK_GRANTED;
-			wakeup(whead);
-		}
-	}
 }
 
 static void

Modified: head/sys/sys/rangelock.h
==============================================================================
--- head/sys/sys/rangelock.h	Thu Aug 15 20:03:22 2013	(r254379)
+++ head/sys/sys/rangelock.h	Thu Aug 15 20:19:17 2013	(r254380)
@@ -48,9 +48,13 @@ struct rl_q_entry;
  * Access to the structure itself is synchronized with the externally
  * supplied mutex.
  *
- * rl_waiters is the queue of lock requests in the order of arrival.
+ * rl_waiters is the queue containing in order (a) granted write lock
+ * requests, (b) granted read lock requests, and (c) in order of arrival,
+ * lock requests which cannot be granted yet.
+ *
  * rl_currdep is the first lock request that cannot be granted now due
- * to the preceding requests conflicting with it.
+ * to the preceding requests conflicting with it (i.e., it points to
+ * position (c) in the list above).
  */
 struct rangelock {
 	TAILQ_HEAD(, rl_q_entry) rl_waiters;


More information about the svn-src-all mailing list