2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "hashtable.hpp"
12 #include <common/common.hpp>
13 #include <common/defaults.hpp>
14 #include <common/urcu.hpp>
18 #include <urcu/compiler.h>
20 /* seed_lock protects both seed_init and lttng_ht_seed. */
21 static pthread_mutex_t seed_lock
= PTHREAD_MUTEX_INITIALIZER
;
22 static bool seed_init
;
24 static unsigned long min_hash_alloc_size
= 1;
25 static unsigned long max_hash_buckets_size
= 0;
28 * Getter/lookup functions need to be called with RCU read-side lock
29 * held. However, modification functions (add, add_unique, replace, del)
30 * take the RCU lock internally, so it does not matter whether the
31 * caller hold the RCU lock or not.
35 * Match function for string node.
37 static int match_str(struct cds_lfht_node
*node
, const void *key
)
39 struct lttng_ht_node_str
*match_node
=
40 lttng::utils::container_of(node
, <tng_ht_node_str::node
);
42 return hash_match_key_str(match_node
->key
, (void *) key
);
46 * Match function for ulong node.
48 static int match_ulong(struct cds_lfht_node
*node
, const void *key
)
50 struct lttng_ht_node_ulong
*match_node
=
51 lttng::utils::container_of(node
, <tng_ht_node_ulong::node
);
53 return hash_match_key_ulong((void *) match_node
->key
, (void *) key
);
57 * Match function for u64 node.
59 static int match_u64(struct cds_lfht_node
*node
, const void *key
)
61 struct lttng_ht_node_u64
*match_node
=
62 lttng::utils::container_of(node
, <tng_ht_node_u64::node
);
64 return hash_match_key_u64(&match_node
->key
, (void *) key
);
68 * Match function for two uint64_t node.
70 static int match_two_u64(struct cds_lfht_node
*node
, const void *key
)
72 struct lttng_ht_node_two_u64
*match_node
=
73 lttng::utils::container_of(node
, <tng_ht_node_two_u64::node
);
75 return hash_match_key_two_u64((void *) &match_node
->key
, (void *) key
);
78 static inline const char *lttng_ht_type_str(enum lttng_ht_type type
)
81 case LTTNG_HT_TYPE_STRING
:
83 case LTTNG_HT_TYPE_ULONG
:
85 case LTTNG_HT_TYPE_U64
:
87 case LTTNG_HT_TYPE_TWO_U64
:
90 ERR("Unknown lttng hashtable type %d", type
);
96 * Return an allocated lttng hashtable.
98 struct lttng_ht
*lttng_ht_new(unsigned long size
, lttng_ht_type type
)
104 size
= DEFAULT_HT_SIZE
;
106 pthread_mutex_lock(&seed_lock
);
108 lttng_ht_seed
= (unsigned long) time(nullptr);
111 pthread_mutex_unlock(&seed_lock
);
113 ht
= zmalloc
<lttng_ht
>();
115 PERROR("zmalloc lttng_ht");
119 ht
->ht
= cds_lfht_new(size
,
121 max_hash_buckets_size
,
122 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
125 * There is already an assert in the RCU hashtable code so if the ht is
126 * NULL here there is a *huge* problem.
128 LTTNG_ASSERT(ht
->ht
);
131 case LTTNG_HT_TYPE_STRING
:
132 ht
->match_fct
= match_str
;
133 ht
->hash_fct
= hash_key_str
;
135 case LTTNG_HT_TYPE_ULONG
:
136 ht
->match_fct
= match_ulong
;
137 ht
->hash_fct
= hash_key_ulong
;
139 case LTTNG_HT_TYPE_U64
:
140 ht
->match_fct
= match_u64
;
141 ht
->hash_fct
= hash_key_u64
;
143 case LTTNG_HT_TYPE_TWO_U64
:
144 ht
->match_fct
= match_two_u64
;
145 ht
->hash_fct
= hash_key_two_u64
;
148 ERR("Unknown lttng hashtable type %d", type
);
149 lttng_ht_destroy(ht
);
153 DBG3("Created hashtable size %lu at %p of type %s", size
, ht
->ht
, lttng_ht_type_str(type
));
162 * Free a lttng hashtable.
164 void lttng_ht_destroy(struct lttng_ht
*ht
)
168 ret
= cds_lfht_destroy(ht
->ht
, nullptr);
174 * Init lttng ht node string.
176 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
181 cds_lfht_node_init(&node
->node
);
185 * Init lttng ht node unsigned long.
187 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
, unsigned long key
)
192 cds_lfht_node_init(&node
->node
);
196 * Init lttng ht node uint64_t.
198 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
, uint64_t key
)
203 cds_lfht_node_init(&node
->node
);
207 * Init lttng ht node with two uint64_t.
209 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
, uint64_t key1
, uint64_t key2
)
213 node
->key
.key1
= key1
;
214 node
->key
.key2
= key2
;
215 cds_lfht_node_init(&node
->node
);
219 * Free lttng ht node string.
221 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
228 * Free lttng ht node unsigned long.
230 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
237 * Free lttng ht node uint64_t.
239 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
246 * Free lttng ht node two uint64_t.
248 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
255 * Lookup function in hashtable.
257 void lttng_ht_lookup(struct lttng_ht
*ht
, const void *key
, struct lttng_ht_iter
*iter
)
260 LTTNG_ASSERT(ht
->ht
);
262 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
), ht
->match_fct
, key
, &iter
->iter
);
266 * Add unique string node to hashtable.
268 void lttng_ht_add_unique_str(struct lttng_ht
*ht
, struct lttng_ht_node_str
*node
)
270 struct cds_lfht_node
*node_ptr
;
272 LTTNG_ASSERT(ht
->ht
);
275 /* RCU read lock protects from ABA. */
276 lttng::urcu::read_lock_guard read_lock
;
277 node_ptr
= cds_lfht_add_unique(ht
->ht
,
278 ht
->hash_fct(node
->key
, lttng_ht_seed
),
282 LTTNG_ASSERT(node_ptr
== &node
->node
);
286 * Add string node to hashtable.
288 void lttng_ht_add_str(struct lttng_ht
*ht
, struct lttng_ht_node_str
*node
)
291 LTTNG_ASSERT(ht
->ht
);
294 /* RCU read lock protects from ABA. */
295 lttng::urcu::read_lock_guard read_lock
;
296 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
), &node
->node
);
300 * Add unsigned long node to hashtable.
302 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
305 LTTNG_ASSERT(ht
->ht
);
308 /* RCU read lock protects from ABA. */
309 lttng::urcu::read_lock_guard read_lock
;
310 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), &node
->node
);
314 * Add uint64_t node to hashtable.
316 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
319 LTTNG_ASSERT(ht
->ht
);
322 /* RCU read lock protects from ABA. */
323 lttng::urcu::read_lock_guard read_lock
;
324 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
), &node
->node
);
328 * Add unique unsigned long node to hashtable.
330 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
332 struct cds_lfht_node
*node_ptr
;
334 LTTNG_ASSERT(ht
->ht
);
337 /* RCU read lock protects from ABA. */
338 lttng::urcu::read_lock_guard read_lock
;
339 node_ptr
= cds_lfht_add_unique(ht
->ht
,
340 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
344 LTTNG_ASSERT(node_ptr
== &node
->node
);
348 * Add unique uint64_t node to hashtable.
350 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
352 struct cds_lfht_node
*node_ptr
;
354 LTTNG_ASSERT(ht
->ht
);
357 /* RCU read lock protects from ABA. */
358 lttng::urcu::read_lock_guard read_lock
;
359 node_ptr
= cds_lfht_add_unique(ht
->ht
,
360 ht
->hash_fct(&node
->key
, lttng_ht_seed
),
364 LTTNG_ASSERT(node_ptr
== &node
->node
);
368 * Add unique two uint64_t node to hashtable.
370 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
, struct lttng_ht_node_two_u64
*node
)
372 struct cds_lfht_node
*node_ptr
;
374 LTTNG_ASSERT(ht
->ht
);
377 /* RCU read lock protects from ABA. */
378 lttng::urcu::read_lock_guard read_lock
;
379 node_ptr
= cds_lfht_add_unique(ht
->ht
,
380 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
),
384 LTTNG_ASSERT(node_ptr
== &node
->node
);
388 * Add replace unsigned long node to hashtable.
390 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
391 struct lttng_ht_node_ulong
*node
)
393 struct cds_lfht_node
*node_ptr
;
395 LTTNG_ASSERT(ht
->ht
);
398 /* RCU read lock protects from ABA. */
399 lttng::urcu::read_lock_guard read_lock
;
400 node_ptr
= cds_lfht_add_replace(ht
->ht
,
401 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
408 return lttng::utils::container_of(node_ptr
, <tng_ht_node_ulong::node
);
413 * Add replace unsigned long node to hashtable.
415 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
416 struct lttng_ht_node_u64
*node
)
418 struct cds_lfht_node
*node_ptr
;
420 LTTNG_ASSERT(ht
->ht
);
423 /* RCU read lock protects from ABA. */
424 lttng::urcu::read_lock_guard read_lock
;
425 node_ptr
= cds_lfht_add_replace(ht
->ht
,
426 ht
->hash_fct(&node
->key
, lttng_ht_seed
),
433 LTTNG_ASSERT(node_ptr
== &node
->node
);
434 return lttng::utils::container_of(node_ptr
, <tng_ht_node_u64::node
);
439 * Delete node from hashtable.
441 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
446 LTTNG_ASSERT(ht
->ht
);
449 /* RCU read lock protects from ABA. */
450 lttng::urcu::read_lock_guard read_lock
;
451 ret
= cds_lfht_del(ht
->ht
, iter
->iter
.node
);
456 * Get first node in the hashtable.
458 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
461 LTTNG_ASSERT(ht
->ht
);
464 cds_lfht_first(ht
->ht
, &iter
->iter
);
468 * Get next node in the hashtable.
470 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
473 LTTNG_ASSERT(ht
->ht
);
476 cds_lfht_next(ht
->ht
, &iter
->iter
);
480 * Return the number of nodes in the hashtable.
482 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
488 LTTNG_ASSERT(ht
->ht
);
490 /* RCU read lock protects from ABA and allows RCU traversal. */
491 lttng::urcu::read_lock_guard read_lock
;
492 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);