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
, struct lttng_consumer_local_data
*ctx
)
116 unsigned long consumed_pos
, produced_pos
;
117 struct lttng_consumer_channel
*channel
;
118 struct lttng_consumer_stream
*stream
;
120 DBG("Kernel consumer snapshot channel %lu", key
);
124 channel
= consumer_find_channel(key
);
126 ERR("No channel found for key %lu", key
);
131 /* Splice is not supported yet for channel snapshot. */
132 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
133 ERR("Unsupported output %d", channel
->output
);
138 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
140 * Lock stream because we are about to change its state.
142 pthread_mutex_lock(&stream
->lock
);
145 * Assign the received relayd ID so we can use it for streaming. The streams
146 * are not visible to anyone so this is OK to change it.
148 stream
->net_seq_idx
= relayd_id
;
149 channel
->relayd_id
= relayd_id
;
150 if (relayd_id
!= (uint64_t) -1ULL) {
151 ret
= consumer_send_relayd_stream(stream
, path
);
153 ERR("sending stream to relayd");
157 ret
= utils_create_stream_file(path
, stream
->name
,
158 stream
->chan
->tracefile_size
,
159 stream
->tracefile_count_current
,
160 stream
->uid
, stream
->gid
);
162 ERR("utils_create_stream_file");
166 stream
->out_fd
= ret
;
167 stream
->tracefile_size_current
= 0;
169 DBG("Kernel consumer snapshot stream %s/%s (%lu)", path
,
170 stream
->name
, stream
->key
);
173 ret
= kernctl_buffer_flush(stream
->wait_fd
);
175 ERR("Failed to flush kernel stream");
179 ret
= lttng_kconsumer_take_snapshot(stream
);
181 ERR("Taking kernel snapshot");
185 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
187 ERR("Produced kernel snapshot position");
191 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
193 ERR("Consumerd kernel snapshot position");
197 if (stream
->max_sb_size
== 0) {
198 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
199 &stream
->max_sb_size
);
201 ERR("Getting kernel max_sb_size");
206 while (consumed_pos
< produced_pos
) {
208 unsigned long len
, padded_len
;
210 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
212 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
214 if (errno
!= EAGAIN
) {
215 PERROR("kernctl_get_subbuf snapshot");
218 DBG("Kernel consumer get subbuf failed. Skipping it.");
219 consumed_pos
+= stream
->max_sb_size
;
223 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
225 ERR("Snapshot kernctl_get_subbuf_size");
226 goto error_put_subbuf
;
229 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
231 ERR("Snapshot kernctl_get_padded_subbuf_size");
232 goto error_put_subbuf
;
235 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
238 * We write the padded len in local tracefiles but the data len
239 * when using a relay. Display the error but continue processing
240 * to try to release the subbuffer.
242 if (relayd_id
!= (uint64_t) -1ULL) {
243 if (read_len
!= len
) {
244 ERR("Error sending to the relay (ret: %zd != len: %lu)",
248 if (read_len
!= padded_len
) {
249 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
250 read_len
, padded_len
);
254 ret
= kernctl_put_subbuf(stream
->wait_fd
);
256 ERR("Snapshot kernctl_put_subbuf");
259 consumed_pos
+= stream
->max_sb_size
;
262 if (relayd_id
== (uint64_t) -1ULL) {
263 if (stream
->out_fd
>= 0) {
264 ret
= close(stream
->out_fd
);
266 PERROR("Kernel consumer snapshot close out_fd");
272 close_relayd_stream(stream
);
273 stream
->net_seq_idx
= (uint64_t) -1ULL;
275 pthread_mutex_unlock(&stream
->lock
);
283 ret
= kernctl_put_subbuf(stream
->wait_fd
);
285 ERR("Snapshot kernctl_put_subbuf error path");
288 pthread_mutex_unlock(&stream
->lock
);
295 * Read the whole metadata available for a snapshot.
297 * Returns 0 on success, < 0 on error
299 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
300 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
302 int ret
, use_relayd
= 0;
304 struct lttng_consumer_channel
*metadata_channel
;
305 struct lttng_consumer_stream
*metadata_stream
;
309 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
314 metadata_channel
= consumer_find_channel(key
);
315 if (!metadata_channel
) {
316 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
321 metadata_stream
= metadata_channel
->metadata_stream
;
322 assert(metadata_stream
);
324 /* Flag once that we have a valid relayd for the stream. */
325 if (relayd_id
!= (uint64_t) -1ULL) {
330 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
335 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
336 metadata_stream
->chan
->tracefile_size
,
337 metadata_stream
->tracefile_count_current
,
338 metadata_stream
->uid
, metadata_stream
->gid
);
342 metadata_stream
->out_fd
= ret
;
346 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
348 if (ret_read
!= -EPERM
) {
349 ERR("Kernel snapshot reading metadata subbuffer (ret: %ld)",
353 /* ret_read is negative at this point so we will exit the loop. */
356 } while (ret_read
>= 0);
359 close_relayd_stream(metadata_stream
);
360 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
362 if (metadata_stream
->out_fd
>= 0) {
363 ret
= close(metadata_stream
->out_fd
);
365 PERROR("Kernel consumer snapshot metadata close out_fd");
367 * Don't go on error here since the snapshot was successful at this
368 * point but somehow the close failed.
371 metadata_stream
->out_fd
= -1;
377 cds_list_del(&metadata_stream
->send_node
);
378 consumer_stream_destroy(metadata_stream
, NULL
);
379 metadata_channel
->metadata_stream
= NULL
;
386 * Receive command from session daemon and process it.
388 * Return 1 on success else a negative value or 0.
390 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
391 int sock
, struct pollfd
*consumer_sockpoll
)
394 enum lttng_error_code ret_code
= LTTNG_OK
;
395 struct lttcomm_consumer_msg msg
;
397 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
398 if (ret
!= sizeof(msg
)) {
400 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
405 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
407 * Notify the session daemon that the command is completed.
409 * On transport layer error, the function call will print an error
410 * message so handling the returned code is a bit useless since we
411 * return an error code anyway.
413 (void) consumer_send_status_msg(sock
, ret_code
);
417 /* relayd needs RCU read-side protection */
420 switch (msg
.cmd_type
) {
421 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
423 /* Session daemon status message are handled in the following call. */
424 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
425 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
426 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
429 case LTTNG_CONSUMER_ADD_CHANNEL
:
431 struct lttng_consumer_channel
*new_channel
;
434 /* First send a status message before receiving the fds. */
435 ret
= consumer_send_status_msg(sock
, ret_code
);
437 /* Somehow, the session daemon is not responding anymore. */
440 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
441 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
442 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
443 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
444 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
445 msg
.u
.channel
.tracefile_size
,
446 msg
.u
.channel
.tracefile_count
, 0,
447 msg
.u
.channel
.monitor
);
448 if (new_channel
== NULL
) {
449 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
452 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
453 switch (msg
.u
.channel
.output
) {
454 case LTTNG_EVENT_SPLICE
:
455 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
457 case LTTNG_EVENT_MMAP
:
458 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
461 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
465 /* Translate and save channel type. */
466 switch (msg
.u
.channel
.type
) {
467 case CONSUMER_CHANNEL_TYPE_DATA
:
468 case CONSUMER_CHANNEL_TYPE_METADATA
:
469 new_channel
->type
= msg
.u
.channel
.type
;
476 if (ctx
->on_recv_channel
!= NULL
) {
477 ret_recv
= ctx
->on_recv_channel(new_channel
);
479 ret
= consumer_add_channel(new_channel
, ctx
);
480 } else if (ret_recv
< 0) {
484 ret
= consumer_add_channel(new_channel
, ctx
);
487 /* If we received an error in add_channel, we need to report it. */
489 ret
= consumer_send_status_msg(sock
, ret
);
498 case LTTNG_CONSUMER_ADD_STREAM
:
501 struct lttng_pipe
*stream_pipe
;
502 struct lttng_consumer_stream
*new_stream
;
503 struct lttng_consumer_channel
*channel
;
507 * Get stream's channel reference. Needed when adding the stream to the
510 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
513 * We could not find the channel. Can happen if cpu hotplug
514 * happens while tearing down.
516 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
517 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
520 /* First send a status message before receiving the fds. */
521 ret
= consumer_send_status_msg(sock
, ret_code
);
523 /* Somehow, the session daemon is not responding anymore. */
526 if (ret_code
!= LTTNG_OK
) {
527 /* Channel was not found. */
532 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
537 /* Get stream file descriptor from socket */
538 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
539 if (ret
!= sizeof(fd
)) {
540 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
546 * Send status code to session daemon only if the recv works. If the
547 * above recv() failed, the session daemon is notified through the
548 * error socket and the teardown is eventually done.
550 ret
= consumer_send_status_msg(sock
, ret_code
);
552 /* Somehow, the session daemon is not responding anymore. */
556 new_stream
= consumer_allocate_stream(channel
->key
,
558 LTTNG_CONSUMER_ACTIVE_STREAM
,
568 if (new_stream
== NULL
) {
573 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
579 new_stream
->chan
= channel
;
580 new_stream
->wait_fd
= fd
;
581 switch (channel
->output
) {
582 case CONSUMER_CHANNEL_SPLICE
:
583 new_stream
->output
= LTTNG_EVENT_SPLICE
;
585 case CONSUMER_CHANNEL_MMAP
:
586 new_stream
->output
= LTTNG_EVENT_MMAP
;
589 ERR("Stream output unknown %d", channel
->output
);
594 * We've just assigned the channel to the stream so increment the
595 * refcount right now. We don't need to increment the refcount for
596 * streams in no monitor because we handle manually the cleanup of
597 * those. It is very important to make sure there is NO prior
598 * consumer_del_stream() calls or else the refcount will be unbalanced.
600 if (channel
->monitor
) {
601 uatomic_inc(&new_stream
->chan
->refcount
);
605 * The buffer flush is done on the session daemon side for the kernel
606 * so no need for the stream "hangup_flush_done" variable to be
607 * tracked. This is important for a kernel stream since we don't rely
608 * on the flush state of the stream to read data. It's not the case for
609 * user space tracing.
611 new_stream
->hangup_flush_done
= 0;
613 if (ctx
->on_recv_stream
) {
614 ret
= ctx
->on_recv_stream(new_stream
);
616 consumer_stream_free(new_stream
);
621 if (new_stream
->metadata_flag
) {
622 channel
->metadata_stream
= new_stream
;
625 /* Do not monitor this stream. */
626 if (!channel
->monitor
) {
627 DBG("Kernel consumer add stream %s in no monitor mode with "
628 "relayd id %" PRIu64
, new_stream
->name
,
629 new_stream
->net_seq_idx
);
630 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
634 /* Send stream to relayd if the stream has an ID. */
635 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
636 ret
= consumer_send_relayd_stream(new_stream
,
637 new_stream
->chan
->pathname
);
639 consumer_stream_free(new_stream
);
644 /* Get the right pipe where the stream will be sent. */
645 if (new_stream
->metadata_flag
) {
646 stream_pipe
= ctx
->consumer_metadata_pipe
;
648 stream_pipe
= ctx
->consumer_data_pipe
;
651 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
653 ERR("Consumer write %s stream to pipe %d",
654 new_stream
->metadata_flag
? "metadata" : "data",
655 lttng_pipe_get_writefd(stream_pipe
));
656 consumer_stream_free(new_stream
);
660 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
661 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
664 case LTTNG_CONSUMER_UPDATE_STREAM
:
669 case LTTNG_CONSUMER_DESTROY_RELAYD
:
671 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
672 struct consumer_relayd_sock_pair
*relayd
;
674 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
676 /* Get relayd reference if exists. */
677 relayd
= consumer_find_relayd(index
);
678 if (relayd
== NULL
) {
679 DBG("Unable to find relayd %" PRIu64
, index
);
680 ret_code
= LTTNG_ERR_NO_CONSUMER
;
684 * Each relayd socket pair has a refcount of stream attached to it
685 * which tells if the relayd is still active or not depending on the
688 * This will set the destroy flag of the relayd object and destroy it
689 * if the refcount reaches zero when called.
691 * The destroy can happen either here or when a stream fd hangs up.
694 consumer_flag_relayd_for_destroy(relayd
);
697 ret
= consumer_send_status_msg(sock
, ret_code
);
699 /* Somehow, the session daemon is not responding anymore. */
705 case LTTNG_CONSUMER_DATA_PENDING
:
708 uint64_t id
= msg
.u
.data_pending
.session_id
;
710 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
712 ret
= consumer_data_pending(id
);
714 /* Send back returned value to session daemon */
715 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
717 PERROR("send data pending ret code");
722 * No need to send back a status message since the data pending
723 * returned value is the response.
727 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
729 if (msg
.u
.snapshot_channel
.metadata
== 1) {
730 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
731 msg
.u
.snapshot_channel
.pathname
,
732 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
734 ERR("Snapshot metadata failed");
735 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
738 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
739 msg
.u
.snapshot_channel
.pathname
,
740 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
742 ERR("Snapshot channel failed");
743 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
747 ret
= consumer_send_status_msg(sock
, ret_code
);
749 /* Somehow, the session daemon is not responding anymore. */
754 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
756 uint64_t key
= msg
.u
.destroy_channel
.key
;
757 struct lttng_consumer_channel
*channel
;
759 channel
= consumer_find_channel(key
);
761 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
762 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
765 ret
= consumer_send_status_msg(sock
, ret_code
);
767 /* Somehow, the session daemon is not responding anymore. */
772 * This command should ONLY be issued for channel with streams set in
775 assert(!channel
->monitor
);
778 * The refcount should ALWAYS be 0 in the case of a channel in no
781 assert(!uatomic_sub_return(&channel
->refcount
, 1));
783 consumer_del_channel(channel
);
795 * Return 1 to indicate success since the 0 value can be a socket
796 * shutdown during the recv() or send() call.
802 /* This will issue a consumer stop. */
807 * Consume data on a file descriptor and write it on a trace file.
809 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
810 struct lttng_consumer_local_data
*ctx
)
812 unsigned long len
, subbuf_size
, padding
;
815 int infd
= stream
->wait_fd
;
817 DBG("In read_subbuffer (infd : %d)", infd
);
818 /* Get the next subbuffer */
819 err
= kernctl_get_next_subbuf(infd
);
823 * This is a debug message even for single-threaded consumer,
824 * because poll() have more relaxed criterions than get subbuf,
825 * so get_subbuf may fail for short race windows where poll()
826 * would issue wakeups.
828 DBG("Reserving sub buffer failed (everything is normal, "
829 "it is due to concurrency)");
833 /* Get the full subbuffer size including padding */
834 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
837 perror("Getting sub-buffer len failed.");
842 switch (stream
->chan
->output
) {
843 case CONSUMER_CHANNEL_SPLICE
:
845 * XXX: The lttng-modules splice "actor" does not handle copying
846 * partial pages hence only using the subbuffer size without the
847 * padding makes the splice fail.
852 /* splice the subbuffer to the tracefile */
853 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
856 * XXX: Splice does not support network streaming so the return value
857 * is simply checked against subbuf_size and not like the mmap() op.
859 if (ret
!= subbuf_size
) {
861 * display the error but continue processing to try
862 * to release the subbuffer
864 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
868 case CONSUMER_CHANNEL_MMAP
:
869 /* Get subbuffer size without padding */
870 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
873 perror("Getting sub-buffer len failed.");
878 /* Make sure the tracer is not gone mad on us! */
879 assert(len
>= subbuf_size
);
881 padding
= len
- subbuf_size
;
883 /* write the subbuffer to the tracefile */
884 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
887 * The mmap operation should write subbuf_size amount of data when
888 * network streaming or the full padding (len) size when we are _not_
891 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
892 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
894 * Display the error but continue processing to try to release the
897 ERR("Error writing to tracefile "
898 "(ret: %zd != len: %lu != subbuf_size: %lu)",
899 ret
, len
, subbuf_size
);
903 ERR("Unknown output method");
907 err
= kernctl_put_next_subbuf(infd
);
910 if (errno
== EFAULT
) {
911 perror("Error in unreserving sub buffer\n");
912 } else if (errno
== EIO
) {
913 /* Should never happen with newer LTTng versions */
914 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
925 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
932 * Don't create anything if this is set for streaming or should not be
935 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
936 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
937 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
938 stream
->uid
, stream
->gid
);
942 stream
->out_fd
= ret
;
943 stream
->tracefile_size_current
= 0;
946 if (stream
->output
== LTTNG_EVENT_MMAP
) {
947 /* get the len of the mmap region */
948 unsigned long mmap_len
;
950 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
953 PERROR("kernctl_get_mmap_len");
956 stream
->mmap_len
= (size_t) mmap_len
;
958 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
959 MAP_PRIVATE
, stream
->wait_fd
, 0);
960 if (stream
->mmap_base
== MAP_FAILED
) {
961 PERROR("Error mmaping");
967 /* we return 0 to let the library handle the FD internally */
971 if (stream
->out_fd
>= 0) {
974 err
= close(stream
->out_fd
);
983 * Check if data is still being extracted from the buffers for a specific
984 * stream. Consumer data lock MUST be acquired before calling this function
985 * and the stream lock.
987 * Return 1 if the traced data are still getting read else 0 meaning that the
988 * data is available for trace viewer reading.
990 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
996 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
998 /* There is still data so let's put back this subbuffer. */
999 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1001 ret
= 1; /* Data is pending */
1005 /* Data is NOT pending and ready to be read. */