2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
12 #include <lttng/ust-ctl.h>
18 #include <sys/socket.h>
20 #include <sys/types.h>
23 #include <urcu/list.h>
28 #include <bin/lttng-consumerd/health-consumerd.h>
29 #include <common/common.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/relayd/relayd.h>
32 #include <common/compat/fcntl.h>
33 #include <common/compat/endian.h>
34 #include <common/consumer/consumer-metadata-cache.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/utils.h>
38 #include <common/index/index.h>
39 #include <common/consumer/consumer.h>
40 #include <common/shm.h>
41 #include <common/optional.h>
43 #include "ust-consumer.h"
45 #define INT_MAX_STR_LEN 12 /* includes \0 */
47 extern struct lttng_consumer_global_data consumer_data
;
48 extern int consumer_poll_timeout
;
51 * Free channel object and all streams associated with it. This MUST be used
52 * only and only if the channel has _NEVER_ been added to the global channel
55 static void destroy_channel(struct lttng_consumer_channel
*channel
)
57 struct lttng_consumer_stream
*stream
, *stmp
;
61 DBG("UST consumer cleaning stream list");
63 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
68 cds_list_del(&stream
->send_node
);
69 ustctl_destroy_stream(stream
->ustream
);
70 lttng_trace_chunk_put(stream
->trace_chunk
);
75 * If a channel is available meaning that was created before the streams
79 lttng_ustconsumer_del_channel(channel
);
80 lttng_ustconsumer_free_channel(channel
);
83 if (channel
->trace_chunk
) {
84 lttng_trace_chunk_put(channel
->trace_chunk
);
91 * Add channel to internal consumer state.
93 * Returns 0 on success or else a negative value.
95 static int add_channel(struct lttng_consumer_channel
*channel
,
96 struct lttng_consumer_local_data
*ctx
)
103 if (ctx
->on_recv_channel
!= NULL
) {
104 ret
= ctx
->on_recv_channel(channel
);
106 ret
= consumer_add_channel(channel
, ctx
);
107 } else if (ret
< 0) {
108 /* Most likely an ENOMEM. */
109 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
113 ret
= consumer_add_channel(channel
, ctx
);
116 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
123 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
124 * error value if applicable is set in it else it is kept untouched.
126 * Return NULL on error else the newly allocated stream object.
128 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
129 struct lttng_consumer_channel
*channel
,
130 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
133 struct lttng_consumer_stream
*stream
= NULL
;
138 stream
= consumer_stream_create(
145 channel
->trace_chunk
,
150 if (stream
== NULL
) {
154 * We could not find the channel. Can happen if cpu hotplug
155 * happens while tearing down.
157 DBG3("Could not find channel");
162 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
168 consumer_stream_update_channel_attributes(stream
, channel
);
172 *_alloc_ret
= alloc_ret
;
178 * Send the given stream pointer to the corresponding thread.
180 * Returns 0 on success else a negative value.
182 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
183 struct lttng_consumer_local_data
*ctx
)
186 struct lttng_pipe
*stream_pipe
;
188 /* Get the right pipe where the stream will be sent. */
189 if (stream
->metadata_flag
) {
190 consumer_add_metadata_stream(stream
);
191 stream_pipe
= ctx
->consumer_metadata_pipe
;
193 consumer_add_data_stream(stream
);
194 stream_pipe
= ctx
->consumer_data_pipe
;
198 * From this point on, the stream's ownership has been moved away from
199 * the channel and it becomes globally visible. Hence, remove it from
200 * the local stream list to prevent the stream from being both local and
203 stream
->globally_visible
= 1;
204 cds_list_del(&stream
->send_node
);
206 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
208 ERR("Consumer write %s stream to pipe %d",
209 stream
->metadata_flag
? "metadata" : "data",
210 lttng_pipe_get_writefd(stream_pipe
));
211 if (stream
->metadata_flag
) {
212 consumer_del_stream_for_metadata(stream
);
214 consumer_del_stream_for_data(stream
);
224 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
226 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
229 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
230 stream_shm_path
[PATH_MAX
- 1] = '\0';
231 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
236 strncat(stream_shm_path
, cpu_nr
,
237 PATH_MAX
- strlen(stream_shm_path
) - 1);
244 * Create streams for the given channel using liblttng-ust-ctl.
245 * The channel lock must be acquired by the caller.
247 * Return 0 on success else a negative value.
249 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
250 struct lttng_consumer_local_data
*ctx
)
253 struct ustctl_consumer_stream
*ustream
;
254 struct lttng_consumer_stream
*stream
;
255 pthread_mutex_t
*current_stream_lock
= NULL
;
261 * While a stream is available from ustctl. When NULL is returned, we've
262 * reached the end of the possible stream for the channel.
264 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
266 int ust_metadata_pipe
[2];
268 health_code_update();
270 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
271 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
273 ERR("Create ust metadata poll pipe");
276 wait_fd
= ust_metadata_pipe
[0];
278 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
281 /* Allocate consumer stream object. */
282 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
286 stream
->ustream
= ustream
;
288 * Store it so we can save multiple function calls afterwards since
289 * this value is used heavily in the stream threads. This is UST
290 * specific so this is why it's done after allocation.
292 stream
->wait_fd
= wait_fd
;
295 * Increment channel refcount since the channel reference has now been
296 * assigned in the allocation process above.
298 if (stream
->chan
->monitor
) {
299 uatomic_inc(&stream
->chan
->refcount
);
302 pthread_mutex_lock(&stream
->lock
);
303 current_stream_lock
= &stream
->lock
;
305 * Order is important this is why a list is used. On error, the caller
306 * should clean this list.
308 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
310 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
311 &stream
->max_sb_size
);
313 ERR("ustctl_get_max_subbuf_size failed for stream %s",
318 /* Do actions once stream has been received. */
319 if (ctx
->on_recv_stream
) {
320 ret
= ctx
->on_recv_stream(stream
);
326 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
327 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
329 /* Set next CPU stream. */
330 channel
->streams
.count
= ++cpu
;
332 /* Keep stream reference when creating metadata. */
333 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
334 channel
->metadata_stream
= stream
;
335 if (channel
->monitor
) {
336 /* Set metadata poll pipe if we created one */
337 memcpy(stream
->ust_metadata_poll_pipe
,
339 sizeof(ust_metadata_pipe
));
342 pthread_mutex_unlock(&stream
->lock
);
343 current_stream_lock
= NULL
;
350 if (current_stream_lock
) {
351 pthread_mutex_unlock(current_stream_lock
);
356 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
357 const struct lttng_credentials
*session_credentials
)
359 char shm_path
[PATH_MAX
];
362 if (!channel
->shm_path
[0]) {
363 return shm_create_anonymous("ust-consumer");
365 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
369 return run_as_open(shm_path
,
370 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
371 lttng_credentials_get_uid(session_credentials
),
372 lttng_credentials_get_gid(session_credentials
));
379 * Create an UST channel with the given attributes and send it to the session
380 * daemon using the ust ctl API.
382 * Return 0 on success or else a negative value.
384 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
385 struct ustctl_consumer_channel_attr
*attr
,
386 struct ustctl_consumer_channel
**ust_chanp
)
388 int ret
, nr_stream_fds
, i
, j
;
390 struct ustctl_consumer_channel
*ust_channel
;
395 assert(channel
->buffer_credentials
.is_set
);
397 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
398 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
399 "switch_timer_interval: %u, read_timer_interval: %u, "
400 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
401 attr
->num_subbuf
, attr
->switch_timer_interval
,
402 attr
->read_timer_interval
, attr
->output
, attr
->type
);
404 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
407 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
408 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
413 for (i
= 0; i
< nr_stream_fds
; i
++) {
414 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
415 &channel
->buffer_credentials
.value
);
416 if (stream_fds
[i
] < 0) {
421 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
426 channel
->nr_stream_fds
= nr_stream_fds
;
427 channel
->stream_fds
= stream_fds
;
428 *ust_chanp
= ust_channel
;
434 for (j
= i
- 1; j
>= 0; j
--) {
437 closeret
= close(stream_fds
[j
]);
441 if (channel
->shm_path
[0]) {
442 char shm_path
[PATH_MAX
];
444 closeret
= get_stream_shm_path(shm_path
,
445 channel
->shm_path
, j
);
447 ERR("Cannot get stream shm path");
449 closeret
= run_as_unlink(shm_path
,
450 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
451 channel
->buffer_credentials
)),
452 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
453 channel
->buffer_credentials
)));
455 PERROR("unlink %s", shm_path
);
459 /* Try to rmdir all directories under shm_path root. */
460 if (channel
->root_shm_path
[0]) {
461 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
462 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
463 channel
->buffer_credentials
)),
464 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
465 channel
->buffer_credentials
)),
466 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
474 * Send a single given stream to the session daemon using the sock.
476 * Return 0 on success else a negative value.
478 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
485 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
487 /* Send stream to session daemon. */
488 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
498 * Send channel to sessiond and relayd if applicable.
500 * Return 0 on success or else a negative value.
502 static int send_channel_to_sessiond_and_relayd(int sock
,
503 struct lttng_consumer_channel
*channel
,
504 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
506 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
507 struct lttng_consumer_stream
*stream
;
508 uint64_t net_seq_idx
= -1ULL;
514 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
516 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
517 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
519 health_code_update();
521 /* Try to send the stream to the relayd if one is available. */
522 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
523 stream
->key
, channel
->name
);
524 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
527 * Flag that the relayd was the problem here probably due to a
528 * communicaton error on the socket.
533 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
535 if (net_seq_idx
== -1ULL) {
536 net_seq_idx
= stream
->net_seq_idx
;
541 /* Inform sessiond that we are about to send channel and streams. */
542 ret
= consumer_send_status_msg(sock
, ret_code
);
543 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
545 * Either the session daemon is not responding or the relayd died so we
551 /* Send channel to sessiond. */
552 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
557 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
562 /* The channel was sent successfully to the sessiond at this point. */
563 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
565 health_code_update();
567 /* Send stream to session daemon. */
568 ret
= send_sessiond_stream(sock
, stream
);
574 /* Tell sessiond there is no more stream. */
575 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
580 DBG("UST consumer NULL stream sent to sessiond");
585 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
592 * Creates a channel and streams and add the channel it to the channel internal
593 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
596 * Return 0 on success or else, a negative value is returned and the channel
597 * MUST be destroyed by consumer_del_channel().
599 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
600 struct lttng_consumer_channel
*channel
,
601 struct ustctl_consumer_channel_attr
*attr
)
610 * This value is still used by the kernel consumer since for the kernel,
611 * the stream ownership is not IN the consumer so we need to have the
612 * number of left stream that needs to be initialized so we can know when
613 * to delete the channel (see consumer.c).
615 * As for the user space tracer now, the consumer creates and sends the
616 * stream to the session daemon which only sends them to the application
617 * once every stream of a channel is received making this value useless
618 * because we they will be added to the poll thread before the application
619 * receives them. This ensures that a stream can not hang up during
620 * initilization of a channel.
622 channel
->nb_init_stream_left
= 0;
624 /* The reply msg status is handled in the following call. */
625 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
630 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
633 * For the snapshots (no monitor), we create the metadata streams
634 * on demand, not during the channel creation.
636 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
641 /* Open all streams for this channel. */
642 pthread_mutex_lock(&channel
->lock
);
643 ret
= create_ust_streams(channel
, ctx
);
644 pthread_mutex_unlock(&channel
->lock
);
654 * Send all stream of a channel to the right thread handling it.
656 * On error, return a negative value else 0 on success.
658 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
659 struct lttng_consumer_local_data
*ctx
)
662 struct lttng_consumer_stream
*stream
, *stmp
;
667 /* Send streams to the corresponding thread. */
668 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
671 health_code_update();
673 /* Sending the stream to the thread. */
674 ret
= send_stream_to_thread(stream
, ctx
);
677 * If we are unable to send the stream to the thread, there is
678 * a big problem so just stop everything.
689 * Flush channel's streams using the given key to retrieve the channel.
691 * Return 0 on success else an LTTng error code.
693 static int flush_channel(uint64_t chan_key
)
696 struct lttng_consumer_channel
*channel
;
697 struct lttng_consumer_stream
*stream
;
699 struct lttng_ht_iter iter
;
701 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
704 channel
= consumer_find_channel(chan_key
);
706 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
707 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
711 ht
= consumer_data
.stream_per_chan_id_ht
;
713 /* For each stream of the channel id, flush it. */
714 cds_lfht_for_each_entry_duplicate(ht
->ht
,
715 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
716 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
718 health_code_update();
720 pthread_mutex_lock(&stream
->lock
);
723 * Protect against concurrent teardown of a stream.
725 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
729 if (!stream
->quiescent
) {
730 ustctl_flush_buffer(stream
->ustream
, 0);
731 stream
->quiescent
= true;
734 pthread_mutex_unlock(&stream
->lock
);
742 * Clear quiescent state from channel's streams using the given key to
743 * retrieve the channel.
745 * Return 0 on success else an LTTng error code.
747 static int clear_quiescent_channel(uint64_t chan_key
)
750 struct lttng_consumer_channel
*channel
;
751 struct lttng_consumer_stream
*stream
;
753 struct lttng_ht_iter iter
;
755 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
758 channel
= consumer_find_channel(chan_key
);
760 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
761 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
765 ht
= consumer_data
.stream_per_chan_id_ht
;
767 /* For each stream of the channel id, clear quiescent state. */
768 cds_lfht_for_each_entry_duplicate(ht
->ht
,
769 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
770 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
772 health_code_update();
774 pthread_mutex_lock(&stream
->lock
);
775 stream
->quiescent
= false;
776 pthread_mutex_unlock(&stream
->lock
);
784 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
786 * Return 0 on success else an LTTng error code.
788 static int close_metadata(uint64_t chan_key
)
791 struct lttng_consumer_channel
*channel
;
792 unsigned int channel_monitor
;
794 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
796 channel
= consumer_find_channel(chan_key
);
799 * This is possible if the metadata thread has issue a delete because
800 * the endpoint point of the stream hung up. There is no way the
801 * session daemon can know about it thus use a DBG instead of an actual
804 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
805 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
809 pthread_mutex_lock(&consumer_data
.lock
);
810 pthread_mutex_lock(&channel
->lock
);
811 channel_monitor
= channel
->monitor
;
812 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
816 lttng_ustconsumer_close_metadata(channel
);
817 pthread_mutex_unlock(&channel
->lock
);
818 pthread_mutex_unlock(&consumer_data
.lock
);
821 * The ownership of a metadata channel depends on the type of
822 * session to which it belongs. In effect, the monitor flag is checked
823 * to determine if this metadata channel is in "snapshot" mode or not.
825 * In the non-snapshot case, the metadata channel is created along with
826 * a single stream which will remain present until the metadata channel
827 * is destroyed (on the destruction of its session). In this case, the
828 * metadata stream in "monitored" by the metadata poll thread and holds
829 * the ownership of its channel.
831 * Closing the metadata will cause the metadata stream's "metadata poll
832 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
833 * thread which will teardown the metadata stream which, in return,
834 * deletes the metadata channel.
836 * In the snapshot case, the metadata stream is created and destroyed
837 * on every snapshot record. Since the channel doesn't have an owner
838 * other than the session daemon, it is safe to destroy it immediately
839 * on reception of the CLOSE_METADATA command.
841 if (!channel_monitor
) {
843 * The channel and consumer_data locks must be
844 * released before this call since consumer_del_channel
845 * re-acquires the channel and consumer_data locks to teardown
846 * the channel and queue its reclamation by the "call_rcu"
849 consumer_del_channel(channel
);
854 pthread_mutex_unlock(&channel
->lock
);
855 pthread_mutex_unlock(&consumer_data
.lock
);
861 * RCU read side lock MUST be acquired before calling this function.
863 * Return 0 on success else an LTTng error code.
865 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
868 struct lttng_consumer_channel
*metadata
;
870 DBG("UST consumer setup metadata key %" PRIu64
, key
);
872 metadata
= consumer_find_channel(key
);
874 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
875 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
880 * In no monitor mode, the metadata channel has no stream(s) so skip the
881 * ownership transfer to the metadata thread.
883 if (!metadata
->monitor
) {
884 DBG("Metadata channel in no monitor");
890 * Send metadata stream to relayd if one available. Availability is
891 * known if the stream is still in the list of the channel.
893 if (cds_list_empty(&metadata
->streams
.head
)) {
894 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
895 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
896 goto error_no_stream
;
899 /* Send metadata stream to relayd if needed. */
900 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
901 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
904 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
907 ret
= consumer_send_relayd_streams_sent(
908 metadata
->metadata_stream
->net_seq_idx
);
910 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
916 * Ownership of metadata stream is passed along. Freeing is handled by
919 ret
= send_streams_to_thread(metadata
, ctx
);
922 * If we are unable to send the stream to the thread, there is
923 * a big problem so just stop everything.
925 ret
= LTTCOMM_CONSUMERD_FATAL
;
926 goto send_streams_error
;
928 /* List MUST be empty after or else it could be reused. */
929 assert(cds_list_empty(&metadata
->streams
.head
));
936 * Delete metadata channel on error. At this point, the metadata stream can
937 * NOT be monitored by the metadata thread thus having the guarantee that
938 * the stream is still in the local stream list of the channel. This call
939 * will make sure to clean that list.
941 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
942 cds_list_del(&metadata
->metadata_stream
->send_node
);
943 metadata
->metadata_stream
= NULL
;
951 * Snapshot the whole metadata.
952 * RCU read-side lock must be held by the caller.
954 * Returns 0 on success, < 0 on error
956 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
957 uint64_t key
, char *path
, uint64_t relayd_id
,
958 struct lttng_consumer_local_data
*ctx
)
961 struct lttng_consumer_stream
*metadata_stream
;
966 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
971 assert(!metadata_channel
->monitor
);
973 health_code_update();
976 * Ask the sessiond if we have new metadata waiting and update the
977 * consumer metadata cache.
979 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
984 health_code_update();
987 * The metadata stream is NOT created in no monitor mode when the channel
988 * is created on a sessiond ask channel command.
990 ret
= create_ust_streams(metadata_channel
, ctx
);
995 metadata_stream
= metadata_channel
->metadata_stream
;
996 assert(metadata_stream
);
998 pthread_mutex_lock(&metadata_stream
->lock
);
999 if (relayd_id
!= (uint64_t) -1ULL) {
1000 metadata_stream
->net_seq_idx
= relayd_id
;
1001 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1003 ret
= consumer_stream_create_output_files(metadata_stream
,
1006 pthread_mutex_unlock(&metadata_stream
->lock
);
1012 health_code_update();
1014 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1022 * Clean up the stream completly because the next snapshot will use a new
1025 consumer_stream_destroy(metadata_stream
, NULL
);
1026 cds_list_del(&metadata_stream
->send_node
);
1027 metadata_channel
->metadata_stream
= NULL
;
1035 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1039 unsigned long mmap_offset
;
1040 const char *mmap_base
;
1042 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1044 ERR("Failed to get mmap base for stream `%s`",
1050 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1052 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1057 *addr
= mmap_base
+ mmap_offset
;
1064 * Take a snapshot of all the stream of a channel.
1065 * RCU read-side lock and the channel lock must be held by the caller.
1067 * Returns 0 on success, < 0 on error
1069 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1070 uint64_t key
, char *path
, uint64_t relayd_id
,
1071 uint64_t nb_packets_per_stream
,
1072 struct lttng_consumer_local_data
*ctx
)
1075 unsigned use_relayd
= 0;
1076 unsigned long consumed_pos
, produced_pos
;
1077 struct lttng_consumer_stream
*stream
;
1084 if (relayd_id
!= (uint64_t) -1ULL) {
1088 assert(!channel
->monitor
);
1089 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1091 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1092 health_code_update();
1094 /* Lock stream because we are about to change its state. */
1095 pthread_mutex_lock(&stream
->lock
);
1096 assert(channel
->trace_chunk
);
1097 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1099 * Can't happen barring an internal error as the channel
1100 * holds a reference to the trace chunk.
1102 ERR("Failed to acquire reference to channel's trace chunk");
1106 assert(!stream
->trace_chunk
);
1107 stream
->trace_chunk
= channel
->trace_chunk
;
1109 stream
->net_seq_idx
= relayd_id
;
1112 ret
= consumer_send_relayd_stream(stream
, path
);
1117 ret
= consumer_stream_create_output_files(stream
,
1122 DBG("UST consumer snapshot stream (%" PRIu64
")",
1127 * If tracing is active, we want to perform a "full" buffer flush.
1128 * Else, if quiescent, it has already been done by the prior stop.
1130 if (!stream
->quiescent
) {
1131 ustctl_flush_buffer(stream
->ustream
, 0);
1134 ret
= lttng_ustconsumer_take_snapshot(stream
);
1136 ERR("Taking UST snapshot");
1140 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1142 ERR("Produced UST snapshot position");
1146 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1148 ERR("Consumerd UST snapshot position");
1153 * The original value is sent back if max stream size is larger than
1154 * the possible size of the snapshot. Also, we assume that the session
1155 * daemon should never send a maximum stream size that is lower than
1158 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1159 produced_pos
, nb_packets_per_stream
,
1160 stream
->max_sb_size
);
1162 while ((long) (consumed_pos
- produced_pos
) < 0) {
1164 unsigned long len
, padded_len
;
1165 const char *subbuf_addr
;
1166 struct lttng_buffer_view subbuf_view
;
1168 health_code_update();
1170 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1172 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1174 if (ret
!= -EAGAIN
) {
1175 PERROR("ustctl_get_subbuf snapshot");
1176 goto error_close_stream
;
1178 DBG("UST consumer get subbuf failed. Skipping it.");
1179 consumed_pos
+= stream
->max_sb_size
;
1180 stream
->chan
->lost_packets
++;
1184 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1186 ERR("Snapshot ustctl_get_subbuf_size");
1187 goto error_put_subbuf
;
1190 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1192 ERR("Snapshot ustctl_get_padded_subbuf_size");
1193 goto error_put_subbuf
;
1196 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1198 goto error_put_subbuf
;
1201 subbuf_view
= lttng_buffer_view_init(
1202 subbuf_addr
, 0, padded_len
);
1203 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1204 stream
, &subbuf_view
, padded_len
- len
);
1206 if (read_len
!= len
) {
1208 goto error_put_subbuf
;
1211 if (read_len
!= padded_len
) {
1213 goto error_put_subbuf
;
1217 ret
= ustctl_put_subbuf(stream
->ustream
);
1219 ERR("Snapshot ustctl_put_subbuf");
1220 goto error_close_stream
;
1222 consumed_pos
+= stream
->max_sb_size
;
1225 /* Simply close the stream so we can use it on the next snapshot. */
1226 consumer_stream_close(stream
);
1227 pthread_mutex_unlock(&stream
->lock
);
1234 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1235 ERR("Snapshot ustctl_put_subbuf");
1238 consumer_stream_close(stream
);
1240 pthread_mutex_unlock(&stream
->lock
);
1246 * Receive the metadata updates from the sessiond. Supports receiving
1247 * overlapping metadata, but is needs to always belong to a contiguous
1248 * range starting from 0.
1249 * Be careful about the locks held when calling this function: it needs
1250 * the metadata cache flush to concurrently progress in order to
1253 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1254 uint64_t len
, uint64_t version
,
1255 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1257 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1260 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1262 metadata_str
= zmalloc(len
* sizeof(char));
1263 if (!metadata_str
) {
1264 PERROR("zmalloc metadata string");
1265 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1269 health_code_update();
1271 /* Receive metadata string. */
1272 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1274 /* Session daemon is dead so return gracefully. */
1279 health_code_update();
1281 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1282 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1285 /* Unable to handle metadata. Notify session daemon. */
1286 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1288 * Skip metadata flush on write error since the offset and len might
1289 * not have been updated which could create an infinite loop below when
1290 * waiting for the metadata cache to be flushed.
1292 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1295 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1300 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1301 DBG("Waiting for metadata to be flushed");
1303 health_code_update();
1305 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1315 * Receive command from session daemon and process it.
1317 * Return 1 on success else a negative value or 0.
1319 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1320 int sock
, struct pollfd
*consumer_sockpoll
)
1323 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1324 struct lttcomm_consumer_msg msg
;
1325 struct lttng_consumer_channel
*channel
= NULL
;
1327 health_code_update();
1329 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1330 if (ret
!= sizeof(msg
)) {
1331 DBG("Consumer received unexpected message size %zd (expects %zu)",
1334 * The ret value might 0 meaning an orderly shutdown but this is ok
1335 * since the caller handles this.
1338 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1344 health_code_update();
1347 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1349 health_code_update();
1351 /* relayd needs RCU read-side lock */
1354 switch (msg
.cmd_type
) {
1355 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1357 /* Session daemon status message are handled in the following call. */
1358 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1359 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1360 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1361 msg
.u
.relayd_sock
.relayd_session_id
);
1364 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1366 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1367 struct consumer_relayd_sock_pair
*relayd
;
1369 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1371 /* Get relayd reference if exists. */
1372 relayd
= consumer_find_relayd(index
);
1373 if (relayd
== NULL
) {
1374 DBG("Unable to find relayd %" PRIu64
, index
);
1375 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1379 * Each relayd socket pair has a refcount of stream attached to it
1380 * which tells if the relayd is still active or not depending on the
1383 * This will set the destroy flag of the relayd object and destroy it
1384 * if the refcount reaches zero when called.
1386 * The destroy can happen either here or when a stream fd hangs up.
1389 consumer_flag_relayd_for_destroy(relayd
);
1392 goto end_msg_sessiond
;
1394 case LTTNG_CONSUMER_UPDATE_STREAM
:
1399 case LTTNG_CONSUMER_DATA_PENDING
:
1401 int ret
, is_data_pending
;
1402 uint64_t id
= msg
.u
.data_pending
.session_id
;
1404 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1406 is_data_pending
= consumer_data_pending(id
);
1408 /* Send back returned value to session daemon */
1409 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1410 sizeof(is_data_pending
));
1412 DBG("Error when sending the data pending ret code: %d", ret
);
1417 * No need to send back a status message since the data pending
1418 * returned value is the response.
1422 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1425 struct ustctl_consumer_channel_attr attr
;
1426 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1427 const struct lttng_credentials buffer_credentials
= {
1428 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1429 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1432 /* Create a plain object and reserve a channel key. */
1433 channel
= consumer_allocate_channel(
1434 msg
.u
.ask_channel
.key
,
1435 msg
.u
.ask_channel
.session_id
,
1436 msg
.u
.ask_channel
.chunk_id
.is_set
?
1438 msg
.u
.ask_channel
.pathname
,
1439 msg
.u
.ask_channel
.name
,
1440 msg
.u
.ask_channel
.relayd_id
,
1441 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1442 msg
.u
.ask_channel
.tracefile_size
,
1443 msg
.u
.ask_channel
.tracefile_count
,
1444 msg
.u
.ask_channel
.session_id_per_pid
,
1445 msg
.u
.ask_channel
.monitor
,
1446 msg
.u
.ask_channel
.live_timer_interval
,
1447 msg
.u
.ask_channel
.is_live
,
1448 msg
.u
.ask_channel
.root_shm_path
,
1449 msg
.u
.ask_channel
.shm_path
);
1451 goto end_channel_error
;
1454 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1455 buffer_credentials
);
1458 * Assign UST application UID to the channel. This value is ignored for
1459 * per PID buffers. This is specific to UST thus setting this after the
1462 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1464 /* Build channel attributes from received message. */
1465 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1466 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1467 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1468 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1469 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1470 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1471 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1472 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1474 /* Match channel buffer type to the UST abi. */
1475 switch (msg
.u
.ask_channel
.output
) {
1476 case LTTNG_EVENT_MMAP
:
1478 attr
.output
= LTTNG_UST_MMAP
;
1482 /* Translate and save channel type. */
1483 switch (msg
.u
.ask_channel
.type
) {
1484 case LTTNG_UST_CHAN_PER_CPU
:
1485 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1486 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1488 * Set refcount to 1 for owner. Below, we will
1489 * pass ownership to the
1490 * consumer_thread_channel_poll() thread.
1492 channel
->refcount
= 1;
1494 case LTTNG_UST_CHAN_METADATA
:
1495 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1496 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1503 health_code_update();
1505 ret
= ask_channel(ctx
, channel
, &attr
);
1507 goto end_channel_error
;
1510 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1511 ret
= consumer_metadata_cache_allocate(channel
);
1513 ERR("Allocating metadata cache");
1514 goto end_channel_error
;
1516 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1517 attr
.switch_timer_interval
= 0;
1519 int monitor_start_ret
;
1521 consumer_timer_live_start(channel
,
1522 msg
.u
.ask_channel
.live_timer_interval
);
1523 monitor_start_ret
= consumer_timer_monitor_start(
1525 msg
.u
.ask_channel
.monitor_timer_interval
);
1526 if (monitor_start_ret
< 0) {
1527 ERR("Starting channel monitoring timer failed");
1528 goto end_channel_error
;
1532 health_code_update();
1535 * Add the channel to the internal state AFTER all streams were created
1536 * and successfully sent to session daemon. This way, all streams must
1537 * be ready before this channel is visible to the threads.
1538 * If add_channel succeeds, ownership of the channel is
1539 * passed to consumer_thread_channel_poll().
1541 ret
= add_channel(channel
, ctx
);
1543 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1544 if (channel
->switch_timer_enabled
== 1) {
1545 consumer_timer_switch_stop(channel
);
1547 consumer_metadata_cache_destroy(channel
);
1549 if (channel
->live_timer_enabled
== 1) {
1550 consumer_timer_live_stop(channel
);
1552 if (channel
->monitor_timer_enabled
== 1) {
1553 consumer_timer_monitor_stop(channel
);
1555 goto end_channel_error
;
1558 health_code_update();
1561 * Channel and streams are now created. Inform the session daemon that
1562 * everything went well and should wait to receive the channel and
1563 * streams with ustctl API.
1565 ret
= consumer_send_status_channel(sock
, channel
);
1568 * There is probably a problem on the socket.
1575 case LTTNG_CONSUMER_GET_CHANNEL
:
1577 int ret
, relayd_err
= 0;
1578 uint64_t key
= msg
.u
.get_channel
.key
;
1579 struct lttng_consumer_channel
*channel
;
1581 channel
= consumer_find_channel(key
);
1583 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1584 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1585 goto end_get_channel
;
1588 health_code_update();
1590 /* Send the channel to sessiond (and relayd, if applicable). */
1591 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1596 * We were unable to send to the relayd the stream so avoid
1597 * sending back a fatal error to the thread since this is OK
1598 * and the consumer can continue its work. The above call
1599 * has sent the error status message to the sessiond.
1601 goto end_get_channel_nosignal
;
1604 * The communicaton was broken hence there is a bad state between
1605 * the consumer and sessiond so stop everything.
1607 goto error_get_channel_fatal
;
1610 health_code_update();
1613 * In no monitor mode, the streams ownership is kept inside the channel
1614 * so don't send them to the data thread.
1616 if (!channel
->monitor
) {
1617 goto end_get_channel
;
1620 ret
= send_streams_to_thread(channel
, ctx
);
1623 * If we are unable to send the stream to the thread, there is
1624 * a big problem so just stop everything.
1626 goto error_get_channel_fatal
;
1628 /* List MUST be empty after or else it could be reused. */
1629 assert(cds_list_empty(&channel
->streams
.head
));
1631 goto end_msg_sessiond
;
1632 error_get_channel_fatal
:
1634 end_get_channel_nosignal
:
1637 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1639 uint64_t key
= msg
.u
.destroy_channel
.key
;
1642 * Only called if streams have not been sent to stream
1643 * manager thread. However, channel has been sent to
1644 * channel manager thread.
1646 notify_thread_del_channel(ctx
, key
);
1647 goto end_msg_sessiond
;
1649 case LTTNG_CONSUMER_CLOSE_METADATA
:
1653 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1658 goto end_msg_sessiond
;
1660 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1664 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1669 goto end_msg_sessiond
;
1671 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1675 ret
= clear_quiescent_channel(
1676 msg
.u
.clear_quiescent_channel
.key
);
1681 goto end_msg_sessiond
;
1683 case LTTNG_CONSUMER_PUSH_METADATA
:
1686 uint64_t len
= msg
.u
.push_metadata
.len
;
1687 uint64_t key
= msg
.u
.push_metadata
.key
;
1688 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1689 uint64_t version
= msg
.u
.push_metadata
.version
;
1690 struct lttng_consumer_channel
*channel
;
1692 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1695 channel
= consumer_find_channel(key
);
1698 * This is possible if the metadata creation on the consumer side
1699 * is in flight vis-a-vis a concurrent push metadata from the
1700 * session daemon. Simply return that the channel failed and the
1701 * session daemon will handle that message correctly considering
1702 * that this race is acceptable thus the DBG() statement here.
1704 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1705 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1706 goto end_push_metadata_msg_sessiond
;
1709 health_code_update();
1713 * There is nothing to receive. We have simply
1714 * checked whether the channel can be found.
1716 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1717 goto end_push_metadata_msg_sessiond
;
1720 /* Tell session daemon we are ready to receive the metadata. */
1721 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1723 /* Somehow, the session daemon is not responding anymore. */
1724 goto error_push_metadata_fatal
;
1727 health_code_update();
1729 /* Wait for more data. */
1730 health_poll_entry();
1731 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1734 goto error_push_metadata_fatal
;
1737 health_code_update();
1739 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1740 len
, version
, channel
, 0, 1);
1742 /* error receiving from sessiond */
1743 goto error_push_metadata_fatal
;
1746 goto end_push_metadata_msg_sessiond
;
1748 end_push_metadata_msg_sessiond
:
1749 goto end_msg_sessiond
;
1750 error_push_metadata_fatal
:
1753 case LTTNG_CONSUMER_SETUP_METADATA
:
1757 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1761 goto end_msg_sessiond
;
1763 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1765 struct lttng_consumer_channel
*channel
;
1766 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1768 channel
= consumer_find_channel(key
);
1770 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1771 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1773 if (msg
.u
.snapshot_channel
.metadata
) {
1774 ret
= snapshot_metadata(channel
, key
,
1775 msg
.u
.snapshot_channel
.pathname
,
1776 msg
.u
.snapshot_channel
.relayd_id
,
1779 ERR("Snapshot metadata failed");
1780 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1783 ret
= snapshot_channel(channel
, key
,
1784 msg
.u
.snapshot_channel
.pathname
,
1785 msg
.u
.snapshot_channel
.relayd_id
,
1786 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1789 ERR("Snapshot channel failed");
1790 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1794 health_code_update();
1795 ret
= consumer_send_status_msg(sock
, ret_code
);
1797 /* Somehow, the session daemon is not responding anymore. */
1800 health_code_update();
1803 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1806 uint64_t discarded_events
;
1807 struct lttng_ht_iter iter
;
1808 struct lttng_ht
*ht
;
1809 struct lttng_consumer_stream
*stream
;
1810 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1811 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1813 DBG("UST consumer discarded events command for session id %"
1816 pthread_mutex_lock(&consumer_data
.lock
);
1818 ht
= consumer_data
.stream_list_ht
;
1821 * We only need a reference to the channel, but they are not
1822 * directly indexed, so we just use the first matching stream
1823 * to extract the information we need, we default to 0 if not
1824 * found (no events are dropped if the channel is not yet in
1827 discarded_events
= 0;
1828 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1829 ht
->hash_fct(&id
, lttng_ht_seed
),
1831 &iter
.iter
, stream
, node_session_id
.node
) {
1832 if (stream
->chan
->key
== key
) {
1833 discarded_events
= stream
->chan
->discarded_events
;
1837 pthread_mutex_unlock(&consumer_data
.lock
);
1840 DBG("UST consumer discarded events command for session id %"
1841 PRIu64
", channel key %" PRIu64
, id
, key
);
1843 health_code_update();
1845 /* Send back returned value to session daemon */
1846 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1848 PERROR("send discarded events");
1854 case LTTNG_CONSUMER_LOST_PACKETS
:
1857 uint64_t lost_packets
;
1858 struct lttng_ht_iter iter
;
1859 struct lttng_ht
*ht
;
1860 struct lttng_consumer_stream
*stream
;
1861 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1862 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1864 DBG("UST consumer lost packets command for session id %"
1867 pthread_mutex_lock(&consumer_data
.lock
);
1869 ht
= consumer_data
.stream_list_ht
;
1872 * We only need a reference to the channel, but they are not
1873 * directly indexed, so we just use the first matching stream
1874 * to extract the information we need, we default to 0 if not
1875 * found (no packets lost if the channel is not yet in use).
1878 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1879 ht
->hash_fct(&id
, lttng_ht_seed
),
1881 &iter
.iter
, stream
, node_session_id
.node
) {
1882 if (stream
->chan
->key
== key
) {
1883 lost_packets
= stream
->chan
->lost_packets
;
1887 pthread_mutex_unlock(&consumer_data
.lock
);
1890 DBG("UST consumer lost packets command for session id %"
1891 PRIu64
", channel key %" PRIu64
, id
, key
);
1893 health_code_update();
1895 /* Send back returned value to session daemon */
1896 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1897 sizeof(lost_packets
));
1899 PERROR("send lost packets");
1905 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1907 int channel_monitor_pipe
;
1909 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1910 /* Successfully received the command's type. */
1911 ret
= consumer_send_status_msg(sock
, ret_code
);
1916 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1918 if (ret
!= sizeof(channel_monitor_pipe
)) {
1919 ERR("Failed to receive channel monitor pipe");
1923 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1924 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1925 channel_monitor_pipe
);
1929 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1930 /* Set the pipe as non-blocking. */
1931 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1933 PERROR("fcntl get flags of the channel monitoring pipe");
1938 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1939 flags
| O_NONBLOCK
);
1941 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1944 DBG("Channel monitor pipe set as non-blocking");
1946 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1948 goto end_msg_sessiond
;
1950 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1952 struct lttng_consumer_channel
*channel
;
1953 uint64_t key
= msg
.u
.rotate_channel
.key
;
1955 channel
= consumer_find_channel(key
);
1957 DBG("Channel %" PRIu64
" not found", key
);
1958 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1961 * Sample the rotate position of all the streams in
1964 ret
= lttng_consumer_rotate_channel(channel
, key
,
1965 msg
.u
.rotate_channel
.relayd_id
,
1966 msg
.u
.rotate_channel
.metadata
,
1969 ERR("Rotate channel failed");
1970 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1973 health_code_update();
1975 ret
= consumer_send_status_msg(sock
, ret_code
);
1977 /* Somehow, the session daemon is not responding anymore. */
1978 goto end_rotate_channel_nosignal
;
1982 * Rotate the streams that are ready right now.
1983 * FIXME: this is a second consecutive iteration over the
1984 * streams in a channel, there is probably a better way to
1985 * handle this, but it needs to be after the
1986 * consumer_send_status_msg() call.
1989 ret
= lttng_consumer_rotate_ready_streams(
1992 ERR("Rotate channel failed");
1996 end_rotate_channel_nosignal
:
1999 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2001 struct lttng_consumer_channel
*channel
;
2002 uint64_t key
= msg
.u
.clear_channel
.key
;
2004 channel
= consumer_find_channel(key
);
2006 DBG("Channel %" PRIu64
" not found", key
);
2007 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2009 ret
= lttng_consumer_clear_channel(channel
);
2011 ERR("Clear channel failed key %" PRIu64
, key
);
2015 health_code_update();
2017 ret
= consumer_send_status_msg(sock
, ret_code
);
2019 /* Somehow, the session daemon is not responding anymore. */
2024 case LTTNG_CONSUMER_INIT
:
2026 ret_code
= lttng_consumer_init_command(ctx
,
2027 msg
.u
.init
.sessiond_uuid
);
2028 health_code_update();
2029 ret
= consumer_send_status_msg(sock
, ret_code
);
2031 /* Somehow, the session daemon is not responding anymore. */
2036 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2038 const struct lttng_credentials credentials
= {
2039 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2040 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2042 const bool is_local_trace
=
2043 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2044 const uint64_t relayd_id
=
2045 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2046 const char *chunk_override_name
=
2047 *msg
.u
.create_trace_chunk
.override_name
?
2048 msg
.u
.create_trace_chunk
.override_name
:
2050 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2053 * The session daemon will only provide a chunk directory file
2054 * descriptor for local traces.
2056 if (is_local_trace
) {
2059 /* Acnowledge the reception of the command. */
2060 ret
= consumer_send_status_msg(sock
,
2061 LTTCOMM_CONSUMERD_SUCCESS
);
2063 /* Somehow, the session daemon is not responding anymore. */
2068 * Receive trace chunk domain dirfd.
2070 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2071 if (ret
!= sizeof(chunk_dirfd
)) {
2072 ERR("Failed to receive trace chunk domain directory file descriptor");
2076 DBG("Received trace chunk domain directory fd (%d)",
2078 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2080 if (!chunk_directory_handle
) {
2081 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2082 if (close(chunk_dirfd
)) {
2083 PERROR("Failed to close chunk directory file descriptor");
2089 ret_code
= lttng_consumer_create_trace_chunk(
2090 !is_local_trace
? &relayd_id
: NULL
,
2091 msg
.u
.create_trace_chunk
.session_id
,
2092 msg
.u
.create_trace_chunk
.chunk_id
,
2093 (time_t) msg
.u
.create_trace_chunk
2094 .creation_timestamp
,
2095 chunk_override_name
,
2096 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2099 chunk_directory_handle
);
2100 lttng_directory_handle_put(chunk_directory_handle
);
2101 goto end_msg_sessiond
;
2103 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2105 enum lttng_trace_chunk_command_type close_command
=
2106 msg
.u
.close_trace_chunk
.close_command
.value
;
2107 const uint64_t relayd_id
=
2108 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2109 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2110 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2113 ret_code
= lttng_consumer_close_trace_chunk(
2114 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2117 msg
.u
.close_trace_chunk
.session_id
,
2118 msg
.u
.close_trace_chunk
.chunk_id
,
2119 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2120 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2122 NULL
, closed_trace_chunk_path
);
2123 reply
.ret_code
= ret_code
;
2124 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2125 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2126 if (ret
!= sizeof(reply
)) {
2129 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2131 if (ret
!= reply
.path_length
) {
2136 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2138 const uint64_t relayd_id
=
2139 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2141 ret_code
= lttng_consumer_trace_chunk_exists(
2142 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2144 msg
.u
.trace_chunk_exists
.session_id
,
2145 msg
.u
.trace_chunk_exists
.chunk_id
);
2146 goto end_msg_sessiond
;
2148 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2150 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2151 struct lttng_consumer_channel
*channel
=
2152 consumer_find_channel(key
);
2155 pthread_mutex_lock(&channel
->lock
);
2156 ret_code
= lttng_consumer_open_channel_packets(channel
);
2157 pthread_mutex_unlock(&channel
->lock
);
2160 * The channel could have disappeared in per-pid
2163 DBG("Channel %" PRIu64
" not found", key
);
2164 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2167 health_code_update();
2168 goto end_msg_sessiond
;
2176 * Return 1 to indicate success since the 0 value can be a socket
2177 * shutdown during the recv() or send() call.
2184 * The returned value here is not useful since either way we'll return 1 to
2185 * the caller because the session daemon socket management is done
2186 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2188 ret
= consumer_send_status_msg(sock
, ret_code
);
2198 * Free channel here since no one has a reference to it. We don't
2199 * free after that because a stream can store this pointer.
2201 destroy_channel(channel
);
2203 /* We have to send a status channel message indicating an error. */
2204 ret
= consumer_send_status_channel(sock
, NULL
);
2206 /* Stop everything if session daemon can not be notified. */
2213 /* This will issue a consumer stop. */
2219 health_code_update();
2223 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2224 int producer_active
)
2227 assert(stream
->ustream
);
2229 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2233 * Take a snapshot for a specific stream.
2235 * Returns 0 on success, < 0 on error
2237 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2240 assert(stream
->ustream
);
2242 return ustctl_snapshot(stream
->ustream
);
2246 * Sample consumed and produced positions for a specific stream.
2248 * Returns 0 on success, < 0 on error.
2250 int lttng_ustconsumer_sample_snapshot_positions(
2251 struct lttng_consumer_stream
*stream
)
2254 assert(stream
->ustream
);
2256 return ustctl_snapshot_sample_positions(stream
->ustream
);
2260 * Get the produced position
2262 * Returns 0 on success, < 0 on error
2264 int lttng_ustconsumer_get_produced_snapshot(
2265 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2268 assert(stream
->ustream
);
2271 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2275 * Get the consumed position
2277 * Returns 0 on success, < 0 on error
2279 int lttng_ustconsumer_get_consumed_snapshot(
2280 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2283 assert(stream
->ustream
);
2286 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2289 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2293 assert(stream
->ustream
);
2295 ustctl_flush_buffer(stream
->ustream
, producer
);
2298 void lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2301 assert(stream
->ustream
);
2303 ustctl_clear_buffer(stream
->ustream
);
2306 int lttng_ustconsumer_get_current_timestamp(
2307 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2310 assert(stream
->ustream
);
2313 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2316 int lttng_ustconsumer_get_sequence_number(
2317 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2320 assert(stream
->ustream
);
2323 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2327 * Called when the stream signals the consumer that it has hung up.
2329 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2332 assert(stream
->ustream
);
2334 pthread_mutex_lock(&stream
->lock
);
2335 if (!stream
->quiescent
) {
2336 ustctl_flush_buffer(stream
->ustream
, 0);
2337 stream
->quiescent
= true;
2339 pthread_mutex_unlock(&stream
->lock
);
2340 stream
->hangup_flush_done
= 1;
2343 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2348 assert(chan
->uchan
);
2349 assert(chan
->buffer_credentials
.is_set
);
2351 if (chan
->switch_timer_enabled
== 1) {
2352 consumer_timer_switch_stop(chan
);
2354 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2357 ret
= close(chan
->stream_fds
[i
]);
2361 if (chan
->shm_path
[0]) {
2362 char shm_path
[PATH_MAX
];
2364 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2366 ERR("Cannot get stream shm path");
2368 ret
= run_as_unlink(shm_path
,
2369 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2370 chan
->buffer_credentials
)),
2371 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2372 chan
->buffer_credentials
)));
2374 PERROR("unlink %s", shm_path
);
2380 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2383 assert(chan
->uchan
);
2384 assert(chan
->buffer_credentials
.is_set
);
2386 consumer_metadata_cache_destroy(chan
);
2387 ustctl_destroy_channel(chan
->uchan
);
2388 /* Try to rmdir all directories under shm_path root. */
2389 if (chan
->root_shm_path
[0]) {
2390 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2391 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2392 chan
->buffer_credentials
)),
2393 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2394 chan
->buffer_credentials
)),
2395 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2397 free(chan
->stream_fds
);
2400 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2403 assert(stream
->ustream
);
2405 if (stream
->chan
->switch_timer_enabled
== 1) {
2406 consumer_timer_switch_stop(stream
->chan
);
2408 ustctl_destroy_stream(stream
->ustream
);
2411 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2414 assert(stream
->ustream
);
2416 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2419 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2422 assert(stream
->ustream
);
2424 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2428 void metadata_stream_reset_cache_consumed_position(
2429 struct lttng_consumer_stream
*stream
)
2431 DBG("Reset metadata cache of session %" PRIu64
,
2432 stream
->chan
->session_id
);
2433 stream
->ust_metadata_pushed
= 0;
2437 * Write up to one packet from the metadata cache to the channel.
2439 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2440 * negative value on error.
2443 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2448 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2449 if (stream
->chan
->metadata_cache
->max_offset
==
2450 stream
->ust_metadata_pushed
) {
2452 * In the context of a user space metadata channel, a
2453 * change in version can be detected in two ways:
2454 * 1) During the pre-consume of the `read_subbuffer` loop,
2455 * 2) When populating the metadata ring buffer (i.e. here).
2457 * This function is invoked when there is no metadata
2458 * available in the ring-buffer. If all data was consumed
2459 * up to the size of the metadata cache, there is no metadata
2460 * to insert in the ring-buffer.
2462 * However, the metadata version could still have changed (a
2463 * regeneration without any new data will yield the same cache
2466 * The cache's version is checked for a version change and the
2467 * consumed position is reset if one occurred.
2469 * This check is only necessary for the user space domain as
2470 * it has to manage the cache explicitly. If this reset was not
2471 * performed, no metadata would be consumed (and no reset would
2472 * occur as part of the pre-consume) until the metadata size
2473 * exceeded the cache size.
2475 if (stream
->metadata_version
!=
2476 stream
->chan
->metadata_cache
->version
) {
2477 metadata_stream_reset_cache_consumed_position(stream
);
2478 consumer_stream_metadata_set_version(stream
,
2479 stream
->chan
->metadata_cache
->version
);
2486 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2487 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2488 stream
->chan
->metadata_cache
->max_offset
2489 - stream
->ust_metadata_pushed
);
2490 assert(write_len
!= 0);
2491 if (write_len
< 0) {
2492 ERR("Writing one metadata packet");
2496 stream
->ust_metadata_pushed
+= write_len
;
2498 assert(stream
->chan
->metadata_cache
->max_offset
>=
2499 stream
->ust_metadata_pushed
);
2503 * Switch packet (but don't open the next one) on every commit of
2504 * a metadata packet. Since the subbuffer is fully filled (with padding,
2505 * if needed), the stream is "quiescent" after this commit.
2507 ustctl_flush_buffer(stream
->ustream
, 1);
2508 stream
->quiescent
= true;
2510 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2516 * Sync metadata meaning request them to the session daemon and snapshot to the
2517 * metadata thread can consumer them.
2519 * Metadata stream lock is held here, but we need to release it when
2520 * interacting with sessiond, else we cause a deadlock with live
2521 * awaiting on metadata to be pushed out.
2523 * The RCU read side lock must be held by the caller.
2525 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2526 struct lttng_consumer_local_data
*ctx
,
2527 struct lttng_consumer_stream
*metadata_stream
)
2530 enum sync_metadata_status status
;
2531 struct lttng_consumer_channel
*metadata_channel
;
2534 assert(metadata_stream
);
2536 metadata_channel
= metadata_stream
->chan
;
2537 pthread_mutex_unlock(&metadata_stream
->lock
);
2539 * Request metadata from the sessiond, but don't wait for the flush
2540 * because we locked the metadata thread.
2542 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2543 pthread_mutex_lock(&metadata_stream
->lock
);
2545 status
= SYNC_METADATA_STATUS_ERROR
;
2550 * The metadata stream and channel can be deleted while the
2551 * metadata stream lock was released. The streamed is checked
2552 * for deletion before we use it further.
2554 * Note that it is safe to access a logically-deleted stream since its
2555 * existence is still guaranteed by the RCU read side lock. However,
2556 * it should no longer be used. The close/deletion of the metadata
2557 * channel and stream already guarantees that all metadata has been
2558 * consumed. Therefore, there is nothing left to do in this function.
2560 if (consumer_stream_is_deleted(metadata_stream
)) {
2561 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2562 metadata_stream
->key
);
2563 status
= SYNC_METADATA_STATUS_NO_DATA
;
2567 ret
= commit_one_metadata_packet(metadata_stream
);
2569 status
= SYNC_METADATA_STATUS_ERROR
;
2571 } else if (ret
> 0) {
2572 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2573 } else /* ret == 0 */ {
2574 status
= SYNC_METADATA_STATUS_NO_DATA
;
2578 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2580 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2581 status
= SYNC_METADATA_STATUS_ERROR
;
2590 * Return 0 on success else a negative value.
2592 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2593 struct lttng_consumer_local_data
*ctx
)
2596 struct ustctl_consumer_stream
*ustream
;
2601 ustream
= stream
->ustream
;
2604 * First, we are going to check if there is a new subbuffer available
2605 * before reading the stream wait_fd.
2607 /* Get the next subbuffer */
2608 ret
= ustctl_get_next_subbuf(ustream
);
2610 /* No more data found, flag the stream. */
2611 stream
->has_data
= 0;
2616 ret
= ustctl_put_subbuf(ustream
);
2619 /* This stream still has data. Flag it and wake up the data thread. */
2620 stream
->has_data
= 1;
2622 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2625 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2626 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2631 /* The wake up pipe has been notified. */
2632 ctx
->has_wakeup
= 1;
2640 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2645 * We can consume the 1 byte written into the wait_fd by
2646 * UST. Don't trigger error if we cannot read this one byte
2647 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2649 * This is only done when the stream is monitored by a thread,
2650 * before the flush is done after a hangup and if the stream
2651 * is not flagged with data since there might be nothing to
2652 * consume in the wait fd but still have data available
2653 * flagged by the consumer wake up pipe.
2655 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2659 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2660 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2668 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2669 struct stream_subbuffer
*subbuf
)
2673 ret
= ustctl_get_subbuf_size(
2674 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2679 ret
= ustctl_get_padded_subbuf_size(
2680 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2689 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2690 struct stream_subbuffer
*subbuf
)
2694 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2699 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2705 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2706 struct stream_subbuffer
*subbuf
)
2710 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2715 ret
= ustctl_get_packet_size(
2716 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2718 PERROR("Failed to get sub-buffer packet size");
2722 ret
= ustctl_get_content_size(
2723 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2725 PERROR("Failed to get sub-buffer content size");
2729 ret
= ustctl_get_timestamp_begin(
2730 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2732 PERROR("Failed to get sub-buffer begin timestamp");
2736 ret
= ustctl_get_timestamp_end(
2737 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2739 PERROR("Failed to get sub-buffer end timestamp");
2743 ret
= ustctl_get_events_discarded(
2744 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2746 PERROR("Failed to get sub-buffer events discarded count");
2750 ret
= ustctl_get_sequence_number(stream
->ustream
,
2751 &subbuf
->info
.data
.sequence_number
.value
);
2753 /* May not be supported by older LTTng-modules. */
2754 if (ret
!= -ENOTTY
) {
2755 PERROR("Failed to get sub-buffer sequence number");
2759 subbuf
->info
.data
.sequence_number
.is_set
= true;
2762 ret
= ustctl_get_stream_id(
2763 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2765 PERROR("Failed to get stream id");
2769 ret
= ustctl_get_instance_id(stream
->ustream
,
2770 &subbuf
->info
.data
.stream_instance_id
.value
);
2772 /* May not be supported by older LTTng-modules. */
2773 if (ret
!= -ENOTTY
) {
2774 PERROR("Failed to get stream instance id");
2778 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2784 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2785 struct stream_subbuffer
*subbuffer
)
2790 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2796 ret
= get_current_subbuf_addr(stream
, &addr
);
2801 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2802 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2803 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2808 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2809 struct stream_subbuffer
*subbuffer
)
2813 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2818 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2826 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2827 struct stream_subbuffer
*subbuffer
)
2834 unsigned long consumed_pos
, produced_pos
;
2837 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2839 got_subbuffer
= true;
2841 got_subbuffer
= false;
2842 if (ret
!= -EAGAIN
) {
2849 * Determine if the cache is empty and ensure that a sub-buffer
2850 * is made available if the cache is not empty.
2852 if (!got_subbuffer
) {
2853 ret
= commit_one_metadata_packet(stream
);
2854 if (ret
< 0 && ret
!= -ENOBUFS
) {
2856 } else if (ret
== 0) {
2857 /* Not an error, the cache is empty. */
2862 cache_empty
= false;
2865 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2866 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2867 stream
->ust_metadata_pushed
;
2868 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2870 } while (!got_subbuffer
);
2872 /* Populate sub-buffer infos and view. */
2873 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2878 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2881 * -EAGAIN is not expected since we got a sub-buffer and haven't
2882 * pushed the consumption position yet (on put_next).
2884 PERROR("Failed to take a snapshot of metadata buffer positions");
2888 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2890 PERROR("Failed to get metadata consumed position");
2894 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2896 PERROR("Failed to get metadata produced position");
2900 /* Last sub-buffer of the ring buffer ? */
2901 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2904 * The sessiond registry lock ensures that coherent units of metadata
2905 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2906 * acquired, the cache is empty, and it is the only available sub-buffer
2907 * available, it is safe to assume that it is "coherent".
2909 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2911 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2916 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2917 struct stream_subbuffer
*subbuffer
)
2919 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2925 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2926 struct lttng_consumer_local_data
*ctx
)
2928 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
2931 static int lttng_ustconsumer_set_stream_ops(
2932 struct lttng_consumer_stream
*stream
)
2936 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
2937 if (stream
->metadata_flag
) {
2938 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2939 get_next_subbuffer_metadata
;
2940 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2941 extract_metadata_subbuffer_info
;
2942 stream
->read_subbuffer_ops
.reset_metadata
=
2943 metadata_stream_reset_cache_consumed_position
;
2944 if (stream
->chan
->is_live
) {
2945 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
2946 ret
= consumer_stream_enable_metadata_bucketization(
2953 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2955 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2956 extract_data_subbuffer_info
;
2957 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
2958 if (stream
->chan
->is_live
) {
2959 stream
->read_subbuffer_ops
.send_live_beacon
=
2960 consumer_flush_ust_index
;
2964 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
2970 * Called when a stream is created.
2972 * Return 0 on success or else a negative value.
2974 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2981 * Don't create anything if this is set for streaming or if there is
2982 * no current trace chunk on the parent channel.
2984 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
2985 stream
->chan
->trace_chunk
) {
2986 ret
= consumer_stream_create_output_files(stream
, true);
2992 lttng_ustconsumer_set_stream_ops(stream
);
3000 * Check if data is still being extracted from the buffers for a specific
3001 * stream. Consumer data lock MUST be acquired before calling this function
3002 * and the stream lock.
3004 * Return 1 if the traced data are still getting read else 0 meaning that the
3005 * data is available for trace viewer reading.
3007 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3012 assert(stream
->ustream
);
3014 DBG("UST consumer checking data pending");
3016 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3021 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3022 uint64_t contiguous
, pushed
;
3024 /* Ease our life a bit. */
3025 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3026 pushed
= stream
->ust_metadata_pushed
;
3029 * We can simply check whether all contiguously available data
3030 * has been pushed to the ring buffer, since the push operation
3031 * is performed within get_next_subbuf(), and because both
3032 * get_next_subbuf() and put_next_subbuf() are issued atomically
3033 * thanks to the stream lock within
3034 * lttng_ustconsumer_read_subbuffer(). This basically means that
3035 * whetnever ust_metadata_pushed is incremented, the associated
3036 * metadata has been consumed from the metadata stream.
3038 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3039 contiguous
, pushed
);
3040 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3041 if ((contiguous
!= pushed
) ||
3042 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3043 ret
= 1; /* Data is pending */
3047 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3050 * There is still data so let's put back this
3053 ret
= ustctl_put_subbuf(stream
->ustream
);
3055 ret
= 1; /* Data is pending */
3060 /* Data is NOT pending so ready to be read. */
3068 * Stop a given metadata channel timer if enabled and close the wait fd which
3069 * is the poll pipe of the metadata stream.
3071 * This MUST be called with the metadata channel lock acquired.
3073 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3078 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3080 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3082 if (metadata
->switch_timer_enabled
== 1) {
3083 consumer_timer_switch_stop(metadata
);
3086 if (!metadata
->metadata_stream
) {
3091 * Closing write side so the thread monitoring the stream wakes up if any
3092 * and clean the metadata stream.
3094 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3095 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3097 PERROR("closing metadata pipe write side");
3099 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3107 * Close every metadata stream wait fd of the metadata hash table. This
3108 * function MUST be used very carefully so not to run into a race between the
3109 * metadata thread handling streams and this function closing their wait fd.
3111 * For UST, this is used when the session daemon hangs up. Its the metadata
3112 * producer so calling this is safe because we are assured that no state change
3113 * can occur in the metadata thread for the streams in the hash table.
3115 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3117 struct lttng_ht_iter iter
;
3118 struct lttng_consumer_stream
*stream
;
3120 assert(metadata_ht
);
3121 assert(metadata_ht
->ht
);
3123 DBG("UST consumer closing all metadata streams");
3126 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3129 health_code_update();
3131 pthread_mutex_lock(&stream
->chan
->lock
);
3132 lttng_ustconsumer_close_metadata(stream
->chan
);
3133 pthread_mutex_unlock(&stream
->chan
->lock
);
3139 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3143 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3145 ERR("Unable to close wakeup fd");
3150 * Please refer to consumer-timer.c before adding any lock within this
3151 * function or any of its callees. Timers have a very strict locking
3152 * semantic with respect to teardown. Failure to respect this semantic
3153 * introduces deadlocks.
3155 * DON'T hold the metadata lock when calling this function, else this
3156 * can cause deadlock involving consumer awaiting for metadata to be
3157 * pushed out due to concurrent interaction with the session daemon.
3159 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3160 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3162 struct lttcomm_metadata_request_msg request
;
3163 struct lttcomm_consumer_msg msg
;
3164 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3165 uint64_t len
, key
, offset
, version
;
3169 assert(channel
->metadata_cache
);
3171 memset(&request
, 0, sizeof(request
));
3173 /* send the metadata request to sessiond */
3174 switch (consumer_data
.type
) {
3175 case LTTNG_CONSUMER64_UST
:
3176 request
.bits_per_long
= 64;
3178 case LTTNG_CONSUMER32_UST
:
3179 request
.bits_per_long
= 32;
3182 request
.bits_per_long
= 0;
3186 request
.session_id
= channel
->session_id
;
3187 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3189 * Request the application UID here so the metadata of that application can
3190 * be sent back. The channel UID corresponds to the user UID of the session
3191 * used for the rights on the stream file(s).
3193 request
.uid
= channel
->ust_app_uid
;
3194 request
.key
= channel
->key
;
3196 DBG("Sending metadata request to sessiond, session id %" PRIu64
3197 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3198 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3201 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3203 health_code_update();
3205 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3208 ERR("Asking metadata to sessiond");
3212 health_code_update();
3214 /* Receive the metadata from sessiond */
3215 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3217 if (ret
!= sizeof(msg
)) {
3218 DBG("Consumer received unexpected message size %d (expects %zu)",
3220 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3222 * The ret value might 0 meaning an orderly shutdown but this is ok
3223 * since the caller handles this.
3228 health_code_update();
3230 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3231 /* No registry found */
3232 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3236 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3237 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3242 len
= msg
.u
.push_metadata
.len
;
3243 key
= msg
.u
.push_metadata
.key
;
3244 offset
= msg
.u
.push_metadata
.target_offset
;
3245 version
= msg
.u
.push_metadata
.version
;
3247 assert(key
== channel
->key
);
3249 DBG("No new metadata to receive for key %" PRIu64
, key
);
3252 health_code_update();
3254 /* Tell session daemon we are ready to receive the metadata. */
3255 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3256 LTTCOMM_CONSUMERD_SUCCESS
);
3257 if (ret
< 0 || len
== 0) {
3259 * Somehow, the session daemon is not responding anymore or there is
3260 * nothing to receive.
3265 health_code_update();
3267 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3268 key
, offset
, len
, version
, channel
, timer
, wait
);
3271 * Only send the status msg if the sessiond is alive meaning a positive
3274 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3279 health_code_update();
3281 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3286 * Return the ustctl call for the get stream id.
3288 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3289 uint64_t *stream_id
)
3294 return ustctl_get_stream_id(stream
->ustream
, stream_id
);