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
13 #include <lttng/ust-ctl.h>
19 #include <sys/socket.h>
21 #include <sys/types.h>
24 #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>
40 #include "ust-consumer.h"
42 #define INT_MAX_STR_LEN 12 /* includes \0 */
44 extern struct lttng_consumer_global_data consumer_data
;
45 extern int consumer_poll_timeout
;
48 * Free channel object and all streams associated with it. This MUST be used
49 * only and only if the channel has _NEVER_ been added to the global channel
52 static void destroy_channel(struct lttng_consumer_channel
*channel
)
54 struct lttng_consumer_stream
*stream
, *stmp
;
58 DBG("UST consumer cleaning stream list");
60 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
65 cds_list_del(&stream
->send_node
);
66 ustctl_destroy_stream(stream
->ustream
);
67 lttng_trace_chunk_put(stream
->trace_chunk
);
72 * If a channel is available meaning that was created before the streams
76 lttng_ustconsumer_del_channel(channel
);
77 lttng_ustconsumer_free_channel(channel
);
83 * Add channel to internal consumer state.
85 * Returns 0 on success or else a negative value.
87 static int add_channel(struct lttng_consumer_channel
*channel
,
88 struct lttng_consumer_local_data
*ctx
)
95 if (ctx
->on_recv_channel
!= NULL
) {
96 ret
= ctx
->on_recv_channel(channel
);
98 ret
= consumer_add_channel(channel
, ctx
);
100 /* Most likely an ENOMEM. */
101 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
105 ret
= consumer_add_channel(channel
, ctx
);
108 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
115 * Allocate and return a consumer channel object.
117 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
118 const uint64_t *chunk_id
, const char *pathname
, const char *name
,
119 uint64_t relayd_id
, uint64_t key
, enum lttng_event_output output
,
120 uint64_t tracefile_size
, uint64_t tracefile_count
,
121 uint64_t session_id_per_pid
, unsigned int monitor
,
122 unsigned int live_timer_interval
,
123 const char *root_shm_path
, const char *shm_path
)
128 return consumer_allocate_channel(key
, session_id
, chunk_id
, pathname
,
129 name
, relayd_id
, output
, tracefile_size
,
130 tracefile_count
, session_id_per_pid
, monitor
,
131 live_timer_interval
, root_shm_path
, shm_path
);
135 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
136 * error value if applicable is set in it else it is kept untouched.
138 * Return NULL on error else the newly allocated stream object.
140 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
141 struct lttng_consumer_channel
*channel
,
142 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
145 struct lttng_consumer_stream
*stream
= NULL
;
150 stream
= consumer_allocate_stream(
157 channel
->trace_chunk
,
162 if (stream
== NULL
) {
166 * We could not find the channel. Can happen if cpu hotplug
167 * happens while tearing down.
169 DBG3("Could not find channel");
174 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
180 consumer_stream_update_channel_attributes(stream
, channel
);
184 *_alloc_ret
= alloc_ret
;
190 * Send the given stream pointer to the corresponding thread.
192 * Returns 0 on success else a negative value.
194 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
195 struct lttng_consumer_local_data
*ctx
)
198 struct lttng_pipe
*stream_pipe
;
200 /* Get the right pipe where the stream will be sent. */
201 if (stream
->metadata_flag
) {
202 consumer_add_metadata_stream(stream
);
203 stream_pipe
= ctx
->consumer_metadata_pipe
;
205 consumer_add_data_stream(stream
);
206 stream_pipe
= ctx
->consumer_data_pipe
;
210 * From this point on, the stream's ownership has been moved away from
211 * the channel and it becomes globally visible. Hence, remove it from
212 * the local stream list to prevent the stream from being both local and
215 stream
->globally_visible
= 1;
216 cds_list_del(&stream
->send_node
);
218 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
220 ERR("Consumer write %s stream to pipe %d",
221 stream
->metadata_flag
? "metadata" : "data",
222 lttng_pipe_get_writefd(stream_pipe
));
223 if (stream
->metadata_flag
) {
224 consumer_del_stream_for_metadata(stream
);
226 consumer_del_stream_for_data(stream
);
236 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
238 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
241 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
242 stream_shm_path
[PATH_MAX
- 1] = '\0';
243 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
248 strncat(stream_shm_path
, cpu_nr
,
249 PATH_MAX
- strlen(stream_shm_path
) - 1);
256 * Create streams for the given channel using liblttng-ust-ctl.
257 * The channel lock must be acquired by the caller.
259 * Return 0 on success else a negative value.
261 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
262 struct lttng_consumer_local_data
*ctx
)
265 struct ustctl_consumer_stream
*ustream
;
266 struct lttng_consumer_stream
*stream
;
267 pthread_mutex_t
*current_stream_lock
= NULL
;
273 * While a stream is available from ustctl. When NULL is returned, we've
274 * reached the end of the possible stream for the channel.
276 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
278 int ust_metadata_pipe
[2];
280 health_code_update();
282 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
283 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
285 ERR("Create ust metadata poll pipe");
288 wait_fd
= ust_metadata_pipe
[0];
290 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
293 /* Allocate consumer stream object. */
294 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
298 stream
->ustream
= ustream
;
300 * Store it so we can save multiple function calls afterwards since
301 * this value is used heavily in the stream threads. This is UST
302 * specific so this is why it's done after allocation.
304 stream
->wait_fd
= wait_fd
;
307 * Increment channel refcount since the channel reference has now been
308 * assigned in the allocation process above.
310 if (stream
->chan
->monitor
) {
311 uatomic_inc(&stream
->chan
->refcount
);
314 pthread_mutex_lock(&stream
->lock
);
315 current_stream_lock
= &stream
->lock
;
317 * Order is important this is why a list is used. On error, the caller
318 * should clean this list.
320 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
322 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
323 &stream
->max_sb_size
);
325 ERR("ustctl_get_max_subbuf_size failed for stream %s",
330 /* Do actions once stream has been received. */
331 if (ctx
->on_recv_stream
) {
332 ret
= ctx
->on_recv_stream(stream
);
338 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
339 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
341 /* Set next CPU stream. */
342 channel
->streams
.count
= ++cpu
;
344 /* Keep stream reference when creating metadata. */
345 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
346 channel
->metadata_stream
= stream
;
347 if (channel
->monitor
) {
348 /* Set metadata poll pipe if we created one */
349 memcpy(stream
->ust_metadata_poll_pipe
,
351 sizeof(ust_metadata_pipe
));
354 pthread_mutex_unlock(&stream
->lock
);
355 current_stream_lock
= NULL
;
362 if (current_stream_lock
) {
363 pthread_mutex_unlock(current_stream_lock
);
369 * create_posix_shm is never called concurrently within a process.
372 int create_posix_shm(void)
374 char tmp_name
[NAME_MAX
];
377 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
383 * Allocate shm, and immediately unlink its shm oject, keeping
384 * only the file descriptor as a reference to the object.
385 * We specifically do _not_ use the / at the beginning of the
386 * pathname so that some OS implementations can keep it local to
387 * the process (POSIX leaves this implementation-defined).
389 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
394 ret
= shm_unlink(tmp_name
);
395 if (ret
< 0 && errno
!= ENOENT
) {
396 PERROR("shm_unlink");
397 goto error_shm_release
;
410 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
411 const struct lttng_credentials
*session_credentials
)
413 char shm_path
[PATH_MAX
];
416 if (!channel
->shm_path
[0]) {
417 return create_posix_shm();
419 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
423 return run_as_open(shm_path
,
424 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
425 session_credentials
->uid
, session_credentials
->gid
);
432 * Create an UST channel with the given attributes and send it to the session
433 * daemon using the ust ctl API.
435 * Return 0 on success or else a negative value.
437 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
438 struct ustctl_consumer_channel_attr
*attr
,
439 struct ustctl_consumer_channel
**ust_chanp
)
441 int ret
, nr_stream_fds
, i
, j
;
443 struct ustctl_consumer_channel
*ust_channel
;
448 assert(channel
->buffer_credentials
.is_set
);
450 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
451 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
452 "switch_timer_interval: %u, read_timer_interval: %u, "
453 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
454 attr
->num_subbuf
, attr
->switch_timer_interval
,
455 attr
->read_timer_interval
, attr
->output
, attr
->type
);
457 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
460 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
461 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
466 for (i
= 0; i
< nr_stream_fds
; i
++) {
467 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
468 &channel
->buffer_credentials
.value
);
469 if (stream_fds
[i
] < 0) {
474 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
479 channel
->nr_stream_fds
= nr_stream_fds
;
480 channel
->stream_fds
= stream_fds
;
481 *ust_chanp
= ust_channel
;
487 for (j
= i
- 1; j
>= 0; j
--) {
490 closeret
= close(stream_fds
[j
]);
494 if (channel
->shm_path
[0]) {
495 char shm_path
[PATH_MAX
];
497 closeret
= get_stream_shm_path(shm_path
,
498 channel
->shm_path
, j
);
500 ERR("Cannot get stream shm path");
502 closeret
= run_as_unlink(shm_path
,
503 channel
->buffer_credentials
.value
.uid
,
504 channel
->buffer_credentials
.value
.gid
);
506 PERROR("unlink %s", shm_path
);
510 /* Try to rmdir all directories under shm_path root. */
511 if (channel
->root_shm_path
[0]) {
512 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
513 channel
->buffer_credentials
.value
.uid
,
514 channel
->buffer_credentials
.value
.gid
,
515 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
523 * Send a single given stream to the session daemon using the sock.
525 * Return 0 on success else a negative value.
527 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
534 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
536 /* Send stream to session daemon. */
537 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
547 * Send channel to sessiond and relayd if applicable.
549 * Return 0 on success or else a negative value.
551 static int send_channel_to_sessiond_and_relayd(int sock
,
552 struct lttng_consumer_channel
*channel
,
553 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
555 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
556 struct lttng_consumer_stream
*stream
;
557 uint64_t net_seq_idx
= -1ULL;
563 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
565 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
566 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
568 health_code_update();
570 /* Try to send the stream to the relayd if one is available. */
571 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
572 stream
->key
, channel
->name
);
573 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
576 * Flag that the relayd was the problem here probably due to a
577 * communicaton error on the socket.
582 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
584 if (net_seq_idx
== -1ULL) {
585 net_seq_idx
= stream
->net_seq_idx
;
590 /* Inform sessiond that we are about to send channel and streams. */
591 ret
= consumer_send_status_msg(sock
, ret_code
);
592 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
594 * Either the session daemon is not responding or the relayd died so we
600 /* Send channel to sessiond. */
601 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
606 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
611 /* The channel was sent successfully to the sessiond at this point. */
612 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
614 health_code_update();
616 /* Send stream to session daemon. */
617 ret
= send_sessiond_stream(sock
, stream
);
623 /* Tell sessiond there is no more stream. */
624 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
629 DBG("UST consumer NULL stream sent to sessiond");
634 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
641 * Creates a channel and streams and add the channel it to the channel internal
642 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
645 * Return 0 on success or else, a negative value is returned and the channel
646 * MUST be destroyed by consumer_del_channel().
648 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
649 struct lttng_consumer_channel
*channel
,
650 struct ustctl_consumer_channel_attr
*attr
)
659 * This value is still used by the kernel consumer since for the kernel,
660 * the stream ownership is not IN the consumer so we need to have the
661 * number of left stream that needs to be initialized so we can know when
662 * to delete the channel (see consumer.c).
664 * As for the user space tracer now, the consumer creates and sends the
665 * stream to the session daemon which only sends them to the application
666 * once every stream of a channel is received making this value useless
667 * because we they will be added to the poll thread before the application
668 * receives them. This ensures that a stream can not hang up during
669 * initilization of a channel.
671 channel
->nb_init_stream_left
= 0;
673 /* The reply msg status is handled in the following call. */
674 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
679 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
682 * For the snapshots (no monitor), we create the metadata streams
683 * on demand, not during the channel creation.
685 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
690 /* Open all streams for this channel. */
691 pthread_mutex_lock(&channel
->lock
);
692 ret
= create_ust_streams(channel
, ctx
);
693 pthread_mutex_unlock(&channel
->lock
);
703 * Send all stream of a channel to the right thread handling it.
705 * On error, return a negative value else 0 on success.
707 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
708 struct lttng_consumer_local_data
*ctx
)
711 struct lttng_consumer_stream
*stream
, *stmp
;
716 /* Send streams to the corresponding thread. */
717 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
720 health_code_update();
722 /* Sending the stream to the thread. */
723 ret
= send_stream_to_thread(stream
, ctx
);
726 * If we are unable to send the stream to the thread, there is
727 * a big problem so just stop everything.
738 * Flush channel's streams using the given key to retrieve the channel.
740 * Return 0 on success else an LTTng error code.
742 static int flush_channel(uint64_t chan_key
)
745 struct lttng_consumer_channel
*channel
;
746 struct lttng_consumer_stream
*stream
;
748 struct lttng_ht_iter iter
;
750 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
753 channel
= consumer_find_channel(chan_key
);
755 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
756 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
760 ht
= consumer_data
.stream_per_chan_id_ht
;
762 /* For each stream of the channel id, flush it. */
763 cds_lfht_for_each_entry_duplicate(ht
->ht
,
764 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
765 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
767 health_code_update();
769 pthread_mutex_lock(&stream
->lock
);
772 * Protect against concurrent teardown of a stream.
774 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
778 if (!stream
->quiescent
) {
779 ustctl_flush_buffer(stream
->ustream
, 0);
780 stream
->quiescent
= true;
783 pthread_mutex_unlock(&stream
->lock
);
791 * Clear quiescent state from channel's streams using the given key to
792 * retrieve the channel.
794 * Return 0 on success else an LTTng error code.
796 static int clear_quiescent_channel(uint64_t chan_key
)
799 struct lttng_consumer_channel
*channel
;
800 struct lttng_consumer_stream
*stream
;
802 struct lttng_ht_iter iter
;
804 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
807 channel
= consumer_find_channel(chan_key
);
809 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
810 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
814 ht
= consumer_data
.stream_per_chan_id_ht
;
816 /* For each stream of the channel id, clear quiescent state. */
817 cds_lfht_for_each_entry_duplicate(ht
->ht
,
818 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
819 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
821 health_code_update();
823 pthread_mutex_lock(&stream
->lock
);
824 stream
->quiescent
= false;
825 pthread_mutex_unlock(&stream
->lock
);
833 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
835 * Return 0 on success else an LTTng error code.
837 static int close_metadata(uint64_t chan_key
)
840 struct lttng_consumer_channel
*channel
;
841 unsigned int channel_monitor
;
843 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
845 channel
= consumer_find_channel(chan_key
);
848 * This is possible if the metadata thread has issue a delete because
849 * the endpoint point of the stream hung up. There is no way the
850 * session daemon can know about it thus use a DBG instead of an actual
853 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
854 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
858 pthread_mutex_lock(&consumer_data
.lock
);
859 pthread_mutex_lock(&channel
->lock
);
860 channel_monitor
= channel
->monitor
;
861 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
865 lttng_ustconsumer_close_metadata(channel
);
866 pthread_mutex_unlock(&channel
->lock
);
867 pthread_mutex_unlock(&consumer_data
.lock
);
870 * The ownership of a metadata channel depends on the type of
871 * session to which it belongs. In effect, the monitor flag is checked
872 * to determine if this metadata channel is in "snapshot" mode or not.
874 * In the non-snapshot case, the metadata channel is created along with
875 * a single stream which will remain present until the metadata channel
876 * is destroyed (on the destruction of its session). In this case, the
877 * metadata stream in "monitored" by the metadata poll thread and holds
878 * the ownership of its channel.
880 * Closing the metadata will cause the metadata stream's "metadata poll
881 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
882 * thread which will teardown the metadata stream which, in return,
883 * deletes the metadata channel.
885 * In the snapshot case, the metadata stream is created and destroyed
886 * on every snapshot record. Since the channel doesn't have an owner
887 * other than the session daemon, it is safe to destroy it immediately
888 * on reception of the CLOSE_METADATA command.
890 if (!channel_monitor
) {
892 * The channel and consumer_data locks must be
893 * released before this call since consumer_del_channel
894 * re-acquires the channel and consumer_data locks to teardown
895 * the channel and queue its reclamation by the "call_rcu"
898 consumer_del_channel(channel
);
903 pthread_mutex_unlock(&channel
->lock
);
904 pthread_mutex_unlock(&consumer_data
.lock
);
910 * RCU read side lock MUST be acquired before calling this function.
912 * Return 0 on success else an LTTng error code.
914 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
917 struct lttng_consumer_channel
*metadata
;
919 DBG("UST consumer setup metadata key %" PRIu64
, key
);
921 metadata
= consumer_find_channel(key
);
923 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
924 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
929 * In no monitor mode, the metadata channel has no stream(s) so skip the
930 * ownership transfer to the metadata thread.
932 if (!metadata
->monitor
) {
933 DBG("Metadata channel in no monitor");
939 * Send metadata stream to relayd if one available. Availability is
940 * known if the stream is still in the list of the channel.
942 if (cds_list_empty(&metadata
->streams
.head
)) {
943 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
944 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
945 goto error_no_stream
;
948 /* Send metadata stream to relayd if needed. */
949 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
950 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
953 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
956 ret
= consumer_send_relayd_streams_sent(
957 metadata
->metadata_stream
->net_seq_idx
);
959 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
965 * Ownership of metadata stream is passed along. Freeing is handled by
968 ret
= send_streams_to_thread(metadata
, ctx
);
971 * If we are unable to send the stream to the thread, there is
972 * a big problem so just stop everything.
974 ret
= LTTCOMM_CONSUMERD_FATAL
;
975 goto send_streams_error
;
977 /* List MUST be empty after or else it could be reused. */
978 assert(cds_list_empty(&metadata
->streams
.head
));
985 * Delete metadata channel on error. At this point, the metadata stream can
986 * NOT be monitored by the metadata thread thus having the guarantee that
987 * the stream is still in the local stream list of the channel. This call
988 * will make sure to clean that list.
990 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
991 cds_list_del(&metadata
->metadata_stream
->send_node
);
992 metadata
->metadata_stream
= NULL
;
1000 * Snapshot the whole metadata.
1001 * RCU read-side lock must be held by the caller.
1003 * Returns 0 on success, < 0 on error
1005 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
1006 uint64_t key
, char *path
, uint64_t relayd_id
,
1007 struct lttng_consumer_local_data
*ctx
)
1010 struct lttng_consumer_stream
*metadata_stream
;
1015 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1020 assert(!metadata_channel
->monitor
);
1022 health_code_update();
1025 * Ask the sessiond if we have new metadata waiting and update the
1026 * consumer metadata cache.
1028 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1033 health_code_update();
1036 * The metadata stream is NOT created in no monitor mode when the channel
1037 * is created on a sessiond ask channel command.
1039 ret
= create_ust_streams(metadata_channel
, ctx
);
1044 metadata_stream
= metadata_channel
->metadata_stream
;
1045 assert(metadata_stream
);
1047 pthread_mutex_lock(&metadata_stream
->lock
);
1048 if (relayd_id
!= (uint64_t) -1ULL) {
1049 metadata_stream
->net_seq_idx
= relayd_id
;
1050 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1052 ret
= consumer_stream_create_output_files(metadata_stream
,
1055 pthread_mutex_unlock(&metadata_stream
->lock
);
1061 health_code_update();
1063 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1071 * Clean up the stream completly because the next snapshot will use a new
1074 consumer_stream_destroy(metadata_stream
, NULL
);
1075 cds_list_del(&metadata_stream
->send_node
);
1076 metadata_channel
->metadata_stream
= NULL
;
1084 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1088 unsigned long mmap_offset
;
1089 const char *mmap_base
;
1091 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1093 ERR("Failed to get mmap base for stream `%s`",
1099 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1101 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1106 *addr
= mmap_base
+ mmap_offset
;
1113 * Take a snapshot of all the stream of a channel.
1114 * RCU read-side lock and the channel lock must be held by the caller.
1116 * Returns 0 on success, < 0 on error
1118 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1119 uint64_t key
, char *path
, uint64_t relayd_id
,
1120 uint64_t nb_packets_per_stream
,
1121 struct lttng_consumer_local_data
*ctx
)
1124 unsigned use_relayd
= 0;
1125 unsigned long consumed_pos
, produced_pos
;
1126 struct lttng_consumer_stream
*stream
;
1133 if (relayd_id
!= (uint64_t) -1ULL) {
1137 assert(!channel
->monitor
);
1138 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1140 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1141 health_code_update();
1143 /* Lock stream because we are about to change its state. */
1144 pthread_mutex_lock(&stream
->lock
);
1145 assert(channel
->trace_chunk
);
1146 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1148 * Can't happen barring an internal error as the channel
1149 * holds a reference to the trace chunk.
1151 ERR("Failed to acquire reference to channel's trace chunk");
1155 assert(!stream
->trace_chunk
);
1156 stream
->trace_chunk
= channel
->trace_chunk
;
1158 stream
->net_seq_idx
= relayd_id
;
1161 ret
= consumer_send_relayd_stream(stream
, path
);
1166 ret
= consumer_stream_create_output_files(stream
,
1171 DBG("UST consumer snapshot stream (%" PRIu64
")",
1176 * If tracing is active, we want to perform a "full" buffer flush.
1177 * Else, if quiescent, it has already been done by the prior stop.
1179 if (!stream
->quiescent
) {
1180 ustctl_flush_buffer(stream
->ustream
, 0);
1183 ret
= lttng_ustconsumer_take_snapshot(stream
);
1185 ERR("Taking UST snapshot");
1189 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1191 ERR("Produced UST snapshot position");
1195 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1197 ERR("Consumerd UST snapshot position");
1202 * The original value is sent back if max stream size is larger than
1203 * the possible size of the snapshot. Also, we assume that the session
1204 * daemon should never send a maximum stream size that is lower than
1207 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1208 produced_pos
, nb_packets_per_stream
,
1209 stream
->max_sb_size
);
1211 while ((long) (consumed_pos
- produced_pos
) < 0) {
1213 unsigned long len
, padded_len
;
1214 const char *subbuf_addr
;
1215 struct lttng_buffer_view subbuf_view
;
1217 health_code_update();
1219 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1221 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1223 if (ret
!= -EAGAIN
) {
1224 PERROR("ustctl_get_subbuf snapshot");
1225 goto error_close_stream
;
1227 DBG("UST consumer get subbuf failed. Skipping it.");
1228 consumed_pos
+= stream
->max_sb_size
;
1229 stream
->chan
->lost_packets
++;
1233 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1235 ERR("Snapshot ustctl_get_subbuf_size");
1236 goto error_put_subbuf
;
1239 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1241 ERR("Snapshot ustctl_get_padded_subbuf_size");
1242 goto error_put_subbuf
;
1245 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1247 goto error_put_subbuf
;
1250 subbuf_view
= lttng_buffer_view_init(
1251 subbuf_addr
, 0, padded_len
);
1252 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1253 stream
, &subbuf_view
, padded_len
- len
,
1256 if (read_len
!= len
) {
1258 goto error_put_subbuf
;
1261 if (read_len
!= padded_len
) {
1263 goto error_put_subbuf
;
1267 ret
= ustctl_put_subbuf(stream
->ustream
);
1269 ERR("Snapshot ustctl_put_subbuf");
1270 goto error_close_stream
;
1272 consumed_pos
+= stream
->max_sb_size
;
1275 /* Simply close the stream so we can use it on the next snapshot. */
1276 consumer_stream_close(stream
);
1277 pthread_mutex_unlock(&stream
->lock
);
1284 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1285 ERR("Snapshot ustctl_put_subbuf");
1288 consumer_stream_close(stream
);
1290 pthread_mutex_unlock(&stream
->lock
);
1296 * Receive the metadata updates from the sessiond. Supports receiving
1297 * overlapping metadata, but is needs to always belong to a contiguous
1298 * range starting from 0.
1299 * Be careful about the locks held when calling this function: it needs
1300 * the metadata cache flush to concurrently progress in order to
1303 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1304 uint64_t len
, uint64_t version
,
1305 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1307 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1310 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1312 metadata_str
= zmalloc(len
* sizeof(char));
1313 if (!metadata_str
) {
1314 PERROR("zmalloc metadata string");
1315 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1319 health_code_update();
1321 /* Receive metadata string. */
1322 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1324 /* Session daemon is dead so return gracefully. */
1329 health_code_update();
1331 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1332 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1335 /* Unable to handle metadata. Notify session daemon. */
1336 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1338 * Skip metadata flush on write error since the offset and len might
1339 * not have been updated which could create an infinite loop below when
1340 * waiting for the metadata cache to be flushed.
1342 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1345 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1350 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1351 DBG("Waiting for metadata to be flushed");
1353 health_code_update();
1355 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1365 * Receive command from session daemon and process it.
1367 * Return 1 on success else a negative value or 0.
1369 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1370 int sock
, struct pollfd
*consumer_sockpoll
)
1373 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1374 struct lttcomm_consumer_msg msg
;
1375 struct lttng_consumer_channel
*channel
= NULL
;
1377 health_code_update();
1379 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1380 if (ret
!= sizeof(msg
)) {
1381 DBG("Consumer received unexpected message size %zd (expects %zu)",
1384 * The ret value might 0 meaning an orderly shutdown but this is ok
1385 * since the caller handles this.
1388 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1394 health_code_update();
1397 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1399 health_code_update();
1401 /* relayd needs RCU read-side lock */
1404 switch (msg
.cmd_type
) {
1405 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1407 /* Session daemon status message are handled in the following call. */
1408 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1409 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1410 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1411 msg
.u
.relayd_sock
.relayd_session_id
);
1414 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1416 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1417 struct consumer_relayd_sock_pair
*relayd
;
1419 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1421 /* Get relayd reference if exists. */
1422 relayd
= consumer_find_relayd(index
);
1423 if (relayd
== NULL
) {
1424 DBG("Unable to find relayd %" PRIu64
, index
);
1425 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1429 * Each relayd socket pair has a refcount of stream attached to it
1430 * which tells if the relayd is still active or not depending on the
1433 * This will set the destroy flag of the relayd object and destroy it
1434 * if the refcount reaches zero when called.
1436 * The destroy can happen either here or when a stream fd hangs up.
1439 consumer_flag_relayd_for_destroy(relayd
);
1442 goto end_msg_sessiond
;
1444 case LTTNG_CONSUMER_UPDATE_STREAM
:
1449 case LTTNG_CONSUMER_DATA_PENDING
:
1451 int ret
, is_data_pending
;
1452 uint64_t id
= msg
.u
.data_pending
.session_id
;
1454 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1456 is_data_pending
= consumer_data_pending(id
);
1458 /* Send back returned value to session daemon */
1459 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1460 sizeof(is_data_pending
));
1462 DBG("Error when sending the data pending ret code: %d", ret
);
1467 * No need to send back a status message since the data pending
1468 * returned value is the response.
1472 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1475 struct ustctl_consumer_channel_attr attr
;
1476 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1477 const struct lttng_credentials buffer_credentials
= {
1478 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1479 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1482 /* Create a plain object and reserve a channel key. */
1483 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
1484 msg
.u
.ask_channel
.chunk_id
.is_set
?
1486 msg
.u
.ask_channel
.pathname
,
1487 msg
.u
.ask_channel
.name
,
1488 msg
.u
.ask_channel
.relayd_id
,
1489 msg
.u
.ask_channel
.key
,
1490 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1491 msg
.u
.ask_channel
.tracefile_size
,
1492 msg
.u
.ask_channel
.tracefile_count
,
1493 msg
.u
.ask_channel
.session_id_per_pid
,
1494 msg
.u
.ask_channel
.monitor
,
1495 msg
.u
.ask_channel
.live_timer_interval
,
1496 msg
.u
.ask_channel
.root_shm_path
,
1497 msg
.u
.ask_channel
.shm_path
);
1499 goto end_channel_error
;
1502 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1503 buffer_credentials
);
1506 * Assign UST application UID to the channel. This value is ignored for
1507 * per PID buffers. This is specific to UST thus setting this after the
1510 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1512 /* Build channel attributes from received message. */
1513 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1514 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1515 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1516 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1517 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1518 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1519 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1520 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1522 /* Match channel buffer type to the UST abi. */
1523 switch (msg
.u
.ask_channel
.output
) {
1524 case LTTNG_EVENT_MMAP
:
1526 attr
.output
= LTTNG_UST_MMAP
;
1530 /* Translate and save channel type. */
1531 switch (msg
.u
.ask_channel
.type
) {
1532 case LTTNG_UST_CHAN_PER_CPU
:
1533 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1534 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1536 * Set refcount to 1 for owner. Below, we will
1537 * pass ownership to the
1538 * consumer_thread_channel_poll() thread.
1540 channel
->refcount
= 1;
1542 case LTTNG_UST_CHAN_METADATA
:
1543 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1544 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1551 health_code_update();
1553 ret
= ask_channel(ctx
, channel
, &attr
);
1555 goto end_channel_error
;
1558 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1559 ret
= consumer_metadata_cache_allocate(channel
);
1561 ERR("Allocating metadata cache");
1562 goto end_channel_error
;
1564 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1565 attr
.switch_timer_interval
= 0;
1567 int monitor_start_ret
;
1569 consumer_timer_live_start(channel
,
1570 msg
.u
.ask_channel
.live_timer_interval
);
1571 monitor_start_ret
= consumer_timer_monitor_start(
1573 msg
.u
.ask_channel
.monitor_timer_interval
);
1574 if (monitor_start_ret
< 0) {
1575 ERR("Starting channel monitoring timer failed");
1576 goto end_channel_error
;
1580 health_code_update();
1583 * Add the channel to the internal state AFTER all streams were created
1584 * and successfully sent to session daemon. This way, all streams must
1585 * be ready before this channel is visible to the threads.
1586 * If add_channel succeeds, ownership of the channel is
1587 * passed to consumer_thread_channel_poll().
1589 ret
= add_channel(channel
, ctx
);
1591 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1592 if (channel
->switch_timer_enabled
== 1) {
1593 consumer_timer_switch_stop(channel
);
1595 consumer_metadata_cache_destroy(channel
);
1597 if (channel
->live_timer_enabled
== 1) {
1598 consumer_timer_live_stop(channel
);
1600 if (channel
->monitor_timer_enabled
== 1) {
1601 consumer_timer_monitor_stop(channel
);
1603 goto end_channel_error
;
1606 health_code_update();
1609 * Channel and streams are now created. Inform the session daemon that
1610 * everything went well and should wait to receive the channel and
1611 * streams with ustctl API.
1613 ret
= consumer_send_status_channel(sock
, channel
);
1616 * There is probably a problem on the socket.
1623 case LTTNG_CONSUMER_GET_CHANNEL
:
1625 int ret
, relayd_err
= 0;
1626 uint64_t key
= msg
.u
.get_channel
.key
;
1627 struct lttng_consumer_channel
*channel
;
1629 channel
= consumer_find_channel(key
);
1631 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1632 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1633 goto end_get_channel
;
1636 health_code_update();
1638 /* Send the channel to sessiond (and relayd, if applicable). */
1639 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1644 * We were unable to send to the relayd the stream so avoid
1645 * sending back a fatal error to the thread since this is OK
1646 * and the consumer can continue its work. The above call
1647 * has sent the error status message to the sessiond.
1649 goto end_get_channel_nosignal
;
1652 * The communicaton was broken hence there is a bad state between
1653 * the consumer and sessiond so stop everything.
1655 goto error_get_channel_fatal
;
1658 health_code_update();
1661 * In no monitor mode, the streams ownership is kept inside the channel
1662 * so don't send them to the data thread.
1664 if (!channel
->monitor
) {
1665 goto end_get_channel
;
1668 ret
= send_streams_to_thread(channel
, ctx
);
1671 * If we are unable to send the stream to the thread, there is
1672 * a big problem so just stop everything.
1674 goto error_get_channel_fatal
;
1676 /* List MUST be empty after or else it could be reused. */
1677 assert(cds_list_empty(&channel
->streams
.head
));
1679 goto end_msg_sessiond
;
1680 error_get_channel_fatal
:
1682 end_get_channel_nosignal
:
1685 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1687 uint64_t key
= msg
.u
.destroy_channel
.key
;
1690 * Only called if streams have not been sent to stream
1691 * manager thread. However, channel has been sent to
1692 * channel manager thread.
1694 notify_thread_del_channel(ctx
, key
);
1695 goto end_msg_sessiond
;
1697 case LTTNG_CONSUMER_CLOSE_METADATA
:
1701 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1706 goto end_msg_sessiond
;
1708 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1712 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1717 goto end_msg_sessiond
;
1719 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1723 ret
= clear_quiescent_channel(
1724 msg
.u
.clear_quiescent_channel
.key
);
1729 goto end_msg_sessiond
;
1731 case LTTNG_CONSUMER_PUSH_METADATA
:
1734 uint64_t len
= msg
.u
.push_metadata
.len
;
1735 uint64_t key
= msg
.u
.push_metadata
.key
;
1736 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1737 uint64_t version
= msg
.u
.push_metadata
.version
;
1738 struct lttng_consumer_channel
*channel
;
1740 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1743 channel
= consumer_find_channel(key
);
1746 * This is possible if the metadata creation on the consumer side
1747 * is in flight vis-a-vis a concurrent push metadata from the
1748 * session daemon. Simply return that the channel failed and the
1749 * session daemon will handle that message correctly considering
1750 * that this race is acceptable thus the DBG() statement here.
1752 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1753 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1754 goto end_push_metadata_msg_sessiond
;
1757 health_code_update();
1761 * There is nothing to receive. We have simply
1762 * checked whether the channel can be found.
1764 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1765 goto end_push_metadata_msg_sessiond
;
1768 /* Tell session daemon we are ready to receive the metadata. */
1769 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1771 /* Somehow, the session daemon is not responding anymore. */
1772 goto error_push_metadata_fatal
;
1775 health_code_update();
1777 /* Wait for more data. */
1778 health_poll_entry();
1779 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1782 goto error_push_metadata_fatal
;
1785 health_code_update();
1787 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1788 len
, version
, channel
, 0, 1);
1790 /* error receiving from sessiond */
1791 goto error_push_metadata_fatal
;
1794 goto end_push_metadata_msg_sessiond
;
1796 end_push_metadata_msg_sessiond
:
1797 goto end_msg_sessiond
;
1798 error_push_metadata_fatal
:
1801 case LTTNG_CONSUMER_SETUP_METADATA
:
1805 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1809 goto end_msg_sessiond
;
1811 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1813 struct lttng_consumer_channel
*channel
;
1814 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1816 channel
= consumer_find_channel(key
);
1818 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1819 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1821 if (msg
.u
.snapshot_channel
.metadata
) {
1822 ret
= snapshot_metadata(channel
, key
,
1823 msg
.u
.snapshot_channel
.pathname
,
1824 msg
.u
.snapshot_channel
.relayd_id
,
1827 ERR("Snapshot metadata failed");
1828 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1831 ret
= snapshot_channel(channel
, key
,
1832 msg
.u
.snapshot_channel
.pathname
,
1833 msg
.u
.snapshot_channel
.relayd_id
,
1834 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1837 ERR("Snapshot channel failed");
1838 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1842 health_code_update();
1843 ret
= consumer_send_status_msg(sock
, ret_code
);
1845 /* Somehow, the session daemon is not responding anymore. */
1848 health_code_update();
1851 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1854 uint64_t discarded_events
;
1855 struct lttng_ht_iter iter
;
1856 struct lttng_ht
*ht
;
1857 struct lttng_consumer_stream
*stream
;
1858 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1859 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1861 DBG("UST consumer discarded events command for session id %"
1864 pthread_mutex_lock(&consumer_data
.lock
);
1866 ht
= consumer_data
.stream_list_ht
;
1869 * We only need a reference to the channel, but they are not
1870 * directly indexed, so we just use the first matching stream
1871 * to extract the information we need, we default to 0 if not
1872 * found (no events are dropped if the channel is not yet in
1875 discarded_events
= 0;
1876 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1877 ht
->hash_fct(&id
, lttng_ht_seed
),
1879 &iter
.iter
, stream
, node_session_id
.node
) {
1880 if (stream
->chan
->key
== key
) {
1881 discarded_events
= stream
->chan
->discarded_events
;
1885 pthread_mutex_unlock(&consumer_data
.lock
);
1888 DBG("UST consumer discarded events command for session id %"
1889 PRIu64
", channel key %" PRIu64
, id
, key
);
1891 health_code_update();
1893 /* Send back returned value to session daemon */
1894 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1896 PERROR("send discarded events");
1902 case LTTNG_CONSUMER_LOST_PACKETS
:
1905 uint64_t lost_packets
;
1906 struct lttng_ht_iter iter
;
1907 struct lttng_ht
*ht
;
1908 struct lttng_consumer_stream
*stream
;
1909 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1910 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1912 DBG("UST consumer lost packets command for session id %"
1915 pthread_mutex_lock(&consumer_data
.lock
);
1917 ht
= consumer_data
.stream_list_ht
;
1920 * We only need a reference to the channel, but they are not
1921 * directly indexed, so we just use the first matching stream
1922 * to extract the information we need, we default to 0 if not
1923 * found (no packets lost if the channel is not yet in use).
1926 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1927 ht
->hash_fct(&id
, lttng_ht_seed
),
1929 &iter
.iter
, stream
, node_session_id
.node
) {
1930 if (stream
->chan
->key
== key
) {
1931 lost_packets
= stream
->chan
->lost_packets
;
1935 pthread_mutex_unlock(&consumer_data
.lock
);
1938 DBG("UST consumer lost packets command for session id %"
1939 PRIu64
", channel key %" PRIu64
, id
, key
);
1941 health_code_update();
1943 /* Send back returned value to session daemon */
1944 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1945 sizeof(lost_packets
));
1947 PERROR("send lost packets");
1953 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1955 int channel_monitor_pipe
;
1957 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1958 /* Successfully received the command's type. */
1959 ret
= consumer_send_status_msg(sock
, ret_code
);
1964 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1966 if (ret
!= sizeof(channel_monitor_pipe
)) {
1967 ERR("Failed to receive channel monitor pipe");
1971 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1972 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1973 channel_monitor_pipe
);
1977 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1978 /* Set the pipe as non-blocking. */
1979 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1981 PERROR("fcntl get flags of the channel monitoring pipe");
1986 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1987 flags
| O_NONBLOCK
);
1989 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1992 DBG("Channel monitor pipe set as non-blocking");
1994 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1996 goto end_msg_sessiond
;
1998 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2000 struct lttng_consumer_channel
*channel
;
2001 uint64_t key
= msg
.u
.rotate_channel
.key
;
2003 channel
= consumer_find_channel(key
);
2005 DBG("Channel %" PRIu64
" not found", key
);
2006 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2009 * Sample the rotate position of all the streams in
2012 ret
= lttng_consumer_rotate_channel(channel
, key
,
2013 msg
.u
.rotate_channel
.relayd_id
,
2014 msg
.u
.rotate_channel
.metadata
,
2017 ERR("Rotate channel failed");
2018 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2021 health_code_update();
2023 ret
= consumer_send_status_msg(sock
, ret_code
);
2025 /* Somehow, the session daemon is not responding anymore. */
2026 goto end_rotate_channel_nosignal
;
2030 * Rotate the streams that are ready right now.
2031 * FIXME: this is a second consecutive iteration over the
2032 * streams in a channel, there is probably a better way to
2033 * handle this, but it needs to be after the
2034 * consumer_send_status_msg() call.
2037 ret
= lttng_consumer_rotate_ready_streams(
2040 ERR("Rotate channel failed");
2044 end_rotate_channel_nosignal
:
2047 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2049 struct lttng_consumer_channel
*channel
;
2050 uint64_t key
= msg
.u
.clear_channel
.key
;
2052 channel
= consumer_find_channel(key
);
2054 DBG("Channel %" PRIu64
" not found", key
);
2055 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2057 ret
= lttng_consumer_clear_channel(channel
);
2059 ERR("Clear channel failed key %" PRIu64
, key
);
2063 health_code_update();
2065 ret
= consumer_send_status_msg(sock
, ret_code
);
2067 /* Somehow, the session daemon is not responding anymore. */
2072 case LTTNG_CONSUMER_INIT
:
2074 ret_code
= lttng_consumer_init_command(ctx
,
2075 msg
.u
.init
.sessiond_uuid
);
2076 health_code_update();
2077 ret
= consumer_send_status_msg(sock
, ret_code
);
2079 /* Somehow, the session daemon is not responding anymore. */
2084 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2086 const struct lttng_credentials credentials
= {
2087 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2088 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2090 const bool is_local_trace
=
2091 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2092 const uint64_t relayd_id
=
2093 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2094 const char *chunk_override_name
=
2095 *msg
.u
.create_trace_chunk
.override_name
?
2096 msg
.u
.create_trace_chunk
.override_name
:
2098 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2101 * The session daemon will only provide a chunk directory file
2102 * descriptor for local traces.
2104 if (is_local_trace
) {
2107 /* Acnowledge the reception of the command. */
2108 ret
= consumer_send_status_msg(sock
,
2109 LTTCOMM_CONSUMERD_SUCCESS
);
2111 /* Somehow, the session daemon is not responding anymore. */
2116 * Receive trace chunk domain dirfd.
2118 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2119 if (ret
!= sizeof(chunk_dirfd
)) {
2120 ERR("Failed to receive trace chunk domain directory file descriptor");
2124 DBG("Received trace chunk domain directory fd (%d)",
2126 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2128 if (!chunk_directory_handle
) {
2129 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2130 if (close(chunk_dirfd
)) {
2131 PERROR("Failed to close chunk directory file descriptor");
2137 ret_code
= lttng_consumer_create_trace_chunk(
2138 !is_local_trace
? &relayd_id
: NULL
,
2139 msg
.u
.create_trace_chunk
.session_id
,
2140 msg
.u
.create_trace_chunk
.chunk_id
,
2141 (time_t) msg
.u
.create_trace_chunk
2142 .creation_timestamp
,
2143 chunk_override_name
,
2144 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2147 chunk_directory_handle
);
2148 lttng_directory_handle_put(chunk_directory_handle
);
2149 goto end_msg_sessiond
;
2151 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2153 enum lttng_trace_chunk_command_type close_command
=
2154 msg
.u
.close_trace_chunk
.close_command
.value
;
2155 const uint64_t relayd_id
=
2156 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2157 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2158 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2161 ret_code
= lttng_consumer_close_trace_chunk(
2162 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2165 msg
.u
.close_trace_chunk
.session_id
,
2166 msg
.u
.close_trace_chunk
.chunk_id
,
2167 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2168 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2170 NULL
, closed_trace_chunk_path
);
2171 reply
.ret_code
= ret_code
;
2172 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2173 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2174 if (ret
!= sizeof(reply
)) {
2177 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2179 if (ret
!= reply
.path_length
) {
2184 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2186 const uint64_t relayd_id
=
2187 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2189 ret_code
= lttng_consumer_trace_chunk_exists(
2190 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2192 msg
.u
.trace_chunk_exists
.session_id
,
2193 msg
.u
.trace_chunk_exists
.chunk_id
);
2194 goto end_msg_sessiond
;
2202 * Return 1 to indicate success since the 0 value can be a socket
2203 * shutdown during the recv() or send() call.
2210 * The returned value here is not useful since either way we'll return 1 to
2211 * the caller because the session daemon socket management is done
2212 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2214 ret
= consumer_send_status_msg(sock
, ret_code
);
2224 * Free channel here since no one has a reference to it. We don't
2225 * free after that because a stream can store this pointer.
2227 destroy_channel(channel
);
2229 /* We have to send a status channel message indicating an error. */
2230 ret
= consumer_send_status_channel(sock
, NULL
);
2232 /* Stop everything if session daemon can not be notified. */
2239 /* This will issue a consumer stop. */
2245 health_code_update();
2249 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2250 int producer_active
)
2253 assert(stream
->ustream
);
2255 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2259 * Take a snapshot for a specific stream.
2261 * Returns 0 on success, < 0 on error
2263 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2266 assert(stream
->ustream
);
2268 return ustctl_snapshot(stream
->ustream
);
2272 * Sample consumed and produced positions for a specific stream.
2274 * Returns 0 on success, < 0 on error.
2276 int lttng_ustconsumer_sample_snapshot_positions(
2277 struct lttng_consumer_stream
*stream
)
2280 assert(stream
->ustream
);
2282 return ustctl_snapshot_sample_positions(stream
->ustream
);
2286 * Get the produced position
2288 * Returns 0 on success, < 0 on error
2290 int lttng_ustconsumer_get_produced_snapshot(
2291 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2294 assert(stream
->ustream
);
2297 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2301 * Get the consumed position
2303 * Returns 0 on success, < 0 on error
2305 int lttng_ustconsumer_get_consumed_snapshot(
2306 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2309 assert(stream
->ustream
);
2312 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2315 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2319 assert(stream
->ustream
);
2321 ustctl_flush_buffer(stream
->ustream
, producer
);
2324 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2327 assert(stream
->ustream
);
2329 ustctl_clear_buffer(stream
->ustream
);
2332 int lttng_ustconsumer_get_current_timestamp(
2333 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2336 assert(stream
->ustream
);
2339 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2342 int lttng_ustconsumer_get_sequence_number(
2343 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2346 assert(stream
->ustream
);
2349 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2353 * Called when the stream signals the consumer that it has hung up.
2355 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2358 assert(stream
->ustream
);
2360 pthread_mutex_lock(&stream
->lock
);
2361 if (!stream
->quiescent
) {
2362 ustctl_flush_buffer(stream
->ustream
, 0);
2363 stream
->quiescent
= true;
2365 pthread_mutex_unlock(&stream
->lock
);
2366 stream
->hangup_flush_done
= 1;
2369 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2374 assert(chan
->uchan
);
2375 assert(chan
->buffer_credentials
.is_set
);
2377 if (chan
->switch_timer_enabled
== 1) {
2378 consumer_timer_switch_stop(chan
);
2380 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2383 ret
= close(chan
->stream_fds
[i
]);
2387 if (chan
->shm_path
[0]) {
2388 char shm_path
[PATH_MAX
];
2390 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2392 ERR("Cannot get stream shm path");
2394 ret
= run_as_unlink(shm_path
,
2395 chan
->buffer_credentials
.value
.uid
,
2396 chan
->buffer_credentials
.value
.gid
);
2398 PERROR("unlink %s", shm_path
);
2404 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2407 assert(chan
->uchan
);
2408 assert(chan
->buffer_credentials
.is_set
);
2410 consumer_metadata_cache_destroy(chan
);
2411 ustctl_destroy_channel(chan
->uchan
);
2412 /* Try to rmdir all directories under shm_path root. */
2413 if (chan
->root_shm_path
[0]) {
2414 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2415 chan
->buffer_credentials
.value
.uid
,
2416 chan
->buffer_credentials
.value
.gid
,
2417 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2419 free(chan
->stream_fds
);
2422 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2425 assert(stream
->ustream
);
2427 if (stream
->chan
->switch_timer_enabled
== 1) {
2428 consumer_timer_switch_stop(stream
->chan
);
2430 ustctl_destroy_stream(stream
->ustream
);
2433 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2436 assert(stream
->ustream
);
2438 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2441 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2444 assert(stream
->ustream
);
2446 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2450 * Populate index values of a UST stream. Values are set in big endian order.
2452 * Return 0 on success or else a negative value.
2454 static int get_index_values(struct ctf_packet_index
*index
,
2455 struct ustctl_consumer_stream
*ustream
)
2458 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
2459 events_discarded
, stream_id
, stream_instance_id
,
2462 ret
= ustctl_get_timestamp_begin(ustream
, ×tamp_begin
);
2464 PERROR("ustctl_get_timestamp_begin");
2468 ret
= ustctl_get_timestamp_end(ustream
, ×tamp_end
);
2470 PERROR("ustctl_get_timestamp_end");
2474 ret
= ustctl_get_events_discarded(ustream
, &events_discarded
);
2476 PERROR("ustctl_get_events_discarded");
2480 ret
= ustctl_get_content_size(ustream
, &content_size
);
2482 PERROR("ustctl_get_content_size");
2486 ret
= ustctl_get_packet_size(ustream
, &packet_size
);
2488 PERROR("ustctl_get_packet_size");
2492 ret
= ustctl_get_stream_id(ustream
, &stream_id
);
2494 PERROR("ustctl_get_stream_id");
2498 ret
= ustctl_get_instance_id(ustream
, &stream_instance_id
);
2500 PERROR("ustctl_get_instance_id");
2504 ret
= ustctl_get_sequence_number(ustream
, &packet_seq_num
);
2506 PERROR("ustctl_get_sequence_number");
2510 *index
= (typeof(*index
)) {
2511 .offset
= index
->offset
,
2512 .packet_size
= htobe64(packet_size
),
2513 .content_size
= htobe64(content_size
),
2514 .timestamp_begin
= htobe64(timestamp_begin
),
2515 .timestamp_end
= htobe64(timestamp_end
),
2516 .events_discarded
= htobe64(events_discarded
),
2517 .stream_id
= htobe64(stream_id
),
2518 .stream_instance_id
= htobe64(stream_instance_id
),
2519 .packet_seq_num
= htobe64(packet_seq_num
),
2527 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2528 struct consumer_metadata_cache
*cache
)
2530 DBG("Metadata stream update to version %" PRIu64
,
2532 stream
->ust_metadata_pushed
= 0;
2533 stream
->metadata_version
= cache
->version
;
2534 stream
->reset_metadata_flag
= 1;
2538 * Check if the version of the metadata stream and metadata cache match.
2539 * If the cache got updated, reset the metadata stream.
2540 * The stream lock and metadata cache lock MUST be held.
2541 * Return 0 on success, a negative value on error.
2544 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2547 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2549 if (cache
->version
== stream
->metadata_version
) {
2552 metadata_stream_reset_cache(stream
, cache
);
2559 * Write up to one packet from the metadata cache to the channel.
2561 * Returns the number of bytes pushed in the cache, or a negative value
2565 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2570 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2571 ret
= metadata_stream_check_version(stream
);
2575 if (stream
->chan
->metadata_cache
->max_offset
2576 == stream
->ust_metadata_pushed
) {
2581 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2582 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2583 stream
->chan
->metadata_cache
->max_offset
2584 - stream
->ust_metadata_pushed
);
2585 assert(write_len
!= 0);
2586 if (write_len
< 0) {
2587 ERR("Writing one metadata packet");
2591 stream
->ust_metadata_pushed
+= write_len
;
2593 assert(stream
->chan
->metadata_cache
->max_offset
>=
2594 stream
->ust_metadata_pushed
);
2598 * Switch packet (but don't open the next one) on every commit of
2599 * a metadata packet. Since the subbuffer is fully filled (with padding,
2600 * if needed), the stream is "quiescent" after this commit.
2602 ustctl_flush_buffer(stream
->ustream
, 1);
2603 stream
->quiescent
= true;
2605 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2611 * Sync metadata meaning request them to the session daemon and snapshot to the
2612 * metadata thread can consumer them.
2614 * Metadata stream lock is held here, but we need to release it when
2615 * interacting with sessiond, else we cause a deadlock with live
2616 * awaiting on metadata to be pushed out.
2618 * The RCU read side lock must be held by the caller.
2620 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2621 * is empty or a negative value on error.
2623 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2624 struct lttng_consumer_stream
*metadata_stream
)
2628 struct lttng_consumer_channel
*metadata_channel
;
2631 assert(metadata_stream
);
2633 metadata_channel
= metadata_stream
->chan
;
2634 pthread_mutex_unlock(&metadata_stream
->lock
);
2636 * Request metadata from the sessiond, but don't wait for the flush
2637 * because we locked the metadata thread.
2639 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2640 pthread_mutex_lock(&metadata_stream
->lock
);
2646 * The metadata stream and channel can be deleted while the
2647 * metadata stream lock was released. The streamed is checked
2648 * for deletion before we use it further.
2650 * Note that it is safe to access a logically-deleted stream since its
2651 * existence is still guaranteed by the RCU read side lock. However,
2652 * it should no longer be used. The close/deletion of the metadata
2653 * channel and stream already guarantees that all metadata has been
2654 * consumed. Therefore, there is nothing left to do in this function.
2656 if (consumer_stream_is_deleted(metadata_stream
)) {
2657 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2658 metadata_stream
->key
);
2663 ret
= commit_one_metadata_packet(metadata_stream
);
2666 } else if (ret
> 0) {
2670 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2672 if (errno
!= EAGAIN
) {
2673 ERR("Sync metadata, taking UST snapshot");
2676 DBG("No new metadata when syncing them.");
2677 /* No new metadata, exit. */
2683 * After this flush, we still need to extract metadata.
2694 * Return 0 on success else a negative value.
2696 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2697 struct lttng_consumer_local_data
*ctx
)
2700 struct ustctl_consumer_stream
*ustream
;
2705 ustream
= stream
->ustream
;
2708 * First, we are going to check if there is a new subbuffer available
2709 * before reading the stream wait_fd.
2711 /* Get the next subbuffer */
2712 ret
= ustctl_get_next_subbuf(ustream
);
2714 /* No more data found, flag the stream. */
2715 stream
->has_data
= 0;
2720 ret
= ustctl_put_subbuf(ustream
);
2723 /* This stream still has data. Flag it and wake up the data thread. */
2724 stream
->has_data
= 1;
2726 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2729 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2730 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2735 /* The wake up pipe has been notified. */
2736 ctx
->has_wakeup
= 1;
2745 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2748 uint64_t seq
, discarded
;
2750 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2752 PERROR("ustctl_get_sequence_number");
2756 * Start the sequence when we extract the first packet in case we don't
2757 * start at 0 (for example if a consumer is not connected to the
2758 * session immediately after the beginning).
2760 if (stream
->last_sequence_number
== -1ULL) {
2761 stream
->last_sequence_number
= seq
;
2762 } else if (seq
> stream
->last_sequence_number
) {
2763 stream
->chan
->lost_packets
+= seq
-
2764 stream
->last_sequence_number
- 1;
2766 /* seq <= last_sequence_number */
2767 ERR("Sequence number inconsistent : prev = %" PRIu64
2768 ", current = %" PRIu64
,
2769 stream
->last_sequence_number
, seq
);
2773 stream
->last_sequence_number
= seq
;
2775 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2777 PERROR("kernctl_get_events_discarded");
2780 if (discarded
< stream
->last_discarded_events
) {
2782 * Overflow has occurred. We assume only one wrap-around
2785 stream
->chan
->discarded_events
+=
2786 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2787 stream
->last_discarded_events
+ discarded
;
2789 stream
->chan
->discarded_events
+= discarded
-
2790 stream
->last_discarded_events
;
2792 stream
->last_discarded_events
= discarded
;
2800 * Read subbuffer from the given stream.
2802 * Stream and channel locks MUST be acquired by the caller.
2804 * Return 0 on success else a negative value.
2806 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2807 struct lttng_consumer_local_data
*ctx
)
2809 unsigned long len
, subbuf_size
, padding
;
2810 int err
, write_index
= 1, rotation_ret
;
2812 struct ustctl_consumer_stream
*ustream
;
2813 struct ctf_packet_index index
;
2814 const char *subbuf_addr
;
2815 struct lttng_buffer_view subbuf_view
;
2818 assert(stream
->ustream
);
2821 DBG("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
2824 /* Ease our life for what's next. */
2825 ustream
= stream
->ustream
;
2828 * We can consume the 1 byte written into the wait_fd by UST. Don't trigger
2829 * error if we cannot read this one byte (read returns 0), or if the error
2830 * is EAGAIN or EWOULDBLOCK.
2832 * This is only done when the stream is monitored by a thread, before the
2833 * flush is done after a hangup and if the stream is not flagged with data
2834 * since there might be nothing to consume in the wait fd but still have
2835 * data available flagged by the consumer wake up pipe.
2837 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2841 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2842 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2849 * If the stream was flagged to be ready for rotation before we extract the
2850 * next packet, rotate it now.
2852 if (stream
->rotate_ready
) {
2853 DBG("Rotate stream before extracting data");
2854 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
2855 if (rotation_ret
< 0) {
2856 ERR("Stream rotation error");
2863 /* Get the next subbuffer */
2864 err
= ustctl_get_next_subbuf(ustream
);
2867 * Populate metadata info if the existing info has
2868 * already been read.
2870 if (stream
->metadata_flag
) {
2871 ret
= commit_one_metadata_packet(stream
);
2878 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2880 * This is a debug message even for single-threaded consumer,
2881 * because poll() have more relaxed criterions than get subbuf,
2882 * so get_subbuf may fail for short race windows where poll()
2883 * would issue wakeups.
2885 DBG("Reserving sub buffer failed (everything is normal, "
2886 "it is due to concurrency) [ret: %d]", err
);
2889 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2891 if (!stream
->metadata_flag
) {
2892 index
.offset
= htobe64(stream
->out_fd_offset
);
2893 ret
= get_index_values(&index
, ustream
);
2895 err
= ustctl_put_subbuf(ustream
);
2900 /* Update the stream's sequence and discarded events count. */
2901 ret
= update_stream_stats(stream
);
2903 PERROR("kernctl_get_events_discarded");
2904 err
= ustctl_put_subbuf(ustream
);
2912 /* Get the full padded subbuffer size */
2913 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2916 /* Get subbuffer data size (without padding) */
2917 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2920 /* Make sure we don't get a subbuffer size bigger than the padded */
2921 assert(len
>= subbuf_size
);
2923 padding
= len
- subbuf_size
;
2925 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
2928 goto error_put_subbuf
;
2931 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
2933 /* write the subbuffer to the tracefile */
2934 ret
= lttng_consumer_on_read_subbuffer_mmap(
2935 ctx
, stream
, &subbuf_view
, padding
, &index
);
2937 * The mmap operation should write subbuf_size amount of data when
2938 * network streaming or the full padding (len) size when we are _not_
2941 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2942 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2944 * Display the error but continue processing to try to release the
2945 * subbuffer. This is a DBG statement since any unexpected kill or
2946 * signal, the application gets unregistered, relayd gets closed or
2947 * anything that affects the buffer lifetime will trigger this error.
2948 * So, for the sake of the user, don't print this error since it can
2949 * happen and it is OK with the code flow.
2951 DBG("Error writing to tracefile "
2952 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2953 ret
, len
, subbuf_size
);
2957 err
= ustctl_put_next_subbuf(ustream
);
2961 * This will consumer the byte on the wait_fd if and only if there is not
2962 * next subbuffer to be acquired.
2964 if (!stream
->metadata_flag
) {
2965 ret
= notify_if_more_data(stream
, ctx
);
2971 /* Write index if needed. */
2976 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2978 * In live, block until all the metadata is sent.
2980 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2981 assert(!stream
->missed_metadata_flush
);
2982 stream
->waiting_on_metadata
= true;
2983 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2985 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2987 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2988 stream
->waiting_on_metadata
= false;
2989 if (stream
->missed_metadata_flush
) {
2990 stream
->missed_metadata_flush
= false;
2991 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2992 (void) consumer_flush_ust_index(stream
);
2994 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
3002 assert(!stream
->metadata_flag
);
3003 err
= consumer_stream_write_index(stream
, &index
);
3010 * After extracting the packet, we check if the stream is now ready to be
3011 * rotated and perform the action immediately.
3013 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3014 if (rotation_ret
== 1) {
3015 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3016 if (rotation_ret
< 0) {
3017 ERR("Stream rotation error");
3021 } else if (rotation_ret
< 0) {
3022 ERR("Checking if stream is ready to rotate");
3031 * Called when a stream is created.
3033 * Return 0 on success or else a negative value.
3035 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3042 * Don't create anything if this is set for streaming or if there is
3043 * no current trace chunk on the parent channel.
3045 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3046 stream
->chan
->trace_chunk
) {
3047 ret
= consumer_stream_create_output_files(stream
, true);
3059 * Check if data is still being extracted from the buffers for a specific
3060 * stream. Consumer data lock MUST be acquired before calling this function
3061 * and the stream lock.
3063 * Return 1 if the traced data are still getting read else 0 meaning that the
3064 * data is available for trace viewer reading.
3066 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3071 assert(stream
->ustream
);
3073 DBG("UST consumer checking data pending");
3075 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3080 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3081 uint64_t contiguous
, pushed
;
3083 /* Ease our life a bit. */
3084 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3085 pushed
= stream
->ust_metadata_pushed
;
3088 * We can simply check whether all contiguously available data
3089 * has been pushed to the ring buffer, since the push operation
3090 * is performed within get_next_subbuf(), and because both
3091 * get_next_subbuf() and put_next_subbuf() are issued atomically
3092 * thanks to the stream lock within
3093 * lttng_ustconsumer_read_subbuffer(). This basically means that
3094 * whetnever ust_metadata_pushed is incremented, the associated
3095 * metadata has been consumed from the metadata stream.
3097 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3098 contiguous
, pushed
);
3099 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3100 if ((contiguous
!= pushed
) ||
3101 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3102 ret
= 1; /* Data is pending */
3106 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3109 * There is still data so let's put back this
3112 ret
= ustctl_put_subbuf(stream
->ustream
);
3114 ret
= 1; /* Data is pending */
3119 /* Data is NOT pending so ready to be read. */
3127 * Stop a given metadata channel timer if enabled and close the wait fd which
3128 * is the poll pipe of the metadata stream.
3130 * This MUST be called with the metadata channel lock acquired.
3132 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3137 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3139 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3141 if (metadata
->switch_timer_enabled
== 1) {
3142 consumer_timer_switch_stop(metadata
);
3145 if (!metadata
->metadata_stream
) {
3150 * Closing write side so the thread monitoring the stream wakes up if any
3151 * and clean the metadata stream.
3153 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3154 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3156 PERROR("closing metadata pipe write side");
3158 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3166 * Close every metadata stream wait fd of the metadata hash table. This
3167 * function MUST be used very carefully so not to run into a race between the
3168 * metadata thread handling streams and this function closing their wait fd.
3170 * For UST, this is used when the session daemon hangs up. Its the metadata
3171 * producer so calling this is safe because we are assured that no state change
3172 * can occur in the metadata thread for the streams in the hash table.
3174 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3176 struct lttng_ht_iter iter
;
3177 struct lttng_consumer_stream
*stream
;
3179 assert(metadata_ht
);
3180 assert(metadata_ht
->ht
);
3182 DBG("UST consumer closing all metadata streams");
3185 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3188 health_code_update();
3190 pthread_mutex_lock(&stream
->chan
->lock
);
3191 lttng_ustconsumer_close_metadata(stream
->chan
);
3192 pthread_mutex_unlock(&stream
->chan
->lock
);
3198 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3202 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3204 ERR("Unable to close wakeup fd");
3209 * Please refer to consumer-timer.c before adding any lock within this
3210 * function or any of its callees. Timers have a very strict locking
3211 * semantic with respect to teardown. Failure to respect this semantic
3212 * introduces deadlocks.
3214 * DON'T hold the metadata lock when calling this function, else this
3215 * can cause deadlock involving consumer awaiting for metadata to be
3216 * pushed out due to concurrent interaction with the session daemon.
3218 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3219 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3221 struct lttcomm_metadata_request_msg request
;
3222 struct lttcomm_consumer_msg msg
;
3223 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3224 uint64_t len
, key
, offset
, version
;
3228 assert(channel
->metadata_cache
);
3230 memset(&request
, 0, sizeof(request
));
3232 /* send the metadata request to sessiond */
3233 switch (consumer_data
.type
) {
3234 case LTTNG_CONSUMER64_UST
:
3235 request
.bits_per_long
= 64;
3237 case LTTNG_CONSUMER32_UST
:
3238 request
.bits_per_long
= 32;
3241 request
.bits_per_long
= 0;
3245 request
.session_id
= channel
->session_id
;
3246 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3248 * Request the application UID here so the metadata of that application can
3249 * be sent back. The channel UID corresponds to the user UID of the session
3250 * used for the rights on the stream file(s).
3252 request
.uid
= channel
->ust_app_uid
;
3253 request
.key
= channel
->key
;
3255 DBG("Sending metadata request to sessiond, session id %" PRIu64
3256 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3257 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3260 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3262 health_code_update();
3264 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3267 ERR("Asking metadata to sessiond");
3271 health_code_update();
3273 /* Receive the metadata from sessiond */
3274 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3276 if (ret
!= sizeof(msg
)) {
3277 DBG("Consumer received unexpected message size %d (expects %zu)",
3279 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3281 * The ret value might 0 meaning an orderly shutdown but this is ok
3282 * since the caller handles this.
3287 health_code_update();
3289 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3290 /* No registry found */
3291 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3295 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3296 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3301 len
= msg
.u
.push_metadata
.len
;
3302 key
= msg
.u
.push_metadata
.key
;
3303 offset
= msg
.u
.push_metadata
.target_offset
;
3304 version
= msg
.u
.push_metadata
.version
;
3306 assert(key
== channel
->key
);
3308 DBG("No new metadata to receive for key %" PRIu64
, key
);
3311 health_code_update();
3313 /* Tell session daemon we are ready to receive the metadata. */
3314 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3315 LTTCOMM_CONSUMERD_SUCCESS
);
3316 if (ret
< 0 || len
== 0) {
3318 * Somehow, the session daemon is not responding anymore or there is
3319 * nothing to receive.
3324 health_code_update();
3326 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3327 key
, offset
, len
, version
, channel
, timer
, wait
);
3330 * Only send the status msg if the sessiond is alive meaning a positive
3333 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3338 health_code_update();
3340 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3345 * Return the ustctl call for the get stream id.
3347 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3348 uint64_t *stream_id
)
3353 return ustctl_get_stream_id(stream
->ustream
, stream_id
);