Add Lai Jiangshan's copyright to rculfhash
[urcu.git] / rculfhash.c
1 /*
2 * rculfhash.c
3 *
4 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
5 *
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /*
25 * Based on the following articles:
26 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
27 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
28 * - Michael, M. M. High performance dynamic lock-free hash tables
29 * and list-based sets. In Proceedings of the fourteenth annual ACM
30 * symposium on Parallel algorithms and architectures, ACM Press,
31 * (2002), 73-82.
32 *
33 * Some specificities of this Lock-Free Resizable RCU Hash Table
34 * implementation:
35 *
36 * - RCU read-side critical section allows readers to perform hash
37 * table lookups and use the returned objects safely by delaying
38 * memory reclaim of a grace period.
39 * - Add and remove operations are lock-free, and do not need to
40 * allocate memory. They need to be executed within RCU read-side
41 * critical section to ensure the objects they read are valid and to
42 * deal with the cmpxchg ABA problem.
43 * - add and add_unique operations are supported. add_unique checks if
44 * the node key already exists in the hash table. It ensures no key
45 * duplicata exists.
46 * - The resize operation executes concurrently with add/remove/lookup.
47 * - Hash table nodes are contained within a split-ordered list. This
48 * list is ordered by incrementing reversed-bits-hash value.
49 * - An index of bucket nodes is kept. These bucket nodes are the hash
50 * table "buckets", and they are also chained together in the
51 * split-ordered list, which allows recursive expansion.
52 * - The resize operation for small tables only allows expanding the hash table.
53 * It is triggered automatically by detecting long chains in the add
54 * operation.
55 * - The resize operation for larger tables (and available through an
56 * API) allows both expanding and shrinking the hash table.
57 * - Split-counters are used to keep track of the number of
58 * nodes within the hash table for automatic resize triggering.
59 * - Resize operation initiated by long chain detection is executed by a
60 * call_rcu thread, which keeps lock-freedom of add and remove.
61 * - Resize operations are protected by a mutex.
62 * - The removal operation is split in two parts: first, a "removed"
63 * flag is set in the next pointer within the node to remove. Then,
64 * a "garbage collection" is performed in the bucket containing the
65 * removed node (from the start of the bucket up to the removed node).
66 * All encountered nodes with "removed" flag set in their next
67 * pointers are removed from the linked-list. If the cmpxchg used for
68 * removal fails (due to concurrent garbage-collection or concurrent
69 * add), we retry from the beginning of the bucket. This ensures that
70 * the node with "removed" flag set is removed from the hash table
71 * (not visible to lookups anymore) before the RCU read-side critical
72 * section held across removal ends. Furthermore, this ensures that
73 * the node with "removed" flag set is removed from the linked-list
74 * before its memory is reclaimed. Only the thread which removal
75 * successfully set the "removed" flag (with a cmpxchg) into a node's
76 * next pointer is considered to have succeeded its removal (and thus
77 * owns the node to reclaim). Because we garbage-collect starting from
78 * an invariant node (the start-of-bucket bucket node) up to the
79 * "removed" node (or find a reverse-hash that is higher), we are sure
80 * that a successful traversal of the chain leads to a chain that is
81 * present in the linked-list (the start node is never removed) and
82 * that is does not contain the "removed" node anymore, even if
83 * concurrent delete/add operations are changing the structure of the
84 * list concurrently.
85 * - The add operation performs gargage collection of buckets if it
86 * encounters nodes with removed flag set in the bucket where it wants
87 * to add its new node. This ensures lock-freedom of add operation by
88 * helping the remover unlink nodes from the list rather than to wait
89 * for it do to so.
90 * - A RCU "order table" indexed by log2(hash index) is copied and
91 * expanded by the resize operation. This order table allows finding
92 * the "bucket node" tables.
93 * - There is one bucket node table per hash index order. The size of
94 * each bucket node table is half the number of hashes contained in
95 * this order (except for order 0).
96 * - synchronzie_rcu is used to garbage-collect the old bucket node table.
97 * - The per-order bucket node tables contain a compact version of the
98 * hash table nodes. These tables are invariant after they are
99 * populated into the hash table.
100 *
101 * Bucket node tables:
102 *
103 * hash table hash table the last all bucket node tables
104 * order size bucket node 0 1 2 3 4 5 6(index)
105 * table size
106 * 0 1 1 1
107 * 1 2 1 1 1
108 * 2 4 2 1 1 2
109 * 3 8 4 1 1 2 4
110 * 4 16 8 1 1 2 4 8
111 * 5 32 16 1 1 2 4 8 16
112 * 6 64 32 1 1 2 4 8 16 32
113 *
114 * When growing/shrinking, we only focus on the last bucket node table
115 * which size is (!order ? 1 : (1 << (order -1))).
116 *
117 * Example for growing/shrinking:
118 * grow hash table from order 5 to 6: init the index=6 bucket node table
119 * shrink hash table from order 6 to 5: fini the index=6 bucket node table
120 *
121 * A bit of ascii art explanation:
122 *
123 * Order index is the off-by-one compare to the actual power of 2 because
124 * we use index 0 to deal with the 0 special-case.
125 *
126 * This shows the nodes for a small table ordered by reversed bits:
127 *
128 * bits reverse
129 * 0 000 000
130 * 4 100 001
131 * 2 010 010
132 * 6 110 011
133 * 1 001 100
134 * 5 101 101
135 * 3 011 110
136 * 7 111 111
137 *
138 * This shows the nodes in order of non-reversed bits, linked by
139 * reversed-bit order.
140 *
141 * order bits reverse
142 * 0 0 000 000
143 * 1 | 1 001 100 <-
144 * 2 | | 2 010 010 <- |
145 * | | | 3 011 110 | <- |
146 * 3 -> | | | 4 100 001 | |
147 * -> | | 5 101 101 |
148 * -> | 6 110 011
149 * -> 7 111 111
150 */
151
152 #define _LGPL_SOURCE
153 #include <stdlib.h>
154 #include <errno.h>
155 #include <assert.h>
156 #include <stdio.h>
157 #include <stdint.h>
158 #include <string.h>
159
160 #include "config.h"
161 #include <urcu.h>
162 #include <urcu-call-rcu.h>
163 #include <urcu/arch.h>
164 #include <urcu/uatomic.h>
165 #include <urcu/compiler.h>
166 #include <urcu/rculfhash.h>
167 #include <stdio.h>
168 #include <pthread.h>
169
170 #ifdef DEBUG
171 #define dbg_printf(fmt, args...) printf("[debug rculfhash] " fmt, ## args)
172 #else
173 #define dbg_printf(fmt, args...)
174 #endif
175
176 /*
177 * Split-counters lazily update the global counter each 1024
178 * addition/removal. It automatically keeps track of resize required.
179 * We use the bucket length as indicator for need to expand for small
180 * tables and machines lacking per-cpu data suppport.
181 */
182 #define COUNT_COMMIT_ORDER 10
183 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
184 #define CHAIN_LEN_TARGET 1
185 #define CHAIN_LEN_RESIZE_THRESHOLD 3
186
187 /*
188 * Define the minimum table size.
189 */
190 #define MIN_TABLE_SIZE 1
191
192 #if (CAA_BITS_PER_LONG == 32)
193 #define MAX_TABLE_ORDER 32
194 #else
195 #define MAX_TABLE_ORDER 64
196 #endif
197
198 /*
199 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
200 */
201 #define MIN_PARTITION_PER_THREAD_ORDER 12
202 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
203
204 #ifndef min
205 #define min(a, b) ((a) < (b) ? (a) : (b))
206 #endif
207
208 #ifndef max
209 #define max(a, b) ((a) > (b) ? (a) : (b))
210 #endif
211
212 /*
213 * The removed flag needs to be updated atomically with the pointer.
214 * It indicates that no node must attach to the node scheduled for
215 * removal, and that node garbage collection must be performed.
216 * The bucket flag does not require to be updated atomically with the
217 * pointer, but it is added as a pointer low bit flag to save space.
218 */
219 #define REMOVED_FLAG (1UL << 0)
220 #define BUCKET_FLAG (1UL << 1)
221 #define FLAGS_MASK ((1UL << 2) - 1)
222
223 /* Value of the end pointer. Should not interact with flags. */
224 #define END_VALUE NULL
225
226 /*
227 * ht_items_count: Split-counters counting the number of node addition
228 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
229 * is set at hash table creation.
230 *
231 * These are free-running counters, never reset to zero. They count the
232 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
233 * operations to update the global counter. We choose a power-of-2 value
234 * for the trigger to deal with 32 or 64-bit overflow of the counter.
235 */
236 struct ht_items_count {
237 unsigned long add, del;
238 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
239
240 /*
241 * rcu_level: Contains the per order-index-level bucket node table. The
242 * size of each bucket node table is half the number of hashes contained
243 * in this order (except for order 0). The minimum allocation size
244 * parameter allows combining the bucket node arrays of the lowermost
245 * levels to improve cache locality for small index orders.
246 */
247 struct rcu_level {
248 /* Note: manually update allocation length when adding a field */
249 struct cds_lfht_node nodes[0];
250 };
251
252 /*
253 * rcu_table: Contains the size and desired new size if a resize
254 * operation is in progress, as well as the statically-sized array of
255 * rcu_level pointers.
256 */
257 struct rcu_table {
258 unsigned long size; /* always a power of 2, shared (RCU) */
259 unsigned long resize_target;
260 int resize_initiated;
261 struct rcu_level *tbl[MAX_TABLE_ORDER];
262 };
263
264 /*
265 * cds_lfht: Top-level data structure representing a lock-free hash
266 * table. Defined in the implementation file to make it be an opaque
267 * cookie to users.
268 */
269 struct cds_lfht {
270 struct rcu_table t;
271 unsigned long min_alloc_order;
272 unsigned long min_alloc_size;
273 int flags;
274 /*
275 * We need to put the work threads offline (QSBR) when taking this
276 * mutex, because we use synchronize_rcu within this mutex critical
277 * section, which waits on read-side critical sections, and could
278 * therefore cause grace-period deadlock if we hold off RCU G.P.
279 * completion.
280 */
281 pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */
282 unsigned int in_progress_resize, in_progress_destroy;
283 void (*cds_lfht_call_rcu)(struct rcu_head *head,
284 void (*func)(struct rcu_head *head));
285 void (*cds_lfht_synchronize_rcu)(void);
286 void (*cds_lfht_rcu_read_lock)(void);
287 void (*cds_lfht_rcu_read_unlock)(void);
288 void (*cds_lfht_rcu_thread_offline)(void);
289 void (*cds_lfht_rcu_thread_online)(void);
290 void (*cds_lfht_rcu_register_thread)(void);
291 void (*cds_lfht_rcu_unregister_thread)(void);
292 pthread_attr_t *resize_attr; /* Resize threads attributes */
293 long count; /* global approximate item count */
294 struct ht_items_count *split_count; /* split item count */
295 };
296
297 /*
298 * rcu_resize_work: Contains arguments passed to RCU worker thread
299 * responsible for performing lazy resize.
300 */
301 struct rcu_resize_work {
302 struct rcu_head head;
303 struct cds_lfht *ht;
304 };
305
306 /*
307 * partition_resize_work: Contains arguments passed to worker threads
308 * executing the hash table resize on partitions of the hash table
309 * assigned to each processor's worker thread.
310 */
311 struct partition_resize_work {
312 pthread_t thread_id;
313 struct cds_lfht *ht;
314 unsigned long i, start, len;
315 void (*fct)(struct cds_lfht *ht, unsigned long i,
316 unsigned long start, unsigned long len);
317 };
318
319 static
320 void _cds_lfht_add(struct cds_lfht *ht,
321 cds_lfht_match_fct match,
322 const void *key,
323 unsigned long size,
324 struct cds_lfht_node *node,
325 struct cds_lfht_iter *unique_ret,
326 int bucket);
327
328 /*
329 * Algorithm to reverse bits in a word by lookup table, extended to
330 * 64-bit words.
331 * Source:
332 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
333 * Originally from Public Domain.
334 */
335
336 static const uint8_t BitReverseTable256[256] =
337 {
338 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
339 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
340 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
341 R6(0), R6(2), R6(1), R6(3)
342 };
343 #undef R2
344 #undef R4
345 #undef R6
346
347 static
348 uint8_t bit_reverse_u8(uint8_t v)
349 {
350 return BitReverseTable256[v];
351 }
352
353 static __attribute__((unused))
354 uint32_t bit_reverse_u32(uint32_t v)
355 {
356 return ((uint32_t) bit_reverse_u8(v) << 24) |
357 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
358 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
359 ((uint32_t) bit_reverse_u8(v >> 24));
360 }
361
362 static __attribute__((unused))
363 uint64_t bit_reverse_u64(uint64_t v)
364 {
365 return ((uint64_t) bit_reverse_u8(v) << 56) |
366 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
367 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
368 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
369 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
370 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
371 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
372 ((uint64_t) bit_reverse_u8(v >> 56));
373 }
374
375 static
376 unsigned long bit_reverse_ulong(unsigned long v)
377 {
378 #if (CAA_BITS_PER_LONG == 32)
379 return bit_reverse_u32(v);
380 #else
381 return bit_reverse_u64(v);
382 #endif
383 }
384
385 /*
386 * fls: returns the position of the most significant bit.
387 * Returns 0 if no bit is set, else returns the position of the most
388 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
389 */
390 #if defined(__i386) || defined(__x86_64)
391 static inline
392 unsigned int fls_u32(uint32_t x)
393 {
394 int r;
395
396 asm("bsrl %1,%0\n\t"
397 "jnz 1f\n\t"
398 "movl $-1,%0\n\t"
399 "1:\n\t"
400 : "=r" (r) : "rm" (x));
401 return r + 1;
402 }
403 #define HAS_FLS_U32
404 #endif
405
406 #if defined(__x86_64)
407 static inline
408 unsigned int fls_u64(uint64_t x)
409 {
410 long r;
411
412 asm("bsrq %1,%0\n\t"
413 "jnz 1f\n\t"
414 "movq $-1,%0\n\t"
415 "1:\n\t"
416 : "=r" (r) : "rm" (x));
417 return r + 1;
418 }
419 #define HAS_FLS_U64
420 #endif
421
422 #ifndef HAS_FLS_U64
423 static __attribute__((unused))
424 unsigned int fls_u64(uint64_t x)
425 {
426 unsigned int r = 64;
427
428 if (!x)
429 return 0;
430
431 if (!(x & 0xFFFFFFFF00000000ULL)) {
432 x <<= 32;
433 r -= 32;
434 }
435 if (!(x & 0xFFFF000000000000ULL)) {
436 x <<= 16;
437 r -= 16;
438 }
439 if (!(x & 0xFF00000000000000ULL)) {
440 x <<= 8;
441 r -= 8;
442 }
443 if (!(x & 0xF000000000000000ULL)) {
444 x <<= 4;
445 r -= 4;
446 }
447 if (!(x & 0xC000000000000000ULL)) {
448 x <<= 2;
449 r -= 2;
450 }
451 if (!(x & 0x8000000000000000ULL)) {
452 x <<= 1;
453 r -= 1;
454 }
455 return r;
456 }
457 #endif
458
459 #ifndef HAS_FLS_U32
460 static __attribute__((unused))
461 unsigned int fls_u32(uint32_t x)
462 {
463 unsigned int r = 32;
464
465 if (!x)
466 return 0;
467 if (!(x & 0xFFFF0000U)) {
468 x <<= 16;
469 r -= 16;
470 }
471 if (!(x & 0xFF000000U)) {
472 x <<= 8;
473 r -= 8;
474 }
475 if (!(x & 0xF0000000U)) {
476 x <<= 4;
477 r -= 4;
478 }
479 if (!(x & 0xC0000000U)) {
480 x <<= 2;
481 r -= 2;
482 }
483 if (!(x & 0x80000000U)) {
484 x <<= 1;
485 r -= 1;
486 }
487 return r;
488 }
489 #endif
490
491 unsigned int fls_ulong(unsigned long x)
492 {
493 #if (CAA_BITS_PER_LONG == 32)
494 return fls_u32(x);
495 #else
496 return fls_u64(x);
497 #endif
498 }
499
500 /*
501 * Return the minimum order for which x <= (1UL << order).
502 * Return -1 if x is 0.
503 */
504 int get_count_order_u32(uint32_t x)
505 {
506 if (!x)
507 return -1;
508
509 return fls_u32(x - 1);
510 }
511
512 /*
513 * Return the minimum order for which x <= (1UL << order).
514 * Return -1 if x is 0.
515 */
516 int get_count_order_ulong(unsigned long x)
517 {
518 if (!x)
519 return -1;
520
521 return fls_ulong(x - 1);
522 }
523
524 #ifdef POISON_FREE
525 #define poison_free(ptr) \
526 do { \
527 if (ptr) { \
528 memset(ptr, 0x42, sizeof(*(ptr))); \
529 free(ptr); \
530 } \
531 } while (0)
532 #else
533 #define poison_free(ptr) free(ptr)
534 #endif
535
536 static
537 void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
538
539 static
540 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
541 unsigned long count);
542
543 static long nr_cpus_mask = -1;
544 static long split_count_mask = -1;
545
546 #if defined(HAVE_SYSCONF)
547 static void ht_init_nr_cpus_mask(void)
548 {
549 long maxcpus;
550
551 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
552 if (maxcpus <= 0) {
553 nr_cpus_mask = -2;
554 return;
555 }
556 /*
557 * round up number of CPUs to next power of two, so we
558 * can use & for modulo.
559 */
560 maxcpus = 1UL << get_count_order_ulong(maxcpus);
561 nr_cpus_mask = maxcpus - 1;
562 }
563 #else /* #if defined(HAVE_SYSCONF) */
564 static void ht_init_nr_cpus_mask(void)
565 {
566 nr_cpus_mask = -2;
567 }
568 #endif /* #else #if defined(HAVE_SYSCONF) */
569
570 static
571 void alloc_split_items_count(struct cds_lfht *ht)
572 {
573 struct ht_items_count *count;
574
575 if (nr_cpus_mask == -1) {
576 ht_init_nr_cpus_mask();
577 if (nr_cpus_mask < 0)
578 split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
579 else
580 split_count_mask = nr_cpus_mask;
581 }
582
583 assert(split_count_mask >= 0);
584
585 if (ht->flags & CDS_LFHT_ACCOUNTING) {
586 ht->split_count = calloc(split_count_mask + 1, sizeof(*count));
587 assert(ht->split_count);
588 } else {
589 ht->split_count = NULL;
590 }
591 }
592
593 static
594 void free_split_items_count(struct cds_lfht *ht)
595 {
596 poison_free(ht->split_count);
597 }
598
599 #if defined(HAVE_SCHED_GETCPU)
600 static
601 int ht_get_split_count_index(unsigned long hash)
602 {
603 int cpu;
604
605 assert(split_count_mask >= 0);
606 cpu = sched_getcpu();
607 if (caa_unlikely(cpu < 0))
608 return hash & split_count_mask;
609 else
610 return cpu & split_count_mask;
611 }
612 #else /* #if defined(HAVE_SCHED_GETCPU) */
613 static
614 int ht_get_split_count_index(unsigned long hash)
615 {
616 return hash & split_count_mask;
617 }
618 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
619
620 static
621 void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
622 {
623 unsigned long split_count;
624 int index;
625
626 if (caa_unlikely(!ht->split_count))
627 return;
628 index = ht_get_split_count_index(hash);
629 split_count = uatomic_add_return(&ht->split_count[index].add, 1);
630 if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
631 long count;
632
633 dbg_printf("add split count %lu\n", split_count);
634 count = uatomic_add_return(&ht->count,
635 1UL << COUNT_COMMIT_ORDER);
636 /* If power of 2 */
637 if (!(count & (count - 1))) {
638 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
639 return;
640 dbg_printf("add set global %ld\n", count);
641 cds_lfht_resize_lazy_count(ht, size,
642 count >> (CHAIN_LEN_TARGET - 1));
643 }
644 }
645 }
646
647 static
648 void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
649 {
650 unsigned long split_count;
651 int index;
652
653 if (caa_unlikely(!ht->split_count))
654 return;
655 index = ht_get_split_count_index(hash);
656 split_count = uatomic_add_return(&ht->split_count[index].del, 1);
657 if (caa_unlikely(!(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))) {
658 long count;
659
660 dbg_printf("del split count %lu\n", split_count);
661 count = uatomic_add_return(&ht->count,
662 -(1UL << COUNT_COMMIT_ORDER));
663 /* If power of 2 */
664 if (!(count & (count - 1))) {
665 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
666 return;
667 dbg_printf("del set global %ld\n", count);
668 /*
669 * Don't shrink table if the number of nodes is below a
670 * certain threshold.
671 */
672 if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
673 return;
674 cds_lfht_resize_lazy_count(ht, size,
675 count >> (CHAIN_LEN_TARGET - 1));
676 }
677 }
678 }
679
680 static
681 void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
682 {
683 unsigned long count;
684
685 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
686 return;
687 count = uatomic_read(&ht->count);
688 /*
689 * Use bucket-local length for small table expand and for
690 * environments lacking per-cpu data support.
691 */
692 if (count >= (1UL << COUNT_COMMIT_ORDER))
693 return;
694 if (chain_len > 100)
695 dbg_printf("WARNING: large chain length: %u.\n",
696 chain_len);
697 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
698 cds_lfht_resize_lazy_grow(ht, size,
699 get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
700 }
701
702 static
703 struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
704 {
705 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
706 }
707
708 static
709 int is_removed(struct cds_lfht_node *node)
710 {
711 return ((unsigned long) node) & REMOVED_FLAG;
712 }
713
714 static
715 struct cds_lfht_node *flag_removed(struct cds_lfht_node *node)
716 {
717 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG);
718 }
719
720 static
721 int is_bucket(struct cds_lfht_node *node)
722 {
723 return ((unsigned long) node) & BUCKET_FLAG;
724 }
725
726 static
727 struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node)
728 {
729 return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG);
730 }
731
732 static
733 struct cds_lfht_node *get_end(void)
734 {
735 return (struct cds_lfht_node *) END_VALUE;
736 }
737
738 static
739 int is_end(struct cds_lfht_node *node)
740 {
741 return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
742 }
743
744 static
745 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
746 unsigned long v)
747 {
748 unsigned long old1, old2;
749
750 old1 = uatomic_read(ptr);
751 do {
752 old2 = old1;
753 if (old2 >= v)
754 return old2;
755 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
756 return old2;
757 }
758
759 static
760 struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
761 unsigned long hash)
762 {
763 unsigned long index, order;
764
765 assert(size > 0);
766 index = hash & (size - 1);
767
768 if (index < ht->min_alloc_size) {
769 dbg_printf("lookup hash %lu index %lu order 0 aridx 0\n",
770 hash, index);
771 return &ht->t.tbl[0]->nodes[index];
772 }
773 /*
774 * equivalent to get_count_order_ulong(index + 1), but optimizes
775 * away the non-existing 0 special-case for
776 * get_count_order_ulong.
777 */
778 order = fls_ulong(index);
779 dbg_printf("lookup hash %lu index %lu order %lu aridx %lu\n",
780 hash, index, order, index & ((1UL << (order - 1)) - 1));
781 return &ht->t.tbl[order]->nodes[index & ((1UL << (order - 1)) - 1)];
782 }
783
784 /*
785 * Remove all logically deleted nodes from a bucket up to a certain node key.
786 */
787 static
788 void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node)
789 {
790 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
791
792 assert(!is_bucket(bucket));
793 assert(!is_removed(bucket));
794 assert(!is_bucket(node));
795 assert(!is_removed(node));
796 for (;;) {
797 iter_prev = bucket;
798 /* We can always skip the bucket node initially */
799 iter = rcu_dereference(iter_prev->next);
800 assert(!is_removed(iter));
801 assert(iter_prev->reverse_hash <= node->reverse_hash);
802 /*
803 * We should never be called with bucket (start of chain)
804 * and logically removed node (end of path compression
805 * marker) being the actual same node. This would be a
806 * bug in the algorithm implementation.
807 */
808 assert(bucket != node);
809 for (;;) {
810 if (caa_unlikely(is_end(iter)))
811 return;
812 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
813 return;
814 next = rcu_dereference(clear_flag(iter)->next);
815 if (caa_likely(is_removed(next)))
816 break;
817 iter_prev = clear_flag(iter);
818 iter = next;
819 }
820 assert(!is_removed(iter));
821 if (is_bucket(iter))
822 new_next = flag_bucket(clear_flag(next));
823 else
824 new_next = clear_flag(next);
825 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
826 }
827 return;
828 }
829
830 static
831 int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
832 struct cds_lfht_node *old_node,
833 struct cds_lfht_node *old_next,
834 struct cds_lfht_node *new_node)
835 {
836 struct cds_lfht_node *bucket, *ret_next;
837
838 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
839 return -ENOENT;
840
841 assert(!is_removed(old_node));
842 assert(!is_bucket(old_node));
843 assert(!is_removed(new_node));
844 assert(!is_bucket(new_node));
845 assert(new_node != old_node);
846 for (;;) {
847 /* Insert after node to be replaced */
848 if (is_removed(old_next)) {
849 /*
850 * Too late, the old node has been removed under us
851 * between lookup and replace. Fail.
852 */
853 return -ENOENT;
854 }
855 assert(!is_bucket(old_next));
856 assert(new_node != clear_flag(old_next));
857 new_node->next = clear_flag(old_next);
858 /*
859 * Here is the whole trick for lock-free replace: we add
860 * the replacement node _after_ the node we want to
861 * replace by atomically setting its next pointer at the
862 * same time we set its removal flag. Given that
863 * the lookups/get next use an iterator aware of the
864 * next pointer, they will either skip the old node due
865 * to the removal flag and see the new node, or use
866 * the old node, but will not see the new one.
867 */
868 ret_next = uatomic_cmpxchg(&old_node->next,
869 old_next, flag_removed(new_node));
870 if (ret_next == old_next)
871 break; /* We performed the replacement. */
872 old_next = ret_next;
873 }
874
875 /*
876 * Ensure that the old node is not visible to readers anymore:
877 * lookup for the node, and remove it (along with any other
878 * logically removed node) if found.
879 */
880 bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
881 _cds_lfht_gc_bucket(bucket, new_node);
882
883 assert(is_removed(rcu_dereference(old_node->next)));
884 return 0;
885 }
886
887 /*
888 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
889 * mode. A NULL unique_ret allows creation of duplicate keys.
890 */
891 static
892 void _cds_lfht_add(struct cds_lfht *ht,
893 cds_lfht_match_fct match,
894 const void *key,
895 unsigned long size,
896 struct cds_lfht_node *node,
897 struct cds_lfht_iter *unique_ret,
898 int bucket_flag)
899 {
900 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
901 *return_node;
902 struct cds_lfht_node *bucket;
903
904 assert(!is_bucket(node));
905 assert(!is_removed(node));
906 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
907 for (;;) {
908 uint32_t chain_len = 0;
909
910 /*
911 * iter_prev points to the non-removed node prior to the
912 * insert location.
913 */
914 iter_prev = bucket;
915 /* We can always skip the bucket node initially */
916 iter = rcu_dereference(iter_prev->next);
917 assert(iter_prev->reverse_hash <= node->reverse_hash);
918 for (;;) {
919 if (caa_unlikely(is_end(iter)))
920 goto insert;
921 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
922 goto insert;
923
924 /* bucket node is the first node of the identical-hash-value chain */
925 if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash)
926 goto insert;
927
928 next = rcu_dereference(clear_flag(iter)->next);
929 if (caa_unlikely(is_removed(next)))
930 goto gc_node;
931
932 /* uniquely add */
933 if (unique_ret
934 && !is_bucket(next)
935 && clear_flag(iter)->reverse_hash == node->reverse_hash) {
936 struct cds_lfht_iter d_iter = { .node = node, .next = iter, };
937
938 /*
939 * uniquely adding inserts the node as the first
940 * node of the identical-hash-value node chain.
941 *
942 * This semantic ensures no duplicated keys
943 * should ever be observable in the table
944 * (including observe one node by one node
945 * by forward iterations)
946 */
947 cds_lfht_next_duplicate(ht, match, key, &d_iter);
948 if (!d_iter.node)
949 goto insert;
950
951 *unique_ret = d_iter;
952 return;
953 }
954
955 /* Only account for identical reverse hash once */
956 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
957 && !is_bucket(next))
958 check_resize(ht, size, ++chain_len);
959 iter_prev = clear_flag(iter);
960 iter = next;
961 }
962
963 insert:
964 assert(node != clear_flag(iter));
965 assert(!is_removed(iter_prev));
966 assert(!is_removed(iter));
967 assert(iter_prev != node);
968 if (!bucket_flag)
969 node->next = clear_flag(iter);
970 else
971 node->next = flag_bucket(clear_flag(iter));
972 if (is_bucket(iter))
973 new_node = flag_bucket(node);
974 else
975 new_node = node;
976 if (uatomic_cmpxchg(&iter_prev->next, iter,
977 new_node) != iter) {
978 continue; /* retry */
979 } else {
980 return_node = node;
981 goto end;
982 }
983
984 gc_node:
985 assert(!is_removed(iter));
986 if (is_bucket(iter))
987 new_next = flag_bucket(clear_flag(next));
988 else
989 new_next = clear_flag(next);
990 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
991 /* retry */
992 }
993 end:
994 if (unique_ret) {
995 unique_ret->node = return_node;
996 /* unique_ret->next left unset, never used. */
997 }
998 }
999
1000 static
1001 int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
1002 struct cds_lfht_node *node,
1003 int bucket_removal)
1004 {
1005 struct cds_lfht_node *bucket, *next, *old;
1006
1007 if (!node) /* Return -ENOENT if asked to delete NULL node */
1008 return -ENOENT;
1009
1010 /* logically delete the node */
1011 assert(!is_bucket(node));
1012 assert(!is_removed(node));
1013 old = rcu_dereference(node->next);
1014 do {
1015 struct cds_lfht_node *new_next;
1016
1017 next = old;
1018 if (caa_unlikely(is_removed(next)))
1019 return -ENOENT;
1020 if (bucket_removal)
1021 assert(is_bucket(next));
1022 else
1023 assert(!is_bucket(next));
1024 new_next = flag_removed(next);
1025 old = uatomic_cmpxchg(&node->next, next, new_next);
1026 } while (old != next);
1027 /* We performed the (logical) deletion. */
1028
1029 /*
1030 * Ensure that the node is not visible to readers anymore: lookup for
1031 * the node, and remove it (along with any other logically removed node)
1032 * if found.
1033 */
1034 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
1035 _cds_lfht_gc_bucket(bucket, node);
1036
1037 assert(is_removed(rcu_dereference(node->next)));
1038 return 0;
1039 }
1040
1041 static
1042 void *partition_resize_thread(void *arg)
1043 {
1044 struct partition_resize_work *work = arg;
1045
1046 work->ht->cds_lfht_rcu_register_thread();
1047 work->fct(work->ht, work->i, work->start, work->len);
1048 work->ht->cds_lfht_rcu_unregister_thread();
1049 return NULL;
1050 }
1051
1052 static
1053 void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
1054 unsigned long len,
1055 void (*fct)(struct cds_lfht *ht, unsigned long i,
1056 unsigned long start, unsigned long len))
1057 {
1058 unsigned long partition_len;
1059 struct partition_resize_work *work;
1060 int thread, ret;
1061 unsigned long nr_threads;
1062
1063 /*
1064 * Note: nr_cpus_mask + 1 is always power of 2.
1065 * We spawn just the number of threads we need to satisfy the minimum
1066 * partition size, up to the number of CPUs in the system.
1067 */
1068 if (nr_cpus_mask > 0) {
1069 nr_threads = min(nr_cpus_mask + 1,
1070 len >> MIN_PARTITION_PER_THREAD_ORDER);
1071 } else {
1072 nr_threads = 1;
1073 }
1074 partition_len = len >> get_count_order_ulong(nr_threads);
1075 work = calloc(nr_threads, sizeof(*work));
1076 assert(work);
1077 for (thread = 0; thread < nr_threads; thread++) {
1078 work[thread].ht = ht;
1079 work[thread].i = i;
1080 work[thread].len = partition_len;
1081 work[thread].start = thread * partition_len;
1082 work[thread].fct = fct;
1083 ret = pthread_create(&(work[thread].thread_id), ht->resize_attr,
1084 partition_resize_thread, &work[thread]);
1085 assert(!ret);
1086 }
1087 for (thread = 0; thread < nr_threads; thread++) {
1088 ret = pthread_join(work[thread].thread_id, NULL);
1089 assert(!ret);
1090 }
1091 free(work);
1092 }
1093
1094 /*
1095 * Holding RCU read lock to protect _cds_lfht_add against memory
1096 * reclaim that could be performed by other call_rcu worker threads (ABA
1097 * problem).
1098 *
1099 * When we reach a certain length, we can split this population phase over
1100 * many worker threads, based on the number of CPUs available in the system.
1101 * This should therefore take care of not having the expand lagging behind too
1102 * many concurrent insertion threads by using the scheduler's ability to
1103 * schedule bucket node population fairly with insertions.
1104 */
1105 static
1106 void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
1107 unsigned long start, unsigned long len)
1108 {
1109 unsigned long j;
1110
1111 assert(i > ht->min_alloc_order);
1112 ht->cds_lfht_rcu_read_lock();
1113 for (j = start; j < start + len; j++) {
1114 struct cds_lfht_node *new_node = &ht->t.tbl[i]->nodes[j];
1115
1116 dbg_printf("init populate: i %lu j %lu hash %lu\n",
1117 i, j, (1UL << (i - 1)) + j);
1118 new_node->reverse_hash =
1119 bit_reverse_ulong((1UL << (i - 1)) + j);
1120 _cds_lfht_add(ht, NULL, NULL, 1UL << (i - 1),
1121 new_node, NULL, 1);
1122 }
1123 ht->cds_lfht_rcu_read_unlock();
1124 }
1125
1126 static
1127 void init_table_populate(struct cds_lfht *ht, unsigned long i,
1128 unsigned long len)
1129 {
1130 assert(nr_cpus_mask != -1);
1131 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
1132 ht->cds_lfht_rcu_thread_online();
1133 init_table_populate_partition(ht, i, 0, len);
1134 ht->cds_lfht_rcu_thread_offline();
1135 return;
1136 }
1137 partition_resize_helper(ht, i, len, init_table_populate_partition);
1138 }
1139
1140 static
1141 void init_table(struct cds_lfht *ht,
1142 unsigned long first_order, unsigned long last_order)
1143 {
1144 unsigned long i;
1145
1146 dbg_printf("init table: first_order %lu last_order %lu\n",
1147 first_order, last_order);
1148 assert(first_order > ht->min_alloc_order);
1149 for (i = first_order; i <= last_order; i++) {
1150 unsigned long len;
1151
1152 len = 1UL << (i - 1);
1153 dbg_printf("init order %lu len: %lu\n", i, len);
1154
1155 /* Stop expand if the resize target changes under us */
1156 if (CMM_LOAD_SHARED(ht->t.resize_target) < (1UL << i))
1157 break;
1158
1159 ht->t.tbl[i] = calloc(1, len * sizeof(struct cds_lfht_node));
1160 assert(ht->t.tbl[i]);
1161
1162 /*
1163 * Set all bucket nodes reverse hash values for a level and
1164 * link all bucket nodes into the table.
1165 */
1166 init_table_populate(ht, i, len);
1167
1168 /*
1169 * Update table size.
1170 */
1171 cmm_smp_wmb(); /* populate data before RCU size */
1172 CMM_STORE_SHARED(ht->t.size, 1UL << i);
1173
1174 dbg_printf("init new size: %lu\n", 1UL << i);
1175 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1176 break;
1177 }
1178 }
1179
1180 /*
1181 * Holding RCU read lock to protect _cds_lfht_remove against memory
1182 * reclaim that could be performed by other call_rcu worker threads (ABA
1183 * problem).
1184 * For a single level, we logically remove and garbage collect each node.
1185 *
1186 * As a design choice, we perform logical removal and garbage collection on a
1187 * node-per-node basis to simplify this algorithm. We also assume keeping good
1188 * cache locality of the operation would overweight possible performance gain
1189 * that could be achieved by batching garbage collection for multiple levels.
1190 * However, this would have to be justified by benchmarks.
1191 *
1192 * Concurrent removal and add operations are helping us perform garbage
1193 * collection of logically removed nodes. We guarantee that all logically
1194 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1195 * invoked to free a hole level of bucket nodes (after a grace period).
1196 *
1197 * Logical removal and garbage collection can therefore be done in batch or on a
1198 * node-per-node basis, as long as the guarantee above holds.
1199 *
1200 * When we reach a certain length, we can split this removal over many worker
1201 * threads, based on the number of CPUs available in the system. This should
1202 * take care of not letting resize process lag behind too many concurrent
1203 * updater threads actively inserting into the hash table.
1204 */
1205 static
1206 void remove_table_partition(struct cds_lfht *ht, unsigned long i,
1207 unsigned long start, unsigned long len)
1208 {
1209 unsigned long j;
1210
1211 assert(i > ht->min_alloc_order);
1212 ht->cds_lfht_rcu_read_lock();
1213 for (j = start; j < start + len; j++) {
1214 struct cds_lfht_node *fini_node = &ht->t.tbl[i]->nodes[j];
1215
1216 dbg_printf("remove entry: i %lu j %lu hash %lu\n",
1217 i, j, (1UL << (i - 1)) + j);
1218 fini_node->reverse_hash =
1219 bit_reverse_ulong((1UL << (i - 1)) + j);
1220 (void) _cds_lfht_del(ht, 1UL << (i - 1), fini_node, 1);
1221 }
1222 ht->cds_lfht_rcu_read_unlock();
1223 }
1224
1225 static
1226 void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
1227 {
1228
1229 assert(nr_cpus_mask != -1);
1230 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
1231 ht->cds_lfht_rcu_thread_online();
1232 remove_table_partition(ht, i, 0, len);
1233 ht->cds_lfht_rcu_thread_offline();
1234 return;
1235 }
1236 partition_resize_helper(ht, i, len, remove_table_partition);
1237 }
1238
1239 static
1240 void fini_table(struct cds_lfht *ht,
1241 unsigned long first_order, unsigned long last_order)
1242 {
1243 long i;
1244 void *free_by_rcu = NULL;
1245
1246 dbg_printf("fini table: first_order %lu last_order %lu\n",
1247 first_order, last_order);
1248 assert(first_order > ht->min_alloc_order);
1249 for (i = last_order; i >= first_order; i--) {
1250 unsigned long len;
1251
1252 len = 1UL << (i - 1);
1253 dbg_printf("fini order %lu len: %lu\n", i, len);
1254
1255 /* Stop shrink if the resize target changes under us */
1256 if (CMM_LOAD_SHARED(ht->t.resize_target) > (1UL << (i - 1)))
1257 break;
1258
1259 cmm_smp_wmb(); /* populate data before RCU size */
1260 CMM_STORE_SHARED(ht->t.size, 1UL << (i - 1));
1261
1262 /*
1263 * We need to wait for all add operations to reach Q.S. (and
1264 * thus use the new table for lookups) before we can start
1265 * releasing the old bucket nodes. Otherwise their lookup will
1266 * return a logically removed node as insert position.
1267 */
1268 ht->cds_lfht_synchronize_rcu();
1269 if (free_by_rcu)
1270 free(free_by_rcu);
1271
1272 /*
1273 * Set "removed" flag in bucket nodes about to be removed.
1274 * Unlink all now-logically-removed bucket node pointers.
1275 * Concurrent add/remove operation are helping us doing
1276 * the gc.
1277 */
1278 remove_table(ht, i, len);
1279
1280 free_by_rcu = ht->t.tbl[i];
1281
1282 dbg_printf("fini new size: %lu\n", 1UL << i);
1283 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1284 break;
1285 }
1286
1287 if (free_by_rcu) {
1288 ht->cds_lfht_synchronize_rcu();
1289 free(free_by_rcu);
1290 }
1291 }
1292
1293 static
1294 void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size)
1295 {
1296 struct cds_lfht_node *prev, *node;
1297 unsigned long order, len, i, j;
1298
1299 ht->t.tbl[0] = calloc(1, ht->min_alloc_size * sizeof(struct cds_lfht_node));
1300 assert(ht->t.tbl[0]);
1301
1302 dbg_printf("create bucket: order %lu index %lu hash %lu\n", 0, 0, 0);
1303 ht->t.tbl[0]->nodes[0].next = flag_bucket(get_end());
1304 ht->t.tbl[0]->nodes[0].reverse_hash = 0;
1305
1306 for (order = 1; order < get_count_order_ulong(size) + 1; order++) {
1307 len = 1UL << (order - 1);
1308 if (order <= ht->min_alloc_order) {
1309 ht->t.tbl[order] = (struct rcu_level *) (ht->t.tbl[0]->nodes + len);
1310 } else {
1311 ht->t.tbl[order] = calloc(1, len * sizeof(struct cds_lfht_node));
1312 assert(ht->t.tbl[order]);
1313 }
1314
1315 i = 0;
1316 prev = ht->t.tbl[i]->nodes;
1317 for (j = 0; j < len; j++) {
1318 if (j & (j - 1)) { /* Between power of 2 */
1319 prev++;
1320 } else if (j) { /* At each power of 2 */
1321 i++;
1322 prev = ht->t.tbl[i]->nodes;
1323 }
1324
1325 node = &ht->t.tbl[order]->nodes[j];
1326 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1327 order, j, j + len);
1328 node->next = prev->next;
1329 assert(is_bucket(node->next));
1330 node->reverse_hash = bit_reverse_ulong(j + len);
1331 prev->next = flag_bucket(node);
1332 }
1333 }
1334 }
1335
1336 struct cds_lfht *_cds_lfht_new(unsigned long init_size,
1337 unsigned long min_alloc_size,
1338 int flags,
1339 void (*cds_lfht_call_rcu)(struct rcu_head *head,
1340 void (*func)(struct rcu_head *head)),
1341 void (*cds_lfht_synchronize_rcu)(void),
1342 void (*cds_lfht_rcu_read_lock)(void),
1343 void (*cds_lfht_rcu_read_unlock)(void),
1344 void (*cds_lfht_rcu_thread_offline)(void),
1345 void (*cds_lfht_rcu_thread_online)(void),
1346 void (*cds_lfht_rcu_register_thread)(void),
1347 void (*cds_lfht_rcu_unregister_thread)(void),
1348 pthread_attr_t *attr)
1349 {
1350 struct cds_lfht *ht;
1351 unsigned long order;
1352
1353 /* min_alloc_size must be power of two */
1354 if (!min_alloc_size || (min_alloc_size & (min_alloc_size - 1)))
1355 return NULL;
1356 /* init_size must be power of two */
1357 if (!init_size || (init_size & (init_size - 1)))
1358 return NULL;
1359 min_alloc_size = max(min_alloc_size, MIN_TABLE_SIZE);
1360 init_size = max(init_size, min_alloc_size);
1361 ht = calloc(1, sizeof(struct cds_lfht));
1362 assert(ht);
1363 ht->flags = flags;
1364 ht->cds_lfht_call_rcu = cds_lfht_call_rcu;
1365 ht->cds_lfht_synchronize_rcu = cds_lfht_synchronize_rcu;
1366 ht->cds_lfht_rcu_read_lock = cds_lfht_rcu_read_lock;
1367 ht->cds_lfht_rcu_read_unlock = cds_lfht_rcu_read_unlock;
1368 ht->cds_lfht_rcu_thread_offline = cds_lfht_rcu_thread_offline;
1369 ht->cds_lfht_rcu_thread_online = cds_lfht_rcu_thread_online;
1370 ht->cds_lfht_rcu_register_thread = cds_lfht_rcu_register_thread;
1371 ht->cds_lfht_rcu_unregister_thread = cds_lfht_rcu_unregister_thread;
1372 ht->resize_attr = attr;
1373 alloc_split_items_count(ht);
1374 /* this mutex should not nest in read-side C.S. */
1375 pthread_mutex_init(&ht->resize_mutex, NULL);
1376 order = get_count_order_ulong(init_size);
1377 ht->t.resize_target = 1UL << order;
1378 ht->min_alloc_size = min_alloc_size;
1379 ht->min_alloc_order = get_count_order_ulong(min_alloc_size);
1380 cds_lfht_create_bucket(ht, 1UL << order);
1381 ht->t.size = 1UL << order;
1382 return ht;
1383 }
1384
1385 void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
1386 cds_lfht_match_fct match, const void *key,
1387 struct cds_lfht_iter *iter)
1388 {
1389 struct cds_lfht_node *node, *next, *bucket;
1390 unsigned long reverse_hash, size;
1391
1392 reverse_hash = bit_reverse_ulong(hash);
1393
1394 size = rcu_dereference(ht->t.size);
1395 bucket = lookup_bucket(ht, size, hash);
1396 /* We can always skip the bucket node initially */
1397 node = rcu_dereference(bucket->next);
1398 node = clear_flag(node);
1399 for (;;) {
1400 if (caa_unlikely(is_end(node))) {
1401 node = next = NULL;
1402 break;
1403 }
1404 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
1405 node = next = NULL;
1406 break;
1407 }
1408 next = rcu_dereference(node->next);
1409 assert(node == clear_flag(node));
1410 if (caa_likely(!is_removed(next))
1411 && !is_bucket(next)
1412 && node->reverse_hash == reverse_hash
1413 && caa_likely(match(node, key))) {
1414 break;
1415 }
1416 node = clear_flag(next);
1417 }
1418 assert(!node || !is_bucket(rcu_dereference(node->next)));
1419 iter->node = node;
1420 iter->next = next;
1421 }
1422
1423 void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
1424 const void *key, struct cds_lfht_iter *iter)
1425 {
1426 struct cds_lfht_node *node, *next;
1427 unsigned long reverse_hash;
1428
1429 node = iter->node;
1430 reverse_hash = node->reverse_hash;
1431 next = iter->next;
1432 node = clear_flag(next);
1433
1434 for (;;) {
1435 if (caa_unlikely(is_end(node))) {
1436 node = next = NULL;
1437 break;
1438 }
1439 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
1440 node = next = NULL;
1441 break;
1442 }
1443 next = rcu_dereference(node->next);
1444 if (caa_likely(!is_removed(next))
1445 && !is_bucket(next)
1446 && caa_likely(match(node, key))) {
1447 break;
1448 }
1449 node = clear_flag(next);
1450 }
1451 assert(!node || !is_bucket(rcu_dereference(node->next)));
1452 iter->node = node;
1453 iter->next = next;
1454 }
1455
1456 void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1457 {
1458 struct cds_lfht_node *node, *next;
1459
1460 node = clear_flag(iter->next);
1461 for (;;) {
1462 if (caa_unlikely(is_end(node))) {
1463 node = next = NULL;
1464 break;
1465 }
1466 next = rcu_dereference(node->next);
1467 if (caa_likely(!is_removed(next))
1468 && !is_bucket(next)) {
1469 break;
1470 }
1471 node = clear_flag(next);
1472 }
1473 assert(!node || !is_bucket(rcu_dereference(node->next)));
1474 iter->node = node;
1475 iter->next = next;
1476 }
1477
1478 void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1479 {
1480 struct cds_lfht_node *lookup;
1481
1482 /*
1483 * Get next after first bucket node. The first bucket node is the
1484 * first node of the linked list.
1485 */
1486 lookup = &ht->t.tbl[0]->nodes[0];
1487 iter->next = lookup->next;
1488 cds_lfht_next(ht, iter);
1489 }
1490
1491 void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1492 struct cds_lfht_node *node)
1493 {
1494 unsigned long size;
1495
1496 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
1497 size = rcu_dereference(ht->t.size);
1498 _cds_lfht_add(ht, NULL, NULL, size, node, NULL, 0);
1499 ht_count_add(ht, size, hash);
1500 }
1501
1502 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
1503 unsigned long hash,
1504 cds_lfht_match_fct match,
1505 const void *key,
1506 struct cds_lfht_node *node)
1507 {
1508 unsigned long size;
1509 struct cds_lfht_iter iter;
1510
1511 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
1512 size = rcu_dereference(ht->t.size);
1513 _cds_lfht_add(ht, match, key, size, node, &iter, 0);
1514 if (iter.node == node)
1515 ht_count_add(ht, size, hash);
1516 return iter.node;
1517 }
1518
1519 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
1520 unsigned long hash,
1521 cds_lfht_match_fct match,
1522 const void *key,
1523 struct cds_lfht_node *node)
1524 {
1525 unsigned long size;
1526 struct cds_lfht_iter iter;
1527
1528 node->reverse_hash = bit_reverse_ulong((unsigned long) hash);
1529 size = rcu_dereference(ht->t.size);
1530 for (;;) {
1531 _cds_lfht_add(ht, match, key, size, node, &iter, 0);
1532 if (iter.node == node) {
1533 ht_count_add(ht, size, hash);
1534 return NULL;
1535 }
1536
1537 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1538 return iter.node;
1539 }
1540 }
1541
1542 int cds_lfht_replace(struct cds_lfht *ht, struct cds_lfht_iter *old_iter,
1543 struct cds_lfht_node *new_node)
1544 {
1545 unsigned long size;
1546
1547 size = rcu_dereference(ht->t.size);
1548 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1549 new_node);
1550 }
1551
1552 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1553 {
1554 unsigned long size, hash;
1555 int ret;
1556
1557 size = rcu_dereference(ht->t.size);
1558 ret = _cds_lfht_del(ht, size, iter->node, 0);
1559 if (!ret) {
1560 hash = bit_reverse_ulong(iter->node->reverse_hash);
1561 ht_count_del(ht, size, hash);
1562 }
1563 return ret;
1564 }
1565
1566 static
1567 int cds_lfht_delete_bucket(struct cds_lfht *ht)
1568 {
1569 struct cds_lfht_node *node;
1570 unsigned long order, i, size;
1571
1572 /* Check that the table is empty */
1573 node = &ht->t.tbl[0]->nodes[0];
1574 do {
1575 node = clear_flag(node)->next;
1576 if (!is_bucket(node))
1577 return -EPERM;
1578 assert(!is_removed(node));
1579 } while (!is_end(node));
1580 /*
1581 * size accessed without rcu_dereference because hash table is
1582 * being destroyed.
1583 */
1584 size = ht->t.size;
1585 /* Internal sanity check: all nodes left should be bucket */
1586 for (order = 0; order < get_count_order_ulong(size) + 1; order++) {
1587 unsigned long len;
1588
1589 len = !order ? 1 : 1UL << (order - 1);
1590 for (i = 0; i < len; i++) {
1591 dbg_printf("delete order %lu i %lu hash %lu\n",
1592 order, i,
1593 bit_reverse_ulong(ht->t.tbl[order]->nodes[i].reverse_hash));
1594 assert(is_bucket(ht->t.tbl[order]->nodes[i].next));
1595 }
1596
1597 if (order == ht->min_alloc_order)
1598 poison_free(ht->t.tbl[0]);
1599 else if (order > ht->min_alloc_order)
1600 poison_free(ht->t.tbl[order]);
1601 /* Nothing to delete for order < ht->min_alloc_order */
1602 }
1603 return 0;
1604 }
1605
1606 /*
1607 * Should only be called when no more concurrent readers nor writers can
1608 * possibly access the table.
1609 */
1610 int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
1611 {
1612 int ret;
1613
1614 /* Wait for in-flight resize operations to complete */
1615 _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
1616 cmm_smp_mb(); /* Store destroy before load resize */
1617 while (uatomic_read(&ht->in_progress_resize))
1618 poll(NULL, 0, 100); /* wait for 100ms */
1619 ret = cds_lfht_delete_bucket(ht);
1620 if (ret)
1621 return ret;
1622 free_split_items_count(ht);
1623 if (attr)
1624 *attr = ht->resize_attr;
1625 poison_free(ht);
1626 return ret;
1627 }
1628
1629 void cds_lfht_count_nodes(struct cds_lfht *ht,
1630 long *approx_before,
1631 unsigned long *count,
1632 unsigned long *removed,
1633 long *approx_after)
1634 {
1635 struct cds_lfht_node *node, *next;
1636 unsigned long nr_bucket = 0;
1637
1638 *approx_before = 0;
1639 if (ht->split_count) {
1640 int i;
1641
1642 for (i = 0; i < split_count_mask + 1; i++) {
1643 *approx_before += uatomic_read(&ht->split_count[i].add);
1644 *approx_before -= uatomic_read(&ht->split_count[i].del);
1645 }
1646 }
1647
1648 *count = 0;
1649 *removed = 0;
1650
1651 /* Count non-bucket nodes in the table */
1652 node = &ht->t.tbl[0]->nodes[0];
1653 do {
1654 next = rcu_dereference(node->next);
1655 if (is_removed(next)) {
1656 if (!is_bucket(next))
1657 (*removed)++;
1658 else
1659 (nr_bucket)++;
1660 } else if (!is_bucket(next))
1661 (*count)++;
1662 else
1663 (nr_bucket)++;
1664 node = clear_flag(next);
1665 } while (!is_end(node));
1666 dbg_printf("number of bucket nodes: %lu\n", nr_bucket);
1667 *approx_after = 0;
1668 if (ht->split_count) {
1669 int i;
1670
1671 for (i = 0; i < split_count_mask + 1; i++) {
1672 *approx_after += uatomic_read(&ht->split_count[i].add);
1673 *approx_after -= uatomic_read(&ht->split_count[i].del);
1674 }
1675 }
1676 }
1677
1678 /* called with resize mutex held */
1679 static
1680 void _do_cds_lfht_grow(struct cds_lfht *ht,
1681 unsigned long old_size, unsigned long new_size)
1682 {
1683 unsigned long old_order, new_order;
1684
1685 old_order = get_count_order_ulong(old_size);
1686 new_order = get_count_order_ulong(new_size);
1687 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1688 old_size, old_order, new_size, new_order);
1689 assert(new_size > old_size);
1690 init_table(ht, old_order + 1, new_order);
1691 }
1692
1693 /* called with resize mutex held */
1694 static
1695 void _do_cds_lfht_shrink(struct cds_lfht *ht,
1696 unsigned long old_size, unsigned long new_size)
1697 {
1698 unsigned long old_order, new_order;
1699
1700 new_size = max(new_size, ht->min_alloc_size);
1701 old_order = get_count_order_ulong(old_size);
1702 new_order = get_count_order_ulong(new_size);
1703 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1704 old_size, old_order, new_size, new_order);
1705 assert(new_size < old_size);
1706
1707 /* Remove and unlink all bucket nodes to remove. */
1708 fini_table(ht, new_order + 1, old_order);
1709 }
1710
1711
1712 /* called with resize mutex held */
1713 static
1714 void _do_cds_lfht_resize(struct cds_lfht *ht)
1715 {
1716 unsigned long new_size, old_size;
1717
1718 /*
1719 * Resize table, re-do if the target size has changed under us.
1720 */
1721 do {
1722 assert(uatomic_read(&ht->in_progress_resize));
1723 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1724 break;
1725 ht->t.resize_initiated = 1;
1726 old_size = ht->t.size;
1727 new_size = CMM_LOAD_SHARED(ht->t.resize_target);
1728 if (old_size < new_size)
1729 _do_cds_lfht_grow(ht, old_size, new_size);
1730 else if (old_size > new_size)
1731 _do_cds_lfht_shrink(ht, old_size, new_size);
1732 ht->t.resize_initiated = 0;
1733 /* write resize_initiated before read resize_target */
1734 cmm_smp_mb();
1735 } while (ht->t.size != CMM_LOAD_SHARED(ht->t.resize_target));
1736 }
1737
1738 static
1739 unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
1740 {
1741 return _uatomic_xchg_monotonic_increase(&ht->t.resize_target, new_size);
1742 }
1743
1744 static
1745 void resize_target_update_count(struct cds_lfht *ht,
1746 unsigned long count)
1747 {
1748 count = max(count, ht->min_alloc_size);
1749 uatomic_set(&ht->t.resize_target, count);
1750 }
1751
1752 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
1753 {
1754 resize_target_update_count(ht, new_size);
1755 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
1756 ht->cds_lfht_rcu_thread_offline();
1757 pthread_mutex_lock(&ht->resize_mutex);
1758 _do_cds_lfht_resize(ht);
1759 pthread_mutex_unlock(&ht->resize_mutex);
1760 ht->cds_lfht_rcu_thread_online();
1761 }
1762
1763 static
1764 void do_resize_cb(struct rcu_head *head)
1765 {
1766 struct rcu_resize_work *work =
1767 caa_container_of(head, struct rcu_resize_work, head);
1768 struct cds_lfht *ht = work->ht;
1769
1770 ht->cds_lfht_rcu_thread_offline();
1771 pthread_mutex_lock(&ht->resize_mutex);
1772 _do_cds_lfht_resize(ht);
1773 pthread_mutex_unlock(&ht->resize_mutex);
1774 ht->cds_lfht_rcu_thread_online();
1775 poison_free(work);
1776 cmm_smp_mb(); /* finish resize before decrement */
1777 uatomic_dec(&ht->in_progress_resize);
1778 }
1779
1780 static
1781 void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
1782 {
1783 struct rcu_resize_work *work;
1784
1785 /* Store resize_target before read resize_initiated */
1786 cmm_smp_mb();
1787 if (!CMM_LOAD_SHARED(ht->t.resize_initiated)) {
1788 uatomic_inc(&ht->in_progress_resize);
1789 cmm_smp_mb(); /* increment resize count before load destroy */
1790 if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
1791 uatomic_dec(&ht->in_progress_resize);
1792 return;
1793 }
1794 work = malloc(sizeof(*work));
1795 work->ht = ht;
1796 ht->cds_lfht_call_rcu(&work->head, do_resize_cb);
1797 CMM_STORE_SHARED(ht->t.resize_initiated, 1);
1798 }
1799 }
1800
1801 static
1802 void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
1803 {
1804 unsigned long target_size = size << growth;
1805
1806 if (resize_target_grow(ht, target_size) >= target_size)
1807 return;
1808
1809 __cds_lfht_resize_lazy_launch(ht);
1810 }
1811
1812 /*
1813 * We favor grow operations over shrink. A shrink operation never occurs
1814 * if a grow operation is queued for lazy execution. A grow operation
1815 * cancels any pending shrink lazy execution.
1816 */
1817 static
1818 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
1819 unsigned long count)
1820 {
1821 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1822 return;
1823 count = max(count, ht->min_alloc_size);
1824 if (count == size)
1825 return; /* Already the right size, no resize needed */
1826 if (count > size) { /* lazy grow */
1827 if (resize_target_grow(ht, count) >= count)
1828 return;
1829 } else { /* lazy shrink */
1830 for (;;) {
1831 unsigned long s;
1832
1833 s = uatomic_cmpxchg(&ht->t.resize_target, size, count);
1834 if (s == size)
1835 break; /* no resize needed */
1836 if (s > size)
1837 return; /* growing is/(was just) in progress */
1838 if (s <= count)
1839 return; /* some other thread do shrink */
1840 size = s;
1841 }
1842 }
1843 __cds_lfht_resize_lazy_launch(ht);
1844 }
This page took 0.063949 seconds and 5 git commands to generate.