rculfhash: count nodes (per-cpu) as expand criterion
[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 #define CHAIN_LEN_TARGET 4
123 #define CHAIN_LEN_RESIZE_THRESHOLD 8
124
125 /* Commit counter changes to global counter each 1024 steps */
126 #define COUNT_COMMIT_ORDER 10
127
128 #ifndef max
129 #define max(a, b) ((a) > (b) ? (a) : (b))
130 #endif
131
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 */
137 #define REMOVED_FLAG (1UL << 0)
138 #define DUMMY_FLAG (1UL << 1)
139 #define FLAGS_MASK ((1UL << 2) - 1)
140
141 struct ht_items_count {
142 unsigned long v;
143 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
144
145 struct rcu_table {
146 unsigned long size; /* always a power of 2 */
147 unsigned long resize_target;
148 int resize_initiated;
149 struct rcu_head head;
150 struct _cds_lfht_node *tbl[0];
151 };
152
153 struct cds_lfht {
154 struct rcu_table *t; /* shared */
155 cds_lfht_hash_fct hash_fct;
156 cds_lfht_compare_fct compare_fct;
157 unsigned long hash_seed;
158 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
159 unsigned int in_progress_resize, in_progress_destroy;
160 void (*cds_lfht_call_rcu)(struct rcu_head *head,
161 void (*func)(struct rcu_head *head));
162 unsigned long count; /* global approximate item count */
163 struct ht_items_count *percpu_count; /* per-cpu item count */
164 };
165
166 struct rcu_resize_work {
167 struct rcu_head head;
168 struct cds_lfht *ht;
169 };
170
171 /*
172 * Algorithm to reverse bits in a word by lookup table, extended to
173 * 64-bit words.
174 * Source:
175 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
176 * Originally from Public Domain.
177 */
178
179 static const uint8_t BitReverseTable256[256] =
180 {
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
189
190 static
191 uint8_t bit_reverse_u8(uint8_t v)
192 {
193 return BitReverseTable256[v];
194 }
195
196 static __attribute__((unused))
197 uint32_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));
203 }
204
205 static __attribute__((unused))
206 uint64_t bit_reverse_u64(uint64_t v)
207 {
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
218 static
219 unsigned 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
228 /*
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).
232 */
233 #if defined(__i386) || defined(__x86_64)
234 static inline
235 unsigned int fls_u32(uint32_t x)
236 {
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)
250 static inline
251 unsigned 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
266 static __attribute__((unused))
267 unsigned 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
303 static __attribute__((unused))
304 unsigned int fls_u32(uint32_t x)
305 {
306 unsigned int r = 32;
307
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
334 unsigned int fls_ulong(unsigned long x)
335 {
336 #if (CAA_BITS_PER_lONG == 32)
337 return fls_u32(x);
338 #else
339 return fls_u64(x);
340 #endif
341 }
342
343 int 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
353 int 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;
361 }
362
363 static
364 void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth);
365
366 /*
367 * If the sched_getcpu() and sysconf(_SC_NPROCESSORS_CONF) calls are
368 * available, then we support hash table item accounting.
369 * In the unfortunate event the number of CPUs reported would be
370 * inaccurate, we use modulo arithmetic on the number of CPUs we got.
371 */
372
373 #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF)
374
375 static long nr_cpus_mask = -1;
376
377 static
378 struct ht_items_count *alloc_per_cpu_items_count(void)
379 {
380 struct ht_items_count *count;
381
382 switch (nr_cpus_mask) {
383 case -2:
384 return NULL;
385 case -1:
386 {
387 long maxcpus;
388
389 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
390 if (maxcpus <= 0) {
391 nr_cpus_mask = -2;
392 return NULL;
393 }
394 /*
395 * round up number of CPUs to next power of two, so we
396 * can use & for modulo.
397 */
398 maxcpus = 1UL << get_count_order_ulong(maxcpus);
399 nr_cpus_mask = maxcpus - 1;
400 }
401 /* Fall-through */
402 default:
403 return calloc(nr_cpus_mask + 1, sizeof(*count));
404 }
405 }
406
407 static
408 void free_per_cpu_items_count(struct ht_items_count *count)
409 {
410 free(count);
411 }
412
413 static
414 int ht_get_cpu(void)
415 {
416 int cpu;
417
418 assert(nr_cpus_mask >= 0);
419 cpu = sched_getcpu();
420 if (unlikely(cpu < 0))
421 return cpu;
422 else
423 return cpu & nr_cpus_mask;
424 }
425
426 static
427 unsigned long ht_count_update(struct cds_lfht *ht, unsigned int value)
428 {
429 int cpu;
430
431 if (unlikely(!ht->percpu_count))
432 return 0;
433 cpu = ht_get_cpu();
434 if (unlikely(cpu < 0))
435 return 0;
436 return uatomic_add_return(&ht->percpu_count[cpu].v, 1);
437 }
438
439 static
440 void ht_count_add(struct cds_lfht *ht, struct rcu_table *t)
441 {
442 unsigned long percpu_count;
443
444 percpu_count = ht_count_update(ht, 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 dbg_printf("add global %lu\n", count);
454 cds_lfht_resize_lazy(ht, t, 1);
455 }
456 }
457 }
458
459 static
460 void ht_count_remove(struct cds_lfht *ht, struct rcu_table *t)
461 {
462 unsigned long percpu_count;
463
464 percpu_count = ht_count_update(ht, -1);
465 if (unlikely(!(percpu_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
466 unsigned long count;
467
468 dbg_printf("remove percpu %lu\n", percpu_count);
469 count = uatomic_add_return(&ht->count,
470 1UL << COUNT_COMMIT_ORDER);
471 /* If power of 2 */
472 if (!(count & (count - 1))) {
473 dbg_printf("remove global %lu\n", count);
474 cds_lfht_resize_lazy(ht, t, -1);
475 }
476 }
477 }
478
479 #else /* #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
480
481 static const long nr_cpus_mask = -1;
482
483 static
484 struct ht_items_count *alloc_per_cpu_items_count(void)
485 {
486 return NULL;
487 }
488
489 static
490 void free_per_cpu_items_count(struct ht_items_count *count)
491 {
492 }
493
494 static
495 void ht_count_add(struct cds_lfht *ht)
496 {
497 }
498
499 static
500 void ht_count_remove(struct cds_lfht *ht)
501 {
502 }
503
504 #endif /* #else #if defined(HAVE_SCHED_GETCPU) && defined(HAVE_SYSCONF) */
505
506
507 static
508 void check_resize(struct cds_lfht *ht, struct rcu_table *t,
509 uint32_t chain_len)
510 {
511 return; //TEST
512 if (chain_len > 100)
513 dbg_printf("WARNING: large chain length: %u.\n",
514 chain_len);
515 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
516 cds_lfht_resize_lazy(ht, t,
517 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
518 }
519
520 static
521 struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
522 {
523 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
524 }
525
526 static
527 int is_removed(struct cds_lfht_node *node)
528 {
529 return ((unsigned long) node) & REMOVED_FLAG;
530 }
531
532 static
533 struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
534 {
535 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
536 }
537
538 static
539 int is_dummy(struct cds_lfht_node *node)
540 {
541 return ((unsigned long) node) & DUMMY_FLAG;
542 }
543
544 static
545 struct cds_lfht_node *flag_dummy(struct cds_lfht_node *node)
546 {
547 return (struct cds_lfht_node *) (((unsigned long) node) | DUMMY_FLAG);
548 }
549
550 static
551 unsigned long _uatomic_max(unsigned long *ptr, unsigned long v)
552 {
553 unsigned long old1, old2;
554
555 old1 = uatomic_read(ptr);
556 do {
557 old2 = old1;
558 if (old2 >= v)
559 return old2;
560 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
561 return v;
562 }
563
564 /*
565 * Remove all logically deleted nodes from a bucket up to a certain node key.
566 */
567 static
568 void _cds_lfht_gc_bucket(struct cds_lfht_node *dummy, struct cds_lfht_node *node)
569 {
570 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
571
572 for (;;) {
573 iter_prev = dummy;
574 /* We can always skip the dummy node initially */
575 iter = rcu_dereference(iter_prev->p.next);
576 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
577 for (;;) {
578 if (unlikely(!clear_flag(iter)))
579 return;
580 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
581 return;
582 next = rcu_dereference(clear_flag(iter)->p.next);
583 if (likely(is_removed(next)))
584 break;
585 iter_prev = clear_flag(iter);
586 iter = next;
587 }
588 assert(!is_removed(iter));
589 if (is_dummy(iter))
590 new_next = flag_dummy(clear_flag(next));
591 else
592 new_next = clear_flag(next);
593 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
594 }
595 }
596
597 static
598 struct cds_lfht_node *_cds_lfht_add(struct cds_lfht *ht, struct rcu_table *t,
599 struct cds_lfht_node *node, int unique, int dummy)
600 {
601 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
602 *dummy_node;
603 struct _cds_lfht_node *lookup;
604 unsigned long hash, index, order;
605
606 if (!t->size) {
607 assert(dummy);
608 node->p.next = flag_dummy(NULL);
609 return node; /* Initial first add (head) */
610 }
611 hash = bit_reverse_ulong(node->p.reverse_hash);
612 for (;;) {
613 uint32_t chain_len = 0;
614
615 /*
616 * iter_prev points to the non-removed node prior to the
617 * insert location.
618 */
619 index = hash & (t->size - 1);
620 order = get_count_order_ulong(index + 1);
621 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
622 iter_prev = (struct cds_lfht_node *) lookup;
623 /* We can always skip the dummy node initially */
624 iter = rcu_dereference(iter_prev->p.next);
625 assert(iter_prev->p.reverse_hash <= node->p.reverse_hash);
626 for (;;) {
627 if (unlikely(!clear_flag(iter)))
628 goto insert;
629 if (likely(clear_flag(iter)->p.reverse_hash > node->p.reverse_hash))
630 goto insert;
631 next = rcu_dereference(clear_flag(iter)->p.next);
632 if (unlikely(is_removed(next)))
633 goto gc_node;
634 if (unique
635 && !is_dummy(next)
636 && !ht->compare_fct(node->key, node->key_len,
637 clear_flag(iter)->key,
638 clear_flag(iter)->key_len))
639 return clear_flag(iter);
640 /* Only account for identical reverse hash once */
641 if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash
642 && !is_dummy(next))
643 check_resize(ht, t, ++chain_len);
644 iter_prev = clear_flag(iter);
645 iter = next;
646 }
647 insert:
648 assert(node != clear_flag(iter));
649 assert(!is_removed(iter_prev));
650 assert(iter_prev != node);
651 if (!dummy)
652 node->p.next = clear_flag(iter);
653 else
654 node->p.next = flag_dummy(clear_flag(iter));
655 if (is_dummy(iter))
656 new_node = flag_dummy(node);
657 else
658 new_node = node;
659 if (uatomic_cmpxchg(&iter_prev->p.next, iter,
660 new_node) != iter)
661 continue; /* retry */
662 else
663 goto gc_end;
664 gc_node:
665 assert(!is_removed(iter));
666 if (is_dummy(iter))
667 new_next = flag_dummy(clear_flag(next));
668 else
669 new_next = clear_flag(next);
670 (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next);
671 /* retry */
672 }
673 gc_end:
674 /* Garbage collect logically removed nodes in the bucket */
675 index = hash & (t->size - 1);
676 order = get_count_order_ulong(index + 1);
677 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
678 dummy_node = (struct cds_lfht_node *) lookup;
679 _cds_lfht_gc_bucket(dummy_node, node);
680 return node;
681 }
682
683 static
684 int _cds_lfht_remove(struct cds_lfht *ht, struct rcu_table *t,
685 struct cds_lfht_node *node)
686 {
687 struct cds_lfht_node *dummy, *next, *old;
688 struct _cds_lfht_node *lookup;
689 int flagged = 0;
690 unsigned long hash, index, order;
691
692 /* logically delete the node */
693 old = rcu_dereference(node->p.next);
694 do {
695 next = old;
696 if (unlikely(is_removed(next)))
697 goto end;
698 assert(!is_dummy(next));
699 old = uatomic_cmpxchg(&node->p.next, next,
700 flag_removed(next));
701 } while (old != next);
702
703 /* We performed the (logical) deletion. */
704 flagged = 1;
705
706 /*
707 * Ensure that the node is not visible to readers anymore: lookup for
708 * the node, and remove it (along with any other logically removed node)
709 * if found.
710 */
711 hash = bit_reverse_ulong(node->p.reverse_hash);
712 index = hash & (t->size - 1);
713 order = get_count_order_ulong(index + 1);
714 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
715 dummy = (struct cds_lfht_node *) lookup;
716 _cds_lfht_gc_bucket(dummy, node);
717 end:
718 /*
719 * Only the flagging action indicated that we (and no other)
720 * removed the node from the hash.
721 */
722 if (flagged) {
723 assert(is_removed(rcu_dereference(node->p.next)));
724 return 0;
725 } else
726 return -ENOENT;
727 }
728
729 static
730 void init_table(struct cds_lfht *ht, struct rcu_table *t,
731 unsigned long first_order, unsigned long len_order)
732 {
733 unsigned long i, end_order;
734
735 dbg_printf("init table: first_order %lu end_order %lu\n",
736 first_order, first_order + len_order);
737 end_order = first_order + len_order;
738 t->size = !first_order ? 0 : (1UL << (first_order - 1));
739 for (i = first_order; i < end_order; i++) {
740 unsigned long j, len;
741
742 len = !i ? 1 : 1UL << (i - 1);
743 dbg_printf("init order %lu len: %lu\n", i, len);
744 t->tbl[i] = calloc(len, sizeof(struct _cds_lfht_node));
745 for (j = 0; j < len; j++) {
746 dbg_printf("init entry: i %lu j %lu hash %lu\n",
747 i, j, !i ? 0 : (1UL << (i - 1)) + j);
748 struct cds_lfht_node *new_node =
749 (struct cds_lfht_node *) &t->tbl[i][j];
750 new_node->p.reverse_hash =
751 bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j);
752 (void) _cds_lfht_add(ht, t, new_node, 0, 1);
753 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
754 break;
755 }
756 /* Update table size */
757 t->size = !i ? 1 : (1UL << i);
758 dbg_printf("init new size: %lu\n", t->size);
759 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
760 break;
761 }
762 t->resize_target = t->size;
763 t->resize_initiated = 0;
764 }
765
766 struct cds_lfht *cds_lfht_new(cds_lfht_hash_fct hash_fct,
767 cds_lfht_compare_fct compare_fct,
768 unsigned long hash_seed,
769 unsigned long init_size,
770 void (*cds_lfht_call_rcu)(struct rcu_head *head,
771 void (*func)(struct rcu_head *head)))
772 {
773 struct cds_lfht *ht;
774 unsigned long order;
775
776 /* init_size must be power of two */
777 if (init_size && (init_size & (init_size - 1)))
778 return NULL;
779 ht = calloc(1, sizeof(struct cds_lfht));
780 ht->hash_fct = hash_fct;
781 ht->compare_fct = compare_fct;
782 ht->hash_seed = hash_seed;
783 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
784 ht->in_progress_resize = 0;
785 ht->percpu_count = alloc_per_cpu_items_count();
786 /* this mutex should not nest in read-side C.S. */
787 pthread_mutex_init(&ht->resize_mutex, NULL);
788 order = get_count_order_ulong(max(init_size, 1)) + 1;
789 ht->t = calloc(1, sizeof(struct cds_lfht)
790 + (order * sizeof(struct _cds_lfht_node *)));
791 ht->t->size = 0;
792 pthread_mutex_lock(&ht->resize_mutex);
793 init_table(ht, ht->t, 0, order);
794 pthread_mutex_unlock(&ht->resize_mutex);
795 return ht;
796 }
797
798 struct cds_lfht_node *cds_lfht_lookup(struct cds_lfht *ht, void *key, size_t key_len)
799 {
800 struct rcu_table *t;
801 struct cds_lfht_node *node, *next;
802 struct _cds_lfht_node *lookup;
803 unsigned long hash, reverse_hash, index, order;
804
805 hash = ht->hash_fct(key, key_len, ht->hash_seed);
806 reverse_hash = bit_reverse_ulong(hash);
807
808 t = rcu_dereference(ht->t);
809 index = hash & (t->size - 1);
810 order = get_count_order_ulong(index + 1);
811 lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)];
812 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
813 hash, index, order, index & ((1UL << (order - 1)) - 1));
814 node = (struct cds_lfht_node *) lookup;
815 for (;;) {
816 if (unlikely(!node))
817 break;
818 if (unlikely(node->p.reverse_hash > reverse_hash)) {
819 node = NULL;
820 break;
821 }
822 next = rcu_dereference(node->p.next);
823 if (likely(!is_removed(next))
824 && !is_dummy(next)
825 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
826 break;
827 }
828 node = clear_flag(next);
829 }
830 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
831 return node;
832 }
833
834 struct cds_lfht_node *cds_lfht_next(struct cds_lfht *ht,
835 struct cds_lfht_node *node)
836 {
837 struct cds_lfht_node *next;
838 unsigned long reverse_hash;
839 void *key;
840 size_t key_len;
841
842 reverse_hash = node->p.reverse_hash;
843 key = node->key;
844 key_len = node->key_len;
845 next = rcu_dereference(node->p.next);
846 node = clear_flag(next);
847
848 for (;;) {
849 if (unlikely(!node))
850 break;
851 if (unlikely(node->p.reverse_hash > reverse_hash)) {
852 node = NULL;
853 break;
854 }
855 next = rcu_dereference(node->p.next);
856 if (likely(!is_removed(next))
857 && !is_dummy(next)
858 && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) {
859 break;
860 }
861 node = clear_flag(next);
862 }
863 assert(!node || !is_dummy(rcu_dereference(node->p.next)));
864 return node;
865 }
866
867 void cds_lfht_add(struct cds_lfht *ht, struct cds_lfht_node *node)
868 {
869 struct rcu_table *t;
870 unsigned long hash;
871
872 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
873 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
874
875 t = rcu_dereference(ht->t);
876 (void) _cds_lfht_add(ht, t, node, 0, 0);
877 ht_count_add(ht, t);
878 }
879
880 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
881 struct cds_lfht_node *node)
882 {
883 struct rcu_table *t;
884 unsigned long hash;
885 struct cds_lfht_node *ret;
886
887 hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed);
888 node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash);
889
890 t = rcu_dereference(ht->t);
891 ret = _cds_lfht_add(ht, t, node, 1, 0);
892 if (ret != node)
893 ht_count_add(ht, t);
894 return ret;
895 }
896
897 int cds_lfht_remove(struct cds_lfht *ht, struct cds_lfht_node *node)
898 {
899 struct rcu_table *t;
900 int ret;
901
902 t = rcu_dereference(ht->t);
903 ret = _cds_lfht_remove(ht, t, node);
904 if (!ret)
905 ht_count_remove(ht, t);
906 return ret;
907 }
908
909 static
910 int cds_lfht_delete_dummy(struct cds_lfht *ht)
911 {
912 struct rcu_table *t;
913 struct cds_lfht_node *node;
914 struct _cds_lfht_node *lookup;
915 unsigned long order, i;
916
917 t = ht->t;
918 /* Check that the table is empty */
919 lookup = &t->tbl[0][0];
920 node = (struct cds_lfht_node *) lookup;
921 do {
922 node = clear_flag(node)->p.next;
923 if (!is_dummy(node))
924 return -EPERM;
925 assert(!is_removed(node));
926 } while (clear_flag(node));
927 /* Internal sanity check: all nodes left should be dummy */
928 for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) {
929 unsigned long len;
930
931 len = !order ? 1 : 1UL << (order - 1);
932 for (i = 0; i < len; i++) {
933 dbg_printf("delete order %lu i %lu hash %lu\n",
934 order, i,
935 bit_reverse_ulong(t->tbl[order][i].reverse_hash));
936 assert(is_dummy(t->tbl[order][i].next));
937 }
938 free(t->tbl[order]);
939 }
940 return 0;
941 }
942
943 /*
944 * Should only be called when no more concurrent readers nor writers can
945 * possibly access the table.
946 */
947 int cds_lfht_destroy(struct cds_lfht *ht)
948 {
949 int ret;
950
951 /* Wait for in-flight resize operations to complete */
952 CMM_STORE_SHARED(ht->in_progress_destroy, 1);
953 while (uatomic_read(&ht->in_progress_resize))
954 poll(NULL, 0, 100); /* wait for 100ms */
955 ret = cds_lfht_delete_dummy(ht);
956 if (ret)
957 return ret;
958 free(ht->t);
959 free_per_cpu_items_count(ht->percpu_count);
960 free(ht);
961 return ret;
962 }
963
964 void cds_lfht_count_nodes(struct cds_lfht *ht,
965 unsigned long *count,
966 unsigned long *removed)
967 {
968 struct rcu_table *t;
969 struct cds_lfht_node *node, *next;
970 struct _cds_lfht_node *lookup;
971 unsigned long nr_dummy = 0;
972
973 *count = 0;
974 *removed = 0;
975
976 t = rcu_dereference(ht->t);
977 /* Count non-dummy nodes in the table */
978 lookup = &t->tbl[0][0];
979 node = (struct cds_lfht_node *) lookup;
980 do {
981 next = rcu_dereference(node->p.next);
982 if (is_removed(next)) {
983 assert(!is_dummy(next));
984 (*removed)++;
985 } else if (!is_dummy(next))
986 (*count)++;
987 else
988 (nr_dummy)++;
989 node = clear_flag(next);
990 } while (node);
991 dbg_printf("number of dummy nodes: %lu\n", nr_dummy);
992 }
993
994 static
995 void cds_lfht_free_table_cb(struct rcu_head *head)
996 {
997 struct rcu_table *t =
998 caa_container_of(head, struct rcu_table, head);
999 free(t);
1000 }
1001
1002 /* called with resize mutex held */
1003 static
1004 void _do_cds_lfht_resize(struct cds_lfht *ht)
1005 {
1006 unsigned long new_size, old_size, old_order, new_order;
1007 struct rcu_table *new_t, *old_t;
1008
1009 old_t = ht->t;
1010 old_size = old_t->size;
1011 old_order = get_count_order_ulong(old_size) + 1;
1012
1013 new_size = CMM_LOAD_SHARED(old_t->resize_target);
1014 if (old_size == new_size)
1015 return;
1016 new_order = get_count_order_ulong(new_size) + 1;
1017 printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1018 old_size, old_order, new_size, new_order);
1019 new_t = malloc(sizeof(struct cds_lfht)
1020 + (new_order * sizeof(struct _cds_lfht_node *)));
1021 assert(new_size > old_size);
1022 memcpy(&new_t->tbl, &old_t->tbl,
1023 old_order * sizeof(struct _cds_lfht_node *));
1024 init_table(ht, new_t, old_order, new_order - old_order);
1025 /* Changing table and size atomically wrt lookups */
1026 rcu_assign_pointer(ht->t, new_t);
1027 ht->cds_lfht_call_rcu(&old_t->head, cds_lfht_free_table_cb);
1028 }
1029
1030 static
1031 unsigned long resize_target_update(struct rcu_table *t,
1032 int growth_order)
1033 {
1034 return _uatomic_max(&t->resize_target,
1035 t->size << growth_order);
1036 }
1037
1038 void cds_lfht_resize(struct cds_lfht *ht, int growth)
1039 {
1040 struct rcu_table *t = rcu_dereference(ht->t);
1041 unsigned long target_size;
1042
1043 if (growth < 0) {
1044 /*
1045 * Silently refuse to shrink hash table. (not supported)
1046 */
1047 dbg_printf("shrinking hash table not supported.\n");
1048 return;
1049 }
1050
1051 target_size = resize_target_update(t, growth);
1052 if (t->size < target_size) {
1053 CMM_STORE_SHARED(t->resize_initiated, 1);
1054 pthread_mutex_lock(&ht->resize_mutex);
1055 _do_cds_lfht_resize(ht);
1056 pthread_mutex_unlock(&ht->resize_mutex);
1057 }
1058 }
1059
1060 static
1061 void do_resize_cb(struct rcu_head *head)
1062 {
1063 struct rcu_resize_work *work =
1064 caa_container_of(head, struct rcu_resize_work, head);
1065 struct cds_lfht *ht = work->ht;
1066
1067 pthread_mutex_lock(&ht->resize_mutex);
1068 _do_cds_lfht_resize(ht);
1069 pthread_mutex_unlock(&ht->resize_mutex);
1070 free(work);
1071 cmm_smp_mb(); /* finish resize before decrement */
1072 uatomic_dec(&ht->in_progress_resize);
1073 }
1074
1075 static
1076 void cds_lfht_resize_lazy(struct cds_lfht *ht, struct rcu_table *t, int growth)
1077 {
1078 struct rcu_resize_work *work;
1079 unsigned long target_size;
1080
1081 target_size = resize_target_update(t, growth);
1082 if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) {
1083 uatomic_inc(&ht->in_progress_resize);
1084 cmm_smp_mb(); /* increment resize count before calling it */
1085 work = malloc(sizeof(*work));
1086 work->ht = ht;
1087 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1088 CMM_STORE_SHARED(t->resize_initiated, 1);
1089 }
1090 }
This page took 0.04981 seconds and 5 git commands to generate.