From patchwork Sat Apr 30 03:33:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 8986541 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 83FE8BF29F for ; Sat, 30 Apr 2016 03:38:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 91C2B201CE for ; Sat, 30 Apr 2016 03:38:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 50084201CD for ; Sat, 30 Apr 2016 03:38:12 +0000 (UTC) Received: from localhost ([::1]:57193 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLjM-0002t8-H1 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 23:38:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59247) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLfR-0004A4-U7 for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:34:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1awLf7-00005m-H7 for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:34:00 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:44877) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLf6-0008Ta-6l for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:33:45 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id ABDB020AF2 for ; Fri, 29 Apr 2016 23:33:21 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute1.internal (MEProxy); Fri, 29 Apr 2016 23:33:21 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-sasl-enc:x-sasl-enc; s=mesmtp; bh=+5GPUyYE0mwN/tjYC3wAolo1XiQ =; b=DiliVZ4X1uQfLZhUelznddgGP2+4etCwHfGlQwqNX9y4klVyr5nTy10V6YZ L/MgzEyPouqxp9+QOrJsEi3wNscqzg/fYrEQpaN7JgMeAiaFQeYPWE925bTgVOPm PthBVHM91H19A8BVWLtbq3uo8QGjNApO5zGi3Gzdks74LQAo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=+5GP UyYE0mwN/tjYC3wAolo1XiQ=; b=H/KWwKDQK7bI3/3MgRYtwH2qKOJl/Phu5Z9X a8OkwY3nJ8zdOK1IO9kQixUamMK7vdqR3QhngEMHibaAfG0nOc5yfAJ9k1Ks4WFR PC0fv+X4ypcNkjdo2CPiWDWCjoVWN2zFqCa61sdgeegc0DAWQ+aXfhk+chmBtTKv Ff0RLFE= X-Sasl-enc: OvBboppBKmW+CLWo91Vcg6GZxE8DkMybU/wXEt6ClP4c 1461987201 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 55E9F6801A8; Fri, 29 Apr 2016 23:33:21 -0400 (EDT) From: "Emilio G. Cota" To: QEMU Developers , MTTCG Devel Date: Fri, 29 Apr 2016 23:33:14 -0400 Message-Id: <1461987197-31264-12-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461987197-31264-1-git-send-email-cota@braap.org> References: <1461987197-31264-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.29 Subject: [Qemu-devel] [PATCH v4 11/14] qht: QEMU's fast, resizable and scalable Hash Table X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Sergey Fedorov , Richard Henderson , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Peter Crosthwaite Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a hash table with optional auto-resizing and MRU promotion for reads and writes. Its implementation goal is to stay fast while scaling for read-mostly workloads. A hash table with these features will be necessary for the scalability of the ongoing MTTCG work; before those changes arrive we can already benefit from the single-threaded speedup that qht also provides. Signed-off-by: Emilio G. Cota Reviewed-by: Richard Henderson --- include/qemu/qht.h | 67 +++++ util/Makefile.objs | 2 +- util/qht.c | 722 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 790 insertions(+), 1 deletion(-) create mode 100644 include/qemu/qht.h create mode 100644 util/qht.c diff --git a/include/qemu/qht.h b/include/qemu/qht.h new file mode 100644 index 0000000..8beea75 --- /dev/null +++ b/include/qemu/qht.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2016, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#ifndef QEMU_QHT_H +#define QEMU_QHT_H + +#include "qemu/osdep.h" +#include "qemu-common.h" +#include "qemu/seqlock.h" +#include "qemu/qdist.h" +#include "qemu/rcu.h" + +struct qht { + struct qht_map *map; + unsigned int mode; +}; + +struct qht_stats { + size_t head_buckets; + size_t used_head_buckets; + size_t entries; + struct qdist chain; + struct qdist occupancy; +}; + +typedef bool (*qht_lookup_func_t)(const void *obj, const void *userp); +typedef void (*qht_iter_func_t)(struct qht *ht, void *p, uint32_t h, void *up); + +#define QHT_MODE_MRU_LOOKUP 0x1 /* move looked-up items to head */ +#define QHT_MODE_MRU_INSERT 0x2 /* insert new elements at the head */ +#define QHT_MODE_AUTO_RESIZE 0x4 /* auto-resize when heavily loaded */ + +void qht_init(struct qht *ht, size_t n_elems, unsigned int mode); + +/* call only when there are no readers left */ +void qht_destroy(struct qht *ht); + +/* call with an external lock held */ +void qht_reset(struct qht *ht); + +/* call with an external lock held */ +void qht_reset_size(struct qht *ht, size_t n_elems); + +/* call with an external lock held */ +void qht_insert(struct qht *ht, void *p, uint32_t hash); + +/* call with an external lock held */ +bool qht_remove(struct qht *ht, const void *p, uint32_t hash); + +/* call with an external lock held */ +void qht_iter(struct qht *ht, qht_iter_func_t func, void *userp); + +/* call with an external lock held */ +void qht_grow(struct qht *ht); + +void *qht_lookup(struct qht *ht, qht_lookup_func_t func, const void *userp, + uint32_t hash); + +/* pass @stats to qht_statistics_destroy() when done */ +void qht_statistics_init(struct qht *ht, struct qht_stats *stats); + +void qht_statistics_destroy(struct qht_stats *stats); + +#endif /* QEMU_QHT_H */ diff --git a/util/Makefile.objs b/util/Makefile.objs index 5985a2e..7952f6d 100644 --- a/util/Makefile.objs +++ b/util/Makefile.objs @@ -1,4 +1,4 @@ -util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o qdist.o +util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o qdist.o qht.o util-obj-$(CONFIG_POSIX) += compatfd.o util-obj-$(CONFIG_POSIX) += event_notifier-posix.o util-obj-$(CONFIG_POSIX) += mmap-alloc.o diff --git a/util/qht.c b/util/qht.c new file mode 100644 index 0000000..ef64510 --- /dev/null +++ b/util/qht.c @@ -0,0 +1,722 @@ +/* + * qht.c - QEMU Hash Table, designed to scale for read-mostly workloads. + * + * Copyright (C) 2016, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + * + * Assumptions: + * - Writers and iterators must take an external lock. + * - NULL cannot be inserted as a pointer value. + * + * Features: + * - Optional auto-resizing: the hash table resizes up if the load surpasses + * a certain threshold. Resizing is done concurrently with readers. + * - Optional bucket MRU promotion policy. + * + * The key structure is the bucket, which is cacheline-sized. Buckets + * contain a few hash values and pointers; the u32 hash values are stored in + * full so that resizing is fast. Having this structure instead of directly + * chaining items has three advantages: + * - Failed lookups fail fast, and touch a minimum number of cache lines. + * - Resizing the hash table with concurrent lookups is easy. + * - We can have a few Most-Recently-Used (MRU) hash-pointer pairs in the same + * head bucket. This helps scalability, since MRU promotions (i.e. writes to + * the bucket) become less common. + * + * For concurrent lookups we use a per-bucket seqlock; per-bucket spinlocks + * allow readers (lookups) to upgrade to writers and thus implement an MRU + * promotion policy; these MRU-induced writes do not touch the cache lines of + * other head buckets. + * + * There are two types of buckets: + * 1. "head" buckets are the ones allocated in the array of buckets in qht_map. + * 2. all "non-head" buckets (i.e. all others) are members of a chain that + * starts from a head bucket. + * Note that the seqlock and spinlock of a head bucket applies to all buckets + * chained to it; these two fields are unused in non-head buckets. + * + * On removals, we move the last valid item in the chain to the position of the + * just-removed entry. This makes lookups slightly faster, since the moment an + * invalid entry is found, the (failed) lookup is over. + * + * Resizing is done by taking all spinlocks (so that no readers-turned-writers + * can race with us) and then placing all elements into a new hash table. Last, + * the ht->map pointer is set, and the old map is freed once no RCU readers can + * see it anymore. + * + * Related Work: + * - Idea of cacheline-sized buckets with full hashes taken from: + * David, Guerraoui & Trigonakis, "Asynchronized Concurrency: + * The Secret to Scaling Concurrent Search Data Structures", ASPLOS'15. + * - Why not RCU-based hash tables? They would allow us to get rid of the + * seqlock, but resizing would take forever since RCU read critical + * sections in QEMU take quite a long time. + * More info on relativistic hash tables: + * + Triplett, McKenney & Walpole, "Resizable, Scalable, Concurrent Hash + * Tables via Relativistic Programming", USENIX ATC'11. + * + Corbet, "Relativistic hash tables, part 1: Algorithms", @ lwn.net, 2014. + * https://lwn.net/Articles/612021/ + */ +#include "qemu/qht.h" +#include "qemu/atomic.h" + +//#define QHT_DEBUG + +/* + * We want to avoid false sharing of cache lines. Most systems have 64-byte + * cache lines so we go with it for simplicity. + * + * Note that systems with smaller cache lines will be fine (the struct is + * almost 64-bytes); systems with larger cache lines might suffer from + * some false sharing. + */ +#define QHT_BUCKET_ALIGN 64 + +/* define these to keep sizeof(qht_bucket) within QHT_BUCKET_ALIGN */ +#if HOST_LONG_BITS == 32 +#define QHT_BUCKET_ENTRIES 6 +#else /* 64-bit */ +#define QHT_BUCKET_ENTRIES 4 +#endif + +struct qht_bucket { + QemuSpin lock; + QemuSeqLock sequence; + uint32_t hashes[QHT_BUCKET_ENTRIES]; + void *pointers[QHT_BUCKET_ENTRIES]; + struct qht_bucket *next; +} QEMU_ALIGNED(QHT_BUCKET_ALIGN); + +QEMU_BUILD_BUG_ON(sizeof(struct qht_bucket) > QHT_BUCKET_ALIGN); + +/* Note: keep @rcu as the top element to help valgrind find the whole struct */ +struct qht_map { + struct rcu_head rcu; + struct qht_bucket *buckets; + size_t n; + size_t n_items; + size_t n_items_threshold; +}; + +#ifdef QHT_DEBUG +static void qht_bucket_debug(struct qht_bucket *b) +{ + bool seen_empty = false; + bool corrupt = false; + int i; + + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (b->pointers[i] == NULL) { + seen_empty = true; + continue; + } + if (seen_empty) { + fprintf(stderr, "%s: b: %p, pos: %i, hash: 0x%x, p: %p\n", + __func__, b, i, b->hashes[i], b->pointers[i]); + corrupt = true; + } + } + b = b->next; + } while (b); + assert(!corrupt); +} + +static void qht_map_debug(struct qht_map *map) +{ + int i; + + for (i = 0; i < map->n; i++) { + qht_bucket_debug(&map->buckets[i]); + } +} +#else +static inline void qht_bucket_debug(struct qht_bucket *b) +{ } + +static inline void qht_map_debug(struct qht_map *map) +{ } +#endif /* QHT_DEBUG */ + +static inline size_t qht_elems_to_buckets(size_t n_elems) +{ + return pow2ceil(n_elems / QHT_BUCKET_ENTRIES); +} + +static inline void qht_head_init(struct qht_bucket *b) +{ + memset(b, 0, sizeof(*b)); + qemu_spin_init(&b->lock); + seqlock_init(&b->sequence); +} + +static inline +struct qht_bucket *qht_map_to_bucket(struct qht_map *map, uint32_t hash) +{ + return &map->buckets[hash & (map->n - 1)]; +} + +static inline void qht_chain_destroy(struct qht_bucket *head) +{ + struct qht_bucket *curr = head->next; + struct qht_bucket *prev; + + while (curr) { + prev = curr; + curr = curr->next; + qemu_vfree(prev); + } +} + +/* pass only an orphan map */ +static void qht_map_destroy(struct qht_map *map) +{ + size_t i; + + for (i = 0; i < map->n; i++) { + qht_chain_destroy(&map->buckets[i]); + } + qemu_vfree(map->buckets); + g_free(map); +} + +static void qht_map_reclaim(struct rcu_head *rcu) +{ + struct qht_map *map = container_of(rcu, struct qht_map, rcu); + + qht_map_destroy(map); +} + +static struct qht_map *qht_map_create(size_t n) +{ + struct qht_map *map; + size_t i; + + map = g_malloc(sizeof(*map)); + map->n = n; + map->n_items = 0; + map->n_items_threshold = n * QHT_BUCKET_ENTRIES / 2; + map->buckets = qemu_memalign(QHT_BUCKET_ALIGN, sizeof(*map->buckets) * n); + for (i = 0; i < n; i++) { + qht_head_init(&map->buckets[i]); + } + return map; +} + +static inline void qht_publish(struct qht *ht, struct qht_map *new) +{ + /* Readers should see a properly initialized map; pair with smp_rmb() */ + smp_wmb(); + atomic_set(&ht->map, new); +} + +void qht_init(struct qht *ht, size_t n_elems, unsigned int mode) +{ + struct qht_map *map; + size_t n = qht_elems_to_buckets(n_elems); + + map = qht_map_create(n); + ht->mode = mode; + qht_publish(ht, map); +} + +/* call only when there are no readers left */ +void qht_destroy(struct qht *ht) +{ + qht_map_destroy(ht->map); + memset(ht, 0, sizeof(*ht)); +} + +static void qht_bucket_reset(struct qht_bucket *head) +{ + struct qht_bucket *b = head; + int i; + + qemu_spin_lock(&head->lock); + seqlock_write_begin(&head->sequence); + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (b->pointers[i] == NULL) { + goto done; + } + atomic_set(&b->hashes[i], 0); + atomic_set(&b->pointers[i], NULL); + } + b = b->next; + } while (b); + done: + seqlock_write_end(&head->sequence); + qemu_spin_unlock(&head->lock); +} + +/* call with an external lock held */ +void qht_reset(struct qht *ht) +{ + struct qht_map *map = ht->map; + size_t i; + + for (i = 0; i < map->n; i++) { + qht_bucket_reset(&map->buckets[i]); + } + qht_map_debug(map); + map->n_items = 0; +} + +/* call with an external lock held */ +void qht_reset_size(struct qht *ht, size_t n_elems) +{ + struct qht_map *old = ht->map; + + qht_reset(ht); + if (old->n == qht_elems_to_buckets(n_elems)) { + return; + } + qht_init(ht, n_elems, ht->mode); + call_rcu1(&old->rcu, qht_map_reclaim); +} + +/* + * Call with head->lock held. + * + * This function moves the item in @orig[@pos] to @head[0]. In order to + * do this, the item i in @head is moved to @head's i+1 position, + * with the last item being moved to @orig[@pos]. + * + * Note: @orig == @head is a bug. + */ +static inline void qht_bucket_mru__locked(struct qht_bucket *head, + struct qht_bucket *orig, int pos) +{ + uint32_t *dest_hash; + void **dest_p; + void *p; + uint32_t hash; + int i; + + assert(orig != head); + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (i == QHT_BUCKET_ENTRIES - 1) { + dest_hash = &orig->hashes[pos]; + dest_p = &orig->pointers[pos]; + } else { + dest_hash = &head->hashes[i + 1]; + dest_p = &head->pointers[i + 1]; + } + hash = *dest_hash; + p = *dest_p; + + atomic_set(dest_hash, head->hashes[i]); + atomic_set(dest_p, head->pointers[i]); + + atomic_set(&head->hashes[i], hash); + atomic_set(&head->pointers[i], p); + } +} + +static void qht_bucket_mru(struct qht_bucket *head, struct qht_bucket *orig, + const void *p, int pos) +{ + qemu_spin_lock(&head->lock); + if (unlikely(orig->pointers[pos] != p)) { + /* while we acquired the lock, the bucket was updated, so bail out */ + goto out; + } + seqlock_write_begin(&head->sequence); + qht_bucket_mru__locked(head, orig, pos); + seqlock_write_end(&head->sequence); + out: + qemu_spin_unlock(&head->lock); +} + +/* + * @far_bucket and @pos are only filled in if the match is found in a bucket + * that is not the head bucket. + */ +static inline +void *qht_do_lookup(struct qht_bucket *head, struct qht_bucket **far_bucket, + int *pos, qht_lookup_func_t func, const void *userp, + uint32_t hash) +{ + struct qht_bucket *b = head; + int i; + + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + void *p = atomic_read(&b->pointers[i]); + + if (unlikely(p == NULL)) { + return NULL; + } + if (atomic_read(&b->hashes[i]) == hash) { + if (likely(func(p, userp))) { + if (unlikely(b != head)) { + *far_bucket = b; + *pos = i; + } + return p; + } + } + } + b = atomic_read(&b->next); + /* + * This barrier guarantees that we will read a properly initialized + * b->next; it is paired with an smp_wmb() before setting b->next. + */ + smp_rmb(); + } while (b); + return NULL; +} + +void *qht_lookup(struct qht *ht, qht_lookup_func_t func, const void *userp, + uint32_t hash) +{ + struct qht_bucket *far_bucket = NULL; + struct qht_bucket *b; + struct qht_map *map; + uint32_t version; + int pos = 0; + void *ret; + + map = atomic_read(&ht->map); + /* paired with smp_wmb() before setting ht->map */ + smp_rmb(); + b = qht_map_to_bucket(map, hash); + + do { + version = seqlock_read_begin(&b->sequence); + ret = qht_do_lookup(b, &far_bucket, &pos, func, userp, hash); + } while (seqlock_read_retry(&b->sequence, version)); + if ((ht->mode & QHT_MODE_MRU_LOOKUP) && unlikely(far_bucket)) { + qht_bucket_mru(b, far_bucket, ret, pos); + } + return ret; +} + +/* call with head->lock held */ +static void qht_insert__locked(struct qht *ht, struct qht_map *map, + struct qht_bucket *head, void *p, uint32_t hash) +{ + struct qht_bucket *b = head; + struct qht_bucket *prev = NULL; + struct qht_bucket *new = NULL; + int i; + + for (;;) { + if (b == NULL) { + b = qemu_memalign(QHT_BUCKET_ALIGN, sizeof(*b)); + memset(b, 0, sizeof(*b)); + new = b; + } + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (b->pointers[i]) { + continue; + } + /* found an empty key: acquire the seqlock and write */ + seqlock_write_begin(&head->sequence); + if (new) { + /* + * This barrier is paired with smp_rmb() after reading + * b->next when not holding b->lock. + */ + smp_wmb(); + atomic_set(&prev->next, b); + } + atomic_set(&b->hashes[i], hash); + atomic_set(&b->pointers[i], p); + if ((ht->mode & QHT_MODE_MRU_INSERT) && b != head) { + qht_bucket_mru__locked(head, b, i); + } + seqlock_write_end(&head->sequence); + map->n_items++; + return; + } + prev = b; + b = b->next; + } +} + +/* call with an external lock held */ +void qht_insert(struct qht *ht, void *p, uint32_t hash) +{ + struct qht_map *map = ht->map; + struct qht_bucket *b = qht_map_to_bucket(map, hash); + + /* NULL pointers are not supported */ + assert(p); + + qemu_spin_lock(&b->lock); + qht_insert__locked(ht, map, b, p, hash); + qht_bucket_debug(b); + qemu_spin_unlock(&b->lock); + + if ((ht->mode & QHT_MODE_AUTO_RESIZE) && + unlikely(map->n_items > map->n_items_threshold)) { + qht_grow(ht); + } +} + +static inline bool qht_entry_is_last(struct qht_bucket *b, int pos) +{ + if (pos == QHT_BUCKET_ENTRIES - 1) { + if (b->next == NULL) { + return true; + } + return b->next->pointers[0] == NULL; + } + return b->pointers[pos + 1] == NULL; +} + +static void +qht_entry_move(struct qht_bucket *to, int i, struct qht_bucket *from, int j) +{ + assert(!(to == from && i == j)); + assert(to->pointers[i] == NULL); + assert(from->pointers[j]); + + atomic_set(&to->hashes[i], from->hashes[j]); + atomic_set(&to->pointers[i], from->pointers[j]); + + atomic_set(&from->hashes[j], 0); + atomic_set(&from->pointers[j], NULL); +} + +/* + * Find the last valid entry in @head, and swap it with @orig[pos], which has + * just been invalidated. + */ +static inline void qht_bucket_fill_hole(struct qht_bucket *orig, int pos) +{ + struct qht_bucket *b = orig; + struct qht_bucket *prev = NULL; + int i; + + if (qht_entry_is_last(orig, pos)) { + return; + } + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (b->pointers[i] || (b == orig && i == pos)) { + continue; + } + if (i > 0) { + return qht_entry_move(orig, pos, b, i - 1); + } + assert(prev); + return qht_entry_move(orig, pos, prev, QHT_BUCKET_ENTRIES - 1); + } + prev = b; + b = b->next; + } while (b); + /* no free entries other than orig[pos], so swap it with the last one */ + qht_entry_move(orig, pos, prev, QHT_BUCKET_ENTRIES - 1); +} + +/* call with b->lock held */ +static inline bool qht_remove__locked(struct qht_map *map, struct qht_bucket *b, + const void *p, uint32_t hash) +{ + int i; + + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + void *q = b->pointers[i]; + + if (unlikely(q == NULL)) { + return false; + } + if (q == p) { + assert(b->hashes[i] == hash); + atomic_set(&b->hashes[i], 0); + atomic_set(&b->pointers[i], NULL); + map->n_items--; + qht_bucket_fill_hole(b, i); + return true; + } + } + b = b->next; + } while (b); + return false; +} + +/* call with an external lock held */ +bool qht_remove(struct qht *ht, const void *p, uint32_t hash) +{ + struct qht_map *map = ht->map; + struct qht_bucket *b = qht_map_to_bucket(map, hash); + bool ret; + + qemu_spin_lock(&b->lock); + seqlock_write_begin(&b->sequence); + ret = qht_remove__locked(map, b, p, hash); + qht_bucket_debug(b); + seqlock_write_end(&b->sequence); + qemu_spin_unlock(&b->lock); + return ret; +} + +/* + * acquire all spinlocks from a map, so that writers cannot race with + * readers-turned-writers. + */ +static void qht_lock(struct qht *ht) +{ + struct qht_map *map = ht->map; + size_t i; + + /* if readers cannot upgrade, do nothing */ + if (!(ht->mode & QHT_MODE_MRU_LOOKUP)) { + return; + } + for (i = 0; i < map->n; i++) { + struct qht_bucket *b = &map->buckets[i]; + + qemu_spin_lock(&b->lock); + } +} + +static void qht_unlock(struct qht *ht) +{ + struct qht_map *map = ht->map; + size_t i; + + if (!(ht->mode & QHT_MODE_MRU_LOOKUP)) { + return; + } + for (i = 0; i < map->n; i++) { + struct qht_bucket *b = &map->buckets[i]; + + qemu_spin_unlock(&b->lock); + } +} + +static inline void qht_bucket_iter(struct qht *ht, struct qht_bucket *b, + qht_iter_func_t func, void *userp) +{ + int i; + + do { + for (i = 0; i < QHT_BUCKET_ENTRIES; i++) { + if (b->pointers[i] == NULL) { + return; + } + func(ht, b->pointers[i], b->hashes[i], userp); + } + b = b->next; + } while (b); +} + +/* external lock + all of the map's locks held (if !MRU_LOOKUP) */ +static inline void qht_map_iter__locked(struct qht *ht, struct qht_map *map, + qht_iter_func_t func, void *userp) +{ + size_t i; + + for (i = 0; i < map->n; i++) { + qht_bucket_iter(ht, &map->buckets[i], func, userp); + } +} + +/* call with an external lock held */ +void qht_iter(struct qht *ht, qht_iter_func_t func, void *userp) +{ + qht_lock(ht); + qht_map_iter__locked(ht, ht->map, func, userp); + qht_unlock(ht); +} + +static void qht_map_copy(struct qht *ht, void *p, uint32_t hash, void *userp) +{ + struct qht_map *new = userp; + struct qht_bucket *b = qht_map_to_bucket(new, hash); + + /* no need to acquire b->lock because no thread has seen this map yet */ + qht_insert__locked(ht, new, b, p, hash); +} + +/* call with an external lock held */ +void qht_grow(struct qht *ht) +{ + struct qht_map *old = ht->map; + struct qht_map *new; + size_t n = old->n * 2; + + new = qht_map_create(n); + qht_iter(ht, qht_map_copy, new); + qht_map_debug(new); + + qht_publish(ht, new); + call_rcu1(&old->rcu, qht_map_reclaim); +} + +/* pass @stats to qht_statistics_destroy() when done */ +void qht_statistics_init(struct qht *ht, struct qht_stats *stats) +{ + struct qht_map *map; + int i; + + map = atomic_read(&ht->map); + /* paired with smp_wmb() before setting ht->map */ + smp_rmb(); + + /* + * Cannot read map->n_entries because once we're done reading the map, + * the value might have changed. Instead, we'll count the elements we see. + * + * Reading map->n is fine though; it is constant for each map. + */ + stats->head_buckets = map->n; + stats->used_head_buckets = 0; + stats->entries = 0; + qdist_init(&stats->chain); + qdist_init(&stats->occupancy); + + for (i = 0; i < map->n; i++) { + struct qht_bucket *head = &map->buckets[i]; + struct qht_bucket *b; + uint32_t version; + size_t buckets; + size_t entries; + int j; + + do { + version = seqlock_read_begin(&head->sequence); + buckets = 0; + entries = 0; + b = head; + do { + for (j = 0; j < QHT_BUCKET_ENTRIES; j++) { + if (atomic_read(&b->pointers[j]) == NULL) { + break; + } + entries++; + } + buckets++; + b = atomic_read(&b->next); + /* + * This barrier guarantees that we will read a properly + * initialized b->next; it is paired with an smp_wmb() + * before setting b->next. + */ + smp_rmb(); + } while (b); + } while (seqlock_read_retry(&head->sequence, version)); + + if (entries) { + qdist_inc(&stats->chain, buckets); + qdist_inc(&stats->occupancy, + (double)entries / QHT_BUCKET_ENTRIES / buckets); + stats->used_head_buckets++; + stats->entries += entries; + } else { + qdist_inc(&stats->occupancy, 0); + } + } +} + +void qht_statistics_destroy(struct qht_stats *stats) +{ + qdist_destroy(&stats->occupancy); + qdist_destroy(&stats->chain); +}