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.hpp>
24 #include <common/common.hpp>
25 #include <common/kernel-ctl/kernel-ctl.hpp>
26 #include <common/sessiond-comm/sessiond-comm.hpp>
27 #include <common/sessiond-comm/relayd.hpp>
28 #include <common/compat/fcntl.hpp>
29 #include <common/compat/endian.hpp>
30 #include <common/pipe.hpp>
31 #include <common/relayd/relayd.hpp>
32 #include <common/utils.hpp>
33 #include <common/consumer/consumer-stream.hpp>
34 #include <common/index/index.hpp>
35 #include <common/consumer/consumer-timer.hpp>
36 #include <common/optional.hpp>
37 #include <common/buffer-view.hpp>
38 #include <common/consumer/consumer.hpp>
39 #include <common/consumer/metadata-bucket.hpp>
41 #include "kernel-consumer.hpp"
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
)
151 struct lttng_consumer_stream
*stream
;
153 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
155 /* Prevent channel modifications while we perform the snapshot.*/
156 pthread_mutex_lock(&channel
->lock
);
160 /* Splice is not supported yet for channel snapshot. */
161 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
162 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
168 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
169 unsigned long consumed_pos
, produced_pos
;
171 health_code_update();
174 * Lock stream because we are about to change its state.
176 pthread_mutex_lock(&stream
->lock
);
178 LTTNG_ASSERT(channel
->trace_chunk
);
179 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
181 * Can't happen barring an internal error as the channel
182 * holds a reference to the trace chunk.
184 ERR("Failed to acquire reference to channel's trace chunk");
188 LTTNG_ASSERT(!stream
->trace_chunk
);
189 stream
->trace_chunk
= channel
->trace_chunk
;
192 * Assign the received relayd ID so we can use it for streaming. The streams
193 * are not visible to anyone so this is OK to change it.
195 stream
->net_seq_idx
= relayd_id
;
196 channel
->relayd_id
= relayd_id
;
197 if (relayd_id
!= (uint64_t) -1ULL) {
198 ret
= consumer_send_relayd_stream(stream
, path
);
200 ERR("sending stream to relayd");
201 goto error_close_stream_output
;
204 ret
= consumer_stream_create_output_files(stream
,
207 goto error_close_stream_output
;
209 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
213 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
216 * Doing a buffer flush which does not take into
217 * account empty packets. This is not perfect
218 * for stream intersection, but required as a
219 * fall-back when "flush_empty" is not
220 * implemented by lttng-modules.
222 ret
= kernctl_buffer_flush(stream
->wait_fd
);
224 ERR("Failed to flush kernel stream");
225 goto error_close_stream_output
;
230 ret
= lttng_kconsumer_take_snapshot(stream
);
232 ERR("Taking kernel snapshot");
233 goto error_close_stream_output
;
236 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
238 ERR("Produced kernel snapshot position");
239 goto error_close_stream_output
;
242 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
244 ERR("Consumerd kernel snapshot position");
245 goto error_close_stream_output
;
248 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
249 produced_pos
, nb_packets_per_stream
,
250 stream
->max_sb_size
);
252 while ((long) (consumed_pos
- produced_pos
) < 0) {
254 unsigned long len
, padded_len
;
255 const char *subbuf_addr
;
256 struct lttng_buffer_view subbuf_view
;
258 health_code_update();
259 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
261 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
263 if (ret
!= -EAGAIN
) {
264 PERROR("kernctl_get_subbuf snapshot");
265 goto error_close_stream_output
;
267 DBG("Kernel consumer get subbuf failed. Skipping it.");
268 consumed_pos
+= stream
->max_sb_size
;
269 stream
->chan
->lost_packets
++;
273 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
275 ERR("Snapshot kernctl_get_subbuf_size");
276 goto error_put_subbuf
;
279 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
281 ERR("Snapshot kernctl_get_padded_subbuf_size");
282 goto error_put_subbuf
;
285 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
287 goto error_put_subbuf
;
290 subbuf_view
= lttng_buffer_view_init(
291 subbuf_addr
, 0, padded_len
);
292 read_len
= lttng_consumer_on_read_subbuffer_mmap(
293 stream
, &subbuf_view
,
296 * We write the padded len in local tracefiles but the data len
297 * when using a relay. Display the error but continue processing
298 * to try to release the subbuffer.
300 if (relayd_id
!= (uint64_t) -1ULL) {
301 if (read_len
!= len
) {
302 ERR("Error sending to the relay (ret: %zd != len: %lu)",
306 if (read_len
!= padded_len
) {
307 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
308 read_len
, padded_len
);
312 ret
= kernctl_put_subbuf(stream
->wait_fd
);
314 ERR("Snapshot kernctl_put_subbuf");
315 goto error_close_stream_output
;
317 consumed_pos
+= stream
->max_sb_size
;
320 consumer_stream_close_output(stream
);
321 pthread_mutex_unlock(&stream
->lock
);
329 ret
= kernctl_put_subbuf(stream
->wait_fd
);
331 ERR("Snapshot kernctl_put_subbuf error path");
333 error_close_stream_output
:
334 consumer_stream_close_output(stream
);
336 pthread_mutex_unlock(&stream
->lock
);
339 pthread_mutex_unlock(&channel
->lock
);
344 * Read the whole metadata available for a snapshot.
345 * RCU read-side lock must be held across this function to ensure existence of
348 * Returns 0 on success, < 0 on error
350 static int lttng_kconsumer_snapshot_metadata(
351 struct lttng_consumer_channel
*metadata_channel
,
352 uint64_t key
, char *path
, uint64_t relayd_id
,
353 struct lttng_consumer_local_data
*ctx
)
355 int ret
, use_relayd
= 0;
357 struct lttng_consumer_stream
*metadata_stream
;
361 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
366 metadata_stream
= metadata_channel
->metadata_stream
;
367 LTTNG_ASSERT(metadata_stream
);
369 metadata_stream
->read_subbuffer_ops
.lock(metadata_stream
);
370 LTTNG_ASSERT(metadata_channel
->trace_chunk
);
371 LTTNG_ASSERT(metadata_stream
->trace_chunk
);
373 /* Flag once that we have a valid relayd for the stream. */
374 if (relayd_id
!= (uint64_t) -1ULL) {
379 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
384 ret
= consumer_stream_create_output_files(metadata_stream
,
392 health_code_update();
394 ret_read
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
396 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
401 } while (ret_read
> 0);
404 close_relayd_stream(metadata_stream
);
405 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
407 if (metadata_stream
->out_fd
>= 0) {
408 ret
= close(metadata_stream
->out_fd
);
410 PERROR("Kernel consumer snapshot metadata close out_fd");
412 * Don't go on error here since the snapshot was successful at this
413 * point but somehow the close failed.
416 metadata_stream
->out_fd
= -1;
417 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
418 metadata_stream
->trace_chunk
= NULL
;
424 metadata_stream
->read_subbuffer_ops
.unlock(metadata_stream
);
425 consumer_stream_destroy(metadata_stream
, NULL
);
426 metadata_channel
->metadata_stream
= NULL
;
432 * Receive command from session daemon and process it.
434 * Return 1 on success else a negative value or 0.
436 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
437 int sock
, struct pollfd
*consumer_sockpoll
)
440 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
441 struct lttcomm_consumer_msg msg
;
443 health_code_update();
448 ret_recv
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
449 if (ret_recv
!= sizeof(msg
)) {
451 lttng_consumer_send_error(ctx
,
452 LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
459 health_code_update();
461 /* Deprecated command */
462 LTTNG_ASSERT(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
464 health_code_update();
466 /* relayd needs RCU read-side protection */
469 switch (msg
.cmd_type
) {
470 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
472 uint32_t major
= msg
.u
.relayd_sock
.major
;
473 uint32_t minor
= msg
.u
.relayd_sock
.minor
;
474 enum lttcomm_sock_proto protocol
= (enum lttcomm_sock_proto
)
475 msg
.u
.relayd_sock
.relayd_socket_protocol
;
477 /* Session daemon status message are handled in the following call. */
478 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
479 msg
.u
.relayd_sock
.type
, ctx
, sock
,
480 consumer_sockpoll
, msg
.u
.relayd_sock
.session_id
,
481 msg
.u
.relayd_sock
.relayd_session_id
, major
,
485 case LTTNG_CONSUMER_ADD_CHANNEL
:
487 struct lttng_consumer_channel
*new_channel
;
488 int ret_send_status
, ret_add_channel
= 0;
489 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
491 health_code_update();
493 /* First send a status message before receiving the fds. */
494 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
495 if (ret_send_status
< 0) {
496 /* Somehow, the session daemon is not responding anymore. */
500 health_code_update();
502 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
503 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
504 msg
.u
.channel
.session_id
,
505 msg
.u
.channel
.chunk_id
.is_set
?
507 msg
.u
.channel
.pathname
,
509 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
510 msg
.u
.channel
.tracefile_size
,
511 msg
.u
.channel
.tracefile_count
, 0,
512 msg
.u
.channel
.monitor
,
513 msg
.u
.channel
.live_timer_interval
,
514 msg
.u
.channel
.is_live
,
516 if (new_channel
== NULL
) {
517 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
520 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
521 switch (msg
.u
.channel
.output
) {
522 case LTTNG_EVENT_SPLICE
:
523 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
525 case LTTNG_EVENT_MMAP
:
526 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
529 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
533 /* Translate and save channel type. */
534 switch (msg
.u
.channel
.type
) {
535 case CONSUMER_CHANNEL_TYPE_DATA
:
536 case CONSUMER_CHANNEL_TYPE_METADATA
:
537 new_channel
->type
= (consumer_channel_type
) msg
.u
.channel
.type
;
544 health_code_update();
546 if (ctx
->on_recv_channel
!= NULL
) {
547 int ret_recv_channel
=
548 ctx
->on_recv_channel(new_channel
);
549 if (ret_recv_channel
== 0) {
550 ret_add_channel
= consumer_add_channel(
552 } else if (ret_recv_channel
< 0) {
557 consumer_add_channel(new_channel
, ctx
);
559 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&&
561 int monitor_start_ret
;
563 DBG("Consumer starting monitor timer");
564 consumer_timer_live_start(new_channel
,
565 msg
.u
.channel
.live_timer_interval
);
566 monitor_start_ret
= consumer_timer_monitor_start(
568 msg
.u
.channel
.monitor_timer_interval
);
569 if (monitor_start_ret
< 0) {
570 ERR("Starting channel monitoring timer failed");
575 health_code_update();
577 /* If we received an error in add_channel, we need to report it. */
578 if (ret_add_channel
< 0) {
579 ret_send_status
= consumer_send_status_msg(
580 sock
, ret_add_channel
);
581 if (ret_send_status
< 0) {
589 case LTTNG_CONSUMER_ADD_STREAM
:
592 struct lttng_pipe
*stream_pipe
;
593 struct lttng_consumer_stream
*new_stream
;
594 struct lttng_consumer_channel
*channel
;
596 int ret_send_status
, ret_poll
, ret_get_max_subbuf_size
;
597 ssize_t ret_pipe_write
, ret_recv
;
600 * Get stream's channel reference. Needed when adding the stream to the
603 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
606 * We could not find the channel. Can happen if cpu hotplug
607 * happens while tearing down.
609 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
610 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
613 health_code_update();
615 /* First send a status message before receiving the fds. */
616 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
617 if (ret_send_status
< 0) {
618 /* Somehow, the session daemon is not responding anymore. */
619 goto error_add_stream_fatal
;
622 health_code_update();
624 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
625 /* Channel was not found. */
626 goto error_add_stream_nosignal
;
631 ret_poll
= lttng_consumer_poll_socket(consumer_sockpoll
);
634 goto error_add_stream_fatal
;
637 health_code_update();
639 /* Get stream file descriptor from socket */
640 ret_recv
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
641 if (ret_recv
!= sizeof(fd
)) {
642 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
647 health_code_update();
650 * Send status code to session daemon only if the recv works. If the
651 * above recv() failed, the session daemon is notified through the
652 * error socket and the teardown is eventually done.
654 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
655 if (ret_send_status
< 0) {
656 /* Somehow, the session daemon is not responding anymore. */
657 goto error_add_stream_nosignal
;
660 health_code_update();
662 pthread_mutex_lock(&channel
->lock
);
663 new_stream
= consumer_stream_create(
670 channel
->trace_chunk
,
675 if (new_stream
== NULL
) {
680 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
683 pthread_mutex_unlock(&channel
->lock
);
684 goto error_add_stream_nosignal
;
687 new_stream
->wait_fd
= fd
;
688 ret_get_max_subbuf_size
= kernctl_get_max_subbuf_size(
689 new_stream
->wait_fd
, &new_stream
->max_sb_size
);
690 if (ret_get_max_subbuf_size
< 0) {
691 pthread_mutex_unlock(&channel
->lock
);
692 ERR("Failed to get kernel maximal subbuffer size");
693 goto error_add_stream_nosignal
;
696 consumer_stream_update_channel_attributes(new_stream
,
700 * We've just assigned the channel to the stream so increment the
701 * refcount right now. We don't need to increment the refcount for
702 * streams in no monitor because we handle manually the cleanup of
703 * those. It is very important to make sure there is NO prior
704 * consumer_del_stream() calls or else the refcount will be unbalanced.
706 if (channel
->monitor
) {
707 uatomic_inc(&new_stream
->chan
->refcount
);
711 * The buffer flush is done on the session daemon side for the kernel
712 * so no need for the stream "hangup_flush_done" variable to be
713 * tracked. This is important for a kernel stream since we don't rely
714 * on the flush state of the stream to read data. It's not the case for
715 * user space tracing.
717 new_stream
->hangup_flush_done
= 0;
719 health_code_update();
721 pthread_mutex_lock(&new_stream
->lock
);
722 if (ctx
->on_recv_stream
) {
723 int ret_recv_stream
= ctx
->on_recv_stream(new_stream
);
724 if (ret_recv_stream
< 0) {
725 pthread_mutex_unlock(&new_stream
->lock
);
726 pthread_mutex_unlock(&channel
->lock
);
727 consumer_stream_free(new_stream
);
728 goto error_add_stream_nosignal
;
731 health_code_update();
733 if (new_stream
->metadata_flag
) {
734 channel
->metadata_stream
= new_stream
;
737 /* Do not monitor this stream. */
738 if (!channel
->monitor
) {
739 DBG("Kernel consumer add stream %s in no monitor mode with "
740 "relayd id %" PRIu64
, new_stream
->name
,
741 new_stream
->net_seq_idx
);
742 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
743 pthread_mutex_unlock(&new_stream
->lock
);
744 pthread_mutex_unlock(&channel
->lock
);
748 /* Send stream to relayd if the stream has an ID. */
749 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
750 int ret_send_relayd_stream
;
752 ret_send_relayd_stream
= consumer_send_relayd_stream(
753 new_stream
, new_stream
->chan
->pathname
);
754 if (ret_send_relayd_stream
< 0) {
755 pthread_mutex_unlock(&new_stream
->lock
);
756 pthread_mutex_unlock(&channel
->lock
);
757 consumer_stream_free(new_stream
);
758 goto error_add_stream_nosignal
;
762 * If adding an extra stream to an already
763 * existing channel (e.g. cpu hotplug), we need
764 * to send the "streams_sent" command to relayd.
766 if (channel
->streams_sent_to_relayd
) {
767 int ret_send_relayd_streams_sent
;
769 ret_send_relayd_streams_sent
=
770 consumer_send_relayd_streams_sent(
771 new_stream
->net_seq_idx
);
772 if (ret_send_relayd_streams_sent
< 0) {
773 pthread_mutex_unlock(&new_stream
->lock
);
774 pthread_mutex_unlock(&channel
->lock
);
775 goto error_add_stream_nosignal
;
779 pthread_mutex_unlock(&new_stream
->lock
);
780 pthread_mutex_unlock(&channel
->lock
);
782 /* Get the right pipe where the stream will be sent. */
783 if (new_stream
->metadata_flag
) {
784 consumer_add_metadata_stream(new_stream
);
785 stream_pipe
= ctx
->consumer_metadata_pipe
;
787 consumer_add_data_stream(new_stream
);
788 stream_pipe
= ctx
->consumer_data_pipe
;
791 /* Visible to other threads */
792 new_stream
->globally_visible
= 1;
794 health_code_update();
796 ret_pipe_write
= lttng_pipe_write(
797 stream_pipe
, &new_stream
, sizeof(new_stream
));
798 if (ret_pipe_write
< 0) {
799 ERR("Consumer write %s stream to pipe %d",
800 new_stream
->metadata_flag
? "metadata" : "data",
801 lttng_pipe_get_writefd(stream_pipe
));
802 if (new_stream
->metadata_flag
) {
803 consumer_del_stream_for_metadata(new_stream
);
805 consumer_del_stream_for_data(new_stream
);
807 goto error_add_stream_nosignal
;
810 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
811 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
814 error_add_stream_nosignal
:
816 error_add_stream_fatal
:
819 case LTTNG_CONSUMER_STREAMS_SENT
:
821 struct lttng_consumer_channel
*channel
;
825 * Get stream's channel reference. Needed when adding the stream to the
828 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
831 * We could not find the channel. Can happen if cpu hotplug
832 * happens while tearing down.
834 ERR("Unable to find channel key %" PRIu64
,
835 msg
.u
.sent_streams
.channel_key
);
836 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
839 health_code_update();
842 * Send status code to session daemon.
844 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
845 if (ret_send_status
< 0 ||
846 ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
847 /* Somehow, the session daemon is not responding anymore. */
848 goto error_streams_sent_nosignal
;
851 health_code_update();
854 * We should not send this message if we don't monitor the
855 * streams in this channel.
857 if (!channel
->monitor
) {
858 goto end_error_streams_sent
;
861 health_code_update();
862 /* Send stream to relayd if the stream has an ID. */
863 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
864 int ret_send_relay_streams
;
866 ret_send_relay_streams
= consumer_send_relayd_streams_sent(
867 msg
.u
.sent_streams
.net_seq_idx
);
868 if (ret_send_relay_streams
< 0) {
869 goto error_streams_sent_nosignal
;
871 channel
->streams_sent_to_relayd
= true;
873 end_error_streams_sent
:
875 error_streams_sent_nosignal
:
878 case LTTNG_CONSUMER_UPDATE_STREAM
:
883 case LTTNG_CONSUMER_DESTROY_RELAYD
:
885 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
886 struct consumer_relayd_sock_pair
*relayd
;
889 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
891 /* Get relayd reference if exists. */
892 relayd
= consumer_find_relayd(index
);
893 if (relayd
== NULL
) {
894 DBG("Unable to find relayd %" PRIu64
, index
);
895 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
899 * Each relayd socket pair has a refcount of stream attached to it
900 * which tells if the relayd is still active or not depending on the
903 * This will set the destroy flag of the relayd object and destroy it
904 * if the refcount reaches zero when called.
906 * The destroy can happen either here or when a stream fd hangs up.
909 consumer_flag_relayd_for_destroy(relayd
);
912 health_code_update();
914 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
915 if (ret_send_status
< 0) {
916 /* Somehow, the session daemon is not responding anymore. */
922 case LTTNG_CONSUMER_DATA_PENDING
:
924 int32_t ret_data_pending
;
925 uint64_t id
= msg
.u
.data_pending
.session_id
;
928 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
930 ret_data_pending
= consumer_data_pending(id
);
932 health_code_update();
934 /* Send back returned value to session daemon */
935 ret_send
= lttcomm_send_unix_sock(sock
, &ret_data_pending
,
936 sizeof(ret_data_pending
));
938 PERROR("send data pending ret code");
943 * No need to send back a status message since the data pending
944 * returned value is the response.
948 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
950 struct lttng_consumer_channel
*channel
;
951 uint64_t key
= msg
.u
.snapshot_channel
.key
;
954 channel
= consumer_find_channel(key
);
956 ERR("Channel %" PRIu64
" not found", key
);
957 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
959 if (msg
.u
.snapshot_channel
.metadata
== 1) {
962 ret_snapshot
= lttng_kconsumer_snapshot_metadata(
964 msg
.u
.snapshot_channel
.pathname
,
965 msg
.u
.snapshot_channel
.relayd_id
,
967 if (ret_snapshot
< 0) {
968 ERR("Snapshot metadata failed");
969 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
974 ret_snapshot
= lttng_kconsumer_snapshot_channel(
976 msg
.u
.snapshot_channel
.pathname
,
977 msg
.u
.snapshot_channel
.relayd_id
,
978 msg
.u
.snapshot_channel
979 .nb_packets_per_stream
);
980 if (ret_snapshot
< 0) {
981 ERR("Snapshot channel failed");
982 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
986 health_code_update();
988 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
989 if (ret_send_status
< 0) {
990 /* Somehow, the session daemon is not responding anymore. */
995 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
997 uint64_t key
= msg
.u
.destroy_channel
.key
;
998 struct lttng_consumer_channel
*channel
;
1001 channel
= consumer_find_channel(key
);
1003 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1004 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1007 health_code_update();
1009 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1010 if (ret_send_status
< 0) {
1011 /* Somehow, the session daemon is not responding anymore. */
1012 goto end_destroy_channel
;
1015 health_code_update();
1017 /* Stop right now if no channel was found. */
1019 goto end_destroy_channel
;
1023 * This command should ONLY be issued for channel with streams set in
1026 LTTNG_ASSERT(!channel
->monitor
);
1029 * The refcount should ALWAYS be 0 in the case of a channel in no
1032 LTTNG_ASSERT(!uatomic_sub_return(&channel
->refcount
, 1));
1034 consumer_del_channel(channel
);
1035 end_destroy_channel
:
1038 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1042 struct lttng_consumer_channel
*channel
;
1043 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1044 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1046 DBG("Kernel consumer discarded events command for session id %"
1047 PRIu64
", channel key %" PRIu64
, id
, key
);
1049 channel
= consumer_find_channel(key
);
1051 ERR("Kernel consumer discarded events channel %"
1052 PRIu64
" not found", key
);
1055 count
= channel
->discarded_events
;
1058 health_code_update();
1060 /* Send back returned value to session daemon */
1061 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1063 PERROR("send discarded events");
1069 case LTTNG_CONSUMER_LOST_PACKETS
:
1073 struct lttng_consumer_channel
*channel
;
1074 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1075 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1077 DBG("Kernel consumer lost packets command for session id %"
1078 PRIu64
", channel key %" PRIu64
, id
, key
);
1080 channel
= consumer_find_channel(key
);
1082 ERR("Kernel consumer lost packets channel %"
1083 PRIu64
" not found", key
);
1086 count
= channel
->lost_packets
;
1089 health_code_update();
1091 /* Send back returned value to session daemon */
1092 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1094 PERROR("send lost packets");
1100 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1102 int channel_monitor_pipe
;
1103 int ret_send_status
, ret_set_channel_monitor_pipe
;
1106 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1107 /* Successfully received the command's type. */
1108 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1109 if (ret_send_status
< 0) {
1113 ret_recv
= lttcomm_recv_fds_unix_sock(
1114 sock
, &channel_monitor_pipe
, 1);
1115 if (ret_recv
!= sizeof(channel_monitor_pipe
)) {
1116 ERR("Failed to receive channel monitor pipe");
1120 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1121 ret_set_channel_monitor_pipe
=
1122 consumer_timer_thread_set_channel_monitor_pipe(
1123 channel_monitor_pipe
);
1124 if (!ret_set_channel_monitor_pipe
) {
1128 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1129 /* Set the pipe as non-blocking. */
1130 ret_fcntl
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1131 if (ret_fcntl
== -1) {
1132 PERROR("fcntl get flags of the channel monitoring pipe");
1137 ret_fcntl
= fcntl(channel_monitor_pipe
, F_SETFL
,
1138 flags
| O_NONBLOCK
);
1139 if (ret_fcntl
== -1) {
1140 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1143 DBG("Channel monitor pipe set as non-blocking");
1145 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1147 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1148 if (ret_send_status
< 0) {
1153 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1155 struct lttng_consumer_channel
*channel
;
1156 uint64_t key
= msg
.u
.rotate_channel
.key
;
1157 int ret_send_status
;
1159 DBG("Consumer rotate channel %" PRIu64
, key
);
1161 channel
= consumer_find_channel(key
);
1163 ERR("Channel %" PRIu64
" not found", key
);
1164 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1167 * Sample the rotate position of all the streams in this channel.
1169 int ret_rotate_channel
;
1171 ret_rotate_channel
= lttng_consumer_rotate_channel(
1173 msg
.u
.rotate_channel
.relayd_id
);
1174 if (ret_rotate_channel
< 0) {
1175 ERR("Rotate channel failed");
1176 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1179 health_code_update();
1182 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1183 if (ret_send_status
< 0) {
1184 /* Somehow, the session daemon is not responding anymore. */
1185 goto error_rotate_channel
;
1188 /* Rotate the streams that are ready right now. */
1191 ret_rotate
= lttng_consumer_rotate_ready_streams(
1193 if (ret_rotate
< 0) {
1194 ERR("Rotate ready streams failed");
1198 error_rotate_channel
:
1201 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1203 struct lttng_consumer_channel
*channel
;
1204 uint64_t key
= msg
.u
.clear_channel
.key
;
1205 int ret_send_status
;
1207 channel
= consumer_find_channel(key
);
1209 DBG("Channel %" PRIu64
" not found", key
);
1210 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1212 int ret_clear_channel
;
1215 lttng_consumer_clear_channel(channel
);
1216 if (ret_clear_channel
) {
1217 ERR("Clear channel failed");
1218 ret_code
= (lttcomm_return_code
) ret_clear_channel
;
1221 health_code_update();
1224 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1225 if (ret_send_status
< 0) {
1226 /* Somehow, the session daemon is not responding anymore. */
1232 case LTTNG_CONSUMER_INIT
:
1234 int ret_send_status
;
1235 lttng_uuid sessiond_uuid
;
1237 std::copy(std::begin(msg
.u
.init
.sessiond_uuid
), std::end(msg
.u
.init
.sessiond_uuid
),
1238 sessiond_uuid
.begin());
1240 ret_code
= lttng_consumer_init_command(ctx
,
1242 health_code_update();
1243 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1244 if (ret_send_status
< 0) {
1245 /* Somehow, the session daemon is not responding anymore. */
1250 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1252 const struct lttng_credentials credentials
= {
1253 .uid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.uid
),
1254 .gid
= LTTNG_OPTIONAL_INIT_VALUE(msg
.u
.create_trace_chunk
.credentials
.value
.gid
),
1256 const bool is_local_trace
=
1257 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1258 const uint64_t relayd_id
=
1259 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1260 const char *chunk_override_name
=
1261 *msg
.u
.create_trace_chunk
.override_name
?
1262 msg
.u
.create_trace_chunk
.override_name
:
1264 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1267 * The session daemon will only provide a chunk directory file
1268 * descriptor for local traces.
1270 if (is_local_trace
) {
1272 int ret_send_status
;
1275 /* Acnowledge the reception of the command. */
1276 ret_send_status
= consumer_send_status_msg(
1277 sock
, LTTCOMM_CONSUMERD_SUCCESS
);
1278 if (ret_send_status
< 0) {
1279 /* Somehow, the session daemon is not responding anymore. */
1283 ret_recv
= lttcomm_recv_fds_unix_sock(
1284 sock
, &chunk_dirfd
, 1);
1285 if (ret_recv
!= sizeof(chunk_dirfd
)) {
1286 ERR("Failed to receive trace chunk directory file descriptor");
1290 DBG("Received trace chunk directory fd (%d)",
1292 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1294 if (!chunk_directory_handle
) {
1295 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1296 if (close(chunk_dirfd
)) {
1297 PERROR("Failed to close chunk directory file descriptor");
1303 ret_code
= lttng_consumer_create_trace_chunk(
1304 !is_local_trace
? &relayd_id
: NULL
,
1305 msg
.u
.create_trace_chunk
.session_id
,
1306 msg
.u
.create_trace_chunk
.chunk_id
,
1307 (time_t) msg
.u
.create_trace_chunk
1308 .creation_timestamp
,
1309 chunk_override_name
,
1310 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1313 chunk_directory_handle
);
1314 lttng_directory_handle_put(chunk_directory_handle
);
1315 goto end_msg_sessiond
;
1317 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1319 enum lttng_trace_chunk_command_type close_command
=
1320 (lttng_trace_chunk_command_type
) msg
.u
.close_trace_chunk
.close_command
.value
;
1321 const uint64_t relayd_id
=
1322 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1323 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1324 char path
[LTTNG_PATH_MAX
];
1327 ret_code
= lttng_consumer_close_trace_chunk(
1328 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1331 msg
.u
.close_trace_chunk
.session_id
,
1332 msg
.u
.close_trace_chunk
.chunk_id
,
1333 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1334 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1337 reply
.ret_code
= ret_code
;
1338 reply
.path_length
= strlen(path
) + 1;
1339 ret_send
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1340 if (ret_send
!= sizeof(reply
)) {
1343 ret_send
= lttcomm_send_unix_sock(
1344 sock
, path
, reply
.path_length
);
1345 if (ret_send
!= reply
.path_length
) {
1350 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1352 const uint64_t relayd_id
=
1353 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1355 ret_code
= lttng_consumer_trace_chunk_exists(
1356 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1358 msg
.u
.trace_chunk_exists
.session_id
,
1359 msg
.u
.trace_chunk_exists
.chunk_id
);
1360 goto end_msg_sessiond
;
1362 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS
:
1364 const uint64_t key
= msg
.u
.open_channel_packets
.key
;
1365 struct lttng_consumer_channel
*channel
=
1366 consumer_find_channel(key
);
1369 pthread_mutex_lock(&channel
->lock
);
1370 ret_code
= lttng_consumer_open_channel_packets(channel
);
1371 pthread_mutex_unlock(&channel
->lock
);
1373 WARN("Channel %" PRIu64
" not found", key
);
1374 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1377 health_code_update();
1378 goto end_msg_sessiond
;
1386 * Return 1 to indicate success since the 0 value can be a socket
1387 * shutdown during the recv() or send() call.
1392 /* This will issue a consumer stop. */
1397 * The returned value here is not useful since either way we'll return 1 to
1398 * the caller because the session daemon socket management is done
1399 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1402 int ret_send_status
;
1404 ret_send_status
= consumer_send_status_msg(sock
, ret_code
);
1405 if (ret_send_status
< 0) {
1413 health_code_update();
1419 * Sync metadata meaning request them to the session daemon and snapshot to the
1420 * metadata thread can consumer them.
1422 * Metadata stream lock MUST be acquired.
1424 enum sync_metadata_status
lttng_kconsumer_sync_metadata(
1425 struct lttng_consumer_stream
*metadata
)
1428 enum sync_metadata_status status
;
1430 LTTNG_ASSERT(metadata
);
1432 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1434 ERR("Failed to flush kernel stream");
1435 status
= SYNC_METADATA_STATUS_ERROR
;
1439 ret
= kernctl_snapshot(metadata
->wait_fd
);
1441 if (errno
== EAGAIN
) {
1442 /* No new metadata, exit. */
1443 DBG("Sync metadata, no new kernel metadata");
1444 status
= SYNC_METADATA_STATUS_NO_DATA
;
1446 ERR("Sync metadata, taking kernel snapshot failed.");
1447 status
= SYNC_METADATA_STATUS_ERROR
;
1450 status
= SYNC_METADATA_STATUS_NEW_DATA
;
1458 int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
1459 struct stream_subbuffer
*subbuf
)
1463 ret
= kernctl_get_subbuf_size(
1464 stream
->wait_fd
, &subbuf
->info
.data
.subbuf_size
);
1469 ret
= kernctl_get_padded_subbuf_size(
1470 stream
->wait_fd
, &subbuf
->info
.data
.padded_subbuf_size
);
1480 int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
1481 struct stream_subbuffer
*subbuf
)
1485 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1490 ret
= kernctl_get_metadata_version(
1491 stream
->wait_fd
, &subbuf
->info
.metadata
.version
);
1501 int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
1502 struct stream_subbuffer
*subbuf
)
1506 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1511 ret
= kernctl_get_packet_size(
1512 stream
->wait_fd
, &subbuf
->info
.data
.packet_size
);
1514 PERROR("Failed to get sub-buffer packet size");
1518 ret
= kernctl_get_content_size(
1519 stream
->wait_fd
, &subbuf
->info
.data
.content_size
);
1521 PERROR("Failed to get sub-buffer content size");
1525 ret
= kernctl_get_timestamp_begin(
1526 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_begin
);
1528 PERROR("Failed to get sub-buffer begin timestamp");
1532 ret
= kernctl_get_timestamp_end(
1533 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_end
);
1535 PERROR("Failed to get sub-buffer end timestamp");
1539 ret
= kernctl_get_events_discarded(
1540 stream
->wait_fd
, &subbuf
->info
.data
.events_discarded
);
1542 PERROR("Failed to get sub-buffer events discarded count");
1546 ret
= kernctl_get_sequence_number(stream
->wait_fd
,
1547 &subbuf
->info
.data
.sequence_number
.value
);
1549 /* May not be supported by older LTTng-modules. */
1550 if (ret
!= -ENOTTY
) {
1551 PERROR("Failed to get sub-buffer sequence number");
1555 subbuf
->info
.data
.sequence_number
.is_set
= true;
1558 ret
= kernctl_get_stream_id(
1559 stream
->wait_fd
, &subbuf
->info
.data
.stream_id
);
1561 PERROR("Failed to get stream id");
1565 ret
= kernctl_get_instance_id(stream
->wait_fd
,
1566 &subbuf
->info
.data
.stream_instance_id
.value
);
1568 /* May not be supported by older LTTng-modules. */
1569 if (ret
!= -ENOTTY
) {
1570 PERROR("Failed to get stream instance id");
1574 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
1581 enum get_next_subbuffer_status
get_subbuffer_common(
1582 struct lttng_consumer_stream
*stream
,
1583 struct stream_subbuffer
*subbuffer
)
1586 enum get_next_subbuffer_status status
;
1588 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1591 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1596 * The caller only expects -ENODATA when there is no data to
1597 * read, but the kernel tracer returns -EAGAIN when there is
1598 * currently no data for a non-finalized stream, and -ENODATA
1599 * when there is no data for a finalized stream. Those can be
1600 * combined into a -ENODATA return value.
1602 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1605 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1609 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1612 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1619 enum get_next_subbuffer_status
get_next_subbuffer_splice(
1620 struct lttng_consumer_stream
*stream
,
1621 struct stream_subbuffer
*subbuffer
)
1623 const enum get_next_subbuffer_status status
=
1624 get_subbuffer_common(stream
, subbuffer
);
1626 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1630 subbuffer
->buffer
.fd
= stream
->wait_fd
;
1636 enum get_next_subbuffer_status
get_next_subbuffer_mmap(
1637 struct lttng_consumer_stream
*stream
,
1638 struct stream_subbuffer
*subbuffer
)
1641 enum get_next_subbuffer_status status
;
1644 status
= get_subbuffer_common(stream
, subbuffer
);
1645 if (status
!= GET_NEXT_SUBBUFFER_STATUS_OK
) {
1649 ret
= get_current_subbuf_addr(stream
, &addr
);
1651 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1655 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1656 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1662 enum get_next_subbuffer_status
get_next_subbuffer_metadata_check(struct lttng_consumer_stream
*stream
,
1663 struct stream_subbuffer
*subbuffer
)
1668 enum get_next_subbuffer_status status
;
1670 ret
= kernctl_get_next_subbuf_metadata_check(stream
->wait_fd
,
1676 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1682 LTTNG_OPTIONAL_SET(&subbuffer
->info
.metadata
.coherent
, coherent
);
1684 ret
= get_current_subbuf_addr(stream
, &addr
);
1689 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1690 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1691 DBG("Got metadata packet with padded_subbuf_size = %lu, coherent = %s",
1692 subbuffer
->info
.metadata
.padded_subbuf_size
,
1693 coherent
? "true" : "false");
1696 * The caller only expects -ENODATA when there is no data to read, but
1697 * the kernel tracer returns -EAGAIN when there is currently no data
1698 * for a non-finalized stream, and -ENODATA when there is no data for a
1699 * finalized stream. Those can be combined into a -ENODATA return value.
1703 status
= GET_NEXT_SUBBUFFER_STATUS_OK
;
1708 * The caller only expects -ENODATA when there is no data to
1709 * read, but the kernel tracer returns -EAGAIN when there is
1710 * currently no data for a non-finalized stream, and -ENODATA
1711 * when there is no data for a finalized stream. Those can be
1712 * combined into a -ENODATA return value.
1714 status
= GET_NEXT_SUBBUFFER_STATUS_NO_DATA
;
1717 status
= GET_NEXT_SUBBUFFER_STATUS_ERROR
;
1725 int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
1726 struct stream_subbuffer
*subbuffer
__attribute__((unused
)))
1728 const int ret
= kernctl_put_next_subbuf(stream
->wait_fd
);
1731 if (ret
== -EFAULT
) {
1732 PERROR("Error in unreserving sub buffer");
1733 } else if (ret
== -EIO
) {
1734 /* Should never happen with newer LTTng versions */
1735 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1743 bool is_get_next_check_metadata_available(int tracer_fd
)
1745 const int ret
= kernctl_get_next_subbuf_metadata_check(tracer_fd
, NULL
);
1746 const bool available
= ret
!= -ENOTTY
;
1749 /* get succeeded, make sure to put the subbuffer. */
1750 kernctl_put_subbuf(tracer_fd
);
1757 int signal_metadata(struct lttng_consumer_stream
*stream
,
1758 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
1760 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
1761 return pthread_cond_broadcast(&stream
->metadata_rdv
) ? -errno
: 0;
1765 int lttng_kconsumer_set_stream_ops(
1766 struct lttng_consumer_stream
*stream
)
1770 if (stream
->metadata_flag
&& stream
->chan
->is_live
) {
1771 DBG("Attempting to enable metadata bucketization for live consumers");
1772 if (is_get_next_check_metadata_available(stream
->wait_fd
)) {
1773 DBG("Kernel tracer supports get_next_subbuffer_metadata_check, metadata will be accumulated until a coherent state is reached");
1774 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1775 get_next_subbuffer_metadata_check
;
1776 ret
= consumer_stream_enable_metadata_bucketization(
1783 * The kernel tracer version is too old to indicate
1784 * when the metadata stream has reached a "coherent"
1785 * (parseable) point.
1787 * This means that a live viewer may see an incoherent
1788 * sequence of metadata and fail to parse it.
1790 WARN("Kernel tracer does not support get_next_subbuffer_metadata_check which may cause live clients to fail to parse the metadata stream");
1791 metadata_bucket_destroy(stream
->metadata_bucket
);
1792 stream
->metadata_bucket
= NULL
;
1795 stream
->read_subbuffer_ops
.on_sleep
= signal_metadata
;
1798 if (!stream
->read_subbuffer_ops
.get_next_subbuffer
) {
1799 if (stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
) {
1800 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1801 get_next_subbuffer_mmap
;
1803 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1804 get_next_subbuffer_splice
;
1808 if (stream
->metadata_flag
) {
1809 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1810 extract_metadata_subbuffer_info
;
1812 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1813 extract_data_subbuffer_info
;
1814 if (stream
->chan
->is_live
) {
1815 stream
->read_subbuffer_ops
.send_live_beacon
=
1816 consumer_flush_kernel_index
;
1820 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
1825 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1829 LTTNG_ASSERT(stream
);
1832 * Don't create anything if this is set for streaming or if there is
1833 * no current trace chunk on the parent channel.
1835 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1836 stream
->chan
->trace_chunk
) {
1837 ret
= consumer_stream_create_output_files(stream
, true);
1843 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1844 /* get the len of the mmap region */
1845 unsigned long mmap_len
;
1847 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1849 PERROR("kernctl_get_mmap_len");
1850 goto error_close_fd
;
1852 stream
->mmap_len
= (size_t) mmap_len
;
1854 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1855 MAP_PRIVATE
, stream
->wait_fd
, 0);
1856 if (stream
->mmap_base
== MAP_FAILED
) {
1857 PERROR("Error mmaping");
1859 goto error_close_fd
;
1863 ret
= lttng_kconsumer_set_stream_ops(stream
);
1865 goto error_close_fd
;
1868 /* we return 0 to let the library handle the FD internally */
1872 if (stream
->out_fd
>= 0) {
1875 err
= close(stream
->out_fd
);
1877 stream
->out_fd
= -1;
1884 * Check if data is still being extracted from the buffers for a specific
1885 * stream. Consumer data lock MUST be acquired before calling this function
1886 * and the stream lock.
1888 * Return 1 if the traced data are still getting read else 0 meaning that the
1889 * data is available for trace viewer reading.
1891 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1895 LTTNG_ASSERT(stream
);
1897 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1902 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1904 /* There is still data so let's put back this subbuffer. */
1905 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1906 LTTNG_ASSERT(ret
== 0);
1907 ret
= 1; /* Data is pending */
1911 /* Data is NOT pending and ready to be read. */