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
17 #include <sys/socket.h>
18 #include <sys/types.h>
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <common/common.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/sessiond-comm/relayd.h>
28 #include <common/compat/fcntl.h>
29 #include <common/compat/endian.h>
30 #include <common/pipe.h>
31 #include <common/relayd/relayd.h>
32 #include <common/utils.h>
33 #include <common/consumer/consumer-stream.h>
34 #include <common/index/index.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/optional.h>
38 #include "kernel-consumer.h"
40 extern struct lttng_consumer_global_data consumer_data
;
41 extern int consumer_poll_timeout
;
44 * Take a snapshot for a specific fd
46 * Returns 0 on success, < 0 on error
48 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
51 int infd
= stream
->wait_fd
;
53 ret
= kernctl_snapshot(infd
);
55 * -EAGAIN is not an error, it just means that there is no data to
58 if (ret
!= 0 && ret
!= -EAGAIN
) {
59 PERROR("Getting sub-buffer snapshot.");
66 * Sample consumed and produced positions for a specific fd.
68 * Returns 0 on success, < 0 on error.
70 int lttng_kconsumer_sample_snapshot_positions(
71 struct lttng_consumer_stream
*stream
)
75 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
79 * Get the produced position
81 * Returns 0 on success, < 0 on error
83 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
87 int infd
= stream
->wait_fd
;
89 ret
= kernctl_snapshot_get_produced(infd
, pos
);
91 PERROR("kernctl_snapshot_get_produced");
98 * Get the consumerd position
100 * Returns 0 on success, < 0 on error
102 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
106 int infd
= stream
->wait_fd
;
108 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
110 PERROR("kernctl_snapshot_get_consumed");
117 * Take a snapshot of all the stream of a channel
118 * RCU read-side lock must be held across this function to ensure existence of
119 * channel. The channel lock must be held by the caller.
121 * Returns 0 on success, < 0 on error
123 static int lttng_kconsumer_snapshot_channel(
124 struct lttng_consumer_channel
*channel
,
125 uint64_t key
, char *path
, uint64_t relayd_id
,
126 uint64_t nb_packets_per_stream
,
127 struct lttng_consumer_local_data
*ctx
)
130 struct lttng_consumer_stream
*stream
;
132 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
138 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
144 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
145 unsigned long consumed_pos
, produced_pos
;
147 health_code_update();
150 * Lock stream because we are about to change its state.
152 pthread_mutex_lock(&stream
->lock
);
154 assert(channel
->trace_chunk
);
155 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
157 * Can't happen barring an internal error as the channel
158 * holds a reference to the trace chunk.
160 ERR("Failed to acquire reference to channel's trace chunk");
164 assert(!stream
->trace_chunk
);
165 stream
->trace_chunk
= channel
->trace_chunk
;
168 * Assign the received relayd ID so we can use it for streaming. The streams
169 * are not visible to anyone so this is OK to change it.
171 stream
->net_seq_idx
= relayd_id
;
172 channel
->relayd_id
= relayd_id
;
173 if (relayd_id
!= (uint64_t) -1ULL) {
174 ret
= consumer_send_relayd_stream(stream
, path
);
176 ERR("sending stream to relayd");
180 ret
= consumer_stream_create_output_files(stream
,
185 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
189 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
192 * Doing a buffer flush which does not take into
193 * account empty packets. This is not perfect
194 * for stream intersection, but required as a
195 * fall-back when "flush_empty" is not
196 * implemented by lttng-modules.
198 ret
= kernctl_buffer_flush(stream
->wait_fd
);
200 ERR("Failed to flush kernel stream");
206 ret
= lttng_kconsumer_take_snapshot(stream
);
208 ERR("Taking kernel snapshot");
212 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
214 ERR("Produced kernel snapshot position");
218 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
220 ERR("Consumerd kernel snapshot position");
224 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
225 produced_pos
, nb_packets_per_stream
,
226 stream
->max_sb_size
);
228 while ((long) (consumed_pos
- produced_pos
) < 0) {
230 unsigned long len
, padded_len
;
232 health_code_update();
234 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
236 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
238 if (ret
!= -EAGAIN
) {
239 PERROR("kernctl_get_subbuf snapshot");
242 DBG("Kernel consumer get subbuf failed. Skipping it.");
243 consumed_pos
+= stream
->max_sb_size
;
244 stream
->chan
->lost_packets
++;
248 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
250 ERR("Snapshot kernctl_get_subbuf_size");
251 goto error_put_subbuf
;
254 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
256 ERR("Snapshot kernctl_get_padded_subbuf_size");
257 goto error_put_subbuf
;
260 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
261 padded_len
- len
, NULL
);
263 * We write the padded len in local tracefiles but the data len
264 * when using a relay. Display the error but continue processing
265 * to try to release the subbuffer.
267 if (relayd_id
!= (uint64_t) -1ULL) {
268 if (read_len
!= len
) {
269 ERR("Error sending to the relay (ret: %zd != len: %lu)",
273 if (read_len
!= padded_len
) {
274 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
275 read_len
, padded_len
);
279 ret
= kernctl_put_subbuf(stream
->wait_fd
);
281 ERR("Snapshot kernctl_put_subbuf");
284 consumed_pos
+= stream
->max_sb_size
;
287 if (relayd_id
== (uint64_t) -1ULL) {
288 if (stream
->out_fd
>= 0) {
289 ret
= close(stream
->out_fd
);
291 PERROR("Kernel consumer snapshot close out_fd");
297 close_relayd_stream(stream
);
298 stream
->net_seq_idx
= (uint64_t) -1ULL;
300 lttng_trace_chunk_put(stream
->trace_chunk
);
301 stream
->trace_chunk
= NULL
;
302 pthread_mutex_unlock(&stream
->lock
);
310 ret
= kernctl_put_subbuf(stream
->wait_fd
);
312 ERR("Snapshot kernctl_put_subbuf error path");
315 pthread_mutex_unlock(&stream
->lock
);
322 * Read the whole metadata available for a snapshot.
323 * RCU read-side lock must be held across this function to ensure existence of
324 * metadata_channel. The channel lock must be held by the caller.
326 * Returns 0 on success, < 0 on error
328 static int lttng_kconsumer_snapshot_metadata(
329 struct lttng_consumer_channel
*metadata_channel
,
330 uint64_t key
, char *path
, uint64_t relayd_id
,
331 struct lttng_consumer_local_data
*ctx
)
333 int ret
, use_relayd
= 0;
335 struct lttng_consumer_stream
*metadata_stream
;
339 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
344 metadata_stream
= metadata_channel
->metadata_stream
;
345 assert(metadata_stream
);
347 pthread_mutex_lock(&metadata_stream
->lock
);
348 assert(metadata_channel
->trace_chunk
);
349 assert(metadata_stream
->trace_chunk
);
351 /* Flag once that we have a valid relayd for the stream. */
352 if (relayd_id
!= (uint64_t) -1ULL) {
357 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
362 ret
= consumer_stream_create_output_files(metadata_stream
,
370 health_code_update();
372 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
374 if (ret_read
!= -EAGAIN
) {
375 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
380 /* ret_read is negative at this point so we will exit the loop. */
383 } while (ret_read
>= 0);
386 close_relayd_stream(metadata_stream
);
387 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
389 if (metadata_stream
->out_fd
>= 0) {
390 ret
= close(metadata_stream
->out_fd
);
392 PERROR("Kernel consumer snapshot metadata close out_fd");
394 * Don't go on error here since the snapshot was successful at this
395 * point but somehow the close failed.
398 metadata_stream
->out_fd
= -1;
399 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
400 metadata_stream
->trace_chunk
= NULL
;
406 pthread_mutex_unlock(&metadata_stream
->lock
);
407 cds_list_del(&metadata_stream
->send_node
);
408 consumer_stream_destroy(metadata_stream
, NULL
);
409 metadata_channel
->metadata_stream
= NULL
;
415 * Receive command from session daemon and process it.
417 * Return 1 on success else a negative value or 0.
419 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
420 int sock
, struct pollfd
*consumer_sockpoll
)
423 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
424 struct lttcomm_consumer_msg msg
;
426 health_code_update();
428 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
429 if (ret
!= sizeof(msg
)) {
431 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
437 health_code_update();
439 /* Deprecated command */
440 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
442 health_code_update();
444 /* relayd needs RCU read-side protection */
447 switch (msg
.cmd_type
) {
448 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
450 /* Session daemon status message are handled in the following call. */
451 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
452 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
453 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
454 msg
.u
.relayd_sock
.relayd_session_id
);
457 case LTTNG_CONSUMER_ADD_CHANNEL
:
459 struct lttng_consumer_channel
*new_channel
;
461 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
463 health_code_update();
465 /* First send a status message before receiving the fds. */
466 ret
= consumer_send_status_msg(sock
, ret_code
);
468 /* Somehow, the session daemon is not responding anymore. */
472 health_code_update();
474 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
475 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
476 msg
.u
.channel
.session_id
,
477 msg
.u
.channel
.chunk_id
.is_set
?
479 msg
.u
.channel
.pathname
,
481 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
482 msg
.u
.channel
.tracefile_size
,
483 msg
.u
.channel
.tracefile_count
, 0,
484 msg
.u
.channel
.monitor
,
485 msg
.u
.channel
.live_timer_interval
,
487 if (new_channel
== NULL
) {
488 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
491 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
492 switch (msg
.u
.channel
.output
) {
493 case LTTNG_EVENT_SPLICE
:
494 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
496 case LTTNG_EVENT_MMAP
:
497 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
500 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
504 /* Translate and save channel type. */
505 switch (msg
.u
.channel
.type
) {
506 case CONSUMER_CHANNEL_TYPE_DATA
:
507 case CONSUMER_CHANNEL_TYPE_METADATA
:
508 new_channel
->type
= msg
.u
.channel
.type
;
515 health_code_update();
517 if (ctx
->on_recv_channel
!= NULL
) {
518 ret_recv
= ctx
->on_recv_channel(new_channel
);
520 ret
= consumer_add_channel(new_channel
, ctx
);
521 } else if (ret_recv
< 0) {
525 ret
= consumer_add_channel(new_channel
, ctx
);
527 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
528 int monitor_start_ret
;
530 DBG("Consumer starting monitor timer");
531 consumer_timer_live_start(new_channel
,
532 msg
.u
.channel
.live_timer_interval
);
533 monitor_start_ret
= consumer_timer_monitor_start(
535 msg
.u
.channel
.monitor_timer_interval
);
536 if (monitor_start_ret
< 0) {
537 ERR("Starting channel monitoring timer failed");
543 health_code_update();
545 /* If we received an error in add_channel, we need to report it. */
547 ret
= consumer_send_status_msg(sock
, ret
);
556 case LTTNG_CONSUMER_ADD_STREAM
:
559 struct lttng_pipe
*stream_pipe
;
560 struct lttng_consumer_stream
*new_stream
;
561 struct lttng_consumer_channel
*channel
;
565 * Get stream's channel reference. Needed when adding the stream to the
568 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
571 * We could not find the channel. Can happen if cpu hotplug
572 * happens while tearing down.
574 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
575 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
578 health_code_update();
580 /* First send a status message before receiving the fds. */
581 ret
= consumer_send_status_msg(sock
, ret_code
);
583 /* Somehow, the session daemon is not responding anymore. */
584 goto error_add_stream_fatal
;
587 health_code_update();
589 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
590 /* Channel was not found. */
591 goto error_add_stream_nosignal
;
596 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
599 goto error_add_stream_fatal
;
602 health_code_update();
604 /* Get stream file descriptor from socket */
605 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
606 if (ret
!= sizeof(fd
)) {
607 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
611 health_code_update();
614 * Send status code to session daemon only if the recv works. If the
615 * above recv() failed, the session daemon is notified through the
616 * error socket and the teardown is eventually done.
618 ret
= consumer_send_status_msg(sock
, ret_code
);
620 /* Somehow, the session daemon is not responding anymore. */
621 goto error_add_stream_nosignal
;
624 health_code_update();
626 pthread_mutex_lock(&channel
->lock
);
627 new_stream
= consumer_allocate_stream(channel
->key
,
632 channel
->trace_chunk
,
637 if (new_stream
== NULL
) {
642 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
645 pthread_mutex_unlock(&channel
->lock
);
646 goto error_add_stream_nosignal
;
649 new_stream
->chan
= channel
;
650 new_stream
->wait_fd
= fd
;
651 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
652 &new_stream
->max_sb_size
);
654 pthread_mutex_unlock(&channel
->lock
);
655 ERR("Failed to get kernel maximal subbuffer size");
656 goto error_add_stream_nosignal
;
659 consumer_stream_update_channel_attributes(new_stream
,
661 switch (channel
->output
) {
662 case CONSUMER_CHANNEL_SPLICE
:
663 new_stream
->output
= LTTNG_EVENT_SPLICE
;
664 ret
= utils_create_pipe(new_stream
->splice_pipe
);
666 pthread_mutex_unlock(&channel
->lock
);
667 goto error_add_stream_nosignal
;
670 case CONSUMER_CHANNEL_MMAP
:
671 new_stream
->output
= LTTNG_EVENT_MMAP
;
674 ERR("Stream output unknown %d", channel
->output
);
675 pthread_mutex_unlock(&channel
->lock
);
676 goto error_add_stream_nosignal
;
680 * We've just assigned the channel to the stream so increment the
681 * refcount right now. We don't need to increment the refcount for
682 * streams in no monitor because we handle manually the cleanup of
683 * those. It is very important to make sure there is NO prior
684 * consumer_del_stream() calls or else the refcount will be unbalanced.
686 if (channel
->monitor
) {
687 uatomic_inc(&new_stream
->chan
->refcount
);
691 * The buffer flush is done on the session daemon side for the kernel
692 * so no need for the stream "hangup_flush_done" variable to be
693 * tracked. This is important for a kernel stream since we don't rely
694 * on the flush state of the stream to read data. It's not the case for
695 * user space tracing.
697 new_stream
->hangup_flush_done
= 0;
699 health_code_update();
701 pthread_mutex_lock(&new_stream
->lock
);
702 if (ctx
->on_recv_stream
) {
703 ret
= ctx
->on_recv_stream(new_stream
);
705 pthread_mutex_unlock(&new_stream
->lock
);
706 pthread_mutex_unlock(&channel
->lock
);
707 consumer_stream_free(new_stream
);
708 goto error_add_stream_nosignal
;
711 health_code_update();
713 if (new_stream
->metadata_flag
) {
714 channel
->metadata_stream
= new_stream
;
717 /* Do not monitor this stream. */
718 if (!channel
->monitor
) {
719 DBG("Kernel consumer add stream %s in no monitor mode with "
720 "relayd id %" PRIu64
, new_stream
->name
,
721 new_stream
->net_seq_idx
);
722 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
723 pthread_mutex_unlock(&new_stream
->lock
);
724 pthread_mutex_unlock(&channel
->lock
);
728 /* Send stream to relayd if the stream has an ID. */
729 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
730 ret
= consumer_send_relayd_stream(new_stream
,
731 new_stream
->chan
->pathname
);
733 pthread_mutex_unlock(&new_stream
->lock
);
734 pthread_mutex_unlock(&channel
->lock
);
735 consumer_stream_free(new_stream
);
736 goto error_add_stream_nosignal
;
740 * If adding an extra stream to an already
741 * existing channel (e.g. cpu hotplug), we need
742 * to send the "streams_sent" command to relayd.
744 if (channel
->streams_sent_to_relayd
) {
745 ret
= consumer_send_relayd_streams_sent(
746 new_stream
->net_seq_idx
);
748 pthread_mutex_unlock(&new_stream
->lock
);
749 pthread_mutex_unlock(&channel
->lock
);
750 goto error_add_stream_nosignal
;
754 pthread_mutex_unlock(&new_stream
->lock
);
755 pthread_mutex_unlock(&channel
->lock
);
757 /* Get the right pipe where the stream will be sent. */
758 if (new_stream
->metadata_flag
) {
759 consumer_add_metadata_stream(new_stream
);
760 stream_pipe
= ctx
->consumer_metadata_pipe
;
762 consumer_add_data_stream(new_stream
);
763 stream_pipe
= ctx
->consumer_data_pipe
;
766 /* Visible to other threads */
767 new_stream
->globally_visible
= 1;
769 health_code_update();
771 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
773 ERR("Consumer write %s stream to pipe %d",
774 new_stream
->metadata_flag
? "metadata" : "data",
775 lttng_pipe_get_writefd(stream_pipe
));
776 if (new_stream
->metadata_flag
) {
777 consumer_del_stream_for_metadata(new_stream
);
779 consumer_del_stream_for_data(new_stream
);
781 goto error_add_stream_nosignal
;
784 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
785 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
788 error_add_stream_nosignal
:
790 error_add_stream_fatal
:
793 case LTTNG_CONSUMER_STREAMS_SENT
:
795 struct lttng_consumer_channel
*channel
;
798 * Get stream's channel reference. Needed when adding the stream to the
801 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
804 * We could not find the channel. Can happen if cpu hotplug
805 * happens while tearing down.
807 ERR("Unable to find channel key %" PRIu64
,
808 msg
.u
.sent_streams
.channel_key
);
809 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
812 health_code_update();
815 * Send status code to session daemon.
817 ret
= consumer_send_status_msg(sock
, ret_code
);
818 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
819 /* Somehow, the session daemon is not responding anymore. */
820 goto error_streams_sent_nosignal
;
823 health_code_update();
826 * We should not send this message if we don't monitor the
827 * streams in this channel.
829 if (!channel
->monitor
) {
830 goto end_error_streams_sent
;
833 health_code_update();
834 /* Send stream to relayd if the stream has an ID. */
835 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
836 ret
= consumer_send_relayd_streams_sent(
837 msg
.u
.sent_streams
.net_seq_idx
);
839 goto error_streams_sent_nosignal
;
841 channel
->streams_sent_to_relayd
= true;
843 end_error_streams_sent
:
845 error_streams_sent_nosignal
:
848 case LTTNG_CONSUMER_UPDATE_STREAM
:
853 case LTTNG_CONSUMER_DESTROY_RELAYD
:
855 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
856 struct consumer_relayd_sock_pair
*relayd
;
858 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
860 /* Get relayd reference if exists. */
861 relayd
= consumer_find_relayd(index
);
862 if (relayd
== NULL
) {
863 DBG("Unable to find relayd %" PRIu64
, index
);
864 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
868 * Each relayd socket pair has a refcount of stream attached to it
869 * which tells if the relayd is still active or not depending on the
872 * This will set the destroy flag of the relayd object and destroy it
873 * if the refcount reaches zero when called.
875 * The destroy can happen either here or when a stream fd hangs up.
878 consumer_flag_relayd_for_destroy(relayd
);
881 health_code_update();
883 ret
= consumer_send_status_msg(sock
, ret_code
);
885 /* Somehow, the session daemon is not responding anymore. */
891 case LTTNG_CONSUMER_DATA_PENDING
:
894 uint64_t id
= msg
.u
.data_pending
.session_id
;
896 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
898 ret
= consumer_data_pending(id
);
900 health_code_update();
902 /* Send back returned value to session daemon */
903 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
905 PERROR("send data pending ret code");
910 * No need to send back a status message since the data pending
911 * returned value is the response.
915 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
917 struct lttng_consumer_channel
*channel
;
918 uint64_t key
= msg
.u
.snapshot_channel
.key
;
920 channel
= consumer_find_channel(key
);
922 ERR("Channel %" PRIu64
" not found", key
);
923 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
925 pthread_mutex_lock(&channel
->lock
);
926 if (msg
.u
.snapshot_channel
.metadata
== 1) {
927 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
928 msg
.u
.snapshot_channel
.pathname
,
929 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
931 ERR("Snapshot metadata failed");
932 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
935 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
936 msg
.u
.snapshot_channel
.pathname
,
937 msg
.u
.snapshot_channel
.relayd_id
,
938 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
941 ERR("Snapshot channel failed");
942 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
945 pthread_mutex_unlock(&channel
->lock
);
947 health_code_update();
949 ret
= consumer_send_status_msg(sock
, ret_code
);
951 /* Somehow, the session daemon is not responding anymore. */
956 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
958 uint64_t key
= msg
.u
.destroy_channel
.key
;
959 struct lttng_consumer_channel
*channel
;
961 channel
= consumer_find_channel(key
);
963 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
964 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
967 health_code_update();
969 ret
= consumer_send_status_msg(sock
, ret_code
);
971 /* Somehow, the session daemon is not responding anymore. */
972 goto end_destroy_channel
;
975 health_code_update();
977 /* Stop right now if no channel was found. */
979 goto end_destroy_channel
;
983 * This command should ONLY be issued for channel with streams set in
986 assert(!channel
->monitor
);
989 * The refcount should ALWAYS be 0 in the case of a channel in no
992 assert(!uatomic_sub_return(&channel
->refcount
, 1));
994 consumer_del_channel(channel
);
998 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1002 struct lttng_consumer_channel
*channel
;
1003 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1004 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1006 DBG("Kernel consumer discarded events command for session id %"
1007 PRIu64
", channel key %" PRIu64
, id
, key
);
1009 channel
= consumer_find_channel(key
);
1011 ERR("Kernel consumer discarded events channel %"
1012 PRIu64
" not found", key
);
1015 count
= channel
->discarded_events
;
1018 health_code_update();
1020 /* Send back returned value to session daemon */
1021 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1023 PERROR("send discarded events");
1029 case LTTNG_CONSUMER_LOST_PACKETS
:
1033 struct lttng_consumer_channel
*channel
;
1034 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1035 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1037 DBG("Kernel consumer lost packets command for session id %"
1038 PRIu64
", channel key %" PRIu64
, id
, key
);
1040 channel
= consumer_find_channel(key
);
1042 ERR("Kernel consumer lost packets channel %"
1043 PRIu64
" not found", key
);
1046 count
= channel
->lost_packets
;
1049 health_code_update();
1051 /* Send back returned value to session daemon */
1052 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1054 PERROR("send lost packets");
1060 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1062 int channel_monitor_pipe
;
1064 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1065 /* Successfully received the command's type. */
1066 ret
= consumer_send_status_msg(sock
, ret_code
);
1071 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1073 if (ret
!= sizeof(channel_monitor_pipe
)) {
1074 ERR("Failed to receive channel monitor pipe");
1078 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1079 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1080 channel_monitor_pipe
);
1084 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1085 /* Set the pipe as non-blocking. */
1086 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1088 PERROR("fcntl get flags of the channel monitoring pipe");
1093 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1094 flags
| O_NONBLOCK
);
1096 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1099 DBG("Channel monitor pipe set as non-blocking");
1101 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1103 ret
= consumer_send_status_msg(sock
, ret_code
);
1109 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1111 struct lttng_consumer_channel
*channel
;
1112 uint64_t key
= msg
.u
.rotate_channel
.key
;
1114 DBG("Consumer rotate channel %" PRIu64
, key
);
1116 channel
= consumer_find_channel(key
);
1118 ERR("Channel %" PRIu64
" not found", key
);
1119 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1122 * Sample the rotate position of all the streams in this channel.
1124 ret
= lttng_consumer_rotate_channel(channel
, key
,
1125 msg
.u
.rotate_channel
.relayd_id
,
1126 msg
.u
.rotate_channel
.metadata
,
1129 ERR("Rotate channel failed");
1130 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1133 health_code_update();
1135 ret
= consumer_send_status_msg(sock
, ret_code
);
1137 /* Somehow, the session daemon is not responding anymore. */
1138 goto error_rotate_channel
;
1141 /* Rotate the streams that are ready right now. */
1142 ret
= lttng_consumer_rotate_ready_streams(
1145 ERR("Rotate ready streams failed");
1149 error_rotate_channel
:
1152 case LTTNG_CONSUMER_CLEAR_CHANNEL
:
1154 struct lttng_consumer_channel
*channel
;
1155 uint64_t key
= msg
.u
.clear_channel
.key
;
1157 channel
= consumer_find_channel(key
);
1159 DBG("Channel %" PRIu64
" not found", key
);
1160 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1162 ret
= lttng_consumer_clear_channel(channel
);
1164 ERR("Clear channel failed");
1168 health_code_update();
1170 ret
= consumer_send_status_msg(sock
, ret_code
);
1172 /* Somehow, the session daemon is not responding anymore. */
1178 case LTTNG_CONSUMER_INIT
:
1180 ret_code
= lttng_consumer_init_command(ctx
,
1181 msg
.u
.init
.sessiond_uuid
);
1182 health_code_update();
1183 ret
= consumer_send_status_msg(sock
, ret_code
);
1185 /* Somehow, the session daemon is not responding anymore. */
1190 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1192 const struct lttng_credentials credentials
= {
1193 .uid
= msg
.u
.create_trace_chunk
.credentials
.value
.uid
,
1194 .gid
= msg
.u
.create_trace_chunk
.credentials
.value
.gid
,
1196 const bool is_local_trace
=
1197 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1198 const uint64_t relayd_id
=
1199 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1200 const char *chunk_override_name
=
1201 *msg
.u
.create_trace_chunk
.override_name
?
1202 msg
.u
.create_trace_chunk
.override_name
:
1204 struct lttng_directory_handle
*chunk_directory_handle
= NULL
;
1207 * The session daemon will only provide a chunk directory file
1208 * descriptor for local traces.
1210 if (is_local_trace
) {
1213 /* Acnowledge the reception of the command. */
1214 ret
= consumer_send_status_msg(sock
,
1215 LTTCOMM_CONSUMERD_SUCCESS
);
1217 /* Somehow, the session daemon is not responding anymore. */
1221 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1222 if (ret
!= sizeof(chunk_dirfd
)) {
1223 ERR("Failed to receive trace chunk directory file descriptor");
1227 DBG("Received trace chunk directory fd (%d)",
1229 chunk_directory_handle
= lttng_directory_handle_create_from_dirfd(
1231 if (!chunk_directory_handle
) {
1232 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1233 if (close(chunk_dirfd
)) {
1234 PERROR("Failed to close chunk directory file descriptor");
1240 ret_code
= lttng_consumer_create_trace_chunk(
1241 !is_local_trace
? &relayd_id
: NULL
,
1242 msg
.u
.create_trace_chunk
.session_id
,
1243 msg
.u
.create_trace_chunk
.chunk_id
,
1244 (time_t) msg
.u
.create_trace_chunk
1245 .creation_timestamp
,
1246 chunk_override_name
,
1247 msg
.u
.create_trace_chunk
.credentials
.is_set
?
1250 chunk_directory_handle
);
1251 lttng_directory_handle_put(chunk_directory_handle
);
1252 goto end_msg_sessiond
;
1254 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1256 enum lttng_trace_chunk_command_type close_command
=
1257 msg
.u
.close_trace_chunk
.close_command
.value
;
1258 const uint64_t relayd_id
=
1259 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1260 struct lttcomm_consumer_close_trace_chunk_reply reply
;
1261 char path
[LTTNG_PATH_MAX
];
1263 ret_code
= lttng_consumer_close_trace_chunk(
1264 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1267 msg
.u
.close_trace_chunk
.session_id
,
1268 msg
.u
.close_trace_chunk
.chunk_id
,
1269 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
,
1270 msg
.u
.close_trace_chunk
.close_command
.is_set
?
1273 reply
.ret_code
= ret_code
;
1274 reply
.path_length
= strlen(path
) + 1;
1275 ret
= lttcomm_send_unix_sock(sock
, &reply
, sizeof(reply
));
1276 if (ret
!= sizeof(reply
)) {
1279 ret
= lttcomm_send_unix_sock(sock
, path
, reply
.path_length
);
1280 if (ret
!= reply
.path_length
) {
1285 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1287 const uint64_t relayd_id
=
1288 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1290 ret_code
= lttng_consumer_trace_chunk_exists(
1291 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1293 msg
.u
.trace_chunk_exists
.session_id
,
1294 msg
.u
.trace_chunk_exists
.chunk_id
);
1295 goto end_msg_sessiond
;
1303 * Return 1 to indicate success since the 0 value can be a socket
1304 * shutdown during the recv() or send() call.
1309 /* This will issue a consumer stop. */
1314 * The returned value here is not useful since either way we'll return 1 to
1315 * the caller because the session daemon socket management is done
1316 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1318 ret
= consumer_send_status_msg(sock
, ret_code
);
1324 health_code_update();
1330 * Populate index values of a kernel stream. Values are set in big endian order.
1332 * Return 0 on success or else a negative value.
1334 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1337 uint64_t packet_size
, content_size
, timestamp_begin
, timestamp_end
,
1338 events_discarded
, stream_id
, stream_instance_id
,
1341 ret
= kernctl_get_timestamp_begin(infd
, ×tamp_begin
);
1343 PERROR("kernctl_get_timestamp_begin");
1347 ret
= kernctl_get_timestamp_end(infd
, ×tamp_end
);
1349 PERROR("kernctl_get_timestamp_end");
1353 ret
= kernctl_get_events_discarded(infd
, &events_discarded
);
1355 PERROR("kernctl_get_events_discarded");
1359 ret
= kernctl_get_content_size(infd
, &content_size
);
1361 PERROR("kernctl_get_content_size");
1365 ret
= kernctl_get_packet_size(infd
, &packet_size
);
1367 PERROR("kernctl_get_packet_size");
1371 ret
= kernctl_get_stream_id(infd
, &stream_id
);
1373 PERROR("kernctl_get_stream_id");
1377 ret
= kernctl_get_instance_id(infd
, &stream_instance_id
);
1379 if (ret
== -ENOTTY
) {
1380 /* Command not implemented by lttng-modules. */
1381 stream_instance_id
= -1ULL;
1383 PERROR("kernctl_get_instance_id");
1388 ret
= kernctl_get_sequence_number(infd
, &packet_seq_num
);
1390 if (ret
== -ENOTTY
) {
1391 /* Command not implemented by lttng-modules. */
1392 packet_seq_num
= -1ULL;
1395 PERROR("kernctl_get_sequence_number");
1399 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1401 *index
= (typeof(*index
)) {
1402 .offset
= index
->offset
,
1403 .packet_size
= htobe64(packet_size
),
1404 .content_size
= htobe64(content_size
),
1405 .timestamp_begin
= htobe64(timestamp_begin
),
1406 .timestamp_end
= htobe64(timestamp_end
),
1407 .events_discarded
= htobe64(events_discarded
),
1408 .stream_id
= htobe64(stream_id
),
1409 .stream_instance_id
= htobe64(stream_instance_id
),
1410 .packet_seq_num
= htobe64(packet_seq_num
),
1417 * Sync metadata meaning request them to the session daemon and snapshot to the
1418 * metadata thread can consumer them.
1420 * Metadata stream lock MUST be acquired.
1422 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1423 * is empty or a negative value on error.
1425 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1431 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1433 ERR("Failed to flush kernel stream");
1437 ret
= kernctl_snapshot(metadata
->wait_fd
);
1439 if (ret
!= -EAGAIN
) {
1440 ERR("Sync metadata, taking kernel snapshot failed.");
1443 DBG("Sync metadata, no new kernel metadata");
1444 /* No new metadata, exit. */
1454 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1457 uint64_t seq
, discarded
;
1459 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1461 if (ret
== -ENOTTY
) {
1462 /* Command not implemented by lttng-modules. */
1464 stream
->sequence_number_unavailable
= true;
1466 PERROR("kernctl_get_sequence_number");
1472 * Start the sequence when we extract the first packet in case we don't
1473 * start at 0 (for example if a consumer is not connected to the
1474 * session immediately after the beginning).
1476 if (stream
->last_sequence_number
== -1ULL) {
1477 stream
->last_sequence_number
= seq
;
1478 } else if (seq
> stream
->last_sequence_number
) {
1479 stream
->chan
->lost_packets
+= seq
-
1480 stream
->last_sequence_number
- 1;
1482 /* seq <= last_sequence_number */
1483 ERR("Sequence number inconsistent : prev = %" PRIu64
1484 ", current = %" PRIu64
,
1485 stream
->last_sequence_number
, seq
);
1489 stream
->last_sequence_number
= seq
;
1491 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1493 PERROR("kernctl_get_events_discarded");
1496 if (discarded
< stream
->last_discarded_events
) {
1498 * Overflow has occurred. We assume only one wrap-around
1501 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1502 stream
->last_discarded_events
+ discarded
;
1504 stream
->chan
->discarded_events
+= discarded
-
1505 stream
->last_discarded_events
;
1507 stream
->last_discarded_events
= discarded
;
1515 * Check if the local version of the metadata stream matches with the version
1516 * of the metadata stream in the kernel. If it was updated, set the reset flag
1520 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1523 uint64_t cur_version
;
1525 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1527 if (ret
== -ENOTTY
) {
1529 * LTTng-modules does not implement this
1535 ERR("Failed to get the metadata version");
1539 if (stream
->metadata_version
== cur_version
) {
1544 DBG("New metadata version detected");
1545 stream
->metadata_version
= cur_version
;
1546 stream
->reset_metadata_flag
= 1;
1554 * Consume data on a file descriptor and write it on a trace file.
1555 * The stream and channel locks must be held by the caller.
1557 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1558 struct lttng_consumer_local_data
*ctx
)
1560 unsigned long len
, subbuf_size
, padding
;
1561 int err
, write_index
= 1, rotation_ret
;
1563 int infd
= stream
->wait_fd
;
1564 struct ctf_packet_index index
= {};
1566 DBG("In read_subbuffer (infd : %d)", infd
);
1569 * If the stream was flagged to be ready for rotation before we extract the
1570 * next packet, rotate it now.
1572 if (stream
->rotate_ready
) {
1573 DBG("Rotate stream before extracting data");
1574 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1575 if (rotation_ret
< 0) {
1576 ERR("Stream rotation error");
1582 /* Get the next subbuffer */
1583 err
= kernctl_get_next_subbuf(infd
);
1586 * This is a debug message even for single-threaded consumer,
1587 * because poll() have more relaxed criterions than get subbuf,
1588 * so get_subbuf may fail for short race windows where poll()
1589 * would issue wakeups.
1591 DBG("Reserving sub buffer failed (everything is normal, "
1592 "it is due to concurrency)");
1597 /* Get the full subbuffer size including padding */
1598 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1600 PERROR("Getting sub-buffer len failed.");
1601 err
= kernctl_put_subbuf(infd
);
1603 if (err
== -EFAULT
) {
1604 PERROR("Error in unreserving sub buffer\n");
1605 } else if (err
== -EIO
) {
1606 /* Should never happen with newer LTTng versions */
1607 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1616 if (!stream
->metadata_flag
) {
1617 ret
= get_index_values(&index
, infd
);
1619 err
= kernctl_put_subbuf(infd
);
1621 if (err
== -EFAULT
) {
1622 PERROR("Error in unreserving sub buffer\n");
1623 } else if (err
== -EIO
) {
1624 /* Should never happen with newer LTTng versions */
1625 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1632 ret
= update_stream_stats(stream
);
1634 err
= kernctl_put_subbuf(infd
);
1636 if (err
== -EFAULT
) {
1637 PERROR("Error in unreserving sub buffer\n");
1638 } else if (err
== -EIO
) {
1639 /* Should never happen with newer LTTng versions */
1640 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1649 ret
= metadata_stream_check_version(infd
, stream
);
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.");
1666 switch (stream
->chan
->output
) {
1667 case CONSUMER_CHANNEL_SPLICE
:
1669 * XXX: The lttng-modules splice "actor" does not handle copying
1670 * partial pages hence only using the subbuffer size without the
1671 * padding makes the splice fail.
1676 /* splice the subbuffer to the tracefile */
1677 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1680 * XXX: Splice does not support network streaming so the return value
1681 * is simply checked against subbuf_size and not like the mmap() op.
1683 if (ret
!= subbuf_size
) {
1685 * display the error but continue processing to try
1686 * to release the subbuffer
1688 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1693 case CONSUMER_CHANNEL_MMAP
:
1694 /* Get subbuffer size without padding */
1695 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1697 PERROR("Getting sub-buffer len failed.");
1698 err
= kernctl_put_subbuf(infd
);
1700 if (err
== -EFAULT
) {
1701 PERROR("Error in unreserving sub buffer\n");
1702 } else if (err
== -EIO
) {
1703 /* Should never happen with newer LTTng versions */
1704 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1713 /* Make sure the tracer is not gone mad on us! */
1714 assert(len
>= subbuf_size
);
1716 padding
= len
- subbuf_size
;
1718 /* write the subbuffer to the tracefile */
1719 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1722 * The mmap operation should write subbuf_size amount of data when
1723 * network streaming or the full padding (len) size when we are _not_
1726 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1727 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1729 * Display the error but continue processing to try to release the
1730 * subbuffer. This is a DBG statement since this is possible to
1731 * happen without being a critical error.
1733 DBG("Error writing to tracefile "
1734 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1735 ret
, len
, subbuf_size
);
1740 ERR("Unknown output method");
1744 err
= kernctl_put_next_subbuf(infd
);
1746 if (err
== -EFAULT
) {
1747 PERROR("Error in unreserving sub buffer\n");
1748 } else if (err
== -EIO
) {
1749 /* Should never happen with newer LTTng versions */
1750 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1756 /* Write index if needed. */
1761 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1763 * In live, block until all the metadata is sent.
1765 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1766 assert(!stream
->missed_metadata_flush
);
1767 stream
->waiting_on_metadata
= true;
1768 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1770 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1772 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1773 stream
->waiting_on_metadata
= false;
1774 if (stream
->missed_metadata_flush
) {
1775 stream
->missed_metadata_flush
= false;
1776 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1777 (void) consumer_flush_kernel_index(stream
);
1779 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1786 err
= consumer_stream_write_index(stream
, &index
);
1793 * After extracting the packet, we check if the stream is now ready to be
1794 * rotated and perform the action immediately.
1796 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1797 if (rotation_ret
== 1) {
1798 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1799 if (rotation_ret
< 0) {
1800 ERR("Stream rotation error");
1804 } else if (rotation_ret
< 0) {
1805 ERR("Checking if stream is ready to rotate");
1814 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1821 * Don't create anything if this is set for streaming or if there is
1822 * no current trace chunk on the parent channel.
1824 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1825 stream
->chan
->trace_chunk
) {
1826 ret
= consumer_stream_create_output_files(stream
, true);
1832 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1833 /* get the len of the mmap region */
1834 unsigned long mmap_len
;
1836 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1838 PERROR("kernctl_get_mmap_len");
1839 goto error_close_fd
;
1841 stream
->mmap_len
= (size_t) mmap_len
;
1843 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1844 MAP_PRIVATE
, stream
->wait_fd
, 0);
1845 if (stream
->mmap_base
== MAP_FAILED
) {
1846 PERROR("Error mmaping");
1848 goto error_close_fd
;
1852 /* we return 0 to let the library handle the FD internally */
1856 if (stream
->out_fd
>= 0) {
1859 err
= close(stream
->out_fd
);
1861 stream
->out_fd
= -1;
1868 * Check if data is still being extracted from the buffers for a specific
1869 * stream. Consumer data lock MUST be acquired before calling this function
1870 * and the stream lock.
1872 * Return 1 if the traced data are still getting read else 0 meaning that the
1873 * data is available for trace viewer reading.
1875 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1881 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1886 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1888 /* There is still data so let's put back this subbuffer. */
1889 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1891 ret
= 1; /* Data is pending */
1895 /* Data is NOT pending and ready to be read. */