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(stream
->wait_fd
);
204 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
;
244 health_code_update();
246 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
248 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
250 if (ret
!= -EAGAIN
) {
251 PERROR("kernctl_get_subbuf snapshot");
254 DBG("Kernel consumer get subbuf failed. Skipping it.");
255 consumed_pos
+= stream
->max_sb_size
;
258 * Start accounting lost packets only when we
259 * already have extracted packets (to match the
260 * content of the final snapshot).
262 if (!before_first_packet
) {
268 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
270 ERR("Snapshot kernctl_get_subbuf_size");
271 goto error_put_subbuf
;
274 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
276 ERR("Snapshot kernctl_get_padded_subbuf_size");
277 goto error_put_subbuf
;
280 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
281 padded_len
- len
, NULL
);
283 * We write the padded len in local tracefiles but the data len
284 * when using a relay. Display the error but continue processing
285 * to try to release the subbuffer.
287 if (relayd_id
!= (uint64_t) -1ULL) {
288 if (read_len
!= len
) {
289 ERR("Error sending to the relay (ret: %zd != len: %lu)",
293 if (read_len
!= padded_len
) {
294 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
295 read_len
, padded_len
);
299 ret
= kernctl_put_subbuf(stream
->wait_fd
);
301 ERR("Snapshot kernctl_put_subbuf");
304 consumed_pos
+= stream
->max_sb_size
;
307 * Only account lost packets located between
308 * succesfully extracted packets (do not account before
309 * and after since they are not visible in the
310 * resulting snapshot).
312 stream
->chan
->lost_packets
+= lost_packet
;
314 before_first_packet
= false;
317 if (relayd_id
== (uint64_t) -1ULL) {
318 if (stream
->out_fd
>= 0) {
319 ret
= close(stream
->out_fd
);
321 PERROR("Kernel consumer snapshot close out_fd");
327 close_relayd_stream(stream
);
328 stream
->net_seq_idx
= (uint64_t) -1ULL;
330 pthread_mutex_unlock(&stream
->lock
);
338 ret
= kernctl_put_subbuf(stream
->wait_fd
);
340 ERR("Snapshot kernctl_put_subbuf error path");
343 pthread_mutex_unlock(&stream
->lock
);
350 * Read the whole metadata available for a snapshot.
352 * Returns 0 on success, < 0 on error
354 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
355 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
357 int ret
, use_relayd
= 0;
359 struct lttng_consumer_channel
*metadata_channel
;
360 struct lttng_consumer_stream
*metadata_stream
;
364 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
369 metadata_channel
= consumer_find_channel(key
);
370 if (!metadata_channel
) {
371 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
376 metadata_stream
= metadata_channel
->metadata_stream
;
377 assert(metadata_stream
);
379 /* Flag once that we have a valid relayd for the stream. */
380 if (relayd_id
!= (uint64_t) -1ULL) {
385 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
390 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
391 metadata_stream
->chan
->tracefile_size
,
392 metadata_stream
->tracefile_count_current
,
393 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
397 metadata_stream
->out_fd
= ret
;
401 health_code_update();
403 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
405 if (ret_read
!= -EAGAIN
) {
406 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
410 /* ret_read is negative at this point so we will exit the loop. */
413 } while (ret_read
>= 0);
416 close_relayd_stream(metadata_stream
);
417 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
419 if (metadata_stream
->out_fd
>= 0) {
420 ret
= close(metadata_stream
->out_fd
);
422 PERROR("Kernel consumer snapshot metadata close out_fd");
424 * Don't go on error here since the snapshot was successful at this
425 * point but somehow the close failed.
428 metadata_stream
->out_fd
= -1;
434 cds_list_del(&metadata_stream
->send_node
);
435 consumer_stream_destroy(metadata_stream
, NULL
);
436 metadata_channel
->metadata_stream
= NULL
;
443 * Receive command from session daemon and process it.
445 * Return 1 on success else a negative value or 0.
447 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
448 int sock
, struct pollfd
*consumer_sockpoll
)
451 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
452 struct lttcomm_consumer_msg msg
;
454 health_code_update();
456 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
457 if (ret
!= sizeof(msg
)) {
459 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
465 health_code_update();
467 /* Deprecated command */
468 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
470 health_code_update();
472 /* relayd needs RCU read-side protection */
475 switch (msg
.cmd_type
) {
476 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
478 /* Session daemon status message are handled in the following call. */
479 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
480 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
481 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
482 msg
.u
.relayd_sock
.relayd_session_id
);
485 case LTTNG_CONSUMER_ADD_CHANNEL
:
487 struct lttng_consumer_channel
*new_channel
;
490 health_code_update();
492 /* First send a status message before receiving the fds. */
493 ret
= consumer_send_status_msg(sock
, ret_code
);
495 /* Somehow, the session daemon is not responding anymore. */
499 health_code_update();
501 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
502 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
503 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
504 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
505 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
506 msg
.u
.channel
.tracefile_size
,
507 msg
.u
.channel
.tracefile_count
, 0,
508 msg
.u
.channel
.monitor
,
509 msg
.u
.channel
.live_timer_interval
,
511 if (new_channel
== NULL
) {
512 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
515 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
516 switch (msg
.u
.channel
.output
) {
517 case LTTNG_EVENT_SPLICE
:
518 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
520 case LTTNG_EVENT_MMAP
:
521 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
524 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
528 /* Translate and save channel type. */
529 switch (msg
.u
.channel
.type
) {
530 case CONSUMER_CHANNEL_TYPE_DATA
:
531 case CONSUMER_CHANNEL_TYPE_METADATA
:
532 new_channel
->type
= msg
.u
.channel
.type
;
539 health_code_update();
541 if (ctx
->on_recv_channel
!= NULL
) {
542 ret_recv
= ctx
->on_recv_channel(new_channel
);
544 ret
= consumer_add_channel(new_channel
, ctx
);
545 } else if (ret_recv
< 0) {
549 ret
= consumer_add_channel(new_channel
, ctx
);
551 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
552 int monitor_start_ret
;
554 DBG("Consumer starting monitor timer");
555 consumer_timer_live_start(new_channel
,
556 msg
.u
.channel
.live_timer_interval
);
557 monitor_start_ret
= consumer_timer_monitor_start(
559 msg
.u
.channel
.monitor_timer_interval
);
560 if (monitor_start_ret
< 0) {
561 ERR("Starting channel monitoring timer failed");
567 health_code_update();
569 /* If we received an error in add_channel, we need to report it. */
571 ret
= consumer_send_status_msg(sock
, ret
);
580 case LTTNG_CONSUMER_ADD_STREAM
:
583 struct lttng_pipe
*stream_pipe
;
584 struct lttng_consumer_stream
*new_stream
;
585 struct lttng_consumer_channel
*channel
;
589 * Get stream's channel reference. Needed when adding the stream to the
592 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
595 * We could not find the channel. Can happen if cpu hotplug
596 * happens while tearing down.
598 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
599 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
602 health_code_update();
604 /* First send a status message before receiving the fds. */
605 ret
= consumer_send_status_msg(sock
, ret_code
);
607 /* Somehow, the session daemon is not responding anymore. */
611 health_code_update();
613 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
614 /* Channel was not found. */
620 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
626 health_code_update();
628 /* Get stream file descriptor from socket */
629 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
630 if (ret
!= sizeof(fd
)) {
631 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
636 health_code_update();
639 * Send status code to session daemon only if the recv works. If the
640 * above recv() failed, the session daemon is notified through the
641 * error socket and the teardown is eventually done.
643 ret
= consumer_send_status_msg(sock
, ret_code
);
645 /* Somehow, the session daemon is not responding anymore. */
649 health_code_update();
651 new_stream
= consumer_allocate_stream(channel
->key
,
653 LTTNG_CONSUMER_ACTIVE_STREAM
,
663 if (new_stream
== NULL
) {
668 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
674 new_stream
->chan
= channel
;
675 new_stream
->wait_fd
= fd
;
676 switch (channel
->output
) {
677 case CONSUMER_CHANNEL_SPLICE
:
678 new_stream
->output
= LTTNG_EVENT_SPLICE
;
679 ret
= utils_create_pipe(new_stream
->splice_pipe
);
684 case CONSUMER_CHANNEL_MMAP
:
685 new_stream
->output
= LTTNG_EVENT_MMAP
;
688 ERR("Stream output unknown %d", channel
->output
);
693 * We've just assigned the channel to the stream so increment the
694 * refcount right now. We don't need to increment the refcount for
695 * streams in no monitor because we handle manually the cleanup of
696 * those. It is very important to make sure there is NO prior
697 * consumer_del_stream() calls or else the refcount will be unbalanced.
699 if (channel
->monitor
) {
700 uatomic_inc(&new_stream
->chan
->refcount
);
704 * The buffer flush is done on the session daemon side for the kernel
705 * so no need for the stream "hangup_flush_done" variable to be
706 * tracked. This is important for a kernel stream since we don't rely
707 * on the flush state of the stream to read data. It's not the case for
708 * user space tracing.
710 new_stream
->hangup_flush_done
= 0;
712 health_code_update();
714 if (ctx
->on_recv_stream
) {
715 ret
= ctx
->on_recv_stream(new_stream
);
717 consumer_stream_free(new_stream
);
722 health_code_update();
724 if (new_stream
->metadata_flag
) {
725 channel
->metadata_stream
= new_stream
;
728 /* Do not monitor this stream. */
729 if (!channel
->monitor
) {
730 DBG("Kernel consumer add stream %s in no monitor mode with "
731 "relayd id %" PRIu64
, new_stream
->name
,
732 new_stream
->net_seq_idx
);
733 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
737 /* Send stream to relayd if the stream has an ID. */
738 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
739 ret
= consumer_send_relayd_stream(new_stream
,
740 new_stream
->chan
->pathname
);
742 consumer_stream_free(new_stream
);
747 * If adding an extra stream to an already
748 * existing channel (e.g. cpu hotplug), we need
749 * to send the "streams_sent" command to relayd.
751 if (channel
->streams_sent_to_relayd
) {
752 ret
= consumer_send_relayd_streams_sent(
753 new_stream
->net_seq_idx
);
760 /* Get the right pipe where the stream will be sent. */
761 if (new_stream
->metadata_flag
) {
762 ret
= consumer_add_metadata_stream(new_stream
);
764 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
766 consumer_stream_free(new_stream
);
769 stream_pipe
= ctx
->consumer_metadata_pipe
;
771 ret
= consumer_add_data_stream(new_stream
);
773 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
775 consumer_stream_free(new_stream
);
778 stream_pipe
= ctx
->consumer_data_pipe
;
781 /* Vitible to other threads */
782 new_stream
->globally_visible
= 1;
784 health_code_update();
786 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
788 ERR("Consumer write %s stream to pipe %d",
789 new_stream
->metadata_flag
? "metadata" : "data",
790 lttng_pipe_get_writefd(stream_pipe
));
791 if (new_stream
->metadata_flag
) {
792 consumer_del_stream_for_metadata(new_stream
);
794 consumer_del_stream_for_data(new_stream
);
799 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
800 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
803 case LTTNG_CONSUMER_STREAMS_SENT
:
805 struct lttng_consumer_channel
*channel
;
808 * Get stream's channel reference. Needed when adding the stream to the
811 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
814 * We could not find the channel. Can happen if cpu hotplug
815 * happens while tearing down.
817 ERR("Unable to find channel key %" PRIu64
,
818 msg
.u
.sent_streams
.channel_key
);
819 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
822 health_code_update();
825 * Send status code to session daemon.
827 ret
= consumer_send_status_msg(sock
, ret_code
);
828 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
829 /* Somehow, the session daemon is not responding anymore. */
833 health_code_update();
836 * We should not send this message if we don't monitor the
837 * streams in this channel.
839 if (!channel
->monitor
) {
843 health_code_update();
844 /* Send stream to relayd if the stream has an ID. */
845 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
846 ret
= consumer_send_relayd_streams_sent(
847 msg
.u
.sent_streams
.net_seq_idx
);
851 channel
->streams_sent_to_relayd
= true;
855 case LTTNG_CONSUMER_UPDATE_STREAM
:
860 case LTTNG_CONSUMER_DESTROY_RELAYD
:
862 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
863 struct consumer_relayd_sock_pair
*relayd
;
865 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
867 /* Get relayd reference if exists. */
868 relayd
= consumer_find_relayd(index
);
869 if (relayd
== NULL
) {
870 DBG("Unable to find relayd %" PRIu64
, index
);
871 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
875 * Each relayd socket pair has a refcount of stream attached to it
876 * which tells if the relayd is still active or not depending on the
879 * This will set the destroy flag of the relayd object and destroy it
880 * if the refcount reaches zero when called.
882 * The destroy can happen either here or when a stream fd hangs up.
885 consumer_flag_relayd_for_destroy(relayd
);
888 health_code_update();
890 ret
= consumer_send_status_msg(sock
, ret_code
);
892 /* Somehow, the session daemon is not responding anymore. */
898 case LTTNG_CONSUMER_DATA_PENDING
:
901 uint64_t id
= msg
.u
.data_pending
.session_id
;
903 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
905 ret
= consumer_data_pending(id
);
907 health_code_update();
909 /* Send back returned value to session daemon */
910 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
912 PERROR("send data pending ret code");
917 * No need to send back a status message since the data pending
918 * returned value is the response.
922 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
924 if (msg
.u
.snapshot_channel
.metadata
== 1) {
925 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
926 msg
.u
.snapshot_channel
.pathname
,
927 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
929 ERR("Snapshot metadata failed");
930 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
933 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
934 msg
.u
.snapshot_channel
.pathname
,
935 msg
.u
.snapshot_channel
.relayd_id
,
936 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
939 ERR("Snapshot channel failed");
940 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
944 health_code_update();
946 ret
= consumer_send_status_msg(sock
, ret_code
);
948 /* Somehow, the session daemon is not responding anymore. */
953 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
955 uint64_t key
= msg
.u
.destroy_channel
.key
;
956 struct lttng_consumer_channel
*channel
;
958 channel
= consumer_find_channel(key
);
960 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
961 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
964 health_code_update();
966 ret
= consumer_send_status_msg(sock
, ret_code
);
968 /* Somehow, the session daemon is not responding anymore. */
972 health_code_update();
974 /* Stop right now if no channel was found. */
980 * This command should ONLY be issued for channel with streams set in
983 assert(!channel
->monitor
);
986 * The refcount should ALWAYS be 0 in the case of a channel in no
989 assert(!uatomic_sub_return(&channel
->refcount
, 1));
991 consumer_del_channel(channel
);
995 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
998 struct lttng_consumer_channel
*channel
;
999 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1000 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1002 DBG("Kernel consumer discarded events command for session id %"
1003 PRIu64
", channel key %" PRIu64
, id
, key
);
1005 channel
= consumer_find_channel(key
);
1007 ERR("Kernel consumer discarded events channel %"
1008 PRIu64
" not found", key
);
1011 ret
= channel
->discarded_events
;
1014 health_code_update();
1016 /* Send back returned value to session daemon */
1017 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1019 PERROR("send discarded events");
1025 case LTTNG_CONSUMER_LOST_PACKETS
:
1028 struct lttng_consumer_channel
*channel
;
1029 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1030 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1032 DBG("Kernel consumer lost packets command for session id %"
1033 PRIu64
", channel key %" PRIu64
, id
, key
);
1035 channel
= consumer_find_channel(key
);
1037 ERR("Kernel consumer lost packets channel %"
1038 PRIu64
" not found", key
);
1041 ret
= channel
->lost_packets
;
1044 health_code_update();
1046 /* Send back returned value to session daemon */
1047 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1049 PERROR("send lost packets");
1055 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1057 int channel_monitor_pipe
;
1059 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1060 /* Successfully received the command's type. */
1061 ret
= consumer_send_status_msg(sock
, ret_code
);
1066 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1068 if (ret
!= sizeof(channel_monitor_pipe
)) {
1069 ERR("Failed to receive channel monitor pipe");
1073 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1074 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1075 channel_monitor_pipe
);
1079 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1080 /* Set the pipe as non-blocking. */
1081 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1083 PERROR("fcntl get flags of the channel monitoring pipe");
1088 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1089 flags
| O_NONBLOCK
);
1091 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1094 DBG("Channel monitor pipe set as non-blocking");
1096 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1098 ret
= consumer_send_status_msg(sock
, ret_code
);
1112 * Return 1 to indicate success since the 0 value can be a socket
1113 * shutdown during the recv() or send() call.
1115 health_code_update();
1120 /* This will issue a consumer stop. */
1125 * Populate index values of a kernel stream. Values are set in big endian order.
1127 * Return 0 on success or else a negative value.
1129 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1133 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1135 PERROR("kernctl_get_timestamp_begin");
1138 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1140 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1142 PERROR("kernctl_get_timestamp_end");
1145 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1147 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1149 PERROR("kernctl_get_events_discarded");
1152 index
->events_discarded
= htobe64(index
->events_discarded
);
1154 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1156 PERROR("kernctl_get_content_size");
1159 index
->content_size
= htobe64(index
->content_size
);
1161 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1163 PERROR("kernctl_get_packet_size");
1166 index
->packet_size
= htobe64(index
->packet_size
);
1168 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1170 PERROR("kernctl_get_stream_id");
1173 index
->stream_id
= htobe64(index
->stream_id
);
1175 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1177 if (ret
== -ENOTTY
) {
1178 /* Command not implemented by lttng-modules. */
1179 index
->stream_instance_id
= -1ULL;
1182 PERROR("kernctl_get_instance_id");
1186 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1188 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1190 if (ret
== -ENOTTY
) {
1191 /* Command not implemented by lttng-modules. */
1192 index
->packet_seq_num
= -1ULL;
1195 PERROR("kernctl_get_sequence_number");
1199 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1205 * Sync metadata meaning request them to the session daemon and snapshot to the
1206 * metadata thread can consumer them.
1208 * Metadata stream lock MUST be acquired.
1210 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1211 * is empty or a negative value on error.
1213 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1219 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1221 ERR("Failed to flush kernel stream");
1225 ret
= kernctl_snapshot(metadata
->wait_fd
);
1227 if (ret
!= -EAGAIN
) {
1228 ERR("Sync metadata, taking kernel snapshot failed.");
1231 DBG("Sync metadata, no new kernel metadata");
1232 /* No new metadata, exit. */
1242 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1245 uint64_t seq
, discarded
;
1247 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1249 if (ret
== -ENOTTY
) {
1250 /* Command not implemented by lttng-modules. */
1254 PERROR("kernctl_get_sequence_number");
1260 * Start the sequence when we extract the first packet in case we don't
1261 * start at 0 (for example if a consumer is not connected to the
1262 * session immediately after the beginning).
1264 if (stream
->last_sequence_number
== -1ULL) {
1265 stream
->last_sequence_number
= seq
;
1266 } else if (seq
> stream
->last_sequence_number
) {
1267 stream
->chan
->lost_packets
+= seq
-
1268 stream
->last_sequence_number
- 1;
1270 /* seq <= last_sequence_number */
1271 ERR("Sequence number inconsistent : prev = %" PRIu64
1272 ", current = %" PRIu64
,
1273 stream
->last_sequence_number
, seq
);
1277 stream
->last_sequence_number
= seq
;
1279 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1281 PERROR("kernctl_get_events_discarded");
1284 if (discarded
< stream
->last_discarded_events
) {
1286 * Overflow has occurred. We assume only one wrap-around
1289 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1290 stream
->last_discarded_events
+ discarded
;
1292 stream
->chan
->discarded_events
+= discarded
-
1293 stream
->last_discarded_events
;
1295 stream
->last_discarded_events
= discarded
;
1303 * Check if the local version of the metadata stream matches with the version
1304 * of the metadata stream in the kernel. If it was updated, set the reset flag
1308 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1311 uint64_t cur_version
;
1313 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1315 if (ret
== -ENOTTY
) {
1317 * LTTng-modules does not implement this
1323 ERR("Failed to get the metadata version");
1327 if (stream
->metadata_version
== cur_version
) {
1332 DBG("New metadata version detected");
1333 stream
->metadata_version
= cur_version
;
1334 stream
->reset_metadata_flag
= 1;
1342 * Consume data on a file descriptor and write it on a trace file.
1344 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1345 struct lttng_consumer_local_data
*ctx
)
1347 unsigned long len
, subbuf_size
, padding
;
1348 int err
, write_index
= 1;
1350 int infd
= stream
->wait_fd
;
1351 struct ctf_packet_index index
;
1353 DBG("In read_subbuffer (infd : %d)", infd
);
1355 /* Get the next subbuffer */
1356 err
= kernctl_get_next_subbuf(infd
);
1359 * This is a debug message even for single-threaded consumer,
1360 * because poll() have more relaxed criterions than get subbuf,
1361 * so get_subbuf may fail for short race windows where poll()
1362 * would issue wakeups.
1364 DBG("Reserving sub buffer failed (everything is normal, "
1365 "it is due to concurrency)");
1370 /* Get the full subbuffer size including padding */
1371 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1373 PERROR("Getting sub-buffer len failed.");
1374 err
= kernctl_put_subbuf(infd
);
1376 if (err
== -EFAULT
) {
1377 PERROR("Error in unreserving sub buffer\n");
1378 } else if (err
== -EIO
) {
1379 /* Should never happen with newer LTTng versions */
1380 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1389 if (!stream
->metadata_flag
) {
1390 ret
= get_index_values(&index
, infd
);
1392 err
= kernctl_put_subbuf(infd
);
1394 if (err
== -EFAULT
) {
1395 PERROR("Error in unreserving sub buffer\n");
1396 } else if (err
== -EIO
) {
1397 /* Should never happen with newer LTTng versions */
1398 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1405 ret
= update_stream_stats(stream
);
1407 err
= kernctl_put_subbuf(infd
);
1409 if (err
== -EFAULT
) {
1410 PERROR("Error in unreserving sub buffer\n");
1411 } else if (err
== -EIO
) {
1412 /* Should never happen with newer LTTng versions */
1413 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1422 ret
= metadata_stream_check_version(infd
, 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 switch (stream
->chan
->output
) {
1440 case CONSUMER_CHANNEL_SPLICE
:
1442 * XXX: The lttng-modules splice "actor" does not handle copying
1443 * partial pages hence only using the subbuffer size without the
1444 * padding makes the splice fail.
1449 /* splice the subbuffer to the tracefile */
1450 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1453 * XXX: Splice does not support network streaming so the return value
1454 * is simply checked against subbuf_size and not like the mmap() op.
1456 if (ret
!= subbuf_size
) {
1458 * display the error but continue processing to try
1459 * to release the subbuffer
1461 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1466 case CONSUMER_CHANNEL_MMAP
:
1467 /* Get subbuffer size without padding */
1468 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1470 PERROR("Getting sub-buffer len failed.");
1471 err
= kernctl_put_subbuf(infd
);
1473 if (err
== -EFAULT
) {
1474 PERROR("Error in unreserving sub buffer\n");
1475 } else if (err
== -EIO
) {
1476 /* Should never happen with newer LTTng versions */
1477 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1486 /* Make sure the tracer is not gone mad on us! */
1487 assert(len
>= subbuf_size
);
1489 padding
= len
- subbuf_size
;
1491 /* write the subbuffer to the tracefile */
1492 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1495 * The mmap operation should write subbuf_size amount of data when
1496 * network streaming or the full padding (len) size when we are _not_
1499 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1500 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1502 * Display the error but continue processing to try to release the
1503 * subbuffer. This is a DBG statement since this is possible to
1504 * happen without being a critical error.
1506 DBG("Error writing to tracefile "
1507 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1508 ret
, len
, subbuf_size
);
1513 ERR("Unknown output method");
1517 err
= kernctl_put_next_subbuf(infd
);
1519 if (err
== -EFAULT
) {
1520 PERROR("Error in unreserving sub buffer\n");
1521 } else if (err
== -EIO
) {
1522 /* Should never happen with newer LTTng versions */
1523 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1529 /* Write index if needed. */
1534 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1536 * In live, block until all the metadata is sent.
1538 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1539 assert(!stream
->missed_metadata_flush
);
1540 stream
->waiting_on_metadata
= true;
1541 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1543 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1545 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1546 stream
->waiting_on_metadata
= false;
1547 if (stream
->missed_metadata_flush
) {
1548 stream
->missed_metadata_flush
= false;
1549 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1550 (void) consumer_flush_kernel_index(stream
);
1552 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1559 err
= consumer_stream_write_index(stream
, &index
);
1568 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1575 * Don't create anything if this is set for streaming or should not be
1578 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1579 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1580 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1581 stream
->uid
, stream
->gid
, NULL
);
1585 stream
->out_fd
= ret
;
1586 stream
->tracefile_size_current
= 0;
1588 if (!stream
->metadata_flag
) {
1589 struct lttng_index_file
*index_file
;
1591 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1592 stream
->name
, stream
->uid
, stream
->gid
,
1593 stream
->chan
->tracefile_size
,
1594 stream
->tracefile_count_current
,
1595 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1599 assert(!stream
->index_file
);
1600 stream
->index_file
= index_file
;
1604 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1605 /* get the len of the mmap region */
1606 unsigned long mmap_len
;
1608 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1610 PERROR("kernctl_get_mmap_len");
1611 goto error_close_fd
;
1613 stream
->mmap_len
= (size_t) mmap_len
;
1615 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1616 MAP_PRIVATE
, stream
->wait_fd
, 0);
1617 if (stream
->mmap_base
== MAP_FAILED
) {
1618 PERROR("Error mmaping");
1620 goto error_close_fd
;
1624 /* we return 0 to let the library handle the FD internally */
1628 if (stream
->out_fd
>= 0) {
1631 err
= close(stream
->out_fd
);
1633 stream
->out_fd
= -1;
1640 * Check if data is still being extracted from the buffers for a specific
1641 * stream. Consumer data lock MUST be acquired before calling this function
1642 * and the stream lock.
1644 * Return 1 if the traced data are still getting read else 0 meaning that the
1645 * data is available for trace viewer reading.
1647 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1653 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1658 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1660 /* There is still data so let's put back this subbuffer. */
1661 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1663 ret
= 1; /* Data is pending */
1667 /* Data is NOT pending and ready to be read. */