2 * Copyright (C) 2011 EfficiOS Inc.
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
;
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 caa_container_of(node
, struct lttng_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 caa_container_of(node
, struct lttng_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 caa_container_of(node
, struct lttng_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 caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);
75 return hash_match_key_two_u64((void *) &match_node
->key
, (void *) key
);
79 * Return an allocated lttng hashtable.
82 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
88 size
= DEFAULT_HT_SIZE
;
90 pthread_mutex_lock(&seed_lock
);
92 lttng_ht_seed
= (unsigned long) time(NULL
);
95 pthread_mutex_unlock(&seed_lock
);
97 ht
= zmalloc(sizeof(*ht
));
99 PERROR("zmalloc lttng_ht");
103 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
104 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
106 * There is already an assert in the RCU hashtable code so if the ht is
107 * NULL here there is a *huge* problem.
112 case LTTNG_HT_TYPE_STRING
:
113 ht
->match_fct
= match_str
;
114 ht
->hash_fct
= hash_key_str
;
116 case LTTNG_HT_TYPE_ULONG
:
117 ht
->match_fct
= match_ulong
;
118 ht
->hash_fct
= hash_key_ulong
;
120 case LTTNG_HT_TYPE_U64
:
121 ht
->match_fct
= match_u64
;
122 ht
->hash_fct
= hash_key_u64
;
124 case LTTNG_HT_TYPE_TWO_U64
:
125 ht
->match_fct
= match_two_u64
;
126 ht
->hash_fct
= hash_key_two_u64
;
129 ERR("Unknown lttng hashtable type %d", type
);
130 lttng_ht_destroy(ht
);
134 DBG3("Created hashtable size %lu at %p of type %d", size
, ht
->ht
, type
);
143 * Free a lttng hashtable.
146 void lttng_ht_destroy(struct lttng_ht
*ht
)
150 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
156 * Init lttng ht node string.
159 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
164 cds_lfht_node_init(&node
->node
);
168 * Init lttng ht node unsigned long.
171 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
177 cds_lfht_node_init(&node
->node
);
181 * Init lttng ht node uint64_t.
184 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
190 cds_lfht_node_init(&node
->node
);
194 * Init lttng ht node with two uint64_t.
197 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
,
198 uint64_t key1
, uint64_t key2
)
202 node
->key
.key1
= key1
;
203 node
->key
.key2
= key2
;
204 cds_lfht_node_init(&node
->node
);
208 * Free lttng ht node string.
211 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
218 * Free lttng ht node unsigned long.
221 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
228 * Free lttng ht node uint64_t.
231 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
238 * Free lttng ht node two uint64_t.
241 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
248 * Lookup function in hashtable.
251 void lttng_ht_lookup(struct lttng_ht
*ht
, const void *key
,
252 struct lttng_ht_iter
*iter
)
257 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
258 ht
->match_fct
, key
, &iter
->iter
);
262 * Add unique string node to hashtable.
265 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
266 struct lttng_ht_node_str
*node
)
268 struct cds_lfht_node
*node_ptr
;
273 /* RCU read lock protects from ABA. */
275 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
276 ht
->match_fct
, node
->key
, &node
->node
);
278 assert(node_ptr
== &node
->node
);
282 * Add string node to hashtable.
285 void lttng_ht_add_str(struct lttng_ht
*ht
,
286 struct lttng_ht_node_str
*node
)
292 /* RCU read lock protects from ABA. */
294 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
300 * Add unsigned long node to hashtable.
303 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
309 /* RCU read lock protects from ABA. */
311 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
317 * Add uint64_t node to hashtable.
320 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
326 /* RCU read lock protects from ABA. */
328 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
334 * Add unique unsigned long node to hashtable.
337 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
338 struct lttng_ht_node_ulong
*node
)
340 struct cds_lfht_node
*node_ptr
;
345 /* RCU read lock protects from ABA. */
347 node_ptr
= cds_lfht_add_unique(ht
->ht
,
348 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
349 (void *) node
->key
, &node
->node
);
351 assert(node_ptr
== &node
->node
);
355 * Add unique uint64_t node to hashtable.
358 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
359 struct lttng_ht_node_u64
*node
)
361 struct cds_lfht_node
*node_ptr
;
366 /* RCU read lock protects from ABA. */
368 node_ptr
= cds_lfht_add_unique(ht
->ht
,
369 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
370 &node
->key
, &node
->node
);
372 assert(node_ptr
== &node
->node
);
376 * Add unique two uint64_t node to hashtable.
379 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
,
380 struct lttng_ht_node_two_u64
*node
)
382 struct cds_lfht_node
*node_ptr
;
387 /* RCU read lock protects from ABA. */
389 node_ptr
= cds_lfht_add_unique(ht
->ht
,
390 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
), ht
->match_fct
,
391 (void *) &node
->key
, &node
->node
);
393 assert(node_ptr
== &node
->node
);
397 * Add replace unsigned long node to hashtable.
400 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
401 struct lttng_ht_node_ulong
*node
)
403 struct cds_lfht_node
*node_ptr
;
408 /* RCU read lock protects from ABA. */
410 node_ptr
= cds_lfht_add_replace(ht
->ht
,
411 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
412 (void *) node
->key
, &node
->node
);
417 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
419 assert(node_ptr
== &node
->node
);
423 * Add replace unsigned long node to hashtable.
426 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
427 struct lttng_ht_node_u64
*node
)
429 struct cds_lfht_node
*node_ptr
;
434 /* RCU read lock protects from ABA. */
436 node_ptr
= cds_lfht_add_replace(ht
->ht
,
437 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
438 &node
->key
, &node
->node
);
443 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
445 assert(node_ptr
== &node
->node
);
449 * Delete node from hashtable.
452 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
460 /* RCU read lock protects from ABA. */
462 ret
= cds_lfht_del(ht
->ht
, iter
->iter
.node
);
468 * Get first node in the hashtable.
471 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
477 cds_lfht_first(ht
->ht
, &iter
->iter
);
481 * Get next node in the hashtable.
484 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
490 cds_lfht_next(ht
->ht
, &iter
->iter
);
494 * Return the number of nodes in the hashtable.
497 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
505 /* RCU read lock protects from ABA and allows RCU traversal. */
507 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
514 * Return lttng ht string node from iterator.
517 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
518 struct lttng_ht_iter
*iter
)
520 struct cds_lfht_node
*node
;
523 node
= cds_lfht_iter_get_node(&iter
->iter
);
527 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
531 * Return lttng ht unsigned long node from iterator.
534 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
535 struct lttng_ht_iter
*iter
)
537 struct cds_lfht_node
*node
;
540 node
= cds_lfht_iter_get_node(&iter
->iter
);
544 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
548 * Return lttng ht unsigned long node from iterator.
551 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
552 struct lttng_ht_iter
*iter
)
554 struct cds_lfht_node
*node
;
557 node
= cds_lfht_iter_get_node(&iter
->iter
);
561 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
565 * Return lttng ht stream and index id node from iterator.
568 struct lttng_ht_node_two_u64
*lttng_ht_iter_get_node_two_u64(
569 struct lttng_ht_iter
*iter
)
571 struct cds_lfht_node
*node
;
574 node
= cds_lfht_iter_get_node(&iter
->iter
);
578 return caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);