2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <lttng/ust-ctl.h>
28 #include <sys/socket.h>
30 #include <sys/types.h>
33 #include <urcu/list.h>
38 #include <bin/lttng-consumerd/health-consumerd.h>
39 #include <common/common.h>
40 #include <common/sessiond-comm/sessiond-comm.h>
41 #include <common/relayd/relayd.h>
42 #include <common/compat/fcntl.h>
43 #include <common/compat/endian.h>
44 #include <common/consumer/consumer-metadata-cache.h>
45 #include <common/consumer/consumer-stream.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/utils.h>
48 #include <common/index/index.h>
49 #include <common/consumer/consumer.h>
50 #include <common/optional.h>
52 #include "ust-consumer.h"
54 #define INT_MAX_STR_LEN 12 /* includes \0 */
56 extern struct lttng_consumer_global_data consumer_data
;
57 extern int consumer_poll_timeout
;
60 * Free channel object and all streams associated with it. This MUST be used
61 * only and only if the channel has _NEVER_ been added to the global channel
64 static void destroy_channel(struct lttng_consumer_channel
*channel
)
66 struct lttng_consumer_stream
*stream
, *stmp
;
70 DBG("UST consumer cleaning stream list");
72 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
77 cds_list_del(&stream
->send_node
);
78 ustctl_destroy_stream(stream
->ustream
);
79 lttng_trace_chunk_put(stream
->trace_chunk
);
84 * If a channel is available meaning that was created before the streams
88 lttng_ustconsumer_del_channel(channel
);
89 lttng_ustconsumer_free_channel(channel
);
92 if (channel
->trace_chunk
) {
93 lttng_trace_chunk_put(channel
->trace_chunk
);
100 * Add channel to internal consumer state.
102 * Returns 0 on success or else a negative value.
104 static int add_channel(struct lttng_consumer_channel
*channel
,
105 struct lttng_consumer_local_data
*ctx
)
112 if (ctx
->on_recv_channel
!= NULL
) {
113 ret
= ctx
->on_recv_channel(channel
);
115 ret
= consumer_add_channel(channel
, ctx
);
116 } else if (ret
< 0) {
117 /* Most likely an ENOMEM. */
118 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
122 ret
= consumer_add_channel(channel
, ctx
);
125 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
132 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
133 * error value if applicable is set in it else it is kept untouched.
135 * Return NULL on error else the newly allocated stream object.
137 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
138 struct lttng_consumer_channel
*channel
,
139 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
142 struct lttng_consumer_stream
*stream
= NULL
;
147 stream
= consumer_stream_create(
154 channel
->trace_chunk
,
159 if (stream
== NULL
) {
163 * We could not find the channel. Can happen if cpu hotplug
164 * happens while tearing down.
166 DBG3("Could not find channel");
171 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
177 consumer_stream_update_channel_attributes(stream
, channel
);
181 *_alloc_ret
= alloc_ret
;
187 * Send the given stream pointer to the corresponding thread.
189 * Returns 0 on success else a negative value.
191 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
192 struct lttng_consumer_local_data
*ctx
)
195 struct lttng_pipe
*stream_pipe
;
197 /* Get the right pipe where the stream will be sent. */
198 if (stream
->metadata_flag
) {
199 consumer_add_metadata_stream(stream
);
200 stream_pipe
= ctx
->consumer_metadata_pipe
;
202 consumer_add_data_stream(stream
);
203 stream_pipe
= ctx
->consumer_data_pipe
;
207 * From this point on, the stream's ownership has been moved away from
208 * the channel and it becomes globally visible. Hence, remove it from
209 * the local stream list to prevent the stream from being both local and
212 stream
->globally_visible
= 1;
213 cds_list_del(&stream
->send_node
);
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
);
233 int get_stream_shm_path(char *stream_shm_path
, const char *shm_path
, int cpu
)
235 char cpu_nr
[INT_MAX_STR_LEN
]; /* int max len */
238 strncpy(stream_shm_path
, shm_path
, PATH_MAX
);
239 stream_shm_path
[PATH_MAX
- 1] = '\0';
240 ret
= snprintf(cpu_nr
, INT_MAX_STR_LEN
, "%i", cpu
);
245 strncat(stream_shm_path
, cpu_nr
,
246 PATH_MAX
- strlen(stream_shm_path
) - 1);
253 * Create streams for the given channel using liblttng-ust-ctl.
254 * The channel lock must be acquired by the caller.
256 * Return 0 on success else a negative value.
258 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
259 struct lttng_consumer_local_data
*ctx
)
262 struct ustctl_consumer_stream
*ustream
;
263 struct lttng_consumer_stream
*stream
;
264 pthread_mutex_t
*current_stream_lock
= NULL
;
270 * While a stream is available from ustctl. When NULL is returned, we've
271 * reached the end of the possible stream for the channel.
273 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
275 int ust_metadata_pipe
[2];
277 health_code_update();
279 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& channel
->monitor
) {
280 ret
= utils_create_pipe_cloexec_nonblock(ust_metadata_pipe
);
282 ERR("Create ust metadata poll pipe");
285 wait_fd
= ust_metadata_pipe
[0];
287 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
290 /* Allocate consumer stream object. */
291 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
295 stream
->ustream
= ustream
;
297 * Store it so we can save multiple function calls afterwards since
298 * this value is used heavily in the stream threads. This is UST
299 * specific so this is why it's done after allocation.
301 stream
->wait_fd
= wait_fd
;
304 * Increment channel refcount since the channel reference has now been
305 * assigned in the allocation process above.
307 if (stream
->chan
->monitor
) {
308 uatomic_inc(&stream
->chan
->refcount
);
311 pthread_mutex_lock(&stream
->lock
);
312 current_stream_lock
= &stream
->lock
;
314 * Order is important this is why a list is used. On error, the caller
315 * should clean this list.
317 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
319 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
320 &stream
->max_sb_size
);
322 ERR("ustctl_get_max_subbuf_size failed for stream %s",
327 /* Do actions once stream has been received. */
328 if (ctx
->on_recv_stream
) {
329 ret
= ctx
->on_recv_stream(stream
);
335 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
336 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
338 /* Set next CPU stream. */
339 channel
->streams
.count
= ++cpu
;
341 /* Keep stream reference when creating metadata. */
342 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
343 channel
->metadata_stream
= stream
;
344 if (channel
->monitor
) {
345 /* Set metadata poll pipe if we created one */
346 memcpy(stream
->ust_metadata_poll_pipe
,
348 sizeof(ust_metadata_pipe
));
351 pthread_mutex_unlock(&stream
->lock
);
352 current_stream_lock
= NULL
;
359 if (current_stream_lock
) {
360 pthread_mutex_unlock(current_stream_lock
);
366 * create_posix_shm is never called concurrently within a process.
369 int create_posix_shm(void)
371 char tmp_name
[NAME_MAX
];
374 ret
= snprintf(tmp_name
, NAME_MAX
, "/ust-shm-consumer-%d", getpid());
380 * Allocate shm, and immediately unlink its shm oject, keeping
381 * only the file descriptor as a reference to the object.
382 * We specifically do _not_ use the / at the beginning of the
383 * pathname so that some OS implementations can keep it local to
384 * the process (POSIX leaves this implementation-defined).
386 shmfd
= shm_open(tmp_name
, O_CREAT
| O_EXCL
| O_RDWR
, 0700);
391 ret
= shm_unlink(tmp_name
);
392 if (ret
< 0 && errno
!= ENOENT
) {
393 PERROR("shm_unlink");
394 goto error_shm_release
;
407 static int open_ust_stream_fd(struct lttng_consumer_channel
*channel
, int cpu
,
408 const struct lttng_credentials
*session_credentials
)
410 char shm_path
[PATH_MAX
];
413 if (!channel
->shm_path
[0]) {
414 return create_posix_shm();
416 ret
= get_stream_shm_path(shm_path
, channel
->shm_path
, cpu
);
420 return run_as_open(shm_path
,
421 O_RDWR
| O_CREAT
| O_EXCL
, S_IRUSR
| S_IWUSR
,
422 session_credentials
->uid
, session_credentials
->gid
);
429 * Create an UST channel with the given attributes and send it to the session
430 * daemon using the ust ctl API.
432 * Return 0 on success or else a negative value.
434 static int create_ust_channel(struct lttng_consumer_channel
*channel
,
435 struct ustctl_consumer_channel_attr
*attr
,
436 struct ustctl_consumer_channel
**ust_chanp
)
438 int ret
, nr_stream_fds
, i
, j
;
440 struct ustctl_consumer_channel
*ust_channel
;
445 assert(channel
->buffer_credentials
.is_set
);
447 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
448 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
449 "switch_timer_interval: %u, read_timer_interval: %u, "
450 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
451 attr
->num_subbuf
, attr
->switch_timer_interval
,
452 attr
->read_timer_interval
, attr
->output
, attr
->type
);
454 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
)
457 nr_stream_fds
= ustctl_get_nr_stream_per_channel();
458 stream_fds
= zmalloc(nr_stream_fds
* sizeof(*stream_fds
));
463 for (i
= 0; i
< nr_stream_fds
; i
++) {
464 stream_fds
[i
] = open_ust_stream_fd(channel
, i
,
465 &channel
->buffer_credentials
.value
);
466 if (stream_fds
[i
] < 0) {
471 ust_channel
= ustctl_create_channel(attr
, stream_fds
, nr_stream_fds
);
476 channel
->nr_stream_fds
= nr_stream_fds
;
477 channel
->stream_fds
= stream_fds
;
478 *ust_chanp
= ust_channel
;
484 for (j
= i
- 1; j
>= 0; j
--) {
487 closeret
= close(stream_fds
[j
]);
491 if (channel
->shm_path
[0]) {
492 char shm_path
[PATH_MAX
];
494 closeret
= get_stream_shm_path(shm_path
,
495 channel
->shm_path
, j
);
497 ERR("Cannot get stream shm path");
499 closeret
= run_as_unlink(shm_path
,
500 channel
->buffer_credentials
.value
.uid
,
501 channel
->buffer_credentials
.value
.gid
);
503 PERROR("unlink %s", shm_path
);
507 /* Try to rmdir all directories under shm_path root. */
508 if (channel
->root_shm_path
[0]) {
509 (void) run_as_rmdir_recursive(channel
->root_shm_path
,
510 channel
->buffer_credentials
.value
.uid
,
511 channel
->buffer_credentials
.value
.gid
,
512 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
520 * Send a single given stream to the session daemon using the sock.
522 * Return 0 on success else a negative value.
524 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
531 DBG("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
533 /* Send stream to session daemon. */
534 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
544 * Send channel to sessiond and relayd if applicable.
546 * Return 0 on success or else a negative value.
548 static int send_channel_to_sessiond_and_relayd(int sock
,
549 struct lttng_consumer_channel
*channel
,
550 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
552 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
553 struct lttng_consumer_stream
*stream
;
554 uint64_t net_seq_idx
= -1ULL;
560 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
562 if (channel
->relayd_id
!= (uint64_t) -1ULL) {
563 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
565 health_code_update();
567 /* Try to send the stream to the relayd if one is available. */
568 DBG("Sending stream %" PRIu64
" of channel \"%s\" to relayd",
569 stream
->key
, channel
->name
);
570 ret
= consumer_send_relayd_stream(stream
, stream
->chan
->pathname
);
573 * Flag that the relayd was the problem here probably due to a
574 * communicaton error on the socket.
579 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
581 if (net_seq_idx
== -1ULL) {
582 net_seq_idx
= stream
->net_seq_idx
;
587 /* Inform sessiond that we are about to send channel and streams. */
588 ret
= consumer_send_status_msg(sock
, ret_code
);
589 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
591 * Either the session daemon is not responding or the relayd died so we
597 /* Send channel to sessiond. */
598 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
603 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
608 /* The channel was sent successfully to the sessiond at this point. */
609 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
611 health_code_update();
613 /* Send stream to session daemon. */
614 ret
= send_sessiond_stream(sock
, stream
);
620 /* Tell sessiond there is no more stream. */
621 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
626 DBG("UST consumer NULL stream sent to sessiond");
631 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
638 * Creates a channel and streams and add the channel it to the channel internal
639 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
642 * Return 0 on success or else, a negative value is returned and the channel
643 * MUST be destroyed by consumer_del_channel().
645 static int ask_channel(struct lttng_consumer_local_data
*ctx
,
646 struct lttng_consumer_channel
*channel
,
647 struct ustctl_consumer_channel_attr
*attr
)
656 * This value is still used by the kernel consumer since for the kernel,
657 * the stream ownership is not IN the consumer so we need to have the
658 * number of left stream that needs to be initialized so we can know when
659 * to delete the channel (see consumer.c).
661 * As for the user space tracer now, the consumer creates and sends the
662 * stream to the session daemon which only sends them to the application
663 * once every stream of a channel is received making this value useless
664 * because we they will be added to the poll thread before the application
665 * receives them. This ensures that a stream can not hang up during
666 * initilization of a channel.
668 channel
->nb_init_stream_left
= 0;
670 /* The reply msg status is handled in the following call. */
671 ret
= create_ust_channel(channel
, attr
, &channel
->uchan
);
676 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
679 * For the snapshots (no monitor), we create the metadata streams
680 * on demand, not during the channel creation.
682 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
&& !channel
->monitor
) {
687 /* Open all streams for this channel. */
688 pthread_mutex_lock(&channel
->lock
);
689 ret
= create_ust_streams(channel
, ctx
);
690 pthread_mutex_unlock(&channel
->lock
);
700 * Send all stream of a channel to the right thread handling it.
702 * On error, return a negative value else 0 on success.
704 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
705 struct lttng_consumer_local_data
*ctx
)
708 struct lttng_consumer_stream
*stream
, *stmp
;
713 /* Send streams to the corresponding thread. */
714 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
717 health_code_update();
719 /* Sending the stream to the thread. */
720 ret
= send_stream_to_thread(stream
, ctx
);
723 * If we are unable to send the stream to the thread, there is
724 * a big problem so just stop everything.
735 * Flush channel's streams using the given key to retrieve the channel.
737 * Return 0 on success else an LTTng error code.
739 static int flush_channel(uint64_t chan_key
)
742 struct lttng_consumer_channel
*channel
;
743 struct lttng_consumer_stream
*stream
;
745 struct lttng_ht_iter iter
;
747 DBG("UST consumer flush channel key %" PRIu64
, chan_key
);
750 channel
= consumer_find_channel(chan_key
);
752 ERR("UST consumer flush channel %" PRIu64
" not found", chan_key
);
753 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
757 ht
= consumer_data
.stream_per_chan_id_ht
;
759 /* For each stream of the channel id, flush it. */
760 cds_lfht_for_each_entry_duplicate(ht
->ht
,
761 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
762 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
764 health_code_update();
766 pthread_mutex_lock(&stream
->lock
);
769 * Protect against concurrent teardown of a stream.
771 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
775 if (!stream
->quiescent
) {
776 ustctl_flush_buffer(stream
->ustream
, 0);
777 stream
->quiescent
= true;
780 pthread_mutex_unlock(&stream
->lock
);
788 * Clear quiescent state from channel's streams using the given key to
789 * retrieve the channel.
791 * Return 0 on success else an LTTng error code.
793 static int clear_quiescent_channel(uint64_t chan_key
)
796 struct lttng_consumer_channel
*channel
;
797 struct lttng_consumer_stream
*stream
;
799 struct lttng_ht_iter iter
;
801 DBG("UST consumer clear quiescent channel key %" PRIu64
, chan_key
);
804 channel
= consumer_find_channel(chan_key
);
806 ERR("UST consumer clear quiescent channel %" PRIu64
" not found", chan_key
);
807 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
811 ht
= consumer_data
.stream_per_chan_id_ht
;
813 /* For each stream of the channel id, clear quiescent state. */
814 cds_lfht_for_each_entry_duplicate(ht
->ht
,
815 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
816 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
818 health_code_update();
820 pthread_mutex_lock(&stream
->lock
);
821 stream
->quiescent
= false;
822 pthread_mutex_unlock(&stream
->lock
);
830 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
832 * Return 0 on success else an LTTng error code.
834 static int close_metadata(uint64_t chan_key
)
837 struct lttng_consumer_channel
*channel
;
838 unsigned int channel_monitor
;
840 DBG("UST consumer close metadata key %" PRIu64
, chan_key
);
842 channel
= consumer_find_channel(chan_key
);
845 * This is possible if the metadata thread has issue a delete because
846 * the endpoint point of the stream hung up. There is no way the
847 * session daemon can know about it thus use a DBG instead of an actual
850 DBG("UST consumer close metadata %" PRIu64
" not found", chan_key
);
851 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
855 pthread_mutex_lock(&consumer_data
.lock
);
856 pthread_mutex_lock(&channel
->lock
);
857 channel_monitor
= channel
->monitor
;
858 if (cds_lfht_is_node_deleted(&channel
->node
.node
)) {
862 lttng_ustconsumer_close_metadata(channel
);
863 pthread_mutex_unlock(&channel
->lock
);
864 pthread_mutex_unlock(&consumer_data
.lock
);
867 * The ownership of a metadata channel depends on the type of
868 * session to which it belongs. In effect, the monitor flag is checked
869 * to determine if this metadata channel is in "snapshot" mode or not.
871 * In the non-snapshot case, the metadata channel is created along with
872 * a single stream which will remain present until the metadata channel
873 * is destroyed (on the destruction of its session). In this case, the
874 * metadata stream in "monitored" by the metadata poll thread and holds
875 * the ownership of its channel.
877 * Closing the metadata will cause the metadata stream's "metadata poll
878 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
879 * thread which will teardown the metadata stream which, in return,
880 * deletes the metadata channel.
882 * In the snapshot case, the metadata stream is created and destroyed
883 * on every snapshot record. Since the channel doesn't have an owner
884 * other than the session daemon, it is safe to destroy it immediately
885 * on reception of the CLOSE_METADATA command.
887 if (!channel_monitor
) {
889 * The channel and consumer_data locks must be
890 * released before this call since consumer_del_channel
891 * re-acquires the channel and consumer_data locks to teardown
892 * the channel and queue its reclamation by the "call_rcu"
895 consumer_del_channel(channel
);
900 pthread_mutex_unlock(&channel
->lock
);
901 pthread_mutex_unlock(&consumer_data
.lock
);
907 * RCU read side lock MUST be acquired before calling this function.
909 * Return 0 on success else an LTTng error code.
911 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
914 struct lttng_consumer_channel
*metadata
;
916 DBG("UST consumer setup metadata key %" PRIu64
, key
);
918 metadata
= consumer_find_channel(key
);
920 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
921 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
926 * In no monitor mode, the metadata channel has no stream(s) so skip the
927 * ownership transfer to the metadata thread.
929 if (!metadata
->monitor
) {
930 DBG("Metadata channel in no monitor");
936 * Send metadata stream to relayd if one available. Availability is
937 * known if the stream is still in the list of the channel.
939 if (cds_list_empty(&metadata
->streams
.head
)) {
940 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
941 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
942 goto error_no_stream
;
945 /* Send metadata stream to relayd if needed. */
946 if (metadata
->metadata_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
947 ret
= consumer_send_relayd_stream(metadata
->metadata_stream
,
950 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
953 ret
= consumer_send_relayd_streams_sent(
954 metadata
->metadata_stream
->net_seq_idx
);
956 ret
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
962 * Ownership of metadata stream is passed along. Freeing is handled by
965 ret
= send_streams_to_thread(metadata
, ctx
);
968 * If we are unable to send the stream to the thread, there is
969 * a big problem so just stop everything.
971 ret
= LTTCOMM_CONSUMERD_FATAL
;
972 goto send_streams_error
;
974 /* List MUST be empty after or else it could be reused. */
975 assert(cds_list_empty(&metadata
->streams
.head
));
982 * Delete metadata channel on error. At this point, the metadata stream can
983 * NOT be monitored by the metadata thread thus having the guarantee that
984 * the stream is still in the local stream list of the channel. This call
985 * will make sure to clean that list.
987 consumer_stream_destroy(metadata
->metadata_stream
, NULL
);
988 cds_list_del(&metadata
->metadata_stream
->send_node
);
989 metadata
->metadata_stream
= NULL
;
997 * Snapshot the whole metadata.
998 * RCU read-side lock must be held by the caller.
1000 * Returns 0 on success, < 0 on error
1002 static int snapshot_metadata(struct lttng_consumer_channel
*metadata_channel
,
1003 uint64_t key
, char *path
, uint64_t relayd_id
,
1004 struct lttng_consumer_local_data
*ctx
)
1007 struct lttng_consumer_stream
*metadata_stream
;
1012 DBG("UST consumer snapshot metadata with key %" PRIu64
" at path %s",
1017 assert(!metadata_channel
->monitor
);
1019 health_code_update();
1022 * Ask the sessiond if we have new metadata waiting and update the
1023 * consumer metadata cache.
1025 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 1);
1030 health_code_update();
1033 * The metadata stream is NOT created in no monitor mode when the channel
1034 * is created on a sessiond ask channel command.
1036 ret
= create_ust_streams(metadata_channel
, ctx
);
1041 metadata_stream
= metadata_channel
->metadata_stream
;
1042 assert(metadata_stream
);
1044 pthread_mutex_lock(&metadata_stream
->lock
);
1045 if (relayd_id
!= (uint64_t) -1ULL) {
1046 metadata_stream
->net_seq_idx
= relayd_id
;
1047 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
1049 ret
= consumer_stream_create_output_files(metadata_stream
,
1052 pthread_mutex_unlock(&metadata_stream
->lock
);
1058 health_code_update();
1060 ret
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
1068 * Clean up the stream completly because the next snapshot will use a new
1071 consumer_stream_destroy(metadata_stream
, NULL
);
1072 cds_list_del(&metadata_stream
->send_node
);
1073 metadata_channel
->metadata_stream
= NULL
;
1081 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
1085 unsigned long mmap_offset
;
1086 const char *mmap_base
;
1088 mmap_base
= ustctl_get_mmap_base(stream
->ustream
);
1090 ERR("Failed to get mmap base for stream `%s`",
1096 ret
= ustctl_get_mmap_read_offset(stream
->ustream
, &mmap_offset
);
1098 ERR("Failed to get mmap offset for stream `%s`", stream
->name
);
1103 *addr
= mmap_base
+ mmap_offset
;
1110 * Take a snapshot of all the stream of a channel.
1111 * RCU read-side lock and the channel lock must be held by the caller.
1113 * Returns 0 on success, < 0 on error
1115 static int snapshot_channel(struct lttng_consumer_channel
*channel
,
1116 uint64_t key
, char *path
, uint64_t relayd_id
,
1117 uint64_t nb_packets_per_stream
,
1118 struct lttng_consumer_local_data
*ctx
)
1121 unsigned use_relayd
= 0;
1122 unsigned long consumed_pos
, produced_pos
;
1123 struct lttng_consumer_stream
*stream
;
1130 if (relayd_id
!= (uint64_t) -1ULL) {
1134 assert(!channel
->monitor
);
1135 DBG("UST consumer snapshot channel %" PRIu64
, key
);
1137 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
1138 health_code_update();
1140 /* Lock stream because we are about to change its state. */
1141 pthread_mutex_lock(&stream
->lock
);
1142 assert(channel
->trace_chunk
);
1143 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
1145 * Can't happen barring an internal error as the channel
1146 * holds a reference to the trace chunk.
1148 ERR("Failed to acquire reference to channel's trace chunk");
1152 assert(!stream
->trace_chunk
);
1153 stream
->trace_chunk
= channel
->trace_chunk
;
1155 stream
->net_seq_idx
= relayd_id
;
1158 ret
= consumer_send_relayd_stream(stream
, path
);
1163 ret
= consumer_stream_create_output_files(stream
,
1168 DBG("UST consumer snapshot stream (%" PRIu64
")",
1173 * If tracing is active, we want to perform a "full" buffer flush.
1174 * Else, if quiescent, it has already been done by the prior stop.
1176 if (!stream
->quiescent
) {
1177 ustctl_flush_buffer(stream
->ustream
, 0);
1180 ret
= lttng_ustconsumer_take_snapshot(stream
);
1182 ERR("Taking UST snapshot");
1186 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
1188 ERR("Produced UST snapshot position");
1192 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
1194 ERR("Consumerd UST snapshot position");
1199 * The original value is sent back if max stream size is larger than
1200 * the possible size of the snapshot. Also, we assume that the session
1201 * daemon should never send a maximum stream size that is lower than
1204 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
1205 produced_pos
, nb_packets_per_stream
,
1206 stream
->max_sb_size
);
1208 while ((long) (consumed_pos
- produced_pos
) < 0) {
1210 unsigned long len
, padded_len
;
1211 const char *subbuf_addr
;
1212 struct lttng_buffer_view subbuf_view
;
1214 health_code_update();
1216 DBG("UST consumer taking snapshot at pos %lu", consumed_pos
);
1218 ret
= ustctl_get_subbuf(stream
->ustream
, &consumed_pos
);
1220 if (ret
!= -EAGAIN
) {
1221 PERROR("ustctl_get_subbuf snapshot");
1222 goto error_close_stream
;
1224 DBG("UST consumer get subbuf failed. Skipping it.");
1225 consumed_pos
+= stream
->max_sb_size
;
1226 stream
->chan
->lost_packets
++;
1230 ret
= ustctl_get_subbuf_size(stream
->ustream
, &len
);
1232 ERR("Snapshot ustctl_get_subbuf_size");
1233 goto error_put_subbuf
;
1236 ret
= ustctl_get_padded_subbuf_size(stream
->ustream
, &padded_len
);
1238 ERR("Snapshot ustctl_get_padded_subbuf_size");
1239 goto error_put_subbuf
;
1242 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1244 goto error_put_subbuf
;
1247 subbuf_view
= lttng_buffer_view_init(
1248 subbuf_addr
, 0, padded_len
);
1249 read_len
= lttng_consumer_on_read_subbuffer_mmap(
1250 stream
, &subbuf_view
, padded_len
- len
);
1252 if (read_len
!= len
) {
1254 goto error_put_subbuf
;
1257 if (read_len
!= padded_len
) {
1259 goto error_put_subbuf
;
1263 ret
= ustctl_put_subbuf(stream
->ustream
);
1265 ERR("Snapshot ustctl_put_subbuf");
1266 goto error_close_stream
;
1268 consumed_pos
+= stream
->max_sb_size
;
1271 /* Simply close the stream so we can use it on the next snapshot. */
1272 consumer_stream_close(stream
);
1273 pthread_mutex_unlock(&stream
->lock
);
1280 if (ustctl_put_subbuf(stream
->ustream
) < 0) {
1281 ERR("Snapshot ustctl_put_subbuf");
1284 consumer_stream_close(stream
);
1286 pthread_mutex_unlock(&stream
->lock
);
1292 * Receive the metadata updates from the sessiond. Supports receiving
1293 * overlapping metadata, but is needs to always belong to a contiguous
1294 * range starting from 0.
1295 * Be careful about the locks held when calling this function: it needs
1296 * the metadata cache flush to concurrently progress in order to
1299 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
1300 uint64_t len
, uint64_t version
,
1301 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
1303 int ret
, ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1306 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
, len
);
1308 metadata_str
= zmalloc(len
* sizeof(char));
1309 if (!metadata_str
) {
1310 PERROR("zmalloc metadata string");
1311 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
1315 health_code_update();
1317 /* Receive metadata string. */
1318 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
1320 /* Session daemon is dead so return gracefully. */
1325 health_code_update();
1327 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
1328 ret
= consumer_metadata_cache_write(channel
, offset
, len
, version
,
1330 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
1332 /* Unable to handle metadata. Notify session daemon. */
1333 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1335 * Skip metadata flush on write error since the offset and len might
1336 * not have been updated which could create an infinite loop below when
1337 * waiting for the metadata cache to be flushed.
1345 while (consumer_metadata_cache_flushed(channel
, offset
+ len
, timer
)) {
1346 DBG("Waiting for metadata to be flushed");
1348 health_code_update();
1350 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
1360 * Receive command from session daemon and process it.
1362 * Return 1 on success else a negative value or 0.
1364 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1365 int sock
, struct pollfd
*consumer_sockpoll
)
1368 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1369 struct lttcomm_consumer_msg msg
;
1370 struct lttng_consumer_channel
*channel
= NULL
;
1372 health_code_update();
1374 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
1375 if (ret
!= sizeof(msg
)) {
1376 DBG("Consumer received unexpected message size %zd (expects %zu)",
1379 * The ret value might 0 meaning an orderly shutdown but this is ok
1380 * since the caller handles this.
1383 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1389 health_code_update();
1392 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
1394 health_code_update();
1396 /* relayd needs RCU read-side lock */
1399 switch (msg
.cmd_type
) {
1400 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
1402 /* Session daemon status message are handled in the following call. */
1403 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
1404 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
1405 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
1406 msg
.u
.relayd_sock
.relayd_session_id
);
1409 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1411 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1412 struct consumer_relayd_sock_pair
*relayd
;
1414 DBG("UST consumer destroying relayd %" PRIu64
, index
);
1416 /* Get relayd reference if exists. */
1417 relayd
= consumer_find_relayd(index
);
1418 if (relayd
== NULL
) {
1419 DBG("Unable to find relayd %" PRIu64
, index
);
1420 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1424 * Each relayd socket pair has a refcount of stream attached to it
1425 * which tells if the relayd is still active or not depending on the
1428 * This will set the destroy flag of the relayd object and destroy it
1429 * if the refcount reaches zero when called.
1431 * The destroy can happen either here or when a stream fd hangs up.
1434 consumer_flag_relayd_for_destroy(relayd
);
1437 goto end_msg_sessiond
;
1439 case LTTNG_CONSUMER_UPDATE_STREAM
:
1444 case LTTNG_CONSUMER_DATA_PENDING
:
1446 int ret
, is_data_pending
;
1447 uint64_t id
= msg
.u
.data_pending
.session_id
;
1449 DBG("UST consumer data pending command for id %" PRIu64
, id
);
1451 is_data_pending
= consumer_data_pending(id
);
1453 /* Send back returned value to session daemon */
1454 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
1455 sizeof(is_data_pending
));
1457 DBG("Error when sending the data pending ret code: %d", ret
);
1462 * No need to send back a status message since the data pending
1463 * returned value is the response.
1467 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
1470 struct ustctl_consumer_channel_attr attr
;
1471 const uint64_t chunk_id
= msg
.u
.ask_channel
.chunk_id
.value
;
1472 const struct lttng_credentials buffer_credentials
= {
1473 .uid
= msg
.u
.ask_channel
.buffer_credentials
.uid
,
1474 .gid
= msg
.u
.ask_channel
.buffer_credentials
.gid
,
1477 /* Create a plain object and reserve a channel key. */
1478 channel
= consumer_allocate_channel(
1479 msg
.u
.ask_channel
.key
,
1480 msg
.u
.ask_channel
.session_id
,
1481 msg
.u
.ask_channel
.chunk_id
.is_set
?
1483 msg
.u
.ask_channel
.pathname
,
1484 msg
.u
.ask_channel
.name
,
1485 msg
.u
.ask_channel
.relayd_id
,
1486 (enum lttng_event_output
) msg
.u
.ask_channel
.output
,
1487 msg
.u
.ask_channel
.tracefile_size
,
1488 msg
.u
.ask_channel
.tracefile_count
,
1489 msg
.u
.ask_channel
.session_id_per_pid
,
1490 msg
.u
.ask_channel
.monitor
,
1491 msg
.u
.ask_channel
.live_timer_interval
,
1492 msg
.u
.ask_channel
.is_live
,
1493 msg
.u
.ask_channel
.root_shm_path
,
1494 msg
.u
.ask_channel
.shm_path
);
1496 goto end_channel_error
;
1499 LTTNG_OPTIONAL_SET(&channel
->buffer_credentials
,
1500 buffer_credentials
);
1503 * Assign UST application UID to the channel. This value is ignored for
1504 * per PID buffers. This is specific to UST thus setting this after the
1507 channel
->ust_app_uid
= msg
.u
.ask_channel
.ust_app_uid
;
1509 /* Build channel attributes from received message. */
1510 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
1511 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
1512 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
1513 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
1514 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
1515 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
1516 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
1517 attr
.blocking_timeout
= msg
.u
.ask_channel
.blocking_timeout
;
1519 /* Match channel buffer type to the UST abi. */
1520 switch (msg
.u
.ask_channel
.output
) {
1521 case LTTNG_EVENT_MMAP
:
1523 attr
.output
= LTTNG_UST_MMAP
;
1527 /* Translate and save channel type. */
1528 switch (msg
.u
.ask_channel
.type
) {
1529 case LTTNG_UST_CHAN_PER_CPU
:
1530 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
1531 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
1533 * Set refcount to 1 for owner. Below, we will
1534 * pass ownership to the
1535 * consumer_thread_channel_poll() thread.
1537 channel
->refcount
= 1;
1539 case LTTNG_UST_CHAN_METADATA
:
1540 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
1541 attr
.type
= LTTNG_UST_CHAN_METADATA
;
1548 health_code_update();
1550 ret
= ask_channel(ctx
, channel
, &attr
);
1552 goto end_channel_error
;
1555 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1556 ret
= consumer_metadata_cache_allocate(channel
);
1558 ERR("Allocating metadata cache");
1559 goto end_channel_error
;
1561 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
1562 attr
.switch_timer_interval
= 0;
1564 int monitor_start_ret
;
1566 consumer_timer_live_start(channel
,
1567 msg
.u
.ask_channel
.live_timer_interval
);
1568 monitor_start_ret
= consumer_timer_monitor_start(
1570 msg
.u
.ask_channel
.monitor_timer_interval
);
1571 if (monitor_start_ret
< 0) {
1572 ERR("Starting channel monitoring timer failed");
1573 goto end_channel_error
;
1577 health_code_update();
1580 * Add the channel to the internal state AFTER all streams were created
1581 * and successfully sent to session daemon. This way, all streams must
1582 * be ready before this channel is visible to the threads.
1583 * If add_channel succeeds, ownership of the channel is
1584 * passed to consumer_thread_channel_poll().
1586 ret
= add_channel(channel
, ctx
);
1588 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
1589 if (channel
->switch_timer_enabled
== 1) {
1590 consumer_timer_switch_stop(channel
);
1592 consumer_metadata_cache_destroy(channel
);
1594 if (channel
->live_timer_enabled
== 1) {
1595 consumer_timer_live_stop(channel
);
1597 if (channel
->monitor_timer_enabled
== 1) {
1598 consumer_timer_monitor_stop(channel
);
1600 goto end_channel_error
;
1603 health_code_update();
1606 * Channel and streams are now created. Inform the session daemon that
1607 * everything went well and should wait to receive the channel and
1608 * streams with ustctl API.
1610 ret
= consumer_send_status_channel(sock
, channel
);
1613 * There is probably a problem on the socket.
1620 case LTTNG_CONSUMER_GET_CHANNEL
:
1622 int ret
, relayd_err
= 0;
1623 uint64_t key
= msg
.u
.get_channel
.key
;
1624 struct lttng_consumer_channel
*channel
;
1626 channel
= consumer_find_channel(key
);
1628 ERR("UST consumer get channel key %" PRIu64
" not found", key
);
1629 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1630 goto end_get_channel
;
1633 health_code_update();
1635 /* Send the channel to sessiond (and relayd, if applicable). */
1636 ret
= send_channel_to_sessiond_and_relayd(sock
, channel
, ctx
,
1641 * We were unable to send to the relayd the stream so avoid
1642 * sending back a fatal error to the thread since this is OK
1643 * and the consumer can continue its work. The above call
1644 * has sent the error status message to the sessiond.
1646 goto end_get_channel_nosignal
;
1649 * The communicaton was broken hence there is a bad state between
1650 * the consumer and sessiond so stop everything.
1652 goto error_get_channel_fatal
;
1655 health_code_update();
1658 * In no monitor mode, the streams ownership is kept inside the channel
1659 * so don't send them to the data thread.
1661 if (!channel
->monitor
) {
1662 goto end_get_channel
;
1665 ret
= send_streams_to_thread(channel
, ctx
);
1668 * If we are unable to send the stream to the thread, there is
1669 * a big problem so just stop everything.
1671 goto error_get_channel_fatal
;
1673 /* List MUST be empty after or else it could be reused. */
1674 assert(cds_list_empty(&channel
->streams
.head
));
1676 goto end_msg_sessiond
;
1677 error_get_channel_fatal
:
1679 end_get_channel_nosignal
:
1682 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1684 uint64_t key
= msg
.u
.destroy_channel
.key
;
1687 * Only called if streams have not been sent to stream
1688 * manager thread. However, channel has been sent to
1689 * channel manager thread.
1691 notify_thread_del_channel(ctx
, key
);
1692 goto end_msg_sessiond
;
1694 case LTTNG_CONSUMER_CLOSE_METADATA
:
1698 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1703 goto end_msg_sessiond
;
1705 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1709 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1714 goto end_msg_sessiond
;
1716 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL
:
1720 ret
= clear_quiescent_channel(
1721 msg
.u
.clear_quiescent_channel
.key
);
1726 goto end_msg_sessiond
;
1728 case LTTNG_CONSUMER_PUSH_METADATA
:
1731 uint64_t len
= msg
.u
.push_metadata
.len
;
1732 uint64_t key
= msg
.u
.push_metadata
.key
;
1733 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1734 uint64_t version
= msg
.u
.push_metadata
.version
;
1735 struct lttng_consumer_channel
*channel
;
1737 DBG("UST consumer push metadata key %" PRIu64
" of len %" PRIu64
, key
,
1740 channel
= consumer_find_channel(key
);
1743 * This is possible if the metadata creation on the consumer side
1744 * is in flight vis-a-vis a concurrent push metadata from the
1745 * session daemon. Simply return that the channel failed and the
1746 * session daemon will handle that message correctly considering
1747 * that this race is acceptable thus the DBG() statement here.
1749 DBG("UST consumer push metadata %" PRIu64
" not found", key
);
1750 ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
1751 goto end_push_metadata_msg_sessiond
;
1754 health_code_update();
1758 * There is nothing to receive. We have simply
1759 * checked whether the channel can be found.
1761 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1762 goto end_push_metadata_msg_sessiond
;
1765 /* Tell session daemon we are ready to receive the metadata. */
1766 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1768 /* Somehow, the session daemon is not responding anymore. */
1769 goto error_push_metadata_fatal
;
1772 health_code_update();
1774 /* Wait for more data. */
1775 health_poll_entry();
1776 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
1779 goto error_push_metadata_fatal
;
1782 health_code_update();
1784 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1785 len
, version
, channel
, 0, 1);
1787 /* error receiving from sessiond */
1788 goto error_push_metadata_fatal
;
1791 goto end_push_metadata_msg_sessiond
;
1793 end_push_metadata_msg_sessiond
:
1794 goto end_msg_sessiond
;
1795 error_push_metadata_fatal
:
1798 case LTTNG_CONSUMER_SETUP_METADATA
:
1802 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1806 goto end_msg_sessiond
;
1808 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1810 struct lttng_consumer_channel
*channel
;
1811 uint64_t key
= msg
.u
.snapshot_channel
.key
;
1813 channel
= consumer_find_channel(key
);
1815 DBG("UST snapshot channel not found for key %" PRIu64
, key
);
1816 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1818 if (msg
.u
.snapshot_channel
.metadata
) {
1819 ret
= snapshot_metadata(channel
, key
,
1820 msg
.u
.snapshot_channel
.pathname
,
1821 msg
.u
.snapshot_channel
.relayd_id
,
1824 ERR("Snapshot metadata failed");
1825 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1828 ret
= snapshot_channel(channel
, key
,
1829 msg
.u
.snapshot_channel
.pathname
,
1830 msg
.u
.snapshot_channel
.relayd_id
,
1831 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1834 ERR("Snapshot channel failed");
1835 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1839 health_code_update();
1840 ret
= consumer_send_status_msg(sock
, ret_code
);
1842 /* Somehow, the session daemon is not responding anymore. */
1845 health_code_update();
1848 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1851 uint64_t discarded_events
;
1852 struct lttng_ht_iter iter
;
1853 struct lttng_ht
*ht
;
1854 struct lttng_consumer_stream
*stream
;
1855 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1856 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1858 DBG("UST consumer discarded events command for session id %"
1861 pthread_mutex_lock(&consumer_data
.lock
);
1863 ht
= consumer_data
.stream_list_ht
;
1866 * We only need a reference to the channel, but they are not
1867 * directly indexed, so we just use the first matching stream
1868 * to extract the information we need, we default to 0 if not
1869 * found (no events are dropped if the channel is not yet in
1872 discarded_events
= 0;
1873 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1874 ht
->hash_fct(&id
, lttng_ht_seed
),
1876 &iter
.iter
, stream
, node_session_id
.node
) {
1877 if (stream
->chan
->key
== key
) {
1878 discarded_events
= stream
->chan
->discarded_events
;
1882 pthread_mutex_unlock(&consumer_data
.lock
);
1885 DBG("UST consumer discarded events command for session id %"
1886 PRIu64
", channel key %" PRIu64
, id
, key
);
1888 health_code_update();
1890 /* Send back returned value to session daemon */
1891 ret
= lttcomm_send_unix_sock(sock
, &discarded_events
, sizeof(discarded_events
));
1893 PERROR("send discarded events");
1899 case LTTNG_CONSUMER_LOST_PACKETS
:
1902 uint64_t lost_packets
;
1903 struct lttng_ht_iter iter
;
1904 struct lttng_ht
*ht
;
1905 struct lttng_consumer_stream
*stream
;
1906 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1907 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1909 DBG("UST consumer lost packets command for session id %"
1912 pthread_mutex_lock(&consumer_data
.lock
);
1914 ht
= consumer_data
.stream_list_ht
;
1917 * We only need a reference to the channel, but they are not
1918 * directly indexed, so we just use the first matching stream
1919 * to extract the information we need, we default to 0 if not
1920 * found (no packets lost if the channel is not yet in use).
1923 cds_lfht_for_each_entry_duplicate(ht
->ht
,
1924 ht
->hash_fct(&id
, lttng_ht_seed
),
1926 &iter
.iter
, stream
, node_session_id
.node
) {
1927 if (stream
->chan
->key
== key
) {
1928 lost_packets
= stream
->chan
->lost_packets
;
1932 pthread_mutex_unlock(&consumer_data
.lock
);
1935 DBG("UST consumer lost packets command for session id %"
1936 PRIu64
", channel key %" PRIu64
, id
, key
);
1938 health_code_update();
1940 /* Send back returned value to session daemon */
1941 ret
= lttcomm_send_unix_sock(sock
, &lost_packets
,
1942 sizeof(lost_packets
));
1944 PERROR("send lost packets");
1950 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1952 int channel_monitor_pipe
;
1954 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1955 /* Successfully received the command's type. */
1956 ret
= consumer_send_status_msg(sock
, ret_code
);
1961 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1963 if (ret
!= sizeof(channel_monitor_pipe
)) {
1964 ERR("Failed to receive channel monitor pipe");
1968 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1969 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1970 channel_monitor_pipe
);
1974 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1975 /* Set the pipe as non-blocking. */
1976 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1978 PERROR("fcntl get flags of the channel monitoring pipe");
1983 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1984 flags
| O_NONBLOCK
);
1986 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1989 DBG("Channel monitor pipe set as non-blocking");
1991 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1993 goto end_msg_sessiond
;
1995 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1997 struct lttng_consumer_channel
*channel
;
1998 uint64_t key
= msg
.u
.rotate_channel
.key
;
2000 channel
= consumer_find_channel(key
);
2002 DBG("Channel %" PRIu64
" not found", key
);
2003 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
2006 * Sample the rotate position of all the streams in
2009 ret
= lttng_consumer_rotate_channel(channel
, key
,
2010 msg
.u
.rotate_channel
.relayd_id
,
2011 msg
.u
.rotate_channel
.metadata
,
2014 ERR("Rotate channel failed");
2015 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
2018 health_code_update();
2020 ret
= consumer_send_status_msg(sock
, ret_code
);
2022 /* Somehow, the session daemon is not responding anymore. */
2023 goto end_rotate_channel_nosignal
;
2027 * Rotate the streams that are ready right now.
2028 * FIXME: this is a second consecutive iteration over the
2029 * streams in a channel, there is probably a better way to
2030 * handle this, but it needs to be after the
2031 * consumer_send_status_msg() call.
2034 ret
= lttng_consumer_rotate_ready_streams(
2037 ERR("Rotate channel failed");
2041 end_rotate_channel_nosignal
:
2044 case LTTNG_CONSUMER_INIT
:
2046 ret_code
= lttng_consumer_init_command(ctx
,
2047 msg
.u
.init
.sessiond_uuid
);
2048 health_code_update();
2049 ret
= consumer_send_status_msg(sock
, ret_code
);
2051 /* Somehow, the session daemon is not responding anymore. */
2056 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
2058 const struct lttng_credentials credentials
= {
2059 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
2060 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
2062 const bool is_local_trace
=
2063 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
2064 const uint64_t relayd_id
=
2065 msg
.u
.create_trace_chunk
.relayd_id
.value
;
2066 const char *chunk_override_name
=
2067 *msg
.u
.create_trace_chunk
.override_name
?
2068 msg
.u
.create_trace_chunk
.override_name
:
2070 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
2071 LTTNG_OPTIONAL_INIT
;
2074 * The session daemon will only provide a chunk directory file
2075 * descriptor for local traces.
2077 if (is_local_trace
) {
2080 /* Acnowledge the reception of the command. */
2081 ret
= consumer_send_status_msg(sock
,
2082 LTTCOMM_CONSUMERD_SUCCESS
);
2084 /* Somehow, the session daemon is not responding anymore. */
2088 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
2089 if (ret
!= sizeof(chunk_dirfd
)) {
2090 ERR("Failed to receive trace chunk directory file descriptor");
2094 DBG("Received trace chunk directory fd (%d)",
2096 ret
= lttng_directory_handle_init_from_dirfd(
2097 &chunk_directory_handle
.value
,
2100 ERR("Failed to initialize chunk directory handle from directory file descriptor");
2101 if (close(chunk_dirfd
)) {
2102 PERROR("Failed to close chunk directory file descriptor");
2106 chunk_directory_handle
.is_set
= true;
2109 ret_code
= lttng_consumer_create_trace_chunk(
2110 !is_local_trace
? &relayd_id
: NULL
,
2111 msg
.u
.create_trace_chunk
.session_id
,
2112 msg
.u
.create_trace_chunk
.chunk_id
,
2113 (time_t) msg
.u
.create_trace_chunk
2114 .creation_timestamp
,
2115 chunk_override_name
,
2116 msg
.u
.create_trace_chunk
.credentials
.is_set
?
2119 chunk_directory_handle
.is_set
?
2120 &chunk_directory_handle
.value
:
2123 if (chunk_directory_handle
.is_set
) {
2124 lttng_directory_handle_fini(
2125 &chunk_directory_handle
.value
);
2127 goto end_msg_sessiond
;
2129 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
2131 enum lttng_trace_chunk_command_type close_command
=
2132 msg
.u
.close_trace_chunk
.close_command
.value
;
2133 const uint64_t relayd_id
=
2134 msg
.u
.close_trace_chunk
.relayd_id
.value
;
2135 struct lttcomm_consumer_close_trace_chunk_reply reply
;
2136 char closed_trace_chunk_path
[LTTNG_PATH_MAX
];
2139 ret_code
= lttng_consumer_close_trace_chunk(
2140 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
2143 msg
.u
.close_trace_chunk
.session_id
,
2144 msg
.u
.close_trace_chunk
.chunk_id
,
2145 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
2146 msg
.u
.close_trace_chunk
.close_command
.is_set
?
2148 NULL
, closed_trace_chunk_path
);
2149 reply
.ret_code
= ret_code
;
2150 reply
.path_length
= strlen(closed_trace_chunk_path
) + 1;
2151 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
2152 if (ret
!= sizeof(reply
)) {
2155 ret
= lttcomm_send_unix_sock(sock
, closed_trace_chunk_path
,
2157 if (ret
!= reply
.path_length
) {
2162 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
2164 const uint64_t relayd_id
=
2165 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
2167 ret_code
= lttng_consumer_trace_chunk_exists(
2168 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
2170 msg
.u
.trace_chunk_exists
.session_id
,
2171 msg
.u
.trace_chunk_exists
.chunk_id
);
2172 goto end_msg_sessiond
;
2180 * Return 1 to indicate success since the 0 value can be a socket
2181 * shutdown during the recv() or send() call.
2188 * The returned value here is not useful since either way we'll return 1 to
2189 * the caller because the session daemon socket management is done
2190 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2192 ret
= consumer_send_status_msg(sock
, ret_code
);
2202 * Free channel here since no one has a reference to it. We don't
2203 * free after that because a stream can store this pointer.
2205 destroy_channel(channel
);
2207 /* We have to send a status channel message indicating an error. */
2208 ret
= consumer_send_status_channel(sock
, NULL
);
2210 /* Stop everything if session daemon can not be notified. */
2217 /* This will issue a consumer stop. */
2223 health_code_update();
2227 void lttng_ustctl_flush_buffer(struct lttng_consumer_stream
*stream
,
2228 int producer_active
)
2231 assert(stream
->ustream
);
2233 ustctl_flush_buffer(stream
->ustream
, producer_active
);
2237 * Take a snapshot for a specific stream.
2239 * Returns 0 on success, < 0 on error
2241 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2244 assert(stream
->ustream
);
2246 return ustctl_snapshot(stream
->ustream
);
2250 * Sample consumed and produced positions for a specific stream.
2252 * Returns 0 on success, < 0 on error.
2254 int lttng_ustconsumer_sample_snapshot_positions(
2255 struct lttng_consumer_stream
*stream
)
2258 assert(stream
->ustream
);
2260 return ustctl_snapshot_sample_positions(stream
->ustream
);
2264 * Get the produced position
2266 * Returns 0 on success, < 0 on error
2268 int lttng_ustconsumer_get_produced_snapshot(
2269 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2272 assert(stream
->ustream
);
2275 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
2279 * Get the consumed position
2281 * Returns 0 on success, < 0 on error
2283 int lttng_ustconsumer_get_consumed_snapshot(
2284 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2287 assert(stream
->ustream
);
2290 return ustctl_snapshot_get_consumed(stream
->ustream
, pos
);
2293 void lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream
*stream
,
2297 assert(stream
->ustream
);
2299 ustctl_flush_buffer(stream
->ustream
, producer
);
2302 int lttng_ustconsumer_get_current_timestamp(
2303 struct lttng_consumer_stream
*stream
, uint64_t *ts
)
2306 assert(stream
->ustream
);
2309 return ustctl_get_current_timestamp(stream
->ustream
, ts
);
2312 int lttng_ustconsumer_get_sequence_number(
2313 struct lttng_consumer_stream
*stream
, uint64_t *seq
)
2316 assert(stream
->ustream
);
2319 return ustctl_get_sequence_number(stream
->ustream
, seq
);
2323 * Called when the stream signals the consumer that it has hung up.
2325 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
2328 assert(stream
->ustream
);
2330 pthread_mutex_lock(&stream
->lock
);
2331 if (!stream
->quiescent
) {
2332 ustctl_flush_buffer(stream
->ustream
, 0);
2333 stream
->quiescent
= true;
2335 pthread_mutex_unlock(&stream
->lock
);
2336 stream
->hangup_flush_done
= 1;
2339 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
2344 assert(chan
->uchan
);
2345 assert(chan
->buffer_credentials
.is_set
);
2347 if (chan
->switch_timer_enabled
== 1) {
2348 consumer_timer_switch_stop(chan
);
2350 for (i
= 0; i
< chan
->nr_stream_fds
; i
++) {
2353 ret
= close(chan
->stream_fds
[i
]);
2357 if (chan
->shm_path
[0]) {
2358 char shm_path
[PATH_MAX
];
2360 ret
= get_stream_shm_path(shm_path
, chan
->shm_path
, i
);
2362 ERR("Cannot get stream shm path");
2364 ret
= run_as_unlink(shm_path
,
2365 chan
->buffer_credentials
.value
.uid
,
2366 chan
->buffer_credentials
.value
.gid
);
2368 PERROR("unlink %s", shm_path
);
2374 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel
*chan
)
2377 assert(chan
->uchan
);
2378 assert(chan
->buffer_credentials
.is_set
);
2380 consumer_metadata_cache_destroy(chan
);
2381 ustctl_destroy_channel(chan
->uchan
);
2382 /* Try to rmdir all directories under shm_path root. */
2383 if (chan
->root_shm_path
[0]) {
2384 (void) run_as_rmdir_recursive(chan
->root_shm_path
,
2385 chan
->buffer_credentials
.value
.uid
,
2386 chan
->buffer_credentials
.value
.gid
,
2387 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
2389 free(chan
->stream_fds
);
2392 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
2395 assert(stream
->ustream
);
2397 if (stream
->chan
->switch_timer_enabled
== 1) {
2398 consumer_timer_switch_stop(stream
->chan
);
2400 ustctl_destroy_stream(stream
->ustream
);
2403 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream
*stream
)
2406 assert(stream
->ustream
);
2408 return ustctl_stream_get_wakeup_fd(stream
->ustream
);
2411 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream
*stream
)
2414 assert(stream
->ustream
);
2416 return ustctl_stream_close_wakeup_fd(stream
->ustream
);
2420 void metadata_stream_reset_cache_consumed_position(
2421 struct lttng_consumer_stream
*stream
)
2423 DBG("Reset metadata cache of session %" PRIu64
,
2424 stream
->chan
->session_id
);
2425 stream
->ust_metadata_pushed
= 0;
2429 * Write up to one packet from the metadata cache to the channel.
2431 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2432 * negative value on error.
2435 int commit_one_metadata_packet(struct lttng_consumer_stream
*stream
)
2440 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2441 if (stream
->chan
->metadata_cache
->max_offset
==
2442 stream
->ust_metadata_pushed
) {
2444 * In the context of a user space metadata channel, a
2445 * change in version can be detected in two ways:
2446 * 1) During the pre-consume of the `read_subbuffer` loop,
2447 * 2) When populating the metadata ring buffer (i.e. here).
2449 * This function is invoked when there is no metadata
2450 * available in the ring-buffer. If all data was consumed
2451 * up to the size of the metadata cache, there is no metadata
2452 * to insert in the ring-buffer.
2454 * However, the metadata version could still have changed (a
2455 * regeneration without any new data will yield the same cache
2458 * The cache's version is checked for a version change and the
2459 * consumed position is reset if one occurred.
2461 * This check is only necessary for the user space domain as
2462 * it has to manage the cache explicitly. If this reset was not
2463 * performed, no metadata would be consumed (and no reset would
2464 * occur as part of the pre-consume) until the metadata size
2465 * exceeded the cache size.
2467 if (stream
->metadata_version
!=
2468 stream
->chan
->metadata_cache
->version
) {
2469 metadata_stream_reset_cache_consumed_position(stream
);
2470 consumer_stream_metadata_set_version(stream
,
2471 stream
->chan
->metadata_cache
->version
);
2478 write_len
= ustctl_write_one_packet_to_channel(stream
->chan
->uchan
,
2479 &stream
->chan
->metadata_cache
->data
[stream
->ust_metadata_pushed
],
2480 stream
->chan
->metadata_cache
->max_offset
2481 - stream
->ust_metadata_pushed
);
2482 assert(write_len
!= 0);
2483 if (write_len
< 0) {
2484 ERR("Writing one metadata packet");
2488 stream
->ust_metadata_pushed
+= write_len
;
2490 assert(stream
->chan
->metadata_cache
->max_offset
>=
2491 stream
->ust_metadata_pushed
);
2495 * Switch packet (but don't open the next one) on every commit of
2496 * a metadata packet. Since the subbuffer is fully filled (with padding,
2497 * if needed), the stream is "quiescent" after this commit.
2499 ustctl_flush_buffer(stream
->ustream
, 1);
2500 stream
->quiescent
= true;
2502 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2508 * Sync metadata meaning request them to the session daemon and snapshot to the
2509 * metadata thread can consumer them.
2511 * Metadata stream lock is held here, but we need to release it when
2512 * interacting with sessiond, else we cause a deadlock with live
2513 * awaiting on metadata to be pushed out.
2515 * The RCU read side lock must be held by the caller.
2517 enum sync_metadata_status
lttng_ustconsumer_sync_metadata(
2518 struct lttng_consumer_local_data
*ctx
,
2519 struct lttng_consumer_stream
*metadata_stream
)
2522 enum sync_metadata_status status
;
2523 struct lttng_consumer_channel
*metadata_channel
;
2526 assert(metadata_stream
);
2528 metadata_channel
= metadata_stream
->chan
;
2529 pthread_mutex_unlock(&metadata_stream
->lock
);
2531 * Request metadata from the sessiond, but don't wait for the flush
2532 * because we locked the metadata thread.
2534 ret
= lttng_ustconsumer_request_metadata(ctx
, metadata_channel
, 0, 0);
2535 pthread_mutex_lock(&metadata_stream
->lock
);
2537 status
= SYNC_METADATA_STATUS_ERROR
;
2542 * The metadata stream and channel can be deleted while the
2543 * metadata stream lock was released. The streamed is checked
2544 * for deletion before we use it further.
2546 * Note that it is safe to access a logically-deleted stream since its
2547 * existence is still guaranteed by the RCU read side lock. However,
2548 * it should no longer be used. The close/deletion of the metadata
2549 * channel and stream already guarantees that all metadata has been
2550 * consumed. Therefore, there is nothing left to do in this function.
2552 if (consumer_stream_is_deleted(metadata_stream
)) {
2553 DBG("Metadata stream %" PRIu64
" was deleted during the metadata synchronization",
2554 metadata_stream
->key
);
2555 status
= SYNC_METADATA_STATUS_NO_DATA
;
2559 ret
= commit_one_metadata_packet(metadata_stream
);
2561 status
= SYNC_METADATA_STATUS_ERROR
;
2563 } else if (ret
> 0) {
2564 status
= SYNC_METADATA_STATUS_NEW_DATA
;
2565 } else /* ret == 0 */ {
2566 status
= SYNC_METADATA_STATUS_NO_DATA
;
2570 ret
= ustctl_snapshot(metadata_stream
->ustream
);
2572 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret
);
2573 status
= SYNC_METADATA_STATUS_ERROR
;
2582 * Return 0 on success else a negative value.
2584 static int notify_if_more_data(struct lttng_consumer_stream
*stream
,
2585 struct lttng_consumer_local_data
*ctx
)
2588 struct ustctl_consumer_stream
*ustream
;
2593 ustream
= stream
->ustream
;
2596 * First, we are going to check if there is a new subbuffer available
2597 * before reading the stream wait_fd.
2599 /* Get the next subbuffer */
2600 ret
= ustctl_get_next_subbuf(ustream
);
2602 /* No more data found, flag the stream. */
2603 stream
->has_data
= 0;
2608 ret
= ustctl_put_subbuf(ustream
);
2611 /* This stream still has data. Flag it and wake up the data thread. */
2612 stream
->has_data
= 1;
2614 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !ctx
->has_wakeup
) {
2617 writelen
= lttng_pipe_write(ctx
->consumer_wakeup_pipe
, "!", 1);
2618 if (writelen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2623 /* The wake up pipe has been notified. */
2624 ctx
->has_wakeup
= 1;
2632 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream
*stream
)
2637 * We can consume the 1 byte written into the wait_fd by
2638 * UST. Don't trigger error if we cannot read this one byte
2639 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2641 * This is only done when the stream is monitored by a thread,
2642 * before the flush is done after a hangup and if the stream
2643 * is not flagged with data since there might be nothing to
2644 * consume in the wait fd but still have data available
2645 * flagged by the consumer wake up pipe.
2647 if (stream
->monitor
&& !stream
->hangup_flush_done
&& !stream
->has_data
) {
2651 readlen
= lttng_read(stream
->wait_fd
, &dummy
, 1);
2652 if (readlen
< 0 && errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) {
2660 static int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
2661 struct stream_subbuffer
*subbuf
)
2665 ret
= ustctl_get_subbuf_size(
2666 stream
->ustream
, &subbuf
->info
.data
.subbuf_size
);
2671 ret
= ustctl_get_padded_subbuf_size(
2672 stream
->ustream
, &subbuf
->info
.data
.padded_subbuf_size
);
2681 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
2682 struct stream_subbuffer
*subbuf
)
2686 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2691 subbuf
->info
.metadata
.version
= stream
->metadata_version
;
2697 static int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
2698 struct stream_subbuffer
*subbuf
)
2702 ret
= extract_common_subbuffer_info(stream
, subbuf
);
2707 ret
= ustctl_get_packet_size(
2708 stream
->ustream
, &subbuf
->info
.data
.packet_size
);
2710 PERROR("Failed to get sub-buffer packet size");
2714 ret
= ustctl_get_content_size(
2715 stream
->ustream
, &subbuf
->info
.data
.content_size
);
2717 PERROR("Failed to get sub-buffer content size");
2721 ret
= ustctl_get_timestamp_begin(
2722 stream
->ustream
, &subbuf
->info
.data
.timestamp_begin
);
2724 PERROR("Failed to get sub-buffer begin timestamp");
2728 ret
= ustctl_get_timestamp_end(
2729 stream
->ustream
, &subbuf
->info
.data
.timestamp_end
);
2731 PERROR("Failed to get sub-buffer end timestamp");
2735 ret
= ustctl_get_events_discarded(
2736 stream
->ustream
, &subbuf
->info
.data
.events_discarded
);
2738 PERROR("Failed to get sub-buffer events discarded count");
2742 ret
= ustctl_get_sequence_number(stream
->ustream
,
2743 &subbuf
->info
.data
.sequence_number
.value
);
2745 /* May not be supported by older LTTng-modules. */
2746 if (ret
!= -ENOTTY
) {
2747 PERROR("Failed to get sub-buffer sequence number");
2751 subbuf
->info
.data
.sequence_number
.is_set
= true;
2754 ret
= ustctl_get_stream_id(
2755 stream
->ustream
, &subbuf
->info
.data
.stream_id
);
2757 PERROR("Failed to get stream id");
2761 ret
= ustctl_get_instance_id(stream
->ustream
,
2762 &subbuf
->info
.data
.stream_instance_id
.value
);
2764 /* May not be supported by older LTTng-modules. */
2765 if (ret
!= -ENOTTY
) {
2766 PERROR("Failed to get stream instance id");
2770 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
2776 static int get_next_subbuffer_common(struct lttng_consumer_stream
*stream
,
2777 struct stream_subbuffer
*subbuffer
)
2782 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
2788 ret
= get_current_subbuf_addr(stream
, &addr
);
2793 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
2794 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
2795 assert(subbuffer
->buffer
.buffer
.data
!= NULL
);
2800 static int get_next_subbuffer(struct lttng_consumer_stream
*stream
,
2801 struct stream_subbuffer
*subbuffer
)
2805 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2810 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2818 static int get_next_subbuffer_metadata(struct lttng_consumer_stream
*stream
,
2819 struct stream_subbuffer
*subbuffer
)
2826 unsigned long consumed_pos
, produced_pos
;
2829 ret
= ustctl_get_next_subbuf(stream
->ustream
);
2831 got_subbuffer
= true;
2833 got_subbuffer
= false;
2834 if (ret
!= -EAGAIN
) {
2841 * Determine if the cache is empty and ensure that a sub-buffer
2842 * is made available if the cache is not empty.
2844 if (!got_subbuffer
) {
2845 ret
= commit_one_metadata_packet(stream
);
2846 if (ret
< 0 && ret
!= -ENOBUFS
) {
2848 } else if (ret
== 0) {
2849 /* Not an error, the cache is empty. */
2854 cache_empty
= false;
2857 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2858 cache_empty
= stream
->chan
->metadata_cache
->max_offset
==
2859 stream
->ust_metadata_pushed
;
2860 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2862 } while (!got_subbuffer
);
2864 /* Populate sub-buffer infos and view. */
2865 ret
= get_next_subbuffer_common(stream
, subbuffer
);
2870 ret
= lttng_ustconsumer_sample_snapshot_positions(stream
);
2873 * -EAGAIN is not expected since we got a sub-buffer and haven't
2874 * pushed the consumption position yet (on put_next).
2876 PERROR("Failed to take a snapshot of metadata buffer positions");
2880 ret
= lttng_ustconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
2882 PERROR("Failed to get metadata consumed position");
2886 ret
= lttng_ustconsumer_get_produced_snapshot(stream
, &produced_pos
);
2888 PERROR("Failed to get metadata produced position");
2892 /* Last sub-buffer of the ring buffer ? */
2893 buffer_empty
= (consumed_pos
+ stream
->max_sb_size
) == produced_pos
;
2896 * The sessiond registry lock ensures that coherent units of metadata
2897 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
2898 * acquired, the cache is empty, and it is the only available sub-buffer
2899 * available, it is safe to assume that it is "coherent".
2901 coherent
= got_subbuffer
&& cache_empty
&& buffer_empty
;
2903 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
2908 static int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
2909 struct stream_subbuffer
*subbuffer
)
2911 const int ret
= ustctl_put_next_subbuf(stream
->ustream
);
2917 static int signal_metadata(struct lttng_consumer_stream
*stream
,
2918 struct lttng_consumer_local_data
*ctx
)
2920 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
2923 static int lttng_ustconsumer_set_stream_ops(
2924 struct lttng_consumer_stream
*stream
)
2928 stream
->read_subbuffer_ops
.on_wake_up
= consumer_stream_ust_on_wake_up
;
2929 if (stream
->metadata_flag
) {
2930 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2931 get_next_subbuffer_metadata
;
2932 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2933 extract_metadata_subbuffer_info
;
2934 stream
->read_subbuffer_ops
.reset_metadata
=
2935 metadata_stream_reset_cache_consumed_position
;
2936 if (stream
->chan
->is_live
) {
2937 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
2938 ret
= consumer_stream_enable_metadata_bucketization(
2945 stream
->read_subbuffer_ops
.get_next_subbuffer
=
2947 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
2948 extract_data_subbuffer_info
;
2949 stream
->read_subbuffer_ops
.on_sleep
= notify_if_more_data
;
2950 if (stream
->chan
->is_live
) {
2951 stream
->read_subbuffer_ops
.send_live_beacon
=
2952 consumer_flush_ust_index
;
2956 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
2962 * Called when a stream is created.
2964 * Return 0 on success or else a negative value.
2966 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2973 * Don't create anything if this is set for streaming or if there is
2974 * no current trace chunk on the parent channel.
2976 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
2977 stream
->chan
->trace_chunk
) {
2978 ret
= consumer_stream_create_output_files(stream
, true);
2984 lttng_ustconsumer_set_stream_ops(stream
);
2992 * Check if data is still being extracted from the buffers for a specific
2993 * stream. Consumer data lock MUST be acquired before calling this function
2994 * and the stream lock.
2996 * Return 1 if the traced data are still getting read else 0 meaning that the
2997 * data is available for trace viewer reading.
2999 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
3004 assert(stream
->ustream
);
3006 DBG("UST consumer checking data pending");
3008 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
3013 if (stream
->chan
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
3014 uint64_t contiguous
, pushed
;
3016 /* Ease our life a bit. */
3017 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
3018 contiguous
= stream
->chan
->metadata_cache
->max_offset
;
3019 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
3020 pushed
= stream
->ust_metadata_pushed
;
3023 * We can simply check whether all contiguously available data
3024 * has been pushed to the ring buffer, since the push operation
3025 * is performed within get_next_subbuf(), and because both
3026 * get_next_subbuf() and put_next_subbuf() are issued atomically
3027 * thanks to the stream lock within
3028 * lttng_ustconsumer_read_subbuffer(). This basically means that
3029 * whetnever ust_metadata_pushed is incremented, the associated
3030 * metadata has been consumed from the metadata stream.
3032 DBG("UST consumer metadata pending check: contiguous %" PRIu64
" vs pushed %" PRIu64
,
3033 contiguous
, pushed
);
3034 assert(((int64_t) (contiguous
- pushed
)) >= 0);
3035 if ((contiguous
!= pushed
) ||
3036 (((int64_t) contiguous
- pushed
) > 0 || contiguous
== 0)) {
3037 ret
= 1; /* Data is pending */
3041 ret
= ustctl_get_next_subbuf(stream
->ustream
);
3044 * There is still data so let's put back this
3047 ret
= ustctl_put_subbuf(stream
->ustream
);
3049 ret
= 1; /* Data is pending */
3054 /* Data is NOT pending so ready to be read. */
3062 * Stop a given metadata channel timer if enabled and close the wait fd which
3063 * is the poll pipe of the metadata stream.
3065 * This MUST be called with the metadata channel lock acquired.
3067 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel
*metadata
)
3072 assert(metadata
->type
== CONSUMER_CHANNEL_TYPE_METADATA
);
3074 DBG("Closing metadata channel key %" PRIu64
, metadata
->key
);
3076 if (metadata
->switch_timer_enabled
== 1) {
3077 consumer_timer_switch_stop(metadata
);
3080 if (!metadata
->metadata_stream
) {
3085 * Closing write side so the thread monitoring the stream wakes up if any
3086 * and clean the metadata stream.
3088 if (metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] >= 0) {
3089 ret
= close(metadata
->metadata_stream
->ust_metadata_poll_pipe
[1]);
3091 PERROR("closing metadata pipe write side");
3093 metadata
->metadata_stream
->ust_metadata_poll_pipe
[1] = -1;
3101 * Close every metadata stream wait fd of the metadata hash table. This
3102 * function MUST be used very carefully so not to run into a race between the
3103 * metadata thread handling streams and this function closing their wait fd.
3105 * For UST, this is used when the session daemon hangs up. Its the metadata
3106 * producer so calling this is safe because we are assured that no state change
3107 * can occur in the metadata thread for the streams in the hash table.
3109 void lttng_ustconsumer_close_all_metadata(struct lttng_ht
*metadata_ht
)
3111 struct lttng_ht_iter iter
;
3112 struct lttng_consumer_stream
*stream
;
3114 assert(metadata_ht
);
3115 assert(metadata_ht
->ht
);
3117 DBG("UST consumer closing all metadata streams");
3120 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
3123 health_code_update();
3125 pthread_mutex_lock(&stream
->chan
->lock
);
3126 lttng_ustconsumer_close_metadata(stream
->chan
);
3127 pthread_mutex_unlock(&stream
->chan
->lock
);
3133 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
3137 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
3139 ERR("Unable to close wakeup fd");
3144 * Please refer to consumer-timer.c before adding any lock within this
3145 * function or any of its callees. Timers have a very strict locking
3146 * semantic with respect to teardown. Failure to respect this semantic
3147 * introduces deadlocks.
3149 * DON'T hold the metadata lock when calling this function, else this
3150 * can cause deadlock involving consumer awaiting for metadata to be
3151 * pushed out due to concurrent interaction with the session daemon.
3153 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
3154 struct lttng_consumer_channel
*channel
, int timer
, int wait
)
3156 struct lttcomm_metadata_request_msg request
;
3157 struct lttcomm_consumer_msg msg
;
3158 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3159 uint64_t len
, key
, offset
, version
;
3163 assert(channel
->metadata_cache
);
3165 memset(&request
, 0, sizeof(request
));
3167 /* send the metadata request to sessiond */
3168 switch (consumer_data
.type
) {
3169 case LTTNG_CONSUMER64_UST
:
3170 request
.bits_per_long
= 64;
3172 case LTTNG_CONSUMER32_UST
:
3173 request
.bits_per_long
= 32;
3176 request
.bits_per_long
= 0;
3180 request
.session_id
= channel
->session_id
;
3181 request
.session_id_per_pid
= channel
->session_id_per_pid
;
3183 * Request the application UID here so the metadata of that application can
3184 * be sent back. The channel UID corresponds to the user UID of the session
3185 * used for the rights on the stream file(s).
3187 request
.uid
= channel
->ust_app_uid
;
3188 request
.key
= channel
->key
;
3190 DBG("Sending metadata request to sessiond, session id %" PRIu64
3191 ", per-pid %" PRIu64
", app UID %u and channel key %" PRIu64
,
3192 request
.session_id
, request
.session_id_per_pid
, request
.uid
,
3195 pthread_mutex_lock(&ctx
->metadata_socket_lock
);
3197 health_code_update();
3199 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
3202 ERR("Asking metadata to sessiond");
3206 health_code_update();
3208 /* Receive the metadata from sessiond */
3209 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
3211 if (ret
!= sizeof(msg
)) {
3212 DBG("Consumer received unexpected message size %d (expects %zu)",
3214 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
3216 * The ret value might 0 meaning an orderly shutdown but this is ok
3217 * since the caller handles this.
3222 health_code_update();
3224 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
3225 /* No registry found */
3226 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3230 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
3231 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
3236 len
= msg
.u
.push_metadata
.len
;
3237 key
= msg
.u
.push_metadata
.key
;
3238 offset
= msg
.u
.push_metadata
.target_offset
;
3239 version
= msg
.u
.push_metadata
.version
;
3241 assert(key
== channel
->key
);
3243 DBG("No new metadata to receive for key %" PRIu64
, key
);
3246 health_code_update();
3248 /* Tell session daemon we are ready to receive the metadata. */
3249 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
3250 LTTCOMM_CONSUMERD_SUCCESS
);
3251 if (ret
< 0 || len
== 0) {
3253 * Somehow, the session daemon is not responding anymore or there is
3254 * nothing to receive.
3259 health_code_update();
3261 ret
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
3262 key
, offset
, len
, version
, channel
, timer
, wait
);
3265 * Only send the status msg if the sessiond is alive meaning a positive
3268 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret
);
3273 health_code_update();
3275 pthread_mutex_unlock(&ctx
->metadata_socket_lock
);
3280 * Return the ustctl call for the get stream id.
3282 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream
*stream
,
3283 uint64_t *stream_id
)
3288 return ustctl_get_stream_id(stream
->ustream
, stream_id
);