2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/socket.h>
18 #include <sys/types.h>
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/sessiond-comm/relayd.h>
29 #include <common/compat/fcntl.h>
30 #include <common/compat/endian.h>
31 #include <common/pipe.h>
32 #include <common/relayd/relayd.h>
33 #include <common/utils.h>
34 #include <common/consumer/consumer-stream.h>
35 #include <common/index/index.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/optional.h>
38 #include <common/buffer-view.h>
39 #include <common/consumer/consumer.h>
40 #include <common/consumer/metadata-bucket.h>
42 #include "kernel-consumer.h"
44 extern struct lttng_consumer_global_data the_consumer_data
;
45 extern int consumer_poll_timeout
;
48 * Take a snapshot for a specific fd
50 * Returns 0 on success, < 0 on error
52 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
55 int infd
= stream
->wait_fd
;
57 ret
= kernctl_snapshot(infd
);
59 * -EAGAIN is not an error, it just means that there is no data to
62 if (ret
!= 0 && ret
!= -EAGAIN
) {
63 PERROR("Getting sub-buffer snapshot.");
70 * Sample consumed and produced positions for a specific fd.
72 * Returns 0 on success, < 0 on error.
74 int lttng_kconsumer_sample_snapshot_positions(
75 struct lttng_consumer_stream
*stream
)
79 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
83 * Get the produced position
85 * Returns 0 on success, < 0 on error
87 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
91 int infd
= stream
->wait_fd
;
93 ret
= kernctl_snapshot_get_produced(infd
, pos
);
95 PERROR("kernctl_snapshot_get_produced");
102 * Get the consumerd position
104 * Returns 0 on success, < 0 on error
106 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
110 int infd
= stream
->wait_fd
;
112 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
114 PERROR("kernctl_snapshot_get_consumed");
121 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
125 unsigned long mmap_offset
;
126 const char *mmap_base
= stream
->mmap_base
;
128 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
130 PERROR("Failed to get mmap read offset");
134 *addr
= mmap_base
+ mmap_offset
;
140 * Take a snapshot of all the stream of a channel
141 * RCU read-side lock must be held across this function to ensure existence of
144 * Returns 0 on success, < 0 on error
146 static int lttng_kconsumer_snapshot_channel(
147 struct lttng_consumer_channel
*channel
,
148 uint64_t key
, char *path
, uint64_t relayd_id
,
149 uint64_t nb_packets_per_stream
,
150 struct lttng_consumer_local_data
*ctx
)
153 struct lttng_consumer_stream
*stream
;
155 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
157 /* Prevent channel modifications while we perform the snapshot.*/
158 pthread_mutex_lock(&channel
->lock
);
162 /* Splice is not supported yet for channel snapshot. */
163 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
164 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
170 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
171 unsigned long consumed_pos
, produced_pos
;
173 health_code_update();
176 * Lock stream because we are about to change its state.
178 pthread_mutex_lock(&stream
->lock
);
180 assert(channel
->trace_chunk
);
181 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
183 * Can't happen barring an internal error as the channel
184 * holds a reference to the trace chunk.
186 ERR("Failed to acquire reference to channel's trace chunk");
190 assert(!stream
->trace_chunk
);
191 stream
->trace_chunk
= channel
->trace_chunk
;
194 * Assign the received relayd ID so we can use it for streaming. The streams
195 * are not visible to anyone so this is OK to change it.
197 stream
->net_seq_idx
= relayd_id
;
198 channel
->relayd_id
= relayd_id
;
199 if (relayd_id
!= (uint64_t) -1ULL) {
200 ret
= consumer_send_relayd_stream(stream
, path
);
202 ERR("sending stream to relayd");
206 ret
= consumer_stream_create_output_files(stream
,
211 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
215 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
218 * Doing a buffer flush which does not take into
219 * account empty packets. This is not perfect
220 * for stream intersection, but required as a
221 * fall-back when "flush_empty" is not
222 * implemented by lttng-modules.
224 ret
= kernctl_buffer_flush(stream
->wait_fd
);
226 ERR("Failed to flush kernel stream");
232 ret
= lttng_kconsumer_take_snapshot(stream
);
234 ERR("Taking kernel snapshot");
238 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
240 ERR("Produced kernel snapshot position");
244 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
246 ERR("Consumerd kernel snapshot position");
250 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
251 produced_pos
, nb_packets_per_stream
,
252 stream
->max_sb_size
);
254 while ((long) (consumed_pos
- produced_pos
) < 0) {
256 unsigned long len
, padded_len
;
257 const char *subbuf_addr
;
258 struct lttng_buffer_view subbuf_view
;
260 health_code_update();
261 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
263 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
265 if (ret
!= -EAGAIN
) {
266 PERROR("kernctl_get_subbuf snapshot");
269 DBG("Kernel consumer get subbuf failed. Skipping it.");
270 consumed_pos
+= stream
->max_sb_size
;
271 stream
->chan
->lost_packets
++;
275 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
277 ERR("Snapshot kernctl_get_subbuf_size");
278 goto error_put_subbuf
;
281 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
283 ERR("Snapshot kernctl_get_padded_subbuf_size");
284 goto error_put_subbuf
;
287 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
289 goto error_put_subbuf
;
292 subbuf_view
= lttng_buffer_view_init(
293 subbuf_addr
, 0, padded_len
);
294 read_len
= lttng_consumer_on_read_subbuffer_mmap(
295 stream
, &subbuf_view
,
298 * We write the padded len in local tracefiles but the data len
299 * when using a relay. Display the error but continue processing
300 * to try to release the subbuffer.
302 if (relayd_id
!= (uint64_t) -1ULL) {
303 if (read_len
!= len
) {
304 ERR("Error sending to the relay (ret: %zd != len: %lu)",
308 if (read_len
!= padded_len
) {
309 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
310 read_len
, padded_len
);
314 ret
= kernctl_put_subbuf(stream
->wait_fd
);
316 ERR("Snapshot kernctl_put_subbuf");
319 consumed_pos
+= stream
->max_sb_size
;
322 if (relayd_id
== (uint64_t) -1ULL) {
323 if (stream
->out_fd
>= 0) {
324 ret
= close(stream
->out_fd
);
326 PERROR("Kernel consumer snapshot close out_fd");
332 close_relayd_stream(stream
);
333 stream
->net_seq_idx
= (uint64_t) -1ULL;
335 lttng_trace_chunk_put(stream
->trace_chunk
);
336 stream
->trace_chunk
= NULL
;
337 pthread_mutex_unlock(&stream
->lock
);
345 ret
= kernctl_put_subbuf(stream
->wait_fd
);
347 ERR("Snapshot kernctl_put_subbuf error path");
350 pthread_mutex_unlock(&stream
->lock
);
353 pthread_mutex_unlock(&channel
->lock
);
358 * Read the whole metadata available for a snapshot.
359 * RCU read-side lock must be held across this function to ensure existence of
362 * Returns 0 on success, < 0 on error
364 static int lttng_kconsumer_snapshot_metadata(
365 struct lttng_consumer_channel
*metadata_channel
,
366 uint64_t key
, char *path
, uint64_t relayd_id
,
367 struct lttng_consumer_local_data
*ctx
)
369 int ret
, use_relayd
= 0;
371 struct lttng_consumer_stream
*metadata_stream
;
375 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
380 metadata_stream
= metadata_channel
->metadata_stream
;
381 assert(metadata_stream
);
383 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
384 assert(metadata_channel
->trace_chunk
);
385 assert(metadata_stream
->trace_chunk
);
387 /* Flag once that we have a valid relayd for the stream. */
388 if (relayd_id
!= (uint64_t) -1ULL) {
393 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
398 ret
= consumer_stream_create_output_files(metadata_stream
,
406 health_code_update();
408 ret_read
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
410 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
415 } while (ret_read
> 0);
418 close_relayd_stream(metadata_stream
);
419 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
421 if (metadata_stream
->out_fd
>= 0) {
422 ret
= close(metadata_stream
->out_fd
);
424 PERROR("Kernel consumer snapshot metadata close out_fd");
426 * Don't go on error here since the snapshot was successful at this
427 * point but somehow the close failed.
430 metadata_stream
->out_fd
= -1;
431 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
432 metadata_stream
->trace_chunk
= NULL
;
438 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
439 cds_list_del(&metadata_stream
->send_node
);
440 consumer_stream_destroy(metadata_stream
, NULL
);
441 metadata_channel
->metadata_stream
= NULL
;
447 * Receive command from session daemon and process it.
449 * Return 1 on success else a negative value or 0.
451 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
452 int sock
, struct pollfd
*consumer_sockpoll
)
455 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
456 struct lttcomm_consumer_msg msg
;
458 health_code_update();
463 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
464 if (ret_recv
!= sizeof(msg
)) {
466 lttng_consumer_send_error(ctx
,
467 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
474 health_code_update();
476 /* Deprecated command */
477 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
479 health_code_update();
481 /* relayd needs RCU read-side protection */
484 switch (msg
.cmd_type
) {
485 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
487 uint32_t major
= msg
.u
.relayd_sock
.major
;
488 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
489 enum lttcomm_sock_proto protocol
= (enum lttcomm_sock_proto
)
490 msg
.u
.relayd_sock
.relayd_socket_protocol
;
492 /* Session daemon status message are handled in the following call. */
493 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
494 msg
.u
.relayd_sock
.type
, ctx
, sock
,
495 consumer_sockpoll
, msg
.u
.relayd_sock
.session_id
,
496 msg
.u
.relayd_sock
.relayd_session_id
, major
,
500 case LTTNG_CONSUMER_ADD_CHANNEL
:
502 struct lttng_consumer_channel
*new_channel
;
503 int ret_send_status
, ret_add_channel
= 0;
504 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
506 health_code_update();
508 /* First send a status message before receiving the fds. */
509 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
510 if (ret_send_status
< 0) {
511 /* Somehow, the session daemon is not responding anymore. */
515 health_code_update();
517 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
518 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
519 msg
.u
.channel
.session_id
,
520 msg
.u
.channel
.chunk_id
.is_set
?
522 msg
.u
.channel
.pathname
,
524 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
525 msg
.u
.channel
.tracefile_size
,
526 msg
.u
.channel
.tracefile_count
, 0,
527 msg
.u
.channel
.monitor
,
528 msg
.u
.channel
.live_timer_interval
,
529 msg
.u
.channel
.is_live
,
531 if (new_channel
== NULL
) {
532 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
535 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
536 switch (msg
.u
.channel
.output
) {
537 case LTTNG_EVENT_SPLICE
:
538 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
540 case LTTNG_EVENT_MMAP
:
541 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
544 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
548 /* Translate and save channel type. */
549 switch (msg
.u
.channel
.type
) {
550 case CONSUMER_CHANNEL_TYPE_DATA
:
551 case CONSUMER_CHANNEL_TYPE_METADATA
:
552 new_channel
->type
= msg
.u
.channel
.type
;
559 health_code_update();
561 if (ctx
->on_recv_channel
!= NULL
) {
562 int ret_recv_channel
=
563 ctx
->on_recv_channel(new_channel
);
564 if (ret_recv_channel
== 0) {
565 ret_add_channel
= consumer_add_channel(
567 } else if (ret_recv_channel
< 0) {
572 consumer_add_channel(new_channel
, ctx
);
574 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&&
576 int monitor_start_ret
;
578 DBG("Consumer starting monitor timer");
579 consumer_timer_live_start(new_channel
,
580 msg
.u
.channel
.live_timer_interval
);
581 monitor_start_ret
= consumer_timer_monitor_start(
583 msg
.u
.channel
.monitor_timer_interval
);
584 if (monitor_start_ret
< 0) {
585 ERR("Starting channel monitoring timer failed");
590 health_code_update();
592 /* If we received an error in add_channel, we need to report it. */
593 if (ret_add_channel
< 0) {
594 ret_send_status
= consumer_send_status_msg(
595 sock
, ret_add_channel
);
596 if (ret_send_status
< 0) {
604 case LTTNG_CONSUMER_ADD_STREAM
:
607 struct lttng_pipe
*stream_pipe
;
608 struct lttng_consumer_stream
*new_stream
;
609 struct lttng_consumer_channel
*channel
;
611 int ret_send_status
, ret_poll
, ret_get_max_subbuf_size
;
612 ssize_t ret_pipe_write
, ret_recv
;
615 * Get stream's channel reference. Needed when adding the stream to the
618 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
621 * We could not find the channel. Can happen if cpu hotplug
622 * happens while tearing down.
624 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
625 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
628 health_code_update();
630 /* First send a status message before receiving the fds. */
631 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
632 if (ret_send_status
< 0) {
633 /* Somehow, the session daemon is not responding anymore. */
634 goto error_add_stream_fatal
;
637 health_code_update();
639 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
640 /* Channel was not found. */
641 goto error_add_stream_nosignal
;
646 ret_poll
= lttng_consumer_poll_socket(consumer_sockpoll
);
649 goto error_add_stream_fatal
;
652 health_code_update();
654 /* Get stream file descriptor from socket */
655 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
656 if (ret_recv
!= sizeof(fd
)) {
657 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
662 health_code_update();
665 * Send status code to session daemon only if the recv works. If the
666 * above recv() failed, the session daemon is notified through the
667 * error socket and the teardown is eventually done.
669 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
670 if (ret_send_status
< 0) {
671 /* Somehow, the session daemon is not responding anymore. */
672 goto error_add_stream_nosignal
;
675 health_code_update();
677 pthread_mutex_lock(&channel
->lock
);
678 new_stream
= consumer_stream_create(
685 channel
->trace_chunk
,
690 if (new_stream
== NULL
) {
695 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
698 pthread_mutex_unlock(&channel
->lock
);
699 goto error_add_stream_nosignal
;
702 new_stream
->wait_fd
= fd
;
703 ret_get_max_subbuf_size
= kernctl_get_max_subbuf_size(
704 new_stream
->wait_fd
, &new_stream
->max_sb_size
);
705 if (ret_get_max_subbuf_size
< 0) {
706 pthread_mutex_unlock(&channel
->lock
);
707 ERR("Failed to get kernel maximal subbuffer size");
708 goto error_add_stream_nosignal
;
711 consumer_stream_update_channel_attributes(new_stream
,
715 * We've just assigned the channel to the stream so increment the
716 * refcount right now. We don't need to increment the refcount for
717 * streams in no monitor because we handle manually the cleanup of
718 * those. It is very important to make sure there is NO prior
719 * consumer_del_stream() calls or else the refcount will be unbalanced.
721 if (channel
->monitor
) {
722 uatomic_inc(&new_stream
->chan
->refcount
);
726 * The buffer flush is done on the session daemon side for the kernel
727 * so no need for the stream "hangup_flush_done" variable to be
728 * tracked. This is important for a kernel stream since we don't rely
729 * on the flush state of the stream to read data. It's not the case for
730 * user space tracing.
732 new_stream
->hangup_flush_done
= 0;
734 health_code_update();
736 pthread_mutex_lock(&new_stream
->lock
);
737 if (ctx
->on_recv_stream
) {
738 int ret_recv_stream
= ctx
->on_recv_stream(new_stream
);
739 if (ret_recv_stream
< 0) {
740 pthread_mutex_unlock(&new_stream
->lock
);
741 pthread_mutex_unlock(&channel
->lock
);
742 consumer_stream_free(new_stream
);
743 goto error_add_stream_nosignal
;
746 health_code_update();
748 if (new_stream
->metadata_flag
) {
749 channel
->metadata_stream
= new_stream
;
752 /* Do not monitor this stream. */
753 if (!channel
->monitor
) {
754 DBG("Kernel consumer add stream %s in no monitor mode with "
755 "relayd id %" PRIu64
, new_stream
->name
,
756 new_stream
->net_seq_idx
);
757 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
758 pthread_mutex_unlock(&new_stream
->lock
);
759 pthread_mutex_unlock(&channel
->lock
);
763 /* Send stream to relayd if the stream has an ID. */
764 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
765 int ret_send_relayd_stream
;
767 ret_send_relayd_stream
= consumer_send_relayd_stream(
768 new_stream
, new_stream
->chan
->pathname
);
769 if (ret_send_relayd_stream
< 0) {
770 pthread_mutex_unlock(&new_stream
->lock
);
771 pthread_mutex_unlock(&channel
->lock
);
772 consumer_stream_free(new_stream
);
773 goto error_add_stream_nosignal
;
777 * If adding an extra stream to an already
778 * existing channel (e.g. cpu hotplug), we need
779 * to send the "streams_sent" command to relayd.
781 if (channel
->streams_sent_to_relayd
) {
782 int ret_send_relayd_streams_sent
;
784 ret_send_relayd_streams_sent
=
785 consumer_send_relayd_streams_sent(
786 new_stream
->net_seq_idx
);
787 if (ret_send_relayd_streams_sent
< 0) {
788 pthread_mutex_unlock(&new_stream
->lock
);
789 pthread_mutex_unlock(&channel
->lock
);
790 goto error_add_stream_nosignal
;
794 pthread_mutex_unlock(&new_stream
->lock
);
795 pthread_mutex_unlock(&channel
->lock
);
797 /* Get the right pipe where the stream will be sent. */
798 if (new_stream
->metadata_flag
) {
799 consumer_add_metadata_stream(new_stream
);
800 stream_pipe
= ctx
->consumer_metadata_pipe
;
802 consumer_add_data_stream(new_stream
);
803 stream_pipe
= ctx
->consumer_data_pipe
;
806 /* Visible to other threads */
807 new_stream
->globally_visible
= 1;
809 health_code_update();
811 ret_pipe_write
= lttng_pipe_write(
812 stream_pipe
, &new_stream
, sizeof(new_stream
));
813 if (ret_pipe_write
< 0) {
814 ERR("Consumer write %s stream to pipe %d",
815 new_stream
->metadata_flag
? "metadata" : "data",
816 lttng_pipe_get_writefd(stream_pipe
));
817 if (new_stream
->metadata_flag
) {
818 consumer_del_stream_for_metadata(new_stream
);
820 consumer_del_stream_for_data(new_stream
);
822 goto error_add_stream_nosignal
;
825 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
826 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
829 error_add_stream_nosignal
:
831 error_add_stream_fatal
:
834 case LTTNG_CONSUMER_STREAMS_SENT
:
836 struct lttng_consumer_channel
*channel
;
840 * Get stream's channel reference. Needed when adding the stream to the
843 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
846 * We could not find the channel. Can happen if cpu hotplug
847 * happens while tearing down.
849 ERR("Unable to find channel key %" PRIu64
,
850 msg
.u
.sent_streams
.channel_key
);
851 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
854 health_code_update();
857 * Send status code to session daemon.
859 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
860 if (ret_send_status
< 0 ||
861 ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
862 /* Somehow, the session daemon is not responding anymore. */
863 goto error_streams_sent_nosignal
;
866 health_code_update();
869 * We should not send this message if we don't monitor the
870 * streams in this channel.
872 if (!channel
->monitor
) {
873 goto end_error_streams_sent
;
876 health_code_update();
877 /* Send stream to relayd if the stream has an ID. */
878 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
879 int ret_send_relay_streams
;
881 ret_send_relay_streams
= consumer_send_relayd_streams_sent(
882 msg
.u
.sent_streams
.net_seq_idx
);
883 if (ret_send_relay_streams
< 0) {
884 goto error_streams_sent_nosignal
;
886 channel
->streams_sent_to_relayd
= true;
888 end_error_streams_sent
:
890 error_streams_sent_nosignal
:
893 case LTTNG_CONSUMER_UPDATE_STREAM
:
898 case LTTNG_CONSUMER_DESTROY_RELAYD
:
900 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
901 struct consumer_relayd_sock_pair
*relayd
;
904 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
906 /* Get relayd reference if exists. */
907 relayd
= consumer_find_relayd(index
);
908 if (relayd
== NULL
) {
909 DBG("Unable to find relayd %" PRIu64
, index
);
910 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
914 * Each relayd socket pair has a refcount of stream attached to it
915 * which tells if the relayd is still active or not depending on the
918 * This will set the destroy flag of the relayd object and destroy it
919 * if the refcount reaches zero when called.
921 * The destroy can happen either here or when a stream fd hangs up.
924 consumer_flag_relayd_for_destroy(relayd
);
927 health_code_update();
929 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
930 if (ret_send_status
< 0) {
931 /* Somehow, the session daemon is not responding anymore. */
937 case LTTNG_CONSUMER_DATA_PENDING
:
939 int32_t ret_data_pending
;
940 uint64_t id
= msg
.u
.data_pending
.session_id
;
943 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
945 ret_data_pending
= consumer_data_pending(id
);
947 health_code_update();
949 /* Send back returned value to session daemon */
950 ret_send
= lttcomm_send_unix_sock(sock
, &ret_data_pending
,
951 sizeof(ret_data_pending
));
953 PERROR("send data pending ret code");
958 * No need to send back a status message since the data pending
959 * returned value is the response.
963 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
965 struct lttng_consumer_channel
*channel
;
966 uint64_t key
= msg
.u
.snapshot_channel
.key
;
969 channel
= consumer_find_channel(key
);
971 ERR("Channel %" PRIu64
" not found", key
);
972 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
974 if (msg
.u
.snapshot_channel
.metadata
== 1) {
977 ret_snapshot
= lttng_kconsumer_snapshot_metadata(
979 msg
.u
.snapshot_channel
.pathname
,
980 msg
.u
.snapshot_channel
.relayd_id
,
982 if (ret_snapshot
< 0) {
983 ERR("Snapshot metadata failed");
984 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
989 ret_snapshot
= lttng_kconsumer_snapshot_channel(
991 msg
.u
.snapshot_channel
.pathname
,
992 msg
.u
.snapshot_channel
.relayd_id
,
993 msg
.u
.snapshot_channel
994 .nb_packets_per_stream
,
996 if (ret_snapshot
< 0) {
997 ERR("Snapshot channel failed");
998 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1002 health_code_update();
1004 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1005 if (ret_send_status
< 0) {
1006 /* Somehow, the session daemon is not responding anymore. */
1011 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1013 uint64_t key
= msg
.u
.destroy_channel
.key
;
1014 struct lttng_consumer_channel
*channel
;
1015 int ret_send_status
;
1017 channel
= consumer_find_channel(key
);
1019 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1020 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1023 health_code_update();
1025 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1026 if (ret_send_status
< 0) {
1027 /* Somehow, the session daemon is not responding anymore. */
1028 goto end_destroy_channel
;
1031 health_code_update();
1033 /* Stop right now if no channel was found. */
1035 goto end_destroy_channel
;
1039 * This command should ONLY be issued for channel with streams set in
1042 assert(!channel
->monitor
);
1045 * The refcount should ALWAYS be 0 in the case of a channel in no
1048 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1050 consumer_del_channel(channel
);
1051 end_destroy_channel
:
1054 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1058 struct lttng_consumer_channel
*channel
;
1059 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1060 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1062 DBG("Kernel consumer discarded events command for session id %"
1063 PRIu64
", channel key %" PRIu64
, id
, key
);
1065 channel
= consumer_find_channel(key
);
1067 ERR("Kernel consumer discarded events channel %"
1068 PRIu64
" not found", key
);
1071 count
= channel
->discarded_events
;
1074 health_code_update();
1076 /* Send back returned value to session daemon */
1077 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1079 PERROR("send discarded events");
1085 case LTTNG_CONSUMER_LOST_PACKETS
:
1089 struct lttng_consumer_channel
*channel
;
1090 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1091 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1093 DBG("Kernel consumer lost packets command for session id %"
1094 PRIu64
", channel key %" PRIu64
, id
, key
);
1096 channel
= consumer_find_channel(key
);
1098 ERR("Kernel consumer lost packets channel %"
1099 PRIu64
" not found", key
);
1102 count
= channel
->lost_packets
;
1105 health_code_update();
1107 /* Send back returned value to session daemon */
1108 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1110 PERROR("send lost packets");
1116 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1118 int channel_monitor_pipe
;
1119 int ret_send_status
, ret_set_channel_monitor_pipe
;
1122 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1123 /* Successfully received the command's type. */
1124 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1125 if (ret_send_status
< 0) {
1129 ret_recv
= lttcomm_recv_fds_unix_sock(
1130 sock
, &channel_monitor_pipe
, 1);
1131 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1132 ERR("Failed to receive channel monitor pipe");
1136 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1137 ret_set_channel_monitor_pipe
=
1138 consumer_timer_thread_set_channel_monitor_pipe(
1139 channel_monitor_pipe
);
1140 if (!ret_set_channel_monitor_pipe
) {
1144 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1145 /* Set the pipe as non-blocking. */
1146 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1147 if (ret_fcntl
== -1) {
1148 PERROR("fcntl get flags of the channel monitoring pipe");
1153 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
1154 flags
| O_NONBLOCK
);
1155 if (ret_fcntl
== -1) {
1156 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1159 DBG("Channel monitor pipe set as non-blocking");
1161 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1163 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1164 if (ret_send_status
< 0) {
1169 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1171 struct lttng_consumer_channel
*channel
;
1172 uint64_t key
= msg
.u
.rotate_channel
.key
;
1173 int ret_send_status
;
1175 DBG("Consumer rotate channel %" PRIu64
, key
);
1177 channel
= consumer_find_channel(key
);
1179 ERR("Channel %" PRIu64
" not found", key
);
1180 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1183 * Sample the rotate position of all the streams in this channel.
1185 int ret_rotate_channel
;
1187 ret_rotate_channel
= lttng_consumer_rotate_channel(
1189 msg
.u
.rotate_channel
.relayd_id
,
1190 msg
.u
.rotate_channel
.metadata
, ctx
);
1191 if (ret_rotate_channel
< 0) {
1192 ERR("Rotate channel failed");
1193 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1196 health_code_update();
1199 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1200 if (ret_send_status
< 0) {
1201 /* Somehow, the session daemon is not responding anymore. */
1202 goto error_rotate_channel
;
1205 /* Rotate the streams that are ready right now. */
1208 ret_rotate
= lttng_consumer_rotate_ready_streams(
1210 if (ret_rotate
< 0) {
1211 ERR("Rotate ready streams failed");
1215 error_rotate_channel
:
1218 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1220 struct lttng_consumer_channel
*channel
;
1221 uint64_t key
= msg
.u
.clear_channel
.key
;
1222 int ret_send_status
;
1224 channel
= consumer_find_channel(key
);
1226 DBG("Channel %" PRIu64
" not found", key
);
1227 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1229 int ret_clear_channel
;
1232 lttng_consumer_clear_channel(channel
);
1233 if (ret_clear_channel
) {
1234 ERR("Clear channel failed");
1235 ret_code
= ret_clear_channel
;
1238 health_code_update();
1241 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1242 if (ret_send_status
< 0) {
1243 /* Somehow, the session daemon is not responding anymore. */
1249 case LTTNG_CONSUMER_INIT
:
1251 int ret_send_status
;
1253 ret_code
= lttng_consumer_init_command(ctx
,
1254 msg
.u
.init
.sessiond_uuid
);
1255 health_code_update();
1256 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1257 if (ret_send_status
< 0) {
1258 /* Somehow, the session daemon is not responding anymore. */
1263 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1265 const struct lttng_credentials credentials
= {
1266 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
1267 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
1269 const bool is_local_trace
=
1270 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1271 const uint64_t relayd_id
=
1272 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1273 const char *chunk_override_name
=
1274 *msg
.u
.create_trace_chunk
.override_name
?
1275 msg
.u
.create_trace_chunk
.override_name
:
1277 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1280 * The session daemon will only provide a chunk directory file
1281 * descriptor for local traces.
1283 if (is_local_trace
) {
1285 int ret_send_status
;
1288 /* Acnowledge the reception of the command. */
1289 ret_send_status
= consumer_send_status_msg(
1290 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1291 if (ret_send_status
< 0) {
1292 /* Somehow, the session daemon is not responding anymore. */
1296 ret_recv
= lttcomm_recv_fds_unix_sock(
1297 sock
, &chunk_dirfd
, 1);
1298 if (ret_recv
!= sizeof(chunk_dirfd
)) {
1299 ERR("Failed to receive trace chunk directory file descriptor");
1303 DBG("Received trace chunk directory fd (%d)",
1305 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1307 if (!chunk_directory_handle
) {
1308 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1309 if (close(chunk_dirfd
)) {
1310 PERROR("Failed to close chunk directory file descriptor");
1316 ret_code
= lttng_consumer_create_trace_chunk(
1317 !is_local_trace
? &relayd_id
: NULL
,
1318 msg
.u
.create_trace_chunk
.session_id
,
1319 msg
.u
.create_trace_chunk
.chunk_id
,
1320 (time_t) msg
.u
.create_trace_chunk
1321 .creation_timestamp
,
1322 chunk_override_name
,
1323 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1326 chunk_directory_handle
);
1327 lttng_directory_handle_put(chunk_directory_handle
);
1328 goto end_msg_sessiond
;
1330 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1332 enum lttng_trace_chunk_command_type close_command
=
1333 msg
.u
.close_trace_chunk
.close_command
.value
;
1334 const uint64_t relayd_id
=
1335 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1336 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1337 char path
[LTTNG_PATH_MAX
];
1340 ret_code
= lttng_consumer_close_trace_chunk(
1341 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1344 msg
.u
.close_trace_chunk
.session_id
,
1345 msg
.u
.close_trace_chunk
.chunk_id
,
1346 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1347 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1350 reply
.ret_code
= ret_code
;
1351 reply
.path_length
= strlen(path
) + 1;
1352 ret_send
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1353 if (ret_send
!= sizeof(reply
)) {
1356 ret_send
= lttcomm_send_unix_sock(
1357 sock
, path
, reply
.path_length
);
1358 if (ret_send
!= reply
.path_length
) {
1363 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1365 const uint64_t relayd_id
=
1366 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1368 ret_code
= lttng_consumer_trace_chunk_exists(
1369 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1371 msg
.u
.trace_chunk_exists
.session_id
,
1372 msg
.u
.trace_chunk_exists
.chunk_id
);
1373 goto end_msg_sessiond
;
1375 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
1377 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
1378 struct lttng_consumer_channel
*channel
=
1379 consumer_find_channel(key
);
1382 pthread_mutex_lock(&channel
->lock
);
1383 ret_code
= lttng_consumer_open_channel_packets(channel
);
1384 pthread_mutex_unlock(&channel
->lock
);
1386 WARN("Channel %" PRIu64
" not found", key
);
1387 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1390 health_code_update();
1391 goto end_msg_sessiond
;
1399 * Return 1 to indicate success since the 0 value can be a socket
1400 * shutdown during the recv() or send() call.
1405 /* This will issue a consumer stop. */
1410 * The returned value here is not useful since either way we'll return 1 to
1411 * the caller because the session daemon socket management is done
1412 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1415 int ret_send_status
;
1417 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1418 if (ret_send_status
< 0) {
1426 health_code_update();
1432 * Sync metadata meaning request them to the session daemon and snapshot to the
1433 * metadata thread can consumer them.
1435 * Metadata stream lock MUST be acquired.
1437 enum sync_metadata_status
lttng_kconsumer_sync_metadata(
1438 struct lttng_consumer_stream
*metadata
)
1441 enum sync_metadata_status status
;
1445 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1447 ERR("Failed to flush kernel stream");
1448 status
= SYNC_METADATA_STATUS_ERROR
;
1452 ret
= kernctl_snapshot(metadata
->wait_fd
);
1454 if (errno
== EAGAIN
) {
1455 /* No new metadata, exit. */
1456 DBG("Sync metadata, no new kernel metadata");
1457 status
= SYNC_METADATA_STATUS_NO_DATA
;
1459 ERR("Sync metadata, taking kernel snapshot failed.");
1460 status
= SYNC_METADATA_STATUS_ERROR
;
1463 status
= SYNC_METADATA_STATUS_NEW_DATA
;
1471 int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
1472 struct stream_subbuffer
*subbuf
)
1476 ret
= kernctl_get_subbuf_size(
1477 stream
->wait_fd
, &subbuf
->info
.data
.subbuf_size
);
1482 ret
= kernctl_get_padded_subbuf_size(
1483 stream
->wait_fd
, &subbuf
->info
.data
.padded_subbuf_size
);
1493 int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
1494 struct stream_subbuffer
*subbuf
)
1498 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1503 ret
= kernctl_get_metadata_version(
1504 stream
->wait_fd
, &subbuf
->info
.metadata
.version
);
1514 int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
1515 struct stream_subbuffer
*subbuf
)
1519 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1524 ret
= kernctl_get_packet_size(
1525 stream
->wait_fd
, &subbuf
->info
.data
.packet_size
);
1527 PERROR("Failed to get sub-buffer packet size");
1531 ret
= kernctl_get_content_size(
1532 stream
->wait_fd
, &subbuf
->info
.data
.content_size
);
1534 PERROR("Failed to get sub-buffer content size");
1538 ret
= kernctl_get_timestamp_begin(
1539 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_begin
);
1541 PERROR("Failed to get sub-buffer begin timestamp");
1545 ret
= kernctl_get_timestamp_end(
1546 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_end
);
1548 PERROR("Failed to get sub-buffer end timestamp");
1552 ret
= kernctl_get_events_discarded(
1553 stream
->wait_fd
, &subbuf
->info
.data
.events_discarded
);
1555 PERROR("Failed to get sub-buffer events discarded count");
1559 ret
= kernctl_get_sequence_number(stream
->wait_fd
,
1560 &subbuf
->info
.data
.sequence_number
.value
);
1562 /* May not be supported by older LTTng-modules. */
1563 if (ret
!= -ENOTTY
) {
1564 PERROR("Failed to get sub-buffer sequence number");
1568 subbuf
->info
.data
.sequence_number
.is_set
= true;
1571 ret
= kernctl_get_stream_id(
1572 stream
->wait_fd
, &subbuf
->info
.data
.stream_id
);
1574 PERROR("Failed to get stream id");
1578 ret
= kernctl_get_instance_id(stream
->wait_fd
,
1579 &subbuf
->info
.data
.stream_instance_id
.value
);
1581 /* May not be supported by older LTTng-modules. */
1582 if (ret
!= -ENOTTY
) {
1583 PERROR("Failed to get stream instance id");
1587 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
1594 enum get_next_subbuffer_status
get_subbuffer_common(
1595 struct lttng_consumer_stream
*stream
,
1596 struct stream_subbuffer
*subbuffer
)
1599 enum get_next_subbuffer_status status
;
1601 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1604 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1609 * The caller only expects -ENODATA when there is no data to
1610 * read, but the kernel tracer returns -EAGAIN when there is
1611 * currently no data for a non-finalized stream, and -ENODATA
1612 * when there is no data for a finalized stream. Those can be
1613 * combined into a -ENODATA return value.
1615 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1618 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1622 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1625 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1632 enum get_next_subbuffer_status
get_next_subbuffer_splice(
1633 struct lttng_consumer_stream
*stream
,
1634 struct stream_subbuffer
*subbuffer
)
1636 const enum get_next_subbuffer_status status
=
1637 get_subbuffer_common(stream
, subbuffer
);
1639 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1643 subbuffer
->buffer
.fd
= stream
->wait_fd
;
1649 enum get_next_subbuffer_status
get_next_subbuffer_mmap(
1650 struct lttng_consumer_stream
*stream
,
1651 struct stream_subbuffer
*subbuffer
)
1654 enum get_next_subbuffer_status status
;
1657 status
= get_subbuffer_common(stream
, subbuffer
);
1658 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1662 ret
= get_current_subbuf_addr(stream
, &addr
);
1664 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1668 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1669 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1675 enum get_next_subbuffer_status
get_next_subbuffer_metadata_check(struct lttng_consumer_stream
*stream
,
1676 struct stream_subbuffer
*subbuffer
)
1681 enum get_next_subbuffer_status status
;
1683 ret
= kernctl_get_next_subbuf_metadata_check(stream
->wait_fd
,
1689 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1695 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
1697 ret
= get_current_subbuf_addr(stream
, &addr
);
1702 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1703 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1704 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1705 subbuffer
->info
.metadata
.padded_subbuf_size
,
1706 coherent
? "true" : "false");
1709 * The caller only expects -ENODATA when there is no data to read, but
1710 * the kernel tracer returns -EAGAIN when there is currently no data
1711 * for a non-finalized stream, and -ENODATA when there is no data for a
1712 * finalized stream. Those can be combined into a -ENODATA return value.
1716 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1721 * The caller only expects -ENODATA when there is no data to
1722 * read, but the kernel tracer returns -EAGAIN when there is
1723 * currently no data for a non-finalized stream, and -ENODATA
1724 * when there is no data for a finalized stream. Those can be
1725 * combined into a -ENODATA return value.
1727 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1730 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1738 int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
1739 struct stream_subbuffer
*subbuffer
)
1741 const int ret
= kernctl_put_next_subbuf(stream
->wait_fd
);
1744 if (ret
== -EFAULT
) {
1745 PERROR("Error in unreserving sub buffer");
1746 } else if (ret
== -EIO
) {
1747 /* Should never happen with newer LTTng versions */
1748 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1756 bool is_get_next_check_metadata_available(int tracer_fd
)
1758 const int ret
= kernctl_get_next_subbuf_metadata_check(tracer_fd
, NULL
);
1759 const bool available
= ret
!= -ENOTTY
;
1762 /* get succeeded, make sure to put the subbuffer. */
1763 kernctl_put_subbuf(tracer_fd
);
1770 int signal_metadata(struct lttng_consumer_stream
*stream
,
1771 struct lttng_consumer_local_data
*ctx
)
1773 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
1774 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
1778 int lttng_kconsumer_set_stream_ops(
1779 struct lttng_consumer_stream
*stream
)
1783 if (stream
->metadata_flag
&& stream
->chan
->is_live
) {
1784 DBG("Attempting to enable metadata bucketization for live consumers");
1785 if (is_get_next_check_metadata_available(stream
->wait_fd
)) {
1786 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1787 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1788 get_next_subbuffer_metadata_check
;
1789 ret
= consumer_stream_enable_metadata_bucketization(
1796 * The kernel tracer version is too old to indicate
1797 * when the metadata stream has reached a "coherent"
1798 * (parseable) point.
1800 * This means that a live viewer may see an incoherent
1801 * sequence of metadata and fail to parse it.
1803 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1804 metadata_bucket_destroy(stream
->metadata_bucket
);
1805 stream
->metadata_bucket
= NULL
;
1808 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
1811 if (!stream
->read_subbuffer_ops
.get_next_subbuffer
) {
1812 if (stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
) {
1813 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1814 get_next_subbuffer_mmap
;
1816 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1817 get_next_subbuffer_splice
;
1821 if (stream
->metadata_flag
) {
1822 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1823 extract_metadata_subbuffer_info
;
1825 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1826 extract_data_subbuffer_info
;
1827 if (stream
->chan
->is_live
) {
1828 stream
->read_subbuffer_ops
.send_live_beacon
=
1829 consumer_flush_kernel_index
;
1833 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
1838 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1845 * Don't create anything if this is set for streaming or if there is
1846 * no current trace chunk on the parent channel.
1848 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1849 stream
->chan
->trace_chunk
) {
1850 ret
= consumer_stream_create_output_files(stream
, true);
1856 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1857 /* get the len of the mmap region */
1858 unsigned long mmap_len
;
1860 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1862 PERROR("kernctl_get_mmap_len");
1863 goto error_close_fd
;
1865 stream
->mmap_len
= (size_t) mmap_len
;
1867 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1868 MAP_PRIVATE
, stream
->wait_fd
, 0);
1869 if (stream
->mmap_base
== MAP_FAILED
) {
1870 PERROR("Error mmaping");
1872 goto error_close_fd
;
1876 ret
= lttng_kconsumer_set_stream_ops(stream
);
1878 goto error_close_fd
;
1881 /* we return 0 to let the library handle the FD internally */
1885 if (stream
->out_fd
>= 0) {
1888 err
= close(stream
->out_fd
);
1890 stream
->out_fd
= -1;
1897 * Check if data is still being extracted from the buffers for a specific
1898 * stream. Consumer data lock MUST be acquired before calling this function
1899 * and the stream lock.
1901 * Return 1 if the traced data are still getting read else 0 meaning that the
1902 * data is available for trace viewer reading.
1904 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1910 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1915 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1917 /* There is still data so let's put back this subbuffer. */
1918 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1920 ret
= 1; /* Data is pending */
1924 /* Data is NOT pending and ready to be read. */