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
,
517 if (new_channel
== NULL
) {
518 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
521 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
522 switch (msg
.u
.channel
.output
) {
523 case LTTNG_EVENT_SPLICE
:
524 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
526 case LTTNG_EVENT_MMAP
:
527 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
530 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
534 /* Translate and save channel type. */
535 switch (msg
.u
.channel
.type
) {
536 case CONSUMER_CHANNEL_TYPE_DATA
:
537 case CONSUMER_CHANNEL_TYPE_METADATA
:
538 new_channel
->type
= msg
.u
.channel
.type
;
545 health_code_update();
547 if (ctx
->on_recv_channel
!= NULL
) {
548 ret_recv
= ctx
->on_recv_channel(new_channel
);
550 ret
= consumer_add_channel(new_channel
, ctx
);
551 } else if (ret_recv
< 0) {
555 ret
= consumer_add_channel(new_channel
, ctx
);
557 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
558 int monitor_start_ret
;
560 DBG("Consumer starting monitor timer");
561 consumer_timer_live_start(new_channel
,
562 msg
.u
.channel
.live_timer_interval
);
563 monitor_start_ret
= consumer_timer_monitor_start(
565 msg
.u
.channel
.monitor_timer_interval
);
566 if (monitor_start_ret
< 0) {
567 ERR("Starting channel monitoring timer failed");
573 health_code_update();
575 /* If we received an error in add_channel, we need to report it. */
577 ret
= consumer_send_status_msg(sock
, ret
);
586 case LTTNG_CONSUMER_ADD_STREAM
:
589 struct lttng_pipe
*stream_pipe
;
590 struct lttng_consumer_stream
*new_stream
;
591 struct lttng_consumer_channel
*channel
;
595 * Get stream's channel reference. Needed when adding the stream to the
598 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
601 * We could not find the channel. Can happen if cpu hotplug
602 * happens while tearing down.
604 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
605 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
608 health_code_update();
610 /* First send a status message before receiving the fds. */
611 ret
= consumer_send_status_msg(sock
, ret_code
);
613 /* Somehow, the session daemon is not responding anymore. */
614 goto error_add_stream_fatal
;
617 health_code_update();
619 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
620 /* Channel was not found. */
621 goto error_add_stream_nosignal
;
626 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
629 goto error_add_stream_fatal
;
632 health_code_update();
634 /* Get stream file descriptor from socket */
635 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
636 if (ret
!= sizeof(fd
)) {
637 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
641 health_code_update();
644 * Send status code to session daemon only if the recv works. If the
645 * above recv() failed, the session daemon is notified through the
646 * error socket and the teardown is eventually done.
648 ret
= consumer_send_status_msg(sock
, ret_code
);
650 /* Somehow, the session daemon is not responding anymore. */
651 goto error_add_stream_nosignal
;
654 health_code_update();
656 pthread_mutex_lock(&channel
->lock
);
657 new_stream
= consumer_allocate_stream(channel
->key
,
662 channel
->trace_chunk
,
667 if (new_stream
== NULL
) {
672 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
675 pthread_mutex_unlock(&channel
->lock
);
676 goto error_add_stream_nosignal
;
679 new_stream
->chan
= channel
;
680 new_stream
->wait_fd
= fd
;
681 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
682 &new_stream
->max_sb_size
);
684 pthread_mutex_unlock(&channel
->lock
);
685 ERR("Failed to get kernel maximal subbuffer size");
686 goto error_add_stream_nosignal
;
689 consumer_stream_update_channel_attributes(new_stream
,
691 switch (channel
->output
) {
692 case CONSUMER_CHANNEL_SPLICE
:
693 new_stream
->output
= LTTNG_EVENT_SPLICE
;
694 ret
= utils_create_pipe(new_stream
->splice_pipe
);
696 pthread_mutex_unlock(&channel
->lock
);
697 goto error_add_stream_nosignal
;
700 case CONSUMER_CHANNEL_MMAP
:
701 new_stream
->output
= LTTNG_EVENT_MMAP
;
704 ERR("Stream output unknown %d", channel
->output
);
705 pthread_mutex_unlock(&channel
->lock
);
706 goto error_add_stream_nosignal
;
710 * We've just assigned the channel to the stream so increment the
711 * refcount right now. We don't need to increment the refcount for
712 * streams in no monitor because we handle manually the cleanup of
713 * those. It is very important to make sure there is NO prior
714 * consumer_del_stream() calls or else the refcount will be unbalanced.
716 if (channel
->monitor
) {
717 uatomic_inc(&new_stream
->chan
->refcount
);
721 * The buffer flush is done on the session daemon side for the kernel
722 * so no need for the stream "hangup_flush_done" variable to be
723 * tracked. This is important for a kernel stream since we don't rely
724 * on the flush state of the stream to read data. It's not the case for
725 * user space tracing.
727 new_stream
->hangup_flush_done
= 0;
729 health_code_update();
731 pthread_mutex_lock(&new_stream
->lock
);
732 if (ctx
->on_recv_stream
) {
733 ret
= ctx
->on_recv_stream(new_stream
);
735 pthread_mutex_unlock(&new_stream
->lock
);
736 pthread_mutex_unlock(&channel
->lock
);
737 consumer_stream_free(new_stream
);
738 goto error_add_stream_nosignal
;
741 health_code_update();
743 if (new_stream
->metadata_flag
) {
744 channel
->metadata_stream
= new_stream
;
747 /* Do not monitor this stream. */
748 if (!channel
->monitor
) {
749 DBG("Kernel consumer add stream %s in no monitor mode with "
750 "relayd id %" PRIu64
, new_stream
->name
,
751 new_stream
->net_seq_idx
);
752 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
753 pthread_mutex_unlock(&new_stream
->lock
);
754 pthread_mutex_unlock(&channel
->lock
);
758 /* Send stream to relayd if the stream has an ID. */
759 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
760 ret
= consumer_send_relayd_stream(new_stream
,
761 new_stream
->chan
->pathname
);
763 pthread_mutex_unlock(&new_stream
->lock
);
764 pthread_mutex_unlock(&channel
->lock
);
765 consumer_stream_free(new_stream
);
766 goto error_add_stream_nosignal
;
770 * If adding an extra stream to an already
771 * existing channel (e.g. cpu hotplug), we need
772 * to send the "streams_sent" command to relayd.
774 if (channel
->streams_sent_to_relayd
) {
775 ret
= consumer_send_relayd_streams_sent(
776 new_stream
->net_seq_idx
);
778 pthread_mutex_unlock(&new_stream
->lock
);
779 pthread_mutex_unlock(&channel
->lock
);
780 goto error_add_stream_nosignal
;
784 pthread_mutex_unlock(&new_stream
->lock
);
785 pthread_mutex_unlock(&channel
->lock
);
787 /* Get the right pipe where the stream will be sent. */
788 if (new_stream
->metadata_flag
) {
789 consumer_add_metadata_stream(new_stream
);
790 stream_pipe
= ctx
->consumer_metadata_pipe
;
792 consumer_add_data_stream(new_stream
);
793 stream_pipe
= ctx
->consumer_data_pipe
;
796 /* Visible to other threads */
797 new_stream
->globally_visible
= 1;
799 health_code_update();
801 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
803 ERR("Consumer write %s stream to pipe %d",
804 new_stream
->metadata_flag
? "metadata" : "data",
805 lttng_pipe_get_writefd(stream_pipe
));
806 if (new_stream
->metadata_flag
) {
807 consumer_del_stream_for_metadata(new_stream
);
809 consumer_del_stream_for_data(new_stream
);
811 goto error_add_stream_nosignal
;
814 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
815 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
818 error_add_stream_nosignal
:
820 error_add_stream_fatal
:
823 case LTTNG_CONSUMER_STREAMS_SENT
:
825 struct lttng_consumer_channel
*channel
;
828 * Get stream's channel reference. Needed when adding the stream to the
831 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
834 * We could not find the channel. Can happen if cpu hotplug
835 * happens while tearing down.
837 ERR("Unable to find channel key %" PRIu64
,
838 msg
.u
.sent_streams
.channel_key
);
839 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
842 health_code_update();
845 * Send status code to session daemon.
847 ret
= consumer_send_status_msg(sock
, ret_code
);
848 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
849 /* Somehow, the session daemon is not responding anymore. */
850 goto error_streams_sent_nosignal
;
853 health_code_update();
856 * We should not send this message if we don't monitor the
857 * streams in this channel.
859 if (!channel
->monitor
) {
860 goto end_error_streams_sent
;
863 health_code_update();
864 /* Send stream to relayd if the stream has an ID. */
865 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
866 ret
= consumer_send_relayd_streams_sent(
867 msg
.u
.sent_streams
.net_seq_idx
);
869 goto error_streams_sent_nosignal
;
871 channel
->streams_sent_to_relayd
= true;
873 end_error_streams_sent
:
875 error_streams_sent_nosignal
:
878 case LTTNG_CONSUMER_UPDATE_STREAM
:
883 case LTTNG_CONSUMER_DESTROY_RELAYD
:
885 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
886 struct consumer_relayd_sock_pair
*relayd
;
888 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
890 /* Get relayd reference if exists. */
891 relayd
= consumer_find_relayd(index
);
892 if (relayd
== NULL
) {
893 DBG("Unable to find relayd %" PRIu64
, index
);
894 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
898 * Each relayd socket pair has a refcount of stream attached to it
899 * which tells if the relayd is still active or not depending on the
902 * This will set the destroy flag of the relayd object and destroy it
903 * if the refcount reaches zero when called.
905 * The destroy can happen either here or when a stream fd hangs up.
908 consumer_flag_relayd_for_destroy(relayd
);
911 health_code_update();
913 ret
= consumer_send_status_msg(sock
, ret_code
);
915 /* Somehow, the session daemon is not responding anymore. */
921 case LTTNG_CONSUMER_DATA_PENDING
:
924 uint64_t id
= msg
.u
.data_pending
.session_id
;
926 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
928 ret
= consumer_data_pending(id
);
930 health_code_update();
932 /* Send back returned value to session daemon */
933 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
935 PERROR("send data pending ret code");
940 * No need to send back a status message since the data pending
941 * returned value is the response.
945 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
947 struct lttng_consumer_channel
*channel
;
948 uint64_t key
= msg
.u
.snapshot_channel
.key
;
950 channel
= consumer_find_channel(key
);
952 ERR("Channel %" PRIu64
" not found", key
);
953 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
955 pthread_mutex_lock(&channel
->lock
);
956 if (msg
.u
.snapshot_channel
.metadata
== 1) {
957 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
958 msg
.u
.snapshot_channel
.pathname
,
959 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
961 ERR("Snapshot metadata failed");
962 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
965 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
966 msg
.u
.snapshot_channel
.pathname
,
967 msg
.u
.snapshot_channel
.relayd_id
,
968 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
971 ERR("Snapshot channel failed");
972 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
975 pthread_mutex_unlock(&channel
->lock
);
977 health_code_update();
979 ret
= consumer_send_status_msg(sock
, ret_code
);
981 /* Somehow, the session daemon is not responding anymore. */
986 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
988 uint64_t key
= msg
.u
.destroy_channel
.key
;
989 struct lttng_consumer_channel
*channel
;
991 channel
= consumer_find_channel(key
);
993 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
994 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
997 health_code_update();
999 ret
= consumer_send_status_msg(sock
, ret_code
);
1001 /* Somehow, the session daemon is not responding anymore. */
1002 goto end_destroy_channel
;
1005 health_code_update();
1007 /* Stop right now if no channel was found. */
1009 goto end_destroy_channel
;
1013 * This command should ONLY be issued for channel with streams set in
1016 assert(!channel
->monitor
);
1019 * The refcount should ALWAYS be 0 in the case of a channel in no
1022 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1024 consumer_del_channel(channel
);
1025 end_destroy_channel
:
1028 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1032 struct lttng_consumer_channel
*channel
;
1033 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1034 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1036 DBG("Kernel consumer discarded events command for session id %"
1037 PRIu64
", channel key %" PRIu64
, id
, key
);
1039 channel
= consumer_find_channel(key
);
1041 ERR("Kernel consumer discarded events channel %"
1042 PRIu64
" not found", key
);
1045 count
= channel
->discarded_events
;
1048 health_code_update();
1050 /* Send back returned value to session daemon */
1051 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1053 PERROR("send discarded events");
1059 case LTTNG_CONSUMER_LOST_PACKETS
:
1063 struct lttng_consumer_channel
*channel
;
1064 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1065 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1067 DBG("Kernel consumer lost packets command for session id %"
1068 PRIu64
", channel key %" PRIu64
, id
, key
);
1070 channel
= consumer_find_channel(key
);
1072 ERR("Kernel consumer lost packets channel %"
1073 PRIu64
" not found", key
);
1076 count
= channel
->lost_packets
;
1079 health_code_update();
1081 /* Send back returned value to session daemon */
1082 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1084 PERROR("send lost packets");
1090 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1092 int channel_monitor_pipe
;
1094 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1095 /* Successfully received the command's type. */
1096 ret
= consumer_send_status_msg(sock
, ret_code
);
1101 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1103 if (ret
!= sizeof(channel_monitor_pipe
)) {
1104 ERR("Failed to receive channel monitor pipe");
1108 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1109 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1110 channel_monitor_pipe
);
1114 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1115 /* Set the pipe as non-blocking. */
1116 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1118 PERROR("fcntl get flags of the channel monitoring pipe");
1123 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1124 flags
| O_NONBLOCK
);
1126 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1129 DBG("Channel monitor pipe set as non-blocking");
1131 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1133 ret
= consumer_send_status_msg(sock
, ret_code
);
1139 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1141 struct lttng_consumer_channel
*channel
;
1142 uint64_t key
= msg
.u
.rotate_channel
.key
;
1144 DBG("Consumer rotate channel %" PRIu64
, key
);
1146 channel
= consumer_find_channel(key
);
1148 ERR("Channel %" PRIu64
" not found", key
);
1149 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1152 * Sample the rotate position of all the streams in this channel.
1154 ret
= lttng_consumer_rotate_channel(channel
, key
,
1155 msg
.u
.rotate_channel
.relayd_id
,
1156 msg
.u
.rotate_channel
.metadata
,
1159 ERR("Rotate channel failed");
1160 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1163 health_code_update();
1165 ret
= consumer_send_status_msg(sock
, ret_code
);
1167 /* Somehow, the session daemon is not responding anymore. */
1168 goto error_rotate_channel
;
1171 /* Rotate the streams that are ready right now. */
1172 ret
= lttng_consumer_rotate_ready_streams(
1175 ERR("Rotate ready streams failed");
1179 error_rotate_channel
:
1182 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1184 struct lttng_consumer_channel
*channel
;
1185 uint64_t key
= msg
.u
.clear_channel
.key
;
1187 channel
= consumer_find_channel(key
);
1189 DBG("Channel %" PRIu64
" not found", key
);
1190 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1192 ret
= lttng_consumer_clear_channel(channel
);
1194 ERR("Clear channel failed");
1198 health_code_update();
1200 ret
= consumer_send_status_msg(sock
, ret_code
);
1202 /* Somehow, the session daemon is not responding anymore. */
1208 case LTTNG_CONSUMER_INIT
:
1210 ret_code
= lttng_consumer_init_command(ctx
,
1211 msg
.u
.init
.sessiond_uuid
);
1212 health_code_update();
1213 ret
= consumer_send_status_msg(sock
, ret_code
);
1215 /* Somehow, the session daemon is not responding anymore. */
1220 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1222 const struct lttng_credentials credentials
= {
1223 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1224 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1226 const bool is_local_trace
=
1227 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1228 const uint64_t relayd_id
=
1229 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1230 const char *chunk_override_name
=
1231 *msg
.u
.create_trace_chunk
.override_name
?
1232 msg
.u
.create_trace_chunk
.override_name
:
1234 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1237 * The session daemon will only provide a chunk directory file
1238 * descriptor for local traces.
1240 if (is_local_trace
) {
1243 /* Acnowledge the reception of the command. */
1244 ret
= consumer_send_status_msg(sock
,
1245 LTTCOMM_CONSUMERD_SUCCESS
);
1247 /* Somehow, the session daemon is not responding anymore. */
1251 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1252 if (ret
!= sizeof(chunk_dirfd
)) {
1253 ERR("Failed to receive trace chunk directory file descriptor");
1257 DBG("Received trace chunk directory fd (%d)",
1259 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1261 if (!chunk_directory_handle
) {
1262 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1263 if (close(chunk_dirfd
)) {
1264 PERROR("Failed to close chunk directory file descriptor");
1270 ret_code
= lttng_consumer_create_trace_chunk(
1271 !is_local_trace
? &relayd_id
: NULL
,
1272 msg
.u
.create_trace_chunk
.session_id
,
1273 msg
.u
.create_trace_chunk
.chunk_id
,
1274 (time_t) msg
.u
.create_trace_chunk
1275 .creation_timestamp
,
1276 chunk_override_name
,
1277 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1280 chunk_directory_handle
);
1281 lttng_directory_handle_put(chunk_directory_handle
);
1282 goto end_msg_sessiond
;
1284 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1286 enum lttng_trace_chunk_command_type close_command
=
1287 msg
.u
.close_trace_chunk
.close_command
.value
;
1288 const uint64_t relayd_id
=
1289 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1290 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1291 char path
[LTTNG_PATH_MAX
];
1293 ret_code
= lttng_consumer_close_trace_chunk(
1294 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1297 msg
.u
.close_trace_chunk
.session_id
,
1298 msg
.u
.close_trace_chunk
.chunk_id
,
1299 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1300 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1303 reply
.ret_code
= ret_code
;
1304 reply
.path_length
= strlen(path
) + 1;
1305 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1306 if (ret
!= sizeof(reply
)) {
1309 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1310 if (ret
!= reply
.path_length
) {
1315 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1317 const uint64_t relayd_id
=
1318 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1320 ret_code
= lttng_consumer_trace_chunk_exists(
1321 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1323 msg
.u
.trace_chunk_exists
.session_id
,
1324 msg
.u
.trace_chunk_exists
.chunk_id
);
1325 goto end_msg_sessiond
;
1333 * Return 1 to indicate success since the 0 value can be a socket
1334 * shutdown during the recv() or send() call.
1339 /* This will issue a consumer stop. */
1344 * The returned value here is not useful since either way we'll return 1 to
1345 * the caller because the session daemon socket management is done
1346 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1348 ret
= consumer_send_status_msg(sock
, ret_code
);
1354 health_code_update();
1360 * Populate index values of a kernel stream. Values are set in big endian order.
1362 * Return 0 on success or else a negative value.
1364 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1367 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1368 events_discarded
, stream_id
, stream_instance_id
,
1371 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1373 PERROR("kernctl_get_timestamp_begin");
1377 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1379 PERROR("kernctl_get_timestamp_end");
1383 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1385 PERROR("kernctl_get_events_discarded");
1389 ret
= kernctl_get_content_size(infd
, &content_size
);
1391 PERROR("kernctl_get_content_size");
1395 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1397 PERROR("kernctl_get_packet_size");
1401 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1403 PERROR("kernctl_get_stream_id");
1407 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1409 if (ret
== -ENOTTY
) {
1410 /* Command not implemented by lttng-modules. */
1411 stream_instance_id
= -1ULL;
1413 PERROR("kernctl_get_instance_id");
1418 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1420 if (ret
== -ENOTTY
) {
1421 /* Command not implemented by lttng-modules. */
1422 packet_seq_num
= -1ULL;
1425 PERROR("kernctl_get_sequence_number");
1429 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1431 *index
= (typeof(*index
)) {
1432 .offset
= index
->offset
,
1433 .packet_size
= htobe64(packet_size
),
1434 .content_size
= htobe64(content_size
),
1435 .timestamp_begin
= htobe64(timestamp_begin
),
1436 .timestamp_end
= htobe64(timestamp_end
),
1437 .events_discarded
= htobe64(events_discarded
),
1438 .stream_id
= htobe64(stream_id
),
1439 .stream_instance_id
= htobe64(stream_instance_id
),
1440 .packet_seq_num
= htobe64(packet_seq_num
),
1447 * Sync metadata meaning request them to the session daemon and snapshot to the
1448 * metadata thread can consumer them.
1450 * Metadata stream lock MUST be acquired.
1452 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1453 * is empty or a negative value on error.
1455 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1461 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1463 ERR("Failed to flush kernel stream");
1467 ret
= kernctl_snapshot(metadata
->wait_fd
);
1469 if (ret
!= -EAGAIN
) {
1470 ERR("Sync metadata, taking kernel snapshot failed.");
1473 DBG("Sync metadata, no new kernel metadata");
1474 /* No new metadata, exit. */
1484 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1487 uint64_t seq
, discarded
;
1489 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1491 if (ret
== -ENOTTY
) {
1492 /* Command not implemented by lttng-modules. */
1494 stream
->sequence_number_unavailable
= true;
1496 PERROR("kernctl_get_sequence_number");
1502 * Start the sequence when we extract the first packet in case we don't
1503 * start at 0 (for example if a consumer is not connected to the
1504 * session immediately after the beginning).
1506 if (stream
->last_sequence_number
== -1ULL) {
1507 stream
->last_sequence_number
= seq
;
1508 } else if (seq
> stream
->last_sequence_number
) {
1509 stream
->chan
->lost_packets
+= seq
-
1510 stream
->last_sequence_number
- 1;
1512 /* seq <= last_sequence_number */
1513 ERR("Sequence number inconsistent : prev = %" PRIu64
1514 ", current = %" PRIu64
,
1515 stream
->last_sequence_number
, seq
);
1519 stream
->last_sequence_number
= seq
;
1521 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1523 PERROR("kernctl_get_events_discarded");
1526 if (discarded
< stream
->last_discarded_events
) {
1528 * Overflow has occurred. We assume only one wrap-around
1531 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1532 stream
->last_discarded_events
+ discarded
;
1534 stream
->chan
->discarded_events
+= discarded
-
1535 stream
->last_discarded_events
;
1537 stream
->last_discarded_events
= discarded
;
1545 * Check if the local version of the metadata stream matches with the version
1546 * of the metadata stream in the kernel. If it was updated, set the reset flag
1550 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1553 uint64_t cur_version
;
1555 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1557 if (ret
== -ENOTTY
) {
1559 * LTTng-modules does not implement this
1565 ERR("Failed to get the metadata version");
1569 if (stream
->metadata_version
== cur_version
) {
1574 DBG("New metadata version detected");
1575 stream
->metadata_version
= cur_version
;
1576 stream
->reset_metadata_flag
= 1;
1584 * Consume data on a file descriptor and write it on a trace file.
1585 * The stream and channel locks must be held by the caller.
1587 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1588 struct lttng_consumer_local_data
*ctx
)
1590 unsigned long len
, subbuf_size
, padding
;
1591 int err
, write_index
= 1, rotation_ret
;
1593 int infd
= stream
->wait_fd
;
1594 struct ctf_packet_index index
= {};
1596 DBG("In read_subbuffer (infd : %d)", infd
);
1599 * If the stream was flagged to be ready for rotation before we extract the
1600 * next packet, rotate it now.
1602 if (stream
->rotate_ready
) {
1603 DBG("Rotate stream before extracting data");
1604 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1605 if (rotation_ret
< 0) {
1606 ERR("Stream rotation error");
1612 /* Get the next subbuffer */
1613 err
= kernctl_get_next_subbuf(infd
);
1616 * This is a debug message even for single-threaded consumer,
1617 * because poll() have more relaxed criterions than get subbuf,
1618 * so get_subbuf may fail for short race windows where poll()
1619 * would issue wakeups.
1621 DBG("Reserving sub buffer failed (everything is normal, "
1622 "it is due to concurrency)");
1627 /* Get the full subbuffer size including padding */
1628 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1630 PERROR("Getting sub-buffer len failed.");
1631 err
= kernctl_put_subbuf(infd
);
1633 if (err
== -EFAULT
) {
1634 PERROR("Error in unreserving sub buffer\n");
1635 } else if (err
== -EIO
) {
1636 /* Should never happen with newer LTTng versions */
1637 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1646 if (!stream
->metadata_flag
) {
1647 ret
= get_index_values(&index
, infd
);
1649 err
= kernctl_put_subbuf(infd
);
1651 if (err
== -EFAULT
) {
1652 PERROR("Error in unreserving sub buffer\n");
1653 } else if (err
== -EIO
) {
1654 /* Should never happen with newer LTTng versions */
1655 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1662 ret
= update_stream_stats(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 ret
= metadata_stream_check_version(infd
, stream
);
1681 err
= kernctl_put_subbuf(infd
);
1683 if (err
== -EFAULT
) {
1684 PERROR("Error in unreserving sub buffer\n");
1685 } else if (err
== -EIO
) {
1686 /* Should never happen with newer LTTng versions */
1687 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1696 switch (stream
->chan
->output
) {
1697 case CONSUMER_CHANNEL_SPLICE
:
1699 * XXX: The lttng-modules splice "actor" does not handle copying
1700 * partial pages hence only using the subbuffer size without the
1701 * padding makes the splice fail.
1706 /* splice the subbuffer to the tracefile */
1707 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1710 * XXX: Splice does not support network streaming so the return value
1711 * is simply checked against subbuf_size and not like the mmap() op.
1713 if (ret
!= subbuf_size
) {
1715 * display the error but continue processing to try
1716 * to release the subbuffer
1718 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1723 case CONSUMER_CHANNEL_MMAP
:
1725 const char *subbuf_addr
;
1726 struct lttng_buffer_view subbuf_view
;
1728 /* Get subbuffer size without padding */
1729 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1731 PERROR("Getting sub-buffer len failed.");
1732 err
= kernctl_put_subbuf(infd
);
1734 if (err
== -EFAULT
) {
1735 PERROR("Error in unreserving sub buffer\n");
1736 } else if (err
== -EIO
) {
1737 /* Should never happen with newer LTTng versions */
1738 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1747 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1749 goto error_put_subbuf
;
1752 /* Make sure the tracer is not gone mad on us! */
1753 assert(len
>= subbuf_size
);
1755 padding
= len
- subbuf_size
;
1757 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
1759 /* write the subbuffer to the tracefile */
1760 ret
= lttng_consumer_on_read_subbuffer_mmap(
1761 ctx
, stream
, &subbuf_view
, padding
, &index
);
1763 * The mmap operation should write subbuf_size amount of data
1764 * when network streaming or the full padding (len) size when we
1765 * are _not_ streaming.
1767 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1768 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1770 * Display the error but continue processing to try to release the
1771 * subbuffer. This is a DBG statement since this is possible to
1772 * happen without being a critical error.
1774 DBG("Error writing to tracefile "
1775 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1776 ret
, len
, subbuf_size
);
1782 ERR("Unknown output method");
1786 err
= kernctl_put_next_subbuf(infd
);
1788 if (err
== -EFAULT
) {
1789 PERROR("Error in unreserving sub buffer\n");
1790 } else if (err
== -EIO
) {
1791 /* Should never happen with newer LTTng versions */
1792 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1798 /* Write index if needed. */
1803 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1805 * In live, block until all the metadata is sent.
1807 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1808 assert(!stream
->missed_metadata_flush
);
1809 stream
->waiting_on_metadata
= true;
1810 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1812 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1814 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1815 stream
->waiting_on_metadata
= false;
1816 if (stream
->missed_metadata_flush
) {
1817 stream
->missed_metadata_flush
= false;
1818 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1819 (void) consumer_flush_kernel_index(stream
);
1821 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1828 err
= consumer_stream_write_index(stream
, &index
);
1835 * After extracting the packet, we check if the stream is now ready to be
1836 * rotated and perform the action immediately.
1838 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1839 if (rotation_ret
== 1) {
1840 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1841 if (rotation_ret
< 0) {
1842 ERR("Stream rotation error");
1846 } else if (rotation_ret
< 0) {
1847 ERR("Checking if stream is ready to rotate");
1856 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1863 * Don't create anything if this is set for streaming or if there is
1864 * no current trace chunk on the parent channel.
1866 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1867 stream
->chan
->trace_chunk
) {
1868 ret
= consumer_stream_create_output_files(stream
, true);
1874 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1875 /* get the len of the mmap region */
1876 unsigned long mmap_len
;
1878 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1880 PERROR("kernctl_get_mmap_len");
1881 goto error_close_fd
;
1883 stream
->mmap_len
= (size_t) mmap_len
;
1885 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1886 MAP_PRIVATE
, stream
->wait_fd
, 0);
1887 if (stream
->mmap_base
== MAP_FAILED
) {
1888 PERROR("Error mmaping");
1890 goto error_close_fd
;
1894 /* we return 0 to let the library handle the FD internally */
1898 if (stream
->out_fd
>= 0) {
1901 err
= close(stream
->out_fd
);
1903 stream
->out_fd
= -1;
1910 * Check if data is still being extracted from the buffers for a specific
1911 * stream. Consumer data lock MUST be acquired before calling this function
1912 * and the stream lock.
1914 * Return 1 if the traced data are still getting read else 0 meaning that the
1915 * data is available for trace viewer reading.
1917 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1923 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1928 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1930 /* There is still data so let's put back this subbuffer. */
1931 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1933 ret
= 1; /* Data is pending */
1937 /* Data is NOT pending and ready to be read. */