1 #ifndef _LTTNG_UST_RCULFHASH_H
2 #define _LTTNG_UST_RCULFHASH_H
7 * Userspace RCU library - Lock-Free RCU Hash Table
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <urcu/compiler.h>
36 struct lttng_ust_lfht
;
39 * lttng_ust_lfht_node: Contains the next pointers and reverse-hash
40 * value required for lookup and traversal of the hash table.
42 * struct lttng_ust_lfht_node should be aligned on 8-bytes boundaries because
43 * the three lower bits are used as flags. It is worth noting that the
44 * information contained within these three bits could be represented on
45 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
46 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
47 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
48 * is set. Given the minimum size of struct lttng_ust_lfht_node is 8 bytes on
49 * 32-bit architectures, we choose to go for simplicity and reserve
52 * struct lttng_ust_lfht_node can be embedded into a structure (as a field).
53 * caa_container_of() can be used to get the structure from the struct
54 * lttng_ust_lfht_node after a lookup.
56 * The structure which embeds it typically holds the key (or key-value
57 * pair) of the object. The caller code is responsible for calculation
58 * of the hash value for lttng_ust_lfht APIs.
60 struct lttng_ust_lfht_node
{
61 struct lttng_ust_lfht_node
*next
; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
62 unsigned long reverse_hash
;
63 } __attribute__((aligned(8)));
65 /* lttng_ust_lfht_iter: Used to track state while traversing a hash chain. */
66 struct lttng_ust_lfht_iter
{
67 struct lttng_ust_lfht_node
*node
, *next
;
71 struct lttng_ust_lfht_node
*lttng_ust_lfht_iter_get_node(struct lttng_ust_lfht_iter
*iter
)
76 struct rcu_flavor_struct
;
80 * Ensure reader and writer threads are registered as urcu readers.
83 typedef int (*lttng_ust_lfht_match_fct
)(struct lttng_ust_lfht_node
*node
, const void *key
);
86 * lttng_ust_lfht_node_init - initialize a hash table node
87 * @node: the node to initialize.
89 * This function is kept to be eventually used for debugging purposes
90 * (detection of memory corruption).
93 void lttng_ust_lfht_node_init(struct lttng_ust_lfht_node
*node
)
98 * Hash table creation flags.
101 LTTNG_UST_LFHT_AUTO_RESIZE
= (1U << 0),
102 LTTNG_UST_LFHT_ACCOUNTING
= (1U << 1),
105 struct lttng_ust_lfht_mm_type
{
106 struct lttng_ust_lfht
*(*alloc_lttng_ust_lfht
)(unsigned long min_nr_alloc_buckets
,
107 unsigned long max_nr_buckets
);
108 void (*alloc_bucket_table
)(struct lttng_ust_lfht
*ht
, unsigned long order
);
109 void (*free_bucket_table
)(struct lttng_ust_lfht
*ht
, unsigned long order
);
110 struct lttng_ust_lfht_node
*(*bucket_at
)(struct lttng_ust_lfht
*ht
,
111 unsigned long index
);
115 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_order
;
117 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_chunk
;
119 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_mmap
;
122 * lttng_ust_lfht_new - allocate a hash table.
123 * @init_size: number of buckets to allocate initially. Must be power of two.
124 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
125 * (must be power of two)
126 * @max_nr_buckets: the maximum number of hash table buckets allowed.
127 * (must be power of two, 0 is accepted, means
129 * @flags: hash table creation flags (can be combined with bitwise or: '|').
131 * LTTNG_UST_LFHT_AUTO_RESIZE: automatically resize hash table.
132 * LTTNG_UST_LFHT_ACCOUNTING: count the number of node addition
133 * and removal in the table
135 * Return NULL on error.
136 * Note: the RCU flavor must be already included before the hash table header.
139 extern struct lttng_ust_lfht
*lttng_ust_lfht_new(unsigned long init_size
,
140 unsigned long min_nr_alloc_buckets
,
141 unsigned long max_nr_buckets
,
143 const struct lttng_ust_lfht_mm_type
*mm
);
146 * lttng_ust_lfht_destroy - destroy a hash table.
147 * @ht: the hash table to destroy.
149 * Return 0 on success, negative error value on error.
151 * Prior to liburcu 0.10:
152 * - Threads calling this API need to be registered RCU read-side
154 * - lttng_ust_lfht_destroy should *not* be called from a RCU read-side
155 * critical section. It should *not* be called from a call_rcu thread
158 * Starting from liburcu 0.10, rculfhash implements its own worker
159 * thread to handle resize operations, which removes RCU requirements on
160 * lttng_ust_lfht_destroy.
163 extern int lttng_ust_lfht_destroy(struct lttng_ust_lfht
*ht
);
166 * lttng_ust_lfht_count_nodes - count the number of nodes in the hash table.
167 * @ht: the hash table.
168 * @split_count_before: sample the node count split-counter before traversal.
169 * @count: traverse the hash table, count the number of nodes observed.
170 * @split_count_after: sample the node count split-counter after traversal.
172 * Call with rcu_read_lock held.
173 * Threads calling this API need to be registered RCU read-side threads.
176 extern void lttng_ust_lfht_count_nodes(struct lttng_ust_lfht
*ht
,
177 long *split_count_before
,
178 unsigned long *count
,
179 long *split_count_after
);
182 * lttng_ust_lfht_lookup - lookup a node by key.
183 * @ht: the hash table.
184 * @hash: the key hash.
185 * @match: the key match function.
186 * @key: the current node key.
187 * @iter: node, if found (output). *iter->node set to NULL if not found.
189 * Call with rcu_read_lock held.
190 * Threads calling this API need to be registered RCU read-side threads.
191 * This function acts as a rcu_dereference() to read the node pointer.
194 extern void lttng_ust_lfht_lookup(struct lttng_ust_lfht
*ht
, unsigned long hash
,
195 lttng_ust_lfht_match_fct match
, const void *key
,
196 struct lttng_ust_lfht_iter
*iter
);
199 * lttng_ust_lfht_next_duplicate - get the next item with same key, after iterator.
200 * @ht: the hash table.
201 * @match: the key match function.
202 * @key: the current node key.
203 * @iter: input: current iterator.
204 * output: node, if found. *iter->node set to NULL if not found.
206 * Uses an iterator initialized by a lookup or traversal. Important: the
207 * iterator _needs_ to be initialized before calling
208 * lttng_ust_lfht_next_duplicate.
209 * Sets *iter-node to the following node with same key.
210 * Sets *iter->node to NULL if no following node exists with same key.
211 * RCU read-side lock must be held across lttng_ust_lfht_lookup and
212 * lttng_ust_lfht_next calls, and also between lttng_ust_lfht_next calls using the
213 * node returned by a previous lttng_ust_lfht_next.
214 * Call with rcu_read_lock held.
215 * Threads calling this API need to be registered RCU read-side threads.
216 * This function acts as a rcu_dereference() to read the node pointer.
219 extern void lttng_ust_lfht_next_duplicate(struct lttng_ust_lfht
*ht
,
220 lttng_ust_lfht_match_fct match
, const void *key
,
221 struct lttng_ust_lfht_iter
*iter
);
224 * lttng_ust_lfht_first - get the first node in the table.
225 * @ht: the hash table.
226 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
228 * Output in "*iter". *iter->node set to NULL if table is empty.
229 * Call with rcu_read_lock held.
230 * Threads calling this API need to be registered RCU read-side threads.
231 * This function acts as a rcu_dereference() to read the node pointer.
234 extern void lttng_ust_lfht_first(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_iter
*iter
);
237 * lttng_ust_lfht_next - get the next node in the table.
238 * @ht: the hash table.
239 * @iter: input: current iterator.
240 * output: next node, if exists. *iter->node set to NULL if not found.
242 * Input/Output in "*iter". *iter->node set to NULL if *iter was
243 * pointing to the last table node.
244 * Call with rcu_read_lock held.
245 * Threads calling this API need to be registered RCU read-side threads.
246 * This function acts as a rcu_dereference() to read the node pointer.
249 extern void lttng_ust_lfht_next(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_iter
*iter
);
252 * lttng_ust_lfht_add - add a node to the hash table.
253 * @ht: the hash table.
254 * @hash: the key hash.
255 * @node: the node to add.
257 * This function supports adding redundant keys into the table.
258 * Call with rcu_read_lock held.
259 * Threads calling this API need to be registered RCU read-side threads.
260 * This function issues a full memory barrier before and after its
264 extern void lttng_ust_lfht_add(struct lttng_ust_lfht
*ht
, unsigned long hash
,
265 struct lttng_ust_lfht_node
*node
);
268 * lttng_ust_lfht_add_unique - add a node to hash table, if key is not present.
269 * @ht: the hash table.
270 * @hash: the node's hash.
271 * @match: the key match function.
272 * @key: the node's key.
273 * @node: the node to try adding.
275 * Return the node added upon success.
276 * Return the unique node already present upon failure. If
277 * lttng_ust_lfht_add_unique fails, the node passed as parameter should be
278 * freed by the caller. In this case, the caller does NOT need to wait
279 * for a grace period before freeing or re-using the node.
280 * Call with rcu_read_lock held.
281 * Threads calling this API need to be registered RCU read-side threads.
283 * The semantic of this function is that if only this function is used
284 * to add keys into the table, no duplicated keys should ever be
285 * observable in the table. The same guarantee apply for combination of
286 * add_unique and add_replace (see below).
288 * Upon success, this function issues a full memory barrier before and
289 * after its atomic commit. Upon failure, this function acts like a
290 * simple lookup operation: it acts as a rcu_dereference() to read the
291 * node pointer. The failure case does not guarantee any other memory
295 extern struct lttng_ust_lfht_node
*lttng_ust_lfht_add_unique(struct lttng_ust_lfht
*ht
,
297 lttng_ust_lfht_match_fct match
,
299 struct lttng_ust_lfht_node
*node
);
302 * lttng_ust_lfht_add_replace - replace or add a node within hash table.
303 * @ht: the hash table.
304 * @hash: the node's hash.
305 * @match: the key match function.
306 * @key: the node's key.
307 * @node: the node to add.
309 * Return the node replaced upon success. If no node matching the key
310 * was present, return NULL, which also means the operation succeeded.
311 * This replacement operation should never fail.
312 * Call with rcu_read_lock held.
313 * Threads calling this API need to be registered RCU read-side threads.
314 * After successful replacement, a grace period must be waited for before
315 * freeing or re-using the memory reserved for the returned node.
317 * The semantic of replacement vs lookups and traversals is the
318 * following: if lookups and traversals are performed between a key
319 * unique insertion and its removal, we guarantee that the lookups and
320 * traversals will always find exactly one instance of the key if it is
321 * replaced concurrently with the lookups.
323 * Providing this semantic allows us to ensure that replacement-only
324 * schemes will never generate duplicated keys. It also allows us to
325 * guarantee that a combination of add_replace and add_unique updates
326 * will never generate duplicated keys.
328 * This function issues a full memory barrier before and after its
332 extern struct lttng_ust_lfht_node
*lttng_ust_lfht_add_replace(struct lttng_ust_lfht
*ht
,
334 lttng_ust_lfht_match_fct match
,
336 struct lttng_ust_lfht_node
*node
);
339 * lttng_ust_lfht_replace - replace a node pointed to by iter within hash table.
340 * @ht: the hash table.
341 * @old_iter: the iterator position of the node to replace.
342 * @hash: the node's hash.
343 * @match: the key match function.
344 * @key: the node's key.
345 * @new_node: the new node to use as replacement.
347 * Return 0 if replacement is successful, negative value otherwise.
348 * Replacing a NULL old node or an already removed node will fail with
350 * If the hash or value of the node to replace and the new node differ,
351 * this function returns -EINVAL without proceeding to the replacement.
352 * Old node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next.
353 * RCU read-side lock must be held between lookup and replacement.
354 * Call with rcu_read_lock held.
355 * Threads calling this API need to be registered RCU read-side threads.
356 * After successful replacement, a grace period must be waited for before
357 * freeing or re-using the memory reserved for the old node (which can
358 * be accessed with lttng_ust_lfht_iter_get_node).
360 * The semantic of replacement vs lookups is the same as
361 * lttng_ust_lfht_add_replace().
363 * Upon success, this function issues a full memory barrier before and
364 * after its atomic commit. Upon failure, this function does not issue
365 * any memory barrier.
368 extern int lttng_ust_lfht_replace(struct lttng_ust_lfht
*ht
,
369 struct lttng_ust_lfht_iter
*old_iter
,
371 lttng_ust_lfht_match_fct match
,
373 struct lttng_ust_lfht_node
*new_node
);
376 * lttng_ust_lfht_del - remove node pointed to by iterator from hash table.
377 * @ht: the hash table.
378 * @node: the node to delete.
380 * Return 0 if the node is successfully removed, negative value
382 * Deleting a NULL node or an already removed node will fail with a
384 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
385 * followed by use of lttng_ust_lfht_iter_get_node.
386 * RCU read-side lock must be held between lookup and removal.
387 * Call with rcu_read_lock held.
388 * Threads calling this API need to be registered RCU read-side threads.
389 * After successful removal, a grace period must be waited for before
390 * freeing or re-using the memory reserved for old node (which can be
391 * accessed with lttng_ust_lfht_iter_get_node).
392 * Upon success, this function issues a full memory barrier before and
393 * after its atomic commit. Upon failure, this function does not issue
394 * any memory barrier.
397 extern int lttng_ust_lfht_del(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_node
*node
);
400 * lttng_ust_lfht_is_node_deleted - query whether a node is removed from hash table.
402 * Return non-zero if the node is deleted from the hash table, 0
404 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
405 * followed by use of lttng_ust_lfht_iter_get_node.
406 * RCU read-side lock must be held between lookup and call to this
408 * Call with rcu_read_lock held.
409 * Threads calling this API need to be registered RCU read-side threads.
410 * This function does not issue any memory barrier.
413 extern int lttng_ust_lfht_is_node_deleted(const struct lttng_ust_lfht_node
*node
);
416 * lttng_ust_lfht_resize - Force a hash table resize
417 * @ht: the hash table.
418 * @new_size: update to this hash table size.
420 * Threads calling this API need to be registered RCU read-side threads.
421 * This function does not (necessarily) issue memory barriers.
422 * lttng_ust_lfht_resize should *not* be called from a RCU read-side critical
426 extern void lttng_ust_lfht_resize(struct lttng_ust_lfht
*ht
, unsigned long new_size
);
429 * Note: it is safe to perform element removal (del), replacement, or
430 * any hash table update operation during any of the following hash
432 * These functions act as rcu_dereference() to read the node pointers.
434 #define lttng_ust_lfht_for_each(ht, iter, node) \
435 for (lttng_ust_lfht_first(ht, iter), \
436 node = lttng_ust_lfht_iter_get_node(iter); \
438 lttng_ust_lfht_next(ht, iter), \
439 node = lttng_ust_lfht_iter_get_node(iter))
441 #define lttng_ust_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
442 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
443 node = lttng_ust_lfht_iter_get_node(iter); \
445 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
446 node = lttng_ust_lfht_iter_get_node(iter))
448 #define lttng_ust_lfht_for_each_entry(ht, iter, pos, member) \
449 for (lttng_ust_lfht_first(ht, iter), \
450 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
451 __typeof__(*(pos)), member); \
452 lttng_ust_lfht_iter_get_node(iter) != NULL; \
453 lttng_ust_lfht_next(ht, iter), \
454 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
455 __typeof__(*(pos)), member))
457 #define lttng_ust_lfht_for_each_entry_duplicate(ht, hash, match, key, \
459 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
460 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
461 __typeof__(*(pos)), member); \
462 lttng_ust_lfht_iter_get_node(iter) != NULL; \
463 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
464 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
465 __typeof__(*(pos)), member))
471 #endif /* _LTTNG_UST_RCULFHASH_H */