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