2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <lttng/ust-ctl.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
32 #include <urcu/list.h>
37 #include <bin/lttng-consumerd/health-consumerd.h>
38 #include <common/common.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/relayd/relayd.h>
41 #include <common/compat/fcntl.h>
42 #include <common/compat/endian.h>
43 #include <common/consumer/consumer-metadata-cache.h>
44 #include <common/consumer/consumer-stream.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/utils.h>
47 #include <common/index/index.h>
48 #include <common/consumer/consumer.h>
50 #include "ust-consumer.h"
52 #define INT_MAX_STR_LEN 12 /* includes \0 */
54 extern struct lttng_consumer_global_data consumer_data
;
55 extern int consumer_poll_timeout
;
56 extern volatile int consumer_quit
;
59 * Free channel object and all streams associated with it. This MUST be used
60 * only and only if the channel has _NEVER_ been added to the global channel
63 static void destroy_channel(struct lttng_consumer_channel
*channel
)
65 struct lttng_consumer_stream
*stream
, *stmp
;
69 DBG("UST consumer cleaning stream list");
71 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
76 cds_list_del(&stream
->send_node
);
77 ustctl_destroy_stream(stream
->ustream
);
82 * If a channel is available meaning that was created before the streams
86 lttng_ustconsumer_del_channel(channel
);
87 lttng_ustconsumer_free_channel(channel
);
93 * Add channel to internal consumer state.
95 * Returns 0 on success or else a negative value.
97 static int add_channel(struct lttng_consumer_channel
*channel
,
98 struct lttng_consumer_local_data
*ctx
)
105 if (ctx
->on_recv_channel
!= NULL
) {
106 ret
= ctx
->on_recv_channel(channel
);
108 ret
= consumer_add_channel(channel
, ctx
);
109 } else if (ret
< 0) {
110 /* Most likely an ENOMEM. */
111 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
115 ret
= consumer_add_channel(channel
, ctx
);
118 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
125 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
126 * error value if applicable is set in it else it is kept untouched.
128 * Return NULL on error else the newly allocated stream object.
130 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
131 struct lttng_consumer_channel
*channel
,
132 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
135 struct lttng_consumer_stream
*stream
= NULL
;
140 stream
= consumer_stream_create(
144 LTTNG_CONSUMER_ACTIVE_STREAM
,
154 if (stream
== NULL
) {
158 * We could not find the channel. Can happen if cpu hotplug
159 * happens while tearing down.
161 DBG3("Could not find channel");
166 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
174 *_alloc_ret
= alloc_ret
;
180 * Send the given stream pointer to the corresponding thread.
182 * Returns 0 on success else a negative value.
184 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
185 struct lttng_consumer_local_data
*ctx
)
188 struct lttng_pipe
*stream_pipe
;
190 /* Get the right pipe where the stream will be sent. */
191 if (stream
->metadata_flag
) {
192 ret
= consumer_add_metadata_stream(stream
);
194 ERR("Consumer add metadata stream %" PRIu64
" failed.",
198 stream_pipe
= ctx
->consumer_metadata_pipe
;
200 ret
= consumer_add_data_stream(stream
);
202 ERR("Consumer add stream %" PRIu64
" failed.",
206 stream_pipe
= ctx
->consumer_data_pipe
;
210 * From this point on, the stream's ownership has been moved away from
211 * the channel and becomes globally visible.
213 stream
->globally_visible
= 1;
215 ret
= lttng_pipe_write(stream_pipe
, &stream
, sizeof(stream
));
217 ERR("Consumer write %s stream to pipe %d",
218 stream
->metadata_flag
? "metadata" : "data",
219 lttng_pipe_get_writefd(stream_pipe
));
220 if (stream
->metadata_flag
) {
221 consumer_del_stream_for_metadata(stream
);
223 consumer_del_stream_for_data(stream
);
231 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
233 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
236 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
237 stream_shm_path
[PATH_MAX
- 1] = '\0';
238 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
243 strncat(stream_shm_path
, cpu_nr
,
244 PATH_MAX
- strlen(stream_shm_path
) - 1);
251 * Create streams for the given channel using liblttng-ust-ctl.
253 * Return 0 on success else a negative value.
255 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
256 struct lttng_consumer_local_data
*ctx
)
259 struct ustctl_consumer_stream
*ustream
;
260 struct lttng_consumer_stream
*stream
;
266 * While a stream is available from ustctl. When NULL is returned, we've
267 * reached the end of the possible stream for the channel.
269 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
271 int ust_metadata_pipe
[2];
273 health_code_update();
275 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
276 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
278 ERR("Create ust metadata poll pipe");
281 wait_fd
= ust_metadata_pipe
[0];
283 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
286 /* Allocate consumer stream object. */
287 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
291 stream
->ustream
= ustream
;
293 * Store it so we can save multiple function calls afterwards since
294 * this value is used heavily in the stream threads. This is UST
295 * specific so this is why it's done after allocation.
297 stream
->wait_fd
= wait_fd
;
300 * Increment channel refcount since the channel reference has now been
301 * assigned in the allocation process above.
303 if (stream
->chan
->monitor
) {
304 uatomic_inc(&stream
->chan
->refcount
);
308 * Order is important this is why a list is used. On error, the caller
309 * should clean this list.
311 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
313 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
314 &stream
->max_sb_size
);
316 ERR("ustctl_get_max_subbuf_size failed for stream %s",
321 /* Do actions once stream has been received. */
322 if (ctx
->on_recv_stream
) {
323 ret
= ctx
->on_recv_stream(stream
);
329 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
330 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
332 /* Set next CPU stream. */
333 channel
->streams
.count
= ++cpu
;
335 /* Keep stream reference when creating metadata. */
336 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
337 channel
->metadata_stream
= stream
;
338 if (channel
->monitor
) {
339 /* Set metadata poll pipe if we created one */
340 memcpy(stream
->ust_metadata_poll_pipe
,
342 sizeof(ust_metadata_pipe
));
355 * create_posix_shm is never called concurrently within a process.
358 int create_posix_shm(void)
360 char tmp_name
[NAME_MAX
];
363 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
369 * Allocate shm, and immediately unlink its shm oject, keeping
370 * only the file descriptor as a reference to the object.
371 * We specifically do _not_ use the / at the beginning of the
372 * pathname so that some OS implementations can keep it local to
373 * the process (POSIX leaves this implementation-defined).
375 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
380 ret
= shm_unlink(tmp_name
);
381 if (ret
< 0 && errno
!= ENOENT
) {
382 PERROR("shm_unlink");
383 goto error_shm_release
;
396 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
,
397 struct ustctl_consumer_channel_attr
*attr
,
400 char shm_path
[PATH_MAX
];
403 if (!channel
->shm_path
[0]) {
404 return create_posix_shm();
406 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
410 return run_as_open(shm_path
,
411 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
412 channel
->uid
, channel
->gid
);
419 * Create an UST channel with the given attributes and send it to the session
420 * daemon using the ust ctl API.
422 * Return 0 on success or else a negative value.
424 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
425 struct ustctl_consumer_channel_attr
*attr
,
426 struct ustctl_consumer_channel
**ust_chanp
)
428 int ret
, nr_stream_fds
, i
, j
;
430 struct ustctl_consumer_channel
*ust_channel
;
436 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
437 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
438 "switch_timer_interval: %u, read_timer_interval: %u, "
439 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
440 attr
->num_subbuf
, attr
->switch_timer_interval
,
441 attr
->read_timer_interval
, attr
->output
, attr
->type
);
443 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
446 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
447 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
452 for (i
= 0; i
< nr_stream_fds
; i
++) {
453 stream_fds
[i
] = open_ust_stream_fd(channel
, attr
, i
);
454 if (stream_fds
[i
] < 0) {
459 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
464 channel
->nr_stream_fds
= nr_stream_fds
;
465 channel
->stream_fds
= stream_fds
;
466 *ust_chanp
= ust_channel
;
472 for (j
= i
- 1; j
>= 0; j
--) {
475 closeret
= close(stream_fds
[j
]);
479 if (channel
->shm_path
[0]) {
480 char shm_path
[PATH_MAX
];
482 closeret
= get_stream_shm_path(shm_path
,
483 channel
->shm_path
, j
);
485 ERR("Cannot get stream shm path");
487 closeret
= run_as_unlink(shm_path
,
488 channel
->uid
, channel
->gid
);
490 PERROR("unlink %s", shm_path
);
494 /* Try to rmdir all directories under shm_path root. */
495 if (channel
->root_shm_path
[0]) {
496 (void) run_as_recursive_rmdir(channel
->root_shm_path
,
497 channel
->uid
, channel
->gid
);
505 * Send a single given stream to the session daemon using the sock.
507 * Return 0 on success else a negative value.
509 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
516 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
518 /* Send stream to session daemon. */
519 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
529 * Send channel to sessiond.
531 * Return 0 on success or else a negative value.
533 static int send_sessiond_channel(int sock
,
534 struct lttng_consumer_channel
*channel
,
535 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
537 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
538 struct lttng_consumer_stream
*stream
;
539 uint64_t relayd_id
= -1ULL;
545 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
547 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
548 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
550 health_code_update();
552 /* Try to send the stream to the relayd if one is available. */
553 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
556 * Flag that the relayd was the problem here probably due to a
557 * communicaton error on the socket.
562 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
564 if (relayd_id
== -1ULL) {
565 relayd_id
= stream
->relayd_id
;
570 /* Inform sessiond that we are about to send channel and streams. */
571 ret
= consumer_send_status_msg(sock
, ret_code
);
572 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
574 * Either the session daemon is not responding or the relayd died so we
580 /* Send channel to sessiond. */
581 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
586 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
591 /* The channel was sent successfully to the sessiond at this point. */
592 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
594 health_code_update();
596 /* Send stream to session daemon. */
597 ret
= send_sessiond_stream(sock
, stream
);
603 /* Tell sessiond there is no more stream. */
604 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
609 DBG("UST consumer NULL stream sent to sessiond");
614 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
621 * Creates a channel and streams and add the channel it to the channel internal
622 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
625 * Return 0 on success or else, a negative value is returned and the channel
626 * MUST be destroyed by consumer_del_channel().
628 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
629 struct lttng_consumer_channel
*channel
,
630 struct ustctl_consumer_channel_attr
*attr
)
639 * This value is still used by the kernel consumer since for the kernel,
640 * the stream ownership is not IN the consumer so we need to have the
641 * number of left stream that needs to be initialized so we can know when
642 * to delete the channel (see consumer.c).
644 * As for the user space tracer now, the consumer creates and sends the
645 * stream to the session daemon which only sends them to the application
646 * once every stream of a channel is received making this value useless
647 * because we they will be added to the poll thread before the application
648 * receives them. This ensures that a stream can not hang up during
649 * initilization of a channel.
651 channel
->nb_init_stream_left
= 0;
653 /* The reply msg status is handled in the following call. */
654 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
659 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
662 * For the snapshots (no monitor), we create the metadata streams
663 * on demand, not during the channel creation.
665 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
670 /* Open all streams for this channel. */
671 ret
= create_ust_streams(channel
, ctx
);
681 * Send all stream of a channel to the right thread handling it.
683 * On error, return a negative value else 0 on success.
685 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
686 struct lttng_consumer_local_data
*ctx
)
689 struct lttng_consumer_stream
*stream
, *stmp
;
694 /* Send streams to the corresponding thread. */
695 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
698 health_code_update();
700 /* Sending the stream to the thread. */
701 ret
= send_stream_to_thread(stream
, ctx
);
704 * If we are unable to send the stream to the thread, there is
705 * a big problem so just stop everything.
707 /* Remove node from the channel stream list. */
708 cds_list_del(&stream
->send_node
);
712 /* Remove node from the channel stream list. */
713 cds_list_del(&stream
->send_node
);
722 * Flush channel's streams using the given key to retrieve the channel.
724 * Return 0 on success else an LTTng error code.
726 static int flush_channel(uint64_t chan_key
)
729 struct lttng_consumer_channel
*channel
;
730 struct lttng_consumer_stream
*stream
;
732 struct lttng_ht_iter iter
;
734 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
737 channel
= consumer_find_channel(chan_key
);
739 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
740 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
744 ht
= consumer_data
.stream_per_chan_id_ht
;
746 /* For each stream of the channel id, flush it. */
747 cds_lfht_for_each_entry_duplicate(ht
->ht
,
748 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
749 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
751 health_code_update();
753 pthread_mutex_lock(&stream
->lock
);
756 * Protect against concurrent teardown of a stream.
758 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
762 if (!stream
->quiescent
) {
763 ustctl_flush_buffer(stream
->ustream
, 0);
764 stream
->quiescent
= true;
767 pthread_mutex_unlock(&stream
->lock
);
775 * Clear quiescent state from channel's streams using the given key to
776 * retrieve the channel.
778 * Return 0 on success else an LTTng error code.
780 static int clear_quiescent_channel(uint64_t chan_key
)
783 struct lttng_consumer_channel
*channel
;
784 struct lttng_consumer_stream
*stream
;
786 struct lttng_ht_iter iter
;
788 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
791 channel
= consumer_find_channel(chan_key
);
793 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
794 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
798 ht
= consumer_data
.stream_per_chan_id_ht
;
800 /* For each stream of the channel id, clear quiescent state. */
801 cds_lfht_for_each_entry_duplicate(ht
->ht
,
802 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
803 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
805 health_code_update();
807 pthread_mutex_lock(&stream
->lock
);
808 stream
->quiescent
= false;
809 pthread_mutex_unlock(&stream
->lock
);
817 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
818 * RCU read side lock MUST be acquired before calling this function.
820 * Return 0 on success else an LTTng error code.
822 static int close_metadata(uint64_t chan_key
)
825 struct lttng_consumer_channel
*channel
;
826 unsigned int channel_monitor
;
828 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
830 channel
= consumer_find_channel(chan_key
);
833 * This is possible if the metadata thread has issue a delete because
834 * the endpoint point of the stream hung up. There is no way the
835 * session daemon can know about it thus use a DBG instead of an actual
838 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
839 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
843 pthread_mutex_lock(&consumer_data
.lock
);
844 pthread_mutex_lock(&channel
->lock
);
845 channel_monitor
= channel
->monitor
;
846 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
850 lttng_ustconsumer_close_metadata(channel
);
851 pthread_mutex_unlock(&channel
->lock
);
852 pthread_mutex_unlock(&consumer_data
.lock
);
855 * The ownership of a metadata channel depends on the type of
856 * session to which it belongs. In effect, the monitor flag is checked
857 * to determine if this metadata channel is in "snapshot" mode or not.
859 * In the non-snapshot case, the metadata channel is created along with
860 * a single stream which will remain present until the metadata channel
861 * is destroyed (on the destruction of its session). In this case, the
862 * metadata stream in "monitored" by the metadata poll thread and holds
863 * the ownership of its channel.
865 * Closing the metadata will cause the metadata stream's "metadata poll
866 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
867 * thread which will teardown the metadata stream which, in return,
868 * deletes the metadata channel.
870 * In the snapshot case, the metadata stream is created and destroyed
871 * on every snapshot record. Since the channel doesn't have an owner
872 * other than the session daemon, it is safe to destroy it immediately
873 * on reception of the CLOSE_METADATA command.
875 if (!channel_monitor
) {
877 * The channel and consumer_data locks must be
878 * released before this call since consumer_del_channel
879 * re-acquires the channel and consumer_data locks to teardown
880 * the channel and queue its reclamation by the "call_rcu"
883 consumer_del_channel(channel
);
888 pthread_mutex_unlock(&channel
->lock
);
889 pthread_mutex_unlock(&consumer_data
.lock
);
895 * RCU read side lock MUST be acquired before calling this function.
897 * Return 0 on success else an LTTng error code.
899 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
902 struct lttng_consumer_channel
*metadata
;
904 DBG("UST consumer setup metadata key %" PRIu64
, key
);
906 metadata
= consumer_find_channel(key
);
908 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
909 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
914 * In no monitor mode, the metadata channel has no stream(s) so skip the
915 * ownership transfer to the metadata thread.
917 if (!metadata
->monitor
) {
918 DBG("Metadata channel in no monitor");
924 * Send metadata stream to relayd if one available. Availability is
925 * known if the stream is still in the list of the channel.
927 if (cds_list_empty(&metadata
->streams
.head
)) {
928 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
929 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
930 goto error_no_stream
;
933 /* Send metadata stream to relayd if needed. */
934 if (metadata
->metadata_stream
->relayd_id
!= (uint64_t) -1ULL) {
935 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
938 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
941 ret
= consumer_send_relayd_streams_sent(
942 metadata
->metadata_stream
->relayd_id
);
944 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
949 ret
= send_streams_to_thread(metadata
, ctx
);
952 * If we are unable to send the stream to the thread, there is
953 * a big problem so just stop everything.
955 ret
= LTTCOMM_CONSUMERD_FATAL
;
958 /* List MUST be empty after or else it could be reused. */
959 assert(cds_list_empty(&metadata
->streams
.head
));
966 * Delete metadata channel on error. At this point, the metadata stream can
967 * NOT be monitored by the metadata thread thus having the guarantee that
968 * the stream is still in the local stream list of the channel. This call
969 * will make sure to clean that list.
971 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
972 cds_list_del(&metadata
->metadata_stream
->send_node
);
973 metadata
->metadata_stream
= NULL
;
980 * Snapshot the whole metadata.
982 * Returns 0 on success, < 0 on error
984 static int snapshot_metadata(uint64_t key
, char *path
, uint64_t relayd_id
,
985 struct lttng_consumer_local_data
*ctx
)
988 struct lttng_consumer_channel
*metadata_channel
;
989 struct lttng_consumer_stream
*metadata_stream
;
994 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
999 metadata_channel
= consumer_find_channel(key
);
1000 if (!metadata_channel
) {
1001 ERR("UST snapshot metadata channel not found for key %" PRIu64
,
1006 assert(!metadata_channel
->monitor
);
1008 health_code_update();
1011 * Ask the sessiond if we have new metadata waiting and update the
1012 * consumer metadata cache.
1014 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1019 health_code_update();
1022 * The metadata stream is NOT created in no monitor mode when the channel
1023 * is created on a sessiond ask channel command.
1025 ret
= create_ust_streams(metadata_channel
, ctx
);
1030 metadata_stream
= metadata_channel
->metadata_stream
;
1031 assert(metadata_stream
);
1033 if (relayd_id
!= (uint64_t) -1ULL) {
1034 metadata_stream
->relayd_id
= relayd_id
;
1035 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1040 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
1041 metadata_stream
->chan
->tracefile_size
,
1042 metadata_stream
->tracefile_count_current
,
1043 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
1047 metadata_stream
->out_fd
= ret
;
1048 metadata_stream
->tracefile_size_current
= 0;
1052 health_code_update();
1054 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1062 * Clean up the stream completly because the next snapshot will use a new
1065 consumer_stream_destroy(metadata_stream
, NULL
);
1066 cds_list_del(&metadata_stream
->send_node
);
1067 metadata_channel
->metadata_stream
= NULL
;
1075 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1079 unsigned long mmap_offset
;
1080 const char *mmap_base
;
1082 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1084 ERR("Failed to get mmap base for stream `%s`",
1090 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1092 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1097 *addr
= mmap_base
+ mmap_offset
;
1104 * Take a snapshot of all the stream of a channel.
1106 * Returns 0 on success, < 0 on error
1108 static int snapshot_channel(uint64_t key
, char *path
, uint64_t relayd_id
,
1109 uint64_t nb_packets_per_stream
, struct lttng_consumer_local_data
*ctx
)
1112 unsigned use_relayd
= 0;
1113 unsigned long consumed_pos
, produced_pos
;
1114 struct lttng_consumer_channel
*channel
;
1115 struct lttng_consumer_stream
*stream
;
1122 if (relayd_id
!= (uint64_t) -1ULL) {
1126 channel
= consumer_find_channel(key
);
1128 ERR("UST snapshot channel not found for key %" PRIu64
, key
);
1132 assert(!channel
->monitor
);
1133 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1135 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1136 health_code_update();
1138 /* Lock stream because we are about to change its state. */
1139 pthread_mutex_lock(&stream
->lock
);
1140 stream
->relayd_id
= relayd_id
;
1143 ret
= consumer_send_relayd_stream(stream
, path
);
1148 ret
= utils_create_stream_file(path
, stream
->name
,
1149 stream
->chan
->tracefile_size
,
1150 stream
->tracefile_count_current
,
1151 stream
->uid
, stream
->gid
, NULL
);
1155 stream
->out_fd
= ret
;
1156 stream
->tracefile_size_current
= 0;
1158 DBG("UST consumer snapshot stream %s/%s (%" PRIu64
")", path
,
1159 stream
->name
, stream
->key
);
1161 if (relayd_id
!= -1ULL) {
1162 ret
= consumer_send_relayd_streams_sent(relayd_id
);
1169 * If tracing is active, we want to perform a "full" buffer flush.
1170 * Else, if quiescent, it has already been done by the prior stop.
1172 if (!stream
->quiescent
) {
1173 ustctl_flush_buffer(stream
->ustream
, 0);
1176 ret
= lttng_ustconsumer_take_snapshot(stream
);
1178 ERR("Taking UST snapshot");
1182 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1184 ERR("Produced UST snapshot position");
1188 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1190 ERR("Consumerd UST snapshot position");
1195 * The original value is sent back if max stream size is larger than
1196 * the possible size of the snapshot. Also, we assume that the session
1197 * daemon should never send a maximum stream size that is lower than
1200 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1201 produced_pos
, nb_packets_per_stream
,
1202 stream
->max_sb_size
);
1204 while (consumed_pos
< produced_pos
) {
1206 unsigned long len
, padded_len
;
1207 const char *subbuf_addr
;
1208 struct lttng_buffer_view subbuf_view
;
1210 health_code_update();
1212 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1214 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1216 if (ret
!= -EAGAIN
) {
1217 PERROR("ustctl_get_subbuf snapshot");
1218 goto error_close_stream
;
1220 DBG("UST consumer get subbuf failed. Skipping it.");
1221 consumed_pos
+= stream
->max_sb_size
;
1222 stream
->chan
->lost_packets
++;
1226 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1228 ERR("Snapshot ustctl_get_subbuf_size");
1229 goto error_put_subbuf
;
1232 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1234 ERR("Snapshot ustctl_get_padded_subbuf_size");
1235 goto error_put_subbuf
;
1238 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1240 goto error_put_subbuf
;
1243 subbuf_view
= lttng_buffer_view_init(
1244 subbuf_addr
, 0, padded_len
);
1245 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
1246 stream
, &subbuf_view
, padded_len
- len
);
1248 if (read_len
!= len
) {
1250 goto error_put_subbuf
;
1253 if (read_len
!= padded_len
) {
1255 goto error_put_subbuf
;
1259 ret
= ustctl_put_subbuf(stream
->ustream
);
1261 ERR("Snapshot ustctl_put_subbuf");
1262 goto error_close_stream
;
1264 consumed_pos
+= stream
->max_sb_size
;
1267 /* Simply close the stream so we can use it on the next snapshot. */
1268 consumer_stream_close(stream
);
1269 pthread_mutex_unlock(&stream
->lock
);
1276 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1277 ERR("Snapshot ustctl_put_subbuf");
1280 consumer_stream_close(stream
);
1282 pthread_mutex_unlock(&stream
->lock
);
1289 * Receive the metadata updates from the sessiond. Supports receiving
1290 * overlapping metadata, but is needs to always belong to a contiguous
1291 * range starting from 0.
1292 * Be careful about the locks held when calling this function: it needs
1293 * the metadata cache flush to concurrently progress in order to
1296 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1297 uint64_t len
, uint64_t version
,
1298 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1300 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1303 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1305 metadata_str
= zmalloc(len
* sizeof(char));
1306 if (!metadata_str
) {
1307 PERROR("zmalloc metadata string");
1308 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1312 health_code_update();
1314 /* Receive metadata string. */
1315 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1317 /* Session daemon is dead so return gracefully. */
1322 health_code_update();
1324 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1325 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1328 /* Unable to handle metadata. Notify session daemon. */
1329 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1331 * Skip metadata flush on write error since the offset and len might
1332 * not have been updated which could create an infinite loop below when
1333 * waiting for the metadata cache to be flushed.
1335 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1338 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1343 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1344 DBG("Waiting for metadata to be flushed");
1346 health_code_update();
1348 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1358 * Receive command from session daemon and process it.
1360 * Return 1 on success else a negative value or 0.
1362 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1363 int sock
, struct pollfd
*consumer_sockpoll
)
1366 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1367 struct lttcomm_consumer_msg msg
;
1368 struct lttng_consumer_channel
*channel
= NULL
;
1370 health_code_update();
1372 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1373 if (ret
!= sizeof(msg
)) {
1374 DBG("Consumer received unexpected message size %zd (expects %zu)",
1377 * The ret value might 0 meaning an orderly shutdown but this is ok
1378 * since the caller handles this.
1381 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1387 health_code_update();
1390 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 /* Session daemon status message are handled in the following call. */
1401 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1402 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1403 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1404 msg
.u
.relayd_sock
.relayd_session_id
);
1407 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1409 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1410 struct consumer_relayd_sock_pair
*relayd
;
1412 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1414 /* Get relayd reference if exists. */
1415 relayd
= consumer_find_relayd(index
);
1416 if (relayd
== NULL
) {
1417 DBG("Unable to find relayd %" PRIu64
, index
);
1418 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1422 * Each relayd socket pair has a refcount of stream attached to it
1423 * which tells if the relayd is still active or not depending on the
1426 * This will set the destroy flag of the relayd object and destroy it
1427 * if the refcount reaches zero when called.
1429 * The destroy can happen either here or when a stream fd hangs up.
1432 consumer_flag_relayd_for_destroy(relayd
);
1435 goto end_msg_sessiond
;
1437 case LTTNG_CONSUMER_UPDATE_STREAM
:
1442 case LTTNG_CONSUMER_DATA_PENDING
:
1444 int ret
, 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
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1453 sizeof(is_data_pending
));
1455 DBG("Error when sending the data pending ret code: %d", ret
);
1460 * No need to send back a status message since the data pending
1461 * returned value is the response.
1465 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1468 struct ustctl_consumer_channel_attr attr
;
1470 /* Create a plain object and reserve a channel key. */
1471 channel
= consumer_allocate_channel(
1472 msg
.u
.ask_channel
.key
,
1473 msg
.u
.ask_channel
.session_id
,
1474 msg
.u
.ask_channel
.pathname
,
1475 msg
.u
.ask_channel
.name
,
1476 msg
.u
.ask_channel
.uid
,
1477 msg
.u
.ask_channel
.gid
,
1478 msg
.u
.ask_channel
.relayd_id
,
1479 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1480 msg
.u
.ask_channel
.tracefile_size
,
1481 msg
.u
.ask_channel
.tracefile_count
,
1482 msg
.u
.ask_channel
.session_id_per_pid
,
1483 msg
.u
.ask_channel
.monitor
,
1484 msg
.u
.ask_channel
.live_timer_interval
,
1485 msg
.u
.ask_channel
.is_live
,
1486 msg
.u
.ask_channel
.root_shm_path
,
1487 msg
.u
.ask_channel
.shm_path
);
1489 goto end_channel_error
;
1493 * Assign UST application UID to the channel. This value is ignored for
1494 * per PID buffers. This is specific to UST thus setting this after the
1497 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1499 /* Build channel attributes from received message. */
1500 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1501 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1502 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1503 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1504 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1505 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1506 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1508 /* Match channel buffer type to the UST abi. */
1509 switch (msg
.u
.ask_channel
.output
) {
1510 case LTTNG_EVENT_MMAP
:
1512 attr
.output
= LTTNG_UST_MMAP
;
1516 /* Translate and save channel type. */
1517 switch (msg
.u
.ask_channel
.type
) {
1518 case LTTNG_UST_CHAN_PER_CPU
:
1519 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1520 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1522 * Set refcount to 1 for owner. Below, we will
1523 * pass ownership to the
1524 * consumer_thread_channel_poll() thread.
1526 channel
->refcount
= 1;
1528 case LTTNG_UST_CHAN_METADATA
:
1529 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1530 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1537 health_code_update();
1539 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
1541 goto end_channel_error
;
1544 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1545 ret
= consumer_metadata_cache_allocate(channel
);
1547 ERR("Allocating metadata cache");
1548 goto end_channel_error
;
1550 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1551 attr
.switch_timer_interval
= 0;
1553 consumer_timer_live_start(channel
,
1554 msg
.u
.ask_channel
.live_timer_interval
);
1557 health_code_update();
1560 * Add the channel to the internal state AFTER all streams were created
1561 * and successfully sent to session daemon. This way, all streams must
1562 * be ready before this channel is visible to the threads.
1563 * If add_channel succeeds, ownership of the channel is
1564 * passed to consumer_thread_channel_poll().
1566 ret
= add_channel(channel
, ctx
);
1568 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1569 if (channel
->switch_timer_enabled
== 1) {
1570 consumer_timer_switch_stop(channel
);
1572 consumer_metadata_cache_destroy(channel
);
1574 if (channel
->live_timer_enabled
== 1) {
1575 consumer_timer_live_stop(channel
);
1577 goto end_channel_error
;
1580 health_code_update();
1583 * Channel and streams are now created. Inform the session daemon that
1584 * everything went well and should wait to receive the channel and
1585 * streams with ustctl API.
1587 ret
= consumer_send_status_channel(sock
, channel
);
1590 * There is probably a problem on the socket.
1597 case LTTNG_CONSUMER_GET_CHANNEL
:
1599 int ret
, relayd_err
= 0;
1600 uint64_t key
= msg
.u
.get_channel
.key
;
1601 struct lttng_consumer_channel
*channel
;
1603 channel
= consumer_find_channel(key
);
1605 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1606 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1607 goto end_msg_sessiond
;
1610 health_code_update();
1612 /* Send everything to sessiond. */
1613 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
1617 * We were unable to send to the relayd the stream so avoid
1618 * sending back a fatal error to the thread since this is OK
1619 * and the consumer can continue its work. The above call
1620 * has sent the error status message to the sessiond.
1625 * The communicaton was broken hence there is a bad state between
1626 * the consumer and sessiond so stop everything.
1631 health_code_update();
1634 * In no monitor mode, the streams ownership is kept inside the channel
1635 * so don't send them to the data thread.
1637 if (!channel
->monitor
) {
1638 goto end_msg_sessiond
;
1641 ret
= send_streams_to_thread(channel
, ctx
);
1644 * If we are unable to send the stream to the thread, there is
1645 * a big problem so just stop everything.
1649 /* List MUST be empty after or else it could be reused. */
1650 assert(cds_list_empty(&channel
->streams
.head
));
1651 goto end_msg_sessiond
;
1653 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1655 uint64_t key
= msg
.u
.destroy_channel
.key
;
1658 * Only called if streams have not been sent to stream
1659 * manager thread. However, channel has been sent to
1660 * channel manager thread.
1662 notify_thread_del_channel(ctx
, key
);
1663 goto end_msg_sessiond
;
1665 case LTTNG_CONSUMER_CLOSE_METADATA
:
1669 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1674 goto end_msg_sessiond
;
1676 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1680 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1685 goto end_msg_sessiond
;
1687 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1691 ret
= clear_quiescent_channel(
1692 msg
.u
.clear_quiescent_channel
.key
);
1697 goto end_msg_sessiond
;
1699 case LTTNG_CONSUMER_PUSH_METADATA
:
1702 uint64_t len
= msg
.u
.push_metadata
.len
;
1703 uint64_t key
= msg
.u
.push_metadata
.key
;
1704 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1705 uint64_t version
= msg
.u
.push_metadata
.version
;
1706 struct lttng_consumer_channel
*channel
;
1708 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1711 channel
= consumer_find_channel(key
);
1714 * This is possible if the metadata creation on the consumer side
1715 * is in flight vis-a-vis a concurrent push metadata from the
1716 * session daemon. Simply return that the channel failed and the
1717 * session daemon will handle that message correctly considering
1718 * that this race is acceptable thus the DBG() statement here.
1720 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1721 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1722 goto end_msg_sessiond
;
1725 health_code_update();
1729 * There is nothing to receive. We have simply
1730 * checked whether the channel can be found.
1732 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1733 goto end_msg_sessiond
;
1736 /* Tell session daemon we are ready to receive the metadata. */
1737 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1739 /* Somehow, the session daemon is not responding anymore. */
1743 health_code_update();
1745 /* Wait for more data. */
1746 health_poll_entry();
1747 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1753 health_code_update();
1755 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1756 len
, version
, channel
, 0, 1);
1758 /* error receiving from sessiond */
1762 goto end_msg_sessiond
;
1765 case LTTNG_CONSUMER_SETUP_METADATA
:
1769 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1773 goto end_msg_sessiond
;
1775 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1777 if (msg
.u
.snapshot_channel
.metadata
) {
1778 ret
= snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1779 msg
.u
.snapshot_channel
.pathname
,
1780 msg
.u
.snapshot_channel
.relayd_id
,
1783 ERR("Snapshot metadata failed");
1784 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1787 ret
= snapshot_channel(msg
.u
.snapshot_channel
.key
,
1788 msg
.u
.snapshot_channel
.pathname
,
1789 msg
.u
.snapshot_channel
.relayd_id
,
1790 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1793 ERR("Snapshot channel failed");
1794 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1798 health_code_update();
1799 ret
= consumer_send_status_msg(sock
, ret_code
);
1801 /* Somehow, the session daemon is not responding anymore. */
1804 health_code_update();
1807 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1810 uint64_t discarded_events
;
1811 struct lttng_ht_iter iter
;
1812 struct lttng_ht
*ht
;
1813 struct lttng_consumer_stream
*stream
;
1814 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1815 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1817 DBG("UST consumer discarded events command for session id %"
1820 pthread_mutex_lock(&consumer_data
.lock
);
1822 ht
= consumer_data
.stream_list_ht
;
1825 * We only need a reference to the channel, but they are not
1826 * directly indexed, so we just use the first matching stream
1827 * to extract the information we need, we default to 0 if not
1828 * found (no events are dropped if the channel is not yet in
1831 discarded_events
= 0;
1832 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1833 ht
->hash_fct(&id
, lttng_ht_seed
),
1835 &iter
.iter
, stream
, node_session_id
.node
) {
1836 if (stream
->chan
->key
== key
) {
1837 discarded_events
= stream
->chan
->discarded_events
;
1841 pthread_mutex_unlock(&consumer_data
.lock
);
1844 DBG("UST consumer discarded events command for session id %"
1845 PRIu64
", channel key %" PRIu64
, id
, key
);
1847 health_code_update();
1849 /* Send back returned value to session daemon */
1850 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1852 PERROR("send discarded events");
1858 case LTTNG_CONSUMER_LOST_PACKETS
:
1861 uint64_t lost_packets
;
1862 struct lttng_ht_iter iter
;
1863 struct lttng_ht
*ht
;
1864 struct lttng_consumer_stream
*stream
;
1865 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1866 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1868 DBG("UST consumer lost packets command for session id %"
1871 pthread_mutex_lock(&consumer_data
.lock
);
1873 ht
= 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 packets lost if the channel is not yet in use).
1882 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1883 ht
->hash_fct(&id
, lttng_ht_seed
),
1885 &iter
.iter
, stream
, node_session_id
.node
) {
1886 if (stream
->chan
->key
== key
) {
1887 lost_packets
= stream
->chan
->lost_packets
;
1891 pthread_mutex_unlock(&consumer_data
.lock
);
1894 DBG("UST consumer lost packets command for session id %"
1895 PRIu64
", channel key %" PRIu64
, id
, key
);
1897 health_code_update();
1899 /* Send back returned value to session daemon */
1900 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1901 sizeof(lost_packets
));
1903 PERROR("send lost packets");
1916 health_code_update();
1919 * Return 1 to indicate success since the 0 value can be a socket
1920 * shutdown during the recv() or send() call.
1926 * The returned value here is not useful since either way we'll return 1 to
1927 * the caller because the session daemon socket management is done
1928 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1930 ret
= consumer_send_status_msg(sock
, ret_code
);
1936 health_code_update();
1942 * Free channel here since no one has a reference to it. We don't
1943 * free after that because a stream can store this pointer.
1945 destroy_channel(channel
);
1947 /* We have to send a status channel message indicating an error. */
1948 ret
= consumer_send_status_channel(sock
, NULL
);
1950 /* Stop everything if session daemon can not be notified. */
1955 health_code_update();
1960 /* This will issue a consumer stop. */
1964 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
1965 int producer_active
)
1968 assert(stream
->ustream
);
1970 ustctl_flush_buffer(stream
->ustream
, producer_active
);
1974 * Take a snapshot for a specific fd
1976 * Returns 0 on success, < 0 on error
1978 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1981 assert(stream
->ustream
);
1983 return ustctl_snapshot(stream
->ustream
);
1987 * Get the produced position
1989 * Returns 0 on success, < 0 on error
1991 int lttng_ustconsumer_get_produced_snapshot(
1992 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1995 assert(stream
->ustream
);
1998 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2002 * Get the consumed position
2004 * Returns 0 on success, < 0 on error
2006 int lttng_ustconsumer_get_consumed_snapshot(
2007 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2010 assert(stream
->ustream
);
2013 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2016 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2020 assert(stream
->ustream
);
2022 ustctl_flush_buffer(stream
->ustream
, producer
);
2025 int lttng_ustconsumer_get_current_timestamp(
2026 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2029 assert(stream
->ustream
);
2032 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2035 int lttng_ustconsumer_get_sequence_number(
2036 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2039 assert(stream
->ustream
);
2042 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2046 * Called when the stream signals the consumer that it has hung up.
2048 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2051 assert(stream
->ustream
);
2053 pthread_mutex_lock(&stream
->lock
);
2054 if (!stream
->quiescent
) {
2055 ustctl_flush_buffer(stream
->ustream
, 0);
2056 stream
->quiescent
= true;
2058 pthread_mutex_unlock(&stream
->lock
);
2059 stream
->hangup_flush_done
= 1;
2062 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2067 assert(chan
->uchan
);
2069 if (chan
->switch_timer_enabled
== 1) {
2070 consumer_timer_switch_stop(chan
);
2072 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2075 ret
= close(chan
->stream_fds
[i
]);
2079 if (chan
->shm_path
[0]) {
2080 char shm_path
[PATH_MAX
];
2082 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2084 ERR("Cannot get stream shm path");
2086 ret
= run_as_unlink(shm_path
, chan
->uid
, chan
->gid
);
2088 PERROR("unlink %s", shm_path
);
2094 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2097 assert(chan
->uchan
);
2099 consumer_metadata_cache_destroy(chan
);
2100 ustctl_destroy_channel(chan
->uchan
);
2101 /* Try to rmdir all directories under shm_path root. */
2102 if (chan
->root_shm_path
[0]) {
2103 (void) run_as_recursive_rmdir(chan
->root_shm_path
,
2104 chan
->uid
, chan
->gid
);
2106 free(chan
->stream_fds
);
2109 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2112 assert(stream
->ustream
);
2114 if (stream
->chan
->switch_timer_enabled
== 1) {
2115 consumer_timer_switch_stop(stream
->chan
);
2117 ustctl_destroy_stream(stream
->ustream
);
2120 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2123 assert(stream
->ustream
);
2125 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2128 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2131 assert(stream
->ustream
);
2133 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2137 void metadata_stream_reset_cache(struct lttng_consumer_stream
*stream
)
2139 DBG("Reset metadata cache of session %" PRIu64
,
2140 stream
->chan
->session_id
);
2141 stream
->ust_metadata_pushed
= 0;
2142 stream
->metadata_version
= stream
->chan
->metadata_cache
->version
;
2143 stream
->reset_metadata_flag
= 1;
2147 * Write up to one packet from the metadata cache to the channel.
2149 * Returns the number of bytes pushed in the cache, or a negative value
2153 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2158 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2159 if (stream
->chan
->metadata_cache
->max_offset
2160 == stream
->ust_metadata_pushed
) {
2165 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2166 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2167 stream
->chan
->metadata_cache
->max_offset
2168 - stream
->ust_metadata_pushed
);
2169 assert(write_len
!= 0);
2170 if (write_len
< 0) {
2171 ERR("Writing one metadata packet");
2175 stream
->ust_metadata_pushed
+= write_len
;
2177 assert(stream
->chan
->metadata_cache
->max_offset
>=
2178 stream
->ust_metadata_pushed
);
2182 * Switch packet (but don't open the next one) on every commit of
2183 * a metadata packet. Since the subbuffer is fully filled (with padding,
2184 * if needed), the stream is "quiescent" after this commit.
2186 ustctl_flush_buffer(stream
->ustream
, 1);
2188 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2194 * Sync metadata meaning request them to the session daemon and snapshot to the
2195 * metadata thread can consumer them.
2197 * Metadata stream lock is held here, but we need to release it when
2198 * interacting with sessiond, else we cause a deadlock with live
2199 * awaiting on metadata to be pushed out.
2201 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
2202 * is empty or a negative value on error.
2204 int lttng_ustconsumer_sync_metadata(struct lttng_consumer_local_data
*ctx
,
2205 struct lttng_consumer_stream
*metadata
)
2213 pthread_mutex_unlock(&metadata
->lock
);
2215 * Request metadata from the sessiond, but don't wait for the flush
2216 * because we locked the metadata thread.
2218 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata
->chan
, 0, 0);
2219 pthread_mutex_lock(&metadata
->lock
);
2224 ret
= commit_one_metadata_packet(metadata
);
2227 } else if (ret
> 0) {
2231 ustctl_flush_buffer(metadata
->ustream
, 1);
2232 ret
= ustctl_snapshot(metadata
->ustream
);
2234 if (errno
!= EAGAIN
) {
2235 ERR("Sync metadata, taking UST snapshot");
2238 DBG("No new metadata when syncing them.");
2239 /* No new metadata, exit. */
2245 * After this flush, we still need to extract metadata.
2256 * Return 0 on success else a negative value.
2258 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2259 struct lttng_consumer_local_data
*ctx
)
2262 struct ustctl_consumer_stream
*ustream
;
2267 ustream
= stream
->ustream
;
2270 * First, we are going to check if there is a new subbuffer available
2271 * before reading the stream wait_fd.
2273 /* Get the next subbuffer */
2274 ret
= ustctl_get_next_subbuf(ustream
);
2276 /* No more data found, flag the stream. */
2277 stream
->has_data
= 0;
2282 ret
= ustctl_put_subbuf(ustream
);
2285 /* This stream still has data. Flag it and wake up the data thread. */
2286 stream
->has_data
= 1;
2288 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2291 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2292 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2297 /* The wake up pipe has been notified. */
2298 ctx
->has_wakeup
= 1;
2306 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2311 * We can consume the 1 byte written into the wait_fd by
2312 * UST. Don't trigger error if we cannot read this one byte
2313 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2315 * This is only done when the stream is monitored by a thread,
2316 * before the flush is done after a hangup and if the stream
2317 * is not flagged with data since there might be nothing to
2318 * consume in the wait fd but still have data available
2319 * flagged by the consumer wake up pipe.
2321 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2325 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2326 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2334 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2335 struct stream_subbuffer
*subbuf
)
2339 ret
= ustctl_get_subbuf_size(
2340 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2345 ret
= ustctl_get_padded_subbuf_size(
2346 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2355 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2356 struct stream_subbuffer
*subbuf
)
2360 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2365 subbuf
->info
.metadata
.version
= stream
->chan
->metadata_cache
->version
;
2371 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2372 struct stream_subbuffer
*subbuf
)
2376 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2381 ret
= ustctl_get_packet_size(
2382 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2384 PERROR("Failed to get sub-buffer packet size");
2388 ret
= ustctl_get_content_size(
2389 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2391 PERROR("Failed to get sub-buffer content size");
2395 ret
= ustctl_get_timestamp_begin(
2396 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2398 PERROR("Failed to get sub-buffer begin timestamp");
2402 ret
= ustctl_get_timestamp_end(
2403 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2405 PERROR("Failed to get sub-buffer end timestamp");
2409 ret
= ustctl_get_events_discarded(
2410 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2412 PERROR("Failed to get sub-buffer events discarded count");
2416 ret
= ustctl_get_sequence_number(stream
->ustream
,
2417 &subbuf
->info
.data
.sequence_number
.value
);
2419 /* May not be supported by older LTTng-modules. */
2420 if (ret
!= -ENOTTY
) {
2421 PERROR("Failed to get sub-buffer sequence number");
2425 subbuf
->info
.data
.sequence_number
.is_set
= true;
2428 ret
= ustctl_get_stream_id(
2429 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2431 PERROR("Failed to get stream id");
2435 ret
= ustctl_get_instance_id(stream
->ustream
,
2436 &subbuf
->info
.data
.stream_instance_id
.value
);
2438 /* May not be supported by older LTTng-modules. */
2439 if (ret
!= -ENOTTY
) {
2440 PERROR("Failed to get stream instance id");
2444 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2450 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2451 struct stream_subbuffer
*subbuffer
)
2456 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2462 ret
= get_current_subbuf_addr(stream
, &addr
);
2467 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2468 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2469 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2474 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2475 struct stream_subbuffer
*subbuffer
)
2479 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2484 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2492 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2493 struct stream_subbuffer
*subbuffer
)
2497 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2499 ret
= commit_one_metadata_packet(stream
);
2502 } else if (ret
== 0) {
2503 /* Not an error, the cache is empty. */
2508 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2514 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2522 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2523 struct stream_subbuffer
*subbuffer
)
2525 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2531 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2532 struct lttng_consumer_local_data
*ctx
)
2534 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
2537 static void lttng_ustconsumer_set_stream_ops(
2538 struct lttng_consumer_stream
*stream
)
2540 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
2541 if (stream
->metadata_flag
) {
2542 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2543 get_next_subbuffer_metadata
;
2544 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2545 extract_metadata_subbuffer_info
;
2546 stream
->read_subbuffer_ops
.reset_metadata
=
2547 metadata_stream_reset_cache
;
2548 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
2550 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2552 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2553 extract_data_subbuffer_info
;
2554 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
2555 if (stream
->chan
->is_live
) {
2556 stream
->read_subbuffer_ops
.send_live_beacon
=
2557 consumer_flush_ust_index
;
2561 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
2565 * Called when a stream is created.
2567 * Return 0 on success or else a negative value.
2569 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2575 /* Don't create anything if this is set for streaming. */
2576 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
2577 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
2578 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
2579 stream
->uid
, stream
->gid
, NULL
);
2583 stream
->out_fd
= ret
;
2584 stream
->tracefile_size_current
= 0;
2586 if (!stream
->metadata_flag
) {
2587 struct lttng_index_file
*index_file
;
2589 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
2590 stream
->name
, stream
->uid
, stream
->gid
,
2591 stream
->chan
->tracefile_size
,
2592 stream
->tracefile_count_current
,
2593 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
2597 stream
->index_file
= index_file
;
2601 lttng_ustconsumer_set_stream_ops(stream
);
2609 * Check if data is still being extracted from the buffers for a specific
2610 * stream. Consumer data lock MUST be acquired before calling this function
2611 * and the stream lock.
2613 * Return 1 if the traced data are still getting read else 0 meaning that the
2614 * data is available for trace viewer reading.
2616 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2621 assert(stream
->ustream
);
2623 DBG("UST consumer checking data pending");
2625 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2630 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
2631 uint64_t contiguous
, pushed
;
2633 /* Ease our life a bit. */
2634 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
2635 pushed
= stream
->ust_metadata_pushed
;
2638 * We can simply check whether all contiguously available data
2639 * has been pushed to the ring buffer, since the push operation
2640 * is performed within get_next_subbuf(), and because both
2641 * get_next_subbuf() and put_next_subbuf() are issued atomically
2642 * thanks to the stream lock within
2643 * lttng_ustconsumer_read_subbuffer(). This basically means that
2644 * whetnever ust_metadata_pushed is incremented, the associated
2645 * metadata has been consumed from the metadata stream.
2647 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
2648 contiguous
, pushed
);
2649 assert(((int64_t) (contiguous
- pushed
)) >= 0);
2650 if ((contiguous
!= pushed
) ||
2651 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
2652 ret
= 1; /* Data is pending */
2656 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2659 * There is still data so let's put back this
2662 ret
= ustctl_put_subbuf(stream
->ustream
);
2664 ret
= 1; /* Data is pending */
2669 /* Data is NOT pending so ready to be read. */
2677 * Stop a given metadata channel timer if enabled and close the wait fd which
2678 * is the poll pipe of the metadata stream.
2680 * This MUST be called with the metadata channel acquired.
2682 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
2687 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
2689 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
2691 if (metadata
->switch_timer_enabled
== 1) {
2692 consumer_timer_switch_stop(metadata
);
2695 if (!metadata
->metadata_stream
) {
2700 * Closing write side so the thread monitoring the stream wakes up if any
2701 * and clean the metadata stream.
2703 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
2704 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
2706 PERROR("closing metadata pipe write side");
2708 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
2716 * Close every metadata stream wait fd of the metadata hash table. This
2717 * function MUST be used very carefully so not to run into a race between the
2718 * metadata thread handling streams and this function closing their wait fd.
2720 * For UST, this is used when the session daemon hangs up. Its the metadata
2721 * producer so calling this is safe because we are assured that no state change
2722 * can occur in the metadata thread for the streams in the hash table.
2724 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
2726 struct lttng_ht_iter iter
;
2727 struct lttng_consumer_stream
*stream
;
2729 assert(metadata_ht
);
2730 assert(metadata_ht
->ht
);
2732 DBG("UST consumer closing all metadata streams");
2735 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
2738 health_code_update();
2740 pthread_mutex_lock(&stream
->chan
->lock
);
2741 lttng_ustconsumer_close_metadata(stream
->chan
);
2742 pthread_mutex_unlock(&stream
->chan
->lock
);
2748 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
2752 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
2754 ERR("Unable to close wakeup fd");
2759 * Please refer to consumer-timer.c before adding any lock within this
2760 * function or any of its callees. Timers have a very strict locking
2761 * semantic with respect to teardown. Failure to respect this semantic
2762 * introduces deadlocks.
2764 * DON'T hold the metadata lock when calling this function, else this
2765 * can cause deadlock involving consumer awaiting for metadata to be
2766 * pushed out due to concurrent interaction with the session daemon.
2768 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
2769 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
2771 struct lttcomm_metadata_request_msg request
;
2772 struct lttcomm_consumer_msg msg
;
2773 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
2774 uint64_t len
, key
, offset
, version
;
2778 assert(channel
->metadata_cache
);
2780 memset(&request
, 0, sizeof(request
));
2782 /* send the metadata request to sessiond */
2783 switch (consumer_data
.type
) {
2784 case LTTNG_CONSUMER64_UST
:
2785 request
.bits_per_long
= 64;
2787 case LTTNG_CONSUMER32_UST
:
2788 request
.bits_per_long
= 32;
2791 request
.bits_per_long
= 0;
2795 request
.session_id
= channel
->session_id
;
2796 request
.session_id_per_pid
= channel
->session_id_per_pid
;
2798 * Request the application UID here so the metadata of that application can
2799 * be sent back. The channel UID corresponds to the user UID of the session
2800 * used for the rights on the stream file(s).
2802 request
.uid
= channel
->ust_app_uid
;
2803 request
.key
= channel
->key
;
2805 DBG("Sending metadata request to sessiond, session id %" PRIu64
2806 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
2807 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
2810 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
2812 health_code_update();
2814 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
2817 ERR("Asking metadata to sessiond");
2821 health_code_update();
2823 /* Receive the metadata from sessiond */
2824 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
2826 if (ret
!= sizeof(msg
)) {
2827 DBG("Consumer received unexpected message size %d (expects %zu)",
2829 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
2831 * The ret value might 0 meaning an orderly shutdown but this is ok
2832 * since the caller handles this.
2837 health_code_update();
2839 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
2840 /* No registry found */
2841 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2845 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
2846 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
2851 len
= msg
.u
.push_metadata
.len
;
2852 key
= msg
.u
.push_metadata
.key
;
2853 offset
= msg
.u
.push_metadata
.target_offset
;
2854 version
= msg
.u
.push_metadata
.version
;
2856 assert(key
== channel
->key
);
2858 DBG("No new metadata to receive for key %" PRIu64
, key
);
2861 health_code_update();
2863 /* Tell session daemon we are ready to receive the metadata. */
2864 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
2865 LTTCOMM_CONSUMERD_SUCCESS
);
2866 if (ret
< 0 || len
== 0) {
2868 * Somehow, the session daemon is not responding anymore or there is
2869 * nothing to receive.
2874 health_code_update();
2876 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
2877 key
, offset
, len
, version
, channel
, timer
, wait
);
2880 * Only send the status msg if the sessiond is alive meaning a positive
2883 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
2888 health_code_update();
2890 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
2895 * Return the ustctl call for the get stream id.
2897 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
2898 uint64_t *stream_id
)
2903 return ustctl_get_stream_id(stream
->ustream
, stream_id
);