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
18 #include <sys/socket.h>
19 #include <sys/types.h>
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/sessiond-comm/relayd.h>
29 #include <common/compat/fcntl.h>
30 #include <common/compat/endian.h>
31 #include <common/pipe.h>
32 #include <common/relayd/relayd.h>
33 #include <common/utils.h>
34 #include <common/consumer/consumer-stream.h>
35 #include <common/index/index.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/optional.h>
39 #include "kernel-consumer.h"
41 extern struct lttng_consumer_global_data consumer_data
;
42 extern int consumer_poll_timeout
;
45 * Take a snapshot for a specific fd
47 * Returns 0 on success, < 0 on error
49 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
52 int infd
= stream
->wait_fd
;
54 ret
= kernctl_snapshot(infd
);
56 * -EAGAIN is not an error, it just means that there is no data to
59 if (ret
!= 0 && ret
!= -EAGAIN
) {
60 PERROR("Getting sub-buffer snapshot.");
67 * Sample consumed and produced positions for a specific fd.
69 * Returns 0 on success, < 0 on error.
71 int lttng_kconsumer_sample_snapshot_positions(
72 struct lttng_consumer_stream
*stream
)
76 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
80 * Get the produced position
82 * Returns 0 on success, < 0 on error
84 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
88 int infd
= stream
->wait_fd
;
90 ret
= kernctl_snapshot_get_produced(infd
, pos
);
92 PERROR("kernctl_snapshot_get_produced");
99 * Get the consumerd position
101 * Returns 0 on success, < 0 on error
103 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
107 int infd
= stream
->wait_fd
;
109 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
111 PERROR("kernctl_snapshot_get_consumed");
118 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
122 unsigned long mmap_offset
;
123 const char *mmap_base
= stream
->mmap_base
;
125 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
127 PERROR("Failed to get mmap read offset");
131 *addr
= mmap_base
+ mmap_offset
;
137 * Take a snapshot of all the stream of a channel
138 * RCU read-side lock must be held across this function to ensure existence of
139 * channel. The channel lock must be held by the caller.
141 * Returns 0 on success, < 0 on error
143 static int lttng_kconsumer_snapshot_channel(
144 struct lttng_consumer_channel
*channel
,
145 uint64_t key
, char *path
, uint64_t relayd_id
,
146 uint64_t nb_packets_per_stream
,
147 struct lttng_consumer_local_data
*ctx
)
150 struct lttng_consumer_stream
*stream
;
152 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
156 /* Splice is not supported yet for channel snapshot. */
157 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
158 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
164 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
165 unsigned long consumed_pos
, produced_pos
;
167 health_code_update();
170 * Lock stream because we are about to change its state.
172 pthread_mutex_lock(&stream
->lock
);
174 assert(channel
->trace_chunk
);
175 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
177 * Can't happen barring an internal error as the channel
178 * holds a reference to the trace chunk.
180 ERR("Failed to acquire reference to channel's trace chunk");
184 assert(!stream
->trace_chunk
);
185 stream
->trace_chunk
= channel
->trace_chunk
;
188 * Assign the received relayd ID so we can use it for streaming. The streams
189 * are not visible to anyone so this is OK to change it.
191 stream
->net_seq_idx
= relayd_id
;
192 channel
->relayd_id
= relayd_id
;
193 if (relayd_id
!= (uint64_t) -1ULL) {
194 ret
= consumer_send_relayd_stream(stream
, path
);
196 ERR("sending stream to relayd");
200 ret
= consumer_stream_create_output_files(stream
,
205 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
209 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
212 * Doing a buffer flush which does not take into
213 * account empty packets. This is not perfect
214 * for stream intersection, but required as a
215 * fall-back when "flush_empty" is not
216 * implemented by lttng-modules.
218 ret
= kernctl_buffer_flush(stream
->wait_fd
);
220 ERR("Failed to flush kernel stream");
226 ret
= lttng_kconsumer_take_snapshot(stream
);
228 ERR("Taking kernel snapshot");
232 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
234 ERR("Produced kernel snapshot position");
238 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
240 ERR("Consumerd kernel snapshot position");
244 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
245 produced_pos
, nb_packets_per_stream
,
246 stream
->max_sb_size
);
248 while ((long) (consumed_pos
- produced_pos
) < 0) {
250 unsigned long len
, padded_len
;
251 const char *subbuf_addr
;
253 health_code_update();
255 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
257 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
259 if (ret
!= -EAGAIN
) {
260 PERROR("kernctl_get_subbuf snapshot");
263 DBG("Kernel consumer get subbuf failed. Skipping it.");
264 consumed_pos
+= stream
->max_sb_size
;
265 stream
->chan
->lost_packets
++;
269 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
271 ERR("Snapshot kernctl_get_subbuf_size");
272 goto error_put_subbuf
;
275 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
277 ERR("Snapshot kernctl_get_padded_subbuf_size");
278 goto error_put_subbuf
;
281 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
283 goto error_put_subbuf
;
286 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
287 stream
, subbuf_addr
, len
,
288 padded_len
- len
, NULL
);
290 * We write the padded len in local tracefiles but the data len
291 * when using a relay. Display the error but continue processing
292 * to try to release the subbuffer.
294 if (relayd_id
!= (uint64_t) -1ULL) {
295 if (read_len
!= len
) {
296 ERR("Error sending to the relay (ret: %zd != len: %lu)",
300 if (read_len
!= padded_len
) {
301 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
302 read_len
, padded_len
);
306 ret
= kernctl_put_subbuf(stream
->wait_fd
);
308 ERR("Snapshot kernctl_put_subbuf");
311 consumed_pos
+= stream
->max_sb_size
;
314 if (relayd_id
== (uint64_t) -1ULL) {
315 if (stream
->out_fd
>= 0) {
316 ret
= close(stream
->out_fd
);
318 PERROR("Kernel consumer snapshot close out_fd");
324 close_relayd_stream(stream
);
325 stream
->net_seq_idx
= (uint64_t) -1ULL;
327 lttng_trace_chunk_put(stream
->trace_chunk
);
328 stream
->trace_chunk
= NULL
;
329 pthread_mutex_unlock(&stream
->lock
);
337 ret
= kernctl_put_subbuf(stream
->wait_fd
);
339 ERR("Snapshot kernctl_put_subbuf error path");
342 pthread_mutex_unlock(&stream
->lock
);
349 * Read the whole metadata available for a snapshot.
350 * RCU read-side lock must be held across this function to ensure existence of
351 * metadata_channel. The channel lock must be held by the caller.
353 * Returns 0 on success, < 0 on error
355 static int lttng_kconsumer_snapshot_metadata(
356 struct lttng_consumer_channel
*metadata_channel
,
357 uint64_t key
, char *path
, uint64_t relayd_id
,
358 struct lttng_consumer_local_data
*ctx
)
360 int ret
, use_relayd
= 0;
362 struct lttng_consumer_stream
*metadata_stream
;
366 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
371 metadata_stream
= metadata_channel
->metadata_stream
;
372 assert(metadata_stream
);
374 pthread_mutex_lock(&metadata_stream
->lock
);
375 assert(metadata_channel
->trace_chunk
);
376 assert(metadata_stream
->trace_chunk
);
378 /* Flag once that we have a valid relayd for the stream. */
379 if (relayd_id
!= (uint64_t) -1ULL) {
384 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
389 ret
= consumer_stream_create_output_files(metadata_stream
,
397 health_code_update();
399 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
401 if (ret_read
!= -EAGAIN
) {
402 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
407 /* ret_read is negative at this point so we will exit the loop. */
410 } while (ret_read
>= 0);
413 close_relayd_stream(metadata_stream
);
414 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
416 if (metadata_stream
->out_fd
>= 0) {
417 ret
= close(metadata_stream
->out_fd
);
419 PERROR("Kernel consumer snapshot metadata close out_fd");
421 * Don't go on error here since the snapshot was successful at this
422 * point but somehow the close failed.
425 metadata_stream
->out_fd
= -1;
426 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
427 metadata_stream
->trace_chunk
= NULL
;
433 pthread_mutex_unlock(&metadata_stream
->lock
);
434 cds_list_del(&metadata_stream
->send_node
);
435 consumer_stream_destroy(metadata_stream
, NULL
);
436 metadata_channel
->metadata_stream
= NULL
;
442 * Receive command from session daemon and process it.
444 * Return 1 on success else a negative value or 0.
446 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
447 int sock
, struct pollfd
*consumer_sockpoll
)
450 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
451 struct lttcomm_consumer_msg msg
;
453 health_code_update();
455 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
456 if (ret
!= sizeof(msg
)) {
458 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
464 health_code_update();
466 /* Deprecated command */
467 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
469 health_code_update();
471 /* relayd needs RCU read-side protection */
474 switch (msg
.cmd_type
) {
475 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
477 /* Session daemon status message are handled in the following call. */
478 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
479 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
480 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
481 msg
.u
.relayd_sock
.relayd_session_id
);
484 case LTTNG_CONSUMER_ADD_CHANNEL
:
486 struct lttng_consumer_channel
*new_channel
;
488 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
490 health_code_update();
492 /* First send a status message before receiving the fds. */
493 ret
= consumer_send_status_msg(sock
, ret_code
);
495 /* Somehow, the session daemon is not responding anymore. */
499 health_code_update();
501 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
502 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
503 msg
.u
.channel
.session_id
,
504 msg
.u
.channel
.chunk_id
.is_set
?
506 msg
.u
.channel
.pathname
,
508 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
509 msg
.u
.channel
.tracefile_size
,
510 msg
.u
.channel
.tracefile_count
, 0,
511 msg
.u
.channel
.monitor
,
512 msg
.u
.channel
.live_timer_interval
,
514 if (new_channel
== NULL
) {
515 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
518 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
519 switch (msg
.u
.channel
.output
) {
520 case LTTNG_EVENT_SPLICE
:
521 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
523 case LTTNG_EVENT_MMAP
:
524 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
527 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
531 /* Translate and save channel type. */
532 switch (msg
.u
.channel
.type
) {
533 case CONSUMER_CHANNEL_TYPE_DATA
:
534 case CONSUMER_CHANNEL_TYPE_METADATA
:
535 new_channel
->type
= msg
.u
.channel
.type
;
542 health_code_update();
544 if (ctx
->on_recv_channel
!= NULL
) {
545 ret_recv
= ctx
->on_recv_channel(new_channel
);
547 ret
= consumer_add_channel(new_channel
, ctx
);
548 } else if (ret_recv
< 0) {
552 ret
= consumer_add_channel(new_channel
, ctx
);
554 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
555 int monitor_start_ret
;
557 DBG("Consumer starting monitor timer");
558 consumer_timer_live_start(new_channel
,
559 msg
.u
.channel
.live_timer_interval
);
560 monitor_start_ret
= consumer_timer_monitor_start(
562 msg
.u
.channel
.monitor_timer_interval
);
563 if (monitor_start_ret
< 0) {
564 ERR("Starting channel monitoring timer failed");
570 health_code_update();
572 /* If we received an error in add_channel, we need to report it. */
574 ret
= consumer_send_status_msg(sock
, ret
);
583 case LTTNG_CONSUMER_ADD_STREAM
:
586 struct lttng_pipe
*stream_pipe
;
587 struct lttng_consumer_stream
*new_stream
;
588 struct lttng_consumer_channel
*channel
;
592 * Get stream's channel reference. Needed when adding the stream to the
595 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
598 * We could not find the channel. Can happen if cpu hotplug
599 * happens while tearing down.
601 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
602 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
605 health_code_update();
607 /* First send a status message before receiving the fds. */
608 ret
= consumer_send_status_msg(sock
, ret_code
);
610 /* Somehow, the session daemon is not responding anymore. */
611 goto error_add_stream_fatal
;
614 health_code_update();
616 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
617 /* Channel was not found. */
618 goto error_add_stream_nosignal
;
623 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
626 goto error_add_stream_fatal
;
629 health_code_update();
631 /* Get stream file descriptor from socket */
632 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
633 if (ret
!= sizeof(fd
)) {
634 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
638 health_code_update();
641 * Send status code to session daemon only if the recv works. If the
642 * above recv() failed, the session daemon is notified through the
643 * error socket and the teardown is eventually done.
645 ret
= consumer_send_status_msg(sock
, ret_code
);
647 /* Somehow, the session daemon is not responding anymore. */
648 goto error_add_stream_nosignal
;
651 health_code_update();
653 pthread_mutex_lock(&channel
->lock
);
654 new_stream
= consumer_allocate_stream(channel
->key
,
659 channel
->trace_chunk
,
664 if (new_stream
== NULL
) {
669 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
672 pthread_mutex_unlock(&channel
->lock
);
673 goto error_add_stream_nosignal
;
676 new_stream
->chan
= channel
;
677 new_stream
->wait_fd
= fd
;
678 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
679 &new_stream
->max_sb_size
);
681 pthread_mutex_unlock(&channel
->lock
);
682 ERR("Failed to get kernel maximal subbuffer size");
683 goto error_add_stream_nosignal
;
686 consumer_stream_update_channel_attributes(new_stream
,
688 switch (channel
->output
) {
689 case CONSUMER_CHANNEL_SPLICE
:
690 new_stream
->output
= LTTNG_EVENT_SPLICE
;
691 ret
= utils_create_pipe(new_stream
->splice_pipe
);
693 pthread_mutex_unlock(&channel
->lock
);
694 goto error_add_stream_nosignal
;
697 case CONSUMER_CHANNEL_MMAP
:
698 new_stream
->output
= LTTNG_EVENT_MMAP
;
701 ERR("Stream output unknown %d", channel
->output
);
702 pthread_mutex_unlock(&channel
->lock
);
703 goto error_add_stream_nosignal
;
707 * We've just assigned the channel to the stream so increment the
708 * refcount right now. We don't need to increment the refcount for
709 * streams in no monitor because we handle manually the cleanup of
710 * those. It is very important to make sure there is NO prior
711 * consumer_del_stream() calls or else the refcount will be unbalanced.
713 if (channel
->monitor
) {
714 uatomic_inc(&new_stream
->chan
->refcount
);
718 * The buffer flush is done on the session daemon side for the kernel
719 * so no need for the stream "hangup_flush_done" variable to be
720 * tracked. This is important for a kernel stream since we don't rely
721 * on the flush state of the stream to read data. It's not the case for
722 * user space tracing.
724 new_stream
->hangup_flush_done
= 0;
726 health_code_update();
728 pthread_mutex_lock(&new_stream
->lock
);
729 if (ctx
->on_recv_stream
) {
730 ret
= ctx
->on_recv_stream(new_stream
);
732 pthread_mutex_unlock(&new_stream
->lock
);
733 pthread_mutex_unlock(&channel
->lock
);
734 consumer_stream_free(new_stream
);
735 goto error_add_stream_nosignal
;
738 health_code_update();
740 if (new_stream
->metadata_flag
) {
741 channel
->metadata_stream
= new_stream
;
744 /* Do not monitor this stream. */
745 if (!channel
->monitor
) {
746 DBG("Kernel consumer add stream %s in no monitor mode with "
747 "relayd id %" PRIu64
, new_stream
->name
,
748 new_stream
->net_seq_idx
);
749 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
750 pthread_mutex_unlock(&new_stream
->lock
);
751 pthread_mutex_unlock(&channel
->lock
);
755 /* Send stream to relayd if the stream has an ID. */
756 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
757 ret
= consumer_send_relayd_stream(new_stream
,
758 new_stream
->chan
->pathname
);
760 pthread_mutex_unlock(&new_stream
->lock
);
761 pthread_mutex_unlock(&channel
->lock
);
762 consumer_stream_free(new_stream
);
763 goto error_add_stream_nosignal
;
767 * If adding an extra stream to an already
768 * existing channel (e.g. cpu hotplug), we need
769 * to send the "streams_sent" command to relayd.
771 if (channel
->streams_sent_to_relayd
) {
772 ret
= consumer_send_relayd_streams_sent(
773 new_stream
->net_seq_idx
);
775 pthread_mutex_unlock(&new_stream
->lock
);
776 pthread_mutex_unlock(&channel
->lock
);
777 goto error_add_stream_nosignal
;
781 pthread_mutex_unlock(&new_stream
->lock
);
782 pthread_mutex_unlock(&channel
->lock
);
784 /* Get the right pipe where the stream will be sent. */
785 if (new_stream
->metadata_flag
) {
786 consumer_add_metadata_stream(new_stream
);
787 stream_pipe
= ctx
->consumer_metadata_pipe
;
789 consumer_add_data_stream(new_stream
);
790 stream_pipe
= ctx
->consumer_data_pipe
;
793 /* Visible to other threads */
794 new_stream
->globally_visible
= 1;
796 health_code_update();
798 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
800 ERR("Consumer write %s stream to pipe %d",
801 new_stream
->metadata_flag
? "metadata" : "data",
802 lttng_pipe_get_writefd(stream_pipe
));
803 if (new_stream
->metadata_flag
) {
804 consumer_del_stream_for_metadata(new_stream
);
806 consumer_del_stream_for_data(new_stream
);
808 goto error_add_stream_nosignal
;
811 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
812 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
815 error_add_stream_nosignal
:
817 error_add_stream_fatal
:
820 case LTTNG_CONSUMER_STREAMS_SENT
:
822 struct lttng_consumer_channel
*channel
;
825 * Get stream's channel reference. Needed when adding the stream to the
828 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
831 * We could not find the channel. Can happen if cpu hotplug
832 * happens while tearing down.
834 ERR("Unable to find channel key %" PRIu64
,
835 msg
.u
.sent_streams
.channel_key
);
836 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
839 health_code_update();
842 * Send status code to session daemon.
844 ret
= consumer_send_status_msg(sock
, ret_code
);
845 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
846 /* Somehow, the session daemon is not responding anymore. */
847 goto error_streams_sent_nosignal
;
850 health_code_update();
853 * We should not send this message if we don't monitor the
854 * streams in this channel.
856 if (!channel
->monitor
) {
857 goto end_error_streams_sent
;
860 health_code_update();
861 /* Send stream to relayd if the stream has an ID. */
862 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
863 ret
= consumer_send_relayd_streams_sent(
864 msg
.u
.sent_streams
.net_seq_idx
);
866 goto error_streams_sent_nosignal
;
868 channel
->streams_sent_to_relayd
= true;
870 end_error_streams_sent
:
872 error_streams_sent_nosignal
:
875 case LTTNG_CONSUMER_UPDATE_STREAM
:
880 case LTTNG_CONSUMER_DESTROY_RELAYD
:
882 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
883 struct consumer_relayd_sock_pair
*relayd
;
885 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
887 /* Get relayd reference if exists. */
888 relayd
= consumer_find_relayd(index
);
889 if (relayd
== NULL
) {
890 DBG("Unable to find relayd %" PRIu64
, index
);
891 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
895 * Each relayd socket pair has a refcount of stream attached to it
896 * which tells if the relayd is still active or not depending on the
899 * This will set the destroy flag of the relayd object and destroy it
900 * if the refcount reaches zero when called.
902 * The destroy can happen either here or when a stream fd hangs up.
905 consumer_flag_relayd_for_destroy(relayd
);
908 health_code_update();
910 ret
= consumer_send_status_msg(sock
, ret_code
);
912 /* Somehow, the session daemon is not responding anymore. */
918 case LTTNG_CONSUMER_DATA_PENDING
:
921 uint64_t id
= msg
.u
.data_pending
.session_id
;
923 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
925 ret
= consumer_data_pending(id
);
927 health_code_update();
929 /* Send back returned value to session daemon */
930 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
932 PERROR("send data pending ret code");
937 * No need to send back a status message since the data pending
938 * returned value is the response.
942 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
944 struct lttng_consumer_channel
*channel
;
945 uint64_t key
= msg
.u
.snapshot_channel
.key
;
947 channel
= consumer_find_channel(key
);
949 ERR("Channel %" PRIu64
" not found", key
);
950 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
952 pthread_mutex_lock(&channel
->lock
);
953 if (msg
.u
.snapshot_channel
.metadata
== 1) {
954 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
955 msg
.u
.snapshot_channel
.pathname
,
956 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
958 ERR("Snapshot metadata failed");
959 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
962 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
963 msg
.u
.snapshot_channel
.pathname
,
964 msg
.u
.snapshot_channel
.relayd_id
,
965 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
968 ERR("Snapshot channel failed");
969 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
972 pthread_mutex_unlock(&channel
->lock
);
974 health_code_update();
976 ret
= consumer_send_status_msg(sock
, ret_code
);
978 /* Somehow, the session daemon is not responding anymore. */
983 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
985 uint64_t key
= msg
.u
.destroy_channel
.key
;
986 struct lttng_consumer_channel
*channel
;
988 channel
= consumer_find_channel(key
);
990 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
991 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
994 health_code_update();
996 ret
= consumer_send_status_msg(sock
, ret_code
);
998 /* Somehow, the session daemon is not responding anymore. */
999 goto end_destroy_channel
;
1002 health_code_update();
1004 /* Stop right now if no channel was found. */
1006 goto end_destroy_channel
;
1010 * This command should ONLY be issued for channel with streams set in
1013 assert(!channel
->monitor
);
1016 * The refcount should ALWAYS be 0 in the case of a channel in no
1019 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1021 consumer_del_channel(channel
);
1022 end_destroy_channel
:
1025 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1029 struct lttng_consumer_channel
*channel
;
1030 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1031 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1033 DBG("Kernel consumer discarded events command for session id %"
1034 PRIu64
", channel key %" PRIu64
, id
, key
);
1036 channel
= consumer_find_channel(key
);
1038 ERR("Kernel consumer discarded events channel %"
1039 PRIu64
" not found", key
);
1042 count
= channel
->discarded_events
;
1045 health_code_update();
1047 /* Send back returned value to session daemon */
1048 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1050 PERROR("send discarded events");
1056 case LTTNG_CONSUMER_LOST_PACKETS
:
1060 struct lttng_consumer_channel
*channel
;
1061 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1062 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1064 DBG("Kernel consumer lost packets command for session id %"
1065 PRIu64
", channel key %" PRIu64
, id
, key
);
1067 channel
= consumer_find_channel(key
);
1069 ERR("Kernel consumer lost packets channel %"
1070 PRIu64
" not found", key
);
1073 count
= channel
->lost_packets
;
1076 health_code_update();
1078 /* Send back returned value to session daemon */
1079 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1081 PERROR("send lost packets");
1087 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1089 int channel_monitor_pipe
;
1091 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1092 /* Successfully received the command's type. */
1093 ret
= consumer_send_status_msg(sock
, ret_code
);
1098 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1100 if (ret
!= sizeof(channel_monitor_pipe
)) {
1101 ERR("Failed to receive channel monitor pipe");
1105 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1106 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1107 channel_monitor_pipe
);
1111 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1112 /* Set the pipe as non-blocking. */
1113 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1115 PERROR("fcntl get flags of the channel monitoring pipe");
1120 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1121 flags
| O_NONBLOCK
);
1123 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1126 DBG("Channel monitor pipe set as non-blocking");
1128 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1130 ret
= consumer_send_status_msg(sock
, ret_code
);
1136 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1138 struct lttng_consumer_channel
*channel
;
1139 uint64_t key
= msg
.u
.rotate_channel
.key
;
1141 DBG("Consumer rotate channel %" PRIu64
, key
);
1143 channel
= consumer_find_channel(key
);
1145 ERR("Channel %" PRIu64
" not found", key
);
1146 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1149 * Sample the rotate position of all the streams in this channel.
1151 ret
= lttng_consumer_rotate_channel(channel
, key
,
1152 msg
.u
.rotate_channel
.relayd_id
,
1153 msg
.u
.rotate_channel
.metadata
,
1156 ERR("Rotate channel failed");
1157 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1160 health_code_update();
1162 ret
= consumer_send_status_msg(sock
, ret_code
);
1164 /* Somehow, the session daemon is not responding anymore. */
1165 goto error_rotate_channel
;
1168 /* Rotate the streams that are ready right now. */
1169 ret
= lttng_consumer_rotate_ready_streams(
1172 ERR("Rotate ready streams failed");
1176 error_rotate_channel
:
1179 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1181 struct lttng_consumer_channel
*channel
;
1182 uint64_t key
= msg
.u
.clear_channel
.key
;
1184 channel
= consumer_find_channel(key
);
1186 DBG("Channel %" PRIu64
" not found", key
);
1187 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1189 ret
= lttng_consumer_clear_channel(channel
);
1191 ERR("Clear channel failed");
1195 health_code_update();
1197 ret
= consumer_send_status_msg(sock
, ret_code
);
1199 /* Somehow, the session daemon is not responding anymore. */
1205 case LTTNG_CONSUMER_INIT
:
1207 ret_code
= lttng_consumer_init_command(ctx
,
1208 msg
.u
.init
.sessiond_uuid
);
1209 health_code_update();
1210 ret
= consumer_send_status_msg(sock
, ret_code
);
1212 /* Somehow, the session daemon is not responding anymore. */
1217 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1219 const struct lttng_credentials credentials
= {
1220 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1221 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1223 const bool is_local_trace
=
1224 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1225 const uint64_t relayd_id
=
1226 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1227 const char *chunk_override_name
=
1228 *msg
.u
.create_trace_chunk
.override_name
?
1229 msg
.u
.create_trace_chunk
.override_name
:
1231 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1234 * The session daemon will only provide a chunk directory file
1235 * descriptor for local traces.
1237 if (is_local_trace
) {
1240 /* Acnowledge the reception of the command. */
1241 ret
= consumer_send_status_msg(sock
,
1242 LTTCOMM_CONSUMERD_SUCCESS
);
1244 /* Somehow, the session daemon is not responding anymore. */
1248 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1249 if (ret
!= sizeof(chunk_dirfd
)) {
1250 ERR("Failed to receive trace chunk directory file descriptor");
1254 DBG("Received trace chunk directory fd (%d)",
1256 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1258 if (!chunk_directory_handle
) {
1259 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1260 if (close(chunk_dirfd
)) {
1261 PERROR("Failed to close chunk directory file descriptor");
1267 ret_code
= lttng_consumer_create_trace_chunk(
1268 !is_local_trace
? &relayd_id
: NULL
,
1269 msg
.u
.create_trace_chunk
.session_id
,
1270 msg
.u
.create_trace_chunk
.chunk_id
,
1271 (time_t) msg
.u
.create_trace_chunk
1272 .creation_timestamp
,
1273 chunk_override_name
,
1274 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1277 chunk_directory_handle
);
1278 lttng_directory_handle_put(chunk_directory_handle
);
1279 goto end_msg_sessiond
;
1281 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1283 enum lttng_trace_chunk_command_type close_command
=
1284 msg
.u
.close_trace_chunk
.close_command
.value
;
1285 const uint64_t relayd_id
=
1286 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1287 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1288 char path
[LTTNG_PATH_MAX
];
1290 ret_code
= lttng_consumer_close_trace_chunk(
1291 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1294 msg
.u
.close_trace_chunk
.session_id
,
1295 msg
.u
.close_trace_chunk
.chunk_id
,
1296 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1297 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1300 reply
.ret_code
= ret_code
;
1301 reply
.path_length
= strlen(path
) + 1;
1302 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1303 if (ret
!= sizeof(reply
)) {
1306 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1307 if (ret
!= reply
.path_length
) {
1312 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1314 const uint64_t relayd_id
=
1315 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1317 ret_code
= lttng_consumer_trace_chunk_exists(
1318 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1320 msg
.u
.trace_chunk_exists
.session_id
,
1321 msg
.u
.trace_chunk_exists
.chunk_id
);
1322 goto end_msg_sessiond
;
1330 * Return 1 to indicate success since the 0 value can be a socket
1331 * shutdown during the recv() or send() call.
1336 /* This will issue a consumer stop. */
1341 * The returned value here is not useful since either way we'll return 1 to
1342 * the caller because the session daemon socket management is done
1343 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1345 ret
= consumer_send_status_msg(sock
, ret_code
);
1351 health_code_update();
1357 * Populate index values of a kernel stream. Values are set in big endian order.
1359 * Return 0 on success or else a negative value.
1361 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1364 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1365 events_discarded
, stream_id
, stream_instance_id
,
1368 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1370 PERROR("kernctl_get_timestamp_begin");
1374 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1376 PERROR("kernctl_get_timestamp_end");
1380 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1382 PERROR("kernctl_get_events_discarded");
1386 ret
= kernctl_get_content_size(infd
, &content_size
);
1388 PERROR("kernctl_get_content_size");
1392 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1394 PERROR("kernctl_get_packet_size");
1398 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1400 PERROR("kernctl_get_stream_id");
1404 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1406 if (ret
== -ENOTTY
) {
1407 /* Command not implemented by lttng-modules. */
1408 stream_instance_id
= -1ULL;
1410 PERROR("kernctl_get_instance_id");
1415 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1417 if (ret
== -ENOTTY
) {
1418 /* Command not implemented by lttng-modules. */
1419 packet_seq_num
= -1ULL;
1422 PERROR("kernctl_get_sequence_number");
1426 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1428 *index
= (typeof(*index
)) {
1429 .offset
= index
->offset
,
1430 .packet_size
= htobe64(packet_size
),
1431 .content_size
= htobe64(content_size
),
1432 .timestamp_begin
= htobe64(timestamp_begin
),
1433 .timestamp_end
= htobe64(timestamp_end
),
1434 .events_discarded
= htobe64(events_discarded
),
1435 .stream_id
= htobe64(stream_id
),
1436 .stream_instance_id
= htobe64(stream_instance_id
),
1437 .packet_seq_num
= htobe64(packet_seq_num
),
1444 * Sync metadata meaning request them to the session daemon and snapshot to the
1445 * metadata thread can consumer them.
1447 * Metadata stream lock MUST be acquired.
1449 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1450 * is empty or a negative value on error.
1452 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1458 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1460 ERR("Failed to flush kernel stream");
1464 ret
= kernctl_snapshot(metadata
->wait_fd
);
1466 if (ret
!= -EAGAIN
) {
1467 ERR("Sync metadata, taking kernel snapshot failed.");
1470 DBG("Sync metadata, no new kernel metadata");
1471 /* No new metadata, exit. */
1481 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1484 uint64_t seq
, discarded
;
1486 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1488 if (ret
== -ENOTTY
) {
1489 /* Command not implemented by lttng-modules. */
1491 stream
->sequence_number_unavailable
= true;
1493 PERROR("kernctl_get_sequence_number");
1499 * Start the sequence when we extract the first packet in case we don't
1500 * start at 0 (for example if a consumer is not connected to the
1501 * session immediately after the beginning).
1503 if (stream
->last_sequence_number
== -1ULL) {
1504 stream
->last_sequence_number
= seq
;
1505 } else if (seq
> stream
->last_sequence_number
) {
1506 stream
->chan
->lost_packets
+= seq
-
1507 stream
->last_sequence_number
- 1;
1509 /* seq <= last_sequence_number */
1510 ERR("Sequence number inconsistent : prev = %" PRIu64
1511 ", current = %" PRIu64
,
1512 stream
->last_sequence_number
, seq
);
1516 stream
->last_sequence_number
= seq
;
1518 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1520 PERROR("kernctl_get_events_discarded");
1523 if (discarded
< stream
->last_discarded_events
) {
1525 * Overflow has occurred. We assume only one wrap-around
1528 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1529 stream
->last_discarded_events
+ discarded
;
1531 stream
->chan
->discarded_events
+= discarded
-
1532 stream
->last_discarded_events
;
1534 stream
->last_discarded_events
= discarded
;
1542 * Check if the local version of the metadata stream matches with the version
1543 * of the metadata stream in the kernel. If it was updated, set the reset flag
1547 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1550 uint64_t cur_version
;
1552 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1554 if (ret
== -ENOTTY
) {
1556 * LTTng-modules does not implement this
1562 ERR("Failed to get the metadata version");
1566 if (stream
->metadata_version
== cur_version
) {
1571 DBG("New metadata version detected");
1572 stream
->metadata_version
= cur_version
;
1573 stream
->reset_metadata_flag
= 1;
1581 * Consume data on a file descriptor and write it on a trace file.
1582 * The stream and channel locks must be held by the caller.
1584 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1585 struct lttng_consumer_local_data
*ctx
)
1587 unsigned long len
, subbuf_size
, padding
;
1588 int err
, write_index
= 1, rotation_ret
;
1590 int infd
= stream
->wait_fd
;
1591 struct ctf_packet_index index
= {};
1593 DBG("In read_subbuffer (infd : %d)", infd
);
1596 * If the stream was flagged to be ready for rotation before we extract the
1597 * next packet, rotate it now.
1599 if (stream
->rotate_ready
) {
1600 DBG("Rotate stream before extracting data");
1601 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1602 if (rotation_ret
< 0) {
1603 ERR("Stream rotation error");
1609 /* Get the next subbuffer */
1610 err
= kernctl_get_next_subbuf(infd
);
1613 * This is a debug message even for single-threaded consumer,
1614 * because poll() have more relaxed criterions than get subbuf,
1615 * so get_subbuf may fail for short race windows where poll()
1616 * would issue wakeups.
1618 DBG("Reserving sub buffer failed (everything is normal, "
1619 "it is due to concurrency)");
1624 /* Get the full subbuffer size including padding */
1625 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1627 PERROR("Getting sub-buffer len failed.");
1628 err
= kernctl_put_subbuf(infd
);
1630 if (err
== -EFAULT
) {
1631 PERROR("Error in unreserving sub buffer\n");
1632 } else if (err
== -EIO
) {
1633 /* Should never happen with newer LTTng versions */
1634 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1643 if (!stream
->metadata_flag
) {
1644 ret
= get_index_values(&index
, infd
);
1646 err
= kernctl_put_subbuf(infd
);
1648 if (err
== -EFAULT
) {
1649 PERROR("Error in unreserving sub buffer\n");
1650 } else if (err
== -EIO
) {
1651 /* Should never happen with newer LTTng versions */
1652 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1659 ret
= update_stream_stats(stream
);
1661 err
= kernctl_put_subbuf(infd
);
1663 if (err
== -EFAULT
) {
1664 PERROR("Error in unreserving sub buffer\n");
1665 } else if (err
== -EIO
) {
1666 /* Should never happen with newer LTTng versions */
1667 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1676 ret
= metadata_stream_check_version(infd
, stream
);
1678 err
= kernctl_put_subbuf(infd
);
1680 if (err
== -EFAULT
) {
1681 PERROR("Error in unreserving sub buffer\n");
1682 } else if (err
== -EIO
) {
1683 /* Should never happen with newer LTTng versions */
1684 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1693 switch (stream
->chan
->output
) {
1694 case CONSUMER_CHANNEL_SPLICE
:
1696 * XXX: The lttng-modules splice "actor" does not handle copying
1697 * partial pages hence only using the subbuffer size without the
1698 * padding makes the splice fail.
1703 /* splice the subbuffer to the tracefile */
1704 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1707 * XXX: Splice does not support network streaming so the return value
1708 * is simply checked against subbuf_size and not like the mmap() op.
1710 if (ret
!= subbuf_size
) {
1712 * display the error but continue processing to try
1713 * to release the subbuffer
1715 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1720 case CONSUMER_CHANNEL_MMAP
:
1722 const char *subbuf_addr
;
1724 /* Get subbuffer size without padding */
1725 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1727 PERROR("Getting sub-buffer len failed.");
1728 err
= kernctl_put_subbuf(infd
);
1730 if (err
== -EFAULT
) {
1731 PERROR("Error in unreserving sub buffer\n");
1732 } else if (err
== -EIO
) {
1733 /* Should never happen with newer LTTng versions */
1734 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1743 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1745 goto error_put_subbuf
;
1748 /* Make sure the tracer is not gone mad on us! */
1749 assert(len
>= subbuf_size
);
1751 padding
= len
- subbuf_size
;
1753 /* write the subbuffer to the tracefile */
1754 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
,
1759 * The mmap operation should write subbuf_size amount of data when
1760 * network streaming or the full padding (len) size when we are _not_
1763 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1764 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1766 * Display the error but continue processing to try to release the
1767 * subbuffer. This is a DBG statement since this is possible to
1768 * happen without being a critical error.
1770 DBG("Error writing to tracefile "
1771 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1772 ret
, len
, subbuf_size
);
1778 ERR("Unknown output method");
1782 err
= kernctl_put_next_subbuf(infd
);
1784 if (err
== -EFAULT
) {
1785 PERROR("Error in unreserving sub buffer\n");
1786 } else if (err
== -EIO
) {
1787 /* Should never happen with newer LTTng versions */
1788 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1794 /* Write index if needed. */
1799 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1801 * In live, block until all the metadata is sent.
1803 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1804 assert(!stream
->missed_metadata_flush
);
1805 stream
->waiting_on_metadata
= true;
1806 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1808 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1810 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1811 stream
->waiting_on_metadata
= false;
1812 if (stream
->missed_metadata_flush
) {
1813 stream
->missed_metadata_flush
= false;
1814 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1815 (void) consumer_flush_kernel_index(stream
);
1817 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1824 err
= consumer_stream_write_index(stream
, &index
);
1831 * After extracting the packet, we check if the stream is now ready to be
1832 * rotated and perform the action immediately.
1834 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1835 if (rotation_ret
== 1) {
1836 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1837 if (rotation_ret
< 0) {
1838 ERR("Stream rotation error");
1842 } else if (rotation_ret
< 0) {
1843 ERR("Checking if stream is ready to rotate");
1852 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1859 * Don't create anything if this is set for streaming or if there is
1860 * no current trace chunk on the parent channel.
1862 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1863 stream
->chan
->trace_chunk
) {
1864 ret
= consumer_stream_create_output_files(stream
, true);
1870 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1871 /* get the len of the mmap region */
1872 unsigned long mmap_len
;
1874 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1876 PERROR("kernctl_get_mmap_len");
1877 goto error_close_fd
;
1879 stream
->mmap_len
= (size_t) mmap_len
;
1881 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1882 MAP_PRIVATE
, stream
->wait_fd
, 0);
1883 if (stream
->mmap_base
== MAP_FAILED
) {
1884 PERROR("Error mmaping");
1886 goto error_close_fd
;
1890 /* we return 0 to let the library handle the FD internally */
1894 if (stream
->out_fd
>= 0) {
1897 err
= close(stream
->out_fd
);
1899 stream
->out_fd
= -1;
1906 * Check if data is still being extracted from the buffers for a specific
1907 * stream. Consumer data lock MUST be acquired before calling this function
1908 * and the stream lock.
1910 * Return 1 if the traced data are still getting read else 0 meaning that the
1911 * data is available for trace viewer reading.
1913 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1919 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1924 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1926 /* There is still data so let's put back this subbuffer. */
1927 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1929 ret
= 1; /* Data is pending */
1933 /* Data is NOT pending and ready to be read. */