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>
43 #include "kernel-consumer.h"
45 extern struct lttng_consumer_global_data consumer_data
;
46 extern int consumer_poll_timeout
;
47 extern volatile int consumer_quit
;
50 * Take a snapshot for a specific fd
52 * Returns 0 on success, < 0 on error
54 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
57 int infd
= stream
->wait_fd
;
59 ret
= kernctl_snapshot(infd
);
61 perror("Getting sub-buffer snapshot.");
69 * Get the produced position
71 * Returns 0 on success, < 0 on error
73 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
77 int infd
= stream
->wait_fd
;
79 ret
= kernctl_snapshot_get_produced(infd
, pos
);
81 perror("kernctl_snapshot_get_produced");
89 * Get the consumerd position
91 * Returns 0 on success, < 0 on error
93 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
97 int infd
= stream
->wait_fd
;
99 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
101 perror("kernctl_snapshot_get_consumed");
109 * Take a snapshot of all the stream of a channel
111 * Returns 0 on success, < 0 on error
113 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
114 uint64_t relayd_id
, uint64_t max_stream_size
,
115 struct lttng_consumer_local_data
*ctx
)
118 unsigned long consumed_pos
, produced_pos
;
119 struct lttng_consumer_channel
*channel
;
120 struct lttng_consumer_stream
*stream
;
122 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
126 channel
= consumer_find_channel(key
);
128 ERR("No channel found for key %" PRIu64
, key
);
133 /* Splice is not supported yet for channel snapshot. */
134 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
135 ERR("Unsupported output %d", channel
->output
);
140 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
142 * Lock stream because we are about to change its state.
144 pthread_mutex_lock(&stream
->lock
);
147 * Assign the received relayd ID so we can use it for streaming. The streams
148 * are not visible to anyone so this is OK to change it.
150 stream
->net_seq_idx
= relayd_id
;
151 channel
->relayd_id
= relayd_id
;
152 if (relayd_id
!= (uint64_t) -1ULL) {
153 ret
= consumer_send_relayd_stream(stream
, path
);
155 ERR("sending stream to relayd");
159 ret
= utils_create_stream_file(path
, stream
->name
,
160 stream
->chan
->tracefile_size
,
161 stream
->tracefile_count_current
,
162 stream
->uid
, stream
->gid
, NULL
);
164 ERR("utils_create_stream_file");
168 stream
->out_fd
= ret
;
169 stream
->tracefile_size_current
= 0;
171 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
172 path
, stream
->name
, stream
->key
);
175 ret
= kernctl_buffer_flush(stream
->wait_fd
);
177 ERR("Failed to flush kernel stream");
182 ret
= lttng_kconsumer_take_snapshot(stream
);
184 ERR("Taking kernel snapshot");
188 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
190 ERR("Produced kernel snapshot position");
194 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
196 ERR("Consumerd kernel snapshot position");
200 if (stream
->max_sb_size
== 0) {
201 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
202 &stream
->max_sb_size
);
204 ERR("Getting kernel max_sb_size");
211 * The original value is sent back if max stream size is larger than
212 * the possible size of the snapshot. Also, we asume that the session
213 * daemon should never send a maximum stream size that is lower than
216 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
217 produced_pos
, max_stream_size
);
219 while (consumed_pos
< produced_pos
) {
221 unsigned long len
, padded_len
;
223 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
225 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
227 if (errno
!= EAGAIN
) {
228 PERROR("kernctl_get_subbuf snapshot");
232 DBG("Kernel consumer get subbuf failed. Skipping it.");
233 consumed_pos
+= stream
->max_sb_size
;
237 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
239 ERR("Snapshot kernctl_get_subbuf_size");
241 goto error_put_subbuf
;
244 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
246 ERR("Snapshot kernctl_get_padded_subbuf_size");
248 goto error_put_subbuf
;
251 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
252 padded_len
- len
, NULL
);
254 * We write the padded len in local tracefiles but the data len
255 * when using a relay. Display the error but continue processing
256 * to try to release the subbuffer.
258 if (relayd_id
!= (uint64_t) -1ULL) {
259 if (read_len
!= len
) {
260 ERR("Error sending to the relay (ret: %zd != len: %lu)",
264 if (read_len
!= padded_len
) {
265 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
266 read_len
, padded_len
);
270 ret
= kernctl_put_subbuf(stream
->wait_fd
);
272 ERR("Snapshot kernctl_put_subbuf");
276 consumed_pos
+= stream
->max_sb_size
;
279 if (relayd_id
== (uint64_t) -1ULL) {
280 if (stream
->out_fd
>= 0) {
281 ret
= close(stream
->out_fd
);
283 PERROR("Kernel consumer snapshot close out_fd");
289 close_relayd_stream(stream
);
290 stream
->net_seq_idx
= (uint64_t) -1ULL;
292 pthread_mutex_unlock(&stream
->lock
);
300 ret
= kernctl_put_subbuf(stream
->wait_fd
);
303 ERR("Snapshot kernctl_put_subbuf error path");
306 pthread_mutex_unlock(&stream
->lock
);
313 * Read the whole metadata available for a snapshot.
315 * Returns 0 on success, < 0 on error
317 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
318 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
320 int ret
, use_relayd
= 0;
322 struct lttng_consumer_channel
*metadata_channel
;
323 struct lttng_consumer_stream
*metadata_stream
;
327 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
332 metadata_channel
= consumer_find_channel(key
);
333 if (!metadata_channel
) {
334 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
339 metadata_stream
= metadata_channel
->metadata_stream
;
340 assert(metadata_stream
);
342 /* Flag once that we have a valid relayd for the stream. */
343 if (relayd_id
!= (uint64_t) -1ULL) {
348 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
353 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
354 metadata_stream
->chan
->tracefile_size
,
355 metadata_stream
->tracefile_count_current
,
356 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
360 metadata_stream
->out_fd
= ret
;
364 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
366 if (ret_read
!= -EAGAIN
) {
367 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
371 /* ret_read is negative at this point so we will exit the loop. */
374 } while (ret_read
>= 0);
377 close_relayd_stream(metadata_stream
);
378 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
380 if (metadata_stream
->out_fd
>= 0) {
381 ret
= close(metadata_stream
->out_fd
);
383 PERROR("Kernel consumer snapshot metadata close out_fd");
385 * Don't go on error here since the snapshot was successful at this
386 * point but somehow the close failed.
389 metadata_stream
->out_fd
= -1;
395 cds_list_del(&metadata_stream
->send_node
);
396 consumer_stream_destroy(metadata_stream
, NULL
);
397 metadata_channel
->metadata_stream
= NULL
;
404 * Receive command from session daemon and process it.
406 * Return 1 on success else a negative value or 0.
408 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
409 int sock
, struct pollfd
*consumer_sockpoll
)
412 enum lttng_error_code ret_code
= LTTNG_OK
;
413 struct lttcomm_consumer_msg msg
;
415 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
416 if (ret
!= sizeof(msg
)) {
418 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
423 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
425 * Notify the session daemon that the command is completed.
427 * On transport layer error, the function call will print an error
428 * message so handling the returned code is a bit useless since we
429 * return an error code anyway.
431 (void) consumer_send_status_msg(sock
, ret_code
);
435 /* relayd needs RCU read-side protection */
438 switch (msg
.cmd_type
) {
439 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
441 /* Session daemon status message are handled in the following call. */
442 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
443 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
444 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
447 case LTTNG_CONSUMER_ADD_CHANNEL
:
449 struct lttng_consumer_channel
*new_channel
;
452 /* First send a status message before receiving the fds. */
453 ret
= consumer_send_status_msg(sock
, ret_code
);
455 /* Somehow, the session daemon is not responding anymore. */
458 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
459 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
460 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
461 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
462 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
463 msg
.u
.channel
.tracefile_size
,
464 msg
.u
.channel
.tracefile_count
, 0,
465 msg
.u
.channel
.monitor
,
466 msg
.u
.channel
.live_timer_interval
);
467 if (new_channel
== NULL
) {
468 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
471 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
472 switch (msg
.u
.channel
.output
) {
473 case LTTNG_EVENT_SPLICE
:
474 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
476 case LTTNG_EVENT_MMAP
:
477 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
480 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
484 /* Translate and save channel type. */
485 switch (msg
.u
.channel
.type
) {
486 case CONSUMER_CHANNEL_TYPE_DATA
:
487 case CONSUMER_CHANNEL_TYPE_METADATA
:
488 new_channel
->type
= msg
.u
.channel
.type
;
495 if (ctx
->on_recv_channel
!= NULL
) {
496 ret_recv
= ctx
->on_recv_channel(new_channel
);
498 ret
= consumer_add_channel(new_channel
, ctx
);
499 } else if (ret_recv
< 0) {
503 ret
= consumer_add_channel(new_channel
, ctx
);
506 /* If we received an error in add_channel, we need to report it. */
508 ret
= consumer_send_status_msg(sock
, ret
);
517 case LTTNG_CONSUMER_ADD_STREAM
:
520 struct lttng_pipe
*stream_pipe
;
521 struct lttng_consumer_stream
*new_stream
;
522 struct lttng_consumer_channel
*channel
;
526 * Get stream's channel reference. Needed when adding the stream to the
529 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
532 * We could not find the channel. Can happen if cpu hotplug
533 * happens while tearing down.
535 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
536 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
539 /* First send a status message before receiving the fds. */
540 ret
= consumer_send_status_msg(sock
, ret_code
);
542 /* Somehow, the session daemon is not responding anymore. */
545 if (ret_code
!= LTTNG_OK
) {
546 /* Channel was not found. */
551 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
556 /* Get stream file descriptor from socket */
557 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
558 if (ret
!= sizeof(fd
)) {
559 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
565 * Send status code to session daemon only if the recv works. If the
566 * above recv() failed, the session daemon is notified through the
567 * error socket and the teardown is eventually done.
569 ret
= consumer_send_status_msg(sock
, ret_code
);
571 /* Somehow, the session daemon is not responding anymore. */
575 new_stream
= consumer_allocate_stream(channel
->key
,
577 LTTNG_CONSUMER_ACTIVE_STREAM
,
587 if (new_stream
== NULL
) {
592 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
598 new_stream
->chan
= channel
;
599 new_stream
->wait_fd
= fd
;
600 switch (channel
->output
) {
601 case CONSUMER_CHANNEL_SPLICE
:
602 new_stream
->output
= LTTNG_EVENT_SPLICE
;
604 case CONSUMER_CHANNEL_MMAP
:
605 new_stream
->output
= LTTNG_EVENT_MMAP
;
608 ERR("Stream output unknown %d", channel
->output
);
613 * We've just assigned the channel to the stream so increment the
614 * refcount right now. We don't need to increment the refcount for
615 * streams in no monitor because we handle manually the cleanup of
616 * those. It is very important to make sure there is NO prior
617 * consumer_del_stream() calls or else the refcount will be unbalanced.
619 if (channel
->monitor
) {
620 uatomic_inc(&new_stream
->chan
->refcount
);
624 * The buffer flush is done on the session daemon side for the kernel
625 * so no need for the stream "hangup_flush_done" variable to be
626 * tracked. This is important for a kernel stream since we don't rely
627 * on the flush state of the stream to read data. It's not the case for
628 * user space tracing.
630 new_stream
->hangup_flush_done
= 0;
632 if (ctx
->on_recv_stream
) {
633 ret
= ctx
->on_recv_stream(new_stream
);
635 consumer_stream_free(new_stream
);
640 if (new_stream
->metadata_flag
) {
641 channel
->metadata_stream
= new_stream
;
644 /* Do not monitor this stream. */
645 if (!channel
->monitor
) {
646 DBG("Kernel consumer add stream %s in no monitor mode with "
647 "relayd id %" PRIu64
, new_stream
->name
,
648 new_stream
->net_seq_idx
);
649 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
653 /* Send stream to relayd if the stream has an ID. */
654 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
655 ret
= consumer_send_relayd_stream(new_stream
,
656 new_stream
->chan
->pathname
);
658 consumer_stream_free(new_stream
);
663 /* Get the right pipe where the stream will be sent. */
664 if (new_stream
->metadata_flag
) {
665 ret
= consumer_add_metadata_stream(new_stream
);
667 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
669 consumer_stream_free(new_stream
);
672 stream_pipe
= ctx
->consumer_metadata_pipe
;
674 ret
= consumer_add_data_stream(new_stream
);
676 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
678 consumer_stream_free(new_stream
);
681 stream_pipe
= ctx
->consumer_data_pipe
;
684 /* Vitible to other threads */
685 new_stream
->globally_visible
= 1;
687 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
689 ERR("Consumer write %s stream to pipe %d",
690 new_stream
->metadata_flag
? "metadata" : "data",
691 lttng_pipe_get_writefd(stream_pipe
));
692 if (new_stream
->metadata_flag
) {
693 consumer_del_stream_for_metadata(new_stream
);
695 consumer_del_stream_for_data(new_stream
);
700 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
701 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
704 case LTTNG_CONSUMER_UPDATE_STREAM
:
709 case LTTNG_CONSUMER_DESTROY_RELAYD
:
711 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
712 struct consumer_relayd_sock_pair
*relayd
;
714 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
716 /* Get relayd reference if exists. */
717 relayd
= consumer_find_relayd(index
);
718 if (relayd
== NULL
) {
719 DBG("Unable to find relayd %" PRIu64
, index
);
720 ret_code
= LTTNG_ERR_NO_CONSUMER
;
724 * Each relayd socket pair has a refcount of stream attached to it
725 * which tells if the relayd is still active or not depending on the
728 * This will set the destroy flag of the relayd object and destroy it
729 * if the refcount reaches zero when called.
731 * The destroy can happen either here or when a stream fd hangs up.
734 consumer_flag_relayd_for_destroy(relayd
);
737 ret
= consumer_send_status_msg(sock
, ret_code
);
739 /* Somehow, the session daemon is not responding anymore. */
745 case LTTNG_CONSUMER_DATA_PENDING
:
748 uint64_t id
= msg
.u
.data_pending
.session_id
;
750 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
752 ret
= consumer_data_pending(id
);
754 /* Send back returned value to session daemon */
755 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
757 PERROR("send data pending ret code");
762 * No need to send back a status message since the data pending
763 * returned value is the response.
767 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
769 if (msg
.u
.snapshot_channel
.metadata
== 1) {
770 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
771 msg
.u
.snapshot_channel
.pathname
,
772 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
774 ERR("Snapshot metadata failed");
775 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
778 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
779 msg
.u
.snapshot_channel
.pathname
,
780 msg
.u
.snapshot_channel
.relayd_id
,
781 msg
.u
.snapshot_channel
.max_stream_size
,
784 ERR("Snapshot channel failed");
785 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
789 ret
= consumer_send_status_msg(sock
, ret_code
);
791 /* Somehow, the session daemon is not responding anymore. */
796 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
798 uint64_t key
= msg
.u
.destroy_channel
.key
;
799 struct lttng_consumer_channel
*channel
;
801 channel
= consumer_find_channel(key
);
803 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
804 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
807 ret
= consumer_send_status_msg(sock
, ret_code
);
809 /* Somehow, the session daemon is not responding anymore. */
814 * This command should ONLY be issued for channel with streams set in
817 assert(!channel
->monitor
);
820 * The refcount should ALWAYS be 0 in the case of a channel in no
823 assert(!uatomic_sub_return(&channel
->refcount
, 1));
825 consumer_del_channel(channel
);
837 * Return 1 to indicate success since the 0 value can be a socket
838 * shutdown during the recv() or send() call.
844 /* This will issue a consumer stop. */
849 * Populate index values of a kernel stream. Values are set in big endian order.
851 * Return 0 on success or else a negative value.
853 static int get_index_values(struct lttng_packet_index
*index
, int infd
)
857 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
859 PERROR("kernctl_get_timestamp_begin");
862 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
864 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
866 PERROR("kernctl_get_timestamp_end");
869 index
->timestamp_end
= htobe64(index
->timestamp_end
);
871 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
873 PERROR("kernctl_get_events_discarded");
876 index
->events_discarded
= htobe64(index
->events_discarded
);
878 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
880 PERROR("kernctl_get_content_size");
883 index
->content_size
= htobe64(index
->content_size
);
885 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
887 PERROR("kernctl_get_packet_size");
890 index
->packet_size
= htobe64(index
->packet_size
);
892 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
894 PERROR("kernctl_get_stream_id");
897 index
->stream_id
= htobe64(index
->stream_id
);
904 * Consume data on a file descriptor and write it on a trace file.
906 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
907 struct lttng_consumer_local_data
*ctx
)
909 unsigned long len
, subbuf_size
, padding
;
910 int err
, write_index
= 0;
912 int infd
= stream
->wait_fd
;
913 struct lttng_packet_index index
;
915 DBG("In read_subbuffer (infd : %d)", infd
);
917 /* Indicate that for this stream we have to write the index. */
918 if (stream
->index_fd
>= 0) {
922 /* Get the next subbuffer */
923 err
= kernctl_get_next_subbuf(infd
);
926 * This is a debug message even for single-threaded consumer,
927 * because poll() have more relaxed criterions than get subbuf,
928 * so get_subbuf may fail for short race windows where poll()
929 * would issue wakeups.
931 DBG("Reserving sub buffer failed (everything is normal, "
932 "it is due to concurrency)");
937 /* Get the full subbuffer size including padding */
938 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
940 perror("Getting sub-buffer len failed.");
945 if (!stream
->metadata_flag
&& write_index
) {
946 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. */
1032 err
= index_write(stream
->index_fd
, &index
, sizeof(index
));
1043 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1050 * Don't create anything if this is set for streaming or should not be
1053 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1054 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1055 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1056 stream
->uid
, stream
->gid
, NULL
);
1060 stream
->out_fd
= ret
;
1061 stream
->tracefile_size_current
= 0;
1063 if (!stream
->metadata_flag
) {
1064 ret
= index_create_file(stream
->chan
->pathname
,
1065 stream
->name
, stream
->uid
, stream
->gid
,
1066 stream
->chan
->tracefile_size
,
1067 stream
->tracefile_count_current
);
1071 stream
->index_fd
= ret
;
1075 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1076 /* get the len of the mmap region */
1077 unsigned long mmap_len
;
1079 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1081 PERROR("kernctl_get_mmap_len");
1083 goto error_close_fd
;
1085 stream
->mmap_len
= (size_t) mmap_len
;
1087 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1088 MAP_PRIVATE
, stream
->wait_fd
, 0);
1089 if (stream
->mmap_base
== MAP_FAILED
) {
1090 PERROR("Error mmaping");
1092 goto error_close_fd
;
1096 /* we return 0 to let the library handle the FD internally */
1100 if (stream
->out_fd
>= 0) {
1103 err
= close(stream
->out_fd
);
1105 stream
->out_fd
= -1;
1112 * Check if data is still being extracted from the buffers for a specific
1113 * stream. Consumer data lock MUST be acquired before calling this function
1114 * and the stream lock.
1116 * Return 1 if the traced data are still getting read else 0 meaning that the
1117 * data is available for trace viewer reading.
1119 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1125 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1130 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1132 /* There is still data so let's put back this subbuffer. */
1133 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1135 ret
= 1; /* Data is pending */
1139 /* Data is NOT pending and ready to be read. */