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 <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer-timer.h>
46 #include "kernel-consumer.h"
48 extern struct lttng_consumer_global_data consumer_data
;
49 extern int consumer_poll_timeout
;
50 extern volatile int consumer_quit
;
53 * Take a snapshot for a specific fd
55 * Returns 0 on success, < 0 on error
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
60 int infd
= stream
->wait_fd
;
62 ret
= kernctl_snapshot(infd
);
64 PERROR("Getting sub-buffer snapshot.");
72 * Get the produced position
74 * Returns 0 on success, < 0 on error
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
80 int infd
= stream
->wait_fd
;
82 ret
= kernctl_snapshot_get_produced(infd
, pos
);
84 PERROR("kernctl_snapshot_get_produced");
92 * Get the consumerd position
94 * Returns 0 on success, < 0 on error
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
100 int infd
= stream
->wait_fd
;
102 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
104 PERROR("kernctl_snapshot_get_consumed");
112 * Take a snapshot of all the stream of a channel
114 * Returns 0 on success, < 0 on error
116 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
117 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
118 struct lttng_consumer_local_data
*ctx
)
121 unsigned long consumed_pos
, produced_pos
;
122 struct lttng_consumer_channel
*channel
;
123 struct lttng_consumer_stream
*stream
;
125 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
129 channel
= consumer_find_channel(key
);
131 ERR("No channel found for key %" PRIu64
, key
);
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
138 ERR("Unsupported output %d", channel
->output
);
143 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
145 health_code_update();
148 * Lock stream because we are about to change its state.
150 pthread_mutex_lock(&stream
->lock
);
153 * Assign the received relayd ID so we can use it for streaming. The streams
154 * are not visible to anyone so this is OK to change it.
156 stream
->net_seq_idx
= relayd_id
;
157 channel
->relayd_id
= relayd_id
;
158 if (relayd_id
!= (uint64_t) -1ULL) {
159 ret
= consumer_send_relayd_stream(stream
, path
);
161 ERR("sending stream to relayd");
165 ret
= utils_create_stream_file(path
, stream
->name
,
166 stream
->chan
->tracefile_size
,
167 stream
->tracefile_count_current
,
168 stream
->uid
, stream
->gid
, NULL
);
170 ERR("utils_create_stream_file");
174 stream
->out_fd
= ret
;
175 stream
->tracefile_size_current
= 0;
177 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
178 path
, stream
->name
, stream
->key
);
180 if (relayd_id
!= -1ULL) {
181 ret
= consumer_send_relayd_streams_sent(relayd_id
);
183 ERR("sending streams sent to relayd");
188 ret
= kernctl_buffer_flush(stream
->wait_fd
);
190 ERR("Failed to flush kernel stream");
195 ret
= lttng_kconsumer_take_snapshot(stream
);
197 ERR("Taking kernel snapshot");
201 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
203 ERR("Produced kernel snapshot position");
207 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
209 ERR("Consumerd kernel snapshot position");
213 if (stream
->max_sb_size
== 0) {
214 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
215 &stream
->max_sb_size
);
217 ERR("Getting kernel max_sb_size");
223 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
224 produced_pos
, nb_packets_per_stream
,
225 stream
->max_sb_size
);
227 while (consumed_pos
< produced_pos
) {
229 unsigned long len
, padded_len
;
231 health_code_update();
233 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
235 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
237 if (errno
!= EAGAIN
) {
238 PERROR("kernctl_get_subbuf snapshot");
242 DBG("Kernel consumer get subbuf failed. Skipping it.");
243 consumed_pos
+= stream
->max_sb_size
;
247 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
249 ERR("Snapshot kernctl_get_subbuf_size");
251 goto error_put_subbuf
;
254 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
256 ERR("Snapshot kernctl_get_padded_subbuf_size");
258 goto error_put_subbuf
;
261 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
262 padded_len
- len
, NULL
);
264 * We write the padded len in local tracefiles but the data len
265 * when using a relay. Display the error but continue processing
266 * to try to release the subbuffer.
268 if (relayd_id
!= (uint64_t) -1ULL) {
269 if (read_len
!= len
) {
270 ERR("Error sending to the relay (ret: %zd != len: %lu)",
274 if (read_len
!= padded_len
) {
275 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
276 read_len
, padded_len
);
280 ret
= kernctl_put_subbuf(stream
->wait_fd
);
282 ERR("Snapshot kernctl_put_subbuf");
286 consumed_pos
+= stream
->max_sb_size
;
289 if (relayd_id
== (uint64_t) -1ULL) {
290 if (stream
->out_fd
>= 0) {
291 ret
= close(stream
->out_fd
);
293 PERROR("Kernel consumer snapshot close out_fd");
299 close_relayd_stream(stream
);
300 stream
->net_seq_idx
= (uint64_t) -1ULL;
302 pthread_mutex_unlock(&stream
->lock
);
310 ret
= kernctl_put_subbuf(stream
->wait_fd
);
313 ERR("Snapshot kernctl_put_subbuf error path");
316 pthread_mutex_unlock(&stream
->lock
);
323 * Read the whole metadata available for a snapshot.
325 * Returns 0 on success, < 0 on error
327 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
328 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
330 int ret
, use_relayd
= 0;
332 struct lttng_consumer_channel
*metadata_channel
;
333 struct lttng_consumer_stream
*metadata_stream
;
337 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
342 metadata_channel
= consumer_find_channel(key
);
343 if (!metadata_channel
) {
344 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
349 metadata_stream
= metadata_channel
->metadata_stream
;
350 assert(metadata_stream
);
352 /* Flag once that we have a valid relayd for the stream. */
353 if (relayd_id
!= (uint64_t) -1ULL) {
358 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
363 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
364 metadata_stream
->chan
->tracefile_size
,
365 metadata_stream
->tracefile_count_current
,
366 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
370 metadata_stream
->out_fd
= ret
;
374 health_code_update();
376 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
378 if (ret_read
!= -EAGAIN
) {
379 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
383 /* ret_read is negative at this point so we will exit the loop. */
386 } while (ret_read
>= 0);
389 close_relayd_stream(metadata_stream
);
390 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
392 if (metadata_stream
->out_fd
>= 0) {
393 ret
= close(metadata_stream
->out_fd
);
395 PERROR("Kernel consumer snapshot metadata close out_fd");
397 * Don't go on error here since the snapshot was successful at this
398 * point but somehow the close failed.
401 metadata_stream
->out_fd
= -1;
407 cds_list_del(&metadata_stream
->send_node
);
408 consumer_stream_destroy(metadata_stream
, NULL
);
409 metadata_channel
->metadata_stream
= NULL
;
416 * Receive command from session daemon and process it.
418 * Return 1 on success else a negative value or 0.
420 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
421 int sock
, struct pollfd
*consumer_sockpoll
)
424 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
425 struct lttcomm_consumer_msg msg
;
427 health_code_update();
429 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
430 if (ret
!= sizeof(msg
)) {
432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
438 health_code_update();
440 /* Deprecated command */
441 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
443 health_code_update();
445 /* relayd needs RCU read-side protection */
448 switch (msg
.cmd_type
) {
449 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
451 /* Session daemon status message are handled in the following call. */
452 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
453 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
454 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
455 msg
.u
.relayd_sock
.relayd_session_id
);
458 case LTTNG_CONSUMER_ADD_CHANNEL
:
460 struct lttng_consumer_channel
*new_channel
;
463 health_code_update();
465 /* First send a status message before receiving the fds. */
466 ret
= consumer_send_status_msg(sock
, ret_code
);
468 /* Somehow, the session daemon is not responding anymore. */
472 health_code_update();
474 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
475 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
476 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
477 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
478 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
479 msg
.u
.channel
.tracefile_size
,
480 msg
.u
.channel
.tracefile_count
, 0,
481 msg
.u
.channel
.monitor
,
482 msg
.u
.channel
.live_timer_interval
);
483 if (new_channel
== NULL
) {
484 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
487 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
488 switch (msg
.u
.channel
.output
) {
489 case LTTNG_EVENT_SPLICE
:
490 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
492 case LTTNG_EVENT_MMAP
:
493 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
496 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
500 /* Translate and save channel type. */
501 switch (msg
.u
.channel
.type
) {
502 case CONSUMER_CHANNEL_TYPE_DATA
:
503 case CONSUMER_CHANNEL_TYPE_METADATA
:
504 new_channel
->type
= msg
.u
.channel
.type
;
511 health_code_update();
513 if (ctx
->on_recv_channel
!= NULL
) {
514 ret_recv
= ctx
->on_recv_channel(new_channel
);
516 ret
= consumer_add_channel(new_channel
, ctx
);
517 } else if (ret_recv
< 0) {
521 ret
= consumer_add_channel(new_channel
, ctx
);
523 if (CONSUMER_CHANNEL_TYPE_DATA
) {
524 consumer_timer_live_start(new_channel
,
525 msg
.u
.channel
.live_timer_interval
);
528 health_code_update();
530 /* If we received an error in add_channel, we need to report it. */
532 ret
= consumer_send_status_msg(sock
, ret
);
541 case LTTNG_CONSUMER_ADD_STREAM
:
544 struct lttng_pipe
*stream_pipe
;
545 struct lttng_consumer_stream
*new_stream
;
546 struct lttng_consumer_channel
*channel
;
550 * Get stream's channel reference. Needed when adding the stream to the
553 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
556 * We could not find the channel. Can happen if cpu hotplug
557 * happens while tearing down.
559 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
560 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
563 health_code_update();
565 /* First send a status message before receiving the fds. */
566 ret
= consumer_send_status_msg(sock
, ret_code
);
568 /* Somehow, the session daemon is not responding anymore. */
572 health_code_update();
574 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
575 /* Channel was not found. */
581 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
587 health_code_update();
589 /* Get stream file descriptor from socket */
590 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
591 if (ret
!= sizeof(fd
)) {
592 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
597 health_code_update();
600 * Send status code to session daemon only if the recv works. If the
601 * above recv() failed, the session daemon is notified through the
602 * error socket and the teardown is eventually done.
604 ret
= consumer_send_status_msg(sock
, ret_code
);
606 /* Somehow, the session daemon is not responding anymore. */
610 health_code_update();
612 new_stream
= consumer_allocate_stream(channel
->key
,
614 LTTNG_CONSUMER_ACTIVE_STREAM
,
624 if (new_stream
== NULL
) {
629 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
635 new_stream
->chan
= channel
;
636 new_stream
->wait_fd
= fd
;
637 switch (channel
->output
) {
638 case CONSUMER_CHANNEL_SPLICE
:
639 new_stream
->output
= LTTNG_EVENT_SPLICE
;
640 ret
= utils_create_pipe(new_stream
->splice_pipe
);
645 case CONSUMER_CHANNEL_MMAP
:
646 new_stream
->output
= LTTNG_EVENT_MMAP
;
649 ERR("Stream output unknown %d", channel
->output
);
654 * We've just assigned the channel to the stream so increment the
655 * refcount right now. We don't need to increment the refcount for
656 * streams in no monitor because we handle manually the cleanup of
657 * those. It is very important to make sure there is NO prior
658 * consumer_del_stream() calls or else the refcount will be unbalanced.
660 if (channel
->monitor
) {
661 uatomic_inc(&new_stream
->chan
->refcount
);
665 * The buffer flush is done on the session daemon side for the kernel
666 * so no need for the stream "hangup_flush_done" variable to be
667 * tracked. This is important for a kernel stream since we don't rely
668 * on the flush state of the stream to read data. It's not the case for
669 * user space tracing.
671 new_stream
->hangup_flush_done
= 0;
673 health_code_update();
675 if (ctx
->on_recv_stream
) {
676 ret
= ctx
->on_recv_stream(new_stream
);
678 consumer_stream_free(new_stream
);
683 health_code_update();
685 if (new_stream
->metadata_flag
) {
686 channel
->metadata_stream
= new_stream
;
689 /* Do not monitor this stream. */
690 if (!channel
->monitor
) {
691 DBG("Kernel consumer add stream %s in no monitor mode with "
692 "relayd id %" PRIu64
, new_stream
->name
,
693 new_stream
->net_seq_idx
);
694 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
698 /* Send stream to relayd if the stream has an ID. */
699 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
700 ret
= consumer_send_relayd_stream(new_stream
,
701 new_stream
->chan
->pathname
);
703 consumer_stream_free(new_stream
);
708 /* Get the right pipe where the stream will be sent. */
709 if (new_stream
->metadata_flag
) {
710 ret
= consumer_add_metadata_stream(new_stream
);
712 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
714 consumer_stream_free(new_stream
);
717 stream_pipe
= ctx
->consumer_metadata_pipe
;
719 ret
= consumer_add_data_stream(new_stream
);
721 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
723 consumer_stream_free(new_stream
);
726 stream_pipe
= ctx
->consumer_data_pipe
;
729 /* Vitible to other threads */
730 new_stream
->globally_visible
= 1;
732 health_code_update();
734 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
736 ERR("Consumer write %s stream to pipe %d",
737 new_stream
->metadata_flag
? "metadata" : "data",
738 lttng_pipe_get_writefd(stream_pipe
));
739 if (new_stream
->metadata_flag
) {
740 consumer_del_stream_for_metadata(new_stream
);
742 consumer_del_stream_for_data(new_stream
);
747 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
748 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
751 case LTTNG_CONSUMER_STREAMS_SENT
:
753 struct lttng_consumer_channel
*channel
;
756 * Get stream's channel reference. Needed when adding the stream to the
759 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
762 * We could not find the channel. Can happen if cpu hotplug
763 * happens while tearing down.
765 ERR("Unable to find channel key %" PRIu64
,
766 msg
.u
.sent_streams
.channel_key
);
767 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
770 health_code_update();
773 * Send status code to session daemon.
775 ret
= consumer_send_status_msg(sock
, ret_code
);
776 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
777 /* Somehow, the session daemon is not responding anymore. */
781 health_code_update();
784 * We should not send this message if we don't monitor the
785 * streams in this channel.
787 if (!channel
->monitor
) {
791 health_code_update();
792 /* Send stream to relayd if the stream has an ID. */
793 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
794 ret
= consumer_send_relayd_streams_sent(
795 msg
.u
.sent_streams
.net_seq_idx
);
802 case LTTNG_CONSUMER_UPDATE_STREAM
:
807 case LTTNG_CONSUMER_DESTROY_RELAYD
:
809 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
810 struct consumer_relayd_sock_pair
*relayd
;
812 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
814 /* Get relayd reference if exists. */
815 relayd
= consumer_find_relayd(index
);
816 if (relayd
== NULL
) {
817 DBG("Unable to find relayd %" PRIu64
, index
);
818 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
822 * Each relayd socket pair has a refcount of stream attached to it
823 * which tells if the relayd is still active or not depending on the
826 * This will set the destroy flag of the relayd object and destroy it
827 * if the refcount reaches zero when called.
829 * The destroy can happen either here or when a stream fd hangs up.
832 consumer_flag_relayd_for_destroy(relayd
);
835 health_code_update();
837 ret
= consumer_send_status_msg(sock
, ret_code
);
839 /* Somehow, the session daemon is not responding anymore. */
845 case LTTNG_CONSUMER_DATA_PENDING
:
848 uint64_t id
= msg
.u
.data_pending
.session_id
;
850 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
852 ret
= consumer_data_pending(id
);
854 health_code_update();
856 /* Send back returned value to session daemon */
857 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
859 PERROR("send data pending ret code");
864 * No need to send back a status message since the data pending
865 * returned value is the response.
869 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
871 if (msg
.u
.snapshot_channel
.metadata
== 1) {
872 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
873 msg
.u
.snapshot_channel
.pathname
,
874 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
876 ERR("Snapshot metadata failed");
877 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
880 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
881 msg
.u
.snapshot_channel
.pathname
,
882 msg
.u
.snapshot_channel
.relayd_id
,
883 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
886 ERR("Snapshot channel failed");
887 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
891 health_code_update();
893 ret
= consumer_send_status_msg(sock
, ret_code
);
895 /* Somehow, the session daemon is not responding anymore. */
900 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
902 uint64_t key
= msg
.u
.destroy_channel
.key
;
903 struct lttng_consumer_channel
*channel
;
905 channel
= consumer_find_channel(key
);
907 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
908 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
911 health_code_update();
913 ret
= consumer_send_status_msg(sock
, ret_code
);
915 /* Somehow, the session daemon is not responding anymore. */
919 health_code_update();
921 /* Stop right now if no channel was found. */
927 * This command should ONLY be issued for channel with streams set in
930 assert(!channel
->monitor
);
933 * The refcount should ALWAYS be 0 in the case of a channel in no
936 assert(!uatomic_sub_return(&channel
->refcount
, 1));
938 consumer_del_channel(channel
);
950 * Return 1 to indicate success since the 0 value can be a socket
951 * shutdown during the recv() or send() call.
953 health_code_update();
958 /* This will issue a consumer stop. */
963 * Populate index values of a kernel stream. Values are set in big endian order.
965 * Return 0 on success or else a negative value.
967 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
971 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
973 PERROR("kernctl_get_timestamp_begin");
976 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
978 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
980 PERROR("kernctl_get_timestamp_end");
983 index
->timestamp_end
= htobe64(index
->timestamp_end
);
985 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
987 PERROR("kernctl_get_events_discarded");
990 index
->events_discarded
= htobe64(index
->events_discarded
);
992 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
994 PERROR("kernctl_get_content_size");
997 index
->content_size
= htobe64(index
->content_size
);
999 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1001 PERROR("kernctl_get_packet_size");
1004 index
->packet_size
= htobe64(index
->packet_size
);
1006 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1008 PERROR("kernctl_get_stream_id");
1011 index
->stream_id
= htobe64(index
->stream_id
);
1017 * Sync metadata meaning request them to the session daemon and snapshot to the
1018 * metadata thread can consumer them.
1020 * Metadata stream lock MUST be acquired.
1022 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1023 * is empty or a negative value on error.
1025 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1031 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1033 ERR("Failed to flush kernel stream");
1037 ret
= kernctl_snapshot(metadata
->wait_fd
);
1039 if (errno
!= EAGAIN
) {
1040 ERR("Sync metadata, taking kernel snapshot failed.");
1043 DBG("Sync metadata, no new kernel metadata");
1044 /* No new metadata, exit. */
1054 * Consume data on a file descriptor and write it on a trace file.
1056 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1057 struct lttng_consumer_local_data
*ctx
)
1059 unsigned long len
, subbuf_size
, padding
;
1060 int err
, write_index
= 1;
1062 int infd
= stream
->wait_fd
;
1063 struct ctf_packet_index index
;
1065 DBG("In read_subbuffer (infd : %d)", infd
);
1067 /* Get the next subbuffer */
1068 err
= kernctl_get_next_subbuf(infd
);
1071 * This is a debug message even for single-threaded consumer,
1072 * because poll() have more relaxed criterions than get subbuf,
1073 * so get_subbuf may fail for short race windows where poll()
1074 * would issue wakeups.
1076 DBG("Reserving sub buffer failed (everything is normal, "
1077 "it is due to concurrency)");
1082 /* Get the full subbuffer size including padding */
1083 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1085 PERROR("Getting sub-buffer len failed.");
1086 err
= kernctl_put_subbuf(infd
);
1088 if (errno
== EFAULT
) {
1089 PERROR("Error in unreserving sub buffer\n");
1090 } else if (errno
== EIO
) {
1091 /* Should never happen with newer LTTng versions */
1092 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1101 if (!stream
->metadata_flag
) {
1102 ret
= get_index_values(&index
, infd
);
1104 err
= kernctl_put_subbuf(infd
);
1106 if (errno
== EFAULT
) {
1107 PERROR("Error in unreserving sub buffer\n");
1108 } else if (errno
== EIO
) {
1109 /* Should never happen with newer LTTng versions */
1110 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1121 switch (stream
->chan
->output
) {
1122 case CONSUMER_CHANNEL_SPLICE
:
1124 * XXX: The lttng-modules splice "actor" does not handle copying
1125 * partial pages hence only using the subbuffer size without the
1126 * padding makes the splice fail.
1131 /* splice the subbuffer to the tracefile */
1132 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1135 * XXX: Splice does not support network streaming so the return value
1136 * is simply checked against subbuf_size and not like the mmap() op.
1138 if (ret
!= subbuf_size
) {
1140 * display the error but continue processing to try
1141 * to release the subbuffer
1143 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1148 case CONSUMER_CHANNEL_MMAP
:
1149 /* Get subbuffer size without padding */
1150 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1152 PERROR("Getting sub-buffer len failed.");
1153 err
= kernctl_put_subbuf(infd
);
1155 if (errno
== EFAULT
) {
1156 PERROR("Error in unreserving sub buffer\n");
1157 } else if (errno
== EIO
) {
1158 /* Should never happen with newer LTTng versions */
1159 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1168 /* Make sure the tracer is not gone mad on us! */
1169 assert(len
>= subbuf_size
);
1171 padding
= len
- subbuf_size
;
1173 /* write the subbuffer to the tracefile */
1174 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1177 * The mmap operation should write subbuf_size amount of data when
1178 * network streaming or the full padding (len) size when we are _not_
1181 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1182 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1184 * Display the error but continue processing to try to release the
1185 * subbuffer. This is a DBG statement since this is possible to
1186 * happen without being a critical error.
1188 DBG("Error writing to tracefile "
1189 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1190 ret
, len
, subbuf_size
);
1195 ERR("Unknown output method");
1199 err
= kernctl_put_next_subbuf(infd
);
1201 if (errno
== EFAULT
) {
1202 PERROR("Error in unreserving sub buffer\n");
1203 } else if (errno
== EIO
) {
1204 /* Should never happen with newer LTTng versions */
1205 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1211 /* Write index if needed. */
1216 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1218 * In live, block until all the metadata is sent.
1220 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1221 assert(!stream
->missed_metadata_flush
);
1222 stream
->waiting_on_metadata
= true;
1223 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1225 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1227 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1228 stream
->waiting_on_metadata
= false;
1229 if (stream
->missed_metadata_flush
) {
1230 stream
->missed_metadata_flush
= false;
1231 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1232 (void) consumer_flush_kernel_index(stream
);
1234 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1241 err
= consumer_stream_write_index(stream
, &index
);
1250 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1257 * Don't create anything if this is set for streaming or should not be
1260 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1261 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1262 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1263 stream
->uid
, stream
->gid
, NULL
);
1267 stream
->out_fd
= ret
;
1268 stream
->tracefile_size_current
= 0;
1270 if (!stream
->metadata_flag
) {
1271 ret
= index_create_file(stream
->chan
->pathname
,
1272 stream
->name
, stream
->uid
, stream
->gid
,
1273 stream
->chan
->tracefile_size
,
1274 stream
->tracefile_count_current
);
1278 stream
->index_fd
= ret
;
1282 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1283 /* get the len of the mmap region */
1284 unsigned long mmap_len
;
1286 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1288 PERROR("kernctl_get_mmap_len");
1290 goto error_close_fd
;
1292 stream
->mmap_len
= (size_t) mmap_len
;
1294 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1295 MAP_PRIVATE
, stream
->wait_fd
, 0);
1296 if (stream
->mmap_base
== MAP_FAILED
) {
1297 PERROR("Error mmaping");
1299 goto error_close_fd
;
1303 /* we return 0 to let the library handle the FD internally */
1307 if (stream
->out_fd
>= 0) {
1310 err
= close(stream
->out_fd
);
1312 stream
->out_fd
= -1;
1319 * Check if data is still being extracted from the buffers for a specific
1320 * stream. Consumer data lock MUST be acquired before calling this function
1321 * and the stream lock.
1323 * Return 1 if the traced data are still getting read else 0 meaning that the
1324 * data is available for trace viewer reading.
1326 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1332 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1337 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1339 /* There is still data so let's put back this subbuffer. */
1340 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1342 ret
= 1; /* Data is pending */
1346 /* Data is NOT pending and ready to be read. */