Commit | Line | Data |
---|---|---|
10544ee8 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
10544ee8 | 3 | * |
c0c0989a MJ |
4 | * Copyright 2010-2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com> | |
10544ee8 | 6 | * |
c0c0989a | 7 | * Userspace RCU library - Lock-Free Resizable RCU Hash Table |
10544ee8 MD |
8 | */ |
9 | ||
10 | /* | |
11 | * Based on the following articles: | |
12 | * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free | |
13 | * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405. | |
14 | * - Michael, M. M. High performance dynamic lock-free hash tables | |
15 | * and list-based sets. In Proceedings of the fourteenth annual ACM | |
16 | * symposium on Parallel algorithms and architectures, ACM Press, | |
17 | * (2002), 73-82. | |
18 | * | |
19 | * Some specificities of this Lock-Free Resizable RCU Hash Table | |
20 | * implementation: | |
21 | * | |
22 | * - RCU read-side critical section allows readers to perform hash | |
23 | * table lookups, as well as traversals, and use the returned objects | |
24 | * safely by allowing memory reclaim to take place only after a grace | |
25 | * period. | |
26 | * - Add and remove operations are lock-free, and do not need to | |
27 | * allocate memory. They need to be executed within RCU read-side | |
28 | * critical section to ensure the objects they read are valid and to | |
29 | * deal with the cmpxchg ABA problem. | |
30 | * - add and add_unique operations are supported. add_unique checks if | |
31 | * the node key already exists in the hash table. It ensures not to | |
32 | * populate a duplicate key if the node key already exists in the hash | |
33 | * table. | |
34 | * - The resize operation executes concurrently with | |
35 | * add/add_unique/add_replace/remove/lookup/traversal. | |
36 | * - Hash table nodes are contained within a split-ordered list. This | |
37 | * list is ordered by incrementing reversed-bits-hash value. | |
38 | * - An index of bucket nodes is kept. These bucket nodes are the hash | |
39 | * table "buckets". These buckets are internal nodes that allow to | |
40 | * perform a fast hash lookup, similarly to a skip list. These | |
41 | * buckets are chained together in the split-ordered list, which | |
42 | * allows recursive expansion by inserting new buckets between the | |
43 | * existing buckets. The split-ordered list allows adding new buckets | |
44 | * between existing buckets as the table needs to grow. | |
45 | * - The resize operation for small tables only allows expanding the | |
46 | * hash table. It is triggered automatically by detecting long chains | |
47 | * in the add operation. | |
48 | * - The resize operation for larger tables (and available through an | |
49 | * API) allows both expanding and shrinking the hash table. | |
50 | * - Split-counters are used to keep track of the number of | |
51 | * nodes within the hash table for automatic resize triggering. | |
52 | * - Resize operation initiated by long chain detection is executed by a | |
53 | * worker thread, which keeps lock-freedom of add and remove. | |
54 | * - Resize operations are protected by a mutex. | |
55 | * - The removal operation is split in two parts: first, a "removed" | |
56 | * flag is set in the next pointer within the node to remove. Then, | |
57 | * a "garbage collection" is performed in the bucket containing the | |
58 | * removed node (from the start of the bucket up to the removed node). | |
59 | * All encountered nodes with "removed" flag set in their next | |
60 | * pointers are removed from the linked-list. If the cmpxchg used for | |
61 | * removal fails (due to concurrent garbage-collection or concurrent | |
62 | * add), we retry from the beginning of the bucket. This ensures that | |
63 | * the node with "removed" flag set is removed from the hash table | |
64 | * (not visible to lookups anymore) before the RCU read-side critical | |
65 | * section held across removal ends. Furthermore, this ensures that | |
66 | * the node with "removed" flag set is removed from the linked-list | |
67 | * before its memory is reclaimed. After setting the "removal" flag, | |
68 | * only the thread which removal is the first to set the "removal | |
69 | * owner" flag (with an xchg) into a node's next pointer is considered | |
70 | * to have succeeded its removal (and thus owns the node to reclaim). | |
71 | * Because we garbage-collect starting from an invariant node (the | |
72 | * start-of-bucket bucket node) up to the "removed" node (or find a | |
73 | * reverse-hash that is higher), we are sure that a successful | |
74 | * traversal of the chain leads to a chain that is present in the | |
75 | * linked-list (the start node is never removed) and that it does not | |
76 | * contain the "removed" node anymore, even if concurrent delete/add | |
77 | * operations are changing the structure of the list concurrently. | |
78 | * - The add operations perform garbage collection of buckets if they | |
79 | * encounter nodes with removed flag set in the bucket where they want | |
80 | * to add their new node. This ensures lock-freedom of add operation by | |
81 | * helping the remover unlink nodes from the list rather than to wait | |
82 | * for it do to so. | |
83 | * - There are three memory backends for the hash table buckets: the | |
84 | * "order table", the "chunks", and the "mmap". | |
85 | * - These bucket containers contain a compact version of the hash table | |
86 | * nodes. | |
87 | * - The RCU "order table": | |
88 | * - has a first level table indexed by log2(hash index) which is | |
89 | * copied and expanded by the resize operation. This order table | |
90 | * allows finding the "bucket node" tables. | |
91 | * - There is one bucket node table per hash index order. The size of | |
92 | * each bucket node table is half the number of hashes contained in | |
93 | * this order (except for order 0). | |
94 | * - The RCU "chunks" is best suited for close interaction with a page | |
95 | * allocator. It uses a linear array as index to "chunks" containing | |
96 | * each the same number of buckets. | |
97 | * - The RCU "mmap" memory backend uses a single memory map to hold | |
98 | * all buckets. | |
99 | * - synchronize_rcu is used to garbage-collect the old bucket node table. | |
100 | * | |
101 | * Ordering Guarantees: | |
102 | * | |
103 | * To discuss these guarantees, we first define "read" operation as any | |
104 | * of the the basic lttng_ust_lfht_lookup, lttng_ust_lfht_next_duplicate, | |
105 | * lttng_ust_lfht_first, lttng_ust_lfht_next operation, as well as | |
106 | * lttng_ust_lfht_add_unique (failure). | |
107 | * | |
108 | * We define "read traversal" operation as any of the following | |
109 | * group of operations | |
110 | * - lttng_ust_lfht_lookup followed by iteration with lttng_ust_lfht_next_duplicate | |
111 | * (and/or lttng_ust_lfht_next, although less common). | |
112 | * - lttng_ust_lfht_add_unique (failure) followed by iteration with | |
113 | * lttng_ust_lfht_next_duplicate (and/or lttng_ust_lfht_next, although less | |
114 | * common). | |
115 | * - lttng_ust_lfht_first followed iteration with lttng_ust_lfht_next (and/or | |
116 | * lttng_ust_lfht_next_duplicate, although less common). | |
117 | * | |
118 | * We define "write" operations as any of lttng_ust_lfht_add, lttng_ust_lfht_replace, | |
119 | * lttng_ust_lfht_add_unique (success), lttng_ust_lfht_add_replace, lttng_ust_lfht_del. | |
120 | * | |
121 | * When lttng_ust_lfht_add_unique succeeds (returns the node passed as | |
122 | * parameter), it acts as a "write" operation. When lttng_ust_lfht_add_unique | |
123 | * fails (returns a node different from the one passed as parameter), it | |
124 | * acts as a "read" operation. A lttng_ust_lfht_add_unique failure is a | |
125 | * lttng_ust_lfht_lookup "read" operation, therefore, any ordering guarantee | |
126 | * referring to "lookup" imply any of "lookup" or lttng_ust_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 lttng_ust_lfht_lookup() might be passed to a lttng_ust_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. | |
192 | * | |
193 | * Bucket node tables: | |
194 | * | |
195 | * hash table hash table the last all bucket node tables | |
196 | * order size bucket node 0 1 2 3 4 5 6(index) | |
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 | * | |
206 | * When growing/shrinking, we only focus on the last bucket node table | |
207 | * which size is (!order ? 1 : (1 << (order -1))). | |
208 | * | |
209 | * Example for growing/shrinking: | |
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 | |
212 | * | |
213 | * A bit of ascii art explanation: | |
214 | * | |
215 | * The order index is the off-by-one compared to the actual power of 2 | |
216 | * because 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 | |
235 | * 1 | 1 001 100 <- | |
236 | * 2 | | 2 010 010 <- | | |
237 | * | | | 3 011 110 | <- | | |
238 | * 3 -> | | | 4 100 001 | | | |
239 | * -> | | 5 101 101 | | |
240 | * -> | 6 110 011 | |
241 | * -> 7 111 111 | |
242 | */ | |
243 | ||
244 | /* | |
245 | * Note on port to lttng-ust: auto-resize and accounting features are | |
246 | * removed. | |
247 | */ | |
248 | ||
249 | #define _LGPL_SOURCE | |
250 | #include <stdlib.h> | |
251 | #include <errno.h> | |
252 | #include <assert.h> | |
253 | #include <stdio.h> | |
254 | #include <stdint.h> | |
255 | #include <string.h> | |
256 | #include <sched.h> | |
257 | #include <unistd.h> | |
258 | ||
2eba8e39 | 259 | #include <lttng/ust-arch.h> |
10544ee8 MD |
260 | #include <lttng/urcu/pointer.h> |
261 | #include <urcu/arch.h> | |
262 | #include <urcu/uatomic.h> | |
263 | #include <urcu/compiler.h> | |
264 | #include "rculfhash.h" | |
265 | #include "rculfhash-internal.h" | |
266 | #include <stdio.h> | |
267 | #include <pthread.h> | |
268 | #include <signal.h> | |
269 | ||
270 | /* | |
271 | * Split-counters lazily update the global counter each 1024 | |
272 | * addition/removal. It automatically keeps track of resize required. | |
273 | * We use the bucket length as indicator for need to expand for small | |
274 | * tables and machines lacking per-cpu data support. | |
275 | */ | |
276 | #define COUNT_COMMIT_ORDER 10 | |
277 | ||
278 | /* | |
279 | * Define the minimum table size. | |
280 | */ | |
281 | #define MIN_TABLE_ORDER 0 | |
282 | #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER) | |
283 | ||
284 | /* | |
285 | * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink. | |
286 | */ | |
287 | #define MIN_PARTITION_PER_THREAD_ORDER 12 | |
288 | #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER) | |
289 | ||
290 | /* | |
291 | * The removed flag needs to be updated atomically with the pointer. | |
292 | * It indicates that no node must attach to the node scheduled for | |
293 | * removal, and that node garbage collection must be performed. | |
294 | * The bucket flag does not require to be updated atomically with the | |
295 | * pointer, but it is added as a pointer low bit flag to save space. | |
296 | * The "removal owner" flag is used to detect which of the "del" | |
297 | * operation that has set the "removed flag" gets to return the removed | |
298 | * node to its caller. Note that the replace operation does not need to | |
299 | * iteract with the "removal owner" flag, because it validates that | |
300 | * the "removed" flag is not set before performing its cmpxchg. | |
301 | */ | |
302 | #define REMOVED_FLAG (1UL << 0) | |
303 | #define BUCKET_FLAG (1UL << 1) | |
304 | #define REMOVAL_OWNER_FLAG (1UL << 2) | |
305 | #define FLAGS_MASK ((1UL << 3) - 1) | |
306 | ||
307 | /* Value of the end pointer. Should not interact with flags. */ | |
308 | #define END_VALUE NULL | |
309 | ||
310 | /* | |
311 | * ht_items_count: Split-counters counting the number of node addition | |
312 | * and removal in the table. Only used if the LTTNG_UST_LFHT_ACCOUNTING flag | |
313 | * is set at hash table creation. | |
314 | * | |
315 | * These are free-running counters, never reset to zero. They count the | |
316 | * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER) | |
317 | * operations to update the global counter. We choose a power-of-2 value | |
318 | * for the trigger to deal with 32 or 64-bit overflow of the counter. | |
319 | */ | |
320 | struct ht_items_count { | |
321 | unsigned long add, del; | |
322 | } __attribute__((aligned(CAA_CACHE_LINE_SIZE))); | |
323 | ||
324 | #ifdef CONFIG_LTTNG_UST_LFHT_ITER_DEBUG | |
325 | ||
326 | static | |
327 | void lttng_ust_lfht_iter_debug_set_ht(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter) | |
328 | { | |
329 | iter->lfht = ht; | |
330 | } | |
331 | ||
332 | #define lttng_ust_lfht_iter_debug_assert(...) assert(__VA_ARGS__) | |
333 | ||
334 | #else | |
335 | ||
336 | static | |
337 | void lttng_ust_lfht_iter_debug_set_ht(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter) | |
338 | { | |
339 | } | |
340 | ||
341 | #define lttng_ust_lfht_iter_debug_assert(...) | |
342 | ||
343 | #endif | |
344 | ||
345 | /* | |
346 | * Algorithm to reverse bits in a word by lookup table, extended to | |
347 | * 64-bit words. | |
348 | * Source: | |
349 | * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable | |
350 | * Originally from Public Domain. | |
351 | */ | |
352 | ||
353 | static const uint8_t BitReverseTable256[256] = | |
354 | { | |
355 | #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64 | |
356 | #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16) | |
357 | #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 ) | |
358 | R6(0), R6(2), R6(1), R6(3) | |
359 | }; | |
360 | #undef R2 | |
361 | #undef R4 | |
362 | #undef R6 | |
363 | ||
364 | static | |
365 | uint8_t bit_reverse_u8(uint8_t v) | |
366 | { | |
367 | return BitReverseTable256[v]; | |
368 | } | |
369 | ||
370 | #if (CAA_BITS_PER_LONG == 32) | |
371 | static | |
372 | uint32_t bit_reverse_u32(uint32_t v) | |
373 | { | |
374 | return ((uint32_t) bit_reverse_u8(v) << 24) | | |
375 | ((uint32_t) bit_reverse_u8(v >> 8) << 16) | | |
376 | ((uint32_t) bit_reverse_u8(v >> 16) << 8) | | |
377 | ((uint32_t) bit_reverse_u8(v >> 24)); | |
378 | } | |
379 | #else | |
380 | static | |
381 | uint64_t bit_reverse_u64(uint64_t v) | |
382 | { | |
383 | return ((uint64_t) bit_reverse_u8(v) << 56) | | |
384 | ((uint64_t) bit_reverse_u8(v >> 8) << 48) | | |
385 | ((uint64_t) bit_reverse_u8(v >> 16) << 40) | | |
386 | ((uint64_t) bit_reverse_u8(v >> 24) << 32) | | |
387 | ((uint64_t) bit_reverse_u8(v >> 32) << 24) | | |
388 | ((uint64_t) bit_reverse_u8(v >> 40) << 16) | | |
389 | ((uint64_t) bit_reverse_u8(v >> 48) << 8) | | |
390 | ((uint64_t) bit_reverse_u8(v >> 56)); | |
391 | } | |
392 | #endif | |
393 | ||
394 | static | |
395 | unsigned long bit_reverse_ulong(unsigned long v) | |
396 | { | |
397 | #if (CAA_BITS_PER_LONG == 32) | |
398 | return bit_reverse_u32(v); | |
399 | #else | |
400 | return bit_reverse_u64(v); | |
401 | #endif | |
402 | } | |
403 | ||
404 | /* | |
405 | * fls: returns the position of the most significant bit. | |
406 | * Returns 0 if no bit is set, else returns the position of the most | |
407 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
408 | */ | |
2eba8e39 | 409 | #if defined(LTTNG_UST_ARCH_X86) |
10544ee8 MD |
410 | static inline |
411 | unsigned int fls_u32(uint32_t x) | |
412 | { | |
413 | int r; | |
414 | ||
415 | __asm__ ("bsrl %1,%0\n\t" | |
416 | "jnz 1f\n\t" | |
417 | "movl $-1,%0\n\t" | |
418 | "1:\n\t" | |
419 | : "=r" (r) : "rm" (x)); | |
420 | return r + 1; | |
421 | } | |
422 | #define HAS_FLS_U32 | |
423 | #endif | |
424 | ||
2eba8e39 | 425 | #if defined(LTTNG_UST_ARCH_AMD64) |
10544ee8 MD |
426 | static inline |
427 | unsigned int fls_u64(uint64_t x) | |
428 | { | |
429 | long r; | |
430 | ||
431 | __asm__ ("bsrq %1,%0\n\t" | |
432 | "jnz 1f\n\t" | |
433 | "movq $-1,%0\n\t" | |
434 | "1:\n\t" | |
435 | : "=r" (r) : "rm" (x)); | |
436 | return r + 1; | |
437 | } | |
438 | #define HAS_FLS_U64 | |
439 | #endif | |
440 | ||
441 | #ifndef HAS_FLS_U64 | |
8da9deee MJ |
442 | static |
443 | unsigned int fls_u64(uint64_t x) | |
444 | __attribute__((unused)); | |
445 | static | |
10544ee8 MD |
446 | unsigned int fls_u64(uint64_t x) |
447 | { | |
448 | unsigned int r = 64; | |
449 | ||
450 | if (!x) | |
451 | return 0; | |
452 | ||
453 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
454 | x <<= 32; | |
455 | r -= 32; | |
456 | } | |
457 | if (!(x & 0xFFFF000000000000ULL)) { | |
458 | x <<= 16; | |
459 | r -= 16; | |
460 | } | |
461 | if (!(x & 0xFF00000000000000ULL)) { | |
462 | x <<= 8; | |
463 | r -= 8; | |
464 | } | |
465 | if (!(x & 0xF000000000000000ULL)) { | |
466 | x <<= 4; | |
467 | r -= 4; | |
468 | } | |
469 | if (!(x & 0xC000000000000000ULL)) { | |
470 | x <<= 2; | |
471 | r -= 2; | |
472 | } | |
473 | if (!(x & 0x8000000000000000ULL)) { | |
474 | x <<= 1; | |
475 | r -= 1; | |
476 | } | |
477 | return r; | |
478 | } | |
479 | #endif | |
480 | ||
481 | #ifndef HAS_FLS_U32 | |
8da9deee MJ |
482 | static |
483 | unsigned int fls_u32(uint32_t x) | |
484 | __attribute__((unused)); | |
485 | static | |
10544ee8 MD |
486 | unsigned int fls_u32(uint32_t x) |
487 | { | |
488 | unsigned int r = 32; | |
489 | ||
490 | if (!x) | |
491 | return 0; | |
492 | if (!(x & 0xFFFF0000U)) { | |
493 | x <<= 16; | |
494 | r -= 16; | |
495 | } | |
496 | if (!(x & 0xFF000000U)) { | |
497 | x <<= 8; | |
498 | r -= 8; | |
499 | } | |
500 | if (!(x & 0xF0000000U)) { | |
501 | x <<= 4; | |
502 | r -= 4; | |
503 | } | |
504 | if (!(x & 0xC0000000U)) { | |
505 | x <<= 2; | |
506 | r -= 2; | |
507 | } | |
508 | if (!(x & 0x80000000U)) { | |
509 | x <<= 1; | |
510 | r -= 1; | |
511 | } | |
512 | return r; | |
513 | } | |
514 | #endif | |
515 | ||
516 | unsigned int lttng_ust_lfht_fls_ulong(unsigned long x) | |
517 | { | |
518 | #if (CAA_BITS_PER_LONG == 32) | |
519 | return fls_u32(x); | |
520 | #else | |
521 | return fls_u64(x); | |
522 | #endif | |
523 | } | |
524 | ||
525 | /* | |
526 | * Return the minimum order for which x <= (1UL << order). | |
527 | * Return -1 if x is 0. | |
528 | */ | |
529 | int lttng_ust_lfht_get_count_order_u32(uint32_t x) | |
530 | { | |
531 | if (!x) | |
532 | return -1; | |
533 | ||
534 | return fls_u32(x - 1); | |
535 | } | |
536 | ||
537 | /* | |
538 | * Return the minimum order for which x <= (1UL << order). | |
539 | * Return -1 if x is 0. | |
540 | */ | |
541 | int lttng_ust_lfht_get_count_order_ulong(unsigned long x) | |
542 | { | |
543 | if (!x) | |
544 | return -1; | |
545 | ||
546 | return lttng_ust_lfht_fls_ulong(x - 1); | |
547 | } | |
548 | ||
549 | static | |
550 | struct lttng_ust_lfht_node *clear_flag(struct lttng_ust_lfht_node *node) | |
551 | { | |
552 | return (struct lttng_ust_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK); | |
553 | } | |
554 | ||
555 | static | |
556 | int is_removed(const struct lttng_ust_lfht_node *node) | |
557 | { | |
558 | return ((unsigned long) node) & REMOVED_FLAG; | |
559 | } | |
560 | ||
561 | static | |
562 | int is_bucket(struct lttng_ust_lfht_node *node) | |
563 | { | |
564 | return ((unsigned long) node) & BUCKET_FLAG; | |
565 | } | |
566 | ||
567 | static | |
568 | struct lttng_ust_lfht_node *flag_bucket(struct lttng_ust_lfht_node *node) | |
569 | { | |
570 | return (struct lttng_ust_lfht_node *) (((unsigned long) node) | BUCKET_FLAG); | |
571 | } | |
572 | ||
573 | static | |
574 | int is_removal_owner(struct lttng_ust_lfht_node *node) | |
575 | { | |
576 | return ((unsigned long) node) & REMOVAL_OWNER_FLAG; | |
577 | } | |
578 | ||
579 | static | |
580 | struct lttng_ust_lfht_node *flag_removal_owner(struct lttng_ust_lfht_node *node) | |
581 | { | |
582 | return (struct lttng_ust_lfht_node *) (((unsigned long) node) | REMOVAL_OWNER_FLAG); | |
583 | } | |
584 | ||
585 | static | |
586 | struct lttng_ust_lfht_node *flag_removed_or_removal_owner(struct lttng_ust_lfht_node *node) | |
587 | { | |
588 | return (struct lttng_ust_lfht_node *) (((unsigned long) node) | REMOVED_FLAG | REMOVAL_OWNER_FLAG); | |
589 | } | |
590 | ||
591 | static | |
592 | struct lttng_ust_lfht_node *get_end(void) | |
593 | { | |
594 | return (struct lttng_ust_lfht_node *) END_VALUE; | |
595 | } | |
596 | ||
597 | static | |
598 | int is_end(struct lttng_ust_lfht_node *node) | |
599 | { | |
600 | return clear_flag(node) == (struct lttng_ust_lfht_node *) END_VALUE; | |
601 | } | |
602 | ||
603 | static | |
604 | void lttng_ust_lfht_alloc_bucket_table(struct lttng_ust_lfht *ht, unsigned long order) | |
605 | { | |
606 | return ht->mm->alloc_bucket_table(ht, order); | |
607 | } | |
608 | ||
609 | /* | |
610 | * lttng_ust_lfht_free_bucket_table() should be called with decreasing order. | |
611 | * When lttng_ust_lfht_free_bucket_table(0) is called, it means the whole | |
612 | * lfht is destroyed. | |
613 | */ | |
614 | static | |
615 | void lttng_ust_lfht_free_bucket_table(struct lttng_ust_lfht *ht, unsigned long order) | |
616 | { | |
617 | return ht->mm->free_bucket_table(ht, order); | |
618 | } | |
619 | ||
620 | static inline | |
621 | struct lttng_ust_lfht_node *bucket_at(struct lttng_ust_lfht *ht, unsigned long index) | |
622 | { | |
623 | return ht->bucket_at(ht, index); | |
624 | } | |
625 | ||
626 | static inline | |
627 | struct lttng_ust_lfht_node *lookup_bucket(struct lttng_ust_lfht *ht, unsigned long size, | |
628 | unsigned long hash) | |
629 | { | |
630 | assert(size > 0); | |
631 | return bucket_at(ht, hash & (size - 1)); | |
632 | } | |
633 | ||
634 | /* | |
635 | * Remove all logically deleted nodes from a bucket up to a certain node key. | |
636 | */ | |
637 | static | |
638 | void _lttng_ust_lfht_gc_bucket(struct lttng_ust_lfht_node *bucket, struct lttng_ust_lfht_node *node) | |
639 | { | |
640 | struct lttng_ust_lfht_node *iter_prev, *iter, *next, *new_next; | |
641 | ||
642 | assert(!is_bucket(bucket)); | |
643 | assert(!is_removed(bucket)); | |
644 | assert(!is_removal_owner(bucket)); | |
645 | assert(!is_bucket(node)); | |
646 | assert(!is_removed(node)); | |
647 | assert(!is_removal_owner(node)); | |
648 | for (;;) { | |
649 | iter_prev = bucket; | |
650 | /* We can always skip the bucket node initially */ | |
651 | iter = lttng_ust_rcu_dereference(iter_prev->next); | |
652 | assert(!is_removed(iter)); | |
653 | assert(!is_removal_owner(iter)); | |
654 | assert(iter_prev->reverse_hash <= node->reverse_hash); | |
655 | /* | |
656 | * We should never be called with bucket (start of chain) | |
657 | * and logically removed node (end of path compression | |
658 | * marker) being the actual same node. This would be a | |
659 | * bug in the algorithm implementation. | |
660 | */ | |
661 | assert(bucket != node); | |
662 | for (;;) { | |
663 | if (caa_unlikely(is_end(iter))) | |
664 | return; | |
665 | if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash)) | |
666 | return; | |
667 | next = lttng_ust_rcu_dereference(clear_flag(iter)->next); | |
668 | if (caa_likely(is_removed(next))) | |
669 | break; | |
670 | iter_prev = clear_flag(iter); | |
671 | iter = next; | |
672 | } | |
673 | assert(!is_removed(iter)); | |
674 | assert(!is_removal_owner(iter)); | |
675 | if (is_bucket(iter)) | |
676 | new_next = flag_bucket(clear_flag(next)); | |
677 | else | |
678 | new_next = clear_flag(next); | |
679 | (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next); | |
680 | } | |
681 | } | |
682 | ||
683 | static | |
684 | int _lttng_ust_lfht_replace(struct lttng_ust_lfht *ht, unsigned long size, | |
685 | struct lttng_ust_lfht_node *old_node, | |
686 | struct lttng_ust_lfht_node *old_next, | |
687 | struct lttng_ust_lfht_node *new_node) | |
688 | { | |
689 | struct lttng_ust_lfht_node *bucket, *ret_next; | |
690 | ||
691 | if (!old_node) /* Return -ENOENT if asked to replace NULL node */ | |
692 | return -ENOENT; | |
693 | ||
694 | assert(!is_removed(old_node)); | |
695 | assert(!is_removal_owner(old_node)); | |
696 | assert(!is_bucket(old_node)); | |
697 | assert(!is_removed(new_node)); | |
698 | assert(!is_removal_owner(new_node)); | |
699 | assert(!is_bucket(new_node)); | |
700 | assert(new_node != old_node); | |
701 | for (;;) { | |
702 | /* Insert after node to be replaced */ | |
703 | if (is_removed(old_next)) { | |
704 | /* | |
705 | * Too late, the old node has been removed under us | |
706 | * between lookup and replace. Fail. | |
707 | */ | |
708 | return -ENOENT; | |
709 | } | |
710 | assert(old_next == clear_flag(old_next)); | |
711 | assert(new_node != old_next); | |
712 | /* | |
713 | * REMOVAL_OWNER flag is _NEVER_ set before the REMOVED | |
714 | * flag. It is either set atomically at the same time | |
715 | * (replace) or after (del). | |
716 | */ | |
717 | assert(!is_removal_owner(old_next)); | |
718 | new_node->next = old_next; | |
719 | /* | |
720 | * Here is the whole trick for lock-free replace: we add | |
721 | * the replacement node _after_ the node we want to | |
722 | * replace by atomically setting its next pointer at the | |
723 | * same time we set its removal flag. Given that | |
724 | * the lookups/get next use an iterator aware of the | |
725 | * next pointer, they will either skip the old node due | |
726 | * to the removal flag and see the new node, or use | |
727 | * the old node, but will not see the new one. | |
728 | * This is a replacement of a node with another node | |
729 | * that has the same value: we are therefore not | |
730 | * removing a value from the hash table. We set both the | |
731 | * REMOVED and REMOVAL_OWNER flags atomically so we own | |
732 | * the node after successful cmpxchg. | |
733 | */ | |
734 | ret_next = uatomic_cmpxchg(&old_node->next, | |
735 | old_next, flag_removed_or_removal_owner(new_node)); | |
736 | if (ret_next == old_next) | |
737 | break; /* We performed the replacement. */ | |
738 | old_next = ret_next; | |
739 | } | |
740 | ||
741 | /* | |
742 | * Ensure that the old node is not visible to readers anymore: | |
743 | * lookup for the node, and remove it (along with any other | |
744 | * logically removed node) if found. | |
745 | */ | |
746 | bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash)); | |
747 | _lttng_ust_lfht_gc_bucket(bucket, new_node); | |
748 | ||
749 | assert(is_removed(CMM_LOAD_SHARED(old_node->next))); | |
750 | return 0; | |
751 | } | |
752 | ||
753 | /* | |
754 | * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add | |
755 | * mode. A NULL unique_ret allows creation of duplicate keys. | |
756 | */ | |
757 | static | |
758 | void _lttng_ust_lfht_add(struct lttng_ust_lfht *ht, | |
759 | unsigned long hash, | |
760 | lttng_ust_lfht_match_fct match, | |
761 | const void *key, | |
762 | unsigned long size, | |
763 | struct lttng_ust_lfht_node *node, | |
764 | struct lttng_ust_lfht_iter *unique_ret, | |
765 | int bucket_flag) | |
766 | { | |
767 | struct lttng_ust_lfht_node *iter_prev, *iter, *next, *new_node, *new_next, | |
768 | *return_node; | |
769 | struct lttng_ust_lfht_node *bucket; | |
770 | ||
771 | assert(!is_bucket(node)); | |
772 | assert(!is_removed(node)); | |
773 | assert(!is_removal_owner(node)); | |
774 | bucket = lookup_bucket(ht, size, hash); | |
775 | for (;;) { | |
776 | /* | |
777 | * iter_prev points to the non-removed node prior to the | |
778 | * insert location. | |
779 | */ | |
780 | iter_prev = bucket; | |
781 | /* We can always skip the bucket node initially */ | |
782 | iter = lttng_ust_rcu_dereference(iter_prev->next); | |
783 | assert(iter_prev->reverse_hash <= node->reverse_hash); | |
784 | for (;;) { | |
785 | if (caa_unlikely(is_end(iter))) | |
786 | goto insert; | |
787 | if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash)) | |
788 | goto insert; | |
789 | ||
790 | /* bucket node is the first node of the identical-hash-value chain */ | |
791 | if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash) | |
792 | goto insert; | |
793 | ||
794 | next = lttng_ust_rcu_dereference(clear_flag(iter)->next); | |
795 | if (caa_unlikely(is_removed(next))) | |
796 | goto gc_node; | |
797 | ||
798 | /* uniquely add */ | |
799 | if (unique_ret | |
800 | && !is_bucket(next) | |
801 | && clear_flag(iter)->reverse_hash == node->reverse_hash) { | |
802 | struct lttng_ust_lfht_iter d_iter = { | |
803 | .node = node, | |
804 | .next = iter, | |
805 | #ifdef CONFIG_LTTNG_UST_LFHT_ITER_DEBUG | |
806 | .lfht = ht, | |
807 | #endif | |
808 | }; | |
809 | ||
810 | /* | |
811 | * uniquely adding inserts the node as the first | |
812 | * node of the identical-hash-value node chain. | |
813 | * | |
814 | * This semantic ensures no duplicated keys | |
815 | * should ever be observable in the table | |
816 | * (including traversing the table node by | |
817 | * node by forward iterations) | |
818 | */ | |
819 | lttng_ust_lfht_next_duplicate(ht, match, key, &d_iter); | |
820 | if (!d_iter.node) | |
821 | goto insert; | |
822 | ||
823 | *unique_ret = d_iter; | |
824 | return; | |
825 | } | |
826 | ||
827 | iter_prev = clear_flag(iter); | |
828 | iter = next; | |
829 | } | |
830 | ||
831 | insert: | |
832 | assert(node != clear_flag(iter)); | |
833 | assert(!is_removed(iter_prev)); | |
834 | assert(!is_removal_owner(iter_prev)); | |
835 | assert(!is_removed(iter)); | |
836 | assert(!is_removal_owner(iter)); | |
837 | assert(iter_prev != node); | |
838 | if (!bucket_flag) | |
839 | node->next = clear_flag(iter); | |
840 | else | |
841 | node->next = flag_bucket(clear_flag(iter)); | |
842 | if (is_bucket(iter)) | |
843 | new_node = flag_bucket(node); | |
844 | else | |
845 | new_node = node; | |
846 | if (uatomic_cmpxchg(&iter_prev->next, iter, | |
847 | new_node) != iter) { | |
848 | continue; /* retry */ | |
849 | } else { | |
850 | return_node = node; | |
851 | goto end; | |
852 | } | |
853 | ||
854 | gc_node: | |
855 | assert(!is_removed(iter)); | |
856 | assert(!is_removal_owner(iter)); | |
857 | if (is_bucket(iter)) | |
858 | new_next = flag_bucket(clear_flag(next)); | |
859 | else | |
860 | new_next = clear_flag(next); | |
861 | (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next); | |
862 | /* retry */ | |
863 | } | |
864 | end: | |
865 | if (unique_ret) { | |
866 | unique_ret->node = return_node; | |
867 | /* unique_ret->next left unset, never used. */ | |
868 | } | |
869 | } | |
870 | ||
871 | static | |
872 | int _lttng_ust_lfht_del(struct lttng_ust_lfht *ht, unsigned long size, | |
873 | struct lttng_ust_lfht_node *node) | |
874 | { | |
875 | struct lttng_ust_lfht_node *bucket, *next; | |
876 | ||
877 | if (!node) /* Return -ENOENT if asked to delete NULL node */ | |
878 | return -ENOENT; | |
879 | ||
880 | /* logically delete the node */ | |
881 | assert(!is_bucket(node)); | |
882 | assert(!is_removed(node)); | |
883 | assert(!is_removal_owner(node)); | |
884 | ||
885 | /* | |
886 | * We are first checking if the node had previously been | |
887 | * logically removed (this check is not atomic with setting the | |
888 | * logical removal flag). Return -ENOENT if the node had | |
889 | * previously been removed. | |
890 | */ | |
891 | next = CMM_LOAD_SHARED(node->next); /* next is not dereferenced */ | |
892 | if (caa_unlikely(is_removed(next))) | |
893 | return -ENOENT; | |
894 | assert(!is_bucket(next)); | |
895 | /* | |
896 | * The del operation semantic guarantees a full memory barrier | |
897 | * before the uatomic_or atomic commit of the deletion flag. | |
898 | */ | |
899 | cmm_smp_mb__before_uatomic_or(); | |
900 | /* | |
901 | * We set the REMOVED_FLAG unconditionally. Note that there may | |
902 | * be more than one concurrent thread setting this flag. | |
903 | * Knowing which wins the race will be known after the garbage | |
904 | * collection phase, stay tuned! | |
905 | */ | |
906 | uatomic_or(&node->next, REMOVED_FLAG); | |
907 | /* We performed the (logical) deletion. */ | |
908 | ||
909 | /* | |
910 | * Ensure that the node is not visible to readers anymore: lookup for | |
911 | * the node, and remove it (along with any other logically removed node) | |
912 | * if found. | |
913 | */ | |
914 | bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash)); | |
915 | _lttng_ust_lfht_gc_bucket(bucket, node); | |
916 | ||
917 | assert(is_removed(CMM_LOAD_SHARED(node->next))); | |
918 | /* | |
919 | * Last phase: atomically exchange node->next with a version | |
920 | * having "REMOVAL_OWNER_FLAG" set. If the returned node->next | |
921 | * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own | |
922 | * the node and win the removal race. | |
923 | * It is interesting to note that all "add" paths are forbidden | |
924 | * to change the next pointer starting from the point where the | |
925 | * REMOVED_FLAG is set, so here using a read, followed by a | |
926 | * xchg() suffice to guarantee that the xchg() will ever only | |
927 | * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag | |
928 | * was already set). | |
929 | */ | |
930 | if (!is_removal_owner(uatomic_xchg(&node->next, | |
931 | flag_removal_owner(node->next)))) | |
932 | return 0; | |
933 | else | |
934 | return -ENOENT; | |
935 | } | |
936 | ||
937 | /* | |
938 | * Never called with size < 1. | |
939 | */ | |
940 | static | |
941 | void lttng_ust_lfht_create_bucket(struct lttng_ust_lfht *ht, unsigned long size) | |
942 | { | |
943 | struct lttng_ust_lfht_node *prev, *node; | |
944 | unsigned long order, len, i; | |
945 | int bucket_order; | |
946 | ||
947 | lttng_ust_lfht_alloc_bucket_table(ht, 0); | |
948 | ||
949 | dbg_printf("create bucket: order 0 index 0 hash 0\n"); | |
950 | node = bucket_at(ht, 0); | |
951 | node->next = flag_bucket(get_end()); | |
952 | node->reverse_hash = 0; | |
953 | ||
954 | bucket_order = lttng_ust_lfht_get_count_order_ulong(size); | |
955 | assert(bucket_order >= 0); | |
956 | ||
957 | for (order = 1; order < (unsigned long) bucket_order + 1; order++) { | |
958 | len = 1UL << (order - 1); | |
959 | lttng_ust_lfht_alloc_bucket_table(ht, order); | |
960 | ||
961 | for (i = 0; i < len; i++) { | |
962 | /* | |
963 | * Now, we are trying to init the node with the | |
964 | * hash=(len+i) (which is also a bucket with the | |
965 | * index=(len+i)) and insert it into the hash table, | |
966 | * so this node has to be inserted after the bucket | |
967 | * with the index=(len+i)&(len-1)=i. And because there | |
968 | * is no other non-bucket node nor bucket node with | |
969 | * larger index/hash inserted, so the bucket node | |
970 | * being inserted should be inserted directly linked | |
971 | * after the bucket node with index=i. | |
972 | */ | |
973 | prev = bucket_at(ht, i); | |
974 | node = bucket_at(ht, len + i); | |
975 | ||
976 | dbg_printf("create bucket: order %lu index %lu hash %lu\n", | |
977 | order, len + i, len + i); | |
978 | node->reverse_hash = bit_reverse_ulong(len + i); | |
979 | ||
980 | /* insert after prev */ | |
981 | assert(is_bucket(prev->next)); | |
982 | node->next = prev->next; | |
983 | prev->next = flag_bucket(node); | |
984 | } | |
985 | } | |
986 | } | |
987 | ||
988 | #if (CAA_BITS_PER_LONG > 32) | |
989 | /* | |
990 | * For 64-bit architectures, with max number of buckets small enough not to | |
991 | * use the entire 64-bit memory mapping space (and allowing a fair number of | |
992 | * hash table instances), use the mmap allocator, which is faster. Otherwise, | |
993 | * fallback to the order allocator. | |
994 | */ | |
995 | static | |
996 | const struct lttng_ust_lfht_mm_type *get_mm_type(unsigned long max_nr_buckets) | |
997 | { | |
998 | if (max_nr_buckets && max_nr_buckets <= (1ULL << 32)) | |
999 | return <tng_ust_lfht_mm_mmap; | |
1000 | else | |
1001 | return <tng_ust_lfht_mm_order; | |
1002 | } | |
1003 | #else | |
1004 | /* | |
1005 | * For 32-bit architectures, use the order allocator. | |
1006 | */ | |
1007 | static | |
1008 | const struct lttng_ust_lfht_mm_type *get_mm_type(unsigned long max_nr_buckets) | |
1009 | { | |
1010 | return <tng_ust_lfht_mm_order; | |
1011 | } | |
1012 | #endif | |
1013 | ||
1014 | struct lttng_ust_lfht *lttng_ust_lfht_new(unsigned long init_size, | |
1015 | unsigned long min_nr_alloc_buckets, | |
1016 | unsigned long max_nr_buckets, | |
1017 | int flags, | |
1018 | const struct lttng_ust_lfht_mm_type *mm) | |
1019 | { | |
1020 | struct lttng_ust_lfht *ht; | |
1021 | unsigned long order; | |
1022 | ||
1023 | /* min_nr_alloc_buckets must be power of two */ | |
1024 | if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1))) | |
1025 | return NULL; | |
1026 | ||
1027 | /* init_size must be power of two */ | |
1028 | if (!init_size || (init_size & (init_size - 1))) | |
1029 | return NULL; | |
1030 | ||
1031 | /* | |
1032 | * Memory management plugin default. | |
1033 | */ | |
1034 | if (!mm) | |
1035 | mm = get_mm_type(max_nr_buckets); | |
1036 | ||
1037 | /* max_nr_buckets == 0 for order based mm means infinite */ | |
1038 | if (mm == <tng_ust_lfht_mm_order && !max_nr_buckets) | |
1039 | max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1); | |
1040 | ||
1041 | /* max_nr_buckets must be power of two */ | |
1042 | if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1))) | |
1043 | return NULL; | |
1044 | ||
1045 | if (flags & LTTNG_UST_LFHT_AUTO_RESIZE) | |
1046 | return NULL; | |
1047 | ||
1048 | min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE); | |
1049 | init_size = max(init_size, MIN_TABLE_SIZE); | |
1050 | max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets); | |
1051 | init_size = min(init_size, max_nr_buckets); | |
1052 | ||
1053 | ht = mm->alloc_lttng_ust_lfht(min_nr_alloc_buckets, max_nr_buckets); | |
1054 | assert(ht); | |
1055 | assert(ht->mm == mm); | |
1056 | assert(ht->bucket_at == mm->bucket_at); | |
1057 | ||
1058 | ht->flags = flags; | |
1059 | /* this mutex should not nest in read-side C.S. */ | |
1060 | pthread_mutex_init(&ht->resize_mutex, NULL); | |
1061 | order = lttng_ust_lfht_get_count_order_ulong(init_size); | |
1062 | ht->resize_target = 1UL << order; | |
1063 | lttng_ust_lfht_create_bucket(ht, 1UL << order); | |
1064 | ht->size = 1UL << order; | |
1065 | return ht; | |
1066 | } | |
1067 | ||
1068 | void lttng_ust_lfht_lookup(struct lttng_ust_lfht *ht, unsigned long hash, | |
1069 | lttng_ust_lfht_match_fct match, const void *key, | |
1070 | struct lttng_ust_lfht_iter *iter) | |
1071 | { | |
1072 | struct lttng_ust_lfht_node *node, *next, *bucket; | |
1073 | unsigned long reverse_hash, size; | |
1074 | ||
1075 | lttng_ust_lfht_iter_debug_set_ht(ht, iter); | |
1076 | ||
1077 | reverse_hash = bit_reverse_ulong(hash); | |
1078 | ||
1079 | size = lttng_ust_rcu_dereference(ht->size); | |
1080 | bucket = lookup_bucket(ht, size, hash); | |
1081 | /* We can always skip the bucket node initially */ | |
1082 | node = lttng_ust_rcu_dereference(bucket->next); | |
1083 | node = clear_flag(node); | |
1084 | for (;;) { | |
1085 | if (caa_unlikely(is_end(node))) { | |
1086 | node = next = NULL; | |
1087 | break; | |
1088 | } | |
1089 | if (caa_unlikely(node->reverse_hash > reverse_hash)) { | |
1090 | node = next = NULL; | |
1091 | break; | |
1092 | } | |
1093 | next = lttng_ust_rcu_dereference(node->next); | |
1094 | assert(node == clear_flag(node)); | |
1095 | if (caa_likely(!is_removed(next)) | |
1096 | && !is_bucket(next) | |
1097 | && node->reverse_hash == reverse_hash | |
1098 | && caa_likely(match(node, key))) { | |
1099 | break; | |
1100 | } | |
1101 | node = clear_flag(next); | |
1102 | } | |
1103 | assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next))); | |
1104 | iter->node = node; | |
1105 | iter->next = next; | |
1106 | } | |
1107 | ||
1108 | void lttng_ust_lfht_next_duplicate(struct lttng_ust_lfht *ht, lttng_ust_lfht_match_fct match, | |
1109 | const void *key, struct lttng_ust_lfht_iter *iter) | |
1110 | { | |
1111 | struct lttng_ust_lfht_node *node, *next; | |
1112 | unsigned long reverse_hash; | |
1113 | ||
1114 | lttng_ust_lfht_iter_debug_assert(ht == iter->lfht); | |
1115 | node = iter->node; | |
1116 | reverse_hash = node->reverse_hash; | |
1117 | next = iter->next; | |
1118 | node = clear_flag(next); | |
1119 | ||
1120 | for (;;) { | |
1121 | if (caa_unlikely(is_end(node))) { | |
1122 | node = next = NULL; | |
1123 | break; | |
1124 | } | |
1125 | if (caa_unlikely(node->reverse_hash > reverse_hash)) { | |
1126 | node = next = NULL; | |
1127 | break; | |
1128 | } | |
1129 | next = lttng_ust_rcu_dereference(node->next); | |
1130 | if (caa_likely(!is_removed(next)) | |
1131 | && !is_bucket(next) | |
1132 | && caa_likely(match(node, key))) { | |
1133 | break; | |
1134 | } | |
1135 | node = clear_flag(next); | |
1136 | } | |
1137 | assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next))); | |
1138 | iter->node = node; | |
1139 | iter->next = next; | |
1140 | } | |
1141 | ||
1142 | void lttng_ust_lfht_next(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter) | |
1143 | { | |
1144 | struct lttng_ust_lfht_node *node, *next; | |
1145 | ||
1146 | lttng_ust_lfht_iter_debug_assert(ht == iter->lfht); | |
1147 | node = clear_flag(iter->next); | |
1148 | for (;;) { | |
1149 | if (caa_unlikely(is_end(node))) { | |
1150 | node = next = NULL; | |
1151 | break; | |
1152 | } | |
1153 | next = lttng_ust_rcu_dereference(node->next); | |
1154 | if (caa_likely(!is_removed(next)) | |
1155 | && !is_bucket(next)) { | |
1156 | break; | |
1157 | } | |
1158 | node = clear_flag(next); | |
1159 | } | |
1160 | assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next))); | |
1161 | iter->node = node; | |
1162 | iter->next = next; | |
1163 | } | |
1164 | ||
1165 | void lttng_ust_lfht_first(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter) | |
1166 | { | |
1167 | lttng_ust_lfht_iter_debug_set_ht(ht, iter); | |
1168 | /* | |
1169 | * Get next after first bucket node. The first bucket node is the | |
1170 | * first node of the linked list. | |
1171 | */ | |
1172 | iter->next = bucket_at(ht, 0)->next; | |
1173 | lttng_ust_lfht_next(ht, iter); | |
1174 | } | |
1175 | ||
1176 | void lttng_ust_lfht_add(struct lttng_ust_lfht *ht, unsigned long hash, | |
1177 | struct lttng_ust_lfht_node *node) | |
1178 | { | |
1179 | unsigned long size; | |
1180 | ||
1181 | node->reverse_hash = bit_reverse_ulong(hash); | |
1182 | size = lttng_ust_rcu_dereference(ht->size); | |
1183 | _lttng_ust_lfht_add(ht, hash, NULL, NULL, size, node, NULL, 0); | |
1184 | } | |
1185 | ||
1186 | struct lttng_ust_lfht_node *lttng_ust_lfht_add_unique(struct lttng_ust_lfht *ht, | |
1187 | unsigned long hash, | |
1188 | lttng_ust_lfht_match_fct match, | |
1189 | const void *key, | |
1190 | struct lttng_ust_lfht_node *node) | |
1191 | { | |
1192 | unsigned long size; | |
1193 | struct lttng_ust_lfht_iter iter; | |
1194 | ||
1195 | node->reverse_hash = bit_reverse_ulong(hash); | |
1196 | size = lttng_ust_rcu_dereference(ht->size); | |
1197 | _lttng_ust_lfht_add(ht, hash, match, key, size, node, &iter, 0); | |
1198 | return iter.node; | |
1199 | } | |
1200 | ||
1201 | struct lttng_ust_lfht_node *lttng_ust_lfht_add_replace(struct lttng_ust_lfht *ht, | |
1202 | unsigned long hash, | |
1203 | lttng_ust_lfht_match_fct match, | |
1204 | const void *key, | |
1205 | struct lttng_ust_lfht_node *node) | |
1206 | { | |
1207 | unsigned long size; | |
1208 | struct lttng_ust_lfht_iter iter; | |
1209 | ||
1210 | node->reverse_hash = bit_reverse_ulong(hash); | |
1211 | size = lttng_ust_rcu_dereference(ht->size); | |
1212 | for (;;) { | |
1213 | _lttng_ust_lfht_add(ht, hash, match, key, size, node, &iter, 0); | |
1214 | if (iter.node == node) { | |
1215 | return NULL; | |
1216 | } | |
1217 | ||
1218 | if (!_lttng_ust_lfht_replace(ht, size, iter.node, iter.next, node)) | |
1219 | return iter.node; | |
1220 | } | |
1221 | } | |
1222 | ||
1223 | int lttng_ust_lfht_replace(struct lttng_ust_lfht *ht, | |
1224 | struct lttng_ust_lfht_iter *old_iter, | |
1225 | unsigned long hash, | |
1226 | lttng_ust_lfht_match_fct match, | |
1227 | const void *key, | |
1228 | struct lttng_ust_lfht_node *new_node) | |
1229 | { | |
1230 | unsigned long size; | |
1231 | ||
1232 | new_node->reverse_hash = bit_reverse_ulong(hash); | |
1233 | if (!old_iter->node) | |
1234 | return -ENOENT; | |
1235 | if (caa_unlikely(old_iter->node->reverse_hash != new_node->reverse_hash)) | |
1236 | return -EINVAL; | |
1237 | if (caa_unlikely(!match(old_iter->node, key))) | |
1238 | return -EINVAL; | |
1239 | size = lttng_ust_rcu_dereference(ht->size); | |
1240 | return _lttng_ust_lfht_replace(ht, size, old_iter->node, old_iter->next, | |
1241 | new_node); | |
1242 | } | |
1243 | ||
1244 | int lttng_ust_lfht_del(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_node *node) | |
1245 | { | |
1246 | unsigned long size; | |
1247 | ||
1248 | size = lttng_ust_rcu_dereference(ht->size); | |
1249 | return _lttng_ust_lfht_del(ht, size, node); | |
1250 | } | |
1251 | ||
1252 | int lttng_ust_lfht_is_node_deleted(const struct lttng_ust_lfht_node *node) | |
1253 | { | |
1254 | return is_removed(CMM_LOAD_SHARED(node->next)); | |
1255 | } | |
1256 | ||
1257 | static | |
1258 | int lttng_ust_lfht_delete_bucket(struct lttng_ust_lfht *ht) | |
1259 | { | |
1260 | struct lttng_ust_lfht_node *node; | |
1261 | unsigned long order, i, size; | |
1262 | ||
1263 | /* Check that the table is empty */ | |
1264 | node = bucket_at(ht, 0); | |
1265 | do { | |
1266 | node = clear_flag(node)->next; | |
1267 | if (!is_bucket(node)) | |
1268 | return -EPERM; | |
1269 | assert(!is_removed(node)); | |
1270 | assert(!is_removal_owner(node)); | |
1271 | } while (!is_end(node)); | |
1272 | /* | |
1273 | * size accessed without lttng_ust_rcu_dereference because hash table is | |
1274 | * being destroyed. | |
1275 | */ | |
1276 | size = ht->size; | |
1277 | /* Internal sanity check: all nodes left should be buckets */ | |
1278 | for (i = 0; i < size; i++) { | |
1279 | node = bucket_at(ht, i); | |
1280 | dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n", | |
1281 | i, i, bit_reverse_ulong(node->reverse_hash)); | |
1282 | assert(is_bucket(node->next)); | |
1283 | } | |
1284 | ||
1285 | for (order = lttng_ust_lfht_get_count_order_ulong(size); (long)order >= 0; order--) | |
1286 | lttng_ust_lfht_free_bucket_table(ht, order); | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | /* | |
1292 | * Should only be called when no more concurrent readers nor writers can | |
1293 | * possibly access the table. | |
1294 | */ | |
1295 | int lttng_ust_lfht_destroy(struct lttng_ust_lfht *ht) | |
1296 | { | |
1297 | int ret; | |
1298 | ||
1299 | ret = lttng_ust_lfht_delete_bucket(ht); | |
1300 | if (ret) | |
1301 | return ret; | |
1302 | ret = pthread_mutex_destroy(&ht->resize_mutex); | |
1303 | if (ret) | |
1304 | ret = -EBUSY; | |
1305 | poison_free(ht); | |
1306 | return ret; | |
1307 | } |