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
,
527 if (new_channel
== NULL
) {
528 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
531 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
532 switch (msg
.u
.channel
.output
) {
533 case LTTNG_EVENT_SPLICE
:
534 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
536 case LTTNG_EVENT_MMAP
:
537 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
540 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
544 /* Translate and save channel type. */
545 switch (msg
.u
.channel
.type
) {
546 case CONSUMER_CHANNEL_TYPE_DATA
:
547 case CONSUMER_CHANNEL_TYPE_METADATA
:
548 new_channel
->type
= msg
.u
.channel
.type
;
555 health_code_update();
557 if (ctx
->on_recv_channel
!= NULL
) {
558 ret_recv
= ctx
->on_recv_channel(new_channel
);
560 ret
= consumer_add_channel(new_channel
, ctx
);
561 } else if (ret_recv
< 0) {
565 ret
= consumer_add_channel(new_channel
, ctx
);
567 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
568 int monitor_start_ret
;
570 DBG("Consumer starting monitor timer");
571 consumer_timer_live_start(new_channel
,
572 msg
.u
.channel
.live_timer_interval
);
573 monitor_start_ret
= consumer_timer_monitor_start(
575 msg
.u
.channel
.monitor_timer_interval
);
576 if (monitor_start_ret
< 0) {
577 ERR("Starting channel monitoring timer failed");
583 health_code_update();
585 /* If we received an error in add_channel, we need to report it. */
587 ret
= consumer_send_status_msg(sock
, ret
);
596 case LTTNG_CONSUMER_ADD_STREAM
:
599 struct lttng_pipe
*stream_pipe
;
600 struct lttng_consumer_stream
*new_stream
;
601 struct lttng_consumer_channel
*channel
;
605 * Get stream's channel reference. Needed when adding the stream to the
608 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
611 * We could not find the channel. Can happen if cpu hotplug
612 * happens while tearing down.
614 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
615 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
618 health_code_update();
620 /* First send a status message before receiving the fds. */
621 ret
= consumer_send_status_msg(sock
, ret_code
);
623 /* Somehow, the session daemon is not responding anymore. */
624 goto error_add_stream_fatal
;
627 health_code_update();
629 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
630 /* Channel was not found. */
631 goto error_add_stream_nosignal
;
636 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
639 goto error_add_stream_fatal
;
642 health_code_update();
644 /* Get stream file descriptor from socket */
645 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
646 if (ret
!= sizeof(fd
)) {
647 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
651 health_code_update();
654 * Send status code to session daemon only if the recv works. If the
655 * above recv() failed, the session daemon is notified through the
656 * error socket and the teardown is eventually done.
658 ret
= consumer_send_status_msg(sock
, ret_code
);
660 /* Somehow, the session daemon is not responding anymore. */
661 goto error_add_stream_nosignal
;
664 health_code_update();
666 pthread_mutex_lock(&channel
->lock
);
667 new_stream
= consumer_allocate_stream(channel
->key
,
672 channel
->trace_chunk
,
677 if (new_stream
== NULL
) {
682 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
685 pthread_mutex_unlock(&channel
->lock
);
686 goto error_add_stream_nosignal
;
689 new_stream
->chan
= channel
;
690 new_stream
->wait_fd
= fd
;
691 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
692 &new_stream
->max_sb_size
);
694 pthread_mutex_unlock(&channel
->lock
);
695 ERR("Failed to get kernel maximal subbuffer size");
696 goto error_add_stream_nosignal
;
699 consumer_stream_update_channel_attributes(new_stream
,
701 switch (channel
->output
) {
702 case CONSUMER_CHANNEL_SPLICE
:
703 new_stream
->output
= LTTNG_EVENT_SPLICE
;
704 ret
= utils_create_pipe(new_stream
->splice_pipe
);
706 pthread_mutex_unlock(&channel
->lock
);
707 goto error_add_stream_nosignal
;
710 case CONSUMER_CHANNEL_MMAP
:
711 new_stream
->output
= LTTNG_EVENT_MMAP
;
714 ERR("Stream output unknown %d", channel
->output
);
715 pthread_mutex_unlock(&channel
->lock
);
716 goto error_add_stream_nosignal
;
720 * We've just assigned the channel to the stream so increment the
721 * refcount right now. We don't need to increment the refcount for
722 * streams in no monitor because we handle manually the cleanup of
723 * those. It is very important to make sure there is NO prior
724 * consumer_del_stream() calls or else the refcount will be unbalanced.
726 if (channel
->monitor
) {
727 uatomic_inc(&new_stream
->chan
->refcount
);
731 * The buffer flush is done on the session daemon side for the kernel
732 * so no need for the stream "hangup_flush_done" variable to be
733 * tracked. This is important for a kernel stream since we don't rely
734 * on the flush state of the stream to read data. It's not the case for
735 * user space tracing.
737 new_stream
->hangup_flush_done
= 0;
739 health_code_update();
741 pthread_mutex_lock(&new_stream
->lock
);
742 if (ctx
->on_recv_stream
) {
743 ret
= ctx
->on_recv_stream(new_stream
);
745 pthread_mutex_unlock(&new_stream
->lock
);
746 pthread_mutex_unlock(&channel
->lock
);
747 consumer_stream_free(new_stream
);
748 goto error_add_stream_nosignal
;
751 health_code_update();
753 if (new_stream
->metadata_flag
) {
754 channel
->metadata_stream
= new_stream
;
757 /* Do not monitor this stream. */
758 if (!channel
->monitor
) {
759 DBG("Kernel consumer add stream %s in no monitor mode with "
760 "relayd id %" PRIu64
, new_stream
->name
,
761 new_stream
->net_seq_idx
);
762 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
763 pthread_mutex_unlock(&new_stream
->lock
);
764 pthread_mutex_unlock(&channel
->lock
);
768 /* Send stream to relayd if the stream has an ID. */
769 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
770 ret
= consumer_send_relayd_stream(new_stream
,
771 new_stream
->chan
->pathname
);
773 pthread_mutex_unlock(&new_stream
->lock
);
774 pthread_mutex_unlock(&channel
->lock
);
775 consumer_stream_free(new_stream
);
776 goto error_add_stream_nosignal
;
780 * If adding an extra stream to an already
781 * existing channel (e.g. cpu hotplug), we need
782 * to send the "streams_sent" command to relayd.
784 if (channel
->streams_sent_to_relayd
) {
785 ret
= consumer_send_relayd_streams_sent(
786 new_stream
->net_seq_idx
);
788 pthread_mutex_unlock(&new_stream
->lock
);
789 pthread_mutex_unlock(&channel
->lock
);
790 goto error_add_stream_nosignal
;
794 pthread_mutex_unlock(&new_stream
->lock
);
795 pthread_mutex_unlock(&channel
->lock
);
797 /* Get the right pipe where the stream will be sent. */
798 if (new_stream
->metadata_flag
) {
799 consumer_add_metadata_stream(new_stream
);
800 stream_pipe
= ctx
->consumer_metadata_pipe
;
802 consumer_add_data_stream(new_stream
);
803 stream_pipe
= ctx
->consumer_data_pipe
;
806 /* Visible to other threads */
807 new_stream
->globally_visible
= 1;
809 health_code_update();
811 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
813 ERR("Consumer write %s stream to pipe %d",
814 new_stream
->metadata_flag
? "metadata" : "data",
815 lttng_pipe_get_writefd(stream_pipe
));
816 if (new_stream
->metadata_flag
) {
817 consumer_del_stream_for_metadata(new_stream
);
819 consumer_del_stream_for_data(new_stream
);
821 goto error_add_stream_nosignal
;
824 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
825 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
828 error_add_stream_nosignal
:
830 error_add_stream_fatal
:
833 case LTTNG_CONSUMER_STREAMS_SENT
:
835 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
= consumer_send_status_msg(sock
, ret_code
);
858 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
859 /* Somehow, the session daemon is not responding anymore. */
860 goto error_streams_sent_nosignal
;
863 health_code_update();
866 * We should not send this message if we don't monitor the
867 * streams in this channel.
869 if (!channel
->monitor
) {
870 goto end_error_streams_sent
;
873 health_code_update();
874 /* Send stream to relayd if the stream has an ID. */
875 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
876 ret
= consumer_send_relayd_streams_sent(
877 msg
.u
.sent_streams
.net_seq_idx
);
879 goto error_streams_sent_nosignal
;
881 channel
->streams_sent_to_relayd
= true;
883 end_error_streams_sent
:
885 error_streams_sent_nosignal
:
888 case LTTNG_CONSUMER_UPDATE_STREAM
:
893 case LTTNG_CONSUMER_DESTROY_RELAYD
:
895 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
896 struct consumer_relayd_sock_pair
*relayd
;
898 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
900 /* Get relayd reference if exists. */
901 relayd
= consumer_find_relayd(index
);
902 if (relayd
== NULL
) {
903 DBG("Unable to find relayd %" PRIu64
, index
);
904 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
908 * Each relayd socket pair has a refcount of stream attached to it
909 * which tells if the relayd is still active or not depending on the
912 * This will set the destroy flag of the relayd object and destroy it
913 * if the refcount reaches zero when called.
915 * The destroy can happen either here or when a stream fd hangs up.
918 consumer_flag_relayd_for_destroy(relayd
);
921 health_code_update();
923 ret
= consumer_send_status_msg(sock
, ret_code
);
925 /* Somehow, the session daemon is not responding anymore. */
931 case LTTNG_CONSUMER_DATA_PENDING
:
934 uint64_t id
= msg
.u
.data_pending
.session_id
;
936 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
938 ret
= consumer_data_pending(id
);
940 health_code_update();
942 /* Send back returned value to session daemon */
943 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
945 PERROR("send data pending ret code");
950 * No need to send back a status message since the data pending
951 * returned value is the response.
955 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
957 struct lttng_consumer_channel
*channel
;
958 uint64_t key
= msg
.u
.snapshot_channel
.key
;
960 channel
= consumer_find_channel(key
);
962 ERR("Channel %" PRIu64
" not found", key
);
963 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
965 pthread_mutex_lock(&channel
->lock
);
966 if (msg
.u
.snapshot_channel
.metadata
== 1) {
967 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
968 msg
.u
.snapshot_channel
.pathname
,
969 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
971 ERR("Snapshot metadata failed");
972 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
975 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
976 msg
.u
.snapshot_channel
.pathname
,
977 msg
.u
.snapshot_channel
.relayd_id
,
978 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
981 ERR("Snapshot channel failed");
982 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
985 pthread_mutex_unlock(&channel
->lock
);
987 health_code_update();
989 ret
= consumer_send_status_msg(sock
, ret_code
);
991 /* Somehow, the session daemon is not responding anymore. */
996 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
998 uint64_t key
= msg
.u
.destroy_channel
.key
;
999 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
= consumer_send_status_msg(sock
, ret_code
);
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 assert(!channel
->monitor
);
1029 * The refcount should ALWAYS be 0 in the case of a channel in no
1032 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
;
1104 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1105 /* Successfully received the command's type. */
1106 ret
= consumer_send_status_msg(sock
, ret_code
);
1111 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1113 if (ret
!= sizeof(channel_monitor_pipe
)) {
1114 ERR("Failed to receive channel monitor pipe");
1118 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1119 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1120 channel_monitor_pipe
);
1124 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1125 /* Set the pipe as non-blocking. */
1126 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1128 PERROR("fcntl get flags of the channel monitoring pipe");
1133 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1134 flags
| O_NONBLOCK
);
1136 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1139 DBG("Channel monitor pipe set as non-blocking");
1141 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1143 ret
= consumer_send_status_msg(sock
, ret_code
);
1149 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1151 struct lttng_consumer_channel
*channel
;
1152 uint64_t key
= msg
.u
.rotate_channel
.key
;
1154 DBG("Consumer rotate channel %" PRIu64
, key
);
1156 channel
= consumer_find_channel(key
);
1158 ERR("Channel %" PRIu64
" not found", key
);
1159 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1162 * Sample the rotate position of all the streams in this channel.
1164 ret
= lttng_consumer_rotate_channel(channel
, key
,
1165 msg
.u
.rotate_channel
.relayd_id
,
1166 msg
.u
.rotate_channel
.metadata
,
1169 ERR("Rotate channel failed");
1170 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1173 health_code_update();
1175 ret
= consumer_send_status_msg(sock
, ret_code
);
1177 /* Somehow, the session daemon is not responding anymore. */
1178 goto error_rotate_channel
;
1181 /* Rotate the streams that are ready right now. */
1182 ret
= lttng_consumer_rotate_ready_streams(
1185 ERR("Rotate ready streams failed");
1189 error_rotate_channel
:
1192 case LTTNG_CONSUMER_INIT
:
1194 ret_code
= lttng_consumer_init_command(ctx
,
1195 msg
.u
.init
.sessiond_uuid
);
1196 health_code_update();
1197 ret
= consumer_send_status_msg(sock
, ret_code
);
1199 /* Somehow, the session daemon is not responding anymore. */
1204 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1206 const struct lttng_credentials credentials
= {
1207 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1208 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1210 const bool is_local_trace
=
1211 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1212 const uint64_t relayd_id
=
1213 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1214 const char *chunk_override_name
=
1215 *msg
.u
.create_trace_chunk
.override_name
?
1216 msg
.u
.create_trace_chunk
.override_name
:
1218 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
1219 LTTNG_OPTIONAL_INIT
;
1222 * The session daemon will only provide a chunk directory file
1223 * descriptor for local traces.
1225 if (is_local_trace
) {
1228 /* Acnowledge the reception of the command. */
1229 ret
= consumer_send_status_msg(sock
,
1230 LTTCOMM_CONSUMERD_SUCCESS
);
1232 /* Somehow, the session daemon is not responding anymore. */
1236 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1237 if (ret
!= sizeof(chunk_dirfd
)) {
1238 ERR("Failed to receive trace chunk directory file descriptor");
1242 DBG("Received trace chunk directory fd (%d)",
1244 ret
= lttng_directory_handle_init_from_dirfd(
1245 &chunk_directory_handle
.value
,
1248 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1249 if (close(chunk_dirfd
)) {
1250 PERROR("Failed to close chunk directory file descriptor");
1254 chunk_directory_handle
.is_set
= true;
1257 ret_code
= lttng_consumer_create_trace_chunk(
1258 !is_local_trace
? &relayd_id
: NULL
,
1259 msg
.u
.create_trace_chunk
.session_id
,
1260 msg
.u
.create_trace_chunk
.chunk_id
,
1261 (time_t) msg
.u
.create_trace_chunk
1262 .creation_timestamp
,
1263 chunk_override_name
,
1264 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1267 chunk_directory_handle
.is_set
?
1268 &chunk_directory_handle
.value
:
1271 if (chunk_directory_handle
.is_set
) {
1272 lttng_directory_handle_fini(
1273 &chunk_directory_handle
.value
);
1275 goto end_msg_sessiond
;
1277 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1279 enum lttng_trace_chunk_command_type close_command
=
1280 msg
.u
.close_trace_chunk
.close_command
.value
;
1281 const uint64_t relayd_id
=
1282 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1283 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1284 char path
[LTTNG_PATH_MAX
];
1286 ret_code
= lttng_consumer_close_trace_chunk(
1287 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1290 msg
.u
.close_trace_chunk
.session_id
,
1291 msg
.u
.close_trace_chunk
.chunk_id
,
1292 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1293 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1296 reply
.ret_code
= ret_code
;
1297 reply
.path_length
= strlen(path
) + 1;
1298 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1299 if (ret
!= sizeof(reply
)) {
1302 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1303 if (ret
!= reply
.path_length
) {
1308 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1310 const uint64_t relayd_id
=
1311 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1313 ret_code
= lttng_consumer_trace_chunk_exists(
1314 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1316 msg
.u
.trace_chunk_exists
.session_id
,
1317 msg
.u
.trace_chunk_exists
.chunk_id
);
1318 goto end_msg_sessiond
;
1326 * Return 1 to indicate success since the 0 value can be a socket
1327 * shutdown during the recv() or send() call.
1332 /* This will issue a consumer stop. */
1337 * The returned value here is not useful since either way we'll return 1 to
1338 * the caller because the session daemon socket management is done
1339 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1341 ret
= consumer_send_status_msg(sock
, ret_code
);
1347 health_code_update();
1353 * Populate index values of a kernel stream. Values are set in big endian order.
1355 * Return 0 on success or else a negative value.
1357 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1360 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1361 events_discarded
, stream_id
, stream_instance_id
,
1364 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1366 PERROR("kernctl_get_timestamp_begin");
1370 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1372 PERROR("kernctl_get_timestamp_end");
1376 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1378 PERROR("kernctl_get_events_discarded");
1382 ret
= kernctl_get_content_size(infd
, &content_size
);
1384 PERROR("kernctl_get_content_size");
1388 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1390 PERROR("kernctl_get_packet_size");
1394 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1396 PERROR("kernctl_get_stream_id");
1400 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1402 if (ret
== -ENOTTY
) {
1403 /* Command not implemented by lttng-modules. */
1404 stream_instance_id
= -1ULL;
1406 PERROR("kernctl_get_instance_id");
1411 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1413 if (ret
== -ENOTTY
) {
1414 /* Command not implemented by lttng-modules. */
1415 packet_seq_num
= -1ULL;
1418 PERROR("kernctl_get_sequence_number");
1422 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1424 *index
= (typeof(*index
)) {
1425 .offset
= index
->offset
,
1426 .packet_size
= htobe64(packet_size
),
1427 .content_size
= htobe64(content_size
),
1428 .timestamp_begin
= htobe64(timestamp_begin
),
1429 .timestamp_end
= htobe64(timestamp_end
),
1430 .events_discarded
= htobe64(events_discarded
),
1431 .stream_id
= htobe64(stream_id
),
1432 .stream_instance_id
= htobe64(stream_instance_id
),
1433 .packet_seq_num
= htobe64(packet_seq_num
),
1440 * Sync metadata meaning request them to the session daemon and snapshot to the
1441 * metadata thread can consumer them.
1443 * Metadata stream lock MUST be acquired.
1445 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1446 * is empty or a negative value on error.
1448 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1454 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1456 ERR("Failed to flush kernel stream");
1460 ret
= kernctl_snapshot(metadata
->wait_fd
);
1462 if (ret
!= -EAGAIN
) {
1463 ERR("Sync metadata, taking kernel snapshot failed.");
1466 DBG("Sync metadata, no new kernel metadata");
1467 /* No new metadata, exit. */
1477 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1480 uint64_t seq
, discarded
;
1482 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1484 if (ret
== -ENOTTY
) {
1485 /* Command not implemented by lttng-modules. */
1487 stream
->sequence_number_unavailable
= true;
1489 PERROR("kernctl_get_sequence_number");
1495 * Start the sequence when we extract the first packet in case we don't
1496 * start at 0 (for example if a consumer is not connected to the
1497 * session immediately after the beginning).
1499 if (stream
->last_sequence_number
== -1ULL) {
1500 stream
->last_sequence_number
= seq
;
1501 } else if (seq
> stream
->last_sequence_number
) {
1502 stream
->chan
->lost_packets
+= seq
-
1503 stream
->last_sequence_number
- 1;
1505 /* seq <= last_sequence_number */
1506 ERR("Sequence number inconsistent : prev = %" PRIu64
1507 ", current = %" PRIu64
,
1508 stream
->last_sequence_number
, seq
);
1512 stream
->last_sequence_number
= seq
;
1514 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1516 PERROR("kernctl_get_events_discarded");
1519 if (discarded
< stream
->last_discarded_events
) {
1521 * Overflow has occurred. We assume only one wrap-around
1524 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1525 stream
->last_discarded_events
+ discarded
;
1527 stream
->chan
->discarded_events
+= discarded
-
1528 stream
->last_discarded_events
;
1530 stream
->last_discarded_events
= discarded
;
1538 * Check if the local version of the metadata stream matches with the version
1539 * of the metadata stream in the kernel. If it was updated, set the reset flag
1543 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1546 uint64_t cur_version
;
1548 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1550 if (ret
== -ENOTTY
) {
1552 * LTTng-modules does not implement this
1558 ERR("Failed to get the metadata version");
1562 if (stream
->metadata_version
== cur_version
) {
1567 DBG("New metadata version detected");
1568 stream
->metadata_version
= cur_version
;
1569 stream
->reset_metadata_flag
= 1;
1577 * Consume data on a file descriptor and write it on a trace file.
1578 * The stream and channel locks must be held by the caller.
1580 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1581 struct lttng_consumer_local_data
*ctx
)
1583 unsigned long len
, subbuf_size
, padding
;
1584 int err
, write_index
= 1, rotation_ret
;
1586 int infd
= stream
->wait_fd
;
1587 struct ctf_packet_index index
= {};
1589 DBG("In read_subbuffer (infd : %d)", infd
);
1592 * If the stream was flagged to be ready for rotation before we extract the
1593 * next packet, rotate it now.
1595 if (stream
->rotate_ready
) {
1596 DBG("Rotate stream before extracting data");
1597 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1598 if (rotation_ret
< 0) {
1599 ERR("Stream rotation error");
1605 /* Get the next subbuffer */
1606 err
= kernctl_get_next_subbuf(infd
);
1609 * This is a debug message even for single-threaded consumer,
1610 * because poll() have more relaxed criterions than get subbuf,
1611 * so get_subbuf may fail for short race windows where poll()
1612 * would issue wakeups.
1614 DBG("Reserving sub buffer failed (everything is normal, "
1615 "it is due to concurrency)");
1620 /* Get the full subbuffer size including padding */
1621 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1623 PERROR("Getting sub-buffer len failed.");
1624 err
= kernctl_put_subbuf(infd
);
1626 if (err
== -EFAULT
) {
1627 PERROR("Error in unreserving sub buffer\n");
1628 } else if (err
== -EIO
) {
1629 /* Should never happen with newer LTTng versions */
1630 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1639 if (!stream
->metadata_flag
) {
1640 ret
= get_index_values(&index
, infd
);
1642 err
= kernctl_put_subbuf(infd
);
1644 if (err
== -EFAULT
) {
1645 PERROR("Error in unreserving sub buffer\n");
1646 } else if (err
== -EIO
) {
1647 /* Should never happen with newer LTTng versions */
1648 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1655 ret
= update_stream_stats(stream
);
1657 err
= kernctl_put_subbuf(infd
);
1659 if (err
== -EFAULT
) {
1660 PERROR("Error in unreserving sub buffer\n");
1661 } else if (err
== -EIO
) {
1662 /* Should never happen with newer LTTng versions */
1663 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1672 ret
= metadata_stream_check_version(infd
, stream
);
1674 err
= kernctl_put_subbuf(infd
);
1676 if (err
== -EFAULT
) {
1677 PERROR("Error in unreserving sub buffer\n");
1678 } else if (err
== -EIO
) {
1679 /* Should never happen with newer LTTng versions */
1680 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1689 switch (stream
->chan
->output
) {
1690 case CONSUMER_CHANNEL_SPLICE
:
1692 * XXX: The lttng-modules splice "actor" does not handle copying
1693 * partial pages hence only using the subbuffer size without the
1694 * padding makes the splice fail.
1699 /* splice the subbuffer to the tracefile */
1700 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1703 * XXX: Splice does not support network streaming so the return value
1704 * is simply checked against subbuf_size and not like the mmap() op.
1706 if (ret
!= subbuf_size
) {
1708 * display the error but continue processing to try
1709 * to release the subbuffer
1711 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1716 case CONSUMER_CHANNEL_MMAP
:
1718 const char *subbuf_addr
;
1719 struct lttng_buffer_view subbuf_view
;
1721 /* Get subbuffer size without padding */
1722 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1724 PERROR("Getting sub-buffer len failed.");
1725 err
= kernctl_put_subbuf(infd
);
1727 if (err
== -EFAULT
) {
1728 PERROR("Error in unreserving sub buffer\n");
1729 } else if (err
== -EIO
) {
1730 /* Should never happen with newer LTTng versions */
1731 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1740 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1742 goto error_put_subbuf
;
1745 /* Make sure the tracer is not gone mad on us! */
1746 assert(len
>= subbuf_size
);
1748 padding
= len
- subbuf_size
;
1750 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
1752 /* write the subbuffer to the tracefile */
1753 ret
= lttng_consumer_on_read_subbuffer_mmap(
1754 ctx
, stream
, &subbuf_view
, padding
, &index
);
1756 * The mmap operation should write subbuf_size amount of data
1757 * when network streaming or the full padding (len) size when we
1758 * are _not_ streaming.
1760 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1761 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1763 * Display the error but continue processing to try to release the
1764 * subbuffer. This is a DBG statement since this is possible to
1765 * happen without being a critical error.
1767 DBG("Error writing to tracefile "
1768 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1769 ret
, len
, subbuf_size
);
1775 ERR("Unknown output method");
1779 err
= kernctl_put_next_subbuf(infd
);
1781 if (err
== -EFAULT
) {
1782 PERROR("Error in unreserving sub buffer\n");
1783 } else if (err
== -EIO
) {
1784 /* Should never happen with newer LTTng versions */
1785 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1791 /* Write index if needed. */
1796 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1798 * In live, block until all the metadata is sent.
1800 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1801 assert(!stream
->missed_metadata_flush
);
1802 stream
->waiting_on_metadata
= true;
1803 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1805 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1807 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1808 stream
->waiting_on_metadata
= false;
1809 if (stream
->missed_metadata_flush
) {
1810 stream
->missed_metadata_flush
= false;
1811 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1812 (void) consumer_flush_kernel_index(stream
);
1814 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1821 err
= consumer_stream_write_index(stream
, &index
);
1828 * After extracting the packet, we check if the stream is now ready to be
1829 * rotated and perform the action immediately.
1831 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1832 if (rotation_ret
== 1) {
1833 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1834 if (rotation_ret
< 0) {
1835 ERR("Stream rotation error");
1839 } else if (rotation_ret
< 0) {
1840 ERR("Checking if stream is ready to rotate");
1849 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1856 * Don't create anything if this is set for streaming or if there is
1857 * no current trace chunk on the parent channel.
1859 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1860 stream
->chan
->trace_chunk
) {
1861 ret
= consumer_stream_create_output_files(stream
, true);
1867 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1868 /* get the len of the mmap region */
1869 unsigned long mmap_len
;
1871 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1873 PERROR("kernctl_get_mmap_len");
1874 goto error_close_fd
;
1876 stream
->mmap_len
= (size_t) mmap_len
;
1878 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1879 MAP_PRIVATE
, stream
->wait_fd
, 0);
1880 if (stream
->mmap_base
== MAP_FAILED
) {
1881 PERROR("Error mmaping");
1883 goto error_close_fd
;
1887 /* we return 0 to let the library handle the FD internally */
1891 if (stream
->out_fd
>= 0) {
1894 err
= close(stream
->out_fd
);
1896 stream
->out_fd
= -1;
1903 * Check if data is still being extracted from the buffers for a specific
1904 * stream. Consumer data lock MUST be acquired before calling this function
1905 * and the stream lock.
1907 * Return 1 if the traced data are still getting read else 0 meaning that the
1908 * data is available for trace viewer reading.
1910 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1916 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1921 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1923 /* There is still data so let's put back this subbuffer. */
1924 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1926 ret
= 1; /* Data is pending */
1930 /* Data is NOT pending and ready to be read. */