2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
46 #include "kernel-consumer.h"
48 extern struct lttng_consumer_global_data consumer_data
;
49 extern int consumer_poll_timeout
;
50 extern volatile int consumer_quit
;
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 * Get the produced position
73 * Returns 0 on success, < 0 on error
75 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
79 int infd
= stream
->wait_fd
;
81 ret
= kernctl_snapshot_get_produced(infd
, pos
);
83 PERROR("kernctl_snapshot_get_produced");
90 * Get the consumerd position
92 * Returns 0 on success, < 0 on error
94 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
98 int infd
= stream
->wait_fd
;
100 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
102 PERROR("kernctl_snapshot_get_consumed");
109 * Take a snapshot of all the stream of a channel
111 * Returns 0 on success, < 0 on error
113 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
114 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
115 struct lttng_consumer_local_data
*ctx
)
118 struct lttng_consumer_channel
*channel
;
119 struct lttng_consumer_stream
*stream
;
121 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
125 channel
= consumer_find_channel(key
);
127 ERR("No channel found for key %" PRIu64
, key
);
132 /* Splice is not supported yet for channel snapshot. */
133 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
134 ERR("Unsupported output %d", channel
->output
);
139 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
140 unsigned long consumed_pos
, produced_pos
;
142 health_code_update();
145 * Lock stream because we are about to change its state.
147 pthread_mutex_lock(&stream
->lock
);
150 * Assign the received relayd ID so we can use it for streaming. The streams
151 * are not visible to anyone so this is OK to change it.
153 stream
->net_seq_idx
= relayd_id
;
154 channel
->relayd_id
= relayd_id
;
155 if (relayd_id
!= (uint64_t) -1ULL) {
156 ret
= consumer_send_relayd_stream(stream
, path
);
158 ERR("sending stream to relayd");
162 ret
= utils_create_stream_file(path
, stream
->name
,
163 stream
->chan
->tracefile_size
,
164 stream
->tracefile_count_current
,
165 stream
->uid
, stream
->gid
, NULL
);
167 ERR("utils_create_stream_file");
171 stream
->out_fd
= ret
;
172 stream
->tracefile_size_current
= 0;
174 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
175 path
, stream
->name
, stream
->key
);
177 if (relayd_id
!= -1ULL) {
178 ret
= consumer_send_relayd_streams_sent(relayd_id
);
180 ERR("sending streams sent to relayd");
183 channel
->streams_sent_to_relayd
= true;
186 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
189 * Doing a buffer flush which does not take into
190 * account empty packets. This is not perfect
191 * for stream intersection, but required as a
192 * fall-back when "flush_empty" is not
193 * implemented by lttng-modules.
195 ret
= kernctl_buffer_flush(stream
->wait_fd
);
197 ERR("Failed to flush kernel stream");
203 ret
= lttng_kconsumer_take_snapshot(stream
);
205 ERR("Taking kernel snapshot");
209 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
211 ERR("Produced kernel snapshot position");
215 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
217 ERR("Consumerd kernel snapshot position");
221 if (stream
->max_sb_size
== 0) {
222 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
223 &stream
->max_sb_size
);
225 ERR("Getting kernel max_sb_size");
230 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
231 produced_pos
, nb_packets_per_stream
,
232 stream
->max_sb_size
);
234 while (consumed_pos
< produced_pos
) {
236 unsigned long len
, padded_len
;
238 health_code_update();
240 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
242 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
244 if (ret
!= -EAGAIN
) {
245 PERROR("kernctl_get_subbuf snapshot");
248 DBG("Kernel consumer get subbuf failed. Skipping it.");
249 consumed_pos
+= stream
->max_sb_size
;
250 stream
->chan
->lost_packets
++;
254 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
256 ERR("Snapshot kernctl_get_subbuf_size");
257 goto error_put_subbuf
;
260 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
262 ERR("Snapshot kernctl_get_padded_subbuf_size");
263 goto error_put_subbuf
;
266 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
267 padded_len
- len
, NULL
);
269 * We write the padded len in local tracefiles but the data len
270 * when using a relay. Display the error but continue processing
271 * to try to release the subbuffer.
273 if (relayd_id
!= (uint64_t) -1ULL) {
274 if (read_len
!= len
) {
275 ERR("Error sending to the relay (ret: %zd != len: %lu)",
279 if (read_len
!= padded_len
) {
280 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
281 read_len
, padded_len
);
285 ret
= kernctl_put_subbuf(stream
->wait_fd
);
287 ERR("Snapshot kernctl_put_subbuf");
290 consumed_pos
+= stream
->max_sb_size
;
293 if (relayd_id
== (uint64_t) -1ULL) {
294 if (stream
->out_fd
>= 0) {
295 ret
= close(stream
->out_fd
);
297 PERROR("Kernel consumer snapshot close out_fd");
303 close_relayd_stream(stream
);
304 stream
->net_seq_idx
= (uint64_t) -1ULL;
306 pthread_mutex_unlock(&stream
->lock
);
314 ret
= kernctl_put_subbuf(stream
->wait_fd
);
316 ERR("Snapshot kernctl_put_subbuf error path");
319 pthread_mutex_unlock(&stream
->lock
);
326 * Read the whole metadata available for a snapshot.
328 * Returns 0 on success, < 0 on error
330 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
331 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
333 int ret
, use_relayd
= 0;
335 struct lttng_consumer_channel
*metadata_channel
;
336 struct lttng_consumer_stream
*metadata_stream
;
340 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
345 metadata_channel
= consumer_find_channel(key
);
346 if (!metadata_channel
) {
347 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
352 metadata_stream
= metadata_channel
->metadata_stream
;
353 assert(metadata_stream
);
355 /* Flag once that we have a valid relayd for the stream. */
356 if (relayd_id
!= (uint64_t) -1ULL) {
361 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
366 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
367 metadata_stream
->chan
->tracefile_size
,
368 metadata_stream
->tracefile_count_current
,
369 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
373 metadata_stream
->out_fd
= ret
;
377 health_code_update();
379 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
381 if (ret_read
!= -EAGAIN
) {
382 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
386 /* ret_read is negative at this point so we will exit the loop. */
389 } while (ret_read
>= 0);
392 close_relayd_stream(metadata_stream
);
393 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
395 if (metadata_stream
->out_fd
>= 0) {
396 ret
= close(metadata_stream
->out_fd
);
398 PERROR("Kernel consumer snapshot metadata close out_fd");
400 * Don't go on error here since the snapshot was successful at this
401 * point but somehow the close failed.
404 metadata_stream
->out_fd
= -1;
410 cds_list_del(&metadata_stream
->send_node
);
411 consumer_stream_destroy(metadata_stream
, NULL
);
412 metadata_channel
->metadata_stream
= NULL
;
419 * Receive command from session daemon and process it.
421 * Return 1 on success else a negative value or 0.
423 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
424 int sock
, struct pollfd
*consumer_sockpoll
)
427 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
428 struct lttcomm_consumer_msg msg
;
430 health_code_update();
432 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
433 if (ret
!= sizeof(msg
)) {
435 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
441 health_code_update();
443 /* Deprecated command */
444 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
446 health_code_update();
448 /* relayd needs RCU read-side protection */
451 switch (msg
.cmd_type
) {
452 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
454 /* Session daemon status message are handled in the following call. */
455 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
456 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
457 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
458 msg
.u
.relayd_sock
.relayd_session_id
);
461 case LTTNG_CONSUMER_ADD_CHANNEL
:
463 struct lttng_consumer_channel
*new_channel
;
466 health_code_update();
468 /* First send a status message before receiving the fds. */
469 ret
= consumer_send_status_msg(sock
, ret_code
);
471 /* Somehow, the session daemon is not responding anymore. */
475 health_code_update();
477 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
478 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
479 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
480 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
481 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
482 msg
.u
.channel
.tracefile_size
,
483 msg
.u
.channel
.tracefile_count
, 0,
484 msg
.u
.channel
.monitor
,
485 msg
.u
.channel
.live_timer_interval
,
487 if (new_channel
== NULL
) {
488 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
491 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
492 switch (msg
.u
.channel
.output
) {
493 case LTTNG_EVENT_SPLICE
:
494 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
496 case LTTNG_EVENT_MMAP
:
497 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
500 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
504 /* Translate and save channel type. */
505 switch (msg
.u
.channel
.type
) {
506 case CONSUMER_CHANNEL_TYPE_DATA
:
507 case CONSUMER_CHANNEL_TYPE_METADATA
:
508 new_channel
->type
= msg
.u
.channel
.type
;
515 health_code_update();
517 if (ctx
->on_recv_channel
!= NULL
) {
518 ret_recv
= ctx
->on_recv_channel(new_channel
);
520 ret
= consumer_add_channel(new_channel
, ctx
);
521 } else if (ret_recv
< 0) {
525 ret
= consumer_add_channel(new_channel
, ctx
);
527 if (CONSUMER_CHANNEL_TYPE_DATA
) {
528 consumer_timer_live_start(new_channel
,
529 msg
.u
.channel
.live_timer_interval
);
532 health_code_update();
534 /* If we received an error in add_channel, we need to report it. */
536 ret
= consumer_send_status_msg(sock
, ret
);
545 case LTTNG_CONSUMER_ADD_STREAM
:
548 struct lttng_pipe
*stream_pipe
;
549 struct lttng_consumer_stream
*new_stream
;
550 struct lttng_consumer_channel
*channel
;
554 * Get stream's channel reference. Needed when adding the stream to the
557 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
560 * We could not find the channel. Can happen if cpu hotplug
561 * happens while tearing down.
563 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
564 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
567 health_code_update();
569 /* First send a status message before receiving the fds. */
570 ret
= consumer_send_status_msg(sock
, ret_code
);
572 /* Somehow, the session daemon is not responding anymore. */
576 health_code_update();
578 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
579 /* Channel was not found. */
585 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
591 health_code_update();
593 /* Get stream file descriptor from socket */
594 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
595 if (ret
!= sizeof(fd
)) {
596 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
601 health_code_update();
604 * Send status code to session daemon only if the recv works. If the
605 * above recv() failed, the session daemon is notified through the
606 * error socket and the teardown is eventually done.
608 ret
= consumer_send_status_msg(sock
, ret_code
);
610 /* Somehow, the session daemon is not responding anymore. */
614 health_code_update();
616 new_stream
= consumer_allocate_stream(channel
->key
,
618 LTTNG_CONSUMER_ACTIVE_STREAM
,
628 if (new_stream
== NULL
) {
633 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
639 new_stream
->chan
= channel
;
640 new_stream
->wait_fd
= fd
;
641 switch (channel
->output
) {
642 case CONSUMER_CHANNEL_SPLICE
:
643 new_stream
->output
= LTTNG_EVENT_SPLICE
;
644 ret
= utils_create_pipe(new_stream
->splice_pipe
);
649 case CONSUMER_CHANNEL_MMAP
:
650 new_stream
->output
= LTTNG_EVENT_MMAP
;
653 ERR("Stream output unknown %d", channel
->output
);
658 * We've just assigned the channel to the stream so increment the
659 * refcount right now. We don't need to increment the refcount for
660 * streams in no monitor because we handle manually the cleanup of
661 * those. It is very important to make sure there is NO prior
662 * consumer_del_stream() calls or else the refcount will be unbalanced.
664 if (channel
->monitor
) {
665 uatomic_inc(&new_stream
->chan
->refcount
);
669 * The buffer flush is done on the session daemon side for the kernel
670 * so no need for the stream "hangup_flush_done" variable to be
671 * tracked. This is important for a kernel stream since we don't rely
672 * on the flush state of the stream to read data. It's not the case for
673 * user space tracing.
675 new_stream
->hangup_flush_done
= 0;
677 health_code_update();
679 if (ctx
->on_recv_stream
) {
680 ret
= ctx
->on_recv_stream(new_stream
);
682 consumer_stream_free(new_stream
);
687 health_code_update();
689 if (new_stream
->metadata_flag
) {
690 channel
->metadata_stream
= new_stream
;
693 /* Do not monitor this stream. */
694 if (!channel
->monitor
) {
695 DBG("Kernel consumer add stream %s in no monitor mode with "
696 "relayd id %" PRIu64
, new_stream
->name
,
697 new_stream
->net_seq_idx
);
698 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
702 /* Send stream to relayd if the stream has an ID. */
703 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
704 ret
= consumer_send_relayd_stream(new_stream
,
705 new_stream
->chan
->pathname
);
707 consumer_stream_free(new_stream
);
712 * If adding an extra stream to an already
713 * existing channel (e.g. cpu hotplug), we need
714 * to send the "streams_sent" command to relayd.
716 if (channel
->streams_sent_to_relayd
) {
717 ret
= consumer_send_relayd_streams_sent(
718 new_stream
->net_seq_idx
);
725 /* Get the right pipe where the stream will be sent. */
726 if (new_stream
->metadata_flag
) {
727 ret
= consumer_add_metadata_stream(new_stream
);
729 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
731 consumer_stream_free(new_stream
);
734 stream_pipe
= ctx
->consumer_metadata_pipe
;
736 ret
= consumer_add_data_stream(new_stream
);
738 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
740 consumer_stream_free(new_stream
);
743 stream_pipe
= ctx
->consumer_data_pipe
;
746 /* Vitible to other threads */
747 new_stream
->globally_visible
= 1;
749 health_code_update();
751 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
753 ERR("Consumer write %s stream to pipe %d",
754 new_stream
->metadata_flag
? "metadata" : "data",
755 lttng_pipe_get_writefd(stream_pipe
));
756 if (new_stream
->metadata_flag
) {
757 consumer_del_stream_for_metadata(new_stream
);
759 consumer_del_stream_for_data(new_stream
);
764 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
765 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
768 case LTTNG_CONSUMER_STREAMS_SENT
:
770 struct lttng_consumer_channel
*channel
;
773 * Get stream's channel reference. Needed when adding the stream to the
776 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
779 * We could not find the channel. Can happen if cpu hotplug
780 * happens while tearing down.
782 ERR("Unable to find channel key %" PRIu64
,
783 msg
.u
.sent_streams
.channel_key
);
784 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
787 health_code_update();
790 * Send status code to session daemon.
792 ret
= consumer_send_status_msg(sock
, ret_code
);
793 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
794 /* Somehow, the session daemon is not responding anymore. */
798 health_code_update();
801 * We should not send this message if we don't monitor the
802 * streams in this channel.
804 if (!channel
->monitor
) {
808 health_code_update();
809 /* Send stream to relayd if the stream has an ID. */
810 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
811 ret
= consumer_send_relayd_streams_sent(
812 msg
.u
.sent_streams
.net_seq_idx
);
816 channel
->streams_sent_to_relayd
= true;
820 case LTTNG_CONSUMER_UPDATE_STREAM
:
825 case LTTNG_CONSUMER_DESTROY_RELAYD
:
827 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
828 struct consumer_relayd_sock_pair
*relayd
;
830 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
832 /* Get relayd reference if exists. */
833 relayd
= consumer_find_relayd(index
);
834 if (relayd
== NULL
) {
835 DBG("Unable to find relayd %" PRIu64
, index
);
836 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
840 * Each relayd socket pair has a refcount of stream attached to it
841 * which tells if the relayd is still active or not depending on the
844 * This will set the destroy flag of the relayd object and destroy it
845 * if the refcount reaches zero when called.
847 * The destroy can happen either here or when a stream fd hangs up.
850 consumer_flag_relayd_for_destroy(relayd
);
853 health_code_update();
855 ret
= consumer_send_status_msg(sock
, ret_code
);
857 /* Somehow, the session daemon is not responding anymore. */
863 case LTTNG_CONSUMER_DATA_PENDING
:
866 uint64_t id
= msg
.u
.data_pending
.session_id
;
868 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
870 ret
= consumer_data_pending(id
);
872 health_code_update();
874 /* Send back returned value to session daemon */
875 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
877 PERROR("send data pending ret code");
882 * No need to send back a status message since the data pending
883 * returned value is the response.
887 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
889 if (msg
.u
.snapshot_channel
.metadata
== 1) {
890 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
891 msg
.u
.snapshot_channel
.pathname
,
892 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
894 ERR("Snapshot metadata failed");
895 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
898 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
899 msg
.u
.snapshot_channel
.pathname
,
900 msg
.u
.snapshot_channel
.relayd_id
,
901 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
904 ERR("Snapshot channel failed");
905 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
909 health_code_update();
911 ret
= consumer_send_status_msg(sock
, ret_code
);
913 /* Somehow, the session daemon is not responding anymore. */
918 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
920 uint64_t key
= msg
.u
.destroy_channel
.key
;
921 struct lttng_consumer_channel
*channel
;
923 channel
= consumer_find_channel(key
);
925 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
926 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
929 health_code_update();
931 ret
= consumer_send_status_msg(sock
, ret_code
);
933 /* Somehow, the session daemon is not responding anymore. */
937 health_code_update();
939 /* Stop right now if no channel was found. */
945 * This command should ONLY be issued for channel with streams set in
948 assert(!channel
->monitor
);
951 * The refcount should ALWAYS be 0 in the case of a channel in no
954 assert(!uatomic_sub_return(&channel
->refcount
, 1));
956 consumer_del_channel(channel
);
960 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
963 struct lttng_consumer_channel
*channel
;
964 uint64_t id
= msg
.u
.discarded_events
.session_id
;
965 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
967 DBG("Kernel consumer discarded events command for session id %"
968 PRIu64
", channel key %" PRIu64
, id
, key
);
970 channel
= consumer_find_channel(key
);
972 ERR("Kernel consumer discarded events channel %"
973 PRIu64
" not found", key
);
976 ret
= channel
->discarded_events
;
979 health_code_update();
981 /* Send back returned value to session daemon */
982 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
984 PERROR("send discarded events");
990 case LTTNG_CONSUMER_LOST_PACKETS
:
993 struct lttng_consumer_channel
*channel
;
994 uint64_t id
= msg
.u
.lost_packets
.session_id
;
995 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
997 DBG("Kernel consumer lost packets command for session id %"
998 PRIu64
", channel key %" PRIu64
, id
, key
);
1000 channel
= consumer_find_channel(key
);
1002 ERR("Kernel consumer lost packets channel %"
1003 PRIu64
" not found", key
);
1006 ret
= channel
->lost_packets
;
1009 health_code_update();
1011 /* Send back returned value to session daemon */
1012 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1014 PERROR("send lost packets");
1028 * Return 1 to indicate success since the 0 value can be a socket
1029 * shutdown during the recv() or send() call.
1031 health_code_update();
1036 /* This will issue a consumer stop. */
1041 * Populate index values of a kernel stream. Values are set in big endian order.
1043 * Return 0 on success or else a negative value.
1045 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1049 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1051 PERROR("kernctl_get_timestamp_begin");
1054 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1056 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1058 PERROR("kernctl_get_timestamp_end");
1061 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1063 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1065 PERROR("kernctl_get_events_discarded");
1068 index
->events_discarded
= htobe64(index
->events_discarded
);
1070 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1072 PERROR("kernctl_get_content_size");
1075 index
->content_size
= htobe64(index
->content_size
);
1077 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1079 PERROR("kernctl_get_packet_size");
1082 index
->packet_size
= htobe64(index
->packet_size
);
1084 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1086 PERROR("kernctl_get_stream_id");
1089 index
->stream_id
= htobe64(index
->stream_id
);
1091 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1093 if (ret
== -ENOTTY
) {
1094 /* Command not implemented by lttng-modules. */
1095 index
->stream_instance_id
= -1ULL;
1098 PERROR("kernctl_get_instance_id");
1102 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1104 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1106 if (ret
== -ENOTTY
) {
1107 /* Command not implemented by lttng-modules. */
1108 index
->packet_seq_num
= -1ULL;
1111 PERROR("kernctl_get_sequence_number");
1115 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1121 * Sync metadata meaning request them to the session daemon and snapshot to the
1122 * metadata thread can consumer them.
1124 * Metadata stream lock MUST be acquired.
1126 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1127 * is empty or a negative value on error.
1129 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1135 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1137 ERR("Failed to flush kernel stream");
1141 ret
= kernctl_snapshot(metadata
->wait_fd
);
1143 if (ret
!= -EAGAIN
) {
1144 ERR("Sync metadata, taking kernel snapshot failed.");
1147 DBG("Sync metadata, no new kernel metadata");
1148 /* No new metadata, exit. */
1158 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1161 uint64_t seq
, discarded
;
1163 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1165 if (ret
== -ENOTTY
) {
1166 /* Command not implemented by lttng-modules. */
1170 PERROR("kernctl_get_sequence_number");
1176 * Start the sequence when we extract the first packet in case we don't
1177 * start at 0 (for example if a consumer is not connected to the
1178 * session immediately after the beginning).
1180 if (stream
->last_sequence_number
== -1ULL) {
1181 stream
->last_sequence_number
= seq
;
1182 } else if (seq
> stream
->last_sequence_number
) {
1183 stream
->chan
->lost_packets
+= seq
-
1184 stream
->last_sequence_number
- 1;
1186 /* seq <= last_sequence_number */
1187 ERR("Sequence number inconsistent : prev = %" PRIu64
1188 ", current = %" PRIu64
,
1189 stream
->last_sequence_number
, seq
);
1193 stream
->last_sequence_number
= seq
;
1195 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1197 PERROR("kernctl_get_events_discarded");
1200 if (discarded
< stream
->last_discarded_events
) {
1202 * Overflow has occurred. We assume only one wrap-around
1205 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1206 stream
->last_discarded_events
+ discarded
;
1208 stream
->chan
->discarded_events
+= discarded
-
1209 stream
->last_discarded_events
;
1211 stream
->last_discarded_events
= discarded
;
1219 * Check if the local version of the metadata stream matches with the version
1220 * of the metadata stream in the kernel. If it was updated, set the reset flag
1224 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1227 uint64_t cur_version
;
1229 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1231 if (ret
== -ENOTTY
) {
1233 * LTTng-modules does not implement this
1239 ERR("Failed to get the metadata version");
1243 if (stream
->metadata_version
== cur_version
) {
1248 DBG("New metadata version detected");
1249 stream
->metadata_version
= cur_version
;
1250 stream
->reset_metadata_flag
= 1;
1258 * Consume data on a file descriptor and write it on a trace file.
1260 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1261 struct lttng_consumer_local_data
*ctx
)
1263 unsigned long len
, subbuf_size
, padding
;
1264 int err
, write_index
= 1;
1266 int infd
= stream
->wait_fd
;
1267 struct ctf_packet_index index
;
1269 DBG("In read_subbuffer (infd : %d)", infd
);
1271 /* Get the next subbuffer */
1272 err
= kernctl_get_next_subbuf(infd
);
1275 * This is a debug message even for single-threaded consumer,
1276 * because poll() have more relaxed criterions than get subbuf,
1277 * so get_subbuf may fail for short race windows where poll()
1278 * would issue wakeups.
1280 DBG("Reserving sub buffer failed (everything is normal, "
1281 "it is due to concurrency)");
1286 /* Get the full subbuffer size including padding */
1287 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1289 PERROR("Getting sub-buffer len failed.");
1290 err
= kernctl_put_subbuf(infd
);
1292 if (err
== -EFAULT
) {
1293 PERROR("Error in unreserving sub buffer\n");
1294 } else if (err
== -EIO
) {
1295 /* Should never happen with newer LTTng versions */
1296 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1305 if (!stream
->metadata_flag
) {
1306 ret
= get_index_values(&index
, infd
);
1308 err
= kernctl_put_subbuf(infd
);
1310 if (err
== -EFAULT
) {
1311 PERROR("Error in unreserving sub buffer\n");
1312 } else if (err
== -EIO
) {
1313 /* Should never happen with newer LTTng versions */
1314 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1321 ret
= update_stream_stats(stream
);
1323 err
= kernctl_put_subbuf(infd
);
1325 if (err
== -EFAULT
) {
1326 PERROR("Error in unreserving sub buffer\n");
1327 } else if (err
== -EIO
) {
1328 /* Should never happen with newer LTTng versions */
1329 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1338 ret
= metadata_stream_check_version(infd
, stream
);
1340 err
= kernctl_put_subbuf(infd
);
1342 if (err
== -EFAULT
) {
1343 PERROR("Error in unreserving sub buffer\n");
1344 } else if (err
== -EIO
) {
1345 /* Should never happen with newer LTTng versions */
1346 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1355 switch (stream
->chan
->output
) {
1356 case CONSUMER_CHANNEL_SPLICE
:
1358 * XXX: The lttng-modules splice "actor" does not handle copying
1359 * partial pages hence only using the subbuffer size without the
1360 * padding makes the splice fail.
1365 /* splice the subbuffer to the tracefile */
1366 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1369 * XXX: Splice does not support network streaming so the return value
1370 * is simply checked against subbuf_size and not like the mmap() op.
1372 if (ret
!= subbuf_size
) {
1374 * display the error but continue processing to try
1375 * to release the subbuffer
1377 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1382 case CONSUMER_CHANNEL_MMAP
:
1383 /* Get subbuffer size without padding */
1384 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1386 PERROR("Getting sub-buffer len failed.");
1387 err
= kernctl_put_subbuf(infd
);
1389 if (err
== -EFAULT
) {
1390 PERROR("Error in unreserving sub buffer\n");
1391 } else if (err
== -EIO
) {
1392 /* Should never happen with newer LTTng versions */
1393 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1402 /* Make sure the tracer is not gone mad on us! */
1403 assert(len
>= subbuf_size
);
1405 padding
= len
- subbuf_size
;
1407 /* write the subbuffer to the tracefile */
1408 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1411 * The mmap operation should write subbuf_size amount of data when
1412 * network streaming or the full padding (len) size when we are _not_
1415 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1416 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1418 * Display the error but continue processing to try to release the
1419 * subbuffer. This is a DBG statement since this is possible to
1420 * happen without being a critical error.
1422 DBG("Error writing to tracefile "
1423 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1424 ret
, len
, subbuf_size
);
1429 ERR("Unknown output method");
1433 err
= kernctl_put_next_subbuf(infd
);
1435 if (err
== -EFAULT
) {
1436 PERROR("Error in unreserving sub buffer\n");
1437 } else if (err
== -EIO
) {
1438 /* Should never happen with newer LTTng versions */
1439 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1445 /* Write index if needed. */
1450 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1452 * In live, block until all the metadata is sent.
1454 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1455 assert(!stream
->missed_metadata_flush
);
1456 stream
->waiting_on_metadata
= true;
1457 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1459 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1461 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1462 stream
->waiting_on_metadata
= false;
1463 if (stream
->missed_metadata_flush
) {
1464 stream
->missed_metadata_flush
= false;
1465 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1466 (void) consumer_flush_kernel_index(stream
);
1468 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1475 err
= consumer_stream_write_index(stream
, &index
);
1484 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1491 * Don't create anything if this is set for streaming or should not be
1494 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1495 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1496 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1497 stream
->uid
, stream
->gid
, NULL
);
1501 stream
->out_fd
= ret
;
1502 stream
->tracefile_size_current
= 0;
1504 if (!stream
->metadata_flag
) {
1505 struct lttng_index_file
*index_file
;
1507 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1508 stream
->name
, stream
->uid
, stream
->gid
,
1509 stream
->chan
->tracefile_size
,
1510 stream
->tracefile_count_current
,
1511 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1515 stream
->index_file
= index_file
;
1519 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1520 /* get the len of the mmap region */
1521 unsigned long mmap_len
;
1523 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1525 PERROR("kernctl_get_mmap_len");
1526 goto error_close_fd
;
1528 stream
->mmap_len
= (size_t) mmap_len
;
1530 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1531 MAP_PRIVATE
, stream
->wait_fd
, 0);
1532 if (stream
->mmap_base
== MAP_FAILED
) {
1533 PERROR("Error mmaping");
1535 goto error_close_fd
;
1539 /* we return 0 to let the library handle the FD internally */
1543 if (stream
->out_fd
>= 0) {
1546 err
= close(stream
->out_fd
);
1548 stream
->out_fd
= -1;
1555 * Check if data is still being extracted from the buffers for a specific
1556 * stream. Consumer data lock MUST be acquired before calling this function
1557 * and the stream lock.
1559 * Return 1 if the traced data are still getting read else 0 meaning that the
1560 * data is available for trace viewer reading.
1562 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1568 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1573 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1575 /* There is still data so let's put back this subbuffer. */
1576 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1578 ret
= 1; /* Data is pending */
1582 /* Data is NOT pending and ready to be read. */