2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <urcu/compiler.h>
14 #include <common/common.h>
15 #include <common/defaults.h>
17 #include "hashtable.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
;
23 unsigned long lttng_ht_seed
;
25 static unsigned long min_hash_alloc_size
= 1;
26 static unsigned long max_hash_buckets_size
= 0;
29 * Getter/lookup functions need to be called with RCU read-side lock
30 * held. However, modification functions (add, add_unique, replace, del)
31 * take the RCU lock internally, so it does not matter whether the
32 * caller hold the RCU lock or not.
36 * Match function for string node.
38 static int match_str(struct cds_lfht_node
*node
, const void *key
)
40 struct lttng_ht_node_str
*match_node
=
41 caa_container_of(node
, struct lttng_ht_node_str
, node
);
43 return hash_match_key_str(match_node
->key
, (void *) key
);
47 * Match function for ulong node.
49 static int match_ulong(struct cds_lfht_node
*node
, const void *key
)
51 struct lttng_ht_node_ulong
*match_node
=
52 caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
54 return hash_match_key_ulong((void *) match_node
->key
, (void *) key
);
58 * Match function for u64 node.
60 static int match_u64(struct cds_lfht_node
*node
, const void *key
)
62 struct lttng_ht_node_u64
*match_node
=
63 caa_container_of(node
, struct lttng_ht_node_u64
, node
);
65 return hash_match_key_u64(&match_node
->key
, (void *) key
);
69 * Match function for two uint64_t node.
71 static int match_two_u64(struct cds_lfht_node
*node
, const void *key
)
73 struct lttng_ht_node_two_u64
*match_node
=
74 caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
76 return hash_match_key_two_u64((void *) &match_node
->key
, (void *) key
);
80 * Return an allocated lttng hashtable.
83 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
89 size
= DEFAULT_HT_SIZE
;
91 pthread_mutex_lock(&seed_lock
);
93 lttng_ht_seed
= (unsigned long) time(NULL
);
96 pthread_mutex_unlock(&seed_lock
);
98 ht
= zmalloc(sizeof(*ht
));
100 PERROR("zmalloc lttng_ht");
104 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
105 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
107 * There is already an assert in the RCU hashtable code so if the ht is
108 * NULL here there is a *huge* problem.
113 case LTTNG_HT_TYPE_STRING
:
114 ht
->match_fct
= match_str
;
115 ht
->hash_fct
= hash_key_str
;
117 case LTTNG_HT_TYPE_ULONG
:
118 ht
->match_fct
= match_ulong
;
119 ht
->hash_fct
= hash_key_ulong
;
121 case LTTNG_HT_TYPE_U64
:
122 ht
->match_fct
= match_u64
;
123 ht
->hash_fct
= hash_key_u64
;
125 case LTTNG_HT_TYPE_TWO_U64
:
126 ht
->match_fct
= match_two_u64
;
127 ht
->hash_fct
= hash_key_two_u64
;
130 ERR("Unknown lttng hashtable type %d", type
);
131 lttng_ht_destroy(ht
);
135 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
144 * Free a lttng hashtable.
147 void lttng_ht_destroy(struct lttng_ht
*ht
)
151 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
157 * Init lttng ht node string.
160 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
165 cds_lfht_node_init(&node
->node
);
169 * Init lttng ht node unsigned long.
172 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
178 cds_lfht_node_init(&node
->node
);
182 * Init lttng ht node uint64_t.
185 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
191 cds_lfht_node_init(&node
->node
);
195 * Init lttng ht node with two uint64_t.
198 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
,
199 uint64_t key1
, uint64_t key2
)
203 node
->key
.key1
= key1
;
204 node
->key
.key2
= key2
;
205 cds_lfht_node_init(&node
->node
);
209 * Free lttng ht node string.
212 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
219 * Free lttng ht node unsigned long.
222 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
229 * Free lttng ht node uint64_t.
232 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
239 * Free lttng ht node two uint64_t.
242 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
249 * Lookup function in hashtable.
252 void lttng_ht_lookup(struct lttng_ht
*ht
, const void *key
,
253 struct lttng_ht_iter
*iter
)
258 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
259 ht
->match_fct
, key
, &iter
->iter
);
263 * Add unique string node to hashtable.
266 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
267 struct lttng_ht_node_str
*node
)
269 struct cds_lfht_node
*node_ptr
;
274 /* RCU read lock protects from ABA. */
276 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
277 ht
->match_fct
, node
->key
, &node
->node
);
279 assert(node_ptr
== &node
->node
);
283 * Add string node to hashtable.
286 void lttng_ht_add_str(struct lttng_ht
*ht
,
287 struct lttng_ht_node_str
*node
)
293 /* RCU read lock protects from ABA. */
295 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
301 * Add unsigned long node to hashtable.
304 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
310 /* RCU read lock protects from ABA. */
312 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
318 * Add uint64_t node to hashtable.
321 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
327 /* RCU read lock protects from ABA. */
329 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
335 * Add unique unsigned long node to hashtable.
338 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
339 struct lttng_ht_node_ulong
*node
)
341 struct cds_lfht_node
*node_ptr
;
346 /* RCU read lock protects from ABA. */
348 node_ptr
= cds_lfht_add_unique(ht
->ht
,
349 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
350 (void *) node
->key
, &node
->node
);
352 assert(node_ptr
== &node
->node
);
356 * Add unique uint64_t node to hashtable.
359 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
360 struct lttng_ht_node_u64
*node
)
362 struct cds_lfht_node
*node_ptr
;
367 /* RCU read lock protects from ABA. */
369 node_ptr
= cds_lfht_add_unique(ht
->ht
,
370 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
371 &node
->key
, &node
->node
);
373 assert(node_ptr
== &node
->node
);
377 * Add unique two uint64_t node to hashtable.
380 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
,
381 struct lttng_ht_node_two_u64
*node
)
383 struct cds_lfht_node
*node_ptr
;
388 /* RCU read lock protects from ABA. */
390 node_ptr
= cds_lfht_add_unique(ht
->ht
,
391 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
), ht
->match_fct
,
392 (void *) &node
->key
, &node
->node
);
394 assert(node_ptr
== &node
->node
);
398 * Add replace unsigned long node to hashtable.
401 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
402 struct lttng_ht_node_ulong
*node
)
404 struct cds_lfht_node
*node_ptr
;
409 /* RCU read lock protects from ABA. */
411 node_ptr
= cds_lfht_add_replace(ht
->ht
,
412 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
413 (void *) node
->key
, &node
->node
);
418 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
420 assert(node_ptr
== &node
->node
);
424 * Add replace unsigned long node to hashtable.
427 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
428 struct lttng_ht_node_u64
*node
)
430 struct cds_lfht_node
*node_ptr
;
435 /* RCU read lock protects from ABA. */
437 node_ptr
= cds_lfht_add_replace(ht
->ht
,
438 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
439 &node
->key
, &node
->node
);
444 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
446 assert(node_ptr
== &node
->node
);
450 * Delete node from hashtable.
453 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
461 /* RCU read lock protects from ABA. */
463 ret
= cds_lfht_del(ht
->ht
, iter
->iter
.node
);
469 * Get first node in the hashtable.
472 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
478 cds_lfht_first(ht
->ht
, &iter
->iter
);
482 * Get next node in the hashtable.
485 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
491 cds_lfht_next(ht
->ht
, &iter
->iter
);
495 * Return the number of nodes in the hashtable.
498 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
506 /* RCU read lock protects from ABA and allows RCU traversal. */
508 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
515 * Return lttng ht string node from iterator.
518 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
519 struct lttng_ht_iter
*iter
)
521 struct cds_lfht_node
*node
;
524 node
= cds_lfht_iter_get_node(&iter
->iter
);
528 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
532 * Return lttng ht unsigned long node from iterator.
535 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
536 struct lttng_ht_iter
*iter
)
538 struct cds_lfht_node
*node
;
541 node
= cds_lfht_iter_get_node(&iter
->iter
);
545 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
549 * Return lttng ht unsigned long node from iterator.
552 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
553 struct lttng_ht_iter
*iter
)
555 struct cds_lfht_node
*node
;
558 node
= cds_lfht_iter_get_node(&iter
->iter
);
562 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
566 * Return lttng ht stream and index id node from iterator.
569 struct lttng_ht_node_two_u64
*lttng_ht_iter_get_node_two_u64(
570 struct lttng_ht_iter
*iter
)
572 struct cds_lfht_node
*node
;
575 node
= cds_lfht_iter_get_node(&iter
->iter
);
579 return caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);