2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 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/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer/consumer-timer.h>
47 #include "kernel-consumer.h"
49 extern struct lttng_consumer_global_data consumer_data
;
50 extern int consumer_poll_timeout
;
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.");
71 * Sample consumed and produced positions for a specific fd.
73 * Returns 0 on success, < 0 on error.
75 int lttng_kconsumer_sample_snapshot_positions(
76 struct lttng_consumer_stream
*stream
)
80 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
84 * Get the produced position
86 * Returns 0 on success, < 0 on error
88 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
92 int infd
= stream
->wait_fd
;
94 ret
= kernctl_snapshot_get_produced(infd
, pos
);
96 PERROR("kernctl_snapshot_get_produced");
103 * Get the consumerd position
105 * Returns 0 on success, < 0 on error
107 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
111 int infd
= stream
->wait_fd
;
113 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
115 PERROR("kernctl_snapshot_get_consumed");
122 * Take a snapshot of all the stream of a channel
124 * Returns 0 on success, < 0 on error
126 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
127 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
128 struct lttng_consumer_local_data
*ctx
)
131 struct lttng_consumer_channel
*channel
;
132 struct lttng_consumer_stream
*stream
;
134 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
138 channel
= consumer_find_channel(key
);
140 ERR("No channel found for key %" PRIu64
, key
);
145 /* Splice is not supported yet for channel snapshot. */
146 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
147 ERR("Unsupported output %d", channel
->output
);
152 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
153 /* Are we at a position _before_ the first available packet ? */
154 bool before_first_packet
= true;
155 unsigned long consumed_pos
, produced_pos
;
157 health_code_update();
160 * Lock stream because we are about to change its state.
162 pthread_mutex_lock(&stream
->lock
);
165 * Assign the received relayd ID so we can use it for streaming. The streams
166 * are not visible to anyone so this is OK to change it.
168 stream
->net_seq_idx
= relayd_id
;
169 channel
->relayd_id
= relayd_id
;
170 if (relayd_id
!= (uint64_t) -1ULL) {
171 ret
= consumer_send_relayd_stream(stream
, path
);
173 ERR("sending stream to relayd");
177 ret
= utils_create_stream_file(path
, stream
->name
,
178 stream
->chan
->tracefile_size
,
179 stream
->tracefile_count_current
,
180 stream
->uid
, stream
->gid
, NULL
);
182 ERR("utils_create_stream_file");
186 stream
->out_fd
= ret
;
187 stream
->tracefile_size_current
= 0;
189 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
190 path
, stream
->name
, stream
->key
);
192 if (relayd_id
!= -1ULL) {
193 ret
= consumer_send_relayd_streams_sent(relayd_id
);
195 ERR("sending streams sent to relayd");
198 channel
->streams_sent_to_relayd
= true;
201 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
204 * Doing a buffer flush which does not take into
205 * account empty packets. This is not perfect
206 * for stream intersection, but required as a
207 * fall-back when "flush_empty" is not
208 * implemented by lttng-modules.
210 ret
= kernctl_buffer_flush(stream
->wait_fd
);
212 ERR("Failed to flush kernel stream");
218 ret
= lttng_kconsumer_take_snapshot(stream
);
220 ERR("Taking kernel snapshot");
224 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
226 ERR("Produced kernel snapshot position");
230 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
232 ERR("Consumerd kernel snapshot position");
236 if (stream
->max_sb_size
== 0) {
237 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
238 &stream
->max_sb_size
);
240 ERR("Getting kernel max_sb_size");
245 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
246 produced_pos
, nb_packets_per_stream
,
247 stream
->max_sb_size
);
249 while (consumed_pos
< produced_pos
) {
251 unsigned long len
, padded_len
;
254 health_code_update();
256 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
258 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
260 if (ret
!= -EAGAIN
) {
261 PERROR("kernctl_get_subbuf snapshot");
264 DBG("Kernel consumer get subbuf failed. Skipping it.");
265 consumed_pos
+= stream
->max_sb_size
;
268 * Start accounting lost packets only when we
269 * already have extracted packets (to match the
270 * content of the final snapshot).
272 if (!before_first_packet
) {
278 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
280 ERR("Snapshot kernctl_get_subbuf_size");
281 goto error_put_subbuf
;
284 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
286 ERR("Snapshot kernctl_get_padded_subbuf_size");
287 goto error_put_subbuf
;
290 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
291 padded_len
- len
, NULL
);
293 * We write the padded len in local tracefiles but the data len
294 * when using a relay. Display the error but continue processing
295 * to try to release the subbuffer.
297 if (relayd_id
!= (uint64_t) -1ULL) {
298 if (read_len
!= len
) {
299 ERR("Error sending to the relay (ret: %zd != len: %lu)",
303 if (read_len
!= padded_len
) {
304 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
305 read_len
, padded_len
);
309 ret
= kernctl_put_subbuf(stream
->wait_fd
);
311 ERR("Snapshot kernctl_put_subbuf");
314 consumed_pos
+= stream
->max_sb_size
;
317 * Only account lost packets located between
318 * succesfully extracted packets (do not account before
319 * and after since they are not visible in the
320 * resulting snapshot).
322 stream
->chan
->lost_packets
+= lost_packet
;
324 before_first_packet
= false;
327 if (relayd_id
== (uint64_t) -1ULL) {
328 if (stream
->out_fd
>= 0) {
329 ret
= close(stream
->out_fd
);
331 PERROR("Kernel consumer snapshot close out_fd");
337 close_relayd_stream(stream
);
338 stream
->net_seq_idx
= (uint64_t) -1ULL;
340 pthread_mutex_unlock(&stream
->lock
);
348 ret
= kernctl_put_subbuf(stream
->wait_fd
);
350 ERR("Snapshot kernctl_put_subbuf error path");
353 pthread_mutex_unlock(&stream
->lock
);
360 * Read the whole metadata available for a snapshot.
362 * Returns 0 on success, < 0 on error
364 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
365 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
367 int ret
, use_relayd
= 0;
369 struct lttng_consumer_channel
*metadata_channel
;
370 struct lttng_consumer_stream
*metadata_stream
;
374 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
379 metadata_channel
= consumer_find_channel(key
);
380 if (!metadata_channel
) {
381 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
386 metadata_stream
= metadata_channel
->metadata_stream
;
387 assert(metadata_stream
);
389 /* Flag once that we have a valid relayd for the stream. */
390 if (relayd_id
!= (uint64_t) -1ULL) {
395 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
400 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
401 metadata_stream
->chan
->tracefile_size
,
402 metadata_stream
->tracefile_count_current
,
403 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
407 metadata_stream
->out_fd
= ret
;
411 health_code_update();
413 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
415 if (ret_read
!= -EAGAIN
) {
416 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
420 /* ret_read is negative at this point so we will exit the loop. */
423 } while (ret_read
>= 0);
426 close_relayd_stream(metadata_stream
);
427 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
429 if (metadata_stream
->out_fd
>= 0) {
430 ret
= close(metadata_stream
->out_fd
);
432 PERROR("Kernel consumer snapshot metadata close out_fd");
434 * Don't go on error here since the snapshot was successful at this
435 * point but somehow the close failed.
438 metadata_stream
->out_fd
= -1;
444 cds_list_del(&metadata_stream
->send_node
);
445 consumer_stream_destroy(metadata_stream
, NULL
);
446 metadata_channel
->metadata_stream
= NULL
;
453 * Receive command from session daemon and process it.
455 * Return 1 on success else a negative value or 0.
457 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
458 int sock
, struct pollfd
*consumer_sockpoll
)
461 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
462 struct lttcomm_consumer_msg msg
;
464 health_code_update();
466 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
467 if (ret
!= sizeof(msg
)) {
469 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
475 health_code_update();
477 /* Deprecated command */
478 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
480 health_code_update();
482 /* relayd needs RCU read-side protection */
485 switch (msg
.cmd_type
) {
486 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
488 /* Session daemon status message are handled in the following call. */
489 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
490 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
491 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
492 msg
.u
.relayd_sock
.relayd_session_id
);
495 case LTTNG_CONSUMER_ADD_CHANNEL
:
497 struct lttng_consumer_channel
*new_channel
;
500 health_code_update();
502 /* First send a status message before receiving the fds. */
503 ret
= consumer_send_status_msg(sock
, ret_code
);
505 /* Somehow, the session daemon is not responding anymore. */
509 health_code_update();
511 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
512 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
513 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
514 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
515 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
516 msg
.u
.channel
.tracefile_size
,
517 msg
.u
.channel
.tracefile_count
, 0,
518 msg
.u
.channel
.monitor
,
519 msg
.u
.channel
.live_timer_interval
,
521 if (new_channel
== NULL
) {
522 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
525 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
526 switch (msg
.u
.channel
.output
) {
527 case LTTNG_EVENT_SPLICE
:
528 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
530 case LTTNG_EVENT_MMAP
:
531 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
534 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
538 /* Translate and save channel type. */
539 switch (msg
.u
.channel
.type
) {
540 case CONSUMER_CHANNEL_TYPE_DATA
:
541 case CONSUMER_CHANNEL_TYPE_METADATA
:
542 new_channel
->type
= msg
.u
.channel
.type
;
549 health_code_update();
551 if (ctx
->on_recv_channel
!= NULL
) {
552 ret_recv
= ctx
->on_recv_channel(new_channel
);
554 ret
= consumer_add_channel(new_channel
, ctx
);
555 } else if (ret_recv
< 0) {
559 ret
= consumer_add_channel(new_channel
, ctx
);
561 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
562 int monitor_start_ret
;
564 DBG("Consumer starting monitor timer");
565 consumer_timer_live_start(new_channel
,
566 msg
.u
.channel
.live_timer_interval
);
567 monitor_start_ret
= consumer_timer_monitor_start(
569 msg
.u
.channel
.monitor_timer_interval
);
570 if (monitor_start_ret
< 0) {
571 ERR("Starting channel monitoring timer failed");
577 health_code_update();
579 /* If we received an error in add_channel, we need to report it. */
581 ret
= consumer_send_status_msg(sock
, ret
);
590 case LTTNG_CONSUMER_ADD_STREAM
:
593 struct lttng_pipe
*stream_pipe
;
594 struct lttng_consumer_stream
*new_stream
;
595 struct lttng_consumer_channel
*channel
;
599 * Get stream's channel reference. Needed when adding the stream to the
602 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
605 * We could not find the channel. Can happen if cpu hotplug
606 * happens while tearing down.
608 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
609 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
612 health_code_update();
614 /* First send a status message before receiving the fds. */
615 ret
= consumer_send_status_msg(sock
, ret_code
);
617 /* Somehow, the session daemon is not responding anymore. */
621 health_code_update();
623 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
624 /* Channel was not found. */
630 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
636 health_code_update();
638 /* Get stream file descriptor from socket */
639 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
640 if (ret
!= sizeof(fd
)) {
641 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
646 health_code_update();
649 * Send status code to session daemon only if the recv works. If the
650 * above recv() failed, the session daemon is notified through the
651 * error socket and the teardown is eventually done.
653 ret
= consumer_send_status_msg(sock
, ret_code
);
655 /* Somehow, the session daemon is not responding anymore. */
659 health_code_update();
661 new_stream
= consumer_allocate_stream(channel
->key
,
663 LTTNG_CONSUMER_ACTIVE_STREAM
,
673 if (new_stream
== NULL
) {
678 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
684 new_stream
->chan
= channel
;
685 new_stream
->wait_fd
= fd
;
686 switch (channel
->output
) {
687 case CONSUMER_CHANNEL_SPLICE
:
688 new_stream
->output
= LTTNG_EVENT_SPLICE
;
689 ret
= utils_create_pipe(new_stream
->splice_pipe
);
694 case CONSUMER_CHANNEL_MMAP
:
695 new_stream
->output
= LTTNG_EVENT_MMAP
;
698 ERR("Stream output unknown %d", channel
->output
);
703 * We've just assigned the channel to the stream so increment the
704 * refcount right now. We don't need to increment the refcount for
705 * streams in no monitor because we handle manually the cleanup of
706 * those. It is very important to make sure there is NO prior
707 * consumer_del_stream() calls or else the refcount will be unbalanced.
709 if (channel
->monitor
) {
710 uatomic_inc(&new_stream
->chan
->refcount
);
714 * The buffer flush is done on the session daemon side for the kernel
715 * so no need for the stream "hangup_flush_done" variable to be
716 * tracked. This is important for a kernel stream since we don't rely
717 * on the flush state of the stream to read data. It's not the case for
718 * user space tracing.
720 new_stream
->hangup_flush_done
= 0;
722 health_code_update();
724 if (ctx
->on_recv_stream
) {
725 ret
= ctx
->on_recv_stream(new_stream
);
727 consumer_stream_free(new_stream
);
732 health_code_update();
734 if (new_stream
->metadata_flag
) {
735 channel
->metadata_stream
= new_stream
;
738 /* Do not monitor this stream. */
739 if (!channel
->monitor
) {
740 DBG("Kernel consumer add stream %s in no monitor mode with "
741 "relayd id %" PRIu64
, new_stream
->name
,
742 new_stream
->net_seq_idx
);
743 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
747 /* Send stream to relayd if the stream has an ID. */
748 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
749 ret
= consumer_send_relayd_stream(new_stream
,
750 new_stream
->chan
->pathname
);
752 consumer_stream_free(new_stream
);
757 * If adding an extra stream to an already
758 * existing channel (e.g. cpu hotplug), we need
759 * to send the "streams_sent" command to relayd.
761 if (channel
->streams_sent_to_relayd
) {
762 ret
= consumer_send_relayd_streams_sent(
763 new_stream
->net_seq_idx
);
770 /* Get the right pipe where the stream will be sent. */
771 if (new_stream
->metadata_flag
) {
772 ret
= consumer_add_metadata_stream(new_stream
);
774 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
776 consumer_stream_free(new_stream
);
779 stream_pipe
= ctx
->consumer_metadata_pipe
;
781 ret
= consumer_add_data_stream(new_stream
);
783 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
785 consumer_stream_free(new_stream
);
788 stream_pipe
= ctx
->consumer_data_pipe
;
791 /* Vitible to other threads */
792 new_stream
->globally_visible
= 1;
794 health_code_update();
796 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
798 ERR("Consumer write %s stream to pipe %d",
799 new_stream
->metadata_flag
? "metadata" : "data",
800 lttng_pipe_get_writefd(stream_pipe
));
801 if (new_stream
->metadata_flag
) {
802 consumer_del_stream_for_metadata(new_stream
);
804 consumer_del_stream_for_data(new_stream
);
809 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
810 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
813 case LTTNG_CONSUMER_STREAMS_SENT
:
815 struct lttng_consumer_channel
*channel
;
818 * Get stream's channel reference. Needed when adding the stream to the
821 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
824 * We could not find the channel. Can happen if cpu hotplug
825 * happens while tearing down.
827 ERR("Unable to find channel key %" PRIu64
,
828 msg
.u
.sent_streams
.channel_key
);
829 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
832 health_code_update();
835 * Send status code to session daemon.
837 ret
= consumer_send_status_msg(sock
, ret_code
);
838 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
839 /* Somehow, the session daemon is not responding anymore. */
843 health_code_update();
846 * We should not send this message if we don't monitor the
847 * streams in this channel.
849 if (!channel
->monitor
) {
853 health_code_update();
854 /* Send stream to relayd if the stream has an ID. */
855 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
856 ret
= consumer_send_relayd_streams_sent(
857 msg
.u
.sent_streams
.net_seq_idx
);
861 channel
->streams_sent_to_relayd
= true;
865 case LTTNG_CONSUMER_UPDATE_STREAM
:
870 case LTTNG_CONSUMER_DESTROY_RELAYD
:
872 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
873 struct consumer_relayd_sock_pair
*relayd
;
875 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
877 /* Get relayd reference if exists. */
878 relayd
= consumer_find_relayd(index
);
879 if (relayd
== NULL
) {
880 DBG("Unable to find relayd %" PRIu64
, index
);
881 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
885 * Each relayd socket pair has a refcount of stream attached to it
886 * which tells if the relayd is still active or not depending on the
889 * This will set the destroy flag of the relayd object and destroy it
890 * if the refcount reaches zero when called.
892 * The destroy can happen either here or when a stream fd hangs up.
895 consumer_flag_relayd_for_destroy(relayd
);
898 health_code_update();
900 ret
= consumer_send_status_msg(sock
, ret_code
);
902 /* Somehow, the session daemon is not responding anymore. */
908 case LTTNG_CONSUMER_DATA_PENDING
:
911 uint64_t id
= msg
.u
.data_pending
.session_id
;
913 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
915 ret
= consumer_data_pending(id
);
917 health_code_update();
919 /* Send back returned value to session daemon */
920 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
922 PERROR("send data pending ret code");
927 * No need to send back a status message since the data pending
928 * returned value is the response.
932 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
934 if (msg
.u
.snapshot_channel
.metadata
== 1) {
935 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
936 msg
.u
.snapshot_channel
.pathname
,
937 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
939 ERR("Snapshot metadata failed");
940 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
943 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
944 msg
.u
.snapshot_channel
.pathname
,
945 msg
.u
.snapshot_channel
.relayd_id
,
946 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
949 ERR("Snapshot channel failed");
950 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
954 health_code_update();
956 ret
= consumer_send_status_msg(sock
, ret_code
);
958 /* Somehow, the session daemon is not responding anymore. */
963 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
965 uint64_t key
= msg
.u
.destroy_channel
.key
;
966 struct lttng_consumer_channel
*channel
;
968 channel
= consumer_find_channel(key
);
970 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
971 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
974 health_code_update();
976 ret
= consumer_send_status_msg(sock
, ret_code
);
978 /* Somehow, the session daemon is not responding anymore. */
982 health_code_update();
984 /* Stop right now if no channel was found. */
990 * This command should ONLY be issued for channel with streams set in
993 assert(!channel
->monitor
);
996 * The refcount should ALWAYS be 0 in the case of a channel in no
999 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1001 consumer_del_channel(channel
);
1005 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1008 struct lttng_consumer_channel
*channel
;
1009 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1010 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1012 DBG("Kernel consumer discarded events command for session id %"
1013 PRIu64
", channel key %" PRIu64
, id
, key
);
1015 channel
= consumer_find_channel(key
);
1017 ERR("Kernel consumer discarded events channel %"
1018 PRIu64
" not found", key
);
1021 ret
= channel
->discarded_events
;
1024 health_code_update();
1026 /* Send back returned value to session daemon */
1027 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1029 PERROR("send discarded events");
1035 case LTTNG_CONSUMER_LOST_PACKETS
:
1038 struct lttng_consumer_channel
*channel
;
1039 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1040 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1042 DBG("Kernel consumer lost packets command for session id %"
1043 PRIu64
", channel key %" PRIu64
, id
, key
);
1045 channel
= consumer_find_channel(key
);
1047 ERR("Kernel consumer lost packets channel %"
1048 PRIu64
" not found", key
);
1051 ret
= channel
->lost_packets
;
1054 health_code_update();
1056 /* Send back returned value to session daemon */
1057 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1059 PERROR("send lost packets");
1065 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1067 int channel_monitor_pipe
;
1069 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1070 /* Successfully received the command's type. */
1071 ret
= consumer_send_status_msg(sock
, ret_code
);
1076 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1078 if (ret
!= sizeof(channel_monitor_pipe
)) {
1079 ERR("Failed to receive channel monitor pipe");
1083 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1084 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1085 channel_monitor_pipe
);
1089 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1090 /* Set the pipe as non-blocking. */
1091 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1093 PERROR("fcntl get flags of the channel monitoring pipe");
1098 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1099 flags
| O_NONBLOCK
);
1101 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1104 DBG("Channel monitor pipe set as non-blocking");
1106 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1108 ret
= consumer_send_status_msg(sock
, ret_code
);
1122 * Return 1 to indicate success since the 0 value can be a socket
1123 * shutdown during the recv() or send() call.
1125 health_code_update();
1130 /* This will issue a consumer stop. */
1135 * Populate index values of a kernel stream. Values are set in big endian order.
1137 * Return 0 on success or else a negative value.
1139 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1143 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1145 PERROR("kernctl_get_timestamp_begin");
1148 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1150 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1152 PERROR("kernctl_get_timestamp_end");
1155 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1157 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1159 PERROR("kernctl_get_events_discarded");
1162 index
->events_discarded
= htobe64(index
->events_discarded
);
1164 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1166 PERROR("kernctl_get_content_size");
1169 index
->content_size
= htobe64(index
->content_size
);
1171 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1173 PERROR("kernctl_get_packet_size");
1176 index
->packet_size
= htobe64(index
->packet_size
);
1178 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1180 PERROR("kernctl_get_stream_id");
1183 index
->stream_id
= htobe64(index
->stream_id
);
1185 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1187 if (ret
== -ENOTTY
) {
1188 /* Command not implemented by lttng-modules. */
1189 index
->stream_instance_id
= -1ULL;
1192 PERROR("kernctl_get_instance_id");
1196 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1198 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1200 if (ret
== -ENOTTY
) {
1201 /* Command not implemented by lttng-modules. */
1202 index
->packet_seq_num
= -1ULL;
1205 PERROR("kernctl_get_sequence_number");
1209 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1215 * Sync metadata meaning request them to the session daemon and snapshot to the
1216 * metadata thread can consumer them.
1218 * Metadata stream lock MUST be acquired.
1220 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1221 * is empty or a negative value on error.
1223 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1229 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1231 ERR("Failed to flush kernel stream");
1235 ret
= kernctl_snapshot(metadata
->wait_fd
);
1237 if (ret
!= -EAGAIN
) {
1238 ERR("Sync metadata, taking kernel snapshot failed.");
1241 DBG("Sync metadata, no new kernel metadata");
1242 /* No new metadata, exit. */
1252 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1255 uint64_t seq
, discarded
;
1257 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1259 if (ret
== -ENOTTY
) {
1260 /* Command not implemented by lttng-modules. */
1264 PERROR("kernctl_get_sequence_number");
1270 * Start the sequence when we extract the first packet in case we don't
1271 * start at 0 (for example if a consumer is not connected to the
1272 * session immediately after the beginning).
1274 if (stream
->last_sequence_number
== -1ULL) {
1275 stream
->last_sequence_number
= seq
;
1276 } else if (seq
> stream
->last_sequence_number
) {
1277 stream
->chan
->lost_packets
+= seq
-
1278 stream
->last_sequence_number
- 1;
1280 /* seq <= last_sequence_number */
1281 ERR("Sequence number inconsistent : prev = %" PRIu64
1282 ", current = %" PRIu64
,
1283 stream
->last_sequence_number
, seq
);
1287 stream
->last_sequence_number
= seq
;
1289 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1291 PERROR("kernctl_get_events_discarded");
1294 if (discarded
< stream
->last_discarded_events
) {
1296 * Overflow has occurred. We assume only one wrap-around
1299 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1300 stream
->last_discarded_events
+ discarded
;
1302 stream
->chan
->discarded_events
+= discarded
-
1303 stream
->last_discarded_events
;
1305 stream
->last_discarded_events
= discarded
;
1313 * Check if the local version of the metadata stream matches with the version
1314 * of the metadata stream in the kernel. If it was updated, set the reset flag
1318 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1321 uint64_t cur_version
;
1323 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1325 if (ret
== -ENOTTY
) {
1327 * LTTng-modules does not implement this
1333 ERR("Failed to get the metadata version");
1337 if (stream
->metadata_version
== cur_version
) {
1342 DBG("New metadata version detected");
1343 stream
->metadata_version
= cur_version
;
1344 stream
->reset_metadata_flag
= 1;
1352 * Consume data on a file descriptor and write it on a trace file.
1354 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1355 struct lttng_consumer_local_data
*ctx
)
1357 unsigned long len
, subbuf_size
, padding
;
1358 int err
, write_index
= 1;
1360 int infd
= stream
->wait_fd
;
1361 struct ctf_packet_index index
;
1363 DBG("In read_subbuffer (infd : %d)", infd
);
1365 /* Get the next subbuffer */
1366 err
= kernctl_get_next_subbuf(infd
);
1369 * This is a debug message even for single-threaded consumer,
1370 * because poll() have more relaxed criterions than get subbuf,
1371 * so get_subbuf may fail for short race windows where poll()
1372 * would issue wakeups.
1374 DBG("Reserving sub buffer failed (everything is normal, "
1375 "it is due to concurrency)");
1380 /* Get the full subbuffer size including padding */
1381 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1383 PERROR("Getting sub-buffer len failed.");
1384 err
= kernctl_put_subbuf(infd
);
1386 if (err
== -EFAULT
) {
1387 PERROR("Error in unreserving sub buffer\n");
1388 } else if (err
== -EIO
) {
1389 /* Should never happen with newer LTTng versions */
1390 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1399 if (!stream
->metadata_flag
) {
1400 ret
= get_index_values(&index
, infd
);
1402 err
= kernctl_put_subbuf(infd
);
1404 if (err
== -EFAULT
) {
1405 PERROR("Error in unreserving sub buffer\n");
1406 } else if (err
== -EIO
) {
1407 /* Should never happen with newer LTTng versions */
1408 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1415 ret
= update_stream_stats(stream
);
1417 err
= kernctl_put_subbuf(infd
);
1419 if (err
== -EFAULT
) {
1420 PERROR("Error in unreserving sub buffer\n");
1421 } else if (err
== -EIO
) {
1422 /* Should never happen with newer LTTng versions */
1423 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1432 ret
= metadata_stream_check_version(infd
, stream
);
1434 err
= kernctl_put_subbuf(infd
);
1436 if (err
== -EFAULT
) {
1437 PERROR("Error in unreserving sub buffer\n");
1438 } else if (err
== -EIO
) {
1439 /* Should never happen with newer LTTng versions */
1440 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1449 switch (stream
->chan
->output
) {
1450 case CONSUMER_CHANNEL_SPLICE
:
1452 * XXX: The lttng-modules splice "actor" does not handle copying
1453 * partial pages hence only using the subbuffer size without the
1454 * padding makes the splice fail.
1459 /* splice the subbuffer to the tracefile */
1460 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1463 * XXX: Splice does not support network streaming so the return value
1464 * is simply checked against subbuf_size and not like the mmap() op.
1466 if (ret
!= subbuf_size
) {
1468 * display the error but continue processing to try
1469 * to release the subbuffer
1471 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1476 case CONSUMER_CHANNEL_MMAP
:
1477 /* Get subbuffer size without padding */
1478 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1480 PERROR("Getting sub-buffer len failed.");
1481 err
= kernctl_put_subbuf(infd
);
1483 if (err
== -EFAULT
) {
1484 PERROR("Error in unreserving sub buffer\n");
1485 } else if (err
== -EIO
) {
1486 /* Should never happen with newer LTTng versions */
1487 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1496 /* Make sure the tracer is not gone mad on us! */
1497 assert(len
>= subbuf_size
);
1499 padding
= len
- subbuf_size
;
1501 /* write the subbuffer to the tracefile */
1502 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1505 * The mmap operation should write subbuf_size amount of data when
1506 * network streaming or the full padding (len) size when we are _not_
1509 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1510 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1512 * Display the error but continue processing to try to release the
1513 * subbuffer. This is a DBG statement since this is possible to
1514 * happen without being a critical error.
1516 DBG("Error writing to tracefile "
1517 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1518 ret
, len
, subbuf_size
);
1523 ERR("Unknown output method");
1527 err
= kernctl_put_next_subbuf(infd
);
1529 if (err
== -EFAULT
) {
1530 PERROR("Error in unreserving sub buffer\n");
1531 } else if (err
== -EIO
) {
1532 /* Should never happen with newer LTTng versions */
1533 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1539 /* Write index if needed. */
1544 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1546 * In live, block until all the metadata is sent.
1548 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1549 assert(!stream
->missed_metadata_flush
);
1550 stream
->waiting_on_metadata
= true;
1551 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1553 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1555 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1556 stream
->waiting_on_metadata
= false;
1557 if (stream
->missed_metadata_flush
) {
1558 stream
->missed_metadata_flush
= false;
1559 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1560 (void) consumer_flush_kernel_index(stream
);
1562 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1569 err
= consumer_stream_write_index(stream
, &index
);
1578 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1585 * Don't create anything if this is set for streaming or should not be
1588 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1589 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1590 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1591 stream
->uid
, stream
->gid
, NULL
);
1595 stream
->out_fd
= ret
;
1596 stream
->tracefile_size_current
= 0;
1598 if (!stream
->metadata_flag
) {
1599 struct lttng_index_file
*index_file
;
1601 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1602 stream
->name
, stream
->uid
, stream
->gid
,
1603 stream
->chan
->tracefile_size
,
1604 stream
->tracefile_count_current
,
1605 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1609 assert(!stream
->index_file
);
1610 stream
->index_file
= index_file
;
1614 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1615 /* get the len of the mmap region */
1616 unsigned long mmap_len
;
1618 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1620 PERROR("kernctl_get_mmap_len");
1621 goto error_close_fd
;
1623 stream
->mmap_len
= (size_t) mmap_len
;
1625 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1626 MAP_PRIVATE
, stream
->wait_fd
, 0);
1627 if (stream
->mmap_base
== MAP_FAILED
) {
1628 PERROR("Error mmaping");
1630 goto error_close_fd
;
1634 /* we return 0 to let the library handle the FD internally */
1638 if (stream
->out_fd
>= 0) {
1641 err
= close(stream
->out_fd
);
1643 stream
->out_fd
= -1;
1650 * Check if data is still being extracted from the buffers for a specific
1651 * stream. Consumer data lock MUST be acquired before calling this function
1652 * and the stream lock.
1654 * Return 1 if the traced data are still getting read else 0 meaning that the
1655 * data is available for trace viewer reading.
1657 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1663 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1668 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1670 /* There is still data so let's put back this subbuffer. */
1671 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1673 ret
= 1; /* Data is pending */
1677 /* Data is NOT pending and ready to be read. */