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>
27 #include <bin/lttng-consumerd/health-consumerd.h>
28 #include <common/common.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/relayd/relayd.h>
31 #include <common/compat/fcntl.h>
32 #include <common/compat/endian.h>
33 #include <common/consumer/consumer-metadata-cache.h>
34 #include <common/consumer/consumer-stream.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/utils.h>
37 #include <common/index/index.h>
39 #include "ust-consumer.h"
41 #define INT_MAX_STR_LEN 12 /* includes \0 */
43 extern struct lttng_consumer_global_data consumer_data
;
44 extern int consumer_poll_timeout
;
47 * Free channel object and all streams associated with it. This MUST be used
48 * only and only if the channel has _NEVER_ been added to the global channel
51 static void destroy_channel(struct lttng_consumer_channel
*channel
)
53 struct lttng_consumer_stream
*stream
, *stmp
;
57 DBG("UST consumer cleaning stream list");
59 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
64 cds_list_del(&stream
->send_node
);
65 ustctl_destroy_stream(stream
->ustream
);
66 lttng_trace_chunk_put(stream
->trace_chunk
);
71 * If a channel is available meaning that was created before the streams
75 lttng_ustconsumer_del_channel(channel
);
76 lttng_ustconsumer_free_channel(channel
);
82 * Add channel to internal consumer state.
84 * Returns 0 on success or else a negative value.
86 static int add_channel(struct lttng_consumer_channel
*channel
,
87 struct lttng_consumer_local_data
*ctx
)
94 if (ctx
->on_recv_channel
!= NULL
) {
95 ret
= ctx
->on_recv_channel(channel
);
97 ret
= consumer_add_channel(channel
, ctx
);
99 /* Most likely an ENOMEM. */
100 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
104 ret
= consumer_add_channel(channel
, ctx
);
107 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
114 * Allocate and return a consumer channel object.
116 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
117 const uint64_t *chunk_id
, const char *pathname
, const char *name
,
118 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
119 uint64_t tracefile_size
, uint64_t tracefile_count
,
120 uint64_t session_id_per_pid
, unsigned int monitor
,
121 unsigned int live_timer_interval
,
122 const char *root_shm_path
, const char *shm_path
)
127 return consumer_allocate_channel(key
, session_id
, chunk_id
, pathname
,
128 name
, relayd_id
, output
, tracefile_size
,
129 tracefile_count
, session_id_per_pid
, monitor
,
130 live_timer_interval
, root_shm_path
, shm_path
);
134 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
135 * error value if applicable is set in it else it is kept untouched.
137 * Return NULL on error else the newly allocated stream object.
139 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
140 struct lttng_consumer_channel
*channel
,
141 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
144 struct lttng_consumer_stream
*stream
= NULL
;
149 stream
= consumer_allocate_stream(channel
->key
,
154 channel
->trace_chunk
,
159 if (stream
== NULL
) {
163 * We could not find the channel. Can happen if cpu hotplug
164 * happens while tearing down.
166 DBG3("Could not find channel");
171 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
177 consumer_stream_update_channel_attributes(stream
, channel
);
178 stream
->chan
= channel
;
182 *_alloc_ret
= alloc_ret
;
188 * Send the given stream pointer to the corresponding thread.
190 * Returns 0 on success else a negative value.
192 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
193 struct lttng_consumer_local_data
*ctx
)
196 struct lttng_pipe
*stream_pipe
;
198 /* Get the right pipe where the stream will be sent. */
199 if (stream
->metadata_flag
) {
200 consumer_add_metadata_stream(stream
);
201 stream_pipe
= ctx
->consumer_metadata_pipe
;
203 consumer_add_data_stream(stream
);
204 stream_pipe
= ctx
->consumer_data_pipe
;
208 * From this point on, the stream's ownership has been moved away from
209 * the channel and it becomes globally visible. Hence, remove it from
210 * the local stream list to prevent the stream from being both local and
213 stream
->globally_visible
= 1;
214 cds_list_del(&stream
->send_node
);
216 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
218 ERR("Consumer write %s stream to pipe %d",
219 stream
->metadata_flag
? "metadata" : "data",
220 lttng_pipe_get_writefd(stream_pipe
));
221 if (stream
->metadata_flag
) {
222 consumer_del_stream_for_metadata(stream
);
224 consumer_del_stream_for_data(stream
);
234 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
236 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
239 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
240 stream_shm_path
[PATH_MAX
- 1] = '\0';
241 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
246 strncat(stream_shm_path
, cpu_nr
,
247 PATH_MAX
- strlen(stream_shm_path
) - 1);
254 * Create streams for the given channel using liblttng-ust-ctl.
255 * The channel lock must be acquired by the caller.
257 * Return 0 on success else a negative value.
259 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
260 struct lttng_consumer_local_data
*ctx
)
263 struct ustctl_consumer_stream
*ustream
;
264 struct lttng_consumer_stream
*stream
;
265 pthread_mutex_t
*current_stream_lock
= NULL
;
271 * While a stream is available from ustctl. When NULL is returned, we've
272 * reached the end of the possible stream for the channel.
274 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
276 int ust_metadata_pipe
[2];
278 health_code_update();
280 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
281 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
283 ERR("Create ust metadata poll pipe");
286 wait_fd
= ust_metadata_pipe
[0];
288 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
291 /* Allocate consumer stream object. */
292 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
296 stream
->ustream
= ustream
;
298 * Store it so we can save multiple function calls afterwards since
299 * this value is used heavily in the stream threads. This is UST
300 * specific so this is why it's done after allocation.
302 stream
->wait_fd
= wait_fd
;
305 * Increment channel refcount since the channel reference has now been
306 * assigned in the allocation process above.
308 if (stream
->chan
->monitor
) {
309 uatomic_inc(&stream
->chan
->refcount
);
312 pthread_mutex_lock(&stream
->lock
);
313 current_stream_lock
= &stream
->lock
;
315 * Order is important this is why a list is used. On error, the caller
316 * should clean this list.
318 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
320 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
321 &stream
->max_sb_size
);
323 ERR("ustctl_get_max_subbuf_size failed for stream %s",
328 /* Do actions once stream has been received. */
329 if (ctx
->on_recv_stream
) {
330 ret
= ctx
->on_recv_stream(stream
);
336 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
337 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
339 /* Set next CPU stream. */
340 channel
->streams
.count
= ++cpu
;
342 /* Keep stream reference when creating metadata. */
343 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
344 channel
->metadata_stream
= stream
;
345 if (channel
->monitor
) {
346 /* Set metadata poll pipe if we created one */
347 memcpy(stream
->ust_metadata_poll_pipe
,
349 sizeof(ust_metadata_pipe
));
352 pthread_mutex_unlock(&stream
->lock
);
353 current_stream_lock
= NULL
;
360 if (current_stream_lock
) {
361 pthread_mutex_unlock(current_stream_lock
);
367 * create_posix_shm is never called concurrently within a process.
370 int create_posix_shm(void)
372 char tmp_name
[NAME_MAX
];
375 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
381 * Allocate shm, and immediately unlink its shm oject, keeping
382 * only the file descriptor as a reference to the object.
383 * We specifically do _not_ use the / at the beginning of the
384 * pathname so that some OS implementations can keep it local to
385 * the process (POSIX leaves this implementation-defined).
387 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
392 ret
= shm_unlink(tmp_name
);
393 if (ret
< 0 && errno
!= ENOENT
) {
394 PERROR("shm_unlink");
395 goto error_shm_release
;
408 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
409 const struct lttng_credentials
*session_credentials
)
411 char shm_path
[PATH_MAX
];
414 if (!channel
->shm_path
[0]) {
415 return create_posix_shm();
417 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
421 return run_as_open(shm_path
,
422 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
423 session_credentials
->uid
, session_credentials
->gid
);
430 * Create an UST channel with the given attributes and send it to the session
431 * daemon using the ust ctl API.
433 * Return 0 on success or else a negative value.
435 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
436 struct ustctl_consumer_channel_attr
*attr
,
437 struct ustctl_consumer_channel
**ust_chanp
)
439 int ret
, nr_stream_fds
, i
, j
;
441 struct ustctl_consumer_channel
*ust_channel
;
446 assert(channel
->buffer_credentials
.is_set
);
448 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
449 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
450 "switch_timer_interval: %u, read_timer_interval: %u, "
451 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
452 attr
->num_subbuf
, attr
->switch_timer_interval
,
453 attr
->read_timer_interval
, attr
->output
, attr
->type
);
455 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
458 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
459 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
464 for (i
= 0; i
< nr_stream_fds
; i
++) {
465 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
466 &channel
->buffer_credentials
.value
);
467 if (stream_fds
[i
] < 0) {
472 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
477 channel
->nr_stream_fds
= nr_stream_fds
;
478 channel
->stream_fds
= stream_fds
;
479 *ust_chanp
= ust_channel
;
485 for (j
= i
- 1; j
>= 0; j
--) {
488 closeret
= close(stream_fds
[j
]);
492 if (channel
->shm_path
[0]) {
493 char shm_path
[PATH_MAX
];
495 closeret
= get_stream_shm_path(shm_path
,
496 channel
->shm_path
, j
);
498 ERR("Cannot get stream shm path");
500 closeret
= run_as_unlink(shm_path
,
501 channel
->buffer_credentials
.value
.uid
,
502 channel
->buffer_credentials
.value
.gid
);
504 PERROR("unlink %s", shm_path
);
508 /* Try to rmdir all directories under shm_path root. */
509 if (channel
->root_shm_path
[0]) {
510 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
511 channel
->buffer_credentials
.value
.uid
,
512 channel
->buffer_credentials
.value
.gid
,
513 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
521 * Send a single given stream to the session daemon using the sock.
523 * Return 0 on success else a negative value.
525 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
532 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
534 /* Send stream to session daemon. */
535 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
545 * Send channel to sessiond and relayd if applicable.
547 * Return 0 on success or else a negative value.
549 static int send_channel_to_sessiond_and_relayd(int sock
,
550 struct lttng_consumer_channel
*channel
,
551 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
553 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
554 struct lttng_consumer_stream
*stream
;
555 uint64_t net_seq_idx
= -1ULL;
561 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
563 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
564 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
566 health_code_update();
568 /* Try to send the stream to the relayd if one is available. */
569 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
570 stream
->key
, channel
->name
);
571 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
574 * Flag that the relayd was the problem here probably due to a
575 * communicaton error on the socket.
580 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
582 if (net_seq_idx
== -1ULL) {
583 net_seq_idx
= stream
->net_seq_idx
;
588 /* Inform sessiond that we are about to send channel and streams. */
589 ret
= consumer_send_status_msg(sock
, ret_code
);
590 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
592 * Either the session daemon is not responding or the relayd died so we
598 /* Send channel to sessiond. */
599 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
604 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
609 /* The channel was sent successfully to the sessiond at this point. */
610 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
612 health_code_update();
614 /* Send stream to session daemon. */
615 ret
= send_sessiond_stream(sock
, stream
);
621 /* Tell sessiond there is no more stream. */
622 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
627 DBG("UST consumer NULL stream sent to sessiond");
632 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
639 * Creates a channel and streams and add the channel it to the channel internal
640 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
643 * Return 0 on success or else, a negative value is returned and the channel
644 * MUST be destroyed by consumer_del_channel().
646 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
647 struct lttng_consumer_channel
*channel
,
648 struct ustctl_consumer_channel_attr
*attr
)
657 * This value is still used by the kernel consumer since for the kernel,
658 * the stream ownership is not IN the consumer so we need to have the
659 * number of left stream that needs to be initialized so we can know when
660 * to delete the channel (see consumer.c).
662 * As for the user space tracer now, the consumer creates and sends the
663 * stream to the session daemon which only sends them to the application
664 * once every stream of a channel is received making this value useless
665 * because we they will be added to the poll thread before the application
666 * receives them. This ensures that a stream can not hang up during
667 * initilization of a channel.
669 channel
->nb_init_stream_left
= 0;
671 /* The reply msg status is handled in the following call. */
672 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
677 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
680 * For the snapshots (no monitor), we create the metadata streams
681 * on demand, not during the channel creation.
683 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
688 /* Open all streams for this channel. */
689 pthread_mutex_lock(&channel
->lock
);
690 ret
= create_ust_streams(channel
, ctx
);
691 pthread_mutex_unlock(&channel
->lock
);
701 * Send all stream of a channel to the right thread handling it.
703 * On error, return a negative value else 0 on success.
705 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
706 struct lttng_consumer_local_data
*ctx
)
709 struct lttng_consumer_stream
*stream
, *stmp
;
714 /* Send streams to the corresponding thread. */
715 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
718 health_code_update();
720 /* Sending the stream to the thread. */
721 ret
= send_stream_to_thread(stream
, ctx
);
724 * If we are unable to send the stream to the thread, there is
725 * a big problem so just stop everything.
736 * Flush channel's streams using the given key to retrieve the channel.
738 * Return 0 on success else an LTTng error code.
740 static int flush_channel(uint64_t chan_key
)
743 struct lttng_consumer_channel
*channel
;
744 struct lttng_consumer_stream
*stream
;
746 struct lttng_ht_iter iter
;
748 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
751 channel
= consumer_find_channel(chan_key
);
753 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
754 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
758 ht
= consumer_data
.stream_per_chan_id_ht
;
760 /* For each stream of the channel id, flush it. */
761 cds_lfht_for_each_entry_duplicate(ht
->ht
,
762 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
763 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
765 health_code_update();
767 pthread_mutex_lock(&stream
->lock
);
770 * Protect against concurrent teardown of a stream.
772 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
776 if (!stream
->quiescent
) {
777 ustctl_flush_buffer(stream
->ustream
, 0);
778 stream
->quiescent
= true;
781 pthread_mutex_unlock(&stream
->lock
);
789 * Clear quiescent state from channel's streams using the given key to
790 * retrieve the channel.
792 * Return 0 on success else an LTTng error code.
794 static int clear_quiescent_channel(uint64_t chan_key
)
797 struct lttng_consumer_channel
*channel
;
798 struct lttng_consumer_stream
*stream
;
800 struct lttng_ht_iter iter
;
802 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
805 channel
= consumer_find_channel(chan_key
);
807 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
808 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
812 ht
= consumer_data
.stream_per_chan_id_ht
;
814 /* For each stream of the channel id, clear quiescent state. */
815 cds_lfht_for_each_entry_duplicate(ht
->ht
,
816 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
817 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
819 health_code_update();
821 pthread_mutex_lock(&stream
->lock
);
822 stream
->quiescent
= false;
823 pthread_mutex_unlock(&stream
->lock
);
831 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
833 * Return 0 on success else an LTTng error code.
835 static int close_metadata(uint64_t chan_key
)
838 struct lttng_consumer_channel
*channel
;
839 unsigned int channel_monitor
;
841 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
843 channel
= consumer_find_channel(chan_key
);
846 * This is possible if the metadata thread has issue a delete because
847 * the endpoint point of the stream hung up. There is no way the
848 * session daemon can know about it thus use a DBG instead of an actual
851 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
852 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
856 pthread_mutex_lock(&consumer_data
.lock
);
857 pthread_mutex_lock(&channel
->lock
);
858 channel_monitor
= channel
->monitor
;
859 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
863 lttng_ustconsumer_close_metadata(channel
);
864 pthread_mutex_unlock(&channel
->lock
);
865 pthread_mutex_unlock(&consumer_data
.lock
);
868 * The ownership of a metadata channel depends on the type of
869 * session to which it belongs. In effect, the monitor flag is checked
870 * to determine if this metadata channel is in "snapshot" mode or not.
872 * In the non-snapshot case, the metadata channel is created along with
873 * a single stream which will remain present until the metadata channel
874 * is destroyed (on the destruction of its session). In this case, the
875 * metadata stream in "monitored" by the metadata poll thread and holds
876 * the ownership of its channel.
878 * Closing the metadata will cause the metadata stream's "metadata poll
879 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
880 * thread which will teardown the metadata stream which, in return,
881 * deletes the metadata channel.
883 * In the snapshot case, the metadata stream is created and destroyed
884 * on every snapshot record. Since the channel doesn't have an owner
885 * other than the session daemon, it is safe to destroy it immediately
886 * on reception of the CLOSE_METADATA command.
888 if (!channel_monitor
) {
890 * The channel and consumer_data locks must be
891 * released before this call since consumer_del_channel
892 * re-acquires the channel and consumer_data locks to teardown
893 * the channel and queue its reclamation by the "call_rcu"
896 consumer_del_channel(channel
);
901 pthread_mutex_unlock(&channel
->lock
);
902 pthread_mutex_unlock(&consumer_data
.lock
);
908 * RCU read side lock MUST be acquired before calling this function.
910 * Return 0 on success else an LTTng error code.
912 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
915 struct lttng_consumer_channel
*metadata
;
917 DBG("UST consumer setup metadata key %" PRIu64
, key
);
919 metadata
= consumer_find_channel(key
);
921 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
922 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
927 * In no monitor mode, the metadata channel has no stream(s) so skip the
928 * ownership transfer to the metadata thread.
930 if (!metadata
->monitor
) {
931 DBG("Metadata channel in no monitor");
937 * Send metadata stream to relayd if one available. Availability is
938 * known if the stream is still in the list of the channel.
940 if (cds_list_empty(&metadata
->streams
.head
)) {
941 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
942 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
943 goto error_no_stream
;
946 /* Send metadata stream to relayd if needed. */
947 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
948 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
951 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
954 ret
= consumer_send_relayd_streams_sent(
955 metadata
->metadata_stream
->net_seq_idx
);
957 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
963 * Ownership of metadata stream is passed along. Freeing is handled by
966 ret
= send_streams_to_thread(metadata
, ctx
);
969 * If we are unable to send the stream to the thread, there is
970 * a big problem so just stop everything.
972 ret
= LTTCOMM_CONSUMERD_FATAL
;
973 goto send_streams_error
;
975 /* List MUST be empty after or else it could be reused. */
976 assert(cds_list_empty(&metadata
->streams
.head
));
983 * Delete metadata channel on error. At this point, the metadata stream can
984 * NOT be monitored by the metadata thread thus having the guarantee that
985 * the stream is still in the local stream list of the channel. This call
986 * will make sure to clean that list.
988 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
989 cds_list_del(&metadata
->metadata_stream
->send_node
);
990 metadata
->metadata_stream
= NULL
;
998 * Snapshot the whole metadata.
999 * RCU read-side lock must be held by the caller.
1001 * Returns 0 on success, < 0 on error
1003 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
1004 uint64_t key
, char *path
, uint64_t relayd_id
,
1005 struct lttng_consumer_local_data
*ctx
)
1008 struct lttng_consumer_stream
*metadata_stream
;
1013 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1018 assert(!metadata_channel
->monitor
);
1020 health_code_update();
1023 * Ask the sessiond if we have new metadata waiting and update the
1024 * consumer metadata cache.
1026 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1031 health_code_update();
1034 * The metadata stream is NOT created in no monitor mode when the channel
1035 * is created on a sessiond ask channel command.
1037 ret
= create_ust_streams(metadata_channel
, ctx
);
1042 metadata_stream
= metadata_channel
->metadata_stream
;
1043 assert(metadata_stream
);
1045 pthread_mutex_lock(&metadata_stream
->lock
);
1046 if (relayd_id
!= (uint64_t) -1ULL) {
1047 metadata_stream
->net_seq_idx
= relayd_id
;
1048 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1050 ret
= consumer_stream_create_output_files(metadata_stream
,
1053 pthread_mutex_unlock(&metadata_stream
->lock
);
1059 health_code_update();
1061 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1069 * Clean up the stream completly because the next snapshot will use a new
1072 consumer_stream_destroy(metadata_stream
, NULL
);
1073 cds_list_del(&metadata_stream
->send_node
);
1074 metadata_channel
->metadata_stream
= NULL
;
1082 * Take a snapshot of all the stream of a channel.
1083 * RCU read-side lock and the channel lock must be held by the caller.
1085 * Returns 0 on success, < 0 on error
1087 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1088 uint64_t key
, char *path
, uint64_t relayd_id
,
1089 uint64_t nb_packets_per_stream
,
1090 struct lttng_consumer_local_data
*ctx
)
1093 unsigned use_relayd
= 0;
1094 unsigned long consumed_pos
, produced_pos
;
1095 struct lttng_consumer_stream
*stream
;
1102 if (relayd_id
!= (uint64_t) -1ULL) {
1106 assert(!channel
->monitor
);
1107 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1109 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1110 health_code_update();
1112 /* Lock stream because we are about to change its state. */
1113 pthread_mutex_lock(&stream
->lock
);
1114 assert(channel
->trace_chunk
);
1115 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1117 * Can't happen barring an internal error as the channel
1118 * holds a reference to the trace chunk.
1120 ERR("Failed to acquire reference to channel's trace chunk");
1124 assert(!stream
->trace_chunk
);
1125 stream
->trace_chunk
= channel
->trace_chunk
;
1127 stream
->net_seq_idx
= relayd_id
;
1130 ret
= consumer_send_relayd_stream(stream
, path
);
1135 ret
= consumer_stream_create_output_files(stream
,
1140 DBG("UST consumer snapshot stream (%" PRIu64
")",
1145 * If tracing is active, we want to perform a "full" buffer flush.
1146 * Else, if quiescent, it has already been done by the prior stop.
1148 if (!stream
->quiescent
) {
1149 ustctl_flush_buffer(stream
->ustream
, 0);
1152 ret
= lttng_ustconsumer_take_snapshot(stream
);
1154 ERR("Taking UST snapshot");
1158 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1160 ERR("Produced UST snapshot position");
1164 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1166 ERR("Consumerd UST snapshot position");
1171 * The original value is sent back if max stream size is larger than
1172 * the possible size of the snapshot. Also, we assume that the session
1173 * daemon should never send a maximum stream size that is lower than
1176 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1177 produced_pos
, nb_packets_per_stream
,
1178 stream
->max_sb_size
);
1180 while ((long) (consumed_pos
- produced_pos
) < 0) {
1182 unsigned long len
, padded_len
;
1184 health_code_update();
1186 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1188 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1190 if (ret
!= -EAGAIN
) {
1191 PERROR("ustctl_get_subbuf snapshot");
1192 goto error_close_stream
;
1194 DBG("UST consumer get subbuf failed. Skipping it.");
1195 consumed_pos
+= stream
->max_sb_size
;
1196 stream
->chan
->lost_packets
++;
1200 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1202 ERR("Snapshot ustctl_get_subbuf_size");
1203 goto error_put_subbuf
;
1206 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1208 ERR("Snapshot ustctl_get_padded_subbuf_size");
1209 goto error_put_subbuf
;
1212 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
1213 padded_len
- len
, NULL
);
1215 if (read_len
!= len
) {
1217 goto error_put_subbuf
;
1220 if (read_len
!= padded_len
) {
1222 goto error_put_subbuf
;
1226 ret
= ustctl_put_subbuf(stream
->ustream
);
1228 ERR("Snapshot ustctl_put_subbuf");
1229 goto error_close_stream
;
1231 consumed_pos
+= stream
->max_sb_size
;
1234 /* Simply close the stream so we can use it on the next snapshot. */
1235 consumer_stream_close(stream
);
1236 pthread_mutex_unlock(&stream
->lock
);
1243 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1244 ERR("Snapshot ustctl_put_subbuf");
1247 consumer_stream_close(stream
);
1249 pthread_mutex_unlock(&stream
->lock
);
1255 * Receive the metadata updates from the sessiond. Supports receiving
1256 * overlapping metadata, but is needs to always belong to a contiguous
1257 * range starting from 0.
1258 * Be careful about the locks held when calling this function: it needs
1259 * the metadata cache flush to concurrently progress in order to
1262 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1263 uint64_t len
, uint64_t version
,
1264 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1266 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1269 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1271 metadata_str
= zmalloc(len
* sizeof(char));
1272 if (!metadata_str
) {
1273 PERROR("zmalloc metadata string");
1274 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1278 health_code_update();
1280 /* Receive metadata string. */
1281 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1283 /* Session daemon is dead so return gracefully. */
1288 health_code_update();
1290 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1291 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1294 /* Unable to handle metadata. Notify session daemon. */
1295 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1297 * Skip metadata flush on write error since the offset and len might
1298 * not have been updated which could create an infinite loop below when
1299 * waiting for the metadata cache to be flushed.
1301 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1304 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1309 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1310 DBG("Waiting for metadata to be flushed");
1312 health_code_update();
1314 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1324 * Receive command from session daemon and process it.
1326 * Return 1 on success else a negative value or 0.
1328 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1329 int sock
, struct pollfd
*consumer_sockpoll
)
1332 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1333 struct lttcomm_consumer_msg msg
;
1334 struct lttng_consumer_channel
*channel
= NULL
;
1336 health_code_update();
1338 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1339 if (ret
!= sizeof(msg
)) {
1340 DBG("Consumer received unexpected message size %zd (expects %zu)",
1343 * The ret value might 0 meaning an orderly shutdown but this is ok
1344 * since the caller handles this.
1347 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1353 health_code_update();
1356 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1358 health_code_update();
1360 /* relayd needs RCU read-side lock */
1363 switch (msg
.cmd_type
) {
1364 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1366 /* Session daemon status message are handled in the following call. */
1367 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1368 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1369 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1370 msg
.u
.relayd_sock
.relayd_session_id
);
1373 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1375 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1376 struct consumer_relayd_sock_pair
*relayd
;
1378 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1380 /* Get relayd reference if exists. */
1381 relayd
= consumer_find_relayd(index
);
1382 if (relayd
== NULL
) {
1383 DBG("Unable to find relayd %" PRIu64
, index
);
1384 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1388 * Each relayd socket pair has a refcount of stream attached to it
1389 * which tells if the relayd is still active or not depending on the
1392 * This will set the destroy flag of the relayd object and destroy it
1393 * if the refcount reaches zero when called.
1395 * The destroy can happen either here or when a stream fd hangs up.
1398 consumer_flag_relayd_for_destroy(relayd
);
1401 goto end_msg_sessiond
;
1403 case LTTNG_CONSUMER_UPDATE_STREAM
:
1408 case LTTNG_CONSUMER_DATA_PENDING
:
1410 int ret
, is_data_pending
;
1411 uint64_t id
= msg
.u
.data_pending
.session_id
;
1413 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1415 is_data_pending
= consumer_data_pending(id
);
1417 /* Send back returned value to session daemon */
1418 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1419 sizeof(is_data_pending
));
1421 DBG("Error when sending the data pending ret code: %d", ret
);
1426 * No need to send back a status message since the data pending
1427 * returned value is the response.
1431 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1434 struct ustctl_consumer_channel_attr attr
;
1435 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1436 const struct lttng_credentials buffer_credentials
= {
1437 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1438 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1441 /* Create a plain object and reserve a channel key. */
1442 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1443 msg
.u
.ask_channel
.chunk_id
.is_set
?
1445 msg
.u
.ask_channel
.pathname
,
1446 msg
.u
.ask_channel
.name
,
1447 msg
.u
.ask_channel
.relayd_id
,
1448 msg
.u
.ask_channel
.key
,
1449 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1450 msg
.u
.ask_channel
.tracefile_size
,
1451 msg
.u
.ask_channel
.tracefile_count
,
1452 msg
.u
.ask_channel
.session_id_per_pid
,
1453 msg
.u
.ask_channel
.monitor
,
1454 msg
.u
.ask_channel
.live_timer_interval
,
1455 msg
.u
.ask_channel
.root_shm_path
,
1456 msg
.u
.ask_channel
.shm_path
);
1458 goto end_channel_error
;
1461 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1462 buffer_credentials
);
1465 * Assign UST application UID to the channel. This value is ignored for
1466 * per PID buffers. This is specific to UST thus setting this after the
1469 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1471 /* Build channel attributes from received message. */
1472 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1473 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1474 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1475 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1476 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1477 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1478 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1479 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1481 /* Match channel buffer type to the UST abi. */
1482 switch (msg
.u
.ask_channel
.output
) {
1483 case LTTNG_EVENT_MMAP
:
1485 attr
.output
= LTTNG_UST_MMAP
;
1489 /* Translate and save channel type. */
1490 switch (msg
.u
.ask_channel
.type
) {
1491 case LTTNG_UST_CHAN_PER_CPU
:
1492 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1493 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1495 * Set refcount to 1 for owner. Below, we will
1496 * pass ownership to the
1497 * consumer_thread_channel_poll() thread.
1499 channel
->refcount
= 1;
1501 case LTTNG_UST_CHAN_METADATA
:
1502 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1503 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1510 health_code_update();
1512 ret
= ask_channel(ctx
, channel
, &attr
);
1514 goto end_channel_error
;
1517 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1518 ret
= consumer_metadata_cache_allocate(channel
);
1520 ERR("Allocating metadata cache");
1521 goto end_channel_error
;
1523 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1524 attr
.switch_timer_interval
= 0;
1526 int monitor_start_ret
;
1528 consumer_timer_live_start(channel
,
1529 msg
.u
.ask_channel
.live_timer_interval
);
1530 monitor_start_ret
= consumer_timer_monitor_start(
1532 msg
.u
.ask_channel
.monitor_timer_interval
);
1533 if (monitor_start_ret
< 0) {
1534 ERR("Starting channel monitoring timer failed");
1535 goto end_channel_error
;
1539 health_code_update();
1542 * Add the channel to the internal state AFTER all streams were created
1543 * and successfully sent to session daemon. This way, all streams must
1544 * be ready before this channel is visible to the threads.
1545 * If add_channel succeeds, ownership of the channel is
1546 * passed to consumer_thread_channel_poll().
1548 ret
= add_channel(channel
, ctx
);
1550 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1551 if (channel
->switch_timer_enabled
== 1) {
1552 consumer_timer_switch_stop(channel
);
1554 consumer_metadata_cache_destroy(channel
);
1556 if (channel
->live_timer_enabled
== 1) {
1557 consumer_timer_live_stop(channel
);
1559 if (channel
->monitor_timer_enabled
== 1) {
1560 consumer_timer_monitor_stop(channel
);
1562 goto end_channel_error
;
1565 health_code_update();
1568 * Channel and streams are now created. Inform the session daemon that
1569 * everything went well and should wait to receive the channel and
1570 * streams with ustctl API.
1572 ret
= consumer_send_status_channel(sock
, channel
);
1575 * There is probably a problem on the socket.
1582 case LTTNG_CONSUMER_GET_CHANNEL
:
1584 int ret
, relayd_err
= 0;
1585 uint64_t key
= msg
.u
.get_channel
.key
;
1586 struct lttng_consumer_channel
*channel
;
1588 channel
= consumer_find_channel(key
);
1590 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1591 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1592 goto end_get_channel
;
1595 health_code_update();
1597 /* Send the channel to sessiond (and relayd, if applicable). */
1598 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1603 * We were unable to send to the relayd the stream so avoid
1604 * sending back a fatal error to the thread since this is OK
1605 * and the consumer can continue its work. The above call
1606 * has sent the error status message to the sessiond.
1608 goto end_get_channel_nosignal
;
1611 * The communicaton was broken hence there is a bad state between
1612 * the consumer and sessiond so stop everything.
1614 goto error_get_channel_fatal
;
1617 health_code_update();
1620 * In no monitor mode, the streams ownership is kept inside the channel
1621 * so don't send them to the data thread.
1623 if (!channel
->monitor
) {
1624 goto end_get_channel
;
1627 ret
= send_streams_to_thread(channel
, ctx
);
1630 * If we are unable to send the stream to the thread, there is
1631 * a big problem so just stop everything.
1633 goto error_get_channel_fatal
;
1635 /* List MUST be empty after or else it could be reused. */
1636 assert(cds_list_empty(&channel
->streams
.head
));
1638 goto end_msg_sessiond
;
1639 error_get_channel_fatal
:
1641 end_get_channel_nosignal
:
1644 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1646 uint64_t key
= msg
.u
.destroy_channel
.key
;
1649 * Only called if streams have not been sent to stream
1650 * manager thread. However, channel has been sent to
1651 * channel manager thread.
1653 notify_thread_del_channel(ctx
, key
);
1654 goto end_msg_sessiond
;
1656 case LTTNG_CONSUMER_CLOSE_METADATA
:
1660 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1665 goto end_msg_sessiond
;
1667 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1671 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1676 goto end_msg_sessiond
;
1678 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1682 ret
= clear_quiescent_channel(
1683 msg
.u
.clear_quiescent_channel
.key
);
1688 goto end_msg_sessiond
;
1690 case LTTNG_CONSUMER_PUSH_METADATA
:
1693 uint64_t len
= msg
.u
.push_metadata
.len
;
1694 uint64_t key
= msg
.u
.push_metadata
.key
;
1695 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1696 uint64_t version
= msg
.u
.push_metadata
.version
;
1697 struct lttng_consumer_channel
*channel
;
1699 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1702 channel
= consumer_find_channel(key
);
1705 * This is possible if the metadata creation on the consumer side
1706 * is in flight vis-a-vis a concurrent push metadata from the
1707 * session daemon. Simply return that the channel failed and the
1708 * session daemon will handle that message correctly considering
1709 * that this race is acceptable thus the DBG() statement here.
1711 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1712 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1713 goto end_push_metadata_msg_sessiond
;
1716 health_code_update();
1720 * There is nothing to receive. We have simply
1721 * checked whether the channel can be found.
1723 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1724 goto end_push_metadata_msg_sessiond
;
1727 /* Tell session daemon we are ready to receive the metadata. */
1728 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1730 /* Somehow, the session daemon is not responding anymore. */
1731 goto error_push_metadata_fatal
;
1734 health_code_update();
1736 /* Wait for more data. */
1737 health_poll_entry();
1738 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1741 goto error_push_metadata_fatal
;
1744 health_code_update();
1746 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1747 len
, version
, channel
, 0, 1);
1749 /* error receiving from sessiond */
1750 goto error_push_metadata_fatal
;
1753 goto end_push_metadata_msg_sessiond
;
1755 end_push_metadata_msg_sessiond
:
1756 goto end_msg_sessiond
;
1757 error_push_metadata_fatal
:
1760 case LTTNG_CONSUMER_SETUP_METADATA
:
1764 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1768 goto end_msg_sessiond
;
1770 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1772 struct lttng_consumer_channel
*channel
;
1773 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1775 channel
= consumer_find_channel(key
);
1777 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1778 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1780 if (msg
.u
.snapshot_channel
.metadata
) {
1781 ret
= snapshot_metadata(channel
, key
,
1782 msg
.u
.snapshot_channel
.pathname
,
1783 msg
.u
.snapshot_channel
.relayd_id
,
1786 ERR("Snapshot metadata failed");
1787 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1790 ret
= snapshot_channel(channel
, key
,
1791 msg
.u
.snapshot_channel
.pathname
,
1792 msg
.u
.snapshot_channel
.relayd_id
,
1793 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1796 ERR("Snapshot channel failed");
1797 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1801 health_code_update();
1802 ret
= consumer_send_status_msg(sock
, ret_code
);
1804 /* Somehow, the session daemon is not responding anymore. */
1807 health_code_update();
1810 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1813 uint64_t discarded_events
;
1814 struct lttng_ht_iter iter
;
1815 struct lttng_ht
*ht
;
1816 struct lttng_consumer_stream
*stream
;
1817 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1818 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1820 DBG("UST consumer discarded events command for session id %"
1823 pthread_mutex_lock(&consumer_data
.lock
);
1825 ht
= consumer_data
.stream_list_ht
;
1828 * We only need a reference to the channel, but they are not
1829 * directly indexed, so we just use the first matching stream
1830 * to extract the information we need, we default to 0 if not
1831 * found (no events are dropped if the channel is not yet in
1834 discarded_events
= 0;
1835 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1836 ht
->hash_fct(&id
, lttng_ht_seed
),
1838 &iter
.iter
, stream
, node_session_id
.node
) {
1839 if (stream
->chan
->key
== key
) {
1840 discarded_events
= stream
->chan
->discarded_events
;
1844 pthread_mutex_unlock(&consumer_data
.lock
);
1847 DBG("UST consumer discarded events command for session id %"
1848 PRIu64
", channel key %" PRIu64
, id
, key
);
1850 health_code_update();
1852 /* Send back returned value to session daemon */
1853 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1855 PERROR("send discarded events");
1861 case LTTNG_CONSUMER_LOST_PACKETS
:
1864 uint64_t lost_packets
;
1865 struct lttng_ht_iter iter
;
1866 struct lttng_ht
*ht
;
1867 struct lttng_consumer_stream
*stream
;
1868 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1869 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1871 DBG("UST consumer lost packets command for session id %"
1874 pthread_mutex_lock(&consumer_data
.lock
);
1876 ht
= consumer_data
.stream_list_ht
;
1879 * We only need a reference to the channel, but they are not
1880 * directly indexed, so we just use the first matching stream
1881 * to extract the information we need, we default to 0 if not
1882 * found (no packets lost if the channel is not yet in use).
1885 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1886 ht
->hash_fct(&id
, lttng_ht_seed
),
1888 &iter
.iter
, stream
, node_session_id
.node
) {
1889 if (stream
->chan
->key
== key
) {
1890 lost_packets
= stream
->chan
->lost_packets
;
1894 pthread_mutex_unlock(&consumer_data
.lock
);
1897 DBG("UST consumer lost packets command for session id %"
1898 PRIu64
", channel key %" PRIu64
, id
, key
);
1900 health_code_update();
1902 /* Send back returned value to session daemon */
1903 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1904 sizeof(lost_packets
));
1906 PERROR("send lost packets");
1912 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1914 int channel_monitor_pipe
;
1916 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1917 /* Successfully received the command's type. */
1918 ret
= consumer_send_status_msg(sock
, ret_code
);
1923 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1925 if (ret
!= sizeof(channel_monitor_pipe
)) {
1926 ERR("Failed to receive channel monitor pipe");
1930 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1931 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1932 channel_monitor_pipe
);
1936 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1937 /* Set the pipe as non-blocking. */
1938 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1940 PERROR("fcntl get flags of the channel monitoring pipe");
1945 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1946 flags
| O_NONBLOCK
);
1948 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1951 DBG("Channel monitor pipe set as non-blocking");
1953 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1955 goto end_msg_sessiond
;
1957 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1959 struct lttng_consumer_channel
*channel
;
1960 uint64_t key
= msg
.u
.rotate_channel
.key
;
1962 channel
= consumer_find_channel(key
);
1964 DBG("Channel %" PRIu64
" not found", key
);
1965 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1968 * Sample the rotate position of all the streams in
1971 ret
= lttng_consumer_rotate_channel(channel
, key
,
1972 msg
.u
.rotate_channel
.relayd_id
,
1973 msg
.u
.rotate_channel
.metadata
,
1976 ERR("Rotate channel failed");
1977 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1980 health_code_update();
1982 ret
= consumer_send_status_msg(sock
, ret_code
);
1984 /* Somehow, the session daemon is not responding anymore. */
1985 goto end_rotate_channel_nosignal
;
1989 * Rotate the streams that are ready right now.
1990 * FIXME: this is a second consecutive iteration over the
1991 * streams in a channel, there is probably a better way to
1992 * handle this, but it needs to be after the
1993 * consumer_send_status_msg() call.
1996 ret
= lttng_consumer_rotate_ready_streams(
1999 ERR("Rotate channel failed");
2003 end_rotate_channel_nosignal
:
2006 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2008 struct lttng_consumer_channel
*channel
;
2009 uint64_t key
= msg
.u
.clear_channel
.key
;
2011 channel
= consumer_find_channel(key
);
2013 DBG("Channel %" PRIu64
" not found", key
);
2014 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2016 ret
= lttng_consumer_clear_channel(channel
);
2018 ERR("Clear channel failed key %" PRIu64
, key
);
2022 health_code_update();
2024 ret
= consumer_send_status_msg(sock
, ret_code
);
2026 /* Somehow, the session daemon is not responding anymore. */
2031 case LTTNG_CONSUMER_INIT
:
2033 ret_code
= lttng_consumer_init_command(ctx
,
2034 msg
.u
.init
.sessiond_uuid
);
2035 health_code_update();
2036 ret
= consumer_send_status_msg(sock
, ret_code
);
2038 /* Somehow, the session daemon is not responding anymore. */
2043 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2045 const struct lttng_credentials credentials
= {
2046 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2047 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2049 const bool is_local_trace
=
2050 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2051 const uint64_t relayd_id
=
2052 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2053 const char *chunk_override_name
=
2054 *msg
.u
.create_trace_chunk
.override_name
?
2055 msg
.u
.create_trace_chunk
.override_name
:
2057 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2060 * The session daemon will only provide a chunk directory file
2061 * descriptor for local traces.
2063 if (is_local_trace
) {
2066 /* Acnowledge the reception of the command. */
2067 ret
= consumer_send_status_msg(sock
,
2068 LTTCOMM_CONSUMERD_SUCCESS
);
2070 /* Somehow, the session daemon is not responding anymore. */
2075 * Receive trace chunk domain dirfd.
2077 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2078 if (ret
!= sizeof(chunk_dirfd
)) {
2079 ERR("Failed to receive trace chunk domain directory file descriptor");
2083 DBG("Received trace chunk domain directory fd (%d)",
2085 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2087 if (!chunk_directory_handle
) {
2088 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2089 if (close(chunk_dirfd
)) {
2090 PERROR("Failed to close chunk directory file descriptor");
2096 ret_code
= lttng_consumer_create_trace_chunk(
2097 !is_local_trace
? &relayd_id
: NULL
,
2098 msg
.u
.create_trace_chunk
.session_id
,
2099 msg
.u
.create_trace_chunk
.chunk_id
,
2100 (time_t) msg
.u
.create_trace_chunk
2101 .creation_timestamp
,
2102 chunk_override_name
,
2103 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2106 chunk_directory_handle
);
2107 lttng_directory_handle_put(chunk_directory_handle
);
2108 goto end_msg_sessiond
;
2110 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2112 enum lttng_trace_chunk_command_type close_command
=
2113 msg
.u
.close_trace_chunk
.close_command
.value
;
2114 const uint64_t relayd_id
=
2115 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2116 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2117 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2120 ret_code
= lttng_consumer_close_trace_chunk(
2121 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2124 msg
.u
.close_trace_chunk
.session_id
,
2125 msg
.u
.close_trace_chunk
.chunk_id
,
2126 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2127 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2129 NULL
, closed_trace_chunk_path
);
2130 reply
.ret_code
= ret_code
;
2131 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2132 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2133 if (ret
!= sizeof(reply
)) {
2136 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2138 if (ret
!= reply
.path_length
) {
2143 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2145 const uint64_t relayd_id
=
2146 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2148 ret_code
= lttng_consumer_trace_chunk_exists(
2149 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2151 msg
.u
.trace_chunk_exists
.session_id
,
2152 msg
.u
.trace_chunk_exists
.chunk_id
);
2153 goto end_msg_sessiond
;
2161 * Return 1 to indicate success since the 0 value can be a socket
2162 * shutdown during the recv() or send() call.
2169 * The returned value here is not useful since either way we'll return 1 to
2170 * the caller because the session daemon socket management is done
2171 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2173 ret
= consumer_send_status_msg(sock
, ret_code
);
2183 * Free channel here since no one has a reference to it. We don't
2184 * free after that because a stream can store this pointer.
2186 destroy_channel(channel
);
2188 /* We have to send a status channel message indicating an error. */
2189 ret
= consumer_send_status_channel(sock
, NULL
);
2191 /* Stop everything if session daemon can not be notified. */
2198 /* This will issue a consumer stop. */
2204 health_code_update();
2209 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
2210 * compiled out, we isolate it in this library.
2212 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
2216 assert(stream
->ustream
);
2218 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
2222 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
2223 * compiled out, we isolate it in this library.
2225 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
2228 assert(stream
->ustream
);
2230 return ustctl_get_mmap_base(stream
->ustream
);
2233 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2234 int producer_active
)
2237 assert(stream
->ustream
);
2239 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2243 * Take a snapshot for a specific stream.
2245 * Returns 0 on success, < 0 on error
2247 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2250 assert(stream
->ustream
);
2252 return ustctl_snapshot(stream
->ustream
);
2256 * Sample consumed and produced positions for a specific stream.
2258 * Returns 0 on success, < 0 on error.
2260 int lttng_ustconsumer_sample_snapshot_positions(
2261 struct lttng_consumer_stream
*stream
)
2264 assert(stream
->ustream
);
2266 return ustctl_snapshot_sample_positions(stream
->ustream
);
2270 * Get the produced position
2272 * Returns 0 on success, < 0 on error
2274 int lttng_ustconsumer_get_produced_snapshot(
2275 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2278 assert(stream
->ustream
);
2281 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2285 * Get the consumed position
2287 * Returns 0 on success, < 0 on error
2289 int lttng_ustconsumer_get_consumed_snapshot(
2290 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2293 assert(stream
->ustream
);
2296 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2299 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2303 assert(stream
->ustream
);
2305 ustctl_flush_buffer(stream
->ustream
, producer
);
2308 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2311 assert(stream
->ustream
);
2313 ustctl_clear_buffer(stream
->ustream
);
2316 int lttng_ustconsumer_get_current_timestamp(
2317 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2320 assert(stream
->ustream
);
2323 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2326 int lttng_ustconsumer_get_sequence_number(
2327 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2330 assert(stream
->ustream
);
2333 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2337 * Called when the stream signals the consumer that it has hung up.
2339 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2342 assert(stream
->ustream
);
2344 pthread_mutex_lock(&stream
->lock
);
2345 if (!stream
->quiescent
) {
2346 ustctl_flush_buffer(stream
->ustream
, 0);
2347 stream
->quiescent
= true;
2349 pthread_mutex_unlock(&stream
->lock
);
2350 stream
->hangup_flush_done
= 1;
2353 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2358 assert(chan
->uchan
);
2359 assert(chan
->buffer_credentials
.is_set
);
2361 if (chan
->switch_timer_enabled
== 1) {
2362 consumer_timer_switch_stop(chan
);
2364 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2367 ret
= close(chan
->stream_fds
[i
]);
2371 if (chan
->shm_path
[0]) {
2372 char shm_path
[PATH_MAX
];
2374 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2376 ERR("Cannot get stream shm path");
2378 ret
= run_as_unlink(shm_path
,
2379 chan
->buffer_credentials
.value
.uid
,
2380 chan
->buffer_credentials
.value
.gid
);
2382 PERROR("unlink %s", shm_path
);
2388 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2391 assert(chan
->uchan
);
2392 assert(chan
->buffer_credentials
.is_set
);
2394 consumer_metadata_cache_destroy(chan
);
2395 ustctl_destroy_channel(chan
->uchan
);
2396 /* Try to rmdir all directories under shm_path root. */
2397 if (chan
->root_shm_path
[0]) {
2398 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2399 chan
->buffer_credentials
.value
.uid
,
2400 chan
->buffer_credentials
.value
.gid
,
2401 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2403 free(chan
->stream_fds
);
2406 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2409 assert(stream
->ustream
);
2411 if (stream
->chan
->switch_timer_enabled
== 1) {
2412 consumer_timer_switch_stop(stream
->chan
);
2414 ustctl_destroy_stream(stream
->ustream
);
2417 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2420 assert(stream
->ustream
);
2422 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2425 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2428 assert(stream
->ustream
);
2430 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2434 * Populate index values of a UST stream. Values are set in big endian order.
2436 * Return 0 on success or else a negative value.
2438 static int get_index_values(struct ctf_packet_index
*index
,
2439 struct ustctl_consumer_stream
*ustream
)
2442 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
2443 events_discarded
, stream_id
, stream_instance_id
,
2446 ret
= ustctl_get_timestamp_begin(ustream
, ×tamp_begin
);
2448 PERROR("ustctl_get_timestamp_begin");
2452 ret
= ustctl_get_timestamp_end(ustream
, ×tamp_end
);
2454 PERROR("ustctl_get_timestamp_end");
2458 ret
= ustctl_get_events_discarded(ustream
, &events_discarded
);
2460 PERROR("ustctl_get_events_discarded");
2464 ret
= ustctl_get_content_size(ustream
, &content_size
);
2466 PERROR("ustctl_get_content_size");
2470 ret
= ustctl_get_packet_size(ustream
, &packet_size
);
2472 PERROR("ustctl_get_packet_size");
2476 ret
= ustctl_get_stream_id(ustream
, &stream_id
);
2478 PERROR("ustctl_get_stream_id");
2482 ret
= ustctl_get_instance_id(ustream
, &stream_instance_id
);
2484 PERROR("ustctl_get_instance_id");
2488 ret
= ustctl_get_sequence_number(ustream
, &packet_seq_num
);
2490 PERROR("ustctl_get_sequence_number");
2494 *index
= (typeof(*index
)) {
2495 .offset
= index
->offset
,
2496 .packet_size
= htobe64(packet_size
),
2497 .content_size
= htobe64(content_size
),
2498 .timestamp_begin
= htobe64(timestamp_begin
),
2499 .timestamp_end
= htobe64(timestamp_end
),
2500 .events_discarded
= htobe64(events_discarded
),
2501 .stream_id
= htobe64(stream_id
),
2502 .stream_instance_id
= htobe64(stream_instance_id
),
2503 .packet_seq_num
= htobe64(packet_seq_num
),
2511 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2512 struct consumer_metadata_cache
*cache
)
2514 DBG("Metadata stream update to version %" PRIu64
,
2516 stream
->ust_metadata_pushed
= 0;
2517 stream
->metadata_version
= cache
->version
;
2518 stream
->reset_metadata_flag
= 1;
2522 * Check if the version of the metadata stream and metadata cache match.
2523 * If the cache got updated, reset the metadata stream.
2524 * The stream lock and metadata cache lock MUST be held.
2525 * Return 0 on success, a negative value on error.
2528 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2531 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2533 if (cache
->version
== stream
->metadata_version
) {
2536 metadata_stream_reset_cache(stream
, cache
);
2543 * Write up to one packet from the metadata cache to the channel.
2545 * Returns the number of bytes pushed in the cache, or a negative value
2549 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2554 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2555 ret
= metadata_stream_check_version(stream
);
2559 if (stream
->chan
->metadata_cache
->max_offset
2560 == stream
->ust_metadata_pushed
) {
2565 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2566 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2567 stream
->chan
->metadata_cache
->max_offset
2568 - stream
->ust_metadata_pushed
);
2569 assert(write_len
!= 0);
2570 if (write_len
< 0) {
2571 ERR("Writing one metadata packet");
2575 stream
->ust_metadata_pushed
+= write_len
;
2577 assert(stream
->chan
->metadata_cache
->max_offset
>=
2578 stream
->ust_metadata_pushed
);
2582 * Switch packet (but don't open the next one) on every commit of
2583 * a metadata packet. Since the subbuffer is fully filled (with padding,
2584 * if needed), the stream is "quiescent" after this commit.
2586 ustctl_flush_buffer(stream
->ustream
, 1);
2587 stream
->quiescent
= true;
2589 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2595 * Sync metadata meaning request them to the session daemon and snapshot to the
2596 * metadata thread can consumer them.
2598 * Metadata stream lock is held here, but we need to release it when
2599 * interacting with sessiond, else we cause a deadlock with live
2600 * awaiting on metadata to be pushed out.
2602 * The RCU read side lock must be held by the caller.
2604 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2605 * is empty or a negative value on error.
2607 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2608 struct lttng_consumer_stream
*metadata_stream
)
2612 struct lttng_consumer_channel
*metadata_channel
;
2615 assert(metadata_stream
);
2617 metadata_channel
= metadata_stream
->chan
;
2618 pthread_mutex_unlock(&metadata_stream
->lock
);
2620 * Request metadata from the sessiond, but don't wait for the flush
2621 * because we locked the metadata thread.
2623 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2624 pthread_mutex_lock(&metadata_stream
->lock
);
2630 * The metadata stream and channel can be deleted while the
2631 * metadata stream lock was released. The streamed is checked
2632 * for deletion before we use it further.
2634 * Note that it is safe to access a logically-deleted stream since its
2635 * existence is still guaranteed by the RCU read side lock. However,
2636 * it should no longer be used. The close/deletion of the metadata
2637 * channel and stream already guarantees that all metadata has been
2638 * consumed. Therefore, there is nothing left to do in this function.
2640 if (consumer_stream_is_deleted(metadata_stream
)) {
2641 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2642 metadata_stream
->key
);
2647 ret
= commit_one_metadata_packet(metadata_stream
);
2650 } else if (ret
> 0) {
2654 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2656 if (errno
!= EAGAIN
) {
2657 ERR("Sync metadata, taking UST snapshot");
2660 DBG("No new metadata when syncing them.");
2661 /* No new metadata, exit. */
2667 * After this flush, we still need to extract metadata.
2678 * Return 0 on success else a negative value.
2680 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2681 struct lttng_consumer_local_data
*ctx
)
2684 struct ustctl_consumer_stream
*ustream
;
2689 ustream
= stream
->ustream
;
2692 * First, we are going to check if there is a new subbuffer available
2693 * before reading the stream wait_fd.
2695 /* Get the next subbuffer */
2696 ret
= ustctl_get_next_subbuf(ustream
);
2698 /* No more data found, flag the stream. */
2699 stream
->has_data
= 0;
2704 ret
= ustctl_put_subbuf(ustream
);
2707 /* This stream still has data. Flag it and wake up the data thread. */
2708 stream
->has_data
= 1;
2710 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2713 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2714 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2719 /* The wake up pipe has been notified. */
2720 ctx
->has_wakeup
= 1;
2729 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2732 uint64_t seq
, discarded
;
2734 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2736 PERROR("ustctl_get_sequence_number");
2740 * Start the sequence when we extract the first packet in case we don't
2741 * start at 0 (for example if a consumer is not connected to the
2742 * session immediately after the beginning).
2744 if (stream
->last_sequence_number
== -1ULL) {
2745 stream
->last_sequence_number
= seq
;
2746 } else if (seq
> stream
->last_sequence_number
) {
2747 stream
->chan
->lost_packets
+= seq
-
2748 stream
->last_sequence_number
- 1;
2750 /* seq <= last_sequence_number */
2751 ERR("Sequence number inconsistent : prev = %" PRIu64
2752 ", current = %" PRIu64
,
2753 stream
->last_sequence_number
, seq
);
2757 stream
->last_sequence_number
= seq
;
2759 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2761 PERROR("kernctl_get_events_discarded");
2764 if (discarded
< stream
->last_discarded_events
) {
2766 * Overflow has occurred. We assume only one wrap-around
2769 stream
->chan
->discarded_events
+=
2770 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2771 stream
->last_discarded_events
+ discarded
;
2773 stream
->chan
->discarded_events
+= discarded
-
2774 stream
->last_discarded_events
;
2776 stream
->last_discarded_events
= discarded
;
2784 * Read subbuffer from the given stream.
2786 * Stream and channel locks MUST be acquired by the caller.
2788 * Return 0 on success else a negative value.
2790 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2791 struct lttng_consumer_local_data
*ctx
)
2793 unsigned long len
, subbuf_size
, padding
;
2794 int err
, write_index
= 1, rotation_ret
;
2796 struct ustctl_consumer_stream
*ustream
;
2797 struct ctf_packet_index index
;
2800 assert(stream
->ustream
);
2803 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2806 /* Ease our life for what's next. */
2807 ustream
= stream
->ustream
;
2810 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2811 * error if we cannot read this one byte (read returns 0), or if the error
2812 * is EAGAIN or EWOULDBLOCK.
2814 * This is only done when the stream is monitored by a thread, before the
2815 * flush is done after a hangup and if the stream is not flagged with data
2816 * since there might be nothing to consume in the wait fd but still have
2817 * data available flagged by the consumer wake up pipe.
2819 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2823 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2824 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2831 * If the stream was flagged to be ready for rotation before we extract the
2832 * next packet, rotate it now.
2834 if (stream
->rotate_ready
) {
2835 DBG("Rotate stream before extracting data");
2836 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2837 if (rotation_ret
< 0) {
2838 ERR("Stream rotation error");
2845 /* Get the next subbuffer */
2846 err
= ustctl_get_next_subbuf(ustream
);
2849 * Populate metadata info if the existing info has
2850 * already been read.
2852 if (stream
->metadata_flag
) {
2853 ret
= commit_one_metadata_packet(stream
);
2860 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2862 * This is a debug message even for single-threaded consumer,
2863 * because poll() have more relaxed criterions than get subbuf,
2864 * so get_subbuf may fail for short race windows where poll()
2865 * would issue wakeups.
2867 DBG("Reserving sub buffer failed (everything is normal, "
2868 "it is due to concurrency) [ret: %d]", err
);
2871 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2873 if (!stream
->metadata_flag
) {
2874 index
.offset
= htobe64(stream
->out_fd_offset
);
2875 ret
= get_index_values(&index
, ustream
);
2877 err
= ustctl_put_subbuf(ustream
);
2882 /* Update the stream's sequence and discarded events count. */
2883 ret
= update_stream_stats(stream
);
2885 PERROR("kernctl_get_events_discarded");
2886 err
= ustctl_put_subbuf(ustream
);
2894 /* Get the full padded subbuffer size */
2895 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2898 /* Get subbuffer data size (without padding) */
2899 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2902 /* Make sure we don't get a subbuffer size bigger than the padded */
2903 assert(len
>= subbuf_size
);
2905 padding
= len
- subbuf_size
;
2907 /* write the subbuffer to the tracefile */
2908 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
, &index
);
2910 * The mmap operation should write subbuf_size amount of data when network
2911 * streaming or the full padding (len) size when we are _not_ streaming.
2913 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2914 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2916 * Display the error but continue processing to try to release the
2917 * subbuffer. This is a DBG statement since any unexpected kill or
2918 * signal, the application gets unregistered, relayd gets closed or
2919 * anything that affects the buffer lifetime will trigger this error.
2920 * So, for the sake of the user, don't print this error since it can
2921 * happen and it is OK with the code flow.
2923 DBG("Error writing to tracefile "
2924 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2925 ret
, len
, subbuf_size
);
2928 err
= ustctl_put_next_subbuf(ustream
);
2932 * This will consumer the byte on the wait_fd if and only if there is not
2933 * next subbuffer to be acquired.
2935 if (!stream
->metadata_flag
) {
2936 ret
= notify_if_more_data(stream
, ctx
);
2942 /* Write index if needed. */
2947 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2949 * In live, block until all the metadata is sent.
2951 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2952 assert(!stream
->missed_metadata_flush
);
2953 stream
->waiting_on_metadata
= true;
2954 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2956 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2958 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2959 stream
->waiting_on_metadata
= false;
2960 if (stream
->missed_metadata_flush
) {
2961 stream
->missed_metadata_flush
= false;
2962 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2963 (void) consumer_flush_ust_index(stream
);
2965 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2973 assert(!stream
->metadata_flag
);
2974 err
= consumer_stream_write_index(stream
, &index
);
2981 * After extracting the packet, we check if the stream is now ready to be
2982 * rotated and perform the action immediately.
2984 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
2985 if (rotation_ret
== 1) {
2986 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2987 if (rotation_ret
< 0) {
2988 ERR("Stream rotation error");
2992 } else if (rotation_ret
< 0) {
2993 ERR("Checking if stream is ready to rotate");
3002 * Called when a stream is created.
3004 * Return 0 on success or else a negative value.
3006 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3013 * Don't create anything if this is set for streaming or if there is
3014 * no current trace chunk on the parent channel.
3016 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3017 stream
->chan
->trace_chunk
) {
3018 ret
= consumer_stream_create_output_files(stream
, true);
3030 * Check if data is still being extracted from the buffers for a specific
3031 * stream. Consumer data lock MUST be acquired before calling this function
3032 * and the stream lock.
3034 * Return 1 if the traced data are still getting read else 0 meaning that the
3035 * data is available for trace viewer reading.
3037 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3042 assert(stream
->ustream
);
3044 DBG("UST consumer checking data pending");
3046 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3051 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3052 uint64_t contiguous
, pushed
;
3054 /* Ease our life a bit. */
3055 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3056 pushed
= stream
->ust_metadata_pushed
;
3059 * We can simply check whether all contiguously available data
3060 * has been pushed to the ring buffer, since the push operation
3061 * is performed within get_next_subbuf(), and because both
3062 * get_next_subbuf() and put_next_subbuf() are issued atomically
3063 * thanks to the stream lock within
3064 * lttng_ustconsumer_read_subbuffer(). This basically means that
3065 * whetnever ust_metadata_pushed is incremented, the associated
3066 * metadata has been consumed from the metadata stream.
3068 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3069 contiguous
, pushed
);
3070 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3071 if ((contiguous
!= pushed
) ||
3072 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3073 ret
= 1; /* Data is pending */
3077 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3080 * There is still data so let's put back this
3083 ret
= ustctl_put_subbuf(stream
->ustream
);
3085 ret
= 1; /* Data is pending */
3090 /* Data is NOT pending so ready to be read. */
3098 * Stop a given metadata channel timer if enabled and close the wait fd which
3099 * is the poll pipe of the metadata stream.
3101 * This MUST be called with the metadata channel lock acquired.
3103 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3108 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3110 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3112 if (metadata
->switch_timer_enabled
== 1) {
3113 consumer_timer_switch_stop(metadata
);
3116 if (!metadata
->metadata_stream
) {
3121 * Closing write side so the thread monitoring the stream wakes up if any
3122 * and clean the metadata stream.
3124 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3125 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3127 PERROR("closing metadata pipe write side");
3129 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3137 * Close every metadata stream wait fd of the metadata hash table. This
3138 * function MUST be used very carefully so not to run into a race between the
3139 * metadata thread handling streams and this function closing their wait fd.
3141 * For UST, this is used when the session daemon hangs up. Its the metadata
3142 * producer so calling this is safe because we are assured that no state change
3143 * can occur in the metadata thread for the streams in the hash table.
3145 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3147 struct lttng_ht_iter iter
;
3148 struct lttng_consumer_stream
*stream
;
3150 assert(metadata_ht
);
3151 assert(metadata_ht
->ht
);
3153 DBG("UST consumer closing all metadata streams");
3156 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3159 health_code_update();
3161 pthread_mutex_lock(&stream
->chan
->lock
);
3162 lttng_ustconsumer_close_metadata(stream
->chan
);
3163 pthread_mutex_unlock(&stream
->chan
->lock
);
3169 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3173 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3175 ERR("Unable to close wakeup fd");
3180 * Please refer to consumer-timer.c before adding any lock within this
3181 * function or any of its callees. Timers have a very strict locking
3182 * semantic with respect to teardown. Failure to respect this semantic
3183 * introduces deadlocks.
3185 * DON'T hold the metadata lock when calling this function, else this
3186 * can cause deadlock involving consumer awaiting for metadata to be
3187 * pushed out due to concurrent interaction with the session daemon.
3189 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3190 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3192 struct lttcomm_metadata_request_msg request
;
3193 struct lttcomm_consumer_msg msg
;
3194 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3195 uint64_t len
, key
, offset
, version
;
3199 assert(channel
->metadata_cache
);
3201 memset(&request
, 0, sizeof(request
));
3203 /* send the metadata request to sessiond */
3204 switch (consumer_data
.type
) {
3205 case LTTNG_CONSUMER64_UST
:
3206 request
.bits_per_long
= 64;
3208 case LTTNG_CONSUMER32_UST
:
3209 request
.bits_per_long
= 32;
3212 request
.bits_per_long
= 0;
3216 request
.session_id
= channel
->session_id
;
3217 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3219 * Request the application UID here so the metadata of that application can
3220 * be sent back. The channel UID corresponds to the user UID of the session
3221 * used for the rights on the stream file(s).
3223 request
.uid
= channel
->ust_app_uid
;
3224 request
.key
= channel
->key
;
3226 DBG("Sending metadata request to sessiond, session id %" PRIu64
3227 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3228 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3231 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3233 health_code_update();
3235 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3238 ERR("Asking metadata to sessiond");
3242 health_code_update();
3244 /* Receive the metadata from sessiond */
3245 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3247 if (ret
!= sizeof(msg
)) {
3248 DBG("Consumer received unexpected message size %d (expects %zu)",
3250 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3252 * The ret value might 0 meaning an orderly shutdown but this is ok
3253 * since the caller handles this.
3258 health_code_update();
3260 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3261 /* No registry found */
3262 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3266 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3267 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3272 len
= msg
.u
.push_metadata
.len
;
3273 key
= msg
.u
.push_metadata
.key
;
3274 offset
= msg
.u
.push_metadata
.target_offset
;
3275 version
= msg
.u
.push_metadata
.version
;
3277 assert(key
== channel
->key
);
3279 DBG("No new metadata to receive for key %" PRIu64
, key
);
3282 health_code_update();
3284 /* Tell session daemon we are ready to receive the metadata. */
3285 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3286 LTTCOMM_CONSUMERD_SUCCESS
);
3287 if (ret
< 0 || len
== 0) {
3289 * Somehow, the session daemon is not responding anymore or there is
3290 * nothing to receive.
3295 health_code_update();
3297 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3298 key
, offset
, len
, version
, channel
, timer
, wait
);
3301 * Only send the status msg if the sessiond is alive meaning a positive
3304 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3309 health_code_update();
3311 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3316 * Return the ustctl call for the get stream id.
3318 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3319 uint64_t *stream_id
)
3324 return ustctl_get_stream_id(stream
->ustream
, stream_id
);