summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcperciva <cperciva@FreeBSD.org>2013-08-15 20:19:17 +0000
committercperciva <cperciva@FreeBSD.org>2013-08-15 20:19:17 +0000
commit8544609e649a9e074023f253de79d42b4d7d11e6 (patch)
tree4269ac322b715795b2eb9b74bd3b25dc79b21f76
parent6d37e23f0ac7ee8dd61ffd3d96cbdbef9994b541 (diff)
downloadFreeBSD-src-8544609e649a9e074023f253de79d42b4d7d11e6.zip
FreeBSD-src-8544609e649a9e074023f253de79d42b4d7d11e6.tar.gz
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
-rw-r--r--sys/kern/kern_rangelock.c56
-rw-r--r--sys/sys/rangelock.h8
2 files changed, 35 insertions, 29 deletions
diff --git a/sys/kern/kern_rangelock.c b/sys/kern/kern_rangelock.c
index 1b4dfd6..1c0faa3 100644
--- a/sys/kern/kern_rangelock.c
+++ b/sys/kern/kern_rangelock.c
@@ -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_entry *e1,
static void
rangelock_calc_block(struct rangelock *lock)
{
- struct rl_q_entry *entry, *entry1, *whead;
-
- 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;
+ struct rl_q_entry *entry, *nextentry, *entry1;
+
+ 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
diff --git a/sys/sys/rangelock.h b/sys/sys/rangelock.h
index fb0d252..2a904c0 100644
--- a/sys/sys/rangelock.h
+++ b/sys/sys/rangelock.h
@@ -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;
OpenPOWER on IntegriCloud