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.hpp>
29 #include <common/common.hpp>
30 #include <common/sessiond-comm/sessiond-comm.hpp>
31 #include <common/relayd/relayd.hpp>
32 #include <common/compat/fcntl.hpp>
33 #include <common/compat/endian.hpp>
34 #include <common/consumer/consumer-metadata-cache.hpp>
35 #include <common/consumer/consumer-stream.hpp>
36 #include <common/consumer/consumer-timer.hpp>
37 #include <common/utils.hpp>
38 #include <common/index/index.hpp>
39 #include <common/consumer/consumer.hpp>
40 #include <common/shm.hpp>
41 #include <common/optional.hpp>
43 #include "ust-consumer.hpp"
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 * Add channel to internal consumer state.
55 * Returns 0 on success or else a negative value.
57 static int add_channel(struct lttng_consumer_channel
*channel
,
58 struct lttng_consumer_local_data
*ctx
)
62 LTTNG_ASSERT(channel
);
65 if (ctx
->on_recv_channel
!= NULL
) {
66 ret
= ctx
->on_recv_channel(channel
);
68 ret
= consumer_add_channel(channel
, ctx
);
70 /* Most likely an ENOMEM. */
71 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
75 ret
= consumer_add_channel(channel
, ctx
);
78 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
85 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
86 * error value if applicable is set in it else it is kept untouched.
88 * Return NULL on error else the newly allocated stream object.
90 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
91 struct lttng_consumer_channel
*channel
,
92 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
95 struct lttng_consumer_stream
*stream
= NULL
;
97 LTTNG_ASSERT(channel
);
100 stream
= consumer_stream_create(
107 channel
->trace_chunk
,
112 if (stream
== NULL
) {
116 * We could not find the channel. Can happen if cpu hotplug
117 * happens while tearing down.
119 DBG3("Could not find channel");
124 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
130 consumer_stream_update_channel_attributes(stream
, channel
);
134 *_alloc_ret
= alloc_ret
;
140 * Send the given stream pointer to the corresponding thread.
142 * Returns 0 on success else a negative value.
144 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
145 struct lttng_consumer_local_data
*ctx
)
148 struct lttng_pipe
*stream_pipe
;
150 /* Get the right pipe where the stream will be sent. */
151 if (stream
->metadata_flag
) {
152 consumer_add_metadata_stream(stream
);
153 stream_pipe
= ctx
->consumer_metadata_pipe
;
155 consumer_add_data_stream(stream
);
156 stream_pipe
= ctx
->consumer_data_pipe
;
160 * From this point on, the stream's ownership has been moved away from
161 * the channel and it becomes globally visible. Hence, remove it from
162 * the local stream list to prevent the stream from being both local and
165 stream
->globally_visible
= 1;
166 cds_list_del_init(&stream
->send_node
);
168 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
170 ERR("Consumer write %s stream to pipe %d",
171 stream
->metadata_flag
? "metadata" : "data",
172 lttng_pipe_get_writefd(stream_pipe
));
173 if (stream
->metadata_flag
) {
174 consumer_del_stream_for_metadata(stream
);
176 consumer_del_stream_for_data(stream
);
186 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
188 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
191 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
192 stream_shm_path
[PATH_MAX
- 1] = '\0';
193 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
198 strncat(stream_shm_path
, cpu_nr
,
199 PATH_MAX
- strlen(stream_shm_path
) - 1);
206 * Create streams for the given channel using liblttng-ust-ctl.
207 * The channel lock must be acquired by the caller.
209 * Return 0 on success else a negative value.
211 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
212 struct lttng_consumer_local_data
*ctx
)
215 struct lttng_ust_ctl_consumer_stream
*ustream
;
216 struct lttng_consumer_stream
*stream
;
217 pthread_mutex_t
*current_stream_lock
= NULL
;
219 LTTNG_ASSERT(channel
);
223 * While a stream is available from ustctl. When NULL is returned, we've
224 * reached the end of the possible stream for the channel.
226 while ((ustream
= lttng_ust_ctl_create_stream(channel
->uchan
, cpu
))) {
228 int ust_metadata_pipe
[2];
230 health_code_update();
232 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
233 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
235 ERR("Create ust metadata poll pipe");
238 wait_fd
= ust_metadata_pipe
[0];
240 wait_fd
= lttng_ust_ctl_stream_get_wait_fd(ustream
);
243 /* Allocate consumer stream object. */
244 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
248 stream
->ustream
= ustream
;
250 * Store it so we can save multiple function calls afterwards since
251 * this value is used heavily in the stream threads. This is UST
252 * specific so this is why it's done after allocation.
254 stream
->wait_fd
= wait_fd
;
257 * Increment channel refcount since the channel reference has now been
258 * assigned in the allocation process above.
260 if (stream
->chan
->monitor
) {
261 uatomic_inc(&stream
->chan
->refcount
);
264 pthread_mutex_lock(&stream
->lock
);
265 current_stream_lock
= &stream
->lock
;
267 * Order is important this is why a list is used. On error, the caller
268 * should clean this list.
270 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
272 ret
= lttng_ust_ctl_get_max_subbuf_size(stream
->ustream
,
273 &stream
->max_sb_size
);
275 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s",
280 /* Do actions once stream has been received. */
281 if (ctx
->on_recv_stream
) {
282 ret
= ctx
->on_recv_stream(stream
);
288 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
289 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
291 /* Set next CPU stream. */
292 channel
->streams
.count
= ++cpu
;
294 /* Keep stream reference when creating metadata. */
295 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
296 channel
->metadata_stream
= stream
;
297 if (channel
->monitor
) {
298 /* Set metadata poll pipe if we created one */
299 memcpy(stream
->ust_metadata_poll_pipe
,
301 sizeof(ust_metadata_pipe
));
304 pthread_mutex_unlock(&stream
->lock
);
305 current_stream_lock
= NULL
;
312 if (current_stream_lock
) {
313 pthread_mutex_unlock(current_stream_lock
);
318 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
319 const struct lttng_credentials
*session_credentials
)
321 char shm_path
[PATH_MAX
];
324 if (!channel
->shm_path
[0]) {
325 return shm_create_anonymous("ust-consumer");
327 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
331 return run_as_open(shm_path
,
332 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
333 lttng_credentials_get_uid(session_credentials
),
334 lttng_credentials_get_gid(session_credentials
));
341 * Create an UST channel with the given attributes and send it to the session
342 * daemon using the ust ctl API.
344 * Return 0 on success or else a negative value.
346 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
347 struct lttng_ust_ctl_consumer_channel_attr
*attr
,
348 struct lttng_ust_ctl_consumer_channel
**ust_chanp
)
350 int ret
, nr_stream_fds
, i
, j
;
352 struct lttng_ust_ctl_consumer_channel
*ust_channel
;
354 LTTNG_ASSERT(channel
);
356 LTTNG_ASSERT(ust_chanp
);
357 LTTNG_ASSERT(channel
->buffer_credentials
.is_set
);
359 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
360 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
361 "switch_timer_interval: %u, read_timer_interval: %u, "
362 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
363 attr
->num_subbuf
, attr
->switch_timer_interval
,
364 attr
->read_timer_interval
, attr
->output
, attr
->type
);
366 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
369 nr_stream_fds
= lttng_ust_ctl_get_nr_stream_per_channel();
370 stream_fds
= calloc
<int>(nr_stream_fds
);
375 for (i
= 0; i
< nr_stream_fds
; i
++) {
376 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
377 &channel
->buffer_credentials
.value
);
378 if (stream_fds
[i
] < 0) {
383 ust_channel
= lttng_ust_ctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
388 channel
->nr_stream_fds
= nr_stream_fds
;
389 channel
->stream_fds
= stream_fds
;
390 *ust_chanp
= ust_channel
;
396 for (j
= i
- 1; j
>= 0; j
--) {
399 closeret
= close(stream_fds
[j
]);
403 if (channel
->shm_path
[0]) {
404 char shm_path
[PATH_MAX
];
406 closeret
= get_stream_shm_path(shm_path
,
407 channel
->shm_path
, j
);
409 ERR("Cannot get stream shm path");
411 closeret
= run_as_unlink(shm_path
,
412 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
413 channel
->buffer_credentials
)),
414 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
415 channel
->buffer_credentials
)));
417 PERROR("unlink %s", shm_path
);
421 /* Try to rmdir all directories under shm_path root. */
422 if (channel
->root_shm_path
[0]) {
423 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
424 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
425 channel
->buffer_credentials
)),
426 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
427 channel
->buffer_credentials
)),
428 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
436 * Send a single given stream to the session daemon using the sock.
438 * Return 0 on success else a negative value.
440 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
444 LTTNG_ASSERT(stream
);
445 LTTNG_ASSERT(sock
>= 0);
447 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
449 /* Send stream to session daemon. */
450 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, stream
->ustream
);
460 * Send channel to sessiond and relayd if applicable.
462 * Return 0 on success or else a negative value.
464 static int send_channel_to_sessiond_and_relayd(int sock
,
465 struct lttng_consumer_channel
*channel
,
466 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
468 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
469 struct lttng_consumer_stream
*stream
;
470 uint64_t net_seq_idx
= -1ULL;
472 LTTNG_ASSERT(channel
);
474 LTTNG_ASSERT(sock
>= 0);
476 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
478 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
479 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
481 health_code_update();
483 /* Try to send the stream to the relayd if one is available. */
484 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
485 stream
->key
, channel
->name
);
486 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
489 * Flag that the relayd was the problem here probably due to a
490 * communicaton error on the socket.
495 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
497 if (net_seq_idx
== -1ULL) {
498 net_seq_idx
= stream
->net_seq_idx
;
503 /* Inform sessiond that we are about to send channel and streams. */
504 ret
= consumer_send_status_msg(sock
, ret_code
);
505 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
507 * Either the session daemon is not responding or the relayd died so we
513 /* Send channel to sessiond. */
514 ret
= lttng_ust_ctl_send_channel_to_sessiond(sock
, channel
->uchan
);
519 ret
= lttng_ust_ctl_channel_close_wakeup_fd(channel
->uchan
);
524 /* The channel was sent successfully to the sessiond at this point. */
525 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
527 health_code_update();
529 /* Send stream to session daemon. */
530 ret
= send_sessiond_stream(sock
, stream
);
536 /* Tell sessiond there is no more stream. */
537 ret
= lttng_ust_ctl_send_stream_to_sessiond(sock
, NULL
);
542 DBG("UST consumer NULL stream sent to sessiond");
547 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
554 * Creates a channel and streams and add the channel it to the channel internal
555 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
558 * Return 0 on success or else, a negative value is returned and the channel
559 * MUST be destroyed by consumer_del_channel().
561 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
562 struct lttng_consumer_channel
*channel
,
563 struct lttng_ust_ctl_consumer_channel_attr
*attr
)
568 LTTNG_ASSERT(channel
);
572 * This value is still used by the kernel consumer since for the kernel,
573 * the stream ownership is not IN the consumer so we need to have the
574 * number of left stream that needs to be initialized so we can know when
575 * to delete the channel (see consumer.c).
577 * As for the user space tracer now, the consumer creates and sends the
578 * stream to the session daemon which only sends them to the application
579 * once every stream of a channel is received making this value useless
580 * because we they will be added to the poll thread before the application
581 * receives them. This ensures that a stream can not hang up during
582 * initilization of a channel.
584 channel
->nb_init_stream_left
= 0;
586 /* The reply msg status is handled in the following call. */
587 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
592 channel
->wait_fd
= lttng_ust_ctl_channel_get_wait_fd(channel
->uchan
);
595 * For the snapshots (no monitor), we create the metadata streams
596 * on demand, not during the channel creation.
598 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
603 /* Open all streams for this channel. */
604 pthread_mutex_lock(&channel
->lock
);
605 ret
= create_ust_streams(channel
, ctx
);
606 pthread_mutex_unlock(&channel
->lock
);
616 * Send all stream of a channel to the right thread handling it.
618 * On error, return a negative value else 0 on success.
620 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
621 struct lttng_consumer_local_data
*ctx
)
624 struct lttng_consumer_stream
*stream
, *stmp
;
626 LTTNG_ASSERT(channel
);
629 /* Send streams to the corresponding thread. */
630 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
633 health_code_update();
635 /* Sending the stream to the thread. */
636 ret
= send_stream_to_thread(stream
, ctx
);
639 * If we are unable to send the stream to the thread, there is
640 * a big problem so just stop everything.
651 * Flush channel's streams using the given key to retrieve the channel.
653 * Return 0 on success else an LTTng error code.
655 static int flush_channel(uint64_t chan_key
)
658 struct lttng_consumer_channel
*channel
;
659 struct lttng_consumer_stream
*stream
;
661 struct lttng_ht_iter iter
;
663 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
666 channel
= consumer_find_channel(chan_key
);
668 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
669 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
673 ht
= the_consumer_data
.stream_per_chan_id_ht
;
675 /* For each stream of the channel id, flush it. */
676 cds_lfht_for_each_entry_duplicate(ht
->ht
,
677 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
678 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
680 health_code_update();
682 pthread_mutex_lock(&stream
->lock
);
685 * Protect against concurrent teardown of a stream.
687 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
691 if (!stream
->quiescent
) {
692 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
694 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64
", channel name = '%s'",
695 chan_key
, channel
->name
);
696 ret
= LTTNG_ERR_BUFFER_FLUSH_FAILED
;
697 pthread_mutex_unlock(&stream
->lock
);
700 stream
->quiescent
= true;
703 pthread_mutex_unlock(&stream
->lock
);
707 * Send one last buffer statistics update to the session daemon. This
708 * ensures that the session daemon gets at least one statistics update
709 * per channel even in the case of short-lived channels, such as when a
710 * short-lived app is traced in per-pid mode.
712 sample_and_send_channel_buffer_stats(channel
);
719 * Clear quiescent state from channel's streams using the given key to
720 * retrieve the channel.
722 * Return 0 on success else an LTTng error code.
724 static int clear_quiescent_channel(uint64_t chan_key
)
727 struct lttng_consumer_channel
*channel
;
728 struct lttng_consumer_stream
*stream
;
730 struct lttng_ht_iter iter
;
732 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
735 channel
= consumer_find_channel(chan_key
);
737 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
738 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
742 ht
= the_consumer_data
.stream_per_chan_id_ht
;
744 /* For each stream of the channel id, clear quiescent state. */
745 cds_lfht_for_each_entry_duplicate(ht
->ht
,
746 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
747 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
749 health_code_update();
751 pthread_mutex_lock(&stream
->lock
);
752 stream
->quiescent
= false;
753 pthread_mutex_unlock(&stream
->lock
);
761 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
763 * Return 0 on success else an LTTng error code.
765 static int close_metadata(uint64_t chan_key
)
768 struct lttng_consumer_channel
*channel
;
769 unsigned int channel_monitor
;
771 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
773 channel
= consumer_find_channel(chan_key
);
776 * This is possible if the metadata thread has issue a delete because
777 * the endpoint point of the stream hung up. There is no way the
778 * session daemon can know about it thus use a DBG instead of an actual
781 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
782 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
786 pthread_mutex_lock(&the_consumer_data
.lock
);
787 pthread_mutex_lock(&channel
->lock
);
788 channel_monitor
= channel
->monitor
;
789 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
793 lttng_ustconsumer_close_metadata(channel
);
794 pthread_mutex_unlock(&channel
->lock
);
795 pthread_mutex_unlock(&the_consumer_data
.lock
);
798 * The ownership of a metadata channel depends on the type of
799 * session to which it belongs. In effect, the monitor flag is checked
800 * to determine if this metadata channel is in "snapshot" mode or not.
802 * In the non-snapshot case, the metadata channel is created along with
803 * a single stream which will remain present until the metadata channel
804 * is destroyed (on the destruction of its session). In this case, the
805 * metadata stream in "monitored" by the metadata poll thread and holds
806 * the ownership of its channel.
808 * Closing the metadata will cause the metadata stream's "metadata poll
809 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
810 * thread which will teardown the metadata stream which, in return,
811 * deletes the metadata channel.
813 * In the snapshot case, the metadata stream is created and destroyed
814 * on every snapshot record. Since the channel doesn't have an owner
815 * other than the session daemon, it is safe to destroy it immediately
816 * on reception of the CLOSE_METADATA command.
818 if (!channel_monitor
) {
820 * The channel and consumer_data locks must be
821 * released before this call since consumer_del_channel
822 * re-acquires the channel and consumer_data locks to teardown
823 * the channel and queue its reclamation by the "call_rcu"
826 consumer_del_channel(channel
);
831 pthread_mutex_unlock(&channel
->lock
);
832 pthread_mutex_unlock(&the_consumer_data
.lock
);
838 * RCU read side lock MUST be acquired before calling this function.
840 * Return 0 on success else an LTTng error code.
842 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
845 struct lttng_consumer_channel
*metadata
;
847 ASSERT_RCU_READ_LOCKED();
849 DBG("UST consumer setup metadata key %" PRIu64
, key
);
851 metadata
= consumer_find_channel(key
);
853 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
854 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
859 * In no monitor mode, the metadata channel has no stream(s) so skip the
860 * ownership transfer to the metadata thread.
862 if (!metadata
->monitor
) {
863 DBG("Metadata channel in no monitor");
869 * Send metadata stream to relayd if one available. Availability is
870 * known if the stream is still in the list of the channel.
872 if (cds_list_empty(&metadata
->streams
.head
)) {
873 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
874 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
875 goto error_no_stream
;
878 /* Send metadata stream to relayd if needed. */
879 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
880 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
883 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
886 ret
= consumer_send_relayd_streams_sent(
887 metadata
->metadata_stream
->net_seq_idx
);
889 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
895 * Ownership of metadata stream is passed along. Freeing is handled by
898 ret
= send_streams_to_thread(metadata
, ctx
);
901 * If we are unable to send the stream to the thread, there is
902 * a big problem so just stop everything.
904 ret
= LTTCOMM_CONSUMERD_FATAL
;
905 goto send_streams_error
;
907 /* List MUST be empty after or else it could be reused. */
908 LTTNG_ASSERT(cds_list_empty(&metadata
->streams
.head
));
915 * Delete metadata channel on error. At this point, the metadata stream can
916 * NOT be monitored by the metadata thread thus having the guarantee that
917 * the stream is still in the local stream list of the channel. This call
918 * will make sure to clean that list.
920 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
921 metadata
->metadata_stream
= NULL
;
929 * Snapshot the whole metadata.
930 * RCU read-side lock must be held by the caller.
932 * Returns 0 on success, < 0 on error
934 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
935 uint64_t key
, char *path
, uint64_t relayd_id
,
936 struct lttng_consumer_local_data
*ctx
)
939 struct lttng_consumer_stream
*metadata_stream
;
943 ASSERT_RCU_READ_LOCKED();
945 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
950 LTTNG_ASSERT(!metadata_channel
->monitor
);
952 health_code_update();
955 * Ask the sessiond if we have new metadata waiting and update the
956 * consumer metadata cache.
958 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
963 health_code_update();
966 * The metadata stream is NOT created in no monitor mode when the channel
967 * is created on a sessiond ask channel command.
969 ret
= create_ust_streams(metadata_channel
, ctx
);
974 metadata_stream
= metadata_channel
->metadata_stream
;
975 LTTNG_ASSERT(metadata_stream
);
977 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
978 if (relayd_id
!= (uint64_t) -1ULL) {
979 metadata_stream
->net_seq_idx
= relayd_id
;
980 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
982 ret
= consumer_stream_create_output_files(metadata_stream
,
990 health_code_update();
991 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
998 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
1000 * Clean up the stream completely because the next snapshot will use a
1001 * new metadata stream.
1003 consumer_stream_destroy(metadata_stream
, NULL
);
1004 metadata_channel
->metadata_stream
= NULL
;
1012 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1016 unsigned long mmap_offset
;
1017 const char *mmap_base
;
1019 mmap_base
= (const char *) lttng_ust_ctl_get_mmap_base(stream
->ustream
);
1021 ERR("Failed to get mmap base for stream `%s`",
1027 ret
= lttng_ust_ctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1029 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1034 *addr
= mmap_base
+ mmap_offset
;
1041 * Take a snapshot of all the stream of a channel.
1042 * RCU read-side lock and the channel lock must be held by the caller.
1044 * Returns 0 on success, < 0 on error
1046 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1047 uint64_t key
, char *path
, uint64_t relayd_id
,
1048 uint64_t nb_packets_per_stream
,
1049 struct lttng_consumer_local_data
*ctx
)
1052 unsigned use_relayd
= 0;
1053 unsigned long consumed_pos
, produced_pos
;
1054 struct lttng_consumer_stream
*stream
;
1058 ASSERT_RCU_READ_LOCKED();
1062 if (relayd_id
!= (uint64_t) -1ULL) {
1066 LTTNG_ASSERT(!channel
->monitor
);
1067 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1069 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1070 health_code_update();
1072 /* Lock stream because we are about to change its state. */
1073 pthread_mutex_lock(&stream
->lock
);
1074 LTTNG_ASSERT(channel
->trace_chunk
);
1075 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1077 * Can't happen barring an internal error as the channel
1078 * holds a reference to the trace chunk.
1080 ERR("Failed to acquire reference to channel's trace chunk");
1084 LTTNG_ASSERT(!stream
->trace_chunk
);
1085 stream
->trace_chunk
= channel
->trace_chunk
;
1087 stream
->net_seq_idx
= relayd_id
;
1090 ret
= consumer_send_relayd_stream(stream
, path
);
1092 goto error_close_stream
;
1095 ret
= consumer_stream_create_output_files(stream
,
1098 goto error_close_stream
;
1100 DBG("UST consumer snapshot stream (%" PRIu64
")",
1105 * If tracing is active, we want to perform a "full" buffer flush.
1106 * Else, if quiescent, it has already been done by the prior stop.
1108 if (!stream
->quiescent
) {
1109 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
1111 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
", channel name = '%s'",
1112 channel
->key
, channel
->name
);
1117 ret
= lttng_ustconsumer_take_snapshot(stream
);
1119 ERR("Taking UST snapshot");
1120 goto error_close_stream
;
1123 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1125 ERR("Produced UST snapshot position");
1126 goto error_close_stream
;
1129 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1131 ERR("Consumerd UST snapshot position");
1132 goto error_close_stream
;
1136 * The original value is sent back if max stream size is larger than
1137 * the possible size of the snapshot. Also, we assume that the session
1138 * daemon should never send a maximum stream size that is lower than
1141 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1142 produced_pos
, nb_packets_per_stream
,
1143 stream
->max_sb_size
);
1145 while ((long) (consumed_pos
- produced_pos
) < 0) {
1147 unsigned long len
, padded_len
;
1148 const char *subbuf_addr
;
1149 struct lttng_buffer_view subbuf_view
;
1151 health_code_update();
1153 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1155 ret
= lttng_ust_ctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1157 if (ret
!= -EAGAIN
) {
1158 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1159 goto error_close_stream
;
1161 DBG("UST consumer get subbuf failed. Skipping it.");
1162 consumed_pos
+= stream
->max_sb_size
;
1163 stream
->chan
->lost_packets
++;
1167 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &len
);
1169 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1170 goto error_put_subbuf
;
1173 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1175 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1176 goto error_put_subbuf
;
1179 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1181 goto error_put_subbuf
;
1184 subbuf_view
= lttng_buffer_view_init(
1185 subbuf_addr
, 0, padded_len
);
1186 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1187 stream
, &subbuf_view
, padded_len
- len
);
1189 if (read_len
!= len
) {
1191 goto error_put_subbuf
;
1194 if (read_len
!= padded_len
) {
1196 goto error_put_subbuf
;
1200 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
1202 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1203 goto error_close_stream
;
1205 consumed_pos
+= stream
->max_sb_size
;
1208 /* Simply close the stream so we can use it on the next snapshot. */
1209 consumer_stream_close_output(stream
);
1210 pthread_mutex_unlock(&stream
->lock
);
1217 if (lttng_ust_ctl_put_subbuf(stream
->ustream
) < 0) {
1218 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1221 consumer_stream_close_output(stream
);
1223 pthread_mutex_unlock(&stream
->lock
);
1229 void metadata_stream_reset_cache_consumed_position(
1230 struct lttng_consumer_stream
*stream
)
1232 ASSERT_LOCKED(stream
->lock
);
1234 DBG("Reset metadata cache of session %" PRIu64
,
1235 stream
->chan
->session_id
);
1236 stream
->ust_metadata_pushed
= 0;
1240 * Receive the metadata updates from the sessiond. Supports receiving
1241 * overlapping metadata, but is needs to always belong to a contiguous
1242 * range starting from 0.
1243 * Be careful about the locks held when calling this function: it needs
1244 * the metadata cache flush to concurrently progress in order to
1247 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1248 uint64_t len
, uint64_t version
,
1249 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1251 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1253 enum consumer_metadata_cache_write_status cache_write_status
;
1255 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1257 metadata_str
= calloc
<char>(len
);
1258 if (!metadata_str
) {
1259 PERROR("zmalloc metadata string");
1260 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1264 health_code_update();
1266 /* Receive metadata string. */
1267 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1269 /* Session daemon is dead so return gracefully. */
1274 health_code_update();
1276 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1277 cache_write_status
= consumer_metadata_cache_write(
1278 channel
->metadata_cache
, offset
, len
, version
,
1280 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1281 switch (cache_write_status
) {
1282 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1284 * The write entirely overlapped with existing contents of the
1285 * same metadata version (same content); there is nothing to do.
1288 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1290 * The metadata cache was invalidated (previously pushed
1291 * content has been overwritten). Reset the stream's consumed
1292 * metadata position to ensure the metadata poll thread consumes
1297 * channel::metadata_stream can be null when the metadata
1298 * channel is under a snapshot session type. No need to update
1299 * the stream position in that scenario.
1301 if (channel
->metadata_stream
!= NULL
) {
1302 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1303 metadata_stream_reset_cache_consumed_position(
1304 channel
->metadata_stream
);
1305 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1307 /* Validate we are in snapshot mode. */
1308 LTTNG_ASSERT(!channel
->monitor
);
1311 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1313 * In both cases, the metadata poll thread has new data to
1316 ret
= consumer_metadata_wakeup_pipe(channel
);
1318 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1322 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1323 /* Unable to handle metadata. Notify session daemon. */
1324 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1326 * Skip metadata flush on write error since the offset and len might
1327 * not have been updated which could create an infinite loop below when
1328 * waiting for the metadata cache to be flushed.
1338 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1339 DBG("Waiting for metadata to be flushed");
1341 health_code_update();
1343 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1353 * Receive command from session daemon and process it.
1355 * Return 1 on success else a negative value or 0.
1357 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1358 int sock
, struct pollfd
*consumer_sockpoll
)
1361 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1362 struct lttcomm_consumer_msg msg
;
1363 struct lttng_consumer_channel
*channel
= NULL
;
1365 health_code_update();
1370 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1371 if (ret_recv
!= sizeof(msg
)) {
1372 DBG("Consumer received unexpected message size %zd (expects %zu)",
1373 ret_recv
, sizeof(msg
));
1375 * The ret value might 0 meaning an orderly shutdown but this is ok
1376 * since the caller handles this.
1379 lttng_consumer_send_error(ctx
,
1380 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1387 health_code_update();
1390 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1392 health_code_update();
1394 /* relayd needs RCU read-side lock */
1397 switch (msg
.cmd_type
) {
1398 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1400 uint32_t major
= msg
.u
.relayd_sock
.major
;
1401 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
1402 enum lttcomm_sock_proto protocol
=
1403 (enum lttcomm_sock_proto
) msg
.u
.relayd_sock
1404 .relayd_socket_protocol
;
1406 /* Session daemon status message are handled in the following call. */
1407 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1408 msg
.u
.relayd_sock
.type
, ctx
, sock
,
1409 consumer_sockpoll
, msg
.u
.relayd_sock
.session_id
,
1410 msg
.u
.relayd_sock
.relayd_session_id
, major
,
1414 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1416 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1417 struct consumer_relayd_sock_pair
*relayd
;
1419 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1421 /* Get relayd reference if exists. */
1422 relayd
= consumer_find_relayd(index
);
1423 if (relayd
== NULL
) {
1424 DBG("Unable to find relayd %" PRIu64
, index
);
1425 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1429 * Each relayd socket pair has a refcount of stream attached to it
1430 * which tells if the relayd is still active or not depending on the
1433 * This will set the destroy flag of the relayd object and destroy it
1434 * if the refcount reaches zero when called.
1436 * The destroy can happen either here or when a stream fd hangs up.
1439 consumer_flag_relayd_for_destroy(relayd
);
1442 goto end_msg_sessiond
;
1444 case LTTNG_CONSUMER_UPDATE_STREAM
:
1449 case LTTNG_CONSUMER_DATA_PENDING
:
1451 int is_data_pending
;
1453 uint64_t id
= msg
.u
.data_pending
.session_id
;
1455 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1457 is_data_pending
= consumer_data_pending(id
);
1459 /* Send back returned value to session daemon */
1460 ret_send
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1461 sizeof(is_data_pending
));
1463 DBG("Error when sending the data pending ret code: %zd",
1469 * No need to send back a status message since the data pending
1470 * returned value is the response.
1474 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1476 int ret_ask_channel
, ret_add_channel
, ret_send
;
1477 struct lttng_ust_ctl_consumer_channel_attr attr
;
1478 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1479 const struct lttng_credentials buffer_credentials
= {
1480 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1481 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1484 /* Create a plain object and reserve a channel key. */
1485 channel
= consumer_allocate_channel(
1486 msg
.u
.ask_channel
.key
,
1487 msg
.u
.ask_channel
.session_id
,
1488 msg
.u
.ask_channel
.chunk_id
.is_set
?
1490 msg
.u
.ask_channel
.pathname
,
1491 msg
.u
.ask_channel
.name
,
1492 msg
.u
.ask_channel
.relayd_id
,
1493 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1494 msg
.u
.ask_channel
.tracefile_size
,
1495 msg
.u
.ask_channel
.tracefile_count
,
1496 msg
.u
.ask_channel
.session_id_per_pid
,
1497 msg
.u
.ask_channel
.monitor
,
1498 msg
.u
.ask_channel
.live_timer_interval
,
1499 msg
.u
.ask_channel
.is_live
,
1500 msg
.u
.ask_channel
.root_shm_path
,
1501 msg
.u
.ask_channel
.shm_path
);
1503 goto end_channel_error
;
1506 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1507 buffer_credentials
);
1510 * Assign UST application UID to the channel. This value is ignored for
1511 * per PID buffers. This is specific to UST thus setting this after the
1514 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1516 /* Build channel attributes from received message. */
1517 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1518 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1519 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1520 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1521 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1522 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1523 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1524 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1526 /* Match channel buffer type to the UST abi. */
1527 switch (msg
.u
.ask_channel
.output
) {
1528 case LTTNG_EVENT_MMAP
:
1530 attr
.output
= LTTNG_UST_ABI_MMAP
;
1534 /* Translate and save channel type. */
1535 switch (msg
.u
.ask_channel
.type
) {
1536 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1537 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1538 attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1540 * Set refcount to 1 for owner. Below, we will
1541 * pass ownership to the
1542 * consumer_thread_channel_poll() thread.
1544 channel
->refcount
= 1;
1546 case LTTNG_UST_ABI_CHAN_METADATA
:
1547 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1548 attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1555 health_code_update();
1557 ret_ask_channel
= ask_channel(ctx
, channel
, &attr
);
1558 if (ret_ask_channel
< 0) {
1559 goto end_channel_error
;
1562 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1565 ret_allocate
= consumer_metadata_cache_allocate(
1567 if (ret_allocate
< 0) {
1568 ERR("Allocating metadata cache");
1569 goto end_channel_error
;
1571 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1572 attr
.switch_timer_interval
= 0;
1574 int monitor_start_ret
;
1576 consumer_timer_live_start(channel
,
1577 msg
.u
.ask_channel
.live_timer_interval
);
1578 monitor_start_ret
= consumer_timer_monitor_start(
1580 msg
.u
.ask_channel
.monitor_timer_interval
);
1581 if (monitor_start_ret
< 0) {
1582 ERR("Starting channel monitoring timer failed");
1583 goto end_channel_error
;
1587 health_code_update();
1590 * Add the channel to the internal state AFTER all streams were created
1591 * and successfully sent to session daemon. This way, all streams must
1592 * be ready before this channel is visible to the threads.
1593 * If add_channel succeeds, ownership of the channel is
1594 * passed to consumer_thread_channel_poll().
1596 ret_add_channel
= add_channel(channel
, ctx
);
1597 if (ret_add_channel
< 0) {
1598 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1599 if (channel
->switch_timer_enabled
== 1) {
1600 consumer_timer_switch_stop(channel
);
1602 consumer_metadata_cache_destroy(channel
);
1604 if (channel
->live_timer_enabled
== 1) {
1605 consumer_timer_live_stop(channel
);
1607 if (channel
->monitor_timer_enabled
== 1) {
1608 consumer_timer_monitor_stop(channel
);
1610 goto end_channel_error
;
1613 health_code_update();
1616 * Channel and streams are now created. Inform the session daemon that
1617 * everything went well and should wait to receive the channel and
1618 * streams with ustctl API.
1620 ret_send
= consumer_send_status_channel(sock
, channel
);
1623 * There is probably a problem on the socket.
1630 case LTTNG_CONSUMER_GET_CHANNEL
:
1632 int ret
, relayd_err
= 0;
1633 uint64_t key
= msg
.u
.get_channel
.key
;
1634 struct lttng_consumer_channel
*found_channel
;
1636 found_channel
= consumer_find_channel(key
);
1637 if (!found_channel
) {
1638 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1639 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1640 goto end_get_channel
;
1643 health_code_update();
1645 /* Send the channel to sessiond (and relayd, if applicable). */
1646 ret
= send_channel_to_sessiond_and_relayd(
1647 sock
, found_channel
, ctx
, &relayd_err
);
1651 * We were unable to send to the relayd the stream so avoid
1652 * sending back a fatal error to the thread since this is OK
1653 * and the consumer can continue its work. The above call
1654 * has sent the error status message to the sessiond.
1656 goto end_get_channel_nosignal
;
1659 * The communicaton was broken hence there is a bad state between
1660 * the consumer and sessiond so stop everything.
1662 goto error_get_channel_fatal
;
1665 health_code_update();
1668 * In no monitor mode, the streams ownership is kept inside the channel
1669 * so don't send them to the data thread.
1671 if (!found_channel
->monitor
) {
1672 goto end_get_channel
;
1675 ret
= send_streams_to_thread(found_channel
, ctx
);
1678 * If we are unable to send the stream to the thread, there is
1679 * a big problem so just stop everything.
1681 goto error_get_channel_fatal
;
1683 /* List MUST be empty after or else it could be reused. */
1684 LTTNG_ASSERT(cds_list_empty(&found_channel
->streams
.head
));
1686 goto end_msg_sessiond
;
1687 error_get_channel_fatal
:
1689 end_get_channel_nosignal
:
1692 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1694 uint64_t key
= msg
.u
.destroy_channel
.key
;
1697 * Only called if streams have not been sent to stream
1698 * manager thread. However, channel has been sent to
1699 * channel manager thread.
1701 notify_thread_del_channel(ctx
, key
);
1702 goto end_msg_sessiond
;
1704 case LTTNG_CONSUMER_CLOSE_METADATA
:
1708 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1710 ret_code
= (lttcomm_return_code
) ret
;
1713 goto end_msg_sessiond
;
1715 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1719 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1721 ret_code
= (lttcomm_return_code
) ret
;
1724 goto end_msg_sessiond
;
1726 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1730 ret
= clear_quiescent_channel(
1731 msg
.u
.clear_quiescent_channel
.key
);
1733 ret_code
= (lttcomm_return_code
) ret
;
1736 goto end_msg_sessiond
;
1738 case LTTNG_CONSUMER_PUSH_METADATA
:
1741 uint64_t len
= msg
.u
.push_metadata
.len
;
1742 uint64_t key
= msg
.u
.push_metadata
.key
;
1743 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1744 uint64_t version
= msg
.u
.push_metadata
.version
;
1745 struct lttng_consumer_channel
*found_channel
;
1747 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1750 found_channel
= consumer_find_channel(key
);
1751 if (!found_channel
) {
1753 * This is possible if the metadata creation on the consumer side
1754 * is in flight vis-a-vis a concurrent push metadata from the
1755 * session daemon. Simply return that the channel failed and the
1756 * session daemon will handle that message correctly considering
1757 * that this race is acceptable thus the DBG() statement here.
1759 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1760 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1761 goto end_push_metadata_msg_sessiond
;
1764 health_code_update();
1768 * There is nothing to receive. We have simply
1769 * checked whether the channel can be found.
1771 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1772 goto end_push_metadata_msg_sessiond
;
1775 /* Tell session daemon we are ready to receive the metadata. */
1776 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1778 /* Somehow, the session daemon is not responding anymore. */
1779 goto error_push_metadata_fatal
;
1782 health_code_update();
1784 /* Wait for more data. */
1785 health_poll_entry();
1786 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1789 goto error_push_metadata_fatal
;
1792 health_code_update();
1794 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
, len
,
1795 version
, found_channel
, 0, 1);
1797 /* error receiving from sessiond */
1798 goto error_push_metadata_fatal
;
1800 ret_code
= (lttcomm_return_code
) ret
;
1801 goto end_push_metadata_msg_sessiond
;
1803 end_push_metadata_msg_sessiond
:
1804 goto end_msg_sessiond
;
1805 error_push_metadata_fatal
:
1808 case LTTNG_CONSUMER_SETUP_METADATA
:
1812 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1814 ret_code
= (lttcomm_return_code
) ret
;
1816 goto end_msg_sessiond
;
1818 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1820 struct lttng_consumer_channel
*found_channel
;
1821 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1824 found_channel
= consumer_find_channel(key
);
1825 if (!found_channel
) {
1826 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1827 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1829 if (msg
.u
.snapshot_channel
.metadata
) {
1832 ret_snapshot
= snapshot_metadata(found_channel
,
1834 msg
.u
.snapshot_channel
.pathname
,
1835 msg
.u
.snapshot_channel
.relayd_id
,
1837 if (ret_snapshot
< 0) {
1838 ERR("Snapshot metadata failed");
1839 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1844 ret_snapshot
= snapshot_channel(found_channel
,
1846 msg
.u
.snapshot_channel
.pathname
,
1847 msg
.u
.snapshot_channel
.relayd_id
,
1848 msg
.u
.snapshot_channel
1849 .nb_packets_per_stream
,
1851 if (ret_snapshot
< 0) {
1852 ERR("Snapshot channel failed");
1853 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1857 health_code_update();
1858 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1860 /* Somehow, the session daemon is not responding anymore. */
1863 health_code_update();
1866 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1869 uint64_t discarded_events
;
1870 struct lttng_ht_iter iter
;
1871 struct lttng_ht
*ht
;
1872 struct lttng_consumer_stream
*stream
;
1873 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1874 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1876 DBG("UST consumer discarded events command for session id %"
1879 pthread_mutex_lock(&the_consumer_data
.lock
);
1881 ht
= the_consumer_data
.stream_list_ht
;
1884 * We only need a reference to the channel, but they are not
1885 * directly indexed, so we just use the first matching stream
1886 * to extract the information we need, we default to 0 if not
1887 * found (no events are dropped if the channel is not yet in
1890 discarded_events
= 0;
1891 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1892 ht
->hash_fct(&id
, lttng_ht_seed
),
1894 &iter
.iter
, stream
, node_session_id
.node
) {
1895 if (stream
->chan
->key
== key
) {
1896 discarded_events
= stream
->chan
->discarded_events
;
1900 pthread_mutex_unlock(&the_consumer_data
.lock
);
1903 DBG("UST consumer discarded events command for session id %"
1904 PRIu64
", channel key %" PRIu64
, id
, key
);
1906 health_code_update();
1908 /* Send back returned value to session daemon */
1909 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1911 PERROR("send discarded events");
1917 case LTTNG_CONSUMER_LOST_PACKETS
:
1920 uint64_t lost_packets
;
1921 struct lttng_ht_iter iter
;
1922 struct lttng_ht
*ht
;
1923 struct lttng_consumer_stream
*stream
;
1924 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1925 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1927 DBG("UST consumer lost packets command for session id %"
1930 pthread_mutex_lock(&the_consumer_data
.lock
);
1932 ht
= the_consumer_data
.stream_list_ht
;
1935 * We only need a reference to the channel, but they are not
1936 * directly indexed, so we just use the first matching stream
1937 * to extract the information we need, we default to 0 if not
1938 * found (no packets lost if the channel is not yet in use).
1941 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1942 ht
->hash_fct(&id
, lttng_ht_seed
),
1944 &iter
.iter
, stream
, node_session_id
.node
) {
1945 if (stream
->chan
->key
== key
) {
1946 lost_packets
= stream
->chan
->lost_packets
;
1950 pthread_mutex_unlock(&the_consumer_data
.lock
);
1953 DBG("UST consumer lost packets command for session id %"
1954 PRIu64
", channel key %" PRIu64
, id
, key
);
1956 health_code_update();
1958 /* Send back returned value to session daemon */
1959 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1960 sizeof(lost_packets
));
1962 PERROR("send lost packets");
1968 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1970 int channel_monitor_pipe
, ret_send
,
1971 ret_set_channel_monitor_pipe
;
1974 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1975 /* Successfully received the command's type. */
1976 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1981 ret_recv
= lttcomm_recv_fds_unix_sock(
1982 sock
, &channel_monitor_pipe
, 1);
1983 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1984 ERR("Failed to receive channel monitor pipe");
1988 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1989 ret_set_channel_monitor_pipe
=
1990 consumer_timer_thread_set_channel_monitor_pipe(
1991 channel_monitor_pipe
);
1992 if (!ret_set_channel_monitor_pipe
) {
1996 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1997 /* Set the pipe as non-blocking. */
1998 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1999 if (ret_fcntl
== -1) {
2000 PERROR("fcntl get flags of the channel monitoring pipe");
2005 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
2006 flags
| O_NONBLOCK
);
2007 if (ret_fcntl
== -1) {
2008 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2011 DBG("Channel monitor pipe set as non-blocking");
2013 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2015 goto end_msg_sessiond
;
2017 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2019 struct lttng_consumer_channel
*found_channel
;
2020 uint64_t key
= msg
.u
.rotate_channel
.key
;
2021 int ret_send_status
;
2023 found_channel
= consumer_find_channel(key
);
2024 if (!found_channel
) {
2025 DBG("Channel %" PRIu64
" not found", key
);
2026 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2031 * Sample the rotate position of all the streams in
2034 rotate_channel
= lttng_consumer_rotate_channel(
2036 msg
.u
.rotate_channel
.relayd_id
);
2037 if (rotate_channel
< 0) {
2038 ERR("Rotate channel failed");
2039 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2042 health_code_update();
2045 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2046 if (ret_send_status
< 0) {
2047 /* Somehow, the session daemon is not responding anymore. */
2048 goto end_rotate_channel_nosignal
;
2052 * Rotate the streams that are ready right now.
2053 * FIXME: this is a second consecutive iteration over the
2054 * streams in a channel, there is probably a better way to
2055 * handle this, but it needs to be after the
2056 * consumer_send_status_msg() call.
2058 if (found_channel
) {
2059 int ret_rotate_read_streams
;
2061 ret_rotate_read_streams
=
2062 lttng_consumer_rotate_ready_streams(
2063 found_channel
, key
);
2064 if (ret_rotate_read_streams
< 0) {
2065 ERR("Rotate channel failed");
2069 end_rotate_channel_nosignal
:
2072 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2074 struct lttng_consumer_channel
*found_channel
;
2075 uint64_t key
= msg
.u
.clear_channel
.key
;
2076 int ret_send_status
;
2078 found_channel
= consumer_find_channel(key
);
2079 if (!found_channel
) {
2080 DBG("Channel %" PRIu64
" not found", key
);
2081 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2083 int ret_clear_channel
;
2085 ret_clear_channel
= lttng_consumer_clear_channel(
2087 if (ret_clear_channel
) {
2088 ERR("Clear channel failed key %" PRIu64
, key
);
2089 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
2092 health_code_update();
2094 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2095 if (ret_send_status
< 0) {
2096 /* Somehow, the session daemon is not responding anymore. */
2101 case LTTNG_CONSUMER_INIT
:
2103 int ret_send_status
;
2104 lttng_uuid sessiond_uuid
;
2106 std::copy(std::begin(msg
.u
.init
.sessiond_uuid
), std::end(msg
.u
.init
.sessiond_uuid
),
2107 sessiond_uuid
.begin());
2108 ret_code
= lttng_consumer_init_command(ctx
, sessiond_uuid
);
2109 health_code_update();
2110 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2111 if (ret_send_status
< 0) {
2112 /* Somehow, the session daemon is not responding anymore. */
2117 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2119 const struct lttng_credentials credentials
= {
2120 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2121 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2123 const bool is_local_trace
=
2124 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2125 const uint64_t relayd_id
=
2126 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2127 const char *chunk_override_name
=
2128 *msg
.u
.create_trace_chunk
.override_name
?
2129 msg
.u
.create_trace_chunk
.override_name
:
2131 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2134 * The session daemon will only provide a chunk directory file
2135 * descriptor for local traces.
2137 if (is_local_trace
) {
2139 int ret_send_status
;
2142 /* Acnowledge the reception of the command. */
2143 ret_send_status
= consumer_send_status_msg(
2144 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
2145 if (ret_send_status
< 0) {
2146 /* Somehow, the session daemon is not responding anymore. */
2151 * Receive trace chunk domain dirfd.
2153 ret_recv
= lttcomm_recv_fds_unix_sock(
2154 sock
, &chunk_dirfd
, 1);
2155 if (ret_recv
!= sizeof(chunk_dirfd
)) {
2156 ERR("Failed to receive trace chunk domain directory file descriptor");
2160 DBG("Received trace chunk domain directory fd (%d)",
2162 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2164 if (!chunk_directory_handle
) {
2165 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2166 if (close(chunk_dirfd
)) {
2167 PERROR("Failed to close chunk directory file descriptor");
2173 ret_code
= lttng_consumer_create_trace_chunk(
2174 !is_local_trace
? &relayd_id
: NULL
,
2175 msg
.u
.create_trace_chunk
.session_id
,
2176 msg
.u
.create_trace_chunk
.chunk_id
,
2177 (time_t) msg
.u
.create_trace_chunk
2178 .creation_timestamp
,
2179 chunk_override_name
,
2180 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2183 chunk_directory_handle
);
2184 lttng_directory_handle_put(chunk_directory_handle
);
2185 goto end_msg_sessiond
;
2187 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2189 enum lttng_trace_chunk_command_type close_command
=
2190 (lttng_trace_chunk_command_type
)
2191 msg
.u
.close_trace_chunk
.close_command
.value
;
2192 const uint64_t relayd_id
=
2193 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2194 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2195 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2198 ret_code
= lttng_consumer_close_trace_chunk(
2199 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2202 msg
.u
.close_trace_chunk
.session_id
,
2203 msg
.u
.close_trace_chunk
.chunk_id
,
2204 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2205 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2207 NULL
, closed_trace_chunk_path
);
2208 reply
.ret_code
= ret_code
;
2209 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2210 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2211 if (ret
!= sizeof(reply
)) {
2214 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2216 if (ret
!= reply
.path_length
) {
2221 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2223 const uint64_t relayd_id
=
2224 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2226 ret_code
= lttng_consumer_trace_chunk_exists(
2227 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2229 msg
.u
.trace_chunk_exists
.session_id
,
2230 msg
.u
.trace_chunk_exists
.chunk_id
);
2231 goto end_msg_sessiond
;
2233 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2235 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2236 struct lttng_consumer_channel
*found_channel
=
2237 consumer_find_channel(key
);
2239 if (found_channel
) {
2240 pthread_mutex_lock(&found_channel
->lock
);
2241 ret_code
= lttng_consumer_open_channel_packets(
2243 pthread_mutex_unlock(&found_channel
->lock
);
2246 * The channel could have disappeared in per-pid
2249 DBG("Channel %" PRIu64
" not found", key
);
2250 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2253 health_code_update();
2254 goto end_msg_sessiond
;
2262 * Return 1 to indicate success since the 0 value can be a socket
2263 * shutdown during the recv() or send() call.
2270 * The returned value here is not useful since either way we'll return 1 to
2271 * the caller because the session daemon socket management is done
2272 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2275 int ret_send_status
;
2277 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2278 if (ret_send_status
< 0) {
2288 consumer_del_channel(channel
);
2290 /* We have to send a status channel message indicating an error. */
2292 int ret_send_status
;
2294 ret_send_status
= consumer_send_status_channel(sock
, NULL
);
2295 if (ret_send_status
< 0) {
2296 /* Stop everything if session daemon can not be notified. */
2305 /* This will issue a consumer stop. */
2311 health_code_update();
2315 int lttng_ust_flush_buffer(struct lttng_consumer_stream
*stream
,
2316 int producer_active
)
2318 LTTNG_ASSERT(stream
);
2319 LTTNG_ASSERT(stream
->ustream
);
2321 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer_active
);
2325 * Take a snapshot for a specific stream.
2327 * Returns 0 on success, < 0 on error
2329 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2331 LTTNG_ASSERT(stream
);
2332 LTTNG_ASSERT(stream
->ustream
);
2334 return lttng_ust_ctl_snapshot(stream
->ustream
);
2338 * Sample consumed and produced positions for a specific stream.
2340 * Returns 0 on success, < 0 on error.
2342 int lttng_ustconsumer_sample_snapshot_positions(
2343 struct lttng_consumer_stream
*stream
)
2345 LTTNG_ASSERT(stream
);
2346 LTTNG_ASSERT(stream
->ustream
);
2348 return lttng_ust_ctl_snapshot_sample_positions(stream
->ustream
);
2352 * Get the produced position
2354 * Returns 0 on success, < 0 on error
2356 int lttng_ustconsumer_get_produced_snapshot(
2357 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2359 LTTNG_ASSERT(stream
);
2360 LTTNG_ASSERT(stream
->ustream
);
2363 return lttng_ust_ctl_snapshot_get_produced(stream
->ustream
, pos
);
2367 * Get the consumed position
2369 * Returns 0 on success, < 0 on error
2371 int lttng_ustconsumer_get_consumed_snapshot(
2372 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2374 LTTNG_ASSERT(stream
);
2375 LTTNG_ASSERT(stream
->ustream
);
2378 return lttng_ust_ctl_snapshot_get_consumed(stream
->ustream
, pos
);
2381 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2384 LTTNG_ASSERT(stream
);
2385 LTTNG_ASSERT(stream
->ustream
);
2387 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer
);
2390 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2392 LTTNG_ASSERT(stream
);
2393 LTTNG_ASSERT(stream
->ustream
);
2395 return lttng_ust_ctl_clear_buffer(stream
->ustream
);
2398 int lttng_ustconsumer_get_current_timestamp(
2399 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2401 LTTNG_ASSERT(stream
);
2402 LTTNG_ASSERT(stream
->ustream
);
2405 return lttng_ust_ctl_get_current_timestamp(stream
->ustream
, ts
);
2408 int lttng_ustconsumer_get_sequence_number(
2409 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2411 LTTNG_ASSERT(stream
);
2412 LTTNG_ASSERT(stream
->ustream
);
2415 return lttng_ust_ctl_get_sequence_number(stream
->ustream
, seq
);
2419 * Called when the stream signals the consumer that it has hung up.
2421 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2423 LTTNG_ASSERT(stream
);
2424 LTTNG_ASSERT(stream
->ustream
);
2426 pthread_mutex_lock(&stream
->lock
);
2427 if (!stream
->quiescent
) {
2428 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 0) < 0) {
2429 ERR("Failed to flush buffer on stream hang-up");
2431 stream
->quiescent
= true;
2435 stream
->hangup_flush_done
= 1;
2436 pthread_mutex_unlock(&stream
->lock
);
2439 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2444 LTTNG_ASSERT(chan
->uchan
);
2445 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2447 if (chan
->switch_timer_enabled
== 1) {
2448 consumer_timer_switch_stop(chan
);
2450 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2453 ret
= close(chan
->stream_fds
[i
]);
2457 if (chan
->shm_path
[0]) {
2458 char shm_path
[PATH_MAX
];
2460 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2462 ERR("Cannot get stream shm path");
2464 ret
= run_as_unlink(shm_path
,
2465 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2466 chan
->buffer_credentials
)),
2467 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2468 chan
->buffer_credentials
)));
2470 PERROR("unlink %s", shm_path
);
2476 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2479 LTTNG_ASSERT(chan
->uchan
);
2480 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2482 consumer_metadata_cache_destroy(chan
);
2483 lttng_ust_ctl_destroy_channel(chan
->uchan
);
2484 /* Try to rmdir all directories under shm_path root. */
2485 if (chan
->root_shm_path
[0]) {
2486 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2487 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2488 chan
->buffer_credentials
)),
2489 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2490 chan
->buffer_credentials
)),
2491 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2493 free(chan
->stream_fds
);
2496 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2498 LTTNG_ASSERT(stream
);
2499 LTTNG_ASSERT(stream
->ustream
);
2501 if (stream
->chan
->switch_timer_enabled
== 1) {
2502 consumer_timer_switch_stop(stream
->chan
);
2504 lttng_ust_ctl_destroy_stream(stream
->ustream
);
2507 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2509 LTTNG_ASSERT(stream
);
2510 LTTNG_ASSERT(stream
->ustream
);
2512 return lttng_ust_ctl_stream_get_wakeup_fd(stream
->ustream
);
2515 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2517 LTTNG_ASSERT(stream
);
2518 LTTNG_ASSERT(stream
->ustream
);
2520 return lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
2524 * Write up to one packet from the metadata cache to the channel.
2526 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2527 * negative value on error.
2530 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2535 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2536 if (stream
->chan
->metadata_cache
->contents
.size
==
2537 stream
->ust_metadata_pushed
) {
2539 * In the context of a user space metadata channel, a
2540 * change in version can be detected in two ways:
2541 * 1) During the pre-consume of the `read_subbuffer` loop,
2542 * 2) When populating the metadata ring buffer (i.e. here).
2544 * This function is invoked when there is no metadata
2545 * available in the ring-buffer. If all data was consumed
2546 * up to the size of the metadata cache, there is no metadata
2547 * to insert in the ring-buffer.
2549 * However, the metadata version could still have changed (a
2550 * regeneration without any new data will yield the same cache
2553 * The cache's version is checked for a version change and the
2554 * consumed position is reset if one occurred.
2556 * This check is only necessary for the user space domain as
2557 * it has to manage the cache explicitly. If this reset was not
2558 * performed, no metadata would be consumed (and no reset would
2559 * occur as part of the pre-consume) until the metadata size
2560 * exceeded the cache size.
2562 if (stream
->metadata_version
!=
2563 stream
->chan
->metadata_cache
->version
) {
2564 metadata_stream_reset_cache_consumed_position(stream
);
2565 consumer_stream_metadata_set_version(stream
,
2566 stream
->chan
->metadata_cache
->version
);
2573 write_len
= lttng_ust_ctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2574 &stream
->chan
->metadata_cache
->contents
.data
[stream
->ust_metadata_pushed
],
2575 stream
->chan
->metadata_cache
->contents
.size
-
2576 stream
->ust_metadata_pushed
);
2577 LTTNG_ASSERT(write_len
!= 0);
2578 if (write_len
< 0) {
2579 ERR("Writing one metadata packet");
2583 stream
->ust_metadata_pushed
+= write_len
;
2585 LTTNG_ASSERT(stream
->chan
->metadata_cache
->contents
.size
>=
2586 stream
->ust_metadata_pushed
);
2590 * Switch packet (but don't open the next one) on every commit of
2591 * a metadata packet. Since the subbuffer is fully filled (with padding,
2592 * if needed), the stream is "quiescent" after this commit.
2594 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 1)) {
2595 ERR("Failed to flush buffer while committing one metadata packet");
2598 stream
->quiescent
= true;
2601 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2607 * Sync metadata meaning request them to the session daemon and snapshot to the
2608 * metadata thread can consumer them.
2610 * Metadata stream lock is held here, but we need to release it when
2611 * interacting with sessiond, else we cause a deadlock with live
2612 * awaiting on metadata to be pushed out.
2614 * The RCU read side lock must be held by the caller.
2616 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2617 struct lttng_consumer_local_data
*ctx
,
2618 struct lttng_consumer_stream
*metadata_stream
)
2621 enum sync_metadata_status status
;
2622 struct lttng_consumer_channel
*metadata_channel
;
2625 LTTNG_ASSERT(metadata_stream
);
2626 ASSERT_RCU_READ_LOCKED();
2628 metadata_channel
= metadata_stream
->chan
;
2629 pthread_mutex_unlock(&metadata_stream
->lock
);
2631 * Request metadata from the sessiond, but don't wait for the flush
2632 * because we locked the metadata thread.
2634 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2635 pthread_mutex_lock(&metadata_stream
->lock
);
2637 status
= SYNC_METADATA_STATUS_ERROR
;
2642 * The metadata stream and channel can be deleted while the
2643 * metadata stream lock was released. The streamed is checked
2644 * for deletion before we use it further.
2646 * Note that it is safe to access a logically-deleted stream since its
2647 * existence is still guaranteed by the RCU read side lock. However,
2648 * it should no longer be used. The close/deletion of the metadata
2649 * channel and stream already guarantees that all metadata has been
2650 * consumed. Therefore, there is nothing left to do in this function.
2652 if (consumer_stream_is_deleted(metadata_stream
)) {
2653 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2654 metadata_stream
->key
);
2655 status
= SYNC_METADATA_STATUS_NO_DATA
;
2659 ret
= commit_one_metadata_packet(metadata_stream
);
2661 status
= SYNC_METADATA_STATUS_ERROR
;
2663 } else if (ret
> 0) {
2664 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2665 } else /* ret == 0 */ {
2666 status
= SYNC_METADATA_STATUS_NO_DATA
;
2670 ret
= lttng_ust_ctl_snapshot(metadata_stream
->ustream
);
2672 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2673 status
= SYNC_METADATA_STATUS_ERROR
;
2682 * Return 0 on success else a negative value.
2684 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2685 struct lttng_consumer_local_data
*ctx
)
2688 struct lttng_ust_ctl_consumer_stream
*ustream
;
2690 LTTNG_ASSERT(stream
);
2693 ustream
= stream
->ustream
;
2696 * First, we are going to check if there is a new subbuffer available
2697 * before reading the stream wait_fd.
2699 /* Get the next subbuffer */
2700 ret
= lttng_ust_ctl_get_next_subbuf(ustream
);
2702 /* No more data found, flag the stream. */
2703 stream
->has_data
= 0;
2708 ret
= lttng_ust_ctl_put_subbuf(ustream
);
2711 /* This stream still has data. Flag it and wake up the data thread. */
2712 stream
->has_data
= 1;
2714 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2717 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2718 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2723 /* The wake up pipe has been notified. */
2724 ctx
->has_wakeup
= 1;
2732 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2737 * We can consume the 1 byte written into the wait_fd by
2738 * UST. Don't trigger error if we cannot read this one byte
2739 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2741 * This is only done when the stream is monitored by a thread,
2742 * before the flush is done after a hangup and if the stream
2743 * is not flagged with data since there might be nothing to
2744 * consume in the wait fd but still have data available
2745 * flagged by the consumer wake up pipe.
2747 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2751 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2752 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2760 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2761 struct stream_subbuffer
*subbuf
)
2765 ret
= lttng_ust_ctl_get_subbuf_size(
2766 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2771 ret
= lttng_ust_ctl_get_padded_subbuf_size(
2772 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2781 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2782 struct stream_subbuffer
*subbuf
)
2786 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2791 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2797 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2798 struct stream_subbuffer
*subbuf
)
2802 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2807 ret
= lttng_ust_ctl_get_packet_size(
2808 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2810 PERROR("Failed to get sub-buffer packet size");
2814 ret
= lttng_ust_ctl_get_content_size(
2815 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2817 PERROR("Failed to get sub-buffer content size");
2821 ret
= lttng_ust_ctl_get_timestamp_begin(
2822 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2824 PERROR("Failed to get sub-buffer begin timestamp");
2828 ret
= lttng_ust_ctl_get_timestamp_end(
2829 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2831 PERROR("Failed to get sub-buffer end timestamp");
2835 ret
= lttng_ust_ctl_get_events_discarded(
2836 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2838 PERROR("Failed to get sub-buffer events discarded count");
2842 ret
= lttng_ust_ctl_get_sequence_number(stream
->ustream
,
2843 &subbuf
->info
.data
.sequence_number
.value
);
2845 /* May not be supported by older LTTng-modules. */
2846 if (ret
!= -ENOTTY
) {
2847 PERROR("Failed to get sub-buffer sequence number");
2851 subbuf
->info
.data
.sequence_number
.is_set
= true;
2854 ret
= lttng_ust_ctl_get_stream_id(
2855 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2857 PERROR("Failed to get stream id");
2861 ret
= lttng_ust_ctl_get_instance_id(stream
->ustream
,
2862 &subbuf
->info
.data
.stream_instance_id
.value
);
2864 /* May not be supported by older LTTng-modules. */
2865 if (ret
!= -ENOTTY
) {
2866 PERROR("Failed to get stream instance id");
2870 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2876 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2877 struct stream_subbuffer
*subbuffer
)
2882 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2888 ret
= get_current_subbuf_addr(stream
, &addr
);
2893 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2894 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2895 LTTNG_ASSERT(subbuffer
->buffer
.buffer
.data
!= NULL
);
2900 static enum get_next_subbuffer_status
get_next_subbuffer(
2901 struct lttng_consumer_stream
*stream
,
2902 struct stream_subbuffer
*subbuffer
)
2905 enum get_next_subbuffer_status status
;
2907 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2910 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
2915 * The caller only expects -ENODATA when there is no data to
2916 * read, but the kernel tracer returns -EAGAIN when there is
2917 * currently no data for a non-finalized stream, and -ENODATA
2918 * when there is no data for a finalized stream. Those can be
2919 * combined into a -ENODATA return value.
2921 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2924 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2928 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2930 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2937 static enum get_next_subbuffer_status
get_next_subbuffer_metadata(
2938 struct lttng_consumer_stream
*stream
,
2939 struct stream_subbuffer
*subbuffer
)
2946 unsigned long consumed_pos
, produced_pos
;
2947 enum get_next_subbuffer_status status
;
2950 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2952 got_subbuffer
= true;
2954 got_subbuffer
= false;
2955 if (ret
!= -EAGAIN
) {
2957 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2963 * Determine if the cache is empty and ensure that a sub-buffer
2964 * is made available if the cache is not empty.
2966 if (!got_subbuffer
) {
2967 ret
= commit_one_metadata_packet(stream
);
2968 if (ret
< 0 && ret
!= -ENOBUFS
) {
2969 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2971 } else if (ret
== 0) {
2972 /* Not an error, the cache is empty. */
2974 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2977 cache_empty
= false;
2980 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2981 cache_empty
= stream
->chan
->metadata_cache
->contents
.size
==
2982 stream
->ust_metadata_pushed
;
2983 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2985 } while (!got_subbuffer
);
2987 /* Populate sub-buffer infos and view. */
2988 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2990 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2994 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2997 * -EAGAIN is not expected since we got a sub-buffer and haven't
2998 * pushed the consumption position yet (on put_next).
3000 PERROR("Failed to take a snapshot of metadata buffer positions");
3001 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3005 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
3007 PERROR("Failed to get metadata consumed position");
3008 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3012 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
3014 PERROR("Failed to get metadata produced position");
3015 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3019 /* Last sub-buffer of the ring buffer ? */
3020 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
3023 * The sessiond registry lock ensures that coherent units of metadata
3024 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3025 * acquired, the cache is empty, and it is the only available sub-buffer
3026 * available, it is safe to assume that it is "coherent".
3028 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
3030 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
3031 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
3036 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
3037 struct stream_subbuffer
*subbuffer
__attribute__((unused
)))
3039 const int ret
= lttng_ust_ctl_put_next_subbuf(stream
->ustream
);
3041 LTTNG_ASSERT(ret
== 0);
3045 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3046 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
3048 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3049 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3052 static int lttng_ustconsumer_set_stream_ops(
3053 struct lttng_consumer_stream
*stream
)
3057 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3058 if (stream
->metadata_flag
) {
3059 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3060 get_next_subbuffer_metadata
;
3061 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3062 extract_metadata_subbuffer_info
;
3063 stream
->read_subbuffer_ops
.reset_metadata
=
3064 metadata_stream_reset_cache_consumed_position
;
3065 if (stream
->chan
->is_live
) {
3066 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3067 ret
= consumer_stream_enable_metadata_bucketization(
3074 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3076 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3077 extract_data_subbuffer_info
;
3078 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3079 if (stream
->chan
->is_live
) {
3080 stream
->read_subbuffer_ops
.send_live_beacon
=
3081 consumer_flush_ust_index
;
3085 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3091 * Called when a stream is created.
3093 * Return 0 on success or else a negative value.
3095 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3099 LTTNG_ASSERT(stream
);
3102 * Don't create anything if this is set for streaming or if there is
3103 * no current trace chunk on the parent channel.
3105 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3106 stream
->chan
->trace_chunk
) {
3107 ret
= consumer_stream_create_output_files(stream
, true);
3113 lttng_ustconsumer_set_stream_ops(stream
);
3121 * Check if data is still being extracted from the buffers for a specific
3122 * stream. Consumer data lock MUST be acquired before calling this function
3123 * and the stream lock.
3125 * Return 1 if the traced data are still getting read else 0 meaning that the
3126 * data is available for trace viewer reading.
3128 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3132 LTTNG_ASSERT(stream
);
3133 LTTNG_ASSERT(stream
->ustream
);
3134 ASSERT_LOCKED(stream
->lock
);
3136 DBG("UST consumer checking data pending");
3138 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3143 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3144 uint64_t contiguous
, pushed
;
3146 /* Ease our life a bit. */
3147 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3148 contiguous
= stream
->chan
->metadata_cache
->contents
.size
;
3149 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3150 pushed
= stream
->ust_metadata_pushed
;
3153 * We can simply check whether all contiguously available data
3154 * has been pushed to the ring buffer, since the push operation
3155 * is performed within get_next_subbuf(), and because both
3156 * get_next_subbuf() and put_next_subbuf() are issued atomically
3157 * thanks to the stream lock within
3158 * lttng_ustconsumer_read_subbuffer(). This basically means that
3159 * whetnever ust_metadata_pushed is incremented, the associated
3160 * metadata has been consumed from the metadata stream.
3162 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3163 contiguous
, pushed
);
3164 LTTNG_ASSERT(((int64_t) (contiguous
- pushed
)) >= 0);
3165 if ((contiguous
!= pushed
) ||
3166 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3167 ret
= 1; /* Data is pending */
3171 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
3174 * There is still data so let's put back this
3177 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
3178 LTTNG_ASSERT(ret
== 0);
3179 ret
= 1; /* Data is pending */
3184 /* Data is NOT pending so ready to be read. */
3192 * Stop a given metadata channel timer if enabled and close the wait fd which
3193 * is the poll pipe of the metadata stream.
3195 * This MUST be called with the metadata channel lock acquired.
3197 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3201 LTTNG_ASSERT(metadata
);
3202 LTTNG_ASSERT(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3204 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3206 if (metadata
->switch_timer_enabled
== 1) {
3207 consumer_timer_switch_stop(metadata
);
3210 if (!metadata
->metadata_stream
) {
3215 * Closing write side so the thread monitoring the stream wakes up if any
3216 * and clean the metadata stream.
3218 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3219 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3221 PERROR("closing metadata pipe write side");
3223 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3231 * Close every metadata stream wait fd of the metadata hash table. This
3232 * function MUST be used very carefully so not to run into a race between the
3233 * metadata thread handling streams and this function closing their wait fd.
3235 * For UST, this is used when the session daemon hangs up. Its the metadata
3236 * producer so calling this is safe because we are assured that no state change
3237 * can occur in the metadata thread for the streams in the hash table.
3239 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3241 struct lttng_ht_iter iter
;
3242 struct lttng_consumer_stream
*stream
;
3244 LTTNG_ASSERT(metadata_ht
);
3245 LTTNG_ASSERT(metadata_ht
->ht
);
3247 DBG("UST consumer closing all metadata streams");
3250 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3253 health_code_update();
3255 pthread_mutex_lock(&stream
->chan
->lock
);
3256 lttng_ustconsumer_close_metadata(stream
->chan
);
3257 pthread_mutex_unlock(&stream
->chan
->lock
);
3263 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3267 ret
= lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
3269 ERR("Unable to close wakeup fd");
3274 * Please refer to consumer-timer.c before adding any lock within this
3275 * function or any of its callees. Timers have a very strict locking
3276 * semantic with respect to teardown. Failure to respect this semantic
3277 * introduces deadlocks.
3279 * DON'T hold the metadata lock when calling this function, else this
3280 * can cause deadlock involving consumer awaiting for metadata to be
3281 * pushed out due to concurrent interaction with the session daemon.
3283 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3284 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3286 struct lttcomm_metadata_request_msg request
;
3287 struct lttcomm_consumer_msg msg
;
3288 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3289 uint64_t len
, key
, offset
, version
;
3292 LTTNG_ASSERT(channel
);
3293 LTTNG_ASSERT(channel
->metadata_cache
);
3295 memset(&request
, 0, sizeof(request
));
3297 /* send the metadata request to sessiond */
3298 switch (the_consumer_data
.type
) {
3299 case LTTNG_CONSUMER64_UST
:
3300 request
.bits_per_long
= 64;
3302 case LTTNG_CONSUMER32_UST
:
3303 request
.bits_per_long
= 32;
3306 request
.bits_per_long
= 0;
3310 request
.session_id
= channel
->session_id
;
3311 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3313 * Request the application UID here so the metadata of that application can
3314 * be sent back. The channel UID corresponds to the user UID of the session
3315 * used for the rights on the stream file(s).
3317 request
.uid
= channel
->ust_app_uid
;
3318 request
.key
= channel
->key
;
3320 DBG("Sending metadata request to sessiond, session id %" PRIu64
3321 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3322 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3325 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3327 health_code_update();
3329 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3332 ERR("Asking metadata to sessiond");
3336 health_code_update();
3338 /* Receive the metadata from sessiond */
3339 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3341 if (ret
!= sizeof(msg
)) {
3342 DBG("Consumer received unexpected message size %d (expects %zu)",
3344 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3346 * The ret value might 0 meaning an orderly shutdown but this is ok
3347 * since the caller handles this.
3352 health_code_update();
3354 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3355 /* No registry found */
3356 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3360 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3361 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3366 len
= msg
.u
.push_metadata
.len
;
3367 key
= msg
.u
.push_metadata
.key
;
3368 offset
= msg
.u
.push_metadata
.target_offset
;
3369 version
= msg
.u
.push_metadata
.version
;
3371 LTTNG_ASSERT(key
== channel
->key
);
3373 DBG("No new metadata to receive for key %" PRIu64
, key
);
3376 health_code_update();
3378 /* Tell session daemon we are ready to receive the metadata. */
3379 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3380 LTTCOMM_CONSUMERD_SUCCESS
);
3381 if (ret
< 0 || len
== 0) {
3383 * Somehow, the session daemon is not responding anymore or there is
3384 * nothing to receive.
3389 health_code_update();
3391 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3392 key
, offset
, len
, version
, channel
, timer
, wait
);
3395 * Only send the status msg if the sessiond is alive meaning a positive
3398 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3403 health_code_update();
3405 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3410 * Return the ustctl call for the get stream id.
3412 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3413 uint64_t *stream_id
)
3415 LTTNG_ASSERT(stream
);
3416 LTTNG_ASSERT(stream_id
);
3418 return lttng_ust_ctl_get_stream_id(stream
->ustream
, stream_id
);
3421 void lttng_ustconsumer_sigbus_handle(void *addr
)
3423 lttng_ust_ctl_sigbus_handle(addr
);