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 const char *lttng_ht_type_str(enum lttng_ht_type type
)
82 case LTTNG_HT_TYPE_STRING
:
84 case LTTNG_HT_TYPE_ULONG
:
86 case LTTNG_HT_TYPE_U64
:
88 case LTTNG_HT_TYPE_TWO_U64
:
91 ERR("Unknown lttng hashtable type %d", type
);
97 * Return an allocated lttng hashtable.
100 struct lttng_ht
*lttng_ht_new(unsigned long size
, int type
)
106 size
= DEFAULT_HT_SIZE
;
108 pthread_mutex_lock(&seed_lock
);
110 lttng_ht_seed
= (unsigned long) time(NULL
);
113 pthread_mutex_unlock(&seed_lock
);
115 ht
= zmalloc(sizeof(*ht
));
117 PERROR("zmalloc lttng_ht");
121 ht
->ht
= cds_lfht_new(size
, min_hash_alloc_size
, max_hash_buckets_size
,
122 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
124 * There is already an assert in the RCU hashtable code so if the ht is
125 * NULL here there is a *huge* problem.
130 case LTTNG_HT_TYPE_STRING
:
131 ht
->match_fct
= match_str
;
132 ht
->hash_fct
= hash_key_str
;
134 case LTTNG_HT_TYPE_ULONG
:
135 ht
->match_fct
= match_ulong
;
136 ht
->hash_fct
= hash_key_ulong
;
138 case LTTNG_HT_TYPE_U64
:
139 ht
->match_fct
= match_u64
;
140 ht
->hash_fct
= hash_key_u64
;
142 case LTTNG_HT_TYPE_TWO_U64
:
143 ht
->match_fct
= match_two_u64
;
144 ht
->hash_fct
= hash_key_two_u64
;
147 ERR("Unknown lttng hashtable type %d", type
);
148 lttng_ht_destroy(ht
);
152 DBG3("Created hashtable size %lu at %p of type %s", size
, ht
->ht
,
153 lttng_ht_type_str(type
));
162 * Free a lttng hashtable.
165 void lttng_ht_destroy(struct lttng_ht
*ht
)
169 ret
= cds_lfht_destroy(ht
->ht
, NULL
);
175 * Init lttng ht node string.
178 void lttng_ht_node_init_str(struct lttng_ht_node_str
*node
, char *key
)
183 cds_lfht_node_init(&node
->node
);
187 * Init lttng ht node unsigned long.
190 void lttng_ht_node_init_ulong(struct lttng_ht_node_ulong
*node
,
196 cds_lfht_node_init(&node
->node
);
200 * Init lttng ht node uint64_t.
203 void lttng_ht_node_init_u64(struct lttng_ht_node_u64
*node
,
209 cds_lfht_node_init(&node
->node
);
213 * Init lttng ht node with two uint64_t.
216 void lttng_ht_node_init_two_u64(struct lttng_ht_node_two_u64
*node
,
217 uint64_t key1
, uint64_t key2
)
221 node
->key
.key1
= key1
;
222 node
->key
.key2
= key2
;
223 cds_lfht_node_init(&node
->node
);
227 * Free lttng ht node string.
230 void lttng_ht_node_free_str(struct lttng_ht_node_str
*node
)
237 * Free lttng ht node unsigned long.
240 void lttng_ht_node_free_ulong(struct lttng_ht_node_ulong
*node
)
247 * Free lttng ht node uint64_t.
250 void lttng_ht_node_free_u64(struct lttng_ht_node_u64
*node
)
257 * Free lttng ht node two uint64_t.
260 void lttng_ht_node_free_two_u64(struct lttng_ht_node_two_u64
*node
)
267 * Lookup function in hashtable.
270 void lttng_ht_lookup(struct lttng_ht
*ht
, const void *key
,
271 struct lttng_ht_iter
*iter
)
276 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(key
, lttng_ht_seed
),
277 ht
->match_fct
, key
, &iter
->iter
);
281 * Add unique string node to hashtable.
284 void lttng_ht_add_unique_str(struct lttng_ht
*ht
,
285 struct lttng_ht_node_str
*node
)
287 struct cds_lfht_node
*node_ptr
;
292 /* RCU read lock protects from ABA. */
294 node_ptr
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
295 ht
->match_fct
, node
->key
, &node
->node
);
297 assert(node_ptr
== &node
->node
);
301 * Add string node to hashtable.
304 void lttng_ht_add_str(struct lttng_ht
*ht
,
305 struct lttng_ht_node_str
*node
)
311 /* RCU read lock protects from ABA. */
313 cds_lfht_add(ht
->ht
, ht
->hash_fct(node
->key
, lttng_ht_seed
),
319 * Add unsigned long node to hashtable.
322 void lttng_ht_add_ulong(struct lttng_ht
*ht
, struct lttng_ht_node_ulong
*node
)
328 /* RCU read lock protects from ABA. */
330 cds_lfht_add(ht
->ht
, ht
->hash_fct((void *) node
->key
, lttng_ht_seed
),
336 * Add uint64_t node to hashtable.
339 void lttng_ht_add_u64(struct lttng_ht
*ht
, struct lttng_ht_node_u64
*node
)
345 /* RCU read lock protects from ABA. */
347 cds_lfht_add(ht
->ht
, ht
->hash_fct(&node
->key
, lttng_ht_seed
),
353 * Add unique unsigned long node to hashtable.
356 void lttng_ht_add_unique_ulong(struct lttng_ht
*ht
,
357 struct lttng_ht_node_ulong
*node
)
359 struct cds_lfht_node
*node_ptr
;
364 /* RCU read lock protects from ABA. */
366 node_ptr
= cds_lfht_add_unique(ht
->ht
,
367 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
368 (void *) node
->key
, &node
->node
);
370 assert(node_ptr
== &node
->node
);
374 * Add unique uint64_t node to hashtable.
377 void lttng_ht_add_unique_u64(struct lttng_ht
*ht
,
378 struct lttng_ht_node_u64
*node
)
380 struct cds_lfht_node
*node_ptr
;
385 /* RCU read lock protects from ABA. */
387 node_ptr
= cds_lfht_add_unique(ht
->ht
,
388 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
389 &node
->key
, &node
->node
);
391 assert(node_ptr
== &node
->node
);
395 * Add unique two uint64_t node to hashtable.
398 void lttng_ht_add_unique_two_u64(struct lttng_ht
*ht
,
399 struct lttng_ht_node_two_u64
*node
)
401 struct cds_lfht_node
*node_ptr
;
406 /* RCU read lock protects from ABA. */
408 node_ptr
= cds_lfht_add_unique(ht
->ht
,
409 ht
->hash_fct((void *) &node
->key
, lttng_ht_seed
), ht
->match_fct
,
410 (void *) &node
->key
, &node
->node
);
412 assert(node_ptr
== &node
->node
);
416 * Add replace unsigned long node to hashtable.
419 struct lttng_ht_node_ulong
*lttng_ht_add_replace_ulong(struct lttng_ht
*ht
,
420 struct lttng_ht_node_ulong
*node
)
422 struct cds_lfht_node
*node_ptr
;
427 /* RCU read lock protects from ABA. */
429 node_ptr
= cds_lfht_add_replace(ht
->ht
,
430 ht
->hash_fct((void *) node
->key
, lttng_ht_seed
), ht
->match_fct
,
431 (void *) node
->key
, &node
->node
);
436 return caa_container_of(node_ptr
, struct lttng_ht_node_ulong
, node
);
438 assert(node_ptr
== &node
->node
);
442 * Add replace unsigned long node to hashtable.
445 struct lttng_ht_node_u64
*lttng_ht_add_replace_u64(struct lttng_ht
*ht
,
446 struct lttng_ht_node_u64
*node
)
448 struct cds_lfht_node
*node_ptr
;
453 /* RCU read lock protects from ABA. */
455 node_ptr
= cds_lfht_add_replace(ht
->ht
,
456 ht
->hash_fct(&node
->key
, lttng_ht_seed
), ht
->match_fct
,
457 &node
->key
, &node
->node
);
462 return caa_container_of(node_ptr
, struct lttng_ht_node_u64
, node
);
464 assert(node_ptr
== &node
->node
);
468 * Delete node from hashtable.
471 int lttng_ht_del(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
479 /* RCU read lock protects from ABA. */
481 ret
= cds_lfht_del(ht
->ht
, iter
->iter
.node
);
487 * Get first node in the hashtable.
490 void lttng_ht_get_first(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
496 cds_lfht_first(ht
->ht
, &iter
->iter
);
500 * Get next node in the hashtable.
503 void lttng_ht_get_next(struct lttng_ht
*ht
, struct lttng_ht_iter
*iter
)
509 cds_lfht_next(ht
->ht
, &iter
->iter
);
513 * Return the number of nodes in the hashtable.
516 unsigned long lttng_ht_get_count(struct lttng_ht
*ht
)
524 /* RCU read lock protects from ABA and allows RCU traversal. */
526 cds_lfht_count_nodes(ht
->ht
, &scb
, &count
, &sca
);
533 * Return lttng ht string node from iterator.
536 struct lttng_ht_node_str
*lttng_ht_iter_get_node_str(
537 struct lttng_ht_iter
*iter
)
539 struct cds_lfht_node
*node
;
542 node
= cds_lfht_iter_get_node(&iter
->iter
);
546 return caa_container_of(node
, struct lttng_ht_node_str
, node
);
550 * Return lttng ht unsigned long node from iterator.
553 struct lttng_ht_node_ulong
*lttng_ht_iter_get_node_ulong(
554 struct lttng_ht_iter
*iter
)
556 struct cds_lfht_node
*node
;
559 node
= cds_lfht_iter_get_node(&iter
->iter
);
563 return caa_container_of(node
, struct lttng_ht_node_ulong
, node
);
567 * Return lttng ht unsigned long node from iterator.
570 struct lttng_ht_node_u64
*lttng_ht_iter_get_node_u64(
571 struct lttng_ht_iter
*iter
)
573 struct cds_lfht_node
*node
;
576 node
= cds_lfht_iter_get_node(&iter
->iter
);
580 return caa_container_of(node
, struct lttng_ht_node_u64
, node
);
584 * Return lttng ht stream and index id node from iterator.
587 struct lttng_ht_node_two_u64
*lttng_ht_iter_get_node_two_u64(
588 struct lttng_ht_iter
*iter
)
590 struct cds_lfht_node
*node
;
593 node
= cds_lfht_iter_get_node(&iter
->iter
);
597 return caa_container_of(node
, struct lttng_ht_node_two_u64
, node
);