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
));
172 ERR("Consumer write %s stream to pipe %d",
173 stream
->metadata_flag
? "metadata" : "data",
174 lttng_pipe_get_writefd(stream_pipe
));
175 if (stream
->metadata_flag
) {
176 consumer_del_stream_for_metadata(stream
);
178 consumer_del_stream_for_data(stream
);
187 static int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
189 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
192 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
193 stream_shm_path
[PATH_MAX
- 1] = '\0';
194 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
199 strncat(stream_shm_path
, cpu_nr
, PATH_MAX
- strlen(stream_shm_path
) - 1);
206 * Create streams for the given channel using liblttng-ust-ctl.
207 * The channel lock must be acquired by the caller.
209 * Return 0 on success else a negative value.
211 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
212 struct lttng_consumer_local_data
*ctx
)
215 struct lttng_ust_ctl_consumer_stream
*ustream
;
216 struct lttng_consumer_stream
*stream
;
217 pthread_mutex_t
*current_stream_lock
= nullptr;
219 LTTNG_ASSERT(channel
);
223 * While a stream is available from ustctl. When NULL is returned, we've
224 * reached the end of the possible stream for the channel.
226 while ((ustream
= lttng_ust_ctl_create_stream(channel
->uchan
, cpu
))) {
228 int ust_metadata_pipe
[2];
230 health_code_update();
232 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
233 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
235 ERR("Create ust metadata poll pipe");
238 wait_fd
= ust_metadata_pipe
[0];
240 wait_fd
= lttng_ust_ctl_stream_get_wait_fd(ustream
);
243 /* Allocate consumer stream object. */
244 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
248 stream
->ustream
= ustream
;
250 * Store it so we can save multiple function calls afterwards since
251 * this value is used heavily in the stream threads. This is UST
252 * specific so this is why it's done after allocation.
254 stream
->wait_fd
= wait_fd
;
257 * Increment channel refcount since the channel reference has now been
258 * assigned in the allocation process above.
260 if (stream
->chan
->monitor
) {
261 uatomic_inc(&stream
->chan
->refcount
);
264 pthread_mutex_lock(&stream
->lock
);
265 current_stream_lock
= &stream
->lock
;
267 * Order is important this is why a list is used. On error, the caller
268 * should clean this list.
270 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
272 ret
= lttng_ust_ctl_get_max_subbuf_size(stream
->ustream
, &stream
->max_sb_size
);
274 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s", stream
->name
);
278 /* Do actions once stream has been received. */
279 if (ctx
->on_recv_stream
) {
280 ret
= ctx
->on_recv_stream(stream
);
286 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
289 stream
->relayd_stream_id
);
291 /* Set next CPU stream. */
292 channel
->streams
.count
= ++cpu
;
294 /* Keep stream reference when creating metadata. */
295 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
296 channel
->metadata_stream
= stream
;
297 if (channel
->monitor
) {
298 /* Set metadata poll pipe if we created one */
299 memcpy(stream
->ust_metadata_poll_pipe
,
301 sizeof(ust_metadata_pipe
));
304 pthread_mutex_unlock(&stream
->lock
);
305 current_stream_lock
= nullptr;
312 if (current_stream_lock
) {
313 pthread_mutex_unlock(current_stream_lock
);
318 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
320 const struct lttng_credentials
*session_credentials
)
322 char shm_path
[PATH_MAX
];
325 if (!channel
->shm_path
[0]) {
326 return shm_create_anonymous("ust-consumer");
328 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
332 return run_as_open(shm_path
,
333 O_RDWR
| O_CREAT
| O_EXCL
,
335 lttng_credentials_get_uid(session_credentials
),
336 lttng_credentials_get_gid(session_credentials
));
343 * Create an UST channel with the given attributes and send it to the session
344 * daemon using the ust ctl API.
346 * Return 0 on success or else a negative value.
348 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
349 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
350 struct lttng_ust_ctl_consumer_channel
**ust_chanp
)
352 int ret
, nr_stream_fds
, i
, j
;
354 struct lttng_ust_ctl_consumer_channel
*ust_channel
;
356 LTTNG_ASSERT(channel
);
358 LTTNG_ASSERT(ust_chanp
);
359 LTTNG_ASSERT(channel
->buffer_credentials
.is_set
);
361 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
362 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
363 "switch_timer_interval: %u, read_timer_interval: %u, "
364 "output: %d, type: %d",
368 attr
->switch_timer_interval
,
369 attr
->read_timer_interval
,
373 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
376 nr_stream_fds
= lttng_ust_ctl_get_nr_stream_per_channel();
377 stream_fds
= calloc
<int>(nr_stream_fds
);
382 for (i
= 0; i
< nr_stream_fds
; i
++) {
383 stream_fds
[i
] = open_ust_stream_fd(channel
, i
, &channel
->buffer_credentials
.value
);
384 if (stream_fds
[i
] < 0) {
389 ust_channel
= lttng_ust_ctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
394 channel
->nr_stream_fds
= nr_stream_fds
;
395 channel
->stream_fds
= stream_fds
;
396 *ust_chanp
= ust_channel
;
402 for (j
= i
- 1; j
>= 0; j
--) {
405 closeret
= close(stream_fds
[j
]);
409 if (channel
->shm_path
[0]) {
410 char shm_path
[PATH_MAX
];
412 closeret
= get_stream_shm_path(shm_path
, channel
->shm_path
, j
);
414 ERR("Cannot get stream shm path");
416 closeret
= run_as_unlink(shm_path
,
417 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
418 channel
->buffer_credentials
)),
419 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
420 channel
->buffer_credentials
)));
422 PERROR("unlink %s", shm_path
);
426 /* Try to rmdir all directories under shm_path root. */
427 if (channel
->root_shm_path
[0]) {
428 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
429 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
430 channel
->buffer_credentials
)),
431 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
432 channel
->buffer_credentials
)),
433 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
441 * Send a single given stream to the session daemon using the sock.
443 * Return 0 on success else a negative value.
445 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
449 LTTNG_ASSERT(stream
);
450 LTTNG_ASSERT(sock
>= 0);
452 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
454 /* Send stream to session daemon. */
455 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, stream
->ustream
);
465 * Send channel to sessiond and relayd if applicable.
467 * Return 0 on success or else a negative value.
469 static int send_channel_to_sessiond_and_relayd(int sock
,
470 struct lttng_consumer_channel
*channel
,
471 struct lttng_consumer_local_data
*ctx
,
474 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
475 struct lttng_consumer_stream
*stream
;
476 uint64_t net_seq_idx
= -1ULL;
478 LTTNG_ASSERT(channel
);
480 LTTNG_ASSERT(sock
>= 0);
482 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
484 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
485 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
486 health_code_update();
488 /* Try to send the stream to the relayd if one is available. */
489 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
492 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
495 * Flag that the relayd was the problem here probably due to a
496 * communicaton error on the socket.
501 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
503 if (net_seq_idx
== -1ULL) {
504 net_seq_idx
= stream
->net_seq_idx
;
509 /* Inform sessiond that we are about to send channel and streams. */
510 ret
= consumer_send_status_msg(sock
, ret_code
);
511 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
513 * Either the session daemon is not responding or the relayd died so we
519 /* Send channel to sessiond. */
520 ret
= lttng_ust_ctl_send_channel_to_sessiond(sock
, channel
->uchan
);
525 ret
= lttng_ust_ctl_channel_close_wakeup_fd(channel
->uchan
);
530 /* The channel was sent successfully to the sessiond at this point. */
531 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
532 health_code_update();
534 /* Send stream to session daemon. */
535 ret
= send_sessiond_stream(sock
, stream
);
541 /* Tell sessiond there is no more stream. */
542 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, nullptr);
547 DBG("UST consumer NULL stream sent to sessiond");
552 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
559 * Creates a channel and streams and add the channel it to the channel internal
560 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
563 * Return 0 on success or else, a negative value is returned and the channel
564 * MUST be destroyed by consumer_del_channel().
566 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
567 struct lttng_consumer_channel
*channel
,
568 struct lttng_ust_ctl_consumer_channel_attr
*attr
)
573 LTTNG_ASSERT(channel
);
577 * This value is still used by the kernel consumer since for the kernel,
578 * the stream ownership is not IN the consumer so we need to have the
579 * number of left stream that needs to be initialized so we can know when
580 * to delete the channel (see consumer.c).
582 * As for the user space tracer now, the consumer creates and sends the
583 * stream to the session daemon which only sends them to the application
584 * once every stream of a channel is received making this value useless
585 * because we they will be added to the poll thread before the application
586 * receives them. This ensures that a stream can not hang up during
587 * initilization of a channel.
589 channel
->nb_init_stream_left
= 0;
591 /* The reply msg status is handled in the following call. */
592 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
597 channel
->wait_fd
= lttng_ust_ctl_channel_get_wait_fd(channel
->uchan
);
600 * For the snapshots (no monitor), we create the metadata streams
601 * on demand, not during the channel creation.
603 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
608 /* Open all streams for this channel. */
609 pthread_mutex_lock(&channel
->lock
);
610 ret
= create_ust_streams(channel
, ctx
);
611 pthread_mutex_unlock(&channel
->lock
);
621 * Send all stream of a channel to the right thread handling it.
623 * On error, return a negative value else 0 on success.
625 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
626 struct lttng_consumer_local_data
*ctx
)
629 struct lttng_consumer_stream
*stream
, *stmp
;
631 LTTNG_ASSERT(channel
);
634 /* Send streams to the corresponding thread. */
635 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->streams
.head
, send_node
) {
636 health_code_update();
638 /* Sending the stream to the thread. */
639 ret
= send_stream_to_thread(stream
, ctx
);
642 * If we are unable to send the stream to the thread, there is
643 * a big problem so just stop everything.
654 * Flush channel's streams using the given key to retrieve the channel.
656 * Return 0 on success else an LTTng error code.
658 static int flush_channel(uint64_t chan_key
)
661 struct lttng_consumer_channel
*channel
;
662 struct lttng_consumer_stream
*stream
;
664 struct lttng_ht_iter iter
;
666 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
669 channel
= consumer_find_channel(chan_key
);
671 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
672 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
676 ht
= the_consumer_data
.stream_per_chan_id_ht
;
678 /* For each stream of the channel id, flush it. */
679 cds_lfht_for_each_entry_duplicate(ht
->ht
,
680 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
685 node_channel_id
.node
)
687 health_code_update();
689 pthread_mutex_lock(&stream
->lock
);
692 * Protect against concurrent teardown of a stream.
694 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
698 if (!stream
->quiescent
) {
699 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
701 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64
702 ", channel name = '%s'",
705 ret
= LTTNG_ERR_BUFFER_FLUSH_FAILED
;
706 pthread_mutex_unlock(&stream
->lock
);
709 stream
->quiescent
= true;
712 pthread_mutex_unlock(&stream
->lock
);
716 * Send one last buffer statistics update to the session daemon. This
717 * ensures that the session daemon gets at least one statistics update
718 * per channel even in the case of short-lived channels, such as when a
719 * short-lived app is traced in per-pid mode.
721 sample_and_send_channel_buffer_stats(channel
);
728 * Clear quiescent state from channel's streams using the given key to
729 * retrieve the channel.
731 * Return 0 on success else an LTTng error code.
733 static int clear_quiescent_channel(uint64_t chan_key
)
736 struct lttng_consumer_channel
*channel
;
737 struct lttng_consumer_stream
*stream
;
739 struct lttng_ht_iter iter
;
741 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
744 channel
= consumer_find_channel(chan_key
);
746 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
747 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
751 ht
= the_consumer_data
.stream_per_chan_id_ht
;
753 /* For each stream of the channel id, clear quiescent state. */
754 cds_lfht_for_each_entry_duplicate(ht
->ht
,
755 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
760 node_channel_id
.node
)
762 health_code_update();
764 pthread_mutex_lock(&stream
->lock
);
765 stream
->quiescent
= false;
766 pthread_mutex_unlock(&stream
->lock
);
774 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
776 * Return 0 on success else an LTTng error code.
778 static int close_metadata(uint64_t chan_key
)
781 struct lttng_consumer_channel
*channel
;
782 unsigned int channel_monitor
;
784 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
786 channel
= consumer_find_channel(chan_key
);
789 * This is possible if the metadata thread has issue a delete because
790 * the endpoint point of the stream hung up. There is no way the
791 * session daemon can know about it thus use a DBG instead of an actual
794 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
795 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
799 pthread_mutex_lock(&the_consumer_data
.lock
);
800 pthread_mutex_lock(&channel
->lock
);
801 channel_monitor
= channel
->monitor
;
802 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
806 lttng_ustconsumer_close_metadata(channel
);
807 pthread_mutex_unlock(&channel
->lock
);
808 pthread_mutex_unlock(&the_consumer_data
.lock
);
811 * The ownership of a metadata channel depends on the type of
812 * session to which it belongs. In effect, the monitor flag is checked
813 * to determine if this metadata channel is in "snapshot" mode or not.
815 * In the non-snapshot case, the metadata channel is created along with
816 * a single stream which will remain present until the metadata channel
817 * is destroyed (on the destruction of its session). In this case, the
818 * metadata stream in "monitored" by the metadata poll thread and holds
819 * the ownership of its channel.
821 * Closing the metadata will cause the metadata stream's "metadata poll
822 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
823 * thread which will teardown the metadata stream which, in return,
824 * deletes the metadata channel.
826 * In the snapshot case, the metadata stream is created and destroyed
827 * on every snapshot record. Since the channel doesn't have an owner
828 * other than the session daemon, it is safe to destroy it immediately
829 * on reception of the CLOSE_METADATA command.
831 if (!channel_monitor
) {
833 * The channel and consumer_data locks must be
834 * released before this call since consumer_del_channel
835 * re-acquires the channel and consumer_data locks to teardown
836 * the channel and queue its reclamation by the "call_rcu"
839 consumer_del_channel(channel
);
844 pthread_mutex_unlock(&channel
->lock
);
845 pthread_mutex_unlock(&the_consumer_data
.lock
);
851 * RCU read side lock MUST be acquired before calling this function.
853 * Return 0 on success else an LTTng error code.
855 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
858 struct lttng_consumer_channel
*metadata
;
860 ASSERT_RCU_READ_LOCKED();
862 DBG("UST consumer setup metadata key %" PRIu64
, key
);
864 metadata
= consumer_find_channel(key
);
866 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
867 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
872 * In no monitor mode, the metadata channel has no stream(s) so skip the
873 * ownership transfer to the metadata thread.
875 if (!metadata
->monitor
) {
876 DBG("Metadata channel in no monitor");
882 * Send metadata stream to relayd if one available. Availability is
883 * known if the stream is still in the list of the channel.
885 if (cds_list_empty(&metadata
->streams
.head
)) {
886 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
887 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
888 goto error_no_stream
;
891 /* Send metadata stream to relayd if needed. */
892 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
893 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
, metadata
->pathname
);
895 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
898 ret
= consumer_send_relayd_streams_sent(metadata
->metadata_stream
->net_seq_idx
);
900 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
906 * Ownership of metadata stream is passed along. Freeing is handled by
909 ret
= send_streams_to_thread(metadata
, ctx
);
912 * If we are unable to send the stream to the thread, there is
913 * a big problem so just stop everything.
915 ret
= LTTCOMM_CONSUMERD_FATAL
;
916 goto send_streams_error
;
918 /* List MUST be empty after or else it could be reused. */
919 LTTNG_ASSERT(cds_list_empty(&metadata
->streams
.head
));
926 * Delete metadata channel on error. At this point, the metadata stream can
927 * NOT be monitored by the metadata thread thus having the guarantee that
928 * the stream is still in the local stream list of the channel. This call
929 * will make sure to clean that list.
931 consumer_stream_destroy(metadata
->metadata_stream
, nullptr);
932 metadata
->metadata_stream
= nullptr;
940 * Snapshot the whole metadata.
941 * RCU read-side lock must be held by the caller.
943 * Returns 0 on success, < 0 on error
945 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
949 struct lttng_consumer_local_data
*ctx
)
952 struct lttng_consumer_stream
*metadata_stream
;
956 ASSERT_RCU_READ_LOCKED();
958 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s", key
, path
);
962 LTTNG_ASSERT(!metadata_channel
->monitor
);
964 health_code_update();
967 * Ask the sessiond if we have new metadata waiting and update the
968 * consumer metadata cache.
970 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
975 health_code_update();
978 * The metadata stream is NOT created in no monitor mode when the channel
979 * is created on a sessiond ask channel command.
981 ret
= create_ust_streams(metadata_channel
, ctx
);
986 metadata_stream
= metadata_channel
->metadata_stream
;
987 LTTNG_ASSERT(metadata_stream
);
989 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
990 if (relayd_id
!= (uint64_t) -1ULL) {
991 metadata_stream
->net_seq_idx
= relayd_id
;
992 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
994 ret
= consumer_stream_create_output_files(metadata_stream
, false);
1001 health_code_update();
1002 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1009 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
1011 * Clean up the stream completely because the next snapshot will use a
1012 * new metadata stream.
1014 consumer_stream_destroy(metadata_stream
, nullptr);
1015 metadata_channel
->metadata_stream
= nullptr;
1022 static int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
, const char **addr
)
1025 unsigned long mmap_offset
;
1026 const char *mmap_base
;
1028 mmap_base
= (const char *) lttng_ust_ctl_get_mmap_base(stream
->ustream
);
1030 ERR("Failed to get mmap base for stream `%s`", stream
->name
);
1035 ret
= lttng_ust_ctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1037 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1042 *addr
= mmap_base
+ mmap_offset
;
1048 * Take a snapshot of all the stream of a channel.
1049 * RCU read-side lock and the channel lock must be held by the caller.
1051 * Returns 0 on success, < 0 on error
1053 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1057 uint64_t nb_packets_per_stream
,
1058 struct lttng_consumer_local_data
*ctx
)
1061 unsigned use_relayd
= 0;
1062 unsigned long consumed_pos
, produced_pos
;
1063 struct lttng_consumer_stream
*stream
;
1067 ASSERT_RCU_READ_LOCKED();
1071 if (relayd_id
!= (uint64_t) -1ULL) {
1075 LTTNG_ASSERT(!channel
->monitor
);
1076 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1078 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
1079 health_code_update();
1081 /* Lock stream because we are about to change its state. */
1082 pthread_mutex_lock(&stream
->lock
);
1083 LTTNG_ASSERT(channel
->trace_chunk
);
1084 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1086 * Can't happen barring an internal error as the channel
1087 * holds a reference to the trace chunk.
1089 ERR("Failed to acquire reference to channel's trace chunk");
1093 LTTNG_ASSERT(!stream
->trace_chunk
);
1094 stream
->trace_chunk
= channel
->trace_chunk
;
1096 stream
->net_seq_idx
= relayd_id
;
1099 ret
= consumer_send_relayd_stream(stream
, path
);
1101 goto error_close_stream
;
1104 ret
= consumer_stream_create_output_files(stream
, false);
1106 goto error_close_stream
;
1108 DBG("UST consumer snapshot stream (%" PRIu64
")", stream
->key
);
1112 * If tracing is active, we want to perform a "full" buffer flush.
1113 * Else, if quiescent, it has already been done by the prior stop.
1115 if (!stream
->quiescent
) {
1116 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
1118 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
1119 ", channel name = '%s'",
1126 ret
= lttng_ustconsumer_take_snapshot(stream
);
1128 ERR("Taking UST snapshot");
1129 goto error_close_stream
;
1132 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1134 ERR("Produced UST snapshot position");
1135 goto error_close_stream
;
1138 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1140 ERR("Consumerd UST snapshot position");
1141 goto error_close_stream
;
1145 * The original value is sent back if max stream size is larger than
1146 * the possible size of the snapshot. Also, we assume that the session
1147 * daemon should never send a maximum stream size that is lower than
1150 consumed_pos
= consumer_get_consume_start_pos(
1151 consumed_pos
, produced_pos
, nb_packets_per_stream
, stream
->max_sb_size
);
1153 while ((long) (consumed_pos
- produced_pos
) < 0) {
1155 unsigned long len
, padded_len
;
1156 const char *subbuf_addr
;
1157 struct lttng_buffer_view subbuf_view
;
1159 health_code_update();
1161 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1163 ret
= lttng_ust_ctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1165 if (ret
!= -EAGAIN
) {
1166 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1167 goto error_close_stream
;
1169 DBG("UST consumer get subbuf failed. Skipping it.");
1170 consumed_pos
+= stream
->max_sb_size
;
1171 stream
->chan
->lost_packets
++;
1175 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &len
);
1177 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1178 goto error_put_subbuf
;
1181 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1183 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1184 goto error_put_subbuf
;
1187 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1189 goto error_put_subbuf
;
1192 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, padded_len
);
1193 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1194 stream
, &subbuf_view
, padded_len
- len
);
1196 if (read_len
!= len
) {
1198 goto error_put_subbuf
;
1201 if (read_len
!= padded_len
) {
1203 goto error_put_subbuf
;
1207 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
1209 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1210 goto error_close_stream
;
1212 consumed_pos
+= stream
->max_sb_size
;
1215 /* Simply close the stream so we can use it on the next snapshot. */
1216 consumer_stream_close_output(stream
);
1217 pthread_mutex_unlock(&stream
->lock
);
1224 if (lttng_ust_ctl_put_subbuf(stream
->ustream
) < 0) {
1225 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1228 consumer_stream_close_output(stream
);
1230 pthread_mutex_unlock(&stream
->lock
);
1235 static void metadata_stream_reset_cache_consumed_position(struct lttng_consumer_stream
*stream
)
1237 ASSERT_LOCKED(stream
->lock
);
1239 DBG("Reset metadata cache of session %" PRIu64
, stream
->chan
->session_id
);
1240 stream
->ust_metadata_pushed
= 0;
1244 * Receive the metadata updates from the sessiond. Supports receiving
1245 * overlapping metadata, but is needs to always belong to a contiguous
1246 * range starting from 0.
1247 * Be careful about the locks held when calling this function: it needs
1248 * the metadata cache flush to concurrently progress in order to
1251 int lttng_ustconsumer_recv_metadata(int sock
,
1256 struct lttng_consumer_channel
*channel
,
1260 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1262 enum consumer_metadata_cache_write_status cache_write_status
;
1264 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1266 metadata_str
= calloc
<char>(len
);
1267 if (!metadata_str
) {
1268 PERROR("zmalloc metadata string");
1269 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1273 health_code_update();
1275 /* Receive metadata string. */
1276 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1278 /* Session daemon is dead so return gracefully. */
1283 health_code_update();
1285 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1286 cache_write_status
= consumer_metadata_cache_write(
1287 channel
->metadata_cache
, offset
, len
, version
, metadata_str
);
1288 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1289 switch (cache_write_status
) {
1290 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1292 * The write entirely overlapped with existing contents of the
1293 * same metadata version (same content); there is nothing to do.
1296 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1298 * The metadata cache was invalidated (previously pushed
1299 * content has been overwritten). Reset the stream's consumed
1300 * metadata position to ensure the metadata poll thread consumes
1305 * channel::metadata_stream can be null when the metadata
1306 * channel is under a snapshot session type. No need to update
1307 * the stream position in that scenario.
1309 if (channel
->metadata_stream
!= nullptr) {
1310 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1311 metadata_stream_reset_cache_consumed_position(channel
->metadata_stream
);
1312 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1314 /* Validate we are in snapshot mode. */
1315 LTTNG_ASSERT(!channel
->monitor
);
1318 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1320 * In both cases, the metadata poll thread has new data to
1323 ret
= consumer_metadata_wakeup_pipe(channel
);
1325 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1329 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1330 /* Unable to handle metadata. Notify session daemon. */
1331 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1333 * Skip metadata flush on write error since the offset and len might
1334 * not have been updated which could create an infinite loop below when
1335 * waiting for the metadata cache to be flushed.
1345 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1346 DBG("Waiting for metadata to be flushed");
1348 health_code_update();
1350 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1360 * Receive command from session daemon and process it.
1362 * Return 1 on success else a negative value or 0.
1364 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1366 struct pollfd
*consumer_sockpoll
)
1369 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1370 struct lttcomm_consumer_msg msg
;
1371 struct lttng_consumer_channel
*channel
= nullptr;
1373 health_code_update();
1378 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1379 if (ret_recv
!= sizeof(msg
)) {
1380 DBG("Consumer received unexpected message size %zd (expects %zu)",
1384 * The ret value might 0 meaning an orderly shutdown but this is ok
1385 * since the caller handles this.
1388 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1395 health_code_update();
1398 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1400 health_code_update();
1402 /* relayd needs RCU read-side lock */
1405 switch (msg
.cmd_type
) {
1406 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1408 uint32_t major
= msg
.u
.relayd_sock
.major
;
1409 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
1410 enum lttcomm_sock_proto protocol
=
1411 (enum lttcomm_sock_proto
) msg
.u
.relayd_sock
.relayd_socket_protocol
;
1413 /* Session daemon status message are handled in the following call. */
1414 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1415 msg
.u
.relayd_sock
.type
,
1419 msg
.u
.relayd_sock
.session_id
,
1420 msg
.u
.relayd_sock
.relayd_session_id
,
1426 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1428 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1429 struct consumer_relayd_sock_pair
*relayd
;
1431 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1433 /* Get relayd reference if exists. */
1434 relayd
= consumer_find_relayd(index
);
1435 if (relayd
== nullptr) {
1436 DBG("Unable to find relayd %" PRIu64
, index
);
1437 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1441 * Each relayd socket pair has a refcount of stream attached to it
1442 * which tells if the relayd is still active or not depending on the
1445 * This will set the destroy flag of the relayd object and destroy it
1446 * if the refcount reaches zero when called.
1448 * The destroy can happen either here or when a stream fd hangs up.
1451 consumer_flag_relayd_for_destroy(relayd
);
1454 goto end_msg_sessiond
;
1456 case LTTNG_CONSUMER_UPDATE_STREAM
:
1461 case LTTNG_CONSUMER_DATA_PENDING
:
1463 int is_data_pending
;
1465 uint64_t id
= msg
.u
.data_pending
.session_id
;
1467 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1469 is_data_pending
= consumer_data_pending(id
);
1471 /* Send back returned value to session daemon */
1472 ret_send
= lttcomm_send_unix_sock(sock
, &is_data_pending
, sizeof(is_data_pending
));
1474 DBG("Error when sending the data pending ret code: %zd", ret_send
);
1479 * No need to send back a status message since the data pending
1480 * returned value is the response.
1484 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1486 int ret_ask_channel
, ret_add_channel
, ret_send
;
1487 struct lttng_ust_ctl_consumer_channel_attr attr
;
1488 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1489 const struct lttng_credentials buffer_credentials
= {
1490 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1491 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1494 /* Create a plain object and reserve a channel key. */
1495 channel
= consumer_allocate_channel(
1496 msg
.u
.ask_channel
.key
,
1497 msg
.u
.ask_channel
.session_id
,
1498 msg
.u
.ask_channel
.chunk_id
.is_set
? &chunk_id
: nullptr,
1499 msg
.u
.ask_channel
.pathname
,
1500 msg
.u
.ask_channel
.name
,
1501 msg
.u
.ask_channel
.relayd_id
,
1502 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1503 msg
.u
.ask_channel
.tracefile_size
,
1504 msg
.u
.ask_channel
.tracefile_count
,
1505 msg
.u
.ask_channel
.session_id_per_pid
,
1506 msg
.u
.ask_channel
.monitor
,
1507 msg
.u
.ask_channel
.live_timer_interval
,
1508 msg
.u
.ask_channel
.is_live
,
1509 msg
.u
.ask_channel
.root_shm_path
,
1510 msg
.u
.ask_channel
.shm_path
);
1512 goto end_channel_error
;
1515 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
, buffer_credentials
);
1518 * Assign UST application UID to the channel. This value is ignored for
1519 * per PID buffers. This is specific to UST thus setting this after the
1522 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1524 /* Build channel attributes from received message. */
1525 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1526 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1527 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1528 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1529 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1530 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1531 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1532 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1534 /* Match channel buffer type to the UST abi. */
1535 switch (msg
.u
.ask_channel
.output
) {
1536 case LTTNG_EVENT_MMAP
:
1538 attr
.output
= LTTNG_UST_ABI_MMAP
;
1542 /* Translate and save channel type. */
1543 switch (msg
.u
.ask_channel
.type
) {
1544 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1545 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1546 attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1548 * Set refcount to 1 for owner. Below, we will
1549 * pass ownership to the
1550 * consumer_thread_channel_poll() thread.
1552 channel
->refcount
= 1;
1554 case LTTNG_UST_ABI_CHAN_METADATA
:
1555 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1556 attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1563 health_code_update();
1565 ret_ask_channel
= ask_channel(ctx
, channel
, &attr
);
1566 if (ret_ask_channel
< 0) {
1567 goto end_channel_error
;
1570 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1573 ret_allocate
= consumer_metadata_cache_allocate(channel
);
1574 if (ret_allocate
< 0) {
1575 ERR("Allocating metadata cache");
1576 goto end_channel_error
;
1578 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1579 attr
.switch_timer_interval
= 0;
1581 int monitor_start_ret
;
1583 consumer_timer_live_start(channel
, msg
.u
.ask_channel
.live_timer_interval
);
1584 monitor_start_ret
= consumer_timer_monitor_start(
1585 channel
, msg
.u
.ask_channel
.monitor_timer_interval
);
1586 if (monitor_start_ret
< 0) {
1587 ERR("Starting channel monitoring timer failed");
1588 goto end_channel_error
;
1592 health_code_update();
1595 * Add the channel to the internal state AFTER all streams were created
1596 * and successfully sent to session daemon. This way, all streams must
1597 * be ready before this channel is visible to the threads.
1598 * If add_channel succeeds, ownership of the channel is
1599 * passed to consumer_thread_channel_poll().
1601 ret_add_channel
= add_channel(channel
, ctx
);
1602 if (ret_add_channel
< 0) {
1603 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1604 if (channel
->switch_timer_enabled
== 1) {
1605 consumer_timer_switch_stop(channel
);
1607 consumer_metadata_cache_destroy(channel
);
1609 if (channel
->live_timer_enabled
== 1) {
1610 consumer_timer_live_stop(channel
);
1612 if (channel
->monitor_timer_enabled
== 1) {
1613 consumer_timer_monitor_stop(channel
);
1615 goto end_channel_error
;
1618 health_code_update();
1621 * Channel and streams are now created. Inform the session daemon that
1622 * everything went well and should wait to receive the channel and
1623 * streams with ustctl API.
1625 ret_send
= consumer_send_status_channel(sock
, channel
);
1628 * There is probably a problem on the socket.
1635 case LTTNG_CONSUMER_GET_CHANNEL
:
1637 int ret
, relayd_err
= 0;
1638 uint64_t key
= msg
.u
.get_channel
.key
;
1639 struct lttng_consumer_channel
*found_channel
;
1641 found_channel
= consumer_find_channel(key
);
1642 if (!found_channel
) {
1643 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1644 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1645 goto end_get_channel
;
1648 health_code_update();
1650 /* Send the channel to sessiond (and relayd, if applicable). */
1651 ret
= send_channel_to_sessiond_and_relayd(sock
, found_channel
, ctx
, &relayd_err
);
1655 * We were unable to send to the relayd the stream so avoid
1656 * sending back a fatal error to the thread since this is OK
1657 * and the consumer can continue its work. The above call
1658 * has sent the error status message to the sessiond.
1660 goto end_get_channel_nosignal
;
1663 * The communicaton was broken hence there is a bad state between
1664 * the consumer and sessiond so stop everything.
1666 goto error_get_channel_fatal
;
1669 health_code_update();
1672 * In no monitor mode, the streams ownership is kept inside the channel
1673 * so don't send them to the data thread.
1675 if (!found_channel
->monitor
) {
1676 goto end_get_channel
;
1679 ret
= send_streams_to_thread(found_channel
, ctx
);
1682 * If we are unable to send the stream to the thread, there is
1683 * a big problem so just stop everything.
1685 goto error_get_channel_fatal
;
1687 /* List MUST be empty after or else it could be reused. */
1688 LTTNG_ASSERT(cds_list_empty(&found_channel
->streams
.head
));
1690 goto end_msg_sessiond
;
1691 error_get_channel_fatal
:
1693 end_get_channel_nosignal
:
1696 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1698 uint64_t key
= msg
.u
.destroy_channel
.key
;
1701 * Only called if streams have not been sent to stream
1702 * manager thread. However, channel has been sent to
1703 * channel manager thread.
1705 notify_thread_del_channel(ctx
, key
);
1706 goto end_msg_sessiond
;
1708 case LTTNG_CONSUMER_CLOSE_METADATA
:
1712 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1714 ret_code
= (lttcomm_return_code
) ret
;
1717 goto end_msg_sessiond
;
1719 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1723 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1725 ret_code
= (lttcomm_return_code
) ret
;
1728 goto end_msg_sessiond
;
1730 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1734 ret
= clear_quiescent_channel(msg
.u
.clear_quiescent_channel
.key
);
1736 ret_code
= (lttcomm_return_code
) ret
;
1739 goto end_msg_sessiond
;
1741 case LTTNG_CONSUMER_PUSH_METADATA
:
1744 uint64_t len
= msg
.u
.push_metadata
.len
;
1745 uint64_t key
= msg
.u
.push_metadata
.key
;
1746 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1747 uint64_t version
= msg
.u
.push_metadata
.version
;
1748 struct lttng_consumer_channel
*found_channel
;
1750 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1752 found_channel
= consumer_find_channel(key
);
1753 if (!found_channel
) {
1755 * This is possible if the metadata creation on the consumer side
1756 * is in flight vis-a-vis a concurrent push metadata from the
1757 * session daemon. Simply return that the channel failed and the
1758 * session daemon will handle that message correctly considering
1759 * that this race is acceptable thus the DBG() statement here.
1761 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1762 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1763 goto end_push_metadata_msg_sessiond
;
1766 health_code_update();
1770 * There is nothing to receive. We have simply
1771 * checked whether the channel can be found.
1773 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1774 goto end_push_metadata_msg_sessiond
;
1777 /* Tell session daemon we are ready to receive the metadata. */
1778 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1780 /* Somehow, the session daemon is not responding anymore. */
1781 goto error_push_metadata_fatal
;
1784 health_code_update();
1786 /* Wait for more data. */
1787 health_poll_entry();
1788 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1791 goto error_push_metadata_fatal
;
1794 health_code_update();
1796 ret
= lttng_ustconsumer_recv_metadata(
1797 sock
, key
, offset
, len
, version
, found_channel
, 0, 1);
1799 /* error receiving from sessiond */
1800 goto error_push_metadata_fatal
;
1802 ret_code
= (lttcomm_return_code
) ret
;
1803 goto end_push_metadata_msg_sessiond
;
1805 end_push_metadata_msg_sessiond
:
1806 goto end_msg_sessiond
;
1807 error_push_metadata_fatal
:
1810 case LTTNG_CONSUMER_SETUP_METADATA
:
1814 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1816 ret_code
= (lttcomm_return_code
) ret
;
1818 goto end_msg_sessiond
;
1820 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1822 struct lttng_consumer_channel
*found_channel
;
1823 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1826 found_channel
= consumer_find_channel(key
);
1827 if (!found_channel
) {
1828 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1829 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1831 if (msg
.u
.snapshot_channel
.metadata
) {
1834 ret_snapshot
= snapshot_metadata(found_channel
,
1836 msg
.u
.snapshot_channel
.pathname
,
1837 msg
.u
.snapshot_channel
.relayd_id
,
1839 if (ret_snapshot
< 0) {
1840 ERR("Snapshot metadata failed");
1841 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1846 ret_snapshot
= snapshot_channel(
1849 msg
.u
.snapshot_channel
.pathname
,
1850 msg
.u
.snapshot_channel
.relayd_id
,
1851 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1853 if (ret_snapshot
< 0) {
1854 ERR("Snapshot channel failed");
1855 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1859 health_code_update();
1860 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1862 /* Somehow, the session daemon is not responding anymore. */
1865 health_code_update();
1868 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1871 uint64_t discarded_events
;
1872 struct lttng_ht_iter iter
;
1873 struct lttng_ht
*ht
;
1874 struct lttng_consumer_stream
*stream
;
1875 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1876 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1878 DBG("UST consumer discarded events command for session id %" PRIu64
, id
);
1880 pthread_mutex_lock(&the_consumer_data
.lock
);
1882 ht
= the_consumer_data
.stream_list_ht
;
1885 * We only need a reference to the channel, but they are not
1886 * directly indexed, so we just use the first matching stream
1887 * to extract the information we need, we default to 0 if not
1888 * found (no events are dropped if the channel is not yet in
1891 discarded_events
= 0;
1892 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1893 ht
->hash_fct(&id
, lttng_ht_seed
),
1898 node_session_id
.node
)
1900 if (stream
->chan
->key
== key
) {
1901 discarded_events
= stream
->chan
->discarded_events
;
1905 pthread_mutex_unlock(&the_consumer_data
.lock
);
1908 DBG("UST consumer discarded events command for session id %" PRIu64
1909 ", channel key %" PRIu64
,
1913 health_code_update();
1915 /* Send back returned value to session daemon */
1916 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1918 PERROR("send discarded events");
1924 case LTTNG_CONSUMER_LOST_PACKETS
:
1927 uint64_t lost_packets
;
1928 struct lttng_ht_iter iter
;
1929 struct lttng_ht
*ht
;
1930 struct lttng_consumer_stream
*stream
;
1931 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1932 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1934 DBG("UST consumer lost packets command for session id %" PRIu64
, id
);
1936 pthread_mutex_lock(&the_consumer_data
.lock
);
1938 ht
= the_consumer_data
.stream_list_ht
;
1941 * We only need a reference to the channel, but they are not
1942 * directly indexed, so we just use the first matching stream
1943 * to extract the information we need, we default to 0 if not
1944 * found (no packets lost if the channel is not yet in use).
1947 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1948 ht
->hash_fct(&id
, lttng_ht_seed
),
1953 node_session_id
.node
)
1955 if (stream
->chan
->key
== key
) {
1956 lost_packets
= stream
->chan
->lost_packets
;
1960 pthread_mutex_unlock(&the_consumer_data
.lock
);
1963 DBG("UST consumer lost packets command for session id %" PRIu64
1964 ", channel key %" PRIu64
,
1968 health_code_update();
1970 /* Send back returned value to session daemon */
1971 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
, sizeof(lost_packets
));
1973 PERROR("send lost packets");
1979 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1981 int channel_monitor_pipe
, ret_send
, ret_set_channel_monitor_pipe
;
1984 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1985 /* Successfully received the command's type. */
1986 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1991 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
, 1);
1992 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1993 ERR("Failed to receive channel monitor pipe");
1997 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1998 ret_set_channel_monitor_pipe
=
1999 consumer_timer_thread_set_channel_monitor_pipe(channel_monitor_pipe
);
2000 if (!ret_set_channel_monitor_pipe
) {
2004 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2005 /* Set the pipe as non-blocking. */
2006 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
2007 if (ret_fcntl
== -1) {
2008 PERROR("fcntl get flags of the channel monitoring pipe");
2013 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
, flags
| O_NONBLOCK
);
2014 if (ret_fcntl
== -1) {
2015 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2018 DBG("Channel monitor pipe set as non-blocking");
2020 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2022 goto end_msg_sessiond
;
2024 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2026 struct lttng_consumer_channel
*found_channel
;
2027 uint64_t key
= msg
.u
.rotate_channel
.key
;
2028 int ret_send_status
;
2030 found_channel
= consumer_find_channel(key
);
2031 if (!found_channel
) {
2032 DBG("Channel %" PRIu64
" not found", key
);
2033 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2038 * Sample the rotate position of all the streams in
2041 rotate_channel
= lttng_consumer_rotate_channel(
2042 found_channel
, key
, msg
.u
.rotate_channel
.relayd_id
);
2043 if (rotate_channel
< 0) {
2044 ERR("Rotate channel failed");
2045 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2048 health_code_update();
2051 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2052 if (ret_send_status
< 0) {
2053 /* Somehow, the session daemon is not responding anymore. */
2054 goto end_rotate_channel_nosignal
;
2058 * Rotate the streams that are ready right now.
2059 * FIXME: this is a second consecutive iteration over the
2060 * streams in a channel, there is probably a better way to
2061 * handle this, but it needs to be after the
2062 * consumer_send_status_msg() call.
2064 if (found_channel
) {
2065 int ret_rotate_read_streams
;
2067 ret_rotate_read_streams
=
2068 lttng_consumer_rotate_ready_streams(found_channel
, key
);
2069 if (ret_rotate_read_streams
< 0) {
2070 ERR("Rotate channel failed");
2074 end_rotate_channel_nosignal
:
2077 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2079 struct lttng_consumer_channel
*found_channel
;
2080 uint64_t key
= msg
.u
.clear_channel
.key
;
2081 int ret_send_status
;
2083 found_channel
= consumer_find_channel(key
);
2084 if (!found_channel
) {
2085 DBG("Channel %" PRIu64
" not found", key
);
2086 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2088 int ret_clear_channel
;
2090 ret_clear_channel
= lttng_consumer_clear_channel(found_channel
);
2091 if (ret_clear_channel
) {
2092 ERR("Clear channel failed key %" PRIu64
, key
);
2093 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
2096 health_code_update();
2098 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2099 if (ret_send_status
< 0) {
2100 /* Somehow, the session daemon is not responding anymore. */
2105 case LTTNG_CONSUMER_INIT
:
2107 int ret_send_status
;
2108 lttng_uuid sessiond_uuid
;
2110 std::copy(std::begin(msg
.u
.init
.sessiond_uuid
),
2111 std::end(msg
.u
.init
.sessiond_uuid
),
2112 sessiond_uuid
.begin());
2113 ret_code
= lttng_consumer_init_command(ctx
, sessiond_uuid
);
2114 health_code_update();
2115 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2116 if (ret_send_status
< 0) {
2117 /* Somehow, the session daemon is not responding anymore. */
2122 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2124 const struct lttng_credentials credentials
= {
2125 .uid
= LTTNG_OPTIONAL_INIT_VALUE(
2126 msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2127 .gid
= LTTNG_OPTIONAL_INIT_VALUE(
2128 msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2130 const bool is_local_trace
= !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2131 const uint64_t relayd_id
= msg
.u
.create_trace_chunk
.relayd_id
.value
;
2132 const char *chunk_override_name
= *msg
.u
.create_trace_chunk
.override_name
?
2133 msg
.u
.create_trace_chunk
.override_name
:
2135 struct lttng_directory_handle
*chunk_directory_handle
= nullptr;
2138 * The session daemon will only provide a chunk directory file
2139 * descriptor for local traces.
2141 if (is_local_trace
) {
2143 int ret_send_status
;
2146 /* Acnowledge the reception of the command. */
2147 ret_send_status
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
2148 if (ret_send_status
< 0) {
2149 /* Somehow, the session daemon is not responding anymore. */
2154 * Receive trace chunk domain dirfd.
2156 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2157 if (ret_recv
!= sizeof(chunk_dirfd
)) {
2158 ERR("Failed to receive trace chunk domain directory file descriptor");
2162 DBG("Received trace chunk domain directory fd (%d)", chunk_dirfd
);
2163 chunk_directory_handle
=
2164 lttng_directory_handle_create_from_dirfd(chunk_dirfd
);
2165 if (!chunk_directory_handle
) {
2166 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2167 if (close(chunk_dirfd
)) {
2168 PERROR("Failed to close chunk directory file descriptor");
2174 ret_code
= lttng_consumer_create_trace_chunk(
2175 !is_local_trace
? &relayd_id
: nullptr,
2176 msg
.u
.create_trace_chunk
.session_id
,
2177 msg
.u
.create_trace_chunk
.chunk_id
,
2178 (time_t) msg
.u
.create_trace_chunk
.creation_timestamp
,
2179 chunk_override_name
,
2180 msg
.u
.create_trace_chunk
.credentials
.is_set
? &credentials
: nullptr,
2181 chunk_directory_handle
);
2182 lttng_directory_handle_put(chunk_directory_handle
);
2183 goto end_msg_sessiond
;
2185 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2187 enum lttng_trace_chunk_command_type close_command
=
2188 (lttng_trace_chunk_command_type
) msg
.u
.close_trace_chunk
.close_command
.value
;
2189 const uint64_t relayd_id
= msg
.u
.close_trace_chunk
.relayd_id
.value
;
2190 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2191 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2194 ret_code
= lttng_consumer_close_trace_chunk(
2195 msg
.u
.close_trace_chunk
.relayd_id
.is_set
? &relayd_id
: nullptr,
2196 msg
.u
.close_trace_chunk
.session_id
,
2197 msg
.u
.close_trace_chunk
.chunk_id
,
2198 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2199 msg
.u
.close_trace_chunk
.close_command
.is_set
? &close_command
: nullptr,
2200 closed_trace_chunk_path
);
2201 reply
.ret_code
= ret_code
;
2202 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2203 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2204 if (ret
!= sizeof(reply
)) {
2207 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
, reply
.path_length
);
2208 if (ret
!= reply
.path_length
) {
2213 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2215 const uint64_t relayd_id
= msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2217 ret_code
= lttng_consumer_trace_chunk_exists(
2218 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
? &relayd_id
: nullptr,
2219 msg
.u
.trace_chunk_exists
.session_id
,
2220 msg
.u
.trace_chunk_exists
.chunk_id
);
2221 goto end_msg_sessiond
;
2223 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2225 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2226 struct lttng_consumer_channel
*found_channel
= consumer_find_channel(key
);
2228 if (found_channel
) {
2229 pthread_mutex_lock(&found_channel
->lock
);
2230 ret_code
= lttng_consumer_open_channel_packets(found_channel
);
2231 pthread_mutex_unlock(&found_channel
->lock
);
2234 * The channel could have disappeared in per-pid
2237 DBG("Channel %" PRIu64
" not found", key
);
2238 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2241 health_code_update();
2242 goto end_msg_sessiond
;
2250 * Return 1 to indicate success since the 0 value can be a socket
2251 * shutdown during the recv() or send() call.
2258 * The returned value here is not useful since either way we'll return 1 to
2259 * the caller because the session daemon socket management is done
2260 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2263 int ret_send_status
;
2265 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2266 if (ret_send_status
< 0) {
2276 consumer_del_channel(channel
);
2278 /* We have to send a status channel message indicating an error. */
2280 int ret_send_status
;
2282 ret_send_status
= consumer_send_status_channel(sock
, nullptr);
2283 if (ret_send_status
< 0) {
2284 /* Stop everything if session daemon can not be notified. */
2293 /* This will issue a consumer stop. */
2299 health_code_update();
2303 int lttng_ust_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
2305 LTTNG_ASSERT(stream
);
2306 LTTNG_ASSERT(stream
->ustream
);
2308 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer_active
);
2312 * Take a snapshot for a specific stream.
2314 * Returns 0 on success, < 0 on error
2316 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2318 LTTNG_ASSERT(stream
);
2319 LTTNG_ASSERT(stream
->ustream
);
2321 return lttng_ust_ctl_snapshot(stream
->ustream
);
2325 * Sample consumed and produced positions for a specific stream.
2327 * Returns 0 on success, < 0 on error.
2329 int lttng_ustconsumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2331 LTTNG_ASSERT(stream
);
2332 LTTNG_ASSERT(stream
->ustream
);
2334 return lttng_ust_ctl_snapshot_sample_positions(stream
->ustream
);
2338 * Get the produced position
2340 * Returns 0 on success, < 0 on error
2342 int lttng_ustconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2345 LTTNG_ASSERT(stream
);
2346 LTTNG_ASSERT(stream
->ustream
);
2349 return lttng_ust_ctl_snapshot_get_produced(stream
->ustream
, pos
);
2353 * Get the consumed position
2355 * Returns 0 on success, < 0 on error
2357 int lttng_ustconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2360 LTTNG_ASSERT(stream
);
2361 LTTNG_ASSERT(stream
->ustream
);
2364 return lttng_ust_ctl_snapshot_get_consumed(stream
->ustream
, pos
);
2367 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer
)
2369 LTTNG_ASSERT(stream
);
2370 LTTNG_ASSERT(stream
->ustream
);
2372 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer
);
2375 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2377 LTTNG_ASSERT(stream
);
2378 LTTNG_ASSERT(stream
->ustream
);
2380 return lttng_ust_ctl_clear_buffer(stream
->ustream
);
2383 int lttng_ustconsumer_get_current_timestamp(struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2385 LTTNG_ASSERT(stream
);
2386 LTTNG_ASSERT(stream
->ustream
);
2389 return lttng_ust_ctl_get_current_timestamp(stream
->ustream
, ts
);
2392 int lttng_ustconsumer_get_sequence_number(struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2394 LTTNG_ASSERT(stream
);
2395 LTTNG_ASSERT(stream
->ustream
);
2398 return lttng_ust_ctl_get_sequence_number(stream
->ustream
, seq
);
2402 * Called when the stream signals the consumer that it has hung up.
2404 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2406 LTTNG_ASSERT(stream
);
2407 LTTNG_ASSERT(stream
->ustream
);
2409 pthread_mutex_lock(&stream
->lock
);
2410 if (!stream
->quiescent
) {
2411 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 0) < 0) {
2412 ERR("Failed to flush buffer on stream hang-up");
2414 stream
->quiescent
= true;
2418 stream
->hangup_flush_done
= 1;
2419 pthread_mutex_unlock(&stream
->lock
);
2422 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2427 LTTNG_ASSERT(chan
->uchan
);
2428 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2430 if (chan
->switch_timer_enabled
== 1) {
2431 consumer_timer_switch_stop(chan
);
2433 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2436 ret
= close(chan
->stream_fds
[i
]);
2440 if (chan
->shm_path
[0]) {
2441 char shm_path
[PATH_MAX
];
2443 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2445 ERR("Cannot get stream shm path");
2447 ret
= run_as_unlink(shm_path
,
2448 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2449 chan
->buffer_credentials
)),
2450 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2451 chan
->buffer_credentials
)));
2453 PERROR("unlink %s", shm_path
);
2459 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2462 LTTNG_ASSERT(chan
->uchan
);
2463 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2465 consumer_metadata_cache_destroy(chan
);
2466 lttng_ust_ctl_destroy_channel(chan
->uchan
);
2467 /* Try to rmdir all directories under shm_path root. */
2468 if (chan
->root_shm_path
[0]) {
2469 (void) run_as_rmdir_recursive(
2470 chan
->root_shm_path
,
2471 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(chan
->buffer_credentials
)),
2472 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(chan
->buffer_credentials
)),
2473 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2475 free(chan
->stream_fds
);
2478 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2480 LTTNG_ASSERT(stream
);
2481 LTTNG_ASSERT(stream
->ustream
);
2483 if (stream
->chan
->switch_timer_enabled
== 1) {
2484 consumer_timer_switch_stop(stream
->chan
);
2486 lttng_ust_ctl_destroy_stream(stream
->ustream
);
2489 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2491 LTTNG_ASSERT(stream
);
2492 LTTNG_ASSERT(stream
->ustream
);
2494 return lttng_ust_ctl_stream_get_wakeup_fd(stream
->ustream
);
2497 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2499 LTTNG_ASSERT(stream
);
2500 LTTNG_ASSERT(stream
->ustream
);
2502 return lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
2506 * Write up to one packet from the metadata cache to the channel.
2508 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2509 * negative value on error.
2511 static int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2516 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2517 if (stream
->chan
->metadata_cache
->contents
.size
== stream
->ust_metadata_pushed
) {
2519 * In the context of a user space metadata channel, a
2520 * change in version can be detected in two ways:
2521 * 1) During the pre-consume of the `read_subbuffer` loop,
2522 * 2) When populating the metadata ring buffer (i.e. here).
2524 * This function is invoked when there is no metadata
2525 * available in the ring-buffer. If all data was consumed
2526 * up to the size of the metadata cache, there is no metadata
2527 * to insert in the ring-buffer.
2529 * However, the metadata version could still have changed (a
2530 * regeneration without any new data will yield the same cache
2533 * The cache's version is checked for a version change and the
2534 * consumed position is reset if one occurred.
2536 * This check is only necessary for the user space domain as
2537 * it has to manage the cache explicitly. If this reset was not
2538 * performed, no metadata would be consumed (and no reset would
2539 * occur as part of the pre-consume) until the metadata size
2540 * exceeded the cache size.
2542 if (stream
->metadata_version
!= stream
->chan
->metadata_cache
->version
) {
2543 metadata_stream_reset_cache_consumed_position(stream
);
2544 consumer_stream_metadata_set_version(stream
,
2545 stream
->chan
->metadata_cache
->version
);
2552 write_len
= lttng_ust_ctl_write_one_packet_to_channel(
2553 stream
->chan
->uchan
,
2554 &stream
->chan
->metadata_cache
->contents
.data
[stream
->ust_metadata_pushed
],
2555 stream
->chan
->metadata_cache
->contents
.size
- stream
->ust_metadata_pushed
);
2556 LTTNG_ASSERT(write_len
!= 0);
2557 if (write_len
< 0) {
2558 ERR("Writing one metadata packet");
2562 stream
->ust_metadata_pushed
+= write_len
;
2564 LTTNG_ASSERT(stream
->chan
->metadata_cache
->contents
.size
>= stream
->ust_metadata_pushed
);
2568 * Switch packet (but don't open the next one) on every commit of
2569 * a metadata packet. Since the subbuffer is fully filled (with padding,
2570 * if needed), the stream is "quiescent" after this commit.
2572 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 1)) {
2573 ERR("Failed to flush buffer while committing one metadata packet");
2576 stream
->quiescent
= true;
2579 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2584 * Sync metadata meaning request them to the session daemon and snapshot to the
2585 * metadata thread can consumer them.
2587 * Metadata stream lock is held here, but we need to release it when
2588 * interacting with sessiond, else we cause a deadlock with live
2589 * awaiting on metadata to be pushed out.
2591 * The RCU read side lock must be held by the caller.
2593 enum sync_metadata_status
2594 lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2595 struct lttng_consumer_stream
*metadata_stream
)
2598 enum sync_metadata_status status
;
2599 struct lttng_consumer_channel
*metadata_channel
;
2602 LTTNG_ASSERT(metadata_stream
);
2603 ASSERT_RCU_READ_LOCKED();
2605 metadata_channel
= metadata_stream
->chan
;
2606 pthread_mutex_unlock(&metadata_stream
->lock
);
2608 * Request metadata from the sessiond, but don't wait for the flush
2609 * because we locked the metadata thread.
2611 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2612 pthread_mutex_lock(&metadata_stream
->lock
);
2614 status
= SYNC_METADATA_STATUS_ERROR
;
2619 * The metadata stream and channel can be deleted while the
2620 * metadata stream lock was released. The streamed is checked
2621 * for deletion before we use it further.
2623 * Note that it is safe to access a logically-deleted stream since its
2624 * existence is still guaranteed by the RCU read side lock. However,
2625 * it should no longer be used. The close/deletion of the metadata
2626 * channel and stream already guarantees that all metadata has been
2627 * consumed. Therefore, there is nothing left to do in this function.
2629 if (consumer_stream_is_deleted(metadata_stream
)) {
2630 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2631 metadata_stream
->key
);
2632 status
= SYNC_METADATA_STATUS_NO_DATA
;
2636 ret
= commit_one_metadata_packet(metadata_stream
);
2638 status
= SYNC_METADATA_STATUS_ERROR
;
2640 } else if (ret
> 0) {
2641 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2642 } else /* ret == 0 */ {
2643 status
= SYNC_METADATA_STATUS_NO_DATA
;
2647 ret
= lttng_ust_ctl_snapshot(metadata_stream
->ustream
);
2649 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d",
2651 status
= SYNC_METADATA_STATUS_ERROR
;
2660 * Return 0 on success else a negative value.
2662 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2663 struct lttng_consumer_local_data
*ctx
)
2666 struct lttng_ust_ctl_consumer_stream
*ustream
;
2668 LTTNG_ASSERT(stream
);
2671 ustream
= stream
->ustream
;
2674 * First, we are going to check if there is a new subbuffer available
2675 * before reading the stream wait_fd.
2677 /* Get the next subbuffer */
2678 ret
= lttng_ust_ctl_get_next_subbuf(ustream
);
2680 /* No more data found, flag the stream. */
2681 stream
->has_data
= 0;
2686 ret
= lttng_ust_ctl_put_subbuf(ustream
);
2689 /* This stream still has data. Flag it and wake up the data thread. */
2690 stream
->has_data
= 1;
2692 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2695 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2696 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2701 /* The wake up pipe has been notified. */
2702 ctx
->has_wakeup
= 1;
2710 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2715 * We can consume the 1 byte written into the wait_fd by
2716 * UST. Don't trigger error if we cannot read this one byte
2717 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2719 * This is only done when the stream is monitored by a thread,
2720 * before the flush is done after a hangup and if the stream
2721 * is not flagged with data since there might be nothing to
2722 * consume in the wait fd but still have data available
2723 * flagged by the consumer wake up pipe.
2725 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2729 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2730 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2738 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2739 struct stream_subbuffer
*subbuf
)
2743 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2748 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
,
2749 &subbuf
->info
.data
.padded_subbuf_size
);
2758 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2759 struct stream_subbuffer
*subbuf
)
2763 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2768 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2774 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2775 struct stream_subbuffer
*subbuf
)
2779 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2784 ret
= lttng_ust_ctl_get_packet_size(stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2786 PERROR("Failed to get sub-buffer packet size");
2790 ret
= lttng_ust_ctl_get_content_size(stream
->ustream
, &subbuf
->info
.data
.content_size
);
2792 PERROR("Failed to get sub-buffer content size");
2796 ret
= lttng_ust_ctl_get_timestamp_begin(stream
->ustream
,
2797 &subbuf
->info
.data
.timestamp_begin
);
2799 PERROR("Failed to get sub-buffer begin timestamp");
2803 ret
= lttng_ust_ctl_get_timestamp_end(stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2805 PERROR("Failed to get sub-buffer end timestamp");
2809 ret
= lttng_ust_ctl_get_events_discarded(stream
->ustream
,
2810 &subbuf
->info
.data
.events_discarded
);
2812 PERROR("Failed to get sub-buffer events discarded count");
2816 ret
= lttng_ust_ctl_get_sequence_number(stream
->ustream
,
2817 &subbuf
->info
.data
.sequence_number
.value
);
2819 /* May not be supported by older LTTng-modules. */
2820 if (ret
!= -ENOTTY
) {
2821 PERROR("Failed to get sub-buffer sequence number");
2825 subbuf
->info
.data
.sequence_number
.is_set
= true;
2828 ret
= lttng_ust_ctl_get_stream_id(stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2830 PERROR("Failed to get stream id");
2834 ret
= lttng_ust_ctl_get_instance_id(stream
->ustream
,
2835 &subbuf
->info
.data
.stream_instance_id
.value
);
2837 /* May not be supported by older LTTng-modules. */
2838 if (ret
!= -ENOTTY
) {
2839 PERROR("Failed to get stream instance id");
2843 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2849 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2850 struct stream_subbuffer
*subbuffer
)
2855 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(stream
, subbuffer
);
2860 ret
= get_current_subbuf_addr(stream
, &addr
);
2865 subbuffer
->buffer
.buffer
=
2866 lttng_buffer_view_init(addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2867 LTTNG_ASSERT(subbuffer
->buffer
.buffer
.data
!= nullptr);
2872 static enum get_next_subbuffer_status
get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2873 struct stream_subbuffer
*subbuffer
)
2876 enum get_next_subbuffer_status status
;
2878 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2881 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
2886 * The caller only expects -ENODATA when there is no data to
2887 * read, but the kernel tracer returns -EAGAIN when there is
2888 * currently no data for a non-finalized stream, and -ENODATA
2889 * when there is no data for a finalized stream. Those can be
2890 * combined into a -ENODATA return value.
2892 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2895 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2899 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2901 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2908 static enum get_next_subbuffer_status
2909 get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2910 struct stream_subbuffer
*subbuffer
)
2917 unsigned long consumed_pos
, produced_pos
;
2918 enum get_next_subbuffer_status status
;
2921 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2923 got_subbuffer
= true;
2925 got_subbuffer
= false;
2926 if (ret
!= -EAGAIN
) {
2928 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2934 * Determine if the cache is empty and ensure that a sub-buffer
2935 * is made available if the cache is not empty.
2937 if (!got_subbuffer
) {
2938 ret
= commit_one_metadata_packet(stream
);
2939 if (ret
< 0 && ret
!= -ENOBUFS
) {
2940 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2942 } else if (ret
== 0) {
2943 /* Not an error, the cache is empty. */
2945 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2948 cache_empty
= false;
2951 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2952 cache_empty
= stream
->chan
->metadata_cache
->contents
.size
==
2953 stream
->ust_metadata_pushed
;
2954 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2956 } while (!got_subbuffer
);
2958 /* Populate sub-buffer infos and view. */
2959 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2961 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2965 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2968 * -EAGAIN is not expected since we got a sub-buffer and haven't
2969 * pushed the consumption position yet (on put_next).
2971 PERROR("Failed to take a snapshot of metadata buffer positions");
2972 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2976 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2978 PERROR("Failed to get metadata consumed position");
2979 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2983 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2985 PERROR("Failed to get metadata produced position");
2986 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2990 /* Last sub-buffer of the ring buffer ? */
2991 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2994 * The sessiond registry lock ensures that coherent units of metadata
2995 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2996 * acquired, the cache is empty, and it is the only available sub-buffer
2997 * available, it is safe to assume that it is "coherent".
2999 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
3001 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
3002 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
3007 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
3008 struct stream_subbuffer
*subbuffer
__attribute__((unused
)))
3010 const int ret
= lttng_ust_ctl_put_next_subbuf(stream
->ustream
);
3012 LTTNG_ASSERT(ret
== 0);
3016 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3017 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
3019 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3020 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3023 static int lttng_ustconsumer_set_stream_ops(struct lttng_consumer_stream
*stream
)
3027 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3028 if (stream
->metadata_flag
) {
3029 stream
->read_subbuffer_ops
.get_next_subbuffer
= get_next_subbuffer_metadata
;
3030 stream
->read_subbuffer_ops
.extract_subbuffer_info
= extract_metadata_subbuffer_info
;
3031 stream
->read_subbuffer_ops
.reset_metadata
=
3032 metadata_stream_reset_cache_consumed_position
;
3033 if (stream
->chan
->is_live
) {
3034 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3035 ret
= consumer_stream_enable_metadata_bucketization(stream
);
3041 stream
->read_subbuffer_ops
.get_next_subbuffer
= get_next_subbuffer
;
3042 stream
->read_subbuffer_ops
.extract_subbuffer_info
= extract_data_subbuffer_info
;
3043 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3044 if (stream
->chan
->is_live
) {
3045 stream
->read_subbuffer_ops
.send_live_beacon
= consumer_flush_ust_index
;
3049 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3055 * Called when a stream is created.
3057 * Return 0 on success or else a negative value.
3059 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3063 LTTNG_ASSERT(stream
);
3066 * Don't create anything if this is set for streaming or if there is
3067 * no current trace chunk on the parent channel.
3069 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3070 stream
->chan
->trace_chunk
) {
3071 ret
= consumer_stream_create_output_files(stream
, true);
3077 lttng_ustconsumer_set_stream_ops(stream
);
3085 * Check if data is still being extracted from the buffers for a specific
3086 * stream. Consumer data lock MUST be acquired before calling this function
3087 * and the stream lock.
3089 * Return 1 if the traced data are still getting read else 0 meaning that the
3090 * data is available for trace viewer reading.
3092 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3096 LTTNG_ASSERT(stream
);
3097 LTTNG_ASSERT(stream
->ustream
);
3098 ASSERT_LOCKED(stream
->lock
);
3100 DBG("UST consumer checking data pending");
3102 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3107 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3108 uint64_t contiguous
, pushed
;
3110 /* Ease our life a bit. */
3111 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3112 contiguous
= stream
->chan
->metadata_cache
->contents
.size
;
3113 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3114 pushed
= stream
->ust_metadata_pushed
;
3117 * We can simply check whether all contiguously available data
3118 * has been pushed to the ring buffer, since the push operation
3119 * is performed within get_next_subbuf(), and because both
3120 * get_next_subbuf() and put_next_subbuf() are issued atomically
3121 * thanks to the stream lock within
3122 * lttng_ustconsumer_read_subbuffer(). This basically means that
3123 * whetnever ust_metadata_pushed is incremented, the associated
3124 * metadata has been consumed from the metadata stream.
3126 DBG("UST consumer metadata pending check: contiguous %" PRIu64
3127 " vs pushed %" PRIu64
,
3130 LTTNG_ASSERT(((int64_t) (contiguous
- pushed
)) >= 0);
3131 if ((contiguous
!= pushed
) ||
3132 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3133 ret
= 1; /* Data is pending */
3137 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
3140 * There is still data so let's put back this
3143 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
3144 LTTNG_ASSERT(ret
== 0);
3145 ret
= 1; /* Data is pending */
3150 /* Data is NOT pending so ready to be read. */
3158 * Stop a given metadata channel timer if enabled and close the wait fd which
3159 * is the poll pipe of the metadata stream.
3161 * This MUST be called with the metadata channel lock acquired.
3163 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3167 LTTNG_ASSERT(metadata
);
3168 LTTNG_ASSERT(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3170 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3172 if (metadata
->switch_timer_enabled
== 1) {
3173 consumer_timer_switch_stop(metadata
);
3176 if (!metadata
->metadata_stream
) {
3181 * Closing write side so the thread monitoring the stream wakes up if any
3182 * and clean the metadata stream.
3184 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3185 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3187 PERROR("closing metadata pipe write side");
3189 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3197 * Close every metadata stream wait fd of the metadata hash table. This
3198 * function MUST be used very carefully so not to run into a race between the
3199 * metadata thread handling streams and this function closing their wait fd.
3201 * For UST, this is used when the session daemon hangs up. Its the metadata
3202 * producer so calling this is safe because we are assured that no state change
3203 * can occur in the metadata thread for the streams in the hash table.
3205 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3207 struct lttng_ht_iter iter
;
3208 struct lttng_consumer_stream
*stream
;
3210 LTTNG_ASSERT(metadata_ht
);
3211 LTTNG_ASSERT(metadata_ht
->ht
);
3213 DBG("UST consumer closing all metadata streams");
3216 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
3217 health_code_update();
3219 pthread_mutex_lock(&stream
->chan
->lock
);
3220 lttng_ustconsumer_close_metadata(stream
->chan
);
3221 pthread_mutex_unlock(&stream
->chan
->lock
);
3226 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3230 ret
= lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
3232 ERR("Unable to close wakeup fd");
3237 * Please refer to consumer-timer.c before adding any lock within this
3238 * function or any of its callees. Timers have a very strict locking
3239 * semantic with respect to teardown. Failure to respect this semantic
3240 * introduces deadlocks.
3242 * DON'T hold the metadata lock when calling this function, else this
3243 * can cause deadlock involving consumer awaiting for metadata to be
3244 * pushed out due to concurrent interaction with the session daemon.
3246 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3247 struct lttng_consumer_channel
*channel
,
3251 struct lttcomm_metadata_request_msg request
;
3252 struct lttcomm_consumer_msg msg
;
3253 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3254 uint64_t len
, key
, offset
, version
;
3257 LTTNG_ASSERT(channel
);
3258 LTTNG_ASSERT(channel
->metadata_cache
);
3260 memset(&request
, 0, sizeof(request
));
3262 /* send the metadata request to sessiond */
3263 switch (the_consumer_data
.type
) {
3264 case LTTNG_CONSUMER64_UST
:
3265 request
.bits_per_long
= 64;
3267 case LTTNG_CONSUMER32_UST
:
3268 request
.bits_per_long
= 32;
3271 request
.bits_per_long
= 0;
3275 request
.session_id
= channel
->session_id
;
3276 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3278 * Request the application UID here so the metadata of that application can
3279 * be sent back. The channel UID corresponds to the user UID of the session
3280 * used for the rights on the stream file(s).
3282 request
.uid
= channel
->ust_app_uid
;
3283 request
.key
= channel
->key
;
3285 DBG("Sending metadata request to sessiond, session id %" PRIu64
", per-pid %" PRIu64
3286 ", app UID %u and channel key %" PRIu64
,
3288 request
.session_id_per_pid
,
3292 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3294 health_code_update();
3296 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
, sizeof(request
));
3298 ERR("Asking metadata to sessiond");
3302 health_code_update();
3304 /* Receive the metadata from sessiond */
3305 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
, sizeof(msg
));
3306 if (ret
!= sizeof(msg
)) {
3307 DBG("Consumer received unexpected message size %d (expects %zu)", ret
, sizeof(msg
));
3308 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3310 * The ret value might 0 meaning an orderly shutdown but this is ok
3311 * since the caller handles this.
3316 health_code_update();
3318 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3319 /* No registry found */
3320 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret_code
);
3323 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3324 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3329 len
= msg
.u
.push_metadata
.len
;
3330 key
= msg
.u
.push_metadata
.key
;
3331 offset
= msg
.u
.push_metadata
.target_offset
;
3332 version
= msg
.u
.push_metadata
.version
;
3334 LTTNG_ASSERT(key
== channel
->key
);
3336 DBG("No new metadata to receive for key %" PRIu64
, key
);
3339 health_code_update();
3341 /* Tell session daemon we are ready to receive the metadata. */
3342 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
, LTTCOMM_CONSUMERD_SUCCESS
);
3343 if (ret
< 0 || len
== 0) {
3345 * Somehow, the session daemon is not responding anymore or there is
3346 * nothing to receive.
3351 health_code_update();
3353 ret
= lttng_ustconsumer_recv_metadata(
3354 ctx
->consumer_metadata_socket
, key
, offset
, len
, version
, channel
, timer
, wait
);
3357 * Only send the status msg if the sessiond is alive meaning a positive
3360 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3365 health_code_update();
3367 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3372 * Return the ustctl call for the get stream id.
3374 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
, uint64_t *stream_id
)
3376 LTTNG_ASSERT(stream
);
3377 LTTNG_ASSERT(stream_id
);
3379 return lttng_ust_ctl_get_stream_id(stream
->ustream
, stream_id
);
3382 void lttng_ustconsumer_sigbus_handle(void *addr
)
3384 lttng_ust_ctl_sigbus_handle(addr
);