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