X-Git-Url: http://git.liburcu.org/?p=urcu.git;a=blobdiff_plain;f=rculfhash.c;h=497a9eacdebd764771d1e6e2e88a95c019f83724;hp=9c957cf4026fe1106f6e0f5db3a63b58585e8269;hb=732ad076c182b3ad706cd62f8a6694f921e1bb7c;hpb=abc490a1549a926de13f29c938d8d6635429e096 diff --git a/rculfhash.c b/rculfhash.c index 9c957cf..497a9ea 100644 --- a/rculfhash.c +++ b/rculfhash.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -37,7 +38,15 @@ #include #include -#define BUCKET_SIZE_RESIZE_THRESHOLD 5 +#define DEBUG /* Test */ + +#ifdef DEBUG +#define dbg_printf(args...) printf(args) +#else +#define dbg_printf(args...) +#endif + +#define BUCKET_SIZE_RESIZE_THRESHOLD 4 #ifndef max #define max(a, b) ((a) > (b) ? (a) : (b)) @@ -45,6 +54,7 @@ struct rcu_table { unsigned long size; /* always a power of 2 */ + unsigned long resize_target; struct rcu_head head; struct rcu_ht_node *tbl[0]; }; @@ -52,9 +62,9 @@ struct rcu_table { struct rcu_ht { struct rcu_table *t; /* shared */ ht_hash_fct hash_fct; - void *hashseed; + ht_compare_fct compare_fct; + unsigned long hash_seed; pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */ - unsigned long target_size; void (*ht_call_rcu)(struct rcu_head *head, void (*func)(struct rcu_head *head)); }; @@ -64,21 +74,12 @@ struct rcu_resize_work { struct rcu_ht *ht; }; -static -void ht_resize_lazy(struct rcu_ht *ht, int growth); - -static -void check_resize(struct rcu_ht *ht, unsigned long chain_len) -{ - if (chain_len >= BUCKET_SIZE_RESIZE_THRESHOLD) - ht_resize_lazy(ht, chain_len / BUCKET_SIZE_RESIZE_THRESHOLD); -} - /* * Algorithm to reverse bits in a word by lookup table, extended to * 64-bit words. - * ref. + * Source: * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable + * Originally from Public Domain. */ static const uint8_t BitReverseTable256[256] = @@ -130,6 +131,44 @@ unsigned long bit_reverse_ulong(unsigned long v) #endif } +/* + * Algorithm to find the log2 of a 32-bit unsigned integer. + * source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup + * Originally from Public Domain. + */ +static const char LogTable256[256] = +{ +#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n + -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, + LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6), + LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7) +}; + +uint32_t log2_u32(uint32_t v) +{ + uint32_t t, tt; + + if ((tt = (v >> 16))) + return (t = (tt >> 8)) + ? 24 + LogTable256[t] + : 16 + LogTable256[tt]; + else + return (t = (v >> 8)) + ? 8 + LogTable256[t] + : LogTable256[v]; +} + +static +void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth); + +static +void check_resize(struct rcu_ht *ht, struct rcu_table *t, + uint32_t chain_len) +{ + if (chain_len >= BUCKET_SIZE_RESIZE_THRESHOLD) + ht_resize_lazy(ht, t, log2_u32(chain_len)); +} + static struct rcu_ht_node *clear_flag(struct rcu_ht_node *node) { @@ -149,7 +188,7 @@ struct rcu_ht_node *flag_removed(struct rcu_ht_node *node) } static -void _uatomic_max(unsigned long *ptr, unsigned long v) +unsigned long _uatomic_max(unsigned long *ptr, unsigned long v) { unsigned long old1, old2; @@ -157,17 +196,20 @@ void _uatomic_max(unsigned long *ptr, unsigned long v) do { old2 = old1; if (old2 >= v) - break; + return old2; } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2); + return v; } static -int _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node) +void _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node) { struct rcu_ht_node *iter_prev = NULL, *iter = NULL; + if (!t->size) + return; for (;;) { - unsigned long chain_len = 0; + uint32_t chain_len = 0; iter_prev = rcu_dereference(t->tbl[node->hash & (t->size - 1)]); assert(iter_prev); @@ -179,14 +221,17 @@ int _ht_add(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node) if (iter->reverse_hash < node->reverse_hash) break; iter_prev = iter; - check_resize(ht, ++chain_len); + check_resize(ht, t, ++chain_len); } /* add in iter_prev->next */ if (is_removed(iter)) continue; + assert(node != iter); node->next = iter; + assert(iter_prev != node); if (uatomic_cmpxchg(&iter_prev->next, iter, node) != iter) continue; + break; } } @@ -227,7 +272,8 @@ retry: goto end; } /* set deletion flag */ - if ((old = uatomic_cmpxchg(&iter->next, next, flag_removed(next))) != next) { + if ((old = uatomic_cmpxchg(&iter->next, next, + flag_removed(next))) != next) { if (old == flag_removed(next)) { ret = -ENOENT; goto end; @@ -265,11 +311,12 @@ void init_table(struct rcu_ht *ht, struct rcu_table *t, t->tbl[i]->reverse_hash = bit_reverse_ulong(i); _ht_add(ht, t, t->tbl[i]); } - t->size = end; + t->resize_target = t->size = end; } struct rcu_ht *ht_new(ht_hash_fct hash_fct, - void *hashseed, + ht_compare_fct compare_fct, + unsigned long hash_seed, unsigned long init_size, void (*ht_call_rcu)(struct rcu_head *head, void (*func)(struct rcu_head *head))) @@ -278,29 +325,30 @@ struct rcu_ht *ht_new(ht_hash_fct hash_fct, ht = calloc(1, sizeof(struct rcu_ht)); ht->hash_fct = hash_fct; - ht->hashseed = hashseed; + ht->compare_fct = compare_fct; + ht->hash_seed = hash_seed; + ht->ht_call_rcu = ht_call_rcu; /* this mutex should not nest in read-side C.S. */ pthread_mutex_init(&ht->resize_mutex, NULL); ht->t = calloc(1, sizeof(struct rcu_table) + (max(init_size, 1) * sizeof(struct rcu_ht_node *))); ht->t->size = 0; + pthread_mutex_lock(&ht->resize_mutex); init_table(ht, ht->t, 0, max(init_size, 1)); - ht->target_size = ht->t->size; - ht->ht_call_rcu = ht_call_rcu; + pthread_mutex_unlock(&ht->resize_mutex); return ht; } -struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key) +struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len) { struct rcu_table *t; struct rcu_ht_node *node; unsigned long hash, reverse_hash; - hash = ht->hash_fct(ht->hashseed, key); + hash = ht->hash_fct(key, key_len, ht->hash_seed); reverse_hash = bit_reverse_ulong(hash); t = rcu_dereference(ht->t); - cmm_smp_read_barrier_depends(); /* read t before size and table */ node = rcu_dereference(t->tbl[hash & (t->size - 1)]); for (;;) { if (unlikely(!node)) @@ -309,7 +357,7 @@ struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key) node = NULL; break; } - if (node->key == key) { + if (!ht->compare_fct(node->key, node->key_len, key, key_len)) { if (is_removed(rcu_dereference(node->next))) node = NULL; break; @@ -319,16 +367,15 @@ struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key) return node; } -int ht_add(struct rcu_ht *ht, struct rcu_ht_node *node) +void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node) { struct rcu_table *t; - node->hash = ht->hash_fct(ht->hashseed, node->key); + node->hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); node->reverse_hash = bit_reverse_ulong((unsigned long) node->hash); t = rcu_dereference(ht->t); - cmm_smp_read_barrier_depends(); /* read t before size and table */ - return _ht_add(ht, t, node); + _ht_add(ht, t, node); } int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node) @@ -336,7 +383,6 @@ int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node) struct rcu_table *t; t = rcu_dereference(ht->t); - cmm_smp_read_barrier_depends(); /* read t before size and table */ return _ht_remove(ht, t, node); } @@ -397,35 +443,41 @@ void _do_ht_resize(struct rcu_ht *ht) old_t = ht->t; old_size = old_t->size; - new_size = CMM_LOAD_SHARED(ht->target_size); + new_size = CMM_LOAD_SHARED(old_t->resize_target); + dbg_printf("rculfhash: resize from %lu to %lu buckets\n", + old_size, new_size); if (old_size == new_size) return; - new_t = realloc(old_t, sizeof(struct rcu_table) + new_t = malloc(sizeof(struct rcu_table) + (new_size * sizeof(struct rcu_ht_node *))); - if (new_size > old_size) - init_table(ht, new_t, old_size, new_size - old_size); - cmm_smp_wmb(); /* update content before updating reallocated size */ - CMM_STORE_SHARED(new_t->size, new_size); - if (new_t != old_t) { - /* Changing table and size atomically wrt lookups */ - rcu_assign_pointer(ht->t, new_t); - ht->ht_call_rcu(&old_t->head, ht_free_table_cb); - } + assert(new_size > old_size); + memcpy(&new_t->tbl, &old_t->tbl, + old_size * sizeof(struct rcu_ht_node *)); + init_table(ht, new_t, old_size, new_size - old_size); + /* Changing table and size atomically wrt lookups */ + rcu_assign_pointer(ht->t, new_t); + ht->ht_call_rcu(&old_t->head, ht_free_table_cb); } static -void resize_target_update(struct rcu_ht *ht, int growth_order) +unsigned long resize_target_update(struct rcu_table *t, + int growth_order) { - _uatomic_max(&ht->target_size, - CMM_LOAD_SHARED(ht->target_size) << growth_order); + return _uatomic_max(&t->resize_target, + t->size << growth_order); } void ht_resize(struct rcu_ht *ht, int growth) { - resize_target_update(ht, growth); - pthread_mutex_lock(&ht->resize_mutex); - _do_ht_resize(ht); - pthread_mutex_unlock(&ht->resize_mutex); + struct rcu_table *t = rcu_dereference(ht->t); + unsigned long target_size; + + target_size = resize_target_update(t, growth); + if (t->size < target_size) { + pthread_mutex_lock(&ht->resize_mutex); + _do_ht_resize(ht); + pthread_mutex_unlock(&ht->resize_mutex); + } } static @@ -442,12 +494,15 @@ void do_resize_cb(struct rcu_head *head) } static -void ht_resize_lazy(struct rcu_ht *ht, int growth) +void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth) { struct rcu_resize_work *work; + unsigned long target_size; - work = malloc(sizeof(*work)); - work->ht = ht; - resize_target_update(ht, growth); - ht->ht_call_rcu(&work->head, do_resize_cb); + target_size = resize_target_update(t, growth); + if (t->size < target_size) { + work = malloc(sizeof(*work)); + work->ht = ht; + ht->ht_call_rcu(&work->head, do_resize_cb); + } }