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.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/compat/endian.h>
40 #include <common/pipe.h>
41 #include <common/relayd/relayd.h>
42 #include <common/utils.h>
43 #include <common/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer-timer.h>
47 #include "kernel-consumer.h"
49 extern struct lttng_consumer_global_data consumer_data
;
50 extern int consumer_poll_timeout
;
51 extern volatile int consumer_quit
;
54 * Take a snapshot for a specific fd
56 * Returns 0 on success, < 0 on error
58 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
61 int infd
= stream
->wait_fd
;
63 ret
= kernctl_snapshot(infd
);
65 PERROR("Getting sub-buffer snapshot.");
73 * Get the produced position
75 * Returns 0 on success, < 0 on error
77 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
81 int infd
= stream
->wait_fd
;
83 ret
= kernctl_snapshot_get_produced(infd
, pos
);
85 PERROR("kernctl_snapshot_get_produced");
93 * Get the consumerd position
95 * Returns 0 on success, < 0 on error
97 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
101 int infd
= stream
->wait_fd
;
103 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
105 PERROR("kernctl_snapshot_get_consumed");
113 * Take a snapshot of all the stream of a channel
115 * Returns 0 on success, < 0 on error
117 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
118 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
119 struct lttng_consumer_local_data
*ctx
)
122 unsigned long consumed_pos
, produced_pos
;
123 struct lttng_consumer_channel
*channel
;
124 struct lttng_consumer_stream
*stream
;
126 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
130 channel
= consumer_find_channel(key
);
132 ERR("No channel found for key %" PRIu64
, key
);
137 /* Splice is not supported yet for channel snapshot. */
138 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
139 ERR("Unsupported output %d", channel
->output
);
144 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
146 health_code_update();
149 * Lock stream because we are about to change its state.
151 pthread_mutex_lock(&stream
->lock
);
154 * Assign the received relayd ID so we can use it for streaming. The streams
155 * are not visible to anyone so this is OK to change it.
157 stream
->net_seq_idx
= relayd_id
;
158 channel
->relayd_id
= relayd_id
;
159 if (relayd_id
!= (uint64_t) -1ULL) {
160 ret
= consumer_send_relayd_stream(stream
, path
);
162 ERR("sending stream to relayd");
166 ret
= utils_create_stream_file(path
, stream
->name
,
167 stream
->chan
->tracefile_size
,
168 stream
->tracefile_count_current
,
169 stream
->uid
, stream
->gid
, NULL
);
171 ERR("utils_create_stream_file");
175 stream
->out_fd
= ret
;
176 stream
->tracefile_size_current
= 0;
178 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
179 path
, stream
->name
, stream
->key
);
181 if (relayd_id
!= -1ULL) {
182 ret
= consumer_send_relayd_streams_sent(relayd_id
);
184 ERR("sending streams sent to relayd");
189 ret
= kernctl_buffer_flush(stream
->wait_fd
);
191 ERR("Failed to flush kernel stream");
196 ret
= lttng_kconsumer_take_snapshot(stream
);
198 ERR("Taking kernel snapshot");
202 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
204 ERR("Produced kernel snapshot position");
208 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
210 ERR("Consumerd kernel snapshot position");
214 if (stream
->max_sb_size
== 0) {
215 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
216 &stream
->max_sb_size
);
218 ERR("Getting kernel max_sb_size");
224 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
225 produced_pos
, nb_packets_per_stream
,
226 stream
->max_sb_size
);
228 while (consumed_pos
< produced_pos
) {
230 unsigned long len
, padded_len
;
232 health_code_update();
234 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
236 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
238 if (errno
!= EAGAIN
) {
239 PERROR("kernctl_get_subbuf snapshot");
243 DBG("Kernel consumer get subbuf failed. Skipping it.");
244 consumed_pos
+= stream
->max_sb_size
;
248 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
250 ERR("Snapshot kernctl_get_subbuf_size");
252 goto error_put_subbuf
;
255 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
257 ERR("Snapshot kernctl_get_padded_subbuf_size");
259 goto error_put_subbuf
;
262 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
263 padded_len
- len
, NULL
);
265 * We write the padded len in local tracefiles but the data len
266 * when using a relay. Display the error but continue processing
267 * to try to release the subbuffer.
269 if (relayd_id
!= (uint64_t) -1ULL) {
270 if (read_len
!= len
) {
271 ERR("Error sending to the relay (ret: %zd != len: %lu)",
275 if (read_len
!= padded_len
) {
276 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
277 read_len
, padded_len
);
281 ret
= kernctl_put_subbuf(stream
->wait_fd
);
283 ERR("Snapshot kernctl_put_subbuf");
287 consumed_pos
+= stream
->max_sb_size
;
290 if (relayd_id
== (uint64_t) -1ULL) {
291 if (stream
->out_fd
>= 0) {
292 ret
= close(stream
->out_fd
);
294 PERROR("Kernel consumer snapshot close out_fd");
300 close_relayd_stream(stream
);
301 stream
->net_seq_idx
= (uint64_t) -1ULL;
303 pthread_mutex_unlock(&stream
->lock
);
311 ret
= kernctl_put_subbuf(stream
->wait_fd
);
314 ERR("Snapshot kernctl_put_subbuf error path");
317 pthread_mutex_unlock(&stream
->lock
);
324 * Read the whole metadata available for a snapshot.
326 * Returns 0 on success, < 0 on error
328 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
329 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
331 int ret
, use_relayd
= 0;
333 struct lttng_consumer_channel
*metadata_channel
;
334 struct lttng_consumer_stream
*metadata_stream
;
338 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
343 metadata_channel
= consumer_find_channel(key
);
344 if (!metadata_channel
) {
345 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
350 metadata_stream
= metadata_channel
->metadata_stream
;
351 assert(metadata_stream
);
353 /* Flag once that we have a valid relayd for the stream. */
354 if (relayd_id
!= (uint64_t) -1ULL) {
359 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
364 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
365 metadata_stream
->chan
->tracefile_size
,
366 metadata_stream
->tracefile_count_current
,
367 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
371 metadata_stream
->out_fd
= ret
;
375 health_code_update();
377 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
379 if (ret_read
!= -EAGAIN
) {
380 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
384 /* ret_read is negative at this point so we will exit the loop. */
387 } while (ret_read
>= 0);
390 close_relayd_stream(metadata_stream
);
391 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
393 if (metadata_stream
->out_fd
>= 0) {
394 ret
= close(metadata_stream
->out_fd
);
396 PERROR("Kernel consumer snapshot metadata close out_fd");
398 * Don't go on error here since the snapshot was successful at this
399 * point but somehow the close failed.
402 metadata_stream
->out_fd
= -1;
408 cds_list_del(&metadata_stream
->send_node
);
409 consumer_stream_destroy(metadata_stream
, NULL
);
410 metadata_channel
->metadata_stream
= NULL
;
417 * Receive command from session daemon and process it.
419 * Return 1 on success else a negative value or 0.
421 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
422 int sock
, struct pollfd
*consumer_sockpoll
)
425 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
426 struct lttcomm_consumer_msg msg
;
428 health_code_update();
430 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
431 if (ret
!= sizeof(msg
)) {
433 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
439 health_code_update();
441 /* Deprecated command */
442 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
444 health_code_update();
446 /* relayd needs RCU read-side protection */
449 switch (msg
.cmd_type
) {
450 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
452 /* Session daemon status message are handled in the following call. */
453 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
454 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
455 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
456 msg
.u
.relayd_sock
.relayd_session_id
);
459 case LTTNG_CONSUMER_ADD_CHANNEL
:
461 struct lttng_consumer_channel
*new_channel
;
464 health_code_update();
466 /* First send a status message before receiving the fds. */
467 ret
= consumer_send_status_msg(sock
, ret_code
);
469 /* Somehow, the session daemon is not responding anymore. */
473 health_code_update();
475 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
476 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
477 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
478 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
479 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
480 msg
.u
.channel
.tracefile_size
,
481 msg
.u
.channel
.tracefile_count
, 0,
482 msg
.u
.channel
.monitor
,
483 msg
.u
.channel
.live_timer_interval
,
485 if (new_channel
== NULL
) {
486 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
489 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
490 switch (msg
.u
.channel
.output
) {
491 case LTTNG_EVENT_SPLICE
:
492 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
494 case LTTNG_EVENT_MMAP
:
495 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
498 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
502 /* Translate and save channel type. */
503 switch (msg
.u
.channel
.type
) {
504 case CONSUMER_CHANNEL_TYPE_DATA
:
505 case CONSUMER_CHANNEL_TYPE_METADATA
:
506 new_channel
->type
= msg
.u
.channel
.type
;
513 health_code_update();
515 if (ctx
->on_recv_channel
!= NULL
) {
516 ret_recv
= ctx
->on_recv_channel(new_channel
);
518 ret
= consumer_add_channel(new_channel
, ctx
);
519 } else if (ret_recv
< 0) {
523 ret
= consumer_add_channel(new_channel
, ctx
);
525 if (CONSUMER_CHANNEL_TYPE_DATA
) {
526 consumer_timer_live_start(new_channel
,
527 msg
.u
.channel
.live_timer_interval
);
530 health_code_update();
532 /* If we received an error in add_channel, we need to report it. */
534 ret
= consumer_send_status_msg(sock
, ret
);
543 case LTTNG_CONSUMER_ADD_STREAM
:
546 struct lttng_pipe
*stream_pipe
;
547 struct lttng_consumer_stream
*new_stream
;
548 struct lttng_consumer_channel
*channel
;
552 * Get stream's channel reference. Needed when adding the stream to the
555 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
558 * We could not find the channel. Can happen if cpu hotplug
559 * happens while tearing down.
561 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
562 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
565 health_code_update();
567 /* First send a status message before receiving the fds. */
568 ret
= consumer_send_status_msg(sock
, ret_code
);
570 /* Somehow, the session daemon is not responding anymore. */
574 health_code_update();
576 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
577 /* Channel was not found. */
583 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
589 health_code_update();
591 /* Get stream file descriptor from socket */
592 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
593 if (ret
!= sizeof(fd
)) {
594 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
599 health_code_update();
602 * Send status code to session daemon only if the recv works. If the
603 * above recv() failed, the session daemon is notified through the
604 * error socket and the teardown is eventually done.
606 ret
= consumer_send_status_msg(sock
, ret_code
);
608 /* Somehow, the session daemon is not responding anymore. */
612 health_code_update();
614 new_stream
= consumer_allocate_stream(channel
->key
,
616 LTTNG_CONSUMER_ACTIVE_STREAM
,
626 if (new_stream
== NULL
) {
631 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
637 new_stream
->chan
= channel
;
638 new_stream
->wait_fd
= fd
;
639 switch (channel
->output
) {
640 case CONSUMER_CHANNEL_SPLICE
:
641 new_stream
->output
= LTTNG_EVENT_SPLICE
;
642 ret
= utils_create_pipe(new_stream
->splice_pipe
);
647 case CONSUMER_CHANNEL_MMAP
:
648 new_stream
->output
= LTTNG_EVENT_MMAP
;
651 ERR("Stream output unknown %d", channel
->output
);
656 * We've just assigned the channel to the stream so increment the
657 * refcount right now. We don't need to increment the refcount for
658 * streams in no monitor because we handle manually the cleanup of
659 * those. It is very important to make sure there is NO prior
660 * consumer_del_stream() calls or else the refcount will be unbalanced.
662 if (channel
->monitor
) {
663 uatomic_inc(&new_stream
->chan
->refcount
);
667 * The buffer flush is done on the session daemon side for the kernel
668 * so no need for the stream "hangup_flush_done" variable to be
669 * tracked. This is important for a kernel stream since we don't rely
670 * on the flush state of the stream to read data. It's not the case for
671 * user space tracing.
673 new_stream
->hangup_flush_done
= 0;
675 health_code_update();
677 if (ctx
->on_recv_stream
) {
678 ret
= ctx
->on_recv_stream(new_stream
);
680 consumer_stream_free(new_stream
);
685 health_code_update();
687 if (new_stream
->metadata_flag
) {
688 channel
->metadata_stream
= new_stream
;
691 /* Do not monitor this stream. */
692 if (!channel
->monitor
) {
693 DBG("Kernel consumer add stream %s in no monitor mode with "
694 "relayd id %" PRIu64
, new_stream
->name
,
695 new_stream
->net_seq_idx
);
696 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
700 /* Send stream to relayd if the stream has an ID. */
701 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
702 ret
= consumer_send_relayd_stream(new_stream
,
703 new_stream
->chan
->pathname
);
705 consumer_stream_free(new_stream
);
710 /* Get the right pipe where the stream will be sent. */
711 if (new_stream
->metadata_flag
) {
712 ret
= consumer_add_metadata_stream(new_stream
);
714 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
716 consumer_stream_free(new_stream
);
719 stream_pipe
= ctx
->consumer_metadata_pipe
;
721 ret
= consumer_add_data_stream(new_stream
);
723 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
725 consumer_stream_free(new_stream
);
728 stream_pipe
= ctx
->consumer_data_pipe
;
731 /* Vitible to other threads */
732 new_stream
->globally_visible
= 1;
734 health_code_update();
736 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
738 ERR("Consumer write %s stream to pipe %d",
739 new_stream
->metadata_flag
? "metadata" : "data",
740 lttng_pipe_get_writefd(stream_pipe
));
741 if (new_stream
->metadata_flag
) {
742 consumer_del_stream_for_metadata(new_stream
);
744 consumer_del_stream_for_data(new_stream
);
749 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
750 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
753 case LTTNG_CONSUMER_STREAMS_SENT
:
755 struct lttng_consumer_channel
*channel
;
758 * Get stream's channel reference. Needed when adding the stream to the
761 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
764 * We could not find the channel. Can happen if cpu hotplug
765 * happens while tearing down.
767 ERR("Unable to find channel key %" PRIu64
,
768 msg
.u
.sent_streams
.channel_key
);
769 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
772 health_code_update();
775 * Send status code to session daemon.
777 ret
= consumer_send_status_msg(sock
, ret_code
);
778 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
779 /* Somehow, the session daemon is not responding anymore. */
783 health_code_update();
786 * We should not send this message if we don't monitor the
787 * streams in this channel.
789 if (!channel
->monitor
) {
793 health_code_update();
794 /* Send stream to relayd if the stream has an ID. */
795 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
796 ret
= consumer_send_relayd_streams_sent(
797 msg
.u
.sent_streams
.net_seq_idx
);
804 case LTTNG_CONSUMER_UPDATE_STREAM
:
809 case LTTNG_CONSUMER_DESTROY_RELAYD
:
811 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
812 struct consumer_relayd_sock_pair
*relayd
;
814 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
816 /* Get relayd reference if exists. */
817 relayd
= consumer_find_relayd(index
);
818 if (relayd
== NULL
) {
819 DBG("Unable to find relayd %" PRIu64
, index
);
820 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
824 * Each relayd socket pair has a refcount of stream attached to it
825 * which tells if the relayd is still active or not depending on the
828 * This will set the destroy flag of the relayd object and destroy it
829 * if the refcount reaches zero when called.
831 * The destroy can happen either here or when a stream fd hangs up.
834 consumer_flag_relayd_for_destroy(relayd
);
837 health_code_update();
839 ret
= consumer_send_status_msg(sock
, ret_code
);
841 /* Somehow, the session daemon is not responding anymore. */
847 case LTTNG_CONSUMER_DATA_PENDING
:
850 uint64_t id
= msg
.u
.data_pending
.session_id
;
852 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
854 ret
= consumer_data_pending(id
);
856 health_code_update();
858 /* Send back returned value to session daemon */
859 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
861 PERROR("send data pending ret code");
866 * No need to send back a status message since the data pending
867 * returned value is the response.
871 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
873 if (msg
.u
.snapshot_channel
.metadata
== 1) {
874 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
875 msg
.u
.snapshot_channel
.pathname
,
876 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
878 ERR("Snapshot metadata failed");
879 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
882 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
883 msg
.u
.snapshot_channel
.pathname
,
884 msg
.u
.snapshot_channel
.relayd_id
,
885 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
888 ERR("Snapshot channel failed");
889 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
893 health_code_update();
895 ret
= consumer_send_status_msg(sock
, ret_code
);
897 /* Somehow, the session daemon is not responding anymore. */
902 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
904 uint64_t key
= msg
.u
.destroy_channel
.key
;
905 struct lttng_consumer_channel
*channel
;
907 channel
= consumer_find_channel(key
);
909 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
910 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
913 health_code_update();
915 ret
= consumer_send_status_msg(sock
, ret_code
);
917 /* Somehow, the session daemon is not responding anymore. */
921 health_code_update();
923 /* Stop right now if no channel was found. */
929 * This command should ONLY be issued for channel with streams set in
932 assert(!channel
->monitor
);
935 * The refcount should ALWAYS be 0 in the case of a channel in no
938 assert(!uatomic_sub_return(&channel
->refcount
, 1));
940 consumer_del_channel(channel
);
952 * Return 1 to indicate success since the 0 value can be a socket
953 * shutdown during the recv() or send() call.
955 health_code_update();
960 /* This will issue a consumer stop. */
965 * Populate index values of a kernel stream. Values are set in big endian order.
967 * Return 0 on success or else a negative value.
969 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
973 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
975 PERROR("kernctl_get_timestamp_begin");
978 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
980 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
982 PERROR("kernctl_get_timestamp_end");
985 index
->timestamp_end
= htobe64(index
->timestamp_end
);
987 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
989 PERROR("kernctl_get_events_discarded");
992 index
->events_discarded
= htobe64(index
->events_discarded
);
994 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
996 PERROR("kernctl_get_content_size");
999 index
->content_size
= htobe64(index
->content_size
);
1001 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1003 PERROR("kernctl_get_packet_size");
1006 index
->packet_size
= htobe64(index
->packet_size
);
1008 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1010 PERROR("kernctl_get_stream_id");
1013 index
->stream_id
= htobe64(index
->stream_id
);
1019 * Sync metadata meaning request them to the session daemon and snapshot to the
1020 * metadata thread can consumer them.
1022 * Metadata stream lock MUST be acquired.
1024 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1025 * is empty or a negative value on error.
1027 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1033 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1035 ERR("Failed to flush kernel stream");
1039 ret
= kernctl_snapshot(metadata
->wait_fd
);
1041 if (errno
!= EAGAIN
) {
1042 ERR("Sync metadata, taking kernel snapshot failed.");
1045 DBG("Sync metadata, no new kernel metadata");
1046 /* No new metadata, exit. */
1056 * Consume data on a file descriptor and write it on a trace file.
1058 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1059 struct lttng_consumer_local_data
*ctx
)
1061 unsigned long len
, subbuf_size
, padding
;
1062 int err
, write_index
= 1;
1064 int infd
= stream
->wait_fd
;
1065 struct ctf_packet_index index
;
1067 DBG("In read_subbuffer (infd : %d)", infd
);
1069 /* Get the next subbuffer */
1070 err
= kernctl_get_next_subbuf(infd
);
1073 * This is a debug message even for single-threaded consumer,
1074 * because poll() have more relaxed criterions than get subbuf,
1075 * so get_subbuf may fail for short race windows where poll()
1076 * would issue wakeups.
1078 DBG("Reserving sub buffer failed (everything is normal, "
1079 "it is due to concurrency)");
1084 /* Get the full subbuffer size including padding */
1085 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1087 PERROR("Getting sub-buffer len failed.");
1088 err
= kernctl_put_subbuf(infd
);
1090 if (errno
== EFAULT
) {
1091 PERROR("Error in unreserving sub buffer\n");
1092 } else if (errno
== EIO
) {
1093 /* Should never happen with newer LTTng versions */
1094 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1103 if (!stream
->metadata_flag
) {
1104 ret
= get_index_values(&index
, infd
);
1106 err
= kernctl_put_subbuf(infd
);
1108 if (errno
== EFAULT
) {
1109 PERROR("Error in unreserving sub buffer\n");
1110 } else if (errno
== EIO
) {
1111 /* Should never happen with newer LTTng versions */
1112 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1123 switch (stream
->chan
->output
) {
1124 case CONSUMER_CHANNEL_SPLICE
:
1126 * XXX: The lttng-modules splice "actor" does not handle copying
1127 * partial pages hence only using the subbuffer size without the
1128 * padding makes the splice fail.
1133 /* splice the subbuffer to the tracefile */
1134 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1137 * XXX: Splice does not support network streaming so the return value
1138 * is simply checked against subbuf_size and not like the mmap() op.
1140 if (ret
!= subbuf_size
) {
1142 * display the error but continue processing to try
1143 * to release the subbuffer
1145 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1150 case CONSUMER_CHANNEL_MMAP
:
1151 /* Get subbuffer size without padding */
1152 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1154 PERROR("Getting sub-buffer len failed.");
1155 err
= kernctl_put_subbuf(infd
);
1157 if (errno
== EFAULT
) {
1158 PERROR("Error in unreserving sub buffer\n");
1159 } else if (errno
== EIO
) {
1160 /* Should never happen with newer LTTng versions */
1161 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1170 /* Make sure the tracer is not gone mad on us! */
1171 assert(len
>= subbuf_size
);
1173 padding
= len
- subbuf_size
;
1175 /* write the subbuffer to the tracefile */
1176 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1179 * The mmap operation should write subbuf_size amount of data when
1180 * network streaming or the full padding (len) size when we are _not_
1183 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1184 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1186 * Display the error but continue processing to try to release the
1187 * subbuffer. This is a DBG statement since this is possible to
1188 * happen without being a critical error.
1190 DBG("Error writing to tracefile "
1191 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1192 ret
, len
, subbuf_size
);
1197 ERR("Unknown output method");
1201 err
= kernctl_put_next_subbuf(infd
);
1203 if (errno
== EFAULT
) {
1204 PERROR("Error in unreserving sub buffer\n");
1205 } else if (errno
== EIO
) {
1206 /* Should never happen with newer LTTng versions */
1207 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1213 /* Write index if needed. */
1218 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1220 * In live, block until all the metadata is sent.
1222 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1223 assert(!stream
->missed_metadata_flush
);
1224 stream
->waiting_on_metadata
= true;
1225 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1227 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1229 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1230 stream
->waiting_on_metadata
= false;
1231 if (stream
->missed_metadata_flush
) {
1232 stream
->missed_metadata_flush
= false;
1233 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1234 (void) consumer_flush_kernel_index(stream
);
1236 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1243 err
= consumer_stream_write_index(stream
, &index
);
1252 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1259 * Don't create anything if this is set for streaming or should not be
1262 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1263 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1264 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1265 stream
->uid
, stream
->gid
, NULL
);
1269 stream
->out_fd
= ret
;
1270 stream
->tracefile_size_current
= 0;
1272 if (!stream
->metadata_flag
) {
1273 ret
= index_create_file(stream
->chan
->pathname
,
1274 stream
->name
, stream
->uid
, stream
->gid
,
1275 stream
->chan
->tracefile_size
,
1276 stream
->tracefile_count_current
);
1280 stream
->index_fd
= ret
;
1284 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1285 /* get the len of the mmap region */
1286 unsigned long mmap_len
;
1288 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1290 PERROR("kernctl_get_mmap_len");
1292 goto error_close_fd
;
1294 stream
->mmap_len
= (size_t) mmap_len
;
1296 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1297 MAP_PRIVATE
, stream
->wait_fd
, 0);
1298 if (stream
->mmap_base
== MAP_FAILED
) {
1299 PERROR("Error mmaping");
1301 goto error_close_fd
;
1305 /* we return 0 to let the library handle the FD internally */
1309 if (stream
->out_fd
>= 0) {
1312 err
= close(stream
->out_fd
);
1314 stream
->out_fd
= -1;
1321 * Check if data is still being extracted from the buffers for a specific
1322 * stream. Consumer data lock MUST be acquired before calling this function
1323 * and the stream lock.
1325 * Return 1 if the traced data are still getting read else 0 meaning that the
1326 * data is available for trace viewer reading.
1328 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1334 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1339 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1341 /* There is still data so let's put back this subbuffer. */
1342 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1344 ret
= 1; /* Data is pending */
1348 /* Data is NOT pending and ready to be read. */