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 unsigned long consumed_pos
, produced_pos
;
155 health_code_update();
158 * Lock stream because we are about to change its state.
160 pthread_mutex_lock(&stream
->lock
);
163 * Assign the received relayd ID so we can use it for streaming. The streams
164 * are not visible to anyone so this is OK to change it.
166 stream
->net_seq_idx
= relayd_id
;
167 channel
->relayd_id
= relayd_id
;
168 if (relayd_id
!= (uint64_t) -1ULL) {
169 ret
= consumer_send_relayd_stream(stream
, path
);
171 ERR("sending stream to relayd");
175 ret
= utils_create_stream_file(path
, stream
->name
,
176 stream
->chan
->tracefile_size
,
177 stream
->tracefile_count_current
,
178 stream
->uid
, stream
->gid
, NULL
);
180 ERR("utils_create_stream_file");
184 stream
->out_fd
= ret
;
185 stream
->tracefile_size_current
= 0;
187 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
188 path
, stream
->name
, stream
->key
);
191 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
194 * Doing a buffer flush which does not take into
195 * account empty packets. This is not perfect
196 * for stream intersection, but required as a
197 * fall-back when "flush_empty" is not
198 * implemented by lttng-modules.
200 ret
= kernctl_buffer_flush(stream
->wait_fd
);
202 ERR("Failed to flush kernel stream");
208 ret
= lttng_kconsumer_take_snapshot(stream
);
210 ERR("Taking kernel snapshot");
214 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
216 ERR("Produced kernel snapshot position");
220 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
222 ERR("Consumerd kernel snapshot position");
226 if (stream
->max_sb_size
== 0) {
227 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
228 &stream
->max_sb_size
);
230 ERR("Getting kernel max_sb_size");
235 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
236 produced_pos
, nb_packets_per_stream
,
237 stream
->max_sb_size
);
239 while (consumed_pos
< produced_pos
) {
241 unsigned long len
, padded_len
;
243 health_code_update();
245 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
247 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
249 if (ret
!= -EAGAIN
) {
250 PERROR("kernctl_get_subbuf snapshot");
253 DBG("Kernel consumer get subbuf failed. Skipping it.");
254 consumed_pos
+= stream
->max_sb_size
;
255 stream
->chan
->lost_packets
++;
259 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
261 ERR("Snapshot kernctl_get_subbuf_size");
262 goto error_put_subbuf
;
265 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
267 ERR("Snapshot kernctl_get_padded_subbuf_size");
268 goto error_put_subbuf
;
271 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
272 padded_len
- len
, NULL
);
274 * We write the padded len in local tracefiles but the data len
275 * when using a relay. Display the error but continue processing
276 * to try to release the subbuffer.
278 if (relayd_id
!= (uint64_t) -1ULL) {
279 if (read_len
!= len
) {
280 ERR("Error sending to the relay (ret: %zd != len: %lu)",
284 if (read_len
!= padded_len
) {
285 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
286 read_len
, padded_len
);
290 ret
= kernctl_put_subbuf(stream
->wait_fd
);
292 ERR("Snapshot kernctl_put_subbuf");
295 consumed_pos
+= stream
->max_sb_size
;
298 if (relayd_id
== (uint64_t) -1ULL) {
299 if (stream
->out_fd
>= 0) {
300 ret
= close(stream
->out_fd
);
302 PERROR("Kernel consumer snapshot close out_fd");
308 close_relayd_stream(stream
);
309 stream
->net_seq_idx
= (uint64_t) -1ULL;
311 pthread_mutex_unlock(&stream
->lock
);
319 ret
= kernctl_put_subbuf(stream
->wait_fd
);
321 ERR("Snapshot kernctl_put_subbuf error path");
324 pthread_mutex_unlock(&stream
->lock
);
331 * Read the whole metadata available for a snapshot.
333 * Returns 0 on success, < 0 on error
335 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
336 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
338 int ret
, use_relayd
= 0;
340 struct lttng_consumer_channel
*metadata_channel
;
341 struct lttng_consumer_stream
*metadata_stream
;
345 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
350 metadata_channel
= consumer_find_channel(key
);
351 if (!metadata_channel
) {
352 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
357 metadata_stream
= metadata_channel
->metadata_stream
;
358 assert(metadata_stream
);
360 /* Flag once that we have a valid relayd for the stream. */
361 if (relayd_id
!= (uint64_t) -1ULL) {
366 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
371 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
372 metadata_stream
->chan
->tracefile_size
,
373 metadata_stream
->tracefile_count_current
,
374 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
378 metadata_stream
->out_fd
= ret
;
382 health_code_update();
384 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
386 if (ret_read
!= -EAGAIN
) {
387 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
391 /* ret_read is negative at this point so we will exit the loop. */
394 } while (ret_read
>= 0);
397 close_relayd_stream(metadata_stream
);
398 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
400 if (metadata_stream
->out_fd
>= 0) {
401 ret
= close(metadata_stream
->out_fd
);
403 PERROR("Kernel consumer snapshot metadata close out_fd");
405 * Don't go on error here since the snapshot was successful at this
406 * point but somehow the close failed.
409 metadata_stream
->out_fd
= -1;
415 cds_list_del(&metadata_stream
->send_node
);
416 consumer_stream_destroy(metadata_stream
, NULL
);
417 metadata_channel
->metadata_stream
= NULL
;
424 * Receive command from session daemon and process it.
426 * Return 1 on success else a negative value or 0.
428 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
429 int sock
, struct pollfd
*consumer_sockpoll
)
432 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
433 struct lttcomm_consumer_msg msg
;
435 health_code_update();
437 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
438 if (ret
!= sizeof(msg
)) {
440 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
446 health_code_update();
448 /* Deprecated command */
449 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
451 health_code_update();
453 /* relayd needs RCU read-side protection */
456 switch (msg
.cmd_type
) {
457 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
459 /* Session daemon status message are handled in the following call. */
460 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
461 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
462 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
463 msg
.u
.relayd_sock
.relayd_session_id
);
466 case LTTNG_CONSUMER_ADD_CHANNEL
:
468 struct lttng_consumer_channel
*new_channel
;
471 health_code_update();
473 /* First send a status message before receiving the fds. */
474 ret
= consumer_send_status_msg(sock
, ret_code
);
476 /* Somehow, the session daemon is not responding anymore. */
480 health_code_update();
482 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
483 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
484 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
485 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
486 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
487 msg
.u
.channel
.tracefile_size
,
488 msg
.u
.channel
.tracefile_count
, 0,
489 msg
.u
.channel
.monitor
,
490 msg
.u
.channel
.live_timer_interval
,
492 if (new_channel
== NULL
) {
493 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
496 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
497 switch (msg
.u
.channel
.output
) {
498 case LTTNG_EVENT_SPLICE
:
499 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
501 case LTTNG_EVENT_MMAP
:
502 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
505 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
509 /* Translate and save channel type. */
510 switch (msg
.u
.channel
.type
) {
511 case CONSUMER_CHANNEL_TYPE_DATA
:
512 case CONSUMER_CHANNEL_TYPE_METADATA
:
513 new_channel
->type
= msg
.u
.channel
.type
;
520 health_code_update();
522 if (ctx
->on_recv_channel
!= NULL
) {
523 ret_recv
= ctx
->on_recv_channel(new_channel
);
525 ret
= consumer_add_channel(new_channel
, ctx
);
526 } else if (ret_recv
< 0) {
530 ret
= consumer_add_channel(new_channel
, ctx
);
532 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
533 int monitor_start_ret
;
535 DBG("Consumer starting monitor timer");
536 consumer_timer_live_start(new_channel
,
537 msg
.u
.channel
.live_timer_interval
);
538 monitor_start_ret
= consumer_timer_monitor_start(
540 msg
.u
.channel
.monitor_timer_interval
);
541 if (monitor_start_ret
< 0) {
542 ERR("Starting channel monitoring timer failed");
548 health_code_update();
550 /* If we received an error in add_channel, we need to report it. */
552 ret
= consumer_send_status_msg(sock
, ret
);
561 case LTTNG_CONSUMER_ADD_STREAM
:
564 struct lttng_pipe
*stream_pipe
;
565 struct lttng_consumer_stream
*new_stream
;
566 struct lttng_consumer_channel
*channel
;
570 * Get stream's channel reference. Needed when adding the stream to the
573 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
576 * We could not find the channel. Can happen if cpu hotplug
577 * happens while tearing down.
579 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
580 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
583 health_code_update();
585 /* First send a status message before receiving the fds. */
586 ret
= consumer_send_status_msg(sock
, ret_code
);
588 /* Somehow, the session daemon is not responding anymore. */
592 health_code_update();
594 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
595 /* Channel was not found. */
601 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
607 health_code_update();
609 /* Get stream file descriptor from socket */
610 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
611 if (ret
!= sizeof(fd
)) {
612 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
617 health_code_update();
620 * Send status code to session daemon only if the recv works. If the
621 * above recv() failed, the session daemon is notified through the
622 * error socket and the teardown is eventually done.
624 ret
= consumer_send_status_msg(sock
, ret_code
);
626 /* Somehow, the session daemon is not responding anymore. */
630 health_code_update();
632 new_stream
= consumer_allocate_stream(channel
->key
,
634 LTTNG_CONSUMER_ACTIVE_STREAM
,
644 if (new_stream
== NULL
) {
649 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
655 new_stream
->chan
= channel
;
656 new_stream
->wait_fd
= fd
;
657 switch (channel
->output
) {
658 case CONSUMER_CHANNEL_SPLICE
:
659 new_stream
->output
= LTTNG_EVENT_SPLICE
;
660 ret
= utils_create_pipe(new_stream
->splice_pipe
);
665 case CONSUMER_CHANNEL_MMAP
:
666 new_stream
->output
= LTTNG_EVENT_MMAP
;
669 ERR("Stream output unknown %d", channel
->output
);
674 * We've just assigned the channel to the stream so increment the
675 * refcount right now. We don't need to increment the refcount for
676 * streams in no monitor because we handle manually the cleanup of
677 * those. It is very important to make sure there is NO prior
678 * consumer_del_stream() calls or else the refcount will be unbalanced.
680 if (channel
->monitor
) {
681 uatomic_inc(&new_stream
->chan
->refcount
);
685 * The buffer flush is done on the session daemon side for the kernel
686 * so no need for the stream "hangup_flush_done" variable to be
687 * tracked. This is important for a kernel stream since we don't rely
688 * on the flush state of the stream to read data. It's not the case for
689 * user space tracing.
691 new_stream
->hangup_flush_done
= 0;
693 health_code_update();
695 if (ctx
->on_recv_stream
) {
696 ret
= ctx
->on_recv_stream(new_stream
);
698 consumer_stream_free(new_stream
);
703 health_code_update();
705 if (new_stream
->metadata_flag
) {
706 channel
->metadata_stream
= new_stream
;
709 /* Do not monitor this stream. */
710 if (!channel
->monitor
) {
711 DBG("Kernel consumer add stream %s in no monitor mode with "
712 "relayd id %" PRIu64
, new_stream
->name
,
713 new_stream
->net_seq_idx
);
714 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
718 /* Send stream to relayd if the stream has an ID. */
719 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
720 ret
= consumer_send_relayd_stream(new_stream
,
721 new_stream
->chan
->pathname
);
723 consumer_stream_free(new_stream
);
728 * If adding an extra stream to an already
729 * existing channel (e.g. cpu hotplug), we need
730 * to send the "streams_sent" command to relayd.
732 if (channel
->streams_sent_to_relayd
) {
733 ret
= consumer_send_relayd_streams_sent(
734 new_stream
->net_seq_idx
);
741 /* Get the right pipe where the stream will be sent. */
742 if (new_stream
->metadata_flag
) {
743 consumer_add_metadata_stream(new_stream
);
744 stream_pipe
= ctx
->consumer_metadata_pipe
;
746 consumer_add_data_stream(new_stream
);
747 stream_pipe
= ctx
->consumer_data_pipe
;
750 /* Visible to other threads */
751 new_stream
->globally_visible
= 1;
753 health_code_update();
755 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
757 ERR("Consumer write %s stream to pipe %d",
758 new_stream
->metadata_flag
? "metadata" : "data",
759 lttng_pipe_get_writefd(stream_pipe
));
760 if (new_stream
->metadata_flag
) {
761 consumer_del_stream_for_metadata(new_stream
);
763 consumer_del_stream_for_data(new_stream
);
768 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
769 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
772 case LTTNG_CONSUMER_STREAMS_SENT
:
774 struct lttng_consumer_channel
*channel
;
777 * Get stream's channel reference. Needed when adding the stream to the
780 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
783 * We could not find the channel. Can happen if cpu hotplug
784 * happens while tearing down.
786 ERR("Unable to find channel key %" PRIu64
,
787 msg
.u
.sent_streams
.channel_key
);
788 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
791 health_code_update();
794 * Send status code to session daemon.
796 ret
= consumer_send_status_msg(sock
, ret_code
);
797 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
798 /* Somehow, the session daemon is not responding anymore. */
802 health_code_update();
805 * We should not send this message if we don't monitor the
806 * streams in this channel.
808 if (!channel
->monitor
) {
812 health_code_update();
813 /* Send stream to relayd if the stream has an ID. */
814 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
815 ret
= consumer_send_relayd_streams_sent(
816 msg
.u
.sent_streams
.net_seq_idx
);
820 channel
->streams_sent_to_relayd
= true;
824 case LTTNG_CONSUMER_UPDATE_STREAM
:
829 case LTTNG_CONSUMER_DESTROY_RELAYD
:
831 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
832 struct consumer_relayd_sock_pair
*relayd
;
834 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
836 /* Get relayd reference if exists. */
837 relayd
= consumer_find_relayd(index
);
838 if (relayd
== NULL
) {
839 DBG("Unable to find relayd %" PRIu64
, index
);
840 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
844 * Each relayd socket pair has a refcount of stream attached to it
845 * which tells if the relayd is still active or not depending on the
848 * This will set the destroy flag of the relayd object and destroy it
849 * if the refcount reaches zero when called.
851 * The destroy can happen either here or when a stream fd hangs up.
854 consumer_flag_relayd_for_destroy(relayd
);
857 health_code_update();
859 ret
= consumer_send_status_msg(sock
, ret_code
);
861 /* Somehow, the session daemon is not responding anymore. */
867 case LTTNG_CONSUMER_DATA_PENDING
:
870 uint64_t id
= msg
.u
.data_pending
.session_id
;
872 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
874 ret
= consumer_data_pending(id
);
876 health_code_update();
878 /* Send back returned value to session daemon */
879 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
881 PERROR("send data pending ret code");
886 * No need to send back a status message since the data pending
887 * returned value is the response.
891 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
893 if (msg
.u
.snapshot_channel
.metadata
== 1) {
894 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
895 msg
.u
.snapshot_channel
.pathname
,
896 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
898 ERR("Snapshot metadata failed");
899 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
902 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
903 msg
.u
.snapshot_channel
.pathname
,
904 msg
.u
.snapshot_channel
.relayd_id
,
905 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
908 ERR("Snapshot channel failed");
909 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. */
922 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
924 uint64_t key
= msg
.u
.destroy_channel
.key
;
925 struct lttng_consumer_channel
*channel
;
927 channel
= consumer_find_channel(key
);
929 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
930 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
933 health_code_update();
935 ret
= consumer_send_status_msg(sock
, ret_code
);
937 /* Somehow, the session daemon is not responding anymore. */
941 health_code_update();
943 /* Stop right now if no channel was found. */
949 * This command should ONLY be issued for channel with streams set in
952 assert(!channel
->monitor
);
955 * The refcount should ALWAYS be 0 in the case of a channel in no
958 assert(!uatomic_sub_return(&channel
->refcount
, 1));
960 consumer_del_channel(channel
);
964 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
968 struct lttng_consumer_channel
*channel
;
969 uint64_t id
= msg
.u
.discarded_events
.session_id
;
970 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
972 DBG("Kernel consumer discarded events command for session id %"
973 PRIu64
", channel key %" PRIu64
, id
, key
);
975 channel
= consumer_find_channel(key
);
977 ERR("Kernel consumer discarded events channel %"
978 PRIu64
" not found", key
);
981 count
= channel
->discarded_events
;
984 health_code_update();
986 /* Send back returned value to session daemon */
987 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
989 PERROR("send discarded events");
995 case LTTNG_CONSUMER_LOST_PACKETS
:
999 struct lttng_consumer_channel
*channel
;
1000 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1001 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1003 DBG("Kernel consumer lost packets command for session id %"
1004 PRIu64
", channel key %" PRIu64
, id
, key
);
1006 channel
= consumer_find_channel(key
);
1008 ERR("Kernel consumer lost packets channel %"
1009 PRIu64
" not found", key
);
1012 count
= channel
->lost_packets
;
1015 health_code_update();
1017 /* Send back returned value to session daemon */
1018 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1020 PERROR("send lost packets");
1026 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1028 int channel_monitor_pipe
;
1030 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1031 /* Successfully received the command's type. */
1032 ret
= consumer_send_status_msg(sock
, ret_code
);
1037 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1039 if (ret
!= sizeof(channel_monitor_pipe
)) {
1040 ERR("Failed to receive channel monitor pipe");
1044 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1045 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1046 channel_monitor_pipe
);
1050 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1051 /* Set the pipe as non-blocking. */
1052 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1054 PERROR("fcntl get flags of the channel monitoring pipe");
1059 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1060 flags
| O_NONBLOCK
);
1062 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1065 DBG("Channel monitor pipe set as non-blocking");
1067 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1069 ret
= consumer_send_status_msg(sock
, ret_code
);
1075 case LTTNG_CONSUMER_ROTATE_RENAME
:
1077 DBG("Consumer rename session %" PRIu64
" after rotation, old path = \"%s\", new path = \"%s\"",
1078 msg
.u
.rotate_rename
.session_id
,
1079 msg
.u
.rotate_rename
.old_path
,
1080 msg
.u
.rotate_rename
.new_path
);
1081 ret
= lttng_consumer_rotate_rename(msg
.u
.rotate_rename
.old_path
,
1082 msg
.u
.rotate_rename
.new_path
,
1083 msg
.u
.rotate_rename
.uid
,
1084 msg
.u
.rotate_rename
.gid
,
1085 msg
.u
.rotate_rename
.relayd_id
);
1087 ERR("Rotate rename failed");
1088 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1091 health_code_update();
1093 ret
= consumer_send_status_msg(sock
, ret_code
);
1095 /* Somehow, the session daemon is not responding anymore. */
1100 case LTTNG_CONSUMER_MKDIR
:
1102 DBG("Consumer mkdir %s in session %" PRIu64
,
1104 msg
.u
.mkdir
.session_id
);
1105 ret
= lttng_consumer_mkdir(msg
.u
.mkdir
.path
,
1108 msg
.u
.mkdir
.relayd_id
);
1110 ERR("consumer mkdir failed");
1111 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1114 health_code_update();
1116 ret
= consumer_send_status_msg(sock
, ret_code
);
1118 /* Somehow, the session daemon is not responding anymore. */
1131 * Return 1 to indicate success since the 0 value can be a socket
1132 * shutdown during the recv() or send() call.
1134 health_code_update();
1139 /* This will issue a consumer stop. */
1144 * Populate index values of a kernel stream. Values are set in big endian order.
1146 * Return 0 on success or else a negative value.
1148 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1152 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1154 PERROR("kernctl_get_timestamp_begin");
1157 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1159 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1161 PERROR("kernctl_get_timestamp_end");
1164 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1166 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1168 PERROR("kernctl_get_events_discarded");
1171 index
->events_discarded
= htobe64(index
->events_discarded
);
1173 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1175 PERROR("kernctl_get_content_size");
1178 index
->content_size
= htobe64(index
->content_size
);
1180 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1182 PERROR("kernctl_get_packet_size");
1185 index
->packet_size
= htobe64(index
->packet_size
);
1187 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1189 PERROR("kernctl_get_stream_id");
1192 index
->stream_id
= htobe64(index
->stream_id
);
1194 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1196 if (ret
== -ENOTTY
) {
1197 /* Command not implemented by lttng-modules. */
1198 index
->stream_instance_id
= -1ULL;
1200 PERROR("kernctl_get_instance_id");
1204 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1206 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1208 if (ret
== -ENOTTY
) {
1209 /* Command not implemented by lttng-modules. */
1210 index
->packet_seq_num
= -1ULL;
1213 PERROR("kernctl_get_sequence_number");
1217 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1223 * Sync metadata meaning request them to the session daemon and snapshot to the
1224 * metadata thread can consumer them.
1226 * Metadata stream lock MUST be acquired.
1228 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1229 * is empty or a negative value on error.
1231 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1237 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1239 ERR("Failed to flush kernel stream");
1243 ret
= kernctl_snapshot(metadata
->wait_fd
);
1245 if (ret
!= -EAGAIN
) {
1246 ERR("Sync metadata, taking kernel snapshot failed.");
1249 DBG("Sync metadata, no new kernel metadata");
1250 /* No new metadata, exit. */
1260 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1263 uint64_t seq
, discarded
;
1265 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1267 if (ret
== -ENOTTY
) {
1268 /* Command not implemented by lttng-modules. */
1271 PERROR("kernctl_get_sequence_number");
1277 * Start the sequence when we extract the first packet in case we don't
1278 * start at 0 (for example if a consumer is not connected to the
1279 * session immediately after the beginning).
1281 if (stream
->last_sequence_number
== -1ULL) {
1282 stream
->last_sequence_number
= seq
;
1283 } else if (seq
> stream
->last_sequence_number
) {
1284 stream
->chan
->lost_packets
+= seq
-
1285 stream
->last_sequence_number
- 1;
1287 /* seq <= last_sequence_number */
1288 ERR("Sequence number inconsistent : prev = %" PRIu64
1289 ", current = %" PRIu64
,
1290 stream
->last_sequence_number
, seq
);
1294 stream
->last_sequence_number
= seq
;
1296 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1298 PERROR("kernctl_get_events_discarded");
1301 if (discarded
< stream
->last_discarded_events
) {
1303 * Overflow has occurred. We assume only one wrap-around
1306 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1307 stream
->last_discarded_events
+ discarded
;
1309 stream
->chan
->discarded_events
+= discarded
-
1310 stream
->last_discarded_events
;
1312 stream
->last_discarded_events
= discarded
;
1320 * Check if the local version of the metadata stream matches with the version
1321 * of the metadata stream in the kernel. If it was updated, set the reset flag
1325 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1328 uint64_t cur_version
;
1330 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1332 if (ret
== -ENOTTY
) {
1334 * LTTng-modules does not implement this
1340 ERR("Failed to get the metadata version");
1344 if (stream
->metadata_version
== cur_version
) {
1349 DBG("New metadata version detected");
1350 stream
->metadata_version
= cur_version
;
1351 stream
->reset_metadata_flag
= 1;
1359 * Consume data on a file descriptor and write it on a trace file.
1361 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1362 struct lttng_consumer_local_data
*ctx
)
1364 unsigned long len
, subbuf_size
, padding
;
1365 int err
, write_index
= 1;
1367 int infd
= stream
->wait_fd
;
1368 struct ctf_packet_index index
;
1370 DBG("In read_subbuffer (infd : %d)", infd
);
1372 /* Get the next subbuffer */
1373 err
= kernctl_get_next_subbuf(infd
);
1376 * This is a debug message even for single-threaded consumer,
1377 * because poll() have more relaxed criterions than get subbuf,
1378 * so get_subbuf may fail for short race windows where poll()
1379 * would issue wakeups.
1381 DBG("Reserving sub buffer failed (everything is normal, "
1382 "it is due to concurrency)");
1387 /* Get the full subbuffer size including padding */
1388 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1390 PERROR("Getting sub-buffer len failed.");
1391 err
= kernctl_put_subbuf(infd
);
1393 if (err
== -EFAULT
) {
1394 PERROR("Error in unreserving sub buffer\n");
1395 } else if (err
== -EIO
) {
1396 /* Should never happen with newer LTTng versions */
1397 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1406 if (!stream
->metadata_flag
) {
1407 ret
= get_index_values(&index
, infd
);
1409 err
= kernctl_put_subbuf(infd
);
1411 if (err
== -EFAULT
) {
1412 PERROR("Error in unreserving sub buffer\n");
1413 } else if (err
== -EIO
) {
1414 /* Should never happen with newer LTTng versions */
1415 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1422 ret
= update_stream_stats(stream
);
1424 err
= kernctl_put_subbuf(infd
);
1426 if (err
== -EFAULT
) {
1427 PERROR("Error in unreserving sub buffer\n");
1428 } else if (err
== -EIO
) {
1429 /* Should never happen with newer LTTng versions */
1430 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1439 ret
= metadata_stream_check_version(infd
, stream
);
1441 err
= kernctl_put_subbuf(infd
);
1443 if (err
== -EFAULT
) {
1444 PERROR("Error in unreserving sub buffer\n");
1445 } else if (err
== -EIO
) {
1446 /* Should never happen with newer LTTng versions */
1447 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1456 switch (stream
->chan
->output
) {
1457 case CONSUMER_CHANNEL_SPLICE
:
1459 * XXX: The lttng-modules splice "actor" does not handle copying
1460 * partial pages hence only using the subbuffer size without the
1461 * padding makes the splice fail.
1466 /* splice the subbuffer to the tracefile */
1467 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1470 * XXX: Splice does not support network streaming so the return value
1471 * is simply checked against subbuf_size and not like the mmap() op.
1473 if (ret
!= subbuf_size
) {
1475 * display the error but continue processing to try
1476 * to release the subbuffer
1478 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1483 case CONSUMER_CHANNEL_MMAP
:
1484 /* Get subbuffer size without padding */
1485 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1487 PERROR("Getting sub-buffer len failed.");
1488 err
= kernctl_put_subbuf(infd
);
1490 if (err
== -EFAULT
) {
1491 PERROR("Error in unreserving sub buffer\n");
1492 } else if (err
== -EIO
) {
1493 /* Should never happen with newer LTTng versions */
1494 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1503 /* Make sure the tracer is not gone mad on us! */
1504 assert(len
>= subbuf_size
);
1506 padding
= len
- subbuf_size
;
1508 /* write the subbuffer to the tracefile */
1509 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1512 * The mmap operation should write subbuf_size amount of data when
1513 * network streaming or the full padding (len) size when we are _not_
1516 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1517 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1519 * Display the error but continue processing to try to release the
1520 * subbuffer. This is a DBG statement since this is possible to
1521 * happen without being a critical error.
1523 DBG("Error writing to tracefile "
1524 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1525 ret
, len
, subbuf_size
);
1530 ERR("Unknown output method");
1534 err
= kernctl_put_next_subbuf(infd
);
1536 if (err
== -EFAULT
) {
1537 PERROR("Error in unreserving sub buffer\n");
1538 } else if (err
== -EIO
) {
1539 /* Should never happen with newer LTTng versions */
1540 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1546 /* Write index if needed. */
1551 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1553 * In live, block until all the metadata is sent.
1555 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1556 assert(!stream
->missed_metadata_flush
);
1557 stream
->waiting_on_metadata
= true;
1558 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1560 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1562 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1563 stream
->waiting_on_metadata
= false;
1564 if (stream
->missed_metadata_flush
) {
1565 stream
->missed_metadata_flush
= false;
1566 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1567 (void) consumer_flush_kernel_index(stream
);
1569 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1576 err
= consumer_stream_write_index(stream
, &index
);
1585 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1592 * Don't create anything if this is set for streaming or should not be
1595 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1596 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1597 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1598 stream
->uid
, stream
->gid
, NULL
);
1602 stream
->out_fd
= ret
;
1603 stream
->tracefile_size_current
= 0;
1605 if (!stream
->metadata_flag
) {
1606 struct lttng_index_file
*index_file
;
1608 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1609 stream
->name
, stream
->uid
, stream
->gid
,
1610 stream
->chan
->tracefile_size
,
1611 stream
->tracefile_count_current
,
1612 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1616 assert(!stream
->index_file
);
1617 stream
->index_file
= index_file
;
1621 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1622 /* get the len of the mmap region */
1623 unsigned long mmap_len
;
1625 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1627 PERROR("kernctl_get_mmap_len");
1628 goto error_close_fd
;
1630 stream
->mmap_len
= (size_t) mmap_len
;
1632 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1633 MAP_PRIVATE
, stream
->wait_fd
, 0);
1634 if (stream
->mmap_base
== MAP_FAILED
) {
1635 PERROR("Error mmaping");
1637 goto error_close_fd
;
1641 /* we return 0 to let the library handle the FD internally */
1645 if (stream
->out_fd
>= 0) {
1648 err
= close(stream
->out_fd
);
1650 stream
->out_fd
= -1;
1657 * Check if data is still being extracted from the buffers for a specific
1658 * stream. Consumer data lock MUST be acquired before calling this function
1659 * and the stream lock.
1661 * Return 1 if the traced data are still getting read else 0 meaning that the
1662 * data is available for trace viewer reading.
1664 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1670 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1675 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1677 /* There is still data so let's put back this subbuffer. */
1678 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1680 ret
= 1; /* Data is pending */
1684 /* Data is NOT pending and ready to be read. */