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
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 consumer_stream_destroy(metadata_stream
, NULL
);
439 metadata_channel
->metadata_stream
= NULL
;
445 * Receive command from session daemon and process it.
447 * Return 1 on success else a negative value or 0.
449 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
450 int sock
, struct pollfd
*consumer_sockpoll
)
453 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
454 struct lttcomm_consumer_msg msg
;
456 health_code_update();
461 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
462 if (ret_recv
!= sizeof(msg
)) {
464 lttng_consumer_send_error(ctx
,
465 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
472 health_code_update();
474 /* Deprecated command */
475 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
477 health_code_update();
479 /* relayd needs RCU read-side protection */
482 switch (msg
.cmd_type
) {
483 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
485 uint32_t major
= msg
.u
.relayd_sock
.major
;
486 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
487 enum lttcomm_sock_proto protocol
= (enum lttcomm_sock_proto
)
488 msg
.u
.relayd_sock
.relayd_socket_protocol
;
490 /* Session daemon status message are handled in the following call. */
491 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
492 msg
.u
.relayd_sock
.type
, ctx
, sock
,
493 consumer_sockpoll
, msg
.u
.relayd_sock
.session_id
,
494 msg
.u
.relayd_sock
.relayd_session_id
, major
,
498 case LTTNG_CONSUMER_ADD_CHANNEL
:
500 struct lttng_consumer_channel
*new_channel
;
501 int ret_send_status
, ret_add_channel
= 0;
502 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
504 health_code_update();
506 /* First send a status message before receiving the fds. */
507 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
508 if (ret_send_status
< 0) {
509 /* Somehow, the session daemon is not responding anymore. */
513 health_code_update();
515 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
516 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
517 msg
.u
.channel
.session_id
,
518 msg
.u
.channel
.chunk_id
.is_set
?
520 msg
.u
.channel
.pathname
,
522 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
523 msg
.u
.channel
.tracefile_size
,
524 msg
.u
.channel
.tracefile_count
, 0,
525 msg
.u
.channel
.monitor
,
526 msg
.u
.channel
.live_timer_interval
,
527 msg
.u
.channel
.is_live
,
529 if (new_channel
== NULL
) {
530 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
533 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
534 switch (msg
.u
.channel
.output
) {
535 case LTTNG_EVENT_SPLICE
:
536 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
538 case LTTNG_EVENT_MMAP
:
539 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
542 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
546 /* Translate and save channel type. */
547 switch (msg
.u
.channel
.type
) {
548 case CONSUMER_CHANNEL_TYPE_DATA
:
549 case CONSUMER_CHANNEL_TYPE_METADATA
:
550 new_channel
->type
= (consumer_channel_type
) msg
.u
.channel
.type
;
557 health_code_update();
559 if (ctx
->on_recv_channel
!= NULL
) {
560 int ret_recv_channel
=
561 ctx
->on_recv_channel(new_channel
);
562 if (ret_recv_channel
== 0) {
563 ret_add_channel
= consumer_add_channel(
565 } else if (ret_recv_channel
< 0) {
570 consumer_add_channel(new_channel
, ctx
);
572 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&&
574 int monitor_start_ret
;
576 DBG("Consumer starting monitor timer");
577 consumer_timer_live_start(new_channel
,
578 msg
.u
.channel
.live_timer_interval
);
579 monitor_start_ret
= consumer_timer_monitor_start(
581 msg
.u
.channel
.monitor_timer_interval
);
582 if (monitor_start_ret
< 0) {
583 ERR("Starting channel monitoring timer failed");
588 health_code_update();
590 /* If we received an error in add_channel, we need to report it. */
591 if (ret_add_channel
< 0) {
592 ret_send_status
= consumer_send_status_msg(
593 sock
, ret_add_channel
);
594 if (ret_send_status
< 0) {
602 case LTTNG_CONSUMER_ADD_STREAM
:
605 struct lttng_pipe
*stream_pipe
;
606 struct lttng_consumer_stream
*new_stream
;
607 struct lttng_consumer_channel
*channel
;
609 int ret_send_status
, ret_poll
, ret_get_max_subbuf_size
;
610 ssize_t ret_pipe_write
, ret_recv
;
613 * Get stream's channel reference. Needed when adding the stream to the
616 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
619 * We could not find the channel. Can happen if cpu hotplug
620 * happens while tearing down.
622 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
623 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
626 health_code_update();
628 /* First send a status message before receiving the fds. */
629 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
630 if (ret_send_status
< 0) {
631 /* Somehow, the session daemon is not responding anymore. */
632 goto error_add_stream_fatal
;
635 health_code_update();
637 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
638 /* Channel was not found. */
639 goto error_add_stream_nosignal
;
644 ret_poll
= lttng_consumer_poll_socket(consumer_sockpoll
);
647 goto error_add_stream_fatal
;
650 health_code_update();
652 /* Get stream file descriptor from socket */
653 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
654 if (ret_recv
!= sizeof(fd
)) {
655 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
660 health_code_update();
663 * Send status code to session daemon only if the recv works. If the
664 * above recv() failed, the session daemon is notified through the
665 * error socket and the teardown is eventually done.
667 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
668 if (ret_send_status
< 0) {
669 /* Somehow, the session daemon is not responding anymore. */
670 goto error_add_stream_nosignal
;
673 health_code_update();
675 pthread_mutex_lock(&channel
->lock
);
676 new_stream
= consumer_stream_create(
683 channel
->trace_chunk
,
688 if (new_stream
== NULL
) {
693 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
696 pthread_mutex_unlock(&channel
->lock
);
697 goto error_add_stream_nosignal
;
700 new_stream
->wait_fd
= fd
;
701 ret_get_max_subbuf_size
= kernctl_get_max_subbuf_size(
702 new_stream
->wait_fd
, &new_stream
->max_sb_size
);
703 if (ret_get_max_subbuf_size
< 0) {
704 pthread_mutex_unlock(&channel
->lock
);
705 ERR("Failed to get kernel maximal subbuffer size");
706 goto error_add_stream_nosignal
;
709 consumer_stream_update_channel_attributes(new_stream
,
713 * We've just assigned the channel to the stream so increment the
714 * refcount right now. We don't need to increment the refcount for
715 * streams in no monitor because we handle manually the cleanup of
716 * those. It is very important to make sure there is NO prior
717 * consumer_del_stream() calls or else the refcount will be unbalanced.
719 if (channel
->monitor
) {
720 uatomic_inc(&new_stream
->chan
->refcount
);
724 * The buffer flush is done on the session daemon side for the kernel
725 * so no need for the stream "hangup_flush_done" variable to be
726 * tracked. This is important for a kernel stream since we don't rely
727 * on the flush state of the stream to read data. It's not the case for
728 * user space tracing.
730 new_stream
->hangup_flush_done
= 0;
732 health_code_update();
734 pthread_mutex_lock(&new_stream
->lock
);
735 if (ctx
->on_recv_stream
) {
736 int ret_recv_stream
= ctx
->on_recv_stream(new_stream
);
737 if (ret_recv_stream
< 0) {
738 pthread_mutex_unlock(&new_stream
->lock
);
739 pthread_mutex_unlock(&channel
->lock
);
740 consumer_stream_free(new_stream
);
741 goto error_add_stream_nosignal
;
744 health_code_update();
746 if (new_stream
->metadata_flag
) {
747 channel
->metadata_stream
= new_stream
;
750 /* Do not monitor this stream. */
751 if (!channel
->monitor
) {
752 DBG("Kernel consumer add stream %s in no monitor mode with "
753 "relayd id %" PRIu64
, new_stream
->name
,
754 new_stream
->net_seq_idx
);
755 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
756 pthread_mutex_unlock(&new_stream
->lock
);
757 pthread_mutex_unlock(&channel
->lock
);
761 /* Send stream to relayd if the stream has an ID. */
762 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
763 int ret_send_relayd_stream
;
765 ret_send_relayd_stream
= consumer_send_relayd_stream(
766 new_stream
, new_stream
->chan
->pathname
);
767 if (ret_send_relayd_stream
< 0) {
768 pthread_mutex_unlock(&new_stream
->lock
);
769 pthread_mutex_unlock(&channel
->lock
);
770 consumer_stream_free(new_stream
);
771 goto error_add_stream_nosignal
;
775 * If adding an extra stream to an already
776 * existing channel (e.g. cpu hotplug), we need
777 * to send the "streams_sent" command to relayd.
779 if (channel
->streams_sent_to_relayd
) {
780 int ret_send_relayd_streams_sent
;
782 ret_send_relayd_streams_sent
=
783 consumer_send_relayd_streams_sent(
784 new_stream
->net_seq_idx
);
785 if (ret_send_relayd_streams_sent
< 0) {
786 pthread_mutex_unlock(&new_stream
->lock
);
787 pthread_mutex_unlock(&channel
->lock
);
788 goto error_add_stream_nosignal
;
792 pthread_mutex_unlock(&new_stream
->lock
);
793 pthread_mutex_unlock(&channel
->lock
);
795 /* Get the right pipe where the stream will be sent. */
796 if (new_stream
->metadata_flag
) {
797 consumer_add_metadata_stream(new_stream
);
798 stream_pipe
= ctx
->consumer_metadata_pipe
;
800 consumer_add_data_stream(new_stream
);
801 stream_pipe
= ctx
->consumer_data_pipe
;
804 /* Visible to other threads */
805 new_stream
->globally_visible
= 1;
807 health_code_update();
809 ret_pipe_write
= lttng_pipe_write(
810 stream_pipe
, &new_stream
, sizeof(new_stream
));
811 if (ret_pipe_write
< 0) {
812 ERR("Consumer write %s stream to pipe %d",
813 new_stream
->metadata_flag
? "metadata" : "data",
814 lttng_pipe_get_writefd(stream_pipe
));
815 if (new_stream
->metadata_flag
) {
816 consumer_del_stream_for_metadata(new_stream
);
818 consumer_del_stream_for_data(new_stream
);
820 goto error_add_stream_nosignal
;
823 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
824 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
827 error_add_stream_nosignal
:
829 error_add_stream_fatal
:
832 case LTTNG_CONSUMER_STREAMS_SENT
:
834 struct lttng_consumer_channel
*channel
;
838 * Get stream's channel reference. Needed when adding the stream to the
841 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
844 * We could not find the channel. Can happen if cpu hotplug
845 * happens while tearing down.
847 ERR("Unable to find channel key %" PRIu64
,
848 msg
.u
.sent_streams
.channel_key
);
849 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
852 health_code_update();
855 * Send status code to session daemon.
857 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
858 if (ret_send_status
< 0 ||
859 ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
860 /* Somehow, the session daemon is not responding anymore. */
861 goto error_streams_sent_nosignal
;
864 health_code_update();
867 * We should not send this message if we don't monitor the
868 * streams in this channel.
870 if (!channel
->monitor
) {
871 goto end_error_streams_sent
;
874 health_code_update();
875 /* Send stream to relayd if the stream has an ID. */
876 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
877 int ret_send_relay_streams
;
879 ret_send_relay_streams
= consumer_send_relayd_streams_sent(
880 msg
.u
.sent_streams
.net_seq_idx
);
881 if (ret_send_relay_streams
< 0) {
882 goto error_streams_sent_nosignal
;
884 channel
->streams_sent_to_relayd
= true;
886 end_error_streams_sent
:
888 error_streams_sent_nosignal
:
891 case LTTNG_CONSUMER_UPDATE_STREAM
:
896 case LTTNG_CONSUMER_DESTROY_RELAYD
:
898 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
899 struct consumer_relayd_sock_pair
*relayd
;
902 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
904 /* Get relayd reference if exists. */
905 relayd
= consumer_find_relayd(index
);
906 if (relayd
== NULL
) {
907 DBG("Unable to find relayd %" PRIu64
, index
);
908 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
912 * Each relayd socket pair has a refcount of stream attached to it
913 * which tells if the relayd is still active or not depending on the
916 * This will set the destroy flag of the relayd object and destroy it
917 * if the refcount reaches zero when called.
919 * The destroy can happen either here or when a stream fd hangs up.
922 consumer_flag_relayd_for_destroy(relayd
);
925 health_code_update();
927 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
928 if (ret_send_status
< 0) {
929 /* Somehow, the session daemon is not responding anymore. */
935 case LTTNG_CONSUMER_DATA_PENDING
:
937 int32_t ret_data_pending
;
938 uint64_t id
= msg
.u
.data_pending
.session_id
;
941 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
943 ret_data_pending
= consumer_data_pending(id
);
945 health_code_update();
947 /* Send back returned value to session daemon */
948 ret_send
= lttcomm_send_unix_sock(sock
, &ret_data_pending
,
949 sizeof(ret_data_pending
));
951 PERROR("send data pending ret code");
956 * No need to send back a status message since the data pending
957 * returned value is the response.
961 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
963 struct lttng_consumer_channel
*channel
;
964 uint64_t key
= msg
.u
.snapshot_channel
.key
;
967 channel
= consumer_find_channel(key
);
969 ERR("Channel %" PRIu64
" not found", key
);
970 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
972 if (msg
.u
.snapshot_channel
.metadata
== 1) {
975 ret_snapshot
= lttng_kconsumer_snapshot_metadata(
977 msg
.u
.snapshot_channel
.pathname
,
978 msg
.u
.snapshot_channel
.relayd_id
,
980 if (ret_snapshot
< 0) {
981 ERR("Snapshot metadata failed");
982 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
987 ret_snapshot
= lttng_kconsumer_snapshot_channel(
989 msg
.u
.snapshot_channel
.pathname
,
990 msg
.u
.snapshot_channel
.relayd_id
,
991 msg
.u
.snapshot_channel
992 .nb_packets_per_stream
,
994 if (ret_snapshot
< 0) {
995 ERR("Snapshot channel failed");
996 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
1000 health_code_update();
1002 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1003 if (ret_send_status
< 0) {
1004 /* Somehow, the session daemon is not responding anymore. */
1009 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1011 uint64_t key
= msg
.u
.destroy_channel
.key
;
1012 struct lttng_consumer_channel
*channel
;
1013 int ret_send_status
;
1015 channel
= consumer_find_channel(key
);
1017 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1018 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1021 health_code_update();
1023 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1024 if (ret_send_status
< 0) {
1025 /* Somehow, the session daemon is not responding anymore. */
1026 goto end_destroy_channel
;
1029 health_code_update();
1031 /* Stop right now if no channel was found. */
1033 goto end_destroy_channel
;
1037 * This command should ONLY be issued for channel with streams set in
1040 LTTNG_ASSERT(!channel
->monitor
);
1043 * The refcount should ALWAYS be 0 in the case of a channel in no
1046 LTTNG_ASSERT(!uatomic_sub_return(&channel
->refcount
, 1));
1048 consumer_del_channel(channel
);
1049 end_destroy_channel
:
1052 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1056 struct lttng_consumer_channel
*channel
;
1057 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1058 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1060 DBG("Kernel consumer discarded events command for session id %"
1061 PRIu64
", channel key %" PRIu64
, id
, key
);
1063 channel
= consumer_find_channel(key
);
1065 ERR("Kernel consumer discarded events channel %"
1066 PRIu64
" not found", key
);
1069 count
= channel
->discarded_events
;
1072 health_code_update();
1074 /* Send back returned value to session daemon */
1075 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1077 PERROR("send discarded events");
1083 case LTTNG_CONSUMER_LOST_PACKETS
:
1087 struct lttng_consumer_channel
*channel
;
1088 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1089 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1091 DBG("Kernel consumer lost packets command for session id %"
1092 PRIu64
", channel key %" PRIu64
, id
, key
);
1094 channel
= consumer_find_channel(key
);
1096 ERR("Kernel consumer lost packets channel %"
1097 PRIu64
" not found", key
);
1100 count
= channel
->lost_packets
;
1103 health_code_update();
1105 /* Send back returned value to session daemon */
1106 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1108 PERROR("send lost packets");
1114 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1116 int channel_monitor_pipe
;
1117 int ret_send_status
, ret_set_channel_monitor_pipe
;
1120 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1121 /* Successfully received the command's type. */
1122 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1123 if (ret_send_status
< 0) {
1127 ret_recv
= lttcomm_recv_fds_unix_sock(
1128 sock
, &channel_monitor_pipe
, 1);
1129 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1130 ERR("Failed to receive channel monitor pipe");
1134 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1135 ret_set_channel_monitor_pipe
=
1136 consumer_timer_thread_set_channel_monitor_pipe(
1137 channel_monitor_pipe
);
1138 if (!ret_set_channel_monitor_pipe
) {
1142 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1143 /* Set the pipe as non-blocking. */
1144 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1145 if (ret_fcntl
== -1) {
1146 PERROR("fcntl get flags of the channel monitoring pipe");
1151 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
1152 flags
| O_NONBLOCK
);
1153 if (ret_fcntl
== -1) {
1154 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1157 DBG("Channel monitor pipe set as non-blocking");
1159 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1161 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1162 if (ret_send_status
< 0) {
1167 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1169 struct lttng_consumer_channel
*channel
;
1170 uint64_t key
= msg
.u
.rotate_channel
.key
;
1171 int ret_send_status
;
1173 DBG("Consumer rotate channel %" PRIu64
, key
);
1175 channel
= consumer_find_channel(key
);
1177 ERR("Channel %" PRIu64
" not found", key
);
1178 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1181 * Sample the rotate position of all the streams in this channel.
1183 int ret_rotate_channel
;
1185 ret_rotate_channel
= lttng_consumer_rotate_channel(
1187 msg
.u
.rotate_channel
.relayd_id
,
1188 msg
.u
.rotate_channel
.metadata
, ctx
);
1189 if (ret_rotate_channel
< 0) {
1190 ERR("Rotate channel failed");
1191 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1194 health_code_update();
1197 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1198 if (ret_send_status
< 0) {
1199 /* Somehow, the session daemon is not responding anymore. */
1200 goto error_rotate_channel
;
1203 /* Rotate the streams that are ready right now. */
1206 ret_rotate
= lttng_consumer_rotate_ready_streams(
1208 if (ret_rotate
< 0) {
1209 ERR("Rotate ready streams failed");
1213 error_rotate_channel
:
1216 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1218 struct lttng_consumer_channel
*channel
;
1219 uint64_t key
= msg
.u
.clear_channel
.key
;
1220 int ret_send_status
;
1222 channel
= consumer_find_channel(key
);
1224 DBG("Channel %" PRIu64
" not found", key
);
1225 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1227 int ret_clear_channel
;
1230 lttng_consumer_clear_channel(channel
);
1231 if (ret_clear_channel
) {
1232 ERR("Clear channel failed");
1233 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
1236 health_code_update();
1239 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1240 if (ret_send_status
< 0) {
1241 /* Somehow, the session daemon is not responding anymore. */
1247 case LTTNG_CONSUMER_INIT
:
1249 int ret_send_status
;
1251 ret_code
= lttng_consumer_init_command(ctx
,
1252 msg
.u
.init
.sessiond_uuid
);
1253 health_code_update();
1254 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1255 if (ret_send_status
< 0) {
1256 /* Somehow, the session daemon is not responding anymore. */
1261 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1263 const struct lttng_credentials credentials
= {
1264 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
1265 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
1267 const bool is_local_trace
=
1268 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1269 const uint64_t relayd_id
=
1270 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1271 const char *chunk_override_name
=
1272 *msg
.u
.create_trace_chunk
.override_name
?
1273 msg
.u
.create_trace_chunk
.override_name
:
1275 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1278 * The session daemon will only provide a chunk directory file
1279 * descriptor for local traces.
1281 if (is_local_trace
) {
1283 int ret_send_status
;
1286 /* Acnowledge the reception of the command. */
1287 ret_send_status
= consumer_send_status_msg(
1288 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1289 if (ret_send_status
< 0) {
1290 /* Somehow, the session daemon is not responding anymore. */
1294 ret_recv
= lttcomm_recv_fds_unix_sock(
1295 sock
, &chunk_dirfd
, 1);
1296 if (ret_recv
!= sizeof(chunk_dirfd
)) {
1297 ERR("Failed to receive trace chunk directory file descriptor");
1301 DBG("Received trace chunk directory fd (%d)",
1303 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1305 if (!chunk_directory_handle
) {
1306 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1307 if (close(chunk_dirfd
)) {
1308 PERROR("Failed to close chunk directory file descriptor");
1314 ret_code
= lttng_consumer_create_trace_chunk(
1315 !is_local_trace
? &relayd_id
: NULL
,
1316 msg
.u
.create_trace_chunk
.session_id
,
1317 msg
.u
.create_trace_chunk
.chunk_id
,
1318 (time_t) msg
.u
.create_trace_chunk
1319 .creation_timestamp
,
1320 chunk_override_name
,
1321 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1324 chunk_directory_handle
);
1325 lttng_directory_handle_put(chunk_directory_handle
);
1326 goto end_msg_sessiond
;
1328 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1330 enum lttng_trace_chunk_command_type close_command
=
1331 (lttng_trace_chunk_command_type
) msg
.u
.close_trace_chunk
.close_command
.value
;
1332 const uint64_t relayd_id
=
1333 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1334 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1335 char path
[LTTNG_PATH_MAX
];
1338 ret_code
= lttng_consumer_close_trace_chunk(
1339 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1342 msg
.u
.close_trace_chunk
.session_id
,
1343 msg
.u
.close_trace_chunk
.chunk_id
,
1344 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1345 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1348 reply
.ret_code
= ret_code
;
1349 reply
.path_length
= strlen(path
) + 1;
1350 ret_send
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1351 if (ret_send
!= sizeof(reply
)) {
1354 ret_send
= lttcomm_send_unix_sock(
1355 sock
, path
, reply
.path_length
);
1356 if (ret_send
!= reply
.path_length
) {
1361 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1363 const uint64_t relayd_id
=
1364 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1366 ret_code
= lttng_consumer_trace_chunk_exists(
1367 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1369 msg
.u
.trace_chunk_exists
.session_id
,
1370 msg
.u
.trace_chunk_exists
.chunk_id
);
1371 goto end_msg_sessiond
;
1373 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
1375 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
1376 struct lttng_consumer_channel
*channel
=
1377 consumer_find_channel(key
);
1380 pthread_mutex_lock(&channel
->lock
);
1381 ret_code
= lttng_consumer_open_channel_packets(channel
);
1382 pthread_mutex_unlock(&channel
->lock
);
1384 WARN("Channel %" PRIu64
" not found", key
);
1385 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1388 health_code_update();
1389 goto end_msg_sessiond
;
1397 * Return 1 to indicate success since the 0 value can be a socket
1398 * shutdown during the recv() or send() call.
1403 /* This will issue a consumer stop. */
1408 * The returned value here is not useful since either way we'll return 1 to
1409 * the caller because the session daemon socket management is done
1410 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1413 int ret_send_status
;
1415 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1416 if (ret_send_status
< 0) {
1424 health_code_update();
1430 * Sync metadata meaning request them to the session daemon and snapshot to the
1431 * metadata thread can consumer them.
1433 * Metadata stream lock MUST be acquired.
1435 enum sync_metadata_status
lttng_kconsumer_sync_metadata(
1436 struct lttng_consumer_stream
*metadata
)
1439 enum sync_metadata_status status
;
1441 LTTNG_ASSERT(metadata
);
1443 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1445 ERR("Failed to flush kernel stream");
1446 status
= SYNC_METADATA_STATUS_ERROR
;
1450 ret
= kernctl_snapshot(metadata
->wait_fd
);
1452 if (errno
== EAGAIN
) {
1453 /* No new metadata, exit. */
1454 DBG("Sync metadata, no new kernel metadata");
1455 status
= SYNC_METADATA_STATUS_NO_DATA
;
1457 ERR("Sync metadata, taking kernel snapshot failed.");
1458 status
= SYNC_METADATA_STATUS_ERROR
;
1461 status
= SYNC_METADATA_STATUS_NEW_DATA
;
1469 int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
1470 struct stream_subbuffer
*subbuf
)
1474 ret
= kernctl_get_subbuf_size(
1475 stream
->wait_fd
, &subbuf
->info
.data
.subbuf_size
);
1480 ret
= kernctl_get_padded_subbuf_size(
1481 stream
->wait_fd
, &subbuf
->info
.data
.padded_subbuf_size
);
1491 int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
1492 struct stream_subbuffer
*subbuf
)
1496 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1501 ret
= kernctl_get_metadata_version(
1502 stream
->wait_fd
, &subbuf
->info
.metadata
.version
);
1512 int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
1513 struct stream_subbuffer
*subbuf
)
1517 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1522 ret
= kernctl_get_packet_size(
1523 stream
->wait_fd
, &subbuf
->info
.data
.packet_size
);
1525 PERROR("Failed to get sub-buffer packet size");
1529 ret
= kernctl_get_content_size(
1530 stream
->wait_fd
, &subbuf
->info
.data
.content_size
);
1532 PERROR("Failed to get sub-buffer content size");
1536 ret
= kernctl_get_timestamp_begin(
1537 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_begin
);
1539 PERROR("Failed to get sub-buffer begin timestamp");
1543 ret
= kernctl_get_timestamp_end(
1544 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_end
);
1546 PERROR("Failed to get sub-buffer end timestamp");
1550 ret
= kernctl_get_events_discarded(
1551 stream
->wait_fd
, &subbuf
->info
.data
.events_discarded
);
1553 PERROR("Failed to get sub-buffer events discarded count");
1557 ret
= kernctl_get_sequence_number(stream
->wait_fd
,
1558 &subbuf
->info
.data
.sequence_number
.value
);
1560 /* May not be supported by older LTTng-modules. */
1561 if (ret
!= -ENOTTY
) {
1562 PERROR("Failed to get sub-buffer sequence number");
1566 subbuf
->info
.data
.sequence_number
.is_set
= true;
1569 ret
= kernctl_get_stream_id(
1570 stream
->wait_fd
, &subbuf
->info
.data
.stream_id
);
1572 PERROR("Failed to get stream id");
1576 ret
= kernctl_get_instance_id(stream
->wait_fd
,
1577 &subbuf
->info
.data
.stream_instance_id
.value
);
1579 /* May not be supported by older LTTng-modules. */
1580 if (ret
!= -ENOTTY
) {
1581 PERROR("Failed to get stream instance id");
1585 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
1592 enum get_next_subbuffer_status
get_subbuffer_common(
1593 struct lttng_consumer_stream
*stream
,
1594 struct stream_subbuffer
*subbuffer
)
1597 enum get_next_subbuffer_status status
;
1599 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1602 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1607 * The caller only expects -ENODATA when there is no data to
1608 * read, but the kernel tracer returns -EAGAIN when there is
1609 * currently no data for a non-finalized stream, and -ENODATA
1610 * when there is no data for a finalized stream. Those can be
1611 * combined into a -ENODATA return value.
1613 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1616 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1620 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1623 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1630 enum get_next_subbuffer_status
get_next_subbuffer_splice(
1631 struct lttng_consumer_stream
*stream
,
1632 struct stream_subbuffer
*subbuffer
)
1634 const enum get_next_subbuffer_status status
=
1635 get_subbuffer_common(stream
, subbuffer
);
1637 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1641 subbuffer
->buffer
.fd
= stream
->wait_fd
;
1647 enum get_next_subbuffer_status
get_next_subbuffer_mmap(
1648 struct lttng_consumer_stream
*stream
,
1649 struct stream_subbuffer
*subbuffer
)
1652 enum get_next_subbuffer_status status
;
1655 status
= get_subbuffer_common(stream
, subbuffer
);
1656 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1660 ret
= get_current_subbuf_addr(stream
, &addr
);
1662 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1666 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1667 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1673 enum get_next_subbuffer_status
get_next_subbuffer_metadata_check(struct lttng_consumer_stream
*stream
,
1674 struct stream_subbuffer
*subbuffer
)
1679 enum get_next_subbuffer_status status
;
1681 ret
= kernctl_get_next_subbuf_metadata_check(stream
->wait_fd
,
1687 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1693 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
1695 ret
= get_current_subbuf_addr(stream
, &addr
);
1700 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1701 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1702 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1703 subbuffer
->info
.metadata
.padded_subbuf_size
,
1704 coherent
? "true" : "false");
1707 * The caller only expects -ENODATA when there is no data to read, but
1708 * the kernel tracer returns -EAGAIN when there is currently no data
1709 * for a non-finalized stream, and -ENODATA when there is no data for a
1710 * finalized stream. Those can be combined into a -ENODATA return value.
1714 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1719 * The caller only expects -ENODATA when there is no data to
1720 * read, but the kernel tracer returns -EAGAIN when there is
1721 * currently no data for a non-finalized stream, and -ENODATA
1722 * when there is no data for a finalized stream. Those can be
1723 * combined into a -ENODATA return value.
1725 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1728 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1736 int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
1737 struct stream_subbuffer
*subbuffer
)
1739 const int ret
= kernctl_put_next_subbuf(stream
->wait_fd
);
1742 if (ret
== -EFAULT
) {
1743 PERROR("Error in unreserving sub buffer");
1744 } else if (ret
== -EIO
) {
1745 /* Should never happen with newer LTTng versions */
1746 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1754 bool is_get_next_check_metadata_available(int tracer_fd
)
1756 const int ret
= kernctl_get_next_subbuf_metadata_check(tracer_fd
, NULL
);
1757 const bool available
= ret
!= -ENOTTY
;
1760 /* get succeeded, make sure to put the subbuffer. */
1761 kernctl_put_subbuf(tracer_fd
);
1768 int signal_metadata(struct lttng_consumer_stream
*stream
,
1769 struct lttng_consumer_local_data
*ctx
)
1771 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
1772 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
1776 int lttng_kconsumer_set_stream_ops(
1777 struct lttng_consumer_stream
*stream
)
1781 if (stream
->metadata_flag
&& stream
->chan
->is_live
) {
1782 DBG("Attempting to enable metadata bucketization for live consumers");
1783 if (is_get_next_check_metadata_available(stream
->wait_fd
)) {
1784 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1785 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1786 get_next_subbuffer_metadata_check
;
1787 ret
= consumer_stream_enable_metadata_bucketization(
1794 * The kernel tracer version is too old to indicate
1795 * when the metadata stream has reached a "coherent"
1796 * (parseable) point.
1798 * This means that a live viewer may see an incoherent
1799 * sequence of metadata and fail to parse it.
1801 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1802 metadata_bucket_destroy(stream
->metadata_bucket
);
1803 stream
->metadata_bucket
= NULL
;
1806 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
1809 if (!stream
->read_subbuffer_ops
.get_next_subbuffer
) {
1810 if (stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
) {
1811 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1812 get_next_subbuffer_mmap
;
1814 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1815 get_next_subbuffer_splice
;
1819 if (stream
->metadata_flag
) {
1820 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1821 extract_metadata_subbuffer_info
;
1823 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1824 extract_data_subbuffer_info
;
1825 if (stream
->chan
->is_live
) {
1826 stream
->read_subbuffer_ops
.send_live_beacon
=
1827 consumer_flush_kernel_index
;
1831 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
1836 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1840 LTTNG_ASSERT(stream
);
1843 * Don't create anything if this is set for streaming or if there is
1844 * no current trace chunk on the parent channel.
1846 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1847 stream
->chan
->trace_chunk
) {
1848 ret
= consumer_stream_create_output_files(stream
, true);
1854 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1855 /* get the len of the mmap region */
1856 unsigned long mmap_len
;
1858 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1860 PERROR("kernctl_get_mmap_len");
1861 goto error_close_fd
;
1863 stream
->mmap_len
= (size_t) mmap_len
;
1865 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1866 MAP_PRIVATE
, stream
->wait_fd
, 0);
1867 if (stream
->mmap_base
== MAP_FAILED
) {
1868 PERROR("Error mmaping");
1870 goto error_close_fd
;
1874 ret
= lttng_kconsumer_set_stream_ops(stream
);
1876 goto error_close_fd
;
1879 /* we return 0 to let the library handle the FD internally */
1883 if (stream
->out_fd
>= 0) {
1886 err
= close(stream
->out_fd
);
1888 stream
->out_fd
= -1;
1895 * Check if data is still being extracted from the buffers for a specific
1896 * stream. Consumer data lock MUST be acquired before calling this function
1897 * and the stream lock.
1899 * Return 1 if the traced data are still getting read else 0 meaning that the
1900 * data is available for trace viewer reading.
1902 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1906 LTTNG_ASSERT(stream
);
1908 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1913 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1915 /* There is still data so let's put back this subbuffer. */
1916 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1917 LTTNG_ASSERT(ret
== 0);
1918 ret
= 1; /* Data is pending */
1922 /* Data is NOT pending and ready to be read. */