2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
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
16 #include <sys/socket.h>
17 #include <sys/types.h>
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <common/common.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/sessiond-comm/relayd.h>
28 #include <common/compat/fcntl.h>
29 #include <common/compat/endian.h>
30 #include <common/pipe.h>
31 #include <common/relayd/relayd.h>
32 #include <common/utils.h>
33 #include <common/consumer/consumer-stream.h>
34 #include <common/index/index.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/optional.h>
37 #include <common/buffer-view.h>
38 #include <common/consumer/consumer.h>
39 #include <common/consumer/metadata-bucket.h>
41 #include "kernel-consumer.h"
43 extern struct lttng_consumer_global_data the_consumer_data
;
44 extern int consumer_poll_timeout
;
47 * Take a snapshot for a specific fd
49 * Returns 0 on success, < 0 on error
51 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
54 int infd
= stream
->wait_fd
;
56 ret
= kernctl_snapshot(infd
);
58 * -EAGAIN is not an error, it just means that there is no data to
61 if (ret
!= 0 && ret
!= -EAGAIN
) {
62 PERROR("Getting sub-buffer snapshot.");
69 * Sample consumed and produced positions for a specific fd.
71 * Returns 0 on success, < 0 on error.
73 int lttng_kconsumer_sample_snapshot_positions(
74 struct lttng_consumer_stream
*stream
)
78 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
82 * Get the produced position
84 * Returns 0 on success, < 0 on error
86 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
90 int infd
= stream
->wait_fd
;
92 ret
= kernctl_snapshot_get_produced(infd
, pos
);
94 PERROR("kernctl_snapshot_get_produced");
101 * Get the consumerd position
103 * Returns 0 on success, < 0 on error
105 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
109 int infd
= stream
->wait_fd
;
111 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
113 PERROR("kernctl_snapshot_get_consumed");
120 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
124 unsigned long mmap_offset
;
125 const char *mmap_base
= (const char *) stream
->mmap_base
;
127 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
129 PERROR("Failed to get mmap read offset");
133 *addr
= mmap_base
+ mmap_offset
;
139 * Take a snapshot of all the stream of a channel
140 * RCU read-side lock must be held across this function to ensure existence of
143 * Returns 0 on success, < 0 on error
145 static int lttng_kconsumer_snapshot_channel(
146 struct lttng_consumer_channel
*channel
,
147 uint64_t key
, char *path
, uint64_t relayd_id
,
148 uint64_t nb_packets_per_stream
,
149 struct lttng_consumer_local_data
*ctx
)
152 struct lttng_consumer_stream
*stream
;
154 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
156 /* Prevent channel modifications while we perform the snapshot.*/
157 pthread_mutex_lock(&channel
->lock
);
161 /* Splice is not supported yet for channel snapshot. */
162 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
163 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
169 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
170 unsigned long consumed_pos
, produced_pos
;
172 health_code_update();
175 * Lock stream because we are about to change its state.
177 pthread_mutex_lock(&stream
->lock
);
179 LTTNG_ASSERT(channel
->trace_chunk
);
180 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
182 * Can't happen barring an internal error as the channel
183 * holds a reference to the trace chunk.
185 ERR("Failed to acquire reference to channel's trace chunk");
189 LTTNG_ASSERT(!stream
->trace_chunk
);
190 stream
->trace_chunk
= channel
->trace_chunk
;
193 * Assign the received relayd ID so we can use it for streaming. The streams
194 * are not visible to anyone so this is OK to change it.
196 stream
->net_seq_idx
= relayd_id
;
197 channel
->relayd_id
= relayd_id
;
198 if (relayd_id
!= (uint64_t) -1ULL) {
199 ret
= consumer_send_relayd_stream(stream
, path
);
201 ERR("sending stream to relayd");
205 ret
= consumer_stream_create_output_files(stream
,
210 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
214 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
217 * Doing a buffer flush which does not take into
218 * account empty packets. This is not perfect
219 * for stream intersection, but required as a
220 * fall-back when "flush_empty" is not
221 * implemented by lttng-modules.
223 ret
= kernctl_buffer_flush(stream
->wait_fd
);
225 ERR("Failed to flush kernel stream");
231 ret
= lttng_kconsumer_take_snapshot(stream
);
233 ERR("Taking kernel snapshot");
237 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
239 ERR("Produced kernel snapshot position");
243 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
245 ERR("Consumerd kernel snapshot position");
249 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
250 produced_pos
, nb_packets_per_stream
,
251 stream
->max_sb_size
);
253 while ((long) (consumed_pos
- produced_pos
) < 0) {
255 unsigned long len
, padded_len
;
256 const char *subbuf_addr
;
257 struct lttng_buffer_view subbuf_view
;
259 health_code_update();
260 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
262 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
264 if (ret
!= -EAGAIN
) {
265 PERROR("kernctl_get_subbuf snapshot");
268 DBG("Kernel consumer get subbuf failed. Skipping it.");
269 consumed_pos
+= stream
->max_sb_size
;
270 stream
->chan
->lost_packets
++;
274 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
276 ERR("Snapshot kernctl_get_subbuf_size");
277 goto error_put_subbuf
;
280 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
282 ERR("Snapshot kernctl_get_padded_subbuf_size");
283 goto error_put_subbuf
;
286 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
288 goto error_put_subbuf
;
291 subbuf_view
= lttng_buffer_view_init(
292 subbuf_addr
, 0, padded_len
);
293 read_len
= lttng_consumer_on_read_subbuffer_mmap(
294 stream
, &subbuf_view
,
297 * We write the padded len in local tracefiles but the data len
298 * when using a relay. Display the error but continue processing
299 * to try to release the subbuffer.
301 if (relayd_id
!= (uint64_t) -1ULL) {
302 if (read_len
!= len
) {
303 ERR("Error sending to the relay (ret: %zd != len: %lu)",
307 if (read_len
!= padded_len
) {
308 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
309 read_len
, padded_len
);
313 ret
= kernctl_put_subbuf(stream
->wait_fd
);
315 ERR("Snapshot kernctl_put_subbuf");
318 consumed_pos
+= stream
->max_sb_size
;
321 if (relayd_id
== (uint64_t) -1ULL) {
322 if (stream
->out_fd
>= 0) {
323 ret
= close(stream
->out_fd
);
325 PERROR("Kernel consumer snapshot close out_fd");
331 close_relayd_stream(stream
);
332 stream
->net_seq_idx
= (uint64_t) -1ULL;
334 lttng_trace_chunk_put(stream
->trace_chunk
);
335 stream
->trace_chunk
= NULL
;
336 pthread_mutex_unlock(&stream
->lock
);
344 ret
= kernctl_put_subbuf(stream
->wait_fd
);
346 ERR("Snapshot kernctl_put_subbuf error path");
349 pthread_mutex_unlock(&stream
->lock
);
352 pthread_mutex_unlock(&channel
->lock
);
357 * Read the whole metadata available for a snapshot.
358 * RCU read-side lock must be held across this function to ensure existence of
361 * Returns 0 on success, < 0 on error
363 static int lttng_kconsumer_snapshot_metadata(
364 struct lttng_consumer_channel
*metadata_channel
,
365 uint64_t key
, char *path
, uint64_t relayd_id
,
366 struct lttng_consumer_local_data
*ctx
)
368 int ret
, use_relayd
= 0;
370 struct lttng_consumer_stream
*metadata_stream
;
374 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
379 metadata_stream
= metadata_channel
->metadata_stream
;
380 LTTNG_ASSERT(metadata_stream
);
382 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
383 LTTNG_ASSERT(metadata_channel
->trace_chunk
);
384 LTTNG_ASSERT(metadata_stream
->trace_chunk
);
386 /* Flag once that we have a valid relayd for the stream. */
387 if (relayd_id
!= (uint64_t) -1ULL) {
392 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
397 ret
= consumer_stream_create_output_files(metadata_stream
,
405 health_code_update();
407 ret_read
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
409 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
414 } while (ret_read
> 0);
417 close_relayd_stream(metadata_stream
);
418 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
420 if (metadata_stream
->out_fd
>= 0) {
421 ret
= close(metadata_stream
->out_fd
);
423 PERROR("Kernel consumer snapshot metadata close out_fd");
425 * Don't go on error here since the snapshot was successful at this
426 * point but somehow the close failed.
429 metadata_stream
->out_fd
= -1;
430 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
431 metadata_stream
->trace_chunk
= NULL
;
437 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
438 cds_list_del(&metadata_stream
->send_node
);
439 consumer_stream_destroy(metadata_stream
, NULL
);
440 metadata_channel
->metadata_stream
= NULL
;
446 * Receive command from session daemon and process it.
448 * Return 1 on success else a negative value or 0.
450 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
451 int sock
, struct pollfd
*consumer_sockpoll
)
454 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
455 struct lttcomm_consumer_msg msg
;
457 health_code_update();
462 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
463 if (ret_recv
!= sizeof(msg
)) {
465 lttng_consumer_send_error(ctx
,
466 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
473 health_code_update();
475 /* Deprecated command */
476 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
478 health_code_update();
480 /* relayd needs RCU read-side protection */
483 switch (msg
.cmd_type
) {
484 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
486 /* Session daemon status message are handled in the following call. */
487 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
488 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
489 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
490 msg
.u
.relayd_sock
.relayd_session_id
);
493 case LTTNG_CONSUMER_ADD_CHANNEL
:
495 struct lttng_consumer_channel
*new_channel
;
496 int ret_send_status
, ret_add_channel
= 0;
497 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
499 health_code_update();
501 /* First send a status message before receiving the fds. */
502 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
503 if (ret_send_status
< 0) {
504 /* Somehow, the session daemon is not responding anymore. */
508 health_code_update();
510 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
511 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
512 msg
.u
.channel
.session_id
,
513 msg
.u
.channel
.chunk_id
.is_set
?
515 msg
.u
.channel
.pathname
,
517 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
518 msg
.u
.channel
.tracefile_size
,
519 msg
.u
.channel
.tracefile_count
, 0,
520 msg
.u
.channel
.monitor
,
521 msg
.u
.channel
.live_timer_interval
,
522 msg
.u
.channel
.is_live
,
524 if (new_channel
== NULL
) {
525 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
528 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
529 switch (msg
.u
.channel
.output
) {
530 case LTTNG_EVENT_SPLICE
:
531 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
533 case LTTNG_EVENT_MMAP
:
534 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
537 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
541 /* Translate and save channel type. */
542 switch (msg
.u
.channel
.type
) {
543 case CONSUMER_CHANNEL_TYPE_DATA
:
544 case CONSUMER_CHANNEL_TYPE_METADATA
:
545 new_channel
->type
= (consumer_channel_type
) msg
.u
.channel
.type
;
552 health_code_update();
554 if (ctx
->on_recv_channel
!= NULL
) {
555 int ret_recv_channel
=
556 ctx
->on_recv_channel(new_channel
);
557 if (ret_recv_channel
== 0) {
558 ret_add_channel
= consumer_add_channel(
560 } else if (ret_recv_channel
< 0) {
565 consumer_add_channel(new_channel
, ctx
);
567 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&&
569 int monitor_start_ret
;
571 DBG("Consumer starting monitor timer");
572 consumer_timer_live_start(new_channel
,
573 msg
.u
.channel
.live_timer_interval
);
574 monitor_start_ret
= consumer_timer_monitor_start(
576 msg
.u
.channel
.monitor_timer_interval
);
577 if (monitor_start_ret
< 0) {
578 ERR("Starting channel monitoring timer failed");
583 health_code_update();
585 /* If we received an error in add_channel, we need to report it. */
586 if (ret_add_channel
< 0) {
587 ret_send_status
= consumer_send_status_msg(
588 sock
, ret_add_channel
);
589 if (ret_send_status
< 0) {
597 case LTTNG_CONSUMER_ADD_STREAM
:
600 struct lttng_pipe
*stream_pipe
;
601 struct lttng_consumer_stream
*new_stream
;
602 struct lttng_consumer_channel
*channel
;
604 int ret_send_status
, ret_poll
, ret_get_max_subbuf_size
;
605 ssize_t ret_pipe_write
, ret_recv
;
608 * Get stream's channel reference. Needed when adding the stream to the
611 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
614 * We could not find the channel. Can happen if cpu hotplug
615 * happens while tearing down.
617 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
618 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
621 health_code_update();
623 /* First send a status message before receiving the fds. */
624 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
625 if (ret_send_status
< 0) {
626 /* Somehow, the session daemon is not responding anymore. */
627 goto error_add_stream_fatal
;
630 health_code_update();
632 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
633 /* Channel was not found. */
634 goto error_add_stream_nosignal
;
639 ret_poll
= lttng_consumer_poll_socket(consumer_sockpoll
);
642 goto error_add_stream_fatal
;
645 health_code_update();
647 /* Get stream file descriptor from socket */
648 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
649 if (ret_recv
!= sizeof(fd
)) {
650 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
655 health_code_update();
658 * Send status code to session daemon only if the recv works. If the
659 * above recv() failed, the session daemon is notified through the
660 * error socket and the teardown is eventually done.
662 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
663 if (ret_send_status
< 0) {
664 /* Somehow, the session daemon is not responding anymore. */
665 goto error_add_stream_nosignal
;
668 health_code_update();
670 pthread_mutex_lock(&channel
->lock
);
671 new_stream
= consumer_stream_create(
678 channel
->trace_chunk
,
683 if (new_stream
== NULL
) {
688 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
691 pthread_mutex_unlock(&channel
->lock
);
692 goto error_add_stream_nosignal
;
695 new_stream
->wait_fd
= fd
;
696 ret_get_max_subbuf_size
= kernctl_get_max_subbuf_size(
697 new_stream
->wait_fd
, &new_stream
->max_sb_size
);
698 if (ret_get_max_subbuf_size
< 0) {
699 pthread_mutex_unlock(&channel
->lock
);
700 ERR("Failed to get kernel maximal subbuffer size");
701 goto error_add_stream_nosignal
;
704 consumer_stream_update_channel_attributes(new_stream
,
708 * We've just assigned the channel to the stream so increment the
709 * refcount right now. We don't need to increment the refcount for
710 * streams in no monitor because we handle manually the cleanup of
711 * those. It is very important to make sure there is NO prior
712 * consumer_del_stream() calls or else the refcount will be unbalanced.
714 if (channel
->monitor
) {
715 uatomic_inc(&new_stream
->chan
->refcount
);
719 * The buffer flush is done on the session daemon side for the kernel
720 * so no need for the stream "hangup_flush_done" variable to be
721 * tracked. This is important for a kernel stream since we don't rely
722 * on the flush state of the stream to read data. It's not the case for
723 * user space tracing.
725 new_stream
->hangup_flush_done
= 0;
727 health_code_update();
729 pthread_mutex_lock(&new_stream
->lock
);
730 if (ctx
->on_recv_stream
) {
731 int ret_recv_stream
= ctx
->on_recv_stream(new_stream
);
732 if (ret_recv_stream
< 0) {
733 pthread_mutex_unlock(&new_stream
->lock
);
734 pthread_mutex_unlock(&channel
->lock
);
735 consumer_stream_free(new_stream
);
736 goto error_add_stream_nosignal
;
739 health_code_update();
741 if (new_stream
->metadata_flag
) {
742 channel
->metadata_stream
= new_stream
;
745 /* Do not monitor this stream. */
746 if (!channel
->monitor
) {
747 DBG("Kernel consumer add stream %s in no monitor mode with "
748 "relayd id %" PRIu64
, new_stream
->name
,
749 new_stream
->net_seq_idx
);
750 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
751 pthread_mutex_unlock(&new_stream
->lock
);
752 pthread_mutex_unlock(&channel
->lock
);
756 /* Send stream to relayd if the stream has an ID. */
757 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
758 int ret_send_relayd_stream
;
760 ret_send_relayd_stream
= consumer_send_relayd_stream(
761 new_stream
, new_stream
->chan
->pathname
);
762 if (ret_send_relayd_stream
< 0) {
763 pthread_mutex_unlock(&new_stream
->lock
);
764 pthread_mutex_unlock(&channel
->lock
);
765 consumer_stream_free(new_stream
);
766 goto error_add_stream_nosignal
;
770 * If adding an extra stream to an already
771 * existing channel (e.g. cpu hotplug), we need
772 * to send the "streams_sent" command to relayd.
774 if (channel
->streams_sent_to_relayd
) {
775 int ret_send_relayd_streams_sent
;
777 ret_send_relayd_streams_sent
=
778 consumer_send_relayd_streams_sent(
779 new_stream
->net_seq_idx
);
780 if (ret_send_relayd_streams_sent
< 0) {
781 pthread_mutex_unlock(&new_stream
->lock
);
782 pthread_mutex_unlock(&channel
->lock
);
783 goto error_add_stream_nosignal
;
787 pthread_mutex_unlock(&new_stream
->lock
);
788 pthread_mutex_unlock(&channel
->lock
);
790 /* Get the right pipe where the stream will be sent. */
791 if (new_stream
->metadata_flag
) {
792 consumer_add_metadata_stream(new_stream
);
793 stream_pipe
= ctx
->consumer_metadata_pipe
;
795 consumer_add_data_stream(new_stream
);
796 stream_pipe
= ctx
->consumer_data_pipe
;
799 /* Visible to other threads */
800 new_stream
->globally_visible
= 1;
802 health_code_update();
804 ret_pipe_write
= lttng_pipe_write(
805 stream_pipe
, &new_stream
, sizeof(new_stream
));
806 if (ret_pipe_write
< 0) {
807 ERR("Consumer write %s stream to pipe %d",
808 new_stream
->metadata_flag
? "metadata" : "data",
809 lttng_pipe_get_writefd(stream_pipe
));
810 if (new_stream
->metadata_flag
) {
811 consumer_del_stream_for_metadata(new_stream
);
813 consumer_del_stream_for_data(new_stream
);
815 goto error_add_stream_nosignal
;
818 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
819 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
822 error_add_stream_nosignal
:
824 error_add_stream_fatal
:
827 case LTTNG_CONSUMER_STREAMS_SENT
:
829 struct lttng_consumer_channel
*channel
;
833 * Get stream's channel reference. Needed when adding the stream to the
836 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
839 * We could not find the channel. Can happen if cpu hotplug
840 * happens while tearing down.
842 ERR("Unable to find channel key %" PRIu64
,
843 msg
.u
.sent_streams
.channel_key
);
844 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
847 health_code_update();
850 * Send status code to session daemon.
852 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
853 if (ret_send_status
< 0 ||
854 ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
855 /* Somehow, the session daemon is not responding anymore. */
856 goto error_streams_sent_nosignal
;
859 health_code_update();
862 * We should not send this message if we don't monitor the
863 * streams in this channel.
865 if (!channel
->monitor
) {
866 goto end_error_streams_sent
;
869 health_code_update();
870 /* Send stream to relayd if the stream has an ID. */
871 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
872 int ret_send_relay_streams
;
874 ret_send_relay_streams
= consumer_send_relayd_streams_sent(
875 msg
.u
.sent_streams
.net_seq_idx
);
876 if (ret_send_relay_streams
< 0) {
877 goto error_streams_sent_nosignal
;
879 channel
->streams_sent_to_relayd
= true;
881 end_error_streams_sent
:
883 error_streams_sent_nosignal
:
886 case LTTNG_CONSUMER_UPDATE_STREAM
:
891 case LTTNG_CONSUMER_DESTROY_RELAYD
:
893 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
894 struct consumer_relayd_sock_pair
*relayd
;
897 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
899 /* Get relayd reference if exists. */
900 relayd
= consumer_find_relayd(index
);
901 if (relayd
== NULL
) {
902 DBG("Unable to find relayd %" PRIu64
, index
);
903 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
907 * Each relayd socket pair has a refcount of stream attached to it
908 * which tells if the relayd is still active or not depending on the
911 * This will set the destroy flag of the relayd object and destroy it
912 * if the refcount reaches zero when called.
914 * The destroy can happen either here or when a stream fd hangs up.
917 consumer_flag_relayd_for_destroy(relayd
);
920 health_code_update();
922 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
923 if (ret_send_status
< 0) {
924 /* Somehow, the session daemon is not responding anymore. */
930 case LTTNG_CONSUMER_DATA_PENDING
:
932 int32_t ret_data_pending
;
933 uint64_t id
= msg
.u
.data_pending
.session_id
;
936 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
938 ret_data_pending
= consumer_data_pending(id
);
940 health_code_update();
942 /* Send back returned value to session daemon */
943 ret_send
= lttcomm_send_unix_sock(sock
, &ret_data_pending
,
944 sizeof(ret_data_pending
));
946 PERROR("send data pending ret code");
951 * No need to send back a status message since the data pending
952 * returned value is the response.
956 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
958 struct lttng_consumer_channel
*channel
;
959 uint64_t key
= msg
.u
.snapshot_channel
.key
;
962 channel
= consumer_find_channel(key
);
964 ERR("Channel %" PRIu64
" not found", key
);
965 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
967 if (msg
.u
.snapshot_channel
.metadata
== 1) {
970 ret_snapshot
= lttng_kconsumer_snapshot_metadata(
972 msg
.u
.snapshot_channel
.pathname
,
973 msg
.u
.snapshot_channel
.relayd_id
,
975 if (ret_snapshot
< 0) {
976 ERR("Snapshot metadata failed");
977 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
982 ret_snapshot
= lttng_kconsumer_snapshot_channel(
984 msg
.u
.snapshot_channel
.pathname
,
985 msg
.u
.snapshot_channel
.relayd_id
,
986 msg
.u
.snapshot_channel
987 .nb_packets_per_stream
,
989 if (ret_snapshot
< 0) {
990 ERR("Snapshot channel failed");
991 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
995 health_code_update();
997 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
998 if (ret_send_status
< 0) {
999 /* Somehow, the session daemon is not responding anymore. */
1004 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1006 uint64_t key
= msg
.u
.destroy_channel
.key
;
1007 struct lttng_consumer_channel
*channel
;
1008 int ret_send_status
;
1010 channel
= consumer_find_channel(key
);
1012 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1013 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1016 health_code_update();
1018 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1019 if (ret_send_status
< 0) {
1020 /* Somehow, the session daemon is not responding anymore. */
1021 goto end_destroy_channel
;
1024 health_code_update();
1026 /* Stop right now if no channel was found. */
1028 goto end_destroy_channel
;
1032 * This command should ONLY be issued for channel with streams set in
1035 LTTNG_ASSERT(!channel
->monitor
);
1038 * The refcount should ALWAYS be 0 in the case of a channel in no
1041 LTTNG_ASSERT(!uatomic_sub_return(&channel
->refcount
, 1));
1043 consumer_del_channel(channel
);
1044 end_destroy_channel
:
1047 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1051 struct lttng_consumer_channel
*channel
;
1052 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1053 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1055 DBG("Kernel consumer discarded events command for session id %"
1056 PRIu64
", channel key %" PRIu64
, id
, key
);
1058 channel
= consumer_find_channel(key
);
1060 ERR("Kernel consumer discarded events channel %"
1061 PRIu64
" not found", key
);
1064 count
= channel
->discarded_events
;
1067 health_code_update();
1069 /* Send back returned value to session daemon */
1070 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1072 PERROR("send discarded events");
1078 case LTTNG_CONSUMER_LOST_PACKETS
:
1082 struct lttng_consumer_channel
*channel
;
1083 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1084 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1086 DBG("Kernel consumer lost packets command for session id %"
1087 PRIu64
", channel key %" PRIu64
, id
, key
);
1089 channel
= consumer_find_channel(key
);
1091 ERR("Kernel consumer lost packets channel %"
1092 PRIu64
" not found", key
);
1095 count
= channel
->lost_packets
;
1098 health_code_update();
1100 /* Send back returned value to session daemon */
1101 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1103 PERROR("send lost packets");
1109 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1111 int channel_monitor_pipe
;
1112 int ret_send_status
, ret_set_channel_monitor_pipe
;
1115 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1116 /* Successfully received the command's type. */
1117 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1118 if (ret_send_status
< 0) {
1122 ret_recv
= lttcomm_recv_fds_unix_sock(
1123 sock
, &channel_monitor_pipe
, 1);
1124 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1125 ERR("Failed to receive channel monitor pipe");
1129 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1130 ret_set_channel_monitor_pipe
=
1131 consumer_timer_thread_set_channel_monitor_pipe(
1132 channel_monitor_pipe
);
1133 if (!ret_set_channel_monitor_pipe
) {
1137 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1138 /* Set the pipe as non-blocking. */
1139 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1140 if (ret_fcntl
== -1) {
1141 PERROR("fcntl get flags of the channel monitoring pipe");
1146 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
1147 flags
| O_NONBLOCK
);
1148 if (ret_fcntl
== -1) {
1149 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1152 DBG("Channel monitor pipe set as non-blocking");
1154 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1156 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1157 if (ret_send_status
< 0) {
1162 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1164 struct lttng_consumer_channel
*channel
;
1165 uint64_t key
= msg
.u
.rotate_channel
.key
;
1166 int ret_send_status
;
1168 DBG("Consumer rotate channel %" PRIu64
, key
);
1170 channel
= consumer_find_channel(key
);
1172 ERR("Channel %" PRIu64
" not found", key
);
1173 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1176 * Sample the rotate position of all the streams in this channel.
1178 int ret_rotate_channel
;
1180 ret_rotate_channel
= lttng_consumer_rotate_channel(
1182 msg
.u
.rotate_channel
.relayd_id
,
1183 msg
.u
.rotate_channel
.metadata
, ctx
);
1184 if (ret_rotate_channel
< 0) {
1185 ERR("Rotate channel failed");
1186 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1189 health_code_update();
1192 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1193 if (ret_send_status
< 0) {
1194 /* Somehow, the session daemon is not responding anymore. */
1195 goto error_rotate_channel
;
1198 /* Rotate the streams that are ready right now. */
1201 ret_rotate
= lttng_consumer_rotate_ready_streams(
1203 if (ret_rotate
< 0) {
1204 ERR("Rotate ready streams failed");
1208 error_rotate_channel
:
1211 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1213 struct lttng_consumer_channel
*channel
;
1214 uint64_t key
= msg
.u
.clear_channel
.key
;
1215 int ret_send_status
;
1217 channel
= consumer_find_channel(key
);
1219 DBG("Channel %" PRIu64
" not found", key
);
1220 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1222 int ret_clear_channel
;
1225 lttng_consumer_clear_channel(channel
);
1226 if (ret_clear_channel
) {
1227 ERR("Clear channel failed");
1228 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
1231 health_code_update();
1234 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1235 if (ret_send_status
< 0) {
1236 /* Somehow, the session daemon is not responding anymore. */
1242 case LTTNG_CONSUMER_INIT
:
1244 int ret_send_status
;
1246 ret_code
= lttng_consumer_init_command(ctx
,
1247 msg
.u
.init
.sessiond_uuid
);
1248 health_code_update();
1249 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1250 if (ret_send_status
< 0) {
1251 /* Somehow, the session daemon is not responding anymore. */
1256 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1258 const struct lttng_credentials credentials
= {
1259 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
1260 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
1262 const bool is_local_trace
=
1263 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1264 const uint64_t relayd_id
=
1265 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1266 const char *chunk_override_name
=
1267 *msg
.u
.create_trace_chunk
.override_name
?
1268 msg
.u
.create_trace_chunk
.override_name
:
1270 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1273 * The session daemon will only provide a chunk directory file
1274 * descriptor for local traces.
1276 if (is_local_trace
) {
1278 int ret_send_status
;
1281 /* Acnowledge the reception of the command. */
1282 ret_send_status
= consumer_send_status_msg(
1283 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1284 if (ret_send_status
< 0) {
1285 /* Somehow, the session daemon is not responding anymore. */
1289 ret_recv
= lttcomm_recv_fds_unix_sock(
1290 sock
, &chunk_dirfd
, 1);
1291 if (ret_recv
!= sizeof(chunk_dirfd
)) {
1292 ERR("Failed to receive trace chunk directory file descriptor");
1296 DBG("Received trace chunk directory fd (%d)",
1298 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1300 if (!chunk_directory_handle
) {
1301 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1302 if (close(chunk_dirfd
)) {
1303 PERROR("Failed to close chunk directory file descriptor");
1309 ret_code
= lttng_consumer_create_trace_chunk(
1310 !is_local_trace
? &relayd_id
: NULL
,
1311 msg
.u
.create_trace_chunk
.session_id
,
1312 msg
.u
.create_trace_chunk
.chunk_id
,
1313 (time_t) msg
.u
.create_trace_chunk
1314 .creation_timestamp
,
1315 chunk_override_name
,
1316 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1319 chunk_directory_handle
);
1320 lttng_directory_handle_put(chunk_directory_handle
);
1321 goto end_msg_sessiond
;
1323 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1325 enum lttng_trace_chunk_command_type close_command
=
1326 (lttng_trace_chunk_command_type
) msg
.u
.close_trace_chunk
.close_command
.value
;
1327 const uint64_t relayd_id
=
1328 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1329 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1330 char path
[LTTNG_PATH_MAX
];
1333 ret_code
= lttng_consumer_close_trace_chunk(
1334 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1337 msg
.u
.close_trace_chunk
.session_id
,
1338 msg
.u
.close_trace_chunk
.chunk_id
,
1339 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1340 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1343 reply
.ret_code
= ret_code
;
1344 reply
.path_length
= strlen(path
) + 1;
1345 ret_send
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1346 if (ret_send
!= sizeof(reply
)) {
1349 ret_send
= lttcomm_send_unix_sock(
1350 sock
, path
, reply
.path_length
);
1351 if (ret_send
!= reply
.path_length
) {
1356 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1358 const uint64_t relayd_id
=
1359 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1361 ret_code
= lttng_consumer_trace_chunk_exists(
1362 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1364 msg
.u
.trace_chunk_exists
.session_id
,
1365 msg
.u
.trace_chunk_exists
.chunk_id
);
1366 goto end_msg_sessiond
;
1368 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
1370 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
1371 struct lttng_consumer_channel
*channel
=
1372 consumer_find_channel(key
);
1375 pthread_mutex_lock(&channel
->lock
);
1376 ret_code
= lttng_consumer_open_channel_packets(channel
);
1377 pthread_mutex_unlock(&channel
->lock
);
1379 WARN("Channel %" PRIu64
" not found", key
);
1380 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1383 health_code_update();
1384 goto end_msg_sessiond
;
1392 * Return 1 to indicate success since the 0 value can be a socket
1393 * shutdown during the recv() or send() call.
1398 /* This will issue a consumer stop. */
1403 * The returned value here is not useful since either way we'll return 1 to
1404 * the caller because the session daemon socket management is done
1405 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1408 int ret_send_status
;
1410 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1411 if (ret_send_status
< 0) {
1419 health_code_update();
1425 * Sync metadata meaning request them to the session daemon and snapshot to the
1426 * metadata thread can consumer them.
1428 * Metadata stream lock MUST be acquired.
1430 enum sync_metadata_status
lttng_kconsumer_sync_metadata(
1431 struct lttng_consumer_stream
*metadata
)
1434 enum sync_metadata_status status
;
1436 LTTNG_ASSERT(metadata
);
1438 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1440 ERR("Failed to flush kernel stream");
1441 status
= SYNC_METADATA_STATUS_ERROR
;
1445 ret
= kernctl_snapshot(metadata
->wait_fd
);
1447 if (errno
== EAGAIN
) {
1448 /* No new metadata, exit. */
1449 DBG("Sync metadata, no new kernel metadata");
1450 status
= SYNC_METADATA_STATUS_NO_DATA
;
1452 ERR("Sync metadata, taking kernel snapshot failed.");
1453 status
= SYNC_METADATA_STATUS_ERROR
;
1456 status
= SYNC_METADATA_STATUS_NEW_DATA
;
1464 int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
1465 struct stream_subbuffer
*subbuf
)
1469 ret
= kernctl_get_subbuf_size(
1470 stream
->wait_fd
, &subbuf
->info
.data
.subbuf_size
);
1475 ret
= kernctl_get_padded_subbuf_size(
1476 stream
->wait_fd
, &subbuf
->info
.data
.padded_subbuf_size
);
1486 int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
1487 struct stream_subbuffer
*subbuf
)
1491 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1496 ret
= kernctl_get_metadata_version(
1497 stream
->wait_fd
, &subbuf
->info
.metadata
.version
);
1507 int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
1508 struct stream_subbuffer
*subbuf
)
1512 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1517 ret
= kernctl_get_packet_size(
1518 stream
->wait_fd
, &subbuf
->info
.data
.packet_size
);
1520 PERROR("Failed to get sub-buffer packet size");
1524 ret
= kernctl_get_content_size(
1525 stream
->wait_fd
, &subbuf
->info
.data
.content_size
);
1527 PERROR("Failed to get sub-buffer content size");
1531 ret
= kernctl_get_timestamp_begin(
1532 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_begin
);
1534 PERROR("Failed to get sub-buffer begin timestamp");
1538 ret
= kernctl_get_timestamp_end(
1539 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_end
);
1541 PERROR("Failed to get sub-buffer end timestamp");
1545 ret
= kernctl_get_events_discarded(
1546 stream
->wait_fd
, &subbuf
->info
.data
.events_discarded
);
1548 PERROR("Failed to get sub-buffer events discarded count");
1552 ret
= kernctl_get_sequence_number(stream
->wait_fd
,
1553 &subbuf
->info
.data
.sequence_number
.value
);
1555 /* May not be supported by older LTTng-modules. */
1556 if (ret
!= -ENOTTY
) {
1557 PERROR("Failed to get sub-buffer sequence number");
1561 subbuf
->info
.data
.sequence_number
.is_set
= true;
1564 ret
= kernctl_get_stream_id(
1565 stream
->wait_fd
, &subbuf
->info
.data
.stream_id
);
1567 PERROR("Failed to get stream id");
1571 ret
= kernctl_get_instance_id(stream
->wait_fd
,
1572 &subbuf
->info
.data
.stream_instance_id
.value
);
1574 /* May not be supported by older LTTng-modules. */
1575 if (ret
!= -ENOTTY
) {
1576 PERROR("Failed to get stream instance id");
1580 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
1587 enum get_next_subbuffer_status
get_subbuffer_common(
1588 struct lttng_consumer_stream
*stream
,
1589 struct stream_subbuffer
*subbuffer
)
1592 enum get_next_subbuffer_status status
;
1594 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1597 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1602 * The caller only expects -ENODATA when there is no data to
1603 * read, but the kernel tracer returns -EAGAIN when there is
1604 * currently no data for a non-finalized stream, and -ENODATA
1605 * when there is no data for a finalized stream. Those can be
1606 * combined into a -ENODATA return value.
1608 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1611 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1615 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1618 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1625 enum get_next_subbuffer_status
get_next_subbuffer_splice(
1626 struct lttng_consumer_stream
*stream
,
1627 struct stream_subbuffer
*subbuffer
)
1629 const enum get_next_subbuffer_status status
=
1630 get_subbuffer_common(stream
, subbuffer
);
1632 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1636 subbuffer
->buffer
.fd
= stream
->wait_fd
;
1642 enum get_next_subbuffer_status
get_next_subbuffer_mmap(
1643 struct lttng_consumer_stream
*stream
,
1644 struct stream_subbuffer
*subbuffer
)
1647 enum get_next_subbuffer_status status
;
1650 status
= get_subbuffer_common(stream
, subbuffer
);
1651 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1655 ret
= get_current_subbuf_addr(stream
, &addr
);
1657 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1661 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1662 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1668 enum get_next_subbuffer_status
get_next_subbuffer_metadata_check(struct lttng_consumer_stream
*stream
,
1669 struct stream_subbuffer
*subbuffer
)
1674 enum get_next_subbuffer_status status
;
1676 ret
= kernctl_get_next_subbuf_metadata_check(stream
->wait_fd
,
1682 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1688 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
1690 ret
= get_current_subbuf_addr(stream
, &addr
);
1695 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1696 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1697 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1698 subbuffer
->info
.metadata
.padded_subbuf_size
,
1699 coherent
? "true" : "false");
1702 * The caller only expects -ENODATA when there is no data to read, but
1703 * the kernel tracer returns -EAGAIN when there is currently no data
1704 * for a non-finalized stream, and -ENODATA when there is no data for a
1705 * finalized stream. Those can be combined into a -ENODATA return value.
1709 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1714 * The caller only expects -ENODATA when there is no data to
1715 * read, but the kernel tracer returns -EAGAIN when there is
1716 * currently no data for a non-finalized stream, and -ENODATA
1717 * when there is no data for a finalized stream. Those can be
1718 * combined into a -ENODATA return value.
1720 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1723 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1731 int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
1732 struct stream_subbuffer
*subbuffer
)
1734 const int ret
= kernctl_put_next_subbuf(stream
->wait_fd
);
1737 if (ret
== -EFAULT
) {
1738 PERROR("Error in unreserving sub buffer");
1739 } else if (ret
== -EIO
) {
1740 /* Should never happen with newer LTTng versions */
1741 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1749 bool is_get_next_check_metadata_available(int tracer_fd
)
1751 const int ret
= kernctl_get_next_subbuf_metadata_check(tracer_fd
, NULL
);
1752 const bool available
= ret
!= -ENOTTY
;
1755 /* get succeeded, make sure to put the subbuffer. */
1756 kernctl_put_subbuf(tracer_fd
);
1763 int signal_metadata(struct lttng_consumer_stream
*stream
,
1764 struct lttng_consumer_local_data
*ctx
)
1766 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
1767 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
1771 int lttng_kconsumer_set_stream_ops(
1772 struct lttng_consumer_stream
*stream
)
1776 if (stream
->metadata_flag
&& stream
->chan
->is_live
) {
1777 DBG("Attempting to enable metadata bucketization for live consumers");
1778 if (is_get_next_check_metadata_available(stream
->wait_fd
)) {
1779 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1780 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1781 get_next_subbuffer_metadata_check
;
1782 ret
= consumer_stream_enable_metadata_bucketization(
1789 * The kernel tracer version is too old to indicate
1790 * when the metadata stream has reached a "coherent"
1791 * (parseable) point.
1793 * This means that a live viewer may see an incoherent
1794 * sequence of metadata and fail to parse it.
1796 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1797 metadata_bucket_destroy(stream
->metadata_bucket
);
1798 stream
->metadata_bucket
= NULL
;
1801 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
1804 if (!stream
->read_subbuffer_ops
.get_next_subbuffer
) {
1805 if (stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
) {
1806 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1807 get_next_subbuffer_mmap
;
1809 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1810 get_next_subbuffer_splice
;
1814 if (stream
->metadata_flag
) {
1815 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1816 extract_metadata_subbuffer_info
;
1818 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1819 extract_data_subbuffer_info
;
1820 if (stream
->chan
->is_live
) {
1821 stream
->read_subbuffer_ops
.send_live_beacon
=
1822 consumer_flush_kernel_index
;
1826 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
1831 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1835 LTTNG_ASSERT(stream
);
1838 * Don't create anything if this is set for streaming or if there is
1839 * no current trace chunk on the parent channel.
1841 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1842 stream
->chan
->trace_chunk
) {
1843 ret
= consumer_stream_create_output_files(stream
, true);
1849 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1850 /* get the len of the mmap region */
1851 unsigned long mmap_len
;
1853 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1855 PERROR("kernctl_get_mmap_len");
1856 goto error_close_fd
;
1858 stream
->mmap_len
= (size_t) mmap_len
;
1860 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1861 MAP_PRIVATE
, stream
->wait_fd
, 0);
1862 if (stream
->mmap_base
== MAP_FAILED
) {
1863 PERROR("Error mmaping");
1865 goto error_close_fd
;
1869 ret
= lttng_kconsumer_set_stream_ops(stream
);
1871 goto error_close_fd
;
1874 /* we return 0 to let the library handle the FD internally */
1878 if (stream
->out_fd
>= 0) {
1881 err
= close(stream
->out_fd
);
1883 stream
->out_fd
= -1;
1890 * Check if data is still being extracted from the buffers for a specific
1891 * stream. Consumer data lock MUST be acquired before calling this function
1892 * and the stream lock.
1894 * Return 1 if the traced data are still getting read else 0 meaning that the
1895 * data is available for trace viewer reading.
1897 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1901 LTTNG_ASSERT(stream
);
1903 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1908 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1910 /* There is still data so let's put back this subbuffer. */
1911 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1912 LTTNG_ASSERT(ret
== 0);
1913 ret
= 1; /* Data is pending */
1917 /* Data is NOT pending and ready to be read. */