2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <common/common.h>
23 #include <common/hashtable/utils.h>
25 #include "buffer-registry.h"
27 #include "ust-consumer.h"
32 * Set in main.c during initialization process of the daemon. This contains
33 * buffer_reg_uid object which are global registry for per UID buffer. Object
34 * are indexed by session id and matched by the triplet
35 * <session_id/bits_per_long/uid>.
37 static struct lttng_ht
*buffer_registry_uid
;
40 * Initialized at the daemon start. This contains buffer_reg_pid object and
41 * indexed by session id.
43 static struct lttng_ht
*buffer_registry_pid
;
46 * Match function for the per UID registry hash table. It matches a registry
47 * uid object with the triplet <session_id/abi/uid>.
49 static int ht_match_reg_uid(struct cds_lfht_node
*node
, const void *_key
)
51 struct buffer_reg_uid
*reg
;
52 const struct buffer_reg_uid
*key
;
57 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
.node
);
61 if (key
->session_id
!= reg
->session_id
||
62 key
->bits_per_long
!= reg
->bits_per_long
||
63 key
->uid
!= reg
->uid
) {
74 * Hash function for the per UID registry hash table. This XOR the triplet
77 static unsigned long ht_hash_reg_uid(void *_key
, unsigned long seed
)
80 struct buffer_reg_uid
*key
= _key
;
84 xored_key
= (uint64_t)(key
->session_id
^ key
->bits_per_long
^ key
->uid
);
85 return hash_key_u64(&xored_key
, seed
);
89 * Initialize global buffer per UID registry. Should only be called ONCE!.
91 void buffer_reg_init_uid_registry(void)
93 /* Should be called once. */
94 assert(!buffer_registry_uid
);
95 buffer_registry_uid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
96 assert(buffer_registry_uid
);
97 buffer_registry_uid
->match_fct
= ht_match_reg_uid
;
98 buffer_registry_uid
->hash_fct
= ht_hash_reg_uid
;
100 DBG3("Global buffer per UID registry initialized");
104 * Allocate and initialize object. Set regp with the object pointer.
106 * Return 0 on success else a negative value and regp is untouched.
108 int buffer_reg_uid_create(uint64_t session_id
, uint32_t bits_per_long
, uid_t uid
,
109 enum lttng_domain_type domain
, struct buffer_reg_uid
**regp
,
110 const char *root_shm_path
, const char *shm_path
)
113 struct buffer_reg_uid
*reg
= NULL
;
117 reg
= zmalloc(sizeof(*reg
));
119 PERROR("zmalloc buffer registry uid");
124 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
125 if (!reg
->registry
) {
126 PERROR("zmalloc buffer registry uid session");
131 reg
->session_id
= session_id
;
132 reg
->bits_per_long
= bits_per_long
;
134 reg
->domain
= domain
;
136 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
137 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
138 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
139 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
140 DBG3("shm path '%s' is assigned to uid buffer registry for session id %" PRIu64
,
141 reg
->shm_path
, session_id
);
143 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
144 if (!reg
->registry
->channels
) {
149 cds_lfht_node_init(®
->node
.node
);
152 DBG3("Buffer registry per UID created id: %" PRIu64
", ABI: %u, uid: %d, domain: %d",
153 session_id
, bits_per_long
, uid
, domain
);
165 * Add a buffer registry per UID object to the global registry.
167 void buffer_reg_uid_add(struct buffer_reg_uid
*reg
)
169 struct cds_lfht_node
*nodep
;
170 struct lttng_ht
*ht
= buffer_registry_uid
;
174 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64
,
178 nodep
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(reg
, lttng_ht_seed
),
179 ht
->match_fct
, reg
, ®
->node
.node
);
180 assert(nodep
== ®
->node
.node
);
185 * Find a buffer registry per UID object with given params. RCU read side lock
186 * MUST be acquired before calling this and hold on to protect the object.
188 * Return the object pointer or NULL on error.
190 struct buffer_reg_uid
*buffer_reg_uid_find(uint64_t session_id
,
191 uint32_t bits_per_long
, uid_t uid
)
193 struct lttng_ht_node_u64
*node
;
194 struct lttng_ht_iter iter
;
195 struct buffer_reg_uid
*reg
= NULL
, key
;
196 struct lttng_ht
*ht
= buffer_registry_uid
;
198 /* Setup key we are looking for. */
199 key
.session_id
= session_id
;
200 key
.bits_per_long
= bits_per_long
;
203 DBG3("Buffer registry per UID find id: %" PRIu64
", ABI: %u, uid: %d",
204 session_id
, bits_per_long
, uid
);
206 /* Custom lookup function since it's a different key. */
207 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(&key
, lttng_ht_seed
), ht
->match_fct
,
209 node
= lttng_ht_iter_get_node_u64(&iter
);
213 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
);
220 * Initialize global buffer per PID registry. Should only be called ONCE!.
222 void buffer_reg_init_pid_registry(void)
224 /* Should be called once. */
225 assert(!buffer_registry_pid
);
226 buffer_registry_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
227 assert(buffer_registry_pid
);
229 DBG3("Global buffer per PID registry initialized");
233 * Allocate and initialize object. Set regp with the object pointer.
235 * Return 0 on success else a negative value and regp is untouched.
237 int buffer_reg_pid_create(uint64_t session_id
, struct buffer_reg_pid
**regp
,
238 const char *root_shm_path
, const char *shm_path
)
241 struct buffer_reg_pid
*reg
= NULL
;
245 reg
= zmalloc(sizeof(*reg
));
247 PERROR("zmalloc buffer registry pid");
252 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
253 if (!reg
->registry
) {
254 PERROR("zmalloc buffer registry pid session");
259 /* A cast is done here so we can use the session ID as a u64 ht node. */
260 reg
->session_id
= session_id
;
262 strncpy(reg
->root_shm_path
, root_shm_path
, sizeof(reg
->root_shm_path
));
263 reg
->root_shm_path
[sizeof(reg
->root_shm_path
) - 1] = '\0';
264 strncpy(reg
->shm_path
, shm_path
, sizeof(reg
->shm_path
));
265 reg
->shm_path
[sizeof(reg
->shm_path
) - 1] = '\0';
266 DBG3("shm path '%s' is assigned to pid buffer registry for session id %" PRIu64
,
267 reg
->shm_path
, session_id
);
269 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
270 if (!reg
->registry
->channels
) {
275 lttng_ht_node_init_u64(®
->node
, reg
->session_id
);
278 DBG3("Buffer registry per PID created with session id: %" PRIu64
,
291 * Add a buffer registry per PID object to the global registry.
293 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
297 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64
,
301 lttng_ht_add_unique_u64(buffer_registry_pid
, ®
->node
);
306 * Find a buffer registry per PID object with given params. RCU read side lock
307 * MUST be acquired before calling this and hold on to protect the object.
309 * Return the object pointer or NULL on error.
311 struct buffer_reg_pid
*buffer_reg_pid_find(uint64_t session_id
)
313 struct lttng_ht_node_u64
*node
;
314 struct lttng_ht_iter iter
;
315 struct buffer_reg_pid
*reg
= NULL
;
316 struct lttng_ht
*ht
= buffer_registry_pid
;
318 DBG3("Buffer registry per PID find id: %" PRIu64
, session_id
);
320 lttng_ht_lookup(ht
, &session_id
, &iter
);
321 node
= lttng_ht_iter_get_node_u64(&iter
);
325 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
332 * Allocate and initialize a buffer registry channel with the given key. Set
333 * regp with the object pointer.
335 * Return 0 on success or else a negative value keeping regp untouched.
337 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
339 struct buffer_reg_channel
*reg
;
343 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
345 reg
= zmalloc(sizeof(*reg
));
347 PERROR("zmalloc buffer registry channel");
352 CDS_INIT_LIST_HEAD(®
->streams
);
353 pthread_mutex_init(®
->stream_list_lock
, NULL
);
355 lttng_ht_node_init_u64(®
->node
, key
);
362 * Allocate and initialize a buffer registry stream. Set regp with the object
365 * Return 0 on success or else a negative value keeping regp untouched.
367 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
369 struct buffer_reg_stream
*reg
;
373 DBG3("Buffer registry creating stream");
375 reg
= zmalloc(sizeof(*reg
));
377 PERROR("zmalloc buffer registry stream");
387 * Add stream to the list in the channel.
389 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
390 struct buffer_reg_channel
*channel
)
395 pthread_mutex_lock(&channel
->stream_list_lock
);
396 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
397 channel
->stream_count
++;
398 pthread_mutex_unlock(&channel
->stream_list_lock
);
402 * Add a buffer registry channel object to the given session.
404 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
405 struct buffer_reg_channel
*channel
)
411 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
416 * Find a buffer registry channel object with the given key. RCU read side lock
417 * MUST be acquired and hold on until the object reference is not needed
420 * Return the object pointer or NULL on error.
422 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
423 struct buffer_reg_uid
*reg
)
425 struct lttng_ht_node_u64
*node
;
426 struct lttng_ht_iter iter
;
427 struct buffer_reg_channel
*chan
= NULL
;
432 switch (reg
->domain
) {
433 case LTTNG_DOMAIN_UST
:
434 ht
= reg
->registry
->channels
;
441 lttng_ht_lookup(ht
, &key
, &iter
);
442 node
= lttng_ht_iter_get_node_u64(&iter
);
446 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
453 * Destroy a buffer registry stream with the given domain.
455 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
456 enum lttng_domain_type domain
)
462 DBG3("Buffer registry stream destroy with handle %d",
463 regp
->obj
.ust
->handle
);
466 case LTTNG_DOMAIN_UST
:
470 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
471 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
472 ERR("Buffer reg stream release obj handle %d failed with ret %d",
473 regp
->obj
.ust
->handle
, ret
);
476 lttng_fd_put(LTTNG_FD_APPS
, 2);
488 * Remove buffer registry channel object from the session hash table. RCU read
489 * side lock MUST be acquired before calling this.
491 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
492 struct buffer_reg_channel
*regp
)
495 struct lttng_ht_iter iter
;
500 iter
.iter
.node
= ®p
->node
.node
;
501 ret
= lttng_ht_del(session
->channels
, &iter
);
506 * Destroy a buffer registry channel with the given domain.
508 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
509 enum lttng_domain_type domain
)
515 DBG3("Buffer registry channel destroy with key %" PRIu32
, regp
->key
);
518 case LTTNG_DOMAIN_UST
:
521 struct buffer_reg_stream
*sreg
, *stmp
;
523 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
524 cds_list_del(&sreg
->lnode
);
525 regp
->stream_count
--;
526 buffer_reg_stream_destroy(sreg
, domain
);
530 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
531 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
532 ERR("Buffer reg channel release obj handle %d failed with ret %d",
533 regp
->obj
.ust
->handle
, ret
);
537 lttng_fd_put(LTTNG_FD_APPS
, 1);
549 * Destroy a buffer registry session with the given domain.
551 * Should *NOT* be called with RCU read-side lock held.
553 static void buffer_reg_session_destroy(struct buffer_reg_session
*regp
,
554 enum lttng_domain_type domain
)
557 struct lttng_ht_iter iter
;
558 struct buffer_reg_channel
*reg_chan
;
560 DBG3("Buffer registry session destroy");
562 /* Destroy all channels. */
564 cds_lfht_for_each_entry(regp
->channels
->ht
, &iter
.iter
, reg_chan
,
566 ret
= lttng_ht_del(regp
->channels
, &iter
);
568 buffer_reg_channel_destroy(reg_chan
, domain
);
572 ht_cleanup_push(regp
->channels
);
575 case LTTNG_DOMAIN_UST
:
576 ust_registry_session_destroy(regp
->reg
.ust
);
588 * Remove buffer registry UID object from the global hash table.
590 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
593 struct lttng_ht_iter iter
;
598 iter
.iter
.node
= ®p
->node
.node
;
599 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
604 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
606 struct lttng_ht_node_u64
*node
=
607 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
608 struct buffer_reg_uid
*reg
=
609 caa_container_of(node
, struct buffer_reg_uid
, node
);
611 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
615 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
617 struct lttng_ht_node_u64
*node
=
618 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
619 struct buffer_reg_pid
*reg
=
620 caa_container_of(node
, struct buffer_reg_pid
, node
);
622 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
627 * Destroy buffer registry per UID. The given pointer is NOT removed from any
628 * list or hash table. Use buffer_reg_pid_remove() before calling this function
629 * for the case that the object is in the global hash table.
631 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
632 struct consumer_output
*consumer
)
634 struct consumer_socket
*socket
;
640 DBG3("Buffer registry per UID destroy with id: %" PRIu64
", ABI: %u, uid: %d",
641 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
648 /* Get the right socket from the consumer object. */
649 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
655 switch (regp
->domain
) {
656 case LTTNG_DOMAIN_UST
:
657 if (regp
->registry
->reg
.ust
->metadata_key
) {
658 /* Return value does not matter. This call will print errors. */
659 (void) consumer_close_metadata(socket
,
660 regp
->registry
->reg
.ust
->metadata_key
);
672 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
676 * Remove buffer registry UID object from the global hash table. RCU read side
677 * lock MUST be acquired before calling this.
679 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
682 struct lttng_ht_iter iter
;
686 iter
.iter
.node
= ®p
->node
.node
;
687 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
692 * Destroy buffer registry per PID. The pointer is NOT removed from the global
693 * hash table. Call buffer_reg_pid_remove() before that if the object was
694 * previously added to the global hash table.
696 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
702 DBG3("Buffer registry per PID destroy with id: %" PRIu64
,
705 /* This registry is only used by UST. */
706 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
710 * Destroy per PID and UID registry hash table.
712 * Should *NOT* be called with RCU read-side lock held.
714 void buffer_reg_destroy_registries(void)
716 DBG3("Buffer registry destroy all registry");
717 ht_cleanup_push(buffer_registry_uid
);
718 ht_cleanup_push(buffer_registry_pid
);
This page took 0.049518 seconds and 5 git commands to generate.