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 reg
->registry
->maps
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
140 if (!reg
->registry
->maps
) {
141 lttng_ht_destroy(reg
->registry
->channels
);
147 cds_lfht_node_init(®
->node
.node
);
150 DBG3("Buffer registry per UID created id: %" PRIu64
", ABI: %u, uid: %d, domain: %d",
151 session_id
, bits_per_long
, uid
, domain
);
163 * Add a buffer registry per UID object to the global registry.
165 void buffer_reg_uid_add(struct buffer_reg_uid
*reg
)
167 struct cds_lfht_node
*nodep
;
168 struct lttng_ht
*ht
= buffer_registry_uid
;
172 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64
,
176 nodep
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(reg
, lttng_ht_seed
),
177 ht
->match_fct
, reg
, ®
->node
.node
);
178 assert(nodep
== ®
->node
.node
);
183 * Find a buffer registry per UID object with given params. RCU read side lock
184 * MUST be acquired before calling this and hold on to protect the object.
186 * Return the object pointer or NULL on error.
188 struct buffer_reg_uid
*buffer_reg_uid_find(uint64_t session_id
,
189 uint32_t bits_per_long
, uid_t uid
)
191 struct lttng_ht_node_u64
*node
;
192 struct lttng_ht_iter iter
;
193 struct buffer_reg_uid
*reg
= NULL
, key
;
194 struct lttng_ht
*ht
= buffer_registry_uid
;
196 /* Setup key we are looking for. */
197 key
.session_id
= session_id
;
198 key
.bits_per_long
= bits_per_long
;
201 DBG3("Buffer registry per UID find id: %" PRIu64
", ABI: %u, uid: %d",
202 session_id
, bits_per_long
, uid
);
204 /* Custom lookup function since it's a different key. */
205 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(&key
, lttng_ht_seed
), ht
->match_fct
,
207 node
= lttng_ht_iter_get_node_u64(&iter
);
211 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
);
218 * Initialize global buffer per PID registry. Should only be called ONCE!.
220 void buffer_reg_init_pid_registry(void)
222 /* Should be called once. */
223 assert(!buffer_registry_pid
);
224 buffer_registry_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
225 assert(buffer_registry_pid
);
227 DBG3("Global buffer per PID registry initialized");
231 * Allocate and initialize object. Set regp with the object pointer.
233 * Return 0 on success else a negative value and regp is untouched.
235 int buffer_reg_pid_create(uint64_t session_id
, struct buffer_reg_pid
**regp
,
236 const char *root_shm_path
, const char *shm_path
)
239 struct buffer_reg_pid
*reg
= NULL
;
243 reg
= zmalloc(sizeof(*reg
));
245 PERROR("zmalloc buffer registry pid");
250 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
251 if (!reg
->registry
) {
252 PERROR("zmalloc buffer registry pid session");
257 /* A cast is done here so we can use the session ID as a u64 ht node. */
258 reg
->session_id
= session_id
;
260 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
261 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
262 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
263 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
264 DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64
,
265 reg
->shm_path
, session_id
);
267 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
268 if (!reg
->registry
->channels
) {
273 reg
->registry
->maps
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
274 if (!reg
->registry
->maps
) {
275 lttng_ht_destroy(reg
->registry
->channels
);
280 lttng_ht_node_init_u64(®
->node
, reg
->session_id
);
283 DBG3("Buffer registry per PID created with session id: %" PRIu64
,
296 * Add a buffer registry per PID object to the global registry.
298 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
302 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64
,
306 lttng_ht_add_unique_u64(buffer_registry_pid
, ®
->node
);
311 * Find a buffer registry per PID object with given params. RCU read side lock
312 * MUST be acquired before calling this and hold on to protect the object.
314 * Return the object pointer or NULL on error.
316 struct buffer_reg_pid
*buffer_reg_pid_find(uint64_t session_id
)
318 struct lttng_ht_node_u64
*node
;
319 struct lttng_ht_iter iter
;
320 struct buffer_reg_pid
*reg
= NULL
;
321 struct lttng_ht
*ht
= buffer_registry_pid
;
323 DBG3("Buffer registry per PID find id: %" PRIu64
, session_id
);
325 lttng_ht_lookup(ht
, &session_id
, &iter
);
326 node
= lttng_ht_iter_get_node_u64(&iter
);
330 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
337 * Find the consumer channel key from a UST session per-uid channel key.
339 * Return the matching key or -1 if not found.
341 int buffer_reg_uid_consumer_channel_key(
342 struct cds_list_head
*buffer_reg_uid_list
,
343 uint64_t chan_key
, uint64_t *consumer_chan_key
)
345 struct lttng_ht_iter iter
;
346 struct buffer_reg_uid
*uid_reg
= NULL
;
347 struct buffer_reg_session
*session_reg
= NULL
;
348 struct buffer_reg_channel
*reg_chan
;
353 * For the per-uid registry, we have to iterate since we don't have the
354 * uid and bitness key.
356 cds_list_for_each_entry(uid_reg
, buffer_reg_uid_list
, lnode
) {
357 session_reg
= uid_reg
->registry
;
358 cds_lfht_for_each_entry(session_reg
->channels
->ht
,
359 &iter
.iter
, reg_chan
, node
.node
) {
360 if (reg_chan
->key
== chan_key
) {
361 *consumer_chan_key
= reg_chan
->consumer_key
;
374 * Allocate and initialize a buffer registry channel with the given key. Set
375 * regp with the object pointer.
377 * Return 0 on success or else a negative value keeping regp untouched.
379 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
381 struct buffer_reg_channel
*reg
;
385 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
387 reg
= zmalloc(sizeof(*reg
));
389 PERROR("zmalloc buffer registry channel");
394 CDS_INIT_LIST_HEAD(®
->streams
);
395 pthread_mutex_init(®
->stream_list_lock
, NULL
);
397 lttng_ht_node_init_u64(®
->node
, key
);
404 * Allocate and initialize a buffer registry map with the given key. Set
405 * regp with the object pointer.
407 * Return 0 on success or else a negative value keeping regp untouched.
409 int buffer_reg_map_create(uint64_t key
, struct buffer_reg_map
**regp
)
411 struct buffer_reg_map
*reg
;
415 DBG3("Buffer registry map create with key: %" PRIu64
, key
);
417 reg
= zmalloc(sizeof(*reg
));
419 PERROR("zmalloc buffer registry map");
424 CDS_INIT_LIST_HEAD(®
->counters
);
425 pthread_mutex_init(®
->counter_list_lock
, NULL
);
427 lttng_ht_node_init_u64(®
->node
, key
);
434 * Allocate and initialize a buffer registry stream. Set regp with the object
437 * Return 0 on success or else a negative value keeping regp untouched.
439 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
441 struct buffer_reg_stream
*reg
;
445 DBG3("Buffer registry creating stream");
447 reg
= zmalloc(sizeof(*reg
));
449 PERROR("zmalloc buffer registry stream");
459 * Allocate and initialize a buffer registry map_counter. Set regp with the object
462 * Return 0 on success or else a negative value keeping regp untouched.
464 int buffer_reg_map_counter_create(struct buffer_reg_map_counter
**regp
)
466 struct buffer_reg_map_counter
*reg
;
470 DBG3("Buffer registry creating map_counter");
472 reg
= zmalloc(sizeof(*reg
));
474 PERROR("zmalloc buffer registry map_counter");
484 * Add stream to the list in the channel.
486 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
487 struct buffer_reg_channel
*channel
)
492 pthread_mutex_lock(&channel
->stream_list_lock
);
493 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
494 channel
->stream_count
++;
495 pthread_mutex_unlock(&channel
->stream_list_lock
);
499 * Add map_counter to the list in the map.
501 void buffer_reg_map_counter_add(struct buffer_reg_map_counter
*map_counter
,
502 struct buffer_reg_map
*map
)
507 pthread_mutex_lock(&map
->counter_list_lock
);
508 cds_list_add_tail(&map_counter
->lnode
, &map
->counters
);
509 map
->counter_count
++;
510 pthread_mutex_unlock(&map
->counter_list_lock
);
514 * Add a buffer registry channel object to the given session.
516 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
517 struct buffer_reg_channel
*channel
)
523 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
528 * Add a buffer registry map object to the given session.
530 void buffer_reg_map_add(struct buffer_reg_session
*session
,
531 struct buffer_reg_map
*map
)
537 lttng_ht_add_unique_u64(session
->maps
, &map
->node
);
542 * Find a buffer registry channel object with the given key. RCU read side lock
543 * MUST be acquired and hold on until the object reference is not needed
546 * Return the object pointer or NULL on error.
548 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
549 struct buffer_reg_uid
*reg
)
551 struct lttng_ht_node_u64
*node
;
552 struct lttng_ht_iter iter
;
553 struct buffer_reg_channel
*chan
= NULL
;
558 switch (reg
->domain
) {
559 case LTTNG_DOMAIN_UST
:
560 ht
= reg
->registry
->channels
;
567 lttng_ht_lookup(ht
, &key
, &iter
);
568 node
= lttng_ht_iter_get_node_u64(&iter
);
572 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
579 * Find a buffer registry map object with the given key. RCU read side lock
580 * MUST be acquired and hold on until the object reference is not needed
583 * Return the object pointer or NULL on error.
585 struct buffer_reg_map
*buffer_reg_map_find(uint64_t key
,
586 struct buffer_reg_uid
*reg
)
588 struct lttng_ht_node_u64
*node
;
589 struct lttng_ht_iter iter
;
590 struct buffer_reg_map
*map
= NULL
;
595 switch (reg
->domain
) {
596 case LTTNG_DOMAIN_UST
:
597 ht
= reg
->registry
->maps
;
604 lttng_ht_lookup(ht
, &key
, &iter
);
605 node
= lttng_ht_iter_get_node_u64(&iter
);
609 map
= caa_container_of(node
, struct buffer_reg_map
, node
);
616 * Destroy a buffer registry stream with the given domain.
618 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
619 enum lttng_domain_type domain
)
625 DBG3("Buffer registry stream destroy with handle %d",
626 regp
->obj
.ust
->handle
);
629 case LTTNG_DOMAIN_UST
:
633 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
634 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
635 ERR("Buffer reg stream release obj handle %d failed with ret %d",
636 regp
->obj
.ust
->handle
, ret
);
639 lttng_fd_put(LTTNG_FD_APPS
, 2);
651 * Destroy a buffer registry map_counter with the given domain.
653 void buffer_reg_map_counter_destroy(struct buffer_reg_map_counter
*regp
,
654 enum lttng_domain_type domain
)
660 DBG3("Buffer registry map counter destroy with handle %d",
661 regp
->obj
.ust
->handle
);
664 case LTTNG_DOMAIN_UST
:
668 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
669 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
670 ERR("Buffer reg map counter release obj handle %d failed with ret %d",
671 regp
->obj
.ust
->handle
, ret
);
674 lttng_fd_put(LTTNG_FD_APPS
, 2);
686 * Remove buffer registry channel object from the session hash table. RCU read
687 * side lock MUST be acquired before calling this.
689 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
690 struct buffer_reg_channel
*regp
)
693 struct lttng_ht_iter iter
;
698 iter
.iter
.node
= ®p
->node
.node
;
699 ret
= lttng_ht_del(session
->channels
, &iter
);
704 * Remove buffer registry map object from the session hash table. RCU read
705 * side lock MUST be acquired before calling this.
707 void buffer_reg_map_remove(struct buffer_reg_session
*session
,
708 struct buffer_reg_map
*regp
)
711 struct lttng_ht_iter iter
;
716 iter
.iter
.node
= ®p
->node
.node
;
717 ret
= lttng_ht_del(session
->maps
, &iter
);
722 * Destroy a buffer registry channel with the given domain.
724 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
725 enum lttng_domain_type domain
)
731 DBG3("Buffer registry channel destroy with key %" PRIu32
, regp
->key
);
734 case LTTNG_DOMAIN_UST
:
737 struct buffer_reg_stream
*sreg
, *stmp
;
739 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
740 cds_list_del(&sreg
->lnode
);
741 regp
->stream_count
--;
742 buffer_reg_stream_destroy(sreg
, domain
);
746 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
747 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
748 ERR("Buffer reg channel release obj handle %d failed with ret %d",
749 regp
->obj
.ust
->handle
, ret
);
753 lttng_fd_put(LTTNG_FD_APPS
, 1);
765 * Destroy a buffer registry map with the given domain.
767 void buffer_reg_map_destroy(struct buffer_reg_map
*regp
,
768 enum lttng_domain_type domain
)
774 DBG3("Buffer registry map destroy with key %" PRIu32
, regp
->key
);
777 case LTTNG_DOMAIN_UST
:
780 struct buffer_reg_map_counter
*map_counter_reg
, *tmp
;
782 cds_list_for_each_entry_safe(map_counter_reg
, tmp
, ®p
->counters
, lnode
) {
783 cds_list_del(&map_counter_reg
->lnode
);
784 regp
->counter_count
--;
785 buffer_reg_map_counter_destroy(map_counter_reg
, domain
);
789 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
790 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
791 ERR("Buffer reg map release obj handle %d failed with ret %d",
792 regp
->obj
.ust
->handle
, ret
);
796 lttng_fd_put(LTTNG_FD_APPS
, 1);
808 * Destroy a buffer registry session with the given domain.
810 * Should *NOT* be called with RCU read-side lock held.
812 static void buffer_reg_session_destroy(struct buffer_reg_session
*regp
,
813 enum lttng_domain_type domain
)
816 struct lttng_ht_iter iter
;
817 struct buffer_reg_channel
*reg_chan
;
818 struct buffer_reg_map
*reg_map
;
820 DBG3("Buffer registry session destroy");
822 /* Destroy all channels. */
824 cds_lfht_for_each_entry(regp
->channels
->ht
, &iter
.iter
, reg_chan
,
826 ret
= lttng_ht_del(regp
->channels
, &iter
);
828 buffer_reg_channel_destroy(reg_chan
, domain
);
830 cds_lfht_for_each_entry(regp
->maps
->ht
, &iter
.iter
, reg_map
,
832 ret
= lttng_ht_del(regp
->maps
, &iter
);
834 buffer_reg_map_destroy(reg_map
, domain
);
838 ht_cleanup_push(regp
->channels
);
839 ht_cleanup_push(regp
->maps
);
842 case LTTNG_DOMAIN_UST
:
843 ust_registry_session_destroy(regp
->reg
.ust
);
855 * Remove buffer registry UID object from the global hash table.
857 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
860 struct lttng_ht_iter iter
;
865 iter
.iter
.node
= ®p
->node
.node
;
866 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
871 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
873 struct lttng_ht_node_u64
*node
=
874 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
875 struct buffer_reg_uid
*reg
=
876 caa_container_of(node
, struct buffer_reg_uid
, node
);
878 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
882 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
884 struct lttng_ht_node_u64
*node
=
885 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
886 struct buffer_reg_pid
*reg
=
887 caa_container_of(node
, struct buffer_reg_pid
, node
);
889 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
894 * Destroy buffer registry per UID. The given pointer is NOT removed from any
895 * list or hash table. Use buffer_reg_pid_remove() before calling this function
896 * for the case that the object is in the global hash table.
898 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
899 struct consumer_output
*consumer
)
901 struct consumer_socket
*socket
;
907 DBG3("Buffer registry per UID destroy with id: %" PRIu64
", ABI: %u, uid: %d",
908 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
915 /* Get the right socket from the consumer object. */
916 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
922 switch (regp
->domain
) {
923 case LTTNG_DOMAIN_UST
:
924 if (regp
->registry
->reg
.ust
->metadata_key
) {
925 /* Return value does not matter. This call will print errors. */
926 (void) consumer_close_metadata(socket
,
927 regp
->registry
->reg
.ust
->metadata_key
);
939 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
943 * Remove buffer registry UID object from the global hash table. RCU read side
944 * lock MUST be acquired before calling this.
946 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
949 struct lttng_ht_iter iter
;
953 iter
.iter
.node
= ®p
->node
.node
;
954 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
959 * Destroy buffer registry per PID. The pointer is NOT removed from the global
960 * hash table. Call buffer_reg_pid_remove() before that if the object was
961 * previously added to the global hash table.
963 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
969 DBG3("Buffer registry per PID destroy with id: %" PRIu64
,
972 /* This registry is only used by UST. */
973 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
977 * Destroy per PID and UID registry hash table.
979 * Should *NOT* be called with RCU read-side lock held.
981 void buffer_reg_destroy_registries(void)
983 DBG3("Buffer registry destroy all registry");
984 ht_cleanup_push(buffer_registry_uid
);
985 ht_cleanup_push(buffer_registry_pid
);
This page took 0.057092 seconds and 5 git commands to generate.