2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
13 #include <lttng/ust-ctl.h>
19 #include <sys/socket.h>
21 #include <sys/types.h>
24 #include <urcu/list.h>
28 #include <bin/lttng-consumerd/health-consumerd.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/relayd/relayd.h>
32 #include <common/compat/fcntl.h>
33 #include <common/compat/endian.h>
34 #include <common/consumer/consumer-metadata-cache.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/utils.h>
38 #include <common/index/index.h>
40 #include "ust-consumer.h"
42 #define INT_MAX_STR_LEN 12 /* includes \0 */
44 extern struct lttng_consumer_global_data consumer_data
;
45 extern int consumer_poll_timeout
;
48 * Free channel object and all streams associated with it. This MUST be used
49 * only and only if the channel has _NEVER_ been added to the global channel
52 static void destroy_channel(struct lttng_consumer_channel
*channel
)
54 struct lttng_consumer_stream
*stream
, *stmp
;
58 DBG("UST consumer cleaning stream list");
60 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
65 cds_list_del(&stream
->send_node
);
66 ustctl_destroy_stream(stream
->ustream
);
67 lttng_trace_chunk_put(stream
->trace_chunk
);
72 * If a channel is available meaning that was created before the streams
76 lttng_ustconsumer_del_channel(channel
);
77 lttng_ustconsumer_free_channel(channel
);
83 * Add channel to internal consumer state.
85 * Returns 0 on success or else a negative value.
87 static int add_channel(struct lttng_consumer_channel
*channel
,
88 struct lttng_consumer_local_data
*ctx
)
95 if (ctx
->on_recv_channel
!= NULL
) {
96 ret
= ctx
->on_recv_channel(channel
);
98 ret
= consumer_add_channel(channel
, ctx
);
100 /* Most likely an ENOMEM. */
101 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
105 ret
= consumer_add_channel(channel
, ctx
);
108 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
115 * Allocate and return a consumer channel object.
117 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
118 const uint64_t *chunk_id
, const char *pathname
, const char *name
,
119 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
120 uint64_t tracefile_size
, uint64_t tracefile_count
,
121 uint64_t session_id_per_pid
, unsigned int monitor
,
122 unsigned int live_timer_interval
,
123 const char *root_shm_path
, const char *shm_path
)
128 return consumer_allocate_channel(key
, session_id
, chunk_id
, pathname
,
129 name
, relayd_id
, output
, tracefile_size
,
130 tracefile_count
, session_id_per_pid
, monitor
,
131 live_timer_interval
, root_shm_path
, shm_path
);
135 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
136 * error value if applicable is set in it else it is kept untouched.
138 * Return NULL on error else the newly allocated stream object.
140 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
141 struct lttng_consumer_channel
*channel
,
142 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
145 struct lttng_consumer_stream
*stream
= NULL
;
150 stream
= consumer_allocate_stream(channel
->key
,
155 channel
->trace_chunk
,
160 if (stream
== NULL
) {
164 * We could not find the channel. Can happen if cpu hotplug
165 * happens while tearing down.
167 DBG3("Could not find channel");
172 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
178 consumer_stream_update_channel_attributes(stream
, channel
);
179 stream
->chan
= channel
;
183 *_alloc_ret
= alloc_ret
;
189 * Send the given stream pointer to the corresponding thread.
191 * Returns 0 on success else a negative value.
193 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
194 struct lttng_consumer_local_data
*ctx
)
197 struct lttng_pipe
*stream_pipe
;
199 /* Get the right pipe where the stream will be sent. */
200 if (stream
->metadata_flag
) {
201 consumer_add_metadata_stream(stream
);
202 stream_pipe
= ctx
->consumer_metadata_pipe
;
204 consumer_add_data_stream(stream
);
205 stream_pipe
= ctx
->consumer_data_pipe
;
209 * From this point on, the stream's ownership has been moved away from
210 * the channel and it becomes globally visible. Hence, remove it from
211 * the local stream list to prevent the stream from being both local and
214 stream
->globally_visible
= 1;
215 cds_list_del(&stream
->send_node
);
217 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
219 ERR("Consumer write %s stream to pipe %d",
220 stream
->metadata_flag
? "metadata" : "data",
221 lttng_pipe_get_writefd(stream_pipe
));
222 if (stream
->metadata_flag
) {
223 consumer_del_stream_for_metadata(stream
);
225 consumer_del_stream_for_data(stream
);
235 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
237 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
240 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
241 stream_shm_path
[PATH_MAX
- 1] = '\0';
242 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
247 strncat(stream_shm_path
, cpu_nr
,
248 PATH_MAX
- strlen(stream_shm_path
) - 1);
255 * Create streams for the given channel using liblttng-ust-ctl.
256 * The channel lock must be acquired by the caller.
258 * Return 0 on success else a negative value.
260 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
261 struct lttng_consumer_local_data
*ctx
)
264 struct ustctl_consumer_stream
*ustream
;
265 struct lttng_consumer_stream
*stream
;
266 pthread_mutex_t
*current_stream_lock
= NULL
;
272 * While a stream is available from ustctl. When NULL is returned, we've
273 * reached the end of the possible stream for the channel.
275 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
277 int ust_metadata_pipe
[2];
279 health_code_update();
281 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
282 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
284 ERR("Create ust metadata poll pipe");
287 wait_fd
= ust_metadata_pipe
[0];
289 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
292 /* Allocate consumer stream object. */
293 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
297 stream
->ustream
= ustream
;
299 * Store it so we can save multiple function calls afterwards since
300 * this value is used heavily in the stream threads. This is UST
301 * specific so this is why it's done after allocation.
303 stream
->wait_fd
= wait_fd
;
306 * Increment channel refcount since the channel reference has now been
307 * assigned in the allocation process above.
309 if (stream
->chan
->monitor
) {
310 uatomic_inc(&stream
->chan
->refcount
);
313 pthread_mutex_lock(&stream
->lock
);
314 current_stream_lock
= &stream
->lock
;
316 * Order is important this is why a list is used. On error, the caller
317 * should clean this list.
319 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
321 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
322 &stream
->max_sb_size
);
324 ERR("ustctl_get_max_subbuf_size failed for stream %s",
329 /* Do actions once stream has been received. */
330 if (ctx
->on_recv_stream
) {
331 ret
= ctx
->on_recv_stream(stream
);
337 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
338 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
340 /* Set next CPU stream. */
341 channel
->streams
.count
= ++cpu
;
343 /* Keep stream reference when creating metadata. */
344 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
345 channel
->metadata_stream
= stream
;
346 if (channel
->monitor
) {
347 /* Set metadata poll pipe if we created one */
348 memcpy(stream
->ust_metadata_poll_pipe
,
350 sizeof(ust_metadata_pipe
));
353 pthread_mutex_unlock(&stream
->lock
);
354 current_stream_lock
= NULL
;
361 if (current_stream_lock
) {
362 pthread_mutex_unlock(current_stream_lock
);
368 * create_posix_shm is never called concurrently within a process.
371 int create_posix_shm(void)
373 char tmp_name
[NAME_MAX
];
376 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
382 * Allocate shm, and immediately unlink its shm oject, keeping
383 * only the file descriptor as a reference to the object.
384 * We specifically do _not_ use the / at the beginning of the
385 * pathname so that some OS implementations can keep it local to
386 * the process (POSIX leaves this implementation-defined).
388 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
393 ret
= shm_unlink(tmp_name
);
394 if (ret
< 0 && errno
!= ENOENT
) {
395 PERROR("shm_unlink");
396 goto error_shm_release
;
409 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
410 const struct lttng_credentials
*session_credentials
)
412 char shm_path
[PATH_MAX
];
415 if (!channel
->shm_path
[0]) {
416 return create_posix_shm();
418 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
422 return run_as_open(shm_path
,
423 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
424 session_credentials
->uid
, session_credentials
->gid
);
431 * Create an UST channel with the given attributes and send it to the session
432 * daemon using the ust ctl API.
434 * Return 0 on success or else a negative value.
436 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
437 struct ustctl_consumer_channel_attr
*attr
,
438 struct ustctl_consumer_channel
**ust_chanp
)
440 int ret
, nr_stream_fds
, i
, j
;
442 struct ustctl_consumer_channel
*ust_channel
;
447 assert(channel
->buffer_credentials
.is_set
);
449 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
450 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
451 "switch_timer_interval: %u, read_timer_interval: %u, "
452 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
453 attr
->num_subbuf
, attr
->switch_timer_interval
,
454 attr
->read_timer_interval
, attr
->output
, attr
->type
);
456 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
459 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
460 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
465 for (i
= 0; i
< nr_stream_fds
; i
++) {
466 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
467 &channel
->buffer_credentials
.value
);
468 if (stream_fds
[i
] < 0) {
473 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
478 channel
->nr_stream_fds
= nr_stream_fds
;
479 channel
->stream_fds
= stream_fds
;
480 *ust_chanp
= ust_channel
;
486 for (j
= i
- 1; j
>= 0; j
--) {
489 closeret
= close(stream_fds
[j
]);
493 if (channel
->shm_path
[0]) {
494 char shm_path
[PATH_MAX
];
496 closeret
= get_stream_shm_path(shm_path
,
497 channel
->shm_path
, j
);
499 ERR("Cannot get stream shm path");
501 closeret
= run_as_unlink(shm_path
,
502 channel
->buffer_credentials
.value
.uid
,
503 channel
->buffer_credentials
.value
.gid
);
505 PERROR("unlink %s", shm_path
);
509 /* Try to rmdir all directories under shm_path root. */
510 if (channel
->root_shm_path
[0]) {
511 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
512 channel
->buffer_credentials
.value
.uid
,
513 channel
->buffer_credentials
.value
.gid
,
514 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
522 * Send a single given stream to the session daemon using the sock.
524 * Return 0 on success else a negative value.
526 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
533 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
535 /* Send stream to session daemon. */
536 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
546 * Send channel to sessiond and relayd if applicable.
548 * Return 0 on success or else a negative value.
550 static int send_channel_to_sessiond_and_relayd(int sock
,
551 struct lttng_consumer_channel
*channel
,
552 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
554 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
555 struct lttng_consumer_stream
*stream
;
556 uint64_t net_seq_idx
= -1ULL;
562 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
564 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
565 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
567 health_code_update();
569 /* Try to send the stream to the relayd if one is available. */
570 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
571 stream
->key
, channel
->name
);
572 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
575 * Flag that the relayd was the problem here probably due to a
576 * communicaton error on the socket.
581 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
583 if (net_seq_idx
== -1ULL) {
584 net_seq_idx
= stream
->net_seq_idx
;
589 /* Inform sessiond that we are about to send channel and streams. */
590 ret
= consumer_send_status_msg(sock
, ret_code
);
591 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
593 * Either the session daemon is not responding or the relayd died so we
599 /* Send channel to sessiond. */
600 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
605 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
610 /* The channel was sent successfully to the sessiond at this point. */
611 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
613 health_code_update();
615 /* Send stream to session daemon. */
616 ret
= send_sessiond_stream(sock
, stream
);
622 /* Tell sessiond there is no more stream. */
623 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
628 DBG("UST consumer NULL stream sent to sessiond");
633 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
640 * Creates a channel and streams and add the channel it to the channel internal
641 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
644 * Return 0 on success or else, a negative value is returned and the channel
645 * MUST be destroyed by consumer_del_channel().
647 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
648 struct lttng_consumer_channel
*channel
,
649 struct ustctl_consumer_channel_attr
*attr
)
658 * This value is still used by the kernel consumer since for the kernel,
659 * the stream ownership is not IN the consumer so we need to have the
660 * number of left stream that needs to be initialized so we can know when
661 * to delete the channel (see consumer.c).
663 * As for the user space tracer now, the consumer creates and sends the
664 * stream to the session daemon which only sends them to the application
665 * once every stream of a channel is received making this value useless
666 * because we they will be added to the poll thread before the application
667 * receives them. This ensures that a stream can not hang up during
668 * initilization of a channel.
670 channel
->nb_init_stream_left
= 0;
672 /* The reply msg status is handled in the following call. */
673 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
678 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
681 * For the snapshots (no monitor), we create the metadata streams
682 * on demand, not during the channel creation.
684 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
689 /* Open all streams for this channel. */
690 pthread_mutex_lock(&channel
->lock
);
691 ret
= create_ust_streams(channel
, ctx
);
692 pthread_mutex_unlock(&channel
->lock
);
702 * Send all stream of a channel to the right thread handling it.
704 * On error, return a negative value else 0 on success.
706 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
707 struct lttng_consumer_local_data
*ctx
)
710 struct lttng_consumer_stream
*stream
, *stmp
;
715 /* Send streams to the corresponding thread. */
716 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
719 health_code_update();
721 /* Sending the stream to the thread. */
722 ret
= send_stream_to_thread(stream
, ctx
);
725 * If we are unable to send the stream to the thread, there is
726 * a big problem so just stop everything.
737 * Flush channel's streams using the given key to retrieve the channel.
739 * Return 0 on success else an LTTng error code.
741 static int flush_channel(uint64_t chan_key
)
744 struct lttng_consumer_channel
*channel
;
745 struct lttng_consumer_stream
*stream
;
747 struct lttng_ht_iter iter
;
749 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
752 channel
= consumer_find_channel(chan_key
);
754 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
755 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
759 ht
= consumer_data
.stream_per_chan_id_ht
;
761 /* For each stream of the channel id, flush it. */
762 cds_lfht_for_each_entry_duplicate(ht
->ht
,
763 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
764 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
766 health_code_update();
768 pthread_mutex_lock(&stream
->lock
);
771 * Protect against concurrent teardown of a stream.
773 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
777 if (!stream
->quiescent
) {
778 ustctl_flush_buffer(stream
->ustream
, 0);
779 stream
->quiescent
= true;
782 pthread_mutex_unlock(&stream
->lock
);
790 * Clear quiescent state from channel's streams using the given key to
791 * retrieve the channel.
793 * Return 0 on success else an LTTng error code.
795 static int clear_quiescent_channel(uint64_t chan_key
)
798 struct lttng_consumer_channel
*channel
;
799 struct lttng_consumer_stream
*stream
;
801 struct lttng_ht_iter iter
;
803 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
806 channel
= consumer_find_channel(chan_key
);
808 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
809 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
813 ht
= consumer_data
.stream_per_chan_id_ht
;
815 /* For each stream of the channel id, clear quiescent state. */
816 cds_lfht_for_each_entry_duplicate(ht
->ht
,
817 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
818 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
820 health_code_update();
822 pthread_mutex_lock(&stream
->lock
);
823 stream
->quiescent
= false;
824 pthread_mutex_unlock(&stream
->lock
);
832 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
834 * Return 0 on success else an LTTng error code.
836 static int close_metadata(uint64_t chan_key
)
839 struct lttng_consumer_channel
*channel
;
840 unsigned int channel_monitor
;
842 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
844 channel
= consumer_find_channel(chan_key
);
847 * This is possible if the metadata thread has issue a delete because
848 * the endpoint point of the stream hung up. There is no way the
849 * session daemon can know about it thus use a DBG instead of an actual
852 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
853 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
857 pthread_mutex_lock(&consumer_data
.lock
);
858 pthread_mutex_lock(&channel
->lock
);
859 channel_monitor
= channel
->monitor
;
860 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
864 lttng_ustconsumer_close_metadata(channel
);
865 pthread_mutex_unlock(&channel
->lock
);
866 pthread_mutex_unlock(&consumer_data
.lock
);
869 * The ownership of a metadata channel depends on the type of
870 * session to which it belongs. In effect, the monitor flag is checked
871 * to determine if this metadata channel is in "snapshot" mode or not.
873 * In the non-snapshot case, the metadata channel is created along with
874 * a single stream which will remain present until the metadata channel
875 * is destroyed (on the destruction of its session). In this case, the
876 * metadata stream in "monitored" by the metadata poll thread and holds
877 * the ownership of its channel.
879 * Closing the metadata will cause the metadata stream's "metadata poll
880 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
881 * thread which will teardown the metadata stream which, in return,
882 * deletes the metadata channel.
884 * In the snapshot case, the metadata stream is created and destroyed
885 * on every snapshot record. Since the channel doesn't have an owner
886 * other than the session daemon, it is safe to destroy it immediately
887 * on reception of the CLOSE_METADATA command.
889 if (!channel_monitor
) {
891 * The channel and consumer_data locks must be
892 * released before this call since consumer_del_channel
893 * re-acquires the channel and consumer_data locks to teardown
894 * the channel and queue its reclamation by the "call_rcu"
897 consumer_del_channel(channel
);
902 pthread_mutex_unlock(&channel
->lock
);
903 pthread_mutex_unlock(&consumer_data
.lock
);
909 * RCU read side lock MUST be acquired before calling this function.
911 * Return 0 on success else an LTTng error code.
913 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
916 struct lttng_consumer_channel
*metadata
;
918 DBG("UST consumer setup metadata key %" PRIu64
, key
);
920 metadata
= consumer_find_channel(key
);
922 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
923 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
928 * In no monitor mode, the metadata channel has no stream(s) so skip the
929 * ownership transfer to the metadata thread.
931 if (!metadata
->monitor
) {
932 DBG("Metadata channel in no monitor");
938 * Send metadata stream to relayd if one available. Availability is
939 * known if the stream is still in the list of the channel.
941 if (cds_list_empty(&metadata
->streams
.head
)) {
942 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
943 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
944 goto error_no_stream
;
947 /* Send metadata stream to relayd if needed. */
948 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
949 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
952 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
955 ret
= consumer_send_relayd_streams_sent(
956 metadata
->metadata_stream
->net_seq_idx
);
958 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
964 * Ownership of metadata stream is passed along. Freeing is handled by
967 ret
= send_streams_to_thread(metadata
, ctx
);
970 * If we are unable to send the stream to the thread, there is
971 * a big problem so just stop everything.
973 ret
= LTTCOMM_CONSUMERD_FATAL
;
974 goto send_streams_error
;
976 /* List MUST be empty after or else it could be reused. */
977 assert(cds_list_empty(&metadata
->streams
.head
));
984 * Delete metadata channel on error. At this point, the metadata stream can
985 * NOT be monitored by the metadata thread thus having the guarantee that
986 * the stream is still in the local stream list of the channel. This call
987 * will make sure to clean that list.
989 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
990 cds_list_del(&metadata
->metadata_stream
->send_node
);
991 metadata
->metadata_stream
= NULL
;
999 * Snapshot the whole metadata.
1000 * RCU read-side lock must be held by the caller.
1002 * Returns 0 on success, < 0 on error
1004 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
1005 uint64_t key
, char *path
, uint64_t relayd_id
,
1006 struct lttng_consumer_local_data
*ctx
)
1009 struct lttng_consumer_stream
*metadata_stream
;
1014 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1019 assert(!metadata_channel
->monitor
);
1021 health_code_update();
1024 * Ask the sessiond if we have new metadata waiting and update the
1025 * consumer metadata cache.
1027 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1032 health_code_update();
1035 * The metadata stream is NOT created in no monitor mode when the channel
1036 * is created on a sessiond ask channel command.
1038 ret
= create_ust_streams(metadata_channel
, ctx
);
1043 metadata_stream
= metadata_channel
->metadata_stream
;
1044 assert(metadata_stream
);
1046 pthread_mutex_lock(&metadata_stream
->lock
);
1047 if (relayd_id
!= (uint64_t) -1ULL) {
1048 metadata_stream
->net_seq_idx
= relayd_id
;
1049 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1051 ret
= consumer_stream_create_output_files(metadata_stream
,
1054 pthread_mutex_unlock(&metadata_stream
->lock
);
1060 health_code_update();
1062 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1070 * Clean up the stream completly because the next snapshot will use a new
1073 consumer_stream_destroy(metadata_stream
, NULL
);
1074 cds_list_del(&metadata_stream
->send_node
);
1075 metadata_channel
->metadata_stream
= NULL
;
1083 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1087 unsigned long mmap_offset
;
1088 const char *mmap_base
;
1090 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1092 ERR("Failed to get mmap base for stream `%s`",
1098 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1100 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1105 *addr
= mmap_base
+ mmap_offset
;
1112 * Take a snapshot of all the stream of a channel.
1113 * RCU read-side lock and the channel lock must be held by the caller.
1115 * Returns 0 on success, < 0 on error
1117 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1118 uint64_t key
, char *path
, uint64_t relayd_id
,
1119 uint64_t nb_packets_per_stream
,
1120 struct lttng_consumer_local_data
*ctx
)
1123 unsigned use_relayd
= 0;
1124 unsigned long consumed_pos
, produced_pos
;
1125 struct lttng_consumer_stream
*stream
;
1132 if (relayd_id
!= (uint64_t) -1ULL) {
1136 assert(!channel
->monitor
);
1137 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1139 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1140 health_code_update();
1142 /* Lock stream because we are about to change its state. */
1143 pthread_mutex_lock(&stream
->lock
);
1144 assert(channel
->trace_chunk
);
1145 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1147 * Can't happen barring an internal error as the channel
1148 * holds a reference to the trace chunk.
1150 ERR("Failed to acquire reference to channel's trace chunk");
1154 assert(!stream
->trace_chunk
);
1155 stream
->trace_chunk
= channel
->trace_chunk
;
1157 stream
->net_seq_idx
= relayd_id
;
1160 ret
= consumer_send_relayd_stream(stream
, path
);
1165 ret
= consumer_stream_create_output_files(stream
,
1170 DBG("UST consumer snapshot stream (%" PRIu64
")",
1175 * If tracing is active, we want to perform a "full" buffer flush.
1176 * Else, if quiescent, it has already been done by the prior stop.
1178 if (!stream
->quiescent
) {
1179 ustctl_flush_buffer(stream
->ustream
, 0);
1182 ret
= lttng_ustconsumer_take_snapshot(stream
);
1184 ERR("Taking UST snapshot");
1188 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1190 ERR("Produced UST snapshot position");
1194 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1196 ERR("Consumerd UST snapshot position");
1201 * The original value is sent back if max stream size is larger than
1202 * the possible size of the snapshot. Also, we assume that the session
1203 * daemon should never send a maximum stream size that is lower than
1206 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1207 produced_pos
, nb_packets_per_stream
,
1208 stream
->max_sb_size
);
1210 while ((long) (consumed_pos
- produced_pos
) < 0) {
1212 unsigned long len
, padded_len
;
1213 const char *subbuf_addr
;
1215 health_code_update();
1217 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1219 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1221 if (ret
!= -EAGAIN
) {
1222 PERROR("ustctl_get_subbuf snapshot");
1223 goto error_close_stream
;
1225 DBG("UST consumer get subbuf failed. Skipping it.");
1226 consumed_pos
+= stream
->max_sb_size
;
1227 stream
->chan
->lost_packets
++;
1231 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1233 ERR("Snapshot ustctl_get_subbuf_size");
1234 goto error_put_subbuf
;
1237 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1239 ERR("Snapshot ustctl_get_padded_subbuf_size");
1240 goto error_put_subbuf
;
1243 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1245 goto error_put_subbuf
;
1248 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1249 stream
, subbuf_addr
, len
,
1250 padded_len
- len
, NULL
);
1252 if (read_len
!= len
) {
1254 goto error_put_subbuf
;
1257 if (read_len
!= padded_len
) {
1259 goto error_put_subbuf
;
1263 ret
= ustctl_put_subbuf(stream
->ustream
);
1265 ERR("Snapshot ustctl_put_subbuf");
1266 goto error_close_stream
;
1268 consumed_pos
+= stream
->max_sb_size
;
1271 /* Simply close the stream so we can use it on the next snapshot. */
1272 consumer_stream_close(stream
);
1273 pthread_mutex_unlock(&stream
->lock
);
1280 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1281 ERR("Snapshot ustctl_put_subbuf");
1284 consumer_stream_close(stream
);
1286 pthread_mutex_unlock(&stream
->lock
);
1292 * Receive the metadata updates from the sessiond. Supports receiving
1293 * overlapping metadata, but is needs to always belong to a contiguous
1294 * range starting from 0.
1295 * Be careful about the locks held when calling this function: it needs
1296 * the metadata cache flush to concurrently progress in order to
1299 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1300 uint64_t len
, uint64_t version
,
1301 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1303 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1306 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1308 metadata_str
= zmalloc(len
* sizeof(char));
1309 if (!metadata_str
) {
1310 PERROR("zmalloc metadata string");
1311 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1315 health_code_update();
1317 /* Receive metadata string. */
1318 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1320 /* Session daemon is dead so return gracefully. */
1325 health_code_update();
1327 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1328 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1331 /* Unable to handle metadata. Notify session daemon. */
1332 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1334 * Skip metadata flush on write error since the offset and len might
1335 * not have been updated which could create an infinite loop below when
1336 * waiting for the metadata cache to be flushed.
1338 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1341 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1346 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1347 DBG("Waiting for metadata to be flushed");
1349 health_code_update();
1351 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1361 * Receive command from session daemon and process it.
1363 * Return 1 on success else a negative value or 0.
1365 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1366 int sock
, struct pollfd
*consumer_sockpoll
)
1369 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1370 struct lttcomm_consumer_msg msg
;
1371 struct lttng_consumer_channel
*channel
= NULL
;
1373 health_code_update();
1375 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1376 if (ret
!= sizeof(msg
)) {
1377 DBG("Consumer received unexpected message size %zd (expects %zu)",
1380 * The ret value might 0 meaning an orderly shutdown but this is ok
1381 * since the caller handles this.
1384 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1390 health_code_update();
1393 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1395 health_code_update();
1397 /* relayd needs RCU read-side lock */
1400 switch (msg
.cmd_type
) {
1401 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1403 /* Session daemon status message are handled in the following call. */
1404 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1405 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1406 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1407 msg
.u
.relayd_sock
.relayd_session_id
);
1410 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1412 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1413 struct consumer_relayd_sock_pair
*relayd
;
1415 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1417 /* Get relayd reference if exists. */
1418 relayd
= consumer_find_relayd(index
);
1419 if (relayd
== NULL
) {
1420 DBG("Unable to find relayd %" PRIu64
, index
);
1421 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1425 * Each relayd socket pair has a refcount of stream attached to it
1426 * which tells if the relayd is still active or not depending on the
1429 * This will set the destroy flag of the relayd object and destroy it
1430 * if the refcount reaches zero when called.
1432 * The destroy can happen either here or when a stream fd hangs up.
1435 consumer_flag_relayd_for_destroy(relayd
);
1438 goto end_msg_sessiond
;
1440 case LTTNG_CONSUMER_UPDATE_STREAM
:
1445 case LTTNG_CONSUMER_DATA_PENDING
:
1447 int ret
, is_data_pending
;
1448 uint64_t id
= msg
.u
.data_pending
.session_id
;
1450 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1452 is_data_pending
= consumer_data_pending(id
);
1454 /* Send back returned value to session daemon */
1455 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1456 sizeof(is_data_pending
));
1458 DBG("Error when sending the data pending ret code: %d", ret
);
1463 * No need to send back a status message since the data pending
1464 * returned value is the response.
1468 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1471 struct ustctl_consumer_channel_attr attr
;
1472 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1473 const struct lttng_credentials buffer_credentials
= {
1474 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1475 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1478 /* Create a plain object and reserve a channel key. */
1479 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1480 msg
.u
.ask_channel
.chunk_id
.is_set
?
1482 msg
.u
.ask_channel
.pathname
,
1483 msg
.u
.ask_channel
.name
,
1484 msg
.u
.ask_channel
.relayd_id
,
1485 msg
.u
.ask_channel
.key
,
1486 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1487 msg
.u
.ask_channel
.tracefile_size
,
1488 msg
.u
.ask_channel
.tracefile_count
,
1489 msg
.u
.ask_channel
.session_id_per_pid
,
1490 msg
.u
.ask_channel
.monitor
,
1491 msg
.u
.ask_channel
.live_timer_interval
,
1492 msg
.u
.ask_channel
.root_shm_path
,
1493 msg
.u
.ask_channel
.shm_path
);
1495 goto end_channel_error
;
1498 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1499 buffer_credentials
);
1502 * Assign UST application UID to the channel. This value is ignored for
1503 * per PID buffers. This is specific to UST thus setting this after the
1506 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1508 /* Build channel attributes from received message. */
1509 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1510 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1511 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1512 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1513 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1514 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1515 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1516 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1518 /* Match channel buffer type to the UST abi. */
1519 switch (msg
.u
.ask_channel
.output
) {
1520 case LTTNG_EVENT_MMAP
:
1522 attr
.output
= LTTNG_UST_MMAP
;
1526 /* Translate and save channel type. */
1527 switch (msg
.u
.ask_channel
.type
) {
1528 case LTTNG_UST_CHAN_PER_CPU
:
1529 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1530 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1532 * Set refcount to 1 for owner. Below, we will
1533 * pass ownership to the
1534 * consumer_thread_channel_poll() thread.
1536 channel
->refcount
= 1;
1538 case LTTNG_UST_CHAN_METADATA
:
1539 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1540 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1547 health_code_update();
1549 ret
= ask_channel(ctx
, channel
, &attr
);
1551 goto end_channel_error
;
1554 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1555 ret
= consumer_metadata_cache_allocate(channel
);
1557 ERR("Allocating metadata cache");
1558 goto end_channel_error
;
1560 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1561 attr
.switch_timer_interval
= 0;
1563 int monitor_start_ret
;
1565 consumer_timer_live_start(channel
,
1566 msg
.u
.ask_channel
.live_timer_interval
);
1567 monitor_start_ret
= consumer_timer_monitor_start(
1569 msg
.u
.ask_channel
.monitor_timer_interval
);
1570 if (monitor_start_ret
< 0) {
1571 ERR("Starting channel monitoring timer failed");
1572 goto end_channel_error
;
1576 health_code_update();
1579 * Add the channel to the internal state AFTER all streams were created
1580 * and successfully sent to session daemon. This way, all streams must
1581 * be ready before this channel is visible to the threads.
1582 * If add_channel succeeds, ownership of the channel is
1583 * passed to consumer_thread_channel_poll().
1585 ret
= add_channel(channel
, ctx
);
1587 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1588 if (channel
->switch_timer_enabled
== 1) {
1589 consumer_timer_switch_stop(channel
);
1591 consumer_metadata_cache_destroy(channel
);
1593 if (channel
->live_timer_enabled
== 1) {
1594 consumer_timer_live_stop(channel
);
1596 if (channel
->monitor_timer_enabled
== 1) {
1597 consumer_timer_monitor_stop(channel
);
1599 goto end_channel_error
;
1602 health_code_update();
1605 * Channel and streams are now created. Inform the session daemon that
1606 * everything went well and should wait to receive the channel and
1607 * streams with ustctl API.
1609 ret
= consumer_send_status_channel(sock
, channel
);
1612 * There is probably a problem on the socket.
1619 case LTTNG_CONSUMER_GET_CHANNEL
:
1621 int ret
, relayd_err
= 0;
1622 uint64_t key
= msg
.u
.get_channel
.key
;
1623 struct lttng_consumer_channel
*channel
;
1625 channel
= consumer_find_channel(key
);
1627 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1628 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1629 goto end_get_channel
;
1632 health_code_update();
1634 /* Send the channel to sessiond (and relayd, if applicable). */
1635 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1640 * We were unable to send to the relayd the stream so avoid
1641 * sending back a fatal error to the thread since this is OK
1642 * and the consumer can continue its work. The above call
1643 * has sent the error status message to the sessiond.
1645 goto end_get_channel_nosignal
;
1648 * The communicaton was broken hence there is a bad state between
1649 * the consumer and sessiond so stop everything.
1651 goto error_get_channel_fatal
;
1654 health_code_update();
1657 * In no monitor mode, the streams ownership is kept inside the channel
1658 * so don't send them to the data thread.
1660 if (!channel
->monitor
) {
1661 goto end_get_channel
;
1664 ret
= send_streams_to_thread(channel
, ctx
);
1667 * If we are unable to send the stream to the thread, there is
1668 * a big problem so just stop everything.
1670 goto error_get_channel_fatal
;
1672 /* List MUST be empty after or else it could be reused. */
1673 assert(cds_list_empty(&channel
->streams
.head
));
1675 goto end_msg_sessiond
;
1676 error_get_channel_fatal
:
1678 end_get_channel_nosignal
:
1681 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1683 uint64_t key
= msg
.u
.destroy_channel
.key
;
1686 * Only called if streams have not been sent to stream
1687 * manager thread. However, channel has been sent to
1688 * channel manager thread.
1690 notify_thread_del_channel(ctx
, key
);
1691 goto end_msg_sessiond
;
1693 case LTTNG_CONSUMER_CLOSE_METADATA
:
1697 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1702 goto end_msg_sessiond
;
1704 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1708 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1713 goto end_msg_sessiond
;
1715 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1719 ret
= clear_quiescent_channel(
1720 msg
.u
.clear_quiescent_channel
.key
);
1725 goto end_msg_sessiond
;
1727 case LTTNG_CONSUMER_PUSH_METADATA
:
1730 uint64_t len
= msg
.u
.push_metadata
.len
;
1731 uint64_t key
= msg
.u
.push_metadata
.key
;
1732 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1733 uint64_t version
= msg
.u
.push_metadata
.version
;
1734 struct lttng_consumer_channel
*channel
;
1736 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1739 channel
= consumer_find_channel(key
);
1742 * This is possible if the metadata creation on the consumer side
1743 * is in flight vis-a-vis a concurrent push metadata from the
1744 * session daemon. Simply return that the channel failed and the
1745 * session daemon will handle that message correctly considering
1746 * that this race is acceptable thus the DBG() statement here.
1748 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1749 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1750 goto end_push_metadata_msg_sessiond
;
1753 health_code_update();
1757 * There is nothing to receive. We have simply
1758 * checked whether the channel can be found.
1760 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1761 goto end_push_metadata_msg_sessiond
;
1764 /* Tell session daemon we are ready to receive the metadata. */
1765 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1767 /* Somehow, the session daemon is not responding anymore. */
1768 goto error_push_metadata_fatal
;
1771 health_code_update();
1773 /* Wait for more data. */
1774 health_poll_entry();
1775 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1778 goto error_push_metadata_fatal
;
1781 health_code_update();
1783 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1784 len
, version
, channel
, 0, 1);
1786 /* error receiving from sessiond */
1787 goto error_push_metadata_fatal
;
1790 goto end_push_metadata_msg_sessiond
;
1792 end_push_metadata_msg_sessiond
:
1793 goto end_msg_sessiond
;
1794 error_push_metadata_fatal
:
1797 case LTTNG_CONSUMER_SETUP_METADATA
:
1801 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1805 goto end_msg_sessiond
;
1807 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1809 struct lttng_consumer_channel
*channel
;
1810 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1812 channel
= consumer_find_channel(key
);
1814 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1815 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1817 if (msg
.u
.snapshot_channel
.metadata
) {
1818 ret
= snapshot_metadata(channel
, key
,
1819 msg
.u
.snapshot_channel
.pathname
,
1820 msg
.u
.snapshot_channel
.relayd_id
,
1823 ERR("Snapshot metadata failed");
1824 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1827 ret
= snapshot_channel(channel
, key
,
1828 msg
.u
.snapshot_channel
.pathname
,
1829 msg
.u
.snapshot_channel
.relayd_id
,
1830 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1833 ERR("Snapshot channel failed");
1834 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1838 health_code_update();
1839 ret
= consumer_send_status_msg(sock
, ret_code
);
1841 /* Somehow, the session daemon is not responding anymore. */
1844 health_code_update();
1847 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1850 uint64_t discarded_events
;
1851 struct lttng_ht_iter iter
;
1852 struct lttng_ht
*ht
;
1853 struct lttng_consumer_stream
*stream
;
1854 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1855 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1857 DBG("UST consumer discarded events command for session id %"
1860 pthread_mutex_lock(&consumer_data
.lock
);
1862 ht
= consumer_data
.stream_list_ht
;
1865 * We only need a reference to the channel, but they are not
1866 * directly indexed, so we just use the first matching stream
1867 * to extract the information we need, we default to 0 if not
1868 * found (no events are dropped if the channel is not yet in
1871 discarded_events
= 0;
1872 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1873 ht
->hash_fct(&id
, lttng_ht_seed
),
1875 &iter
.iter
, stream
, node_session_id
.node
) {
1876 if (stream
->chan
->key
== key
) {
1877 discarded_events
= stream
->chan
->discarded_events
;
1881 pthread_mutex_unlock(&consumer_data
.lock
);
1884 DBG("UST consumer discarded events command for session id %"
1885 PRIu64
", channel key %" PRIu64
, id
, key
);
1887 health_code_update();
1889 /* Send back returned value to session daemon */
1890 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1892 PERROR("send discarded events");
1898 case LTTNG_CONSUMER_LOST_PACKETS
:
1901 uint64_t lost_packets
;
1902 struct lttng_ht_iter iter
;
1903 struct lttng_ht
*ht
;
1904 struct lttng_consumer_stream
*stream
;
1905 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1906 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1908 DBG("UST consumer lost packets command for session id %"
1911 pthread_mutex_lock(&consumer_data
.lock
);
1913 ht
= consumer_data
.stream_list_ht
;
1916 * We only need a reference to the channel, but they are not
1917 * directly indexed, so we just use the first matching stream
1918 * to extract the information we need, we default to 0 if not
1919 * found (no packets lost if the channel is not yet in use).
1922 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1923 ht
->hash_fct(&id
, lttng_ht_seed
),
1925 &iter
.iter
, stream
, node_session_id
.node
) {
1926 if (stream
->chan
->key
== key
) {
1927 lost_packets
= stream
->chan
->lost_packets
;
1931 pthread_mutex_unlock(&consumer_data
.lock
);
1934 DBG("UST consumer lost packets command for session id %"
1935 PRIu64
", channel key %" PRIu64
, id
, key
);
1937 health_code_update();
1939 /* Send back returned value to session daemon */
1940 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1941 sizeof(lost_packets
));
1943 PERROR("send lost packets");
1949 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1951 int channel_monitor_pipe
;
1953 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1954 /* Successfully received the command's type. */
1955 ret
= consumer_send_status_msg(sock
, ret_code
);
1960 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1962 if (ret
!= sizeof(channel_monitor_pipe
)) {
1963 ERR("Failed to receive channel monitor pipe");
1967 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1968 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1969 channel_monitor_pipe
);
1973 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1974 /* Set the pipe as non-blocking. */
1975 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1977 PERROR("fcntl get flags of the channel monitoring pipe");
1982 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1983 flags
| O_NONBLOCK
);
1985 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1988 DBG("Channel monitor pipe set as non-blocking");
1990 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1992 goto end_msg_sessiond
;
1994 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1996 struct lttng_consumer_channel
*channel
;
1997 uint64_t key
= msg
.u
.rotate_channel
.key
;
1999 channel
= consumer_find_channel(key
);
2001 DBG("Channel %" PRIu64
" not found", key
);
2002 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2005 * Sample the rotate position of all the streams in
2008 ret
= lttng_consumer_rotate_channel(channel
, key
,
2009 msg
.u
.rotate_channel
.relayd_id
,
2010 msg
.u
.rotate_channel
.metadata
,
2013 ERR("Rotate channel failed");
2014 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2017 health_code_update();
2019 ret
= consumer_send_status_msg(sock
, ret_code
);
2021 /* Somehow, the session daemon is not responding anymore. */
2022 goto end_rotate_channel_nosignal
;
2026 * Rotate the streams that are ready right now.
2027 * FIXME: this is a second consecutive iteration over the
2028 * streams in a channel, there is probably a better way to
2029 * handle this, but it needs to be after the
2030 * consumer_send_status_msg() call.
2033 ret
= lttng_consumer_rotate_ready_streams(
2036 ERR("Rotate channel failed");
2040 end_rotate_channel_nosignal
:
2043 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2045 struct lttng_consumer_channel
*channel
;
2046 uint64_t key
= msg
.u
.clear_channel
.key
;
2048 channel
= consumer_find_channel(key
);
2050 DBG("Channel %" PRIu64
" not found", key
);
2051 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2053 ret
= lttng_consumer_clear_channel(channel
);
2055 ERR("Clear channel failed key %" PRIu64
, key
);
2059 health_code_update();
2061 ret
= consumer_send_status_msg(sock
, ret_code
);
2063 /* Somehow, the session daemon is not responding anymore. */
2068 case LTTNG_CONSUMER_INIT
:
2070 ret_code
= lttng_consumer_init_command(ctx
,
2071 msg
.u
.init
.sessiond_uuid
);
2072 health_code_update();
2073 ret
= consumer_send_status_msg(sock
, ret_code
);
2075 /* Somehow, the session daemon is not responding anymore. */
2080 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2082 const struct lttng_credentials credentials
= {
2083 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2084 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2086 const bool is_local_trace
=
2087 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2088 const uint64_t relayd_id
=
2089 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2090 const char *chunk_override_name
=
2091 *msg
.u
.create_trace_chunk
.override_name
?
2092 msg
.u
.create_trace_chunk
.override_name
:
2094 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2097 * The session daemon will only provide a chunk directory file
2098 * descriptor for local traces.
2100 if (is_local_trace
) {
2103 /* Acnowledge the reception of the command. */
2104 ret
= consumer_send_status_msg(sock
,
2105 LTTCOMM_CONSUMERD_SUCCESS
);
2107 /* Somehow, the session daemon is not responding anymore. */
2112 * Receive trace chunk domain dirfd.
2114 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2115 if (ret
!= sizeof(chunk_dirfd
)) {
2116 ERR("Failed to receive trace chunk domain directory file descriptor");
2120 DBG("Received trace chunk domain directory fd (%d)",
2122 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2124 if (!chunk_directory_handle
) {
2125 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2126 if (close(chunk_dirfd
)) {
2127 PERROR("Failed to close chunk directory file descriptor");
2133 ret_code
= lttng_consumer_create_trace_chunk(
2134 !is_local_trace
? &relayd_id
: NULL
,
2135 msg
.u
.create_trace_chunk
.session_id
,
2136 msg
.u
.create_trace_chunk
.chunk_id
,
2137 (time_t) msg
.u
.create_trace_chunk
2138 .creation_timestamp
,
2139 chunk_override_name
,
2140 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2143 chunk_directory_handle
);
2144 lttng_directory_handle_put(chunk_directory_handle
);
2145 goto end_msg_sessiond
;
2147 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2149 enum lttng_trace_chunk_command_type close_command
=
2150 msg
.u
.close_trace_chunk
.close_command
.value
;
2151 const uint64_t relayd_id
=
2152 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2153 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2154 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2157 ret_code
= lttng_consumer_close_trace_chunk(
2158 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2161 msg
.u
.close_trace_chunk
.session_id
,
2162 msg
.u
.close_trace_chunk
.chunk_id
,
2163 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2164 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2166 NULL
, closed_trace_chunk_path
);
2167 reply
.ret_code
= ret_code
;
2168 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2169 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2170 if (ret
!= sizeof(reply
)) {
2173 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2175 if (ret
!= reply
.path_length
) {
2180 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2182 const uint64_t relayd_id
=
2183 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2185 ret_code
= lttng_consumer_trace_chunk_exists(
2186 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2188 msg
.u
.trace_chunk_exists
.session_id
,
2189 msg
.u
.trace_chunk_exists
.chunk_id
);
2190 goto end_msg_sessiond
;
2198 * Return 1 to indicate success since the 0 value can be a socket
2199 * shutdown during the recv() or send() call.
2206 * The returned value here is not useful since either way we'll return 1 to
2207 * the caller because the session daemon socket management is done
2208 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2210 ret
= consumer_send_status_msg(sock
, ret_code
);
2220 * Free channel here since no one has a reference to it. We don't
2221 * free after that because a stream can store this pointer.
2223 destroy_channel(channel
);
2225 /* We have to send a status channel message indicating an error. */
2226 ret
= consumer_send_status_channel(sock
, NULL
);
2228 /* Stop everything if session daemon can not be notified. */
2235 /* This will issue a consumer stop. */
2241 health_code_update();
2245 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2246 int producer_active
)
2249 assert(stream
->ustream
);
2251 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2255 * Take a snapshot for a specific stream.
2257 * Returns 0 on success, < 0 on error
2259 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2262 assert(stream
->ustream
);
2264 return ustctl_snapshot(stream
->ustream
);
2268 * Sample consumed and produced positions for a specific stream.
2270 * Returns 0 on success, < 0 on error.
2272 int lttng_ustconsumer_sample_snapshot_positions(
2273 struct lttng_consumer_stream
*stream
)
2276 assert(stream
->ustream
);
2278 return ustctl_snapshot_sample_positions(stream
->ustream
);
2282 * Get the produced position
2284 * Returns 0 on success, < 0 on error
2286 int lttng_ustconsumer_get_produced_snapshot(
2287 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2290 assert(stream
->ustream
);
2293 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2297 * Get the consumed position
2299 * Returns 0 on success, < 0 on error
2301 int lttng_ustconsumer_get_consumed_snapshot(
2302 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2305 assert(stream
->ustream
);
2308 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2311 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2315 assert(stream
->ustream
);
2317 ustctl_flush_buffer(stream
->ustream
, producer
);
2320 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2323 assert(stream
->ustream
);
2325 ustctl_clear_buffer(stream
->ustream
);
2328 int lttng_ustconsumer_get_current_timestamp(
2329 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2332 assert(stream
->ustream
);
2335 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2338 int lttng_ustconsumer_get_sequence_number(
2339 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2342 assert(stream
->ustream
);
2345 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2349 * Called when the stream signals the consumer that it has hung up.
2351 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2354 assert(stream
->ustream
);
2356 pthread_mutex_lock(&stream
->lock
);
2357 if (!stream
->quiescent
) {
2358 ustctl_flush_buffer(stream
->ustream
, 0);
2359 stream
->quiescent
= true;
2361 pthread_mutex_unlock(&stream
->lock
);
2362 stream
->hangup_flush_done
= 1;
2365 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2370 assert(chan
->uchan
);
2371 assert(chan
->buffer_credentials
.is_set
);
2373 if (chan
->switch_timer_enabled
== 1) {
2374 consumer_timer_switch_stop(chan
);
2376 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2379 ret
= close(chan
->stream_fds
[i
]);
2383 if (chan
->shm_path
[0]) {
2384 char shm_path
[PATH_MAX
];
2386 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2388 ERR("Cannot get stream shm path");
2390 ret
= run_as_unlink(shm_path
,
2391 chan
->buffer_credentials
.value
.uid
,
2392 chan
->buffer_credentials
.value
.gid
);
2394 PERROR("unlink %s", shm_path
);
2400 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2403 assert(chan
->uchan
);
2404 assert(chan
->buffer_credentials
.is_set
);
2406 consumer_metadata_cache_destroy(chan
);
2407 ustctl_destroy_channel(chan
->uchan
);
2408 /* Try to rmdir all directories under shm_path root. */
2409 if (chan
->root_shm_path
[0]) {
2410 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2411 chan
->buffer_credentials
.value
.uid
,
2412 chan
->buffer_credentials
.value
.gid
,
2413 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2415 free(chan
->stream_fds
);
2418 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2421 assert(stream
->ustream
);
2423 if (stream
->chan
->switch_timer_enabled
== 1) {
2424 consumer_timer_switch_stop(stream
->chan
);
2426 ustctl_destroy_stream(stream
->ustream
);
2429 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2432 assert(stream
->ustream
);
2434 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2437 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2440 assert(stream
->ustream
);
2442 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2446 * Populate index values of a UST stream. Values are set in big endian order.
2448 * Return 0 on success or else a negative value.
2450 static int get_index_values(struct ctf_packet_index
*index
,
2451 struct ustctl_consumer_stream
*ustream
)
2454 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
2455 events_discarded
, stream_id
, stream_instance_id
,
2458 ret
= ustctl_get_timestamp_begin(ustream
, ×tamp_begin
);
2460 PERROR("ustctl_get_timestamp_begin");
2464 ret
= ustctl_get_timestamp_end(ustream
, ×tamp_end
);
2466 PERROR("ustctl_get_timestamp_end");
2470 ret
= ustctl_get_events_discarded(ustream
, &events_discarded
);
2472 PERROR("ustctl_get_events_discarded");
2476 ret
= ustctl_get_content_size(ustream
, &content_size
);
2478 PERROR("ustctl_get_content_size");
2482 ret
= ustctl_get_packet_size(ustream
, &packet_size
);
2484 PERROR("ustctl_get_packet_size");
2488 ret
= ustctl_get_stream_id(ustream
, &stream_id
);
2490 PERROR("ustctl_get_stream_id");
2494 ret
= ustctl_get_instance_id(ustream
, &stream_instance_id
);
2496 PERROR("ustctl_get_instance_id");
2500 ret
= ustctl_get_sequence_number(ustream
, &packet_seq_num
);
2502 PERROR("ustctl_get_sequence_number");
2506 *index
= (typeof(*index
)) {
2507 .offset
= index
->offset
,
2508 .packet_size
= htobe64(packet_size
),
2509 .content_size
= htobe64(content_size
),
2510 .timestamp_begin
= htobe64(timestamp_begin
),
2511 .timestamp_end
= htobe64(timestamp_end
),
2512 .events_discarded
= htobe64(events_discarded
),
2513 .stream_id
= htobe64(stream_id
),
2514 .stream_instance_id
= htobe64(stream_instance_id
),
2515 .packet_seq_num
= htobe64(packet_seq_num
),
2523 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2524 struct consumer_metadata_cache
*cache
)
2526 DBG("Metadata stream update to version %" PRIu64
,
2528 stream
->ust_metadata_pushed
= 0;
2529 stream
->metadata_version
= cache
->version
;
2530 stream
->reset_metadata_flag
= 1;
2534 * Check if the version of the metadata stream and metadata cache match.
2535 * If the cache got updated, reset the metadata stream.
2536 * The stream lock and metadata cache lock MUST be held.
2537 * Return 0 on success, a negative value on error.
2540 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2543 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2545 if (cache
->version
== stream
->metadata_version
) {
2548 metadata_stream_reset_cache(stream
, cache
);
2555 * Write up to one packet from the metadata cache to the channel.
2557 * Returns the number of bytes pushed in the cache, or a negative value
2561 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2566 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2567 ret
= metadata_stream_check_version(stream
);
2571 if (stream
->chan
->metadata_cache
->max_offset
2572 == stream
->ust_metadata_pushed
) {
2577 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2578 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2579 stream
->chan
->metadata_cache
->max_offset
2580 - stream
->ust_metadata_pushed
);
2581 assert(write_len
!= 0);
2582 if (write_len
< 0) {
2583 ERR("Writing one metadata packet");
2587 stream
->ust_metadata_pushed
+= write_len
;
2589 assert(stream
->chan
->metadata_cache
->max_offset
>=
2590 stream
->ust_metadata_pushed
);
2594 * Switch packet (but don't open the next one) on every commit of
2595 * a metadata packet. Since the subbuffer is fully filled (with padding,
2596 * if needed), the stream is "quiescent" after this commit.
2598 ustctl_flush_buffer(stream
->ustream
, 1);
2599 stream
->quiescent
= true;
2601 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2607 * Sync metadata meaning request them to the session daemon and snapshot to the
2608 * metadata thread can consumer them.
2610 * Metadata stream lock is held here, but we need to release it when
2611 * interacting with sessiond, else we cause a deadlock with live
2612 * awaiting on metadata to be pushed out.
2614 * The RCU read side lock must be held by the caller.
2616 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2617 * is empty or a negative value on error.
2619 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2620 struct lttng_consumer_stream
*metadata_stream
)
2624 struct lttng_consumer_channel
*metadata_channel
;
2627 assert(metadata_stream
);
2629 metadata_channel
= metadata_stream
->chan
;
2630 pthread_mutex_unlock(&metadata_stream
->lock
);
2632 * Request metadata from the sessiond, but don't wait for the flush
2633 * because we locked the metadata thread.
2635 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2636 pthread_mutex_lock(&metadata_stream
->lock
);
2642 * The metadata stream and channel can be deleted while the
2643 * metadata stream lock was released. The streamed is checked
2644 * for deletion before we use it further.
2646 * Note that it is safe to access a logically-deleted stream since its
2647 * existence is still guaranteed by the RCU read side lock. However,
2648 * it should no longer be used. The close/deletion of the metadata
2649 * channel and stream already guarantees that all metadata has been
2650 * consumed. Therefore, there is nothing left to do in this function.
2652 if (consumer_stream_is_deleted(metadata_stream
)) {
2653 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2654 metadata_stream
->key
);
2659 ret
= commit_one_metadata_packet(metadata_stream
);
2662 } else if (ret
> 0) {
2666 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2668 if (errno
!= EAGAIN
) {
2669 ERR("Sync metadata, taking UST snapshot");
2672 DBG("No new metadata when syncing them.");
2673 /* No new metadata, exit. */
2679 * After this flush, we still need to extract metadata.
2690 * Return 0 on success else a negative value.
2692 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2693 struct lttng_consumer_local_data
*ctx
)
2696 struct ustctl_consumer_stream
*ustream
;
2701 ustream
= stream
->ustream
;
2704 * First, we are going to check if there is a new subbuffer available
2705 * before reading the stream wait_fd.
2707 /* Get the next subbuffer */
2708 ret
= ustctl_get_next_subbuf(ustream
);
2710 /* No more data found, flag the stream. */
2711 stream
->has_data
= 0;
2716 ret
= ustctl_put_subbuf(ustream
);
2719 /* This stream still has data. Flag it and wake up the data thread. */
2720 stream
->has_data
= 1;
2722 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2725 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2726 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2731 /* The wake up pipe has been notified. */
2732 ctx
->has_wakeup
= 1;
2741 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2744 uint64_t seq
, discarded
;
2746 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2748 PERROR("ustctl_get_sequence_number");
2752 * Start the sequence when we extract the first packet in case we don't
2753 * start at 0 (for example if a consumer is not connected to the
2754 * session immediately after the beginning).
2756 if (stream
->last_sequence_number
== -1ULL) {
2757 stream
->last_sequence_number
= seq
;
2758 } else if (seq
> stream
->last_sequence_number
) {
2759 stream
->chan
->lost_packets
+= seq
-
2760 stream
->last_sequence_number
- 1;
2762 /* seq <= last_sequence_number */
2763 ERR("Sequence number inconsistent : prev = %" PRIu64
2764 ", current = %" PRIu64
,
2765 stream
->last_sequence_number
, seq
);
2769 stream
->last_sequence_number
= seq
;
2771 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2773 PERROR("kernctl_get_events_discarded");
2776 if (discarded
< stream
->last_discarded_events
) {
2778 * Overflow has occurred. We assume only one wrap-around
2781 stream
->chan
->discarded_events
+=
2782 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2783 stream
->last_discarded_events
+ discarded
;
2785 stream
->chan
->discarded_events
+= discarded
-
2786 stream
->last_discarded_events
;
2788 stream
->last_discarded_events
= discarded
;
2796 * Read subbuffer from the given stream.
2798 * Stream and channel locks MUST be acquired by the caller.
2800 * Return 0 on success else a negative value.
2802 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2803 struct lttng_consumer_local_data
*ctx
)
2805 unsigned long len
, subbuf_size
, padding
;
2806 int err
, write_index
= 1, rotation_ret
;
2808 struct ustctl_consumer_stream
*ustream
;
2809 struct ctf_packet_index index
;
2810 const char *subbuf_addr
;
2813 assert(stream
->ustream
);
2816 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2819 /* Ease our life for what's next. */
2820 ustream
= stream
->ustream
;
2823 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2824 * error if we cannot read this one byte (read returns 0), or if the error
2825 * is EAGAIN or EWOULDBLOCK.
2827 * This is only done when the stream is monitored by a thread, before the
2828 * flush is done after a hangup and if the stream is not flagged with data
2829 * since there might be nothing to consume in the wait fd but still have
2830 * data available flagged by the consumer wake up pipe.
2832 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2836 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2837 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2844 * If the stream was flagged to be ready for rotation before we extract the
2845 * next packet, rotate it now.
2847 if (stream
->rotate_ready
) {
2848 DBG("Rotate stream before extracting data");
2849 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2850 if (rotation_ret
< 0) {
2851 ERR("Stream rotation error");
2858 /* Get the next subbuffer */
2859 err
= ustctl_get_next_subbuf(ustream
);
2862 * Populate metadata info if the existing info has
2863 * already been read.
2865 if (stream
->metadata_flag
) {
2866 ret
= commit_one_metadata_packet(stream
);
2873 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2875 * This is a debug message even for single-threaded consumer,
2876 * because poll() have more relaxed criterions than get subbuf,
2877 * so get_subbuf may fail for short race windows where poll()
2878 * would issue wakeups.
2880 DBG("Reserving sub buffer failed (everything is normal, "
2881 "it is due to concurrency) [ret: %d]", err
);
2884 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2886 if (!stream
->metadata_flag
) {
2887 index
.offset
= htobe64(stream
->out_fd_offset
);
2888 ret
= get_index_values(&index
, ustream
);
2890 err
= ustctl_put_subbuf(ustream
);
2895 /* Update the stream's sequence and discarded events count. */
2896 ret
= update_stream_stats(stream
);
2898 PERROR("kernctl_get_events_discarded");
2899 err
= ustctl_put_subbuf(ustream
);
2907 /* Get the full padded subbuffer size */
2908 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2911 /* Get subbuffer data size (without padding) */
2912 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2915 /* Make sure we don't get a subbuffer size bigger than the padded */
2916 assert(len
>= subbuf_size
);
2918 padding
= len
- subbuf_size
;
2920 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
2923 goto error_put_subbuf
;
2926 /* write the subbuffer to the tracefile */
2927 ret
= lttng_consumer_on_read_subbuffer_mmap(
2928 ctx
, stream
, subbuf_addr
, subbuf_size
, padding
, &index
);
2930 * The mmap operation should write subbuf_size amount of data when
2931 * network streaming or the full padding (len) size when we are _not_
2934 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2935 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2937 * Display the error but continue processing to try to release the
2938 * subbuffer. This is a DBG statement since any unexpected kill or
2939 * signal, the application gets unregistered, relayd gets closed or
2940 * anything that affects the buffer lifetime will trigger this error.
2941 * So, for the sake of the user, don't print this error since it can
2942 * happen and it is OK with the code flow.
2944 DBG("Error writing to tracefile "
2945 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2946 ret
, len
, subbuf_size
);
2950 err
= ustctl_put_next_subbuf(ustream
);
2954 * This will consumer the byte on the wait_fd if and only if there is not
2955 * next subbuffer to be acquired.
2957 if (!stream
->metadata_flag
) {
2958 ret
= notify_if_more_data(stream
, ctx
);
2964 /* Write index if needed. */
2969 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2971 * In live, block until all the metadata is sent.
2973 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2974 assert(!stream
->missed_metadata_flush
);
2975 stream
->waiting_on_metadata
= true;
2976 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2978 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2980 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2981 stream
->waiting_on_metadata
= false;
2982 if (stream
->missed_metadata_flush
) {
2983 stream
->missed_metadata_flush
= false;
2984 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2985 (void) consumer_flush_ust_index(stream
);
2987 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2995 assert(!stream
->metadata_flag
);
2996 err
= consumer_stream_write_index(stream
, &index
);
3003 * After extracting the packet, we check if the stream is now ready to be
3004 * rotated and perform the action immediately.
3006 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3007 if (rotation_ret
== 1) {
3008 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3009 if (rotation_ret
< 0) {
3010 ERR("Stream rotation error");
3014 } else if (rotation_ret
< 0) {
3015 ERR("Checking if stream is ready to rotate");
3024 * Called when a stream is created.
3026 * Return 0 on success or else a negative value.
3028 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3035 * Don't create anything if this is set for streaming or if there is
3036 * no current trace chunk on the parent channel.
3038 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3039 stream
->chan
->trace_chunk
) {
3040 ret
= consumer_stream_create_output_files(stream
, true);
3052 * Check if data is still being extracted from the buffers for a specific
3053 * stream. Consumer data lock MUST be acquired before calling this function
3054 * and the stream lock.
3056 * Return 1 if the traced data are still getting read else 0 meaning that the
3057 * data is available for trace viewer reading.
3059 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3064 assert(stream
->ustream
);
3066 DBG("UST consumer checking data pending");
3068 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3073 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3074 uint64_t contiguous
, pushed
;
3076 /* Ease our life a bit. */
3077 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3078 pushed
= stream
->ust_metadata_pushed
;
3081 * We can simply check whether all contiguously available data
3082 * has been pushed to the ring buffer, since the push operation
3083 * is performed within get_next_subbuf(), and because both
3084 * get_next_subbuf() and put_next_subbuf() are issued atomically
3085 * thanks to the stream lock within
3086 * lttng_ustconsumer_read_subbuffer(). This basically means that
3087 * whetnever ust_metadata_pushed is incremented, the associated
3088 * metadata has been consumed from the metadata stream.
3090 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3091 contiguous
, pushed
);
3092 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3093 if ((contiguous
!= pushed
) ||
3094 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3095 ret
= 1; /* Data is pending */
3099 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3102 * There is still data so let's put back this
3105 ret
= ustctl_put_subbuf(stream
->ustream
);
3107 ret
= 1; /* Data is pending */
3112 /* Data is NOT pending so ready to be read. */
3120 * Stop a given metadata channel timer if enabled and close the wait fd which
3121 * is the poll pipe of the metadata stream.
3123 * This MUST be called with the metadata channel lock acquired.
3125 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3130 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3132 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3134 if (metadata
->switch_timer_enabled
== 1) {
3135 consumer_timer_switch_stop(metadata
);
3138 if (!metadata
->metadata_stream
) {
3143 * Closing write side so the thread monitoring the stream wakes up if any
3144 * and clean the metadata stream.
3146 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3147 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3149 PERROR("closing metadata pipe write side");
3151 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3159 * Close every metadata stream wait fd of the metadata hash table. This
3160 * function MUST be used very carefully so not to run into a race between the
3161 * metadata thread handling streams and this function closing their wait fd.
3163 * For UST, this is used when the session daemon hangs up. Its the metadata
3164 * producer so calling this is safe because we are assured that no state change
3165 * can occur in the metadata thread for the streams in the hash table.
3167 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3169 struct lttng_ht_iter iter
;