2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "common/buffer-view.h"
29 #include <sys/socket.h>
30 #include <sys/types.h>
35 #include <bin/lttng-consumerd/health-consumerd.h>
36 #include <common/common.h>
37 #include <common/kernel-ctl/kernel-ctl.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/sessiond-comm/relayd.h>
40 #include <common/compat/fcntl.h>
41 #include <common/compat/endian.h>
42 #include <common/pipe.h>
43 #include <common/relayd/relayd.h>
44 #include <common/utils.h>
45 #include <common/consumer/consumer-stream.h>
46 #include <common/index/index.h>
47 #include <common/consumer/consumer-timer.h>
48 #include <common/optional.h>
50 #include "kernel-consumer.h"
52 extern struct lttng_consumer_global_data consumer_data
;
53 extern int consumer_poll_timeout
;
56 * Take a snapshot for a specific fd
58 * Returns 0 on success, < 0 on error
60 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
63 int infd
= stream
->wait_fd
;
65 ret
= kernctl_snapshot(infd
);
67 * -EAGAIN is not an error, it just means that there is no data to
70 if (ret
!= 0 && ret
!= -EAGAIN
) {
71 PERROR("Getting sub-buffer snapshot.");
78 * Sample consumed and produced positions for a specific fd.
80 * Returns 0 on success, < 0 on error.
82 int lttng_kconsumer_sample_snapshot_positions(
83 struct lttng_consumer_stream
*stream
)
87 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
91 * Get the produced position
93 * Returns 0 on success, < 0 on error
95 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
99 int infd
= stream
->wait_fd
;
101 ret
= kernctl_snapshot_get_produced(infd
, pos
);
103 PERROR("kernctl_snapshot_get_produced");
110 * Get the consumerd position
112 * Returns 0 on success, < 0 on error
114 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
118 int infd
= stream
->wait_fd
;
120 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
122 PERROR("kernctl_snapshot_get_consumed");
129 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
133 unsigned long mmap_offset
;
134 const char *mmap_base
= stream
->mmap_base
;
136 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
138 PERROR("Failed to get mmap read offset");
142 *addr
= mmap_base
+ mmap_offset
;
148 * Take a snapshot of all the stream of a channel
149 * RCU read-side lock must be held across this function to ensure existence of
150 * channel. The channel lock must be held by the caller.
152 * Returns 0 on success, < 0 on error
154 static int lttng_kconsumer_snapshot_channel(
155 struct lttng_consumer_channel
*channel
,
156 uint64_t key
, char *path
, uint64_t relayd_id
,
157 uint64_t nb_packets_per_stream
,
158 struct lttng_consumer_local_data
*ctx
)
161 struct lttng_consumer_stream
*stream
;
163 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
167 /* Splice is not supported yet for channel snapshot. */
168 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
169 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
175 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
176 unsigned long consumed_pos
, produced_pos
;
178 health_code_update();
181 * Lock stream because we are about to change its state.
183 pthread_mutex_lock(&stream
->lock
);
185 assert(channel
->trace_chunk
);
186 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
188 * Can't happen barring an internal error as the channel
189 * holds a reference to the trace chunk.
191 ERR("Failed to acquire reference to channel's trace chunk");
195 assert(!stream
->trace_chunk
);
196 stream
->trace_chunk
= channel
->trace_chunk
;
199 * Assign the received relayd ID so we can use it for streaming. The streams
200 * are not visible to anyone so this is OK to change it.
202 stream
->net_seq_idx
= relayd_id
;
203 channel
->relayd_id
= relayd_id
;
204 if (relayd_id
!= (uint64_t) -1ULL) {
205 ret
= consumer_send_relayd_stream(stream
, path
);
207 ERR("sending stream to relayd");
211 ret
= consumer_stream_create_output_files(stream
,
216 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
220 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
223 * Doing a buffer flush which does not take into
224 * account empty packets. This is not perfect
225 * for stream intersection, but required as a
226 * fall-back when "flush_empty" is not
227 * implemented by lttng-modules.
229 ret
= kernctl_buffer_flush(stream
->wait_fd
);
231 ERR("Failed to flush kernel stream");
237 ret
= lttng_kconsumer_take_snapshot(stream
);
239 ERR("Taking kernel snapshot");
243 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
245 ERR("Produced kernel snapshot position");
249 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
251 ERR("Consumerd kernel snapshot position");
255 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
256 produced_pos
, nb_packets_per_stream
,
257 stream
->max_sb_size
);
259 while ((long) (consumed_pos
- produced_pos
) < 0) {
261 unsigned long len
, padded_len
;
262 const char *subbuf_addr
;
263 struct lttng_buffer_view subbuf_view
;
265 health_code_update();
266 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
268 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
270 if (ret
!= -EAGAIN
) {
271 PERROR("kernctl_get_subbuf snapshot");
274 DBG("Kernel consumer get subbuf failed. Skipping it.");
275 consumed_pos
+= stream
->max_sb_size
;
276 stream
->chan
->lost_packets
++;
280 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
282 ERR("Snapshot kernctl_get_subbuf_size");
283 goto error_put_subbuf
;
286 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
288 ERR("Snapshot kernctl_get_padded_subbuf_size");
289 goto error_put_subbuf
;
292 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
294 goto error_put_subbuf
;
297 subbuf_view
= lttng_buffer_view_init(
298 subbuf_addr
, 0, padded_len
);
299 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
300 stream
, &subbuf_view
,
301 padded_len
- len
, NULL
);
303 * We write the padded len in local tracefiles but the data len
304 * when using a relay. Display the error but continue processing
305 * to try to release the subbuffer.
307 if (relayd_id
!= (uint64_t) -1ULL) {
308 if (read_len
!= len
) {
309 ERR("Error sending to the relay (ret: %zd != len: %lu)",
313 if (read_len
!= padded_len
) {
314 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
315 read_len
, padded_len
);
319 ret
= kernctl_put_subbuf(stream
->wait_fd
);
321 ERR("Snapshot kernctl_put_subbuf");
324 consumed_pos
+= stream
->max_sb_size
;
327 if (relayd_id
== (uint64_t) -1ULL) {
328 if (stream
->out_fd
>= 0) {
329 ret
= close(stream
->out_fd
);
331 PERROR("Kernel consumer snapshot close out_fd");
337 close_relayd_stream(stream
);
338 stream
->net_seq_idx
= (uint64_t) -1ULL;
340 lttng_trace_chunk_put(stream
->trace_chunk
);
341 stream
->trace_chunk
= NULL
;
342 pthread_mutex_unlock(&stream
->lock
);
350 ret
= kernctl_put_subbuf(stream
->wait_fd
);
352 ERR("Snapshot kernctl_put_subbuf error path");
355 pthread_mutex_unlock(&stream
->lock
);
362 * Read the whole metadata available for a snapshot.
363 * RCU read-side lock must be held across this function to ensure existence of
364 * metadata_channel. The channel lock must be held by the caller.
366 * Returns 0 on success, < 0 on error
368 static int lttng_kconsumer_snapshot_metadata(
369 struct lttng_consumer_channel
*metadata_channel
,
370 uint64_t key
, char *path
, uint64_t relayd_id
,
371 struct lttng_consumer_local_data
*ctx
)
373 int ret
, use_relayd
= 0;
375 struct lttng_consumer_stream
*metadata_stream
;
379 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
384 metadata_stream
= metadata_channel
->metadata_stream
;
385 assert(metadata_stream
);
387 pthread_mutex_lock(&metadata_stream
->lock
);
388 assert(metadata_channel
->trace_chunk
);
389 assert(metadata_stream
->trace_chunk
);
391 /* Flag once that we have a valid relayd for the stream. */
392 if (relayd_id
!= (uint64_t) -1ULL) {
397 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
402 ret
= consumer_stream_create_output_files(metadata_stream
,
410 health_code_update();
412 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
414 if (ret_read
!= -EAGAIN
) {
415 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
420 /* ret_read is negative at this point so we will exit the loop. */
423 } while (ret_read
>= 0);
426 close_relayd_stream(metadata_stream
);
427 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
429 if (metadata_stream
->out_fd
>= 0) {
430 ret
= close(metadata_stream
->out_fd
);
432 PERROR("Kernel consumer snapshot metadata close out_fd");
434 * Don't go on error here since the snapshot was successful at this
435 * point but somehow the close failed.
438 metadata_stream
->out_fd
= -1;
439 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
440 metadata_stream
->trace_chunk
= NULL
;
446 pthread_mutex_unlock(&metadata_stream
->lock
);
447 cds_list_del(&metadata_stream
->send_node
);
448 consumer_stream_destroy(metadata_stream
, NULL
);
449 metadata_channel
->metadata_stream
= NULL
;
455 * Receive command from session daemon and process it.
457 * Return 1 on success else a negative value or 0.
459 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
460 int sock
, struct pollfd
*consumer_sockpoll
)
463 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
464 struct lttcomm_consumer_msg msg
;
466 health_code_update();
468 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
469 if (ret
!= sizeof(msg
)) {
471 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
477 health_code_update();
479 /* Deprecated command */
480 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
482 health_code_update();
484 /* relayd needs RCU read-side protection */
487 switch (msg
.cmd_type
) {
488 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
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
, consumer_sockpoll
,
493 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
494 msg
.u
.relayd_sock
.relayd_session_id
);
497 case LTTNG_CONSUMER_ADD_CHANNEL
:
499 struct lttng_consumer_channel
*new_channel
;
501 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
503 health_code_update();
505 /* First send a status message before receiving the fds. */
506 ret
= consumer_send_status_msg(sock
, ret_code
);
508 /* Somehow, the session daemon is not responding anymore. */
512 health_code_update();
514 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
515 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
516 msg
.u
.channel
.session_id
,
517 msg
.u
.channel
.chunk_id
.is_set
?
519 msg
.u
.channel
.pathname
,
521 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
522 msg
.u
.channel
.tracefile_size
,
523 msg
.u
.channel
.tracefile_count
, 0,
524 msg
.u
.channel
.monitor
,
525 msg
.u
.channel
.live_timer_interval
,
526 msg
.u
.channel
.is_live
,
528 if (new_channel
== NULL
) {
529 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
532 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
533 switch (msg
.u
.channel
.output
) {
534 case LTTNG_EVENT_SPLICE
:
535 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
537 case LTTNG_EVENT_MMAP
:
538 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
541 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
545 /* Translate and save channel type. */
546 switch (msg
.u
.channel
.type
) {
547 case CONSUMER_CHANNEL_TYPE_DATA
:
548 case CONSUMER_CHANNEL_TYPE_METADATA
:
549 new_channel
->type
= msg
.u
.channel
.type
;
556 health_code_update();
558 if (ctx
->on_recv_channel
!= NULL
) {
559 ret_recv
= ctx
->on_recv_channel(new_channel
);
561 ret
= consumer_add_channel(new_channel
, ctx
);
562 } else if (ret_recv
< 0) {
566 ret
= consumer_add_channel(new_channel
, ctx
);
568 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
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");
584 health_code_update();
586 /* If we received an error in add_channel, we need to report it. */
588 ret
= consumer_send_status_msg(sock
, ret
);
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
;
606 * Get stream's channel reference. Needed when adding the stream to the
609 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
612 * We could not find the channel. Can happen if cpu hotplug
613 * happens while tearing down.
615 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
616 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
619 health_code_update();
621 /* First send a status message before receiving the fds. */
622 ret
= consumer_send_status_msg(sock
, ret_code
);
624 /* Somehow, the session daemon is not responding anymore. */
625 goto error_add_stream_fatal
;
628 health_code_update();
630 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
631 /* Channel was not found. */
632 goto error_add_stream_nosignal
;
637 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
640 goto error_add_stream_fatal
;
643 health_code_update();
645 /* Get stream file descriptor from socket */
646 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
647 if (ret
!= sizeof(fd
)) {
648 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
652 health_code_update();
655 * Send status code to session daemon only if the recv works. If the
656 * above recv() failed, the session daemon is notified through the
657 * error socket and the teardown is eventually done.
659 ret
= consumer_send_status_msg(sock
, ret_code
);
661 /* Somehow, the session daemon is not responding anymore. */
662 goto error_add_stream_nosignal
;
665 health_code_update();
667 pthread_mutex_lock(&channel
->lock
);
668 new_stream
= consumer_allocate_stream(
675 channel
->trace_chunk
,
680 if (new_stream
== NULL
) {
685 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
688 pthread_mutex_unlock(&channel
->lock
);
689 goto error_add_stream_nosignal
;
692 new_stream
->wait_fd
= fd
;
693 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
694 &new_stream
->max_sb_size
);
696 pthread_mutex_unlock(&channel
->lock
);
697 ERR("Failed to get kernel maximal subbuffer size");
698 goto error_add_stream_nosignal
;
701 consumer_stream_update_channel_attributes(new_stream
,
703 switch (channel
->output
) {
704 case CONSUMER_CHANNEL_SPLICE
:
705 new_stream
->output
= LTTNG_EVENT_SPLICE
;
706 ret
= utils_create_pipe(new_stream
->splice_pipe
);
708 pthread_mutex_unlock(&channel
->lock
);
709 goto error_add_stream_nosignal
;
712 case CONSUMER_CHANNEL_MMAP
:
713 new_stream
->output
= LTTNG_EVENT_MMAP
;
716 ERR("Stream output unknown %d", channel
->output
);
717 pthread_mutex_unlock(&channel
->lock
);
718 goto error_add_stream_nosignal
;
722 * We've just assigned the channel to the stream so increment the
723 * refcount right now. We don't need to increment the refcount for
724 * streams in no monitor because we handle manually the cleanup of
725 * those. It is very important to make sure there is NO prior
726 * consumer_del_stream() calls or else the refcount will be unbalanced.
728 if (channel
->monitor
) {
729 uatomic_inc(&new_stream
->chan
->refcount
);
733 * The buffer flush is done on the session daemon side for the kernel
734 * so no need for the stream "hangup_flush_done" variable to be
735 * tracked. This is important for a kernel stream since we don't rely
736 * on the flush state of the stream to read data. It's not the case for
737 * user space tracing.
739 new_stream
->hangup_flush_done
= 0;
741 health_code_update();
743 pthread_mutex_lock(&new_stream
->lock
);
744 if (ctx
->on_recv_stream
) {
745 ret
= ctx
->on_recv_stream(new_stream
);
747 pthread_mutex_unlock(&new_stream
->lock
);
748 pthread_mutex_unlock(&channel
->lock
);
749 consumer_stream_free(new_stream
);
750 goto error_add_stream_nosignal
;
753 health_code_update();
755 if (new_stream
->metadata_flag
) {
756 channel
->metadata_stream
= new_stream
;
759 /* Do not monitor this stream. */
760 if (!channel
->monitor
) {
761 DBG("Kernel consumer add stream %s in no monitor mode with "
762 "relayd id %" PRIu64
, new_stream
->name
,
763 new_stream
->net_seq_idx
);
764 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
765 pthread_mutex_unlock(&new_stream
->lock
);
766 pthread_mutex_unlock(&channel
->lock
);
770 /* Send stream to relayd if the stream has an ID. */
771 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
772 ret
= consumer_send_relayd_stream(new_stream
,
773 new_stream
->chan
->pathname
);
775 pthread_mutex_unlock(&new_stream
->lock
);
776 pthread_mutex_unlock(&channel
->lock
);
777 consumer_stream_free(new_stream
);
778 goto error_add_stream_nosignal
;
782 * If adding an extra stream to an already
783 * existing channel (e.g. cpu hotplug), we need
784 * to send the "streams_sent" command to relayd.
786 if (channel
->streams_sent_to_relayd
) {
787 ret
= consumer_send_relayd_streams_sent(
788 new_stream
->net_seq_idx
);
790 pthread_mutex_unlock(&new_stream
->lock
);
791 pthread_mutex_unlock(&channel
->lock
);
792 goto error_add_stream_nosignal
;
796 pthread_mutex_unlock(&new_stream
->lock
);
797 pthread_mutex_unlock(&channel
->lock
);
799 /* Get the right pipe where the stream will be sent. */
800 if (new_stream
->metadata_flag
) {
801 consumer_add_metadata_stream(new_stream
);
802 stream_pipe
= ctx
->consumer_metadata_pipe
;
804 consumer_add_data_stream(new_stream
);
805 stream_pipe
= ctx
->consumer_data_pipe
;
808 /* Visible to other threads */
809 new_stream
->globally_visible
= 1;
811 health_code_update();
813 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
815 ERR("Consumer write %s stream to pipe %d",
816 new_stream
->metadata_flag
? "metadata" : "data",
817 lttng_pipe_get_writefd(stream_pipe
));
818 if (new_stream
->metadata_flag
) {
819 consumer_del_stream_for_metadata(new_stream
);
821 consumer_del_stream_for_data(new_stream
);
823 goto error_add_stream_nosignal
;
826 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
827 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
830 error_add_stream_nosignal
:
832 error_add_stream_fatal
:
835 case LTTNG_CONSUMER_STREAMS_SENT
:
837 struct lttng_consumer_channel
*channel
;
840 * Get stream's channel reference. Needed when adding the stream to the
843 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
846 * We could not find the channel. Can happen if cpu hotplug
847 * happens while tearing down.
849 ERR("Unable to find channel key %" PRIu64
,
850 msg
.u
.sent_streams
.channel_key
);
851 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
854 health_code_update();
857 * Send status code to session daemon.
859 ret
= consumer_send_status_msg(sock
, ret_code
);
860 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
861 /* Somehow, the session daemon is not responding anymore. */
862 goto error_streams_sent_nosignal
;
865 health_code_update();
868 * We should not send this message if we don't monitor the
869 * streams in this channel.
871 if (!channel
->monitor
) {
872 goto end_error_streams_sent
;
875 health_code_update();
876 /* Send stream to relayd if the stream has an ID. */
877 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
878 ret
= consumer_send_relayd_streams_sent(
879 msg
.u
.sent_streams
.net_seq_idx
);
881 goto error_streams_sent_nosignal
;
883 channel
->streams_sent_to_relayd
= true;
885 end_error_streams_sent
:
887 error_streams_sent_nosignal
:
890 case LTTNG_CONSUMER_UPDATE_STREAM
:
895 case LTTNG_CONSUMER_DESTROY_RELAYD
:
897 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
898 struct consumer_relayd_sock_pair
*relayd
;
900 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
902 /* Get relayd reference if exists. */
903 relayd
= consumer_find_relayd(index
);
904 if (relayd
== NULL
) {
905 DBG("Unable to find relayd %" PRIu64
, index
);
906 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
910 * Each relayd socket pair has a refcount of stream attached to it
911 * which tells if the relayd is still active or not depending on the
914 * This will set the destroy flag of the relayd object and destroy it
915 * if the refcount reaches zero when called.
917 * The destroy can happen either here or when a stream fd hangs up.
920 consumer_flag_relayd_for_destroy(relayd
);
923 health_code_update();
925 ret
= consumer_send_status_msg(sock
, ret_code
);
927 /* Somehow, the session daemon is not responding anymore. */
933 case LTTNG_CONSUMER_DATA_PENDING
:
936 uint64_t id
= msg
.u
.data_pending
.session_id
;
938 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
940 ret
= consumer_data_pending(id
);
942 health_code_update();
944 /* Send back returned value to session daemon */
945 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
947 PERROR("send data pending ret code");
952 * No need to send back a status message since the data pending
953 * returned value is the response.
957 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
959 struct lttng_consumer_channel
*channel
;
960 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 pthread_mutex_lock(&channel
->lock
);
968 if (msg
.u
.snapshot_channel
.metadata
== 1) {
969 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
970 msg
.u
.snapshot_channel
.pathname
,
971 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
973 ERR("Snapshot metadata failed");
974 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
977 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
978 msg
.u
.snapshot_channel
.pathname
,
979 msg
.u
.snapshot_channel
.relayd_id
,
980 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
983 ERR("Snapshot channel failed");
984 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
987 pthread_mutex_unlock(&channel
->lock
);
989 health_code_update();
991 ret
= consumer_send_status_msg(sock
, ret_code
);
993 /* Somehow, the session daemon is not responding anymore. */
998 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1000 uint64_t key
= msg
.u
.destroy_channel
.key
;
1001 struct lttng_consumer_channel
*channel
;
1003 channel
= consumer_find_channel(key
);
1005 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1006 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1009 health_code_update();
1011 ret
= consumer_send_status_msg(sock
, ret_code
);
1013 /* Somehow, the session daemon is not responding anymore. */
1014 goto end_destroy_channel
;
1017 health_code_update();
1019 /* Stop right now if no channel was found. */
1021 goto end_destroy_channel
;
1025 * This command should ONLY be issued for channel with streams set in
1028 assert(!channel
->monitor
);
1031 * The refcount should ALWAYS be 0 in the case of a channel in no
1034 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1036 consumer_del_channel(channel
);
1037 end_destroy_channel
:
1040 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1044 struct lttng_consumer_channel
*channel
;
1045 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1046 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1048 DBG("Kernel consumer discarded events command for session id %"
1049 PRIu64
", channel key %" PRIu64
, id
, key
);
1051 channel
= consumer_find_channel(key
);
1053 ERR("Kernel consumer discarded events channel %"
1054 PRIu64
" not found", key
);
1057 count
= channel
->discarded_events
;
1060 health_code_update();
1062 /* Send back returned value to session daemon */
1063 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1065 PERROR("send discarded events");
1071 case LTTNG_CONSUMER_LOST_PACKETS
:
1075 struct lttng_consumer_channel
*channel
;
1076 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1077 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1079 DBG("Kernel consumer lost packets command for session id %"
1080 PRIu64
", channel key %" PRIu64
, id
, key
);
1082 channel
= consumer_find_channel(key
);
1084 ERR("Kernel consumer lost packets channel %"
1085 PRIu64
" not found", key
);
1088 count
= channel
->lost_packets
;
1091 health_code_update();
1093 /* Send back returned value to session daemon */
1094 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1096 PERROR("send lost packets");
1102 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1104 int channel_monitor_pipe
;
1106 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1107 /* Successfully received the command's type. */
1108 ret
= consumer_send_status_msg(sock
, ret_code
);
1113 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1115 if (ret
!= sizeof(channel_monitor_pipe
)) {
1116 ERR("Failed to receive channel monitor pipe");
1120 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1121 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1122 channel_monitor_pipe
);
1126 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1127 /* Set the pipe as non-blocking. */
1128 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1130 PERROR("fcntl get flags of the channel monitoring pipe");
1135 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1136 flags
| O_NONBLOCK
);
1138 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1141 DBG("Channel monitor pipe set as non-blocking");
1143 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1145 ret
= consumer_send_status_msg(sock
, ret_code
);
1151 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1153 struct lttng_consumer_channel
*channel
;
1154 uint64_t key
= msg
.u
.rotate_channel
.key
;
1156 DBG("Consumer rotate channel %" PRIu64
, key
);
1158 channel
= consumer_find_channel(key
);
1160 ERR("Channel %" PRIu64
" not found", key
);
1161 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1164 * Sample the rotate position of all the streams in this channel.
1166 ret
= lttng_consumer_rotate_channel(channel
, key
,
1167 msg
.u
.rotate_channel
.relayd_id
,
1168 msg
.u
.rotate_channel
.metadata
,
1171 ERR("Rotate channel failed");
1172 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1175 health_code_update();
1177 ret
= consumer_send_status_msg(sock
, ret_code
);
1179 /* Somehow, the session daemon is not responding anymore. */
1180 goto error_rotate_channel
;
1183 /* Rotate the streams that are ready right now. */
1184 ret
= lttng_consumer_rotate_ready_streams(
1187 ERR("Rotate ready streams failed");
1191 error_rotate_channel
:
1194 case LTTNG_CONSUMER_INIT
:
1196 ret_code
= lttng_consumer_init_command(ctx
,
1197 msg
.u
.init
.sessiond_uuid
);
1198 health_code_update();
1199 ret
= consumer_send_status_msg(sock
, ret_code
);
1201 /* Somehow, the session daemon is not responding anymore. */
1206 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1208 const struct lttng_credentials credentials
= {
1209 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1210 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1212 const bool is_local_trace
=
1213 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1214 const uint64_t relayd_id
=
1215 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1216 const char *chunk_override_name
=
1217 *msg
.u
.create_trace_chunk
.override_name
?
1218 msg
.u
.create_trace_chunk
.override_name
:
1220 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
1221 LTTNG_OPTIONAL_INIT
;
1224 * The session daemon will only provide a chunk directory file
1225 * descriptor for local traces.
1227 if (is_local_trace
) {
1230 /* Acnowledge the reception of the command. */
1231 ret
= consumer_send_status_msg(sock
,
1232 LTTCOMM_CONSUMERD_SUCCESS
);
1234 /* Somehow, the session daemon is not responding anymore. */
1238 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1239 if (ret
!= sizeof(chunk_dirfd
)) {
1240 ERR("Failed to receive trace chunk directory file descriptor");
1244 DBG("Received trace chunk directory fd (%d)",
1246 ret
= lttng_directory_handle_init_from_dirfd(
1247 &chunk_directory_handle
.value
,
1250 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1251 if (close(chunk_dirfd
)) {
1252 PERROR("Failed to close chunk directory file descriptor");
1256 chunk_directory_handle
.is_set
= true;
1259 ret_code
= lttng_consumer_create_trace_chunk(
1260 !is_local_trace
? &relayd_id
: NULL
,
1261 msg
.u
.create_trace_chunk
.session_id
,
1262 msg
.u
.create_trace_chunk
.chunk_id
,
1263 (time_t) msg
.u
.create_trace_chunk
1264 .creation_timestamp
,
1265 chunk_override_name
,
1266 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1269 chunk_directory_handle
.is_set
?
1270 &chunk_directory_handle
.value
:
1273 if (chunk_directory_handle
.is_set
) {
1274 lttng_directory_handle_fini(
1275 &chunk_directory_handle
.value
);
1277 goto end_msg_sessiond
;
1279 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1281 enum lttng_trace_chunk_command_type close_command
=
1282 msg
.u
.close_trace_chunk
.close_command
.value
;
1283 const uint64_t relayd_id
=
1284 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1285 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1286 char path
[LTTNG_PATH_MAX
];
1288 ret_code
= lttng_consumer_close_trace_chunk(
1289 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1292 msg
.u
.close_trace_chunk
.session_id
,
1293 msg
.u
.close_trace_chunk
.chunk_id
,
1294 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1295 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1298 reply
.ret_code
= ret_code
;
1299 reply
.path_length
= strlen(path
) + 1;
1300 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1301 if (ret
!= sizeof(reply
)) {
1304 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1305 if (ret
!= reply
.path_length
) {
1310 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1312 const uint64_t relayd_id
=
1313 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1315 ret_code
= lttng_consumer_trace_chunk_exists(
1316 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1318 msg
.u
.trace_chunk_exists
.session_id
,
1319 msg
.u
.trace_chunk_exists
.chunk_id
);
1320 goto end_msg_sessiond
;
1328 * Return 1 to indicate success since the 0 value can be a socket
1329 * shutdown during the recv() or send() call.
1334 /* This will issue a consumer stop. */
1339 * The returned value here is not useful since either way we'll return 1 to
1340 * the caller because the session daemon socket management is done
1341 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1343 ret
= consumer_send_status_msg(sock
, ret_code
);
1349 health_code_update();
1355 * Populate index values of a kernel stream. Values are set in big endian order.
1357 * Return 0 on success or else a negative value.
1359 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1362 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1363 events_discarded
, stream_id
, stream_instance_id
,
1366 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1368 PERROR("kernctl_get_timestamp_begin");
1372 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1374 PERROR("kernctl_get_timestamp_end");
1378 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1380 PERROR("kernctl_get_events_discarded");
1384 ret
= kernctl_get_content_size(infd
, &content_size
);
1386 PERROR("kernctl_get_content_size");
1390 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1392 PERROR("kernctl_get_packet_size");
1396 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1398 PERROR("kernctl_get_stream_id");
1402 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1404 if (ret
== -ENOTTY
) {
1405 /* Command not implemented by lttng-modules. */
1406 stream_instance_id
= -1ULL;
1408 PERROR("kernctl_get_instance_id");
1413 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1415 if (ret
== -ENOTTY
) {
1416 /* Command not implemented by lttng-modules. */
1417 packet_seq_num
= -1ULL;
1420 PERROR("kernctl_get_sequence_number");
1424 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1426 *index
= (typeof(*index
)) {
1427 .offset
= index
->offset
,
1428 .packet_size
= htobe64(packet_size
),
1429 .content_size
= htobe64(content_size
),
1430 .timestamp_begin
= htobe64(timestamp_begin
),
1431 .timestamp_end
= htobe64(timestamp_end
),
1432 .events_discarded
= htobe64(events_discarded
),
1433 .stream_id
= htobe64(stream_id
),
1434 .stream_instance_id
= htobe64(stream_instance_id
),
1435 .packet_seq_num
= htobe64(packet_seq_num
),
1442 * Sync metadata meaning request them to the session daemon and snapshot to the
1443 * metadata thread can consumer them.
1445 * Metadata stream lock MUST be acquired.
1447 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1448 * is empty or a negative value on error.
1450 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1456 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1458 ERR("Failed to flush kernel stream");
1462 ret
= kernctl_snapshot(metadata
->wait_fd
);
1464 if (ret
!= -EAGAIN
) {
1465 ERR("Sync metadata, taking kernel snapshot failed.");
1468 DBG("Sync metadata, no new kernel metadata");
1469 /* No new metadata, exit. */
1479 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1482 uint64_t seq
, discarded
;
1484 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1486 if (ret
== -ENOTTY
) {
1487 /* Command not implemented by lttng-modules. */
1489 stream
->sequence_number_unavailable
= true;
1491 PERROR("kernctl_get_sequence_number");
1497 * Start the sequence when we extract the first packet in case we don't
1498 * start at 0 (for example if a consumer is not connected to the
1499 * session immediately after the beginning).
1501 if (stream
->last_sequence_number
== -1ULL) {
1502 stream
->last_sequence_number
= seq
;
1503 } else if (seq
> stream
->last_sequence_number
) {
1504 stream
->chan
->lost_packets
+= seq
-
1505 stream
->last_sequence_number
- 1;
1507 /* seq <= last_sequence_number */
1508 ERR("Sequence number inconsistent : prev = %" PRIu64
1509 ", current = %" PRIu64
,
1510 stream
->last_sequence_number
, seq
);
1514 stream
->last_sequence_number
= seq
;
1516 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1518 PERROR("kernctl_get_events_discarded");
1521 if (discarded
< stream
->last_discarded_events
) {
1523 * Overflow has occurred. We assume only one wrap-around
1526 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1527 stream
->last_discarded_events
+ discarded
;
1529 stream
->chan
->discarded_events
+= discarded
-
1530 stream
->last_discarded_events
;
1532 stream
->last_discarded_events
= discarded
;
1540 * Check if the local version of the metadata stream matches with the version
1541 * of the metadata stream in the kernel. If it was updated, set the reset flag
1545 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1548 uint64_t cur_version
;
1550 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1552 if (ret
== -ENOTTY
) {
1554 * LTTng-modules does not implement this
1560 ERR("Failed to get the metadata version");
1564 if (stream
->metadata_version
== cur_version
) {
1569 DBG("New metadata version detected");
1570 stream
->metadata_version
= cur_version
;
1571 stream
->reset_metadata_flag
= 1;
1579 * Consume data on a file descriptor and write it on a trace file.
1580 * The stream and channel locks must be held by the caller.
1582 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1583 struct lttng_consumer_local_data
*ctx
)
1585 unsigned long len
, subbuf_size
, padding
;
1586 int err
, write_index
= 1;
1588 int infd
= stream
->wait_fd
;
1589 struct ctf_packet_index index
= {};
1590 bool in_error_state
= false;
1592 DBG("In read_subbuffer (infd : %d)", infd
);
1595 /* Get the next subbuffer */
1596 err
= kernctl_get_next_subbuf(infd
);
1599 * This is a debug message even for single-threaded consumer,
1600 * because poll() have more relaxed criterions than get subbuf,
1601 * so get_subbuf may fail for short race windows where poll()
1602 * would issue wakeups.
1604 DBG("Reserving sub buffer failed (everything is normal, "
1605 "it is due to concurrency)");
1610 /* Get the full subbuffer size including padding */
1611 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1613 PERROR("Getting sub-buffer len failed.");
1614 err
= kernctl_put_subbuf(infd
);
1616 if (err
== -EFAULT
) {
1617 PERROR("Error in unreserving sub buffer\n");
1618 } else if (err
== -EIO
) {
1619 /* Should never happen with newer LTTng versions */
1620 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1629 if (!stream
->metadata_flag
) {
1630 ret
= get_index_values(&index
, infd
);
1632 err
= kernctl_put_subbuf(infd
);
1634 if (err
== -EFAULT
) {
1635 PERROR("Error in unreserving sub buffer\n");
1636 } else if (err
== -EIO
) {
1637 /* Should never happen with newer LTTng versions */
1638 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1645 ret
= update_stream_stats(stream
);
1647 err
= kernctl_put_subbuf(infd
);
1649 if (err
== -EFAULT
) {
1650 PERROR("Error in unreserving sub buffer\n");
1651 } else if (err
== -EIO
) {
1652 /* Should never happen with newer LTTng versions */
1653 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1662 ret
= metadata_stream_check_version(infd
, stream
);
1664 err
= kernctl_put_subbuf(infd
);
1666 if (err
== -EFAULT
) {
1667 PERROR("Error in unreserving sub buffer\n");
1668 } else if (err
== -EIO
) {
1669 /* Should never happen with newer LTTng versions */
1670 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1679 switch (stream
->chan
->output
) {
1680 case CONSUMER_CHANNEL_SPLICE
:
1682 * XXX: The lttng-modules splice "actor" does not handle copying
1683 * partial pages hence only using the subbuffer size without the
1684 * padding makes the splice fail.
1689 /* splice the subbuffer to the tracefile */
1690 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1693 * XXX: Splice does not support network streaming so the return value
1694 * is simply checked against subbuf_size and not like the mmap() op.
1696 if (ret
!= subbuf_size
) {
1698 * display the error but continue processing to try
1699 * to release the subbuffer
1701 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1706 case CONSUMER_CHANNEL_MMAP
:
1708 const char *subbuf_addr
;
1709 struct lttng_buffer_view subbuf_view
;
1711 /* Get subbuffer size without padding */
1712 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1714 PERROR("Getting sub-buffer len failed.");
1715 err
= kernctl_put_subbuf(infd
);
1717 if (err
== -EFAULT
) {
1718 PERROR("Error in unreserving sub buffer\n");
1719 } else if (err
== -EIO
) {
1720 /* Should never happen with newer LTTng versions */
1721 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1730 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1732 goto error_put_subbuf
;
1735 /* Make sure the tracer is not gone mad on us! */
1736 assert(len
>= subbuf_size
);
1738 padding
= len
- subbuf_size
;
1740 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
1742 /* write the subbuffer to the tracefile */
1743 ret
= lttng_consumer_on_read_subbuffer_mmap(
1744 ctx
, stream
, &subbuf_view
, padding
, &index
);
1746 * The mmap operation should write subbuf_size amount of data
1747 * when network streaming or the full padding (len) size when we
1748 * are _not_ streaming.
1750 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1751 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1753 * Display the error but continue processing to try to release the
1754 * subbuffer. This is a DBG statement since this is possible to
1755 * happen without being a critical error.
1757 DBG("Error writing to tracefile "
1758 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1759 ret
, len
, subbuf_size
);
1765 ERR("Unknown output method");
1769 err
= kernctl_put_next_subbuf(infd
);
1771 if (err
== -EFAULT
) {
1772 PERROR("Error in unreserving sub buffer\n");
1773 } else if (err
== -EIO
) {
1774 /* Should never happen with newer LTTng versions */
1775 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1779 } else if (in_error_state
) {
1783 /* Write index if needed. */
1788 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1790 * In live, block until all the metadata is sent.
1792 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1793 assert(!stream
->missed_metadata_flush
);
1794 stream
->waiting_on_metadata
= true;
1795 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1797 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1799 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1800 stream
->waiting_on_metadata
= false;
1801 if (stream
->missed_metadata_flush
) {
1802 stream
->missed_metadata_flush
= false;
1803 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1804 (void) consumer_flush_kernel_index(stream
);
1806 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1813 err
= consumer_stream_write_index(stream
, &index
);
1823 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1830 * Don't create anything if this is set for streaming or if there is
1831 * no current trace chunk on the parent channel.
1833 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1834 stream
->chan
->trace_chunk
) {
1835 ret
= consumer_stream_create_output_files(stream
, true);
1841 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1842 /* get the len of the mmap region */
1843 unsigned long mmap_len
;
1845 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1847 PERROR("kernctl_get_mmap_len");
1848 goto error_close_fd
;
1850 stream
->mmap_len
= (size_t) mmap_len
;
1852 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1853 MAP_PRIVATE
, stream
->wait_fd
, 0);
1854 if (stream
->mmap_base
== MAP_FAILED
) {
1855 PERROR("Error mmaping");
1857 goto error_close_fd
;
1861 /* we return 0 to let the library handle the FD internally */
1865 if (stream
->out_fd
>= 0) {
1868 err
= close(stream
->out_fd
);
1870 stream
->out_fd
= -1;
1877 * Check if data is still being extracted from the buffers for a specific
1878 * stream. Consumer data lock MUST be acquired before calling this function
1879 * and the stream lock.
1881 * Return 1 if the traced data are still getting read else 0 meaning that the
1882 * data is available for trace viewer reading.
1884 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1890 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1895 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1897 /* There is still data so let's put back this subbuffer. */
1898 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1900 ret
= 1; /* Data is pending */
1904 /* Data is NOT pending and ready to be read. */