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
;
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.");
72 * Sample consumed and produced positions for a specific fd.
74 * Returns 0 on success, < 0 on error.
76 int lttng_kconsumer_sample_snapshot_positions(
77 struct lttng_consumer_stream
*stream
)
81 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
85 * Get the produced position
87 * Returns 0 on success, < 0 on error
89 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
93 int infd
= stream
->wait_fd
;
95 ret
= kernctl_snapshot_get_produced(infd
, pos
);
97 PERROR("kernctl_snapshot_get_produced");
104 * Get the consumerd position
106 * Returns 0 on success, < 0 on error
108 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
112 int infd
= stream
->wait_fd
;
114 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
116 PERROR("kernctl_snapshot_get_consumed");
123 * Take a snapshot of all the stream of a channel
125 * Returns 0 on success, < 0 on error
127 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
128 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
129 struct lttng_consumer_local_data
*ctx
)
132 struct lttng_consumer_channel
*channel
;
133 struct lttng_consumer_stream
*stream
;
135 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
139 channel
= consumer_find_channel(key
);
141 ERR("No channel found for key %" PRIu64
, key
);
146 /* Splice is not supported yet for channel snapshot. */
147 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
148 ERR("Unsupported output %d", channel
->output
);
153 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
154 /* Are we at a position _before_ the first available packet ? */
155 bool before_first_packet
= true;
156 unsigned long consumed_pos
, produced_pos
;
158 health_code_update();
161 * Lock stream because we are about to change its state.
163 pthread_mutex_lock(&stream
->lock
);
166 * Assign the received relayd ID so we can use it for streaming. The streams
167 * are not visible to anyone so this is OK to change it.
169 stream
->net_seq_idx
= relayd_id
;
170 channel
->relayd_id
= relayd_id
;
171 if (relayd_id
!= (uint64_t) -1ULL) {
172 ret
= consumer_send_relayd_stream(stream
, path
);
174 ERR("sending stream to relayd");
178 ret
= utils_create_stream_file(path
, stream
->name
,
179 stream
->chan
->tracefile_size
,
180 stream
->tracefile_count_current
,
181 stream
->uid
, stream
->gid
, NULL
);
183 ERR("utils_create_stream_file");
187 stream
->out_fd
= ret
;
188 stream
->tracefile_size_current
= 0;
190 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
191 path
, stream
->name
, stream
->key
);
193 if (relayd_id
!= -1ULL) {
194 ret
= consumer_send_relayd_streams_sent(relayd_id
);
196 ERR("sending streams sent to relayd");
199 channel
->streams_sent_to_relayd
= true;
202 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
205 * Doing a buffer flush which does not take into
206 * account empty packets. This is not perfect
207 * for stream intersection, but required as a
208 * fall-back when "flush_empty" is not
209 * implemented by lttng-modules.
211 ret
= kernctl_buffer_flush(stream
->wait_fd
);
213 ERR("Failed to flush kernel stream");
219 ret
= lttng_kconsumer_take_snapshot(stream
);
221 ERR("Taking kernel snapshot");
225 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
227 ERR("Produced kernel snapshot position");
231 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
233 ERR("Consumerd kernel snapshot position");
237 if (stream
->max_sb_size
== 0) {
238 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
239 &stream
->max_sb_size
);
241 ERR("Getting kernel max_sb_size");
246 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
247 produced_pos
, nb_packets_per_stream
,
248 stream
->max_sb_size
);
250 while (consumed_pos
< produced_pos
) {
252 unsigned long len
, padded_len
;
255 health_code_update();
257 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
259 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
261 if (ret
!= -EAGAIN
) {
262 PERROR("kernctl_get_subbuf snapshot");
265 DBG("Kernel consumer get subbuf failed. Skipping it.");
266 consumed_pos
+= stream
->max_sb_size
;
269 * Start accounting lost packets only when we
270 * already have extracted packets (to match the
271 * content of the final snapshot).
273 if (!before_first_packet
) {
279 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
281 ERR("Snapshot kernctl_get_subbuf_size");
282 goto error_put_subbuf
;
285 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
287 ERR("Snapshot kernctl_get_padded_subbuf_size");
288 goto error_put_subbuf
;
291 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
292 padded_len
- len
, NULL
);
294 * We write the padded len in local tracefiles but the data len
295 * when using a relay. Display the error but continue processing
296 * to try to release the subbuffer.
298 if (relayd_id
!= (uint64_t) -1ULL) {
299 if (read_len
!= len
) {
300 ERR("Error sending to the relay (ret: %zd != len: %lu)",
304 if (read_len
!= padded_len
) {
305 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
306 read_len
, padded_len
);
310 ret
= kernctl_put_subbuf(stream
->wait_fd
);
312 ERR("Snapshot kernctl_put_subbuf");
315 consumed_pos
+= stream
->max_sb_size
;
318 * Only account lost packets located between
319 * succesfully extracted packets (do not account before
320 * and after since they are not visible in the
321 * resulting snapshot).
323 stream
->chan
->lost_packets
+= lost_packet
;
325 before_first_packet
= false;
328 if (relayd_id
== (uint64_t) -1ULL) {
329 if (stream
->out_fd
>= 0) {
330 ret
= close(stream
->out_fd
);
332 PERROR("Kernel consumer snapshot close out_fd");
338 close_relayd_stream(stream
);
339 stream
->net_seq_idx
= (uint64_t) -1ULL;
341 pthread_mutex_unlock(&stream
->lock
);
349 ret
= kernctl_put_subbuf(stream
->wait_fd
);
351 ERR("Snapshot kernctl_put_subbuf error path");
354 pthread_mutex_unlock(&stream
->lock
);
361 * Read the whole metadata available for a snapshot.
363 * Returns 0 on success, < 0 on error
365 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
366 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
368 int ret
, use_relayd
= 0;
370 struct lttng_consumer_channel
*metadata_channel
;
371 struct lttng_consumer_stream
*metadata_stream
;
375 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
380 metadata_channel
= consumer_find_channel(key
);
381 if (!metadata_channel
) {
382 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
387 metadata_stream
= metadata_channel
->metadata_stream
;
388 assert(metadata_stream
);
390 /* Flag once that we have a valid relayd for the stream. */
391 if (relayd_id
!= (uint64_t) -1ULL) {
396 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
401 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
402 metadata_stream
->chan
->tracefile_size
,
403 metadata_stream
->tracefile_count_current
,
404 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
408 metadata_stream
->out_fd
= ret
;
412 health_code_update();
414 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
416 if (ret_read
!= -EAGAIN
) {
417 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
421 /* ret_read is negative at this point so we will exit the loop. */
424 } while (ret_read
>= 0);
427 close_relayd_stream(metadata_stream
);
428 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
430 if (metadata_stream
->out_fd
>= 0) {
431 ret
= close(metadata_stream
->out_fd
);
433 PERROR("Kernel consumer snapshot metadata close out_fd");
435 * Don't go on error here since the snapshot was successful at this
436 * point but somehow the close failed.
439 metadata_stream
->out_fd
= -1;
445 cds_list_del(&metadata_stream
->send_node
);
446 consumer_stream_destroy(metadata_stream
, NULL
);
447 metadata_channel
->metadata_stream
= NULL
;
454 * Receive command from session daemon and process it.
456 * Return 1 on success else a negative value or 0.
458 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
459 int sock
, struct pollfd
*consumer_sockpoll
)
462 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
463 struct lttcomm_consumer_msg msg
;
465 health_code_update();
467 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
468 if (ret
!= sizeof(msg
)) {
470 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
476 health_code_update();
478 /* Deprecated command */
479 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
481 health_code_update();
483 /* relayd needs RCU read-side protection */
486 switch (msg
.cmd_type
) {
487 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
489 /* Session daemon status message are handled in the following call. */
490 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
491 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
492 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
493 msg
.u
.relayd_sock
.relayd_session_id
);
496 case LTTNG_CONSUMER_ADD_CHANNEL
:
498 struct lttng_consumer_channel
*new_channel
;
501 health_code_update();
503 /* First send a status message before receiving the fds. */
504 ret
= consumer_send_status_msg(sock
, ret_code
);
506 /* Somehow, the session daemon is not responding anymore. */
510 health_code_update();
512 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
513 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
514 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
515 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
516 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
517 msg
.u
.channel
.tracefile_size
,
518 msg
.u
.channel
.tracefile_count
, 0,
519 msg
.u
.channel
.monitor
,
520 msg
.u
.channel
.live_timer_interval
,
522 if (new_channel
== NULL
) {
523 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
526 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
527 switch (msg
.u
.channel
.output
) {
528 case LTTNG_EVENT_SPLICE
:
529 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
531 case LTTNG_EVENT_MMAP
:
532 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
535 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
539 /* Translate and save channel type. */
540 switch (msg
.u
.channel
.type
) {
541 case CONSUMER_CHANNEL_TYPE_DATA
:
542 case CONSUMER_CHANNEL_TYPE_METADATA
:
543 new_channel
->type
= msg
.u
.channel
.type
;
550 health_code_update();
552 if (ctx
->on_recv_channel
!= NULL
) {
553 ret_recv
= ctx
->on_recv_channel(new_channel
);
555 ret
= consumer_add_channel(new_channel
, ctx
);
556 } else if (ret_recv
< 0) {
560 ret
= consumer_add_channel(new_channel
, ctx
);
562 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
563 int monitor_start_ret
;
565 DBG("Consumer starting monitor timer");
566 consumer_timer_live_start(new_channel
,
567 msg
.u
.channel
.live_timer_interval
);
568 monitor_start_ret
= consumer_timer_monitor_start(
570 msg
.u
.channel
.monitor_timer_interval
);
571 if (monitor_start_ret
< 0) {
572 ERR("Starting channel monitoring timer failed");
578 health_code_update();
580 /* If we received an error in add_channel, we need to report it. */
582 ret
= consumer_send_status_msg(sock
, ret
);
591 case LTTNG_CONSUMER_ADD_STREAM
:
594 struct lttng_pipe
*stream_pipe
;
595 struct lttng_consumer_stream
*new_stream
;
596 struct lttng_consumer_channel
*channel
;
600 * Get stream's channel reference. Needed when adding the stream to the
603 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
606 * We could not find the channel. Can happen if cpu hotplug
607 * happens while tearing down.
609 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
610 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
613 health_code_update();
615 /* First send a status message before receiving the fds. */
616 ret
= consumer_send_status_msg(sock
, ret_code
);
618 /* Somehow, the session daemon is not responding anymore. */
622 health_code_update();
624 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
625 /* Channel was not found. */
631 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
637 health_code_update();
639 /* Get stream file descriptor from socket */
640 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
641 if (ret
!= sizeof(fd
)) {
642 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
647 health_code_update();
650 * Send status code to session daemon only if the recv works. If the
651 * above recv() failed, the session daemon is notified through the
652 * error socket and the teardown is eventually done.
654 ret
= consumer_send_status_msg(sock
, ret_code
);
656 /* Somehow, the session daemon is not responding anymore. */
660 health_code_update();
662 new_stream
= consumer_allocate_stream(channel
->key
,
664 LTTNG_CONSUMER_ACTIVE_STREAM
,
674 if (new_stream
== NULL
) {
679 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
685 new_stream
->chan
= channel
;
686 new_stream
->wait_fd
= fd
;
687 switch (channel
->output
) {
688 case CONSUMER_CHANNEL_SPLICE
:
689 new_stream
->output
= LTTNG_EVENT_SPLICE
;
690 ret
= utils_create_pipe(new_stream
->splice_pipe
);
695 case CONSUMER_CHANNEL_MMAP
:
696 new_stream
->output
= LTTNG_EVENT_MMAP
;
699 ERR("Stream output unknown %d", channel
->output
);
704 * We've just assigned the channel to the stream so increment the
705 * refcount right now. We don't need to increment the refcount for
706 * streams in no monitor because we handle manually the cleanup of
707 * those. It is very important to make sure there is NO prior
708 * consumer_del_stream() calls or else the refcount will be unbalanced.
710 if (channel
->monitor
) {
711 uatomic_inc(&new_stream
->chan
->refcount
);
715 * The buffer flush is done on the session daemon side for the kernel
716 * so no need for the stream "hangup_flush_done" variable to be
717 * tracked. This is important for a kernel stream since we don't rely
718 * on the flush state of the stream to read data. It's not the case for
719 * user space tracing.
721 new_stream
->hangup_flush_done
= 0;
723 health_code_update();
725 if (ctx
->on_recv_stream
) {
726 ret
= ctx
->on_recv_stream(new_stream
);
728 consumer_stream_free(new_stream
);
733 health_code_update();
735 if (new_stream
->metadata_flag
) {
736 channel
->metadata_stream
= new_stream
;
739 /* Do not monitor this stream. */
740 if (!channel
->monitor
) {
741 DBG("Kernel consumer add stream %s in no monitor mode with "
742 "relayd id %" PRIu64
, new_stream
->name
,
743 new_stream
->net_seq_idx
);
744 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
748 /* Send stream to relayd if the stream has an ID. */
749 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
750 ret
= consumer_send_relayd_stream(new_stream
,
751 new_stream
->chan
->pathname
);
753 consumer_stream_free(new_stream
);
758 * If adding an extra stream to an already
759 * existing channel (e.g. cpu hotplug), we need
760 * to send the "streams_sent" command to relayd.
762 if (channel
->streams_sent_to_relayd
) {
763 ret
= consumer_send_relayd_streams_sent(
764 new_stream
->net_seq_idx
);
771 /* Get the right pipe where the stream will be sent. */
772 if (new_stream
->metadata_flag
) {
773 ret
= consumer_add_metadata_stream(new_stream
);
775 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
777 consumer_stream_free(new_stream
);
780 stream_pipe
= ctx
->consumer_metadata_pipe
;
782 ret
= consumer_add_data_stream(new_stream
);
784 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
786 consumer_stream_free(new_stream
);
789 stream_pipe
= ctx
->consumer_data_pipe
;
792 /* Vitible to other threads */
793 new_stream
->globally_visible
= 1;
795 health_code_update();
797 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
799 ERR("Consumer write %s stream to pipe %d",
800 new_stream
->metadata_flag
? "metadata" : "data",
801 lttng_pipe_get_writefd(stream_pipe
));
802 if (new_stream
->metadata_flag
) {
803 consumer_del_stream_for_metadata(new_stream
);
805 consumer_del_stream_for_data(new_stream
);
810 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
811 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
814 case LTTNG_CONSUMER_STREAMS_SENT
:
816 struct lttng_consumer_channel
*channel
;
819 * Get stream's channel reference. Needed when adding the stream to the
822 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
825 * We could not find the channel. Can happen if cpu hotplug
826 * happens while tearing down.
828 ERR("Unable to find channel key %" PRIu64
,
829 msg
.u
.sent_streams
.channel_key
);
830 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
833 health_code_update();
836 * Send status code to session daemon.
838 ret
= consumer_send_status_msg(sock
, ret_code
);
839 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
840 /* Somehow, the session daemon is not responding anymore. */
844 health_code_update();
847 * We should not send this message if we don't monitor the
848 * streams in this channel.
850 if (!channel
->monitor
) {
854 health_code_update();
855 /* Send stream to relayd if the stream has an ID. */
856 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
857 ret
= consumer_send_relayd_streams_sent(
858 msg
.u
.sent_streams
.net_seq_idx
);
862 channel
->streams_sent_to_relayd
= true;
866 case LTTNG_CONSUMER_UPDATE_STREAM
:
871 case LTTNG_CONSUMER_DESTROY_RELAYD
:
873 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
874 struct consumer_relayd_sock_pair
*relayd
;
876 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
878 /* Get relayd reference if exists. */
879 relayd
= consumer_find_relayd(index
);
880 if (relayd
== NULL
) {
881 DBG("Unable to find relayd %" PRIu64
, index
);
882 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
886 * Each relayd socket pair has a refcount of stream attached to it
887 * which tells if the relayd is still active or not depending on the
890 * This will set the destroy flag of the relayd object and destroy it
891 * if the refcount reaches zero when called.
893 * The destroy can happen either here or when a stream fd hangs up.
896 consumer_flag_relayd_for_destroy(relayd
);
899 health_code_update();
901 ret
= consumer_send_status_msg(sock
, ret_code
);
903 /* Somehow, the session daemon is not responding anymore. */
909 case LTTNG_CONSUMER_DATA_PENDING
:
912 uint64_t id
= msg
.u
.data_pending
.session_id
;
914 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
916 ret
= consumer_data_pending(id
);
918 health_code_update();
920 /* Send back returned value to session daemon */
921 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
923 PERROR("send data pending ret code");
928 * No need to send back a status message since the data pending
929 * returned value is the response.
933 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
935 if (msg
.u
.snapshot_channel
.metadata
== 1) {
936 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
937 msg
.u
.snapshot_channel
.pathname
,
938 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
940 ERR("Snapshot metadata failed");
941 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
944 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
945 msg
.u
.snapshot_channel
.pathname
,
946 msg
.u
.snapshot_channel
.relayd_id
,
947 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
950 ERR("Snapshot channel failed");
951 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
955 health_code_update();
957 ret
= consumer_send_status_msg(sock
, ret_code
);
959 /* Somehow, the session daemon is not responding anymore. */
964 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
966 uint64_t key
= msg
.u
.destroy_channel
.key
;
967 struct lttng_consumer_channel
*channel
;
969 channel
= consumer_find_channel(key
);
971 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
972 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
975 health_code_update();
977 ret
= consumer_send_status_msg(sock
, ret_code
);
979 /* Somehow, the session daemon is not responding anymore. */
983 health_code_update();
985 /* Stop right now if no channel was found. */
991 * This command should ONLY be issued for channel with streams set in
994 assert(!channel
->monitor
);
997 * The refcount should ALWAYS be 0 in the case of a channel in no
1000 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1002 consumer_del_channel(channel
);
1006 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1009 struct lttng_consumer_channel
*channel
;
1010 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1011 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1013 DBG("Kernel consumer discarded events command for session id %"
1014 PRIu64
", channel key %" PRIu64
, id
, key
);
1016 channel
= consumer_find_channel(key
);
1018 ERR("Kernel consumer discarded events channel %"
1019 PRIu64
" not found", key
);
1022 ret
= channel
->discarded_events
;
1025 health_code_update();
1027 /* Send back returned value to session daemon */
1028 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1030 PERROR("send discarded events");
1036 case LTTNG_CONSUMER_LOST_PACKETS
:
1039 struct lttng_consumer_channel
*channel
;
1040 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1041 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1043 DBG("Kernel consumer lost packets command for session id %"
1044 PRIu64
", channel key %" PRIu64
, id
, key
);
1046 channel
= consumer_find_channel(key
);
1048 ERR("Kernel consumer lost packets channel %"
1049 PRIu64
" not found", key
);
1052 ret
= channel
->lost_packets
;
1055 health_code_update();
1057 /* Send back returned value to session daemon */
1058 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1060 PERROR("send lost packets");
1066 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1068 int channel_monitor_pipe
;
1070 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1071 /* Successfully received the command's type. */
1072 ret
= consumer_send_status_msg(sock
, ret_code
);
1077 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1079 if (ret
!= sizeof(channel_monitor_pipe
)) {
1080 ERR("Failed to receive channel monitor pipe");
1084 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1085 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1086 channel_monitor_pipe
);
1090 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1091 /* Set the pipe as non-blocking. */
1092 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1094 PERROR("fcntl get flags of the channel monitoring pipe");
1099 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1100 flags
| O_NONBLOCK
);
1102 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1105 DBG("Channel monitor pipe set as non-blocking");
1107 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1109 ret
= consumer_send_status_msg(sock
, ret_code
);
1123 * Return 1 to indicate success since the 0 value can be a socket
1124 * shutdown during the recv() or send() call.
1126 health_code_update();
1131 /* This will issue a consumer stop. */
1136 * Populate index values of a kernel stream. Values are set in big endian order.
1138 * Return 0 on success or else a negative value.
1140 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1144 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1146 PERROR("kernctl_get_timestamp_begin");
1149 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1151 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1153 PERROR("kernctl_get_timestamp_end");
1156 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1158 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1160 PERROR("kernctl_get_events_discarded");
1163 index
->events_discarded
= htobe64(index
->events_discarded
);
1165 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1167 PERROR("kernctl_get_content_size");
1170 index
->content_size
= htobe64(index
->content_size
);
1172 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1174 PERROR("kernctl_get_packet_size");
1177 index
->packet_size
= htobe64(index
->packet_size
);
1179 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1181 PERROR("kernctl_get_stream_id");
1184 index
->stream_id
= htobe64(index
->stream_id
);
1186 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1188 if (ret
== -ENOTTY
) {
1189 /* Command not implemented by lttng-modules. */
1190 index
->stream_instance_id
= -1ULL;
1193 PERROR("kernctl_get_instance_id");
1197 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1199 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1201 if (ret
== -ENOTTY
) {
1202 /* Command not implemented by lttng-modules. */
1203 index
->packet_seq_num
= -1ULL;
1206 PERROR("kernctl_get_sequence_number");
1210 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1216 * Sync metadata meaning request them to the session daemon and snapshot to the
1217 * metadata thread can consumer them.
1219 * Metadata stream lock MUST be acquired.
1221 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1222 * is empty or a negative value on error.
1224 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1230 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1232 ERR("Failed to flush kernel stream");
1236 ret
= kernctl_snapshot(metadata
->wait_fd
);
1238 if (ret
!= -EAGAIN
) {
1239 ERR("Sync metadata, taking kernel snapshot failed.");
1242 DBG("Sync metadata, no new kernel metadata");
1243 /* No new metadata, exit. */
1253 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1256 uint64_t seq
, discarded
;
1258 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1260 if (ret
== -ENOTTY
) {
1261 /* Command not implemented by lttng-modules. */
1265 PERROR("kernctl_get_sequence_number");
1271 * Start the sequence when we extract the first packet in case we don't
1272 * start at 0 (for example if a consumer is not connected to the
1273 * session immediately after the beginning).
1275 if (stream
->last_sequence_number
== -1ULL) {
1276 stream
->last_sequence_number
= seq
;
1277 } else if (seq
> stream
->last_sequence_number
) {
1278 stream
->chan
->lost_packets
+= seq
-
1279 stream
->last_sequence_number
- 1;
1281 /* seq <= last_sequence_number */
1282 ERR("Sequence number inconsistent : prev = %" PRIu64
1283 ", current = %" PRIu64
,
1284 stream
->last_sequence_number
, seq
);
1288 stream
->last_sequence_number
= seq
;
1290 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1292 PERROR("kernctl_get_events_discarded");
1295 if (discarded
< stream
->last_discarded_events
) {
1297 * Overflow has occurred. We assume only one wrap-around
1300 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1301 stream
->last_discarded_events
+ discarded
;
1303 stream
->chan
->discarded_events
+= discarded
-
1304 stream
->last_discarded_events
;
1306 stream
->last_discarded_events
= discarded
;
1314 * Check if the local version of the metadata stream matches with the version
1315 * of the metadata stream in the kernel. If it was updated, set the reset flag
1319 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1322 uint64_t cur_version
;
1324 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1326 if (ret
== -ENOTTY
) {
1328 * LTTng-modules does not implement this
1334 ERR("Failed to get the metadata version");
1338 if (stream
->metadata_version
== cur_version
) {
1343 DBG("New metadata version detected");
1344 stream
->metadata_version
= cur_version
;
1345 stream
->reset_metadata_flag
= 1;
1353 * Consume data on a file descriptor and write it on a trace file.
1355 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1356 struct lttng_consumer_local_data
*ctx
)
1358 unsigned long len
, subbuf_size
, padding
;
1359 int err
, write_index
= 1;
1361 int infd
= stream
->wait_fd
;
1362 struct ctf_packet_index index
;
1364 DBG("In read_subbuffer (infd : %d)", infd
);
1366 /* Get the next subbuffer */
1367 err
= kernctl_get_next_subbuf(infd
);
1370 * This is a debug message even for single-threaded consumer,
1371 * because poll() have more relaxed criterions than get subbuf,
1372 * so get_subbuf may fail for short race windows where poll()
1373 * would issue wakeups.
1375 DBG("Reserving sub buffer failed (everything is normal, "
1376 "it is due to concurrency)");
1381 /* Get the full subbuffer size including padding */
1382 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1384 PERROR("Getting sub-buffer len failed.");
1385 err
= kernctl_put_subbuf(infd
);
1387 if (err
== -EFAULT
) {
1388 PERROR("Error in unreserving sub buffer\n");
1389 } else if (err
== -EIO
) {
1390 /* Should never happen with newer LTTng versions */
1391 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1400 if (!stream
->metadata_flag
) {
1401 ret
= get_index_values(&index
, infd
);
1403 err
= kernctl_put_subbuf(infd
);
1405 if (err
== -EFAULT
) {
1406 PERROR("Error in unreserving sub buffer\n");
1407 } else if (err
== -EIO
) {
1408 /* Should never happen with newer LTTng versions */
1409 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1416 ret
= update_stream_stats(stream
);
1418 err
= kernctl_put_subbuf(infd
);
1420 if (err
== -EFAULT
) {
1421 PERROR("Error in unreserving sub buffer\n");
1422 } else if (err
== -EIO
) {
1423 /* Should never happen with newer LTTng versions */
1424 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1433 ret
= metadata_stream_check_version(infd
, stream
);
1435 err
= kernctl_put_subbuf(infd
);
1437 if (err
== -EFAULT
) {
1438 PERROR("Error in unreserving sub buffer\n");
1439 } else if (err
== -EIO
) {
1440 /* Should never happen with newer LTTng versions */
1441 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1450 switch (stream
->chan
->output
) {
1451 case CONSUMER_CHANNEL_SPLICE
:
1453 * XXX: The lttng-modules splice "actor" does not handle copying
1454 * partial pages hence only using the subbuffer size without the
1455 * padding makes the splice fail.
1460 /* splice the subbuffer to the tracefile */
1461 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1464 * XXX: Splice does not support network streaming so the return value
1465 * is simply checked against subbuf_size and not like the mmap() op.
1467 if (ret
!= subbuf_size
) {
1469 * display the error but continue processing to try
1470 * to release the subbuffer
1472 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1477 case CONSUMER_CHANNEL_MMAP
:
1478 /* Get subbuffer size without padding */
1479 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1481 PERROR("Getting sub-buffer len failed.");
1482 err
= kernctl_put_subbuf(infd
);
1484 if (err
== -EFAULT
) {
1485 PERROR("Error in unreserving sub buffer\n");
1486 } else if (err
== -EIO
) {
1487 /* Should never happen with newer LTTng versions */
1488 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1497 /* Make sure the tracer is not gone mad on us! */
1498 assert(len
>= subbuf_size
);
1500 padding
= len
- subbuf_size
;
1502 /* write the subbuffer to the tracefile */
1503 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1506 * The mmap operation should write subbuf_size amount of data when
1507 * network streaming or the full padding (len) size when we are _not_
1510 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1511 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1513 * Display the error but continue processing to try to release the
1514 * subbuffer. This is a DBG statement since this is possible to
1515 * happen without being a critical error.
1517 DBG("Error writing to tracefile "
1518 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1519 ret
, len
, subbuf_size
);
1524 ERR("Unknown output method");
1528 err
= kernctl_put_next_subbuf(infd
);
1530 if (err
== -EFAULT
) {
1531 PERROR("Error in unreserving sub buffer\n");
1532 } else if (err
== -EIO
) {
1533 /* Should never happen with newer LTTng versions */
1534 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1540 /* Write index if needed. */
1545 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1547 * In live, block until all the metadata is sent.
1549 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1550 assert(!stream
->missed_metadata_flush
);
1551 stream
->waiting_on_metadata
= true;
1552 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1554 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1556 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1557 stream
->waiting_on_metadata
= false;
1558 if (stream
->missed_metadata_flush
) {
1559 stream
->missed_metadata_flush
= false;
1560 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1561 (void) consumer_flush_kernel_index(stream
);
1563 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1570 err
= consumer_stream_write_index(stream
, &index
);
1579 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1586 * Don't create anything if this is set for streaming or should not be
1589 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1590 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1591 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1592 stream
->uid
, stream
->gid
, NULL
);
1596 stream
->out_fd
= ret
;
1597 stream
->tracefile_size_current
= 0;
1599 if (!stream
->metadata_flag
) {
1600 struct lttng_index_file
*index_file
;
1602 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1603 stream
->name
, stream
->uid
, stream
->gid
,
1604 stream
->chan
->tracefile_size
,
1605 stream
->tracefile_count_current
,
1606 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1610 assert(!stream
->index_file
);
1611 stream
->index_file
= index_file
;
1615 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1616 /* get the len of the mmap region */
1617 unsigned long mmap_len
;
1619 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1621 PERROR("kernctl_get_mmap_len");
1622 goto error_close_fd
;
1624 stream
->mmap_len
= (size_t) mmap_len
;
1626 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1627 MAP_PRIVATE
, stream
->wait_fd
, 0);
1628 if (stream
->mmap_base
== MAP_FAILED
) {
1629 PERROR("Error mmaping");
1631 goto error_close_fd
;
1635 /* we return 0 to let the library handle the FD internally */
1639 if (stream
->out_fd
>= 0) {
1642 err
= close(stream
->out_fd
);
1644 stream
->out_fd
= -1;
1651 * Check if data is still being extracted from the buffers for a specific
1652 * stream. Consumer data lock MUST be acquired before calling this function
1653 * and the stream lock.
1655 * Return 1 if the traced data are still getting read else 0 meaning that the
1656 * data is available for trace viewer reading.
1658 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1664 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1669 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1671 /* There is still data so let's put back this subbuffer. */
1672 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1674 ret
= 1; /* Data is pending */
1678 /* Data is NOT pending and ready to be read. */