2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <lttng/ust-ctl.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
32 #include <urcu/list.h>
35 #include <common/common.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/relayd/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/consumer-metadata-cache.h>
40 #include <common/consumer-timer.h>
41 #include <common/utils.h>
43 #include "ust-consumer.h"
45 extern struct lttng_consumer_global_data consumer_data
;
46 extern int consumer_poll_timeout
;
47 extern volatile int consumer_quit
;
50 * Free channel object and all streams associated with it. This MUST be used
51 * only and only if the channel has _NEVER_ been added to the global channel
54 static void destroy_channel(struct lttng_consumer_channel
*channel
)
56 struct lttng_consumer_stream
*stream
, *stmp
;
60 DBG("UST consumer cleaning stream list");
62 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
64 cds_list_del(&stream
->send_node
);
65 ustctl_destroy_stream(stream
->ustream
);
70 * If a channel is available meaning that was created before the streams
74 lttng_ustconsumer_del_channel(channel
);
80 * Add channel to internal consumer state.
82 * Returns 0 on success or else a negative value.
84 static int add_channel(struct lttng_consumer_channel
*channel
,
85 struct lttng_consumer_local_data
*ctx
)
92 if (ctx
->on_recv_channel
!= NULL
) {
93 ret
= ctx
->on_recv_channel(channel
);
95 ret
= consumer_add_channel(channel
, ctx
);
97 /* Most likely an ENOMEM. */
98 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
102 ret
= consumer_add_channel(channel
, ctx
);
105 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
112 * Allocate and return a consumer channel object.
114 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
115 const char *pathname
, const char *name
, uid_t uid
, gid_t gid
,
116 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
117 uint64_t tracefile_size
, uint64_t tracefile_count
,
118 uint64_t session_id_per_pid
)
123 return consumer_allocate_channel(key
, session_id
, pathname
, name
, uid
,
124 gid
, relayd_id
, output
, tracefile_size
,
125 tracefile_count
, session_id_per_pid
);
129 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
130 * error value if applicable is set in it else it is kept untouched.
132 * Return NULL on error else the newly allocated stream object.
134 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
135 struct lttng_consumer_channel
*channel
,
136 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
139 struct lttng_consumer_stream
*stream
= NULL
;
144 stream
= consumer_allocate_stream(channel
->key
,
146 LTTNG_CONSUMER_ACTIVE_STREAM
,
155 if (stream
== NULL
) {
159 * We could not find the channel. Can happen if cpu hotplug
160 * happens while tearing down.
162 DBG3("Could not find channel");
167 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
173 stream
->chan
= channel
;
177 *_alloc_ret
= alloc_ret
;
183 * Send the given stream pointer to the corresponding thread.
185 * Returns 0 on success else a negative value.
187 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
188 struct lttng_consumer_local_data
*ctx
)
191 struct lttng_pipe
*stream_pipe
;
193 /* Get the right pipe where the stream will be sent. */
194 if (stream
->metadata_flag
) {
195 ret
= consumer_add_metadata_stream(stream
);
197 ERR("Consumer add metadata stream %" PRIu64
" failed.",
201 stream_pipe
= ctx
->consumer_metadata_pipe
;
203 ret
= consumer_add_data_stream(stream
);
205 ERR("Consumer add stream %" PRIu64
" failed.",
209 stream_pipe
= ctx
->consumer_data_pipe
;
212 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
214 ERR("Consumer write %s stream to pipe %d",
215 stream
->metadata_flag
? "metadata" : "data",
216 lttng_pipe_get_writefd(stream_pipe
));
217 if (stream
->metadata_flag
) {
218 consumer_del_stream_for_metadata(stream
);
220 consumer_del_stream_for_data(stream
);
228 * Search for a relayd object related to the stream. If found, send the stream
231 * On success, returns 0 else a negative value.
233 static int send_stream_to_relayd(struct lttng_consumer_stream
*stream
)
236 struct consumer_relayd_sock_pair
*relayd
;
240 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
241 if (relayd
!= NULL
) {
242 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
243 /* Add stream on the relayd */
244 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
245 stream
->chan
->pathname
, &stream
->relayd_stream_id
,
246 stream
->chan
->tracefile_size
,
247 stream
->chan
->tracefile_count
);
248 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
252 } else if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
253 ERR("Network sequence index %" PRIu64
" unknown. Not adding stream.",
254 stream
->net_seq_idx
);
264 * Create streams for the given channel using liblttng-ust-ctl.
266 * Return 0 on success else a negative value.
268 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
269 struct lttng_consumer_local_data
*ctx
)
272 struct ustctl_consumer_stream
*ustream
;
273 struct lttng_consumer_stream
*stream
;
279 * While a stream is available from ustctl. When NULL is returned, we've
280 * reached the end of the possible stream for the channel.
282 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
285 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
287 /* Allocate consumer stream object. */
288 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
292 stream
->ustream
= ustream
;
294 * Store it so we can save multiple function calls afterwards since
295 * this value is used heavily in the stream threads. This is UST
296 * specific so this is why it's done after allocation.
298 stream
->wait_fd
= wait_fd
;
301 * Increment channel refcount since the channel reference has now been
302 * assigned in the allocation process above.
304 uatomic_inc(&stream
->chan
->refcount
);
307 * Order is important this is why a list is used. On error, the caller
308 * should clean this list.
310 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
312 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
313 &stream
->max_sb_size
);
315 ERR("ustctl_get_max_subbuf_size failed for stream %s",
320 /* Do actions once stream has been received. */
321 if (ctx
->on_recv_stream
) {
322 ret
= ctx
->on_recv_stream(stream
);
328 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
329 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
331 /* Set next CPU stream. */
332 channel
->streams
.count
= ++cpu
;
334 /* Keep stream reference when creating metadata. */
335 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
336 channel
->metadata_stream
= stream
;
348 * Create an UST channel with the given attributes and send it to the session
349 * daemon using the ust ctl API.
351 * Return 0 on success or else a negative value.
353 static int create_ust_channel(struct ustctl_consumer_channel_attr
*attr
,
354 struct ustctl_consumer_channel
**chanp
)
357 struct ustctl_consumer_channel
*channel
;
362 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
363 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
364 "switch_timer_interval: %u, read_timer_interval: %u, "
365 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
366 attr
->num_subbuf
, attr
->switch_timer_interval
,
367 attr
->read_timer_interval
, attr
->output
, attr
->type
);
369 channel
= ustctl_create_channel(attr
);
384 * Send a single given stream to the session daemon using the sock.
386 * Return 0 on success else a negative value.
388 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
395 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
397 /* Send stream to session daemon. */
398 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
408 * Send channel to sessiond.
410 * Return 0 on success or else a negative value.
412 static int send_sessiond_channel(int sock
,
413 struct lttng_consumer_channel
*channel
,
414 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
416 int ret
, ret_code
= LTTNG_OK
;
417 struct lttng_consumer_stream
*stream
;
423 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
425 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
426 /* Try to send the stream to the relayd if one is available. */
427 ret
= send_stream_to_relayd(stream
);
430 * Flag that the relayd was the problem here probably due to a
431 * communicaton error on the socket.
436 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
440 /* Inform sessiond that we are about to send channel and streams. */
441 ret
= consumer_send_status_msg(sock
, ret_code
);
442 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
444 * Either the session daemon is not responding or the relayd died so we
450 /* Send channel to sessiond. */
451 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
456 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
461 /* The channel was sent successfully to the sessiond at this point. */
462 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
463 /* Send stream to session daemon. */
464 ret
= send_sessiond_stream(sock
, stream
);
470 /* Tell sessiond there is no more stream. */
471 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
476 DBG("UST consumer NULL stream sent to sessiond");
481 if (ret_code
!= LTTNG_OK
) {
488 * Creates a channel and streams and add the channel it to the channel internal
489 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
492 * Return 0 on success or else, a negative value is returned and the channel
493 * MUST be destroyed by consumer_del_channel().
495 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
496 struct lttng_consumer_channel
*channel
,
497 struct ustctl_consumer_channel_attr
*attr
)
506 * This value is still used by the kernel consumer since for the kernel,
507 * the stream ownership is not IN the consumer so we need to have the
508 * number of left stream that needs to be initialized so we can know when
509 * to delete the channel (see consumer.c).
511 * As for the user space tracer now, the consumer creates and sends the
512 * stream to the session daemon which only sends them to the application
513 * once every stream of a channel is received making this value useless
514 * because we they will be added to the poll thread before the application
515 * receives them. This ensures that a stream can not hang up during
516 * initilization of a channel.
518 channel
->nb_init_stream_left
= 0;
520 /* The reply msg status is handled in the following call. */
521 ret
= create_ust_channel(attr
, &channel
->uchan
);
526 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
528 /* Open all streams for this channel. */
529 ret
= create_ust_streams(channel
, ctx
);
539 * Send all stream of a channel to the right thread handling it.
541 * On error, return a negative value else 0 on success.
543 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
544 struct lttng_consumer_local_data
*ctx
)
547 struct lttng_consumer_stream
*stream
, *stmp
;
552 /* Send streams to the corresponding thread. */
553 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
555 /* Sending the stream to the thread. */
556 ret
= send_stream_to_thread(stream
, ctx
);
559 * If we are unable to send the stream to the thread, there is
560 * a big problem so just stop everything.
562 /* Remove node from the channel stream list. */
563 cds_list_del(&stream
->send_node
);
567 /* Remove node from the channel stream list. */
568 cds_list_del(&stream
->send_node
);
576 * Write metadata to the given channel using ustctl to convert the string to
578 * Called only from consumer_metadata_cache_write.
579 * The metadata cache lock MUST be acquired to write in the cache.
581 * Return 0 on success else a negative value.
583 int lttng_ustconsumer_push_metadata(struct lttng_consumer_channel
*metadata
,
584 const char *metadata_str
, uint64_t target_offset
, uint64_t len
)
589 assert(metadata_str
);
591 DBG("UST consumer writing metadata to channel %s", metadata
->name
);
593 if (!metadata
->metadata_stream
) {
598 assert(target_offset
<= metadata
->metadata_cache
->max_offset
);
599 ret
= ustctl_write_metadata_to_channel(metadata
->uchan
,
600 metadata_str
+ target_offset
, len
);
602 ERR("ustctl write metadata fail with ret %d, len %" PRIu64
, ret
, len
);
606 ustctl_flush_buffer(metadata
->metadata_stream
->ustream
, 1);
613 * Flush channel's streams using the given key to retrieve the channel.
615 * Return 0 on success else an LTTng error code.
617 static int flush_channel(uint64_t chan_key
)
620 struct lttng_consumer_channel
*channel
;
621 struct lttng_consumer_stream
*stream
;
623 struct lttng_ht_iter iter
;
625 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
628 channel
= consumer_find_channel(chan_key
);
630 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
631 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
635 ht
= consumer_data
.stream_per_chan_id_ht
;
637 /* For each stream of the channel id, flush it. */
638 cds_lfht_for_each_entry_duplicate(ht
->ht
,
639 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
640 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
641 ustctl_flush_buffer(stream
->ustream
, 1);
649 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
650 * RCU read side lock MUST be acquired before calling this function.
652 * Return 0 on success else an LTTng error code.
654 static int close_metadata(uint64_t chan_key
)
657 struct lttng_consumer_channel
*channel
;
659 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
661 channel
= consumer_find_channel(chan_key
);
664 * This is possible if the metadata thread has issue a delete because
665 * the endpoint point of the stream hung up. There is no way the
666 * session daemon can know about it thus use a DBG instead of an actual
669 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
670 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
674 pthread_mutex_lock(&consumer_data
.lock
);
675 pthread_mutex_lock(&channel
->lock
);
677 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
681 if (channel
->switch_timer_enabled
== 1) {
682 DBG("Deleting timer on metadata channel");
683 consumer_timer_switch_stop(channel
);
686 if (channel
->metadata_stream
) {
687 ret
= ustctl_stream_close_wakeup_fd(channel
->metadata_stream
->ustream
);
689 ERR("UST consumer unable to close fd of metadata (ret: %d)", ret
);
690 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
696 pthread_mutex_unlock(&channel
->lock
);
697 pthread_mutex_unlock(&consumer_data
.lock
);
703 * RCU read side lock MUST be acquired before calling this function.
705 * Return 0 on success else an LTTng error code.
707 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
710 struct lttng_consumer_channel
*metadata
;
712 DBG("UST consumer setup metadata key %" PRIu64
, key
);
714 metadata
= consumer_find_channel(key
);
716 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
717 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
722 * Send metadata stream to relayd if one available. Availability is
723 * known if the stream is still in the list of the channel.
725 if (cds_list_empty(&metadata
->streams
.head
)) {
726 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
727 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
731 /* Send metadata stream to relayd if needed. */
732 ret
= send_stream_to_relayd(metadata
->metadata_stream
);
734 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
738 ret
= send_streams_to_thread(metadata
, ctx
);
741 * If we are unable to send the stream to the thread, there is
742 * a big problem so just stop everything.
744 ret
= LTTCOMM_CONSUMERD_FATAL
;
747 /* List MUST be empty after or else it could be reused. */
748 assert(cds_list_empty(&metadata
->streams
.head
));
754 * Delete metadata channel on error. At this point, the metadata stream can
755 * NOT be monitored by the metadata thread thus having the guarantee that
756 * the stream is still in the local stream list of the channel. This call
757 * will make sure to clean that list.
759 consumer_del_channel(metadata
);
765 * Receive the metadata updates from the sessiond.
767 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
768 uint64_t len
, struct lttng_consumer_channel
*channel
,
771 int ret
, ret_code
= LTTNG_OK
;
774 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
776 metadata_str
= zmalloc(len
* sizeof(char));
778 PERROR("zmalloc metadata string");
779 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
783 /* Receive metadata string. */
784 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
786 /* Session daemon is dead so return gracefully. */
792 pthread_mutex_lock(&channel
->lock
);
794 pthread_mutex_lock(&channel
->timer_lock
);
795 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
796 ret
= consumer_metadata_cache_write(channel
, offset
, len
, metadata_str
);
798 /* Unable to handle metadata. Notify session daemon. */
799 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
801 * Skip metadata flush on write error since the offset and len might
802 * not have been updated which could create an infinite loop below when
803 * waiting for the metadata cache to be flushed.
805 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
806 pthread_mutex_unlock(&channel
->timer_lock
);
808 pthread_mutex_unlock(&channel
->lock
);
812 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
813 pthread_mutex_unlock(&channel
->timer_lock
);
815 pthread_mutex_unlock(&channel
->lock
);
818 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
819 DBG("Waiting for metadata to be flushed");
820 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
830 * Receive command from session daemon and process it.
832 * Return 1 on success else a negative value or 0.
834 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
835 int sock
, struct pollfd
*consumer_sockpoll
)
838 enum lttng_error_code ret_code
= LTTNG_OK
;
839 struct lttcomm_consumer_msg msg
;
840 struct lttng_consumer_channel
*channel
= NULL
;
842 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
843 if (ret
!= sizeof(msg
)) {
844 DBG("Consumer received unexpected message size %zd (expects %zu)",
847 * The ret value might 0 meaning an orderly shutdown but this is ok
848 * since the caller handles this.
851 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
856 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
858 * Notify the session daemon that the command is completed.
860 * On transport layer error, the function call will print an error
861 * message so handling the returned code is a bit useless since we
862 * return an error code anyway.
864 (void) consumer_send_status_msg(sock
, ret_code
);
868 /* relayd needs RCU read-side lock */
871 switch (msg
.cmd_type
) {
872 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
874 /* Session daemon status message are handled in the following call. */
875 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
876 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
877 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
880 case LTTNG_CONSUMER_DESTROY_RELAYD
:
882 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
883 struct consumer_relayd_sock_pair
*relayd
;
885 DBG("UST consumer destroying relayd %" PRIu64
, index
);
887 /* Get relayd reference if exists. */
888 relayd
= consumer_find_relayd(index
);
889 if (relayd
== NULL
) {
890 DBG("Unable to find relayd %" PRIu64
, index
);
891 ret_code
= LTTNG_ERR_NO_CONSUMER
;
895 * Each relayd socket pair has a refcount of stream attached to it
896 * which tells if the relayd is still active or not depending on the
899 * This will set the destroy flag of the relayd object and destroy it
900 * if the refcount reaches zero when called.
902 * The destroy can happen either here or when a stream fd hangs up.
905 consumer_flag_relayd_for_destroy(relayd
);
908 goto end_msg_sessiond
;
910 case LTTNG_CONSUMER_UPDATE_STREAM
:
915 case LTTNG_CONSUMER_DATA_PENDING
:
917 int ret
, is_data_pending
;
918 uint64_t id
= msg
.u
.data_pending
.session_id
;
920 DBG("UST consumer data pending command for id %" PRIu64
, id
);
922 is_data_pending
= consumer_data_pending(id
);
924 /* Send back returned value to session daemon */
925 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
926 sizeof(is_data_pending
));
928 DBG("Error when sending the data pending ret code: %d", ret
);
933 * No need to send back a status message since the data pending
934 * returned value is the response.
938 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
941 struct ustctl_consumer_channel_attr attr
;
943 /* Create a plain object and reserve a channel key. */
944 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
945 msg
.u
.ask_channel
.pathname
, msg
.u
.ask_channel
.name
,
946 msg
.u
.ask_channel
.uid
, msg
.u
.ask_channel
.gid
,
947 msg
.u
.ask_channel
.relayd_id
, msg
.u
.ask_channel
.key
,
948 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
949 msg
.u
.ask_channel
.tracefile_size
,
950 msg
.u
.ask_channel
.tracefile_count
,
951 msg
.u
.ask_channel
.session_id_per_pid
);
953 goto end_channel_error
;
956 /* Build channel attributes from received message. */
957 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
958 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
959 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
960 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
961 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
962 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
963 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
965 /* Translate the event output type to UST. */
966 switch (channel
->output
) {
967 case LTTNG_EVENT_SPLICE
:
968 /* Splice not supported so fallback on mmap(). */
969 case LTTNG_EVENT_MMAP
:
971 attr
.output
= CONSUMER_CHANNEL_MMAP
;
975 /* Translate and save channel type. */
976 switch (msg
.u
.ask_channel
.type
) {
977 case LTTNG_UST_CHAN_PER_CPU
:
978 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
979 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
981 * Set refcount to 1 for owner. Below, we will
982 * pass ownership to the
983 * consumer_thread_channel_poll() thread.
985 channel
->refcount
= 1;
987 case LTTNG_UST_CHAN_METADATA
:
988 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
989 attr
.type
= LTTNG_UST_CHAN_METADATA
;
996 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
998 goto end_channel_error
;
1001 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1002 ret
= consumer_metadata_cache_allocate(channel
);
1004 ERR("Allocating metadata cache");
1005 goto end_channel_error
;
1007 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1008 attr
.switch_timer_interval
= 0;
1012 * Add the channel to the internal state AFTER all streams were created
1013 * and successfully sent to session daemon. This way, all streams must
1014 * be ready before this channel is visible to the threads.
1015 * If add_channel succeeds, ownership of the channel is
1016 * passed to consumer_thread_channel_poll().
1018 ret
= add_channel(channel
, ctx
);
1020 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1021 if (channel
->switch_timer_enabled
== 1) {
1022 consumer_timer_switch_stop(channel
);
1024 consumer_metadata_cache_destroy(channel
);
1026 goto end_channel_error
;
1030 * Channel and streams are now created. Inform the session daemon that
1031 * everything went well and should wait to receive the channel and
1032 * streams with ustctl API.
1034 ret
= consumer_send_status_channel(sock
, channel
);
1037 * There is probably a problem on the socket.
1044 case LTTNG_CONSUMER_GET_CHANNEL
:
1046 int ret
, relayd_err
= 0;
1047 uint64_t key
= msg
.u
.get_channel
.key
;
1048 struct lttng_consumer_channel
*channel
;
1050 channel
= consumer_find_channel(key
);
1052 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1053 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1054 goto end_msg_sessiond
;
1057 /* Send everything to sessiond. */
1058 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1062 * We were unable to send to the relayd the stream so avoid
1063 * sending back a fatal error to the thread since this is OK
1064 * and the consumer can continue its work. The above call
1065 * has sent the error status message to the sessiond.
1070 * The communicaton was broken hence there is a bad state between
1071 * the consumer and sessiond so stop everything.
1076 ret
= send_streams_to_thread(channel
, ctx
);
1079 * If we are unable to send the stream to the thread, there is
1080 * a big problem so just stop everything.
1084 /* List MUST be empty after or else it could be reused. */
1085 assert(cds_list_empty(&channel
->streams
.head
));
1087 goto end_msg_sessiond
;
1089 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1091 uint64_t key
= msg
.u
.destroy_channel
.key
;
1094 * Only called if streams have not been sent to stream
1095 * manager thread. However, channel has been sent to
1096 * channel manager thread.
1098 notify_thread_del_channel(ctx
, key
);
1099 goto end_msg_sessiond
;
1101 case LTTNG_CONSUMER_CLOSE_METADATA
:
1105 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1110 goto end_msg_sessiond
;
1112 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1116 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1121 goto end_msg_sessiond
;
1123 case LTTNG_CONSUMER_PUSH_METADATA
:
1126 uint64_t len
= msg
.u
.push_metadata
.len
;
1127 uint64_t key
= msg
.u
.push_metadata
.key
;
1128 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1129 struct lttng_consumer_channel
*channel
;
1131 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1134 channel
= consumer_find_channel(key
);
1136 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
1137 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1138 goto end_msg_sessiond
;
1141 /* Tell session daemon we are ready to receive the metadata. */
1142 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
1144 /* Somehow, the session daemon is not responding anymore. */
1148 /* Wait for more data. */
1149 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1153 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1156 /* error receiving from sessiond */
1160 goto end_msg_sessiond
;
1163 case LTTNG_CONSUMER_SETUP_METADATA
:
1167 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1171 goto end_msg_sessiond
;
1181 * Return 1 to indicate success since the 0 value can be a socket
1182 * shutdown during the recv() or send() call.
1188 * The returned value here is not useful since either way we'll return 1 to
1189 * the caller because the session daemon socket management is done
1190 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1192 ret
= consumer_send_status_msg(sock
, ret_code
);
1201 * Free channel here since no one has a reference to it. We don't
1202 * free after that because a stream can store this pointer.
1204 destroy_channel(channel
);
1206 /* We have to send a status channel message indicating an error. */
1207 ret
= consumer_send_status_channel(sock
, NULL
);
1209 /* Stop everything if session daemon can not be notified. */
1216 /* This will issue a consumer stop. */
1221 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1222 * compiled out, we isolate it in this library.
1224 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
1228 assert(stream
->ustream
);
1230 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
1234 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1235 * compiled out, we isolate it in this library.
1237 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
1240 assert(stream
->ustream
);
1242 return ustctl_get_mmap_base(stream
->ustream
);
1246 * Take a snapshot for a specific fd
1248 * Returns 0 on success, < 0 on error
1250 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1253 assert(stream
->ustream
);
1255 return ustctl_snapshot(stream
->ustream
);
1259 * Get the produced position
1261 * Returns 0 on success, < 0 on error
1263 int lttng_ustconsumer_get_produced_snapshot(
1264 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1267 assert(stream
->ustream
);
1270 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
1274 * Called when the stream signal the consumer that it has hang up.
1276 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
1279 assert(stream
->ustream
);
1281 ustctl_flush_buffer(stream
->ustream
, 0);
1282 stream
->hangup_flush_done
= 1;
1285 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
1288 assert(chan
->uchan
);
1290 if (chan
->switch_timer_enabled
== 1) {
1291 consumer_timer_switch_stop(chan
);
1293 consumer_metadata_cache_destroy(chan
);
1294 ustctl_destroy_channel(chan
->uchan
);
1297 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
1300 assert(stream
->ustream
);
1302 if (stream
->chan
->switch_timer_enabled
== 1) {
1303 consumer_timer_switch_stop(stream
->chan
);
1305 ustctl_destroy_stream(stream
->ustream
);
1308 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1309 struct lttng_consumer_local_data
*ctx
)
1311 unsigned long len
, subbuf_size
, padding
;
1315 struct ustctl_consumer_stream
*ustream
;
1318 assert(stream
->ustream
);
1321 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
1324 /* Ease our life for what's next. */
1325 ustream
= stream
->ustream
;
1327 /* We can consume the 1 byte written into the wait_fd by UST */
1328 if (!stream
->hangup_flush_done
) {
1332 readlen
= read(stream
->wait_fd
, &dummy
, 1);
1333 } while (readlen
== -1 && errno
== EINTR
);
1334 if (readlen
== -1) {
1340 /* Get the next subbuffer */
1341 err
= ustctl_get_next_subbuf(ustream
);
1343 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
1345 * This is a debug message even for single-threaded consumer,
1346 * because poll() have more relaxed criterions than get subbuf,
1347 * so get_subbuf may fail for short race windows where poll()
1348 * would issue wakeups.
1350 DBG("Reserving sub buffer failed (everything is normal, "
1351 "it is due to concurrency) [ret: %d]", err
);
1354 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1355 /* Get the full padded subbuffer size */
1356 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
1359 /* Get subbuffer data size (without padding) */
1360 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
1363 /* Make sure we don't get a subbuffer size bigger than the padded */
1364 assert(len
>= subbuf_size
);
1366 padding
= len
- subbuf_size
;
1367 /* write the subbuffer to the tracefile */
1368 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
);
1370 * The mmap operation should write subbuf_size amount of data when network
1371 * streaming or the full padding (len) size when we are _not_ streaming.
1373 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1374 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1376 * Display the error but continue processing to try to release the
1377 * subbuffer. This is a DBG statement since any unexpected kill or
1378 * signal, the application gets unregistered, relayd gets closed or
1379 * anything that affects the buffer lifetime will trigger this error.
1380 * So, for the sake of the user, don't print this error since it can
1381 * happen and it is OK with the code flow.
1383 DBG("Error writing to tracefile "
1384 "(ret: %ld != len: %lu != subbuf_size: %lu)",
1385 ret
, len
, subbuf_size
);
1387 err
= ustctl_put_next_subbuf(ustream
);
1395 * Called when a stream is created.
1397 * Return 0 on success or else a negative value.
1399 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1403 /* Don't create anything if this is set for streaming. */
1404 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
1405 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1406 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1407 stream
->uid
, stream
->gid
);
1411 stream
->out_fd
= ret
;
1412 stream
->tracefile_size_current
= 0;
1421 * Check if data is still being extracted from the buffers for a specific
1422 * stream. Consumer data lock MUST be acquired before calling this function
1423 * and the stream lock.
1425 * Return 1 if the traced data are still getting read else 0 meaning that the
1426 * data is available for trace viewer reading.
1428 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1433 assert(stream
->ustream
);
1435 DBG("UST consumer checking data pending");
1437 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1442 ret
= ustctl_get_next_subbuf(stream
->ustream
);
1444 /* There is still data so let's put back this subbuffer. */
1445 ret
= ustctl_put_subbuf(stream
->ustream
);
1447 ret
= 1; /* Data is pending */
1451 /* Data is NOT pending so ready to be read. */
1459 * Close every metadata stream wait fd of the metadata hash table. This
1460 * function MUST be used very carefully so not to run into a race between the
1461 * metadata thread handling streams and this function closing their wait fd.
1463 * For UST, this is used when the session daemon hangs up. Its the metadata
1464 * producer so calling this is safe because we are assured that no state change
1465 * can occur in the metadata thread for the streams in the hash table.
1467 void lttng_ustconsumer_close_metadata(struct lttng_ht
*metadata_ht
)
1470 struct lttng_ht_iter iter
;
1471 struct lttng_consumer_stream
*stream
;
1473 assert(metadata_ht
);
1474 assert(metadata_ht
->ht
);
1476 DBG("UST consumer closing all metadata streams");
1479 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
1481 int fd
= stream
->wait_fd
;
1484 * Whatever happens here we have to continue to try to close every
1485 * streams. Let's report at least the error on failure.
1487 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
1489 ERR("Unable to close metadata stream fd %d ret %d", fd
, ret
);
1491 DBG("Metadata wait fd %d closed", fd
);
1496 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
1500 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
1502 ERR("Unable to close wakeup fd");
1507 * Please refer to consumer-timer.c before adding any lock within this
1508 * function or any of its callees. Timers have a very strict locking
1509 * semantic with respect to teardown. Failure to respect this semantic
1510 * introduces deadlocks.
1512 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
1513 struct lttng_consumer_channel
*channel
, int timer
)
1515 struct lttcomm_metadata_request_msg request
;
1516 struct lttcomm_consumer_msg msg
;
1517 enum lttng_error_code ret_code
= LTTNG_OK
;
1518 uint64_t len
, key
, offset
;
1522 assert(channel
->metadata_cache
);
1524 /* send the metadata request to sessiond */
1525 switch (consumer_data
.type
) {
1526 case LTTNG_CONSUMER64_UST
:
1527 request
.bits_per_long
= 64;
1529 case LTTNG_CONSUMER32_UST
:
1530 request
.bits_per_long
= 32;
1533 request
.bits_per_long
= 0;
1537 request
.session_id
= channel
->session_id
;
1538 request
.session_id_per_pid
= channel
->session_id_per_pid
;
1539 request
.uid
= channel
->uid
;
1540 request
.key
= channel
->key
;
1541 DBG("Sending metadata request to sessiond, session id %" PRIu64
1542 ", per-pid %" PRIu64
,
1543 channel
->session_id
,
1544 channel
->session_id_per_pid
);
1546 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
1549 ERR("Asking metadata to sessiond");
1553 /* Receive the metadata from sessiond */
1554 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
1556 if (ret
!= sizeof(msg
)) {
1557 DBG("Consumer received unexpected message size %d (expects %zu)",
1559 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1561 * The ret value might 0 meaning an orderly shutdown but this is ok
1562 * since the caller handles this.
1567 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
1568 /* No registry found */
1569 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
1573 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
1574 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
1579 len
= msg
.u
.push_metadata
.len
;
1580 key
= msg
.u
.push_metadata
.key
;
1581 offset
= msg
.u
.push_metadata
.target_offset
;
1583 assert(key
== channel
->key
);
1585 DBG("No new metadata to receive for key %" PRIu64
, key
);
1588 /* Tell session daemon we are ready to receive the metadata. */
1589 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
1591 if (ret
< 0 || len
== 0) {
1593 * Somehow, the session daemon is not responding anymore or there is
1594 * nothing to receive.
1599 ret_code
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
1600 key
, offset
, len
, channel
, timer
);
1601 if (ret_code
>= 0) {
1603 * Only send the status msg if the sessiond is alive meaning a positive
1606 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret_code
);