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
);
711 * Clear quiescent state from channel's streams using the given key to
712 * retrieve the channel.
714 * Return 0 on success else an LTTng error code.
716 static int clear_quiescent_channel(uint64_t chan_key
)
719 struct lttng_consumer_channel
*channel
;
720 struct lttng_consumer_stream
*stream
;
722 struct lttng_ht_iter iter
;
724 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
727 channel
= consumer_find_channel(chan_key
);
729 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
730 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
734 ht
= the_consumer_data
.stream_per_chan_id_ht
;
736 /* For each stream of the channel id, clear quiescent state. */
737 cds_lfht_for_each_entry_duplicate(ht
->ht
,
738 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
739 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
741 health_code_update();
743 pthread_mutex_lock(&stream
->lock
);
744 stream
->quiescent
= false;
745 pthread_mutex_unlock(&stream
->lock
);
753 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
755 * Return 0 on success else an LTTng error code.
757 static int close_metadata(uint64_t chan_key
)
760 struct lttng_consumer_channel
*channel
;
761 unsigned int channel_monitor
;
763 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
765 channel
= consumer_find_channel(chan_key
);
768 * This is possible if the metadata thread has issue a delete because
769 * the endpoint point of the stream hung up. There is no way the
770 * session daemon can know about it thus use a DBG instead of an actual
773 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
774 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
778 pthread_mutex_lock(&the_consumer_data
.lock
);
779 pthread_mutex_lock(&channel
->lock
);
780 channel_monitor
= channel
->monitor
;
781 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
785 lttng_ustconsumer_close_metadata(channel
);
786 pthread_mutex_unlock(&channel
->lock
);
787 pthread_mutex_unlock(&the_consumer_data
.lock
);
790 * The ownership of a metadata channel depends on the type of
791 * session to which it belongs. In effect, the monitor flag is checked
792 * to determine if this metadata channel is in "snapshot" mode or not.
794 * In the non-snapshot case, the metadata channel is created along with
795 * a single stream which will remain present until the metadata channel
796 * is destroyed (on the destruction of its session). In this case, the
797 * metadata stream in "monitored" by the metadata poll thread and holds
798 * the ownership of its channel.
800 * Closing the metadata will cause the metadata stream's "metadata poll
801 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
802 * thread which will teardown the metadata stream which, in return,
803 * deletes the metadata channel.
805 * In the snapshot case, the metadata stream is created and destroyed
806 * on every snapshot record. Since the channel doesn't have an owner
807 * other than the session daemon, it is safe to destroy it immediately
808 * on reception of the CLOSE_METADATA command.
810 if (!channel_monitor
) {
812 * The channel and consumer_data locks must be
813 * released before this call since consumer_del_channel
814 * re-acquires the channel and consumer_data locks to teardown
815 * the channel and queue its reclamation by the "call_rcu"
818 consumer_del_channel(channel
);
823 pthread_mutex_unlock(&channel
->lock
);
824 pthread_mutex_unlock(&the_consumer_data
.lock
);
830 * RCU read side lock MUST be acquired before calling this function.
832 * Return 0 on success else an LTTng error code.
834 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
837 struct lttng_consumer_channel
*metadata
;
839 ASSERT_RCU_READ_LOCKED();
841 DBG("UST consumer setup metadata key %" PRIu64
, key
);
843 metadata
= consumer_find_channel(key
);
845 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
846 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
851 * In no monitor mode, the metadata channel has no stream(s) so skip the
852 * ownership transfer to the metadata thread.
854 if (!metadata
->monitor
) {
855 DBG("Metadata channel in no monitor");
861 * Send metadata stream to relayd if one available. Availability is
862 * known if the stream is still in the list of the channel.
864 if (cds_list_empty(&metadata
->streams
.head
)) {
865 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
866 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
867 goto error_no_stream
;
870 /* Send metadata stream to relayd if needed. */
871 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
872 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
875 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
878 ret
= consumer_send_relayd_streams_sent(
879 metadata
->metadata_stream
->net_seq_idx
);
881 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
887 * Ownership of metadata stream is passed along. Freeing is handled by
890 ret
= send_streams_to_thread(metadata
, ctx
);
893 * If we are unable to send the stream to the thread, there is
894 * a big problem so just stop everything.
896 ret
= LTTCOMM_CONSUMERD_FATAL
;
897 goto send_streams_error
;
899 /* List MUST be empty after or else it could be reused. */
900 LTTNG_ASSERT(cds_list_empty(&metadata
->streams
.head
));
907 * Delete metadata channel on error. At this point, the metadata stream can
908 * NOT be monitored by the metadata thread thus having the guarantee that
909 * the stream is still in the local stream list of the channel. This call
910 * will make sure to clean that list.
912 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
913 metadata
->metadata_stream
= NULL
;
921 * Snapshot the whole metadata.
922 * RCU read-side lock must be held by the caller.
924 * Returns 0 on success, < 0 on error
926 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
927 uint64_t key
, char *path
, uint64_t relayd_id
,
928 struct lttng_consumer_local_data
*ctx
)
931 struct lttng_consumer_stream
*metadata_stream
;
935 ASSERT_RCU_READ_LOCKED();
937 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
942 LTTNG_ASSERT(!metadata_channel
->monitor
);
944 health_code_update();
947 * Ask the sessiond if we have new metadata waiting and update the
948 * consumer metadata cache.
950 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
955 health_code_update();
958 * The metadata stream is NOT created in no monitor mode when the channel
959 * is created on a sessiond ask channel command.
961 ret
= create_ust_streams(metadata_channel
, ctx
);
966 metadata_stream
= metadata_channel
->metadata_stream
;
967 LTTNG_ASSERT(metadata_stream
);
969 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
970 if (relayd_id
!= (uint64_t) -1ULL) {
971 metadata_stream
->net_seq_idx
= relayd_id
;
972 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
974 ret
= consumer_stream_create_output_files(metadata_stream
,
982 health_code_update();
983 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
990 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
992 * Clean up the stream completely because the next snapshot will use a
993 * new metadata stream.
995 consumer_stream_destroy(metadata_stream
, NULL
);
996 metadata_channel
->metadata_stream
= NULL
;
1004 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1008 unsigned long mmap_offset
;
1009 const char *mmap_base
;
1011 mmap_base
= (const char *) lttng_ust_ctl_get_mmap_base(stream
->ustream
);
1013 ERR("Failed to get mmap base for stream `%s`",
1019 ret
= lttng_ust_ctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1021 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1026 *addr
= mmap_base
+ mmap_offset
;
1033 * Take a snapshot of all the stream of a channel.
1034 * RCU read-side lock and the channel lock must be held by the caller.
1036 * Returns 0 on success, < 0 on error
1038 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1039 uint64_t key
, char *path
, uint64_t relayd_id
,
1040 uint64_t nb_packets_per_stream
,
1041 struct lttng_consumer_local_data
*ctx
)
1044 unsigned use_relayd
= 0;
1045 unsigned long consumed_pos
, produced_pos
;
1046 struct lttng_consumer_stream
*stream
;
1050 ASSERT_RCU_READ_LOCKED();
1054 if (relayd_id
!= (uint64_t) -1ULL) {
1058 LTTNG_ASSERT(!channel
->monitor
);
1059 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1061 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1062 health_code_update();
1064 /* Lock stream because we are about to change its state. */
1065 pthread_mutex_lock(&stream
->lock
);
1066 LTTNG_ASSERT(channel
->trace_chunk
);
1067 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1069 * Can't happen barring an internal error as the channel
1070 * holds a reference to the trace chunk.
1072 ERR("Failed to acquire reference to channel's trace chunk");
1076 LTTNG_ASSERT(!stream
->trace_chunk
);
1077 stream
->trace_chunk
= channel
->trace_chunk
;
1079 stream
->net_seq_idx
= relayd_id
;
1082 ret
= consumer_send_relayd_stream(stream
, path
);
1087 ret
= consumer_stream_create_output_files(stream
,
1092 DBG("UST consumer snapshot stream (%" PRIu64
")",
1097 * If tracing is active, we want to perform a "full" buffer flush.
1098 * Else, if quiescent, it has already been done by the prior stop.
1100 if (!stream
->quiescent
) {
1101 ret
= lttng_ust_ctl_flush_buffer(stream
->ustream
, 0);
1103 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64
", channel name = '%s'",
1104 channel
->key
, channel
->name
);
1109 ret
= lttng_ustconsumer_take_snapshot(stream
);
1111 ERR("Taking UST snapshot");
1115 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1117 ERR("Produced UST snapshot position");
1121 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1123 ERR("Consumerd UST snapshot position");
1128 * The original value is sent back if max stream size is larger than
1129 * the possible size of the snapshot. Also, we assume that the session
1130 * daemon should never send a maximum stream size that is lower than
1133 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1134 produced_pos
, nb_packets_per_stream
,
1135 stream
->max_sb_size
);
1137 while ((long) (consumed_pos
- produced_pos
) < 0) {
1139 unsigned long len
, padded_len
;
1140 const char *subbuf_addr
;
1141 struct lttng_buffer_view subbuf_view
;
1143 health_code_update();
1145 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1147 ret
= lttng_ust_ctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1149 if (ret
!= -EAGAIN
) {
1150 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1151 goto error_close_stream
;
1153 DBG("UST consumer get subbuf failed. Skipping it.");
1154 consumed_pos
+= stream
->max_sb_size
;
1155 stream
->chan
->lost_packets
++;
1159 ret
= lttng_ust_ctl_get_subbuf_size(stream
->ustream
, &len
);
1161 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1162 goto error_put_subbuf
;
1165 ret
= lttng_ust_ctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1167 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1168 goto error_put_subbuf
;
1171 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1173 goto error_put_subbuf
;
1176 subbuf_view
= lttng_buffer_view_init(
1177 subbuf_addr
, 0, padded_len
);
1178 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1179 stream
, &subbuf_view
, padded_len
- len
);
1181 if (read_len
!= len
) {
1183 goto error_put_subbuf
;
1186 if (read_len
!= padded_len
) {
1188 goto error_put_subbuf
;
1192 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
1194 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1195 goto error_close_stream
;
1197 consumed_pos
+= stream
->max_sb_size
;
1200 /* Simply close the stream so we can use it on the next snapshot. */
1201 consumer_stream_close(stream
);
1202 pthread_mutex_unlock(&stream
->lock
);
1209 if (lttng_ust_ctl_put_subbuf(stream
->ustream
) < 0) {
1210 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1213 consumer_stream_close(stream
);
1215 pthread_mutex_unlock(&stream
->lock
);
1221 void metadata_stream_reset_cache_consumed_position(
1222 struct lttng_consumer_stream
*stream
)
1224 ASSERT_LOCKED(stream
->lock
);
1226 DBG("Reset metadata cache of session %" PRIu64
,
1227 stream
->chan
->session_id
);
1228 stream
->ust_metadata_pushed
= 0;
1232 * Receive the metadata updates from the sessiond. Supports receiving
1233 * overlapping metadata, but is needs to always belong to a contiguous
1234 * range starting from 0.
1235 * Be careful about the locks held when calling this function: it needs
1236 * the metadata cache flush to concurrently progress in order to
1239 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1240 uint64_t len
, uint64_t version
,
1241 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1243 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1245 enum consumer_metadata_cache_write_status cache_write_status
;
1247 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1249 metadata_str
= calloc
<char>(len
);
1250 if (!metadata_str
) {
1251 PERROR("zmalloc metadata string");
1252 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1256 health_code_update();
1258 /* Receive metadata string. */
1259 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1261 /* Session daemon is dead so return gracefully. */
1266 health_code_update();
1268 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1269 cache_write_status
= consumer_metadata_cache_write(
1270 channel
->metadata_cache
, offset
, len
, version
,
1272 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1273 switch (cache_write_status
) {
1274 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
:
1276 * The write entirely overlapped with existing contents of the
1277 * same metadata version (same content); there is nothing to do.
1280 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
:
1282 * The metadata cache was invalidated (previously pushed
1283 * content has been overwritten). Reset the stream's consumed
1284 * metadata position to ensure the metadata poll thread consumes
1289 * channel::metadata_stream can be null when the metadata
1290 * channel is under a snapshot session type. No need to update
1291 * the stream position in that scenario.
1293 if (channel
->metadata_stream
!= NULL
) {
1294 pthread_mutex_lock(&channel
->metadata_stream
->lock
);
1295 metadata_stream_reset_cache_consumed_position(
1296 channel
->metadata_stream
);
1297 pthread_mutex_unlock(&channel
->metadata_stream
->lock
);
1299 /* Validate we are in snapshot mode. */
1300 LTTNG_ASSERT(!channel
->monitor
);
1303 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
:
1305 * In both cases, the metadata poll thread has new data to
1308 ret
= consumer_metadata_wakeup_pipe(channel
);
1310 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1314 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
:
1315 /* Unable to handle metadata. Notify session daemon. */
1316 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1318 * Skip metadata flush on write error since the offset and len might
1319 * not have been updated which could create an infinite loop below when
1320 * waiting for the metadata cache to be flushed.
1330 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1331 DBG("Waiting for metadata to be flushed");
1333 health_code_update();
1335 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1345 * Receive command from session daemon and process it.
1347 * Return 1 on success else a negative value or 0.
1349 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1350 int sock
, struct pollfd
*consumer_sockpoll
)
1353 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1354 struct lttcomm_consumer_msg msg
;
1355 struct lttng_consumer_channel
*channel
= NULL
;
1357 health_code_update();
1362 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1363 if (ret_recv
!= sizeof(msg
)) {
1364 DBG("Consumer received unexpected message size %zd (expects %zu)",
1365 ret_recv
, sizeof(msg
));
1367 * The ret value might 0 meaning an orderly shutdown but this is ok
1368 * since the caller handles this.
1371 lttng_consumer_send_error(ctx
,
1372 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1379 health_code_update();
1382 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1384 health_code_update();
1386 /* relayd needs RCU read-side lock */
1389 switch (msg
.cmd_type
) {
1390 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1392 uint32_t major
= msg
.u
.relayd_sock
.major
;
1393 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
1394 enum lttcomm_sock_proto protocol
=
1395 (enum lttcomm_sock_proto
) msg
.u
.relayd_sock
1396 .relayd_socket_protocol
;
1398 /* Session daemon status message are handled in the following call. */
1399 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1400 msg
.u
.relayd_sock
.type
, ctx
, sock
,
1401 consumer_sockpoll
, msg
.u
.relayd_sock
.session_id
,
1402 msg
.u
.relayd_sock
.relayd_session_id
, major
,
1406 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1408 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1409 struct consumer_relayd_sock_pair
*relayd
;
1411 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1413 /* Get relayd reference if exists. */
1414 relayd
= consumer_find_relayd(index
);
1415 if (relayd
== NULL
) {
1416 DBG("Unable to find relayd %" PRIu64
, index
);
1417 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1421 * Each relayd socket pair has a refcount of stream attached to it
1422 * which tells if the relayd is still active or not depending on the
1425 * This will set the destroy flag of the relayd object and destroy it
1426 * if the refcount reaches zero when called.
1428 * The destroy can happen either here or when a stream fd hangs up.
1431 consumer_flag_relayd_for_destroy(relayd
);
1434 goto end_msg_sessiond
;
1436 case LTTNG_CONSUMER_UPDATE_STREAM
:
1441 case LTTNG_CONSUMER_DATA_PENDING
:
1443 int is_data_pending
;
1445 uint64_t id
= msg
.u
.data_pending
.session_id
;
1447 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1449 is_data_pending
= consumer_data_pending(id
);
1451 /* Send back returned value to session daemon */
1452 ret_send
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1453 sizeof(is_data_pending
));
1455 DBG("Error when sending the data pending ret code: %zd",
1461 * No need to send back a status message since the data pending
1462 * returned value is the response.
1466 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1468 int ret_ask_channel
, ret_add_channel
, ret_send
;
1469 struct lttng_ust_ctl_consumer_channel_attr attr
;
1470 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1471 const struct lttng_credentials buffer_credentials
= {
1472 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.uid
),
1473 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.ask_channel
.buffer_credentials
.gid
),
1476 /* Create a plain object and reserve a channel key. */
1477 channel
= consumer_allocate_channel(
1478 msg
.u
.ask_channel
.key
,
1479 msg
.u
.ask_channel
.session_id
,
1480 msg
.u
.ask_channel
.chunk_id
.is_set
?
1482 msg
.u
.ask_channel
.pathname
,
1483 msg
.u
.ask_channel
.name
,
1484 msg
.u
.ask_channel
.relayd_id
,
1485 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1486 msg
.u
.ask_channel
.tracefile_size
,
1487 msg
.u
.ask_channel
.tracefile_count
,
1488 msg
.u
.ask_channel
.session_id_per_pid
,
1489 msg
.u
.ask_channel
.monitor
,
1490 msg
.u
.ask_channel
.live_timer_interval
,
1491 msg
.u
.ask_channel
.is_live
,
1492 msg
.u
.ask_channel
.root_shm_path
,
1493 msg
.u
.ask_channel
.shm_path
);
1495 goto end_channel_error
;
1498 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1499 buffer_credentials
);
1502 * Assign UST application UID to the channel. This value is ignored for
1503 * per PID buffers. This is specific to UST thus setting this after the
1506 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1508 /* Build channel attributes from received message. */
1509 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1510 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1511 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1512 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1513 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1514 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1515 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1516 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1518 /* Match channel buffer type to the UST abi. */
1519 switch (msg
.u
.ask_channel
.output
) {
1520 case LTTNG_EVENT_MMAP
:
1522 attr
.output
= LTTNG_UST_ABI_MMAP
;
1526 /* Translate and save channel type. */
1527 switch (msg
.u
.ask_channel
.type
) {
1528 case LTTNG_UST_ABI_CHAN_PER_CPU
:
1529 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1530 attr
.type
= LTTNG_UST_ABI_CHAN_PER_CPU
;
1532 * Set refcount to 1 for owner. Below, we will
1533 * pass ownership to the
1534 * consumer_thread_channel_poll() thread.
1536 channel
->refcount
= 1;
1538 case LTTNG_UST_ABI_CHAN_METADATA
:
1539 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1540 attr
.type
= LTTNG_UST_ABI_CHAN_METADATA
;
1547 health_code_update();
1549 ret_ask_channel
= ask_channel(ctx
, channel
, &attr
);
1550 if (ret_ask_channel
< 0) {
1551 goto end_channel_error
;
1554 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1557 ret_allocate
= consumer_metadata_cache_allocate(
1559 if (ret_allocate
< 0) {
1560 ERR("Allocating metadata cache");
1561 goto end_channel_error
;
1563 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1564 attr
.switch_timer_interval
= 0;
1566 int monitor_start_ret
;
1568 consumer_timer_live_start(channel
,
1569 msg
.u
.ask_channel
.live_timer_interval
);
1570 monitor_start_ret
= consumer_timer_monitor_start(
1572 msg
.u
.ask_channel
.monitor_timer_interval
);
1573 if (monitor_start_ret
< 0) {
1574 ERR("Starting channel monitoring timer failed");
1575 goto end_channel_error
;
1579 health_code_update();
1582 * Add the channel to the internal state AFTER all streams were created
1583 * and successfully sent to session daemon. This way, all streams must
1584 * be ready before this channel is visible to the threads.
1585 * If add_channel succeeds, ownership of the channel is
1586 * passed to consumer_thread_channel_poll().
1588 ret_add_channel
= add_channel(channel
, ctx
);
1589 if (ret_add_channel
< 0) {
1590 if (msg
.u
.ask_channel
.type
== LTTNG_UST_ABI_CHAN_METADATA
) {
1591 if (channel
->switch_timer_enabled
== 1) {
1592 consumer_timer_switch_stop(channel
);
1594 consumer_metadata_cache_destroy(channel
);
1596 if (channel
->live_timer_enabled
== 1) {
1597 consumer_timer_live_stop(channel
);
1599 if (channel
->monitor_timer_enabled
== 1) {
1600 consumer_timer_monitor_stop(channel
);
1602 goto end_channel_error
;
1605 health_code_update();
1608 * Channel and streams are now created. Inform the session daemon that
1609 * everything went well and should wait to receive the channel and
1610 * streams with ustctl API.
1612 ret_send
= consumer_send_status_channel(sock
, channel
);
1615 * There is probably a problem on the socket.
1622 case LTTNG_CONSUMER_GET_CHANNEL
:
1624 int ret
, relayd_err
= 0;
1625 uint64_t key
= msg
.u
.get_channel
.key
;
1626 struct lttng_consumer_channel
*found_channel
;
1628 found_channel
= consumer_find_channel(key
);
1629 if (!found_channel
) {
1630 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1631 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1632 goto end_get_channel
;
1635 health_code_update();
1637 /* Send the channel to sessiond (and relayd, if applicable). */
1638 ret
= send_channel_to_sessiond_and_relayd(
1639 sock
, found_channel
, ctx
, &relayd_err
);
1643 * We were unable to send to the relayd the stream so avoid
1644 * sending back a fatal error to the thread since this is OK
1645 * and the consumer can continue its work. The above call
1646 * has sent the error status message to the sessiond.
1648 goto end_get_channel_nosignal
;
1651 * The communicaton was broken hence there is a bad state between
1652 * the consumer and sessiond so stop everything.
1654 goto error_get_channel_fatal
;
1657 health_code_update();
1660 * In no monitor mode, the streams ownership is kept inside the channel
1661 * so don't send them to the data thread.
1663 if (!found_channel
->monitor
) {
1664 goto end_get_channel
;
1667 ret
= send_streams_to_thread(found_channel
, ctx
);
1670 * If we are unable to send the stream to the thread, there is
1671 * a big problem so just stop everything.
1673 goto error_get_channel_fatal
;
1675 /* List MUST be empty after or else it could be reused. */
1676 LTTNG_ASSERT(cds_list_empty(&found_channel
->streams
.head
));
1678 goto end_msg_sessiond
;
1679 error_get_channel_fatal
:
1681 end_get_channel_nosignal
:
1684 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1686 uint64_t key
= msg
.u
.destroy_channel
.key
;
1689 * Only called if streams have not been sent to stream
1690 * manager thread. However, channel has been sent to
1691 * channel manager thread.
1693 notify_thread_del_channel(ctx
, key
);
1694 goto end_msg_sessiond
;
1696 case LTTNG_CONSUMER_CLOSE_METADATA
:
1700 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1702 ret_code
= (lttcomm_return_code
) ret
;
1705 goto end_msg_sessiond
;
1707 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1711 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1713 ret_code
= (lttcomm_return_code
) ret
;
1716 goto end_msg_sessiond
;
1718 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1722 ret
= clear_quiescent_channel(
1723 msg
.u
.clear_quiescent_channel
.key
);
1725 ret_code
= (lttcomm_return_code
) ret
;
1728 goto end_msg_sessiond
;
1730 case LTTNG_CONSUMER_PUSH_METADATA
:
1733 uint64_t len
= msg
.u
.push_metadata
.len
;
1734 uint64_t key
= msg
.u
.push_metadata
.key
;
1735 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1736 uint64_t version
= msg
.u
.push_metadata
.version
;
1737 struct lttng_consumer_channel
*found_channel
;
1739 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1742 found_channel
= consumer_find_channel(key
);
1743 if (!found_channel
) {
1745 * This is possible if the metadata creation on the consumer side
1746 * is in flight vis-a-vis a concurrent push metadata from the
1747 * session daemon. Simply return that the channel failed and the
1748 * session daemon will handle that message correctly considering
1749 * that this race is acceptable thus the DBG() statement here.
1751 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1752 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1753 goto end_push_metadata_msg_sessiond
;
1756 health_code_update();
1760 * There is nothing to receive. We have simply
1761 * checked whether the channel can be found.
1763 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1764 goto end_push_metadata_msg_sessiond
;
1767 /* Tell session daemon we are ready to receive the metadata. */
1768 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1770 /* Somehow, the session daemon is not responding anymore. */
1771 goto error_push_metadata_fatal
;
1774 health_code_update();
1776 /* Wait for more data. */
1777 health_poll_entry();
1778 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1781 goto error_push_metadata_fatal
;
1784 health_code_update();
1786 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
, len
,
1787 version
, found_channel
, 0, 1);
1789 /* error receiving from sessiond */
1790 goto error_push_metadata_fatal
;
1792 ret_code
= (lttcomm_return_code
) ret
;
1793 goto end_push_metadata_msg_sessiond
;
1795 end_push_metadata_msg_sessiond
:
1796 goto end_msg_sessiond
;
1797 error_push_metadata_fatal
:
1800 case LTTNG_CONSUMER_SETUP_METADATA
:
1804 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1806 ret_code
= (lttcomm_return_code
) ret
;
1808 goto end_msg_sessiond
;
1810 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1812 struct lttng_consumer_channel
*found_channel
;
1813 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1816 found_channel
= consumer_find_channel(key
);
1817 if (!found_channel
) {
1818 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1819 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1821 if (msg
.u
.snapshot_channel
.metadata
) {
1824 ret_snapshot
= snapshot_metadata(found_channel
,
1826 msg
.u
.snapshot_channel
.pathname
,
1827 msg
.u
.snapshot_channel
.relayd_id
,
1829 if (ret_snapshot
< 0) {
1830 ERR("Snapshot metadata failed");
1831 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1836 ret_snapshot
= snapshot_channel(found_channel
,
1838 msg
.u
.snapshot_channel
.pathname
,
1839 msg
.u
.snapshot_channel
.relayd_id
,
1840 msg
.u
.snapshot_channel
1841 .nb_packets_per_stream
,
1843 if (ret_snapshot
< 0) {
1844 ERR("Snapshot channel failed");
1845 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1849 health_code_update();
1850 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1852 /* Somehow, the session daemon is not responding anymore. */
1855 health_code_update();
1858 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1861 uint64_t discarded_events
;
1862 struct lttng_ht_iter iter
;
1863 struct lttng_ht
*ht
;
1864 struct lttng_consumer_stream
*stream
;
1865 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1866 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1868 DBG("UST consumer discarded events command for session id %"
1871 pthread_mutex_lock(&the_consumer_data
.lock
);
1873 ht
= the_consumer_data
.stream_list_ht
;
1876 * We only need a reference to the channel, but they are not
1877 * directly indexed, so we just use the first matching stream
1878 * to extract the information we need, we default to 0 if not
1879 * found (no events are dropped if the channel is not yet in
1882 discarded_events
= 0;
1883 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1884 ht
->hash_fct(&id
, lttng_ht_seed
),
1886 &iter
.iter
, stream
, node_session_id
.node
) {
1887 if (stream
->chan
->key
== key
) {
1888 discarded_events
= stream
->chan
->discarded_events
;
1892 pthread_mutex_unlock(&the_consumer_data
.lock
);
1895 DBG("UST consumer discarded events command for session id %"
1896 PRIu64
", channel key %" PRIu64
, id
, key
);
1898 health_code_update();
1900 /* Send back returned value to session daemon */
1901 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1903 PERROR("send discarded events");
1909 case LTTNG_CONSUMER_LOST_PACKETS
:
1912 uint64_t lost_packets
;
1913 struct lttng_ht_iter iter
;
1914 struct lttng_ht
*ht
;
1915 struct lttng_consumer_stream
*stream
;
1916 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1917 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1919 DBG("UST consumer lost packets command for session id %"
1922 pthread_mutex_lock(&the_consumer_data
.lock
);
1924 ht
= the_consumer_data
.stream_list_ht
;
1927 * We only need a reference to the channel, but they are not
1928 * directly indexed, so we just use the first matching stream
1929 * to extract the information we need, we default to 0 if not
1930 * found (no packets lost if the channel is not yet in use).
1933 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1934 ht
->hash_fct(&id
, lttng_ht_seed
),
1936 &iter
.iter
, stream
, node_session_id
.node
) {
1937 if (stream
->chan
->key
== key
) {
1938 lost_packets
= stream
->chan
->lost_packets
;
1942 pthread_mutex_unlock(&the_consumer_data
.lock
);
1945 DBG("UST consumer lost packets command for session id %"
1946 PRIu64
", channel key %" PRIu64
, id
, key
);
1948 health_code_update();
1950 /* Send back returned value to session daemon */
1951 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1952 sizeof(lost_packets
));
1954 PERROR("send lost packets");
1960 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1962 int channel_monitor_pipe
, ret_send
,
1963 ret_set_channel_monitor_pipe
;
1966 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1967 /* Successfully received the command's type. */
1968 ret_send
= consumer_send_status_msg(sock
, ret_code
);
1973 ret_recv
= lttcomm_recv_fds_unix_sock(
1974 sock
, &channel_monitor_pipe
, 1);
1975 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1976 ERR("Failed to receive channel monitor pipe");
1980 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1981 ret_set_channel_monitor_pipe
=
1982 consumer_timer_thread_set_channel_monitor_pipe(
1983 channel_monitor_pipe
);
1984 if (!ret_set_channel_monitor_pipe
) {
1988 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1989 /* Set the pipe as non-blocking. */
1990 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1991 if (ret_fcntl
== -1) {
1992 PERROR("fcntl get flags of the channel monitoring pipe");
1997 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
1998 flags
| O_NONBLOCK
);
1999 if (ret_fcntl
== -1) {
2000 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2003 DBG("Channel monitor pipe set as non-blocking");
2005 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
2007 goto end_msg_sessiond
;
2009 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
2011 struct lttng_consumer_channel
*found_channel
;
2012 uint64_t key
= msg
.u
.rotate_channel
.key
;
2013 int ret_send_status
;
2015 found_channel
= consumer_find_channel(key
);
2016 if (!found_channel
) {
2017 DBG("Channel %" PRIu64
" not found", key
);
2018 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2023 * Sample the rotate position of all the streams in
2026 rotate_channel
= lttng_consumer_rotate_channel(
2028 msg
.u
.rotate_channel
.relayd_id
);
2029 if (rotate_channel
< 0) {
2030 ERR("Rotate channel failed");
2031 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2034 health_code_update();
2037 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2038 if (ret_send_status
< 0) {
2039 /* Somehow, the session daemon is not responding anymore. */
2040 goto end_rotate_channel_nosignal
;
2044 * Rotate the streams that are ready right now.
2045 * FIXME: this is a second consecutive iteration over the
2046 * streams in a channel, there is probably a better way to
2047 * handle this, but it needs to be after the
2048 * consumer_send_status_msg() call.
2050 if (found_channel
) {
2051 int ret_rotate_read_streams
;
2053 ret_rotate_read_streams
=
2054 lttng_consumer_rotate_ready_streams(
2055 found_channel
, key
);
2056 if (ret_rotate_read_streams
< 0) {
2057 ERR("Rotate channel failed");
2061 end_rotate_channel_nosignal
:
2064 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
2066 struct lttng_consumer_channel
*found_channel
;
2067 uint64_t key
= msg
.u
.clear_channel
.key
;
2068 int ret_send_status
;
2070 found_channel
= consumer_find_channel(key
);
2071 if (!found_channel
) {
2072 DBG("Channel %" PRIu64
" not found", key
);
2073 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2075 int ret_clear_channel
;
2077 ret_clear_channel
= lttng_consumer_clear_channel(
2079 if (ret_clear_channel
) {
2080 ERR("Clear channel failed key %" PRIu64
, key
);
2081 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
2084 health_code_update();
2086 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2087 if (ret_send_status
< 0) {
2088 /* Somehow, the session daemon is not responding anymore. */
2093 case LTTNG_CONSUMER_INIT
:
2095 int ret_send_status
;
2097 ret_code
= lttng_consumer_init_command(ctx
,
2098 msg
.u
.init
.sessiond_uuid
);
2099 health_code_update();
2100 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2101 if (ret_send_status
< 0) {
2102 /* Somehow, the session daemon is not responding anymore. */
2107 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2109 const struct lttng_credentials credentials
= {
2110 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
2111 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
2113 const bool is_local_trace
=
2114 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2115 const uint64_t relayd_id
=
2116 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2117 const char *chunk_override_name
=
2118 *msg
.u
.create_trace_chunk
.override_name
?
2119 msg
.u
.create_trace_chunk
.override_name
:
2121 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
2124 * The session daemon will only provide a chunk directory file
2125 * descriptor for local traces.
2127 if (is_local_trace
) {
2129 int ret_send_status
;
2132 /* Acnowledge the reception of the command. */
2133 ret_send_status
= consumer_send_status_msg(
2134 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
2135 if (ret_send_status
< 0) {
2136 /* Somehow, the session daemon is not responding anymore. */
2141 * Receive trace chunk domain dirfd.
2143 ret_recv
= lttcomm_recv_fds_unix_sock(
2144 sock
, &chunk_dirfd
, 1);
2145 if (ret_recv
!= sizeof(chunk_dirfd
)) {
2146 ERR("Failed to receive trace chunk domain directory file descriptor");
2150 DBG("Received trace chunk domain directory fd (%d)",
2152 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
2154 if (!chunk_directory_handle
) {
2155 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2156 if (close(chunk_dirfd
)) {
2157 PERROR("Failed to close chunk directory file descriptor");
2163 ret_code
= lttng_consumer_create_trace_chunk(
2164 !is_local_trace
? &relayd_id
: NULL
,
2165 msg
.u
.create_trace_chunk
.session_id
,
2166 msg
.u
.create_trace_chunk
.chunk_id
,
2167 (time_t) msg
.u
.create_trace_chunk
2168 .creation_timestamp
,
2169 chunk_override_name
,
2170 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2173 chunk_directory_handle
);
2174 lttng_directory_handle_put(chunk_directory_handle
);
2175 goto end_msg_sessiond
;
2177 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2179 enum lttng_trace_chunk_command_type close_command
=
2180 (lttng_trace_chunk_command_type
)
2181 msg
.u
.close_trace_chunk
.close_command
.value
;
2182 const uint64_t relayd_id
=
2183 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2184 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2185 char closed_trace_chunk_path
[LTTNG_PATH_MAX
] = {};
2188 ret_code
= lttng_consumer_close_trace_chunk(
2189 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2192 msg
.u
.close_trace_chunk
.session_id
,
2193 msg
.u
.close_trace_chunk
.chunk_id
,
2194 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2195 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2197 NULL
, closed_trace_chunk_path
);
2198 reply
.ret_code
= ret_code
;
2199 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2200 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2201 if (ret
!= sizeof(reply
)) {
2204 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2206 if (ret
!= reply
.path_length
) {
2211 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2213 const uint64_t relayd_id
=
2214 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2216 ret_code
= lttng_consumer_trace_chunk_exists(
2217 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2219 msg
.u
.trace_chunk_exists
.session_id
,
2220 msg
.u
.trace_chunk_exists
.chunk_id
);
2221 goto end_msg_sessiond
;
2223 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
2225 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
2226 struct lttng_consumer_channel
*found_channel
=
2227 consumer_find_channel(key
);
2229 if (found_channel
) {
2230 pthread_mutex_lock(&found_channel
->lock
);
2231 ret_code
= lttng_consumer_open_channel_packets(
2233 pthread_mutex_unlock(&found_channel
->lock
);
2236 * The channel could have disappeared in per-pid
2239 DBG("Channel %" PRIu64
" not found", key
);
2240 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2243 health_code_update();
2244 goto end_msg_sessiond
;
2252 * Return 1 to indicate success since the 0 value can be a socket
2253 * shutdown during the recv() or send() call.
2260 * The returned value here is not useful since either way we'll return 1 to
2261 * the caller because the session daemon socket management is done
2262 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2265 int ret_send_status
;
2267 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
2268 if (ret_send_status
< 0) {
2278 consumer_del_channel(channel
);
2280 /* We have to send a status channel message indicating an error. */
2282 int ret_send_status
;
2284 ret_send_status
= consumer_send_status_channel(sock
, NULL
);
2285 if (ret_send_status
< 0) {
2286 /* Stop everything if session daemon can not be notified. */
2295 /* This will issue a consumer stop. */
2301 health_code_update();
2305 int lttng_ust_flush_buffer(struct lttng_consumer_stream
*stream
,
2306 int producer_active
)
2308 LTTNG_ASSERT(stream
);
2309 LTTNG_ASSERT(stream
->ustream
);
2311 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer_active
);
2315 * Take a snapshot for a specific stream.
2317 * Returns 0 on success, < 0 on error
2319 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2321 LTTNG_ASSERT(stream
);
2322 LTTNG_ASSERT(stream
->ustream
);
2324 return lttng_ust_ctl_snapshot(stream
->ustream
);
2328 * Sample consumed and produced positions for a specific stream.
2330 * Returns 0 on success, < 0 on error.
2332 int lttng_ustconsumer_sample_snapshot_positions(
2333 struct lttng_consumer_stream
*stream
)
2335 LTTNG_ASSERT(stream
);
2336 LTTNG_ASSERT(stream
->ustream
);
2338 return lttng_ust_ctl_snapshot_sample_positions(stream
->ustream
);
2342 * Get the produced position
2344 * Returns 0 on success, < 0 on error
2346 int lttng_ustconsumer_get_produced_snapshot(
2347 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2349 LTTNG_ASSERT(stream
);
2350 LTTNG_ASSERT(stream
->ustream
);
2353 return lttng_ust_ctl_snapshot_get_produced(stream
->ustream
, pos
);
2357 * Get the consumed position
2359 * Returns 0 on success, < 0 on error
2361 int lttng_ustconsumer_get_consumed_snapshot(
2362 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2364 LTTNG_ASSERT(stream
);
2365 LTTNG_ASSERT(stream
->ustream
);
2368 return lttng_ust_ctl_snapshot_get_consumed(stream
->ustream
, pos
);
2371 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2374 LTTNG_ASSERT(stream
);
2375 LTTNG_ASSERT(stream
->ustream
);
2377 return lttng_ust_ctl_flush_buffer(stream
->ustream
, producer
);
2380 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream
*stream
)
2382 LTTNG_ASSERT(stream
);
2383 LTTNG_ASSERT(stream
->ustream
);
2385 return lttng_ust_ctl_clear_buffer(stream
->ustream
);
2388 int lttng_ustconsumer_get_current_timestamp(
2389 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2391 LTTNG_ASSERT(stream
);
2392 LTTNG_ASSERT(stream
->ustream
);
2395 return lttng_ust_ctl_get_current_timestamp(stream
->ustream
, ts
);
2398 int lttng_ustconsumer_get_sequence_number(
2399 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2401 LTTNG_ASSERT(stream
);
2402 LTTNG_ASSERT(stream
->ustream
);
2405 return lttng_ust_ctl_get_sequence_number(stream
->ustream
, seq
);
2409 * Called when the stream signals the consumer that it has hung up.
2411 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2413 LTTNG_ASSERT(stream
);
2414 LTTNG_ASSERT(stream
->ustream
);
2416 pthread_mutex_lock(&stream
->lock
);
2417 if (!stream
->quiescent
) {
2418 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 0) < 0) {
2419 ERR("Failed to flush buffer on stream hang-up");
2421 stream
->quiescent
= true;
2424 pthread_mutex_unlock(&stream
->lock
);
2425 stream
->hangup_flush_done
= 1;
2428 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2433 LTTNG_ASSERT(chan
->uchan
);
2434 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2436 if (chan
->switch_timer_enabled
== 1) {
2437 consumer_timer_switch_stop(chan
);
2439 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2442 ret
= close(chan
->stream_fds
[i
]);
2446 if (chan
->shm_path
[0]) {
2447 char shm_path
[PATH_MAX
];
2449 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2451 ERR("Cannot get stream shm path");
2453 ret
= run_as_unlink(shm_path
,
2454 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2455 chan
->buffer_credentials
)),
2456 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2457 chan
->buffer_credentials
)));
2459 PERROR("unlink %s", shm_path
);
2465 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2468 LTTNG_ASSERT(chan
->uchan
);
2469 LTTNG_ASSERT(chan
->buffer_credentials
.is_set
);
2471 consumer_metadata_cache_destroy(chan
);
2472 lttng_ust_ctl_destroy_channel(chan
->uchan
);
2473 /* Try to rmdir all directories under shm_path root. */
2474 if (chan
->root_shm_path
[0]) {
2475 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2476 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2477 chan
->buffer_credentials
)),
2478 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2479 chan
->buffer_credentials
)),
2480 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2482 free(chan
->stream_fds
);
2485 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2487 LTTNG_ASSERT(stream
);
2488 LTTNG_ASSERT(stream
->ustream
);
2490 if (stream
->chan
->switch_timer_enabled
== 1) {
2491 consumer_timer_switch_stop(stream
->chan
);
2493 lttng_ust_ctl_destroy_stream(stream
->ustream
);
2496 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2498 LTTNG_ASSERT(stream
);
2499 LTTNG_ASSERT(stream
->ustream
);
2501 return lttng_ust_ctl_stream_get_wakeup_fd(stream
->ustream
);
2504 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2506 LTTNG_ASSERT(stream
);
2507 LTTNG_ASSERT(stream
->ustream
);
2509 return lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
2513 * Write up to one packet from the metadata cache to the channel.
2515 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2516 * negative value on error.
2519 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2524 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2525 if (stream
->chan
->metadata_cache
->contents
.size
==
2526 stream
->ust_metadata_pushed
) {
2528 * In the context of a user space metadata channel, a
2529 * change in version can be detected in two ways:
2530 * 1) During the pre-consume of the `read_subbuffer` loop,
2531 * 2) When populating the metadata ring buffer (i.e. here).
2533 * This function is invoked when there is no metadata
2534 * available in the ring-buffer. If all data was consumed
2535 * up to the size of the metadata cache, there is no metadata
2536 * to insert in the ring-buffer.
2538 * However, the metadata version could still have changed (a
2539 * regeneration without any new data will yield the same cache
2542 * The cache's version is checked for a version change and the
2543 * consumed position is reset if one occurred.
2545 * This check is only necessary for the user space domain as
2546 * it has to manage the cache explicitly. If this reset was not
2547 * performed, no metadata would be consumed (and no reset would
2548 * occur as part of the pre-consume) until the metadata size
2549 * exceeded the cache size.
2551 if (stream
->metadata_version
!=
2552 stream
->chan
->metadata_cache
->version
) {
2553 metadata_stream_reset_cache_consumed_position(stream
);
2554 consumer_stream_metadata_set_version(stream
,
2555 stream
->chan
->metadata_cache
->version
);
2562 write_len
= lttng_ust_ctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2563 &stream
->chan
->metadata_cache
->contents
.data
[stream
->ust_metadata_pushed
],
2564 stream
->chan
->metadata_cache
->contents
.size
-
2565 stream
->ust_metadata_pushed
);
2566 LTTNG_ASSERT(write_len
!= 0);
2567 if (write_len
< 0) {
2568 ERR("Writing one metadata packet");
2572 stream
->ust_metadata_pushed
+= write_len
;
2574 LTTNG_ASSERT(stream
->chan
->metadata_cache
->contents
.size
>=
2575 stream
->ust_metadata_pushed
);
2579 * Switch packet (but don't open the next one) on every commit of
2580 * a metadata packet. Since the subbuffer is fully filled (with padding,
2581 * if needed), the stream is "quiescent" after this commit.
2583 if (lttng_ust_ctl_flush_buffer(stream
->ustream
, 1)) {
2584 ERR("Failed to flush buffer while committing one metadata packet");
2587 stream
->quiescent
= true;
2590 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2596 * Sync metadata meaning request them to the session daemon and snapshot to the
2597 * metadata thread can consumer them.
2599 * Metadata stream lock is held here, but we need to release it when
2600 * interacting with sessiond, else we cause a deadlock with live
2601 * awaiting on metadata to be pushed out.
2603 * The RCU read side lock must be held by the caller.
2605 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2606 struct lttng_consumer_local_data
*ctx
,
2607 struct lttng_consumer_stream
*metadata_stream
)
2610 enum sync_metadata_status status
;
2611 struct lttng_consumer_channel
*metadata_channel
;
2614 LTTNG_ASSERT(metadata_stream
);
2615 ASSERT_RCU_READ_LOCKED();
2617 metadata_channel
= metadata_stream
->chan
;
2618 pthread_mutex_unlock(&metadata_stream
->lock
);
2620 * Request metadata from the sessiond, but don't wait for the flush
2621 * because we locked the metadata thread.
2623 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2624 pthread_mutex_lock(&metadata_stream
->lock
);
2626 status
= SYNC_METADATA_STATUS_ERROR
;
2631 * The metadata stream and channel can be deleted while the
2632 * metadata stream lock was released. The streamed is checked
2633 * for deletion before we use it further.
2635 * Note that it is safe to access a logically-deleted stream since its
2636 * existence is still guaranteed by the RCU read side lock. However,
2637 * it should no longer be used. The close/deletion of the metadata
2638 * channel and stream already guarantees that all metadata has been
2639 * consumed. Therefore, there is nothing left to do in this function.
2641 if (consumer_stream_is_deleted(metadata_stream
)) {
2642 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2643 metadata_stream
->key
);
2644 status
= SYNC_METADATA_STATUS_NO_DATA
;
2648 ret
= commit_one_metadata_packet(metadata_stream
);
2650 status
= SYNC_METADATA_STATUS_ERROR
;
2652 } else if (ret
> 0) {
2653 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2654 } else /* ret == 0 */ {
2655 status
= SYNC_METADATA_STATUS_NO_DATA
;
2659 ret
= lttng_ust_ctl_snapshot(metadata_stream
->ustream
);
2661 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2662 status
= SYNC_METADATA_STATUS_ERROR
;
2671 * Return 0 on success else a negative value.
2673 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2674 struct lttng_consumer_local_data
*ctx
)
2677 struct lttng_ust_ctl_consumer_stream
*ustream
;
2679 LTTNG_ASSERT(stream
);
2682 ustream
= stream
->ustream
;
2685 * First, we are going to check if there is a new subbuffer available
2686 * before reading the stream wait_fd.
2688 /* Get the next subbuffer */
2689 ret
= lttng_ust_ctl_get_next_subbuf(ustream
);
2691 /* No more data found, flag the stream. */
2692 stream
->has_data
= 0;
2697 ret
= lttng_ust_ctl_put_subbuf(ustream
);
2700 /* This stream still has data. Flag it and wake up the data thread. */
2701 stream
->has_data
= 1;
2703 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2706 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2707 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2712 /* The wake up pipe has been notified. */
2713 ctx
->has_wakeup
= 1;
2721 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2726 * We can consume the 1 byte written into the wait_fd by
2727 * UST. Don't trigger error if we cannot read this one byte
2728 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2730 * This is only done when the stream is monitored by a thread,
2731 * before the flush is done after a hangup and if the stream
2732 * is not flagged with data since there might be nothing to
2733 * consume in the wait fd but still have data available
2734 * flagged by the consumer wake up pipe.
2736 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2740 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2741 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2749 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2750 struct stream_subbuffer
*subbuf
)
2754 ret
= lttng_ust_ctl_get_subbuf_size(
2755 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2760 ret
= lttng_ust_ctl_get_padded_subbuf_size(
2761 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2770 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2771 struct stream_subbuffer
*subbuf
)
2775 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2780 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2786 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2787 struct stream_subbuffer
*subbuf
)
2791 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2796 ret
= lttng_ust_ctl_get_packet_size(
2797 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2799 PERROR("Failed to get sub-buffer packet size");
2803 ret
= lttng_ust_ctl_get_content_size(
2804 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2806 PERROR("Failed to get sub-buffer content size");
2810 ret
= lttng_ust_ctl_get_timestamp_begin(
2811 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2813 PERROR("Failed to get sub-buffer begin timestamp");
2817 ret
= lttng_ust_ctl_get_timestamp_end(
2818 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2820 PERROR("Failed to get sub-buffer end timestamp");
2824 ret
= lttng_ust_ctl_get_events_discarded(
2825 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2827 PERROR("Failed to get sub-buffer events discarded count");
2831 ret
= lttng_ust_ctl_get_sequence_number(stream
->ustream
,
2832 &subbuf
->info
.data
.sequence_number
.value
);
2834 /* May not be supported by older LTTng-modules. */
2835 if (ret
!= -ENOTTY
) {
2836 PERROR("Failed to get sub-buffer sequence number");
2840 subbuf
->info
.data
.sequence_number
.is_set
= true;
2843 ret
= lttng_ust_ctl_get_stream_id(
2844 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2846 PERROR("Failed to get stream id");
2850 ret
= lttng_ust_ctl_get_instance_id(stream
->ustream
,
2851 &subbuf
->info
.data
.stream_instance_id
.value
);
2853 /* May not be supported by older LTTng-modules. */
2854 if (ret
!= -ENOTTY
) {
2855 PERROR("Failed to get stream instance id");
2859 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2865 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2866 struct stream_subbuffer
*subbuffer
)
2871 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2877 ret
= get_current_subbuf_addr(stream
, &addr
);
2882 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2883 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2884 LTTNG_ASSERT(subbuffer
->buffer
.buffer
.data
!= NULL
);
2889 static enum get_next_subbuffer_status
get_next_subbuffer(
2890 struct lttng_consumer_stream
*stream
,
2891 struct stream_subbuffer
*subbuffer
)
2894 enum get_next_subbuffer_status status
;
2896 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2899 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
2904 * The caller only expects -ENODATA when there is no data to
2905 * read, but the kernel tracer returns -EAGAIN when there is
2906 * currently no data for a non-finalized stream, and -ENODATA
2907 * when there is no data for a finalized stream. Those can be
2908 * combined into a -ENODATA return value.
2910 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2913 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2917 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2919 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2926 static enum get_next_subbuffer_status
get_next_subbuffer_metadata(
2927 struct lttng_consumer_stream
*stream
,
2928 struct stream_subbuffer
*subbuffer
)
2935 unsigned long consumed_pos
, produced_pos
;
2936 enum get_next_subbuffer_status status
;
2939 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
2941 got_subbuffer
= true;
2943 got_subbuffer
= false;
2944 if (ret
!= -EAGAIN
) {
2946 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2952 * Determine if the cache is empty and ensure that a sub-buffer
2953 * is made available if the cache is not empty.
2955 if (!got_subbuffer
) {
2956 ret
= commit_one_metadata_packet(stream
);
2957 if (ret
< 0 && ret
!= -ENOBUFS
) {
2958 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2960 } else if (ret
== 0) {
2961 /* Not an error, the cache is empty. */
2963 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
2966 cache_empty
= false;
2969 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2970 cache_empty
= stream
->chan
->metadata_cache
->contents
.size
==
2971 stream
->ust_metadata_pushed
;
2972 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2974 } while (!got_subbuffer
);
2976 /* Populate sub-buffer infos and view. */
2977 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2979 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2983 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2986 * -EAGAIN is not expected since we got a sub-buffer and haven't
2987 * pushed the consumption position yet (on put_next).
2989 PERROR("Failed to take a snapshot of metadata buffer positions");
2990 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
2994 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2996 PERROR("Failed to get metadata consumed position");
2997 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3001 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
3003 PERROR("Failed to get metadata produced position");
3004 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
3008 /* Last sub-buffer of the ring buffer ? */
3009 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
3012 * The sessiond registry lock ensures that coherent units of metadata
3013 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3014 * acquired, the cache is empty, and it is the only available sub-buffer
3015 * available, it is safe to assume that it is "coherent".
3017 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
3019 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
3020 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
3025 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
3026 struct stream_subbuffer
*subbuffer
__attribute__((unused
)))
3028 const int ret
= lttng_ust_ctl_put_next_subbuf(stream
->ustream
);
3030 LTTNG_ASSERT(ret
== 0);
3034 static int signal_metadata(struct lttng_consumer_stream
*stream
,
3035 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
3037 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
3038 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
3041 static int lttng_ustconsumer_set_stream_ops(
3042 struct lttng_consumer_stream
*stream
)
3046 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
3047 if (stream
->metadata_flag
) {
3048 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3049 get_next_subbuffer_metadata
;
3050 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3051 extract_metadata_subbuffer_info
;
3052 stream
->read_subbuffer_ops
.reset_metadata
=
3053 metadata_stream_reset_cache_consumed_position
;
3054 if (stream
->chan
->is_live
) {
3055 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
3056 ret
= consumer_stream_enable_metadata_bucketization(
3063 stream
->read_subbuffer_ops
.get_next_subbuffer
=
3065 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
3066 extract_data_subbuffer_info
;
3067 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
3068 if (stream
->chan
->is_live
) {
3069 stream
->read_subbuffer_ops
.send_live_beacon
=
3070 consumer_flush_ust_index
;
3074 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
3080 * Called when a stream is created.
3082 * Return 0 on success or else a negative value.
3084 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3088 LTTNG_ASSERT(stream
);
3091 * Don't create anything if this is set for streaming or if there is
3092 * no current trace chunk on the parent channel.
3094 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
3095 stream
->chan
->trace_chunk
) {
3096 ret
= consumer_stream_create_output_files(stream
, true);
3102 lttng_ustconsumer_set_stream_ops(stream
);
3110 * Check if data is still being extracted from the buffers for a specific
3111 * stream. Consumer data lock MUST be acquired before calling this function
3112 * and the stream lock.
3114 * Return 1 if the traced data are still getting read else 0 meaning that the
3115 * data is available for trace viewer reading.
3117 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3121 LTTNG_ASSERT(stream
);
3122 LTTNG_ASSERT(stream
->ustream
);
3123 ASSERT_LOCKED(stream
->lock
);
3125 DBG("UST consumer checking data pending");
3127 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3132 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3133 uint64_t contiguous
, pushed
;
3135 /* Ease our life a bit. */
3136 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3137 contiguous
= stream
->chan
->metadata_cache
->contents
.size
;
3138 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3139 pushed
= stream
->ust_metadata_pushed
;
3142 * We can simply check whether all contiguously available data
3143 * has been pushed to the ring buffer, since the push operation
3144 * is performed within get_next_subbuf(), and because both
3145 * get_next_subbuf() and put_next_subbuf() are issued atomically
3146 * thanks to the stream lock within
3147 * lttng_ustconsumer_read_subbuffer(). This basically means that
3148 * whetnever ust_metadata_pushed is incremented, the associated
3149 * metadata has been consumed from the metadata stream.
3151 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3152 contiguous
, pushed
);
3153 LTTNG_ASSERT(((int64_t) (contiguous
- pushed
)) >= 0);
3154 if ((contiguous
!= pushed
) ||
3155 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3156 ret
= 1; /* Data is pending */
3160 ret
= lttng_ust_ctl_get_next_subbuf(stream
->ustream
);
3163 * There is still data so let's put back this
3166 ret
= lttng_ust_ctl_put_subbuf(stream
->ustream
);
3167 LTTNG_ASSERT(ret
== 0);
3168 ret
= 1; /* Data is pending */
3173 /* Data is NOT pending so ready to be read. */
3181 * Stop a given metadata channel timer if enabled and close the wait fd which
3182 * is the poll pipe of the metadata stream.
3184 * This MUST be called with the metadata channel lock acquired.
3186 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3190 LTTNG_ASSERT(metadata
);
3191 LTTNG_ASSERT(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3193 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3195 if (metadata
->switch_timer_enabled
== 1) {
3196 consumer_timer_switch_stop(metadata
);
3199 if (!metadata
->metadata_stream
) {
3204 * Closing write side so the thread monitoring the stream wakes up if any
3205 * and clean the metadata stream.
3207 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3208 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3210 PERROR("closing metadata pipe write side");
3212 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3220 * Close every metadata stream wait fd of the metadata hash table. This
3221 * function MUST be used very carefully so not to run into a race between the
3222 * metadata thread handling streams and this function closing their wait fd.
3224 * For UST, this is used when the session daemon hangs up. Its the metadata
3225 * producer so calling this is safe because we are assured that no state change
3226 * can occur in the metadata thread for the streams in the hash table.
3228 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3230 struct lttng_ht_iter iter
;
3231 struct lttng_consumer_stream
*stream
;
3233 LTTNG_ASSERT(metadata_ht
);
3234 LTTNG_ASSERT(metadata_ht
->ht
);
3236 DBG("UST consumer closing all metadata streams");
3239 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3242 health_code_update();
3244 pthread_mutex_lock(&stream
->chan
->lock
);
3245 lttng_ustconsumer_close_metadata(stream
->chan
);
3246 pthread_mutex_unlock(&stream
->chan
->lock
);
3252 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3256 ret
= lttng_ust_ctl_stream_close_wakeup_fd(stream
->ustream
);
3258 ERR("Unable to close wakeup fd");
3263 * Please refer to consumer-timer.c before adding any lock within this
3264 * function or any of its callees. Timers have a very strict locking
3265 * semantic with respect to teardown. Failure to respect this semantic
3266 * introduces deadlocks.
3268 * DON'T hold the metadata lock when calling this function, else this
3269 * can cause deadlock involving consumer awaiting for metadata to be
3270 * pushed out due to concurrent interaction with the session daemon.
3272 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3273 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3275 struct lttcomm_metadata_request_msg request
;
3276 struct lttcomm_consumer_msg msg
;
3277 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3278 uint64_t len
, key
, offset
, version
;
3281 LTTNG_ASSERT(channel
);
3282 LTTNG_ASSERT(channel
->metadata_cache
);
3284 memset(&request
, 0, sizeof(request
));
3286 /* send the metadata request to sessiond */
3287 switch (the_consumer_data
.type
) {
3288 case LTTNG_CONSUMER64_UST
:
3289 request
.bits_per_long
= 64;
3291 case LTTNG_CONSUMER32_UST
:
3292 request
.bits_per_long
= 32;
3295 request
.bits_per_long
= 0;
3299 request
.session_id
= channel
->session_id
;
3300 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3302 * Request the application UID here so the metadata of that application can
3303 * be sent back. The channel UID corresponds to the user UID of the session
3304 * used for the rights on the stream file(s).
3306 request
.uid
= channel
->ust_app_uid
;
3307 request
.key
= channel
->key
;
3309 DBG("Sending metadata request to sessiond, session id %" PRIu64
3310 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3311 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3314 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3316 health_code_update();
3318 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3321 ERR("Asking metadata to sessiond");
3325 health_code_update();
3327 /* Receive the metadata from sessiond */
3328 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3330 if (ret
!= sizeof(msg
)) {
3331 DBG("Consumer received unexpected message size %d (expects %zu)",
3333 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3335 * The ret value might 0 meaning an orderly shutdown but this is ok
3336 * since the caller handles this.
3341 health_code_update();
3343 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3344 /* No registry found */
3345 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3349 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3350 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3355 len
= msg
.u
.push_metadata
.len
;
3356 key
= msg
.u
.push_metadata
.key
;
3357 offset
= msg
.u
.push_metadata
.target_offset
;
3358 version
= msg
.u
.push_metadata
.version
;
3360 LTTNG_ASSERT(key
== channel
->key
);
3362 DBG("No new metadata to receive for key %" PRIu64
, key
);
3365 health_code_update();
3367 /* Tell session daemon we are ready to receive the metadata. */
3368 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3369 LTTCOMM_CONSUMERD_SUCCESS
);
3370 if (ret
< 0 || len
== 0) {
3372 * Somehow, the session daemon is not responding anymore or there is
3373 * nothing to receive.
3378 health_code_update();
3380 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3381 key
, offset
, len
, version
, channel
, timer
, wait
);
3384 * Only send the status msg if the sessiond is alive meaning a positive
3387 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3392 health_code_update();
3394 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3399 * Return the ustctl call for the get stream id.
3401 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3402 uint64_t *stream_id
)
3404 LTTNG_ASSERT(stream
);
3405 LTTNG_ASSERT(stream_id
);
3407 return lttng_ust_ctl_get_stream_id(stream
->ustream
, stream_id
);
3410 void lttng_ustconsumer_sigbus_handle(void *addr
)
3412 lttng_ust_ctl_sigbus_handle(addr
);