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>
42 #include "kernel-consumer.h"
44 extern struct lttng_consumer_global_data consumer_data
;
45 extern int consumer_poll_timeout
;
46 extern volatile int consumer_quit
;
49 * Take a snapshot for a specific fd
51 * Returns 0 on success, < 0 on error
53 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
56 int infd
= stream
->wait_fd
;
58 ret
= kernctl_snapshot(infd
);
61 perror("Getting sub-buffer snapshot.");
68 * Get the produced position
70 * Returns 0 on success, < 0 on error
72 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
76 int infd
= stream
->wait_fd
;
78 ret
= kernctl_snapshot_get_produced(infd
, pos
);
81 perror("kernctl_snapshot_get_produced");
88 * Get the consumerd position
90 * Returns 0 on success, < 0 on error
92 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
96 int infd
= stream
->wait_fd
;
98 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
101 perror("kernctl_snapshot_get_consumed");
108 * Find a relayd and send the stream
110 * Returns 0 on success, < 0 on error
113 int send_relayd_stream(struct lttng_consumer_stream
*stream
, char *path
)
115 struct consumer_relayd_sock_pair
*relayd
;
122 stream_path
= stream
->chan
->pathname
;
124 /* The stream is not metadata. Get relayd reference if exists. */
126 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
127 if (relayd
!= NULL
) {
128 /* Add stream on the relayd */
129 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
130 ret
= relayd_add_stream(&relayd
->control_sock
,
131 stream
->name
, stream_path
,
132 &stream
->relayd_stream_id
,
133 stream
->chan
->tracefile_size
,
134 stream
->chan
->tracefile_count
);
135 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
139 uatomic_inc(&relayd
->refcount
);
140 } else if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
141 ERR("Network sequence index %" PRIu64
" unknown. Not adding stream.",
142 stream
->net_seq_idx
);
153 * Find a relayd and close the stream
156 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
158 struct consumer_relayd_sock_pair
*relayd
;
160 /* The stream is not metadata. Get relayd reference if exists. */
162 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
163 if (relayd
!= NULL
) {
164 consumer_stream_relayd_close(stream
, relayd
);
170 * Take a snapshot of all the stream of a channel
172 * Returns 0 on success, < 0 on error
174 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
175 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
178 unsigned long consumed_pos
, produced_pos
;
179 struct lttng_consumer_channel
*channel
;
180 struct lttng_consumer_stream
*stream
;
182 DBG("Kernel consumer snapshot channel %lu", key
);
186 channel
= consumer_find_channel(key
);
188 ERR("No channel found for key %lu", key
);
193 /* Splice is not supported yet for channel snapshot. */
194 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
195 ERR("Unsupported output %d", channel
->output
);
200 cds_list_for_each_entry(stream
, &channel
->stream_no_monitor_list
.head
,
203 * Lock stream because we are about to change its state.
205 pthread_mutex_lock(&stream
->lock
);
207 stream
->net_seq_idx
= relayd_id
;
208 channel
->relayd_id
= relayd_id
;
209 if (relayd_id
!= (uint64_t) -1ULL) {
210 ret
= send_relayd_stream(stream
, path
);
212 ERR("sending stream to relayd");
215 DBG("Stream %s sent to the relayd", stream
->name
);
217 ret
= utils_create_stream_file(path
, stream
->name
,
218 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
219 stream
->uid
, stream
->gid
);
221 ERR("utils_create_stream_file");
225 stream
->out_fd
= ret
;
226 stream
->tracefile_size_current
= 0;
228 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path
,
229 stream
->name
, stream
->key
);
232 ret
= kernctl_buffer_flush(stream
->wait_fd
);
234 ERR("Failed to flush kernel metadata stream");
238 ret
= lttng_kconsumer_take_snapshot(stream
);
240 ERR("Taking kernel snapshot");
244 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
246 ERR("Produced kernel snapshot position");
250 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
252 ERR("Consumerd kernel snapshot position");
256 if (stream
->max_sb_size
== 0) {
257 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
258 &stream
->max_sb_size
);
260 ERR("Getting kernel max_sb_size");
265 while (consumed_pos
< produced_pos
) {
267 unsigned long len
, padded_len
;
269 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
271 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
273 if (errno
!= EAGAIN
) {
274 PERROR("kernctl_get_subbuf snapshot");
277 DBG("Kernel consumer get subbuf failed. Skipping it.");
278 consumed_pos
+= stream
->max_sb_size
;
282 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
284 ERR("Snapshot kernctl_get_subbuf_size");
288 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
290 ERR("Snapshot kernctl_get_padded_subbuf_size");
294 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
297 * We write the padded len in local tracefiles but the
298 * data len when using a relay.
299 * Display the error but continue processing to try to
300 * release the subbuffer.
302 if (relayd_id
!= (uint64_t) -1ULL) {
303 if (read_len
!= len
) {
304 ERR("Error sending to the relay (ret: %zd != len: %lu)",
308 if (read_len
!= padded_len
) {
309 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
310 read_len
, padded_len
);
314 ret
= kernctl_put_subbuf(stream
->wait_fd
);
316 ERR("Snapshot kernctl_put_subbuf");
319 consumed_pos
+= stream
->max_sb_size
;
322 if (relayd_id
== (uint64_t) -1ULL) {
323 ret
= close(stream
->out_fd
);
325 PERROR("Kernel consumer snapshot close out_fd");
330 close_relayd_stream(stream
);
331 stream
->net_seq_idx
= (uint64_t) -1ULL;
333 pthread_mutex_unlock(&stream
->lock
);
341 pthread_mutex_unlock(&stream
->lock
);
348 * Read the whole metadata available for a snapshot.
350 * Returns 0 on success, < 0 on error
352 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
353 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
355 struct lttng_consumer_channel
*metadata_channel
;
356 struct lttng_consumer_stream
*metadata_stream
;
359 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
364 metadata_channel
= consumer_find_channel(key
);
365 if (!metadata_channel
) {
366 ERR("Snapshot kernel metadata channel not found for key %lu", key
);
371 metadata_stream
= metadata_channel
->metadata_stream
;
372 assert(metadata_stream
);
374 if (relayd_id
!= (uint64_t) -1ULL) {
375 ret
= send_relayd_stream(metadata_stream
, path
);
377 ERR("sending stream to relayd");
379 DBG("Stream %s sent to the relayd", metadata_stream
->name
);
381 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
382 metadata_stream
->chan
->tracefile_size
,
383 metadata_stream
->tracefile_count_current
,
384 metadata_stream
->uid
, metadata_stream
->gid
);
388 metadata_stream
->out_fd
= ret
;
393 ret
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
396 ERR("Kernel snapshot reading subbuffer");
399 /* "ret" is negative at this point so we will exit the loop. */
404 if (relayd_id
== (uint64_t) -1ULL) {
405 ret
= close(metadata_stream
->out_fd
);
407 PERROR("Kernel consumer snapshot close out_fd");
410 metadata_stream
->out_fd
= -1;
412 close_relayd_stream(metadata_stream
);
413 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
423 * Receive command from session daemon and process it.
425 * Return 1 on success else a negative value or 0.
427 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
428 int sock
, struct pollfd
*consumer_sockpoll
)
431 enum lttng_error_code ret_code
= LTTNG_OK
;
432 struct lttcomm_consumer_msg msg
;
434 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
435 if (ret
!= sizeof(msg
)) {
436 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
442 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
444 * Notify the session daemon that the command is completed.
446 * On transport layer error, the function call will print an error
447 * message so handling the returned code is a bit useless since we
448 * return an error code anyway.
450 (void) consumer_send_status_msg(sock
, ret_code
);
454 /* relayd needs RCU read-side protection */
457 switch (msg
.cmd_type
) {
458 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
460 /* Session daemon status message are handled in the following call. */
461 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
462 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
463 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
466 case LTTNG_CONSUMER_ADD_CHANNEL
:
468 struct lttng_consumer_channel
*new_channel
;
471 /* First send a status message before receiving the fds. */
472 ret
= consumer_send_status_msg(sock
, ret_code
);
474 /* Somehow, the session daemon is not responding anymore. */
477 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
478 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
479 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
480 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
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 if (new_channel
== NULL
) {
486 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
489 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
490 switch (msg
.u
.channel
.output
) {
491 case LTTNG_EVENT_SPLICE
:
492 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
494 case LTTNG_EVENT_MMAP
:
495 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
498 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
502 /* Translate and save channel type. */
503 switch (msg
.u
.channel
.type
) {
504 case CONSUMER_CHANNEL_TYPE_DATA
:
505 case CONSUMER_CHANNEL_TYPE_METADATA
:
506 new_channel
->type
= msg
.u
.channel
.type
;
513 if (ctx
->on_recv_channel
!= NULL
) {
514 ret_recv
= ctx
->on_recv_channel(new_channel
);
516 ret
= consumer_add_channel(new_channel
, ctx
);
517 } else if (ret_recv
< 0) {
521 ret
= consumer_add_channel(new_channel
, ctx
);
524 /* If we received an error in add_channel, we need to report it. */
526 ret
= consumer_send_status_msg(sock
, ret
);
535 case LTTNG_CONSUMER_ADD_STREAM
:
538 struct lttng_pipe
*stream_pipe
;
539 struct lttng_consumer_stream
*new_stream
;
540 struct lttng_consumer_channel
*channel
;
544 * Get stream's channel reference. Needed when adding the stream to the
547 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
550 * We could not find the channel. Can happen if cpu hotplug
551 * happens while tearing down.
553 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
554 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
557 /* First send a status message before receiving the fds. */
558 ret
= consumer_send_status_msg(sock
, ret_code
);
561 * Somehow, the session daemon is not responding
566 if (ret_code
!= LTTNG_OK
) {
568 * Channel was not found.
574 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
579 /* Get stream file descriptor from socket */
580 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
581 if (ret
!= sizeof(fd
)) {
582 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
588 * Send status code to session daemon only if the recv works. If the
589 * above recv() failed, the session daemon is notified through the
590 * error socket and the teardown is eventually done.
592 ret
= consumer_send_status_msg(sock
, ret_code
);
594 /* Somehow, the session daemon is not responding anymore. */
598 new_stream
= consumer_allocate_stream(channel
->key
,
600 LTTNG_CONSUMER_ACTIVE_STREAM
,
609 if (new_stream
== NULL
) {
614 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
619 new_stream
->chan
= channel
;
620 new_stream
->wait_fd
= fd
;
621 switch (channel
->output
) {
622 case CONSUMER_CHANNEL_SPLICE
:
623 new_stream
->output
= LTTNG_EVENT_SPLICE
;
625 case CONSUMER_CHANNEL_MMAP
:
626 new_stream
->output
= LTTNG_EVENT_MMAP
;
629 ERR("Stream output unknown %d", channel
->output
);
634 * We've just assigned the channel to the stream so increment the
635 * refcount right now. We don't need to increment the refcount for
636 * streams in no monitor because we handle manually the cleanup of
637 * those. It is very important to make sure there is NO prior
638 * consumer_del_stream() calls or else the refcount will be unbalanced.
640 if (channel
->monitor
) {
641 uatomic_inc(&new_stream
->chan
->refcount
);
645 * The buffer flush is done on the session daemon side for the kernel
646 * so no need for the stream "hangup_flush_done" variable to be
647 * tracked. This is important for a kernel stream since we don't rely
648 * on the flush state of the stream to read data. It's not the case for
649 * user space tracing.
651 new_stream
->hangup_flush_done
= 0;
653 if (ctx
->on_recv_stream
) {
654 ret
= ctx
->on_recv_stream(new_stream
);
656 consumer_del_stream(new_stream
, NULL
);
661 if (new_stream
->metadata_flag
) {
662 channel
->metadata_stream
= new_stream
;
665 /* Do not monitor this stream. */
666 if (!channel
->monitor
) {
667 DBG("Kernel consumer add stream %s in no monitor mode with "
668 "relayd id %" PRIu64
, new_stream
->name
,
669 new_stream
->net_seq_idx
);
670 cds_list_add(&new_stream
->no_monitor_node
,
671 &channel
->stream_no_monitor_list
.head
);
675 ret
= send_relayd_stream(new_stream
, NULL
);
677 consumer_del_stream(new_stream
, NULL
);
681 /* Get the right pipe where the stream will be sent. */
682 if (new_stream
->metadata_flag
) {
683 stream_pipe
= ctx
->consumer_metadata_pipe
;
685 stream_pipe
= ctx
->consumer_data_pipe
;
688 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
690 ERR("Consumer write %s stream to pipe %d",
691 new_stream
->metadata_flag
? "metadata" : "data",
692 lttng_pipe_get_writefd(stream_pipe
));
693 consumer_del_stream(new_stream
, NULL
);
697 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
698 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
701 case LTTNG_CONSUMER_UPDATE_STREAM
:
706 case LTTNG_CONSUMER_DESTROY_RELAYD
:
708 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
709 struct consumer_relayd_sock_pair
*relayd
;
711 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
713 /* Get relayd reference if exists. */
714 relayd
= consumer_find_relayd(index
);
715 if (relayd
== NULL
) {
716 DBG("Unable to find relayd %" PRIu64
, index
);
717 ret_code
= LTTNG_ERR_NO_CONSUMER
;
721 * Each relayd socket pair has a refcount of stream attached to it
722 * which tells if the relayd is still active or not depending on the
725 * This will set the destroy flag of the relayd object and destroy it
726 * if the refcount reaches zero when called.
728 * The destroy can happen either here or when a stream fd hangs up.
731 consumer_flag_relayd_for_destroy(relayd
);
734 ret
= consumer_send_status_msg(sock
, ret_code
);
736 /* Somehow, the session daemon is not responding anymore. */
742 case LTTNG_CONSUMER_DATA_PENDING
:
745 uint64_t id
= msg
.u
.data_pending
.session_id
;
747 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
749 ret
= consumer_data_pending(id
);
751 /* Send back returned value to session daemon */
752 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
754 PERROR("send data pending ret code");
759 * No need to send back a status message since the data pending
760 * returned value is the response.
764 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
766 if (msg
.u
.snapshot_channel
.metadata
== 1) {
767 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
768 msg
.u
.snapshot_channel
.pathname
,
769 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
771 ERR("Snapshot metadata failed");
772 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
775 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
776 msg
.u
.snapshot_channel
.pathname
,
777 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
779 ERR("Snapshot channel failed");
780 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
784 ret
= consumer_send_status_msg(sock
, ret_code
);
786 /* Somehow, the session daemon is not responding anymore. */
791 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
793 uint64_t key
= msg
.u
.destroy_channel
.key
;
794 struct lttng_consumer_channel
*channel
;
796 channel
= consumer_find_channel(key
);
798 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
799 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
802 ret
= consumer_send_status_msg(sock
, ret_code
);
804 /* Somehow, the session daemon is not responding anymore. */
809 * This command should ONLY be issued for channel with streams set in
812 assert(!channel
->monitor
);
815 * The refcount should ALWAYS be 0 in the case of a channel in no
818 assert(!uatomic_sub_return(&channel
->refcount
, 1));
820 consumer_del_channel(channel
);
832 * Return 1 to indicate success since the 0 value can be a socket
833 * shutdown during the recv() or send() call.
839 /* This will issue a consumer stop. */
844 * Consume data on a file descriptor and write it on a trace file.
846 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
847 struct lttng_consumer_local_data
*ctx
)
849 unsigned long len
, subbuf_size
, padding
;
852 int infd
= stream
->wait_fd
;
854 DBG("In read_subbuffer (infd : %d)", infd
);
855 /* Get the next subbuffer */
856 err
= kernctl_get_next_subbuf(infd
);
860 * This is a debug message even for single-threaded consumer,
861 * because poll() have more relaxed criterions than get subbuf,
862 * so get_subbuf may fail for short race windows where poll()
863 * would issue wakeups.
865 DBG("Reserving sub buffer failed (everything is normal, "
866 "it is due to concurrency)");
870 /* Get the full subbuffer size including padding */
871 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
874 perror("Getting sub-buffer len failed.");
879 switch (stream
->chan
->output
) {
880 case CONSUMER_CHANNEL_SPLICE
:
882 * XXX: The lttng-modules splice "actor" does not handle copying
883 * partial pages hence only using the subbuffer size without the
884 * padding makes the splice fail.
889 /* splice the subbuffer to the tracefile */
890 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
893 * XXX: Splice does not support network streaming so the return value
894 * is simply checked against subbuf_size and not like the mmap() op.
896 if (ret
!= subbuf_size
) {
898 * display the error but continue processing to try
899 * to release the subbuffer
901 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
905 case CONSUMER_CHANNEL_MMAP
:
906 /* Get subbuffer size without padding */
907 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
910 perror("Getting sub-buffer len failed.");
915 /* Make sure the tracer is not gone mad on us! */
916 assert(len
>= subbuf_size
);
918 padding
= len
- subbuf_size
;
920 /* write the subbuffer to the tracefile */
921 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
924 * The mmap operation should write subbuf_size amount of data when
925 * network streaming or the full padding (len) size when we are _not_
928 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
929 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
931 * Display the error but continue processing to try to release the
934 ERR("Error writing to tracefile "
935 "(ret: %zd != len: %lu != subbuf_size: %lu)",
936 ret
, len
, subbuf_size
);
940 ERR("Unknown output method");
944 err
= kernctl_put_next_subbuf(infd
);
947 if (errno
== EFAULT
) {
948 perror("Error in unreserving sub buffer\n");
949 } else if (errno
== EIO
) {
950 /* Should never happen with newer LTTng versions */
951 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
962 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
969 * Don't create anything if this is set for streaming or should not be
972 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
973 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
974 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
975 stream
->uid
, stream
->gid
);
979 stream
->out_fd
= ret
;
980 stream
->tracefile_size_current
= 0;
983 if (stream
->output
== LTTNG_EVENT_MMAP
) {
984 /* get the len of the mmap region */
985 unsigned long mmap_len
;
987 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
990 PERROR("kernctl_get_mmap_len");
993 stream
->mmap_len
= (size_t) mmap_len
;
995 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
996 MAP_PRIVATE
, stream
->wait_fd
, 0);
997 if (stream
->mmap_base
== MAP_FAILED
) {
998 PERROR("Error mmaping");
1000 goto error_close_fd
;
1004 /* we return 0 to let the library handle the FD internally */
1011 err
= close(stream
->out_fd
);
1019 * Check if data is still being extracted from the buffers for a specific
1020 * stream. Consumer data lock MUST be acquired before calling this function
1021 * and the stream lock.
1023 * Return 1 if the traced data are still getting read else 0 meaning that the
1024 * data is available for trace viewer reading.
1026 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1032 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1034 /* There is still data so let's put back this subbuffer. */
1035 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1037 ret
= 1; /* Data is pending */
1041 /* Data is NOT pending and ready to be read. */