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.");
72 * Get the produced position
74 * Returns 0 on success, < 0 on error
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
80 int infd
= stream
->wait_fd
;
82 ret
= kernctl_snapshot_get_produced(infd
, pos
);
84 PERROR("kernctl_snapshot_get_produced");
92 * Get the consumerd position
94 * Returns 0 on success, < 0 on error
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
100 int infd
= stream
->wait_fd
;
102 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
104 PERROR("kernctl_snapshot_get_consumed");
112 * Take a snapshot of all the stream of a channel
114 * Returns 0 on success, < 0 on error
116 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
117 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
118 struct lttng_consumer_local_data
*ctx
)
121 struct lttng_consumer_channel
*channel
;
122 struct lttng_consumer_stream
*stream
;
124 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
128 channel
= consumer_find_channel(key
);
130 ERR("No channel found for key %" PRIu64
, key
);
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
137 ERR("Unsupported output %d", channel
->output
);
142 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
143 /* Are we at a position _before_ the first available packet ? */
144 bool before_first_packet
= true;
145 unsigned long consumed_pos
, produced_pos
;
147 health_code_update();
150 * Lock stream because we are about to change its state.
152 pthread_mutex_lock(&stream
->lock
);
155 * Assign the received relayd ID so we can use it for streaming. The streams
156 * are not visible to anyone so this is OK to change it.
158 stream
->net_seq_idx
= relayd_id
;
159 channel
->relayd_id
= relayd_id
;
160 if (relayd_id
!= (uint64_t) -1ULL) {
161 ret
= consumer_send_relayd_stream(stream
, path
);
163 ERR("sending stream to relayd");
167 ret
= utils_create_stream_file(path
, stream
->name
,
168 stream
->chan
->tracefile_size
,
169 stream
->tracefile_count_current
,
170 stream
->uid
, stream
->gid
, NULL
);
172 ERR("utils_create_stream_file");
176 stream
->out_fd
= ret
;
177 stream
->tracefile_size_current
= 0;
179 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
180 path
, stream
->name
, stream
->key
);
182 if (relayd_id
!= -1ULL) {
183 ret
= consumer_send_relayd_streams_sent(relayd_id
);
185 ERR("sending streams sent to relayd");
188 channel
->streams_sent_to_relayd
= true;
191 ret
= kernctl_buffer_flush(stream
->wait_fd
);
193 ERR("Failed to flush kernel stream");
198 ret
= lttng_kconsumer_take_snapshot(stream
);
200 ERR("Taking kernel snapshot");
204 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
206 ERR("Produced kernel snapshot position");
210 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
212 ERR("Consumerd kernel snapshot position");
216 if (stream
->max_sb_size
== 0) {
217 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
218 &stream
->max_sb_size
);
220 ERR("Getting kernel max_sb_size");
226 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
227 produced_pos
, nb_packets_per_stream
,
228 stream
->max_sb_size
);
230 while (consumed_pos
< produced_pos
) {
232 unsigned long len
, padded_len
;
235 health_code_update();
237 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
239 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
241 if (errno
!= EAGAIN
) {
242 PERROR("kernctl_get_subbuf snapshot");
246 DBG("Kernel consumer get subbuf failed. Skipping it.");
247 consumed_pos
+= stream
->max_sb_size
;
250 * Start accounting lost packets only when we
251 * already have extracted packets (to match the
252 * content of the final snapshot).
254 if (!before_first_packet
) {
260 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
262 ERR("Snapshot kernctl_get_subbuf_size");
264 goto error_put_subbuf
;
267 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
269 ERR("Snapshot kernctl_get_padded_subbuf_size");
271 goto error_put_subbuf
;
274 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
275 padded_len
- len
, NULL
);
277 * We write the padded len in local tracefiles but the data len
278 * when using a relay. Display the error but continue processing
279 * to try to release the subbuffer.
281 if (relayd_id
!= (uint64_t) -1ULL) {
282 if (read_len
!= len
) {
283 ERR("Error sending to the relay (ret: %zd != len: %lu)",
287 if (read_len
!= padded_len
) {
288 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
289 read_len
, padded_len
);
293 ret
= kernctl_put_subbuf(stream
->wait_fd
);
295 ERR("Snapshot kernctl_put_subbuf");
299 consumed_pos
+= stream
->max_sb_size
;
302 * Only account lost packets located between
303 * succesfully extracted packets (do not account before
304 * and after since they are not visible in the
305 * resulting snapshot).
307 stream
->chan
->lost_packets
+= lost_packet
;
309 before_first_packet
= false;
312 if (relayd_id
== (uint64_t) -1ULL) {
313 if (stream
->out_fd
>= 0) {
314 ret
= close(stream
->out_fd
);
316 PERROR("Kernel consumer snapshot close out_fd");
322 close_relayd_stream(stream
);
323 stream
->net_seq_idx
= (uint64_t) -1ULL;
325 pthread_mutex_unlock(&stream
->lock
);
333 ret
= kernctl_put_subbuf(stream
->wait_fd
);
336 ERR("Snapshot kernctl_put_subbuf error path");
339 pthread_mutex_unlock(&stream
->lock
);
346 * Read the whole metadata available for a snapshot.
348 * Returns 0 on success, < 0 on error
350 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
351 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
353 int ret
, use_relayd
= 0;
355 struct lttng_consumer_channel
*metadata_channel
;
356 struct lttng_consumer_stream
*metadata_stream
;
360 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
365 metadata_channel
= consumer_find_channel(key
);
366 if (!metadata_channel
) {
367 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
372 metadata_stream
= metadata_channel
->metadata_stream
;
373 assert(metadata_stream
);
375 /* Flag once that we have a valid relayd for the stream. */
376 if (relayd_id
!= (uint64_t) -1ULL) {
381 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
386 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
387 metadata_stream
->chan
->tracefile_size
,
388 metadata_stream
->tracefile_count_current
,
389 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
393 metadata_stream
->out_fd
= ret
;
397 health_code_update();
399 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
401 if (ret_read
!= -EAGAIN
) {
402 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
406 /* ret_read is negative at this point so we will exit the loop. */
409 } while (ret_read
>= 0);
412 close_relayd_stream(metadata_stream
);
413 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
415 if (metadata_stream
->out_fd
>= 0) {
416 ret
= close(metadata_stream
->out_fd
);
418 PERROR("Kernel consumer snapshot metadata close out_fd");
420 * Don't go on error here since the snapshot was successful at this
421 * point but somehow the close failed.
424 metadata_stream
->out_fd
= -1;
430 cds_list_del(&metadata_stream
->send_node
);
431 consumer_stream_destroy(metadata_stream
, NULL
);
432 metadata_channel
->metadata_stream
= NULL
;
439 * Receive command from session daemon and process it.
441 * Return 1 on success else a negative value or 0.
443 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
444 int sock
, struct pollfd
*consumer_sockpoll
)
447 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
448 struct lttcomm_consumer_msg msg
;
450 health_code_update();
452 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
453 if (ret
!= sizeof(msg
)) {
455 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
461 health_code_update();
463 /* Deprecated command */
464 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
466 health_code_update();
468 /* relayd needs RCU read-side protection */
471 switch (msg
.cmd_type
) {
472 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
474 /* Session daemon status message are handled in the following call. */
475 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
476 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
477 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
478 msg
.u
.relayd_sock
.relayd_session_id
);
481 case LTTNG_CONSUMER_ADD_CHANNEL
:
483 struct lttng_consumer_channel
*new_channel
;
486 health_code_update();
488 /* First send a status message before receiving the fds. */
489 ret
= consumer_send_status_msg(sock
, ret_code
);
491 /* Somehow, the session daemon is not responding anymore. */
495 health_code_update();
497 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
498 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
499 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
500 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
501 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
502 msg
.u
.channel
.tracefile_size
,
503 msg
.u
.channel
.tracefile_count
, 0,
504 msg
.u
.channel
.monitor
,
505 msg
.u
.channel
.live_timer_interval
,
507 if (new_channel
== NULL
) {
508 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
511 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
512 switch (msg
.u
.channel
.output
) {
513 case LTTNG_EVENT_SPLICE
:
514 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
516 case LTTNG_EVENT_MMAP
:
517 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
520 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
524 /* Translate and save channel type. */
525 switch (msg
.u
.channel
.type
) {
526 case CONSUMER_CHANNEL_TYPE_DATA
:
527 case CONSUMER_CHANNEL_TYPE_METADATA
:
528 new_channel
->type
= msg
.u
.channel
.type
;
535 health_code_update();
537 if (ctx
->on_recv_channel
!= NULL
) {
538 ret_recv
= ctx
->on_recv_channel(new_channel
);
540 ret
= consumer_add_channel(new_channel
, ctx
);
541 } else if (ret_recv
< 0) {
545 ret
= consumer_add_channel(new_channel
, ctx
);
547 if (CONSUMER_CHANNEL_TYPE_DATA
) {
548 consumer_timer_live_start(new_channel
,
549 msg
.u
.channel
.live_timer_interval
);
552 health_code_update();
554 /* If we received an error in add_channel, we need to report it. */
556 ret
= consumer_send_status_msg(sock
, ret
);
565 case LTTNG_CONSUMER_ADD_STREAM
:
568 struct lttng_pipe
*stream_pipe
;
569 struct lttng_consumer_stream
*new_stream
;
570 struct lttng_consumer_channel
*channel
;
574 * Get stream's channel reference. Needed when adding the stream to the
577 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
580 * We could not find the channel. Can happen if cpu hotplug
581 * happens while tearing down.
583 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
584 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
587 health_code_update();
589 /* First send a status message before receiving the fds. */
590 ret
= consumer_send_status_msg(sock
, ret_code
);
592 /* Somehow, the session daemon is not responding anymore. */
596 health_code_update();
598 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
599 /* Channel was not found. */
605 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
611 health_code_update();
613 /* Get stream file descriptor from socket */
614 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
615 if (ret
!= sizeof(fd
)) {
616 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
621 health_code_update();
624 * Send status code to session daemon only if the recv works. If the
625 * above recv() failed, the session daemon is notified through the
626 * error socket and the teardown is eventually done.
628 ret
= consumer_send_status_msg(sock
, ret_code
);
630 /* Somehow, the session daemon is not responding anymore. */
634 health_code_update();
636 new_stream
= consumer_allocate_stream(channel
->key
,
638 LTTNG_CONSUMER_ACTIVE_STREAM
,
648 if (new_stream
== NULL
) {
653 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
659 new_stream
->chan
= channel
;
660 new_stream
->wait_fd
= fd
;
661 switch (channel
->output
) {
662 case CONSUMER_CHANNEL_SPLICE
:
663 new_stream
->output
= LTTNG_EVENT_SPLICE
;
664 ret
= utils_create_pipe(new_stream
->splice_pipe
);
669 case CONSUMER_CHANNEL_MMAP
:
670 new_stream
->output
= LTTNG_EVENT_MMAP
;
673 ERR("Stream output unknown %d", channel
->output
);
678 * We've just assigned the channel to the stream so increment the
679 * refcount right now. We don't need to increment the refcount for
680 * streams in no monitor because we handle manually the cleanup of
681 * those. It is very important to make sure there is NO prior
682 * consumer_del_stream() calls or else the refcount will be unbalanced.
684 if (channel
->monitor
) {
685 uatomic_inc(&new_stream
->chan
->refcount
);
689 * The buffer flush is done on the session daemon side for the kernel
690 * so no need for the stream "hangup_flush_done" variable to be
691 * tracked. This is important for a kernel stream since we don't rely
692 * on the flush state of the stream to read data. It's not the case for
693 * user space tracing.
695 new_stream
->hangup_flush_done
= 0;
697 health_code_update();
699 if (ctx
->on_recv_stream
) {
700 ret
= ctx
->on_recv_stream(new_stream
);
702 consumer_stream_free(new_stream
);
707 health_code_update();
709 if (new_stream
->metadata_flag
) {
710 channel
->metadata_stream
= new_stream
;
713 /* Do not monitor this stream. */
714 if (!channel
->monitor
) {
715 DBG("Kernel consumer add stream %s in no monitor mode with "
716 "relayd id %" PRIu64
, new_stream
->name
,
717 new_stream
->net_seq_idx
);
718 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
722 /* Send stream to relayd if the stream has an ID. */
723 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
724 ret
= consumer_send_relayd_stream(new_stream
,
725 new_stream
->chan
->pathname
);
727 consumer_stream_free(new_stream
);
732 * If adding an extra stream to an already
733 * existing channel (e.g. cpu hotplug), we need
734 * to send the "streams_sent" command to relayd.
736 if (channel
->streams_sent_to_relayd
) {
737 ret
= consumer_send_relayd_streams_sent(
738 new_stream
->net_seq_idx
);
745 /* Get the right pipe where the stream will be sent. */
746 if (new_stream
->metadata_flag
) {
747 ret
= consumer_add_metadata_stream(new_stream
);
749 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
751 consumer_stream_free(new_stream
);
754 stream_pipe
= ctx
->consumer_metadata_pipe
;
756 ret
= consumer_add_data_stream(new_stream
);
758 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
760 consumer_stream_free(new_stream
);
763 stream_pipe
= ctx
->consumer_data_pipe
;
766 /* Vitible to other threads */
767 new_stream
->globally_visible
= 1;
769 health_code_update();
771 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
773 ERR("Consumer write %s stream to pipe %d",
774 new_stream
->metadata_flag
? "metadata" : "data",
775 lttng_pipe_get_writefd(stream_pipe
));
776 if (new_stream
->metadata_flag
) {
777 consumer_del_stream_for_metadata(new_stream
);
779 consumer_del_stream_for_data(new_stream
);
784 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
785 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
788 case LTTNG_CONSUMER_STREAMS_SENT
:
790 struct lttng_consumer_channel
*channel
;
793 * Get stream's channel reference. Needed when adding the stream to the
796 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
799 * We could not find the channel. Can happen if cpu hotplug
800 * happens while tearing down.
802 ERR("Unable to find channel key %" PRIu64
,
803 msg
.u
.sent_streams
.channel_key
);
804 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
807 health_code_update();
810 * Send status code to session daemon.
812 ret
= consumer_send_status_msg(sock
, ret_code
);
813 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
814 /* Somehow, the session daemon is not responding anymore. */
818 health_code_update();
821 * We should not send this message if we don't monitor the
822 * streams in this channel.
824 if (!channel
->monitor
) {
828 health_code_update();
829 /* Send stream to relayd if the stream has an ID. */
830 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
831 ret
= consumer_send_relayd_streams_sent(
832 msg
.u
.sent_streams
.net_seq_idx
);
836 channel
->streams_sent_to_relayd
= true;
840 case LTTNG_CONSUMER_UPDATE_STREAM
:
845 case LTTNG_CONSUMER_DESTROY_RELAYD
:
847 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
848 struct consumer_relayd_sock_pair
*relayd
;
850 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
852 /* Get relayd reference if exists. */
853 relayd
= consumer_find_relayd(index
);
854 if (relayd
== NULL
) {
855 DBG("Unable to find relayd %" PRIu64
, index
);
856 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
860 * Each relayd socket pair has a refcount of stream attached to it
861 * which tells if the relayd is still active or not depending on the
864 * This will set the destroy flag of the relayd object and destroy it
865 * if the refcount reaches zero when called.
867 * The destroy can happen either here or when a stream fd hangs up.
870 consumer_flag_relayd_for_destroy(relayd
);
873 health_code_update();
875 ret
= consumer_send_status_msg(sock
, ret_code
);
877 /* Somehow, the session daemon is not responding anymore. */
883 case LTTNG_CONSUMER_DATA_PENDING
:
886 uint64_t id
= msg
.u
.data_pending
.session_id
;
888 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
890 ret
= consumer_data_pending(id
);
892 health_code_update();
894 /* Send back returned value to session daemon */
895 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
897 PERROR("send data pending ret code");
902 * No need to send back a status message since the data pending
903 * returned value is the response.
907 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
909 if (msg
.u
.snapshot_channel
.metadata
== 1) {
910 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
911 msg
.u
.snapshot_channel
.pathname
,
912 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
914 ERR("Snapshot metadata failed");
915 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
918 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
919 msg
.u
.snapshot_channel
.pathname
,
920 msg
.u
.snapshot_channel
.relayd_id
,
921 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
924 ERR("Snapshot channel failed");
925 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. */
938 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
940 uint64_t key
= msg
.u
.destroy_channel
.key
;
941 struct lttng_consumer_channel
*channel
;
943 channel
= consumer_find_channel(key
);
945 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
946 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
949 health_code_update();
951 ret
= consumer_send_status_msg(sock
, ret_code
);
953 /* Somehow, the session daemon is not responding anymore. */
957 health_code_update();
959 /* Stop right now if no channel was found. */
965 * This command should ONLY be issued for channel with streams set in
968 assert(!channel
->monitor
);
971 * The refcount should ALWAYS be 0 in the case of a channel in no
974 assert(!uatomic_sub_return(&channel
->refcount
, 1));
976 consumer_del_channel(channel
);
980 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
983 struct lttng_consumer_channel
*channel
;
984 uint64_t id
= msg
.u
.discarded_events
.session_id
;
985 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
987 DBG("Kernel consumer discarded events command for session id %"
988 PRIu64
", channel key %" PRIu64
, id
, key
);
990 channel
= consumer_find_channel(key
);
992 ERR("Kernel consumer discarded events channel %"
993 PRIu64
" not found", key
);
996 ret
= channel
->discarded_events
;
999 health_code_update();
1001 /* Send back returned value to session daemon */
1002 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1004 PERROR("send discarded events");
1010 case LTTNG_CONSUMER_LOST_PACKETS
:
1013 struct lttng_consumer_channel
*channel
;
1014 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1015 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1017 DBG("Kernel consumer lost packets command for session id %"
1018 PRIu64
", channel key %" PRIu64
, id
, key
);
1020 channel
= consumer_find_channel(key
);
1022 ERR("Kernel consumer lost packets channel %"
1023 PRIu64
" not found", key
);
1026 ret
= channel
->lost_packets
;
1029 health_code_update();
1031 /* Send back returned value to session daemon */
1032 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1034 PERROR("send lost packets");
1048 * Return 1 to indicate success since the 0 value can be a socket
1049 * shutdown during the recv() or send() call.
1051 health_code_update();
1056 /* This will issue a consumer stop. */
1061 * Populate index values of a kernel stream. Values are set in big endian order.
1063 * Return 0 on success or else a negative value.
1065 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1069 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1071 PERROR("kernctl_get_timestamp_begin");
1074 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1076 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1078 PERROR("kernctl_get_timestamp_end");
1081 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1083 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1085 PERROR("kernctl_get_events_discarded");
1088 index
->events_discarded
= htobe64(index
->events_discarded
);
1090 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1092 PERROR("kernctl_get_content_size");
1095 index
->content_size
= htobe64(index
->content_size
);
1097 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1099 PERROR("kernctl_get_packet_size");
1102 index
->packet_size
= htobe64(index
->packet_size
);
1104 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1106 PERROR("kernctl_get_stream_id");
1109 index
->stream_id
= htobe64(index
->stream_id
);
1111 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1113 if (ret
== -ENOTTY
) {
1114 /* Command not implemented by lttng-modules. */
1115 index
->stream_instance_id
= -1ULL;
1118 PERROR("kernctl_get_instance_id");
1122 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1124 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1126 if (ret
== -ENOTTY
) {
1127 /* Command not implemented by lttng-modules. */
1128 index
->packet_seq_num
= -1ULL;
1131 PERROR("kernctl_get_sequence_number");
1135 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1141 * Sync metadata meaning request them to the session daemon and snapshot to the
1142 * metadata thread can consumer them.
1144 * Metadata stream lock MUST be acquired.
1146 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1147 * is empty or a negative value on error.
1149 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1155 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1157 ERR("Failed to flush kernel stream");
1161 ret
= kernctl_snapshot(metadata
->wait_fd
);
1163 if (errno
!= EAGAIN
) {
1164 ERR("Sync metadata, taking kernel snapshot failed.");
1167 DBG("Sync metadata, no new kernel metadata");
1168 /* No new metadata, exit. */
1178 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1181 uint64_t seq
, discarded
;
1183 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1185 if (ret
== -ENOTTY
) {
1186 /* Command not implemented by lttng-modules. */
1190 PERROR("kernctl_get_sequence_number");
1196 * Start the sequence when we extract the first packet in case we don't
1197 * start at 0 (for example if a consumer is not connected to the
1198 * session immediately after the beginning).
1200 if (stream
->last_sequence_number
== -1ULL) {
1201 stream
->last_sequence_number
= seq
;
1202 } else if (seq
> stream
->last_sequence_number
) {
1203 stream
->chan
->lost_packets
+= seq
-
1204 stream
->last_sequence_number
- 1;
1206 /* seq <= last_sequence_number */
1207 ERR("Sequence number inconsistent : prev = %" PRIu64
1208 ", current = %" PRIu64
,
1209 stream
->last_sequence_number
, seq
);
1213 stream
->last_sequence_number
= seq
;
1215 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1217 PERROR("kernctl_get_events_discarded");
1220 if (discarded
< stream
->last_discarded_events
) {
1222 * Overflow has occurred. We assume only one wrap-around
1225 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1226 stream
->last_discarded_events
+ discarded
;
1228 stream
->chan
->discarded_events
+= discarded
-
1229 stream
->last_discarded_events
;
1231 stream
->last_discarded_events
= discarded
;
1239 * Check if the local version of the metadata stream matches with the version
1240 * of the metadata stream in the kernel. If it was updated, set the reset flag
1244 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1247 uint64_t cur_version
;
1249 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1251 if (ret
== -ENOTTY
) {
1253 * LTTng-modules does not implement this
1259 ERR("Failed to get the metadata version");
1263 if (stream
->metadata_version
== cur_version
) {
1268 DBG("New metadata version detected");
1269 stream
->metadata_version
= cur_version
;
1270 stream
->reset_metadata_flag
= 1;
1278 * Consume data on a file descriptor and write it on a trace file.
1280 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1281 struct lttng_consumer_local_data
*ctx
)
1283 unsigned long len
, subbuf_size
, padding
;
1284 int err
, write_index
= 1;
1286 int infd
= stream
->wait_fd
;
1287 struct ctf_packet_index index
;
1289 DBG("In read_subbuffer (infd : %d)", infd
);
1291 /* Get the next subbuffer */
1292 err
= kernctl_get_next_subbuf(infd
);
1295 * This is a debug message even for single-threaded consumer,
1296 * because poll() have more relaxed criterions than get subbuf,
1297 * so get_subbuf may fail for short race windows where poll()
1298 * would issue wakeups.
1300 DBG("Reserving sub buffer failed (everything is normal, "
1301 "it is due to concurrency)");
1306 /* Get the full subbuffer size including padding */
1307 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1309 PERROR("Getting sub-buffer len failed.");
1310 err
= kernctl_put_subbuf(infd
);
1312 if (errno
== EFAULT
) {
1313 PERROR("Error in unreserving sub buffer\n");
1314 } else if (errno
== EIO
) {
1315 /* Should never happen with newer LTTng versions */
1316 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1325 if (!stream
->metadata_flag
) {
1326 ret
= get_index_values(&index
, infd
);
1328 err
= kernctl_put_subbuf(infd
);
1330 if (errno
== EFAULT
) {
1331 PERROR("Error in unreserving sub buffer\n");
1332 } else if (errno
== EIO
) {
1333 /* Should never happen with newer LTTng versions */
1334 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1341 ret
= update_stream_stats(stream
);
1343 err
= kernctl_put_subbuf(infd
);
1345 if (err
== -EFAULT
) {
1346 PERROR("Error in unreserving sub buffer\n");
1347 } else if (err
== -EIO
) {
1348 /* Should never happen with newer LTTng versions */
1349 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1358 ret
= metadata_stream_check_version(infd
, stream
);
1360 err
= kernctl_put_subbuf(infd
);
1362 if (err
== -EFAULT
) {
1363 PERROR("Error in unreserving sub buffer\n");
1364 } else if (err
== -EIO
) {
1365 /* Should never happen with newer LTTng versions */
1366 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1375 switch (stream
->chan
->output
) {
1376 case CONSUMER_CHANNEL_SPLICE
:
1378 * XXX: The lttng-modules splice "actor" does not handle copying
1379 * partial pages hence only using the subbuffer size without the
1380 * padding makes the splice fail.
1385 /* splice the subbuffer to the tracefile */
1386 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1389 * XXX: Splice does not support network streaming so the return value
1390 * is simply checked against subbuf_size and not like the mmap() op.
1392 if (ret
!= subbuf_size
) {
1394 * display the error but continue processing to try
1395 * to release the subbuffer
1397 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1402 case CONSUMER_CHANNEL_MMAP
:
1403 /* Get subbuffer size without padding */
1404 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1406 PERROR("Getting sub-buffer len failed.");
1407 err
= kernctl_put_subbuf(infd
);
1409 if (errno
== EFAULT
) {
1410 PERROR("Error in unreserving sub buffer\n");
1411 } else if (errno
== EIO
) {
1412 /* Should never happen with newer LTTng versions */
1413 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1422 /* Make sure the tracer is not gone mad on us! */
1423 assert(len
>= subbuf_size
);
1425 padding
= len
- subbuf_size
;
1427 /* write the subbuffer to the tracefile */
1428 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1431 * The mmap operation should write subbuf_size amount of data when
1432 * network streaming or the full padding (len) size when we are _not_
1435 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1436 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1438 * Display the error but continue processing to try to release the
1439 * subbuffer. This is a DBG statement since this is possible to
1440 * happen without being a critical error.
1442 DBG("Error writing to tracefile "
1443 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1444 ret
, len
, subbuf_size
);
1449 ERR("Unknown output method");
1453 err
= kernctl_put_next_subbuf(infd
);
1455 if (errno
== EFAULT
) {
1456 PERROR("Error in unreserving sub buffer\n");
1457 } else if (errno
== EIO
) {
1458 /* Should never happen with newer LTTng versions */
1459 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1465 /* Write index if needed. */
1470 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1472 * In live, block until all the metadata is sent.
1474 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1475 assert(!stream
->missed_metadata_flush
);
1476 stream
->waiting_on_metadata
= true;
1477 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1479 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1481 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1482 stream
->waiting_on_metadata
= false;
1483 if (stream
->missed_metadata_flush
) {
1484 stream
->missed_metadata_flush
= false;
1485 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1486 (void) consumer_flush_kernel_index(stream
);
1488 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1495 err
= consumer_stream_write_index(stream
, &index
);
1504 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1511 * Don't create anything if this is set for streaming or should not be
1514 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1515 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1516 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1517 stream
->uid
, stream
->gid
, NULL
);
1521 stream
->out_fd
= ret
;
1522 stream
->tracefile_size_current
= 0;
1524 if (!stream
->metadata_flag
) {
1525 struct lttng_index_file
*index_file
;
1527 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1528 stream
->name
, stream
->uid
, stream
->gid
,
1529 stream
->chan
->tracefile_size
,
1530 stream
->tracefile_count_current
,
1531 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1535 stream
->index_file
= index_file
;
1539 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1540 /* get the len of the mmap region */
1541 unsigned long mmap_len
;
1543 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1545 PERROR("kernctl_get_mmap_len");
1547 goto error_close_fd
;
1549 stream
->mmap_len
= (size_t) mmap_len
;
1551 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1552 MAP_PRIVATE
, stream
->wait_fd
, 0);
1553 if (stream
->mmap_base
== MAP_FAILED
) {
1554 PERROR("Error mmaping");
1556 goto error_close_fd
;
1560 /* we return 0 to let the library handle the FD internally */
1564 if (stream
->out_fd
>= 0) {
1567 err
= close(stream
->out_fd
);
1569 stream
->out_fd
= -1;
1576 * Check if data is still being extracted from the buffers for a specific
1577 * stream. Consumer data lock MUST be acquired before calling this function
1578 * and the stream lock.
1580 * Return 1 if the traced data are still getting read else 0 meaning that the
1581 * data is available for trace viewer reading.
1583 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1589 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1594 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1596 /* There is still data so let's put back this subbuffer. */
1597 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1599 ret
= 1; /* Data is pending */
1603 /* Data is NOT pending and ready to be read. */