5751b8d587f8a1a12a64a1b2fc207aaba22b0534
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"
30 * Set in main.c during initialization process of the daemon. This contains
31 * buffer_reg_uid object which are global registry for per UID buffer. Object
32 * are indexed by session id and matched by the triplet
33 * <session_id/bits_per_long/uid>.
35 static struct lttng_ht
*buffer_registry_uid
;
38 * Initialized at the daemon start. This contains buffer_reg_pid object and
39 * indexed by session id.
41 static struct lttng_ht
*buffer_registry_pid
;
44 * Match function for the per UID registry hash table. It matches a registry
45 * uid object with the triplet <session_id/abi/uid>.
47 static int ht_match_reg_uid(struct cds_lfht_node
*node
, const void *_key
)
49 struct buffer_reg_uid
*reg
;
50 const struct buffer_reg_uid
*key
;
55 reg
= caa_container_of(node
, struct buffer_reg_uid
, node
.node
);
59 if (key
->session_id
!= reg
->session_id
||
60 key
->bits_per_long
!= reg
->bits_per_long
||
61 key
->uid
!= reg
->uid
) {
72 * Hash function for the per UID registry hash table. This XOR the triplet
75 static unsigned long ht_hash_reg_uid(void *_key
, unsigned long seed
)
78 struct buffer_reg_uid
*key
= _key
;
82 xored_key
= (uint64_t)(key
->session_id
^ key
->bits_per_long
^ key
->uid
);
83 return hash_key_u64(&xored_key
, seed
);
87 * Initialize global buffer per UID registry. Should only be called ONCE!.
89 void buffer_reg_init_uid_registry(void)
91 /* Should be called once. */
92 assert(!buffer_registry_uid
);
93 buffer_registry_uid
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
94 assert(buffer_registry_uid
);
95 buffer_registry_uid
->match_fct
= ht_match_reg_uid
;
96 buffer_registry_uid
->hash_fct
= ht_hash_reg_uid
;
98 DBG3("Global buffer per UID registry initialized");
102 * Allocate and initialize object. Set regp with the object pointer.
104 * Return 0 on success else a negative value and regp is untouched.
106 int buffer_reg_uid_create(int session_id
, uint32_t bits_per_long
, uid_t uid
,
107 enum lttng_domain_type domain
, struct buffer_reg_uid
**regp
)
110 struct buffer_reg_uid
*reg
= NULL
;
114 reg
= zmalloc(sizeof(*reg
));
116 PERROR("zmalloc buffer registry uid");
121 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
123 PERROR("zmalloc buffer registry uid session");
128 reg
->session_id
= session_id
;
129 reg
->bits_per_long
= bits_per_long
;
131 reg
->domain
= domain
;
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: %d, 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: %d",
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(int 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: %d, 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_ULONG
);
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(int session_id
, struct buffer_reg_pid
**regp
)
230 struct buffer_reg_pid
*reg
= NULL
;
234 reg
= zmalloc(sizeof(*reg
));
236 PERROR("zmalloc buffer registry pid");
241 reg
->registry
= zmalloc(sizeof(struct buffer_reg_session
));
243 PERROR("zmalloc buffer registry pid session");
248 /* A cast is done here so we can use the session ID as a u64 ht node. */
249 reg
->session_id
= session_id
;
251 reg
->registry
->channels
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
252 if (!reg
->registry
->channels
) {
257 lttng_ht_node_init_ulong(®
->node
, reg
->session_id
);
260 DBG3("Buffer registry per PID created with session id: %d", session_id
);
272 * Add a buffer registry per PID object to the global registry.
274 void buffer_reg_pid_add(struct buffer_reg_pid
*reg
)
278 DBG3("Buffer registry per PID adding to global registry with id: %d",
282 lttng_ht_add_unique_ulong(buffer_registry_pid
, ®
->node
);
287 * Find a buffer registry per PID object with given params. RCU read side lock
288 * MUST be acquired before calling this and hold on to protect the object.
290 * Return the object pointer or NULL on error.
292 struct buffer_reg_pid
*buffer_reg_pid_find(int session_id
)
294 struct lttng_ht_node_ulong
*node
;
295 struct lttng_ht_iter iter
;
296 struct buffer_reg_pid
*reg
= NULL
;
297 struct lttng_ht
*ht
= buffer_registry_pid
;
299 DBG3("Buffer registry per PID find id: %d", session_id
);
301 lttng_ht_lookup(ht
, (void *)((unsigned long) session_id
), &iter
);
302 node
= lttng_ht_iter_get_node_ulong(&iter
);
306 reg
= caa_container_of(node
, struct buffer_reg_pid
, node
);
313 * Allocate and initialize a buffer registry channel with the given key. Set
314 * regp with the object pointer.
316 * Return 0 on success or else a negative value keeping regp untouched.
318 int buffer_reg_channel_create(uint64_t key
, struct buffer_reg_channel
**regp
)
320 struct buffer_reg_channel
*reg
;
324 DBG3("Buffer registry channel create with key: %" PRIu64
, key
);
326 reg
= zmalloc(sizeof(*reg
));
328 PERROR("zmalloc buffer registry channel");
333 CDS_INIT_LIST_HEAD(®
->streams
);
334 pthread_mutex_init(®
->stream_list_lock
, NULL
);
336 lttng_ht_node_init_u64(®
->node
, key
);
343 * Allocate and initialize a buffer registry stream. Set regp with the object
346 * Return 0 on success or else a negative value keeping regp untouched.
348 int buffer_reg_stream_create(struct buffer_reg_stream
**regp
)
350 struct buffer_reg_stream
*reg
;
354 DBG3("Buffer registry creating stream");
356 reg
= zmalloc(sizeof(*reg
));
358 PERROR("zmalloc buffer registry stream");
368 * Add stream to the list in the channel.
370 void buffer_reg_stream_add(struct buffer_reg_stream
*stream
,
371 struct buffer_reg_channel
*channel
)
376 pthread_mutex_lock(&channel
->stream_list_lock
);
377 cds_list_add_tail(&stream
->lnode
, &channel
->streams
);
378 pthread_mutex_unlock(&channel
->stream_list_lock
);
382 * Add a buffer registry channel object to the given session.
384 void buffer_reg_channel_add(struct buffer_reg_session
*session
,
385 struct buffer_reg_channel
*channel
)
391 lttng_ht_add_unique_u64(session
->channels
, &channel
->node
);
396 * Find a buffer registry channel object with the given key. RCU read side lock
397 * MUST be acquired and hold on until the object reference is not needed
400 * Return the object pointer or NULL on error.
402 struct buffer_reg_channel
*buffer_reg_channel_find(uint64_t key
,
403 struct buffer_reg_uid
*reg
)
405 struct lttng_ht_node_u64
*node
;
406 struct lttng_ht_iter iter
;
407 struct buffer_reg_channel
*chan
= NULL
;
412 switch (reg
->domain
) {
413 case LTTNG_DOMAIN_UST
:
414 ht
= reg
->registry
->channels
;
421 lttng_ht_lookup(ht
, &key
, &iter
);
422 node
= lttng_ht_iter_get_node_u64(&iter
);
426 chan
= caa_container_of(node
, struct buffer_reg_channel
, node
);
433 * Destroy a buffer registry stream with the given domain.
435 void buffer_reg_stream_destroy(struct buffer_reg_stream
*regp
,
436 enum lttng_domain_type domain
)
442 DBG3("Buffer registry stream destroy with handle %d",
443 regp
->obj
.ust
->handle
);
446 case LTTNG_DOMAIN_UST
:
450 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
451 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
452 ERR("Buffer reg stream release obj handle %d failed with ret %d",
453 regp
->obj
.ust
->handle
, ret
);
456 lttng_fd_put(LTTNG_FD_APPS
, 2);
468 * Remove buffer registry channel object from the session hash table. RCU read
469 * side lock MUST be acquired before calling this.
471 void buffer_reg_channel_remove(struct buffer_reg_session
*session
,
472 struct buffer_reg_channel
*regp
)
475 struct lttng_ht_iter iter
;
480 iter
.iter
.node
= ®p
->node
.node
;
481 ret
= lttng_ht_del(session
->channels
, &iter
);
486 * Destroy a buffer registry channel with the given domain.
488 void buffer_reg_channel_destroy(struct buffer_reg_channel
*regp
,
489 enum lttng_domain_type domain
)
495 DBG3("Buffer registry channel destroy with key %" PRIu32
" and handle %d",
496 regp
->key
, regp
->obj
.ust
->handle
);
499 case LTTNG_DOMAIN_UST
:
502 struct buffer_reg_stream
*sreg
, *stmp
;
504 cds_list_for_each_entry_safe(sreg
, stmp
, ®p
->streams
, lnode
) {
505 cds_list_del(&sreg
->lnode
);
506 buffer_reg_stream_destroy(sreg
, domain
);
510 ret
= ust_ctl_release_object(-1, regp
->obj
.ust
);
511 if (ret
< 0 && ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
512 ERR("Buffer reg channel release obj handle %d failed with ret %d",
513 regp
->obj
.ust
->handle
, ret
);
517 lttng_fd_put(LTTNG_FD_APPS
, 1);
529 * Destroy a buffer registry session with the given domain.
531 void buffer_reg_session_destroy(struct buffer_reg_session
*regp
,
532 enum lttng_domain_type domain
)
535 struct lttng_ht_iter iter
;
536 struct buffer_reg_channel
*reg_chan
;
538 DBG3("Buffer registry session destroy");
540 /* Destroy all channels. */
542 cds_lfht_for_each_entry(regp
->channels
->ht
, &iter
.iter
, reg_chan
,
544 ret
= lttng_ht_del(regp
->channels
, &iter
);
546 buffer_reg_channel_destroy(reg_chan
, domain
);
548 lttng_ht_destroy(regp
->channels
);
552 case LTTNG_DOMAIN_UST
:
553 ust_registry_session_destroy(regp
->reg
.ust
);
565 * Remove buffer registry UID object from the global hash table. RCU read side
566 * lock MUST be acquired before calling this.
568 void buffer_reg_uid_remove(struct buffer_reg_uid
*regp
)
571 struct lttng_ht_iter iter
;
575 iter
.iter
.node
= ®p
->node
.node
;
576 ret
= lttng_ht_del(buffer_registry_uid
, &iter
);
580 static void rcu_free_buffer_reg_uid(struct rcu_head
*head
)
582 struct lttng_ht_node_u64
*node
=
583 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
584 struct buffer_reg_uid
*reg
=
585 caa_container_of(node
, struct buffer_reg_uid
, node
);
587 buffer_reg_session_destroy(reg
->registry
, reg
->domain
);
591 static void rcu_free_buffer_reg_pid(struct rcu_head
*head
)
593 struct lttng_ht_node_ulong
*node
=
594 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
595 struct buffer_reg_pid
*reg
=
596 caa_container_of(node
, struct buffer_reg_pid
, node
);
598 buffer_reg_session_destroy(reg
->registry
, LTTNG_DOMAIN_UST
);
603 * Destroy buffer registry per UID. The given pointer is NOT removed from any
604 * list or hash table. Use buffer_reg_pid_remove() before calling this function
605 * for the case that the object is in the global hash table.
607 void buffer_reg_uid_destroy(struct buffer_reg_uid
*regp
,
608 struct consumer_output
*consumer
)
610 struct consumer_socket
*socket
;
616 DBG3("Buffer registry per UID destroy with id: %d, ABI: %u, uid: %d",
617 regp
->session_id
, regp
->bits_per_long
, regp
->uid
);
623 /* Get the right socket from the consumer object. */
624 socket
= consumer_find_socket_by_bitness(regp
->bits_per_long
,
630 switch (regp
->domain
) {
631 case LTTNG_DOMAIN_UST
:
632 if (regp
->registry
->reg
.ust
->metadata_key
) {
633 /* Return value does not matter. This call will print errors. */
634 (void) consumer_close_metadata(socket
,
635 regp
->registry
->reg
.ust
->metadata_key
);
644 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_uid
);
648 * Remove buffer registry UID object from the global hash table. RCU read side
649 * lock MUST be acquired before calling this.
651 void buffer_reg_pid_remove(struct buffer_reg_pid
*regp
)
654 struct lttng_ht_iter iter
;
658 iter
.iter
.node
= ®p
->node
.node
;
659 ret
= lttng_ht_del(buffer_registry_pid
, &iter
);
664 * Destroy buffer registry per PID. The pointer is NOT removed from the global
665 * hash table. Call buffer_reg_pid_remove() before that if the object was
666 * previously added to the global hash table.
668 void buffer_reg_pid_destroy(struct buffer_reg_pid
*regp
)
674 DBG3("Buffer registry per PID destroy with id: %d", regp
->session_id
);
676 /* This registry is only used by UST. */
677 call_rcu(®p
->node
.head
, rcu_free_buffer_reg_pid
);
681 * Destroy per PID and UID registry hash table.
683 void buffer_reg_destroy_registries(void)
685 DBG3("Buffer registry destroy all registry");
686 lttng_ht_destroy(buffer_registry_uid
);
687 lttng_ht_destroy(buffer_registry_pid
);
This page took 0.045637 seconds and 5 git commands to generate.