2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/sessiond-comm/relayd.h>
36 #include <common/compat/fcntl.h>
37 #include <common/pipe.h>
38 #include <common/relayd/relayd.h>
39 #include <common/utils.h>
40 #include <common/consumer-stream.h>
41 #include <common/index/index.h>
42 #include <common/consumer-timer.h>
44 #include "kernel-consumer.h"
46 extern struct lttng_consumer_global_data consumer_data
;
47 extern int consumer_poll_timeout
;
48 extern volatile int consumer_quit
;
51 * Take a snapshot for a specific fd
53 * Returns 0 on success, < 0 on error
55 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
58 int infd
= stream
->wait_fd
;
60 ret
= kernctl_snapshot(infd
);
62 perror("Getting sub-buffer snapshot.");
70 * Get the produced position
72 * Returns 0 on success, < 0 on error
74 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
78 int infd
= stream
->wait_fd
;
80 ret
= kernctl_snapshot_get_produced(infd
, pos
);
82 perror("kernctl_snapshot_get_produced");
90 * Get the consumerd position
92 * Returns 0 on success, < 0 on error
94 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
98 int infd
= stream
->wait_fd
;
100 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
102 perror("kernctl_snapshot_get_consumed");
110 * Take a snapshot of all the stream of a channel
112 * Returns 0 on success, < 0 on error
114 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
115 uint64_t relayd_id
, uint64_t max_stream_size
,
116 struct lttng_consumer_local_data
*ctx
)
119 unsigned long consumed_pos
, produced_pos
;
120 struct lttng_consumer_channel
*channel
;
121 struct lttng_consumer_stream
*stream
;
123 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
127 channel
= consumer_find_channel(key
);
129 ERR("No channel found for key %" PRIu64
, key
);
134 /* Splice is not supported yet for channel snapshot. */
135 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
136 ERR("Unsupported output %d", channel
->output
);
141 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
143 * Lock stream because we are about to change its state.
145 pthread_mutex_lock(&stream
->lock
);
148 * Assign the received relayd ID so we can use it for streaming. The streams
149 * are not visible to anyone so this is OK to change it.
151 stream
->net_seq_idx
= relayd_id
;
152 channel
->relayd_id
= relayd_id
;
153 if (relayd_id
!= (uint64_t) -1ULL) {
154 ret
= consumer_send_relayd_stream(stream
, path
);
156 ERR("sending stream to relayd");
160 ret
= utils_create_stream_file(path
, stream
->name
,
161 stream
->chan
->tracefile_size
,
162 stream
->tracefile_count_current
,
163 stream
->uid
, stream
->gid
, NULL
);
165 ERR("utils_create_stream_file");
169 stream
->out_fd
= ret
;
170 stream
->tracefile_size_current
= 0;
172 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
173 path
, stream
->name
, stream
->key
);
176 ret
= kernctl_buffer_flush(stream
->wait_fd
);
178 ERR("Failed to flush kernel stream");
183 ret
= lttng_kconsumer_take_snapshot(stream
);
185 ERR("Taking kernel snapshot");
189 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
191 ERR("Produced kernel snapshot position");
195 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
197 ERR("Consumerd kernel snapshot position");
201 if (stream
->max_sb_size
== 0) {
202 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
203 &stream
->max_sb_size
);
205 ERR("Getting kernel max_sb_size");
212 * The original value is sent back if max stream size is larger than
213 * the possible size of the snapshot. Also, we asume that the session
214 * daemon should never send a maximum stream size that is lower than
217 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
218 produced_pos
, max_stream_size
);
220 while (consumed_pos
< produced_pos
) {
222 unsigned long len
, padded_len
;
224 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
226 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
228 if (errno
!= EAGAIN
) {
229 PERROR("kernctl_get_subbuf snapshot");
233 DBG("Kernel consumer get subbuf failed. Skipping it.");
234 consumed_pos
+= stream
->max_sb_size
;
238 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
240 ERR("Snapshot kernctl_get_subbuf_size");
242 goto error_put_subbuf
;
245 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
247 ERR("Snapshot kernctl_get_padded_subbuf_size");
249 goto error_put_subbuf
;
252 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
253 padded_len
- len
, NULL
);
255 * We write the padded len in local tracefiles but the data len
256 * when using a relay. Display the error but continue processing
257 * to try to release the subbuffer.
259 if (relayd_id
!= (uint64_t) -1ULL) {
260 if (read_len
!= len
) {
261 ERR("Error sending to the relay (ret: %zd != len: %lu)",
265 if (read_len
!= padded_len
) {
266 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
267 read_len
, padded_len
);
271 ret
= kernctl_put_subbuf(stream
->wait_fd
);
273 ERR("Snapshot kernctl_put_subbuf");
277 consumed_pos
+= stream
->max_sb_size
;
280 if (relayd_id
== (uint64_t) -1ULL) {
281 if (stream
->out_fd
>= 0) {
282 ret
= close(stream
->out_fd
);
284 PERROR("Kernel consumer snapshot close out_fd");
290 close_relayd_stream(stream
);
291 stream
->net_seq_idx
= (uint64_t) -1ULL;
293 pthread_mutex_unlock(&stream
->lock
);
301 ret
= kernctl_put_subbuf(stream
->wait_fd
);
304 ERR("Snapshot kernctl_put_subbuf error path");
307 pthread_mutex_unlock(&stream
->lock
);
314 * Read the whole metadata available for a snapshot.
316 * Returns 0 on success, < 0 on error
318 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
319 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
321 int ret
, use_relayd
= 0;
323 struct lttng_consumer_channel
*metadata_channel
;
324 struct lttng_consumer_stream
*metadata_stream
;
328 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
333 metadata_channel
= consumer_find_channel(key
);
334 if (!metadata_channel
) {
335 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
340 metadata_stream
= metadata_channel
->metadata_stream
;
341 assert(metadata_stream
);
343 /* Flag once that we have a valid relayd for the stream. */
344 if (relayd_id
!= (uint64_t) -1ULL) {
349 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
354 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
355 metadata_stream
->chan
->tracefile_size
,
356 metadata_stream
->tracefile_count_current
,
357 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
361 metadata_stream
->out_fd
= ret
;
365 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
367 if (ret_read
!= -EAGAIN
) {
368 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
372 /* ret_read is negative at this point so we will exit the loop. */
375 } while (ret_read
>= 0);
378 close_relayd_stream(metadata_stream
);
379 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
381 if (metadata_stream
->out_fd
>= 0) {
382 ret
= close(metadata_stream
->out_fd
);
384 PERROR("Kernel consumer snapshot metadata close out_fd");
386 * Don't go on error here since the snapshot was successful at this
387 * point but somehow the close failed.
390 metadata_stream
->out_fd
= -1;
396 cds_list_del(&metadata_stream
->send_node
);
397 consumer_stream_destroy(metadata_stream
, NULL
);
398 metadata_channel
->metadata_stream
= NULL
;
405 * Receive command from session daemon and process it.
407 * Return 1 on success else a negative value or 0.
409 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
410 int sock
, struct pollfd
*consumer_sockpoll
)
413 enum lttng_error_code ret_code
= LTTNG_OK
;
414 struct lttcomm_consumer_msg msg
;
416 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
417 if (ret
!= sizeof(msg
)) {
419 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
424 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
426 * Notify the session daemon that the command is completed.
428 * On transport layer error, the function call will print an error
429 * message so handling the returned code is a bit useless since we
430 * return an error code anyway.
432 (void) consumer_send_status_msg(sock
, ret_code
);
436 /* relayd needs RCU read-side protection */
439 switch (msg
.cmd_type
) {
440 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
442 /* Session daemon status message are handled in the following call. */
443 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
444 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
445 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
446 msg
.u
.relayd_sock
.relayd_session_id
);
449 case LTTNG_CONSUMER_ADD_CHANNEL
:
451 struct lttng_consumer_channel
*new_channel
;
454 /* First send a status message before receiving the fds. */
455 ret
= consumer_send_status_msg(sock
, ret_code
);
457 /* Somehow, the session daemon is not responding anymore. */
460 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
461 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
462 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
463 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
464 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
465 msg
.u
.channel
.tracefile_size
,
466 msg
.u
.channel
.tracefile_count
, 0,
467 msg
.u
.channel
.monitor
,
468 msg
.u
.channel
.live_timer_interval
);
469 if (new_channel
== NULL
) {
470 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
473 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
474 switch (msg
.u
.channel
.output
) {
475 case LTTNG_EVENT_SPLICE
:
476 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
478 case LTTNG_EVENT_MMAP
:
479 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
482 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
486 /* Translate and save channel type. */
487 switch (msg
.u
.channel
.type
) {
488 case CONSUMER_CHANNEL_TYPE_DATA
:
489 case CONSUMER_CHANNEL_TYPE_METADATA
:
490 new_channel
->type
= msg
.u
.channel
.type
;
497 if (ctx
->on_recv_channel
!= NULL
) {
498 ret_recv
= ctx
->on_recv_channel(new_channel
);
500 ret
= consumer_add_channel(new_channel
, ctx
);
501 } else if (ret_recv
< 0) {
505 ret
= consumer_add_channel(new_channel
, ctx
);
507 if (CONSUMER_CHANNEL_TYPE_DATA
) {
508 consumer_timer_live_start(new_channel
,
509 msg
.u
.channel
.live_timer_interval
);
512 /* If we received an error in add_channel, we need to report it. */
514 ret
= consumer_send_status_msg(sock
, ret
);
523 case LTTNG_CONSUMER_ADD_STREAM
:
526 struct lttng_pipe
*stream_pipe
;
527 struct lttng_consumer_stream
*new_stream
;
528 struct lttng_consumer_channel
*channel
;
532 * Get stream's channel reference. Needed when adding the stream to the
535 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
538 * We could not find the channel. Can happen if cpu hotplug
539 * happens while tearing down.
541 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
542 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
545 /* First send a status message before receiving the fds. */
546 ret
= consumer_send_status_msg(sock
, ret_code
);
548 /* Somehow, the session daemon is not responding anymore. */
551 if (ret_code
!= LTTNG_OK
) {
552 /* Channel was not found. */
557 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
562 /* Get stream file descriptor from socket */
563 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
564 if (ret
!= sizeof(fd
)) {
565 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
571 * Send status code to session daemon only if the recv works. If the
572 * above recv() failed, the session daemon is notified through the
573 * error socket and the teardown is eventually done.
575 ret
= consumer_send_status_msg(sock
, ret_code
);
577 /* Somehow, the session daemon is not responding anymore. */
581 new_stream
= consumer_allocate_stream(channel
->key
,
583 LTTNG_CONSUMER_ACTIVE_STREAM
,
593 if (new_stream
== NULL
) {
598 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
604 new_stream
->chan
= channel
;
605 new_stream
->wait_fd
= fd
;
606 switch (channel
->output
) {
607 case CONSUMER_CHANNEL_SPLICE
:
608 new_stream
->output
= LTTNG_EVENT_SPLICE
;
610 case CONSUMER_CHANNEL_MMAP
:
611 new_stream
->output
= LTTNG_EVENT_MMAP
;
614 ERR("Stream output unknown %d", channel
->output
);
619 * We've just assigned the channel to the stream so increment the
620 * refcount right now. We don't need to increment the refcount for
621 * streams in no monitor because we handle manually the cleanup of
622 * those. It is very important to make sure there is NO prior
623 * consumer_del_stream() calls or else the refcount will be unbalanced.
625 if (channel
->monitor
) {
626 uatomic_inc(&new_stream
->chan
->refcount
);
630 * The buffer flush is done on the session daemon side for the kernel
631 * so no need for the stream "hangup_flush_done" variable to be
632 * tracked. This is important for a kernel stream since we don't rely
633 * on the flush state of the stream to read data. It's not the case for
634 * user space tracing.
636 new_stream
->hangup_flush_done
= 0;
638 if (ctx
->on_recv_stream
) {
639 ret
= ctx
->on_recv_stream(new_stream
);
641 consumer_stream_free(new_stream
);
646 if (new_stream
->metadata_flag
) {
647 channel
->metadata_stream
= new_stream
;
650 /* Do not monitor this stream. */
651 if (!channel
->monitor
) {
652 DBG("Kernel consumer add stream %s in no monitor mode with "
653 "relayd id %" PRIu64
, new_stream
->name
,
654 new_stream
->net_seq_idx
);
655 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
659 /* Send stream to relayd if the stream has an ID. */
660 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
661 ret
= consumer_send_relayd_stream(new_stream
,
662 new_stream
->chan
->pathname
);
664 consumer_stream_free(new_stream
);
669 /* Get the right pipe where the stream will be sent. */
670 if (new_stream
->metadata_flag
) {
671 ret
= consumer_add_metadata_stream(new_stream
);
673 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
675 consumer_stream_free(new_stream
);
678 stream_pipe
= ctx
->consumer_metadata_pipe
;
680 ret
= consumer_add_data_stream(new_stream
);
682 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
684 consumer_stream_free(new_stream
);
687 stream_pipe
= ctx
->consumer_data_pipe
;
690 /* Vitible to other threads */
691 new_stream
->globally_visible
= 1;
693 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
695 ERR("Consumer write %s stream to pipe %d",
696 new_stream
->metadata_flag
? "metadata" : "data",
697 lttng_pipe_get_writefd(stream_pipe
));
698 if (new_stream
->metadata_flag
) {
699 consumer_del_stream_for_metadata(new_stream
);
701 consumer_del_stream_for_data(new_stream
);
706 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
707 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
710 case LTTNG_CONSUMER_UPDATE_STREAM
:
715 case LTTNG_CONSUMER_DESTROY_RELAYD
:
717 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
718 struct consumer_relayd_sock_pair
*relayd
;
720 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
722 /* Get relayd reference if exists. */
723 relayd
= consumer_find_relayd(index
);
724 if (relayd
== NULL
) {
725 DBG("Unable to find relayd %" PRIu64
, index
);
726 ret_code
= LTTNG_ERR_NO_CONSUMER
;
730 * Each relayd socket pair has a refcount of stream attached to it
731 * which tells if the relayd is still active or not depending on the
734 * This will set the destroy flag of the relayd object and destroy it
735 * if the refcount reaches zero when called.
737 * The destroy can happen either here or when a stream fd hangs up.
740 consumer_flag_relayd_for_destroy(relayd
);
743 ret
= consumer_send_status_msg(sock
, ret_code
);
745 /* Somehow, the session daemon is not responding anymore. */
751 case LTTNG_CONSUMER_DATA_PENDING
:
754 uint64_t id
= msg
.u
.data_pending
.session_id
;
756 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
758 ret
= consumer_data_pending(id
);
760 /* Send back returned value to session daemon */
761 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
763 PERROR("send data pending ret code");
768 * No need to send back a status message since the data pending
769 * returned value is the response.
773 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
775 if (msg
.u
.snapshot_channel
.metadata
== 1) {
776 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
777 msg
.u
.snapshot_channel
.pathname
,
778 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
780 ERR("Snapshot metadata failed");
781 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
784 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
785 msg
.u
.snapshot_channel
.pathname
,
786 msg
.u
.snapshot_channel
.relayd_id
,
787 msg
.u
.snapshot_channel
.max_stream_size
,
790 ERR("Snapshot channel failed");
791 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
795 ret
= consumer_send_status_msg(sock
, ret_code
);
797 /* Somehow, the session daemon is not responding anymore. */
802 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
804 uint64_t key
= msg
.u
.destroy_channel
.key
;
805 struct lttng_consumer_channel
*channel
;
807 channel
= consumer_find_channel(key
);
809 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
810 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
813 ret
= consumer_send_status_msg(sock
, ret_code
);
815 /* Somehow, the session daemon is not responding anymore. */
820 * This command should ONLY be issued for channel with streams set in
823 assert(!channel
->monitor
);
826 * The refcount should ALWAYS be 0 in the case of a channel in no
829 assert(!uatomic_sub_return(&channel
->refcount
, 1));
831 consumer_del_channel(channel
);
843 * Return 1 to indicate success since the 0 value can be a socket
844 * shutdown during the recv() or send() call.
850 /* This will issue a consumer stop. */
855 * Populate index values of a kernel stream. Values are set in big endian order.
857 * Return 0 on success or else a negative value.
859 static int get_index_values(struct lttng_packet_index
*index
, int infd
)
863 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
865 PERROR("kernctl_get_timestamp_begin");
868 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
870 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
872 PERROR("kernctl_get_timestamp_end");
875 index
->timestamp_end
= htobe64(index
->timestamp_end
);
877 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
879 PERROR("kernctl_get_events_discarded");
882 index
->events_discarded
= htobe64(index
->events_discarded
);
884 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
886 PERROR("kernctl_get_content_size");
889 index
->content_size
= htobe64(index
->content_size
);
891 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
893 PERROR("kernctl_get_packet_size");
896 index
->packet_size
= htobe64(index
->packet_size
);
898 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
900 PERROR("kernctl_get_stream_id");
903 index
->stream_id
= htobe64(index
->stream_id
);
909 * Sync metadata meaning request them to the session daemon and snapshot to the
910 * metadata thread can consumer them.
912 * Metadata stream lock MUST be acquired.
914 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
915 * is empty or a negative value on error.
917 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
923 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
925 ERR("Failed to flush kernel stream");
929 ret
= kernctl_snapshot(metadata
->wait_fd
);
931 if (errno
!= EAGAIN
) {
932 ERR("Sync metadata, taking kernel snapshot failed.");
935 DBG("Sync metadata, no new kernel metadata");
936 /* No new metadata, exit. */
946 * Consume data on a file descriptor and write it on a trace file.
948 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
949 struct lttng_consumer_local_data
*ctx
)
951 unsigned long len
, subbuf_size
, padding
;
952 int err
, write_index
= 1;
954 int infd
= stream
->wait_fd
;
955 struct lttng_packet_index index
;
957 DBG("In read_subbuffer (infd : %d)", infd
);
959 /* Get the next subbuffer */
960 err
= kernctl_get_next_subbuf(infd
);
963 * This is a debug message even for single-threaded consumer,
964 * because poll() have more relaxed criterions than get subbuf,
965 * so get_subbuf may fail for short race windows where poll()
966 * would issue wakeups.
968 DBG("Reserving sub buffer failed (everything is normal, "
969 "it is due to concurrency)");
974 /* Get the full subbuffer size including padding */
975 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
977 perror("Getting sub-buffer len failed.");
982 if (!stream
->metadata_flag
) {
983 ret
= get_index_values(&index
, infd
);
991 switch (stream
->chan
->output
) {
992 case CONSUMER_CHANNEL_SPLICE
:
994 * XXX: The lttng-modules splice "actor" does not handle copying
995 * partial pages hence only using the subbuffer size without the
996 * padding makes the splice fail.
1001 /* splice the subbuffer to the tracefile */
1002 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1005 * XXX: Splice does not support network streaming so the return value
1006 * is simply checked against subbuf_size and not like the mmap() op.
1008 if (ret
!= subbuf_size
) {
1010 * display the error but continue processing to try
1011 * to release the subbuffer
1013 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1018 case CONSUMER_CHANNEL_MMAP
:
1019 /* Get subbuffer size without padding */
1020 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1022 perror("Getting sub-buffer len failed.");
1027 /* Make sure the tracer is not gone mad on us! */
1028 assert(len
>= subbuf_size
);
1030 padding
= len
- subbuf_size
;
1032 /* write the subbuffer to the tracefile */
1033 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1036 * The mmap operation should write subbuf_size amount of data when
1037 * network streaming or the full padding (len) size when we are _not_
1040 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1041 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1043 * Display the error but continue processing to try to release the
1046 ERR("Error writing to tracefile "
1047 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1048 ret
, len
, subbuf_size
);
1053 ERR("Unknown output method");
1057 err
= kernctl_put_next_subbuf(infd
);
1059 if (errno
== EFAULT
) {
1060 perror("Error in unreserving sub buffer\n");
1061 } else if (errno
== EIO
) {
1062 /* Should never happen with newer LTTng versions */
1063 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1069 /* Write index if needed. */
1074 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1076 * In live, block until all the metadata is sent.
1078 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1084 err
= consumer_stream_write_index(stream
, &index
);
1093 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1100 * Don't create anything if this is set for streaming or should not be
1103 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1104 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1105 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1106 stream
->uid
, stream
->gid
, NULL
);
1110 stream
->out_fd
= ret
;
1111 stream
->tracefile_size_current
= 0;
1113 if (!stream
->metadata_flag
) {
1114 ret
= index_create_file(stream
->chan
->pathname
,
1115 stream
->name
, stream
->uid
, stream
->gid
,
1116 stream
->chan
->tracefile_size
,
1117 stream
->tracefile_count_current
);
1121 stream
->index_fd
= ret
;
1125 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1126 /* get the len of the mmap region */
1127 unsigned long mmap_len
;
1129 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1131 PERROR("kernctl_get_mmap_len");
1133 goto error_close_fd
;
1135 stream
->mmap_len
= (size_t) mmap_len
;
1137 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1138 MAP_PRIVATE
, stream
->wait_fd
, 0);
1139 if (stream
->mmap_base
== MAP_FAILED
) {
1140 PERROR("Error mmaping");
1142 goto error_close_fd
;
1146 /* we return 0 to let the library handle the FD internally */
1150 if (stream
->out_fd
>= 0) {
1153 err
= close(stream
->out_fd
);
1155 stream
->out_fd
= -1;
1162 * Check if data is still being extracted from the buffers for a specific
1163 * stream. Consumer data lock MUST be acquired before calling this function
1164 * and the stream lock.
1166 * Return 1 if the traced data are still getting read else 0 meaning that the
1167 * data is available for trace viewer reading.
1169 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1175 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1180 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1182 /* There is still data so let's put back this subbuffer. */
1183 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1185 ret
= 1; /* Data is pending */
1189 /* Data is NOT pending and ready to be read. */