rculfhash: tweak resize thresholds
[urcu.git] / rculfhash.c
1 /*
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
21 */
22
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 * - 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.
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
97 #define _LGPL_SOURCE
98 #include <stdlib.h>
99 #include <errno.h>
100 #include <assert.h>
101 #include <stdio.h>
102 #include <stdint.h>
103 #include <string.h>
104
105 #include "config.h"
106 #include <urcu.h>
107 #include <urcu-call-rcu.h>
108 #include <urcu/arch.h>
109 #include <urcu/uatomic.h>
110 #include <urcu/jhash.h>
111 #include <urcu/compiler.h>
112 #include <urcu/rculfhash.h>
113 #include <stdio.h>
114 #include <pthread.h>
115
116 #ifdef DEBUG
117 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
118 #else
119 #define dbg_printf(fmt, args...)
120 #endif
121
122 /*
123 * Per-CPU split-counters lazily update the global counter each 1024
124 * addition/removal. It automatically keeps track of resize required.
125 * We use the bucket length as indicator for need to expand for small
126 * tables and machines lacking per-cpu data suppport.
127 */
128 #define COUNT_COMMIT_ORDER 10
129 #define CHAIN_LEN_TARGET 1
130 #define CHAIN_LEN_RESIZE_THRESHOLD 3
131
132 #ifndef max
133 #define max(a, b) ((a) > (b) ? (a) : (b))
134 #endif
135
136 /*
137 * The removed flag needs to be updated atomically with the pointer.
138 * The dummy flag does not require to be updated atomically with the
139 * pointer, but it is added as a pointer low bit flag to save space.
140 */
141 #define REMOVED_FLAG (1UL << 0)
142 #define DUMMY_FLAG (1UL << 1)
143 #define FLAGS_MASK ((1UL << 2) - 1)
144
145 struct ht_items_count {
146 unsigned long add, remove;
147 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
148
149 struct rcu_table {
150 unsigned long size; /* always a power of 2 */
151 unsigned long resize_target;
152 int resize_initiated;
153 struct rcu_head head;
154 struct _cds_lfht_node *tbl[0];
155 };
156
157 struct cds_lfht {
158 struct rcu_table *t; /* shared */
159 cds_lfht_hash_fct hash_fct;
160 cds_lfht_compare_fct compare_fct;
161 unsigned long hash_seed;
162 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
163 unsigned int in_progress_resize, in_progress_destroy;
164 void (*cds_lfht_call_rcu)(struct rcu_head *head,
165 void (*func)(struct rcu_head *head));
166 unsigned long count; /* global approximate item count */
167 struct ht_items_count *percpu_count; /* per-cpu item count */
168 };
169
170 struct rcu_resize_work {
171 struct rcu_head head;
172 struct cds_lfht *ht;
173 };
174
175 /*
176 * Algorithm to reverse bits in a word by lookup table, extended to
177 * 64-bit words.
178 * Source:
179 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
180 * Originally from Public Domain.
181 */
182
183 static const uint8_t BitReverseTable256[256] =
184 {
185 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
186 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
187 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
188 R6(0), R6(2), R6(1), R6(3)
189 };
190 #undef R2
191 #undef R4
192 #undef R6
193
194 static
195 uint8_t bit_reverse_u8(uint8_t v)
196 {
197 return BitReverseTable256[v];
198 }
199
200 static __attribute__((unused))
201 uint32_t bit_reverse_u32(uint32_t v)
202 {
203 return ((uint32_t) bit_reverse_u8(v) << 24) |
204 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
205 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
206 ((uint32_t) bit_reverse_u8(v >> 24));
207 }
208
209 static __attribute__((unused))
210 uint64_t bit_reverse_u64(uint64_t v)
211 {
212 return ((uint64_t) bit_reverse_u8(v) << 56) |
213 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
214 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
215 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
216 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
217 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
218 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
219 ((uint64_t) bit_reverse_u8(v >> 56));
220 }
221
222 static
223 unsigned long bit_reverse_ulong(unsigned long v)
224 {
225 #if (CAA_BITS_PER_LONG == 32)
226 return bit_reverse_u32(v);
227 #else
228 return bit_reverse_u64(v);
229 #endif
230 }
231
232 /*
233 * fls: returns the position of the most significant bit.
234 * Returns 0 if no bit is set, else returns the position of the most
235 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
236 */
237 #if defined(__i386) || defined(__x86_64)
238 static inline
239 unsigned int fls_u32(uint32_t x)
240 {
241 int r;
242
243 asm("bsrl %1,%0\n\t"
244 "jnz 1f\n\t"
245 "movl $-1,%0\n\t"
246 "1:\n\t"
247 : "=r" (r) : "rm" (x));
248 return r + 1;
249 }
250 #define HAS_FLS_U32
251 #endif
252
253 #if defined(__x86_64)
254 static inline
255 unsigned int fls_u64(uint64_t x)
256 {
257 long r;
258
259 asm("bsrq %1,%0\n\t"
260 "jnz 1f\n\t"
261 "movq $-1,%0\n\t"
262 "1:\n\t"
263 : "=r" (r) : "rm" (x));
264 return r + 1;
265 }
266 #define HAS_FLS_U64
267 #endif
268
269 #ifndef HAS_FLS_U64
270 static __attribute__((unused))
271 unsigned int fls_u64(uint64_t x)
272 {
273 unsigned int r = 64;
274
275 if (!x)
276 return 0;
277
278 if (!(x & 0xFFFFFFFF00000000ULL)) {
279 x <<= 32;
280 r -= 32;
281 }
282 if (!(x & 0xFFFF000000000000ULL)) {
283 x <<= 16;
284 r -= 16;
285 }
286 if (!(x & 0xFF00000000000000ULL)) {
287 x <<= 8;
288 r -= 8;
289 }
290 if (!(x & 0xF000000000000000ULL)) {
291 x <<= 4;
292 r -= 4;
293 }
294 if (!(x & 0xC000000000000000ULL)) {
295 x <<= 2;
296 r -= 2;
297 }
298 if (!(x & 0x8000000000000000ULL)) {
299 x <<= 1;
300 r -= 1;
301 }
302 return r;
303 }
304 #endif
305
306 #ifndef HAS_FLS_U32
307 static __attribute__((unused))
308 unsigned int fls_u32(uint32_t x)
309 {
310 unsigned int r = 32;
311
312 if (!x)
313 return 0;
314 if (!(x & 0xFFFF0000U)) {
315 x <<= 16;
316 r -= 16;
317 }
318 if (!(x & 0xFF000000U)) {
319 x <<= 8;
320 r -= 8;
321 }
322 if (!(x & 0xF0000000U)) {
323 x <<= 4;
324 r -= 4;
325 }
326 if (!(x & 0xC0000000U)) {
327 x <<= 2;
328 r -= 2;
329 }
330 if (!(x & 0x80000000U)) {
331 x <<= 1;
332 r -= 1;
333 }
334 return r;
335 }
336 #endif
337
338 unsigned int fls_ulong(unsigned long x)
339 {
340 #if (CAA_BITS_PER_lONG == 32)
341 return fls_u32(x);
342 #else
343 return fls_u64(x);
344 #endif
345 }
346
347 int get_count_order_u32(uint32_t x)
348 {
349 int order;
350
351 order = fls_u32(x) - 1;
352 if (x & (x - 1))
353 order++;
354 return order;
355 }
356
357 int get_count_order_ulong(unsigned long x)
358 {
359 int order;
360
361 order = fls_ulong(x) - 1;
362 if (x & (x - 1))
363 order++;
364 return order;
365 }
366
367 static
368 void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth);
369
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 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
377
378 static
379 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
380 unsigned long count);
381
382 static long nr_cpus_mask = -1;
383
384 static
385 struct ht_items_count *alloc_per_cpu_items_count(void)
386 {
387 struct ht_items_count *count;
388
389 switch (nr_cpus_mask) {
390 case -2:
391 return NULL;
392 case -1:
393 {
394 long maxcpus;
395
396 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
397 if (maxcpus <= 0) {
398 nr_cpus_mask = -2;
399 return NULL;
400 }
401 /*
402 * round up number of CPUs to next power of two, so we
403 * can use & for modulo.
404 */
405 maxcpus = 1UL << get_count_order_ulong(maxcpus);
406 nr_cpus_mask = maxcpus - 1;
407 }
408 /* Fall-through */
409 default:
410 return calloc(nr_cpus_mask + 1, sizeof(*count));
411 }
412 }
413
414 static
415 void free_per_cpu_items_count(struct ht_items_count *count)
416 {
417 free(count);
418 }
419
420 static
421 int ht_get_cpu(void)
422 {
423 int cpu;
424
425 assert(nr_cpus_mask >= 0);
426 cpu = sched_getcpu();
427 if (unlikely(cpu < 0))
428 return cpu;
429 else
430 return cpu & nr_cpus_mask;
431 }
432
433 static
434 void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
435 {
436 unsigned long percpu_count;
437 int cpu;
438
439 if (unlikely(!ht->percpu_count))
440 return;
441 cpu = ht_get_cpu();
442 if (unlikely(cpu < 0))
443 return;
444 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].add, 1);
445 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
446 unsigned long count;
447
448 dbg_printf("add percpu %lu\n", percpu_count);
449 count = uatomic_add_return(&ht->count,
450 1UL << COUNT_COMMIT_ORDER);
451 /* If power of 2 */
452 if (!(count & (count - 1))) {
453 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD)
454 < t->size)
455 return;
456 dbg_printf("add set global %lu\n", count);
457 cds_lfht_resize_lazy_count(ht, t,
458 count >> (CHAIN_LEN_TARGET - 1));
459 }
460 }
461 }
462
463 static
464 void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
465 {
466 unsigned long percpu_count;
467 int cpu;
468
469 if (unlikely(!ht->percpu_count))
470 return;
471 cpu = ht_get_cpu();
472 if (unlikely(cpu < 0))
473 return;
474 percpu_count = uatomic_add_return(&ht->percpu_count[cpu].remove, -1);
475 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
476 unsigned long count;
477
478 dbg_printf("remove percpu %lu\n", percpu_count);
479 count = uatomic_add_return(&ht->count,
480 -(1UL << COUNT_COMMIT_ORDER));
481 /* If power of 2 */
482 if (!(count & (count - 1))) {
483 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD)
484 >= t->size)
485 return;
486 dbg_printf("remove set global %lu\n", count);
487 cds_lfht_resize_lazy_count(ht, t,
488 count >> (CHAIN_LEN_TARGET - 1));
489 }
490 }
491 }
492
493 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
494
495 static const long nr_cpus_mask = -1;
496
497 static
498 struct ht_items_count *alloc_per_cpu_items_count(void)
499 {
500 return NULL;
501 }
502
503 static
504 void free_per_cpu_items_count(struct ht_items_count *count)
505 {
506 }
507
508 static
509 void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
510 {
511 }
512
513 static
514 void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
515 {
516 }
517
518 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
519
520
521 static
522 void check_resize(struct cds_lfht *ht, struct rcu_table *t,
523 uint32_t chain_len)
524 {
525 unsigned long count;
526
527 count = uatomic_read(&ht->count);
528 /*
529 * Use bucket-local length for small table expand and for
530 * environments lacking per-cpu data support.
531 */
532 if (count >= (1UL << COUNT_COMMIT_ORDER))
533 return;
534 if (chain_len > 100)
535 dbg_printf("WARNING: large chain length: %u.\n",
536 chain_len);
537 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
538 cds_lfht_resize_lazy(ht, t,
539 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
540 }
541
542 static
543 struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
544 {
545 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
546 }
547
548 static
549 int is_removed(struct cds_lfht_node *node)
550 {
551 return ((unsigned long) node) & REMOVED_FLAG;
552 }
553
554 static
555 struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
556 {
557 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
558 }
559
560 static
561 int is_dummy(struct cds_lfht_node *node)
562 {
563 return ((unsigned long) node) & DUMMY_FLAG;
564 }
565
566 static
567 struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
568 {
569 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
570 }
571
572 static
573 unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
574 {
575 unsigned long old1, old2;
576
577 old1 = uatomic_read(ptr);
578 do {
579 old2 = old1;
580 if (old2 >= v)
581 return old2;
582 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
583 return v;
584 }
585
586 /*
587 * Remove all logically deleted nodes from a bucket up to a certain node key.
588 */
589 static
590 void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
591 {
592 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
593
594 for (;;) {
595 iter_prev = dummy;
596 /* We can always skip the dummy node initially */
597 iter = rcu_dereference(iter_prev->p.next);
598 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
599 for (;;) {
600 if (unlikely(!clear_flag(iter)))
601 return;
602 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
603 return;
604 next = rcu_dereference(clear_flag(iter)->p.next);
605 if (likely(is_removed(next)))
606 break;
607 iter_prev = clear_flag(iter);
608 iter = next;
609 }
610 assert(!is_removed(iter));
611 if (is_dummy(iter))
612 new_next = flag_dummy(clear_flag(next));
613 else
614 new_next = clear_flag(next);
615 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
616 }
617 }
618
619 static
620 struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
621 struct cds_lfht_node *node, int unique, int dummy)
622 {
623 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
624 *dummy_node;
625 struct _cds_lfht_node *lookup;
626 unsigned long hash, index, order;
627
628 if (!t->size) {
629 assert(dummy);
630 node->p.next = flag_dummy(NULL);
631 return node; /* Initial first add (head) */
632 }
633 hash = bit_reverse_ulong(node->p.reverse_hash);
634 for (;;) {
635 uint32_t chain_len = 0;
636
637 /*
638 * iter_prev points to the non-removed node prior to the
639 * insert location.
640 */
641 index = hash & (t->size - 1);
642 order = get_count_order_ulong(index + 1);
643 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
644 iter_prev = (struct cds_lfht_node *) lookup;
645 /* We can always skip the dummy node initially */
646 iter = rcu_dereference(iter_prev->p.next);
647 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
648 for (;;) {
649 if (unlikely(!clear_flag(iter)))
650 goto insert;
651 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
652 goto insert;
653 next = rcu_dereference(clear_flag(iter)->p.next);
654 if (unlikely(is_removed(next)))
655 goto gc_node;
656 if (unique
657 && !is_dummy(next)
658 && !ht->compare_fct(node->key, node->key_len,
659 clear_flag(iter)->key,
660 clear_flag(iter)->key_len))
661 return clear_flag(iter);
662 /* Only account for identical reverse hash once */
663 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
664 && !is_dummy(next))
665 check_resize(ht, t, ++chain_len);
666 iter_prev = clear_flag(iter);
667 iter = next;
668 }
669 insert:
670 assert(node != clear_flag(iter));
671 assert(!is_removed(iter_prev));
672 assert(iter_prev != node);
673 if (!dummy)
674 node->p.next = clear_flag(iter);
675 else
676 node->p.next = flag_dummy(clear_flag(iter));
677 if (is_dummy(iter))
678 new_node = flag_dummy(node);
679 else
680 new_node = node;
681 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
682 new_node) != iter)
683 continue; /* retry */
684 else
685 goto gc_end;
686 gc_node:
687 assert(!is_removed(iter));
688 if (is_dummy(iter))
689 new_next = flag_dummy(clear_flag(next));
690 else
691 new_next = clear_flag(next);
692 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
693 /* retry */
694 }
695 gc_end:
696 /* Garbage collect logically removed nodes in the bucket */
697 index = hash & (t->size - 1);
698 order = get_count_order_ulong(index + 1);
699 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
700 dummy_node = (struct cds_lfht_node *) lookup;
701 _cds_lfht_gc_bucket(dummy_node, node);
702 return node;
703 }
704
705 static
706 int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
707 struct cds_lfht_node *node)
708 {
709 struct cds_lfht_node *dummy, *next, *old;
710 struct _cds_lfht_node *lookup;
711 int flagged = 0;
712 unsigned long hash, index, order;
713
714 /* logically delete the node */
715 old = rcu_dereference(node->p.next);
716 do {
717 next = old;
718 if (unlikely(is_removed(next)))
719 goto end;
720 assert(!is_dummy(next));
721 old = uatomic_cmpxchg(&node->p.next, next,
722 flag_removed(next));
723 } while (old != next);
724
725 /* We performed the (logical) deletion. */
726 flagged = 1;
727
728 /*
729 * Ensure that the node is not visible to readers anymore: lookup for
730 * the node, and remove it (along with any other logically removed node)
731 * if found.
732 */
733 hash = bit_reverse_ulong(node->p.reverse_hash);
734 index = hash & (t->size - 1);
735 order = get_count_order_ulong(index + 1);
736 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
737 dummy = (struct cds_lfht_node *) lookup;
738 _cds_lfht_gc_bucket(dummy, node);
739 end:
740 /*
741 * Only the flagging action indicated that we (and no other)
742 * removed the node from the hash.
743 */
744 if (flagged) {
745 assert(is_removed(rcu_dereference(node->p.next)));
746 return 0;
747 } else
748 return -ENOENT;
749 }
750
751 static
752 void init_table(struct cds_lfht *ht, struct rcu_table *t,
753 unsigned long first_order, unsigned long len_order)
754 {
755 unsigned long i, end_order;
756
757 dbg_printf("init table: first_order %lu end_order %lu\n",
758 first_order, first_order + len_order);
759 end_order = first_order + len_order;
760 t->size = !first_order ? 0 : (1UL << (first_order - 1));
761 for (i = first_order; i < end_order; i++) {
762 unsigned long j, len;
763
764 len = !i ? 1 : 1UL << (i - 1);
765 dbg_printf("init order %lu len: %lu\n", i, len);
766 t->tbl[i] = calloc(len, sizeof(struct _cds_lfht_node));
767 for (j = 0; j < len; j++) {
768 dbg_printf("init entry: i %lu j %lu hash %lu\n",
769 i, j, !i ? 0 : (1UL << (i - 1)) + j);
770 struct cds_lfht_node *new_node =
771 (struct cds_lfht_node *) &t->tbl[i][j];
772 new_node->p.reverse_hash =
773 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
774 (void) _cds_lfht_add(ht, t, new_node, 0, 1);
775 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
776 break;
777 }
778 /* Update table size */
779 t->size = !i ? 1 : (1UL << i);
780 dbg_printf("init new size: %lu\n", t->size);
781 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
782 break;
783 }
784 t->resize_target = t->size;
785 t->resize_initiated = 0;
786 }
787
788 struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
789 cds_lfht_compare_fct compare_fct,
790 unsigned long hash_seed,
791 unsigned long init_size,
792 void (*cds_lfht_call_rcu)(struct rcu_head *head,
793 void (*func)(struct rcu_head *head)))
794 {
795 struct cds_lfht *ht;
796 unsigned long order;
797
798 /* init_size must be power of two */
799 if (init_size && (init_size & (init_size - 1)))
800 return NULL;
801 ht = calloc(1, sizeof(struct cds_lfht));
802 ht->hash_fct = hash_fct;
803 ht->compare_fct = compare_fct;
804 ht->hash_seed = hash_seed;
805 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
806 ht->in_progress_resize = 0;
807 ht->percpu_count = alloc_per_cpu_items_count();
808 /* this mutex should not nest in read-side C.S. */
809 pthread_mutex_init(&ht->resize_mutex, NULL);
810 order = get_count_order_ulong(max(init_size, 1)) + 1;
811 ht->t = calloc(1, sizeof(struct cds_lfht)
812 + (order * sizeof(struct _cds_lfht_node *)));
813 ht->t->size = 0;
814 pthread_mutex_lock(&ht->resize_mutex);
815 init_table(ht, ht->t, 0, order);
816 pthread_mutex_unlock(&ht->resize_mutex);
817 return ht;
818 }
819
820 struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len)
821 {
822 struct rcu_table *t;
823 struct cds_lfht_node *node, *next;
824 struct _cds_lfht_node *lookup;
825 unsigned long hash, reverse_hash, index, order;
826
827 hash = ht->hash_fct(key, key_len, ht->hash_seed);
828 reverse_hash = bit_reverse_ulong(hash);
829
830 t = rcu_dereference(ht->t);
831 index = hash & (t->size - 1);
832 order = get_count_order_ulong(index + 1);
833 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
834 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
835 hash, index, order, index & ((1UL << (order - 1)) - 1));
836 node = (struct cds_lfht_node *) lookup;
837 for (;;) {
838 if (unlikely(!node))
839 break;
840 if (unlikely(node->p.reverse_hash > reverse_hash)) {
841 node = NULL;
842 break;
843 }
844 next = rcu_dereference(node->p.next);
845 if (likely(!is_removed(next))
846 && !is_dummy(next)
847 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
848 break;
849 }
850 node = clear_flag(next);
851 }
852 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
853 return node;
854 }
855
856 struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht,
857 struct cds_lfht_node *node)
858 {
859 struct cds_lfht_node *next;
860 unsigned long reverse_hash;
861 void *key;
862 size_t key_len;
863
864 reverse_hash = node->p.reverse_hash;
865 key = node->key;
866 key_len = node->key_len;
867 next = rcu_dereference(node->p.next);
868 node = clear_flag(next);
869
870 for (;;) {
871 if (unlikely(!node))
872 break;
873 if (unlikely(node->p.reverse_hash > reverse_hash)) {
874 node = NULL;
875 break;
876 }
877 next = rcu_dereference(node->p.next);
878 if (likely(!is_removed(next))
879 && !is_dummy(next)
880 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
881 break;
882 }
883 node = clear_flag(next);
884 }
885 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
886 return node;
887 }
888
889 void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node)
890 {
891 struct rcu_table *t;
892 unsigned long hash;
893
894 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
895 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
896
897 t = rcu_dereference(ht->t);
898 (void) _cds_lfht_add(ht, t, node, 0, 0);
899 ht_count_add(ht, t);
900 }
901
902 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
903 struct cds_lfht_node *node)
904 {
905 struct rcu_table *t;
906 unsigned long hash;
907 struct cds_lfht_node *ret;
908
909 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
910 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
911
912 t = rcu_dereference(ht->t);
913 ret = _cds_lfht_add(ht, t, node, 1, 0);
914 if (ret != node)
915 ht_count_add(ht, t);
916 return ret;
917 }
918
919 int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node)
920 {
921 struct rcu_table *t;
922 int ret;
923
924 t = rcu_dereference(ht->t);
925 ret = _cds_lfht_remove(ht, t, node);
926 if (!ret)
927 ht_count_remove(ht, t);
928 return ret;
929 }
930
931 static
932 int cds_lfht_delete_dummy(struct cds_lfht *ht)
933 {
934 struct rcu_table *t;
935 struct cds_lfht_node *node;
936 struct _cds_lfht_node *lookup;
937 unsigned long order, i;
938
939 t = ht->t;
940 /* Check that the table is empty */
941 lookup = &t->tbl[0][0];
942 node = (struct cds_lfht_node *) lookup;
943 do {
944 node = clear_flag(node)->p.next;
945 if (!is_dummy(node))
946 return -EPERM;
947 assert(!is_removed(node));
948 } while (clear_flag(node));
949 /* Internal sanity check: all nodes left should be dummy */
950 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
951 unsigned long len;
952
953 len = !order ? 1 : 1UL << (order - 1);
954 for (i = 0; i < len; i++) {
955 dbg_printf("delete order %lu i %lu hash %lu\n",
956 order, i,
957 bit_reverse_ulong(t->tbl[order][i].reverse_hash));
958 assert(is_dummy(t->tbl[order][i].next));
959 }
960 free(t->tbl[order]);
961 }
962 return 0;
963 }
964
965 /*
966 * Should only be called when no more concurrent readers nor writers can
967 * possibly access the table.
968 */
969 int cds_lfht_destroy(struct cds_lfht *ht)
970 {
971 int ret;
972
973 /* Wait for in-flight resize operations to complete */
974 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
975 while (uatomic_read(&ht->in_progress_resize))
976 poll(NULL, 0, 100); /* wait for 100ms */
977 ret = cds_lfht_delete_dummy(ht);
978 if (ret)
979 return ret;
980 free(ht->t);
981 free_per_cpu_items_count(ht->percpu_count);
982 free(ht);
983 return ret;
984 }
985
986 void cds_lfht_count_nodes(struct cds_lfht *ht,
987 unsigned long *count,
988 unsigned long *removed)
989 {
990 struct rcu_table *t;
991 struct cds_lfht_node *node, *next;
992 struct _cds_lfht_node *lookup;
993 unsigned long nr_dummy = 0;
994
995 *count = 0;
996 *removed = 0;
997
998 t = rcu_dereference(ht->t);
999 /* Count non-dummy nodes in the table */
1000 lookup = &t->tbl[0][0];
1001 node = (struct cds_lfht_node *) lookup;
1002 do {
1003 next = rcu_dereference(node->p.next);
1004 if (is_removed(next)) {
1005 assert(!is_dummy(next));
1006 (*removed)++;
1007 } else if (!is_dummy(next))
1008 (*count)++;
1009 else
1010 (nr_dummy)++;
1011 node = clear_flag(next);
1012 } while (node);
1013 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
1014 }
1015
1016 static
1017 void cds_lfht_free_table_cb(struct rcu_head *head)
1018 {
1019 struct rcu_table *t =
1020 caa_container_of(head, struct rcu_table, head);
1021 free(t);
1022 }
1023
1024 /* called with resize mutex held */
1025 static
1026 void _do_cds_lfht_resize(struct cds_lfht *ht)
1027 {
1028 unsigned long new_size, old_size, old_order, new_order;
1029 struct rcu_table *new_t, *old_t;
1030
1031 old_t = ht->t;
1032 old_size = old_t->size;
1033 old_order = get_count_order_ulong(old_size) + 1;
1034
1035 new_size = CMM_LOAD_SHARED(old_t->resize_target);
1036 if (old_size == new_size)
1037 return;
1038 new_order = get_count_order_ulong(new_size) + 1;
1039 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1040 old_size, old_order, new_size, new_order);
1041 new_t = malloc(sizeof(struct cds_lfht)
1042 + (new_order * sizeof(struct _cds_lfht_node *)));
1043 assert(new_size > old_size);
1044 memcpy(&new_t->tbl, &old_t->tbl,
1045 old_order * sizeof(struct _cds_lfht_node *));
1046 init_table(ht, new_t, old_order, new_order - old_order);
1047 /* Changing table and size atomically wrt lookups */
1048 rcu_assign_pointer(ht->t, new_t);
1049 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
1050 }
1051
1052 static
1053 unsigned long resize_target_update(struct rcu_table *t,
1054 int growth_order)
1055 {
1056 return _uatomic_max(&t->resize_target,
1057 t->size << growth_order);
1058 }
1059
1060 void cds_lfht_resize(struct cds_lfht *ht, int growth)
1061 {
1062 struct rcu_table *t = rcu_dereference(ht->t);
1063 unsigned long target_size;
1064
1065 if (growth < 0) {
1066 /*
1067 * Silently refuse to shrink hash table. (not supported)
1068 */
1069 dbg_printf("shrinking hash table not supported.\n");
1070 return;
1071 }
1072
1073 target_size = resize_target_update(t, growth);
1074 if (t->size < target_size) {
1075 CMM_STORE_SHARED(t->resize_initiated, 1);
1076 pthread_mutex_lock(&ht->resize_mutex);
1077 _do_cds_lfht_resize(ht);
1078 pthread_mutex_unlock(&ht->resize_mutex);
1079 }
1080 }
1081
1082 static
1083 void do_resize_cb(struct rcu_head *head)
1084 {
1085 struct rcu_resize_work *work =
1086 caa_container_of(head, struct rcu_resize_work, head);
1087 struct cds_lfht *ht = work->ht;
1088
1089 pthread_mutex_lock(&ht->resize_mutex);
1090 _do_cds_lfht_resize(ht);
1091 pthread_mutex_unlock(&ht->resize_mutex);
1092 free(work);
1093 cmm_smp_mb(); /* finish resize before decrement */
1094 uatomic_dec(&ht->in_progress_resize);
1095 }
1096
1097 static
1098 void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth)
1099 {
1100 struct rcu_resize_work *work;
1101 unsigned long target_size;
1102
1103 target_size = resize_target_update(t, growth);
1104 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
1105 uatomic_inc(&ht->in_progress_resize);
1106 cmm_smp_mb(); /* increment resize count before calling it */
1107 work = malloc(sizeof(*work));
1108 work->ht = ht;
1109 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1110 CMM_STORE_SHARED(t->resize_initiated, 1);
1111 }
1112 }
1113
1114 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
1115
1116 static
1117 unsigned long resize_target_update_count(struct rcu_table *t,
1118 unsigned long count)
1119 {
1120 return _uatomic_max(&t->resize_target, count);
1121 }
1122
1123 static
1124 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, struct rcu_table *t,
1125 unsigned long count)
1126 {
1127 struct rcu_resize_work *work;
1128 unsigned long target_size;
1129
1130 target_size = resize_target_update_count(t, count);
1131 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
1132 uatomic_inc(&ht->in_progress_resize);
1133 cmm_smp_mb(); /* increment resize count before calling it */
1134 work = malloc(sizeof(*work));
1135 work->ht = ht;
1136 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1137 CMM_STORE_SHARED(t->resize_initiated, 1);
1138 }
1139 }
1140
1141 #endif
This page took 0.051364 seconds and 5 git commands to generate.