2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/buffer-view.h"
19 #include <sys/socket.h>
20 #include <sys/types.h>
25 #include <bin/lttng-consumerd/health-consumerd.h>
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/sessiond-comm/relayd.h>
30 #include <common/compat/fcntl.h>
31 #include <common/compat/endian.h>
32 #include <common/pipe.h>
33 #include <common/relayd/relayd.h>
34 #include <common/utils.h>
35 #include <common/consumer/consumer-stream.h>
36 #include <common/index/index.h>
37 #include <common/consumer/consumer-timer.h>
38 #include <common/optional.h>
40 #include "kernel-consumer.h"
42 extern struct lttng_consumer_global_data consumer_data
;
43 extern int consumer_poll_timeout
;
46 * Take a snapshot for a specific fd
48 * Returns 0 on success, < 0 on error
50 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
53 int infd
= stream
->wait_fd
;
55 ret
= kernctl_snapshot(infd
);
57 * -EAGAIN is not an error, it just means that there is no data to
60 if (ret
!= 0 && ret
!= -EAGAIN
) {
61 PERROR("Getting sub-buffer snapshot.");
68 * Sample consumed and produced positions for a specific fd.
70 * Returns 0 on success, < 0 on error.
72 int lttng_kconsumer_sample_snapshot_positions(
73 struct lttng_consumer_stream
*stream
)
77 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
81 * Get the produced position
83 * Returns 0 on success, < 0 on error
85 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
89 int infd
= stream
->wait_fd
;
91 ret
= kernctl_snapshot_get_produced(infd
, pos
);
93 PERROR("kernctl_snapshot_get_produced");
100 * Get the consumerd position
102 * Returns 0 on success, < 0 on error
104 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
108 int infd
= stream
->wait_fd
;
110 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
112 PERROR("kernctl_snapshot_get_consumed");
119 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
123 unsigned long mmap_offset
;
124 const char *mmap_base
= stream
->mmap_base
;
126 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
128 PERROR("Failed to get mmap read offset");
132 *addr
= mmap_base
+ mmap_offset
;
138 * Take a snapshot of all the stream of a channel
139 * RCU read-side lock must be held across this function to ensure existence of
140 * channel. The channel lock must be held by the caller.
142 * Returns 0 on success, < 0 on error
144 static int lttng_kconsumer_snapshot_channel(
145 struct lttng_consumer_channel
*channel
,
146 uint64_t key
, char *path
, uint64_t relayd_id
,
147 uint64_t nb_packets_per_stream
,
148 struct lttng_consumer_local_data
*ctx
)
151 struct lttng_consumer_stream
*stream
;
153 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
157 /* Splice is not supported yet for channel snapshot. */
158 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
159 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
165 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
166 unsigned long consumed_pos
, produced_pos
;
168 health_code_update();
171 * Lock stream because we are about to change its state.
173 pthread_mutex_lock(&stream
->lock
);
175 assert(channel
->trace_chunk
);
176 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
178 * Can't happen barring an internal error as the channel
179 * holds a reference to the trace chunk.
181 ERR("Failed to acquire reference to channel's trace chunk");
185 assert(!stream
->trace_chunk
);
186 stream
->trace_chunk
= channel
->trace_chunk
;
189 * Assign the received relayd ID so we can use it for streaming. The streams
190 * are not visible to anyone so this is OK to change it.
192 stream
->net_seq_idx
= relayd_id
;
193 channel
->relayd_id
= relayd_id
;
194 if (relayd_id
!= (uint64_t) -1ULL) {
195 ret
= consumer_send_relayd_stream(stream
, path
);
197 ERR("sending stream to relayd");
201 ret
= consumer_stream_create_output_files(stream
,
206 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
210 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
213 * Doing a buffer flush which does not take into
214 * account empty packets. This is not perfect
215 * for stream intersection, but required as a
216 * fall-back when "flush_empty" is not
217 * implemented by lttng-modules.
219 ret
= kernctl_buffer_flush(stream
->wait_fd
);
221 ERR("Failed to flush kernel stream");
227 ret
= lttng_kconsumer_take_snapshot(stream
);
229 ERR("Taking kernel snapshot");
233 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
235 ERR("Produced kernel snapshot position");
239 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
241 ERR("Consumerd kernel snapshot position");
245 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
246 produced_pos
, nb_packets_per_stream
,
247 stream
->max_sb_size
);
249 while ((long) (consumed_pos
- produced_pos
) < 0) {
251 unsigned long len
, padded_len
;
252 const char *subbuf_addr
;
253 struct lttng_buffer_view subbuf_view
;
255 health_code_update();
256 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
258 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
260 if (ret
!= -EAGAIN
) {
261 PERROR("kernctl_get_subbuf snapshot");
264 DBG("Kernel consumer get subbuf failed. Skipping it.");
265 consumed_pos
+= stream
->max_sb_size
;
266 stream
->chan
->lost_packets
++;
270 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
272 ERR("Snapshot kernctl_get_subbuf_size");
273 goto error_put_subbuf
;
276 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
278 ERR("Snapshot kernctl_get_padded_subbuf_size");
279 goto error_put_subbuf
;
282 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
284 goto error_put_subbuf
;
287 subbuf_view
= lttng_buffer_view_init(
288 subbuf_addr
, 0, padded_len
);
289 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
290 stream
, &subbuf_view
,
291 padded_len
- len
, NULL
);
293 * We write the padded len in local tracefiles but the data len
294 * when using a relay. Display the error but continue processing
295 * to try to release the subbuffer.
297 if (relayd_id
!= (uint64_t) -1ULL) {
298 if (read_len
!= len
) {
299 ERR("Error sending to the relay (ret: %zd != len: %lu)",
303 if (read_len
!= padded_len
) {
304 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
305 read_len
, padded_len
);
309 ret
= kernctl_put_subbuf(stream
->wait_fd
);
311 ERR("Snapshot kernctl_put_subbuf");
314 consumed_pos
+= stream
->max_sb_size
;
317 if (relayd_id
== (uint64_t) -1ULL) {
318 if (stream
->out_fd
>= 0) {
319 ret
= close(stream
->out_fd
);
321 PERROR("Kernel consumer snapshot close out_fd");
327 close_relayd_stream(stream
);
328 stream
->net_seq_idx
= (uint64_t) -1ULL;
330 lttng_trace_chunk_put(stream
->trace_chunk
);
331 stream
->trace_chunk
= NULL
;
332 pthread_mutex_unlock(&stream
->lock
);
340 ret
= kernctl_put_subbuf(stream
->wait_fd
);
342 ERR("Snapshot kernctl_put_subbuf error path");
345 pthread_mutex_unlock(&stream
->lock
);
352 * Read the whole metadata available for a snapshot.
353 * RCU read-side lock must be held across this function to ensure existence of
354 * metadata_channel. The channel lock must be held by the caller.
356 * Returns 0 on success, < 0 on error
358 static int lttng_kconsumer_snapshot_metadata(
359 struct lttng_consumer_channel
*metadata_channel
,
360 uint64_t key
, char *path
, uint64_t relayd_id
,
361 struct lttng_consumer_local_data
*ctx
)
363 int ret
, use_relayd
= 0;
365 struct lttng_consumer_stream
*metadata_stream
;
369 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
374 metadata_stream
= metadata_channel
->metadata_stream
;
375 assert(metadata_stream
);
377 pthread_mutex_lock(&metadata_stream
->lock
);
378 assert(metadata_channel
->trace_chunk
);
379 assert(metadata_stream
->trace_chunk
);
381 /* Flag once that we have a valid relayd for the stream. */
382 if (relayd_id
!= (uint64_t) -1ULL) {
387 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
392 ret
= consumer_stream_create_output_files(metadata_stream
,
400 health_code_update();
402 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
404 if (ret_read
!= -EAGAIN
) {
405 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
410 /* ret_read is negative at this point so we will exit the loop. */
413 } while (ret_read
>= 0);
416 close_relayd_stream(metadata_stream
);
417 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
419 if (metadata_stream
->out_fd
>= 0) {
420 ret
= close(metadata_stream
->out_fd
);
422 PERROR("Kernel consumer snapshot metadata close out_fd");
424 * Don't go on error here since the snapshot was successful at this
425 * point but somehow the close failed.
428 metadata_stream
->out_fd
= -1;
429 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
430 metadata_stream
->trace_chunk
= NULL
;
436 pthread_mutex_unlock(&metadata_stream
->lock
);
437 cds_list_del(&metadata_stream
->send_node
);
438 consumer_stream_destroy(metadata_stream
, NULL
);
439 metadata_channel
->metadata_stream
= NULL
;
445 * Receive command from session daemon and process it.
447 * Return 1 on success else a negative value or 0.
449 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
450 int sock
, struct pollfd
*consumer_sockpoll
)
453 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
454 struct lttcomm_consumer_msg msg
;
456 health_code_update();
458 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
459 if (ret
!= sizeof(msg
)) {
461 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
467 health_code_update();
469 /* Deprecated command */
470 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
472 health_code_update();
474 /* relayd needs RCU read-side protection */
477 switch (msg
.cmd_type
) {
478 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
480 /* Session daemon status message are handled in the following call. */
481 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
482 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
483 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
484 msg
.u
.relayd_sock
.relayd_session_id
);
487 case LTTNG_CONSUMER_ADD_CHANNEL
:
489 struct lttng_consumer_channel
*new_channel
;
491 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
493 health_code_update();
495 /* First send a status message before receiving the fds. */
496 ret
= consumer_send_status_msg(sock
, ret_code
);
498 /* Somehow, the session daemon is not responding anymore. */
502 health_code_update();
504 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
505 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
506 msg
.u
.channel
.session_id
,
507 msg
.u
.channel
.chunk_id
.is_set
?
509 msg
.u
.channel
.pathname
,
511 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
512 msg
.u
.channel
.tracefile_size
,
513 msg
.u
.channel
.tracefile_count
, 0,
514 msg
.u
.channel
.monitor
,
515 msg
.u
.channel
.live_timer_interval
,
516 msg
.u
.channel
.is_live
,
518 if (new_channel
== NULL
) {
519 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
522 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
523 switch (msg
.u
.channel
.output
) {
524 case LTTNG_EVENT_SPLICE
:
525 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
527 case LTTNG_EVENT_MMAP
:
528 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
531 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
535 /* Translate and save channel type. */
536 switch (msg
.u
.channel
.type
) {
537 case CONSUMER_CHANNEL_TYPE_DATA
:
538 case CONSUMER_CHANNEL_TYPE_METADATA
:
539 new_channel
->type
= msg
.u
.channel
.type
;
546 health_code_update();
548 if (ctx
->on_recv_channel
!= NULL
) {
549 ret_recv
= ctx
->on_recv_channel(new_channel
);
551 ret
= consumer_add_channel(new_channel
, ctx
);
552 } else if (ret_recv
< 0) {
556 ret
= consumer_add_channel(new_channel
, ctx
);
558 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
559 int monitor_start_ret
;
561 DBG("Consumer starting monitor timer");
562 consumer_timer_live_start(new_channel
,
563 msg
.u
.channel
.live_timer_interval
);
564 monitor_start_ret
= consumer_timer_monitor_start(
566 msg
.u
.channel
.monitor_timer_interval
);
567 if (monitor_start_ret
< 0) {
568 ERR("Starting channel monitoring timer failed");
574 health_code_update();
576 /* If we received an error in add_channel, we need to report it. */
578 ret
= consumer_send_status_msg(sock
, ret
);
587 case LTTNG_CONSUMER_ADD_STREAM
:
590 struct lttng_pipe
*stream_pipe
;
591 struct lttng_consumer_stream
*new_stream
;
592 struct lttng_consumer_channel
*channel
;
596 * Get stream's channel reference. Needed when adding the stream to the
599 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
602 * We could not find the channel. Can happen if cpu hotplug
603 * happens while tearing down.
605 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
606 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
609 health_code_update();
611 /* First send a status message before receiving the fds. */
612 ret
= consumer_send_status_msg(sock
, ret_code
);
614 /* Somehow, the session daemon is not responding anymore. */
615 goto error_add_stream_fatal
;
618 health_code_update();
620 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
621 /* Channel was not found. */
622 goto error_add_stream_nosignal
;
627 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
630 goto error_add_stream_fatal
;
633 health_code_update();
635 /* Get stream file descriptor from socket */
636 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
637 if (ret
!= sizeof(fd
)) {
638 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
642 health_code_update();
645 * Send status code to session daemon only if the recv works. If the
646 * above recv() failed, the session daemon is notified through the
647 * error socket and the teardown is eventually done.
649 ret
= consumer_send_status_msg(sock
, ret_code
);
651 /* Somehow, the session daemon is not responding anymore. */
652 goto error_add_stream_nosignal
;
655 health_code_update();
657 pthread_mutex_lock(&channel
->lock
);
658 new_stream
= consumer_allocate_stream(
665 channel
->trace_chunk
,
670 if (new_stream
== NULL
) {
675 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
678 pthread_mutex_unlock(&channel
->lock
);
679 goto error_add_stream_nosignal
;
682 new_stream
->wait_fd
= fd
;
683 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
684 &new_stream
->max_sb_size
);
686 pthread_mutex_unlock(&channel
->lock
);
687 ERR("Failed to get kernel maximal subbuffer size");
688 goto error_add_stream_nosignal
;
691 consumer_stream_update_channel_attributes(new_stream
,
693 switch (channel
->output
) {
694 case CONSUMER_CHANNEL_SPLICE
:
695 new_stream
->output
= LTTNG_EVENT_SPLICE
;
696 ret
= utils_create_pipe(new_stream
->splice_pipe
);
698 pthread_mutex_unlock(&channel
->lock
);
699 goto error_add_stream_nosignal
;
702 case CONSUMER_CHANNEL_MMAP
:
703 new_stream
->output
= LTTNG_EVENT_MMAP
;
706 ERR("Stream output unknown %d", channel
->output
);
707 pthread_mutex_unlock(&channel
->lock
);
708 goto error_add_stream_nosignal
;
712 * We've just assigned the channel to the stream so increment the
713 * refcount right now. We don't need to increment the refcount for
714 * streams in no monitor because we handle manually the cleanup of
715 * those. It is very important to make sure there is NO prior
716 * consumer_del_stream() calls or else the refcount will be unbalanced.
718 if (channel
->monitor
) {
719 uatomic_inc(&new_stream
->chan
->refcount
);
723 * The buffer flush is done on the session daemon side for the kernel
724 * so no need for the stream "hangup_flush_done" variable to be
725 * tracked. This is important for a kernel stream since we don't rely
726 * on the flush state of the stream to read data. It's not the case for
727 * user space tracing.
729 new_stream
->hangup_flush_done
= 0;
731 health_code_update();
733 pthread_mutex_lock(&new_stream
->lock
);
734 if (ctx
->on_recv_stream
) {
735 ret
= ctx
->on_recv_stream(new_stream
);
737 pthread_mutex_unlock(&new_stream
->lock
);
738 pthread_mutex_unlock(&channel
->lock
);
739 consumer_stream_free(new_stream
);
740 goto error_add_stream_nosignal
;
743 health_code_update();
745 if (new_stream
->metadata_flag
) {
746 channel
->metadata_stream
= new_stream
;
749 /* Do not monitor this stream. */
750 if (!channel
->monitor
) {
751 DBG("Kernel consumer add stream %s in no monitor mode with "
752 "relayd id %" PRIu64
, new_stream
->name
,
753 new_stream
->net_seq_idx
);
754 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
755 pthread_mutex_unlock(&new_stream
->lock
);
756 pthread_mutex_unlock(&channel
->lock
);
760 /* Send stream to relayd if the stream has an ID. */
761 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
762 ret
= consumer_send_relayd_stream(new_stream
,
763 new_stream
->chan
->pathname
);
765 pthread_mutex_unlock(&new_stream
->lock
);
766 pthread_mutex_unlock(&channel
->lock
);
767 consumer_stream_free(new_stream
);
768 goto error_add_stream_nosignal
;
772 * If adding an extra stream to an already
773 * existing channel (e.g. cpu hotplug), we need
774 * to send the "streams_sent" command to relayd.
776 if (channel
->streams_sent_to_relayd
) {
777 ret
= consumer_send_relayd_streams_sent(
778 new_stream
->net_seq_idx
);
780 pthread_mutex_unlock(&new_stream
->lock
);
781 pthread_mutex_unlock(&channel
->lock
);
782 goto error_add_stream_nosignal
;
786 pthread_mutex_unlock(&new_stream
->lock
);
787 pthread_mutex_unlock(&channel
->lock
);
789 /* Get the right pipe where the stream will be sent. */
790 if (new_stream
->metadata_flag
) {
791 consumer_add_metadata_stream(new_stream
);
792 stream_pipe
= ctx
->consumer_metadata_pipe
;
794 consumer_add_data_stream(new_stream
);
795 stream_pipe
= ctx
->consumer_data_pipe
;
798 /* Visible to other threads */
799 new_stream
->globally_visible
= 1;
801 health_code_update();
803 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
805 ERR("Consumer write %s stream to pipe %d",
806 new_stream
->metadata_flag
? "metadata" : "data",
807 lttng_pipe_get_writefd(stream_pipe
));
808 if (new_stream
->metadata_flag
) {
809 consumer_del_stream_for_metadata(new_stream
);
811 consumer_del_stream_for_data(new_stream
);
813 goto error_add_stream_nosignal
;
816 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
817 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
820 error_add_stream_nosignal
:
822 error_add_stream_fatal
:
825 case LTTNG_CONSUMER_STREAMS_SENT
:
827 struct lttng_consumer_channel
*channel
;
830 * Get stream's channel reference. Needed when adding the stream to the
833 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
836 * We could not find the channel. Can happen if cpu hotplug
837 * happens while tearing down.
839 ERR("Unable to find channel key %" PRIu64
,
840 msg
.u
.sent_streams
.channel_key
);
841 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
844 health_code_update();
847 * Send status code to session daemon.
849 ret
= consumer_send_status_msg(sock
, ret_code
);
850 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
851 /* Somehow, the session daemon is not responding anymore. */
852 goto error_streams_sent_nosignal
;
855 health_code_update();
858 * We should not send this message if we don't monitor the
859 * streams in this channel.
861 if (!channel
->monitor
) {
862 goto end_error_streams_sent
;
865 health_code_update();
866 /* Send stream to relayd if the stream has an ID. */
867 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
868 ret
= consumer_send_relayd_streams_sent(
869 msg
.u
.sent_streams
.net_seq_idx
);
871 goto error_streams_sent_nosignal
;
873 channel
->streams_sent_to_relayd
= true;
875 end_error_streams_sent
:
877 error_streams_sent_nosignal
:
880 case LTTNG_CONSUMER_UPDATE_STREAM
:
885 case LTTNG_CONSUMER_DESTROY_RELAYD
:
887 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
888 struct consumer_relayd_sock_pair
*relayd
;
890 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
892 /* Get relayd reference if exists. */
893 relayd
= consumer_find_relayd(index
);
894 if (relayd
== NULL
) {
895 DBG("Unable to find relayd %" PRIu64
, index
);
896 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
900 * Each relayd socket pair has a refcount of stream attached to it
901 * which tells if the relayd is still active or not depending on the
904 * This will set the destroy flag of the relayd object and destroy it
905 * if the refcount reaches zero when called.
907 * The destroy can happen either here or when a stream fd hangs up.
910 consumer_flag_relayd_for_destroy(relayd
);
913 health_code_update();
915 ret
= consumer_send_status_msg(sock
, ret_code
);
917 /* Somehow, the session daemon is not responding anymore. */
923 case LTTNG_CONSUMER_DATA_PENDING
:
926 uint64_t id
= msg
.u
.data_pending
.session_id
;
928 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
930 ret
= consumer_data_pending(id
);
932 health_code_update();
934 /* Send back returned value to session daemon */
935 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
937 PERROR("send data pending ret code");
942 * No need to send back a status message since the data pending
943 * returned value is the response.
947 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
949 struct lttng_consumer_channel
*channel
;
950 uint64_t key
= msg
.u
.snapshot_channel
.key
;
952 channel
= consumer_find_channel(key
);
954 ERR("Channel %" PRIu64
" not found", key
);
955 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
957 pthread_mutex_lock(&channel
->lock
);
958 if (msg
.u
.snapshot_channel
.metadata
== 1) {
959 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
960 msg
.u
.snapshot_channel
.pathname
,
961 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
963 ERR("Snapshot metadata failed");
964 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
967 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
968 msg
.u
.snapshot_channel
.pathname
,
969 msg
.u
.snapshot_channel
.relayd_id
,
970 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
973 ERR("Snapshot channel failed");
974 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
977 pthread_mutex_unlock(&channel
->lock
);
979 health_code_update();
981 ret
= consumer_send_status_msg(sock
, ret_code
);
983 /* Somehow, the session daemon is not responding anymore. */
988 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
990 uint64_t key
= msg
.u
.destroy_channel
.key
;
991 struct lttng_consumer_channel
*channel
;
993 channel
= consumer_find_channel(key
);
995 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
996 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
999 health_code_update();
1001 ret
= consumer_send_status_msg(sock
, ret_code
);
1003 /* Somehow, the session daemon is not responding anymore. */
1004 goto end_destroy_channel
;
1007 health_code_update();
1009 /* Stop right now if no channel was found. */
1011 goto end_destroy_channel
;
1015 * This command should ONLY be issued for channel with streams set in
1018 assert(!channel
->monitor
);
1021 * The refcount should ALWAYS be 0 in the case of a channel in no
1024 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1026 consumer_del_channel(channel
);
1027 end_destroy_channel
:
1030 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1034 struct lttng_consumer_channel
*channel
;
1035 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1036 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1038 DBG("Kernel consumer discarded events command for session id %"
1039 PRIu64
", channel key %" PRIu64
, id
, key
);
1041 channel
= consumer_find_channel(key
);
1043 ERR("Kernel consumer discarded events channel %"
1044 PRIu64
" not found", key
);
1047 count
= channel
->discarded_events
;
1050 health_code_update();
1052 /* Send back returned value to session daemon */
1053 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1055 PERROR("send discarded events");
1061 case LTTNG_CONSUMER_LOST_PACKETS
:
1065 struct lttng_consumer_channel
*channel
;
1066 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1067 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1069 DBG("Kernel consumer lost packets command for session id %"
1070 PRIu64
", channel key %" PRIu64
, id
, key
);
1072 channel
= consumer_find_channel(key
);
1074 ERR("Kernel consumer lost packets channel %"
1075 PRIu64
" not found", key
);
1078 count
= channel
->lost_packets
;
1081 health_code_update();
1083 /* Send back returned value to session daemon */
1084 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1086 PERROR("send lost packets");
1092 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1094 int channel_monitor_pipe
;
1096 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1097 /* Successfully received the command's type. */
1098 ret
= consumer_send_status_msg(sock
, ret_code
);
1103 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1105 if (ret
!= sizeof(channel_monitor_pipe
)) {
1106 ERR("Failed to receive channel monitor pipe");
1110 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1111 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1112 channel_monitor_pipe
);
1116 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1117 /* Set the pipe as non-blocking. */
1118 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1120 PERROR("fcntl get flags of the channel monitoring pipe");
1125 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1126 flags
| O_NONBLOCK
);
1128 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1131 DBG("Channel monitor pipe set as non-blocking");
1133 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1135 ret
= consumer_send_status_msg(sock
, ret_code
);
1141 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1143 struct lttng_consumer_channel
*channel
;
1144 uint64_t key
= msg
.u
.rotate_channel
.key
;
1146 DBG("Consumer rotate channel %" PRIu64
, key
);
1148 channel
= consumer_find_channel(key
);
1150 ERR("Channel %" PRIu64
" not found", key
);
1151 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1154 * Sample the rotate position of all the streams in this channel.
1156 ret
= lttng_consumer_rotate_channel(channel
, key
,
1157 msg
.u
.rotate_channel
.relayd_id
,
1158 msg
.u
.rotate_channel
.metadata
,
1161 ERR("Rotate channel failed");
1162 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1165 health_code_update();
1167 ret
= consumer_send_status_msg(sock
, ret_code
);
1169 /* Somehow, the session daemon is not responding anymore. */
1170 goto error_rotate_channel
;
1173 /* Rotate the streams that are ready right now. */
1174 ret
= lttng_consumer_rotate_ready_streams(
1177 ERR("Rotate ready streams failed");
1181 error_rotate_channel
:
1184 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1186 struct lttng_consumer_channel
*channel
;
1187 uint64_t key
= msg
.u
.clear_channel
.key
;
1189 channel
= consumer_find_channel(key
);
1191 DBG("Channel %" PRIu64
" not found", key
);
1192 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1194 ret
= lttng_consumer_clear_channel(channel
);
1196 ERR("Clear channel failed");
1200 health_code_update();
1202 ret
= consumer_send_status_msg(sock
, ret_code
);
1204 /* Somehow, the session daemon is not responding anymore. */
1210 case LTTNG_CONSUMER_INIT
:
1212 ret_code
= lttng_consumer_init_command(ctx
,
1213 msg
.u
.init
.sessiond_uuid
);
1214 health_code_update();
1215 ret
= consumer_send_status_msg(sock
, ret_code
);
1217 /* Somehow, the session daemon is not responding anymore. */
1222 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1224 const struct lttng_credentials credentials
= {
1225 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1226 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1228 const bool is_local_trace
=
1229 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1230 const uint64_t relayd_id
=
1231 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1232 const char *chunk_override_name
=
1233 *msg
.u
.create_trace_chunk
.override_name
?
1234 msg
.u
.create_trace_chunk
.override_name
:
1236 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1239 * The session daemon will only provide a chunk directory file
1240 * descriptor for local traces.
1242 if (is_local_trace
) {
1245 /* Acnowledge the reception of the command. */
1246 ret
= consumer_send_status_msg(sock
,
1247 LTTCOMM_CONSUMERD_SUCCESS
);
1249 /* Somehow, the session daemon is not responding anymore. */
1253 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1254 if (ret
!= sizeof(chunk_dirfd
)) {
1255 ERR("Failed to receive trace chunk directory file descriptor");
1259 DBG("Received trace chunk directory fd (%d)",
1261 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1263 if (!chunk_directory_handle
) {
1264 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1265 if (close(chunk_dirfd
)) {
1266 PERROR("Failed to close chunk directory file descriptor");
1272 ret_code
= lttng_consumer_create_trace_chunk(
1273 !is_local_trace
? &relayd_id
: NULL
,
1274 msg
.u
.create_trace_chunk
.session_id
,
1275 msg
.u
.create_trace_chunk
.chunk_id
,
1276 (time_t) msg
.u
.create_trace_chunk
1277 .creation_timestamp
,
1278 chunk_override_name
,
1279 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1282 chunk_directory_handle
);
1283 lttng_directory_handle_put(chunk_directory_handle
);
1284 goto end_msg_sessiond
;
1286 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1288 enum lttng_trace_chunk_command_type close_command
=
1289 msg
.u
.close_trace_chunk
.close_command
.value
;
1290 const uint64_t relayd_id
=
1291 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1292 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1293 char path
[LTTNG_PATH_MAX
];
1295 ret_code
= lttng_consumer_close_trace_chunk(
1296 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1299 msg
.u
.close_trace_chunk
.session_id
,
1300 msg
.u
.close_trace_chunk
.chunk_id
,
1301 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1302 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1305 reply
.ret_code
= ret_code
;
1306 reply
.path_length
= strlen(path
) + 1;
1307 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1308 if (ret
!= sizeof(reply
)) {
1311 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1312 if (ret
!= reply
.path_length
) {
1317 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1319 const uint64_t relayd_id
=
1320 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1322 ret_code
= lttng_consumer_trace_chunk_exists(
1323 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1325 msg
.u
.trace_chunk_exists
.session_id
,
1326 msg
.u
.trace_chunk_exists
.chunk_id
);
1327 goto end_msg_sessiond
;
1335 * Return 1 to indicate success since the 0 value can be a socket
1336 * shutdown during the recv() or send() call.
1341 /* This will issue a consumer stop. */
1346 * The returned value here is not useful since either way we'll return 1 to
1347 * the caller because the session daemon socket management is done
1348 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1350 ret
= consumer_send_status_msg(sock
, ret_code
);
1356 health_code_update();
1362 * Populate index values of a kernel stream. Values are set in big endian order.
1364 * Return 0 on success or else a negative value.
1366 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1369 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1370 events_discarded
, stream_id
, stream_instance_id
,
1373 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1375 PERROR("kernctl_get_timestamp_begin");
1379 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1381 PERROR("kernctl_get_timestamp_end");
1385 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1387 PERROR("kernctl_get_events_discarded");
1391 ret
= kernctl_get_content_size(infd
, &content_size
);
1393 PERROR("kernctl_get_content_size");
1397 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1399 PERROR("kernctl_get_packet_size");
1403 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1405 PERROR("kernctl_get_stream_id");
1409 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1411 if (ret
== -ENOTTY
) {
1412 /* Command not implemented by lttng-modules. */
1413 stream_instance_id
= -1ULL;
1415 PERROR("kernctl_get_instance_id");
1420 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1422 if (ret
== -ENOTTY
) {
1423 /* Command not implemented by lttng-modules. */
1424 packet_seq_num
= -1ULL;
1427 PERROR("kernctl_get_sequence_number");
1431 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1433 *index
= (typeof(*index
)) {
1434 .offset
= index
->offset
,
1435 .packet_size
= htobe64(packet_size
),
1436 .content_size
= htobe64(content_size
),
1437 .timestamp_begin
= htobe64(timestamp_begin
),
1438 .timestamp_end
= htobe64(timestamp_end
),
1439 .events_discarded
= htobe64(events_discarded
),
1440 .stream_id
= htobe64(stream_id
),
1441 .stream_instance_id
= htobe64(stream_instance_id
),
1442 .packet_seq_num
= htobe64(packet_seq_num
),
1449 * Sync metadata meaning request them to the session daemon and snapshot to the
1450 * metadata thread can consumer them.
1452 * Metadata stream lock MUST be acquired.
1454 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1455 * is empty or a negative value on error.
1457 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1463 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1465 ERR("Failed to flush kernel stream");
1469 ret
= kernctl_snapshot(metadata
->wait_fd
);
1471 if (ret
!= -EAGAIN
) {
1472 ERR("Sync metadata, taking kernel snapshot failed.");
1475 DBG("Sync metadata, no new kernel metadata");
1476 /* No new metadata, exit. */
1486 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1489 uint64_t seq
, discarded
;
1491 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1493 if (ret
== -ENOTTY
) {
1494 /* Command not implemented by lttng-modules. */
1496 stream
->sequence_number_unavailable
= true;
1498 PERROR("kernctl_get_sequence_number");
1504 * Start the sequence when we extract the first packet in case we don't
1505 * start at 0 (for example if a consumer is not connected to the
1506 * session immediately after the beginning).
1508 if (stream
->last_sequence_number
== -1ULL) {
1509 stream
->last_sequence_number
= seq
;
1510 } else if (seq
> stream
->last_sequence_number
) {
1511 stream
->chan
->lost_packets
+= seq
-
1512 stream
->last_sequence_number
- 1;
1514 /* seq <= last_sequence_number */
1515 ERR("Sequence number inconsistent : prev = %" PRIu64
1516 ", current = %" PRIu64
,
1517 stream
->last_sequence_number
, seq
);
1521 stream
->last_sequence_number
= seq
;
1523 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1525 PERROR("kernctl_get_events_discarded");
1528 if (discarded
< stream
->last_discarded_events
) {
1530 * Overflow has occurred. We assume only one wrap-around
1533 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1534 stream
->last_discarded_events
+ discarded
;
1536 stream
->chan
->discarded_events
+= discarded
-
1537 stream
->last_discarded_events
;
1539 stream
->last_discarded_events
= discarded
;
1547 * Check if the local version of the metadata stream matches with the version
1548 * of the metadata stream in the kernel. If it was updated, set the reset flag
1552 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1555 uint64_t cur_version
;
1557 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1559 if (ret
== -ENOTTY
) {
1561 * LTTng-modules does not implement this
1567 ERR("Failed to get the metadata version");
1571 if (stream
->metadata_version
== cur_version
) {
1576 DBG("New metadata version detected");
1577 stream
->metadata_version
= cur_version
;
1578 stream
->reset_metadata_flag
= 1;
1586 * Consume data on a file descriptor and write it on a trace file.
1587 * The stream and channel locks must be held by the caller.
1589 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1590 struct lttng_consumer_local_data
*ctx
)
1592 unsigned long len
, subbuf_size
, padding
;
1593 int err
, write_index
= 1, rotation_ret
;
1595 int infd
= stream
->wait_fd
;
1596 struct ctf_packet_index index
= {};
1598 DBG("In read_subbuffer (infd : %d)", infd
);
1601 * If the stream was flagged to be ready for rotation before we extract the
1602 * next packet, rotate it now.
1604 if (stream
->rotate_ready
) {
1605 DBG("Rotate stream before extracting data");
1606 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1607 if (rotation_ret
< 0) {
1608 ERR("Stream rotation error");
1614 /* Get the next subbuffer */
1615 err
= kernctl_get_next_subbuf(infd
);
1618 * This is a debug message even for single-threaded consumer,
1619 * because poll() have more relaxed criterions than get subbuf,
1620 * so get_subbuf may fail for short race windows where poll()
1621 * would issue wakeups.
1623 DBG("Reserving sub buffer failed (everything is normal, "
1624 "it is due to concurrency)");
1629 /* Get the full subbuffer size including padding */
1630 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1632 PERROR("Getting sub-buffer len failed.");
1633 err
= kernctl_put_subbuf(infd
);
1635 if (err
== -EFAULT
) {
1636 PERROR("Error in unreserving sub buffer\n");
1637 } else if (err
== -EIO
) {
1638 /* Should never happen with newer LTTng versions */
1639 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1648 if (!stream
->metadata_flag
) {
1649 ret
= get_index_values(&index
, infd
);
1651 err
= kernctl_put_subbuf(infd
);
1653 if (err
== -EFAULT
) {
1654 PERROR("Error in unreserving sub buffer\n");
1655 } else if (err
== -EIO
) {
1656 /* Should never happen with newer LTTng versions */
1657 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1664 ret
= update_stream_stats(stream
);
1666 err
= kernctl_put_subbuf(infd
);
1668 if (err
== -EFAULT
) {
1669 PERROR("Error in unreserving sub buffer\n");
1670 } else if (err
== -EIO
) {
1671 /* Should never happen with newer LTTng versions */
1672 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1681 ret
= metadata_stream_check_version(infd
, stream
);
1683 err
= kernctl_put_subbuf(infd
);
1685 if (err
== -EFAULT
) {
1686 PERROR("Error in unreserving sub buffer\n");
1687 } else if (err
== -EIO
) {
1688 /* Should never happen with newer LTTng versions */
1689 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1698 switch (stream
->chan
->output
) {
1699 case CONSUMER_CHANNEL_SPLICE
:
1701 * XXX: The lttng-modules splice "actor" does not handle copying
1702 * partial pages hence only using the subbuffer size without the
1703 * padding makes the splice fail.
1708 /* splice the subbuffer to the tracefile */
1709 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1712 * XXX: Splice does not support network streaming so the return value
1713 * is simply checked against subbuf_size and not like the mmap() op.
1715 if (ret
!= subbuf_size
) {
1717 * display the error but continue processing to try
1718 * to release the subbuffer
1720 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1725 case CONSUMER_CHANNEL_MMAP
:
1727 const char *subbuf_addr
;
1728 struct lttng_buffer_view subbuf_view
;
1730 /* Get subbuffer size without padding */
1731 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1733 PERROR("Getting sub-buffer len failed.");
1734 err
= kernctl_put_subbuf(infd
);
1736 if (err
== -EFAULT
) {
1737 PERROR("Error in unreserving sub buffer\n");
1738 } else if (err
== -EIO
) {
1739 /* Should never happen with newer LTTng versions */
1740 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1749 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1751 goto error_put_subbuf
;
1754 /* Make sure the tracer is not gone mad on us! */
1755 assert(len
>= subbuf_size
);
1757 padding
= len
- subbuf_size
;
1759 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
1761 /* write the subbuffer to the tracefile */
1762 ret
= lttng_consumer_on_read_subbuffer_mmap(
1763 ctx
, stream
, &subbuf_view
, padding
, &index
);
1765 * The mmap operation should write subbuf_size amount of data
1766 * when network streaming or the full padding (len) size when we
1767 * are _not_ streaming.
1769 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1770 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1772 * Display the error but continue processing to try to release the
1773 * subbuffer. This is a DBG statement since this is possible to
1774 * happen without being a critical error.
1776 DBG("Error writing to tracefile "
1777 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1778 ret
, len
, subbuf_size
);
1784 ERR("Unknown output method");
1788 err
= kernctl_put_next_subbuf(infd
);
1790 if (err
== -EFAULT
) {
1791 PERROR("Error in unreserving sub buffer\n");
1792 } else if (err
== -EIO
) {
1793 /* Should never happen with newer LTTng versions */
1794 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1800 /* Write index if needed. */
1805 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1807 * In live, block until all the metadata is sent.
1809 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1810 assert(!stream
->missed_metadata_flush
);
1811 stream
->waiting_on_metadata
= true;
1812 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1814 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1816 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1817 stream
->waiting_on_metadata
= false;
1818 if (stream
->missed_metadata_flush
) {
1819 stream
->missed_metadata_flush
= false;
1820 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1821 (void) consumer_flush_kernel_index(stream
);
1823 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1830 err
= consumer_stream_write_index(stream
, &index
);
1837 * After extracting the packet, we check if the stream is now ready to be
1838 * rotated and perform the action immediately.
1840 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1841 if (rotation_ret
== 1) {
1842 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1843 if (rotation_ret
< 0) {
1844 ERR("Stream rotation error");
1848 } else if (rotation_ret
< 0) {
1849 ERR("Checking if stream is ready to rotate");
1858 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1865 * Don't create anything if this is set for streaming or if there is
1866 * no current trace chunk on the parent channel.
1868 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1869 stream
->chan
->trace_chunk
) {
1870 ret
= consumer_stream_create_output_files(stream
, true);
1876 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1877 /* get the len of the mmap region */
1878 unsigned long mmap_len
;
1880 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1882 PERROR("kernctl_get_mmap_len");
1883 goto error_close_fd
;
1885 stream
->mmap_len
= (size_t) mmap_len
;
1887 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1888 MAP_PRIVATE
, stream
->wait_fd
, 0);
1889 if (stream
->mmap_base
== MAP_FAILED
) {
1890 PERROR("Error mmaping");
1892 goto error_close_fd
;
1896 /* we return 0 to let the library handle the FD internally */
1900 if (stream
->out_fd
>= 0) {
1903 err
= close(stream
->out_fd
);
1905 stream
->out_fd
= -1;
1912 * Check if data is still being extracted from the buffers for a specific
1913 * stream. Consumer data lock MUST be acquired before calling this function
1914 * and the stream lock.
1916 * Return 1 if the traced data are still getting read else 0 meaning that the
1917 * data is available for trace viewer reading.
1919 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1925 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1930 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1932 /* There is still data so let's put back this subbuffer. */
1933 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1935 ret
= 1; /* Data is pending */
1939 /* Data is NOT pending and ready to be read. */