2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.hpp>
12 #include <common/hashtable/utils.hpp>
14 #include "buffer-registry.hpp"
15 #include "fd-limit.hpp"
16 #include "ust-consumer.hpp"
17 #include "lttng-ust-ctl.hpp"
18 #include "lttng-ust-error.hpp"
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
);
49 key
= (buffer_reg_uid
*) _key
;
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
= (buffer_reg_uid
*) _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 LTTNG_ASSERT(!buffer_registry_uid
);
85 buffer_registry_uid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
86 LTTNG_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
<buffer_reg_uid
>();
109 PERROR("zmalloc buffer registry uid");
114 reg
->registry
= zmalloc
<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 LTTNG_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 ASSERT_RCU_READ_LOCKED();
190 /* Setup key we are looking for. */
191 key
.session_id
= session_id
;
192 key
.bits_per_long
= bits_per_long
;
195 DBG3("Buffer registry per UID find id: %" PRIu64
", ABI: %u, uid: %d",
196 session_id
, bits_per_long
, uid
);
198 /* Custom lookup function since it's a different key. */
199 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(&key
, lttng_ht_seed
), ht
->match_fct
,
201 node
= lttng_ht_iter_get_node_u64(&iter
);
205 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
);
212 * Initialize global buffer per PID registry. Should only be called ONCE!.
214 void buffer_reg_init_pid_registry(void)
216 /* Should be called once. */
217 LTTNG_ASSERT(!buffer_registry_pid
);
218 buffer_registry_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
219 LTTNG_ASSERT(buffer_registry_pid
);
221 DBG3("Global buffer per PID registry initialized");
225 * Allocate and initialize object. Set regp with the object pointer.
227 * Return 0 on success else a negative value and regp is untouched.
229 int buffer_reg_pid_create(uint64_t session_id
, struct buffer_reg_pid
**regp
,
230 const char *root_shm_path
, const char *shm_path
)
233 struct buffer_reg_pid
*reg
= NULL
;
237 reg
= zmalloc
<buffer_reg_pid
>();
239 PERROR("zmalloc buffer registry pid");
244 reg
->registry
= zmalloc
<buffer_reg_session
>();
245 if (!reg
->registry
) {
246 PERROR("zmalloc buffer registry pid session");
251 /* A cast is done here so we can use the session ID as a u64 ht node. */
252 reg
->session_id
= session_id
;
254 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
255 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
256 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
257 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
258 DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64
,
259 reg
->shm_path
, session_id
);
261 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
262 if (!reg
->registry
->channels
) {
267 lttng_ht_node_init_u64(®
->node
, reg
->session_id
);
270 DBG3("Buffer registry per PID created with session id: %" PRIu64
,
283 * Add a buffer registry per PID object to the global registry.
285 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
289 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64
,
293 lttng_ht_add_unique_u64(buffer_registry_pid
, ®
->node
);
298 * Find a buffer registry per PID object with given params. RCU read side lock
299 * MUST be acquired before calling this and hold on to protect the object.
301 * Return the object pointer or NULL on error.
303 struct buffer_reg_pid
*buffer_reg_pid_find(uint64_t session_id
)
305 struct lttng_ht_node_u64
*node
;
306 struct lttng_ht_iter iter
;
307 struct buffer_reg_pid
*reg
= NULL
;
308 struct lttng_ht
*ht
= buffer_registry_pid
;
310 DBG3("Buffer registry per PID find id: %" PRIu64
, session_id
);
312 lttng_ht_lookup(ht
, &session_id
, &iter
);
313 node
= lttng_ht_iter_get_node_u64(&iter
);
317 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
324 * Find the consumer channel key from a UST session per-uid channel key.
326 * Return the matching key or -1 if not found.
328 int buffer_reg_uid_consumer_channel_key(
329 struct cds_list_head
*buffer_reg_uid_list
,
330 uint64_t chan_key
, uint64_t *consumer_chan_key
)
332 struct lttng_ht_iter iter
;
333 struct buffer_reg_uid
*uid_reg
= NULL
;
334 struct buffer_reg_session
*session_reg
= NULL
;
335 struct buffer_reg_channel
*reg_chan
;
340 * For the per-uid registry, we have to iterate since we don't have the
341 * uid and bitness key.
343 cds_list_for_each_entry(uid_reg
, buffer_reg_uid_list
, lnode
) {
344 session_reg
= uid_reg
->registry
;
345 cds_lfht_for_each_entry(session_reg
->channels
->ht
,
346 &iter
.iter
, reg_chan
, node
.node
) {
347 if (reg_chan
->key
== chan_key
) {
348 *consumer_chan_key
= reg_chan
->consumer_key
;
361 * Allocate and initialize a buffer registry channel with the given key. Set
362 * regp with the object pointer.
364 * Return 0 on success or else a negative value keeping regp untouched.
366 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
368 struct buffer_reg_channel
*reg
;
372 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
374 reg
= zmalloc
<buffer_reg_channel
>();
376 PERROR("zmalloc buffer registry channel");
381 CDS_INIT_LIST_HEAD(®
->streams
);
382 pthread_mutex_init(®
->stream_list_lock
, NULL
);
384 lttng_ht_node_init_u64(®
->node
, key
);
391 * Allocate and initialize a buffer registry stream. Set regp with the object
394 * Return 0 on success or else a negative value keeping regp untouched.
396 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
398 struct buffer_reg_stream
*reg
;
402 DBG3("Buffer registry creating stream");
404 reg
= zmalloc
<buffer_reg_stream
>();
406 PERROR("zmalloc buffer registry stream");
416 * Add stream to the list in the channel.
418 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
419 struct buffer_reg_channel
*channel
)
421 LTTNG_ASSERT(stream
);
422 LTTNG_ASSERT(channel
);
424 pthread_mutex_lock(&channel
->stream_list_lock
);
425 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
426 channel
->stream_count
++;
427 pthread_mutex_unlock(&channel
->stream_list_lock
);
431 * Add a buffer registry channel object to the given session.
433 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
434 struct buffer_reg_channel
*channel
)
436 LTTNG_ASSERT(session
);
437 LTTNG_ASSERT(channel
);
440 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
445 * Find a buffer registry channel object with the given key. RCU read side lock
446 * MUST be acquired and hold on until the object reference is not needed
449 * Return the object pointer or NULL on error.
451 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
452 struct buffer_reg_uid
*reg
)
454 struct lttng_ht_node_u64
*node
;
455 struct lttng_ht_iter iter
;
456 struct buffer_reg_channel
*chan
= NULL
;
461 switch (reg
->domain
) {
462 case LTTNG_DOMAIN_UST
:
463 ht
= reg
->registry
->channels
;
470 lttng_ht_lookup(ht
, &key
, &iter
);
471 node
= lttng_ht_iter_get_node_u64(&iter
);
475 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
482 * Destroy a buffer registry stream with the given domain.
484 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
485 enum lttng_domain_type domain
)
491 DBG3("Buffer registry stream destroy with handle %d",
492 regp
->obj
.ust
->handle
);
495 case LTTNG_DOMAIN_UST
:
499 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
500 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
501 ERR("Buffer reg stream release obj handle %d failed with ret %d",
502 regp
->obj
.ust
->handle
, ret
);
505 lttng_fd_put(LTTNG_FD_APPS
, 2);
517 * Remove buffer registry channel object from the session hash table. RCU read
518 * side lock MUST be acquired before calling this.
520 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
521 struct buffer_reg_channel
*regp
)
524 struct lttng_ht_iter iter
;
526 LTTNG_ASSERT(session
);
529 iter
.iter
.node
= ®p
->node
.node
;
530 ret
= lttng_ht_del(session
->channels
, &iter
);
535 * Destroy a buffer registry channel with the given domain.
537 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
538 enum lttng_domain_type domain
)
544 DBG3("Buffer registry channel destroy with key %" PRIu32
, regp
->key
);
547 case LTTNG_DOMAIN_UST
:
550 struct buffer_reg_stream
*sreg
, *stmp
;
552 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
553 cds_list_del(&sreg
->lnode
);
554 regp
->stream_count
--;
555 buffer_reg_stream_destroy(sreg
, domain
);
559 ret
= ust_app_release_object(NULL
, regp
->obj
.ust
);
560 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
561 ERR("Buffer reg channel release obj handle %d failed with ret %d",
562 regp
->obj
.ust
->handle
, ret
);
566 lttng_fd_put(LTTNG_FD_APPS
, 1);
578 * Destroy a buffer registry session with the given domain.
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 lttng_ht_destroy(regp
->channels
);
602 case LTTNG_DOMAIN_UST
:
603 ust_registry_session_destroy(regp
->reg
.ust
);
614 * Remove buffer registry UID object from the global hash table.
616 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
619 struct lttng_ht_iter iter
;
624 iter
.iter
.node
= ®p
->node
.node
;
625 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
630 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
632 struct lttng_ht_node_u64
*node
=
633 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
634 struct buffer_reg_uid
*reg
=
635 caa_container_of(node
, struct buffer_reg_uid
, node
);
637 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
641 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
643 struct lttng_ht_node_u64
*node
=
644 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
645 struct buffer_reg_pid
*reg
=
646 caa_container_of(node
, struct buffer_reg_pid
, node
);
648 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
653 * Destroy buffer registry per UID. The given pointer is NOT removed from any
654 * list or hash table. Use buffer_reg_pid_remove() before calling this function
655 * for the case that the object is in the global hash table.
657 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
658 struct consumer_output
*consumer
)
660 struct consumer_socket
*socket
;
666 DBG3("Buffer registry per UID destroy with id: %" PRIu64
", ABI: %u, uid: %d",
667 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
674 /* Get the right socket from the consumer object. */
675 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
681 switch (regp
->domain
) {
682 case LTTNG_DOMAIN_UST
:
683 if (regp
->registry
->reg
.ust
->_metadata_key
) {
684 /* Return value does not matter. This call will print errors. */
685 (void) consumer_close_metadata(socket
,
686 regp
->registry
->reg
.ust
->_metadata_key
);
698 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
702 * Remove buffer registry UID object from the global hash table. RCU read side
703 * lock MUST be acquired before calling this.
705 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
708 struct lttng_ht_iter iter
;
712 iter
.iter
.node
= ®p
->node
.node
;
713 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
718 * Destroy buffer registry per PID. The pointer is NOT removed from the global
719 * hash table. Call buffer_reg_pid_remove() before that if the object was
720 * previously added to the global hash table.
722 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
728 DBG3("Buffer registry per PID destroy with id: %" PRIu64
,
731 /* This registry is only used by UST. */
732 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
736 * Destroy per PID and UID registry hash table.
738 void buffer_reg_destroy_registries(void)
740 DBG3("Buffer registry destroy all registry");
741 lttng_ht_destroy(buffer_registry_uid
);
742 lttng_ht_destroy(buffer_registry_pid
);
This page took 0.046319 seconds and 4 git commands to generate.