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.
21 #include <common/common.h>
22 #include <common/hashtable/utils.h>
24 #include "buffer-registry.h"
26 #include "ust-consumer.h"
31 * Set in main.c during initialization process of the daemon. This contains
32 * buffer_reg_uid object which are global registry for per UID buffer. Object
33 * are indexed by session id and matched by the triplet
34 * <session_id/bits_per_long/uid>.
36 static struct lttng_ht
*buffer_registry_uid
;
39 * Initialized at the daemon start. This contains buffer_reg_pid object and
40 * indexed by session id.
42 static struct lttng_ht
*buffer_registry_pid
;
45 * Match function for the per UID registry hash table. It matches a registry
46 * uid object with the triplet <session_id/abi/uid>.
48 static int ht_match_reg_uid(struct cds_lfht_node
*node
, const void *_key
)
50 struct buffer_reg_uid
*reg
;
51 const struct buffer_reg_uid
*key
;
56 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
.node
);
60 if (key
->session_id
!= reg
->session_id
||
61 key
->bits_per_long
!= reg
->bits_per_long
||
62 key
->uid
!= reg
->uid
) {
73 * Hash function for the per UID registry hash table. This XOR the triplet
76 static unsigned long ht_hash_reg_uid(void *_key
, unsigned long seed
)
79 struct buffer_reg_uid
*key
= _key
;
83 xored_key
= (uint64_t)(key
->session_id
^ key
->bits_per_long
^ key
->uid
);
84 return hash_key_u64(&xored_key
, seed
);
88 * Initialize global buffer per UID registry. Should only be called ONCE!.
90 void buffer_reg_init_uid_registry(void)
92 /* Should be called once. */
93 assert(!buffer_registry_uid
);
94 buffer_registry_uid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
95 assert(buffer_registry_uid
);
96 buffer_registry_uid
->match_fct
= ht_match_reg_uid
;
97 buffer_registry_uid
->hash_fct
= ht_hash_reg_uid
;
99 DBG3("Global buffer per UID registry initialized");
103 * Allocate and initialize object. Set regp with the object pointer.
105 * Return 0 on success else a negative value and regp is untouched.
107 int buffer_reg_uid_create(uint64_t session_id
, uint32_t bits_per_long
, uid_t uid
,
108 enum lttng_domain_type domain
, struct buffer_reg_uid
**regp
)
111 struct buffer_reg_uid
*reg
= NULL
;
115 reg
= zmalloc(sizeof(*reg
));
117 PERROR("zmalloc buffer registry uid");
122 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
123 if (!reg
->registry
) {
124 PERROR("zmalloc buffer registry uid session");
129 reg
->session_id
= session_id
;
130 reg
->bits_per_long
= bits_per_long
;
132 reg
->domain
= domain
;
134 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
135 if (!reg
->registry
->channels
) {
140 cds_lfht_node_init(®
->node
.node
);
143 DBG3("Buffer registry per UID created id: %" PRIu64
", ABI: %u, uid: %d, domain: %d",
144 session_id
, bits_per_long
, uid
, domain
);
156 * Add a buffer registry per UID object to the global registry.
158 void buffer_reg_uid_add(struct buffer_reg_uid
*reg
)
160 struct cds_lfht_node
*nodep
;
161 struct lttng_ht
*ht
= buffer_registry_uid
;
165 DBG3("Buffer registry per UID adding to global registry with id: %" PRIu64
,
169 nodep
= cds_lfht_add_unique(ht
->ht
, ht
->hash_fct(reg
, lttng_ht_seed
),
170 ht
->match_fct
, reg
, ®
->node
.node
);
171 assert(nodep
== ®
->node
.node
);
176 * Find a buffer registry per UID object with given params. RCU read side lock
177 * MUST be acquired before calling this and hold on to protect the object.
179 * Return the object pointer or NULL on error.
181 struct buffer_reg_uid
*buffer_reg_uid_find(uint64_t session_id
,
182 uint32_t bits_per_long
, uid_t uid
)
184 struct lttng_ht_node_u64
*node
;
185 struct lttng_ht_iter iter
;
186 struct buffer_reg_uid
*reg
= NULL
, key
;
187 struct lttng_ht
*ht
= buffer_registry_uid
;
189 /* Setup key we are looking for. */
190 key
.session_id
= session_id
;
191 key
.bits_per_long
= bits_per_long
;
194 DBG3("Buffer registry per UID find id: %" PRIu64
", ABI: %u, uid: %d",
195 session_id
, bits_per_long
, uid
);
197 /* Custom lookup function since it's a different key. */
198 cds_lfht_lookup(ht
->ht
, ht
->hash_fct(&key
, lttng_ht_seed
), ht
->match_fct
,
200 node
= lttng_ht_iter_get_node_u64(&iter
);
204 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
);
211 * Initialize global buffer per PID registry. Should only be called ONCE!.
213 void buffer_reg_init_pid_registry(void)
215 /* Should be called once. */
216 assert(!buffer_registry_pid
);
217 buffer_registry_pid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
218 assert(buffer_registry_pid
);
220 DBG3("Global buffer per PID registry initialized");
224 * Allocate and initialize object. Set regp with the object pointer.
226 * Return 0 on success else a negative value and regp is untouched.
228 int buffer_reg_pid_create(uint64_t session_id
, struct buffer_reg_pid
**regp
)
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 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
253 if (!reg
->registry
->channels
) {
258 lttng_ht_node_init_u64(®
->node
, reg
->session_id
);
261 DBG3("Buffer registry per PID created with session id: %" PRIu64
,
274 * Add a buffer registry per PID object to the global registry.
276 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
280 DBG3("Buffer registry per PID adding to global registry with id: %" PRIu64
,
284 lttng_ht_add_unique_u64(buffer_registry_pid
, ®
->node
);
289 * Find a buffer registry per PID object with given params. RCU read side lock
290 * MUST be acquired before calling this and hold on to protect the object.
292 * Return the object pointer or NULL on error.
294 struct buffer_reg_pid
*buffer_reg_pid_find(uint64_t session_id
)
296 struct lttng_ht_node_u64
*node
;
297 struct lttng_ht_iter iter
;
298 struct buffer_reg_pid
*reg
= NULL
;
299 struct lttng_ht
*ht
= buffer_registry_pid
;
301 DBG3("Buffer registry per PID find id: %" PRIu64
, session_id
);
303 lttng_ht_lookup(ht
, &session_id
, &iter
);
304 node
= lttng_ht_iter_get_node_u64(&iter
);
308 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
315 * Allocate and initialize a buffer registry channel with the given key. Set
316 * regp with the object pointer.
318 * Return 0 on success or else a negative value keeping regp untouched.
320 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
322 struct buffer_reg_channel
*reg
;
326 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
328 reg
= zmalloc(sizeof(*reg
));
330 PERROR("zmalloc buffer registry channel");
335 CDS_INIT_LIST_HEAD(®
->streams
);
336 pthread_mutex_init(®
->stream_list_lock
, NULL
);
338 lttng_ht_node_init_u64(®
->node
, key
);
345 * Allocate and initialize a buffer registry stream. Set regp with the object
348 * Return 0 on success or else a negative value keeping regp untouched.
350 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
352 struct buffer_reg_stream
*reg
;
356 DBG3("Buffer registry creating stream");
358 reg
= zmalloc(sizeof(*reg
));
360 PERROR("zmalloc buffer registry stream");
370 * Add stream to the list in the channel.
372 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
373 struct buffer_reg_channel
*channel
)
378 pthread_mutex_lock(&channel
->stream_list_lock
);
379 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
380 pthread_mutex_unlock(&channel
->stream_list_lock
);
384 * Add a buffer registry channel object to the given session.
386 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
387 struct buffer_reg_channel
*channel
)
393 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
398 * Find a buffer registry channel object with the given key. RCU read side lock
399 * MUST be acquired and hold on until the object reference is not needed
402 * Return the object pointer or NULL on error.
404 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
405 struct buffer_reg_uid
*reg
)
407 struct lttng_ht_node_u64
*node
;
408 struct lttng_ht_iter iter
;
409 struct buffer_reg_channel
*chan
= NULL
;
414 switch (reg
->domain
) {
415 case LTTNG_DOMAIN_UST
:
416 ht
= reg
->registry
->channels
;
423 lttng_ht_lookup(ht
, &key
, &iter
);
424 node
= lttng_ht_iter_get_node_u64(&iter
);
428 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
435 * Destroy a buffer registry stream with the given domain.
437 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
438 enum lttng_domain_type domain
)
444 DBG3("Buffer registry stream destroy with handle %d",
445 regp
->obj
.ust
->handle
);
448 case LTTNG_DOMAIN_UST
:
452 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
453 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
454 ERR("Buffer reg stream release obj handle %d failed with ret %d",
455 regp
->obj
.ust
->handle
, ret
);
458 lttng_fd_put(LTTNG_FD_APPS
, 2);
470 * Remove buffer registry channel object from the session hash table. RCU read
471 * side lock MUST be acquired before calling this.
473 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
474 struct buffer_reg_channel
*regp
)
477 struct lttng_ht_iter iter
;
482 iter
.iter
.node
= ®p
->node
.node
;
483 ret
= lttng_ht_del(session
->channels
, &iter
);
488 * Destroy a buffer registry channel with the given domain.
490 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
491 enum lttng_domain_type domain
)
497 DBG3("Buffer registry channel destroy with key %" PRIu32
, regp
->key
);
500 case LTTNG_DOMAIN_UST
:
503 struct buffer_reg_stream
*sreg
, *stmp
;
505 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
506 cds_list_del(&sreg
->lnode
);
507 buffer_reg_stream_destroy(sreg
, domain
);
511 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
512 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
513 ERR("Buffer reg channel release obj handle %d failed with ret %d",
514 regp
->obj
.ust
->handle
, ret
);
518 lttng_fd_put(LTTNG_FD_APPS
, 1);
530 * Destroy a buffer registry session with the given domain.
532 * Should *NOT* be called with RCU read-side lock held.
534 static void buffer_reg_session_destroy(struct buffer_reg_session
*regp
,
535 enum lttng_domain_type domain
)
538 struct lttng_ht_iter iter
;
539 struct buffer_reg_channel
*reg_chan
;
541 DBG3("Buffer registry session destroy");
543 /* Destroy all channels. */
545 cds_lfht_for_each_entry(regp
->channels
->ht
, &iter
.iter
, reg_chan
,
547 ret
= lttng_ht_del(regp
->channels
, &iter
);
549 buffer_reg_channel_destroy(reg_chan
, domain
);
553 ht_cleanup_push(regp
->channels
);
556 case LTTNG_DOMAIN_UST
:
557 ust_registry_session_destroy(regp
->reg
.ust
);
569 * Remove buffer registry UID object from the global hash table.
571 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
574 struct lttng_ht_iter iter
;
579 iter
.iter
.node
= ®p
->node
.node
;
580 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
585 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
587 struct lttng_ht_node_u64
*node
=
588 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
589 struct buffer_reg_uid
*reg
=
590 caa_container_of(node
, struct buffer_reg_uid
, node
);
592 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
596 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
598 struct lttng_ht_node_u64
*node
=
599 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
600 struct buffer_reg_pid
*reg
=
601 caa_container_of(node
, struct buffer_reg_pid
, node
);
603 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
608 * Destroy buffer registry per UID. The given pointer is NOT removed from any
609 * list or hash table. Use buffer_reg_pid_remove() before calling this function
610 * for the case that the object is in the global hash table.
612 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
613 struct consumer_output
*consumer
)
615 struct consumer_socket
*socket
;
621 DBG3("Buffer registry per UID destroy with id: %" PRIu64
", ABI: %u, uid: %d",
622 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
629 /* Get the right socket from the consumer object. */
630 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
636 switch (regp
->domain
) {
637 case LTTNG_DOMAIN_UST
:
638 if (regp
->registry
->reg
.ust
->metadata_key
) {
639 /* Return value does not matter. This call will print errors. */
640 (void) consumer_close_metadata(socket
,
641 regp
->registry
->reg
.ust
->metadata_key
);
653 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
657 * Remove buffer registry UID object from the global hash table. RCU read side
658 * lock MUST be acquired before calling this.
660 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
663 struct lttng_ht_iter iter
;
667 iter
.iter
.node
= ®p
->node
.node
;
668 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
673 * Destroy buffer registry per PID. The pointer is NOT removed from the global
674 * hash table. Call buffer_reg_pid_remove() before that if the object was
675 * previously added to the global hash table.
677 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
683 DBG3("Buffer registry per PID destroy with id: %" PRIu64
,
686 /* This registry is only used by UST. */
687 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
691 * Destroy per PID and UID registry hash table.
693 * Should *NOT* be called with RCU read-side lock held.
695 void buffer_reg_destroy_registries(void)
697 DBG3("Buffer registry destroy all registry");
698 ht_cleanup_push(buffer_registry_uid
);
699 ht_cleanup_push(buffer_registry_pid
);
This page took 0.045088 seconds and 4 git commands to generate.