2 * Copyright (C) 2011 EfficiOS Inc.
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
11 #include <lttng/ust-ctl.h>
12 #include <lttng/ust-sigbus.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 the_consumer_data
;
48 extern int consumer_poll_timeout
;
50 LTTNG_EXPORT
DEFINE_LTTNG_UST_SIGBUS_STATE();
53 * Free channel object and all streams associated with it. This MUST be used
54 * only and only if the channel has _NEVER_ been added to the global channel
57 static void destroy_channel(struct lttng_consumer_channel
*channel
)
59 struct lttng_consumer_stream
*stream
, *stmp
;
61 LTTNG_ASSERT(channel
);
63 DBG("UST consumer cleaning stream list");
65 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
70 cds_list_del(&stream
->send_node
);
71 lttng_ust_ctl_destroy_stream(stream
->ustream
);
72 lttng_trace_chunk_put(stream
->trace_chunk
);
77 * If a channel is available meaning that was created before the streams
81 lttng_ustconsumer_del_channel(channel
);
82 lttng_ustconsumer_free_channel(channel
);
85 if (channel
->trace_chunk
) {
86 lttng_trace_chunk_put(channel
->trace_chunk
);
93 * Add channel to internal consumer state.
95 * Returns 0 on success or else a negative value.
97 static int add_channel(struct lttng_consumer_channel
*channel
,
98 struct lttng_consumer_local_data
*ctx
)
102 LTTNG_ASSERT(channel
);
105 if (ctx
->on_recv_channel
!= NULL
) {
106 ret
= ctx
->on_recv_channel(channel
);
108 ret
= consumer_add_channel(channel
, ctx
);
109 } else if (ret
< 0) {
110 /* Most likely an ENOMEM. */
111 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
115 ret
= consumer_add_channel(channel
, ctx
);
118 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
125 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
126 * error value if applicable is set in it else it is kept untouched.
128 * Return NULL on error else the newly allocated stream object.
130 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
131 struct lttng_consumer_channel
*channel
,
132 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
135 struct lttng_consumer_stream
*stream
= NULL
;
137 LTTNG_ASSERT(channel
);
140 stream
= consumer_stream_create(
147 channel
->trace_chunk
,
152 if (stream
== NULL
) {
156 * We could not find the channel. Can happen if cpu hotplug
157 * happens while tearing down.
159 DBG3("Could not find channel");
164 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
170 consumer_stream_update_channel_attributes(stream
, channel
);
174 *_alloc_ret
= alloc_ret
;
180 * Send the given stream pointer to the corresponding thread.
182 * Returns 0 on success else a negative value.
184 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
185 struct lttng_consumer_local_data
*ctx
)
188 struct lttng_pipe
*stream_pipe
;
190 /* Get the right pipe where the stream will be sent. */
191 if (stream
->metadata_flag
) {
192 consumer_add_metadata_stream(stream
);
193 stream_pipe
= ctx
->consumer_metadata_pipe
;
195 consumer_add_data_stream(stream
);
196 stream_pipe
= ctx
->consumer_data_pipe
;
200 * From this point on, the stream's ownership has been moved away from
201 * the channel and it becomes globally visible. Hence, remove it from
202 * the local stream list to prevent the stream from being both local and
205 stream
->globally_visible
= 1;
206 cds_list_del(&stream
->send_node
);
208 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
210 ERR("Consumer write %s stream to pipe %d",
211 stream
->metadata_flag
? "metadata" : "data",
212 lttng_pipe_get_writefd(stream_pipe
));
213 if (stream
->metadata_flag
) {
214 consumer_del_stream_for_metadata(stream
);
216 consumer_del_stream_for_data(stream
);
226 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
228 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
231 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
232 stream_shm_path
[PATH_MAX
- 1] = '\0';
233 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
238 strncat(stream_shm_path
, cpu_nr
,
239 PATH_MAX
- strlen(stream_shm_path
) - 1);
246 * Create streams for the given channel using liblttng-ust-ctl.
247 * The channel lock must be acquired by the caller.
249 * Return 0 on success else a negative value.
251 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
252 struct lttng_consumer_local_data
*ctx
)
255 struct lttng_ust_ctl_consumer_stream
*ustream
;
256 struct lttng_consumer_stream
*stream
;
257 pthread_mutex_t
*current_stream_lock
= NULL
;
259 LTTNG_ASSERT(channel
);
263 * While a stream is available from ustctl. When NULL is returned, we've
264 * reached the end of the possible stream for the channel.
266 while ((ustream
= lttng_ust_ctl_create_stream(channel
->uchan
, cpu
))) {
268 int ust_metadata_pipe
[2];
270 health_code_update();
272 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
273 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
275 ERR("Create ust metadata poll pipe");
278 wait_fd
= ust_metadata_pipe
[0];
280 wait_fd
= lttng_ust_ctl_stream_get_wait_fd(ustream
);
283 /* Allocate consumer stream object. */
284 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
288 stream
->ustream
= ustream
;
290 * Store it so we can save multiple function calls afterwards since
291 * this value is used heavily in the stream threads. This is UST
292 * specific so this is why it's done after allocation.
294 stream
->wait_fd
= wait_fd
;
297 * Increment channel refcount since the channel reference has now been
298 * assigned in the allocation process above.
300 if (stream
->chan
->monitor
) {
301 uatomic_inc(&stream
->chan
->refcount
);
304 pthread_mutex_lock(&stream
->lock
);
305 current_stream_lock
= &stream
->lock
;
307 * Order is important this is why a list is used. On error, the caller
308 * should clean this list.
310 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
312 ret
= lttng_ust_ctl_get_max_subbuf_size(stream
->ustream
,
313 &stream
->max_sb_size
);
315 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s",
320 /* Do actions once stream has been received. */
321 if (ctx
->on_recv_stream
) {
322 ret
= ctx
->on_recv_stream(stream
);
328 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
329 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
331 /* Set next CPU stream. */
332 channel
->streams
.count
= ++cpu
;
334 /* Keep stream reference when creating metadata. */
335 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
336 channel
->metadata_stream
= stream
;
337 if (channel
->monitor
) {
338 /* Set metadata poll pipe if we created one */
339 memcpy(stream
->ust_metadata_poll_pipe
,
341 sizeof(ust_metadata_pipe
));
344 pthread_mutex_unlock(&stream
->lock
);
345 current_stream_lock
= NULL
;
352 if (current_stream_lock
) {
353 pthread_mutex_unlock(current_stream_lock
);
358 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
359 const struct lttng_credentials
*session_credentials
)
361 char shm_path
[PATH_MAX
];
364 if (!channel
->shm_path
[0]) {
365 return shm_create_anonymous("ust-consumer");
367 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
371 return run_as_open(shm_path
,
372 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
373 lttng_credentials_get_uid(session_credentials
),
374 lttng_credentials_get_gid(session_credentials
));
381 * Create an UST channel with the given attributes and send it to the session
382 * daemon using the ust ctl API.
384 * Return 0 on success or else a negative value.
386 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
387 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
388 struct lttng_ust_ctl_consumer_channel
**ust_chanp
)
390 int ret
, nr_stream_fds
, i
, j
;
392 struct lttng_ust_ctl_consumer_channel
*ust_channel
;
394 LTTNG_ASSERT(channel
);
396 LTTNG_ASSERT(ust_chanp
);
397 LTTNG_ASSERT(channel
->buffer_credentials
.is_set
);
399 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
400 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
401 "switch_timer_interval: %u, read_timer_interval: %u, "
402 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
403 attr
->num_subbuf
, attr
->switch_timer_interval
,
404 attr
->read_timer_interval
, attr
->output
, attr
->type
);
406 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
409 nr_stream_fds
= lttng_ust_ctl_get_nr_stream_per_channel();
410 stream_fds
= (int *) zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
415 for (i
= 0; i
< nr_stream_fds
; i
++) {
416 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
417 &channel
->buffer_credentials
.value
);
418 if (stream_fds
[i
] < 0) {
423 ust_channel
= lttng_ust_ctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
428 channel
->nr_stream_fds
= nr_stream_fds
;
429 channel
->stream_fds
= stream_fds
;
430 *ust_chanp
= ust_channel
;
436 for (j
= i
- 1; j
>= 0; j
--) {
439 closeret
= close(stream_fds
[j
]);
443 if (channel
->shm_path
[0]) {
444 char shm_path
[PATH_MAX
];
446 closeret
= get_stream_shm_path(shm_path
,
447 channel
->shm_path
, j
);
449 ERR("Cannot get stream shm path");
451 closeret
= run_as_unlink(shm_path
,
452 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
453 channel
->buffer_credentials
)),
454 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
455 channel
->buffer_credentials
)));
457 PERROR("unlink %s", shm_path
);
461 /* Try to rmdir all directories under shm_path root. */
462 if (channel
->root_shm_path
[0]) {
463 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
464 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
465 channel
->buffer_credentials
)),
466 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
467 channel
->buffer_credentials
)),
468 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
476 * Send a single given stream to the session daemon using the sock.
478 * Return 0 on success else a negative value.
480 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
484 LTTNG_ASSERT(stream
);
485 LTTNG_ASSERT(sock
>= 0);
487 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
489 /* Send stream to session daemon. */
490 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, stream
->ustream
);
500 * Send channel to sessiond and relayd if applicable.
502 * Return 0 on success or else a negative value.
504 static int send_channel_to_sessiond_and_relayd(int sock
,
505 struct lttng_consumer_channel
*channel
,
506 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
508 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
509 struct lttng_consumer_stream
*stream
;
510 uint64_t net_seq_idx
= -1ULL;
512 LTTNG_ASSERT(channel
);
514 LTTNG_ASSERT(sock
>= 0);
516 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
518 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
519 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
521 health_code_update();
523 /* Try to send the stream to the relayd if one is available. */
524 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
525 stream
->key
, channel
->name
);
526 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
529 * Flag that the relayd was the problem here probably due to a
530 * communicaton error on the socket.
535 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
537 if (net_seq_idx
== -1ULL) {
538 net_seq_idx
= stream
->net_seq_idx
;
543 /* Inform sessiond that we are about to send channel and streams. */
544 ret
= consumer_send_status_msg(sock
, ret_code
);
545 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
547 * Either the session daemon is not responding or the relayd died so we
553 /* Send channel to sessiond. */
554 ret
= lttng_ust_ctl_send_channel_to_sessiond(sock
, channel
->uchan
);
559 ret
= lttng_ust_ctl_channel_close_wakeup_fd(channel
->uchan
);
564 /* The channel was sent successfully to the sessiond at this point. */
565 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
567 health_code_update();
569 /* Send stream to session daemon. */
570 ret
= send_sessiond_stream(sock
, stream
);
576 /* Tell sessiond there is no more stream. */
577 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, NULL
);
582 DBG("UST consumer NULL stream sent to sessiond");
587 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
594 * Creates a channel and streams and add the channel it to the channel internal
595 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
598 * Return 0 on success or else, a negative value is returned and the channel
599 * MUST be destroyed by consumer_del_channel().
601 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
602 struct lttng_consumer_channel
*channel
,
603 struct lttng_ust_ctl_consumer_channel_attr
*attr
)
608 LTTNG_ASSERT(channel
);
612 * This value is still used by the kernel consumer since for the kernel,
613 * the stream ownership is not IN the consumer so we need to have the
614 * number of left stream that needs to be initialized so we can know when
615 * to delete the channel (see consumer.c).
617 * As for the user space tracer now, the consumer creates and sends the
618 * stream to the session daemon which only sends them to the application
619 * once every stream of a channel is received making this value useless
620 * because we they will be added to the poll thread before the application
621 * receives them. This ensures that a stream can not hang up during
622 * initilization of a channel.
624 channel
->nb_init_stream_left
= 0;
626 /* The reply msg status is handled in the following call. */
627 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
632 channel
->wait_fd
= lttng_ust_ctl_channel_get_wait_fd(channel
->uchan
);
635 * For the snapshots (no monitor), we create the metadata streams
636 * on demand, not during the channel creation.
638 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
643 /* Open all streams for this channel. */
644 pthread_mutex_lock(&channel
->lock
);
645 ret
= create_ust_streams(channel
, ctx
);
646 pthread_mutex_unlock(&channel
->lock
);
656 * Send all stream of a channel to the right thread handling it.
658 * On error, return a negative value else 0 on success.
660 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
661 struct lttng_consumer_local_data
*ctx
)
664 struct lttng_consumer_stream
*stream
, *stmp
;
666 LTTNG_ASSERT(channel
);
669 /* Send streams to the corresponding thread. */
670 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
673 health_code_update();
675 /* Sending the stream to the thread. */
676 ret
= send_stream_to_thread(stream
, ctx
);
679 * If we are unable to send the stream to the thread, there is
680 * a big problem so just stop everything.
691 * Flush channel's streams using the given key to retrieve the channel.
693 * Return 0 on success else an LTTng error code.
695 static int flush_channel(uint64_t chan_key
)
698 struct lttng_consumer_channel
*channel
;
699 struct lttng_consumer_stream
*stream
;
701 struct lttng_ht_iter iter
;
703 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
706 channel
= consumer_find_channel(chan_key
);
708 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
709 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
713 ht
= the_consumer_data
.stream_per_chan_id_ht
;
715 /* For each stream of the channel id, flush it. */
716 cds_lfht_for_each_entry_duplicate(ht
->ht
,
717 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
718 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
720 health_code_update();
722 pthread_mutex_lock(&stream
->lock
);
725 * Protect against concurrent teardown of a stream.
727 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
731 if (!stream
->quiescent
) {
732 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
734 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64
", channel name = '%s'",
735 chan_key
, channel
->name
);
736 ret
= LTTNG_ERR_BUFFER_FLUSH_FAILED
;
737 pthread_mutex_unlock(&stream
->lock
);
740 stream
->quiescent
= true;
743 pthread_mutex_unlock(&stream
->lock
);
751 * Clear quiescent state from channel's streams using the given key to
752 * retrieve the channel.
754 * Return 0 on success else an LTTng error code.
756 static int clear_quiescent_channel(uint64_t chan_key
)
759 struct lttng_consumer_channel
*channel
;
760 struct lttng_consumer_stream
*stream
;
762 struct lttng_ht_iter iter
;
764 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
767 channel
= consumer_find_channel(chan_key
);
769 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
770 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
774 ht
= the_consumer_data
.stream_per_chan_id_ht
;
776 /* For each stream of the channel id, clear quiescent state. */
777 cds_lfht_for_each_entry_duplicate(ht
->ht
,
778 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
779 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
781 health_code_update();
783 pthread_mutex_lock(&stream
->lock
);
784 stream
->quiescent
= false;
785 pthread_mutex_unlock(&stream
->lock
);
793 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
795 * Return 0 on success else an LTTng error code.
797 static int close_metadata(uint64_t chan_key
)
800 struct lttng_consumer_channel
*channel
;
801 unsigned int channel_monitor
;
803 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
805 channel
= consumer_find_channel(chan_key
);
808 * This is possible if the metadata thread has issue a delete because
809 * the endpoint point of the stream hung up. There is no way the
810 * session daemon can know about it thus use a DBG instead of an actual
813 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
814 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
818 pthread_mutex_lock(&the_consumer_data
.lock
);
819 pthread_mutex_lock(&channel
->lock
);
820 channel_monitor
= channel
->monitor
;
821 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
825 lttng_ustconsumer_close_metadata(channel
);
826 pthread_mutex_unlock(&channel
->lock
);
827 pthread_mutex_unlock(&the_consumer_data
.lock
);
830 * The ownership of a metadata channel depends on the type of
831 * session to which it belongs. In effect, the monitor flag is checked
832 * to determine if this metadata channel is in "snapshot" mode or not.
834 * In the non-snapshot case, the metadata channel is created along with
835 * a single stream which will remain present until the metadata channel
836 * is destroyed (on the destruction of its session). In this case, the
837 * metadata stream in "monitored" by the metadata poll thread and holds
838 * the ownership of its channel.
840 * Closing the metadata will cause the metadata stream's "metadata poll
841 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
842 * thread which will teardown the metadata stream which, in return,
843 * deletes the metadata channel.
845 * In the snapshot case, the metadata stream is created and destroyed
846 * on every snapshot record. Since the channel doesn't have an owner
847 * other than the session daemon, it is safe to destroy it immediately
848 * on reception of the CLOSE_METADATA command.
850 if (!channel_monitor
) {
852 * The channel and consumer_data locks must be
853 * released before this call since consumer_del_channel
854 * re-acquires the channel and consumer_data locks to teardown
855 * the channel and queue its reclamation by the "call_rcu"
858 consumer_del_channel(channel
);
863 pthread_mutex_unlock(&channel
->lock
);
864 pthread_mutex_unlock(&the_consumer_data
.lock
);
870 * RCU read side lock MUST be acquired before calling this function.
872 * Return 0 on success else an LTTng error code.
874 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
877 struct lttng_consumer_channel
*metadata
;
879 ASSERT_RCU_READ_LOCKED();
881 DBG("UST consumer setup metadata key %" PRIu64
, key
);
883 metadata
= consumer_find_channel(key
);
885 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
886 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
891 * In no monitor mode, the metadata channel has no stream(s) so skip the
892 * ownership transfer to the metadata thread.
894 if (!metadata
->monitor
) {
895 DBG("Metadata channel in no monitor");
901 * Send metadata stream to relayd if one available. Availability is
902 * known if the stream is still in the list of the channel.
904 if (cds_list_empty(&metadata
->streams
.head
)) {
905 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
906 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
907 goto error_no_stream
;
910 /* Send metadata stream to relayd if needed. */
911 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
912 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
915 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
918 ret
= consumer_send_relayd_streams_sent(
919 metadata
->metadata_stream
->net_seq_idx
);
921 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
927 * Ownership of metadata stream is passed along. Freeing is handled by
930 ret
= send_streams_to_thread(metadata
, ctx
);
933 * If we are unable to send the stream to the thread, there is
934 * a big problem so just stop everything.
936 ret
= LTTCOMM_CONSUMERD_FATAL
;
937 goto send_streams_error
;
939 /* List MUST be empty after or else it could be reused. */
940 LTTNG_ASSERT(cds_list_empty(&metadata
->streams
.head
));
947 * Delete metadata channel on error. At this point, the metadata stream can
948 * NOT be monitored by the metadata thread thus having the guarantee that
949 * the stream is still in the local stream list of the channel. This call
950 * will make sure to clean that list.
952 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
953 cds_list_del(&metadata
->metadata_stream
->send_node
);
954 metadata
->metadata_stream
= NULL
;
962 * Snapshot the whole metadata.
963 * RCU read-side lock must be held by the caller.
965 * Returns 0 on success, < 0 on error
967 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
968 uint64_t key
, char *path
, uint64_t relayd_id
,
969 struct lttng_consumer_local_data
*ctx
)
972 struct lttng_consumer_stream
*metadata_stream
;
976 ASSERT_RCU_READ_LOCKED();
978 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
983 LTTNG_ASSERT(!metadata_channel
->monitor
);
985 health_code_update();
988 * Ask the sessiond if we have new metadata waiting and update the
989 * consumer metadata cache.
991 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
996 health_code_update();
999 * The metadata stream is NOT created in no monitor mode when the channel
1000 * is created on a sessiond ask channel command.
1002 ret
= create_ust_streams(metadata_channel
, ctx
);
1007 metadata_stream
= metadata_channel
->metadata_stream
;
1008 LTTNG_ASSERT(metadata_stream
);
1010 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
1011 if (relayd_id
!= (uint64_t) -1ULL) {
1012 metadata_stream
->net_seq_idx
= relayd_id
;
1013 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1015 ret
= consumer_stream_create_output_files(metadata_stream
,
1023 health_code_update();
1024 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1031 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
1033 * Clean up the stream completely because the next snapshot will use a
1034 * new metadata stream.
1036 consumer_stream_destroy(metadata_stream
, NULL
);
1037 cds_list_del(&metadata_stream
->send_node
);
1038 metadata_channel
->metadata_stream
= NULL
;
1046 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1050 unsigned long mmap_offset
;
1051 const char *mmap_base
;
1053 mmap_base
= (const char *) lttng_ust_ctl_get_mmap_base(stream
->ustream
);
1055 ERR("Failed to get mmap base for stream `%s`",
1061 ret
= lttng_ust_ctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1063 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1068 *addr
= mmap_base
+ mmap_offset
;
1075 * Take a snapshot of all the stream of a channel.
1076 * RCU read-side lock and the channel lock must be held by the caller.
1078 * Returns 0 on success, < 0 on error
1080 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1081 uint64_t key
, char *path
, uint64_t relayd_id
,
1082 uint64_t nb_packets_per_stream
,
1083 struct lttng_consumer_local_data
*ctx
)
1086 unsigned use_relayd
= 0;
1087 unsigned long consumed_pos
, produced_pos
;
1088 struct lttng_consumer_stream
*stream
;
1092 ASSERT_RCU_READ_LOCKED();
1096 if (relayd_id
!= (uint64_t) -1ULL) {
1100 LTTNG_ASSERT(!channel
->monitor
);
1101 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1103 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1104 health_code_update();
1106 /* Lock stream because we are about to change its state. */
1107 pthread_mutex_lock(&stream
->lock
);
1108 LTTNG_ASSERT(channel
->trace_chunk
);
1109 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1111 * Can't happen barring an internal error as the channel
1112 * holds a reference to the trace chunk.
1114 ERR("Failed to acquire reference to channel's trace chunk");
1118 LTTNG_ASSERT(!stream
->trace_chunk
);
1119 stream
->trace_chunk
= channel
->trace_chunk
;
1121 stream
->net_seq_idx
= relayd_id
;
1124 ret
= consumer_send_relayd_stream(stream
, path
);
1129 ret
= consumer_stream_create_output_files(stream
,
1134 DBG("UST consumer snapshot stream (%" PRIu64
")",
1139 * If tracing is active, we want to perform a "full" buffer flush.
1140 * Else, if quiescent, it has already been done by the prior stop.
1142 if (!stream
->quiescent
) {
1143 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
1145 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
", channel name = '%s'",
1146 channel
->key
, channel
->name
);
1151 ret
= lttng_ustconsumer_take_snapshot(stream
);
1153 ERR("Taking UST snapshot");
1157 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1159 ERR("Produced UST snapshot position");
1163 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1165 ERR("Consumerd UST snapshot position");
1170 * The original value is sent back if max stream size is larger than
1171 * the possible size of the snapshot. Also, we assume that the session
1172 * daemon should never send a maximum stream size that is lower than
1175 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1176 produced_pos
, nb_packets_per_stream
,
1177 stream
->max_sb_size
);
1179 while ((long) (consumed_pos
- produced_pos
) < 0) {
1181 unsigned long len
, padded_len
;
1182 const char *subbuf_addr
;
1183 struct lttng_buffer_view subbuf_view
;
1185 health_code_update();
1187 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1189 ret
= lttng_ust_ctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1191 if (ret
!= -EAGAIN
) {
1192 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1193 goto error_close_stream
;
1195 DBG("UST consumer get subbuf failed. Skipping it.");
1196 consumed_pos
+= stream
->max_sb_size
;
1197 stream
->chan
->lost_packets
++;
1201 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &len
);
1203 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1204 goto error_put_subbuf
;
1207 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1209 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1210 goto error_put_subbuf
;
1213 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1215 goto error_put_subbuf
;
1218 subbuf_view
= lttng_buffer_view_init(
1219 subbuf_addr
, 0, padded_len
);
1220 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1221 stream
, &subbuf_view
, padded_len
- len
);
1223 if (read_len
!= len
) {
1225 goto error_put_subbuf
;
1228 if (read_len
!= padded_len
) {
1230 goto error_put_subbuf
;
1234 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
1236 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1237 goto error_close_stream
;
1239 consumed_pos
+= stream
->max_sb_size
;
1242 /* Simply close the stream so we can use it on the next snapshot. */
1243 consumer_stream_close(stream
);
1244 pthread_mutex_unlock(&stream
->lock
);
1251 if (lttng_ust_ctl_put_subbuf(stream
->ustream
) < 0) {
1252 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1255 consumer_stream_close(stream
);
1257 pthread_mutex_unlock(&stream
->lock
);
1263 void metadata_stream_reset_cache_consumed_position(
1264 struct lttng_consumer_stream
*stream
)
1266 ASSERT_LOCKED(stream
->lock
);
1268 DBG("Reset metadata cache of session %" PRIu64
,
1269 stream
->chan
->session_id
);
1270 stream
->ust_metadata_pushed
= 0;
1274 * Receive the metadata updates from the sessiond. Supports receiving
1275 * overlapping metadata, but is needs to always belong to a contiguous
1276 * range starting from 0.
1277 * Be careful about the locks held when calling this function: it needs
1278 * the metadata cache flush to concurrently progress in order to
1281 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1282 uint64_t len
, uint64_t version
,
1283 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1285 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1287 enum consumer_metadata_cache_write_status cache_write_status
;
1289 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1291 metadata_str
= (char *) zmalloc(len
* sizeof(char));
1292 if (!metadata_str
) {
1293 PERROR("zmalloc metadata string");
1294 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1298 health_code_update();
1300 /* Receive metadata string. */
1301 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1303 /* Session daemon is dead so return gracefully. */
1308 health_code_update();
1310 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1311 cache_write_status
= consumer_metadata_cache_write(
1312 channel
->metadata_cache
, offset
, len
, version
,
1314 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1315 switch (cache_write_status
) {
1316 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1318 * The write entirely overlapped with existing contents of the
1319 * same metadata version (same content); there is nothing to do.
1322 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1324 * The metadata cache was invalidated (previously pushed
1325 * content has been overwritten). Reset the stream's consumed
1326 * metadata position to ensure the metadata poll thread consumes
1331 * channel::metadata_stream can be null when the metadata
1332 * channel is under a snapshot session type. No need to update
1333 * the stream position in that scenario.
1335 if (channel
->metadata_stream
!= NULL
) {
1336 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1337 metadata_stream_reset_cache_consumed_position(
1338 channel
->metadata_stream
);
1339 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1341 /* Validate we are in snapshot mode. */
1342 LTTNG_ASSERT(!channel
->monitor
);
1345 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1347 * In both cases, the metadata poll thread has new data to
1350 ret
= consumer_metadata_wakeup_pipe(channel
);
1352 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1356 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1357 /* Unable to handle metadata. Notify session daemon. */
1358 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1360 * Skip metadata flush on write error since the offset and len might
1361 * not have been updated which could create an infinite loop below when
1362 * waiting for the metadata cache to be flushed.
1372 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1373 DBG("Waiting for metadata to be flushed");
1375 health_code_update();
1377 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1387 * Receive command from session daemon and process it.
1389 * Return 1 on success else a negative value or 0.
1391 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1392 int sock
, struct pollfd
*consumer_sockpoll
)
1395 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1396 struct lttcomm_consumer_msg msg
;
1397 struct lttng_consumer_channel
*channel
= NULL
;
1399 health_code_update();
1404 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1405 if (ret_recv
!= sizeof(msg
)) {
1406 DBG("Consumer received unexpected message size %zd (expects %zu)",
1407 ret_recv
, sizeof(msg
));
1409 * The ret value might 0 meaning an orderly shutdown but this is ok
1410 * since the caller handles this.
1413 lttng_consumer_send_error(ctx
,
1414 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1421 health_code_update();
1424 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1426 health_code_update();
1428 /* relayd needs RCU read-side lock */
1431 switch (msg
.cmd_type
) {
1432 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1434 /* Session daemon status message are handled in the following call. */
1435 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1436 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1437 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1438 msg
.u
.relayd_sock
.relayd_session_id
);
1441 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1443 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1444 struct consumer_relayd_sock_pair
*relayd
;
1446 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1448 /* Get relayd reference if exists. */
1449 relayd
= consumer_find_relayd(index
);
1450 if (relayd
== NULL
) {
1451 DBG("Unable to find relayd %" PRIu64
, index
);
1452 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1456 * Each relayd socket pair has a refcount of stream attached to it
1457 * which tells if the relayd is still active or not depending on the
1460 * This will set the destroy flag of the relayd object and destroy it
1461 * if the refcount reaches zero when called.
1463 * The destroy can happen either here or when a stream fd hangs up.
1466 consumer_flag_relayd_for_destroy(relayd
);
1469 goto end_msg_sessiond
;
1471 case LTTNG_CONSUMER_UPDATE_STREAM
:
1476 case LTTNG_CONSUMER_DATA_PENDING
:
1478 int is_data_pending
;
1480 uint64_t id
= msg
.u
.data_pending
.session_id
;
1482 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1484 is_data_pending
= consumer_data_pending(id
);
1486 /* Send back returned value to session daemon */
1487 ret_send
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1488 sizeof(is_data_pending
));
1490 DBG("Error when sending the data pending ret code: %zd",
1496 * No need to send back a status message since the data pending
1497 * returned value is the response.
1501 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1503 int ret_ask_channel
, ret_add_channel
, ret_send
;
1504 struct lttng_ust_ctl_consumer_channel_attr attr
;
1505 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1506 const struct lttng_credentials buffer_credentials
= {
1507 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1508 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1511 /* Create a plain object and reserve a channel key. */
1512 channel
= consumer_allocate_channel(
1513 msg
.u
.ask_channel
.key
,
1514 msg
.u
.ask_channel
.session_id
,
1515 msg
.u
.ask_channel
.chunk_id
.is_set
?
1517 msg
.u
.ask_channel
.pathname
,
1518 msg
.u
.ask_channel
.name
,
1519 msg
.u
.ask_channel
.relayd_id
,
1520 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1521 msg
.u
.ask_channel
.tracefile_size
,
1522 msg
.u
.ask_channel
.tracefile_count
,
1523 msg
.u
.ask_channel
.session_id_per_pid
,
1524 msg
.u
.ask_channel
.monitor
,
1525 msg
.u
.ask_channel
.live_timer_interval
,
1526 msg
.u
.ask_channel
.is_live
,
1527 msg
.u
.ask_channel
.root_shm_path
,
1528 msg
.u
.ask_channel
.shm_path
);
1530 goto end_channel_error
;
1533 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1534 buffer_credentials
);
1537 * Assign UST application UID to the channel. This value is ignored for
1538 * per PID buffers. This is specific to UST thus setting this after the
1541 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1543 /* Build channel attributes from received message. */
1544 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1545 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1546 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1547 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1548 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1549 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1550 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1551 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1553 /* Match channel buffer type to the UST abi. */
1554 switch (msg
.u
.ask_channel
.output
) {
1555 case LTTNG_EVENT_MMAP
:
1557 attr
.output
= LTTNG_UST_ABI_MMAP
;
1561 /* Translate and save channel type. */
1562 switch (msg
.u
.ask_channel
.type
) {
1563 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1564 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1565 attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1567 * Set refcount to 1 for owner. Below, we will
1568 * pass ownership to the
1569 * consumer_thread_channel_poll() thread.
1571 channel
->refcount
= 1;
1573 case LTTNG_UST_ABI_CHAN_METADATA
:
1574 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1575 attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1582 health_code_update();
1584 ret_ask_channel
= ask_channel(ctx
, channel
, &attr
);
1585 if (ret_ask_channel
< 0) {
1586 goto end_channel_error
;
1589 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1592 ret_allocate
= consumer_metadata_cache_allocate(
1594 if (ret_allocate
< 0) {
1595 ERR("Allocating metadata cache");
1596 goto end_channel_error
;
1598 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1599 attr
.switch_timer_interval
= 0;
1601 int monitor_start_ret
;
1603 consumer_timer_live_start(channel
,
1604 msg
.u
.ask_channel
.live_timer_interval
);
1605 monitor_start_ret
= consumer_timer_monitor_start(
1607 msg
.u
.ask_channel
.monitor_timer_interval
);
1608 if (monitor_start_ret
< 0) {
1609 ERR("Starting channel monitoring timer failed");
1610 goto end_channel_error
;
1614 health_code_update();
1617 * Add the channel to the internal state AFTER all streams were created
1618 * and successfully sent to session daemon. This way, all streams must
1619 * be ready before this channel is visible to the threads.
1620 * If add_channel succeeds, ownership of the channel is
1621 * passed to consumer_thread_channel_poll().
1623 ret_add_channel
= add_channel(channel
, ctx
);
1624 if (ret_add_channel
< 0) {
1625 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1626 if (channel
->switch_timer_enabled
== 1) {
1627 consumer_timer_switch_stop(channel
);
1629 consumer_metadata_cache_destroy(channel
);
1631 if (channel
->live_timer_enabled
== 1) {
1632 consumer_timer_live_stop(channel
);
1634 if (channel
->monitor_timer_enabled
== 1) {
1635 consumer_timer_monitor_stop(channel
);
1637 goto end_channel_error
;
1640 health_code_update();
1643 * Channel and streams are now created. Inform the session daemon that
1644 * everything went well and should wait to receive the channel and
1645 * streams with ustctl API.
1647 ret_send
= consumer_send_status_channel(sock
, channel
);
1650 * There is probably a problem on the socket.
1657 case LTTNG_CONSUMER_GET_CHANNEL
:
1659 int ret
, relayd_err
= 0;
1660 uint64_t key
= msg
.u
.get_channel
.key
;
1661 struct lttng_consumer_channel
*found_channel
;
1663 found_channel
= consumer_find_channel(key
);
1664 if (!found_channel
) {
1665 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1666 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1667 goto end_get_channel
;
1670 health_code_update();
1672 /* Send the channel to sessiond (and relayd, if applicable). */
1673 ret
= send_channel_to_sessiond_and_relayd(
1674 sock
, found_channel
, ctx
, &relayd_err
);
1678 * We were unable to send to the relayd the stream so avoid
1679 * sending back a fatal error to the thread since this is OK
1680 * and the consumer can continue its work. The above call
1681 * has sent the error status message to the sessiond.
1683 goto end_get_channel_nosignal
;
1686 * The communicaton was broken hence there is a bad state between
1687 * the consumer and sessiond so stop everything.
1689 goto error_get_channel_fatal
;
1692 health_code_update();
1695 * In no monitor mode, the streams ownership is kept inside the channel
1696 * so don't send them to the data thread.
1698 if (!found_channel
->monitor
) {
1699 goto end_get_channel
;
1702 ret
= send_streams_to_thread(found_channel
, ctx
);
1705 * If we are unable to send the stream to the thread, there is
1706 * a big problem so just stop everything.
1708 goto error_get_channel_fatal
;
1710 /* List MUST be empty after or else it could be reused. */
1711 LTTNG_ASSERT(cds_list_empty(&found_channel
->streams
.head
));
1713 goto end_msg_sessiond
;
1714 error_get_channel_fatal
:
1716 end_get_channel_nosignal
:
1719 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1721 uint64_t key
= msg
.u
.destroy_channel
.key
;
1724 * Only called if streams have not been sent to stream
1725 * manager thread. However, channel has been sent to
1726 * channel manager thread.
1728 notify_thread_del_channel(ctx
, key
);
1729 goto end_msg_sessiond
;
1731 case LTTNG_CONSUMER_CLOSE_METADATA
:
1735 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1737 ret_code
= (lttcomm_return_code
) ret
;
1740 goto end_msg_sessiond
;
1742 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1746 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1748 ret_code
= (lttcomm_return_code
) ret
;
1751 goto end_msg_sessiond
;
1753 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1757 ret
= clear_quiescent_channel(
1758 msg
.u
.clear_quiescent_channel
.key
);
1760 ret_code
= (lttcomm_return_code
) ret
;
1763 goto end_msg_sessiond
;
1765 case LTTNG_CONSUMER_PUSH_METADATA
:
1768 uint64_t len
= msg
.u
.push_metadata
.len
;
1769 uint64_t key
= msg
.u
.push_metadata
.key
;
1770 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1771 uint64_t version
= msg
.u
.push_metadata
.version
;
1772 struct lttng_consumer_channel
*found_channel
;
1774 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1777 found_channel
= consumer_find_channel(key
);
1778 if (!found_channel
) {
1780 * This is possible if the metadata creation on the consumer side
1781 * is in flight vis-a-vis a concurrent push metadata from the
1782 * session daemon. Simply return that the channel failed and the
1783 * session daemon will handle that message correctly considering
1784 * that this race is acceptable thus the DBG() statement here.
1786 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1787 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1788 goto end_push_metadata_msg_sessiond
;
1791 health_code_update();
1795 * There is nothing to receive. We have simply
1796 * checked whether the channel can be found.
1798 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1799 goto end_push_metadata_msg_sessiond
;
1802 /* Tell session daemon we are ready to receive the metadata. */
1803 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1805 /* Somehow, the session daemon is not responding anymore. */
1806 goto error_push_metadata_fatal
;
1809 health_code_update();
1811 /* Wait for more data. */
1812 health_poll_entry();
1813 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1816 goto error_push_metadata_fatal
;
1819 health_code_update();
1821 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
, len
,
1822 version
, found_channel
, 0, 1);
1824 /* error receiving from sessiond */
1825 goto error_push_metadata_fatal
;
1827 ret_code
= (lttcomm_return_code
) ret
;
1828 goto end_push_metadata_msg_sessiond
;
1830 end_push_metadata_msg_sessiond
:
1831 goto end_msg_sessiond
;
1832 error_push_metadata_fatal
:
1835 case LTTNG_CONSUMER_SETUP_METADATA
:
1839 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1841 ret_code
= (lttcomm_return_code
) ret
;
1843 goto end_msg_sessiond
;
1845 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1847 struct lttng_consumer_channel
*found_channel
;
1848 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1851 found_channel
= consumer_find_channel(key
);
1852 if (!found_channel
) {
1853 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1854 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1856 if (msg
.u
.snapshot_channel
.metadata
) {
1859 ret_snapshot
= snapshot_metadata(found_channel
,
1861 msg
.u
.snapshot_channel
.pathname
,
1862 msg
.u
.snapshot_channel
.relayd_id
,
1864 if (ret_snapshot
< 0) {
1865 ERR("Snapshot metadata failed");
1866 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1871 ret_snapshot
= snapshot_channel(found_channel
,
1873 msg
.u
.snapshot_channel
.pathname
,
1874 msg
.u
.snapshot_channel
.relayd_id
,
1875 msg
.u
.snapshot_channel
1876 .nb_packets_per_stream
,
1878 if (ret_snapshot
< 0) {
1879 ERR("Snapshot channel failed");
1880 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1884 health_code_update();
1885 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1887 /* Somehow, the session daemon is not responding anymore. */
1890 health_code_update();
1893 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1896 uint64_t discarded_events
;
1897 struct lttng_ht_iter iter
;
1898 struct lttng_ht
*ht
;
1899 struct lttng_consumer_stream
*stream
;
1900 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1901 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1903 DBG("UST consumer discarded events command for session id %"
1906 pthread_mutex_lock(&the_consumer_data
.lock
);
1908 ht
= the_consumer_data
.stream_list_ht
;
1911 * We only need a reference to the channel, but they are not
1912 * directly indexed, so we just use the first matching stream
1913 * to extract the information we need, we default to 0 if not
1914 * found (no events are dropped if the channel is not yet in
1917 discarded_events
= 0;
1918 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1919 ht
->hash_fct(&id
, lttng_ht_seed
),
1921 &iter
.iter
, stream
, node_session_id
.node
) {
1922 if (stream
->chan
->key
== key
) {
1923 discarded_events
= stream
->chan
->discarded_events
;
1927 pthread_mutex_unlock(&the_consumer_data
.lock
);
1930 DBG("UST consumer discarded events command for session id %"
1931 PRIu64
", channel key %" PRIu64
, id
, key
);
1933 health_code_update();
1935 /* Send back returned value to session daemon */
1936 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1938 PERROR("send discarded events");
1944 case LTTNG_CONSUMER_LOST_PACKETS
:
1947 uint64_t lost_packets
;
1948 struct lttng_ht_iter iter
;
1949 struct lttng_ht
*ht
;
1950 struct lttng_consumer_stream
*stream
;
1951 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1952 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1954 DBG("UST consumer lost packets command for session id %"
1957 pthread_mutex_lock(&the_consumer_data
.lock
);
1959 ht
= the_consumer_data
.stream_list_ht
;
1962 * We only need a reference to the channel, but they are not
1963 * directly indexed, so we just use the first matching stream
1964 * to extract the information we need, we default to 0 if not
1965 * found (no packets lost if the channel is not yet in use).
1968 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1969 ht
->hash_fct(&id
, lttng_ht_seed
),
1971 &iter
.iter
, stream
, node_session_id
.node
) {
1972 if (stream
->chan
->key
== key
) {
1973 lost_packets
= stream
->chan
->lost_packets
;
1977 pthread_mutex_unlock(&the_consumer_data
.lock
);
1980 DBG("UST consumer lost packets command for session id %"
1981 PRIu64
", channel key %" PRIu64
, id
, key
);
1983 health_code_update();
1985 /* Send back returned value to session daemon */
1986 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1987 sizeof(lost_packets
));
1989 PERROR("send lost packets");
1995 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1997 int channel_monitor_pipe
, ret_send
,
1998 ret_set_channel_monitor_pipe
;
2001 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2002 /* Successfully received the command's type. */
2003 ret_send
= consumer_send_status_msg(sock
, ret_code
);
2008 ret_recv
= lttcomm_recv_fds_unix_sock(
2009 sock
, &channel_monitor_pipe
, 1);
2010 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
2011 ERR("Failed to receive channel monitor pipe");
2015 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
2016 ret_set_channel_monitor_pipe
=
2017 consumer_timer_thread_set_channel_monitor_pipe(
2018 channel_monitor_pipe
);
2019 if (!ret_set_channel_monitor_pipe
) {
2023 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2024 /* Set the pipe as non-blocking. */
2025 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
2026 if (ret_fcntl
== -1) {
2027 PERROR("fcntl get flags of the channel monitoring pipe");
2032 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
2033 flags
| O_NONBLOCK
);
2034 if (ret_fcntl
== -1) {
2035 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2038 DBG("Channel monitor pipe set as non-blocking");
2040 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2042 goto end_msg_sessiond
;
2044 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2046 struct lttng_consumer_channel
*found_channel
;
2047 uint64_t key
= msg
.u
.rotate_channel
.key
;
2048 int ret_send_status
;
2050 found_channel
= consumer_find_channel(key
);
2051 if (!found_channel
) {
2052 DBG("Channel %" PRIu64
" not found", key
);
2053 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2058 * Sample the rotate position of all the streams in
2061 rotate_channel
= lttng_consumer_rotate_channel(
2063 msg
.u
.rotate_channel
.relayd_id
,
2064 msg
.u
.rotate_channel
.metadata
, ctx
);
2065 if (rotate_channel
< 0) {
2066 ERR("Rotate channel failed");
2067 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2070 health_code_update();
2073 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2074 if (ret_send_status
< 0) {
2075 /* Somehow, the session daemon is not responding anymore. */
2076 goto end_rotate_channel_nosignal
;
2080 * Rotate the streams that are ready right now.
2081 * FIXME: this is a second consecutive iteration over the
2082 * streams in a channel, there is probably a better way to
2083 * handle this, but it needs to be after the
2084 * consumer_send_status_msg() call.
2086 if (found_channel
) {
2087 int ret_rotate_read_streams
;
2089 ret_rotate_read_streams
=
2090 lttng_consumer_rotate_ready_streams(
2093 if (ret_rotate_read_streams
< 0) {
2094 ERR("Rotate channel failed");
2098 end_rotate_channel_nosignal
:
2101 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2103 struct lttng_consumer_channel
*found_channel
;
2104 uint64_t key
= msg
.u
.clear_channel
.key
;
2105 int ret_send_status
;
2107 found_channel
= consumer_find_channel(key
);
2108 if (!found_channel
) {
2109 DBG("Channel %" PRIu64
" not found", key
);
2110 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2112 int ret_clear_channel
;
2114 ret_clear_channel
= lttng_consumer_clear_channel(
2116 if (ret_clear_channel
) {
2117 ERR("Clear channel failed key %" PRIu64
, key
);
2118 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
2121 health_code_update();
2123 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2124 if (ret_send_status
< 0) {
2125 /* Somehow, the session daemon is not responding anymore. */
2130 case LTTNG_CONSUMER_INIT
:
2132 int ret_send_status
;
2134 ret_code
= lttng_consumer_init_command(ctx
,
2135 msg
.u
.init
.sessiond_uuid
);
2136 health_code_update();
2137 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2138 if (ret_send_status
< 0) {
2139 /* Somehow, the session daemon is not responding anymore. */
2144 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2146 const struct lttng_credentials credentials
= {
2147 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2148 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2150 const bool is_local_trace
=
2151 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2152 const uint64_t relayd_id
=
2153 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2154 const char *chunk_override_name
=
2155 *msg
.u
.create_trace_chunk
.override_name
?
2156 msg
.u
.create_trace_chunk
.override_name
:
2158 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2161 * The session daemon will only provide a chunk directory file
2162 * descriptor for local traces.
2164 if (is_local_trace
) {
2166 int ret_send_status
;
2169 /* Acnowledge the reception of the command. */
2170 ret_send_status
= consumer_send_status_msg(
2171 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
2172 if (ret_send_status
< 0) {
2173 /* Somehow, the session daemon is not responding anymore. */
2178 * Receive trace chunk domain dirfd.
2180 ret_recv
= lttcomm_recv_fds_unix_sock(
2181 sock
, &chunk_dirfd
, 1);
2182 if (ret_recv
!= sizeof(chunk_dirfd
)) {
2183 ERR("Failed to receive trace chunk domain directory file descriptor");
2187 DBG("Received trace chunk domain directory fd (%d)",
2189 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2191 if (!chunk_directory_handle
) {
2192 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2193 if (close(chunk_dirfd
)) {
2194 PERROR("Failed to close chunk directory file descriptor");
2200 ret_code
= lttng_consumer_create_trace_chunk(
2201 !is_local_trace
? &relayd_id
: NULL
,
2202 msg
.u
.create_trace_chunk
.session_id
,
2203 msg
.u
.create_trace_chunk
.chunk_id
,
2204 (time_t) msg
.u
.create_trace_chunk
2205 .creation_timestamp
,
2206 chunk_override_name
,
2207 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2210 chunk_directory_handle
);
2211 lttng_directory_handle_put(chunk_directory_handle
);
2212 goto end_msg_sessiond
;
2214 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2216 enum lttng_trace_chunk_command_type close_command
=
2217 (lttng_trace_chunk_command_type
)
2218 msg
.u
.close_trace_chunk
.close_command
.value
;
2219 const uint64_t relayd_id
=
2220 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2221 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2222 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2225 ret_code
= lttng_consumer_close_trace_chunk(
2226 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2229 msg
.u
.close_trace_chunk
.session_id
,
2230 msg
.u
.close_trace_chunk
.chunk_id
,
2231 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2232 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2234 NULL
, closed_trace_chunk_path
);
2235 reply
.ret_code
= ret_code
;
2236 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2237 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2238 if (ret
!= sizeof(reply
)) {
2241 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2243 if (ret
!= reply
.path_length
) {
2248 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2250 const uint64_t relayd_id
=
2251 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2253 ret_code
= lttng_consumer_trace_chunk_exists(
2254 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2256 msg
.u
.trace_chunk_exists
.session_id
,
2257 msg
.u
.trace_chunk_exists
.chunk_id
);
2258 goto end_msg_sessiond
;
2260 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2262 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2263 struct lttng_consumer_channel
*found_channel
=
2264 consumer_find_channel(key
);
2266 if (found_channel
) {
2267 pthread_mutex_lock(&found_channel
->lock
);
2268 ret_code
= lttng_consumer_open_channel_packets(
2270 pthread_mutex_unlock(&found_channel
->lock
);
2273 * The channel could have disappeared in per-pid
2276 DBG("Channel %" PRIu64
" not found", key
);
2277 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2280 health_code_update();
2281 goto end_msg_sessiond
;
2289 * Return 1 to indicate success since the 0 value can be a socket
2290 * shutdown during the recv() or send() call.
2297 * The returned value here is not useful since either way we'll return 1 to
2298 * the caller because the session daemon socket management is done
2299 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2302 int ret_send_status
;
2304 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2305 if (ret_send_status
< 0) {
2316 * Free channel here since no one has a reference to it. We don't
2317 * free after that because a stream can store this pointer.
2319 destroy_channel(channel
);
2321 /* We have to send a status channel message indicating an error. */
2323 int ret_send_status
;
2325 ret_send_status
= consumer_send_status_channel(sock
, NULL
);
2326 if (ret_send_status
< 0) {
2327 /* Stop everything if session daemon can not be notified. */
2336 /* This will issue a consumer stop. */
2342 health_code_update();
2346 int lttng_ust_flush_buffer(struct lttng_consumer_stream
*stream
,
2347 int producer_active
)
2349 LTTNG_ASSERT(stream
);
2350 LTTNG_ASSERT(stream
->ustream
);
2352 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer_active
);
2356 * Take a snapshot for a specific stream.
2358 * Returns 0 on success, < 0 on error
2360 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2362 LTTNG_ASSERT(stream
);
2363 LTTNG_ASSERT(stream
->ustream
);
2365 return lttng_ust_ctl_snapshot(stream
->ustream
);
2369 * Sample consumed and produced positions for a specific stream.
2371 * Returns 0 on success, < 0 on error.
2373 int lttng_ustconsumer_sample_snapshot_positions(
2374 struct lttng_consumer_stream
*stream
)
2376 LTTNG_ASSERT(stream
);
2377 LTTNG_ASSERT(stream
->ustream
);
2379 return lttng_ust_ctl_snapshot_sample_positions(stream
->ustream
);
2383 * Get the produced position
2385 * Returns 0 on success, < 0 on error
2387 int lttng_ustconsumer_get_produced_snapshot(
2388 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2390 LTTNG_ASSERT(stream
);
2391 LTTNG_ASSERT(stream
->ustream
);
2394 return lttng_ust_ctl_snapshot_get_produced(stream
->ustream
, pos
);
2398 * Get the consumed position
2400 * Returns 0 on success, < 0 on error
2402 int lttng_ustconsumer_get_consumed_snapshot(
2403 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2405 LTTNG_ASSERT(stream
);
2406 LTTNG_ASSERT(stream
->ustream
);
2409 return lttng_ust_ctl_snapshot_get_consumed(stream
->ustream
, pos
);
2412 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2415 LTTNG_ASSERT(stream
);
2416 LTTNG_ASSERT(stream
->ustream
);
2418 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer
);
2421 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2423 LTTNG_ASSERT(stream
);
2424 LTTNG_ASSERT(stream
->ustream
);
2426 return lttng_ust_ctl_clear_buffer(stream
->ustream
);
2429 int lttng_ustconsumer_get_current_timestamp(
2430 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2432 LTTNG_ASSERT(stream
);
2433 LTTNG_ASSERT(stream
->ustream
);
2436 return lttng_ust_ctl_get_current_timestamp(stream
->ustream
, ts
);
2439 int lttng_ustconsumer_get_sequence_number(
2440 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2442 LTTNG_ASSERT(stream
);
2443 LTTNG_ASSERT(stream
->ustream
);
2446 return lttng_ust_ctl_get_sequence_number(stream
->ustream
, seq
);
2450 * Called when the stream signals the consumer that it has hung up.
2452 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2454 LTTNG_ASSERT(stream
);
2455 LTTNG_ASSERT(stream
->ustream
);
2457 pthread_mutex_lock(&stream
->lock
);
2458 if (!stream
->quiescent
) {
2459 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 0) < 0) {
2460 ERR("Failed to flush buffer on stream hang-up");
2462 stream
->quiescent
= true;
2465 pthread_mutex_unlock(&stream
->lock
);
2466 stream
->hangup_flush_done
= 1;
2469 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2474 LTTNG_ASSERT(chan
->uchan
);
2475 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2477 if (chan
->switch_timer_enabled
== 1) {
2478 consumer_timer_switch_stop(chan
);
2480 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2483 ret
= close(chan
->stream_fds
[i
]);
2487 if (chan
->shm_path
[0]) {
2488 char shm_path
[PATH_MAX
];
2490 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2492 ERR("Cannot get stream shm path");
2494 ret
= run_as_unlink(shm_path
,
2495 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2496 chan
->buffer_credentials
)),
2497 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2498 chan
->buffer_credentials
)));
2500 PERROR("unlink %s", shm_path
);
2506 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2509 LTTNG_ASSERT(chan
->uchan
);
2510 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2512 consumer_metadata_cache_destroy(chan
);
2513 lttng_ust_ctl_destroy_channel(chan
->uchan
);
2514 /* Try to rmdir all directories under shm_path root. */
2515 if (chan
->root_shm_path
[0]) {
2516 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2517 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2518 chan
->buffer_credentials
)),
2519 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2520 chan
->buffer_credentials
)),
2521 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2523 free(chan
->stream_fds
);
2526 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2528 LTTNG_ASSERT(stream
);
2529 LTTNG_ASSERT(stream
->ustream
);
2531 if (stream
->chan
->switch_timer_enabled
== 1) {
2532 consumer_timer_switch_stop(stream
->chan
);
2534 lttng_ust_ctl_destroy_stream(stream
->ustream
);
2537 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2539 LTTNG_ASSERT(stream
);
2540 LTTNG_ASSERT(stream
->ustream
);
2542 return lttng_ust_ctl_stream_get_wakeup_fd(stream
->ustream
);
2545 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2547 LTTNG_ASSERT(stream
);
2548 LTTNG_ASSERT(stream
->ustream
);
2550 return lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
2554 * Write up to one packet from the metadata cache to the channel.
2556 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2557 * negative value on error.
2560 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2565 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2566 if (stream
->chan
->metadata_cache
->contents
.size
==
2567 stream
->ust_metadata_pushed
) {
2569 * In the context of a user space metadata channel, a
2570 * change in version can be detected in two ways:
2571 * 1) During the pre-consume of the `read_subbuffer` loop,
2572 * 2) When populating the metadata ring buffer (i.e. here).
2574 * This function is invoked when there is no metadata
2575 * available in the ring-buffer. If all data was consumed
2576 * up to the size of the metadata cache, there is no metadata
2577 * to insert in the ring-buffer.
2579 * However, the metadata version could still have changed (a
2580 * regeneration without any new data will yield the same cache
2583 * The cache's version is checked for a version change and the
2584 * consumed position is reset if one occurred.
2586 * This check is only necessary for the user space domain as
2587 * it has to manage the cache explicitly. If this reset was not
2588 * performed, no metadata would be consumed (and no reset would
2589 * occur as part of the pre-consume) until the metadata size
2590 * exceeded the cache size.
2592 if (stream
->metadata_version
!=
2593 stream
->chan
->metadata_cache
->version
) {
2594 metadata_stream_reset_cache_consumed_position(stream
);
2595 consumer_stream_metadata_set_version(stream
,
2596 stream
->chan
->metadata_cache
->version
);
2603 write_len
= lttng_ust_ctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2604 &stream
->chan
->metadata_cache
->contents
.data
[stream
->ust_metadata_pushed
],
2605 stream
->chan
->metadata_cache
->contents
.size
-
2606 stream
->ust_metadata_pushed
);
2607 LTTNG_ASSERT(write_len
!= 0);
2608 if (write_len
< 0) {
2609 ERR("Writing one metadata packet");
2613 stream
->ust_metadata_pushed
+= write_len
;
2615 LTTNG_ASSERT(stream
->chan
->metadata_cache
->contents
.size
>=
2616 stream
->ust_metadata_pushed
);
2620 * Switch packet (but don't open the next one) on every commit of
2621 * a metadata packet. Since the subbuffer is fully filled (with padding,
2622 * if needed), the stream is "quiescent" after this commit.
2624 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 1)) {
2625 ERR("Failed to flush buffer while committing one metadata packet");
2628 stream
->quiescent
= true;
2631 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2637 * Sync metadata meaning request them to the session daemon and snapshot to the
2638 * metadata thread can consumer them.
2640 * Metadata stream lock is held here, but we need to release it when
2641 * interacting with sessiond, else we cause a deadlock with live
2642 * awaiting on metadata to be pushed out.
2644 * The RCU read side lock must be held by the caller.
2646 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2647 struct lttng_consumer_local_data
*ctx
,
2648 struct lttng_consumer_stream
*metadata_stream
)
2651 enum sync_metadata_status status
;
2652 struct lttng_consumer_channel
*metadata_channel
;
2655 LTTNG_ASSERT(metadata_stream
);
2656 ASSERT_RCU_READ_LOCKED();
2658 metadata_channel
= metadata_stream
->chan
;
2659 pthread_mutex_unlock(&metadata_stream
->lock
);
2661 * Request metadata from the sessiond, but don't wait for the flush
2662 * because we locked the metadata thread.
2664 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2665 pthread_mutex_lock(&metadata_stream
->lock
);
2667 status
= SYNC_METADATA_STATUS_ERROR
;
2672 * The metadata stream and channel can be deleted while the
2673 * metadata stream lock was released. The streamed is checked
2674 * for deletion before we use it further.
2676 * Note that it is safe to access a logically-deleted stream since its
2677 * existence is still guaranteed by the RCU read side lock. However,
2678 * it should no longer be used. The close/deletion of the metadata
2679 * channel and stream already guarantees that all metadata has been
2680 * consumed. Therefore, there is nothing left to do in this function.
2682 if (consumer_stream_is_deleted(metadata_stream
)) {
2683 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2684 metadata_stream
->key
);
2685 status
= SYNC_METADATA_STATUS_NO_DATA
;
2689 ret
= commit_one_metadata_packet(metadata_stream
);
2691 status
= SYNC_METADATA_STATUS_ERROR
;
2693 } else if (ret
> 0) {
2694 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2695 } else /* ret == 0 */ {
2696 status
= SYNC_METADATA_STATUS_NO_DATA
;
2700 ret
= lttng_ust_ctl_snapshot(metadata_stream
->ustream
);
2702 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2703 status
= SYNC_METADATA_STATUS_ERROR
;
2712 * Return 0 on success else a negative value.
2714 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2715 struct lttng_consumer_local_data
*ctx
)
2718 struct lttng_ust_ctl_consumer_stream
*ustream
;
2720 LTTNG_ASSERT(stream
);
2723 ustream
= stream
->ustream
;
2726 * First, we are going to check if there is a new subbuffer available
2727 * before reading the stream wait_fd.
2729 /* Get the next subbuffer */
2730 ret
= lttng_ust_ctl_get_next_subbuf(ustream
);
2732 /* No more data found, flag the stream. */
2733 stream
->has_data
= 0;
2738 ret
= lttng_ust_ctl_put_subbuf(ustream
);
2741 /* This stream still has data. Flag it and wake up the data thread. */
2742 stream
->has_data
= 1;
2744 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2747 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2748 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2753 /* The wake up pipe has been notified. */
2754 ctx
->has_wakeup
= 1;
2762 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2767 * We can consume the 1 byte written into the wait_fd by
2768 * UST. Don't trigger error if we cannot read this one byte
2769 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2771 * This is only done when the stream is monitored by a thread,
2772 * before the flush is done after a hangup and if the stream
2773 * is not flagged with data since there might be nothing to
2774 * consume in the wait fd but still have data available
2775 * flagged by the consumer wake up pipe.
2777 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2781 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2782 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2790 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2791 struct stream_subbuffer
*subbuf
)
2795 ret
= lttng_ust_ctl_get_subbuf_size(
2796 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2801 ret
= lttng_ust_ctl_get_padded_subbuf_size(
2802 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2811 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2812 struct stream_subbuffer
*subbuf
)
2816 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2821 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2827 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2828 struct stream_subbuffer
*subbuf
)
2832 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2837 ret
= lttng_ust_ctl_get_packet_size(
2838 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2840 PERROR("Failed to get sub-buffer packet size");
2844 ret
= lttng_ust_ctl_get_content_size(
2845 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2847 PERROR("Failed to get sub-buffer content size");
2851 ret
= lttng_ust_ctl_get_timestamp_begin(
2852 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2854 PERROR("Failed to get sub-buffer begin timestamp");
2858 ret
= lttng_ust_ctl_get_timestamp_end(
2859 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2861 PERROR("Failed to get sub-buffer end timestamp");
2865 ret
= lttng_ust_ctl_get_events_discarded(
2866 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2868 PERROR("Failed to get sub-buffer events discarded count");
2872 ret
= lttng_ust_ctl_get_sequence_number(stream
->ustream
,
2873 &subbuf
->info
.data
.sequence_number
.value
);
2875 /* May not be supported by older LTTng-modules. */
2876 if (ret
!= -ENOTTY
) {
2877 PERROR("Failed to get sub-buffer sequence number");
2881 subbuf
->info
.data
.sequence_number
.is_set
= true;
2884 ret
= lttng_ust_ctl_get_stream_id(
2885 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2887 PERROR("Failed to get stream id");
2891 ret
= lttng_ust_ctl_get_instance_id(stream
->ustream
,
2892 &subbuf
->info
.data
.stream_instance_id
.value
);
2894 /* May not be supported by older LTTng-modules. */
2895 if (ret
!= -ENOTTY
) {
2896 PERROR("Failed to get stream instance id");
2900 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2906 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2907 struct stream_subbuffer
*subbuffer
)
2912 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2918 ret
= get_current_subbuf_addr(stream
, &addr
);
2923 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2924 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2925 LTTNG_ASSERT(subbuffer
->buffer
.buffer
.data
!= NULL
);
2930 static enum get_next_subbuffer_status
get_next_subbuffer(
2931 struct lttng_consumer_stream
*stream
,
2932 struct stream_subbuffer
*subbuffer
)
2935 enum get_next_subbuffer_status status
;
2937 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2940 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
2945 * The caller only expects -ENODATA when there is no data to
2946 * read, but the kernel tracer returns -EAGAIN when there is
2947 * currently no data for a non-finalized stream, and -ENODATA
2948 * when there is no data for a finalized stream. Those can be
2949 * combined into a -ENODATA return value.
2951 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2954 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2958 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2960 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2967 static enum get_next_subbuffer_status
get_next_subbuffer_metadata(
2968 struct lttng_consumer_stream
*stream
,
2969 struct stream_subbuffer
*subbuffer
)
2976 unsigned long consumed_pos
, produced_pos
;
2977 enum get_next_subbuffer_status status
;
2980 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2982 got_subbuffer
= true;
2984 got_subbuffer
= false;
2985 if (ret
!= -EAGAIN
) {
2987 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2993 * Determine if the cache is empty and ensure that a sub-buffer
2994 * is made available if the cache is not empty.
2996 if (!got_subbuffer
) {
2997 ret
= commit_one_metadata_packet(stream
);
2998 if (ret
< 0 && ret
!= -ENOBUFS
) {
2999 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3001 } else if (ret
== 0) {
3002 /* Not an error, the cache is empty. */
3004 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
3007 cache_empty
= false;
3010 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3011 cache_empty
= stream
->chan
->metadata_cache
->contents
.size
==
3012 stream
->ust_metadata_pushed
;
3013 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3015 } while (!got_subbuffer
);
3017 /* Populate sub-buffer infos and view. */
3018 ret
= get_next_subbuffer_common(stream
, subbuffer
);
3020 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3024 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
3027 * -EAGAIN is not expected since we got a sub-buffer and haven't
3028 * pushed the consumption position yet (on put_next).
3030 PERROR("Failed to take a snapshot of metadata buffer positions");
3031 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3035 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
3037 PERROR("Failed to get metadata consumed position");
3038 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3042 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
3044 PERROR("Failed to get metadata produced position");
3045 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3049 /* Last sub-buffer of the ring buffer ? */
3050 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
3053 * The sessiond registry lock ensures that coherent units of metadata
3054 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3055 * acquired, the cache is empty, and it is the only available sub-buffer
3056 * available, it is safe to assume that it is "coherent".
3058 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
3060 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
3061 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
3066 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
3067 struct stream_subbuffer
*subbuffer
)
3069 const int ret
= lttng_ust_ctl_put_next_subbuf(stream
->ustream
);
3071 LTTNG_ASSERT(ret
== 0);
3075 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3076 struct lttng_consumer_local_data
*ctx
)
3078 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3079 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3082 static int lttng_ustconsumer_set_stream_ops(
3083 struct lttng_consumer_stream
*stream
)
3087 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3088 if (stream
->metadata_flag
) {
3089 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3090 get_next_subbuffer_metadata
;
3091 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3092 extract_metadata_subbuffer_info
;
3093 stream
->read_subbuffer_ops
.reset_metadata
=
3094 metadata_stream_reset_cache_consumed_position
;
3095 if (stream
->chan
->is_live
) {
3096 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3097 ret
= consumer_stream_enable_metadata_bucketization(
3104 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3106 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3107 extract_data_subbuffer_info
;
3108 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3109 if (stream
->chan
->is_live
) {
3110 stream
->read_subbuffer_ops
.send_live_beacon
=
3111 consumer_flush_ust_index
;
3115 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3121 * Called when a stream is created.
3123 * Return 0 on success or else a negative value.
3125 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3129 LTTNG_ASSERT(stream
);
3132 * Don't create anything if this is set for streaming or if there is
3133 * no current trace chunk on the parent channel.
3135 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3136 stream
->chan
->trace_chunk
) {
3137 ret
= consumer_stream_create_output_files(stream
, true);
3143 lttng_ustconsumer_set_stream_ops(stream
);
3151 * Check if data is still being extracted from the buffers for a specific
3152 * stream. Consumer data lock MUST be acquired before calling this function
3153 * and the stream lock.
3155 * Return 1 if the traced data are still getting read else 0 meaning that the
3156 * data is available for trace viewer reading.
3158 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3162 LTTNG_ASSERT(stream
);
3163 LTTNG_ASSERT(stream
->ustream
);
3164 ASSERT_LOCKED(stream
->lock
);
3166 DBG("UST consumer checking data pending");
3168 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3173 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3174 uint64_t contiguous
, pushed
;
3176 /* Ease our life a bit. */
3177 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3178 contiguous
= stream
->chan
->metadata_cache
->contents
.size
;
3179 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3180 pushed
= stream
->ust_metadata_pushed
;
3183 * We can simply check whether all contiguously available data
3184 * has been pushed to the ring buffer, since the push operation
3185 * is performed within get_next_subbuf(), and because both
3186 * get_next_subbuf() and put_next_subbuf() are issued atomically
3187 * thanks to the stream lock within
3188 * lttng_ustconsumer_read_subbuffer(). This basically means that
3189 * whetnever ust_metadata_pushed is incremented, the associated
3190 * metadata has been consumed from the metadata stream.
3192 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3193 contiguous
, pushed
);
3194 LTTNG_ASSERT(((int64_t) (contiguous
- pushed
)) >= 0);
3195 if ((contiguous
!= pushed
) ||
3196 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3197 ret
= 1; /* Data is pending */
3201 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
3204 * There is still data so let's put back this
3207 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
3208 LTTNG_ASSERT(ret
== 0);
3209 ret
= 1; /* Data is pending */
3214 /* Data is NOT pending so ready to be read. */
3222 * Stop a given metadata channel timer if enabled and close the wait fd which
3223 * is the poll pipe of the metadata stream.
3225 * This MUST be called with the metadata channel lock acquired.
3227 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3231 LTTNG_ASSERT(metadata
);
3232 LTTNG_ASSERT(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3234 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3236 if (metadata
->switch_timer_enabled
== 1) {
3237 consumer_timer_switch_stop(metadata
);
3240 if (!metadata
->metadata_stream
) {
3245 * Closing write side so the thread monitoring the stream wakes up if any
3246 * and clean the metadata stream.
3248 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3249 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3251 PERROR("closing metadata pipe write side");
3253 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3261 * Close every metadata stream wait fd of the metadata hash table. This
3262 * function MUST be used very carefully so not to run into a race between the
3263 * metadata thread handling streams and this function closing their wait fd.
3265 * For UST, this is used when the session daemon hangs up. Its the metadata
3266 * producer so calling this is safe because we are assured that no state change
3267 * can occur in the metadata thread for the streams in the hash table.
3269 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3271 struct lttng_ht_iter iter
;
3272 struct lttng_consumer_stream
*stream
;
3274 LTTNG_ASSERT(metadata_ht
);
3275 LTTNG_ASSERT(metadata_ht
->ht
);
3277 DBG("UST consumer closing all metadata streams");
3280 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3283 health_code_update();
3285 pthread_mutex_lock(&stream
->chan
->lock
);
3286 lttng_ustconsumer_close_metadata(stream
->chan
);
3287 pthread_mutex_unlock(&stream
->chan
->lock
);
3293 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3297 ret
= lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
3299 ERR("Unable to close wakeup fd");
3304 * Please refer to consumer-timer.c before adding any lock within this
3305 * function or any of its callees. Timers have a very strict locking
3306 * semantic with respect to teardown. Failure to respect this semantic
3307 * introduces deadlocks.
3309 * DON'T hold the metadata lock when calling this function, else this
3310 * can cause deadlock involving consumer awaiting for metadata to be
3311 * pushed out due to concurrent interaction with the session daemon.
3313 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3314 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3316 struct lttcomm_metadata_request_msg request
;
3317 struct lttcomm_consumer_msg msg
;
3318 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3319 uint64_t len
, key
, offset
, version
;
3322 LTTNG_ASSERT(channel
);
3323 LTTNG_ASSERT(channel
->metadata_cache
);
3325 memset(&request
, 0, sizeof(request
));
3327 /* send the metadata request to sessiond */
3328 switch (the_consumer_data
.type
) {
3329 case LTTNG_CONSUMER64_UST
:
3330 request
.bits_per_long
= 64;
3332 case LTTNG_CONSUMER32_UST
:
3333 request
.bits_per_long
= 32;
3336 request
.bits_per_long
= 0;
3340 request
.session_id
= channel
->session_id
;
3341 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3343 * Request the application UID here so the metadata of that application can
3344 * be sent back. The channel UID corresponds to the user UID of the session
3345 * used for the rights on the stream file(s).
3347 request
.uid
= channel
->ust_app_uid
;
3348 request
.key
= channel
->key
;
3350 DBG("Sending metadata request to sessiond, session id %" PRIu64
3351 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3352 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3355 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3357 health_code_update();
3359 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3362 ERR("Asking metadata to sessiond");
3366 health_code_update();
3368 /* Receive the metadata from sessiond */
3369 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3371 if (ret
!= sizeof(msg
)) {
3372 DBG("Consumer received unexpected message size %d (expects %zu)",
3374 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3376 * The ret value might 0 meaning an orderly shutdown but this is ok
3377 * since the caller handles this.
3382 health_code_update();
3384 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3385 /* No registry found */
3386 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3390 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3391 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3396 len
= msg
.u
.push_metadata
.len
;
3397 key
= msg
.u
.push_metadata
.key
;
3398 offset
= msg
.u
.push_metadata
.target_offset
;
3399 version
= msg
.u
.push_metadata
.version
;
3401 LTTNG_ASSERT(key
== channel
->key
);
3403 DBG("No new metadata to receive for key %" PRIu64
, key
);
3406 health_code_update();
3408 /* Tell session daemon we are ready to receive the metadata. */
3409 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3410 LTTCOMM_CONSUMERD_SUCCESS
);
3411 if (ret
< 0 || len
== 0) {
3413 * Somehow, the session daemon is not responding anymore or there is
3414 * nothing to receive.
3419 health_code_update();
3421 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3422 key
, offset
, len
, version
, channel
, timer
, wait
);
3425 * Only send the status msg if the sessiond is alive meaning a positive
3428 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3433 health_code_update();
3435 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3440 * Return the ustctl call for the get stream id.
3442 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3443 uint64_t *stream_id
)
3445 LTTNG_ASSERT(stream
);
3446 LTTNG_ASSERT(stream_id
);
3448 return lttng_ust_ctl_get_stream_id(stream
->ustream
, stream_id
);
3451 void lttng_ustconsumer_sigbus_handle(void *addr
)
3453 lttng_ust_ctl_sigbus_handle(addr
);