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