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