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 stream object. If _alloc_ret is not NULL, the
116 * error value if applicable is set in it else it is kept untouched.
118 * Return NULL on error else the newly allocated stream object.
120 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
121 struct lttng_consumer_channel
*channel
,
122 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
125 struct lttng_consumer_stream
*stream
= NULL
;
130 stream
= consumer_allocate_stream(
137 channel
->trace_chunk
,
142 if (stream
== NULL
) {
146 * We could not find the channel. Can happen if cpu hotplug
147 * happens while tearing down.
149 DBG3("Could not find channel");
154 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
160 consumer_stream_update_channel_attributes(stream
, channel
);
164 *_alloc_ret
= alloc_ret
;
170 * Send the given stream pointer to the corresponding thread.
172 * Returns 0 on success else a negative value.
174 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
175 struct lttng_consumer_local_data
*ctx
)
178 struct lttng_pipe
*stream_pipe
;
180 /* Get the right pipe where the stream will be sent. */
181 if (stream
->metadata_flag
) {
182 consumer_add_metadata_stream(stream
);
183 stream_pipe
= ctx
->consumer_metadata_pipe
;
185 consumer_add_data_stream(stream
);
186 stream_pipe
= ctx
->consumer_data_pipe
;
190 * From this point on, the stream's ownership has been moved away from
191 * the channel and it becomes globally visible. Hence, remove it from
192 * the local stream list to prevent the stream from being both local and
195 stream
->globally_visible
= 1;
196 cds_list_del(&stream
->send_node
);
198 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
200 ERR("Consumer write %s stream to pipe %d",
201 stream
->metadata_flag
? "metadata" : "data",
202 lttng_pipe_get_writefd(stream_pipe
));
203 if (stream
->metadata_flag
) {
204 consumer_del_stream_for_metadata(stream
);
206 consumer_del_stream_for_data(stream
);
216 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
218 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
221 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
222 stream_shm_path
[PATH_MAX
- 1] = '\0';
223 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
228 strncat(stream_shm_path
, cpu_nr
,
229 PATH_MAX
- strlen(stream_shm_path
) - 1);
236 * Create streams for the given channel using liblttng-ust-ctl.
237 * The channel lock must be acquired by the caller.
239 * Return 0 on success else a negative value.
241 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
242 struct lttng_consumer_local_data
*ctx
)
245 struct ustctl_consumer_stream
*ustream
;
246 struct lttng_consumer_stream
*stream
;
247 pthread_mutex_t
*current_stream_lock
= NULL
;
253 * While a stream is available from ustctl. When NULL is returned, we've
254 * reached the end of the possible stream for the channel.
256 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
258 int ust_metadata_pipe
[2];
260 health_code_update();
262 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
263 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
265 ERR("Create ust metadata poll pipe");
268 wait_fd
= ust_metadata_pipe
[0];
270 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
273 /* Allocate consumer stream object. */
274 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
278 stream
->ustream
= ustream
;
280 * Store it so we can save multiple function calls afterwards since
281 * this value is used heavily in the stream threads. This is UST
282 * specific so this is why it's done after allocation.
284 stream
->wait_fd
= wait_fd
;
287 * Increment channel refcount since the channel reference has now been
288 * assigned in the allocation process above.
290 if (stream
->chan
->monitor
) {
291 uatomic_inc(&stream
->chan
->refcount
);
294 pthread_mutex_lock(&stream
->lock
);
295 current_stream_lock
= &stream
->lock
;
297 * Order is important this is why a list is used. On error, the caller
298 * should clean this list.
300 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
302 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
303 &stream
->max_sb_size
);
305 ERR("ustctl_get_max_subbuf_size failed for stream %s",
310 /* Do actions once stream has been received. */
311 if (ctx
->on_recv_stream
) {
312 ret
= ctx
->on_recv_stream(stream
);
318 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
319 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
321 /* Set next CPU stream. */
322 channel
->streams
.count
= ++cpu
;
324 /* Keep stream reference when creating metadata. */
325 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
326 channel
->metadata_stream
= stream
;
327 if (channel
->monitor
) {
328 /* Set metadata poll pipe if we created one */
329 memcpy(stream
->ust_metadata_poll_pipe
,
331 sizeof(ust_metadata_pipe
));
334 pthread_mutex_unlock(&stream
->lock
);
335 current_stream_lock
= NULL
;
342 if (current_stream_lock
) {
343 pthread_mutex_unlock(current_stream_lock
);
349 * create_posix_shm is never called concurrently within a process.
352 int create_posix_shm(void)
354 char tmp_name
[NAME_MAX
];
357 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
363 * Allocate shm, and immediately unlink its shm oject, keeping
364 * only the file descriptor as a reference to the object.
365 * We specifically do _not_ use the / at the beginning of the
366 * pathname so that some OS implementations can keep it local to
367 * the process (POSIX leaves this implementation-defined).
369 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
374 ret
= shm_unlink(tmp_name
);
375 if (ret
< 0 && errno
!= ENOENT
) {
376 PERROR("shm_unlink");
377 goto error_shm_release
;
390 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
391 const struct lttng_credentials
*session_credentials
)
393 char shm_path
[PATH_MAX
];
396 if (!channel
->shm_path
[0]) {
397 return create_posix_shm();
399 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
403 return run_as_open(shm_path
,
404 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
405 session_credentials
->uid
, session_credentials
->gid
);
412 * Create an UST channel with the given attributes and send it to the session
413 * daemon using the ust ctl API.
415 * Return 0 on success or else a negative value.
417 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
418 struct ustctl_consumer_channel_attr
*attr
,
419 struct ustctl_consumer_channel
**ust_chanp
)
421 int ret
, nr_stream_fds
, i
, j
;
423 struct ustctl_consumer_channel
*ust_channel
;
428 assert(channel
->buffer_credentials
.is_set
);
430 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
431 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
432 "switch_timer_interval: %u, read_timer_interval: %u, "
433 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
434 attr
->num_subbuf
, attr
->switch_timer_interval
,
435 attr
->read_timer_interval
, attr
->output
, attr
->type
);
437 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
440 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
441 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
446 for (i
= 0; i
< nr_stream_fds
; i
++) {
447 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
448 &channel
->buffer_credentials
.value
);
449 if (stream_fds
[i
] < 0) {
454 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
459 channel
->nr_stream_fds
= nr_stream_fds
;
460 channel
->stream_fds
= stream_fds
;
461 *ust_chanp
= ust_channel
;
467 for (j
= i
- 1; j
>= 0; j
--) {
470 closeret
= close(stream_fds
[j
]);
474 if (channel
->shm_path
[0]) {
475 char shm_path
[PATH_MAX
];
477 closeret
= get_stream_shm_path(shm_path
,
478 channel
->shm_path
, j
);
480 ERR("Cannot get stream shm path");
482 closeret
= run_as_unlink(shm_path
,
483 channel
->buffer_credentials
.value
.uid
,
484 channel
->buffer_credentials
.value
.gid
);
486 PERROR("unlink %s", shm_path
);
490 /* Try to rmdir all directories under shm_path root. */
491 if (channel
->root_shm_path
[0]) {
492 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
493 channel
->buffer_credentials
.value
.uid
,
494 channel
->buffer_credentials
.value
.gid
,
495 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
503 * Send a single given stream to the session daemon using the sock.
505 * Return 0 on success else a negative value.
507 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
514 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
516 /* Send stream to session daemon. */
517 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
527 * Send channel to sessiond and relayd if applicable.
529 * Return 0 on success or else a negative value.
531 static int send_channel_to_sessiond_and_relayd(int sock
,
532 struct lttng_consumer_channel
*channel
,
533 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
535 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
536 struct lttng_consumer_stream
*stream
;
537 uint64_t net_seq_idx
= -1ULL;
543 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
545 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
546 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
548 health_code_update();
550 /* Try to send the stream to the relayd if one is available. */
551 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
552 stream
->key
, channel
->name
);
553 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
556 * Flag that the relayd was the problem here probably due to a
557 * communicaton error on the socket.
562 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
564 if (net_seq_idx
== -1ULL) {
565 net_seq_idx
= stream
->net_seq_idx
;
570 /* Inform sessiond that we are about to send channel and streams. */
571 ret
= consumer_send_status_msg(sock
, ret_code
);
572 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
574 * Either the session daemon is not responding or the relayd died so we
580 /* Send channel to sessiond. */
581 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
586 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
591 /* The channel was sent successfully to the sessiond at this point. */
592 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
594 health_code_update();
596 /* Send stream to session daemon. */
597 ret
= send_sessiond_stream(sock
, stream
);
603 /* Tell sessiond there is no more stream. */
604 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
609 DBG("UST consumer NULL stream sent to sessiond");
614 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
621 * Creates a channel and streams and add the channel it to the channel internal
622 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
625 * Return 0 on success or else, a negative value is returned and the channel
626 * MUST be destroyed by consumer_del_channel().
628 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
629 struct lttng_consumer_channel
*channel
,
630 struct ustctl_consumer_channel_attr
*attr
)
639 * This value is still used by the kernel consumer since for the kernel,
640 * the stream ownership is not IN the consumer so we need to have the
641 * number of left stream that needs to be initialized so we can know when
642 * to delete the channel (see consumer.c).
644 * As for the user space tracer now, the consumer creates and sends the
645 * stream to the session daemon which only sends them to the application
646 * once every stream of a channel is received making this value useless
647 * because we they will be added to the poll thread before the application
648 * receives them. This ensures that a stream can not hang up during
649 * initilization of a channel.
651 channel
->nb_init_stream_left
= 0;
653 /* The reply msg status is handled in the following call. */
654 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
659 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
662 * For the snapshots (no monitor), we create the metadata streams
663 * on demand, not during the channel creation.
665 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
670 /* Open all streams for this channel. */
671 pthread_mutex_lock(&channel
->lock
);
672 ret
= create_ust_streams(channel
, ctx
);
673 pthread_mutex_unlock(&channel
->lock
);
683 * Send all stream of a channel to the right thread handling it.
685 * On error, return a negative value else 0 on success.
687 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
688 struct lttng_consumer_local_data
*ctx
)
691 struct lttng_consumer_stream
*stream
, *stmp
;
696 /* Send streams to the corresponding thread. */
697 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
700 health_code_update();
702 /* Sending the stream to the thread. */
703 ret
= send_stream_to_thread(stream
, ctx
);
706 * If we are unable to send the stream to the thread, there is
707 * a big problem so just stop everything.
718 * Flush channel's streams using the given key to retrieve the channel.
720 * Return 0 on success else an LTTng error code.
722 static int flush_channel(uint64_t chan_key
)
725 struct lttng_consumer_channel
*channel
;
726 struct lttng_consumer_stream
*stream
;
728 struct lttng_ht_iter iter
;
730 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
733 channel
= consumer_find_channel(chan_key
);
735 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
736 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
740 ht
= consumer_data
.stream_per_chan_id_ht
;
742 /* For each stream of the channel id, flush it. */
743 cds_lfht_for_each_entry_duplicate(ht
->ht
,
744 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
745 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
747 health_code_update();
749 pthread_mutex_lock(&stream
->lock
);
752 * Protect against concurrent teardown of a stream.
754 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
758 if (!stream
->quiescent
) {
759 ustctl_flush_buffer(stream
->ustream
, 0);
760 stream
->quiescent
= true;
763 pthread_mutex_unlock(&stream
->lock
);
771 * Clear quiescent state from channel's streams using the given key to
772 * retrieve the channel.
774 * Return 0 on success else an LTTng error code.
776 static int clear_quiescent_channel(uint64_t chan_key
)
779 struct lttng_consumer_channel
*channel
;
780 struct lttng_consumer_stream
*stream
;
782 struct lttng_ht_iter iter
;
784 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
787 channel
= consumer_find_channel(chan_key
);
789 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
790 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
794 ht
= consumer_data
.stream_per_chan_id_ht
;
796 /* For each stream of the channel id, clear quiescent state. */
797 cds_lfht_for_each_entry_duplicate(ht
->ht
,
798 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
799 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
801 health_code_update();
803 pthread_mutex_lock(&stream
->lock
);
804 stream
->quiescent
= false;
805 pthread_mutex_unlock(&stream
->lock
);
813 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
815 * Return 0 on success else an LTTng error code.
817 static int close_metadata(uint64_t chan_key
)
820 struct lttng_consumer_channel
*channel
;
821 unsigned int channel_monitor
;
823 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
825 channel
= consumer_find_channel(chan_key
);
828 * This is possible if the metadata thread has issue a delete because
829 * the endpoint point of the stream hung up. There is no way the
830 * session daemon can know about it thus use a DBG instead of an actual
833 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
834 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
838 pthread_mutex_lock(&consumer_data
.lock
);
839 pthread_mutex_lock(&channel
->lock
);
840 channel_monitor
= channel
->monitor
;
841 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
845 lttng_ustconsumer_close_metadata(channel
);
846 pthread_mutex_unlock(&channel
->lock
);
847 pthread_mutex_unlock(&consumer_data
.lock
);
850 * The ownership of a metadata channel depends on the type of
851 * session to which it belongs. In effect, the monitor flag is checked
852 * to determine if this metadata channel is in "snapshot" mode or not.
854 * In the non-snapshot case, the metadata channel is created along with
855 * a single stream which will remain present until the metadata channel
856 * is destroyed (on the destruction of its session). In this case, the
857 * metadata stream in "monitored" by the metadata poll thread and holds
858 * the ownership of its channel.
860 * Closing the metadata will cause the metadata stream's "metadata poll
861 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
862 * thread which will teardown the metadata stream which, in return,
863 * deletes the metadata channel.
865 * In the snapshot case, the metadata stream is created and destroyed
866 * on every snapshot record. Since the channel doesn't have an owner
867 * other than the session daemon, it is safe to destroy it immediately
868 * on reception of the CLOSE_METADATA command.
870 if (!channel_monitor
) {
872 * The channel and consumer_data locks must be
873 * released before this call since consumer_del_channel
874 * re-acquires the channel and consumer_data locks to teardown
875 * the channel and queue its reclamation by the "call_rcu"
878 consumer_del_channel(channel
);
883 pthread_mutex_unlock(&channel
->lock
);
884 pthread_mutex_unlock(&consumer_data
.lock
);
890 * RCU read side lock MUST be acquired before calling this function.
892 * Return 0 on success else an LTTng error code.
894 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
897 struct lttng_consumer_channel
*metadata
;
899 DBG("UST consumer setup metadata key %" PRIu64
, key
);
901 metadata
= consumer_find_channel(key
);
903 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
904 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
909 * In no monitor mode, the metadata channel has no stream(s) so skip the
910 * ownership transfer to the metadata thread.
912 if (!metadata
->monitor
) {
913 DBG("Metadata channel in no monitor");
919 * Send metadata stream to relayd if one available. Availability is
920 * known if the stream is still in the list of the channel.
922 if (cds_list_empty(&metadata
->streams
.head
)) {
923 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
924 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
925 goto error_no_stream
;
928 /* Send metadata stream to relayd if needed. */
929 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
930 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
933 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
936 ret
= consumer_send_relayd_streams_sent(
937 metadata
->metadata_stream
->net_seq_idx
);
939 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
945 * Ownership of metadata stream is passed along. Freeing is handled by
948 ret
= send_streams_to_thread(metadata
, ctx
);
951 * If we are unable to send the stream to the thread, there is
952 * a big problem so just stop everything.
954 ret
= LTTCOMM_CONSUMERD_FATAL
;
955 goto send_streams_error
;
957 /* List MUST be empty after or else it could be reused. */
958 assert(cds_list_empty(&metadata
->streams
.head
));
965 * Delete metadata channel on error. At this point, the metadata stream can
966 * NOT be monitored by the metadata thread thus having the guarantee that
967 * the stream is still in the local stream list of the channel. This call
968 * will make sure to clean that list.
970 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
971 cds_list_del(&metadata
->metadata_stream
->send_node
);
972 metadata
->metadata_stream
= NULL
;
980 * Snapshot the whole metadata.
981 * RCU read-side lock must be held by the caller.
983 * Returns 0 on success, < 0 on error
985 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
986 uint64_t key
, char *path
, uint64_t relayd_id
,
987 struct lttng_consumer_local_data
*ctx
)
990 struct lttng_consumer_stream
*metadata_stream
;
995 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1000 assert(!metadata_channel
->monitor
);
1002 health_code_update();
1005 * Ask the sessiond if we have new metadata waiting and update the
1006 * consumer metadata cache.
1008 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1013 health_code_update();
1016 * The metadata stream is NOT created in no monitor mode when the channel
1017 * is created on a sessiond ask channel command.
1019 ret
= create_ust_streams(metadata_channel
, ctx
);
1024 metadata_stream
= metadata_channel
->metadata_stream
;
1025 assert(metadata_stream
);
1027 pthread_mutex_lock(&metadata_stream
->lock
);
1028 if (relayd_id
!= (uint64_t) -1ULL) {
1029 metadata_stream
->net_seq_idx
= relayd_id
;
1030 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1032 ret
= consumer_stream_create_output_files(metadata_stream
,
1035 pthread_mutex_unlock(&metadata_stream
->lock
);
1041 health_code_update();
1043 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
);
1051 * Clean up the stream completly because the next snapshot will use a new
1054 consumer_stream_destroy(metadata_stream
, NULL
);
1055 cds_list_del(&metadata_stream
->send_node
);
1056 metadata_channel
->metadata_stream
= NULL
;
1064 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1068 unsigned long mmap_offset
;
1069 const char *mmap_base
;
1071 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1073 ERR("Failed to get mmap base for stream `%s`",
1079 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1081 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1086 *addr
= mmap_base
+ mmap_offset
;
1093 * Take a snapshot of all the stream of a channel.
1094 * RCU read-side lock and the channel lock must be held by the caller.
1096 * Returns 0 on success, < 0 on error
1098 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1099 uint64_t key
, char *path
, uint64_t relayd_id
,
1100 uint64_t nb_packets_per_stream
,
1101 struct lttng_consumer_local_data
*ctx
)
1104 unsigned use_relayd
= 0;
1105 unsigned long consumed_pos
, produced_pos
;
1106 struct lttng_consumer_stream
*stream
;
1113 if (relayd_id
!= (uint64_t) -1ULL) {
1117 assert(!channel
->monitor
);
1118 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1120 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1121 health_code_update();
1123 /* Lock stream because we are about to change its state. */
1124 pthread_mutex_lock(&stream
->lock
);
1125 assert(channel
->trace_chunk
);
1126 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1128 * Can't happen barring an internal error as the channel
1129 * holds a reference to the trace chunk.
1131 ERR("Failed to acquire reference to channel's trace chunk");
1135 assert(!stream
->trace_chunk
);
1136 stream
->trace_chunk
= channel
->trace_chunk
;
1138 stream
->net_seq_idx
= relayd_id
;
1141 ret
= consumer_send_relayd_stream(stream
, path
);
1146 ret
= consumer_stream_create_output_files(stream
,
1151 DBG("UST consumer snapshot stream (%" PRIu64
")",
1156 * If tracing is active, we want to perform a "full" buffer flush.
1157 * Else, if quiescent, it has already been done by the prior stop.
1159 if (!stream
->quiescent
) {
1160 ustctl_flush_buffer(stream
->ustream
, 0);
1163 ret
= lttng_ustconsumer_take_snapshot(stream
);
1165 ERR("Taking UST snapshot");
1169 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1171 ERR("Produced UST snapshot position");
1175 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1177 ERR("Consumerd UST snapshot position");
1182 * The original value is sent back if max stream size is larger than
1183 * the possible size of the snapshot. Also, we assume that the session
1184 * daemon should never send a maximum stream size that is lower than
1187 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1188 produced_pos
, nb_packets_per_stream
,
1189 stream
->max_sb_size
);
1191 while ((long) (consumed_pos
- produced_pos
) < 0) {
1193 unsigned long len
, padded_len
;
1194 const char *subbuf_addr
;
1195 struct lttng_buffer_view subbuf_view
;
1197 health_code_update();
1199 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1201 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1203 if (ret
!= -EAGAIN
) {
1204 PERROR("ustctl_get_subbuf snapshot");
1205 goto error_close_stream
;
1207 DBG("UST consumer get subbuf failed. Skipping it.");
1208 consumed_pos
+= stream
->max_sb_size
;
1209 stream
->chan
->lost_packets
++;
1213 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1215 ERR("Snapshot ustctl_get_subbuf_size");
1216 goto error_put_subbuf
;
1219 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1221 ERR("Snapshot ustctl_get_padded_subbuf_size");
1222 goto error_put_subbuf
;
1225 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1227 goto error_put_subbuf
;
1230 subbuf_view
= lttng_buffer_view_init(
1231 subbuf_addr
, 0, padded_len
);
1232 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1233 stream
, &subbuf_view
, padded_len
- len
,
1236 if (read_len
!= len
) {
1238 goto error_put_subbuf
;
1241 if (read_len
!= padded_len
) {
1243 goto error_put_subbuf
;
1247 ret
= ustctl_put_subbuf(stream
->ustream
);
1249 ERR("Snapshot ustctl_put_subbuf");
1250 goto error_close_stream
;
1252 consumed_pos
+= stream
->max_sb_size
;
1255 /* Simply close the stream so we can use it on the next snapshot. */
1256 consumer_stream_close(stream
);
1257 pthread_mutex_unlock(&stream
->lock
);
1264 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1265 ERR("Snapshot ustctl_put_subbuf");
1268 consumer_stream_close(stream
);
1270 pthread_mutex_unlock(&stream
->lock
);
1276 * Receive the metadata updates from the sessiond. Supports receiving
1277 * overlapping metadata, but is needs to always belong to a contiguous
1278 * range starting from 0.
1279 * Be careful about the locks held when calling this function: it needs
1280 * the metadata cache flush to concurrently progress in order to
1283 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1284 uint64_t len
, uint64_t version
,
1285 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1287 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1290 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1292 metadata_str
= zmalloc(len
* sizeof(char));
1293 if (!metadata_str
) {
1294 PERROR("zmalloc metadata string");
1295 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1299 health_code_update();
1301 /* Receive metadata string. */
1302 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1304 /* Session daemon is dead so return gracefully. */
1309 health_code_update();
1311 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1312 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1315 /* Unable to handle metadata. Notify session daemon. */
1316 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1318 * Skip metadata flush on write error since the offset and len might
1319 * not have been updated which could create an infinite loop below when
1320 * waiting for the metadata cache to be flushed.
1322 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1325 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1330 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1331 DBG("Waiting for metadata to be flushed");
1333 health_code_update();
1335 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1345 * Receive command from session daemon and process it.
1347 * Return 1 on success else a negative value or 0.
1349 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1350 int sock
, struct pollfd
*consumer_sockpoll
)
1353 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1354 struct lttcomm_consumer_msg msg
;
1355 struct lttng_consumer_channel
*channel
= NULL
;
1357 health_code_update();
1359 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1360 if (ret
!= sizeof(msg
)) {
1361 DBG("Consumer received unexpected message size %zd (expects %zu)",
1364 * The ret value might 0 meaning an orderly shutdown but this is ok
1365 * since the caller handles this.
1368 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1374 health_code_update();
1377 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1379 health_code_update();
1381 /* relayd needs RCU read-side lock */
1384 switch (msg
.cmd_type
) {
1385 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1387 /* Session daemon status message are handled in the following call. */
1388 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1389 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1390 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1391 msg
.u
.relayd_sock
.relayd_session_id
);
1394 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1396 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1397 struct consumer_relayd_sock_pair
*relayd
;
1399 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1401 /* Get relayd reference if exists. */
1402 relayd
= consumer_find_relayd(index
);
1403 if (relayd
== NULL
) {
1404 DBG("Unable to find relayd %" PRIu64
, index
);
1405 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1409 * Each relayd socket pair has a refcount of stream attached to it
1410 * which tells if the relayd is still active or not depending on the
1413 * This will set the destroy flag of the relayd object and destroy it
1414 * if the refcount reaches zero when called.
1416 * The destroy can happen either here or when a stream fd hangs up.
1419 consumer_flag_relayd_for_destroy(relayd
);
1422 goto end_msg_sessiond
;
1424 case LTTNG_CONSUMER_UPDATE_STREAM
:
1429 case LTTNG_CONSUMER_DATA_PENDING
:
1431 int ret
, is_data_pending
;
1432 uint64_t id
= msg
.u
.data_pending
.session_id
;
1434 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1436 is_data_pending
= consumer_data_pending(id
);
1438 /* Send back returned value to session daemon */
1439 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1440 sizeof(is_data_pending
));
1442 DBG("Error when sending the data pending ret code: %d", ret
);
1447 * No need to send back a status message since the data pending
1448 * returned value is the response.
1452 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1455 struct ustctl_consumer_channel_attr attr
;
1456 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1457 const struct lttng_credentials buffer_credentials
= {
1458 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1459 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1462 /* Create a plain object and reserve a channel key. */
1463 channel
= consumer_allocate_channel(
1464 msg
.u
.ask_channel
.key
,
1465 msg
.u
.ask_channel
.session_id
,
1466 msg
.u
.ask_channel
.chunk_id
.is_set
?
1468 msg
.u
.ask_channel
.pathname
,
1469 msg
.u
.ask_channel
.name
,
1470 msg
.u
.ask_channel
.relayd_id
,
1471 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1472 msg
.u
.ask_channel
.tracefile_size
,
1473 msg
.u
.ask_channel
.tracefile_count
,
1474 msg
.u
.ask_channel
.session_id_per_pid
,
1475 msg
.u
.ask_channel
.monitor
,
1476 msg
.u
.ask_channel
.live_timer_interval
,
1477 msg
.u
.ask_channel
.is_live
,
1478 msg
.u
.ask_channel
.root_shm_path
,
1479 msg
.u
.ask_channel
.shm_path
);
1481 goto end_channel_error
;
1484 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1485 buffer_credentials
);
1488 * Assign UST application UID to the channel. This value is ignored for
1489 * per PID buffers. This is specific to UST thus setting this after the
1492 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1494 /* Build channel attributes from received message. */
1495 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1496 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1497 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1498 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1499 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1500 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1501 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1502 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1504 /* Match channel buffer type to the UST abi. */
1505 switch (msg
.u
.ask_channel
.output
) {
1506 case LTTNG_EVENT_MMAP
:
1508 attr
.output
= LTTNG_UST_MMAP
;
1512 /* Translate and save channel type. */
1513 switch (msg
.u
.ask_channel
.type
) {
1514 case LTTNG_UST_CHAN_PER_CPU
:
1515 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1516 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1518 * Set refcount to 1 for owner. Below, we will
1519 * pass ownership to the
1520 * consumer_thread_channel_poll() thread.
1522 channel
->refcount
= 1;
1524 case LTTNG_UST_CHAN_METADATA
:
1525 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1526 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1533 health_code_update();
1535 ret
= ask_channel(ctx
, channel
, &attr
);
1537 goto end_channel_error
;
1540 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1541 ret
= consumer_metadata_cache_allocate(channel
);
1543 ERR("Allocating metadata cache");
1544 goto end_channel_error
;
1546 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1547 attr
.switch_timer_interval
= 0;
1549 int monitor_start_ret
;
1551 consumer_timer_live_start(channel
,
1552 msg
.u
.ask_channel
.live_timer_interval
);
1553 monitor_start_ret
= consumer_timer_monitor_start(
1555 msg
.u
.ask_channel
.monitor_timer_interval
);
1556 if (monitor_start_ret
< 0) {
1557 ERR("Starting channel monitoring timer failed");
1558 goto end_channel_error
;
1562 health_code_update();
1565 * Add the channel to the internal state AFTER all streams were created
1566 * and successfully sent to session daemon. This way, all streams must
1567 * be ready before this channel is visible to the threads.
1568 * If add_channel succeeds, ownership of the channel is
1569 * passed to consumer_thread_channel_poll().
1571 ret
= add_channel(channel
, ctx
);
1573 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1574 if (channel
->switch_timer_enabled
== 1) {
1575 consumer_timer_switch_stop(channel
);
1577 consumer_metadata_cache_destroy(channel
);
1579 if (channel
->live_timer_enabled
== 1) {
1580 consumer_timer_live_stop(channel
);
1582 if (channel
->monitor_timer_enabled
== 1) {
1583 consumer_timer_monitor_stop(channel
);
1585 goto end_channel_error
;
1588 health_code_update();
1591 * Channel and streams are now created. Inform the session daemon that
1592 * everything went well and should wait to receive the channel and
1593 * streams with ustctl API.
1595 ret
= consumer_send_status_channel(sock
, channel
);
1598 * There is probably a problem on the socket.
1605 case LTTNG_CONSUMER_GET_CHANNEL
:
1607 int ret
, relayd_err
= 0;
1608 uint64_t key
= msg
.u
.get_channel
.key
;
1609 struct lttng_consumer_channel
*channel
;
1611 channel
= consumer_find_channel(key
);
1613 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1614 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1615 goto end_get_channel
;
1618 health_code_update();
1620 /* Send the channel to sessiond (and relayd, if applicable). */
1621 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1626 * We were unable to send to the relayd the stream so avoid
1627 * sending back a fatal error to the thread since this is OK
1628 * and the consumer can continue its work. The above call
1629 * has sent the error status message to the sessiond.
1631 goto end_get_channel_nosignal
;
1634 * The communicaton was broken hence there is a bad state between
1635 * the consumer and sessiond so stop everything.
1637 goto error_get_channel_fatal
;
1640 health_code_update();
1643 * In no monitor mode, the streams ownership is kept inside the channel
1644 * so don't send them to the data thread.
1646 if (!channel
->monitor
) {
1647 goto end_get_channel
;
1650 ret
= send_streams_to_thread(channel
, ctx
);
1653 * If we are unable to send the stream to the thread, there is
1654 * a big problem so just stop everything.
1656 goto error_get_channel_fatal
;
1658 /* List MUST be empty after or else it could be reused. */
1659 assert(cds_list_empty(&channel
->streams
.head
));
1661 goto end_msg_sessiond
;
1662 error_get_channel_fatal
:
1664 end_get_channel_nosignal
:
1667 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1669 uint64_t key
= msg
.u
.destroy_channel
.key
;
1672 * Only called if streams have not been sent to stream
1673 * manager thread. However, channel has been sent to
1674 * channel manager thread.
1676 notify_thread_del_channel(ctx
, key
);
1677 goto end_msg_sessiond
;
1679 case LTTNG_CONSUMER_CLOSE_METADATA
:
1683 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1688 goto end_msg_sessiond
;
1690 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1694 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1699 goto end_msg_sessiond
;
1701 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1705 ret
= clear_quiescent_channel(
1706 msg
.u
.clear_quiescent_channel
.key
);
1711 goto end_msg_sessiond
;
1713 case LTTNG_CONSUMER_PUSH_METADATA
:
1716 uint64_t len
= msg
.u
.push_metadata
.len
;
1717 uint64_t key
= msg
.u
.push_metadata
.key
;
1718 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1719 uint64_t version
= msg
.u
.push_metadata
.version
;
1720 struct lttng_consumer_channel
*channel
;
1722 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1725 channel
= consumer_find_channel(key
);
1728 * This is possible if the metadata creation on the consumer side
1729 * is in flight vis-a-vis a concurrent push metadata from the
1730 * session daemon. Simply return that the channel failed and the
1731 * session daemon will handle that message correctly considering
1732 * that this race is acceptable thus the DBG() statement here.
1734 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1735 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1736 goto end_push_metadata_msg_sessiond
;
1739 health_code_update();
1743 * There is nothing to receive. We have simply
1744 * checked whether the channel can be found.
1746 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1747 goto end_push_metadata_msg_sessiond
;
1750 /* Tell session daemon we are ready to receive the metadata. */
1751 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1753 /* Somehow, the session daemon is not responding anymore. */
1754 goto error_push_metadata_fatal
;
1757 health_code_update();
1759 /* Wait for more data. */
1760 health_poll_entry();
1761 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1764 goto error_push_metadata_fatal
;
1767 health_code_update();
1769 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1770 len
, version
, channel
, 0, 1);
1772 /* error receiving from sessiond */
1773 goto error_push_metadata_fatal
;
1776 goto end_push_metadata_msg_sessiond
;
1778 end_push_metadata_msg_sessiond
:
1779 goto end_msg_sessiond
;
1780 error_push_metadata_fatal
:
1783 case LTTNG_CONSUMER_SETUP_METADATA
:
1787 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1791 goto end_msg_sessiond
;
1793 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1795 struct lttng_consumer_channel
*channel
;
1796 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1798 channel
= consumer_find_channel(key
);
1800 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1801 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1803 if (msg
.u
.snapshot_channel
.metadata
) {
1804 ret
= snapshot_metadata(channel
, key
,
1805 msg
.u
.snapshot_channel
.pathname
,
1806 msg
.u
.snapshot_channel
.relayd_id
,
1809 ERR("Snapshot metadata failed");
1810 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1813 ret
= snapshot_channel(channel
, key
,
1814 msg
.u
.snapshot_channel
.pathname
,
1815 msg
.u
.snapshot_channel
.relayd_id
,
1816 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1819 ERR("Snapshot channel failed");
1820 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1824 health_code_update();
1825 ret
= consumer_send_status_msg(sock
, ret_code
);
1827 /* Somehow, the session daemon is not responding anymore. */
1830 health_code_update();
1833 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1836 uint64_t discarded_events
;
1837 struct lttng_ht_iter iter
;
1838 struct lttng_ht
*ht
;
1839 struct lttng_consumer_stream
*stream
;
1840 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1841 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1843 DBG("UST consumer discarded events command for session id %"
1846 pthread_mutex_lock(&consumer_data
.lock
);
1848 ht
= consumer_data
.stream_list_ht
;
1851 * We only need a reference to the channel, but they are not
1852 * directly indexed, so we just use the first matching stream
1853 * to extract the information we need, we default to 0 if not
1854 * found (no events are dropped if the channel is not yet in
1857 discarded_events
= 0;
1858 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1859 ht
->hash_fct(&id
, lttng_ht_seed
),
1861 &iter
.iter
, stream
, node_session_id
.node
) {
1862 if (stream
->chan
->key
== key
) {
1863 discarded_events
= stream
->chan
->discarded_events
;
1867 pthread_mutex_unlock(&consumer_data
.lock
);
1870 DBG("UST consumer discarded events command for session id %"
1871 PRIu64
", channel key %" PRIu64
, id
, key
);
1873 health_code_update();
1875 /* Send back returned value to session daemon */
1876 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1878 PERROR("send discarded events");
1884 case LTTNG_CONSUMER_LOST_PACKETS
:
1887 uint64_t lost_packets
;
1888 struct lttng_ht_iter iter
;
1889 struct lttng_ht
*ht
;
1890 struct lttng_consumer_stream
*stream
;
1891 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1892 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1894 DBG("UST consumer lost packets command for session id %"
1897 pthread_mutex_lock(&consumer_data
.lock
);
1899 ht
= consumer_data
.stream_list_ht
;
1902 * We only need a reference to the channel, but they are not
1903 * directly indexed, so we just use the first matching stream
1904 * to extract the information we need, we default to 0 if not
1905 * found (no packets lost if the channel is not yet in use).
1908 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1909 ht
->hash_fct(&id
, lttng_ht_seed
),
1911 &iter
.iter
, stream
, node_session_id
.node
) {
1912 if (stream
->chan
->key
== key
) {
1913 lost_packets
= stream
->chan
->lost_packets
;
1917 pthread_mutex_unlock(&consumer_data
.lock
);
1920 DBG("UST consumer lost packets command for session id %"
1921 PRIu64
", channel key %" PRIu64
, id
, key
);
1923 health_code_update();
1925 /* Send back returned value to session daemon */
1926 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1927 sizeof(lost_packets
));
1929 PERROR("send lost packets");
1935 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1937 int channel_monitor_pipe
;
1939 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1940 /* Successfully received the command's type. */
1941 ret
= consumer_send_status_msg(sock
, ret_code
);
1946 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1948 if (ret
!= sizeof(channel_monitor_pipe
)) {
1949 ERR("Failed to receive channel monitor pipe");
1953 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1954 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1955 channel_monitor_pipe
);
1959 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1960 /* Set the pipe as non-blocking. */
1961 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1963 PERROR("fcntl get flags of the channel monitoring pipe");
1968 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1969 flags
| O_NONBLOCK
);
1971 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1974 DBG("Channel monitor pipe set as non-blocking");
1976 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1978 goto end_msg_sessiond
;
1980 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1982 struct lttng_consumer_channel
*channel
;
1983 uint64_t key
= msg
.u
.rotate_channel
.key
;
1985 channel
= consumer_find_channel(key
);
1987 DBG("Channel %" PRIu64
" not found", key
);
1988 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1991 * Sample the rotate position of all the streams in
1994 ret
= lttng_consumer_rotate_channel(channel
, key
,
1995 msg
.u
.rotate_channel
.relayd_id
,
1996 msg
.u
.rotate_channel
.metadata
,
1999 ERR("Rotate channel failed");
2000 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2003 health_code_update();
2005 ret
= consumer_send_status_msg(sock
, ret_code
);
2007 /* Somehow, the session daemon is not responding anymore. */
2008 goto end_rotate_channel_nosignal
;
2012 * Rotate the streams that are ready right now.
2013 * FIXME: this is a second consecutive iteration over the
2014 * streams in a channel, there is probably a better way to
2015 * handle this, but it needs to be after the
2016 * consumer_send_status_msg() call.
2019 ret
= lttng_consumer_rotate_ready_streams(
2022 ERR("Rotate channel failed");
2026 end_rotate_channel_nosignal
:
2029 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2031 struct lttng_consumer_channel
*channel
;
2032 uint64_t key
= msg
.u
.clear_channel
.key
;
2034 channel
= consumer_find_channel(key
);
2036 DBG("Channel %" PRIu64
" not found", key
);
2037 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2039 ret
= lttng_consumer_clear_channel(channel
);
2041 ERR("Clear channel failed key %" PRIu64
, key
);
2045 health_code_update();
2047 ret
= consumer_send_status_msg(sock
, ret_code
);
2049 /* Somehow, the session daemon is not responding anymore. */
2054 case LTTNG_CONSUMER_INIT
:
2056 ret_code
= lttng_consumer_init_command(ctx
,
2057 msg
.u
.init
.sessiond_uuid
);
2058 health_code_update();
2059 ret
= consumer_send_status_msg(sock
, ret_code
);
2061 /* Somehow, the session daemon is not responding anymore. */
2066 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2068 const struct lttng_credentials credentials
= {
2069 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2070 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2072 const bool is_local_trace
=
2073 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2074 const uint64_t relayd_id
=
2075 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2076 const char *chunk_override_name
=
2077 *msg
.u
.create_trace_chunk
.override_name
?
2078 msg
.u
.create_trace_chunk
.override_name
:
2080 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2083 * The session daemon will only provide a chunk directory file
2084 * descriptor for local traces.
2086 if (is_local_trace
) {
2089 /* Acnowledge the reception of the command. */
2090 ret
= consumer_send_status_msg(sock
,
2091 LTTCOMM_CONSUMERD_SUCCESS
);
2093 /* Somehow, the session daemon is not responding anymore. */
2098 * Receive trace chunk domain dirfd.
2100 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2101 if (ret
!= sizeof(chunk_dirfd
)) {
2102 ERR("Failed to receive trace chunk domain directory file descriptor");
2106 DBG("Received trace chunk domain directory fd (%d)",
2108 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2110 if (!chunk_directory_handle
) {
2111 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2112 if (close(chunk_dirfd
)) {
2113 PERROR("Failed to close chunk directory file descriptor");
2119 ret_code
= lttng_consumer_create_trace_chunk(
2120 !is_local_trace
? &relayd_id
: NULL
,
2121 msg
.u
.create_trace_chunk
.session_id
,
2122 msg
.u
.create_trace_chunk
.chunk_id
,
2123 (time_t) msg
.u
.create_trace_chunk
2124 .creation_timestamp
,
2125 chunk_override_name
,
2126 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2129 chunk_directory_handle
);
2130 lttng_directory_handle_put(chunk_directory_handle
);
2131 goto end_msg_sessiond
;
2133 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2135 enum lttng_trace_chunk_command_type close_command
=
2136 msg
.u
.close_trace_chunk
.close_command
.value
;
2137 const uint64_t relayd_id
=
2138 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2139 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2140 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2143 ret_code
= lttng_consumer_close_trace_chunk(
2144 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2147 msg
.u
.close_trace_chunk
.session_id
,
2148 msg
.u
.close_trace_chunk
.chunk_id
,
2149 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2150 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2152 NULL
, closed_trace_chunk_path
);
2153 reply
.ret_code
= ret_code
;
2154 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2155 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2156 if (ret
!= sizeof(reply
)) {
2159 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2161 if (ret
!= reply
.path_length
) {
2166 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2168 const uint64_t relayd_id
=
2169 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2171 ret_code
= lttng_consumer_trace_chunk_exists(
2172 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2174 msg
.u
.trace_chunk_exists
.session_id
,
2175 msg
.u
.trace_chunk_exists
.chunk_id
);
2176 goto end_msg_sessiond
;
2184 * Return 1 to indicate success since the 0 value can be a socket
2185 * shutdown during the recv() or send() call.
2192 * The returned value here is not useful since either way we'll return 1 to
2193 * the caller because the session daemon socket management is done
2194 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2196 ret
= consumer_send_status_msg(sock
, ret_code
);
2206 * Free channel here since no one has a reference to it. We don't
2207 * free after that because a stream can store this pointer.
2209 destroy_channel(channel
);
2211 /* We have to send a status channel message indicating an error. */
2212 ret
= consumer_send_status_channel(sock
, NULL
);
2214 /* Stop everything if session daemon can not be notified. */
2221 /* This will issue a consumer stop. */
2227 health_code_update();
2231 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2232 int producer_active
)
2235 assert(stream
->ustream
);
2237 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2241 * Take a snapshot for a specific stream.
2243 * Returns 0 on success, < 0 on error
2245 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2248 assert(stream
->ustream
);
2250 return ustctl_snapshot(stream
->ustream
);
2254 * Sample consumed and produced positions for a specific stream.
2256 * Returns 0 on success, < 0 on error.
2258 int lttng_ustconsumer_sample_snapshot_positions(
2259 struct lttng_consumer_stream
*stream
)
2262 assert(stream
->ustream
);
2264 return ustctl_snapshot_sample_positions(stream
->ustream
);
2268 * Get the produced position
2270 * Returns 0 on success, < 0 on error
2272 int lttng_ustconsumer_get_produced_snapshot(
2273 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2276 assert(stream
->ustream
);
2279 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2283 * Get the consumed position
2285 * Returns 0 on success, < 0 on error
2287 int lttng_ustconsumer_get_consumed_snapshot(
2288 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2291 assert(stream
->ustream
);
2294 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2297 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2301 assert(stream
->ustream
);
2303 ustctl_flush_buffer(stream
->ustream
, producer
);
2306 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2309 assert(stream
->ustream
);
2311 ustctl_clear_buffer(stream
->ustream
);
2314 int lttng_ustconsumer_get_current_timestamp(
2315 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2318 assert(stream
->ustream
);
2321 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2324 int lttng_ustconsumer_get_sequence_number(
2325 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2328 assert(stream
->ustream
);
2331 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2335 * Called when the stream signals the consumer that it has hung up.
2337 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2340 assert(stream
->ustream
);
2342 pthread_mutex_lock(&stream
->lock
);
2343 if (!stream
->quiescent
) {
2344 ustctl_flush_buffer(stream
->ustream
, 0);
2345 stream
->quiescent
= true;
2347 pthread_mutex_unlock(&stream
->lock
);
2348 stream
->hangup_flush_done
= 1;
2351 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2356 assert(chan
->uchan
);
2357 assert(chan
->buffer_credentials
.is_set
);
2359 if (chan
->switch_timer_enabled
== 1) {
2360 consumer_timer_switch_stop(chan
);
2362 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2365 ret
= close(chan
->stream_fds
[i
]);
2369 if (chan
->shm_path
[0]) {
2370 char shm_path
[PATH_MAX
];
2372 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2374 ERR("Cannot get stream shm path");
2376 ret
= run_as_unlink(shm_path
,
2377 chan
->buffer_credentials
.value
.uid
,
2378 chan
->buffer_credentials
.value
.gid
);
2380 PERROR("unlink %s", shm_path
);
2386 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2389 assert(chan
->uchan
);
2390 assert(chan
->buffer_credentials
.is_set
);
2392 consumer_metadata_cache_destroy(chan
);
2393 ustctl_destroy_channel(chan
->uchan
);
2394 /* Try to rmdir all directories under shm_path root. */
2395 if (chan
->root_shm_path
[0]) {
2396 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2397 chan
->buffer_credentials
.value
.uid
,
2398 chan
->buffer_credentials
.value
.gid
,
2399 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2401 free(chan
->stream_fds
);
2404 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2407 assert(stream
->ustream
);
2409 if (stream
->chan
->switch_timer_enabled
== 1) {
2410 consumer_timer_switch_stop(stream
->chan
);
2412 ustctl_destroy_stream(stream
->ustream
);
2415 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2418 assert(stream
->ustream
);
2420 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2423 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2426 assert(stream
->ustream
);
2428 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2432 * Populate index values of a UST stream. Values are set in big endian order.
2434 * Return 0 on success or else a negative value.
2436 static int get_index_values(struct ctf_packet_index
*index
,
2437 struct ustctl_consumer_stream
*ustream
)
2440 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
2441 events_discarded
, stream_id
, stream_instance_id
,
2444 ret
= ustctl_get_timestamp_begin(ustream
, ×tamp_begin
);
2446 PERROR("ustctl_get_timestamp_begin");
2450 ret
= ustctl_get_timestamp_end(ustream
, ×tamp_end
);
2452 PERROR("ustctl_get_timestamp_end");
2456 ret
= ustctl_get_events_discarded(ustream
, &events_discarded
);
2458 PERROR("ustctl_get_events_discarded");
2462 ret
= ustctl_get_content_size(ustream
, &content_size
);
2464 PERROR("ustctl_get_content_size");
2468 ret
= ustctl_get_packet_size(ustream
, &packet_size
);
2470 PERROR("ustctl_get_packet_size");
2474 ret
= ustctl_get_stream_id(ustream
, &stream_id
);
2476 PERROR("ustctl_get_stream_id");
2480 ret
= ustctl_get_instance_id(ustream
, &stream_instance_id
);
2482 PERROR("ustctl_get_instance_id");
2486 ret
= ustctl_get_sequence_number(ustream
, &packet_seq_num
);
2488 PERROR("ustctl_get_sequence_number");
2492 *index
= (typeof(*index
)) {
2493 .offset
= index
->offset
,
2494 .packet_size
= htobe64(packet_size
),
2495 .content_size
= htobe64(content_size
),
2496 .timestamp_begin
= htobe64(timestamp_begin
),
2497 .timestamp_end
= htobe64(timestamp_end
),
2498 .events_discarded
= htobe64(events_discarded
),
2499 .stream_id
= htobe64(stream_id
),
2500 .stream_instance_id
= htobe64(stream_instance_id
),
2501 .packet_seq_num
= htobe64(packet_seq_num
),
2509 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
,
2510 struct consumer_metadata_cache
*cache
)
2512 DBG("Metadata stream update to version %" PRIu64
,
2514 stream
->ust_metadata_pushed
= 0;
2515 stream
->metadata_version
= cache
->version
;
2516 stream
->reset_metadata_flag
= 1;
2520 * Check if the version of the metadata stream and metadata cache match.
2521 * If the cache got updated, reset the metadata stream.
2522 * The stream lock and metadata cache lock MUST be held.
2523 * Return 0 on success, a negative value on error.
2526 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
)
2529 struct consumer_metadata_cache
*cache
= stream
->chan
->metadata_cache
;
2531 if (cache
->version
== stream
->metadata_version
) {
2534 metadata_stream_reset_cache(stream
, cache
);
2541 * Write up to one packet from the metadata cache to the channel.
2543 * Returns the number of bytes pushed in the cache, or a negative value
2547 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2552 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2553 ret
= metadata_stream_check_version(stream
);
2557 if (stream
->chan
->metadata_cache
->max_offset
2558 == stream
->ust_metadata_pushed
) {
2563 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2564 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2565 stream
->chan
->metadata_cache
->max_offset
2566 - stream
->ust_metadata_pushed
);
2567 assert(write_len
!= 0);
2568 if (write_len
< 0) {
2569 ERR("Writing one metadata packet");
2573 stream
->ust_metadata_pushed
+= write_len
;
2575 assert(stream
->chan
->metadata_cache
->max_offset
>=
2576 stream
->ust_metadata_pushed
);
2580 * Switch packet (but don't open the next one) on every commit of
2581 * a metadata packet. Since the subbuffer is fully filled (with padding,
2582 * if needed), the stream is "quiescent" after this commit.
2584 ustctl_flush_buffer(stream
->ustream
, 1);
2585 stream
->quiescent
= true;
2587 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2593 * Sync metadata meaning request them to the session daemon and snapshot to the
2594 * metadata thread can consumer them.
2596 * Metadata stream lock is held here, but we need to release it when
2597 * interacting with sessiond, else we cause a deadlock with live
2598 * awaiting on metadata to be pushed out.
2600 * The RCU read side lock must be held by the caller.
2602 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2603 * is empty or a negative value on error.
2605 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2606 struct lttng_consumer_stream
*metadata_stream
)
2610 struct lttng_consumer_channel
*metadata_channel
;
2613 assert(metadata_stream
);
2615 metadata_channel
= metadata_stream
->chan
;
2616 pthread_mutex_unlock(&metadata_stream
->lock
);
2618 * Request metadata from the sessiond, but don't wait for the flush
2619 * because we locked the metadata thread.
2621 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2622 pthread_mutex_lock(&metadata_stream
->lock
);
2628 * The metadata stream and channel can be deleted while the
2629 * metadata stream lock was released. The streamed is checked
2630 * for deletion before we use it further.
2632 * Note that it is safe to access a logically-deleted stream since its
2633 * existence is still guaranteed by the RCU read side lock. However,
2634 * it should no longer be used. The close/deletion of the metadata
2635 * channel and stream already guarantees that all metadata has been
2636 * consumed. Therefore, there is nothing left to do in this function.
2638 if (consumer_stream_is_deleted(metadata_stream
)) {
2639 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2640 metadata_stream
->key
);
2645 ret
= commit_one_metadata_packet(metadata_stream
);
2648 } else if (ret
> 0) {
2652 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2654 if (errno
!= EAGAIN
) {
2655 ERR("Sync metadata, taking UST snapshot");
2658 DBG("No new metadata when syncing them.");
2659 /* No new metadata, exit. */
2665 * After this flush, we still need to extract metadata.
2676 * Return 0 on success else a negative value.
2678 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2679 struct lttng_consumer_local_data
*ctx
)
2682 struct ustctl_consumer_stream
*ustream
;
2687 ustream
= stream
->ustream
;
2690 * First, we are going to check if there is a new subbuffer available
2691 * before reading the stream wait_fd.
2693 /* Get the next subbuffer */
2694 ret
= ustctl_get_next_subbuf(ustream
);
2696 /* No more data found, flag the stream. */
2697 stream
->has_data
= 0;
2702 ret
= ustctl_put_subbuf(ustream
);
2705 /* This stream still has data. Flag it and wake up the data thread. */
2706 stream
->has_data
= 1;
2708 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2711 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2712 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2717 /* The wake up pipe has been notified. */
2718 ctx
->has_wakeup
= 1;
2727 int update_stream_stats(struct lttng_consumer_stream
*stream
)
2730 uint64_t seq
, discarded
;
2732 ret
= ustctl_get_sequence_number(stream
->ustream
, &seq
);
2734 PERROR("ustctl_get_sequence_number");
2738 * Start the sequence when we extract the first packet in case we don't
2739 * start at 0 (for example if a consumer is not connected to the
2740 * session immediately after the beginning).
2742 if (stream
->last_sequence_number
== -1ULL) {
2743 stream
->last_sequence_number
= seq
;
2744 } else if (seq
> stream
->last_sequence_number
) {
2745 stream
->chan
->lost_packets
+= seq
-
2746 stream
->last_sequence_number
- 1;
2748 /* seq <= last_sequence_number */
2749 ERR("Sequence number inconsistent : prev = %" PRIu64
2750 ", current = %" PRIu64
,
2751 stream
->last_sequence_number
, seq
);
2755 stream
->last_sequence_number
= seq
;
2757 ret
= ustctl_get_events_discarded(stream
->ustream
, &discarded
);
2759 PERROR("kernctl_get_events_discarded");
2762 if (discarded
< stream
->last_discarded_events
) {
2764 * Overflow has occurred. We assume only one wrap-around
2767 stream
->chan
->discarded_events
+=
2768 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
2769 stream
->last_discarded_events
+ discarded
;
2771 stream
->chan
->discarded_events
+= discarded
-
2772 stream
->last_discarded_events
;
2774 stream
->last_discarded_events
= discarded
;
2782 * Read subbuffer from the given stream.
2784 * Stream and channel locks MUST be acquired by the caller.
2786 * Return 0 on success else a negative value.
2788 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2789 struct lttng_consumer_local_data
*ctx
)
2791 unsigned long len
, subbuf_size
, padding
;
2792 int err
, write_index
= 1;
2794 struct ustctl_consumer_stream
*ustream
;
2795 struct ctf_packet_index index
;
2796 const char *subbuf_addr
;
2797 struct lttng_buffer_view subbuf_view
;
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 /* Get the next subbuffer */
2832 err
= ustctl_get_next_subbuf(ustream
);
2835 * Populate metadata info if the existing info has
2836 * already been read.
2838 if (stream
->metadata_flag
) {
2839 ret
= commit_one_metadata_packet(stream
);
2846 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
2848 * This is a debug message even for single-threaded consumer,
2849 * because poll() have more relaxed criterions than get subbuf,
2850 * so get_subbuf may fail for short race windows where poll()
2851 * would issue wakeups.
2853 DBG("Reserving sub buffer failed (everything is normal, "
2854 "it is due to concurrency) [ret: %d]", err
);
2857 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
2859 if (!stream
->metadata_flag
) {
2860 index
.offset
= htobe64(stream
->out_fd_offset
);
2861 ret
= get_index_values(&index
, ustream
);
2863 err
= ustctl_put_subbuf(ustream
);
2868 /* Update the stream's sequence and discarded events count. */
2869 ret
= update_stream_stats(stream
);
2871 PERROR("kernctl_get_events_discarded");
2872 err
= ustctl_put_subbuf(ustream
);
2880 /* Get the full padded subbuffer size */
2881 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
2884 /* Get subbuffer data size (without padding) */
2885 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
2888 /* Make sure we don't get a subbuffer size bigger than the padded */
2889 assert(len
>= subbuf_size
);
2891 padding
= len
- subbuf_size
;
2893 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
2896 goto error_put_subbuf
;
2899 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
2901 /* write the subbuffer to the tracefile */
2902 ret
= lttng_consumer_on_read_subbuffer_mmap(
2903 ctx
, stream
, &subbuf_view
, padding
, &index
);
2905 * The mmap operation should write subbuf_size amount of data when
2906 * network streaming or the full padding (len) size when we are _not_
2909 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
2910 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
2912 * Display the error but continue processing to try to release the
2913 * subbuffer. This is a DBG statement since any unexpected kill or
2914 * signal, the application gets unregistered, relayd gets closed or
2915 * anything that affects the buffer lifetime will trigger this error.
2916 * So, for the sake of the user, don't print this error since it can
2917 * happen and it is OK with the code flow.
2919 DBG("Error writing to tracefile "
2920 "(ret: %ld != len: %lu != subbuf_size: %lu)",
2921 ret
, len
, subbuf_size
);
2925 err
= ustctl_put_next_subbuf(ustream
);
2929 * This will consumer the byte on the wait_fd if and only if there is not
2930 * next subbuffer to be acquired.
2932 if (!stream
->metadata_flag
) {
2933 ret
= notify_if_more_data(stream
, ctx
);
2939 /* Write index if needed. */
2944 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
2946 * In live, block until all the metadata is sent.
2948 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2949 assert(!stream
->missed_metadata_flush
);
2950 stream
->waiting_on_metadata
= true;
2951 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2953 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
2955 pthread_mutex_lock(&stream
->metadata_timer_lock
);
2956 stream
->waiting_on_metadata
= false;
2957 if (stream
->missed_metadata_flush
) {
2958 stream
->missed_metadata_flush
= false;
2959 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2960 (void) consumer_flush_ust_index(stream
);
2962 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
2970 assert(!stream
->metadata_flag
);
2971 err
= consumer_stream_write_index(stream
, &index
);
2982 * Called when a stream is created.
2984 * Return 0 on success or else a negative value.
2986 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2993 * Don't create anything if this is set for streaming or if there is
2994 * no current trace chunk on the parent channel.
2996 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
2997 stream
->chan
->trace_chunk
) {
2998 ret
= consumer_stream_create_output_files(stream
, true);
3010 * Check if data is still being extracted from the buffers for a specific
3011 * stream. Consumer data lock MUST be acquired before calling this function
3012 * and the stream lock.
3014 * Return 1 if the traced data are still getting read else 0 meaning that the
3015 * data is available for trace viewer reading.
3017 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3022 assert(stream
->ustream
);
3024 DBG("UST consumer checking data pending");
3026 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3031 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3032 uint64_t contiguous
, pushed
;
3034 /* Ease our life a bit. */
3035 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3036 pushed
= stream
->ust_metadata_pushed
;
3039 * We can simply check whether all contiguously available data
3040 * has been pushed to the ring buffer, since the push operation
3041 * is performed within get_next_subbuf(), and because both
3042 * get_next_subbuf() and put_next_subbuf() are issued atomically
3043 * thanks to the stream lock within
3044 * lttng_ustconsumer_read_subbuffer(). This basically means that
3045 * whetnever ust_metadata_pushed is incremented, the associated
3046 * metadata has been consumed from the metadata stream.
3048 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3049 contiguous
, pushed
);
3050 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3051 if ((contiguous
!= pushed
) ||
3052 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3053 ret
= 1; /* Data is pending */
3057 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3060 * There is still data so let's put back this
3063 ret
= ustctl_put_subbuf(stream
->ustream
);
3065 ret
= 1; /* Data is pending */
3070 /* Data is NOT pending so ready to be read. */
3078 * Stop a given metadata channel timer if enabled and close the wait fd which
3079 * is the poll pipe of the metadata stream.
3081 * This MUST be called with the metadata channel lock acquired.
3083 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3088 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3090 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3092 if (metadata
->switch_timer_enabled
== 1) {
3093 consumer_timer_switch_stop(metadata
);
3096 if (!metadata
->metadata_stream
) {
3101 * Closing write side so the thread monitoring the stream wakes up if any
3102 * and clean the metadata stream.
3104 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3105 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3107 PERROR("closing metadata pipe write side");
3109 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3117 * Close every metadata stream wait fd of the metadata hash table. This
3118 * function MUST be used very carefully so not to run into a race between the
3119 * metadata thread handling streams and this function closing their wait fd.
3121 * For UST, this is used when the session daemon hangs up. Its the metadata
3122 * producer so calling this is safe because we are assured that no state change
3123 * can occur in the metadata thread for the streams in the hash table.
3125 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3127 struct lttng_ht_iter iter
;
3128 struct lttng_consumer_stream
*stream
;
3130 assert(metadata_ht
);
3131 assert(metadata_ht
->ht
);
3133 DBG("UST consumer closing all metadata streams");
3136 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3139 health_code_update();
3141 pthread_mutex_lock(&stream
->chan
->lock
);
3142 lttng_ustconsumer_close_metadata(stream
->chan
);
3143 pthread_mutex_unlock(&stream
->chan
->lock
);
3149 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3153 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3155 ERR("Unable to close wakeup fd");
3160 * Please refer to consumer-timer.c before adding any lock within this
3161 * function or any of its callees. Timers have a very strict locking
3162 * semantic with respect to teardown. Failure to respect this semantic
3163 * introduces deadlocks.
3165 * DON'T hold the metadata lock when calling this function, else this
3166 * can cause deadlock involving consumer awaiting for metadata to be
3167 * pushed out due to concurrent interaction with the session daemon.
3169 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3170 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3172 struct lttcomm_metadata_request_msg request
;
3173 struct lttcomm_consumer_msg msg
;
3174 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3175 uint64_t len
, key
, offset
, version
;
3179 assert(channel
->metadata_cache
);
3181 memset(&request
, 0, sizeof(request
));
3183 /* send the metadata request to sessiond */
3184 switch (consumer_data
.type
) {
3185 case LTTNG_CONSUMER64_UST
:
3186 request
.bits_per_long
= 64;
3188 case LTTNG_CONSUMER32_UST
:
3189 request
.bits_per_long
= 32;
3192 request
.bits_per_long
= 0;
3196 request
.session_id
= channel
->session_id
;
3197 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3199 * Request the application UID here so the metadata of that application can
3200 * be sent back. The channel UID corresponds to the user UID of the session
3201 * used for the rights on the stream file(s).
3203 request
.uid
= channel
->ust_app_uid
;
3204 request
.key
= channel
->key
;
3206 DBG("Sending metadata request to sessiond, session id %" PRIu64
3207 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3208 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3211 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3213 health_code_update();
3215 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3218 ERR("Asking metadata to sessiond");
3222 health_code_update();
3224 /* Receive the metadata from sessiond */
3225 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3227 if (ret
!= sizeof(msg
)) {
3228 DBG("Consumer received unexpected message size %d (expects %zu)",
3230 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3232 * The ret value might 0 meaning an orderly shutdown but this is ok
3233 * since the caller handles this.
3238 health_code_update();
3240 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3241 /* No registry found */
3242 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3246 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3247 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3252 len
= msg
.u
.push_metadata
.len
;
3253 key
= msg
.u
.push_metadata
.key
;
3254 offset
= msg
.u
.push_metadata
.target_offset
;
3255 version
= msg
.u
.push_metadata
.version
;
3257 assert(key
== channel
->key
);
3259 DBG("No new metadata to receive for key %" PRIu64
, key
);
3262 health_code_update();
3264 /* Tell session daemon we are ready to receive the metadata. */
3265 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3266 LTTCOMM_CONSUMERD_SUCCESS
);
3267 if (ret
< 0 || len
== 0) {
3269 * Somehow, the session daemon is not responding anymore or there is
3270 * nothing to receive.
3275 health_code_update();
3277 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3278 key
, offset
, len
, version
, channel
, timer
, wait
);
3281 * Only send the status msg if the sessiond is alive meaning a positive
3284 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3289 health_code_update();
3291 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3296 * Return the ustctl call for the get stream id.
3298 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3299 uint64_t *stream_id
)
3304 return ustctl_get_stream_id(stream
->ustream
, stream_id
);