2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/consumer-stream.h>
41 #include <common/index/index.h>
42 #include <common/consumer-timer.h>
44 #include "kernel-consumer.h"
46 extern struct lttng_consumer_global_data consumer_data
;
47 extern int consumer_poll_timeout
;
48 extern volatile int consumer_quit
;
51 * Take a snapshot for a specific fd
53 * Returns 0 on success, < 0 on error
55 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
58 int infd
= stream
->wait_fd
;
60 ret
= kernctl_snapshot(infd
);
62 perror("Getting sub-buffer snapshot.");
70 * Get the produced position
72 * Returns 0 on success, < 0 on error
74 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
78 int infd
= stream
->wait_fd
;
80 ret
= kernctl_snapshot_get_produced(infd
, pos
);
82 perror("kernctl_snapshot_get_produced");
90 * Get the consumerd position
92 * Returns 0 on success, < 0 on error
94 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
98 int infd
= stream
->wait_fd
;
100 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
102 perror("kernctl_snapshot_get_consumed");
110 * Take a snapshot of all the stream of a channel
112 * Returns 0 on success, < 0 on error
114 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
115 uint64_t relayd_id
, uint64_t max_stream_size
,
116 struct lttng_consumer_local_data
*ctx
)
119 unsigned long consumed_pos
, produced_pos
;
120 struct lttng_consumer_channel
*channel
;
121 struct lttng_consumer_stream
*stream
;
123 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
127 channel
= consumer_find_channel(key
);
129 ERR("No channel found for key %" PRIu64
, key
);
134 /* Splice is not supported yet for channel snapshot. */
135 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
136 ERR("Unsupported output %d", channel
->output
);
141 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
143 * Lock stream because we are about to change its state.
145 pthread_mutex_lock(&stream
->lock
);
148 * Assign the received relayd ID so we can use it for streaming. The streams
149 * are not visible to anyone so this is OK to change it.
151 stream
->net_seq_idx
= relayd_id
;
152 channel
->relayd_id
= relayd_id
;
153 if (relayd_id
!= (uint64_t) -1ULL) {
154 ret
= consumer_send_relayd_stream(stream
, path
);
156 ERR("sending stream to relayd");
160 ret
= utils_create_stream_file(path
, stream
->name
,
161 stream
->chan
->tracefile_size
,
162 stream
->tracefile_count_current
,
163 stream
->uid
, stream
->gid
, NULL
);
165 ERR("utils_create_stream_file");
169 stream
->out_fd
= ret
;
170 stream
->tracefile_size_current
= 0;
172 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
173 path
, stream
->name
, stream
->key
);
176 ret
= kernctl_buffer_flush(stream
->wait_fd
);
178 ERR("Failed to flush kernel stream");
183 ret
= lttng_kconsumer_take_snapshot(stream
);
185 ERR("Taking kernel snapshot");
189 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
191 ERR("Produced kernel snapshot position");
195 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
197 ERR("Consumerd kernel snapshot position");
201 if (stream
->max_sb_size
== 0) {
202 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
203 &stream
->max_sb_size
);
205 ERR("Getting kernel max_sb_size");
212 * The original value is sent back if max stream size is larger than
213 * the possible size of the snapshot. Also, we asume that the session
214 * daemon should never send a maximum stream size that is lower than
217 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
218 produced_pos
, max_stream_size
);
220 while (consumed_pos
< produced_pos
) {
222 unsigned long len
, padded_len
;
224 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
226 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
228 if (errno
!= EAGAIN
) {
229 PERROR("kernctl_get_subbuf snapshot");
233 DBG("Kernel consumer get subbuf failed. Skipping it.");
234 consumed_pos
+= stream
->max_sb_size
;
238 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
240 ERR("Snapshot kernctl_get_subbuf_size");
242 goto error_put_subbuf
;
245 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
247 ERR("Snapshot kernctl_get_padded_subbuf_size");
249 goto error_put_subbuf
;
252 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
253 padded_len
- len
, NULL
);
255 * We write the padded len in local tracefiles but the data len
256 * when using a relay. Display the error but continue processing
257 * to try to release the subbuffer.
259 if (relayd_id
!= (uint64_t) -1ULL) {
260 if (read_len
!= len
) {
261 ERR("Error sending to the relay (ret: %zd != len: %lu)",
265 if (read_len
!= padded_len
) {
266 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
267 read_len
, padded_len
);
271 ret
= kernctl_put_subbuf(stream
->wait_fd
);
273 ERR("Snapshot kernctl_put_subbuf");
277 consumed_pos
+= stream
->max_sb_size
;
280 if (relayd_id
== (uint64_t) -1ULL) {
281 if (stream
->out_fd
>= 0) {
282 ret
= close(stream
->out_fd
);
284 PERROR("Kernel consumer snapshot close out_fd");
290 close_relayd_stream(stream
);
291 stream
->net_seq_idx
= (uint64_t) -1ULL;
293 pthread_mutex_unlock(&stream
->lock
);
301 ret
= kernctl_put_subbuf(stream
->wait_fd
);
304 ERR("Snapshot kernctl_put_subbuf error path");
307 pthread_mutex_unlock(&stream
->lock
);
314 * Read the whole metadata available for a snapshot.
316 * Returns 0 on success, < 0 on error
318 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
319 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
321 int ret
, use_relayd
= 0;
323 struct lttng_consumer_channel
*metadata_channel
;
324 struct lttng_consumer_stream
*metadata_stream
;
328 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
333 metadata_channel
= consumer_find_channel(key
);
334 if (!metadata_channel
) {
335 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
340 metadata_stream
= metadata_channel
->metadata_stream
;
341 assert(metadata_stream
);
343 /* Flag once that we have a valid relayd for the stream. */
344 if (relayd_id
!= (uint64_t) -1ULL) {
349 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
354 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
355 metadata_stream
->chan
->tracefile_size
,
356 metadata_stream
->tracefile_count_current
,
357 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
361 metadata_stream
->out_fd
= ret
;
365 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
367 if (ret_read
!= -EAGAIN
) {
368 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
372 /* ret_read is negative at this point so we will exit the loop. */
375 } while (ret_read
>= 0);
378 close_relayd_stream(metadata_stream
);
379 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
381 if (metadata_stream
->out_fd
>= 0) {
382 ret
= close(metadata_stream
->out_fd
);
384 PERROR("Kernel consumer snapshot metadata close out_fd");
386 * Don't go on error here since the snapshot was successful at this
387 * point but somehow the close failed.
390 metadata_stream
->out_fd
= -1;
396 cds_list_del(&metadata_stream
->send_node
);
397 consumer_stream_destroy(metadata_stream
, NULL
);
398 metadata_channel
->metadata_stream
= NULL
;
405 * Receive command from session daemon and process it.
407 * Return 1 on success else a negative value or 0.
409 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
410 int sock
, struct pollfd
*consumer_sockpoll
)
413 enum lttng_error_code ret_code
= LTTNG_OK
;
414 struct lttcomm_consumer_msg msg
;
416 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
417 if (ret
!= sizeof(msg
)) {
419 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
424 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
426 * Notify the session daemon that the command is completed.
428 * On transport layer error, the function call will print an error
429 * message so handling the returned code is a bit useless since we
430 * return an error code anyway.
432 (void) consumer_send_status_msg(sock
, ret_code
);
436 /* relayd needs RCU read-side protection */
439 switch (msg
.cmd_type
) {
440 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
442 /* Session daemon status message are handled in the following call. */
443 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
444 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
445 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
446 msg
.u
.relayd_sock
.relayd_session_id
);
449 case LTTNG_CONSUMER_ADD_CHANNEL
:
451 struct lttng_consumer_channel
*new_channel
;
454 /* First send a status message before receiving the fds. */
455 ret
= consumer_send_status_msg(sock
, ret_code
);
457 /* Somehow, the session daemon is not responding anymore. */
460 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
461 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
462 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
463 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
464 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
465 msg
.u
.channel
.tracefile_size
,
466 msg
.u
.channel
.tracefile_count
, 0,
467 msg
.u
.channel
.monitor
,
468 msg
.u
.channel
.live_timer_interval
);
469 if (new_channel
== NULL
) {
470 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
473 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
474 switch (msg
.u
.channel
.output
) {
475 case LTTNG_EVENT_SPLICE
:
476 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
478 case LTTNG_EVENT_MMAP
:
479 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
482 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
486 /* Translate and save channel type. */
487 switch (msg
.u
.channel
.type
) {
488 case CONSUMER_CHANNEL_TYPE_DATA
:
489 case CONSUMER_CHANNEL_TYPE_METADATA
:
490 new_channel
->type
= msg
.u
.channel
.type
;
497 if (ctx
->on_recv_channel
!= NULL
) {
498 ret_recv
= ctx
->on_recv_channel(new_channel
);
500 ret
= consumer_add_channel(new_channel
, ctx
);
501 } else if (ret_recv
< 0) {
505 ret
= consumer_add_channel(new_channel
, ctx
);
507 consumer_timer_live_start(new_channel
, msg
.u
.channel
.live_timer_interval
);
509 /* If we received an error in add_channel, we need to report it. */
511 ret
= consumer_send_status_msg(sock
, ret
);
520 case LTTNG_CONSUMER_ADD_STREAM
:
523 struct lttng_pipe
*stream_pipe
;
524 struct lttng_consumer_stream
*new_stream
;
525 struct lttng_consumer_channel
*channel
;
529 * Get stream's channel reference. Needed when adding the stream to the
532 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
535 * We could not find the channel. Can happen if cpu hotplug
536 * happens while tearing down.
538 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
539 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
542 /* First send a status message before receiving the fds. */
543 ret
= consumer_send_status_msg(sock
, ret_code
);
545 /* Somehow, the session daemon is not responding anymore. */
548 if (ret_code
!= LTTNG_OK
) {
549 /* Channel was not found. */
554 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
559 /* Get stream file descriptor from socket */
560 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
561 if (ret
!= sizeof(fd
)) {
562 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
568 * Send status code to session daemon only if the recv works. If the
569 * above recv() failed, the session daemon is notified through the
570 * error socket and the teardown is eventually done.
572 ret
= consumer_send_status_msg(sock
, ret_code
);
574 /* Somehow, the session daemon is not responding anymore. */
578 new_stream
= consumer_allocate_stream(channel
->key
,
580 LTTNG_CONSUMER_ACTIVE_STREAM
,
590 if (new_stream
== NULL
) {
595 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
601 new_stream
->chan
= channel
;
602 new_stream
->wait_fd
= fd
;
603 switch (channel
->output
) {
604 case CONSUMER_CHANNEL_SPLICE
:
605 new_stream
->output
= LTTNG_EVENT_SPLICE
;
607 case CONSUMER_CHANNEL_MMAP
:
608 new_stream
->output
= LTTNG_EVENT_MMAP
;
611 ERR("Stream output unknown %d", channel
->output
);
616 * We've just assigned the channel to the stream so increment the
617 * refcount right now. We don't need to increment the refcount for
618 * streams in no monitor because we handle manually the cleanup of
619 * those. It is very important to make sure there is NO prior
620 * consumer_del_stream() calls or else the refcount will be unbalanced.
622 if (channel
->monitor
) {
623 uatomic_inc(&new_stream
->chan
->refcount
);
627 * The buffer flush is done on the session daemon side for the kernel
628 * so no need for the stream "hangup_flush_done" variable to be
629 * tracked. This is important for a kernel stream since we don't rely
630 * on the flush state of the stream to read data. It's not the case for
631 * user space tracing.
633 new_stream
->hangup_flush_done
= 0;
635 if (ctx
->on_recv_stream
) {
636 ret
= ctx
->on_recv_stream(new_stream
);
638 consumer_stream_free(new_stream
);
643 if (new_stream
->metadata_flag
) {
644 channel
->metadata_stream
= new_stream
;
647 /* Do not monitor this stream. */
648 if (!channel
->monitor
) {
649 DBG("Kernel consumer add stream %s in no monitor mode with "
650 "relayd id %" PRIu64
, new_stream
->name
,
651 new_stream
->net_seq_idx
);
652 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
656 /* Send stream to relayd if the stream has an ID. */
657 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
658 ret
= consumer_send_relayd_stream(new_stream
,
659 new_stream
->chan
->pathname
);
661 consumer_stream_free(new_stream
);
666 /* Get the right pipe where the stream will be sent. */
667 if (new_stream
->metadata_flag
) {
668 ret
= consumer_add_metadata_stream(new_stream
);
670 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
672 consumer_stream_free(new_stream
);
675 stream_pipe
= ctx
->consumer_metadata_pipe
;
677 ret
= consumer_add_data_stream(new_stream
);
679 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
681 consumer_stream_free(new_stream
);
684 stream_pipe
= ctx
->consumer_data_pipe
;
687 /* Vitible to other threads */
688 new_stream
->globally_visible
= 1;
690 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
692 ERR("Consumer write %s stream to pipe %d",
693 new_stream
->metadata_flag
? "metadata" : "data",
694 lttng_pipe_get_writefd(stream_pipe
));
695 if (new_stream
->metadata_flag
) {
696 consumer_del_stream_for_metadata(new_stream
);
698 consumer_del_stream_for_data(new_stream
);
703 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
704 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
707 case LTTNG_CONSUMER_UPDATE_STREAM
:
712 case LTTNG_CONSUMER_DESTROY_RELAYD
:
714 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
715 struct consumer_relayd_sock_pair
*relayd
;
717 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
719 /* Get relayd reference if exists. */
720 relayd
= consumer_find_relayd(index
);
721 if (relayd
== NULL
) {
722 DBG("Unable to find relayd %" PRIu64
, index
);
723 ret_code
= LTTNG_ERR_NO_CONSUMER
;
727 * Each relayd socket pair has a refcount of stream attached to it
728 * which tells if the relayd is still active or not depending on the
731 * This will set the destroy flag of the relayd object and destroy it
732 * if the refcount reaches zero when called.
734 * The destroy can happen either here or when a stream fd hangs up.
737 consumer_flag_relayd_for_destroy(relayd
);
740 ret
= consumer_send_status_msg(sock
, ret_code
);
742 /* Somehow, the session daemon is not responding anymore. */
748 case LTTNG_CONSUMER_DATA_PENDING
:
751 uint64_t id
= msg
.u
.data_pending
.session_id
;
753 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
755 ret
= consumer_data_pending(id
);
757 /* Send back returned value to session daemon */
758 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
760 PERROR("send data pending ret code");
765 * No need to send back a status message since the data pending
766 * returned value is the response.
770 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
772 if (msg
.u
.snapshot_channel
.metadata
== 1) {
773 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
774 msg
.u
.snapshot_channel
.pathname
,
775 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
777 ERR("Snapshot metadata failed");
778 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
781 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
782 msg
.u
.snapshot_channel
.pathname
,
783 msg
.u
.snapshot_channel
.relayd_id
,
784 msg
.u
.snapshot_channel
.max_stream_size
,
787 ERR("Snapshot channel failed");
788 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
792 ret
= consumer_send_status_msg(sock
, ret_code
);
794 /* Somehow, the session daemon is not responding anymore. */
799 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
801 uint64_t key
= msg
.u
.destroy_channel
.key
;
802 struct lttng_consumer_channel
*channel
;
804 channel
= consumer_find_channel(key
);
806 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
807 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
810 ret
= consumer_send_status_msg(sock
, ret_code
);
812 /* Somehow, the session daemon is not responding anymore. */
817 * This command should ONLY be issued for channel with streams set in
820 assert(!channel
->monitor
);
823 * The refcount should ALWAYS be 0 in the case of a channel in no
826 assert(!uatomic_sub_return(&channel
->refcount
, 1));
828 consumer_del_channel(channel
);
840 * Return 1 to indicate success since the 0 value can be a socket
841 * shutdown during the recv() or send() call.
847 /* This will issue a consumer stop. */
852 * Populate index values of a kernel stream. Values are set in big endian order.
854 * Return 0 on success or else a negative value.
856 static int get_index_values(struct lttng_packet_index
*index
, int infd
)
860 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
862 PERROR("kernctl_get_timestamp_begin");
865 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
867 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
869 PERROR("kernctl_get_timestamp_end");
872 index
->timestamp_end
= htobe64(index
->timestamp_end
);
874 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
876 PERROR("kernctl_get_events_discarded");
879 index
->events_discarded
= htobe64(index
->events_discarded
);
881 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
883 PERROR("kernctl_get_content_size");
886 index
->content_size
= htobe64(index
->content_size
);
888 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
890 PERROR("kernctl_get_packet_size");
893 index
->packet_size
= htobe64(index
->packet_size
);
895 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
897 PERROR("kernctl_get_stream_id");
900 index
->stream_id
= htobe64(index
->stream_id
);
907 * Consume data on a file descriptor and write it on a trace file.
909 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
910 struct lttng_consumer_local_data
*ctx
)
912 unsigned long len
, subbuf_size
, padding
;
913 int err
, write_index
= 1;
915 int infd
= stream
->wait_fd
;
916 struct lttng_packet_index index
;
918 DBG("In read_subbuffer (infd : %d)", infd
);
920 /* Get the next subbuffer */
921 err
= kernctl_get_next_subbuf(infd
);
924 * This is a debug message even for single-threaded consumer,
925 * because poll() have more relaxed criterions than get subbuf,
926 * so get_subbuf may fail for short race windows where poll()
927 * would issue wakeups.
929 DBG("Reserving sub buffer failed (everything is normal, "
930 "it is due to concurrency)");
935 /* Get the full subbuffer size including padding */
936 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
938 perror("Getting sub-buffer len failed.");
943 if (!stream
->metadata_flag
) {
944 ret
= get_index_values(&index
, infd
);
952 switch (stream
->chan
->output
) {
953 case CONSUMER_CHANNEL_SPLICE
:
955 * XXX: The lttng-modules splice "actor" does not handle copying
956 * partial pages hence only using the subbuffer size without the
957 * padding makes the splice fail.
962 /* splice the subbuffer to the tracefile */
963 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
966 * XXX: Splice does not support network streaming so the return value
967 * is simply checked against subbuf_size and not like the mmap() op.
969 if (ret
!= subbuf_size
) {
971 * display the error but continue processing to try
972 * to release the subbuffer
974 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
979 case CONSUMER_CHANNEL_MMAP
:
980 /* Get subbuffer size without padding */
981 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
983 perror("Getting sub-buffer len failed.");
988 /* Make sure the tracer is not gone mad on us! */
989 assert(len
>= subbuf_size
);
991 padding
= len
- subbuf_size
;
993 /* write the subbuffer to the tracefile */
994 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
997 * The mmap operation should write subbuf_size amount of data when
998 * network streaming or the full padding (len) size when we are _not_
1001 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1002 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1004 * Display the error but continue processing to try to release the
1007 ERR("Error writing to tracefile "
1008 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1009 ret
, len
, subbuf_size
);
1014 ERR("Unknown output method");
1018 err
= kernctl_put_next_subbuf(infd
);
1020 if (errno
== EFAULT
) {
1021 perror("Error in unreserving sub buffer\n");
1022 } else if (errno
== EIO
) {
1023 /* Should never happen with newer LTTng versions */
1024 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1030 /* Write index if needed. */
1035 err
= consumer_stream_write_index(stream
, &index
);
1044 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1051 * Don't create anything if this is set for streaming or should not be
1054 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1055 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1056 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1057 stream
->uid
, stream
->gid
, NULL
);
1061 stream
->out_fd
= ret
;
1062 stream
->tracefile_size_current
= 0;
1064 if (!stream
->metadata_flag
) {
1065 ret
= index_create_file(stream
->chan
->pathname
,
1066 stream
->name
, stream
->uid
, stream
->gid
,
1067 stream
->chan
->tracefile_size
,
1068 stream
->tracefile_count_current
);
1072 stream
->index_fd
= ret
;
1076 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1077 /* get the len of the mmap region */
1078 unsigned long mmap_len
;
1080 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1082 PERROR("kernctl_get_mmap_len");
1084 goto error_close_fd
;
1086 stream
->mmap_len
= (size_t) mmap_len
;
1088 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1089 MAP_PRIVATE
, stream
->wait_fd
, 0);
1090 if (stream
->mmap_base
== MAP_FAILED
) {
1091 PERROR("Error mmaping");
1093 goto error_close_fd
;
1097 /* we return 0 to let the library handle the FD internally */
1101 if (stream
->out_fd
>= 0) {
1104 err
= close(stream
->out_fd
);
1106 stream
->out_fd
= -1;
1113 * Check if data is still being extracted from the buffers for a specific
1114 * stream. Consumer data lock MUST be acquired before calling this function
1115 * and the stream lock.
1117 * Return 1 if the traced data are still getting read else 0 meaning that the
1118 * data is available for trace viewer reading.
1120 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1126 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1131 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1133 /* There is still data so let's put back this subbuffer. */
1134 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1136 ret
= 1; /* Data is pending */
1140 /* Data is NOT pending and ready to be read. */