rculfhash: document node alignment
[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.
80 * - A RCU "order table" indexed by log2(hash index) is copied and
81 * expanded by the resize operation. This order table allows finding
82 * the "dummy node" tables.
83 * - There is one dummy node table per hash index order. The size of
84 * each dummy node table is half the number of hashes contained in
85 * this order.
86 * - call_rcu is used to garbage-collect the old order table.
87 * - The per-order dummy node tables contain a compact version of the
88 * hash table nodes. These tables are invariant after they are
89 * populated into the hash table.
90 */
91
2ed95849
MD
92#define _LGPL_SOURCE
93#include <stdlib.h>
e0ba718a
MD
94#include <errno.h>
95#include <assert.h>
96#include <stdio.h>
abc490a1 97#include <stdint.h>
f000907d 98#include <string.h>
e0ba718a 99
2ed95849 100#include <urcu.h>
abc490a1 101#include <urcu-call-rcu.h>
a42cc659
MD
102#include <urcu/arch.h>
103#include <urcu/uatomic.h>
674f7a69 104#include <urcu/jhash.h>
a42cc659 105#include <urcu/compiler.h>
abc490a1 106#include <urcu/rculfhash.h>
5e28c532 107#include <stdio.h>
464a1ec9 108#include <pthread.h>
44395fb7 109
f9830efd 110#ifdef DEBUG
e753ff5a 111#define dbg_printf(fmt, args...) printf(fmt, ## args)
f9830efd 112#else
e753ff5a 113#define dbg_printf(fmt, args...)
f9830efd
MD
114#endif
115
01370f0b
MD
116#define CHAIN_LEN_TARGET 4
117#define CHAIN_LEN_RESIZE_THRESHOLD 8
2ed95849 118
abc490a1
MD
119#ifndef max
120#define max(a, b) ((a) > (b) ? (a) : (b))
121#endif
2ed95849 122
d95bd160
MD
123/*
124 * The removed flag needs to be updated atomically with the pointer.
125 * The dummy flag does not require to be updated atomically with the
126 * pointer, but it is added as a pointer low bit flag to save space.
127 */
d37166c6 128#define REMOVED_FLAG (1UL << 0)
f5596c94
MD
129#define DUMMY_FLAG (1UL << 1)
130#define FLAGS_MASK ((1UL << 2) - 1)
d37166c6 131
395270b6 132struct rcu_table {
abc490a1 133 unsigned long size; /* always a power of 2 */
f9830efd 134 unsigned long resize_target;
11519af6 135 int resize_initiated;
abc490a1 136 struct rcu_head head;
24365af7 137 struct _rcu_ht_node *tbl[0];
395270b6
MD
138};
139
2ed95849 140struct rcu_ht {
395270b6 141 struct rcu_table *t; /* shared */
2ed95849 142 ht_hash_fct hash_fct;
732ad076
MD
143 ht_compare_fct compare_fct;
144 unsigned long hash_seed;
464a1ec9 145 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
33c7c748 146 unsigned int in_progress_resize, in_progress_destroy;
abc490a1
MD
147 void (*ht_call_rcu)(struct rcu_head *head,
148 void (*func)(struct rcu_head *head));
2ed95849
MD
149};
150
abc490a1
MD
151struct rcu_resize_work {
152 struct rcu_head head;
2ed95849 153 struct rcu_ht *ht;
abc490a1 154};
2ed95849 155
abc490a1
MD
156/*
157 * Algorithm to reverse bits in a word by lookup table, extended to
158 * 64-bit words.
f9830efd 159 * Source:
abc490a1 160 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
f9830efd 161 * Originally from Public Domain.
abc490a1
MD
162 */
163
164static const uint8_t BitReverseTable256[256] =
2ed95849 165{
abc490a1
MD
166#define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
167#define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
168#define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
169 R6(0), R6(2), R6(1), R6(3)
170};
171#undef R2
172#undef R4
173#undef R6
2ed95849 174
abc490a1
MD
175static
176uint8_t bit_reverse_u8(uint8_t v)
177{
178 return BitReverseTable256[v];
179}
ab7d5fc6 180
abc490a1
MD
181static __attribute__((unused))
182uint32_t bit_reverse_u32(uint32_t v)
183{
184 return ((uint32_t) bit_reverse_u8(v) << 24) |
185 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
186 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
187 ((uint32_t) bit_reverse_u8(v >> 24));
2ed95849
MD
188}
189
abc490a1
MD
190static __attribute__((unused))
191uint64_t bit_reverse_u64(uint64_t v)
2ed95849 192{
abc490a1
MD
193 return ((uint64_t) bit_reverse_u8(v) << 56) |
194 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
195 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
196 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
197 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
198 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
199 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
200 ((uint64_t) bit_reverse_u8(v >> 56));
201}
202
203static
204unsigned long bit_reverse_ulong(unsigned long v)
205{
206#if (CAA_BITS_PER_LONG == 32)
207 return bit_reverse_u32(v);
208#else
209 return bit_reverse_u64(v);
210#endif
211}
212
f9830efd 213/*
24365af7
MD
214 * fls: returns the position of the most significant bit.
215 * Returns 0 if no bit is set, else returns the position of the most
216 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
f9830efd 217 */
24365af7
MD
218#if defined(__i386) || defined(__x86_64)
219static inline
220unsigned int fls_u32(uint32_t x)
f9830efd 221{
24365af7
MD
222 int r;
223
224 asm("bsrl %1,%0\n\t"
225 "jnz 1f\n\t"
226 "movl $-1,%0\n\t"
227 "1:\n\t"
228 : "=r" (r) : "rm" (x));
229 return r + 1;
230}
231#define HAS_FLS_U32
232#endif
233
234#if defined(__x86_64)
235static inline
236unsigned int fls_u64(uint64_t x)
237{
238 long r;
239
240 asm("bsrq %1,%0\n\t"
241 "jnz 1f\n\t"
242 "movq $-1,%0\n\t"
243 "1:\n\t"
244 : "=r" (r) : "rm" (x));
245 return r + 1;
246}
247#define HAS_FLS_U64
248#endif
249
250#ifndef HAS_FLS_U64
251static __attribute__((unused))
252unsigned int fls_u64(uint64_t x)
253{
254 unsigned int r = 64;
255
256 if (!x)
257 return 0;
258
259 if (!(x & 0xFFFFFFFF00000000ULL)) {
260 x <<= 32;
261 r -= 32;
262 }
263 if (!(x & 0xFFFF000000000000ULL)) {
264 x <<= 16;
265 r -= 16;
266 }
267 if (!(x & 0xFF00000000000000ULL)) {
268 x <<= 8;
269 r -= 8;
270 }
271 if (!(x & 0xF000000000000000ULL)) {
272 x <<= 4;
273 r -= 4;
274 }
275 if (!(x & 0xC000000000000000ULL)) {
276 x <<= 2;
277 r -= 2;
278 }
279 if (!(x & 0x8000000000000000ULL)) {
280 x <<= 1;
281 r -= 1;
282 }
283 return r;
284}
285#endif
286
287#ifndef HAS_FLS_U32
288static __attribute__((unused))
289unsigned int fls_u32(uint32_t x)
290{
291 unsigned int r = 32;
f9830efd 292
24365af7
MD
293 if (!x)
294 return 0;
295 if (!(x & 0xFFFF0000U)) {
296 x <<= 16;
297 r -= 16;
298 }
299 if (!(x & 0xFF000000U)) {
300 x <<= 8;
301 r -= 8;
302 }
303 if (!(x & 0xF0000000U)) {
304 x <<= 4;
305 r -= 4;
306 }
307 if (!(x & 0xC0000000U)) {
308 x <<= 2;
309 r -= 2;
310 }
311 if (!(x & 0x80000000U)) {
312 x <<= 1;
313 r -= 1;
314 }
315 return r;
316}
317#endif
318
319unsigned int fls_ulong(unsigned long x)
f9830efd 320{
24365af7
MD
321#if (CAA_BITS_PER_lONG == 32)
322 return fls_u32(x);
323#else
324 return fls_u64(x);
325#endif
326}
f9830efd 327
24365af7
MD
328int get_count_order_u32(uint32_t x)
329{
330 int order;
331
332 order = fls_u32(x) - 1;
333 if (x & (x - 1))
334 order++;
335 return order;
336}
337
338int get_count_order_ulong(unsigned long x)
339{
340 int order;
341
342 order = fls_ulong(x) - 1;
343 if (x & (x - 1))
344 order++;
345 return order;
f9830efd
MD
346}
347
348static
349void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth);
350
351static
352void check_resize(struct rcu_ht *ht, struct rcu_table *t,
353 uint32_t chain_len)
354{
24365af7
MD
355 if (chain_len > 100)
356 dbg_printf("rculfhash: WARNING: large chain length: %u.\n",
357 chain_len);
3390d470
MD
358 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
359 ht_resize_lazy(ht, t,
01370f0b 360 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
f9830efd
MD
361}
362
abc490a1
MD
363static
364struct rcu_ht_node *clear_flag(struct rcu_ht_node *node)
365{
d37166c6 366 return (struct rcu_ht_node *) (((unsigned long) node) & ~FLAGS_MASK);
abc490a1
MD
367}
368
369static
370int is_removed(struct rcu_ht_node *node)
371{
d37166c6 372 return ((unsigned long) node) & REMOVED_FLAG;
abc490a1
MD
373}
374
375static
376struct rcu_ht_node *flag_removed(struct rcu_ht_node *node)
377{
d37166c6 378 return (struct rcu_ht_node *) (((unsigned long) node) | REMOVED_FLAG);
abc490a1
MD
379}
380
f5596c94
MD
381static
382int is_dummy(struct rcu_ht_node *node)
383{
384 return ((unsigned long) node) & DUMMY_FLAG;
385}
386
387static
388struct rcu_ht_node *flag_dummy(struct rcu_ht_node *node)
389{
390 return (struct rcu_ht_node *) (((unsigned long) node) | DUMMY_FLAG);
391}
392
abc490a1 393static
f9830efd 394unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
abc490a1
MD
395{
396 unsigned long old1, old2;
397
398 old1 = uatomic_read(ptr);
399 do {
400 old2 = old1;
401 if (old2 >= v)
f9830efd 402 return old2;
abc490a1 403 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
f9830efd 404 return v;
abc490a1
MD
405}
406
273399de
MD
407/*
408 * Remove all logically deleted nodes from a bucket up to a certain node key.
409 */
410static
411void _ht_gc_bucket(struct rcu_ht_node *dummy, struct rcu_ht_node *node)
412{
f5596c94 413 struct rcu_ht_node *iter_prev, *iter, *next, *new_next;
273399de
MD
414
415 for (;;) {
416 iter_prev = dummy;
417 /* We can always skip the dummy node initially */
cc4fcb10
MD
418 iter = rcu_dereference(iter_prev->p.next);
419 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
273399de 420 for (;;) {
a2974903 421 if (unlikely(!clear_flag(iter)))
479c8a32 422 return;
cc4fcb10 423 if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash)
273399de 424 return;
cc4fcb10 425 next = rcu_dereference(clear_flag(iter)->p.next);
273399de
MD
426 if (is_removed(next))
427 break;
b453eae1 428 iter_prev = clear_flag(iter);
273399de
MD
429 iter = next;
430 }
431 assert(!is_removed(iter));
f5596c94
MD
432 if (is_dummy(iter))
433 new_next = flag_dummy(clear_flag(next));
434 else
435 new_next = clear_flag(next);
436 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de
MD
437 }
438}
439
abc490a1 440static
18117871 441struct rcu_ht_node *_ht_add(struct rcu_ht *ht, struct rcu_table *t,
f5596c94 442 struct rcu_ht_node *node, int unique, int dummy)
abc490a1 443{
f5596c94
MD
444 struct rcu_ht_node *iter_prev, *iter, *next, *new_node, *new_next,
445 *dummy_node;
24365af7
MD
446 struct _rcu_ht_node *lookup;
447 unsigned long hash, index, order;
abc490a1 448
18117871 449 if (!t->size) {
f5596c94
MD
450 assert(dummy);
451 node->p.next = flag_dummy(NULL);
18117871
MD
452 return node; /* Initial first add (head) */
453 }
cc4fcb10 454 hash = bit_reverse_ulong(node->p.reverse_hash);
abc490a1 455 for (;;) {
f9830efd 456 uint32_t chain_len = 0;
abc490a1 457
11519af6
MD
458 /*
459 * iter_prev points to the non-removed node prior to the
460 * insert location.
11519af6 461 */
24365af7
MD
462 index = hash & (t->size - 1);
463 order = get_count_order_ulong(index + 1);
464 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
465 iter_prev = (struct rcu_ht_node *) lookup;
11519af6 466 /* We can always skip the dummy node initially */
cc4fcb10
MD
467 iter = rcu_dereference(iter_prev->p.next);
468 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
abc490a1 469 for (;;) {
a2974903 470 if (unlikely(!clear_flag(iter)))
273399de 471 goto insert;
cc4fcb10 472 if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash)
273399de 473 goto insert;
cc4fcb10 474 next = rcu_dereference(clear_flag(iter)->p.next);
273399de 475 if (is_removed(next))
9dba85be 476 goto gc_node;
e43f23f8 477 if (unique
1b81fe1a 478 && !is_dummy(next)
e43f23f8
MD
479 && !ht->compare_fct(node->key, node->key_len,
480 clear_flag(iter)->key,
481 clear_flag(iter)->key_len))
18117871 482 return clear_flag(iter);
11519af6 483 /* Only account for identical reverse hash once */
24365af7
MD
484 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
485 && !is_dummy(next))
11519af6
MD
486 check_resize(ht, t, ++chain_len);
487 iter_prev = clear_flag(iter);
273399de 488 iter = next;
abc490a1 489 }
273399de 490 insert:
7ec59d3b 491 assert(node != clear_flag(iter));
11519af6 492 assert(!is_removed(iter_prev));
f000907d 493 assert(iter_prev != node);
f5596c94 494 if (!dummy)
1b81fe1a 495 node->p.next = clear_flag(iter);
f5596c94 496 else
1b81fe1a 497 node->p.next = flag_dummy(clear_flag(iter));
f5596c94
MD
498 if (is_dummy(iter))
499 new_node = flag_dummy(node);
500 else
501 new_node = node;
cc4fcb10 502 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
f5596c94 503 new_node) != iter)
273399de 504 continue; /* retry */
11519af6 505 else
273399de 506 goto gc_end;
9dba85be
MD
507 gc_node:
508 assert(!is_removed(iter));
f5596c94
MD
509 if (is_dummy(iter))
510 new_next = flag_dummy(clear_flag(next));
511 else
512 new_next = clear_flag(next);
513 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
273399de 514 /* retry */
464a1ec9 515 }
273399de
MD
516gc_end:
517 /* Garbage collect logically removed nodes in the bucket */
24365af7
MD
518 index = hash & (t->size - 1);
519 order = get_count_order_ulong(index + 1);
520 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
521 dummy_node = (struct rcu_ht_node *) lookup;
f5596c94 522 _ht_gc_bucket(dummy_node, node);
18117871 523 return node;
abc490a1 524}
464a1ec9 525
abc490a1
MD
526static
527int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node)
528{
273399de 529 struct rcu_ht_node *dummy, *next, *old;
24365af7 530 struct _rcu_ht_node *lookup;
abc490a1 531 int flagged = 0;
24365af7 532 unsigned long hash, index, order;
5e28c532 533
7ec59d3b 534 /* logically delete the node */
cc4fcb10 535 old = rcu_dereference(node->p.next);
7ec59d3b
MD
536 do {
537 next = old;
538 if (is_removed(next))
539 goto end;
1b81fe1a 540 assert(!is_dummy(next));
cc4fcb10 541 old = uatomic_cmpxchg(&node->p.next, next,
7ec59d3b
MD
542 flag_removed(next));
543 } while (old != next);
544
545 /* We performed the (logical) deletion. */
546 flagged = 1;
547
548 /*
549 * Ensure that the node is not visible to readers anymore: lookup for
273399de
MD
550 * the node, and remove it (along with any other logically removed node)
551 * if found.
11519af6 552 */
cc4fcb10 553 hash = bit_reverse_ulong(node->p.reverse_hash);
24365af7
MD
554 index = hash & (t->size - 1);
555 order = get_count_order_ulong(index + 1);
556 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
557 dummy = (struct rcu_ht_node *) lookup;
273399de 558 _ht_gc_bucket(dummy, node);
2ed95849 559end:
11519af6
MD
560 /*
561 * Only the flagging action indicated that we (and no other)
562 * removed the node from the hash.
563 */
7ec59d3b 564 if (flagged) {
cc4fcb10 565 assert(is_removed(rcu_dereference(node->p.next)));
11519af6 566 return 0;
7ec59d3b 567 } else
11519af6 568 return -ENOENT;
abc490a1 569}
2ed95849 570
abc490a1
MD
571static
572void init_table(struct rcu_ht *ht, struct rcu_table *t,
24365af7
MD
573 unsigned long first_order, unsigned long len_order)
574{
575 unsigned long i, end_order;
576
577 dbg_printf("rculfhash: init table: first_order %lu end_order %lu\n",
578 first_order, first_order + len_order);
579 end_order = first_order + len_order;
580 t->size = !first_order ? 0 : (1UL << (first_order - 1));
581 for (i = first_order; i < end_order; i++) {
582 unsigned long j, len;
583
584 len = !i ? 1 : 1UL << (i - 1);
585 dbg_printf("rculfhash: init order %lu len: %lu\n", i, len);
586 t->tbl[i] = calloc(len, sizeof(struct _rcu_ht_node));
587 for (j = 0; j < len; j++) {
588 dbg_printf("rculfhash: init entry: i %lu j %lu hash %lu\n",
589 i, j, !i ? 0 : (1UL << (i - 1)) + j);
590 struct rcu_ht_node *new_node =
591 (struct rcu_ht_node *) &t->tbl[i][j];
592 new_node->p.reverse_hash =
593 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
594 (void) _ht_add(ht, t, new_node, 0, 1);
33c7c748
MD
595 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
596 break;
24365af7
MD
597 }
598 /* Update table size */
599 t->size = !i ? 1 : (1UL << i);
600 dbg_printf("rculfhash: init new size: %lu\n", t->size);
33c7c748
MD
601 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
602 break;
abc490a1 603 }
24365af7 604 t->resize_target = t->size;
11519af6 605 t->resize_initiated = 0;
2ed95849
MD
606}
607
abc490a1 608struct rcu_ht *ht_new(ht_hash_fct hash_fct,
732ad076
MD
609 ht_compare_fct compare_fct,
610 unsigned long hash_seed,
abc490a1
MD
611 unsigned long init_size,
612 void (*ht_call_rcu)(struct rcu_head *head,
613 void (*func)(struct rcu_head *head)))
614{
615 struct rcu_ht *ht;
24365af7 616 unsigned long order;
abc490a1
MD
617
618 ht = calloc(1, sizeof(struct rcu_ht));
619 ht->hash_fct = hash_fct;
732ad076
MD
620 ht->compare_fct = compare_fct;
621 ht->hash_seed = hash_seed;
f000907d 622 ht->ht_call_rcu = ht_call_rcu;
848d4088 623 ht->in_progress_resize = 0;
abc490a1
MD
624 /* this mutex should not nest in read-side C.S. */
625 pthread_mutex_init(&ht->resize_mutex, NULL);
24365af7 626 order = get_count_order_ulong(max(init_size, 1)) + 1;
abc490a1 627 ht->t = calloc(1, sizeof(struct rcu_table)
24365af7 628 + (order * sizeof(struct _rcu_ht_node *)));
abc490a1 629 ht->t->size = 0;
f000907d 630 pthread_mutex_lock(&ht->resize_mutex);
24365af7 631 init_table(ht, ht->t, 0, order);
f000907d 632 pthread_mutex_unlock(&ht->resize_mutex);
abc490a1
MD
633 return ht;
634}
635
732ad076 636struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len)
2ed95849 637{
395270b6 638 struct rcu_table *t;
1b81fe1a 639 struct rcu_ht_node *node, *next;
24365af7
MD
640 struct _rcu_ht_node *lookup;
641 unsigned long hash, reverse_hash, index, order;
2ed95849 642
732ad076 643 hash = ht->hash_fct(key, key_len, ht->hash_seed);
abc490a1 644 reverse_hash = bit_reverse_ulong(hash);
464a1ec9 645
395270b6 646 t = rcu_dereference(ht->t);
24365af7
MD
647 index = hash & (t->size - 1);
648 order = get_count_order_ulong(index + 1);
649 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
650 dbg_printf("rculfhash: lookup hash %lu index %lu order %lu aridx %lu\n",
651 hash, index, order, index & ((1UL << (order - 1)) - 1));
652 node = (struct rcu_ht_node *) lookup;
2ed95849 653 for (;;) {
abc490a1
MD
654 if (unlikely(!node))
655 break;
cc4fcb10 656 if (unlikely(node->p.reverse_hash > reverse_hash)) {
abc490a1
MD
657 node = NULL;
658 break;
2ed95849 659 }
1b81fe1a
MD
660 next = rcu_dereference(node->p.next);
661 if (likely(!is_removed(next))
662 && !is_dummy(next)
49c2e2d6 663 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
273399de 664 break;
2ed95849 665 }
1b81fe1a 666 node = clear_flag(next);
2ed95849 667 }
1b81fe1a 668 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
abc490a1
MD
669 return node;
670}
e0ba718a 671
f000907d 672void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node)
abc490a1
MD
673{
674 struct rcu_table *t;
49c2e2d6 675 unsigned long hash;
ab7d5fc6 676
49c2e2d6 677 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 678 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
2ed95849 679
abc490a1 680 t = rcu_dereference(ht->t);
f5596c94 681 (void) _ht_add(ht, t, node, 0, 0);
3eca1b8c
MD
682}
683
18117871 684struct rcu_ht_node *ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node)
3eca1b8c
MD
685{
686 struct rcu_table *t;
49c2e2d6 687 unsigned long hash;
3eca1b8c 688
49c2e2d6 689 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
cc4fcb10 690 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
3eca1b8c
MD
691
692 t = rcu_dereference(ht->t);
f5596c94 693 return _ht_add(ht, t, node, 1, 0);
2ed95849
MD
694}
695
abc490a1 696int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node)
2ed95849 697{
abc490a1
MD
698 struct rcu_table *t;
699
700 t = rcu_dereference(ht->t);
abc490a1 701 return _ht_remove(ht, t, node);
2ed95849 702}
ab7d5fc6 703
abc490a1
MD
704static
705int ht_delete_dummy(struct rcu_ht *ht)
674f7a69 706{
395270b6 707 struct rcu_table *t;
abc490a1 708 struct rcu_ht_node *node;
24365af7
MD
709 struct _rcu_ht_node *lookup;
710 unsigned long order, i;
674f7a69 711
abc490a1
MD
712 t = ht->t;
713 /* Check that the table is empty */
24365af7
MD
714 lookup = &t->tbl[0][0];
715 node = (struct rcu_ht_node *) lookup;
abc490a1 716 do {
1b81fe1a
MD
717 node = clear_flag(node)->p.next;
718 if (!is_dummy(node))
abc490a1 719 return -EPERM;
273399de 720 assert(!is_removed(node));
a2974903 721 } while (clear_flag(node));
abc490a1 722 /* Internal sanity check: all nodes left should be dummy */
24365af7
MD
723 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
724 unsigned long len;
725
726 len = !order ? 1 : 1UL << (order - 1);
727 for (i = 0; i < len; i++) {
728 dbg_printf("rculfhash: delete order %lu i %lu hash %lu\n",
729 order, i,
730 bit_reverse_ulong(t->tbl[order][i].reverse_hash));
731 assert(is_dummy(t->tbl[order][i].next));
732 }
733 free(t->tbl[order]);
674f7a69 734 }
abc490a1 735 return 0;
674f7a69
MD
736}
737
738/*
739 * Should only be called when no more concurrent readers nor writers can
740 * possibly access the table.
741 */
5e28c532 742int ht_destroy(struct rcu_ht *ht)
674f7a69 743{
5e28c532
MD
744 int ret;
745
848d4088 746 /* Wait for in-flight resize operations to complete */
33c7c748 747 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
848d4088
MD
748 while (uatomic_read(&ht->in_progress_resize))
749 poll(NULL, 0, 100); /* wait for 100ms */
abc490a1
MD
750 ret = ht_delete_dummy(ht);
751 if (ret)
752 return ret;
395270b6 753 free(ht->t);
674f7a69 754 free(ht);
5e28c532 755 return ret;
674f7a69
MD
756}
757
273399de
MD
758void ht_count_nodes(struct rcu_ht *ht,
759 unsigned long *count,
760 unsigned long *removed)
761{
762 struct rcu_table *t;
763 struct rcu_ht_node *node, *next;
24365af7
MD
764 struct _rcu_ht_node *lookup;
765 unsigned long nr_dummy = 0;
273399de
MD
766
767 *count = 0;
768 *removed = 0;
769
770 t = rcu_dereference(ht->t);
24365af7
MD
771 /* Count non-dummy nodes in the table */
772 lookup = &t->tbl[0][0];
773 node = (struct rcu_ht_node *) lookup;
273399de 774 do {
cc4fcb10 775 next = rcu_dereference(node->p.next);
273399de 776 if (is_removed(next)) {
1b81fe1a 777 assert(!is_dummy(next));
273399de 778 (*removed)++;
1b81fe1a 779 } else if (!is_dummy(next))
273399de 780 (*count)++;
24365af7
MD
781 else
782 (nr_dummy)++;
273399de
MD
783 node = clear_flag(next);
784 } while (node);
24365af7 785 dbg_printf("rculfhash: number of dummy nodes: %lu\n", nr_dummy);
273399de
MD
786}
787
abc490a1
MD
788static
789void ht_free_table_cb(struct rcu_head *head)
790{
791 struct rcu_table *t =
792 caa_container_of(head, struct rcu_table, head);
793 free(t);
794}
795
796/* called with resize mutex held */
797static
798void _do_ht_resize(struct rcu_ht *ht)
464a1ec9 799{
24365af7 800 unsigned long new_size, old_size, old_order, new_order;
395270b6 801 struct rcu_table *new_t, *old_t;
464a1ec9 802
395270b6
MD
803 old_t = ht->t;
804 old_size = old_t->size;
24365af7 805 old_order = get_count_order_ulong(old_size) + 1;
464a1ec9 806
f9830efd 807 new_size = CMM_LOAD_SHARED(old_t->resize_target);
abc490a1 808 if (old_size == new_size)
464a1ec9 809 return;
24365af7 810 new_order = get_count_order_ulong(new_size) + 1;
df03fab8
MD
811 printf("rculfhash: resize from %lu (order %lu) to %lu (order %lu) buckets\n",
812 old_size, old_order, new_size, new_order);
f000907d 813 new_t = malloc(sizeof(struct rcu_table)
24365af7 814 + (new_order * sizeof(struct _rcu_ht_node *)));
f000907d
MD
815 assert(new_size > old_size);
816 memcpy(&new_t->tbl, &old_t->tbl,
24365af7
MD
817 old_order * sizeof(struct _rcu_ht_node *));
818 init_table(ht, new_t, old_order, new_order - old_order);
f000907d
MD
819 /* Changing table and size atomically wrt lookups */
820 rcu_assign_pointer(ht->t, new_t);
821 ht->ht_call_rcu(&old_t->head, ht_free_table_cb);
464a1ec9
MD
822}
823
abc490a1 824static
f9830efd
MD
825unsigned long resize_target_update(struct rcu_table *t,
826 int growth_order)
464a1ec9 827{
f9830efd
MD
828 return _uatomic_max(&t->resize_target,
829 t->size << growth_order);
464a1ec9
MD
830}
831
464a1ec9
MD
832void ht_resize(struct rcu_ht *ht, int growth)
833{
f9830efd
MD
834 struct rcu_table *t = rcu_dereference(ht->t);
835 unsigned long target_size;
836
837 target_size = resize_target_update(t, growth);
838 if (t->size < target_size) {
11519af6 839 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd
MD
840 pthread_mutex_lock(&ht->resize_mutex);
841 _do_ht_resize(ht);
842 pthread_mutex_unlock(&ht->resize_mutex);
843 }
abc490a1 844}
464a1ec9 845
abc490a1
MD
846static
847void do_resize_cb(struct rcu_head *head)
848{
849 struct rcu_resize_work *work =
850 caa_container_of(head, struct rcu_resize_work, head);
851 struct rcu_ht *ht = work->ht;
852
853 pthread_mutex_lock(&ht->resize_mutex);
854 _do_ht_resize(ht);
855 pthread_mutex_unlock(&ht->resize_mutex);
856 free(work);
848d4088
MD
857 cmm_smp_mb(); /* finish resize before decrement */
858 uatomic_dec(&ht->in_progress_resize);
464a1ec9
MD
859}
860
abc490a1 861static
f000907d 862void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth)
ab7d5fc6 863{
abc490a1 864 struct rcu_resize_work *work;
f9830efd 865 unsigned long target_size;
abc490a1 866
f9830efd 867 target_size = resize_target_update(t, growth);
11519af6 868 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
848d4088
MD
869 uatomic_inc(&ht->in_progress_resize);
870 cmm_smp_mb(); /* increment resize count before calling it */
f9830efd
MD
871 work = malloc(sizeof(*work));
872 work->ht = ht;
873 ht->ht_call_rcu(&work->head, do_resize_cb);
11519af6 874 CMM_STORE_SHARED(t->resize_initiated, 1);
f9830efd 875 }
ab7d5fc6 876}
This page took 0.065872 seconds and 4 git commands to generate.