2 * Copyright (C) 2011 EfficiOS Inc.
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
11 #include "ust-consumer.hpp"
13 #include <common/common.hpp>
14 #include <common/compat/endian.hpp>
15 #include <common/compat/fcntl.hpp>
16 #include <common/consumer/consumer-metadata-cache.hpp>
17 #include <common/consumer/consumer-stream.hpp>
18 #include <common/consumer/consumer-timer.hpp>
19 #include <common/consumer/consumer.hpp>
20 #include <common/index/index.hpp>
21 #include <common/optional.hpp>
22 #include <common/relayd/relayd.hpp>
23 #include <common/sessiond-comm/sessiond-comm.hpp>
24 #include <common/shm.hpp>
25 #include <common/utils.hpp>
27 #include <lttng/ust-ctl.h>
28 #include <lttng/ust-sigbus.h>
30 #include <bin/lttng-consumerd/health-consumerd.hpp>
40 #include <sys/socket.h>
42 #include <sys/types.h>
44 #include <urcu/list.h>
46 #define INT_MAX_STR_LEN 12 /* includes \0 */
48 extern struct lttng_consumer_global_data the_consumer_data
;
49 extern int consumer_poll_timeout
;
51 LTTNG_EXPORT
DEFINE_LTTNG_UST_SIGBUS_STATE();
54 * Add channel to internal consumer state.
56 * Returns 0 on success or else a negative value.
58 static int add_channel(struct lttng_consumer_channel
*channel
,
59 struct lttng_consumer_local_data
*ctx
)
63 LTTNG_ASSERT(channel
);
66 if (ctx
->on_recv_channel
!= nullptr) {
67 ret
= ctx
->on_recv_channel(channel
);
69 ret
= consumer_add_channel(channel
, ctx
);
71 /* Most likely an ENOMEM. */
72 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
76 ret
= consumer_add_channel(channel
, ctx
);
79 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
86 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
87 * error value if applicable is set in it else it is kept untouched.
89 * Return NULL on error else the newly allocated stream object.
91 static struct lttng_consumer_stream
*allocate_stream(int cpu
,
93 struct lttng_consumer_channel
*channel
,
94 struct lttng_consumer_local_data
*ctx
,
98 struct lttng_consumer_stream
*stream
= nullptr;
100 LTTNG_ASSERT(channel
);
103 stream
= consumer_stream_create(channel
,
109 channel
->trace_chunk
,
114 if (stream
== nullptr) {
118 * We could not find the channel. Can happen if cpu hotplug
119 * happens while tearing down.
121 DBG3("Could not find channel");
126 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
132 consumer_stream_update_channel_attributes(stream
, channel
);
136 *_alloc_ret
= alloc_ret
;
142 * Send the given stream pointer to the corresponding thread.
144 * Returns 0 on success else a negative value.
146 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
147 struct lttng_consumer_local_data
*ctx
)
150 struct lttng_pipe
*stream_pipe
;
152 /* Get the right pipe where the stream will be sent. */
153 if (stream
->metadata_flag
) {
154 consumer_add_metadata_stream(stream
);
155 stream_pipe
= ctx
->consumer_metadata_pipe
;
157 consumer_add_data_stream(stream
);
158 stream_pipe
= ctx
->consumer_data_pipe
;
162 * From this point on, the stream's ownership has been moved away from
163 * the channel and it becomes globally visible. Hence, remove it from
164 * the local stream list to prevent the stream from being both local and
167 stream
->globally_visible
= 1;
168 cds_list_del_init(&stream
->send_node
);
170 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
)); /* NOLINT sizeof used on a
173 ERR("Consumer write %s stream to pipe %d",
174 stream
->metadata_flag
? "metadata" : "data",
175 lttng_pipe_get_writefd(stream_pipe
));
176 if (stream
->metadata_flag
) {
177 consumer_del_stream_for_metadata(stream
);
179 consumer_del_stream_for_data(stream
);
188 static int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
190 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
193 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
194 stream_shm_path
[PATH_MAX
- 1] = '\0';
195 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
200 strncat(stream_shm_path
, cpu_nr
, PATH_MAX
- strlen(stream_shm_path
) - 1);
207 * Create streams for the given channel using liblttng-ust-ctl.
208 * The channel lock must be acquired by the caller.
210 * Return 0 on success else a negative value.
212 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
213 struct lttng_consumer_local_data
*ctx
)
216 struct lttng_ust_ctl_consumer_stream
*ustream
;
217 struct lttng_consumer_stream
*stream
;
218 pthread_mutex_t
*current_stream_lock
= nullptr;
220 LTTNG_ASSERT(channel
);
224 * While a stream is available from ustctl. When NULL is returned, we've
225 * reached the end of the possible stream for the channel.
227 while ((ustream
= lttng_ust_ctl_create_stream(channel
->uchan
, cpu
))) {
229 int ust_metadata_pipe
[2];
231 health_code_update();
233 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
234 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
236 ERR("Create ust metadata poll pipe");
239 wait_fd
= ust_metadata_pipe
[0];
241 wait_fd
= lttng_ust_ctl_stream_get_wait_fd(ustream
);
244 /* Allocate consumer stream object. */
245 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
249 stream
->ustream
= ustream
;
251 * Store it so we can save multiple function calls afterwards since
252 * this value is used heavily in the stream threads. This is UST
253 * specific so this is why it's done after allocation.
255 stream
->wait_fd
= wait_fd
;
258 * Increment channel refcount since the channel reference has now been
259 * assigned in the allocation process above.
261 if (stream
->chan
->monitor
) {
262 uatomic_inc(&stream
->chan
->refcount
);
265 pthread_mutex_lock(&stream
->lock
);
266 current_stream_lock
= &stream
->lock
;
268 * Order is important this is why a list is used. On error, the caller
269 * should clean this list.
271 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
273 ret
= lttng_ust_ctl_get_max_subbuf_size(stream
->ustream
, &stream
->max_sb_size
);
275 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s", stream
->name
);
279 /* Do actions once stream has been received. */
280 if (ctx
->on_recv_stream
) {
281 ret
= ctx
->on_recv_stream(stream
);
287 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
290 stream
->relayd_stream_id
);
292 /* Set next CPU stream. */
293 channel
->streams
.count
= ++cpu
;
295 /* Keep stream reference when creating metadata. */
296 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
297 channel
->metadata_stream
= stream
;
298 if (channel
->monitor
) {
299 /* Set metadata poll pipe if we created one */
300 memcpy(stream
->ust_metadata_poll_pipe
,
302 sizeof(ust_metadata_pipe
));
305 pthread_mutex_unlock(&stream
->lock
);
306 current_stream_lock
= nullptr;
313 if (current_stream_lock
) {
314 pthread_mutex_unlock(current_stream_lock
);
319 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
321 const struct lttng_credentials
*session_credentials
)
323 char shm_path
[PATH_MAX
];
326 if (!channel
->shm_path
[0]) {
327 return shm_create_anonymous("ust-consumer");
329 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
333 return run_as_open(shm_path
,
334 O_RDWR
| O_CREAT
| O_EXCL
,
336 lttng_credentials_get_uid(session_credentials
),
337 lttng_credentials_get_gid(session_credentials
));
344 * Create an UST channel with the given attributes and send it to the session
345 * daemon using the ust ctl API.
347 * Return 0 on success or else a negative value.
349 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
350 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
351 struct lttng_ust_ctl_consumer_channel
**ust_chanp
)
353 int ret
, nr_stream_fds
, i
, j
;
355 struct lttng_ust_ctl_consumer_channel
*ust_channel
;
357 LTTNG_ASSERT(channel
);
359 LTTNG_ASSERT(ust_chanp
);
360 LTTNG_ASSERT(channel
->buffer_credentials
.is_set
);
362 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
363 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
364 "switch_timer_interval: %u, read_timer_interval: %u, "
365 "output: %d, type: %d",
369 attr
->switch_timer_interval
,
370 attr
->read_timer_interval
,
374 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
377 nr_stream_fds
= lttng_ust_ctl_get_nr_stream_per_channel();
378 stream_fds
= calloc
<int>(nr_stream_fds
);
383 for (i
= 0; i
< nr_stream_fds
; i
++) {
384 stream_fds
[i
] = open_ust_stream_fd(channel
, i
, &channel
->buffer_credentials
.value
);
385 if (stream_fds
[i
] < 0) {
390 ust_channel
= lttng_ust_ctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
395 channel
->nr_stream_fds
= nr_stream_fds
;
396 channel
->stream_fds
= stream_fds
;
397 *ust_chanp
= ust_channel
;
403 for (j
= i
- 1; j
>= 0; j
--) {
406 closeret
= close(stream_fds
[j
]);
410 if (channel
->shm_path
[0]) {
411 char shm_path
[PATH_MAX
];
413 closeret
= get_stream_shm_path(shm_path
, channel
->shm_path
, j
);
415 ERR("Cannot get stream shm path");
417 closeret
= run_as_unlink(shm_path
,
418 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
419 channel
->buffer_credentials
)),
420 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
421 channel
->buffer_credentials
)));
423 PERROR("unlink %s", shm_path
);
427 /* Try to rmdir all directories under shm_path root. */
428 if (channel
->root_shm_path
[0]) {
429 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
430 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
431 channel
->buffer_credentials
)),
432 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
433 channel
->buffer_credentials
)),
434 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
442 * Send a single given stream to the session daemon using the sock.
444 * Return 0 on success else a negative value.
446 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
450 LTTNG_ASSERT(stream
);
451 LTTNG_ASSERT(sock
>= 0);
453 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
455 /* Send stream to session daemon. */
456 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, stream
->ustream
);
466 * Send channel to sessiond and relayd if applicable.
468 * Return 0 on success or else a negative value.
470 static int send_channel_to_sessiond_and_relayd(int sock
,
471 struct lttng_consumer_channel
*channel
,
472 struct lttng_consumer_local_data
*ctx
,
475 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
476 struct lttng_consumer_stream
*stream
;
477 uint64_t net_seq_idx
= -1ULL;
479 LTTNG_ASSERT(channel
);
481 LTTNG_ASSERT(sock
>= 0);
483 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
485 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
486 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
487 health_code_update();
489 /* Try to send the stream to the relayd if one is available. */
490 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
493 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
496 * Flag that the relayd was the problem here probably due to a
497 * communicaton error on the socket.
502 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
504 if (net_seq_idx
== -1ULL) {
505 net_seq_idx
= stream
->net_seq_idx
;
510 /* Inform sessiond that we are about to send channel and streams. */
511 ret
= consumer_send_status_msg(sock
, ret_code
);
512 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
514 * Either the session daemon is not responding or the relayd died so we
520 /* Send channel to sessiond. */
521 ret
= lttng_ust_ctl_send_channel_to_sessiond(sock
, channel
->uchan
);
526 ret
= lttng_ust_ctl_channel_close_wakeup_fd(channel
->uchan
);
531 /* The channel was sent successfully to the sessiond at this point. */
532 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
533 health_code_update();
535 /* Send stream to session daemon. */
536 ret
= send_sessiond_stream(sock
, stream
);
542 /* Tell sessiond there is no more stream. */
543 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, nullptr);
548 DBG("UST consumer NULL stream sent to sessiond");
553 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
560 * Creates a channel and streams and add the channel it to the channel internal
561 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
564 * Return 0 on success or else, a negative value is returned and the channel
565 * MUST be destroyed by consumer_del_channel().
567 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
568 struct lttng_consumer_channel
*channel
,
569 struct lttng_ust_ctl_consumer_channel_attr
*attr
)
574 LTTNG_ASSERT(channel
);
578 * This value is still used by the kernel consumer since for the kernel,
579 * the stream ownership is not IN the consumer so we need to have the
580 * number of left stream that needs to be initialized so we can know when
581 * to delete the channel (see consumer.c).
583 * As for the user space tracer now, the consumer creates and sends the
584 * stream to the session daemon which only sends them to the application
585 * once every stream of a channel is received making this value useless
586 * because we they will be added to the poll thread before the application
587 * receives them. This ensures that a stream can not hang up during
588 * initilization of a channel.
590 channel
->nb_init_stream_left
= 0;
592 /* The reply msg status is handled in the following call. */
593 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
598 channel
->wait_fd
= lttng_ust_ctl_channel_get_wait_fd(channel
->uchan
);
601 * For the snapshots (no monitor), we create the metadata streams
602 * on demand, not during the channel creation.
604 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
609 /* Open all streams for this channel. */
610 pthread_mutex_lock(&channel
->lock
);
611 ret
= create_ust_streams(channel
, ctx
);
612 pthread_mutex_unlock(&channel
->lock
);
622 * Send all stream of a channel to the right thread handling it.
624 * On error, return a negative value else 0 on success.
626 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
627 struct lttng_consumer_local_data
*ctx
)
630 struct lttng_consumer_stream
*stream
, *stmp
;
632 LTTNG_ASSERT(channel
);
635 /* Send streams to the corresponding thread. */
636 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->streams
.head
, send_node
) {
637 health_code_update();
639 /* Sending the stream to the thread. */
640 ret
= send_stream_to_thread(stream
, ctx
);
643 * If we are unable to send the stream to the thread, there is
644 * a big problem so just stop everything.
655 * Flush channel's streams using the given key to retrieve the channel.
657 * Return 0 on success else an LTTng error code.
659 static int flush_channel(uint64_t chan_key
)
662 struct lttng_consumer_channel
*channel
;
663 struct lttng_consumer_stream
*stream
;
665 struct lttng_ht_iter iter
;
667 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
670 channel
= consumer_find_channel(chan_key
);
672 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
673 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
677 ht
= the_consumer_data
.stream_per_chan_id_ht
;
679 /* For each stream of the channel id, flush it. */
680 cds_lfht_for_each_entry_duplicate(ht
->ht
,
681 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
686 node_channel_id
.node
)
688 health_code_update();
690 pthread_mutex_lock(&stream
->lock
);
693 * Protect against concurrent teardown of a stream.
695 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
699 if (!stream
->quiescent
) {
700 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
702 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64
703 ", channel name = '%s'",
706 ret
= LTTNG_ERR_BUFFER_FLUSH_FAILED
;
707 pthread_mutex_unlock(&stream
->lock
);
710 stream
->quiescent
= true;
713 pthread_mutex_unlock(&stream
->lock
);
717 * Send one last buffer statistics update to the session daemon. This
718 * ensures that the session daemon gets at least one statistics update
719 * per channel even in the case of short-lived channels, such as when a
720 * short-lived app is traced in per-pid mode.
722 sample_and_send_channel_buffer_stats(channel
);
729 * Clear quiescent state from channel's streams using the given key to
730 * retrieve the channel.
732 * Return 0 on success else an LTTng error code.
734 static int clear_quiescent_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 clear quiescent channel key %" PRIu64
, chan_key
);
745 channel
= consumer_find_channel(chan_key
);
747 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
748 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
752 ht
= the_consumer_data
.stream_per_chan_id_ht
;
754 /* For each stream of the channel id, clear quiescent state. */
755 cds_lfht_for_each_entry_duplicate(ht
->ht
,
756 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
761 node_channel_id
.node
)
763 health_code_update();
765 pthread_mutex_lock(&stream
->lock
);
766 stream
->quiescent
= false;
767 pthread_mutex_unlock(&stream
->lock
);
775 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
777 * Return 0 on success else an LTTng error code.
779 static int close_metadata(uint64_t chan_key
)
782 struct lttng_consumer_channel
*channel
;
783 unsigned int channel_monitor
;
785 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
787 channel
= consumer_find_channel(chan_key
);
790 * This is possible if the metadata thread has issue a delete because
791 * the endpoint point of the stream hung up. There is no way the
792 * session daemon can know about it thus use a DBG instead of an actual
795 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
796 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
800 pthread_mutex_lock(&the_consumer_data
.lock
);
801 pthread_mutex_lock(&channel
->lock
);
802 channel_monitor
= channel
->monitor
;
803 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
807 lttng_ustconsumer_close_metadata(channel
);
808 pthread_mutex_unlock(&channel
->lock
);
809 pthread_mutex_unlock(&the_consumer_data
.lock
);
812 * The ownership of a metadata channel depends on the type of
813 * session to which it belongs. In effect, the monitor flag is checked
814 * to determine if this metadata channel is in "snapshot" mode or not.
816 * In the non-snapshot case, the metadata channel is created along with
817 * a single stream which will remain present until the metadata channel
818 * is destroyed (on the destruction of its session). In this case, the
819 * metadata stream in "monitored" by the metadata poll thread and holds
820 * the ownership of its channel.
822 * Closing the metadata will cause the metadata stream's "metadata poll
823 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
824 * thread which will teardown the metadata stream which, in return,
825 * deletes the metadata channel.
827 * In the snapshot case, the metadata stream is created and destroyed
828 * on every snapshot record. Since the channel doesn't have an owner
829 * other than the session daemon, it is safe to destroy it immediately
830 * on reception of the CLOSE_METADATA command.
832 if (!channel_monitor
) {
834 * The channel and consumer_data locks must be
835 * released before this call since consumer_del_channel
836 * re-acquires the channel and consumer_data locks to teardown
837 * the channel and queue its reclamation by the "call_rcu"
840 consumer_del_channel(channel
);
845 pthread_mutex_unlock(&channel
->lock
);
846 pthread_mutex_unlock(&the_consumer_data
.lock
);
852 * RCU read side lock MUST be acquired before calling this function.
854 * Return 0 on success else an LTTng error code.
856 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
859 struct lttng_consumer_channel
*metadata
;
861 ASSERT_RCU_READ_LOCKED();
863 DBG("UST consumer setup metadata key %" PRIu64
, key
);
865 metadata
= consumer_find_channel(key
);
867 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
868 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
873 * In no monitor mode, the metadata channel has no stream(s) so skip the
874 * ownership transfer to the metadata thread.
876 if (!metadata
->monitor
) {
877 DBG("Metadata channel in no monitor");
883 * Send metadata stream to relayd if one available. Availability is
884 * known if the stream is still in the list of the channel.
886 if (cds_list_empty(&metadata
->streams
.head
)) {
887 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
888 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
889 goto error_no_stream
;
892 /* Send metadata stream to relayd if needed. */
893 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
894 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
, metadata
->pathname
);
896 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
899 ret
= consumer_send_relayd_streams_sent(metadata
->metadata_stream
->net_seq_idx
);
901 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
907 * Ownership of metadata stream is passed along. Freeing is handled by
910 ret
= send_streams_to_thread(metadata
, ctx
);
913 * If we are unable to send the stream to the thread, there is
914 * a big problem so just stop everything.
916 ret
= LTTCOMM_CONSUMERD_FATAL
;
917 goto send_streams_error
;
919 /* List MUST be empty after or else it could be reused. */
920 LTTNG_ASSERT(cds_list_empty(&metadata
->streams
.head
));
927 * Delete metadata channel on error. At this point, the metadata stream can
928 * NOT be monitored by the metadata thread thus having the guarantee that
929 * the stream is still in the local stream list of the channel. This call
930 * will make sure to clean that list.
932 consumer_stream_destroy(metadata
->metadata_stream
, nullptr);
933 metadata
->metadata_stream
= nullptr;
941 * Snapshot the whole metadata.
942 * RCU read-side lock must be held by the caller.
944 * Returns 0 on success, < 0 on error
946 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
950 struct lttng_consumer_local_data
*ctx
)
953 struct lttng_consumer_stream
*metadata_stream
;
957 ASSERT_RCU_READ_LOCKED();
959 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s", key
, path
);
963 LTTNG_ASSERT(!metadata_channel
->monitor
);
965 health_code_update();
968 * Ask the sessiond if we have new metadata waiting and update the
969 * consumer metadata cache.
971 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
976 health_code_update();
979 * The metadata stream is NOT created in no monitor mode when the channel
980 * is created on a sessiond ask channel command.
982 ret
= create_ust_streams(metadata_channel
, ctx
);
987 metadata_stream
= metadata_channel
->metadata_stream
;
988 LTTNG_ASSERT(metadata_stream
);
990 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
991 if (relayd_id
!= (uint64_t) -1ULL) {
992 metadata_stream
->net_seq_idx
= relayd_id
;
993 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
995 ret
= consumer_stream_create_output_files(metadata_stream
, false);
1002 health_code_update();
1003 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1010 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
1012 * Clean up the stream completely because the next snapshot will use a
1013 * new metadata stream.
1015 consumer_stream_destroy(metadata_stream
, nullptr);
1016 metadata_channel
->metadata_stream
= nullptr;
1023 static int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
, const char **addr
)
1026 unsigned long mmap_offset
;
1027 const char *mmap_base
;
1029 mmap_base
= (const char *) lttng_ust_ctl_get_mmap_base(stream
->ustream
);
1031 ERR("Failed to get mmap base for stream `%s`", stream
->name
);
1036 ret
= lttng_ust_ctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1038 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1043 *addr
= mmap_base
+ mmap_offset
;
1049 * Take a snapshot of all the stream of a channel.
1050 * RCU read-side lock and the channel lock must be held by the caller.
1052 * Returns 0 on success, < 0 on error
1054 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1058 uint64_t nb_packets_per_stream
,
1059 struct lttng_consumer_local_data
*ctx
)
1062 unsigned use_relayd
= 0;
1063 unsigned long consumed_pos
, produced_pos
;
1064 struct lttng_consumer_stream
*stream
;
1068 ASSERT_RCU_READ_LOCKED();
1072 if (relayd_id
!= (uint64_t) -1ULL) {
1076 LTTNG_ASSERT(!channel
->monitor
);
1077 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1079 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
1080 health_code_update();
1082 /* Lock stream because we are about to change its state. */
1083 pthread_mutex_lock(&stream
->lock
);
1084 LTTNG_ASSERT(channel
->trace_chunk
);
1085 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1087 * Can't happen barring an internal error as the channel
1088 * holds a reference to the trace chunk.
1090 ERR("Failed to acquire reference to channel's trace chunk");
1094 LTTNG_ASSERT(!stream
->trace_chunk
);
1095 stream
->trace_chunk
= channel
->trace_chunk
;
1097 stream
->net_seq_idx
= relayd_id
;
1100 ret
= consumer_send_relayd_stream(stream
, path
);
1102 goto error_close_stream
;
1105 ret
= consumer_stream_create_output_files(stream
, false);
1107 goto error_close_stream
;
1109 DBG("UST consumer snapshot stream (%" PRIu64
")", stream
->key
);
1113 * If tracing is active, we want to perform a "full" buffer flush.
1114 * Else, if quiescent, it has already been done by the prior stop.
1116 if (!stream
->quiescent
) {
1117 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
1119 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
1120 ", channel name = '%s'",
1127 ret
= lttng_ustconsumer_take_snapshot(stream
);
1129 ERR("Taking UST snapshot");
1130 goto error_close_stream
;
1133 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1135 ERR("Produced UST snapshot position");
1136 goto error_close_stream
;
1139 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1141 ERR("Consumerd UST snapshot position");
1142 goto error_close_stream
;
1146 * The original value is sent back if max stream size is larger than
1147 * the possible size of the snapshot. Also, we assume that the session
1148 * daemon should never send a maximum stream size that is lower than
1151 consumed_pos
= consumer_get_consume_start_pos(
1152 consumed_pos
, produced_pos
, nb_packets_per_stream
, stream
->max_sb_size
);
1154 while ((long) (consumed_pos
- produced_pos
) < 0) {
1156 unsigned long len
, padded_len
;
1157 const char *subbuf_addr
;
1158 struct lttng_buffer_view subbuf_view
;
1160 health_code_update();
1162 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1164 ret
= lttng_ust_ctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1166 if (ret
!= -EAGAIN
) {
1167 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1168 goto error_close_stream
;
1170 DBG("UST consumer get subbuf failed. Skipping it.");
1171 consumed_pos
+= stream
->max_sb_size
;
1172 stream
->chan
->lost_packets
++;
1176 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &len
);
1178 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1179 goto error_put_subbuf
;
1182 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1184 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1185 goto error_put_subbuf
;
1188 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1190 goto error_put_subbuf
;
1193 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, padded_len
);
1194 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1195 stream
, &subbuf_view
, padded_len
- len
);
1197 if (read_len
!= len
) {
1199 goto error_put_subbuf
;
1202 if (read_len
!= padded_len
) {
1204 goto error_put_subbuf
;
1208 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
1210 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1211 goto error_close_stream
;
1213 consumed_pos
+= stream
->max_sb_size
;
1216 /* Simply close the stream so we can use it on the next snapshot. */
1217 consumer_stream_close_output(stream
);
1218 pthread_mutex_unlock(&stream
->lock
);
1225 if (lttng_ust_ctl_put_subbuf(stream
->ustream
) < 0) {
1226 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1229 consumer_stream_close_output(stream
);
1231 pthread_mutex_unlock(&stream
->lock
);
1236 static void metadata_stream_reset_cache_consumed_position(struct lttng_consumer_stream
*stream
)
1238 ASSERT_LOCKED(stream
->lock
);
1240 DBG("Reset metadata cache of session %" PRIu64
, stream
->chan
->session_id
);
1241 stream
->ust_metadata_pushed
= 0;
1245 * Receive the metadata updates from the sessiond. Supports receiving
1246 * overlapping metadata, but is needs to always belong to a contiguous
1247 * range starting from 0.
1248 * Be careful about the locks held when calling this function: it needs
1249 * the metadata cache flush to concurrently progress in order to
1252 int lttng_ustconsumer_recv_metadata(int sock
,
1257 struct lttng_consumer_channel
*channel
,
1261 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1263 enum consumer_metadata_cache_write_status cache_write_status
;
1265 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1267 metadata_str
= calloc
<char>(len
);
1268 if (!metadata_str
) {
1269 PERROR("zmalloc metadata string");
1270 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1274 health_code_update();
1276 /* Receive metadata string. */
1277 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1279 /* Session daemon is dead so return gracefully. */
1284 health_code_update();
1286 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1287 cache_write_status
= consumer_metadata_cache_write(
1288 channel
->metadata_cache
, offset
, len
, version
, metadata_str
);
1289 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1290 switch (cache_write_status
) {
1291 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1293 * The write entirely overlapped with existing contents of the
1294 * same metadata version (same content); there is nothing to do.
1297 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1299 * The metadata cache was invalidated (previously pushed
1300 * content has been overwritten). Reset the stream's consumed
1301 * metadata position to ensure the metadata poll thread consumes
1306 * channel::metadata_stream can be null when the metadata
1307 * channel is under a snapshot session type. No need to update
1308 * the stream position in that scenario.
1310 if (channel
->metadata_stream
!= nullptr) {
1311 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1312 metadata_stream_reset_cache_consumed_position(channel
->metadata_stream
);
1313 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1315 /* Validate we are in snapshot mode. */
1316 LTTNG_ASSERT(!channel
->monitor
);
1319 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1321 * In both cases, the metadata poll thread has new data to
1324 ret
= consumer_metadata_wakeup_pipe(channel
);
1326 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1330 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
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.
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
,
1367 struct pollfd
*consumer_sockpoll
)
1370 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1371 struct lttcomm_consumer_msg msg
;
1372 struct lttng_consumer_channel
*channel
= nullptr;
1374 health_code_update();
1379 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1380 if (ret_recv
!= sizeof(msg
)) {
1381 DBG("Consumer received unexpected message size %zd (expects %zu)",
1385 * The ret value might 0 meaning an orderly shutdown but this is ok
1386 * since the caller handles this.
1389 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1396 health_code_update();
1399 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1401 health_code_update();
1403 /* relayd needs RCU read-side lock */
1406 switch (msg
.cmd_type
) {
1407 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1409 uint32_t major
= msg
.u
.relayd_sock
.major
;
1410 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
1411 enum lttcomm_sock_proto protocol
=
1412 (enum lttcomm_sock_proto
) msg
.u
.relayd_sock
.relayd_socket_protocol
;
1414 /* Session daemon status message are handled in the following call. */
1415 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1416 msg
.u
.relayd_sock
.type
,
1420 msg
.u
.relayd_sock
.session_id
,
1421 msg
.u
.relayd_sock
.relayd_session_id
,
1427 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1429 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1430 struct consumer_relayd_sock_pair
*relayd
;
1432 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1434 /* Get relayd reference if exists. */
1435 relayd
= consumer_find_relayd(index
);
1436 if (relayd
== nullptr) {
1437 DBG("Unable to find relayd %" PRIu64
, index
);
1438 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1442 * Each relayd socket pair has a refcount of stream attached to it
1443 * which tells if the relayd is still active or not depending on the
1446 * This will set the destroy flag of the relayd object and destroy it
1447 * if the refcount reaches zero when called.
1449 * The destroy can happen either here or when a stream fd hangs up.
1452 consumer_flag_relayd_for_destroy(relayd
);
1455 goto end_msg_sessiond
;
1457 case LTTNG_CONSUMER_UPDATE_STREAM
:
1462 case LTTNG_CONSUMER_DATA_PENDING
:
1464 int is_data_pending
;
1466 uint64_t id
= msg
.u
.data_pending
.session_id
;
1468 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1470 is_data_pending
= consumer_data_pending(id
);
1472 /* Send back returned value to session daemon */
1473 ret_send
= lttcomm_send_unix_sock(sock
, &is_data_pending
, sizeof(is_data_pending
));
1475 DBG("Error when sending the data pending ret code: %zd", ret_send
);
1480 * No need to send back a status message since the data pending
1481 * returned value is the response.
1485 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1487 int ret_ask_channel
, ret_add_channel
, ret_send
;
1488 struct lttng_ust_ctl_consumer_channel_attr attr
;
1489 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1490 const struct lttng_credentials buffer_credentials
= {
1491 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1492 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1495 /* Create a plain object and reserve a channel key. */
1496 channel
= consumer_allocate_channel(
1497 msg
.u
.ask_channel
.key
,
1498 msg
.u
.ask_channel
.session_id
,
1499 msg
.u
.ask_channel
.chunk_id
.is_set
? &chunk_id
: nullptr,
1500 msg
.u
.ask_channel
.pathname
,
1501 msg
.u
.ask_channel
.name
,
1502 msg
.u
.ask_channel
.relayd_id
,
1503 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1504 msg
.u
.ask_channel
.tracefile_size
,
1505 msg
.u
.ask_channel
.tracefile_count
,
1506 msg
.u
.ask_channel
.session_id_per_pid
,
1507 msg
.u
.ask_channel
.monitor
,
1508 msg
.u
.ask_channel
.live_timer_interval
,
1509 msg
.u
.ask_channel
.is_live
,
1510 msg
.u
.ask_channel
.root_shm_path
,
1511 msg
.u
.ask_channel
.shm_path
);
1513 goto end_channel_error
;
1516 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
, buffer_credentials
);
1519 * Assign UST application UID to the channel. This value is ignored for
1520 * per PID buffers. This is specific to UST thus setting this after the
1523 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1525 /* Build channel attributes from received message. */
1526 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1527 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1528 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1529 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1530 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1531 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1532 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1533 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1535 /* Match channel buffer type to the UST abi. */
1536 switch (msg
.u
.ask_channel
.output
) {
1537 case LTTNG_EVENT_MMAP
:
1539 attr
.output
= LTTNG_UST_ABI_MMAP
;
1543 /* Translate and save channel type. */
1544 switch (msg
.u
.ask_channel
.type
) {
1545 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1546 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1547 attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1549 * Set refcount to 1 for owner. Below, we will
1550 * pass ownership to the
1551 * consumer_thread_channel_poll() thread.
1553 channel
->refcount
= 1;
1555 case LTTNG_UST_ABI_CHAN_METADATA
:
1556 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1557 attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1564 health_code_update();
1566 ret_ask_channel
= ask_channel(ctx
, channel
, &attr
);
1567 if (ret_ask_channel
< 0) {
1568 goto end_channel_error
;
1571 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1574 ret_allocate
= consumer_metadata_cache_allocate(channel
);
1575 if (ret_allocate
< 0) {
1576 ERR("Allocating metadata cache");
1577 goto end_channel_error
;
1579 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1580 attr
.switch_timer_interval
= 0;
1582 int monitor_start_ret
;
1584 consumer_timer_live_start(channel
, msg
.u
.ask_channel
.live_timer_interval
);
1585 monitor_start_ret
= consumer_timer_monitor_start(
1586 channel
, msg
.u
.ask_channel
.monitor_timer_interval
);
1587 if (monitor_start_ret
< 0) {
1588 ERR("Starting channel monitoring timer failed");
1589 goto end_channel_error
;
1593 health_code_update();
1596 * Add the channel to the internal state AFTER all streams were created
1597 * and successfully sent to session daemon. This way, all streams must
1598 * be ready before this channel is visible to the threads.
1599 * If add_channel succeeds, ownership of the channel is
1600 * passed to consumer_thread_channel_poll().
1602 ret_add_channel
= add_channel(channel
, ctx
);
1603 if (ret_add_channel
< 0) {
1604 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1605 if (channel
->switch_timer_enabled
== 1) {
1606 consumer_timer_switch_stop(channel
);
1608 consumer_metadata_cache_destroy(channel
);
1610 if (channel
->live_timer_enabled
== 1) {
1611 consumer_timer_live_stop(channel
);
1613 if (channel
->monitor_timer_enabled
== 1) {
1614 consumer_timer_monitor_stop(channel
);
1616 goto end_channel_error
;
1619 health_code_update();
1622 * Channel and streams are now created. Inform the session daemon that
1623 * everything went well and should wait to receive the channel and
1624 * streams with ustctl API.
1626 ret_send
= consumer_send_status_channel(sock
, channel
);
1629 * There is probably a problem on the socket.
1636 case LTTNG_CONSUMER_GET_CHANNEL
:
1638 int ret
, relayd_err
= 0;
1639 uint64_t key
= msg
.u
.get_channel
.key
;
1640 struct lttng_consumer_channel
*found_channel
;
1642 found_channel
= consumer_find_channel(key
);
1643 if (!found_channel
) {
1644 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1645 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1646 goto end_get_channel
;
1649 health_code_update();
1651 /* Send the channel to sessiond (and relayd, if applicable). */
1652 ret
= send_channel_to_sessiond_and_relayd(sock
, found_channel
, ctx
, &relayd_err
);
1656 * We were unable to send to the relayd the stream so avoid
1657 * sending back a fatal error to the thread since this is OK
1658 * and the consumer can continue its work. The above call
1659 * has sent the error status message to the sessiond.
1661 goto end_get_channel_nosignal
;
1664 * The communicaton was broken hence there is a bad state between
1665 * the consumer and sessiond so stop everything.
1667 goto error_get_channel_fatal
;
1670 health_code_update();
1673 * In no monitor mode, the streams ownership is kept inside the channel
1674 * so don't send them to the data thread.
1676 if (!found_channel
->monitor
) {
1677 goto end_get_channel
;
1680 ret
= send_streams_to_thread(found_channel
, ctx
);
1683 * If we are unable to send the stream to the thread, there is
1684 * a big problem so just stop everything.
1686 goto error_get_channel_fatal
;
1688 /* List MUST be empty after or else it could be reused. */
1689 LTTNG_ASSERT(cds_list_empty(&found_channel
->streams
.head
));
1691 goto end_msg_sessiond
;
1692 error_get_channel_fatal
:
1694 end_get_channel_nosignal
:
1697 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1699 uint64_t key
= msg
.u
.destroy_channel
.key
;
1702 * Only called if streams have not been sent to stream
1703 * manager thread. However, channel has been sent to
1704 * channel manager thread.
1706 notify_thread_del_channel(ctx
, key
);
1707 goto end_msg_sessiond
;
1709 case LTTNG_CONSUMER_CLOSE_METADATA
:
1713 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1715 ret_code
= (lttcomm_return_code
) ret
;
1718 goto end_msg_sessiond
;
1720 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1724 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1726 ret_code
= (lttcomm_return_code
) ret
;
1729 goto end_msg_sessiond
;
1731 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1735 ret
= clear_quiescent_channel(msg
.u
.clear_quiescent_channel
.key
);
1737 ret_code
= (lttcomm_return_code
) ret
;
1740 goto end_msg_sessiond
;
1742 case LTTNG_CONSUMER_PUSH_METADATA
:
1745 uint64_t len
= msg
.u
.push_metadata
.len
;
1746 uint64_t key
= msg
.u
.push_metadata
.key
;
1747 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1748 uint64_t version
= msg
.u
.push_metadata
.version
;
1749 struct lttng_consumer_channel
*found_channel
;
1751 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1753 found_channel
= consumer_find_channel(key
);
1754 if (!found_channel
) {
1756 * This is possible if the metadata creation on the consumer side
1757 * is in flight vis-a-vis a concurrent push metadata from the
1758 * session daemon. Simply return that the channel failed and the
1759 * session daemon will handle that message correctly considering
1760 * that this race is acceptable thus the DBG() statement here.
1762 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1763 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1764 goto end_push_metadata_msg_sessiond
;
1767 health_code_update();
1771 * There is nothing to receive. We have simply
1772 * checked whether the channel can be found.
1774 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1775 goto end_push_metadata_msg_sessiond
;
1778 /* Tell session daemon we are ready to receive the metadata. */
1779 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1781 /* Somehow, the session daemon is not responding anymore. */
1782 goto error_push_metadata_fatal
;
1785 health_code_update();
1787 /* Wait for more data. */
1788 health_poll_entry();
1789 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1792 goto error_push_metadata_fatal
;
1795 health_code_update();
1797 ret
= lttng_ustconsumer_recv_metadata(
1798 sock
, key
, offset
, len
, version
, found_channel
, 0, 1);
1800 /* error receiving from sessiond */
1801 goto error_push_metadata_fatal
;
1803 ret_code
= (lttcomm_return_code
) ret
;
1804 goto end_push_metadata_msg_sessiond
;
1806 end_push_metadata_msg_sessiond
:
1807 goto end_msg_sessiond
;
1808 error_push_metadata_fatal
:
1811 case LTTNG_CONSUMER_SETUP_METADATA
:
1815 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1817 ret_code
= (lttcomm_return_code
) ret
;
1819 goto end_msg_sessiond
;
1821 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1823 struct lttng_consumer_channel
*found_channel
;
1824 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1827 found_channel
= consumer_find_channel(key
);
1828 if (!found_channel
) {
1829 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1830 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1832 if (msg
.u
.snapshot_channel
.metadata
) {
1835 ret_snapshot
= snapshot_metadata(found_channel
,
1837 msg
.u
.snapshot_channel
.pathname
,
1838 msg
.u
.snapshot_channel
.relayd_id
,
1840 if (ret_snapshot
< 0) {
1841 ERR("Snapshot metadata failed");
1842 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1847 ret_snapshot
= snapshot_channel(
1850 msg
.u
.snapshot_channel
.pathname
,
1851 msg
.u
.snapshot_channel
.relayd_id
,
1852 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1854 if (ret_snapshot
< 0) {
1855 ERR("Snapshot channel failed");
1856 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1860 health_code_update();
1861 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1863 /* Somehow, the session daemon is not responding anymore. */
1866 health_code_update();
1869 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1872 uint64_t discarded_events
;
1873 struct lttng_ht_iter iter
;
1874 struct lttng_ht
*ht
;
1875 struct lttng_consumer_stream
*stream
;
1876 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1877 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1879 DBG("UST consumer discarded events command for session id %" PRIu64
, id
);
1881 pthread_mutex_lock(&the_consumer_data
.lock
);
1883 ht
= the_consumer_data
.stream_list_ht
;
1886 * We only need a reference to the channel, but they are not
1887 * directly indexed, so we just use the first matching stream
1888 * to extract the information we need, we default to 0 if not
1889 * found (no events are dropped if the channel is not yet in
1892 discarded_events
= 0;
1893 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1894 ht
->hash_fct(&id
, lttng_ht_seed
),
1899 node_session_id
.node
)
1901 if (stream
->chan
->key
== key
) {
1902 discarded_events
= stream
->chan
->discarded_events
;
1906 pthread_mutex_unlock(&the_consumer_data
.lock
);
1909 DBG("UST consumer discarded events command for session id %" PRIu64
1910 ", channel key %" PRIu64
,
1914 health_code_update();
1916 /* Send back returned value to session daemon */
1917 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1919 PERROR("send discarded events");
1925 case LTTNG_CONSUMER_LOST_PACKETS
:
1928 uint64_t lost_packets
;
1929 struct lttng_ht_iter iter
;
1930 struct lttng_ht
*ht
;
1931 struct lttng_consumer_stream
*stream
;
1932 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1933 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1935 DBG("UST consumer lost packets command for session id %" PRIu64
, id
);
1937 pthread_mutex_lock(&the_consumer_data
.lock
);
1939 ht
= the_consumer_data
.stream_list_ht
;
1942 * We only need a reference to the channel, but they are not
1943 * directly indexed, so we just use the first matching stream
1944 * to extract the information we need, we default to 0 if not
1945 * found (no packets lost if the channel is not yet in use).
1948 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1949 ht
->hash_fct(&id
, lttng_ht_seed
),
1954 node_session_id
.node
)
1956 if (stream
->chan
->key
== key
) {
1957 lost_packets
= stream
->chan
->lost_packets
;
1961 pthread_mutex_unlock(&the_consumer_data
.lock
);
1964 DBG("UST consumer lost packets command for session id %" PRIu64
1965 ", channel key %" PRIu64
,
1969 health_code_update();
1971 /* Send back returned value to session daemon */
1972 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
, sizeof(lost_packets
));
1974 PERROR("send lost packets");
1980 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1982 int channel_monitor_pipe
, ret_send
, ret_set_channel_monitor_pipe
;
1985 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1986 /* Successfully received the command's type. */
1987 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1992 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
, 1);
1993 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1994 ERR("Failed to receive channel monitor pipe");
1998 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1999 ret_set_channel_monitor_pipe
=
2000 consumer_timer_thread_set_channel_monitor_pipe(channel_monitor_pipe
);
2001 if (!ret_set_channel_monitor_pipe
) {
2005 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2006 /* Set the pipe as non-blocking. */
2007 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
2008 if (ret_fcntl
== -1) {
2009 PERROR("fcntl get flags of the channel monitoring pipe");
2014 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
, flags
| O_NONBLOCK
);
2015 if (ret_fcntl
== -1) {
2016 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2019 DBG("Channel monitor pipe set as non-blocking");
2021 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2023 goto end_msg_sessiond
;
2025 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2027 struct lttng_consumer_channel
*found_channel
;
2028 uint64_t key
= msg
.u
.rotate_channel
.key
;
2029 int ret_send_status
;
2031 found_channel
= consumer_find_channel(key
);
2032 if (!found_channel
) {
2033 DBG("Channel %" PRIu64
" not found", key
);
2034 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2039 * Sample the rotate position of all the streams in
2042 rotate_channel
= lttng_consumer_rotate_channel(
2043 found_channel
, key
, msg
.u
.rotate_channel
.relayd_id
);
2044 if (rotate_channel
< 0) {
2045 ERR("Rotate channel failed");
2046 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2049 health_code_update();
2052 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2053 if (ret_send_status
< 0) {
2054 /* Somehow, the session daemon is not responding anymore. */
2055 goto end_rotate_channel_nosignal
;
2059 * Rotate the streams that are ready right now.
2060 * FIXME: this is a second consecutive iteration over the
2061 * streams in a channel, there is probably a better way to
2062 * handle this, but it needs to be after the
2063 * consumer_send_status_msg() call.
2065 if (found_channel
) {
2066 int ret_rotate_read_streams
;
2068 ret_rotate_read_streams
=
2069 lttng_consumer_rotate_ready_streams(found_channel
, key
);
2070 if (ret_rotate_read_streams
< 0) {
2071 ERR("Rotate channel failed");
2075 end_rotate_channel_nosignal
:
2078 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2080 struct lttng_consumer_channel
*found_channel
;
2081 uint64_t key
= msg
.u
.clear_channel
.key
;
2082 int ret_send_status
;
2084 found_channel
= consumer_find_channel(key
);
2085 if (!found_channel
) {
2086 DBG("Channel %" PRIu64
" not found", key
);
2087 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2089 int ret_clear_channel
;
2091 ret_clear_channel
= lttng_consumer_clear_channel(found_channel
);
2092 if (ret_clear_channel
) {
2093 ERR("Clear channel failed key %" PRIu64
, key
);
2094 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
2097 health_code_update();
2099 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2100 if (ret_send_status
< 0) {
2101 /* Somehow, the session daemon is not responding anymore. */
2106 case LTTNG_CONSUMER_INIT
:
2108 int ret_send_status
;
2109 lttng_uuid sessiond_uuid
;
2111 std::copy(std::begin(msg
.u
.init
.sessiond_uuid
),
2112 std::end(msg
.u
.init
.sessiond_uuid
),
2113 sessiond_uuid
.begin());
2114 ret_code
= lttng_consumer_init_command(ctx
, sessiond_uuid
);
2115 health_code_update();
2116 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2117 if (ret_send_status
< 0) {
2118 /* Somehow, the session daemon is not responding anymore. */
2123 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2125 const struct lttng_credentials credentials
= {
2126 .uid
= LTTNG_OPTIONAL_INIT_VALUE(
2127 msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2128 .gid
= LTTNG_OPTIONAL_INIT_VALUE(
2129 msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2131 const bool is_local_trace
= !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2132 const uint64_t relayd_id
= msg
.u
.create_trace_chunk
.relayd_id
.value
;
2133 const char *chunk_override_name
= *msg
.u
.create_trace_chunk
.override_name
?
2134 msg
.u
.create_trace_chunk
.override_name
:
2136 struct lttng_directory_handle
*chunk_directory_handle
= nullptr;
2139 * The session daemon will only provide a chunk directory file
2140 * descriptor for local traces.
2142 if (is_local_trace
) {
2144 int ret_send_status
;
2147 /* Acnowledge the reception of the command. */
2148 ret_send_status
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
2149 if (ret_send_status
< 0) {
2150 /* Somehow, the session daemon is not responding anymore. */
2155 * Receive trace chunk domain dirfd.
2157 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2158 if (ret_recv
!= sizeof(chunk_dirfd
)) {
2159 ERR("Failed to receive trace chunk domain directory file descriptor");
2163 DBG("Received trace chunk domain directory fd (%d)", chunk_dirfd
);
2164 chunk_directory_handle
=
2165 lttng_directory_handle_create_from_dirfd(chunk_dirfd
);
2166 if (!chunk_directory_handle
) {
2167 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2168 if (close(chunk_dirfd
)) {
2169 PERROR("Failed to close chunk directory file descriptor");
2175 ret_code
= lttng_consumer_create_trace_chunk(
2176 !is_local_trace
? &relayd_id
: nullptr,
2177 msg
.u
.create_trace_chunk
.session_id
,
2178 msg
.u
.create_trace_chunk
.chunk_id
,
2179 (time_t) msg
.u
.create_trace_chunk
.creation_timestamp
,
2180 chunk_override_name
,
2181 msg
.u
.create_trace_chunk
.credentials
.is_set
? &credentials
: nullptr,
2182 chunk_directory_handle
);
2183 lttng_directory_handle_put(chunk_directory_handle
);
2184 goto end_msg_sessiond
;
2186 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2188 enum lttng_trace_chunk_command_type close_command
=
2189 (lttng_trace_chunk_command_type
) msg
.u
.close_trace_chunk
.close_command
.value
;
2190 const uint64_t relayd_id
= msg
.u
.close_trace_chunk
.relayd_id
.value
;
2191 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2192 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2195 ret_code
= lttng_consumer_close_trace_chunk(
2196 msg
.u
.close_trace_chunk
.relayd_id
.is_set
? &relayd_id
: nullptr,
2197 msg
.u
.close_trace_chunk
.session_id
,
2198 msg
.u
.close_trace_chunk
.chunk_id
,
2199 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2200 msg
.u
.close_trace_chunk
.close_command
.is_set
? &close_command
: nullptr,
2201 closed_trace_chunk_path
);
2202 reply
.ret_code
= ret_code
;
2203 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2204 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2205 if (ret
!= sizeof(reply
)) {
2208 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
, reply
.path_length
);
2209 if (ret
!= reply
.path_length
) {
2214 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2216 const uint64_t relayd_id
= msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2218 ret_code
= lttng_consumer_trace_chunk_exists(
2219 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
? &relayd_id
: nullptr,
2220 msg
.u
.trace_chunk_exists
.session_id
,
2221 msg
.u
.trace_chunk_exists
.chunk_id
);
2222 goto end_msg_sessiond
;
2224 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2226 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2227 struct lttng_consumer_channel
*found_channel
= consumer_find_channel(key
);
2229 if (found_channel
) {
2230 pthread_mutex_lock(&found_channel
->lock
);
2231 ret_code
= lttng_consumer_open_channel_packets(found_channel
);
2232 pthread_mutex_unlock(&found_channel
->lock
);
2235 * The channel could have disappeared in per-pid
2238 DBG("Channel %" PRIu64
" not found", key
);
2239 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2242 health_code_update();
2243 goto end_msg_sessiond
;
2251 * Return 1 to indicate success since the 0 value can be a socket
2252 * shutdown during the recv() or send() call.
2259 * The returned value here is not useful since either way we'll return 1 to
2260 * the caller because the session daemon socket management is done
2261 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2264 int ret_send_status
;
2266 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2267 if (ret_send_status
< 0) {
2277 consumer_del_channel(channel
);
2279 /* We have to send a status channel message indicating an error. */
2281 int ret_send_status
;
2283 ret_send_status
= consumer_send_status_channel(sock
, nullptr);
2284 if (ret_send_status
< 0) {
2285 /* Stop everything if session daemon can not be notified. */
2294 /* This will issue a consumer stop. */
2300 health_code_update();
2304 int lttng_ust_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
2306 LTTNG_ASSERT(stream
);
2307 LTTNG_ASSERT(stream
->ustream
);
2309 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer_active
);
2313 * Take a snapshot for a specific stream.
2315 * Returns 0 on success, < 0 on error
2317 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2319 LTTNG_ASSERT(stream
);
2320 LTTNG_ASSERT(stream
->ustream
);
2322 return lttng_ust_ctl_snapshot(stream
->ustream
);
2326 * Sample consumed and produced positions for a specific stream.
2328 * Returns 0 on success, < 0 on error.
2330 int lttng_ustconsumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2332 LTTNG_ASSERT(stream
);
2333 LTTNG_ASSERT(stream
->ustream
);
2335 return lttng_ust_ctl_snapshot_sample_positions(stream
->ustream
);
2339 * Get the produced position
2341 * Returns 0 on success, < 0 on error
2343 int lttng_ustconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2346 LTTNG_ASSERT(stream
);
2347 LTTNG_ASSERT(stream
->ustream
);
2350 return lttng_ust_ctl_snapshot_get_produced(stream
->ustream
, pos
);
2354 * Get the consumed position
2356 * Returns 0 on success, < 0 on error
2358 int lttng_ustconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2361 LTTNG_ASSERT(stream
);
2362 LTTNG_ASSERT(stream
->ustream
);
2365 return lttng_ust_ctl_snapshot_get_consumed(stream
->ustream
, pos
);
2368 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer
)
2370 LTTNG_ASSERT(stream
);
2371 LTTNG_ASSERT(stream
->ustream
);
2373 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer
);
2376 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2378 LTTNG_ASSERT(stream
);
2379 LTTNG_ASSERT(stream
->ustream
);
2381 return lttng_ust_ctl_clear_buffer(stream
->ustream
);
2384 int lttng_ustconsumer_get_current_timestamp(struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2386 LTTNG_ASSERT(stream
);
2387 LTTNG_ASSERT(stream
->ustream
);
2390 return lttng_ust_ctl_get_current_timestamp(stream
->ustream
, ts
);
2393 int lttng_ustconsumer_get_sequence_number(struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2395 LTTNG_ASSERT(stream
);
2396 LTTNG_ASSERT(stream
->ustream
);
2399 return lttng_ust_ctl_get_sequence_number(stream
->ustream
, seq
);
2403 * Called when the stream signals the consumer that it has hung up.
2405 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2407 LTTNG_ASSERT(stream
);
2408 LTTNG_ASSERT(stream
->ustream
);
2410 pthread_mutex_lock(&stream
->lock
);
2411 if (!stream
->quiescent
) {
2412 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 0) < 0) {
2413 ERR("Failed to flush buffer on stream hang-up");
2415 stream
->quiescent
= true;
2419 stream
->hangup_flush_done
= 1;
2420 pthread_mutex_unlock(&stream
->lock
);
2423 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2428 LTTNG_ASSERT(chan
->uchan
);
2429 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2431 if (chan
->switch_timer_enabled
== 1) {
2432 consumer_timer_switch_stop(chan
);
2434 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2437 ret
= close(chan
->stream_fds
[i
]);
2441 if (chan
->shm_path
[0]) {
2442 char shm_path
[PATH_MAX
];
2444 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2446 ERR("Cannot get stream shm path");
2448 ret
= run_as_unlink(shm_path
,
2449 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2450 chan
->buffer_credentials
)),
2451 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2452 chan
->buffer_credentials
)));
2454 PERROR("unlink %s", shm_path
);
2460 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2463 LTTNG_ASSERT(chan
->uchan
);
2464 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2466 consumer_metadata_cache_destroy(chan
);
2467 lttng_ust_ctl_destroy_channel(chan
->uchan
);
2468 /* Try to rmdir all directories under shm_path root. */
2469 if (chan
->root_shm_path
[0]) {
2470 (void) run_as_rmdir_recursive(
2471 chan
->root_shm_path
,
2472 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(chan
->buffer_credentials
)),
2473 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(chan
->buffer_credentials
)),
2474 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2476 free(chan
->stream_fds
);
2479 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2481 LTTNG_ASSERT(stream
);
2482 LTTNG_ASSERT(stream
->ustream
);
2484 if (stream
->chan
->switch_timer_enabled
== 1) {
2485 consumer_timer_switch_stop(stream
->chan
);
2487 lttng_ust_ctl_destroy_stream(stream
->ustream
);
2490 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2492 LTTNG_ASSERT(stream
);
2493 LTTNG_ASSERT(stream
->ustream
);
2495 return lttng_ust_ctl_stream_get_wakeup_fd(stream
->ustream
);
2498 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2500 LTTNG_ASSERT(stream
);
2501 LTTNG_ASSERT(stream
->ustream
);
2503 return lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
2507 * Write up to one packet from the metadata cache to the channel.
2509 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2510 * negative value on error.
2512 static int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2517 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2518 if (stream
->chan
->metadata_cache
->contents
.size
== stream
->ust_metadata_pushed
) {
2520 * In the context of a user space metadata channel, a
2521 * change in version can be detected in two ways:
2522 * 1) During the pre-consume of the `read_subbuffer` loop,
2523 * 2) When populating the metadata ring buffer (i.e. here).
2525 * This function is invoked when there is no metadata
2526 * available in the ring-buffer. If all data was consumed
2527 * up to the size of the metadata cache, there is no metadata
2528 * to insert in the ring-buffer.
2530 * However, the metadata version could still have changed (a
2531 * regeneration without any new data will yield the same cache
2534 * The cache's version is checked for a version change and the
2535 * consumed position is reset if one occurred.
2537 * This check is only necessary for the user space domain as
2538 * it has to manage the cache explicitly. If this reset was not
2539 * performed, no metadata would be consumed (and no reset would
2540 * occur as part of the pre-consume) until the metadata size
2541 * exceeded the cache size.
2543 if (stream
->metadata_version
!= stream
->chan
->metadata_cache
->version
) {
2544 metadata_stream_reset_cache_consumed_position(stream
);
2545 consumer_stream_metadata_set_version(stream
,
2546 stream
->chan
->metadata_cache
->version
);
2553 write_len
= lttng_ust_ctl_write_one_packet_to_channel(
2554 stream
->chan
->uchan
,
2555 &stream
->chan
->metadata_cache
->contents
.data
[stream
->ust_metadata_pushed
],
2556 stream
->chan
->metadata_cache
->contents
.size
- stream
->ust_metadata_pushed
);
2557 LTTNG_ASSERT(write_len
!= 0);
2558 if (write_len
< 0) {
2559 ERR("Writing one metadata packet");
2563 stream
->ust_metadata_pushed
+= write_len
;
2565 LTTNG_ASSERT(stream
->chan
->metadata_cache
->contents
.size
>= stream
->ust_metadata_pushed
);
2569 * Switch packet (but don't open the next one) on every commit of
2570 * a metadata packet. Since the subbuffer is fully filled (with padding,
2571 * if needed), the stream is "quiescent" after this commit.
2573 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 1)) {
2574 ERR("Failed to flush buffer while committing one metadata packet");
2577 stream
->quiescent
= true;
2580 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2585 * Sync metadata meaning request them to the session daemon and snapshot to the
2586 * metadata thread can consumer them.
2588 * Metadata stream lock is held here, but we need to release it when
2589 * interacting with sessiond, else we cause a deadlock with live
2590 * awaiting on metadata to be pushed out.
2592 * The RCU read side lock must be held by the caller.
2594 enum sync_metadata_status
2595 lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2596 struct lttng_consumer_stream
*metadata_stream
)
2599 enum sync_metadata_status status
;
2600 struct lttng_consumer_channel
*metadata_channel
;
2603 LTTNG_ASSERT(metadata_stream
);
2604 ASSERT_RCU_READ_LOCKED();
2606 metadata_channel
= metadata_stream
->chan
;
2607 pthread_mutex_unlock(&metadata_stream
->lock
);
2609 * Request metadata from the sessiond, but don't wait for the flush
2610 * because we locked the metadata thread.
2612 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2613 pthread_mutex_lock(&metadata_stream
->lock
);
2615 status
= SYNC_METADATA_STATUS_ERROR
;
2620 * The metadata stream and channel can be deleted while the
2621 * metadata stream lock was released. The streamed is checked
2622 * for deletion before we use it further.
2624 * Note that it is safe to access a logically-deleted stream since its
2625 * existence is still guaranteed by the RCU read side lock. However,
2626 * it should no longer be used. The close/deletion of the metadata
2627 * channel and stream already guarantees that all metadata has been
2628 * consumed. Therefore, there is nothing left to do in this function.
2630 if (consumer_stream_is_deleted(metadata_stream
)) {
2631 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2632 metadata_stream
->key
);
2633 status
= SYNC_METADATA_STATUS_NO_DATA
;
2637 ret
= commit_one_metadata_packet(metadata_stream
);
2639 status
= SYNC_METADATA_STATUS_ERROR
;
2641 } else if (ret
> 0) {
2642 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2643 } else /* ret == 0 */ {
2644 status
= SYNC_METADATA_STATUS_NO_DATA
;
2648 ret
= lttng_ust_ctl_snapshot(metadata_stream
->ustream
);
2650 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d",
2652 status
= SYNC_METADATA_STATUS_ERROR
;
2661 * Return 0 on success else a negative value.
2663 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2664 struct lttng_consumer_local_data
*ctx
)
2667 struct lttng_ust_ctl_consumer_stream
*ustream
;
2669 LTTNG_ASSERT(stream
);
2672 ustream
= stream
->ustream
;
2675 * First, we are going to check if there is a new subbuffer available
2676 * before reading the stream wait_fd.
2678 /* Get the next subbuffer */
2679 ret
= lttng_ust_ctl_get_next_subbuf(ustream
);
2681 /* No more data found, flag the stream. */
2682 stream
->has_data
= 0;
2687 ret
= lttng_ust_ctl_put_subbuf(ustream
);
2690 /* This stream still has data. Flag it and wake up the data thread. */
2691 stream
->has_data
= 1;
2693 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2696 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2697 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2702 /* The wake up pipe has been notified. */
2703 ctx
->has_wakeup
= 1;
2711 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2716 * We can consume the 1 byte written into the wait_fd by
2717 * UST. Don't trigger error if we cannot read this one byte
2718 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2720 * This is only done when the stream is monitored by a thread,
2721 * before the flush is done after a hangup and if the stream
2722 * is not flagged with data since there might be nothing to
2723 * consume in the wait fd but still have data available
2724 * flagged by the consumer wake up pipe.
2726 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2730 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2731 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2739 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2740 struct stream_subbuffer
*subbuf
)
2744 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2749 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
,
2750 &subbuf
->info
.data
.padded_subbuf_size
);
2759 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2760 struct stream_subbuffer
*subbuf
)
2764 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2769 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2775 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2776 struct stream_subbuffer
*subbuf
)
2780 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2785 ret
= lttng_ust_ctl_get_packet_size(stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2787 PERROR("Failed to get sub-buffer packet size");
2791 ret
= lttng_ust_ctl_get_content_size(stream
->ustream
, &subbuf
->info
.data
.content_size
);
2793 PERROR("Failed to get sub-buffer content size");
2797 ret
= lttng_ust_ctl_get_timestamp_begin(stream
->ustream
,
2798 &subbuf
->info
.data
.timestamp_begin
);
2800 PERROR("Failed to get sub-buffer begin timestamp");
2804 ret
= lttng_ust_ctl_get_timestamp_end(stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2806 PERROR("Failed to get sub-buffer end timestamp");
2810 ret
= lttng_ust_ctl_get_events_discarded(stream
->ustream
,
2811 &subbuf
->info
.data
.events_discarded
);
2813 PERROR("Failed to get sub-buffer events discarded count");
2817 ret
= lttng_ust_ctl_get_sequence_number(stream
->ustream
,
2818 &subbuf
->info
.data
.sequence_number
.value
);
2820 /* May not be supported by older LTTng-modules. */
2821 if (ret
!= -ENOTTY
) {
2822 PERROR("Failed to get sub-buffer sequence number");
2826 subbuf
->info
.data
.sequence_number
.is_set
= true;
2829 ret
= lttng_ust_ctl_get_stream_id(stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2831 PERROR("Failed to get stream id");
2835 ret
= lttng_ust_ctl_get_instance_id(stream
->ustream
,
2836 &subbuf
->info
.data
.stream_instance_id
.value
);
2838 /* May not be supported by older LTTng-modules. */
2839 if (ret
!= -ENOTTY
) {
2840 PERROR("Failed to get stream instance id");
2844 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2850 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2851 struct stream_subbuffer
*subbuffer
)
2856 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(stream
, subbuffer
);
2861 ret
= get_current_subbuf_addr(stream
, &addr
);
2866 subbuffer
->buffer
.buffer
=
2867 lttng_buffer_view_init(addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2868 LTTNG_ASSERT(subbuffer
->buffer
.buffer
.data
!= nullptr);
2873 static enum get_next_subbuffer_status
get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2874 struct stream_subbuffer
*subbuffer
)
2877 enum get_next_subbuffer_status status
;
2879 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2882 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
2887 * The caller only expects -ENODATA when there is no data to
2888 * read, but the kernel tracer returns -EAGAIN when there is
2889 * currently no data for a non-finalized stream, and -ENODATA
2890 * when there is no data for a finalized stream. Those can be
2891 * combined into a -ENODATA return value.
2893 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2896 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2900 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2902 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2909 static enum get_next_subbuffer_status
2910 get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2911 struct stream_subbuffer
*subbuffer
)
2918 unsigned long consumed_pos
, produced_pos
;
2919 enum get_next_subbuffer_status status
;
2922 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2924 got_subbuffer
= true;
2926 got_subbuffer
= false;
2927 if (ret
!= -EAGAIN
) {
2929 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2935 * Determine if the cache is empty and ensure that a sub-buffer
2936 * is made available if the cache is not empty.
2938 if (!got_subbuffer
) {
2939 ret
= commit_one_metadata_packet(stream
);
2940 if (ret
< 0 && ret
!= -ENOBUFS
) {
2941 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2943 } else if (ret
== 0) {
2944 /* Not an error, the cache is empty. */
2946 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2949 cache_empty
= false;
2952 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2953 cache_empty
= stream
->chan
->metadata_cache
->contents
.size
==
2954 stream
->ust_metadata_pushed
;
2955 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2957 } while (!got_subbuffer
);
2959 /* Populate sub-buffer infos and view. */
2960 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2962 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2966 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2969 * -EAGAIN is not expected since we got a sub-buffer and haven't
2970 * pushed the consumption position yet (on put_next).
2972 PERROR("Failed to take a snapshot of metadata buffer positions");
2973 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2977 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2979 PERROR("Failed to get metadata consumed position");
2980 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2984 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2986 PERROR("Failed to get metadata produced position");
2987 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2991 /* Last sub-buffer of the ring buffer ? */
2992 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2995 * The sessiond registry lock ensures that coherent units of metadata
2996 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2997 * acquired, the cache is empty, and it is the only available sub-buffer
2998 * available, it is safe to assume that it is "coherent".
3000 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
3002 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
3003 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
3008 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
3009 struct stream_subbuffer
*subbuffer
__attribute__((unused
)))
3011 const int ret
= lttng_ust_ctl_put_next_subbuf(stream
->ustream
);
3013 LTTNG_ASSERT(ret
== 0);
3017 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3018 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
3020 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3021 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3024 static int lttng_ustconsumer_set_stream_ops(struct lttng_consumer_stream
*stream
)
3028 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3029 if (stream
->metadata_flag
) {
3030 stream
->read_subbuffer_ops
.get_next_subbuffer
= get_next_subbuffer_metadata
;
3031 stream
->read_subbuffer_ops
.extract_subbuffer_info
= extract_metadata_subbuffer_info
;
3032 stream
->read_subbuffer_ops
.reset_metadata
=
3033 metadata_stream_reset_cache_consumed_position
;
3034 if (stream
->chan
->is_live
) {
3035 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3036 ret
= consumer_stream_enable_metadata_bucketization(stream
);
3042 stream
->read_subbuffer_ops
.get_next_subbuffer
= get_next_subbuffer
;
3043 stream
->read_subbuffer_ops
.extract_subbuffer_info
= extract_data_subbuffer_info
;
3044 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3045 if (stream
->chan
->is_live
) {
3046 stream
->read_subbuffer_ops
.send_live_beacon
= consumer_flush_ust_index
;
3050 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3056 * Called when a stream is created.
3058 * Return 0 on success or else a negative value.
3060 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3064 LTTNG_ASSERT(stream
);
3067 * Don't create anything if this is set for streaming or if there is
3068 * no current trace chunk on the parent channel.
3070 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3071 stream
->chan
->trace_chunk
) {
3072 ret
= consumer_stream_create_output_files(stream
, true);
3078 lttng_ustconsumer_set_stream_ops(stream
);
3086 * Check if data is still being extracted from the buffers for a specific
3087 * stream. Consumer data lock MUST be acquired before calling this function
3088 * and the stream lock.
3090 * Return 1 if the traced data are still getting read else 0 meaning that the
3091 * data is available for trace viewer reading.
3093 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3097 LTTNG_ASSERT(stream
);
3098 LTTNG_ASSERT(stream
->ustream
);
3099 ASSERT_LOCKED(stream
->lock
);
3101 DBG("UST consumer checking data pending");
3103 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3108 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3109 uint64_t contiguous
, pushed
;
3111 /* Ease our life a bit. */
3112 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3113 contiguous
= stream
->chan
->metadata_cache
->contents
.size
;
3114 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3115 pushed
= stream
->ust_metadata_pushed
;
3118 * We can simply check whether all contiguously available data
3119 * has been pushed to the ring buffer, since the push operation
3120 * is performed within get_next_subbuf(), and because both
3121 * get_next_subbuf() and put_next_subbuf() are issued atomically
3122 * thanks to the stream lock within
3123 * lttng_ustconsumer_read_subbuffer(). This basically means that
3124 * whetnever ust_metadata_pushed is incremented, the associated
3125 * metadata has been consumed from the metadata stream.
3127 DBG("UST consumer metadata pending check: contiguous %" PRIu64
3128 " vs pushed %" PRIu64
,
3131 LTTNG_ASSERT(((int64_t) (contiguous
- pushed
)) >= 0);
3132 if ((contiguous
!= pushed
) ||
3133 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3134 ret
= 1; /* Data is pending */
3138 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
3141 * There is still data so let's put back this
3144 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
3145 LTTNG_ASSERT(ret
== 0);
3146 ret
= 1; /* Data is pending */
3151 /* Data is NOT pending so ready to be read. */
3159 * Stop a given metadata channel timer if enabled and close the wait fd which
3160 * is the poll pipe of the metadata stream.
3162 * This MUST be called with the metadata channel lock acquired.
3164 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3168 LTTNG_ASSERT(metadata
);
3169 LTTNG_ASSERT(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3171 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3173 if (metadata
->switch_timer_enabled
== 1) {
3174 consumer_timer_switch_stop(metadata
);
3177 if (!metadata
->metadata_stream
) {
3182 * Closing write side so the thread monitoring the stream wakes up if any
3183 * and clean the metadata stream.
3185 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3186 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3188 PERROR("closing metadata pipe write side");
3190 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3198 * Close every metadata stream wait fd of the metadata hash table. This
3199 * function MUST be used very carefully so not to run into a race between the
3200 * metadata thread handling streams and this function closing their wait fd.
3202 * For UST, this is used when the session daemon hangs up. Its the metadata
3203 * producer so calling this is safe because we are assured that no state change
3204 * can occur in the metadata thread for the streams in the hash table.
3206 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3208 struct lttng_ht_iter iter
;
3209 struct lttng_consumer_stream
*stream
;
3211 LTTNG_ASSERT(metadata_ht
);
3212 LTTNG_ASSERT(metadata_ht
->ht
);
3214 DBG("UST consumer closing all metadata streams");
3217 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
3218 health_code_update();
3220 pthread_mutex_lock(&stream
->chan
->lock
);
3221 lttng_ustconsumer_close_metadata(stream
->chan
);
3222 pthread_mutex_unlock(&stream
->chan
->lock
);
3227 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3231 ret
= lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
3233 ERR("Unable to close wakeup fd");
3238 * Please refer to consumer-timer.c before adding any lock within this
3239 * function or any of its callees. Timers have a very strict locking
3240 * semantic with respect to teardown. Failure to respect this semantic
3241 * introduces deadlocks.
3243 * DON'T hold the metadata lock when calling this function, else this
3244 * can cause deadlock involving consumer awaiting for metadata to be
3245 * pushed out due to concurrent interaction with the session daemon.
3247 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3248 struct lttng_consumer_channel
*channel
,
3252 struct lttcomm_metadata_request_msg request
;
3253 struct lttcomm_consumer_msg msg
;
3254 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3255 uint64_t len
, key
, offset
, version
;
3258 LTTNG_ASSERT(channel
);
3259 LTTNG_ASSERT(channel
->metadata_cache
);
3261 memset(&request
, 0, sizeof(request
));
3263 /* send the metadata request to sessiond */
3264 switch (the_consumer_data
.type
) {
3265 case LTTNG_CONSUMER64_UST
:
3266 request
.bits_per_long
= 64;
3268 case LTTNG_CONSUMER32_UST
:
3269 request
.bits_per_long
= 32;
3272 request
.bits_per_long
= 0;
3276 request
.session_id
= channel
->session_id
;
3277 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3279 * Request the application UID here so the metadata of that application can
3280 * be sent back. The channel UID corresponds to the user UID of the session
3281 * used for the rights on the stream file(s).
3283 request
.uid
= channel
->ust_app_uid
;
3284 request
.key
= channel
->key
;
3286 DBG("Sending metadata request to sessiond, session id %" PRIu64
", per-pid %" PRIu64
3287 ", app UID %u and channel key %" PRIu64
,
3289 request
.session_id_per_pid
,
3293 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3295 health_code_update();
3297 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
, sizeof(request
));
3299 ERR("Asking metadata to sessiond");
3303 health_code_update();
3305 /* Receive the metadata from sessiond */
3306 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
, sizeof(msg
));
3307 if (ret
!= sizeof(msg
)) {
3308 DBG("Consumer received unexpected message size %d (expects %zu)", ret
, sizeof(msg
));
3309 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3311 * The ret value might 0 meaning an orderly shutdown but this is ok
3312 * since the caller handles this.
3317 health_code_update();
3319 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3320 /* No registry found */
3321 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret_code
);
3324 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3325 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3330 len
= msg
.u
.push_metadata
.len
;
3331 key
= msg
.u
.push_metadata
.key
;
3332 offset
= msg
.u
.push_metadata
.target_offset
;
3333 version
= msg
.u
.push_metadata
.version
;
3335 LTTNG_ASSERT(key
== channel
->key
);
3337 DBG("No new metadata to receive for key %" PRIu64
, key
);
3340 health_code_update();
3342 /* Tell session daemon we are ready to receive the metadata. */
3343 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
, LTTCOMM_CONSUMERD_SUCCESS
);
3344 if (ret
< 0 || len
== 0) {
3346 * Somehow, the session daemon is not responding anymore or there is
3347 * nothing to receive.
3352 health_code_update();
3354 ret
= lttng_ustconsumer_recv_metadata(
3355 ctx
->consumer_metadata_socket
, key
, offset
, len
, version
, channel
, timer
, wait
);
3358 * Only send the status msg if the sessiond is alive meaning a positive
3361 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3366 health_code_update();
3368 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3373 * Return the ustctl call for the get stream id.
3375 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
, uint64_t *stream_id
)
3377 LTTNG_ASSERT(stream
);
3378 LTTNG_ASSERT(stream_id
);
3380 return lttng_ust_ctl_get_stream_id(stream
->ustream
, stream_id
);
3383 void lttng_ustconsumer_sigbus_handle(void *addr
)
3385 lttng_ust_ctl_sigbus_handle(addr
);