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