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 * Take a snapshot of all the stream of a channel
110 * Returns 0 on success, < 0 on error
112 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
113 uint64_t relayd_id
, uint64_t max_stream_size
,
114 struct lttng_consumer_local_data
*ctx
)
117 unsigned long consumed_pos
, produced_pos
;
118 struct lttng_consumer_channel
*channel
;
119 struct lttng_consumer_stream
*stream
;
121 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
125 channel
= consumer_find_channel(key
);
127 ERR("No channel found for key %" PRIu64
, key
);
132 /* Splice is not supported yet for channel snapshot. */
133 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
134 ERR("Unsupported output %d", channel
->output
);
139 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
141 * Lock stream because we are about to change its state.
143 pthread_mutex_lock(&stream
->lock
);
146 * Assign the received relayd ID so we can use it for streaming. The streams
147 * are not visible to anyone so this is OK to change it.
149 stream
->net_seq_idx
= relayd_id
;
150 channel
->relayd_id
= relayd_id
;
151 if (relayd_id
!= (uint64_t) -1ULL) {
152 ret
= consumer_send_relayd_stream(stream
, path
);
154 ERR("sending stream to relayd");
158 ret
= utils_create_stream_file(path
, stream
->name
,
159 stream
->chan
->tracefile_size
,
160 stream
->tracefile_count_current
,
161 stream
->uid
, stream
->gid
);
163 ERR("utils_create_stream_file");
167 stream
->out_fd
= ret
;
168 stream
->tracefile_size_current
= 0;
170 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path
,
171 stream
->name
, stream
->key
);
174 ret
= kernctl_buffer_flush(stream
->wait_fd
);
176 ERR("Failed to flush kernel stream");
180 ret
= lttng_kconsumer_take_snapshot(stream
);
182 ERR("Taking kernel snapshot");
186 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
188 ERR("Produced kernel snapshot position");
192 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
194 ERR("Consumerd kernel snapshot position");
198 if (stream
->max_sb_size
== 0) {
199 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
200 &stream
->max_sb_size
);
202 ERR("Getting kernel max_sb_size");
208 * The original value is sent back if max stream size is larger than
209 * the possible size of the snapshot. Also, we asume that the session
210 * daemon should never send a maximum stream size that is lower than
213 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
214 produced_pos
, max_stream_size
);
216 while (consumed_pos
< produced_pos
) {
218 unsigned long len
, padded_len
;
220 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
222 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
224 if (errno
!= EAGAIN
) {
225 PERROR("kernctl_get_subbuf snapshot");
228 DBG("Kernel consumer get subbuf failed. Skipping it.");
229 consumed_pos
+= stream
->max_sb_size
;
233 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
235 ERR("Snapshot kernctl_get_subbuf_size");
236 goto error_put_subbuf
;
239 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
241 ERR("Snapshot kernctl_get_padded_subbuf_size");
242 goto error_put_subbuf
;
245 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
248 * We write the padded len in local tracefiles but the data len
249 * when using a relay. Display the error but continue processing
250 * to try to release the subbuffer.
252 if (relayd_id
!= (uint64_t) -1ULL) {
253 if (read_len
!= len
) {
254 ERR("Error sending to the relay (ret: %zd != len: %lu)",
258 if (read_len
!= padded_len
) {
259 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
260 read_len
, padded_len
);
264 ret
= kernctl_put_subbuf(stream
->wait_fd
);
266 ERR("Snapshot kernctl_put_subbuf");
269 consumed_pos
+= stream
->max_sb_size
;
272 if (relayd_id
== (uint64_t) -1ULL) {
273 if (stream
->out_fd
>= 0) {
274 ret
= close(stream
->out_fd
);
276 PERROR("Kernel consumer snapshot close out_fd");
282 close_relayd_stream(stream
);
283 stream
->net_seq_idx
= (uint64_t) -1ULL;
285 pthread_mutex_unlock(&stream
->lock
);
293 ret
= kernctl_put_subbuf(stream
->wait_fd
);
295 ERR("Snapshot kernctl_put_subbuf error path");
298 pthread_mutex_unlock(&stream
->lock
);
305 * Read the whole metadata available for a snapshot.
307 * Returns 0 on success, < 0 on error
309 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
310 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
312 int ret
, use_relayd
= 0;
314 struct lttng_consumer_channel
*metadata_channel
;
315 struct lttng_consumer_stream
*metadata_stream
;
319 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
324 metadata_channel
= consumer_find_channel(key
);
325 if (!metadata_channel
) {
326 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
331 metadata_stream
= metadata_channel
->metadata_stream
;
332 assert(metadata_stream
);
334 /* Flag once that we have a valid relayd for the stream. */
335 if (relayd_id
!= (uint64_t) -1ULL) {
340 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
345 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
346 metadata_stream
->chan
->tracefile_size
,
347 metadata_stream
->tracefile_count_current
,
348 metadata_stream
->uid
, metadata_stream
->gid
);
352 metadata_stream
->out_fd
= ret
;
356 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
358 if (ret_read
!= -EPERM
) {
359 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
363 /* ret_read is negative at this point so we will exit the loop. */
366 } while (ret_read
>= 0);
369 close_relayd_stream(metadata_stream
);
370 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
372 if (metadata_stream
->out_fd
>= 0) {
373 ret
= close(metadata_stream
->out_fd
);
375 PERROR("Kernel consumer snapshot metadata close out_fd");
377 * Don't go on error here since the snapshot was successful at this
378 * point but somehow the close failed.
381 metadata_stream
->out_fd
= -1;
387 cds_list_del(&metadata_stream
->send_node
);
388 consumer_stream_destroy(metadata_stream
, NULL
);
389 metadata_channel
->metadata_stream
= NULL
;
396 * Receive command from session daemon and process it.
398 * Return 1 on success else a negative value or 0.
400 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
401 int sock
, struct pollfd
*consumer_sockpoll
)
404 enum lttng_error_code ret_code
= LTTNG_OK
;
405 struct lttcomm_consumer_msg msg
;
407 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
408 if (ret
!= sizeof(msg
)) {
410 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
415 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
417 * Notify the session daemon that the command is completed.
419 * On transport layer error, the function call will print an error
420 * message so handling the returned code is a bit useless since we
421 * return an error code anyway.
423 (void) consumer_send_status_msg(sock
, ret_code
);
427 /* relayd needs RCU read-side protection */
430 switch (msg
.cmd_type
) {
431 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
433 /* Session daemon status message are handled in the following call. */
434 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
435 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
436 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
439 case LTTNG_CONSUMER_ADD_CHANNEL
:
441 struct lttng_consumer_channel
*new_channel
;
444 /* First send a status message before receiving the fds. */
445 ret
= consumer_send_status_msg(sock
, ret_code
);
447 /* Somehow, the session daemon is not responding anymore. */
450 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
451 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
452 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
453 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
454 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
455 msg
.u
.channel
.tracefile_size
,
456 msg
.u
.channel
.tracefile_count
, 0,
457 msg
.u
.channel
.monitor
);
458 if (new_channel
== NULL
) {
459 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
462 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
463 switch (msg
.u
.channel
.output
) {
464 case LTTNG_EVENT_SPLICE
:
465 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
467 case LTTNG_EVENT_MMAP
:
468 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
471 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
475 /* Translate and save channel type. */
476 switch (msg
.u
.channel
.type
) {
477 case CONSUMER_CHANNEL_TYPE_DATA
:
478 case CONSUMER_CHANNEL_TYPE_METADATA
:
479 new_channel
->type
= msg
.u
.channel
.type
;
486 if (ctx
->on_recv_channel
!= NULL
) {
487 ret_recv
= ctx
->on_recv_channel(new_channel
);
489 ret
= consumer_add_channel(new_channel
, ctx
);
490 } else if (ret_recv
< 0) {
494 ret
= consumer_add_channel(new_channel
, ctx
);
497 /* If we received an error in add_channel, we need to report it. */
499 ret
= consumer_send_status_msg(sock
, ret
);
508 case LTTNG_CONSUMER_ADD_STREAM
:
511 struct lttng_pipe
*stream_pipe
;
512 struct lttng_consumer_stream
*new_stream
;
513 struct lttng_consumer_channel
*channel
;
517 * Get stream's channel reference. Needed when adding the stream to the
520 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
523 * We could not find the channel. Can happen if cpu hotplug
524 * happens while tearing down.
526 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
527 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
530 /* First send a status message before receiving the fds. */
531 ret
= consumer_send_status_msg(sock
, ret_code
);
533 /* Somehow, the session daemon is not responding anymore. */
536 if (ret_code
!= LTTNG_OK
) {
537 /* Channel was not found. */
542 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
547 /* Get stream file descriptor from socket */
548 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
549 if (ret
!= sizeof(fd
)) {
550 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
556 * Send status code to session daemon only if the recv works. If the
557 * above recv() failed, the session daemon is notified through the
558 * error socket and the teardown is eventually done.
560 ret
= consumer_send_status_msg(sock
, ret_code
);
562 /* Somehow, the session daemon is not responding anymore. */
566 new_stream
= consumer_allocate_stream(channel
->key
,
568 LTTNG_CONSUMER_ACTIVE_STREAM
,
578 if (new_stream
== NULL
) {
583 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
589 new_stream
->chan
= channel
;
590 new_stream
->wait_fd
= fd
;
591 switch (channel
->output
) {
592 case CONSUMER_CHANNEL_SPLICE
:
593 new_stream
->output
= LTTNG_EVENT_SPLICE
;
595 case CONSUMER_CHANNEL_MMAP
:
596 new_stream
->output
= LTTNG_EVENT_MMAP
;
599 ERR("Stream output unknown %d", channel
->output
);
604 * We've just assigned the channel to the stream so increment the
605 * refcount right now. We don't need to increment the refcount for
606 * streams in no monitor because we handle manually the cleanup of
607 * those. It is very important to make sure there is NO prior
608 * consumer_del_stream() calls or else the refcount will be unbalanced.
610 if (channel
->monitor
) {
611 uatomic_inc(&new_stream
->chan
->refcount
);
615 * The buffer flush is done on the session daemon side for the kernel
616 * so no need for the stream "hangup_flush_done" variable to be
617 * tracked. This is important for a kernel stream since we don't rely
618 * on the flush state of the stream to read data. It's not the case for
619 * user space tracing.
621 new_stream
->hangup_flush_done
= 0;
623 if (ctx
->on_recv_stream
) {
624 ret
= ctx
->on_recv_stream(new_stream
);
626 consumer_stream_free(new_stream
);
631 if (new_stream
->metadata_flag
) {
632 channel
->metadata_stream
= new_stream
;
635 /* Do not monitor this stream. */
636 if (!channel
->monitor
) {
637 DBG("Kernel consumer add stream %s in no monitor mode with "
638 "relayd id %" PRIu64
, new_stream
->name
,
639 new_stream
->net_seq_idx
);
640 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
644 /* Send stream to relayd if the stream has an ID. */
645 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
646 ret
= consumer_send_relayd_stream(new_stream
,
647 new_stream
->chan
->pathname
);
649 consumer_stream_free(new_stream
);
654 /* Get the right pipe where the stream will be sent. */
655 if (new_stream
->metadata_flag
) {
656 ret
= consumer_add_metadata_stream(new_stream
);
658 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
660 consumer_stream_free(new_stream
);
663 stream_pipe
= ctx
->consumer_metadata_pipe
;
665 ret
= consumer_add_data_stream(new_stream
);
667 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
669 consumer_stream_free(new_stream
);
672 stream_pipe
= ctx
->consumer_data_pipe
;
675 /* Vitible to other threads */
676 new_stream
->globally_visible
= 1;
678 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
680 ERR("Consumer write %s stream to pipe %d",
681 new_stream
->metadata_flag
? "metadata" : "data",
682 lttng_pipe_get_writefd(stream_pipe
));
683 if (new_stream
->metadata_flag
) {
684 consumer_del_stream_for_metadata(new_stream
);
686 consumer_del_stream_for_data(new_stream
);
691 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
692 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
695 case LTTNG_CONSUMER_UPDATE_STREAM
:
700 case LTTNG_CONSUMER_DESTROY_RELAYD
:
702 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
703 struct consumer_relayd_sock_pair
*relayd
;
705 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
707 /* Get relayd reference if exists. */
708 relayd
= consumer_find_relayd(index
);
709 if (relayd
== NULL
) {
710 DBG("Unable to find relayd %" PRIu64
, index
);
711 ret_code
= LTTNG_ERR_NO_CONSUMER
;
715 * Each relayd socket pair has a refcount of stream attached to it
716 * which tells if the relayd is still active or not depending on the
719 * This will set the destroy flag of the relayd object and destroy it
720 * if the refcount reaches zero when called.
722 * The destroy can happen either here or when a stream fd hangs up.
725 consumer_flag_relayd_for_destroy(relayd
);
728 ret
= consumer_send_status_msg(sock
, ret_code
);
730 /* Somehow, the session daemon is not responding anymore. */
736 case LTTNG_CONSUMER_DATA_PENDING
:
739 uint64_t id
= msg
.u
.data_pending
.session_id
;
741 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
743 ret
= consumer_data_pending(id
);
745 /* Send back returned value to session daemon */
746 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
748 PERROR("send data pending ret code");
753 * No need to send back a status message since the data pending
754 * returned value is the response.
758 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
760 if (msg
.u
.snapshot_channel
.metadata
== 1) {
761 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
762 msg
.u
.snapshot_channel
.pathname
,
763 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
765 ERR("Snapshot metadata failed");
766 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
769 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
770 msg
.u
.snapshot_channel
.pathname
,
771 msg
.u
.snapshot_channel
.relayd_id
,
772 msg
.u
.snapshot_channel
.max_stream_size
,
775 ERR("Snapshot channel failed");
776 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
780 ret
= consumer_send_status_msg(sock
, ret_code
);
782 /* Somehow, the session daemon is not responding anymore. */
787 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
789 uint64_t key
= msg
.u
.destroy_channel
.key
;
790 struct lttng_consumer_channel
*channel
;
792 channel
= consumer_find_channel(key
);
794 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
795 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
798 ret
= consumer_send_status_msg(sock
, ret_code
);
800 /* Somehow, the session daemon is not responding anymore. */
805 * This command should ONLY be issued for channel with streams set in
808 assert(!channel
->monitor
);
811 * The refcount should ALWAYS be 0 in the case of a channel in no
814 assert(!uatomic_sub_return(&channel
->refcount
, 1));
816 consumer_del_channel(channel
);
828 * Return 1 to indicate success since the 0 value can be a socket
829 * shutdown during the recv() or send() call.
835 /* This will issue a consumer stop. */
840 * Consume data on a file descriptor and write it on a trace file.
842 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
843 struct lttng_consumer_local_data
*ctx
)
845 unsigned long len
, subbuf_size
, padding
;
848 int infd
= stream
->wait_fd
;
850 DBG("In read_subbuffer (infd : %d)", infd
);
851 /* Get the next subbuffer */
852 err
= kernctl_get_next_subbuf(infd
);
856 * This is a debug message even for single-threaded consumer,
857 * because poll() have more relaxed criterions than get subbuf,
858 * so get_subbuf may fail for short race windows where poll()
859 * would issue wakeups.
861 DBG("Reserving sub buffer failed (everything is normal, "
862 "it is due to concurrency)");
866 /* Get the full subbuffer size including padding */
867 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
870 perror("Getting sub-buffer len failed.");
875 switch (stream
->chan
->output
) {
876 case CONSUMER_CHANNEL_SPLICE
:
878 * XXX: The lttng-modules splice "actor" does not handle copying
879 * partial pages hence only using the subbuffer size without the
880 * padding makes the splice fail.
885 /* splice the subbuffer to the tracefile */
886 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
889 * XXX: Splice does not support network streaming so the return value
890 * is simply checked against subbuf_size and not like the mmap() op.
892 if (ret
!= subbuf_size
) {
894 * display the error but continue processing to try
895 * to release the subbuffer
897 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
901 case CONSUMER_CHANNEL_MMAP
:
902 /* Get subbuffer size without padding */
903 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
906 perror("Getting sub-buffer len failed.");
911 /* Make sure the tracer is not gone mad on us! */
912 assert(len
>= subbuf_size
);
914 padding
= len
- subbuf_size
;
916 /* write the subbuffer to the tracefile */
917 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
920 * The mmap operation should write subbuf_size amount of data when
921 * network streaming or the full padding (len) size when we are _not_
924 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
925 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
927 * Display the error but continue processing to try to release the
930 ERR("Error writing to tracefile "
931 "(ret: %zd != len: %lu != subbuf_size: %lu)",
932 ret
, len
, subbuf_size
);
936 ERR("Unknown output method");
940 err
= kernctl_put_next_subbuf(infd
);
943 if (errno
== EFAULT
) {
944 perror("Error in unreserving sub buffer\n");
945 } else if (errno
== EIO
) {
946 /* Should never happen with newer LTTng versions */
947 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
958 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
965 * Don't create anything if this is set for streaming or should not be
968 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
969 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
970 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
971 stream
->uid
, stream
->gid
);
975 stream
->out_fd
= ret
;
976 stream
->tracefile_size_current
= 0;
979 if (stream
->output
== LTTNG_EVENT_MMAP
) {
980 /* get the len of the mmap region */
981 unsigned long mmap_len
;
983 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
986 PERROR("kernctl_get_mmap_len");
989 stream
->mmap_len
= (size_t) mmap_len
;
991 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
992 MAP_PRIVATE
, stream
->wait_fd
, 0);
993 if (stream
->mmap_base
== MAP_FAILED
) {
994 PERROR("Error mmaping");
1000 /* we return 0 to let the library handle the FD internally */
1004 if (stream
->out_fd
>= 0) {
1007 err
= close(stream
->out_fd
);
1009 stream
->out_fd
= -1;
1016 * Check if data is still being extracted from the buffers for a specific
1017 * stream. Consumer data lock MUST be acquired before calling this function
1018 * and the stream lock.
1020 * Return 1 if the traced data are still getting read else 0 meaning that the
1021 * data is available for trace viewer reading.
1023 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1029 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1034 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1036 /* There is still data so let's put back this subbuffer. */
1037 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1039 ret
= 1; /* Data is pending */
1043 /* Data is NOT pending and ready to be read. */