2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
12 #include <lttng/ust-ctl.h>
18 #include <sys/socket.h>
20 #include <sys/types.h>
23 #include <urcu/list.h>
28 #include <bin/lttng-consumerd/health-consumerd.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/relayd/relayd.h>
32 #include <common/compat/fcntl.h>
33 #include <common/compat/endian.h>
34 #include <common/consumer/consumer-metadata-cache.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/utils.h>
38 #include <common/index/index.h>
39 #include <common/consumer/consumer.h>
40 #include <common/optional.h>
42 #include "ust-consumer.h"
44 #define INT_MAX_STR_LEN 12 /* includes \0 */
46 extern struct lttng_consumer_global_data consumer_data
;
47 extern int consumer_poll_timeout
;
50 * Free channel object and all streams associated with it. This MUST be used
51 * only and only if the channel has _NEVER_ been added to the global channel
54 static void destroy_channel(struct lttng_consumer_channel
*channel
)
56 struct lttng_consumer_stream
*stream
, *stmp
;
60 DBG("UST consumer cleaning stream list");
62 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
67 cds_list_del(&stream
->send_node
);
68 ustctl_destroy_stream(stream
->ustream
);
69 lttng_trace_chunk_put(stream
->trace_chunk
);
74 * If a channel is available meaning that was created before the streams
78 lttng_ustconsumer_del_channel(channel
);
79 lttng_ustconsumer_free_channel(channel
);
82 if (channel
->trace_chunk
) {
83 lttng_trace_chunk_put(channel
->trace_chunk
);
90 * Add channel to internal consumer state.
92 * Returns 0 on success or else a negative value.
94 static int add_channel(struct lttng_consumer_channel
*channel
,
95 struct lttng_consumer_local_data
*ctx
)
102 if (ctx
->on_recv_channel
!= NULL
) {
103 ret
= ctx
->on_recv_channel(channel
);
105 ret
= consumer_add_channel(channel
, ctx
);
106 } else if (ret
< 0) {
107 /* Most likely an ENOMEM. */
108 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
112 ret
= consumer_add_channel(channel
, ctx
);
115 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
122 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
123 * error value if applicable is set in it else it is kept untouched.
125 * Return NULL on error else the newly allocated stream object.
127 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
128 struct lttng_consumer_channel
*channel
,
129 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
132 struct lttng_consumer_stream
*stream
= NULL
;
137 stream
= consumer_stream_create(
144 channel
->trace_chunk
,
149 if (stream
== NULL
) {
153 * We could not find the channel. Can happen if cpu hotplug
154 * happens while tearing down.
156 DBG3("Could not find channel");
161 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
167 consumer_stream_update_channel_attributes(stream
, channel
);
171 *_alloc_ret
= alloc_ret
;
177 * Send the given stream pointer to the corresponding thread.
179 * Returns 0 on success else a negative value.
181 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
182 struct lttng_consumer_local_data
*ctx
)
185 struct lttng_pipe
*stream_pipe
;
187 /* Get the right pipe where the stream will be sent. */
188 if (stream
->metadata_flag
) {
189 consumer_add_metadata_stream(stream
);
190 stream_pipe
= ctx
->consumer_metadata_pipe
;
192 consumer_add_data_stream(stream
);
193 stream_pipe
= ctx
->consumer_data_pipe
;
197 * From this point on, the stream's ownership has been moved away from
198 * the channel and it becomes globally visible. Hence, remove it from
199 * the local stream list to prevent the stream from being both local and
202 stream
->globally_visible
= 1;
203 cds_list_del(&stream
->send_node
);
205 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
207 ERR("Consumer write %s stream to pipe %d",
208 stream
->metadata_flag
? "metadata" : "data",
209 lttng_pipe_get_writefd(stream_pipe
));
210 if (stream
->metadata_flag
) {
211 consumer_del_stream_for_metadata(stream
);
213 consumer_del_stream_for_data(stream
);
223 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
225 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
228 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
229 stream_shm_path
[PATH_MAX
- 1] = '\0';
230 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
235 strncat(stream_shm_path
, cpu_nr
,
236 PATH_MAX
- strlen(stream_shm_path
) - 1);
243 * Create streams for the given channel using liblttng-ust-ctl.
244 * The channel lock must be acquired by the caller.
246 * Return 0 on success else a negative value.
248 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
249 struct lttng_consumer_local_data
*ctx
)
252 struct ustctl_consumer_stream
*ustream
;
253 struct lttng_consumer_stream
*stream
;
254 pthread_mutex_t
*current_stream_lock
= NULL
;
260 * While a stream is available from ustctl. When NULL is returned, we've
261 * reached the end of the possible stream for the channel.
263 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
265 int ust_metadata_pipe
[2];
267 health_code_update();
269 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
270 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
272 ERR("Create ust metadata poll pipe");
275 wait_fd
= ust_metadata_pipe
[0];
277 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
280 /* Allocate consumer stream object. */
281 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
285 stream
->ustream
= ustream
;
287 * Store it so we can save multiple function calls afterwards since
288 * this value is used heavily in the stream threads. This is UST
289 * specific so this is why it's done after allocation.
291 stream
->wait_fd
= wait_fd
;
294 * Increment channel refcount since the channel reference has now been
295 * assigned in the allocation process above.
297 if (stream
->chan
->monitor
) {
298 uatomic_inc(&stream
->chan
->refcount
);
301 pthread_mutex_lock(&stream
->lock
);
302 current_stream_lock
= &stream
->lock
;
304 * Order is important this is why a list is used. On error, the caller
305 * should clean this list.
307 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
309 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
310 &stream
->max_sb_size
);
312 ERR("ustctl_get_max_subbuf_size failed for stream %s",
317 /* Do actions once stream has been received. */
318 if (ctx
->on_recv_stream
) {
319 ret
= ctx
->on_recv_stream(stream
);
325 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
326 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
328 /* Set next CPU stream. */
329 channel
->streams
.count
= ++cpu
;
331 /* Keep stream reference when creating metadata. */
332 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
333 channel
->metadata_stream
= stream
;
334 if (channel
->monitor
) {
335 /* Set metadata poll pipe if we created one */
336 memcpy(stream
->ust_metadata_poll_pipe
,
338 sizeof(ust_metadata_pipe
));
341 pthread_mutex_unlock(&stream
->lock
);
342 current_stream_lock
= NULL
;
349 if (current_stream_lock
) {
350 pthread_mutex_unlock(current_stream_lock
);
356 * create_posix_shm is never called concurrently within a process.
359 int create_posix_shm(void)
361 char tmp_name
[NAME_MAX
];
364 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
370 * Allocate shm, and immediately unlink its shm oject, keeping
371 * only the file descriptor as a reference to the object.
372 * We specifically do _not_ use the / at the beginning of the
373 * pathname so that some OS implementations can keep it local to
374 * the process (POSIX leaves this implementation-defined).
376 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
381 ret
= shm_unlink(tmp_name
);
382 if (ret
< 0 && errno
!= ENOENT
) {
383 PERROR("shm_unlink");
384 goto error_shm_release
;
397 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
398 const struct lttng_credentials
*session_credentials
)
400 char shm_path
[PATH_MAX
];
403 if (!channel
->shm_path
[0]) {
404 return create_posix_shm();
406 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
410 return run_as_open(shm_path
,
411 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
412 session_credentials
->uid
, session_credentials
->gid
);
419 * Create an UST channel with the given attributes and send it to the session
420 * daemon using the ust ctl API.
422 * Return 0 on success or else a negative value.
424 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
425 struct ustctl_consumer_channel_attr
*attr
,
426 struct ustctl_consumer_channel
**ust_chanp
)
428 int ret
, nr_stream_fds
, i
, j
;
430 struct ustctl_consumer_channel
*ust_channel
;
435 assert(channel
->buffer_credentials
.is_set
);
437 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
438 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
439 "switch_timer_interval: %u, read_timer_interval: %u, "
440 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
441 attr
->num_subbuf
, attr
->switch_timer_interval
,
442 attr
->read_timer_interval
, attr
->output
, attr
->type
);
444 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
447 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
448 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
453 for (i
= 0; i
< nr_stream_fds
; i
++) {
454 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
455 &channel
->buffer_credentials
.value
);
456 if (stream_fds
[i
] < 0) {
461 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
466 channel
->nr_stream_fds
= nr_stream_fds
;
467 channel
->stream_fds
= stream_fds
;
468 *ust_chanp
= ust_channel
;
474 for (j
= i
- 1; j
>= 0; j
--) {
477 closeret
= close(stream_fds
[j
]);
481 if (channel
->shm_path
[0]) {
482 char shm_path
[PATH_MAX
];
484 closeret
= get_stream_shm_path(shm_path
,
485 channel
->shm_path
, j
);
487 ERR("Cannot get stream shm path");
489 closeret
= run_as_unlink(shm_path
,
490 channel
->buffer_credentials
.value
.uid
,
491 channel
->buffer_credentials
.value
.gid
);
493 PERROR("unlink %s", shm_path
);
497 /* Try to rmdir all directories under shm_path root. */
498 if (channel
->root_shm_path
[0]) {
499 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
500 channel
->buffer_credentials
.value
.uid
,
501 channel
->buffer_credentials
.value
.gid
,
502 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
510 * Send a single given stream to the session daemon using the sock.
512 * Return 0 on success else a negative value.
514 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
521 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
523 /* Send stream to session daemon. */
524 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
534 * Send channel to sessiond and relayd if applicable.
536 * Return 0 on success or else a negative value.
538 static int send_channel_to_sessiond_and_relayd(int sock
,
539 struct lttng_consumer_channel
*channel
,
540 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
542 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
543 struct lttng_consumer_stream
*stream
;
544 uint64_t net_seq_idx
= -1ULL;
550 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
552 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
553 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
555 health_code_update();
557 /* Try to send the stream to the relayd if one is available. */
558 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
559 stream
->key
, channel
->name
);
560 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
563 * Flag that the relayd was the problem here probably due to a
564 * communicaton error on the socket.
569 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
571 if (net_seq_idx
== -1ULL) {
572 net_seq_idx
= stream
->net_seq_idx
;
577 /* Inform sessiond that we are about to send channel and streams. */
578 ret
= consumer_send_status_msg(sock
, ret_code
);
579 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
581 * Either the session daemon is not responding or the relayd died so we
587 /* Send channel to sessiond. */
588 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
593 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
598 /* The channel was sent successfully to the sessiond at this point. */
599 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
601 health_code_update();
603 /* Send stream to session daemon. */
604 ret
= send_sessiond_stream(sock
, stream
);
610 /* Tell sessiond there is no more stream. */
611 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
616 DBG("UST consumer NULL stream sent to sessiond");
621 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
628 * Creates a channel and streams and add the channel it to the channel internal
629 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
632 * Return 0 on success or else, a negative value is returned and the channel
633 * MUST be destroyed by consumer_del_channel().
635 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
636 struct lttng_consumer_channel
*channel
,
637 struct ustctl_consumer_channel_attr
*attr
)
646 * This value is still used by the kernel consumer since for the kernel,
647 * the stream ownership is not IN the consumer so we need to have the
648 * number of left stream that needs to be initialized so we can know when
649 * to delete the channel (see consumer.c).
651 * As for the user space tracer now, the consumer creates and sends the
652 * stream to the session daemon which only sends them to the application
653 * once every stream of a channel is received making this value useless
654 * because we they will be added to the poll thread before the application
655 * receives them. This ensures that a stream can not hang up during
656 * initilization of a channel.
658 channel
->nb_init_stream_left
= 0;
660 /* The reply msg status is handled in the following call. */
661 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
666 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
669 * For the snapshots (no monitor), we create the metadata streams
670 * on demand, not during the channel creation.
672 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
677 /* Open all streams for this channel. */
678 pthread_mutex_lock(&channel
->lock
);
679 ret
= create_ust_streams(channel
, ctx
);
680 pthread_mutex_unlock(&channel
->lock
);
690 * Send all stream of a channel to the right thread handling it.
692 * On error, return a negative value else 0 on success.
694 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
695 struct lttng_consumer_local_data
*ctx
)
698 struct lttng_consumer_stream
*stream
, *stmp
;
703 /* Send streams to the corresponding thread. */
704 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
707 health_code_update();
709 /* Sending the stream to the thread. */
710 ret
= send_stream_to_thread(stream
, ctx
);
713 * If we are unable to send the stream to the thread, there is
714 * a big problem so just stop everything.
725 * Flush channel's streams using the given key to retrieve the channel.
727 * Return 0 on success else an LTTng error code.
729 static int flush_channel(uint64_t chan_key
)
732 struct lttng_consumer_channel
*channel
;
733 struct lttng_consumer_stream
*stream
;
735 struct lttng_ht_iter iter
;
737 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
740 channel
= consumer_find_channel(chan_key
);
742 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
743 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
747 ht
= consumer_data
.stream_per_chan_id_ht
;
749 /* For each stream of the channel id, flush it. */
750 cds_lfht_for_each_entry_duplicate(ht
->ht
,
751 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
752 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
754 health_code_update();
756 pthread_mutex_lock(&stream
->lock
);
759 * Protect against concurrent teardown of a stream.
761 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
765 if (!stream
->quiescent
) {
766 ustctl_flush_buffer(stream
->ustream
, 0);
767 stream
->quiescent
= true;
770 pthread_mutex_unlock(&stream
->lock
);
778 * Clear quiescent state from channel's streams using the given key to
779 * retrieve the channel.
781 * Return 0 on success else an LTTng error code.
783 static int clear_quiescent_channel(uint64_t chan_key
)
786 struct lttng_consumer_channel
*channel
;
787 struct lttng_consumer_stream
*stream
;
789 struct lttng_ht_iter iter
;
791 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
794 channel
= consumer_find_channel(chan_key
);
796 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
797 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
801 ht
= consumer_data
.stream_per_chan_id_ht
;
803 /* For each stream of the channel id, clear quiescent state. */
804 cds_lfht_for_each_entry_duplicate(ht
->ht
,
805 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
806 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
808 health_code_update();
810 pthread_mutex_lock(&stream
->lock
);
811 stream
->quiescent
= false;
812 pthread_mutex_unlock(&stream
->lock
);
820 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
822 * Return 0 on success else an LTTng error code.
824 static int close_metadata(uint64_t chan_key
)
827 struct lttng_consumer_channel
*channel
;
828 unsigned int channel_monitor
;
830 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
832 channel
= consumer_find_channel(chan_key
);
835 * This is possible if the metadata thread has issue a delete because
836 * the endpoint point of the stream hung up. There is no way the
837 * session daemon can know about it thus use a DBG instead of an actual
840 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
841 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
845 pthread_mutex_lock(&consumer_data
.lock
);
846 pthread_mutex_lock(&channel
->lock
);
847 channel_monitor
= channel
->monitor
;
848 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
852 lttng_ustconsumer_close_metadata(channel
);
853 pthread_mutex_unlock(&channel
->lock
);
854 pthread_mutex_unlock(&consumer_data
.lock
);
857 * The ownership of a metadata channel depends on the type of
858 * session to which it belongs. In effect, the monitor flag is checked
859 * to determine if this metadata channel is in "snapshot" mode or not.
861 * In the non-snapshot case, the metadata channel is created along with
862 * a single stream which will remain present until the metadata channel
863 * is destroyed (on the destruction of its session). In this case, the
864 * metadata stream in "monitored" by the metadata poll thread and holds
865 * the ownership of its channel.
867 * Closing the metadata will cause the metadata stream's "metadata poll
868 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
869 * thread which will teardown the metadata stream which, in return,
870 * deletes the metadata channel.
872 * In the snapshot case, the metadata stream is created and destroyed
873 * on every snapshot record. Since the channel doesn't have an owner
874 * other than the session daemon, it is safe to destroy it immediately
875 * on reception of the CLOSE_METADATA command.
877 if (!channel_monitor
) {
879 * The channel and consumer_data locks must be
880 * released before this call since consumer_del_channel
881 * re-acquires the channel and consumer_data locks to teardown
882 * the channel and queue its reclamation by the "call_rcu"
885 consumer_del_channel(channel
);
890 pthread_mutex_unlock(&channel
->lock
);
891 pthread_mutex_unlock(&consumer_data
.lock
);
897 * RCU read side lock MUST be acquired before calling this function.
899 * Return 0 on success else an LTTng error code.
901 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
904 struct lttng_consumer_channel
*metadata
;
906 DBG("UST consumer setup metadata key %" PRIu64
, key
);
908 metadata
= consumer_find_channel(key
);
910 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
911 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
916 * In no monitor mode, the metadata channel has no stream(s) so skip the
917 * ownership transfer to the metadata thread.
919 if (!metadata
->monitor
) {
920 DBG("Metadata channel in no monitor");
926 * Send metadata stream to relayd if one available. Availability is
927 * known if the stream is still in the list of the channel.
929 if (cds_list_empty(&metadata
->streams
.head
)) {
930 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
931 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
932 goto error_no_stream
;
935 /* Send metadata stream to relayd if needed. */
936 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
937 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
940 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
943 ret
= consumer_send_relayd_streams_sent(
944 metadata
->metadata_stream
->net_seq_idx
);
946 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
952 * Ownership of metadata stream is passed along. Freeing is handled by
955 ret
= send_streams_to_thread(metadata
, ctx
);
958 * If we are unable to send the stream to the thread, there is
959 * a big problem so just stop everything.
961 ret
= LTTCOMM_CONSUMERD_FATAL
;
962 goto send_streams_error
;
964 /* List MUST be empty after or else it could be reused. */
965 assert(cds_list_empty(&metadata
->streams
.head
));
972 * Delete metadata channel on error. At this point, the metadata stream can
973 * NOT be monitored by the metadata thread thus having the guarantee that
974 * the stream is still in the local stream list of the channel. This call
975 * will make sure to clean that list.
977 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
978 cds_list_del(&metadata
->metadata_stream
->send_node
);
979 metadata
->metadata_stream
= NULL
;
987 * Snapshot the whole metadata.
988 * RCU read-side lock must be held by the caller.
990 * Returns 0 on success, < 0 on error
992 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
993 uint64_t key
, char *path
, uint64_t relayd_id
,
994 struct lttng_consumer_local_data
*ctx
)
997 struct lttng_consumer_stream
*metadata_stream
;
1002 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1007 assert(!metadata_channel
->monitor
);
1009 health_code_update();
1012 * Ask the sessiond if we have new metadata waiting and update the
1013 * consumer metadata cache.
1015 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1020 health_code_update();
1023 * The metadata stream is NOT created in no monitor mode when the channel
1024 * is created on a sessiond ask channel command.
1026 ret
= create_ust_streams(metadata_channel
, ctx
);
1031 metadata_stream
= metadata_channel
->metadata_stream
;
1032 assert(metadata_stream
);
1034 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
1035 if (relayd_id
!= (uint64_t) -1ULL) {
1036 metadata_stream
->net_seq_idx
= relayd_id
;
1037 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1039 ret
= consumer_stream_create_output_files(metadata_stream
,
1047 health_code_update();
1048 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1055 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
1057 * Clean up the stream completely because the next snapshot will use a
1058 * new metadata stream.
1060 consumer_stream_destroy(metadata_stream
, NULL
);
1061 cds_list_del(&metadata_stream
->send_node
);
1062 metadata_channel
->metadata_stream
= NULL
;
1070 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1074 unsigned long mmap_offset
;
1075 const char *mmap_base
;
1077 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1079 ERR("Failed to get mmap base for stream `%s`",
1085 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1087 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1092 *addr
= mmap_base
+ mmap_offset
;
1099 * Take a snapshot of all the stream of a channel.
1100 * RCU read-side lock and the channel lock must be held by the caller.
1102 * Returns 0 on success, < 0 on error
1104 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1105 uint64_t key
, char *path
, uint64_t relayd_id
,
1106 uint64_t nb_packets_per_stream
,
1107 struct lttng_consumer_local_data
*ctx
)
1110 unsigned use_relayd
= 0;
1111 unsigned long consumed_pos
, produced_pos
;
1112 struct lttng_consumer_stream
*stream
;
1119 if (relayd_id
!= (uint64_t) -1ULL) {
1123 assert(!channel
->monitor
);
1124 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1126 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1127 health_code_update();
1129 /* Lock stream because we are about to change its state. */
1130 pthread_mutex_lock(&stream
->lock
);
1131 assert(channel
->trace_chunk
);
1132 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1134 * Can't happen barring an internal error as the channel
1135 * holds a reference to the trace chunk.
1137 ERR("Failed to acquire reference to channel's trace chunk");
1141 assert(!stream
->trace_chunk
);
1142 stream
->trace_chunk
= channel
->trace_chunk
;
1144 stream
->net_seq_idx
= relayd_id
;
1147 ret
= consumer_send_relayd_stream(stream
, path
);
1152 ret
= consumer_stream_create_output_files(stream
,
1157 DBG("UST consumer snapshot stream (%" PRIu64
")",
1162 * If tracing is active, we want to perform a "full" buffer flush.
1163 * Else, if quiescent, it has already been done by the prior stop.
1165 if (!stream
->quiescent
) {
1166 ustctl_flush_buffer(stream
->ustream
, 0);
1169 ret
= lttng_ustconsumer_take_snapshot(stream
);
1171 ERR("Taking UST snapshot");
1175 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1177 ERR("Produced UST snapshot position");
1181 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1183 ERR("Consumerd UST snapshot position");
1188 * The original value is sent back if max stream size is larger than
1189 * the possible size of the snapshot. Also, we assume that the session
1190 * daemon should never send a maximum stream size that is lower than
1193 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1194 produced_pos
, nb_packets_per_stream
,
1195 stream
->max_sb_size
);
1197 while ((long) (consumed_pos
- produced_pos
) < 0) {
1199 unsigned long len
, padded_len
;
1200 const char *subbuf_addr
;
1201 struct lttng_buffer_view subbuf_view
;
1203 health_code_update();
1205 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1207 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1209 if (ret
!= -EAGAIN
) {
1210 PERROR("ustctl_get_subbuf snapshot");
1211 goto error_close_stream
;
1213 DBG("UST consumer get subbuf failed. Skipping it.");
1214 consumed_pos
+= stream
->max_sb_size
;
1215 stream
->chan
->lost_packets
++;
1219 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1221 ERR("Snapshot ustctl_get_subbuf_size");
1222 goto error_put_subbuf
;
1225 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1227 ERR("Snapshot ustctl_get_padded_subbuf_size");
1228 goto error_put_subbuf
;
1231 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1233 goto error_put_subbuf
;
1236 subbuf_view
= lttng_buffer_view_init(
1237 subbuf_addr
, 0, padded_len
);
1238 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1239 stream
, &subbuf_view
, padded_len
- len
);
1241 if (read_len
!= len
) {
1243 goto error_put_subbuf
;
1246 if (read_len
!= padded_len
) {
1248 goto error_put_subbuf
;
1252 ret
= ustctl_put_subbuf(stream
->ustream
);
1254 ERR("Snapshot ustctl_put_subbuf");
1255 goto error_close_stream
;
1257 consumed_pos
+= stream
->max_sb_size
;
1260 /* Simply close the stream so we can use it on the next snapshot. */
1261 consumer_stream_close(stream
);
1262 pthread_mutex_unlock(&stream
->lock
);
1269 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1270 ERR("Snapshot ustctl_put_subbuf");
1273 consumer_stream_close(stream
);
1275 pthread_mutex_unlock(&stream
->lock
);
1281 void metadata_stream_reset_cache_consumed_position(
1282 struct lttng_consumer_stream
*stream
)
1284 ASSERT_LOCKED(stream
->lock
);
1286 DBG("Reset metadata cache of session %" PRIu64
,
1287 stream
->chan
->session_id
);
1288 stream
->ust_metadata_pushed
= 0;
1292 * Receive the metadata updates from the sessiond. Supports receiving
1293 * overlapping metadata, but is needs to always belong to a contiguous
1294 * range starting from 0.
1295 * Be careful about the locks held when calling this function: it needs
1296 * the metadata cache flush to concurrently progress in order to
1299 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1300 uint64_t len
, uint64_t version
,
1301 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1303 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1305 enum consumer_metadata_cache_write_status cache_write_status
;
1307 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1309 metadata_str
= zmalloc(len
* sizeof(char));
1310 if (!metadata_str
) {
1311 PERROR("zmalloc metadata string");
1312 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1316 health_code_update();
1318 /* Receive metadata string. */
1319 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1321 /* Session daemon is dead so return gracefully. */
1326 health_code_update();
1328 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1329 cache_write_status
= consumer_metadata_cache_write(
1330 channel
, offset
, len
, version
, metadata_str
);
1331 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1332 switch (cache_write_status
) {
1333 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1335 * The write entirely overlapped with existing contents of the
1336 * same metadata version (same content); there is nothing to do.
1339 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1341 * The metadata cache was invalidated (previously pushed
1342 * content has been overwritten). Reset the stream's consumed
1343 * metadata position to ensure the metadata poll thread consumes
1348 * channel::metadata_stream can be null when the metadata
1349 * channel is under a snapshot session type. No need to update
1350 * the stream position in that scenario.
1352 if (channel
->metadata_stream
!= NULL
) {
1353 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1354 metadata_stream_reset_cache_consumed_position(
1355 channel
->metadata_stream
);
1356 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1359 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1361 * In both cases, the metadata poll thread has new data to
1364 ret
= consumer_metadata_wakeup_pipe(channel
);
1366 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1370 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1371 /* Unable to handle metadata. Notify session daemon. */
1372 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1374 * Skip metadata flush on write error since the offset and len might
1375 * not have been updated which could create an infinite loop below when
1376 * waiting for the metadata cache to be flushed.
1386 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1387 DBG("Waiting for metadata to be flushed");
1389 health_code_update();
1391 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1401 * Receive command from session daemon and process it.
1403 * Return 1 on success else a negative value or 0.
1405 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1406 int sock
, struct pollfd
*consumer_sockpoll
)
1409 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1410 struct lttcomm_consumer_msg msg
;
1411 struct lttng_consumer_channel
*channel
= NULL
;
1413 health_code_update();
1415 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1416 if (ret
!= sizeof(msg
)) {
1417 DBG("Consumer received unexpected message size %zd (expects %zu)",
1420 * The ret value might 0 meaning an orderly shutdown but this is ok
1421 * since the caller handles this.
1424 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1430 health_code_update();
1433 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1435 health_code_update();
1437 /* relayd needs RCU read-side lock */
1440 switch (msg
.cmd_type
) {
1441 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1443 /* Session daemon status message are handled in the following call. */
1444 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1445 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1446 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1447 msg
.u
.relayd_sock
.relayd_session_id
);
1450 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1452 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1453 struct consumer_relayd_sock_pair
*relayd
;
1455 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1457 /* Get relayd reference if exists. */
1458 relayd
= consumer_find_relayd(index
);
1459 if (relayd
== NULL
) {
1460 DBG("Unable to find relayd %" PRIu64
, index
);
1461 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1465 * Each relayd socket pair has a refcount of stream attached to it
1466 * which tells if the relayd is still active or not depending on the
1469 * This will set the destroy flag of the relayd object and destroy it
1470 * if the refcount reaches zero when called.
1472 * The destroy can happen either here or when a stream fd hangs up.
1475 consumer_flag_relayd_for_destroy(relayd
);
1478 goto end_msg_sessiond
;
1480 case LTTNG_CONSUMER_UPDATE_STREAM
:
1485 case LTTNG_CONSUMER_DATA_PENDING
:
1487 int ret
, is_data_pending
;
1488 uint64_t id
= msg
.u
.data_pending
.session_id
;
1490 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1492 is_data_pending
= consumer_data_pending(id
);
1494 /* Send back returned value to session daemon */
1495 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1496 sizeof(is_data_pending
));
1498 DBG("Error when sending the data pending ret code: %d", ret
);
1503 * No need to send back a status message since the data pending
1504 * returned value is the response.
1508 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1511 struct ustctl_consumer_channel_attr attr
;
1512 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1513 const struct lttng_credentials buffer_credentials
= {
1514 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1515 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1518 /* Create a plain object and reserve a channel key. */
1519 channel
= consumer_allocate_channel(
1520 msg
.u
.ask_channel
.key
,
1521 msg
.u
.ask_channel
.session_id
,
1522 msg
.u
.ask_channel
.chunk_id
.is_set
?
1524 msg
.u
.ask_channel
.pathname
,
1525 msg
.u
.ask_channel
.name
,
1526 msg
.u
.ask_channel
.relayd_id
,
1527 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1528 msg
.u
.ask_channel
.tracefile_size
,
1529 msg
.u
.ask_channel
.tracefile_count
,
1530 msg
.u
.ask_channel
.session_id_per_pid
,
1531 msg
.u
.ask_channel
.monitor
,
1532 msg
.u
.ask_channel
.live_timer_interval
,
1533 msg
.u
.ask_channel
.is_live
,
1534 msg
.u
.ask_channel
.root_shm_path
,
1535 msg
.u
.ask_channel
.shm_path
);
1537 goto end_channel_error
;
1540 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1541 buffer_credentials
);
1544 * Assign UST application UID to the channel. This value is ignored for
1545 * per PID buffers. This is specific to UST thus setting this after the
1548 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1550 /* Build channel attributes from received message. */
1551 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1552 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1553 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1554 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1555 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1556 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1557 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1558 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1560 /* Match channel buffer type to the UST abi. */
1561 switch (msg
.u
.ask_channel
.output
) {
1562 case LTTNG_EVENT_MMAP
:
1564 attr
.output
= LTTNG_UST_MMAP
;
1568 /* Translate and save channel type. */
1569 switch (msg
.u
.ask_channel
.type
) {
1570 case LTTNG_UST_CHAN_PER_CPU
:
1571 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1572 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1574 * Set refcount to 1 for owner. Below, we will
1575 * pass ownership to the
1576 * consumer_thread_channel_poll() thread.
1578 channel
->refcount
= 1;
1580 case LTTNG_UST_CHAN_METADATA
:
1581 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1582 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1589 health_code_update();
1591 ret
= ask_channel(ctx
, channel
, &attr
);
1593 goto end_channel_error
;
1596 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1597 ret
= consumer_metadata_cache_allocate(channel
);
1599 ERR("Allocating metadata cache");
1600 goto end_channel_error
;
1602 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1603 attr
.switch_timer_interval
= 0;
1605 int monitor_start_ret
;
1607 consumer_timer_live_start(channel
,
1608 msg
.u
.ask_channel
.live_timer_interval
);
1609 monitor_start_ret
= consumer_timer_monitor_start(
1611 msg
.u
.ask_channel
.monitor_timer_interval
);
1612 if (monitor_start_ret
< 0) {
1613 ERR("Starting channel monitoring timer failed");
1614 goto end_channel_error
;
1618 health_code_update();
1621 * Add the channel to the internal state AFTER all streams were created
1622 * and successfully sent to session daemon. This way, all streams must
1623 * be ready before this channel is visible to the threads.
1624 * If add_channel succeeds, ownership of the channel is
1625 * passed to consumer_thread_channel_poll().
1627 ret
= add_channel(channel
, ctx
);
1629 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1630 if (channel
->switch_timer_enabled
== 1) {
1631 consumer_timer_switch_stop(channel
);
1633 consumer_metadata_cache_destroy(channel
);
1635 if (channel
->live_timer_enabled
== 1) {
1636 consumer_timer_live_stop(channel
);
1638 if (channel
->monitor_timer_enabled
== 1) {
1639 consumer_timer_monitor_stop(channel
);
1641 goto end_channel_error
;
1644 health_code_update();
1647 * Channel and streams are now created. Inform the session daemon that
1648 * everything went well and should wait to receive the channel and
1649 * streams with ustctl API.
1651 ret
= consumer_send_status_channel(sock
, channel
);
1654 * There is probably a problem on the socket.
1661 case LTTNG_CONSUMER_GET_CHANNEL
:
1663 int ret
, relayd_err
= 0;
1664 uint64_t key
= msg
.u
.get_channel
.key
;
1665 struct lttng_consumer_channel
*channel
;
1667 channel
= consumer_find_channel(key
);
1669 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1670 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1671 goto end_get_channel
;
1674 health_code_update();
1676 /* Send the channel to sessiond (and relayd, if applicable). */
1677 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1682 * We were unable to send to the relayd the stream so avoid
1683 * sending back a fatal error to the thread since this is OK
1684 * and the consumer can continue its work. The above call
1685 * has sent the error status message to the sessiond.
1687 goto end_get_channel_nosignal
;
1690 * The communicaton was broken hence there is a bad state between
1691 * the consumer and sessiond so stop everything.
1693 goto error_get_channel_fatal
;
1696 health_code_update();
1699 * In no monitor mode, the streams ownership is kept inside the channel
1700 * so don't send them to the data thread.
1702 if (!channel
->monitor
) {
1703 goto end_get_channel
;
1706 ret
= send_streams_to_thread(channel
, ctx
);
1709 * If we are unable to send the stream to the thread, there is
1710 * a big problem so just stop everything.
1712 goto error_get_channel_fatal
;
1714 /* List MUST be empty after or else it could be reused. */
1715 assert(cds_list_empty(&channel
->streams
.head
));
1717 goto end_msg_sessiond
;
1718 error_get_channel_fatal
:
1720 end_get_channel_nosignal
:
1723 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1725 uint64_t key
= msg
.u
.destroy_channel
.key
;
1728 * Only called if streams have not been sent to stream
1729 * manager thread. However, channel has been sent to
1730 * channel manager thread.
1732 notify_thread_del_channel(ctx
, key
);
1733 goto end_msg_sessiond
;
1735 case LTTNG_CONSUMER_CLOSE_METADATA
:
1739 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1744 goto end_msg_sessiond
;
1746 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1750 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1755 goto end_msg_sessiond
;
1757 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1761 ret
= clear_quiescent_channel(
1762 msg
.u
.clear_quiescent_channel
.key
);
1767 goto end_msg_sessiond
;
1769 case LTTNG_CONSUMER_PUSH_METADATA
:
1772 uint64_t len
= msg
.u
.push_metadata
.len
;
1773 uint64_t key
= msg
.u
.push_metadata
.key
;
1774 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1775 uint64_t version
= msg
.u
.push_metadata
.version
;
1776 struct lttng_consumer_channel
*channel
;
1778 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1781 channel
= consumer_find_channel(key
);
1784 * This is possible if the metadata creation on the consumer side
1785 * is in flight vis-a-vis a concurrent push metadata from the
1786 * session daemon. Simply return that the channel failed and the
1787 * session daemon will handle that message correctly considering
1788 * that this race is acceptable thus the DBG() statement here.
1790 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1791 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1792 goto end_push_metadata_msg_sessiond
;
1795 health_code_update();
1799 * There is nothing to receive. We have simply
1800 * checked whether the channel can be found.
1802 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1803 goto end_push_metadata_msg_sessiond
;
1806 /* Tell session daemon we are ready to receive the metadata. */
1807 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1809 /* Somehow, the session daemon is not responding anymore. */
1810 goto error_push_metadata_fatal
;
1813 health_code_update();
1815 /* Wait for more data. */
1816 health_poll_entry();
1817 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1820 goto error_push_metadata_fatal
;
1823 health_code_update();
1825 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1826 len
, version
, channel
, 0, 1);
1828 /* error receiving from sessiond */
1829 goto error_push_metadata_fatal
;
1832 goto end_push_metadata_msg_sessiond
;
1834 end_push_metadata_msg_sessiond
:
1835 goto end_msg_sessiond
;
1836 error_push_metadata_fatal
:
1839 case LTTNG_CONSUMER_SETUP_METADATA
:
1843 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1847 goto end_msg_sessiond
;
1849 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1851 struct lttng_consumer_channel
*channel
;
1852 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1854 channel
= consumer_find_channel(key
);
1856 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1857 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1859 if (msg
.u
.snapshot_channel
.metadata
) {
1860 ret
= snapshot_metadata(channel
, key
,
1861 msg
.u
.snapshot_channel
.pathname
,
1862 msg
.u
.snapshot_channel
.relayd_id
,
1865 ERR("Snapshot metadata failed");
1866 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1869 ret
= snapshot_channel(channel
, key
,
1870 msg
.u
.snapshot_channel
.pathname
,
1871 msg
.u
.snapshot_channel
.relayd_id
,
1872 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1875 ERR("Snapshot channel failed");
1876 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1880 health_code_update();
1881 ret
= consumer_send_status_msg(sock
, ret_code
);
1883 /* Somehow, the session daemon is not responding anymore. */
1886 health_code_update();
1889 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1892 uint64_t discarded_events
;
1893 struct lttng_ht_iter iter
;
1894 struct lttng_ht
*ht
;
1895 struct lttng_consumer_stream
*stream
;
1896 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1897 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1899 DBG("UST consumer discarded events command for session id %"
1902 pthread_mutex_lock(&consumer_data
.lock
);
1904 ht
= consumer_data
.stream_list_ht
;
1907 * We only need a reference to the channel, but they are not
1908 * directly indexed, so we just use the first matching stream
1909 * to extract the information we need, we default to 0 if not
1910 * found (no events are dropped if the channel is not yet in
1913 discarded_events
= 0;
1914 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1915 ht
->hash_fct(&id
, lttng_ht_seed
),
1917 &iter
.iter
, stream
, node_session_id
.node
) {
1918 if (stream
->chan
->key
== key
) {
1919 discarded_events
= stream
->chan
->discarded_events
;
1923 pthread_mutex_unlock(&consumer_data
.lock
);
1926 DBG("UST consumer discarded events command for session id %"
1927 PRIu64
", channel key %" PRIu64
, id
, key
);
1929 health_code_update();
1931 /* Send back returned value to session daemon */
1932 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1934 PERROR("send discarded events");
1940 case LTTNG_CONSUMER_LOST_PACKETS
:
1943 uint64_t lost_packets
;
1944 struct lttng_ht_iter iter
;
1945 struct lttng_ht
*ht
;
1946 struct lttng_consumer_stream
*stream
;
1947 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1948 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1950 DBG("UST consumer lost packets command for session id %"
1953 pthread_mutex_lock(&consumer_data
.lock
);
1955 ht
= consumer_data
.stream_list_ht
;
1958 * We only need a reference to the channel, but they are not
1959 * directly indexed, so we just use the first matching stream
1960 * to extract the information we need, we default to 0 if not
1961 * found (no packets lost if the channel is not yet in use).
1964 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1965 ht
->hash_fct(&id
, lttng_ht_seed
),
1967 &iter
.iter
, stream
, node_session_id
.node
) {
1968 if (stream
->chan
->key
== key
) {
1969 lost_packets
= stream
->chan
->lost_packets
;
1973 pthread_mutex_unlock(&consumer_data
.lock
);
1976 DBG("UST consumer lost packets command for session id %"
1977 PRIu64
", channel key %" PRIu64
, id
, key
);
1979 health_code_update();
1981 /* Send back returned value to session daemon */
1982 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1983 sizeof(lost_packets
));
1985 PERROR("send lost packets");
1991 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1993 int channel_monitor_pipe
;
1995 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1996 /* Successfully received the command's type. */
1997 ret
= consumer_send_status_msg(sock
, ret_code
);
2002 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
2004 if (ret
!= sizeof(channel_monitor_pipe
)) {
2005 ERR("Failed to receive channel monitor pipe");
2009 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
2010 ret
= consumer_timer_thread_set_channel_monitor_pipe(
2011 channel_monitor_pipe
);
2015 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2016 /* Set the pipe as non-blocking. */
2017 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
2019 PERROR("fcntl get flags of the channel monitoring pipe");
2024 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
2025 flags
| O_NONBLOCK
);
2027 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2030 DBG("Channel monitor pipe set as non-blocking");
2032 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2034 goto end_msg_sessiond
;
2036 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2038 struct lttng_consumer_channel
*channel
;
2039 uint64_t key
= msg
.u
.rotate_channel
.key
;
2041 channel
= consumer_find_channel(key
);
2043 DBG("Channel %" PRIu64
" not found", key
);
2044 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2047 * Sample the rotate position of all the streams in
2050 ret
= lttng_consumer_rotate_channel(channel
, key
,
2051 msg
.u
.rotate_channel
.relayd_id
,
2052 msg
.u
.rotate_channel
.metadata
,
2055 ERR("Rotate channel failed");
2056 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2059 health_code_update();
2061 ret
= consumer_send_status_msg(sock
, ret_code
);
2063 /* Somehow, the session daemon is not responding anymore. */
2064 goto end_rotate_channel_nosignal
;
2068 * Rotate the streams that are ready right now.
2069 * FIXME: this is a second consecutive iteration over the
2070 * streams in a channel, there is probably a better way to
2071 * handle this, but it needs to be after the
2072 * consumer_send_status_msg() call.
2075 ret
= lttng_consumer_rotate_ready_streams(
2078 ERR("Rotate channel failed");
2082 end_rotate_channel_nosignal
:
2085 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2087 struct lttng_consumer_channel
*channel
;
2088 uint64_t key
= msg
.u
.clear_channel
.key
;
2090 channel
= consumer_find_channel(key
);
2092 DBG("Channel %" PRIu64
" not found", key
);
2093 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2095 ret
= lttng_consumer_clear_channel(channel
);
2097 ERR("Clear channel failed key %" PRIu64
, key
);
2101 health_code_update();
2103 ret
= consumer_send_status_msg(sock
, ret_code
);
2105 /* Somehow, the session daemon is not responding anymore. */
2110 case LTTNG_CONSUMER_INIT
:
2112 ret_code
= lttng_consumer_init_command(ctx
,
2113 msg
.u
.init
.sessiond_uuid
);
2114 health_code_update();
2115 ret
= consumer_send_status_msg(sock
, ret_code
);
2117 /* Somehow, the session daemon is not responding anymore. */
2122 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2124 const struct lttng_credentials credentials
= {
2125 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2126 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2128 const bool is_local_trace
=
2129 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2130 const uint64_t relayd_id
=
2131 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2132 const char *chunk_override_name
=
2133 *msg
.u
.create_trace_chunk
.override_name
?
2134 msg
.u
.create_trace_chunk
.override_name
:
2136 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2139 * The session daemon will only provide a chunk directory file
2140 * descriptor for local traces.
2142 if (is_local_trace
) {
2145 /* Acnowledge the reception of the command. */
2146 ret
= consumer_send_status_msg(sock
,
2147 LTTCOMM_CONSUMERD_SUCCESS
);
2149 /* Somehow, the session daemon is not responding anymore. */
2154 * Receive trace chunk domain dirfd.
2156 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2157 if (ret
!= sizeof(chunk_dirfd
)) {
2158 ERR("Failed to receive trace chunk domain directory file descriptor");
2162 DBG("Received trace chunk domain directory fd (%d)",
2164 chunk_directory_handle
= lttng_directory_handle_create_from_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
: NULL
,
2177 msg
.u
.create_trace_chunk
.session_id
,
2178 msg
.u
.create_trace_chunk
.chunk_id
,
2179 (time_t) msg
.u
.create_trace_chunk
2180 .creation_timestamp
,
2181 chunk_override_name
,
2182 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2185 chunk_directory_handle
);
2186 lttng_directory_handle_put(chunk_directory_handle
);
2187 goto end_msg_sessiond
;
2189 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2191 enum lttng_trace_chunk_command_type close_command
=
2192 msg
.u
.close_trace_chunk
.close_command
.value
;
2193 const uint64_t relayd_id
=
2194 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2195 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2196 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2199 ret_code
= lttng_consumer_close_trace_chunk(
2200 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2203 msg
.u
.close_trace_chunk
.session_id
,
2204 msg
.u
.close_trace_chunk
.chunk_id
,
2205 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2206 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2208 NULL
, closed_trace_chunk_path
);
2209 reply
.ret_code
= ret_code
;
2210 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2211 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2212 if (ret
!= sizeof(reply
)) {
2215 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2217 if (ret
!= reply
.path_length
) {
2222 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2224 const uint64_t relayd_id
=
2225 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2227 ret_code
= lttng_consumer_trace_chunk_exists(
2228 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2230 msg
.u
.trace_chunk_exists
.session_id
,
2231 msg
.u
.trace_chunk_exists
.chunk_id
);
2232 goto end_msg_sessiond
;
2234 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2236 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2237 struct lttng_consumer_channel
*channel
=
2238 consumer_find_channel(key
);
2241 pthread_mutex_lock(&channel
->lock
);
2242 ret_code
= lttng_consumer_open_channel_packets(channel
);
2243 pthread_mutex_unlock(&channel
->lock
);
2246 * The channel could have disappeared in per-pid
2249 DBG("Channel %" PRIu64
" not found", key
);
2250 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2253 health_code_update();
2254 goto end_msg_sessiond
;
2262 * Return 1 to indicate success since the 0 value can be a socket
2263 * shutdown during the recv() or send() call.
2270 * The returned value here is not useful since either way we'll return 1 to
2271 * the caller because the session daemon socket management is done
2272 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2274 ret
= consumer_send_status_msg(sock
, ret_code
);
2284 * Free channel here since no one has a reference to it. We don't
2285 * free after that because a stream can store this pointer.
2287 destroy_channel(channel
);
2289 /* We have to send a status channel message indicating an error. */
2290 ret
= consumer_send_status_channel(sock
, NULL
);
2292 /* Stop everything if session daemon can not be notified. */
2299 /* This will issue a consumer stop. */
2305 health_code_update();
2309 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2310 int producer_active
)
2313 assert(stream
->ustream
);
2315 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2319 * Take a snapshot for a specific stream.
2321 * Returns 0 on success, < 0 on error
2323 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2326 assert(stream
->ustream
);
2328 return ustctl_snapshot(stream
->ustream
);
2332 * Sample consumed and produced positions for a specific stream.
2334 * Returns 0 on success, < 0 on error.
2336 int lttng_ustconsumer_sample_snapshot_positions(
2337 struct lttng_consumer_stream
*stream
)
2340 assert(stream
->ustream
);
2342 return ustctl_snapshot_sample_positions(stream
->ustream
);
2346 * Get the produced position
2348 * Returns 0 on success, < 0 on error
2350 int lttng_ustconsumer_get_produced_snapshot(
2351 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2354 assert(stream
->ustream
);
2357 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2361 * Get the consumed position
2363 * Returns 0 on success, < 0 on error
2365 int lttng_ustconsumer_get_consumed_snapshot(
2366 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2369 assert(stream
->ustream
);
2372 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2375 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2379 assert(stream
->ustream
);
2381 ustctl_flush_buffer(stream
->ustream
, producer
);
2384 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2387 assert(stream
->ustream
);
2389 ustctl_clear_buffer(stream
->ustream
);
2392 int lttng_ustconsumer_get_current_timestamp(
2393 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2396 assert(stream
->ustream
);
2399 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2402 int lttng_ustconsumer_get_sequence_number(
2403 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2406 assert(stream
->ustream
);
2409 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2413 * Called when the stream signals the consumer that it has hung up.
2415 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2418 assert(stream
->ustream
);
2420 pthread_mutex_lock(&stream
->lock
);
2421 if (!stream
->quiescent
) {
2422 ustctl_flush_buffer(stream
->ustream
, 0);
2423 stream
->quiescent
= true;
2425 pthread_mutex_unlock(&stream
->lock
);
2426 stream
->hangup_flush_done
= 1;
2429 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2434 assert(chan
->uchan
);
2435 assert(chan
->buffer_credentials
.is_set
);
2437 if (chan
->switch_timer_enabled
== 1) {
2438 consumer_timer_switch_stop(chan
);
2440 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2443 ret
= close(chan
->stream_fds
[i
]);
2447 if (chan
->shm_path
[0]) {
2448 char shm_path
[PATH_MAX
];
2450 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2452 ERR("Cannot get stream shm path");
2454 ret
= run_as_unlink(shm_path
,
2455 chan
->buffer_credentials
.value
.uid
,
2456 chan
->buffer_credentials
.value
.gid
);
2458 PERROR("unlink %s", shm_path
);
2464 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2467 assert(chan
->uchan
);
2468 assert(chan
->buffer_credentials
.is_set
);
2470 consumer_metadata_cache_destroy(chan
);
2471 ustctl_destroy_channel(chan
->uchan
);
2472 /* Try to rmdir all directories under shm_path root. */
2473 if (chan
->root_shm_path
[0]) {
2474 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2475 chan
->buffer_credentials
.value
.uid
,
2476 chan
->buffer_credentials
.value
.gid
,
2477 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2479 free(chan
->stream_fds
);
2482 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2485 assert(stream
->ustream
);
2487 if (stream
->chan
->switch_timer_enabled
== 1) {
2488 consumer_timer_switch_stop(stream
->chan
);
2490 ustctl_destroy_stream(stream
->ustream
);
2493 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2496 assert(stream
->ustream
);
2498 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2501 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2504 assert(stream
->ustream
);
2506 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2510 * Write up to one packet from the metadata cache to the channel.
2512 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2513 * negative value on error.
2516 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2521 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2522 if (stream
->chan
->metadata_cache
->max_offset
==
2523 stream
->ust_metadata_pushed
) {
2525 * In the context of a user space metadata channel, a
2526 * change in version can be detected in two ways:
2527 * 1) During the pre-consume of the `read_subbuffer` loop,
2528 * 2) When populating the metadata ring buffer (i.e. here).
2530 * This function is invoked when there is no metadata
2531 * available in the ring-buffer. If all data was consumed
2532 * up to the size of the metadata cache, there is no metadata
2533 * to insert in the ring-buffer.
2535 * However, the metadata version could still have changed (a
2536 * regeneration without any new data will yield the same cache
2539 * The cache's version is checked for a version change and the
2540 * consumed position is reset if one occurred.
2542 * This check is only necessary for the user space domain as
2543 * it has to manage the cache explicitly. If this reset was not
2544 * performed, no metadata would be consumed (and no reset would
2545 * occur as part of the pre-consume) until the metadata size
2546 * exceeded the cache size.
2548 if (stream
->metadata_version
!=
2549 stream
->chan
->metadata_cache
->version
) {
2550 metadata_stream_reset_cache_consumed_position(stream
);
2551 consumer_stream_metadata_set_version(stream
,
2552 stream
->chan
->metadata_cache
->version
);
2559 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2560 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2561 stream
->chan
->metadata_cache
->max_offset
2562 - stream
->ust_metadata_pushed
);
2563 assert(write_len
!= 0);
2564 if (write_len
< 0) {
2565 ERR("Writing one metadata packet");
2569 stream
->ust_metadata_pushed
+= write_len
;
2571 assert(stream
->chan
->metadata_cache
->max_offset
>=
2572 stream
->ust_metadata_pushed
);
2576 * Switch packet (but don't open the next one) on every commit of
2577 * a metadata packet. Since the subbuffer is fully filled (with padding,
2578 * if needed), the stream is "quiescent" after this commit.
2580 ustctl_flush_buffer(stream
->ustream
, 1);
2581 stream
->quiescent
= true;
2583 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2589 * Sync metadata meaning request them to the session daemon and snapshot to the
2590 * metadata thread can consumer them.
2592 * Metadata stream lock is held here, but we need to release it when
2593 * interacting with sessiond, else we cause a deadlock with live
2594 * awaiting on metadata to be pushed out.
2596 * The RCU read side lock must be held by the caller.
2598 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2599 struct lttng_consumer_local_data
*ctx
,
2600 struct lttng_consumer_stream
*metadata_stream
)
2603 enum sync_metadata_status status
;
2604 struct lttng_consumer_channel
*metadata_channel
;
2607 assert(metadata_stream
);
2609 metadata_channel
= metadata_stream
->chan
;
2610 pthread_mutex_unlock(&metadata_stream
->lock
);
2612 * Request metadata from the sessiond, but don't wait for the flush
2613 * because we locked the metadata thread.
2615 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2616 pthread_mutex_lock(&metadata_stream
->lock
);
2618 status
= SYNC_METADATA_STATUS_ERROR
;
2623 * The metadata stream and channel can be deleted while the
2624 * metadata stream lock was released. The streamed is checked
2625 * for deletion before we use it further.
2627 * Note that it is safe to access a logically-deleted stream since its
2628 * existence is still guaranteed by the RCU read side lock. However,
2629 * it should no longer be used. The close/deletion of the metadata
2630 * channel and stream already guarantees that all metadata has been
2631 * consumed. Therefore, there is nothing left to do in this function.
2633 if (consumer_stream_is_deleted(metadata_stream
)) {
2634 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2635 metadata_stream
->key
);
2636 status
= SYNC_METADATA_STATUS_NO_DATA
;
2640 ret
= commit_one_metadata_packet(metadata_stream
);
2642 status
= SYNC_METADATA_STATUS_ERROR
;
2644 } else if (ret
> 0) {
2645 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2646 } else /* ret == 0 */ {
2647 status
= SYNC_METADATA_STATUS_NO_DATA
;
2651 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2653 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2654 status
= SYNC_METADATA_STATUS_ERROR
;
2663 * Return 0 on success else a negative value.
2665 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2666 struct lttng_consumer_local_data
*ctx
)
2669 struct ustctl_consumer_stream
*ustream
;
2674 ustream
= stream
->ustream
;
2677 * First, we are going to check if there is a new subbuffer available
2678 * before reading the stream wait_fd.
2680 /* Get the next subbuffer */
2681 ret
= ustctl_get_next_subbuf(ustream
);
2683 /* No more data found, flag the stream. */
2684 stream
->has_data
= 0;
2689 ret
= ustctl_put_subbuf(ustream
);
2692 /* This stream still has data. Flag it and wake up the data thread. */
2693 stream
->has_data
= 1;
2695 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2698 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2699 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2704 /* The wake up pipe has been notified. */
2705 ctx
->has_wakeup
= 1;
2713 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2718 * We can consume the 1 byte written into the wait_fd by
2719 * UST. Don't trigger error if we cannot read this one byte
2720 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2722 * This is only done when the stream is monitored by a thread,
2723 * before the flush is done after a hangup and if the stream
2724 * is not flagged with data since there might be nothing to
2725 * consume in the wait fd but still have data available
2726 * flagged by the consumer wake up pipe.
2728 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2732 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2733 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2741 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2742 struct stream_subbuffer
*subbuf
)
2746 ret
= ustctl_get_subbuf_size(
2747 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2752 ret
= ustctl_get_padded_subbuf_size(
2753 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2762 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2763 struct stream_subbuffer
*subbuf
)
2767 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2772 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2778 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2779 struct stream_subbuffer
*subbuf
)
2783 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2788 ret
= ustctl_get_packet_size(
2789 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2791 PERROR("Failed to get sub-buffer packet size");
2795 ret
= ustctl_get_content_size(
2796 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2798 PERROR("Failed to get sub-buffer content size");
2802 ret
= ustctl_get_timestamp_begin(
2803 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2805 PERROR("Failed to get sub-buffer begin timestamp");
2809 ret
= ustctl_get_timestamp_end(
2810 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2812 PERROR("Failed to get sub-buffer end timestamp");
2816 ret
= ustctl_get_events_discarded(
2817 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2819 PERROR("Failed to get sub-buffer events discarded count");
2823 ret
= ustctl_get_sequence_number(stream
->ustream
,
2824 &subbuf
->info
.data
.sequence_number
.value
);
2826 /* May not be supported by older LTTng-modules. */
2827 if (ret
!= -ENOTTY
) {
2828 PERROR("Failed to get sub-buffer sequence number");
2832 subbuf
->info
.data
.sequence_number
.is_set
= true;
2835 ret
= ustctl_get_stream_id(
2836 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2838 PERROR("Failed to get stream id");
2842 ret
= ustctl_get_instance_id(stream
->ustream
,
2843 &subbuf
->info
.data
.stream_instance_id
.value
);
2845 /* May not be supported by older LTTng-modules. */
2846 if (ret
!= -ENOTTY
) {
2847 PERROR("Failed to get stream instance id");
2851 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2857 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2858 struct stream_subbuffer
*subbuffer
)
2863 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2869 ret
= get_current_subbuf_addr(stream
, &addr
);
2874 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2875 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2876 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2881 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2882 struct stream_subbuffer
*subbuffer
)
2886 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2891 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2899 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2900 struct stream_subbuffer
*subbuffer
)
2907 unsigned long consumed_pos
, produced_pos
;
2910 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2912 got_subbuffer
= true;
2914 got_subbuffer
= false;
2915 if (ret
!= -EAGAIN
) {
2922 * Determine if the cache is empty and ensure that a sub-buffer
2923 * is made available if the cache is not empty.
2925 if (!got_subbuffer
) {
2926 ret
= commit_one_metadata_packet(stream
);
2927 if (ret
< 0 && ret
!= -ENOBUFS
) {
2929 } else if (ret
== 0) {
2930 /* Not an error, the cache is empty. */
2935 cache_empty
= false;
2938 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2939 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2940 stream
->ust_metadata_pushed
;
2941 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2943 } while (!got_subbuffer
);
2945 /* Populate sub-buffer infos and view. */
2946 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2951 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2954 * -EAGAIN is not expected since we got a sub-buffer and haven't
2955 * pushed the consumption position yet (on put_next).
2957 PERROR("Failed to take a snapshot of metadata buffer positions");
2961 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2963 PERROR("Failed to get metadata consumed position");
2967 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2969 PERROR("Failed to get metadata produced position");
2973 /* Last sub-buffer of the ring buffer ? */
2974 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2977 * The sessiond registry lock ensures that coherent units of metadata
2978 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2979 * acquired, the cache is empty, and it is the only available sub-buffer
2980 * available, it is safe to assume that it is "coherent".
2982 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2984 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2989 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2990 struct stream_subbuffer
*subbuffer
)
2992 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2998 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2999 struct lttng_consumer_local_data
*ctx
)
3001 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3004 static int lttng_ustconsumer_set_stream_ops(
3005 struct lttng_consumer_stream
*stream
)
3009 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3010 if (stream
->metadata_flag
) {
3011 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3012 get_next_subbuffer_metadata
;
3013 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3014 extract_metadata_subbuffer_info
;
3015 stream
->read_subbuffer_ops
.reset_metadata
=
3016 metadata_stream_reset_cache_consumed_position
;
3017 if (stream
->chan
->is_live
) {
3018 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3019 ret
= consumer_stream_enable_metadata_bucketization(
3026 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3028 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3029 extract_data_subbuffer_info
;
3030 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3031 if (stream
->chan
->is_live
) {
3032 stream
->read_subbuffer_ops
.send_live_beacon
=
3033 consumer_flush_ust_index
;
3037 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3043 * Called when a stream is created.
3045 * Return 0 on success or else a negative value.
3047 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3054 * Don't create anything if this is set for streaming or if there is
3055 * no current trace chunk on the parent channel.
3057 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3058 stream
->chan
->trace_chunk
) {
3059 ret
= consumer_stream_create_output_files(stream
, true);
3065 lttng_ustconsumer_set_stream_ops(stream
);
3073 * Check if data is still being extracted from the buffers for a specific
3074 * stream. Consumer data lock MUST be acquired before calling this function
3075 * and the stream lock.
3077 * Return 1 if the traced data are still getting read else 0 meaning that the
3078 * data is available for trace viewer reading.
3080 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3085 assert(stream
->ustream
);
3086 ASSERT_LOCKED(stream
->lock
);
3088 DBG("UST consumer checking data pending");
3090 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3095 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3096 uint64_t contiguous
, pushed
;
3098 /* Ease our life a bit. */
3099 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3100 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3101 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3102 pushed
= stream
->ust_metadata_pushed
;
3105 * We can simply check whether all contiguously available data
3106 * has been pushed to the ring buffer, since the push operation
3107 * is performed within get_next_subbuf(), and because both
3108 * get_next_subbuf() and put_next_subbuf() are issued atomically
3109 * thanks to the stream lock within
3110 * lttng_ustconsumer_read_subbuffer(). This basically means that
3111 * whetnever ust_metadata_pushed is incremented, the associated
3112 * metadata has been consumed from the metadata stream.
3114 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3115 contiguous
, pushed
);
3116 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3117 if ((contiguous
!= pushed
) ||
3118 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3119 ret
= 1; /* Data is pending */
3123 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3126 * There is still data so let's put back this
3129 ret
= ustctl_put_subbuf(stream
->ustream
);
3131 ret
= 1; /* Data is pending */
3136 /* Data is NOT pending so ready to be read. */
3144 * Stop a given metadata channel timer if enabled and close the wait fd which
3145 * is the poll pipe of the metadata stream.
3147 * This MUST be called with the metadata channel lock acquired.
3149 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3154 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3156 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3158 if (metadata
->switch_timer_enabled
== 1) {
3159 consumer_timer_switch_stop(metadata
);
3162 if (!metadata
->metadata_stream
) {
3167 * Closing write side so the thread monitoring the stream wakes up if any
3168 * and clean the metadata stream.
3170 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3171 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3173 PERROR("closing metadata pipe write side");
3175 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3183 * Close every metadata stream wait fd of the metadata hash table. This
3184 * function MUST be used very carefully so not to run into a race between the
3185 * metadata thread handling streams and this function closing their wait fd.
3187 * For UST, this is used when the session daemon hangs up. Its the metadata
3188 * producer so calling this is safe because we are assured that no state change
3189 * can occur in the metadata thread for the streams in the hash table.
3191 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3193 struct lttng_ht_iter iter
;
3194 struct lttng_consumer_stream
*stream
;
3196 assert(metadata_ht
);
3197 assert(metadata_ht
->ht
);
3199 DBG("UST consumer closing all metadata streams");
3202 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3205 health_code_update();
3207 pthread_mutex_lock(&stream
->chan
->lock
);
3208 lttng_ustconsumer_close_metadata(stream
->chan
);
3209 pthread_mutex_unlock(&stream
->chan
->lock
);
3215 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3219 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3221 ERR("Unable to close wakeup fd");
3226 * Please refer to consumer-timer.c before adding any lock within this
3227 * function or any of its callees. Timers have a very strict locking
3228 * semantic with respect to teardown. Failure to respect this semantic
3229 * introduces deadlocks.
3231 * DON'T hold the metadata lock when calling this function, else this
3232 * can cause deadlock involving consumer awaiting for metadata to be
3233 * pushed out due to concurrent interaction with the session daemon.
3235 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3236 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3238 struct lttcomm_metadata_request_msg request
;
3239 struct lttcomm_consumer_msg msg
;
3240 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3241 uint64_t len
, key
, offset
, version
;
3245 assert(channel
->metadata_cache
);
3247 memset(&request
, 0, sizeof(request
));
3249 /* send the metadata request to sessiond */
3250 switch (consumer_data
.type
) {
3251 case LTTNG_CONSUMER64_UST
:
3252 request
.bits_per_long
= 64;
3254 case LTTNG_CONSUMER32_UST
:
3255 request
.bits_per_long
= 32;
3258 request
.bits_per_long
= 0;
3262 request
.session_id
= channel
->session_id
;
3263 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3265 * Request the application UID here so the metadata of that application can
3266 * be sent back. The channel UID corresponds to the user UID of the session
3267 * used for the rights on the stream file(s).
3269 request
.uid
= channel
->ust_app_uid
;
3270 request
.key
= channel
->key
;
3272 DBG("Sending metadata request to sessiond, session id %" PRIu64
3273 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3274 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3277 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3279 health_code_update();
3281 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3284 ERR("Asking metadata to sessiond");
3288 health_code_update();
3290 /* Receive the metadata from sessiond */
3291 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3293 if (ret
!= sizeof(msg
)) {
3294 DBG("Consumer received unexpected message size %d (expects %zu)",
3296 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3298 * The ret value might 0 meaning an orderly shutdown but this is ok
3299 * since the caller handles this.
3304 health_code_update();
3306 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3307 /* No registry found */
3308 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3312 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3313 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3318 len
= msg
.u
.push_metadata
.len
;
3319 key
= msg
.u
.push_metadata
.key
;
3320 offset
= msg
.u
.push_metadata
.target_offset
;
3321 version
= msg
.u
.push_metadata
.version
;
3323 assert(key
== channel
->key
);
3325 DBG("No new metadata to receive for key %" PRIu64
, key
);
3328 health_code_update();
3330 /* Tell session daemon we are ready to receive the metadata. */
3331 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3332 LTTCOMM_CONSUMERD_SUCCESS
);
3333 if (ret
< 0 || len
== 0) {
3335 * Somehow, the session daemon is not responding anymore or there is
3336 * nothing to receive.
3341 health_code_update();
3343 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3344 key
, offset
, len
, version
, channel
, timer
, wait
);
3347 * Only send the status msg if the sessiond is alive meaning a positive
3350 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3355 health_code_update();
3357 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3362 * Return the ustctl call for the get stream id.
3364 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3365 uint64_t *stream_id
)
3370 return ustctl_get_stream_id(stream
->ustream
, stream_id
);