rcuhash: lazy per-cpu counters, fix resize target update
[urcu.git] / rculfhash.c
CommitLineData
5e28c532 1/*
abc490a1
MD
2 * rculfhash.c
3 *
4 * Userspace RCU library - Lock-Free Expandable RCU Hash Table
5 *
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
5e28c532
MD
21 */
22
e753ff5a
MD
23/*
24 * Based on the following articles:
25 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
26 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
27 * - Michael, M. M. High performance dynamic lock-free hash tables
28 * and list-based sets. In Proceedings of the fourteenth annual ACM
29 * symposium on Parallel algorithms and architectures, ACM Press,
30 * (2002), 73-82.
31 *
32 * Some specificities of this Lock-Free Expandable RCU Hash Table
33 * implementation:
34 *
35 * - RCU read-side critical section allows readers to perform hash
36 * table lookups and use the returned objects safely by delaying
37 * memory reclaim of a grace period.
38 * - Add and remove operations are lock-free, and do not need to
39 * allocate memory. They need to be executed within RCU read-side
40 * critical section to ensure the objects they read are valid and to
41 * deal with the cmpxchg ABA problem.
42 * - add and add_unique operations are supported. add_unique checks if
43 * the node key already exists in the hash table. It ensures no key
44 * duplicata exists.
45 * - The resize operation executes concurrently with add/remove/lookup.
46 * - Hash table nodes are contained within a split-ordered list. This
47 * list is ordered by incrementing reversed-bits-hash value.
48 * - An index of dummy nodes is kept. These dummy nodes are the hash
49 * table "buckets", and they are also chained together in the
50 * split-ordered list, which allows recursive expansion.
51 * - The resize operation only allows expanding the hash table.
52 * It is triggered either through an API call or automatically by
53 * detecting long chains in the add operation.
54 * - Resize operation initiated by long chain detection is executed by a
55 * call_rcu thread, which keeps lock-freedom of add and remove.
56 * - Resize operations are protected by a mutex.
57 * - The removal operation is split in two parts: first, a "removed"
58 * flag is set in the next pointer within the node to remove. Then,
59 * a "garbage collection" is performed in the bucket containing the
60 * removed node (from the start of the bucket up to the removed node).
61 * All encountered nodes with "removed" flag set in their next
62 * pointers are removed from the linked-list. If the cmpxchg used for
63 * removal fails (due to concurrent garbage-collection or concurrent
64 * add), we retry from the beginning of the bucket. This ensures that
65 * the node with "removed" flag set is removed from the hash table
66 * (not visible to lookups anymore) before the RCU read-side critical
67 * section held across removal ends. Furthermore, this ensures that
68 * the node with "removed" flag set is removed from the linked-list
69 * before its memory is reclaimed. Only the thread which removal
70 * successfully set the "removed" flag (with a cmpxchg) into a node's
71 * next pointer is considered to have succeeded its removal (and thus
72 * owns the node to reclaim). Because we garbage-collect starting from
73 * an invariant node (the start-of-bucket dummy node) up to the
74 * "removed" node (or find a reverse-hash that is higher), we are sure
75 * that a successful traversal of the chain leads to a chain that is
76 * present in the linked-list (the start node is never removed) and
77 * that is does not contain the "removed" node anymore, even if
78 * concurrent delete/add operations are changing the structure of the
79 * list concurrently.
29e669f6
MD
80 * - The add operation performs gargage collection of buckets if it
81 * encounters nodes with removed flag set in the bucket where it wants
82 * to add its new node. This ensures lock-freedom of add operation by
83 * helping the remover unlink nodes from the list rather than to wait
84 * for it do to so.
e753ff5a
MD
85 * - A RCU "order table" indexed by log2(hash index) is copied and
86 * expanded by the resize operation. This order table allows finding
87 * the "dummy node" tables.
88 * - There is one dummy node table per hash index order. The size of
89 * each dummy node table is half the number of hashes contained in
90 * this order.
91 * - call_rcu is used to garbage-collect the old order table.
92 * - The per-order dummy node tables contain a compact version of the
93 * hash table nodes. These tables are invariant after they are
94 * populated into the hash table.
95 */
96
2ed95849
MD
97#define _LGPL_SOURCE
98#include <stdlib.h>
e0ba718a
MD
99#include <errno.h>
100#include <assert.h>
101#include <stdio.h>
abc490a1 102#include <stdint.h>
f000907d 103#include <string.h>
e0ba718a 104
df44348d 105#include "config.h"
2ed95849 106#include <urcu.h>
abc490a1 107#include <urcu-call-rcu.h>
a42cc659
MD
108#include <urcu/arch.h>
109#include <urcu/uatomic.h>
674f7a69 110#include <urcu/jhash.h>
a42cc659 111#include <urcu/compiler.h>
abc490a1 112#include <urcu/rculfhash.h>
5e28c532 113#include <stdio.h>
464a1ec9 114#include <pthread.h>
44395fb7 115
f9830efd 116#ifdef DEBUG
f0c29ed7 117#define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
f9830efd 118#else
e753ff5a 119#define dbg_printf(fmt, args...)
f9830efd
MD
120#endif
121
01370f0b
MD
122#define CHAIN_LEN_TARGET 4
123#define CHAIN_LEN_RESIZE_THRESHOLD 8
2ed95849 124
df44348d
MD
125/* Commit counter changes to global counter each 1024 steps */
126#define COUNT_COMMIT_ORDER 10
127
abc490a1
MD
128#ifndef max
129#define max(a, b) ((a) > (b) ? (a) : (b))
130#endif
2ed95849 131
d95bd160
MD
132/*
133 * The removed flag needs to be updated atomically with the pointer.
134 * The dummy flag does not require to be updated atomically with the
135 * pointer, but it is added as a pointer low bit flag to save space.
136 */
d37166c6 137#define REMOVED_FLAG (1UL << 0)
f5596c94
MD
138#define DUMMY_FLAG (1UL << 1)
139#define FLAGS_MASK ((1UL << 2) - 1)
d37166c6 140
df44348d 141struct ht_items_count {
3171717f 142 unsigned long add, remove;
df44348d
MD
143} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
144
395270b6 145struct rcu_table {
abc490a1 146 unsigned long size; /* always a power of 2 */
f9830efd 147 unsigned long resize_target;
11519af6 148 int resize_initiated;
abc490a1 149 struct rcu_head head;
14044b37 150 struct _cds_lfht_node *tbl[0];
395270b6
MD
151};
152
14044b37 153struct cds_lfht {
395270b6 154 struct rcu_table *t; /* shared */
14044b37
MD
155 cds_lfht_hash_fct hash_fct;
156 cds_lfht_compare_fct compare_fct;
732ad076 157 unsigned long hash_seed;
464a1ec9 158 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 159 unsigned int in_progress_resize, in_progress_destroy;
14044b37 160 void (*cds_lfht_call_rcu)(struct rcu_head *head,
abc490a1 161 void (*func)(struct rcu_head *head));
df44348d
MD
162 unsigned long count; /* global approximate item count */
163 struct ht_items_count *percpu_count; /* per-cpu item count */
2ed95849
MD
164};
165
abc490a1
MD
166struct rcu_resize_work {
167 struct rcu_head head;
14044b37 168 struct cds_lfht *ht;
abc490a1 169};
2ed95849 170
abc490a1
MD
171/*
172 * Algorithm to reverse bits in a word by lookup table, extended to
173 * 64-bit words.
f9830efd 174 * Source:
abc490a1 175 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 176 * Originally from Public Domain.
abc490a1
MD
177 */
178
179static const uint8_t BitReverseTable256[256] =
2ed95849 180{
abc490a1
MD
181#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
182#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
183#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
184 R6(0), R6(2), R6(1), R6(3)
185};
186#undef R2
187#undef R4
188#undef R6
2ed95849 189
abc490a1
MD
190static
191uint8_t bit_reverse_u8(uint8_t v)
192{
193 return BitReverseTable256[v];
194}
ab7d5fc6 195
abc490a1
MD
196static __attribute__((unused))
197uint32_t bit_reverse_u32(uint32_t v)
198{
199 return ((uint32_t) bit_reverse_u8(v) << 24) |
200 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
201 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
202 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849
MD
203}
204
abc490a1
MD
205static __attribute__((unused))
206uint64_t bit_reverse_u64(uint64_t v)
2ed95849 207{
abc490a1
MD
208 return ((uint64_t) bit_reverse_u8(v) << 56) |
209 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
210 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
211 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
212 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
213 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
214 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
215 ((uint64_t) bit_reverse_u8(v >> 56));
216}
217
218static
219unsigned long bit_reverse_ulong(unsigned long v)
220{
221#if (CAA_BITS_PER_LONG == 32)
222 return bit_reverse_u32(v);
223#else
224 return bit_reverse_u64(v);
225#endif
226}
227
f9830efd 228/*
24365af7
MD
229 * fls: returns the position of the most significant bit.
230 * Returns 0 if no bit is set, else returns the position of the most
231 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 232 */
24365af7
MD
233#if defined(__i386) || defined(__x86_64)
234static inline
235unsigned int fls_u32(uint32_t x)
f9830efd 236{
24365af7
MD
237 int r;
238
239 asm("bsrl %1,%0\n\t"
240 "jnz 1f\n\t"
241 "movl $-1,%0\n\t"
242 "1:\n\t"
243 : "=r" (r) : "rm" (x));
244 return r + 1;
245}
246#define HAS_FLS_U32
247#endif
248
249#if defined(__x86_64)
250static inline
251unsigned int fls_u64(uint64_t x)
252{
253 long r;
254
255 asm("bsrq %1,%0\n\t"
256 "jnz 1f\n\t"
257 "movq $-1,%0\n\t"
258 "1:\n\t"
259 : "=r" (r) : "rm" (x));
260 return r + 1;
261}
262#define HAS_FLS_U64
263#endif
264
265#ifndef HAS_FLS_U64
266static __attribute__((unused))
267unsigned int fls_u64(uint64_t x)
268{
269 unsigned int r = 64;
270
271 if (!x)
272 return 0;
273
274 if (!(x & 0xFFFFFFFF00000000ULL)) {
275 x <<= 32;
276 r -= 32;
277 }
278 if (!(x & 0xFFFF000000000000ULL)) {
279 x <<= 16;
280 r -= 16;
281 }
282 if (!(x & 0xFF00000000000000ULL)) {
283 x <<= 8;
284 r -= 8;
285 }
286 if (!(x & 0xF000000000000000ULL)) {
287 x <<= 4;
288 r -= 4;
289 }
290 if (!(x & 0xC000000000000000ULL)) {
291 x <<= 2;
292 r -= 2;
293 }
294 if (!(x & 0x8000000000000000ULL)) {
295 x <<= 1;
296 r -= 1;
297 }
298 return r;
299}
300#endif
301
302#ifndef HAS_FLS_U32
303static __attribute__((unused))
304unsigned int fls_u32(uint32_t x)
305{
306 unsigned int r = 32;
f9830efd 307
24365af7
MD
308 if (!x)
309 return 0;
310 if (!(x & 0xFFFF0000U)) {
311 x <<= 16;
312 r -= 16;
313 }
314 if (!(x & 0xFF000000U)) {
315 x <<= 8;
316 r -= 8;
317 }
318 if (!(x & 0xF0000000U)) {
319 x <<= 4;
320 r -= 4;
321 }
322 if (!(x & 0xC0000000U)) {
323 x <<= 2;
324 r -= 2;
325 }
326 if (!(x & 0x80000000U)) {
327 x <<= 1;
328 r -= 1;
329 }
330 return r;
331}
332#endif
333
334unsigned int fls_ulong(unsigned long x)
f9830efd 335{
24365af7
MD
336#if (CAA_BITS_PER_lONG == 32)
337 return fls_u32(x);
338#else
339 return fls_u64(x);
340#endif
341}
f9830efd 342
24365af7
MD
343int get_count_order_u32(uint32_t x)
344{
345 int order;
346
347 order = fls_u32(x) - 1;
348 if (x & (x - 1))
349 order++;
350 return order;
351}
352
353int get_count_order_ulong(unsigned long x)
354{
355 int order;
356
357 order = fls_ulong(x) - 1;
358 if (x & (x - 1))
359 order++;
360 return order;
f9830efd
MD
361}
362
363static
14044b37 364void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth);
f9830efd 365
3171717f
MD
366static
367void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
368 unsigned long count);
369
df44348d
MD
370/*
371 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
372 * available, then we support hash table item accounting.
373 * In the unfortunate event the number of CPUs reported would be
374 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
375 */
376
377#if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
378
379static long nr_cpus_mask = -1;
380
381static
382struct ht_items_count *alloc_per_cpu_items_count(void)
383{
384 struct ht_items_count *count;
385
386 switch (nr_cpus_mask) {
387 case -2:
388 return NULL;
389 case -1:
390 {
391 long maxcpus;
392
393 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
394 if (maxcpus <= 0) {
395 nr_cpus_mask = -2;
396 return NULL;
397 }
398 /*
399 * round up number of CPUs to next power of two, so we
400 * can use & for modulo.
401 */
402 maxcpus = 1UL << get_count_order_ulong(maxcpus);
403 nr_cpus_mask = maxcpus - 1;
404 }
405 /* Fall-through */
406 default:
407 return calloc(nr_cpus_mask + 1, sizeof(*count));
408 }
409}
410
411static
412void free_per_cpu_items_count(struct ht_items_count *count)
413{
414 free(count);
415}
416
417static
418int ht_get_cpu(void)
419{
420 int cpu;
421
422 assert(nr_cpus_mask >= 0);
423 cpu = sched_getcpu();
424 if (unlikely(cpu < 0))
425 return cpu;
426 else
427 return cpu & nr_cpus_mask;
428}
429
430static
3171717f 431void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
df44348d 432{
3171717f 433 unsigned long percpu_count;
df44348d
MD
434 int cpu;
435
436 if (unlikely(!ht->percpu_count))
3171717f 437 return;
df44348d
MD
438 cpu = ht_get_cpu();
439 if (unlikely(cpu < 0))
3171717f
MD
440 return;
441 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].add, 1);
df44348d
MD
442 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
443 unsigned long count;
444
445 dbg_printf("add percpu %lu\n", percpu_count);
446 count = uatomic_add_return(&ht->count,
447 1UL << COUNT_COMMIT_ORDER);
448 /* If power of 2 */
449 if (!(count & (count - 1))) {
450 dbg_printf("add global %lu\n", count);
3171717f 451 cds_lfht_resize_lazy_count(ht, t, count);
df44348d
MD
452 }
453 }
454}
455
456static
457void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
458{
459 unsigned long percpu_count;
3171717f 460 int cpu;
df44348d 461
3171717f
MD
462 if (unlikely(!ht->percpu_count))
463 return;
464 cpu = ht_get_cpu();
465 if (unlikely(cpu < 0))
466 return;
467 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].remove, -1);
df44348d
MD
468 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
469 unsigned long count;
470
471 dbg_printf("remove percpu %lu\n", percpu_count);
472 count = uatomic_add_return(&ht->count,
3171717f 473 -(1UL << COUNT_COMMIT_ORDER));
df44348d
MD
474 /* If power of 2 */
475 if (!(count & (count - 1))) {
476 dbg_printf("remove global %lu\n", count);
3171717f 477 cds_lfht_resize_lazy_count(ht, t, count);
df44348d
MD
478 }
479 }
480}
481
482#else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
483
484static const long nr_cpus_mask = -1;
485
486static
487struct ht_items_count *alloc_per_cpu_items_count(void)
488{
489 return NULL;
490}
491
492static
493void free_per_cpu_items_count(struct ht_items_count *count)
494{
495}
496
497static
498void ht_count_add(struct cds_lfht *ht)
499{
500}
501
502static
503void ht_count_remove(struct cds_lfht *ht)
504{
505}
506
507#endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
508
509
f9830efd 510static
14044b37 511void check_resize(struct cds_lfht *ht, struct rcu_table *t,
f9830efd
MD
512 uint32_t chain_len)
513{
df44348d 514 return; //TEST
24365af7 515 if (chain_len > 100)
f0c29ed7 516 dbg_printf("WARNING: large chain length: %u.\n",
24365af7 517 chain_len);
3390d470 518 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
14044b37 519 cds_lfht_resize_lazy(ht, t,
01370f0b 520 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
f9830efd
MD
521}
522
abc490a1 523static
14044b37 524struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
abc490a1 525{
14044b37 526 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
527}
528
529static
14044b37 530int is_removed(struct cds_lfht_node *node)
abc490a1 531{
d37166c6 532 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
533}
534
535static
14044b37 536struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
abc490a1 537{
14044b37 538 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
abc490a1
MD
539}
540
f5596c94 541static
14044b37 542int is_dummy(struct cds_lfht_node *node)
f5596c94
MD
543{
544 return ((unsigned long) node) & DUMMY_FLAG;
545}
546
547static
14044b37 548struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
f5596c94 549{
14044b37 550 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
f5596c94
MD
551}
552
abc490a1 553static
f9830efd 554unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
abc490a1
MD
555{
556 unsigned long old1, old2;
557
558 old1 = uatomic_read(ptr);
559 do {
560 old2 = old1;
561 if (old2 >= v)
f9830efd 562 return old2;
abc490a1 563 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
f9830efd 564 return v;
abc490a1
MD
565}
566
273399de
MD
567/*
568 * Remove all logically deleted nodes from a bucket up to a certain node key.
569 */
570static
14044b37 571void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
273399de 572{
14044b37 573 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
273399de
MD
574
575 for (;;) {
576 iter_prev = dummy;
577 /* We can always skip the dummy node initially */
cc4fcb10
MD
578 iter = rcu_dereference(iter_prev->p.next);
579 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
273399de 580 for (;;) {
a2974903 581 if (unlikely(!clear_flag(iter)))
479c8a32 582 return;
76412f24 583 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 584 return;
cc4fcb10 585 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 586 if (likely(is_removed(next)))
273399de 587 break;
b453eae1 588 iter_prev = clear_flag(iter);
273399de
MD
589 iter = next;
590 }
591 assert(!is_removed(iter));
f5596c94
MD
592 if (is_dummy(iter))
593 new_next = flag_dummy(clear_flag(next));
594 else
595 new_next = clear_flag(next);
596 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de
MD
597 }
598}
599
abc490a1 600static
14044b37
MD
601struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
602 struct cds_lfht_node *node, int unique, int dummy)
abc490a1 603{
14044b37 604 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
f5596c94 605 *dummy_node;
14044b37 606 struct _cds_lfht_node *lookup;
24365af7 607 unsigned long hash, index, order;
abc490a1 608
18117871 609 if (!t->size) {
f5596c94
MD
610 assert(dummy);
611 node->p.next = flag_dummy(NULL);
18117871
MD
612 return node; /* Initial first add (head) */
613 }
cc4fcb10 614 hash = bit_reverse_ulong(node->p.reverse_hash);
abc490a1 615 for (;;) {
f9830efd 616 uint32_t chain_len = 0;
abc490a1 617
11519af6
MD
618 /*
619 * iter_prev points to the non-removed node prior to the
620 * insert location.
11519af6 621 */
24365af7
MD
622 index = hash & (t->size - 1);
623 order = get_count_order_ulong(index + 1);
624 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37 625 iter_prev = (struct cds_lfht_node *) lookup;
11519af6 626 /* We can always skip the dummy node initially */
cc4fcb10
MD
627 iter = rcu_dereference(iter_prev->p.next);
628 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
abc490a1 629 for (;;) {
a2974903 630 if (unlikely(!clear_flag(iter)))
273399de 631 goto insert;
76412f24 632 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
273399de 633 goto insert;
cc4fcb10 634 next = rcu_dereference(clear_flag(iter)->p.next);
76412f24 635 if (unlikely(is_removed(next)))
9dba85be 636 goto gc_node;
e43f23f8 637 if (unique
1b81fe1a 638 && !is_dummy(next)
e43f23f8
MD
639 && !ht->compare_fct(node->key, node->key_len,
640 clear_flag(iter)->key,
641 clear_flag(iter)->key_len))
18117871 642 return clear_flag(iter);
11519af6 643 /* Only account for identical reverse hash once */
24365af7
MD
644 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
645 && !is_dummy(next))
11519af6
MD
646 check_resize(ht, t, ++chain_len);
647 iter_prev = clear_flag(iter);
273399de 648 iter = next;
abc490a1 649 }
273399de 650 insert:
7ec59d3b 651 assert(node != clear_flag(iter));
11519af6 652 assert(!is_removed(iter_prev));
f000907d 653 assert(iter_prev != node);
f5596c94 654 if (!dummy)
1b81fe1a 655 node->p.next = clear_flag(iter);
f5596c94 656 else
1b81fe1a 657 node->p.next = flag_dummy(clear_flag(iter));
f5596c94
MD
658 if (is_dummy(iter))
659 new_node = flag_dummy(node);
660 else
661 new_node = node;
cc4fcb10 662 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
f5596c94 663 new_node) != iter)
273399de 664 continue; /* retry */
11519af6 665 else
273399de 666 goto gc_end;
9dba85be
MD
667 gc_node:
668 assert(!is_removed(iter));
f5596c94
MD
669 if (is_dummy(iter))
670 new_next = flag_dummy(clear_flag(next));
671 else
672 new_next = clear_flag(next);
673 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de 674 /* retry */
464a1ec9 675 }
273399de
MD
676gc_end:
677 /* Garbage collect logically removed nodes in the bucket */
24365af7
MD
678 index = hash & (t->size - 1);
679 order = get_count_order_ulong(index + 1);
680 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37
MD
681 dummy_node = (struct cds_lfht_node *) lookup;
682 _cds_lfht_gc_bucket(dummy_node, node);
18117871 683 return node;
abc490a1 684}
464a1ec9 685
abc490a1 686static
14044b37
MD
687int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
688 struct cds_lfht_node *node)
abc490a1 689{
14044b37
MD
690 struct cds_lfht_node *dummy, *next, *old;
691 struct _cds_lfht_node *lookup;
abc490a1 692 int flagged = 0;
24365af7 693 unsigned long hash, index, order;
5e28c532 694
7ec59d3b 695 /* logically delete the node */
cc4fcb10 696 old = rcu_dereference(node->p.next);
7ec59d3b
MD
697 do {
698 next = old;
76412f24 699 if (unlikely(is_removed(next)))
7ec59d3b 700 goto end;
1b81fe1a 701 assert(!is_dummy(next));
cc4fcb10 702 old = uatomic_cmpxchg(&node->p.next, next,
7ec59d3b
MD
703 flag_removed(next));
704 } while (old != next);
705
706 /* We performed the (logical) deletion. */
707 flagged = 1;
708
709 /*
710 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
711 * the node, and remove it (along with any other logically removed node)
712 * if found.
11519af6 713 */
cc4fcb10 714 hash = bit_reverse_ulong(node->p.reverse_hash);
24365af7
MD
715 index = hash & (t->size - 1);
716 order = get_count_order_ulong(index + 1);
717 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
14044b37
MD
718 dummy = (struct cds_lfht_node *) lookup;
719 _cds_lfht_gc_bucket(dummy, node);
2ed95849 720end:
11519af6
MD
721 /*
722 * Only the flagging action indicated that we (and no other)
723 * removed the node from the hash.
724 */
7ec59d3b 725 if (flagged) {
cc4fcb10 726 assert(is_removed(rcu_dereference(node->p.next)));
11519af6 727 return 0;
7ec59d3b 728 } else
11519af6 729 return -ENOENT;
abc490a1 730}
2ed95849 731
abc490a1 732static
14044b37 733void init_table(struct cds_lfht *ht, struct rcu_table *t,
24365af7
MD
734 unsigned long first_order, unsigned long len_order)
735{
736 unsigned long i, end_order;
737
f0c29ed7 738 dbg_printf("init table: first_order %lu end_order %lu\n",
24365af7
MD
739 first_order, first_order + len_order);
740 end_order = first_order + len_order;
741 t->size = !first_order ? 0 : (1UL << (first_order - 1));
742 for (i = first_order; i < end_order; i++) {
743 unsigned long j, len;
744
745 len = !i ? 1 : 1UL << (i - 1);
f0c29ed7 746 dbg_printf("init order %lu len: %lu\n", i, len);
14044b37 747 t->tbl[i] = calloc(len, sizeof(struct _cds_lfht_node));
24365af7 748 for (j = 0; j < len; j++) {
f0c29ed7 749 dbg_printf("init entry: i %lu j %lu hash %lu\n",
24365af7 750 i, j, !i ? 0 : (1UL << (i - 1)) + j);
14044b37
MD
751 struct cds_lfht_node *new_node =
752 (struct cds_lfht_node *) &t->tbl[i][j];
24365af7
MD
753 new_node->p.reverse_hash =
754 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
14044b37 755 (void) _cds_lfht_add(ht, t, new_node, 0, 1);
33c7c748
MD
756 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
757 break;
24365af7
MD
758 }
759 /* Update table size */
760 t->size = !i ? 1 : (1UL << i);
f0c29ed7 761 dbg_printf("init new size: %lu\n", t->size);
33c7c748
MD
762 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
763 break;
abc490a1 764 }
24365af7 765 t->resize_target = t->size;
11519af6 766 t->resize_initiated = 0;
2ed95849
MD
767}
768
14044b37
MD
769struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
770 cds_lfht_compare_fct compare_fct,
771 unsigned long hash_seed,
772 unsigned long init_size,
773 void (*cds_lfht_call_rcu)(struct rcu_head *head,
774 void (*func)(struct rcu_head *head)))
abc490a1 775{
14044b37 776 struct cds_lfht *ht;
24365af7 777 unsigned long order;
abc490a1 778
8129be4e 779 /* init_size must be power of two */
49619ea0 780 if (init_size && (init_size & (init_size - 1)))
8129be4e 781 return NULL;
14044b37 782 ht = calloc(1, sizeof(struct cds_lfht));
abc490a1 783 ht->hash_fct = hash_fct;
732ad076
MD
784 ht->compare_fct = compare_fct;
785 ht->hash_seed = hash_seed;
14044b37 786 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
848d4088 787 ht->in_progress_resize = 0;
df44348d 788 ht->percpu_count = alloc_per_cpu_items_count();
abc490a1
MD
789 /* this mutex should not nest in read-side C.S. */
790 pthread_mutex_init(&ht->resize_mutex, NULL);
24365af7 791 order = get_count_order_ulong(max(init_size, 1)) + 1;
14044b37
MD
792 ht->t = calloc(1, sizeof(struct cds_lfht)
793 + (order * sizeof(struct _cds_lfht_node *)));
abc490a1 794 ht->t->size = 0;
f000907d 795 pthread_mutex_lock(&ht->resize_mutex);
24365af7 796 init_table(ht, ht->t, 0, order);
f000907d 797 pthread_mutex_unlock(&ht->resize_mutex);
abc490a1
MD
798 return ht;
799}
800
14044b37 801struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len)
2ed95849 802{
395270b6 803 struct rcu_table *t;
14044b37
MD
804 struct cds_lfht_node *node, *next;
805 struct _cds_lfht_node *lookup;
24365af7 806 unsigned long hash, reverse_hash, index, order;
2ed95849 807
732ad076 808 hash = ht->hash_fct(key, key_len, ht->hash_seed);
abc490a1 809 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 810
395270b6 811 t = rcu_dereference(ht->t);
24365af7
MD
812 index = hash & (t->size - 1);
813 order = get_count_order_ulong(index + 1);
814 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
f0c29ed7 815 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
24365af7 816 hash, index, order, index & ((1UL << (order - 1)) - 1));
14044b37 817 node = (struct cds_lfht_node *) lookup;
2ed95849 818 for (;;) {
abc490a1
MD
819 if (unlikely(!node))
820 break;
cc4fcb10 821 if (unlikely(node->p.reverse_hash > reverse_hash)) {
abc490a1
MD
822 node = NULL;
823 break;
2ed95849 824 }
1b81fe1a
MD
825 next = rcu_dereference(node->p.next);
826 if (likely(!is_removed(next))
827 && !is_dummy(next)
49c2e2d6 828 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
273399de 829 break;
2ed95849 830 }
1b81fe1a 831 node = clear_flag(next);
2ed95849 832 }
1b81fe1a 833 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
abc490a1
MD
834 return node;
835}
e0ba718a 836
a481e5ff
MD
837struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht,
838 struct cds_lfht_node *node)
839{
840 struct cds_lfht_node *next;
841 unsigned long reverse_hash;
842 void *key;
843 size_t key_len;
844
845 reverse_hash = node->p.reverse_hash;
846 key = node->key;
847 key_len = node->key_len;
848 next = rcu_dereference(node->p.next);
849 node = clear_flag(next);
850
851 for (;;) {
852 if (unlikely(!node))
853 break;
854 if (unlikely(node->p.reverse_hash > reverse_hash)) {
855 node = NULL;
856 break;
857 }
858 next = rcu_dereference(node->p.next);
859 if (likely(!is_removed(next))
860 && !is_dummy(next)
861 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
862 break;
863 }
864 node = clear_flag(next);
865 }
866 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
867 return node;
868}
869
14044b37 870void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node)
abc490a1
MD
871{
872 struct rcu_table *t;
49c2e2d6 873 unsigned long hash;
ab7d5fc6 874
49c2e2d6 875 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 876 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
2ed95849 877
abc490a1 878 t = rcu_dereference(ht->t);
14044b37 879 (void) _cds_lfht_add(ht, t, node, 0, 0);
df44348d 880 ht_count_add(ht, t);
3eca1b8c
MD
881}
882
14044b37
MD
883struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
884 struct cds_lfht_node *node)
3eca1b8c
MD
885{
886 struct rcu_table *t;
49c2e2d6 887 unsigned long hash;
df44348d 888 struct cds_lfht_node *ret;
3eca1b8c 889
49c2e2d6 890 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 891 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
3eca1b8c
MD
892
893 t = rcu_dereference(ht->t);
df44348d
MD
894 ret = _cds_lfht_add(ht, t, node, 1, 0);
895 if (ret != node)
896 ht_count_add(ht, t);
897 return ret;
2ed95849
MD
898}
899
14044b37 900int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node)
2ed95849 901{
abc490a1 902 struct rcu_table *t;
df44348d 903 int ret;
abc490a1
MD
904
905 t = rcu_dereference(ht->t);
df44348d
MD
906 ret = _cds_lfht_remove(ht, t, node);
907 if (!ret)
908 ht_count_remove(ht, t);
909 return ret;
2ed95849 910}
ab7d5fc6 911
abc490a1 912static
14044b37 913int cds_lfht_delete_dummy(struct cds_lfht *ht)
674f7a69 914{
395270b6 915 struct rcu_table *t;
14044b37
MD
916 struct cds_lfht_node *node;
917 struct _cds_lfht_node *lookup;
24365af7 918 unsigned long order, i;
674f7a69 919
abc490a1
MD
920 t = ht->t;
921 /* Check that the table is empty */
24365af7 922 lookup = &t->tbl[0][0];
14044b37 923 node = (struct cds_lfht_node *) lookup;
abc490a1 924 do {
1b81fe1a
MD
925 node = clear_flag(node)->p.next;
926 if (!is_dummy(node))
abc490a1 927 return -EPERM;
273399de 928 assert(!is_removed(node));
a2974903 929 } while (clear_flag(node));
abc490a1 930 /* Internal sanity check: all nodes left should be dummy */
24365af7
MD
931 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
932 unsigned long len;
933
934 len = !order ? 1 : 1UL << (order - 1);
935 for (i = 0; i < len; i++) {
f0c29ed7 936 dbg_printf("delete order %lu i %lu hash %lu\n",
24365af7
MD
937 order, i,
938 bit_reverse_ulong(t->tbl[order][i].reverse_hash));
939 assert(is_dummy(t->tbl[order][i].next));
940 }
941 free(t->tbl[order]);
674f7a69 942 }
abc490a1 943 return 0;
674f7a69
MD
944}
945
946/*
947 * Should only be called when no more concurrent readers nor writers can
948 * possibly access the table.
949 */
14044b37 950int cds_lfht_destroy(struct cds_lfht *ht)
674f7a69 951{
5e28c532
MD
952 int ret;
953
848d4088 954 /* Wait for in-flight resize operations to complete */
33c7c748 955 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
848d4088
MD
956 while (uatomic_read(&ht->in_progress_resize))
957 poll(NULL, 0, 100); /* wait for 100ms */
14044b37 958 ret = cds_lfht_delete_dummy(ht);
abc490a1
MD
959 if (ret)
960 return ret;
395270b6 961 free(ht->t);
df44348d 962 free_per_cpu_items_count(ht->percpu_count);
674f7a69 963 free(ht);
5e28c532 964 return ret;
674f7a69
MD
965}
966
14044b37 967void cds_lfht_count_nodes(struct cds_lfht *ht,
273399de
MD
968 unsigned long *count,
969 unsigned long *removed)
970{
971 struct rcu_table *t;
14044b37
MD
972 struct cds_lfht_node *node, *next;
973 struct _cds_lfht_node *lookup;
24365af7 974 unsigned long nr_dummy = 0;
273399de
MD
975
976 *count = 0;
977 *removed = 0;
978
979 t = rcu_dereference(ht->t);
24365af7
MD
980 /* Count non-dummy nodes in the table */
981 lookup = &t->tbl[0][0];
14044b37 982 node = (struct cds_lfht_node *) lookup;
273399de 983 do {
cc4fcb10 984 next = rcu_dereference(node->p.next);
273399de 985 if (is_removed(next)) {
1b81fe1a 986 assert(!is_dummy(next));
273399de 987 (*removed)++;
1b81fe1a 988 } else if (!is_dummy(next))
273399de 989 (*count)++;
24365af7
MD
990 else
991 (nr_dummy)++;
273399de
MD
992 node = clear_flag(next);
993 } while (node);
f0c29ed7 994 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
273399de
MD
995}
996
abc490a1 997static
14044b37 998void cds_lfht_free_table_cb(struct rcu_head *head)
abc490a1
MD
999{
1000 struct rcu_table *t =
1001 caa_container_of(head, struct rcu_table, head);
1002 free(t);
1003}
1004
1005/* called with resize mutex held */
1006static
14044b37 1007void _do_cds_lfht_resize(struct cds_lfht *ht)
464a1ec9 1008{
24365af7 1009 unsigned long new_size, old_size, old_order, new_order;
395270b6 1010 struct rcu_table *new_t, *old_t;
464a1ec9 1011
395270b6
MD
1012 old_t = ht->t;
1013 old_size = old_t->size;
24365af7 1014 old_order = get_count_order_ulong(old_size) + 1;
464a1ec9 1015
f9830efd 1016 new_size = CMM_LOAD_SHARED(old_t->resize_target);
abc490a1 1017 if (old_size == new_size)
464a1ec9 1018 return;
24365af7 1019 new_order = get_count_order_ulong(new_size) + 1;
df44348d 1020 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
df03fab8 1021 old_size, old_order, new_size, new_order);
14044b37
MD
1022 new_t = malloc(sizeof(struct cds_lfht)
1023 + (new_order * sizeof(struct _cds_lfht_node *)));
f000907d
MD
1024 assert(new_size > old_size);
1025 memcpy(&new_t->tbl, &old_t->tbl,
14044b37 1026 old_order * sizeof(struct _cds_lfht_node *));
24365af7 1027 init_table(ht, new_t, old_order, new_order - old_order);
f000907d
MD
1028 /* Changing table and size atomically wrt lookups */
1029 rcu_assign_pointer(ht->t, new_t);
14044b37 1030 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
464a1ec9
MD
1031}
1032
abc490a1 1033static
f9830efd
MD
1034unsigned long resize_target_update(struct rcu_table *t,
1035 int growth_order)
464a1ec9 1036{
f9830efd
MD
1037 return _uatomic_max(&t->resize_target,
1038 t->size << growth_order);
464a1ec9
MD
1039}
1040
3171717f
MD
1041static
1042unsigned long resize_target_update_count(struct rcu_table *t,
1043 unsigned long count)
1044{
1045 return _uatomic_max(&t->resize_target, count);
1046}
1047
14044b37 1048void cds_lfht_resize(struct cds_lfht *ht, int growth)
464a1ec9 1049{
f9830efd
MD
1050 struct rcu_table *t = rcu_dereference(ht->t);
1051 unsigned long target_size;
1052
f0c29ed7
MD
1053 if (growth < 0) {
1054 /*
1055 * Silently refuse to shrink hash table. (not supported)
1056 */
1057 dbg_printf("shrinking hash table not supported.\n");
1058 return;
1059 }
1060
f9830efd
MD
1061 target_size = resize_target_update(t, growth);
1062 if (t->size < target_size) {
11519af6 1063 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 1064 pthread_mutex_lock(&ht->resize_mutex);
14044b37 1065 _do_cds_lfht_resize(ht);
f9830efd
MD
1066 pthread_mutex_unlock(&ht->resize_mutex);
1067 }
abc490a1 1068}
464a1ec9 1069
abc490a1
MD
1070static
1071void do_resize_cb(struct rcu_head *head)
1072{
1073 struct rcu_resize_work *work =
1074 caa_container_of(head, struct rcu_resize_work, head);
14044b37 1075 struct cds_lfht *ht = work->ht;
abc490a1
MD
1076
1077 pthread_mutex_lock(&ht->resize_mutex);
14044b37 1078 _do_cds_lfht_resize(ht);
abc490a1
MD
1079 pthread_mutex_unlock(&ht->resize_mutex);
1080 free(work);
848d4088
MD
1081 cmm_smp_mb(); /* finish resize before decrement */
1082 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
1083}
1084
abc490a1 1085static
14044b37 1086void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth)
ab7d5fc6 1087{
abc490a1 1088 struct rcu_resize_work *work;
f9830efd 1089 unsigned long target_size;
abc490a1 1090
f9830efd 1091 target_size = resize_target_update(t, growth);
11519af6 1092 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
848d4088
MD
1093 uatomic_inc(&ht->in_progress_resize);
1094 cmm_smp_mb(); /* increment resize count before calling it */
f9830efd
MD
1095 work = malloc(sizeof(*work));
1096 work->ht = ht;
14044b37 1097 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
11519af6 1098 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 1099 }
ab7d5fc6 1100}
3171717f
MD
1101
1102static
1103void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
1104 unsigned long count)
1105{
1106 struct rcu_resize_work *work;
1107 unsigned long target_size;
1108
1109 target_size = resize_target_update_count(t, count);
1110 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
1111 uatomic_inc(&ht->in_progress_resize);
1112 cmm_smp_mb(); /* increment resize count before calling it */
1113 work = malloc(sizeof(*work));
1114 work->ht = ht;
1115 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1116 CMM_STORE_SHARED(t->resize_initiated, 1);
1117 }
1118}
This page took 0.077495 seconds and 4 git commands to generate.