2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.h>
12 #include <common/hashtable/utils.h>
14 #include "buffer-registry.h"
16 #include "ust-consumer.h"
17 #include "lttng-ust-ctl.h"
18 #include "lttng-ust-error.h"
22 * Set in main.c during initialization process of the daemon. This contains
23 * buffer_reg_uid object which are global registry for per UID buffer. Object
24 * are indexed by session id and matched by the triplet
25 * <session_id/bits_per_long/uid>.
27 static struct lttng_ht
*buffer_registry_uid
;
30 * Initialized at the daemon start. This contains buffer_reg_pid object and
31 * indexed by session id.
33 static struct lttng_ht
*buffer_registry_pid
;
36 * Match function for the per UID registry hash table. It matches a registry
37 * uid object with the triplet <session_id/abi/uid>.
39 static int ht_match_reg_uid(struct cds_lfht_node
*node
, const void *_key
)
41 struct buffer_reg_uid
*reg
;
42 const struct buffer_reg_uid
*key
;
47 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
.node
);
51 if (key
->session_id
!= reg
->session_id
||
52 key
->bits_per_long
!= reg
->bits_per_long
||
53 key
->uid
!= reg
->uid
) {
64 * Hash function for the per UID registry hash table. This XOR the triplet
67 static unsigned long ht_hash_reg_uid(const void *_key
, unsigned long seed
)
70 const struct buffer_reg_uid
*key
= _key
;
74 xored_key
= (uint64_t)(key
->session_id
^ key
->bits_per_long
^ key
->uid
);
75 return hash_key_u64(&xored_key
, seed
);
79 * Initialize global buffer per UID registry. Should only be called ONCE!.
81 void buffer_reg_init_uid_registry(void)
83 /* Should be called once. */
84 assert(!buffer_registry_uid
);
85 buffer_registry_uid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
86 assert(buffer_registry_uid
);
87 buffer_registry_uid
->match_fct
= ht_match_reg_uid
;
88 buffer_registry_uid
->hash_fct
= ht_hash_reg_uid
;
90 DBG3("Global buffer per UID registry initialized");
94 * Allocate and initialize object. Set regp with the object pointer.
96 * Return 0 on success else a negative value and regp is untouched.
98 int buffer_reg_uid_create(uint64_t session_id
, uint32_t bits_per_long
, uid_t uid
,
99 enum lttng_domain_type domain
, struct buffer_reg_uid
**regp
,
100 const char *root_shm_path
, const char *shm_path
)
103 struct buffer_reg_uid
*reg
= NULL
;
107 reg
= zmalloc(sizeof(*reg
));
109 PERROR("zmalloc buffer registry uid");
114 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
115 if (!reg
->registry
) {
116 PERROR("zmalloc buffer registry uid session");
121 reg
->session_id
= session_id
;
122 reg
->bits_per_long
= bits_per_long
;
124 reg
->domain
= domain
;
126 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
127 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
128 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
129 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
130 DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64
,
131 reg
->shm_path
, session_id
);
133 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
134 if (!reg
->registry
->channels
) {
139 cds_lfht_node_init(®
->node
.node
);
142 DBG3("Buffer registry per UID created id: %" PRIu64
", ABI: %u, uid: %d, domain: %d",
143 session_id
, bits_per_long
, uid
, domain
);
155 * Add a buffer registry per UID object to the global registry.
157 void buffer_reg_uid_add(struct buffer_reg_uid
*reg
)
159 struct cds_lfht_node
*nodep
;
160 struct lttng_ht
*ht
= buffer_registry_uid
;
164 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64
,
168 nodep
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(reg
, lttng_ht_seed
),
169 ht
->match_fct
, reg
, ®
->node
.node
);
170 assert(nodep
== ®
->node
.node
);
175 * Find a buffer registry per UID object with given params. RCU read side lock
176 * MUST be acquired before calling this and hold on to protect the object.
178 * Return the object pointer or NULL on error.
180 struct buffer_reg_uid
*buffer_reg_uid_find(uint64_t session_id
,
181 uint32_t bits_per_long
, uid_t uid
)
183 struct lttng_ht_node_u64
*node
;
184 struct lttng_ht_iter iter
;
185 struct buffer_reg_uid
*reg
= NULL
, key
;
186 struct lttng_ht
*ht
= buffer_registry_uid
;
188 /* Setup key we are looking for. */
189 key
.session_id
= session_id
;
190 key
.bits_per_long
= bits_per_long
;
193 DBG3("Buffer registry per UID find id: %" PRIu64
", ABI: %u, uid: %d",
194 session_id
, bits_per_long
, uid
);
196 /* Custom lookup function since it's a different key. */
197 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(&key
, lttng_ht_seed
), ht
->match_fct
,
199 node
= lttng_ht_iter_get_node_u64(&iter
);
203 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
);
210 * Initialize global buffer per PID registry. Should only be called ONCE!.
212 void buffer_reg_init_pid_registry(void)
214 /* Should be called once. */
215 assert(!buffer_registry_pid
);
216 buffer_registry_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
217 assert(buffer_registry_pid
);
219 DBG3("Global buffer per PID registry initialized");
223 * Allocate and initialize object. Set regp with the object pointer.
225 * Return 0 on success else a negative value and regp is untouched.
227 int buffer_reg_pid_create(uint64_t session_id
, struct buffer_reg_pid
**regp
,
228 const char *root_shm_path
, const char *shm_path
)
231 struct buffer_reg_pid
*reg
= NULL
;
235 reg
= zmalloc(sizeof(*reg
));
237 PERROR("zmalloc buffer registry pid");
242 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
243 if (!reg
->registry
) {
244 PERROR("zmalloc buffer registry pid session");
249 /* A cast is done here so we can use the session ID as a u64 ht node. */
250 reg
->session_id
= session_id
;
252 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
253 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
254 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
255 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
256 DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64
,
257 reg
->shm_path
, session_id
);
259 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
260 if (!reg
->registry
->channels
) {
265 lttng_ht_node_init_u64(®
->node
, reg
->session_id
);
268 DBG3("Buffer registry per PID created with session id: %" PRIu64
,
281 * Add a buffer registry per PID object to the global registry.
283 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
287 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64
,
291 lttng_ht_add_unique_u64(buffer_registry_pid
, ®
->node
);
296 * Find a buffer registry per PID object with given params. RCU read side lock
297 * MUST be acquired before calling this and hold on to protect the object.
299 * Return the object pointer or NULL on error.
301 struct buffer_reg_pid
*buffer_reg_pid_find(uint64_t session_id
)
303 struct lttng_ht_node_u64
*node
;
304 struct lttng_ht_iter iter
;
305 struct buffer_reg_pid
*reg
= NULL
;
306 struct lttng_ht
*ht
= buffer_registry_pid
;
308 DBG3("Buffer registry per PID find id: %" PRIu64
, session_id
);
310 lttng_ht_lookup(ht
, &session_id
, &iter
);
311 node
= lttng_ht_iter_get_node_u64(&iter
);
315 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
322 * Find the consumer channel key from a UST session per-uid channel key.
324 * Return the matching key or -1 if not found.
326 int buffer_reg_uid_consumer_channel_key(
327 struct cds_list_head
*buffer_reg_uid_list
,
328 uint64_t chan_key
, uint64_t *consumer_chan_key
)
330 struct lttng_ht_iter iter
;
331 struct buffer_reg_uid
*uid_reg
= NULL
;
332 struct buffer_reg_session
*session_reg
= NULL
;
333 struct buffer_reg_channel
*reg_chan
;
338 * For the per-uid registry, we have to iterate since we don't have the
339 * uid and bitness key.
341 cds_list_for_each_entry(uid_reg
, buffer_reg_uid_list
, lnode
) {
342 session_reg
= uid_reg
->registry
;
343 cds_lfht_for_each_entry(session_reg
->channels
->ht
,
344 &iter
.iter
, reg_chan
, node
.node
) {
345 if (reg_chan
->key
== chan_key
) {
346 *consumer_chan_key
= reg_chan
->consumer_key
;
359 * Allocate and initialize a buffer registry channel with the given key. Set
360 * regp with the object pointer.
362 * Return 0 on success or else a negative value keeping regp untouched.
364 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
366 struct buffer_reg_channel
*reg
;
370 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
372 reg
= zmalloc(sizeof(*reg
));
374 PERROR("zmalloc buffer registry channel");
379 CDS_INIT_LIST_HEAD(®
->streams
);
380 pthread_mutex_init(®
->stream_list_lock
, NULL
);
382 lttng_ht_node_init_u64(®
->node
, key
);
389 * Allocate and initialize a buffer registry stream. Set regp with the object
392 * Return 0 on success or else a negative value keeping regp untouched.
394 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
396 struct buffer_reg_stream
*reg
;
400 DBG3("Buffer registry creating stream");
402 reg
= zmalloc(sizeof(*reg
));
404 PERROR("zmalloc buffer registry stream");
414 * Add stream to the list in the channel.
416 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
417 struct buffer_reg_channel
*channel
)
422 pthread_mutex_lock(&channel
->stream_list_lock
);
423 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
424 channel
->stream_count
++;
425 pthread_mutex_unlock(&channel
->stream_list_lock
);
429 * Add a buffer registry channel object to the given session.
431 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
432 struct buffer_reg_channel
*channel
)
438 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
443 * Find a buffer registry channel object with the given key. RCU read side lock
444 * MUST be acquired and hold on until the object reference is not needed
447 * Return the object pointer or NULL on error.
449 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
450 struct buffer_reg_uid
*reg
)
452 struct lttng_ht_node_u64
*node
;
453 struct lttng_ht_iter iter
;
454 struct buffer_reg_channel
*chan
= NULL
;
459 switch (reg
->domain
) {
460 case LTTNG_DOMAIN_UST
:
461 ht
= reg
->registry
->channels
;
468 lttng_ht_lookup(ht
, &key
, &iter
);
469 node
= lttng_ht_iter_get_node_u64(&iter
);
473 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
480 * Destroy a buffer registry stream with the given domain.
482 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
483 enum lttng_domain_type domain
)
489 DBG3("Buffer registry stream destroy with handle %d",
490 regp
->obj
.ust
->handle
);
493 case LTTNG_DOMAIN_UST
:
497 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
498 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
499 ERR("Buffer reg stream release obj handle %d failed with ret %d",
500 regp
->obj
.ust
->handle
, ret
);
503 lttng_fd_put(LTTNG_FD_APPS
, 2);
515 * Remove buffer registry channel object from the session hash table. RCU read
516 * side lock MUST be acquired before calling this.
518 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
519 struct buffer_reg_channel
*regp
)
522 struct lttng_ht_iter iter
;
527 iter
.iter
.node
= ®p
->node
.node
;
528 ret
= lttng_ht_del(session
->channels
, &iter
);
533 * Destroy a buffer registry channel with the given domain.
535 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
536 enum lttng_domain_type domain
)
542 DBG3("Buffer registry channel destroy with key %" PRIu32
, regp
->key
);
545 case LTTNG_DOMAIN_UST
:
548 struct buffer_reg_stream
*sreg
, *stmp
;
550 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
551 cds_list_del(&sreg
->lnode
);
552 regp
->stream_count
--;
553 buffer_reg_stream_destroy(sreg
, domain
);
557 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
558 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
559 ERR("Buffer reg channel release obj handle %d failed with ret %d",
560 regp
->obj
.ust
->handle
, ret
);
564 lttng_fd_put(LTTNG_FD_APPS
, 1);
576 * Destroy a buffer registry session with the given domain.
578 * Should *NOT* be called with RCU read-side lock held.
580 static void buffer_reg_session_destroy(struct buffer_reg_session
*regp
,
581 enum lttng_domain_type domain
)
584 struct lttng_ht_iter iter
;
585 struct buffer_reg_channel
*reg_chan
;
587 DBG3("Buffer registry session destroy");
589 /* Destroy all channels. */
591 cds_lfht_for_each_entry(regp
->channels
->ht
, &iter
.iter
, reg_chan
,
593 ret
= lttng_ht_del(regp
->channels
, &iter
);
595 buffer_reg_channel_destroy(reg_chan
, domain
);
599 ht_cleanup_push(regp
->channels
);
602 case LTTNG_DOMAIN_UST
:
603 ust_registry_session_destroy(regp
->reg
.ust
);
615 * Remove buffer registry UID object from the global hash table.
617 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
620 struct lttng_ht_iter iter
;
625 iter
.iter
.node
= ®p
->node
.node
;
626 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
631 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
633 struct lttng_ht_node_u64
*node
=
634 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
635 struct buffer_reg_uid
*reg
=
636 caa_container_of(node
, struct buffer_reg_uid
, node
);
638 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
642 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
644 struct lttng_ht_node_u64
*node
=
645 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
646 struct buffer_reg_pid
*reg
=
647 caa_container_of(node
, struct buffer_reg_pid
, node
);
649 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
654 * Destroy buffer registry per UID. The given pointer is NOT removed from any
655 * list or hash table. Use buffer_reg_pid_remove() before calling this function
656 * for the case that the object is in the global hash table.
658 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
659 struct consumer_output
*consumer
)
661 struct consumer_socket
*socket
;
667 DBG3("Buffer registry per UID destroy with id: %" PRIu64
", ABI: %u, uid: %d",
668 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
675 /* Get the right socket from the consumer object. */
676 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
682 switch (regp
->domain
) {
683 case LTTNG_DOMAIN_UST
:
684 if (regp
->registry
->reg
.ust
->metadata_key
) {
685 /* Return value does not matter. This call will print errors. */
686 (void) consumer_close_metadata(socket
,
687 regp
->registry
->reg
.ust
->metadata_key
);
699 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
703 * Remove buffer registry UID object from the global hash table. RCU read side
704 * lock MUST be acquired before calling this.
706 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
709 struct lttng_ht_iter iter
;
713 iter
.iter
.node
= ®p
->node
.node
;
714 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
719 * Destroy buffer registry per PID. The pointer is NOT removed from the global
720 * hash table. Call buffer_reg_pid_remove() before that if the object was
721 * previously added to the global hash table.
723 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
729 DBG3("Buffer registry per PID destroy with id: %" PRIu64
,
732 /* This registry is only used by UST. */
733 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
737 * Destroy per PID and UID registry hash table.
739 * Should *NOT* be called with RCU read-side lock held.
741 void buffer_reg_destroy_registries(void)
743 DBG3("Buffer registry destroy all registry");
744 ht_cleanup_push(buffer_registry_uid
);
745 ht_cleanup_push(buffer_registry_pid
);
This page took 0.049144 seconds and 4 git commands to generate.