2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/compat/endian.h>
40 #include <common/pipe.h>
41 #include <common/relayd/relayd.h>
42 #include <common/utils.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/optional.h>
48 #include "kernel-consumer.h"
50 extern struct lttng_consumer_global_data consumer_data
;
51 extern int consumer_poll_timeout
;
54 * Take a snapshot for a specific fd
56 * Returns 0 on success, < 0 on error
58 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
61 int infd
= stream
->wait_fd
;
63 ret
= kernctl_snapshot(infd
);
65 * -EAGAIN is not an error, it just means that there is no data to
68 if (ret
!= 0 && ret
!= -EAGAIN
) {
69 PERROR("Getting sub-buffer snapshot.");
76 * Sample consumed and produced positions for a specific fd.
78 * Returns 0 on success, < 0 on error.
80 int lttng_kconsumer_sample_snapshot_positions(
81 struct lttng_consumer_stream
*stream
)
85 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
89 * Get the produced position
91 * Returns 0 on success, < 0 on error
93 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
97 int infd
= stream
->wait_fd
;
99 ret
= kernctl_snapshot_get_produced(infd
, pos
);
101 PERROR("kernctl_snapshot_get_produced");
108 * Get the consumerd position
110 * Returns 0 on success, < 0 on error
112 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
116 int infd
= stream
->wait_fd
;
118 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
120 PERROR("kernctl_snapshot_get_consumed");
127 * Take a snapshot of all the stream of a channel
128 * RCU read-side lock must be held across this function to ensure existence of
129 * channel. The channel lock must be held by the caller.
131 * Returns 0 on success, < 0 on error
133 static int lttng_kconsumer_snapshot_channel(
134 struct lttng_consumer_channel
*channel
,
135 uint64_t key
, char *path
, uint64_t relayd_id
,
136 uint64_t nb_packets_per_stream
,
137 struct lttng_consumer_local_data
*ctx
)
140 struct lttng_consumer_stream
*stream
;
142 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
146 /* Splice is not supported yet for channel snapshot. */
147 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
148 ERR("Unsupported output type for channel \"%s\": mmap output is required to record a snapshot",
154 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
155 unsigned long consumed_pos
, produced_pos
;
157 health_code_update();
160 * Lock stream because we are about to change its state.
162 pthread_mutex_lock(&stream
->lock
);
164 assert(channel
->trace_chunk
);
165 if (!lttng_trace_chunk_get(channel
->trace_chunk
)) {
167 * Can't happen barring an internal error as the channel
168 * holds a reference to the trace chunk.
170 ERR("Failed to acquire reference to channel's trace chunk");
174 assert(!stream
->trace_chunk
);
175 stream
->trace_chunk
= channel
->trace_chunk
;
178 * Assign the received relayd ID so we can use it for streaming. The streams
179 * are not visible to anyone so this is OK to change it.
181 stream
->net_seq_idx
= relayd_id
;
182 channel
->relayd_id
= relayd_id
;
183 if (relayd_id
!= (uint64_t) -1ULL) {
184 ret
= consumer_send_relayd_stream(stream
, path
);
186 ERR("sending stream to relayd");
190 ret
= consumer_stream_create_output_files(stream
,
195 DBG("Kernel consumer snapshot stream (%" PRIu64
")",
199 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
202 * Doing a buffer flush which does not take into
203 * account empty packets. This is not perfect
204 * for stream intersection, but required as a
205 * fall-back when "flush_empty" is not
206 * implemented by lttng-modules.
208 ret
= kernctl_buffer_flush(stream
->wait_fd
);
210 ERR("Failed to flush kernel stream");
216 ret
= lttng_kconsumer_take_snapshot(stream
);
218 ERR("Taking kernel snapshot");
222 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
224 ERR("Produced kernel snapshot position");
228 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
230 ERR("Consumerd kernel snapshot position");
234 if (stream
->max_sb_size
== 0) {
235 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
236 &stream
->max_sb_size
);
238 ERR("Getting kernel max_sb_size");
243 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
244 produced_pos
, nb_packets_per_stream
,
245 stream
->max_sb_size
);
247 while ((long) (consumed_pos
- produced_pos
) < 0) {
249 unsigned long len
, padded_len
;
251 health_code_update();
253 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
255 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
257 if (ret
!= -EAGAIN
) {
258 PERROR("kernctl_get_subbuf snapshot");
261 DBG("Kernel consumer get subbuf failed. Skipping it.");
262 consumed_pos
+= stream
->max_sb_size
;
263 stream
->chan
->lost_packets
++;
267 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
269 ERR("Snapshot kernctl_get_subbuf_size");
270 goto error_put_subbuf
;
273 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
275 ERR("Snapshot kernctl_get_padded_subbuf_size");
276 goto error_put_subbuf
;
279 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
280 padded_len
- len
, NULL
);
282 * We write the padded len in local tracefiles but the data len
283 * when using a relay. Display the error but continue processing
284 * to try to release the subbuffer.
286 if (relayd_id
!= (uint64_t) -1ULL) {
287 if (read_len
!= len
) {
288 ERR("Error sending to the relay (ret: %zd != len: %lu)",
292 if (read_len
!= padded_len
) {
293 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
294 read_len
, padded_len
);
298 ret
= kernctl_put_subbuf(stream
->wait_fd
);
300 ERR("Snapshot kernctl_put_subbuf");
303 consumed_pos
+= stream
->max_sb_size
;
306 if (relayd_id
== (uint64_t) -1ULL) {
307 if (stream
->out_fd
>= 0) {
308 ret
= close(stream
->out_fd
);
310 PERROR("Kernel consumer snapshot close out_fd");
316 close_relayd_stream(stream
);
317 stream
->net_seq_idx
= (uint64_t) -1ULL;
319 lttng_trace_chunk_put(stream
->trace_chunk
);
320 stream
->trace_chunk
= NULL
;
321 pthread_mutex_unlock(&stream
->lock
);
329 ret
= kernctl_put_subbuf(stream
->wait_fd
);
331 ERR("Snapshot kernctl_put_subbuf error path");
334 pthread_mutex_unlock(&stream
->lock
);
341 * Read the whole metadata available for a snapshot.
342 * RCU read-side lock must be held across this function to ensure existence of
343 * metadata_channel. The channel lock must be held by the caller.
345 * Returns 0 on success, < 0 on error
347 static int lttng_kconsumer_snapshot_metadata(
348 struct lttng_consumer_channel
*metadata_channel
,
349 uint64_t key
, char *path
, uint64_t relayd_id
,
350 struct lttng_consumer_local_data
*ctx
)
352 int ret
, use_relayd
= 0;
354 struct lttng_consumer_stream
*metadata_stream
;
358 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
363 metadata_stream
= metadata_channel
->metadata_stream
;
364 assert(metadata_stream
);
366 pthread_mutex_lock(&metadata_stream
->lock
);
367 assert(metadata_channel
->trace_chunk
);
368 assert(metadata_stream
->trace_chunk
);
370 /* Flag once that we have a valid relayd for the stream. */
371 if (relayd_id
!= (uint64_t) -1ULL) {
376 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
381 ret
= consumer_stream_create_output_files(metadata_stream
,
389 health_code_update();
391 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
393 if (ret_read
!= -EAGAIN
) {
394 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
399 /* ret_read is negative at this point so we will exit the loop. */
402 } while (ret_read
>= 0);
405 close_relayd_stream(metadata_stream
);
406 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
408 if (metadata_stream
->out_fd
>= 0) {
409 ret
= close(metadata_stream
->out_fd
);
411 PERROR("Kernel consumer snapshot metadata close out_fd");
413 * Don't go on error here since the snapshot was successful at this
414 * point but somehow the close failed.
417 metadata_stream
->out_fd
= -1;
418 lttng_trace_chunk_put(metadata_stream
->trace_chunk
);
419 metadata_stream
->trace_chunk
= NULL
;
425 pthread_mutex_unlock(&metadata_stream
->lock
);
426 cds_list_del(&metadata_stream
->send_node
);
427 consumer_stream_destroy(metadata_stream
, NULL
);
428 metadata_channel
->metadata_stream
= NULL
;
434 * Receive command from session daemon and process it.
436 * Return 1 on success else a negative value or 0.
438 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
439 int sock
, struct pollfd
*consumer_sockpoll
)
442 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
443 struct lttcomm_consumer_msg msg
;
445 health_code_update();
447 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
448 if (ret
!= sizeof(msg
)) {
450 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
456 health_code_update();
458 /* Deprecated command */
459 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
461 health_code_update();
463 /* relayd needs RCU read-side protection */
466 switch (msg
.cmd_type
) {
467 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
469 /* Session daemon status message are handled in the following call. */
470 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
471 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
472 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
473 msg
.u
.relayd_sock
.relayd_session_id
);
476 case LTTNG_CONSUMER_ADD_CHANNEL
:
478 struct lttng_consumer_channel
*new_channel
;
480 const uint64_t chunk_id
= msg
.u
.channel
.chunk_id
.value
;
482 health_code_update();
484 /* First send a status message before receiving the fds. */
485 ret
= consumer_send_status_msg(sock
, ret_code
);
487 /* Somehow, the session daemon is not responding anymore. */
491 health_code_update();
493 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
494 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
495 msg
.u
.channel
.session_id
,
496 msg
.u
.channel
.chunk_id
.is_set
?
498 msg
.u
.channel
.pathname
,
500 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
501 msg
.u
.channel
.tracefile_size
,
502 msg
.u
.channel
.tracefile_count
, 0,
503 msg
.u
.channel
.monitor
,
504 msg
.u
.channel
.live_timer_interval
,
506 if (new_channel
== NULL
) {
507 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
510 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
511 switch (msg
.u
.channel
.output
) {
512 case LTTNG_EVENT_SPLICE
:
513 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
515 case LTTNG_EVENT_MMAP
:
516 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
519 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
523 /* Translate and save channel type. */
524 switch (msg
.u
.channel
.type
) {
525 case CONSUMER_CHANNEL_TYPE_DATA
:
526 case CONSUMER_CHANNEL_TYPE_METADATA
:
527 new_channel
->type
= msg
.u
.channel
.type
;
534 health_code_update();
536 if (ctx
->on_recv_channel
!= NULL
) {
537 ret_recv
= ctx
->on_recv_channel(new_channel
);
539 ret
= consumer_add_channel(new_channel
, ctx
);
540 } else if (ret_recv
< 0) {
544 ret
= consumer_add_channel(new_channel
, ctx
);
546 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
547 int monitor_start_ret
;
549 DBG("Consumer starting monitor timer");
550 consumer_timer_live_start(new_channel
,
551 msg
.u
.channel
.live_timer_interval
);
552 monitor_start_ret
= consumer_timer_monitor_start(
554 msg
.u
.channel
.monitor_timer_interval
);
555 if (monitor_start_ret
< 0) {
556 ERR("Starting channel monitoring timer failed");
562 health_code_update();
564 /* If we received an error in add_channel, we need to report it. */
566 ret
= consumer_send_status_msg(sock
, ret
);
575 case LTTNG_CONSUMER_ADD_STREAM
:
578 struct lttng_pipe
*stream_pipe
;
579 struct lttng_consumer_stream
*new_stream
;
580 struct lttng_consumer_channel
*channel
;
584 * Get stream's channel reference. Needed when adding the stream to the
587 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
590 * We could not find the channel. Can happen if cpu hotplug
591 * happens while tearing down.
593 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
594 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
597 health_code_update();
599 /* First send a status message before receiving the fds. */
600 ret
= consumer_send_status_msg(sock
, ret_code
);
602 /* Somehow, the session daemon is not responding anymore. */
606 health_code_update();
608 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
609 /* Channel was not found. */
615 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
621 health_code_update();
623 /* Get stream file descriptor from socket */
624 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
625 if (ret
!= sizeof(fd
)) {
626 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
631 health_code_update();
634 * Send status code to session daemon only if the recv works. If the
635 * above recv() failed, the session daemon is notified through the
636 * error socket and the teardown is eventually done.
638 ret
= consumer_send_status_msg(sock
, ret_code
);
640 /* Somehow, the session daemon is not responding anymore. */
644 health_code_update();
646 pthread_mutex_lock(&channel
->lock
);
647 new_stream
= consumer_allocate_stream(channel
->key
,
652 channel
->trace_chunk
,
657 if (new_stream
== NULL
) {
662 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
665 pthread_mutex_unlock(&channel
->lock
);
669 new_stream
->chan
= channel
;
670 new_stream
->wait_fd
= fd
;
671 consumer_stream_update_channel_attributes(new_stream
,
673 switch (channel
->output
) {
674 case CONSUMER_CHANNEL_SPLICE
:
675 new_stream
->output
= LTTNG_EVENT_SPLICE
;
676 ret
= utils_create_pipe(new_stream
->splice_pipe
);
678 pthread_mutex_unlock(&channel
->lock
);
682 case CONSUMER_CHANNEL_MMAP
:
683 new_stream
->output
= LTTNG_EVENT_MMAP
;
686 ERR("Stream output unknown %d", channel
->output
);
687 pthread_mutex_unlock(&channel
->lock
);
692 * We've just assigned the channel to the stream so increment the
693 * refcount right now. We don't need to increment the refcount for
694 * streams in no monitor because we handle manually the cleanup of
695 * those. It is very important to make sure there is NO prior
696 * consumer_del_stream() calls or else the refcount will be unbalanced.
698 if (channel
->monitor
) {
699 uatomic_inc(&new_stream
->chan
->refcount
);
703 * The buffer flush is done on the session daemon side for the kernel
704 * so no need for the stream "hangup_flush_done" variable to be
705 * tracked. This is important for a kernel stream since we don't rely
706 * on the flush state of the stream to read data. It's not the case for
707 * user space tracing.
709 new_stream
->hangup_flush_done
= 0;
711 health_code_update();
713 pthread_mutex_lock(&new_stream
->lock
);
714 if (ctx
->on_recv_stream
) {
715 ret
= ctx
->on_recv_stream(new_stream
);
717 pthread_mutex_unlock(&new_stream
->lock
);
718 pthread_mutex_unlock(&channel
->lock
);
719 consumer_stream_free(new_stream
);
723 health_code_update();
725 if (new_stream
->metadata_flag
) {
726 channel
->metadata_stream
= new_stream
;
729 /* Do not monitor this stream. */
730 if (!channel
->monitor
) {
731 DBG("Kernel consumer add stream %s in no monitor mode with "
732 "relayd id %" PRIu64
, new_stream
->name
,
733 new_stream
->net_seq_idx
);
734 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
735 pthread_mutex_unlock(&new_stream
->lock
);
736 pthread_mutex_unlock(&channel
->lock
);
740 /* Send stream to relayd if the stream has an ID. */
741 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
742 ret
= consumer_send_relayd_stream(new_stream
,
743 new_stream
->chan
->pathname
);
745 pthread_mutex_unlock(&new_stream
->lock
);
746 pthread_mutex_unlock(&channel
->lock
);
747 consumer_stream_free(new_stream
);
752 * If adding an extra stream to an already
753 * existing channel (e.g. cpu hotplug), we need
754 * to send the "streams_sent" command to relayd.
756 if (channel
->streams_sent_to_relayd
) {
757 ret
= consumer_send_relayd_streams_sent(
758 new_stream
->net_seq_idx
);
760 pthread_mutex_unlock(&new_stream
->lock
);
761 pthread_mutex_unlock(&channel
->lock
);
766 pthread_mutex_unlock(&new_stream
->lock
);
767 pthread_mutex_unlock(&channel
->lock
);
769 /* Get the right pipe where the stream will be sent. */
770 if (new_stream
->metadata_flag
) {
771 consumer_add_metadata_stream(new_stream
);
772 stream_pipe
= ctx
->consumer_metadata_pipe
;
774 consumer_add_data_stream(new_stream
);
775 stream_pipe
= ctx
->consumer_data_pipe
;
778 /* Visible to other threads */
779 new_stream
->globally_visible
= 1;
781 health_code_update();
783 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
785 ERR("Consumer write %s stream to pipe %d",
786 new_stream
->metadata_flag
? "metadata" : "data",
787 lttng_pipe_get_writefd(stream_pipe
));
788 if (new_stream
->metadata_flag
) {
789 consumer_del_stream_for_metadata(new_stream
);
791 consumer_del_stream_for_data(new_stream
);
796 DBG("Kernel consumer ADD_STREAM %s (fd: %d) %s with relayd id %" PRIu64
,
797 new_stream
->name
, fd
, new_stream
->chan
->pathname
, new_stream
->relayd_stream_id
);
800 case LTTNG_CONSUMER_STREAMS_SENT
:
802 struct lttng_consumer_channel
*channel
;
805 * Get stream's channel reference. Needed when adding the stream to the
808 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
811 * We could not find the channel. Can happen if cpu hotplug
812 * happens while tearing down.
814 ERR("Unable to find channel key %" PRIu64
,
815 msg
.u
.sent_streams
.channel_key
);
816 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
819 health_code_update();
822 * Send status code to session daemon.
824 ret
= consumer_send_status_msg(sock
, ret_code
);
825 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
826 /* Somehow, the session daemon is not responding anymore. */
830 health_code_update();
833 * We should not send this message if we don't monitor the
834 * streams in this channel.
836 if (!channel
->monitor
) {
840 health_code_update();
841 /* Send stream to relayd if the stream has an ID. */
842 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
843 ret
= consumer_send_relayd_streams_sent(
844 msg
.u
.sent_streams
.net_seq_idx
);
848 channel
->streams_sent_to_relayd
= true;
852 case LTTNG_CONSUMER_UPDATE_STREAM
:
857 case LTTNG_CONSUMER_DESTROY_RELAYD
:
859 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
860 struct consumer_relayd_sock_pair
*relayd
;
862 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
864 /* Get relayd reference if exists. */
865 relayd
= consumer_find_relayd(index
);
866 if (relayd
== NULL
) {
867 DBG("Unable to find relayd %" PRIu64
, index
);
868 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
872 * Each relayd socket pair has a refcount of stream attached to it
873 * which tells if the relayd is still active or not depending on the
876 * This will set the destroy flag of the relayd object and destroy it
877 * if the refcount reaches zero when called.
879 * The destroy can happen either here or when a stream fd hangs up.
882 consumer_flag_relayd_for_destroy(relayd
);
885 health_code_update();
887 ret
= consumer_send_status_msg(sock
, ret_code
);
889 /* Somehow, the session daemon is not responding anymore. */
895 case LTTNG_CONSUMER_DATA_PENDING
:
898 uint64_t id
= msg
.u
.data_pending
.session_id
;
900 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
902 ret
= consumer_data_pending(id
);
904 health_code_update();
906 /* Send back returned value to session daemon */
907 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
909 PERROR("send data pending ret code");
914 * No need to send back a status message since the data pending
915 * returned value is the response.
919 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
921 struct lttng_consumer_channel
*channel
;
922 uint64_t key
= msg
.u
.snapshot_channel
.key
;
924 channel
= consumer_find_channel(key
);
926 ERR("Channel %" PRIu64
" not found", key
);
927 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
929 pthread_mutex_lock(&channel
->lock
);
930 if (msg
.u
.snapshot_channel
.metadata
== 1) {
931 ret
= lttng_kconsumer_snapshot_metadata(channel
, key
,
932 msg
.u
.snapshot_channel
.pathname
,
933 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
935 ERR("Snapshot metadata failed");
936 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
939 ret
= lttng_kconsumer_snapshot_channel(channel
, key
,
940 msg
.u
.snapshot_channel
.pathname
,
941 msg
.u
.snapshot_channel
.relayd_id
,
942 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
945 ERR("Snapshot channel failed");
946 ret_code
= LTTCOMM_CONSUMERD_SNAPSHOT_FAILED
;
949 pthread_mutex_unlock(&channel
->lock
);
951 health_code_update();
953 ret
= consumer_send_status_msg(sock
, ret_code
);
955 /* Somehow, the session daemon is not responding anymore. */
960 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
962 uint64_t key
= msg
.u
.destroy_channel
.key
;
963 struct lttng_consumer_channel
*channel
;
965 channel
= consumer_find_channel(key
);
967 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
968 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
971 health_code_update();
973 ret
= consumer_send_status_msg(sock
, ret_code
);
975 /* Somehow, the session daemon is not responding anymore. */
979 health_code_update();
981 /* Stop right now if no channel was found. */
987 * This command should ONLY be issued for channel with streams set in
990 assert(!channel
->monitor
);
993 * The refcount should ALWAYS be 0 in the case of a channel in no
996 assert(!uatomic_sub_return(&channel
->refcount
, 1));
998 consumer_del_channel(channel
);
1002 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1006 struct lttng_consumer_channel
*channel
;
1007 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1008 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1010 DBG("Kernel consumer discarded events command for session id %"
1011 PRIu64
", channel key %" PRIu64
, id
, key
);
1013 channel
= consumer_find_channel(key
);
1015 ERR("Kernel consumer discarded events channel %"
1016 PRIu64
" not found", key
);
1019 count
= channel
->discarded_events
;
1022 health_code_update();
1024 /* Send back returned value to session daemon */
1025 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1027 PERROR("send discarded events");
1033 case LTTNG_CONSUMER_LOST_PACKETS
:
1037 struct lttng_consumer_channel
*channel
;
1038 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1039 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1041 DBG("Kernel consumer lost packets command for session id %"
1042 PRIu64
", channel key %" PRIu64
, id
, key
);
1044 channel
= consumer_find_channel(key
);
1046 ERR("Kernel consumer lost packets channel %"
1047 PRIu64
" not found", key
);
1050 count
= channel
->lost_packets
;
1053 health_code_update();
1055 /* Send back returned value to session daemon */
1056 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1058 PERROR("send lost packets");
1064 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1066 int channel_monitor_pipe
;
1068 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1069 /* Successfully received the command's type. */
1070 ret
= consumer_send_status_msg(sock
, ret_code
);
1075 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1077 if (ret
!= sizeof(channel_monitor_pipe
)) {
1078 ERR("Failed to receive channel monitor pipe");
1082 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1083 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1084 channel_monitor_pipe
);
1088 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1089 /* Set the pipe as non-blocking. */
1090 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1092 PERROR("fcntl get flags of the channel monitoring pipe");
1097 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1098 flags
| O_NONBLOCK
);
1100 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1103 DBG("Channel monitor pipe set as non-blocking");
1105 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1107 ret
= consumer_send_status_msg(sock
, ret_code
);
1113 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1115 struct lttng_consumer_channel
*channel
;
1116 uint64_t key
= msg
.u
.rotate_channel
.key
;
1118 DBG("Consumer rotate channel %" PRIu64
, key
);
1120 channel
= consumer_find_channel(key
);
1122 ERR("Channel %" PRIu64
" not found", key
);
1123 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1126 * Sample the rotate position of all the streams in this channel.
1128 ret
= lttng_consumer_rotate_channel(channel
, key
,
1129 msg
.u
.rotate_channel
.relayd_id
,
1130 msg
.u
.rotate_channel
.metadata
,
1133 ERR("Rotate channel failed");
1134 ret_code
= LTTCOMM_CONSUMERD_ROTATION_FAIL
;
1137 health_code_update();
1139 ret
= consumer_send_status_msg(sock
, ret_code
);
1141 /* Somehow, the session daemon is not responding anymore. */
1145 /* Rotate the streams that are ready right now. */
1146 ret
= lttng_consumer_rotate_ready_streams(
1149 ERR("Rotate ready streams failed");
1155 case LTTNG_CONSUMER_INIT
:
1157 ret_code
= lttng_consumer_init_command(ctx
,
1158 msg
.u
.init
.sessiond_uuid
);
1159 health_code_update();
1160 ret
= consumer_send_status_msg(sock
, ret_code
);
1162 /* Somehow, the session daemon is not responding anymore. */
1167 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK
:
1169 const struct lttng_credentials credentials
= {
1170 .uid
= msg
.u
.create_trace_chunk
.credentials
.uid
,
1171 .gid
= msg
.u
.create_trace_chunk
.credentials
.gid
,
1173 const bool is_local_trace
=
1174 !msg
.u
.create_trace_chunk
.relayd_id
.is_set
;
1175 const uint64_t relayd_id
=
1176 msg
.u
.create_trace_chunk
.relayd_id
.value
;
1177 const char *chunk_override_name
=
1178 *msg
.u
.create_trace_chunk
.override_name
?
1179 msg
.u
.create_trace_chunk
.override_name
:
1181 LTTNG_OPTIONAL(struct lttng_directory_handle
) chunk_directory_handle
=
1182 LTTNG_OPTIONAL_INIT
;
1185 * The session daemon will only provide a chunk directory file
1186 * descriptor for local traces.
1188 if (is_local_trace
) {
1191 /* Acnowledge the reception of the command. */
1192 ret
= consumer_send_status_msg(sock
,
1193 LTTCOMM_CONSUMERD_SUCCESS
);
1195 /* Somehow, the session daemon is not responding anymore. */
1199 ret
= lttcomm_recv_fds_unix_sock(sock
, &chunk_dirfd
, 1);
1200 if (ret
!= sizeof(chunk_dirfd
)) {
1201 ERR("Failed to receive trace chunk directory file descriptor");
1205 DBG("Received trace chunk directory fd (%d)",
1207 ret
= lttng_directory_handle_init_from_dirfd(
1208 &chunk_directory_handle
.value
,
1211 ERR("Failed to initialize chunk directory handle from directory file descriptor");
1212 if (close(chunk_dirfd
)) {
1213 PERROR("Failed to close chunk directory file descriptor");
1217 chunk_directory_handle
.is_set
= true;
1220 ret_code
= lttng_consumer_create_trace_chunk(
1221 !is_local_trace
? &relayd_id
: NULL
,
1222 msg
.u
.create_trace_chunk
.session_id
,
1223 msg
.u
.create_trace_chunk
.chunk_id
,
1224 (time_t) msg
.u
.create_trace_chunk
.creation_timestamp
,
1225 chunk_override_name
,
1227 chunk_directory_handle
.is_set
?
1228 &chunk_directory_handle
.value
:
1231 if (chunk_directory_handle
.is_set
) {
1232 lttng_directory_handle_fini(
1233 &chunk_directory_handle
.value
);
1235 goto end_msg_sessiond
;
1237 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK
:
1239 const uint64_t relayd_id
=
1240 msg
.u
.close_trace_chunk
.relayd_id
.value
;
1242 ret_code
= lttng_consumer_close_trace_chunk(
1243 msg
.u
.close_trace_chunk
.relayd_id
.is_set
?
1245 msg
.u
.close_trace_chunk
.session_id
,
1246 msg
.u
.close_trace_chunk
.chunk_id
,
1247 (time_t) msg
.u
.close_trace_chunk
.close_timestamp
);
1248 goto end_msg_sessiond
;
1250 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS
:
1252 const uint64_t relayd_id
=
1253 msg
.u
.trace_chunk_exists
.relayd_id
.value
;
1255 ret_code
= lttng_consumer_trace_chunk_exists(
1256 msg
.u
.trace_chunk_exists
.relayd_id
.is_set
?
1258 msg
.u
.trace_chunk_exists
.session_id
,
1259 msg
.u
.trace_chunk_exists
.chunk_id
);
1260 goto end_msg_sessiond
;
1270 * Return 1 to indicate success since the 0 value can be a socket
1271 * shutdown during the recv() or send() call.
1273 health_code_update();
1278 * The returned value here is not useful since either way we'll return 1 to
1279 * the caller because the session daemon socket management is done
1280 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1282 ret
= consumer_send_status_msg(sock
, ret_code
);
1288 health_code_update();
1294 /* This will issue a consumer stop. */
1299 * Populate index values of a kernel stream. Values are set in big endian order.
1301 * Return 0 on success or else a negative value.
1303 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1307 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1309 PERROR("kernctl_get_timestamp_begin");
1312 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1314 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1316 PERROR("kernctl_get_timestamp_end");
1319 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1321 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1323 PERROR("kernctl_get_events_discarded");
1326 index
->events_discarded
= htobe64(index
->events_discarded
);
1328 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1330 PERROR("kernctl_get_content_size");
1333 index
->content_size
= htobe64(index
->content_size
);
1335 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1337 PERROR("kernctl_get_packet_size");
1340 index
->packet_size
= htobe64(index
->packet_size
);
1342 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1344 PERROR("kernctl_get_stream_id");
1347 index
->stream_id
= htobe64(index
->stream_id
);
1349 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1351 if (ret
== -ENOTTY
) {
1352 /* Command not implemented by lttng-modules. */
1353 index
->stream_instance_id
= -1ULL;
1355 PERROR("kernctl_get_instance_id");
1359 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1361 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1363 if (ret
== -ENOTTY
) {
1364 /* Command not implemented by lttng-modules. */
1365 index
->packet_seq_num
= -1ULL;
1368 PERROR("kernctl_get_sequence_number");
1372 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1378 * Sync metadata meaning request them to the session daemon and snapshot to the
1379 * metadata thread can consumer them.
1381 * Metadata stream lock MUST be acquired.
1383 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1384 * is empty or a negative value on error.
1386 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1392 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1394 ERR("Failed to flush kernel stream");
1398 ret
= kernctl_snapshot(metadata
->wait_fd
);
1400 if (ret
!= -EAGAIN
) {
1401 ERR("Sync metadata, taking kernel snapshot failed.");
1404 DBG("Sync metadata, no new kernel metadata");
1405 /* No new metadata, exit. */
1415 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1418 uint64_t seq
, discarded
;
1420 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1422 if (ret
== -ENOTTY
) {
1423 /* Command not implemented by lttng-modules. */
1426 PERROR("kernctl_get_sequence_number");
1432 * Start the sequence when we extract the first packet in case we don't
1433 * start at 0 (for example if a consumer is not connected to the
1434 * session immediately after the beginning).
1436 if (stream
->last_sequence_number
== -1ULL) {
1437 stream
->last_sequence_number
= seq
;
1438 } else if (seq
> stream
->last_sequence_number
) {
1439 stream
->chan
->lost_packets
+= seq
-
1440 stream
->last_sequence_number
- 1;
1442 /* seq <= last_sequence_number */
1443 ERR("Sequence number inconsistent : prev = %" PRIu64
1444 ", current = %" PRIu64
,
1445 stream
->last_sequence_number
, seq
);
1449 stream
->last_sequence_number
= seq
;
1451 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1453 PERROR("kernctl_get_events_discarded");
1456 if (discarded
< stream
->last_discarded_events
) {
1458 * Overflow has occurred. We assume only one wrap-around
1461 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1462 stream
->last_discarded_events
+ discarded
;
1464 stream
->chan
->discarded_events
+= discarded
-
1465 stream
->last_discarded_events
;
1467 stream
->last_discarded_events
= discarded
;
1475 * Check if the local version of the metadata stream matches with the version
1476 * of the metadata stream in the kernel. If it was updated, set the reset flag
1480 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1483 uint64_t cur_version
;
1485 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1487 if (ret
== -ENOTTY
) {
1489 * LTTng-modules does not implement this
1495 ERR("Failed to get the metadata version");
1499 if (stream
->metadata_version
== cur_version
) {
1504 DBG("New metadata version detected");
1505 stream
->metadata_version
= cur_version
;
1506 stream
->reset_metadata_flag
= 1;
1514 * Consume data on a file descriptor and write it on a trace file.
1515 * The stream and channel locks must be held by the caller.
1517 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1518 struct lttng_consumer_local_data
*ctx
)
1520 unsigned long len
, subbuf_size
, padding
;
1521 int err
, write_index
= 1, rotation_ret
;
1523 int infd
= stream
->wait_fd
;
1524 struct ctf_packet_index index
;
1526 DBG("In read_subbuffer (infd : %d)", infd
);
1529 * If the stream was flagged to be ready for rotation before we extract the
1530 * next packet, rotate it now.
1532 if (stream
->rotate_ready
) {
1533 DBG("Rotate stream before extracting data");
1534 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1535 if (rotation_ret
< 0) {
1536 ERR("Stream rotation error");
1542 /* Get the next subbuffer */
1543 err
= kernctl_get_next_subbuf(infd
);
1546 * This is a debug message even for single-threaded consumer,
1547 * because poll() have more relaxed criterions than get subbuf,
1548 * so get_subbuf may fail for short race windows where poll()
1549 * would issue wakeups.
1551 DBG("Reserving sub buffer failed (everything is normal, "
1552 "it is due to concurrency)");
1557 /* Get the full subbuffer size including padding */
1558 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1560 PERROR("Getting sub-buffer len failed.");
1561 err
= kernctl_put_subbuf(infd
);
1563 if (err
== -EFAULT
) {
1564 PERROR("Error in unreserving sub buffer\n");
1565 } else if (err
== -EIO
) {
1566 /* Should never happen with newer LTTng versions */
1567 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1576 if (!stream
->metadata_flag
) {
1577 ret
= get_index_values(&index
, infd
);
1579 err
= kernctl_put_subbuf(infd
);
1581 if (err
== -EFAULT
) {
1582 PERROR("Error in unreserving sub buffer\n");
1583 } else if (err
== -EIO
) {
1584 /* Should never happen with newer LTTng versions */
1585 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1592 ret
= update_stream_stats(stream
);
1594 err
= kernctl_put_subbuf(infd
);
1596 if (err
== -EFAULT
) {
1597 PERROR("Error in unreserving sub buffer\n");
1598 } else if (err
== -EIO
) {
1599 /* Should never happen with newer LTTng versions */
1600 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1609 ret
= metadata_stream_check_version(infd
, stream
);
1611 err
= kernctl_put_subbuf(infd
);
1613 if (err
== -EFAULT
) {
1614 PERROR("Error in unreserving sub buffer\n");
1615 } else if (err
== -EIO
) {
1616 /* Should never happen with newer LTTng versions */
1617 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1626 switch (stream
->chan
->output
) {
1627 case CONSUMER_CHANNEL_SPLICE
:
1629 * XXX: The lttng-modules splice "actor" does not handle copying
1630 * partial pages hence only using the subbuffer size without the
1631 * padding makes the splice fail.
1636 /* splice the subbuffer to the tracefile */
1637 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1640 * XXX: Splice does not support network streaming so the return value
1641 * is simply checked against subbuf_size and not like the mmap() op.
1643 if (ret
!= subbuf_size
) {
1645 * display the error but continue processing to try
1646 * to release the subbuffer
1648 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1653 case CONSUMER_CHANNEL_MMAP
:
1654 /* Get subbuffer size without padding */
1655 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1657 PERROR("Getting sub-buffer len failed.");
1658 err
= kernctl_put_subbuf(infd
);
1660 if (err
== -EFAULT
) {
1661 PERROR("Error in unreserving sub buffer\n");
1662 } else if (err
== -EIO
) {
1663 /* Should never happen with newer LTTng versions */
1664 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1673 /* Make sure the tracer is not gone mad on us! */
1674 assert(len
>= subbuf_size
);
1676 padding
= len
- subbuf_size
;
1678 /* write the subbuffer to the tracefile */
1679 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1682 * The mmap operation should write subbuf_size amount of data when
1683 * network streaming or the full padding (len) size when we are _not_
1686 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1687 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1689 * Display the error but continue processing to try to release the
1690 * subbuffer. This is a DBG statement since this is possible to
1691 * happen without being a critical error.
1693 DBG("Error writing to tracefile "
1694 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1695 ret
, len
, subbuf_size
);
1700 ERR("Unknown output method");
1704 err
= kernctl_put_next_subbuf(infd
);
1706 if (err
== -EFAULT
) {
1707 PERROR("Error in unreserving sub buffer\n");
1708 } else if (err
== -EIO
) {
1709 /* Should never happen with newer LTTng versions */
1710 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1716 /* Write index if needed. */
1721 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1723 * In live, block until all the metadata is sent.
1725 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1726 assert(!stream
->missed_metadata_flush
);
1727 stream
->waiting_on_metadata
= true;
1728 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1730 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1732 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1733 stream
->waiting_on_metadata
= false;
1734 if (stream
->missed_metadata_flush
) {
1735 stream
->missed_metadata_flush
= false;
1736 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1737 (void) consumer_flush_kernel_index(stream
);
1739 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1746 err
= consumer_stream_write_index(stream
, &index
);
1753 * After extracting the packet, we check if the stream is now ready to be
1754 * rotated and perform the action immediately.
1756 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
1757 if (rotation_ret
== 1) {
1758 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1759 if (rotation_ret
< 0) {
1760 ERR("Stream rotation error");
1764 } else if (rotation_ret
< 0) {
1765 ERR("Checking if stream is ready to rotate");
1774 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1781 * Don't create anything if this is set for streaming or if there is
1782 * no current trace chunk on the parent channel.
1784 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
&&
1785 stream
->chan
->trace_chunk
) {
1786 ret
= consumer_stream_create_output_files(stream
, true);
1792 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1793 /* get the len of the mmap region */
1794 unsigned long mmap_len
;
1796 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1798 PERROR("kernctl_get_mmap_len");
1799 goto error_close_fd
;
1801 stream
->mmap_len
= (size_t) mmap_len
;
1803 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1804 MAP_PRIVATE
, stream
->wait_fd
, 0);
1805 if (stream
->mmap_base
== MAP_FAILED
) {
1806 PERROR("Error mmaping");
1808 goto error_close_fd
;
1812 /* we return 0 to let the library handle the FD internally */
1816 if (stream
->out_fd
>= 0) {
1819 err
= close(stream
->out_fd
);
1821 stream
->out_fd
= -1;
1828 * Check if data is still being extracted from the buffers for a specific
1829 * stream. Consumer data lock MUST be acquired before calling this function
1830 * and the stream lock.
1832 * Return 1 if the traced data are still getting read else 0 meaning that the
1833 * data is available for trace viewer reading.
1835 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1841 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1846 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1848 /* There is still data so let's put back this subbuffer. */
1849 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1851 ret
= 1; /* Data is pending */
1855 /* Data is NOT pending and ready to be read. */