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
12 #include <lttng/ust-ctl.h>
18 #include <sys/socket.h>
20 #include <sys/types.h>
23 #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>
39 #include <common/consumer/consumer.h>
40 #include <common/optional.h>
42 #include "ust-consumer.h"
44 #define INT_MAX_STR_LEN 12 /* includes \0 */
46 extern struct lttng_consumer_global_data consumer_data
;
47 extern int consumer_poll_timeout
;
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
,
67 cds_list_del(&stream
->send_node
);
68 ustctl_destroy_stream(stream
->ustream
);
69 lttng_trace_chunk_put(stream
->trace_chunk
);
74 * If a channel is available meaning that was created before the streams
78 lttng_ustconsumer_del_channel(channel
);
79 lttng_ustconsumer_free_channel(channel
);
82 if (channel
->trace_chunk
) {
83 lttng_trace_chunk_put(channel
->trace_chunk
);
90 * Add channel to internal consumer state.
92 * Returns 0 on success or else a negative value.
94 static int add_channel(struct lttng_consumer_channel
*channel
,
95 struct lttng_consumer_local_data
*ctx
)
102 if (ctx
->on_recv_channel
!= NULL
) {
103 ret
= ctx
->on_recv_channel(channel
);
105 ret
= consumer_add_channel(channel
, ctx
);
106 } else if (ret
< 0) {
107 /* Most likely an ENOMEM. */
108 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
112 ret
= consumer_add_channel(channel
, ctx
);
115 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
122 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
123 * error value if applicable is set in it else it is kept untouched.
125 * Return NULL on error else the newly allocated stream object.
127 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
128 struct lttng_consumer_channel
*channel
,
129 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
132 struct lttng_consumer_stream
*stream
= NULL
;
137 stream
= consumer_stream_create(
144 channel
->trace_chunk
,
149 if (stream
== NULL
) {
153 * We could not find the channel. Can happen if cpu hotplug
154 * happens while tearing down.
156 DBG3("Could not find channel");
161 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
167 consumer_stream_update_channel_attributes(stream
, channel
);
171 *_alloc_ret
= alloc_ret
;
177 * Send the given stream pointer to the corresponding thread.
179 * Returns 0 on success else a negative value.
181 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
182 struct lttng_consumer_local_data
*ctx
)
185 struct lttng_pipe
*stream_pipe
;
187 /* Get the right pipe where the stream will be sent. */
188 if (stream
->metadata_flag
) {
189 consumer_add_metadata_stream(stream
);
190 stream_pipe
= ctx
->consumer_metadata_pipe
;
192 consumer_add_data_stream(stream
);
193 stream_pipe
= ctx
->consumer_data_pipe
;
197 * From this point on, the stream's ownership has been moved away from
198 * the channel and it becomes globally visible. Hence, remove it from
199 * the local stream list to prevent the stream from being both local and
202 stream
->globally_visible
= 1;
203 cds_list_del(&stream
->send_node
);
205 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
207 ERR("Consumer write %s stream to pipe %d",
208 stream
->metadata_flag
? "metadata" : "data",
209 lttng_pipe_get_writefd(stream_pipe
));
210 if (stream
->metadata_flag
) {
211 consumer_del_stream_for_metadata(stream
);
213 consumer_del_stream_for_data(stream
);
223 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
225 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
228 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
229 stream_shm_path
[PATH_MAX
- 1] = '\0';
230 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
235 strncat(stream_shm_path
, cpu_nr
,
236 PATH_MAX
- strlen(stream_shm_path
) - 1);
243 * Create streams for the given channel using liblttng-ust-ctl.
244 * The channel lock must be acquired by the caller.
246 * Return 0 on success else a negative value.
248 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
249 struct lttng_consumer_local_data
*ctx
)
252 struct ustctl_consumer_stream
*ustream
;
253 struct lttng_consumer_stream
*stream
;
254 pthread_mutex_t
*current_stream_lock
= NULL
;
260 * While a stream is available from ustctl. When NULL is returned, we've
261 * reached the end of the possible stream for the channel.
263 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
265 int ust_metadata_pipe
[2];
267 health_code_update();
269 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
270 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
272 ERR("Create ust metadata poll pipe");
275 wait_fd
= ust_metadata_pipe
[0];
277 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
280 /* Allocate consumer stream object. */
281 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
285 stream
->ustream
= ustream
;
287 * Store it so we can save multiple function calls afterwards since
288 * this value is used heavily in the stream threads. This is UST
289 * specific so this is why it's done after allocation.
291 stream
->wait_fd
= wait_fd
;
294 * Increment channel refcount since the channel reference has now been
295 * assigned in the allocation process above.
297 if (stream
->chan
->monitor
) {
298 uatomic_inc(&stream
->chan
->refcount
);
301 pthread_mutex_lock(&stream
->lock
);
302 current_stream_lock
= &stream
->lock
;
304 * Order is important this is why a list is used. On error, the caller
305 * should clean this list.
307 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
309 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
310 &stream
->max_sb_size
);
312 ERR("ustctl_get_max_subbuf_size failed for stream %s",
317 /* Do actions once stream has been received. */
318 if (ctx
->on_recv_stream
) {
319 ret
= ctx
->on_recv_stream(stream
);
325 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
326 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
328 /* Set next CPU stream. */
329 channel
->streams
.count
= ++cpu
;
331 /* Keep stream reference when creating metadata. */
332 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
333 channel
->metadata_stream
= stream
;
334 if (channel
->monitor
) {
335 /* Set metadata poll pipe if we created one */
336 memcpy(stream
->ust_metadata_poll_pipe
,
338 sizeof(ust_metadata_pipe
));
341 pthread_mutex_unlock(&stream
->lock
);
342 current_stream_lock
= NULL
;
349 if (current_stream_lock
) {
350 pthread_mutex_unlock(current_stream_lock
);
356 * create_posix_shm is never called concurrently within a process.
359 int create_posix_shm(void)
361 char tmp_name
[NAME_MAX
];
364 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
370 * Allocate shm, and immediately unlink its shm oject, keeping
371 * only the file descriptor as a reference to the object.
372 * We specifically do _not_ use the / at the beginning of the
373 * pathname so that some OS implementations can keep it local to
374 * the process (POSIX leaves this implementation-defined).
376 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
381 ret
= shm_unlink(tmp_name
);
382 if (ret
< 0 && errno
!= ENOENT
) {
383 PERROR("shm_unlink");
384 goto error_shm_release
;
397 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
398 const struct lttng_credentials
*session_credentials
)
400 char shm_path
[PATH_MAX
];
403 if (!channel
->shm_path
[0]) {
404 return create_posix_shm();
406 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
410 return run_as_open(shm_path
,
411 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
412 lttng_credentials_get_uid(session_credentials
),
413 lttng_credentials_get_gid(session_credentials
));
420 * Create an UST channel with the given attributes and send it to the session
421 * daemon using the ust ctl API.
423 * Return 0 on success or else a negative value.
425 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
426 struct ustctl_consumer_channel_attr
*attr
,
427 struct ustctl_consumer_channel
**ust_chanp
)
429 int ret
, nr_stream_fds
, i
, j
;
431 struct ustctl_consumer_channel
*ust_channel
;
436 assert(channel
->buffer_credentials
.is_set
);
438 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
439 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
440 "switch_timer_interval: %u, read_timer_interval: %u, "
441 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
442 attr
->num_subbuf
, attr
->switch_timer_interval
,
443 attr
->read_timer_interval
, attr
->output
, attr
->type
);
445 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
448 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
449 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
454 for (i
= 0; i
< nr_stream_fds
; i
++) {
455 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
456 &channel
->buffer_credentials
.value
);
457 if (stream_fds
[i
] < 0) {
462 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
467 channel
->nr_stream_fds
= nr_stream_fds
;
468 channel
->stream_fds
= stream_fds
;
469 *ust_chanp
= ust_channel
;
475 for (j
= i
- 1; j
>= 0; j
--) {
478 closeret
= close(stream_fds
[j
]);
482 if (channel
->shm_path
[0]) {
483 char shm_path
[PATH_MAX
];
485 closeret
= get_stream_shm_path(shm_path
,
486 channel
->shm_path
, j
);
488 ERR("Cannot get stream shm path");
490 closeret
= run_as_unlink(shm_path
,
491 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
492 channel
->buffer_credentials
)),
493 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
494 channel
->buffer_credentials
)));
496 PERROR("unlink %s", shm_path
);
500 /* Try to rmdir all directories under shm_path root. */
501 if (channel
->root_shm_path
[0]) {
502 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
503 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
504 channel
->buffer_credentials
)),
505 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
506 channel
->buffer_credentials
)),
507 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
515 * Send a single given stream to the session daemon using the sock.
517 * Return 0 on success else a negative value.
519 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
526 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
528 /* Send stream to session daemon. */
529 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
539 * Send channel to sessiond and relayd if applicable.
541 * Return 0 on success or else a negative value.
543 static int send_channel_to_sessiond_and_relayd(int sock
,
544 struct lttng_consumer_channel
*channel
,
545 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
547 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
548 struct lttng_consumer_stream
*stream
;
549 uint64_t net_seq_idx
= -1ULL;
555 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
557 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
558 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
560 health_code_update();
562 /* Try to send the stream to the relayd if one is available. */
563 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
564 stream
->key
, channel
->name
);
565 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
568 * Flag that the relayd was the problem here probably due to a
569 * communicaton error on the socket.
574 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
576 if (net_seq_idx
== -1ULL) {
577 net_seq_idx
= stream
->net_seq_idx
;
582 /* Inform sessiond that we are about to send channel and streams. */
583 ret
= consumer_send_status_msg(sock
, ret_code
);
584 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
586 * Either the session daemon is not responding or the relayd died so we
592 /* Send channel to sessiond. */
593 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
598 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
603 /* The channel was sent successfully to the sessiond at this point. */
604 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
606 health_code_update();
608 /* Send stream to session daemon. */
609 ret
= send_sessiond_stream(sock
, stream
);
615 /* Tell sessiond there is no more stream. */
616 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
621 DBG("UST consumer NULL stream sent to sessiond");
626 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
633 * Creates a channel and streams and add the channel it to the channel internal
634 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
637 * Return 0 on success or else, a negative value is returned and the channel
638 * MUST be destroyed by consumer_del_channel().
640 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
641 struct lttng_consumer_channel
*channel
,
642 struct ustctl_consumer_channel_attr
*attr
)
651 * This value is still used by the kernel consumer since for the kernel,
652 * the stream ownership is not IN the consumer so we need to have the
653 * number of left stream that needs to be initialized so we can know when
654 * to delete the channel (see consumer.c).
656 * As for the user space tracer now, the consumer creates and sends the
657 * stream to the session daemon which only sends them to the application
658 * once every stream of a channel is received making this value useless
659 * because we they will be added to the poll thread before the application
660 * receives them. This ensures that a stream can not hang up during
661 * initilization of a channel.
663 channel
->nb_init_stream_left
= 0;
665 /* The reply msg status is handled in the following call. */
666 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
671 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
674 * For the snapshots (no monitor), we create the metadata streams
675 * on demand, not during the channel creation.
677 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
682 /* Open all streams for this channel. */
683 pthread_mutex_lock(&channel
->lock
);
684 ret
= create_ust_streams(channel
, ctx
);
685 pthread_mutex_unlock(&channel
->lock
);
695 * Send all stream of a channel to the right thread handling it.
697 * On error, return a negative value else 0 on success.
699 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
700 struct lttng_consumer_local_data
*ctx
)
703 struct lttng_consumer_stream
*stream
, *stmp
;
708 /* Send streams to the corresponding thread. */
709 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
712 health_code_update();
714 /* Sending the stream to the thread. */
715 ret
= send_stream_to_thread(stream
, ctx
);
718 * If we are unable to send the stream to the thread, there is
719 * a big problem so just stop everything.
730 * Flush channel's streams using the given key to retrieve the channel.
732 * Return 0 on success else an LTTng error code.
734 static int flush_channel(uint64_t chan_key
)
737 struct lttng_consumer_channel
*channel
;
738 struct lttng_consumer_stream
*stream
;
740 struct lttng_ht_iter iter
;
742 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
745 channel
= consumer_find_channel(chan_key
);
747 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
748 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
752 ht
= consumer_data
.stream_per_chan_id_ht
;
754 /* For each stream of the channel id, flush it. */
755 cds_lfht_for_each_entry_duplicate(ht
->ht
,
756 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
757 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
759 health_code_update();
761 pthread_mutex_lock(&stream
->lock
);
764 * Protect against concurrent teardown of a stream.
766 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
770 if (!stream
->quiescent
) {
771 ustctl_flush_buffer(stream
->ustream
, 0);
772 stream
->quiescent
= true;
775 pthread_mutex_unlock(&stream
->lock
);
783 * Clear quiescent state from channel's streams using the given key to
784 * retrieve the channel.
786 * Return 0 on success else an LTTng error code.
788 static int clear_quiescent_channel(uint64_t chan_key
)
791 struct lttng_consumer_channel
*channel
;
792 struct lttng_consumer_stream
*stream
;
794 struct lttng_ht_iter iter
;
796 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
799 channel
= consumer_find_channel(chan_key
);
801 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
802 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
806 ht
= consumer_data
.stream_per_chan_id_ht
;
808 /* For each stream of the channel id, clear quiescent state. */
809 cds_lfht_for_each_entry_duplicate(ht
->ht
,
810 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
811 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
813 health_code_update();
815 pthread_mutex_lock(&stream
->lock
);
816 stream
->quiescent
= false;
817 pthread_mutex_unlock(&stream
->lock
);
825 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
827 * Return 0 on success else an LTTng error code.
829 static int close_metadata(uint64_t chan_key
)
832 struct lttng_consumer_channel
*channel
;
833 unsigned int channel_monitor
;
835 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
837 channel
= consumer_find_channel(chan_key
);
840 * This is possible if the metadata thread has issue a delete because
841 * the endpoint point of the stream hung up. There is no way the
842 * session daemon can know about it thus use a DBG instead of an actual
845 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
846 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
850 pthread_mutex_lock(&consumer_data
.lock
);
851 pthread_mutex_lock(&channel
->lock
);
852 channel_monitor
= channel
->monitor
;
853 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
857 lttng_ustconsumer_close_metadata(channel
);
858 pthread_mutex_unlock(&channel
->lock
);
859 pthread_mutex_unlock(&consumer_data
.lock
);
862 * The ownership of a metadata channel depends on the type of
863 * session to which it belongs. In effect, the monitor flag is checked
864 * to determine if this metadata channel is in "snapshot" mode or not.
866 * In the non-snapshot case, the metadata channel is created along with
867 * a single stream which will remain present until the metadata channel
868 * is destroyed (on the destruction of its session). In this case, the
869 * metadata stream in "monitored" by the metadata poll thread and holds
870 * the ownership of its channel.
872 * Closing the metadata will cause the metadata stream's "metadata poll
873 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
874 * thread which will teardown the metadata stream which, in return,
875 * deletes the metadata channel.
877 * In the snapshot case, the metadata stream is created and destroyed
878 * on every snapshot record. Since the channel doesn't have an owner
879 * other than the session daemon, it is safe to destroy it immediately
880 * on reception of the CLOSE_METADATA command.
882 if (!channel_monitor
) {
884 * The channel and consumer_data locks must be
885 * released before this call since consumer_del_channel
886 * re-acquires the channel and consumer_data locks to teardown
887 * the channel and queue its reclamation by the "call_rcu"
890 consumer_del_channel(channel
);
895 pthread_mutex_unlock(&channel
->lock
);
896 pthread_mutex_unlock(&consumer_data
.lock
);
902 * RCU read side lock MUST be acquired before calling this function.
904 * Return 0 on success else an LTTng error code.
906 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
909 struct lttng_consumer_channel
*metadata
;
911 DBG("UST consumer setup metadata key %" PRIu64
, key
);
913 metadata
= consumer_find_channel(key
);
915 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
916 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
921 * In no monitor mode, the metadata channel has no stream(s) so skip the
922 * ownership transfer to the metadata thread.
924 if (!metadata
->monitor
) {
925 DBG("Metadata channel in no monitor");
931 * Send metadata stream to relayd if one available. Availability is
932 * known if the stream is still in the list of the channel.
934 if (cds_list_empty(&metadata
->streams
.head
)) {
935 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
936 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
937 goto error_no_stream
;
940 /* Send metadata stream to relayd if needed. */
941 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
942 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
945 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
948 ret
= consumer_send_relayd_streams_sent(
949 metadata
->metadata_stream
->net_seq_idx
);
951 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
957 * Ownership of metadata stream is passed along. Freeing is handled by
960 ret
= send_streams_to_thread(metadata
, ctx
);
963 * If we are unable to send the stream to the thread, there is
964 * a big problem so just stop everything.
966 ret
= LTTCOMM_CONSUMERD_FATAL
;
967 goto send_streams_error
;
969 /* List MUST be empty after or else it could be reused. */
970 assert(cds_list_empty(&metadata
->streams
.head
));
977 * Delete metadata channel on error. At this point, the metadata stream can
978 * NOT be monitored by the metadata thread thus having the guarantee that
979 * the stream is still in the local stream list of the channel. This call
980 * will make sure to clean that list.
982 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
983 cds_list_del(&metadata
->metadata_stream
->send_node
);
984 metadata
->metadata_stream
= NULL
;
992 * Snapshot the whole metadata.
993 * RCU read-side lock must be held by the caller.
995 * Returns 0 on success, < 0 on error
997 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
998 uint64_t key
, char *path
, uint64_t relayd_id
,
999 struct lttng_consumer_local_data
*ctx
)
1002 struct lttng_consumer_stream
*metadata_stream
;
1007 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1012 assert(!metadata_channel
->monitor
);
1014 health_code_update();
1017 * Ask the sessiond if we have new metadata waiting and update the
1018 * consumer metadata cache.
1020 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1025 health_code_update();
1028 * The metadata stream is NOT created in no monitor mode when the channel
1029 * is created on a sessiond ask channel command.
1031 ret
= create_ust_streams(metadata_channel
, ctx
);
1036 metadata_stream
= metadata_channel
->metadata_stream
;
1037 assert(metadata_stream
);
1039 pthread_mutex_lock(&metadata_stream
->lock
);
1040 if (relayd_id
!= (uint64_t) -1ULL) {
1041 metadata_stream
->net_seq_idx
= relayd_id
;
1042 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1044 ret
= consumer_stream_create_output_files(metadata_stream
,
1047 pthread_mutex_unlock(&metadata_stream
->lock
);
1053 health_code_update();
1055 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1063 * Clean up the stream completly because the next snapshot will use a new
1066 consumer_stream_destroy(metadata_stream
, NULL
);
1067 cds_list_del(&metadata_stream
->send_node
);
1068 metadata_channel
->metadata_stream
= NULL
;
1076 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1080 unsigned long mmap_offset
;
1081 const char *mmap_base
;
1083 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1085 ERR("Failed to get mmap base for stream `%s`",
1091 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1093 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1098 *addr
= mmap_base
+ mmap_offset
;
1105 * Take a snapshot of all the stream of a channel.
1106 * RCU read-side lock and the channel lock must be held by the caller.
1108 * Returns 0 on success, < 0 on error
1110 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1111 uint64_t key
, char *path
, uint64_t relayd_id
,
1112 uint64_t nb_packets_per_stream
,
1113 struct lttng_consumer_local_data
*ctx
)
1116 unsigned use_relayd
= 0;
1117 unsigned long consumed_pos
, produced_pos
;
1118 struct lttng_consumer_stream
*stream
;
1125 if (relayd_id
!= (uint64_t) -1ULL) {
1129 assert(!channel
->monitor
);
1130 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1132 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1133 health_code_update();
1135 /* Lock stream because we are about to change its state. */
1136 pthread_mutex_lock(&stream
->lock
);
1137 assert(channel
->trace_chunk
);
1138 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1140 * Can't happen barring an internal error as the channel
1141 * holds a reference to the trace chunk.
1143 ERR("Failed to acquire reference to channel's trace chunk");
1147 assert(!stream
->trace_chunk
);
1148 stream
->trace_chunk
= channel
->trace_chunk
;
1150 stream
->net_seq_idx
= relayd_id
;
1153 ret
= consumer_send_relayd_stream(stream
, path
);
1158 ret
= consumer_stream_create_output_files(stream
,
1163 DBG("UST consumer snapshot stream (%" PRIu64
")",
1168 * If tracing is active, we want to perform a "full" buffer flush.
1169 * Else, if quiescent, it has already been done by the prior stop.
1171 if (!stream
->quiescent
) {
1172 ustctl_flush_buffer(stream
->ustream
, 0);
1175 ret
= lttng_ustconsumer_take_snapshot(stream
);
1177 ERR("Taking UST snapshot");
1181 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1183 ERR("Produced UST snapshot position");
1187 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1189 ERR("Consumerd UST snapshot position");
1194 * The original value is sent back if max stream size is larger than
1195 * the possible size of the snapshot. Also, we assume that the session
1196 * daemon should never send a maximum stream size that is lower than
1199 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1200 produced_pos
, nb_packets_per_stream
,
1201 stream
->max_sb_size
);
1203 while ((long) (consumed_pos
- produced_pos
) < 0) {
1205 unsigned long len
, padded_len
;
1206 const char *subbuf_addr
;
1207 struct lttng_buffer_view subbuf_view
;
1209 health_code_update();
1211 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1213 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1215 if (ret
!= -EAGAIN
) {
1216 PERROR("ustctl_get_subbuf snapshot");
1217 goto error_close_stream
;
1219 DBG("UST consumer get subbuf failed. Skipping it.");
1220 consumed_pos
+= stream
->max_sb_size
;
1221 stream
->chan
->lost_packets
++;
1225 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1227 ERR("Snapshot ustctl_get_subbuf_size");
1228 goto error_put_subbuf
;
1231 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1233 ERR("Snapshot ustctl_get_padded_subbuf_size");
1234 goto error_put_subbuf
;
1237 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1239 goto error_put_subbuf
;
1242 subbuf_view
= lttng_buffer_view_init(
1243 subbuf_addr
, 0, padded_len
);
1244 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1245 stream
, &subbuf_view
, padded_len
- len
);
1247 if (read_len
!= len
) {
1249 goto error_put_subbuf
;
1252 if (read_len
!= padded_len
) {
1254 goto error_put_subbuf
;
1258 ret
= ustctl_put_subbuf(stream
->ustream
);
1260 ERR("Snapshot ustctl_put_subbuf");
1261 goto error_close_stream
;
1263 consumed_pos
+= stream
->max_sb_size
;
1266 /* Simply close the stream so we can use it on the next snapshot. */
1267 consumer_stream_close(stream
);
1268 pthread_mutex_unlock(&stream
->lock
);
1275 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1276 ERR("Snapshot ustctl_put_subbuf");
1279 consumer_stream_close(stream
);
1281 pthread_mutex_unlock(&stream
->lock
);
1287 void metadata_stream_reset_cache_consumed_position(
1288 struct lttng_consumer_stream
*stream
)
1290 ASSERT_LOCKED(stream
->lock
);
1292 DBG("Reset metadata cache of session %" PRIu64
,
1293 stream
->chan
->session_id
);
1294 stream
->ust_metadata_pushed
= 0;
1298 * Receive the metadata updates from the sessiond. Supports receiving
1299 * overlapping metadata, but is needs to always belong to a contiguous
1300 * range starting from 0.
1301 * Be careful about the locks held when calling this function: it needs
1302 * the metadata cache flush to concurrently progress in order to
1305 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1306 uint64_t len
, uint64_t version
,
1307 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1309 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1311 enum consumer_metadata_cache_write_status cache_write_status
;
1313 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1315 metadata_str
= zmalloc(len
* sizeof(char));
1316 if (!metadata_str
) {
1317 PERROR("zmalloc metadata string");
1318 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1322 health_code_update();
1324 /* Receive metadata string. */
1325 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1327 /* Session daemon is dead so return gracefully. */
1332 health_code_update();
1334 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1335 cache_write_status
= consumer_metadata_cache_write(
1336 channel
, offset
, len
, version
, metadata_str
);
1337 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1338 switch (cache_write_status
) {
1339 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1341 * The write entirely overlapped with existing contents of the
1342 * same metadata version (same content); there is nothing to do.
1345 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1347 * The metadata cache was invalidated (previously pushed
1348 * content has been overwritten). Reset the stream's consumed
1349 * metadata position to ensure the metadata poll thread consumes
1352 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1353 metadata_stream_reset_cache_consumed_position(
1354 channel
->metadata_stream
);
1355 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1357 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1359 * In both cases, the metadata poll thread has new data to
1362 ret
= consumer_metadata_wakeup_pipe(channel
);
1364 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1368 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1369 /* Unable to handle metadata. Notify session daemon. */
1370 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1372 * Skip metadata flush on write error since the offset and len might
1373 * not have been updated which could create an infinite loop below when
1374 * waiting for the metadata cache to be flushed.
1384 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1385 DBG("Waiting for metadata to be flushed");
1387 health_code_update();
1389 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1399 * Receive command from session daemon and process it.
1401 * Return 1 on success else a negative value or 0.
1403 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1404 int sock
, struct pollfd
*consumer_sockpoll
)
1407 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1408 struct lttcomm_consumer_msg msg
;
1409 struct lttng_consumer_channel
*channel
= NULL
;
1411 health_code_update();
1413 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1414 if (ret
!= sizeof(msg
)) {
1415 DBG("Consumer received unexpected message size %zd (expects %zu)",
1418 * The ret value might 0 meaning an orderly shutdown but this is ok
1419 * since the caller handles this.
1422 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1428 health_code_update();
1431 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1433 health_code_update();
1435 /* relayd needs RCU read-side lock */
1438 switch (msg
.cmd_type
) {
1439 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1441 /* Session daemon status message are handled in the following call. */
1442 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1443 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1444 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1445 msg
.u
.relayd_sock
.relayd_session_id
);
1448 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1450 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1451 struct consumer_relayd_sock_pair
*relayd
;
1453 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1455 /* Get relayd reference if exists. */
1456 relayd
= consumer_find_relayd(index
);
1457 if (relayd
== NULL
) {
1458 DBG("Unable to find relayd %" PRIu64
, index
);
1459 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1463 * Each relayd socket pair has a refcount of stream attached to it
1464 * which tells if the relayd is still active or not depending on the
1467 * This will set the destroy flag of the relayd object and destroy it
1468 * if the refcount reaches zero when called.
1470 * The destroy can happen either here or when a stream fd hangs up.
1473 consumer_flag_relayd_for_destroy(relayd
);
1476 goto end_msg_sessiond
;
1478 case LTTNG_CONSUMER_UPDATE_STREAM
:
1483 case LTTNG_CONSUMER_DATA_PENDING
:
1485 int ret
, is_data_pending
;
1486 uint64_t id
= msg
.u
.data_pending
.session_id
;
1488 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1490 is_data_pending
= consumer_data_pending(id
);
1492 /* Send back returned value to session daemon */
1493 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1494 sizeof(is_data_pending
));
1496 DBG("Error when sending the data pending ret code: %d", ret
);
1501 * No need to send back a status message since the data pending
1502 * returned value is the response.
1506 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1509 struct ustctl_consumer_channel_attr attr
;
1510 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1511 const struct lttng_credentials buffer_credentials
= {
1512 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1513 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1516 /* Create a plain object and reserve a channel key. */
1517 channel
= consumer_allocate_channel(
1518 msg
.u
.ask_channel
.key
,
1519 msg
.u
.ask_channel
.session_id
,
1520 msg
.u
.ask_channel
.chunk_id
.is_set
?
1522 msg
.u
.ask_channel
.pathname
,
1523 msg
.u
.ask_channel
.name
,
1524 msg
.u
.ask_channel
.relayd_id
,
1525 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1526 msg
.u
.ask_channel
.tracefile_size
,
1527 msg
.u
.ask_channel
.tracefile_count
,
1528 msg
.u
.ask_channel
.session_id_per_pid
,
1529 msg
.u
.ask_channel
.monitor
,
1530 msg
.u
.ask_channel
.live_timer_interval
,
1531 msg
.u
.ask_channel
.is_live
,
1532 msg
.u
.ask_channel
.root_shm_path
,
1533 msg
.u
.ask_channel
.shm_path
);
1535 goto end_channel_error
;
1538 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1539 buffer_credentials
);
1542 * Assign UST application UID to the channel. This value is ignored for
1543 * per PID buffers. This is specific to UST thus setting this after the
1546 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1548 /* Build channel attributes from received message. */
1549 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1550 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1551 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1552 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1553 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1554 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1555 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1556 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1558 /* Match channel buffer type to the UST abi. */
1559 switch (msg
.u
.ask_channel
.output
) {
1560 case LTTNG_EVENT_MMAP
:
1562 attr
.output
= LTTNG_UST_MMAP
;
1566 /* Translate and save channel type. */
1567 switch (msg
.u
.ask_channel
.type
) {
1568 case LTTNG_UST_CHAN_PER_CPU
:
1569 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1570 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1572 * Set refcount to 1 for owner. Below, we will
1573 * pass ownership to the
1574 * consumer_thread_channel_poll() thread.
1576 channel
->refcount
= 1;
1578 case LTTNG_UST_CHAN_METADATA
:
1579 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1580 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1587 health_code_update();
1589 ret
= ask_channel(ctx
, channel
, &attr
);
1591 goto end_channel_error
;
1594 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1595 ret
= consumer_metadata_cache_allocate(channel
);
1597 ERR("Allocating metadata cache");
1598 goto end_channel_error
;
1600 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1601 attr
.switch_timer_interval
= 0;
1603 int monitor_start_ret
;
1605 consumer_timer_live_start(channel
,
1606 msg
.u
.ask_channel
.live_timer_interval
);
1607 monitor_start_ret
= consumer_timer_monitor_start(
1609 msg
.u
.ask_channel
.monitor_timer_interval
);
1610 if (monitor_start_ret
< 0) {
1611 ERR("Starting channel monitoring timer failed");
1612 goto end_channel_error
;
1616 health_code_update();
1619 * Add the channel to the internal state AFTER all streams were created
1620 * and successfully sent to session daemon. This way, all streams must
1621 * be ready before this channel is visible to the threads.
1622 * If add_channel succeeds, ownership of the channel is
1623 * passed to consumer_thread_channel_poll().
1625 ret
= add_channel(channel
, ctx
);
1627 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1628 if (channel
->switch_timer_enabled
== 1) {
1629 consumer_timer_switch_stop(channel
);
1631 consumer_metadata_cache_destroy(channel
);
1633 if (channel
->live_timer_enabled
== 1) {
1634 consumer_timer_live_stop(channel
);
1636 if (channel
->monitor_timer_enabled
== 1) {
1637 consumer_timer_monitor_stop(channel
);
1639 goto end_channel_error
;
1642 health_code_update();
1645 * Channel and streams are now created. Inform the session daemon that
1646 * everything went well and should wait to receive the channel and
1647 * streams with ustctl API.
1649 ret
= consumer_send_status_channel(sock
, channel
);
1652 * There is probably a problem on the socket.
1659 case LTTNG_CONSUMER_GET_CHANNEL
:
1661 int ret
, relayd_err
= 0;
1662 uint64_t key
= msg
.u
.get_channel
.key
;
1663 struct lttng_consumer_channel
*channel
;
1665 channel
= consumer_find_channel(key
);
1667 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1668 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1669 goto end_get_channel
;
1672 health_code_update();
1674 /* Send the channel to sessiond (and relayd, if applicable). */
1675 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1680 * We were unable to send to the relayd the stream so avoid
1681 * sending back a fatal error to the thread since this is OK
1682 * and the consumer can continue its work. The above call
1683 * has sent the error status message to the sessiond.
1685 goto end_get_channel_nosignal
;
1688 * The communicaton was broken hence there is a bad state between
1689 * the consumer and sessiond so stop everything.
1691 goto error_get_channel_fatal
;
1694 health_code_update();
1697 * In no monitor mode, the streams ownership is kept inside the channel
1698 * so don't send them to the data thread.
1700 if (!channel
->monitor
) {
1701 goto end_get_channel
;
1704 ret
= send_streams_to_thread(channel
, ctx
);
1707 * If we are unable to send the stream to the thread, there is
1708 * a big problem so just stop everything.
1710 goto error_get_channel_fatal
;
1712 /* List MUST be empty after or else it could be reused. */
1713 assert(cds_list_empty(&channel
->streams
.head
));
1715 goto end_msg_sessiond
;
1716 error_get_channel_fatal
:
1718 end_get_channel_nosignal
:
1721 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1723 uint64_t key
= msg
.u
.destroy_channel
.key
;
1726 * Only called if streams have not been sent to stream
1727 * manager thread. However, channel has been sent to
1728 * channel manager thread.
1730 notify_thread_del_channel(ctx
, key
);
1731 goto end_msg_sessiond
;
1733 case LTTNG_CONSUMER_CLOSE_METADATA
:
1737 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1742 goto end_msg_sessiond
;
1744 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1748 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1753 goto end_msg_sessiond
;
1755 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1759 ret
= clear_quiescent_channel(
1760 msg
.u
.clear_quiescent_channel
.key
);
1765 goto end_msg_sessiond
;
1767 case LTTNG_CONSUMER_PUSH_METADATA
:
1770 uint64_t len
= msg
.u
.push_metadata
.len
;
1771 uint64_t key
= msg
.u
.push_metadata
.key
;
1772 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1773 uint64_t version
= msg
.u
.push_metadata
.version
;
1774 struct lttng_consumer_channel
*channel
;
1776 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1779 channel
= consumer_find_channel(key
);
1782 * This is possible if the metadata creation on the consumer side
1783 * is in flight vis-a-vis a concurrent push metadata from the
1784 * session daemon. Simply return that the channel failed and the
1785 * session daemon will handle that message correctly considering
1786 * that this race is acceptable thus the DBG() statement here.
1788 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1789 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1790 goto end_push_metadata_msg_sessiond
;
1793 health_code_update();
1797 * There is nothing to receive. We have simply
1798 * checked whether the channel can be found.
1800 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1801 goto end_push_metadata_msg_sessiond
;
1804 /* Tell session daemon we are ready to receive the metadata. */
1805 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1807 /* Somehow, the session daemon is not responding anymore. */
1808 goto error_push_metadata_fatal
;
1811 health_code_update();
1813 /* Wait for more data. */
1814 health_poll_entry();
1815 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1818 goto error_push_metadata_fatal
;
1821 health_code_update();
1823 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1824 len
, version
, channel
, 0, 1);
1826 /* error receiving from sessiond */
1827 goto error_push_metadata_fatal
;
1830 goto end_push_metadata_msg_sessiond
;
1832 end_push_metadata_msg_sessiond
:
1833 goto end_msg_sessiond
;
1834 error_push_metadata_fatal
:
1837 case LTTNG_CONSUMER_SETUP_METADATA
:
1841 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1845 goto end_msg_sessiond
;
1847 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1849 struct lttng_consumer_channel
*channel
;
1850 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1852 channel
= consumer_find_channel(key
);
1854 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1855 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1857 if (msg
.u
.snapshot_channel
.metadata
) {
1858 ret
= snapshot_metadata(channel
, key
,
1859 msg
.u
.snapshot_channel
.pathname
,
1860 msg
.u
.snapshot_channel
.relayd_id
,
1863 ERR("Snapshot metadata failed");
1864 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1867 ret
= snapshot_channel(channel
, key
,
1868 msg
.u
.snapshot_channel
.pathname
,
1869 msg
.u
.snapshot_channel
.relayd_id
,
1870 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1873 ERR("Snapshot channel failed");
1874 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1878 health_code_update();
1879 ret
= consumer_send_status_msg(sock
, ret_code
);
1881 /* Somehow, the session daemon is not responding anymore. */
1884 health_code_update();
1887 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1890 uint64_t discarded_events
;
1891 struct lttng_ht_iter iter
;
1892 struct lttng_ht
*ht
;
1893 struct lttng_consumer_stream
*stream
;
1894 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1895 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1897 DBG("UST consumer discarded events command for session id %"
1900 pthread_mutex_lock(&consumer_data
.lock
);
1902 ht
= consumer_data
.stream_list_ht
;
1905 * We only need a reference to the channel, but they are not
1906 * directly indexed, so we just use the first matching stream
1907 * to extract the information we need, we default to 0 if not
1908 * found (no events are dropped if the channel is not yet in
1911 discarded_events
= 0;
1912 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1913 ht
->hash_fct(&id
, lttng_ht_seed
),
1915 &iter
.iter
, stream
, node_session_id
.node
) {
1916 if (stream
->chan
->key
== key
) {
1917 discarded_events
= stream
->chan
->discarded_events
;
1921 pthread_mutex_unlock(&consumer_data
.lock
);
1924 DBG("UST consumer discarded events command for session id %"
1925 PRIu64
", channel key %" PRIu64
, id
, key
);
1927 health_code_update();
1929 /* Send back returned value to session daemon */
1930 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1932 PERROR("send discarded events");
1938 case LTTNG_CONSUMER_LOST_PACKETS
:
1941 uint64_t lost_packets
;
1942 struct lttng_ht_iter iter
;
1943 struct lttng_ht
*ht
;
1944 struct lttng_consumer_stream
*stream
;
1945 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1946 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1948 DBG("UST consumer lost packets command for session id %"
1951 pthread_mutex_lock(&consumer_data
.lock
);
1953 ht
= consumer_data
.stream_list_ht
;
1956 * We only need a reference to the channel, but they are not
1957 * directly indexed, so we just use the first matching stream
1958 * to extract the information we need, we default to 0 if not
1959 * found (no packets lost if the channel is not yet in use).
1962 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1963 ht
->hash_fct(&id
, lttng_ht_seed
),
1965 &iter
.iter
, stream
, node_session_id
.node
) {
1966 if (stream
->chan
->key
== key
) {
1967 lost_packets
= stream
->chan
->lost_packets
;
1971 pthread_mutex_unlock(&consumer_data
.lock
);
1974 DBG("UST consumer lost packets command for session id %"
1975 PRIu64
", channel key %" PRIu64
, id
, key
);
1977 health_code_update();
1979 /* Send back returned value to session daemon */
1980 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1981 sizeof(lost_packets
));
1983 PERROR("send lost packets");
1989 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1991 int channel_monitor_pipe
;
1993 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1994 /* Successfully received the command's type. */
1995 ret
= consumer_send_status_msg(sock
, ret_code
);
2000 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
2002 if (ret
!= sizeof(channel_monitor_pipe
)) {
2003 ERR("Failed to receive channel monitor pipe");
2007 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
2008 ret
= consumer_timer_thread_set_channel_monitor_pipe(
2009 channel_monitor_pipe
);
2013 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2014 /* Set the pipe as non-blocking. */
2015 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
2017 PERROR("fcntl get flags of the channel monitoring pipe");
2022 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
2023 flags
| O_NONBLOCK
);
2025 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2028 DBG("Channel monitor pipe set as non-blocking");
2030 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2032 goto end_msg_sessiond
;
2034 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2036 struct lttng_consumer_channel
*channel
;
2037 uint64_t key
= msg
.u
.rotate_channel
.key
;
2039 channel
= consumer_find_channel(key
);
2041 DBG("Channel %" PRIu64
" not found", key
);
2042 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2045 * Sample the rotate position of all the streams in
2048 ret
= lttng_consumer_rotate_channel(channel
, key
,
2049 msg
.u
.rotate_channel
.relayd_id
,
2050 msg
.u
.rotate_channel
.metadata
,
2053 ERR("Rotate channel failed");
2054 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2057 health_code_update();
2059 ret
= consumer_send_status_msg(sock
, ret_code
);
2061 /* Somehow, the session daemon is not responding anymore. */
2062 goto end_rotate_channel_nosignal
;
2066 * Rotate the streams that are ready right now.
2067 * FIXME: this is a second consecutive iteration over the
2068 * streams in a channel, there is probably a better way to
2069 * handle this, but it needs to be after the
2070 * consumer_send_status_msg() call.
2073 ret
= lttng_consumer_rotate_ready_streams(
2076 ERR("Rotate channel failed");
2080 end_rotate_channel_nosignal
:
2083 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2085 struct lttng_consumer_channel
*channel
;
2086 uint64_t key
= msg
.u
.clear_channel
.key
;
2088 channel
= consumer_find_channel(key
);
2090 DBG("Channel %" PRIu64
" not found", key
);
2091 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2093 ret
= lttng_consumer_clear_channel(channel
);
2095 ERR("Clear channel failed key %" PRIu64
, key
);
2099 health_code_update();
2101 ret
= consumer_send_status_msg(sock
, ret_code
);
2103 /* Somehow, the session daemon is not responding anymore. */
2108 case LTTNG_CONSUMER_INIT
:
2110 ret_code
= lttng_consumer_init_command(ctx
,
2111 msg
.u
.init
.sessiond_uuid
);
2112 health_code_update();
2113 ret
= consumer_send_status_msg(sock
, ret_code
);
2115 /* Somehow, the session daemon is not responding anymore. */
2120 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2122 const struct lttng_credentials credentials
= {
2123 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2124 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2126 const bool is_local_trace
=
2127 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2128 const uint64_t relayd_id
=
2129 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2130 const char *chunk_override_name
=
2131 *msg
.u
.create_trace_chunk
.override_name
?
2132 msg
.u
.create_trace_chunk
.override_name
:
2134 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2137 * The session daemon will only provide a chunk directory file
2138 * descriptor for local traces.
2140 if (is_local_trace
) {
2143 /* Acnowledge the reception of the command. */
2144 ret
= consumer_send_status_msg(sock
,
2145 LTTCOMM_CONSUMERD_SUCCESS
);
2147 /* Somehow, the session daemon is not responding anymore. */
2152 * Receive trace chunk domain dirfd.
2154 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2155 if (ret
!= sizeof(chunk_dirfd
)) {
2156 ERR("Failed to receive trace chunk domain directory file descriptor");
2160 DBG("Received trace chunk domain directory fd (%d)",
2162 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2164 if (!chunk_directory_handle
) {
2165 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2166 if (close(chunk_dirfd
)) {
2167 PERROR("Failed to close chunk directory file descriptor");
2173 ret_code
= lttng_consumer_create_trace_chunk(
2174 !is_local_trace
? &relayd_id
: NULL
,
2175 msg
.u
.create_trace_chunk
.session_id
,
2176 msg
.u
.create_trace_chunk
.chunk_id
,
2177 (time_t) msg
.u
.create_trace_chunk
2178 .creation_timestamp
,
2179 chunk_override_name
,
2180 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2183 chunk_directory_handle
);
2184 lttng_directory_handle_put(chunk_directory_handle
);
2185 goto end_msg_sessiond
;
2187 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2189 enum lttng_trace_chunk_command_type close_command
=
2190 msg
.u
.close_trace_chunk
.close_command
.value
;
2191 const uint64_t relayd_id
=
2192 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2193 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2194 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2197 ret_code
= lttng_consumer_close_trace_chunk(
2198 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2201 msg
.u
.close_trace_chunk
.session_id
,
2202 msg
.u
.close_trace_chunk
.chunk_id
,
2203 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2204 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2206 NULL
, closed_trace_chunk_path
);
2207 reply
.ret_code
= ret_code
;
2208 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2209 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2210 if (ret
!= sizeof(reply
)) {
2213 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2215 if (ret
!= reply
.path_length
) {
2220 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2222 const uint64_t relayd_id
=
2223 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2225 ret_code
= lttng_consumer_trace_chunk_exists(
2226 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2228 msg
.u
.trace_chunk_exists
.session_id
,
2229 msg
.u
.trace_chunk_exists
.chunk_id
);
2230 goto end_msg_sessiond
;
2232 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2234 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2235 struct lttng_consumer_channel
*channel
=
2236 consumer_find_channel(key
);
2239 pthread_mutex_lock(&channel
->lock
);
2240 ret_code
= lttng_consumer_open_channel_packets(channel
);
2241 pthread_mutex_unlock(&channel
->lock
);
2244 * The channel could have disappeared in per-pid
2247 DBG("Channel %" PRIu64
" not found", key
);
2248 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2251 health_code_update();
2252 goto end_msg_sessiond
;
2260 * Return 1 to indicate success since the 0 value can be a socket
2261 * shutdown during the recv() or send() call.
2268 * The returned value here is not useful since either way we'll return 1 to
2269 * the caller because the session daemon socket management is done
2270 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2272 ret
= consumer_send_status_msg(sock
, ret_code
);
2282 * Free channel here since no one has a reference to it. We don't
2283 * free after that because a stream can store this pointer.
2285 destroy_channel(channel
);
2287 /* We have to send a status channel message indicating an error. */
2288 ret
= consumer_send_status_channel(sock
, NULL
);
2290 /* Stop everything if session daemon can not be notified. */
2297 /* This will issue a consumer stop. */
2303 health_code_update();
2307 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2308 int producer_active
)
2311 assert(stream
->ustream
);
2313 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2317 * Take a snapshot for a specific stream.
2319 * Returns 0 on success, < 0 on error
2321 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2324 assert(stream
->ustream
);
2326 return ustctl_snapshot(stream
->ustream
);
2330 * Sample consumed and produced positions for a specific stream.
2332 * Returns 0 on success, < 0 on error.
2334 int lttng_ustconsumer_sample_snapshot_positions(
2335 struct lttng_consumer_stream
*stream
)
2338 assert(stream
->ustream
);
2340 return ustctl_snapshot_sample_positions(stream
->ustream
);
2344 * Get the produced position
2346 * Returns 0 on success, < 0 on error
2348 int lttng_ustconsumer_get_produced_snapshot(
2349 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2352 assert(stream
->ustream
);
2355 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2359 * Get the consumed position
2361 * Returns 0 on success, < 0 on error
2363 int lttng_ustconsumer_get_consumed_snapshot(
2364 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2367 assert(stream
->ustream
);
2370 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2373 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2377 assert(stream
->ustream
);
2379 ustctl_flush_buffer(stream
->ustream
, producer
);
2382 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2385 assert(stream
->ustream
);
2387 ustctl_clear_buffer(stream
->ustream
);
2390 int lttng_ustconsumer_get_current_timestamp(
2391 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2394 assert(stream
->ustream
);
2397 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2400 int lttng_ustconsumer_get_sequence_number(
2401 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2404 assert(stream
->ustream
);
2407 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2411 * Called when the stream signals the consumer that it has hung up.
2413 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2416 assert(stream
->ustream
);
2418 pthread_mutex_lock(&stream
->lock
);
2419 if (!stream
->quiescent
) {
2420 ustctl_flush_buffer(stream
->ustream
, 0);
2421 stream
->quiescent
= true;
2423 pthread_mutex_unlock(&stream
->lock
);
2424 stream
->hangup_flush_done
= 1;
2427 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2432 assert(chan
->uchan
);
2433 assert(chan
->buffer_credentials
.is_set
);
2435 if (chan
->switch_timer_enabled
== 1) {
2436 consumer_timer_switch_stop(chan
);
2438 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2441 ret
= close(chan
->stream_fds
[i
]);
2445 if (chan
->shm_path
[0]) {
2446 char shm_path
[PATH_MAX
];
2448 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2450 ERR("Cannot get stream shm path");
2452 ret
= run_as_unlink(shm_path
,
2453 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2454 chan
->buffer_credentials
)),
2455 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2456 chan
->buffer_credentials
)));
2458 PERROR("unlink %s", shm_path
);
2464 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2467 assert(chan
->uchan
);
2468 assert(chan
->buffer_credentials
.is_set
);
2470 consumer_metadata_cache_destroy(chan
);
2471 ustctl_destroy_channel(chan
->uchan
);
2472 /* Try to rmdir all directories under shm_path root. */
2473 if (chan
->root_shm_path
[0]) {
2474 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2475 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2476 chan
->buffer_credentials
)),
2477 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2478 chan
->buffer_credentials
)),
2479 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2481 free(chan
->stream_fds
);
2484 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2487 assert(stream
->ustream
);
2489 if (stream
->chan
->switch_timer_enabled
== 1) {
2490 consumer_timer_switch_stop(stream
->chan
);
2492 ustctl_destroy_stream(stream
->ustream
);
2495 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2498 assert(stream
->ustream
);
2500 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2503 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2506 assert(stream
->ustream
);
2508 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2512 * Write up to one packet from the metadata cache to the channel.
2514 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2515 * negative value on error.
2518 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2523 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2524 if (stream
->chan
->metadata_cache
->max_offset
==
2525 stream
->ust_metadata_pushed
) {
2527 * In the context of a user space metadata channel, a
2528 * change in version can be detected in two ways:
2529 * 1) During the pre-consume of the `read_subbuffer` loop,
2530 * 2) When populating the metadata ring buffer (i.e. here).
2532 * This function is invoked when there is no metadata
2533 * available in the ring-buffer. If all data was consumed
2534 * up to the size of the metadata cache, there is no metadata
2535 * to insert in the ring-buffer.
2537 * However, the metadata version could still have changed (a
2538 * regeneration without any new data will yield the same cache
2541 * The cache's version is checked for a version change and the
2542 * consumed position is reset if one occurred.
2544 * This check is only necessary for the user space domain as
2545 * it has to manage the cache explicitly. If this reset was not
2546 * performed, no metadata would be consumed (and no reset would
2547 * occur as part of the pre-consume) until the metadata size
2548 * exceeded the cache size.
2550 if (stream
->metadata_version
!=
2551 stream
->chan
->metadata_cache
->version
) {
2552 metadata_stream_reset_cache_consumed_position(stream
);
2553 consumer_stream_metadata_set_version(stream
,
2554 stream
->chan
->metadata_cache
->version
);
2561 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2562 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2563 stream
->chan
->metadata_cache
->max_offset
2564 - stream
->ust_metadata_pushed
);
2565 assert(write_len
!= 0);
2566 if (write_len
< 0) {
2567 ERR("Writing one metadata packet");
2571 stream
->ust_metadata_pushed
+= write_len
;
2573 assert(stream
->chan
->metadata_cache
->max_offset
>=
2574 stream
->ust_metadata_pushed
);
2578 * Switch packet (but don't open the next one) on every commit of
2579 * a metadata packet. Since the subbuffer is fully filled (with padding,
2580 * if needed), the stream is "quiescent" after this commit.
2582 ustctl_flush_buffer(stream
->ustream
, 1);
2583 stream
->quiescent
= true;
2585 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2591 * Sync metadata meaning request them to the session daemon and snapshot to the
2592 * metadata thread can consumer them.
2594 * Metadata stream lock is held here, but we need to release it when
2595 * interacting with sessiond, else we cause a deadlock with live
2596 * awaiting on metadata to be pushed out.
2598 * The RCU read side lock must be held by the caller.
2600 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2601 struct lttng_consumer_local_data
*ctx
,
2602 struct lttng_consumer_stream
*metadata_stream
)
2605 enum sync_metadata_status status
;
2606 struct lttng_consumer_channel
*metadata_channel
;
2609 assert(metadata_stream
);
2611 metadata_channel
= metadata_stream
->chan
;
2612 pthread_mutex_unlock(&metadata_stream
->lock
);
2614 * Request metadata from the sessiond, but don't wait for the flush
2615 * because we locked the metadata thread.
2617 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2618 pthread_mutex_lock(&metadata_stream
->lock
);
2620 status
= SYNC_METADATA_STATUS_ERROR
;
2625 * The metadata stream and channel can be deleted while the
2626 * metadata stream lock was released. The streamed is checked
2627 * for deletion before we use it further.
2629 * Note that it is safe to access a logically-deleted stream since its
2630 * existence is still guaranteed by the RCU read side lock. However,
2631 * it should no longer be used. The close/deletion of the metadata
2632 * channel and stream already guarantees that all metadata has been
2633 * consumed. Therefore, there is nothing left to do in this function.
2635 if (consumer_stream_is_deleted(metadata_stream
)) {
2636 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2637 metadata_stream
->key
);
2638 status
= SYNC_METADATA_STATUS_NO_DATA
;
2642 ret
= commit_one_metadata_packet(metadata_stream
);
2644 status
= SYNC_METADATA_STATUS_ERROR
;
2646 } else if (ret
> 0) {
2647 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2648 } else /* ret == 0 */ {
2649 status
= SYNC_METADATA_STATUS_NO_DATA
;
2653 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2655 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2656 status
= SYNC_METADATA_STATUS_ERROR
;
2665 * Return 0 on success else a negative value.
2667 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2668 struct lttng_consumer_local_data
*ctx
)
2671 struct ustctl_consumer_stream
*ustream
;
2676 ustream
= stream
->ustream
;
2679 * First, we are going to check if there is a new subbuffer available
2680 * before reading the stream wait_fd.
2682 /* Get the next subbuffer */
2683 ret
= ustctl_get_next_subbuf(ustream
);
2685 /* No more data found, flag the stream. */
2686 stream
->has_data
= 0;
2691 ret
= ustctl_put_subbuf(ustream
);
2694 /* This stream still has data. Flag it and wake up the data thread. */
2695 stream
->has_data
= 1;
2697 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2700 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2701 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2706 /* The wake up pipe has been notified. */
2707 ctx
->has_wakeup
= 1;
2715 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2720 * We can consume the 1 byte written into the wait_fd by
2721 * UST. Don't trigger error if we cannot read this one byte
2722 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2724 * This is only done when the stream is monitored by a thread,
2725 * before the flush is done after a hangup and if the stream
2726 * is not flagged with data since there might be nothing to
2727 * consume in the wait fd but still have data available
2728 * flagged by the consumer wake up pipe.
2730 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2734 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2735 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2743 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2744 struct stream_subbuffer
*subbuf
)
2748 ret
= ustctl_get_subbuf_size(
2749 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2754 ret
= ustctl_get_padded_subbuf_size(
2755 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2764 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2765 struct stream_subbuffer
*subbuf
)
2769 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2774 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2780 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2781 struct stream_subbuffer
*subbuf
)
2785 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2790 ret
= ustctl_get_packet_size(
2791 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2793 PERROR("Failed to get sub-buffer packet size");
2797 ret
= ustctl_get_content_size(
2798 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2800 PERROR("Failed to get sub-buffer content size");
2804 ret
= ustctl_get_timestamp_begin(
2805 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2807 PERROR("Failed to get sub-buffer begin timestamp");
2811 ret
= ustctl_get_timestamp_end(
2812 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2814 PERROR("Failed to get sub-buffer end timestamp");
2818 ret
= ustctl_get_events_discarded(
2819 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2821 PERROR("Failed to get sub-buffer events discarded count");
2825 ret
= ustctl_get_sequence_number(stream
->ustream
,
2826 &subbuf
->info
.data
.sequence_number
.value
);
2828 /* May not be supported by older LTTng-modules. */
2829 if (ret
!= -ENOTTY
) {
2830 PERROR("Failed to get sub-buffer sequence number");
2834 subbuf
->info
.data
.sequence_number
.is_set
= true;
2837 ret
= ustctl_get_stream_id(
2838 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2840 PERROR("Failed to get stream id");
2844 ret
= ustctl_get_instance_id(stream
->ustream
,
2845 &subbuf
->info
.data
.stream_instance_id
.value
);
2847 /* May not be supported by older LTTng-modules. */
2848 if (ret
!= -ENOTTY
) {
2849 PERROR("Failed to get stream instance id");
2853 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2859 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2860 struct stream_subbuffer
*subbuffer
)
2865 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2871 ret
= get_current_subbuf_addr(stream
, &addr
);
2876 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2877 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2878 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2883 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2884 struct stream_subbuffer
*subbuffer
)
2888 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2893 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2901 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2902 struct stream_subbuffer
*subbuffer
)
2909 unsigned long consumed_pos
, produced_pos
;
2912 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2914 got_subbuffer
= true;
2916 got_subbuffer
= false;
2917 if (ret
!= -EAGAIN
) {
2924 * Determine if the cache is empty and ensure that a sub-buffer
2925 * is made available if the cache is not empty.
2927 if (!got_subbuffer
) {
2928 ret
= commit_one_metadata_packet(stream
);
2929 if (ret
< 0 && ret
!= -ENOBUFS
) {
2931 } else if (ret
== 0) {
2932 /* Not an error, the cache is empty. */
2937 cache_empty
= false;
2940 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2941 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2942 stream
->ust_metadata_pushed
;
2943 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2945 } while (!got_subbuffer
);
2947 /* Populate sub-buffer infos and view. */
2948 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2953 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2956 * -EAGAIN is not expected since we got a sub-buffer and haven't
2957 * pushed the consumption position yet (on put_next).
2959 PERROR("Failed to take a snapshot of metadata buffer positions");
2963 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2965 PERROR("Failed to get metadata consumed position");
2969 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2971 PERROR("Failed to get metadata produced position");
2975 /* Last sub-buffer of the ring buffer ? */
2976 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2979 * The sessiond registry lock ensures that coherent units of metadata
2980 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2981 * acquired, the cache is empty, and it is the only available sub-buffer
2982 * available, it is safe to assume that it is "coherent".
2984 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2986 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2991 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2992 struct stream_subbuffer
*subbuffer
)
2994 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
3000 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3001 struct lttng_consumer_local_data
*ctx
)
3003 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3004 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3007 static int lttng_ustconsumer_set_stream_ops(
3008 struct lttng_consumer_stream
*stream
)
3012 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3013 if (stream
->metadata_flag
) {
3014 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3015 get_next_subbuffer_metadata
;
3016 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3017 extract_metadata_subbuffer_info
;
3018 stream
->read_subbuffer_ops
.reset_metadata
=
3019 metadata_stream_reset_cache_consumed_position
;
3020 if (stream
->chan
->is_live
) {
3021 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3022 ret
= consumer_stream_enable_metadata_bucketization(
3029 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3031 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3032 extract_data_subbuffer_info
;
3033 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3034 if (stream
->chan
->is_live
) {
3035 stream
->read_subbuffer_ops
.send_live_beacon
=
3036 consumer_flush_ust_index
;
3040 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3046 * Called when a stream is created.
3048 * Return 0 on success or else a negative value.
3050 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3057 * Don't create anything if this is set for streaming or if there is
3058 * no current trace chunk on the parent channel.
3060 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3061 stream
->chan
->trace_chunk
) {
3062 ret
= consumer_stream_create_output_files(stream
, true);
3068 lttng_ustconsumer_set_stream_ops(stream
);
3076 * Check if data is still being extracted from the buffers for a specific
3077 * stream. Consumer data lock MUST be acquired before calling this function
3078 * and the stream lock.
3080 * Return 1 if the traced data are still getting read else 0 meaning that the
3081 * data is available for trace viewer reading.
3083 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3088 assert(stream
->ustream
);
3089 ASSERT_LOCKED(stream
->lock
);
3091 DBG("UST consumer checking data pending");
3093 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3098 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3099 uint64_t contiguous
, pushed
;
3101 /* Ease our life a bit. */
3102 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3103 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3104 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3105 pushed
= stream
->ust_metadata_pushed
;
3108 * We can simply check whether all contiguously available data
3109 * has been pushed to the ring buffer, since the push operation
3110 * is performed within get_next_subbuf(), and because both
3111 * get_next_subbuf() and put_next_subbuf() are issued atomically
3112 * thanks to the stream lock within
3113 * lttng_ustconsumer_read_subbuffer(). This basically means that
3114 * whetnever ust_metadata_pushed is incremented, the associated
3115 * metadata has been consumed from the metadata stream.
3117 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3118 contiguous
, pushed
);
3119 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3120 if ((contiguous
!= pushed
) ||
3121 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3122 ret
= 1; /* Data is pending */
3126 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3129 * There is still data so let's put back this
3132 ret
= ustctl_put_subbuf(stream
->ustream
);
3134 ret
= 1; /* Data is pending */
3139 /* Data is NOT pending so ready to be read. */
3147 * Stop a given metadata channel timer if enabled and close the wait fd which
3148 * is the poll pipe of the metadata stream.
3150 * This MUST be called with the metadata channel lock acquired.
3152 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3157 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3159 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3161 if (metadata
->switch_timer_enabled
== 1) {
3162 consumer_timer_switch_stop(metadata
);
3165 if (!metadata
->metadata_stream
) {
3170 * Closing write side so the thread monitoring the stream wakes up if any
3171 * and clean the metadata stream.
3173 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3174 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3176 PERROR("closing metadata pipe write side");
3178 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3186 * Close every metadata stream wait fd of the metadata hash table. This
3187 * function MUST be used very carefully so not to run into a race between the
3188 * metadata thread handling streams and this function closing their wait fd.
3190 * For UST, this is used when the session daemon hangs up. Its the metadata
3191 * producer so calling this is safe because we are assured that no state change
3192 * can occur in the metadata thread for the streams in the hash table.
3194 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3196 struct lttng_ht_iter iter
;
3197 struct lttng_consumer_stream
*stream
;
3199 assert(metadata_ht
);
3200 assert(metadata_ht
->ht
);
3202 DBG("UST consumer closing all metadata streams");
3205 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3208 health_code_update();
3210 pthread_mutex_lock(&stream
->chan
->lock
);
3211 lttng_ustconsumer_close_metadata(stream
->chan
);
3212 pthread_mutex_unlock(&stream
->chan
->lock
);
3218 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3222 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3224 ERR("Unable to close wakeup fd");
3229 * Please refer to consumer-timer.c before adding any lock within this
3230 * function or any of its callees. Timers have a very strict locking
3231 * semantic with respect to teardown. Failure to respect this semantic
3232 * introduces deadlocks.
3234 * DON'T hold the metadata lock when calling this function, else this
3235 * can cause deadlock involving consumer awaiting for metadata to be
3236 * pushed out due to concurrent interaction with the session daemon.
3238 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3239 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3241 struct lttcomm_metadata_request_msg request
;
3242 struct lttcomm_consumer_msg msg
;
3243 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3244 uint64_t len
, key
, offset
, version
;
3248 assert(channel
->metadata_cache
);
3250 memset(&request
, 0, sizeof(request
));
3252 /* send the metadata request to sessiond */
3253 switch (consumer_data
.type
) {
3254 case LTTNG_CONSUMER64_UST
:
3255 request
.bits_per_long
= 64;
3257 case LTTNG_CONSUMER32_UST
:
3258 request
.bits_per_long
= 32;
3261 request
.bits_per_long
= 0;
3265 request
.session_id
= channel
->session_id
;
3266 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3268 * Request the application UID here so the metadata of that application can
3269 * be sent back. The channel UID corresponds to the user UID of the session
3270 * used for the rights on the stream file(s).
3272 request
.uid
= channel
->ust_app_uid
;
3273 request
.key
= channel
->key
;
3275 DBG("Sending metadata request to sessiond, session id %" PRIu64
3276 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3277 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3280 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3282 health_code_update();
3284 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3287 ERR("Asking metadata to sessiond");
3291 health_code_update();
3293 /* Receive the metadata from sessiond */
3294 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3296 if (ret
!= sizeof(msg
)) {
3297 DBG("Consumer received unexpected message size %d (expects %zu)",
3299 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3301 * The ret value might 0 meaning an orderly shutdown but this is ok
3302 * since the caller handles this.
3307 health_code_update();
3309 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3310 /* No registry found */
3311 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3315 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3316 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3321 len
= msg
.u
.push_metadata
.len
;
3322 key
= msg
.u
.push_metadata
.key
;
3323 offset
= msg
.u
.push_metadata
.target_offset
;
3324 version
= msg
.u
.push_metadata
.version
;
3326 assert(key
== channel
->key
);
3328 DBG("No new metadata to receive for key %" PRIu64
, key
);
3331 health_code_update();
3333 /* Tell session daemon we are ready to receive the metadata. */
3334 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3335 LTTCOMM_CONSUMERD_SUCCESS
);
3336 if (ret
< 0 || len
== 0) {
3338 * Somehow, the session daemon is not responding anymore or there is
3339 * nothing to receive.
3344 health_code_update();
3346 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3347 key
, offset
, len
, version
, channel
, timer
, wait
);
3350 * Only send the status msg if the sessiond is alive meaning a positive
3353 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3358 health_code_update();
3360 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3365 * Return the ustctl call for the get stream id.
3367 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3368 uint64_t *stream_id
)
3373 return ustctl_get_stream_id(stream
->ustream
, stream_id
);