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 * -EAGAIN is not an error, it just means that there is no data to
67 if (ret
!= 0 && ret
!= -EAGAIN
) {
68 PERROR("Getting sub-buffer snapshot.");
75 * Get the produced position
77 * Returns 0 on success, < 0 on error
79 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
83 int infd
= stream
->wait_fd
;
85 ret
= kernctl_snapshot_get_produced(infd
, pos
);
87 PERROR("kernctl_snapshot_get_produced");
94 * Get the consumerd position
96 * Returns 0 on success, < 0 on error
98 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
102 int infd
= stream
->wait_fd
;
104 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
106 PERROR("kernctl_snapshot_get_consumed");
113 * Take a snapshot of all the stream of a channel
115 * Returns 0 on success, < 0 on error
117 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
118 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
119 struct lttng_consumer_local_data
*ctx
)
122 struct lttng_consumer_channel
*channel
;
123 struct lttng_consumer_stream
*stream
;
125 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
129 channel
= consumer_find_channel(key
);
131 ERR("No channel found for key %" PRIu64
, key
);
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
138 ERR("Unsupported output %d", channel
->output
);
143 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
144 unsigned long consumed_pos
, produced_pos
;
146 health_code_update();
149 * Lock stream because we are about to change its state.
151 pthread_mutex_lock(&stream
->lock
);
154 * Assign the received relayd ID so we can use it for streaming. The streams
155 * are not visible to anyone so this is OK to change it.
157 stream
->net_seq_idx
= relayd_id
;
158 channel
->relayd_id
= relayd_id
;
159 if (relayd_id
!= (uint64_t) -1ULL) {
160 ret
= consumer_send_relayd_stream(stream
, path
);
162 ERR("sending stream to relayd");
166 ret
= utils_create_stream_file(path
, stream
->name
,
167 stream
->chan
->tracefile_size
,
168 stream
->tracefile_count_current
,
169 stream
->uid
, stream
->gid
, NULL
);
171 ERR("utils_create_stream_file");
175 stream
->out_fd
= ret
;
176 stream
->tracefile_size_current
= 0;
178 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
179 path
, stream
->name
, stream
->key
);
181 if (relayd_id
!= -1ULL) {
182 ret
= consumer_send_relayd_streams_sent(relayd_id
);
184 ERR("sending streams sent to relayd");
187 channel
->streams_sent_to_relayd
= true;
190 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
193 * Doing a buffer flush which does not take into
194 * account empty packets. This is not perfect
195 * for stream intersection, but required as a
196 * fall-back when "flush_empty" is not
197 * implemented by lttng-modules.
199 ret
= kernctl_buffer_flush(stream
->wait_fd
);
201 ERR("Failed to flush kernel stream");
207 ret
= lttng_kconsumer_take_snapshot(stream
);
209 ERR("Taking kernel snapshot");
213 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
215 ERR("Produced kernel snapshot position");
219 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
221 ERR("Consumerd kernel snapshot position");
225 if (stream
->max_sb_size
== 0) {
226 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
227 &stream
->max_sb_size
);
229 ERR("Getting kernel max_sb_size");
234 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
235 produced_pos
, nb_packets_per_stream
,
236 stream
->max_sb_size
);
238 while (consumed_pos
< produced_pos
) {
240 unsigned long len
, padded_len
;
242 health_code_update();
244 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
246 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
248 if (ret
!= -EAGAIN
) {
249 PERROR("kernctl_get_subbuf snapshot");
252 DBG("Kernel consumer get subbuf failed. Skipping it.");
253 consumed_pos
+= stream
->max_sb_size
;
254 stream
->chan
->lost_packets
++;
258 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
260 ERR("Snapshot kernctl_get_subbuf_size");
261 goto error_put_subbuf
;
264 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
266 ERR("Snapshot kernctl_get_padded_subbuf_size");
267 goto error_put_subbuf
;
270 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
271 padded_len
- len
, NULL
);
273 * We write the padded len in local tracefiles but the data len
274 * when using a relay. Display the error but continue processing
275 * to try to release the subbuffer.
277 if (relayd_id
!= (uint64_t) -1ULL) {
278 if (read_len
!= len
) {
279 ERR("Error sending to the relay (ret: %zd != len: %lu)",
283 if (read_len
!= padded_len
) {
284 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
285 read_len
, padded_len
);
289 ret
= kernctl_put_subbuf(stream
->wait_fd
);
291 ERR("Snapshot kernctl_put_subbuf");
294 consumed_pos
+= stream
->max_sb_size
;
297 if (relayd_id
== (uint64_t) -1ULL) {
298 if (stream
->out_fd
>= 0) {
299 ret
= close(stream
->out_fd
);
301 PERROR("Kernel consumer snapshot close out_fd");
307 close_relayd_stream(stream
);
308 stream
->net_seq_idx
= (uint64_t) -1ULL;
310 pthread_mutex_unlock(&stream
->lock
);
318 ret
= kernctl_put_subbuf(stream
->wait_fd
);
320 ERR("Snapshot kernctl_put_subbuf error path");
323 pthread_mutex_unlock(&stream
->lock
);
330 * Read the whole metadata available for a snapshot.
332 * Returns 0 on success, < 0 on error
334 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
335 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
337 int ret
, use_relayd
= 0;
339 struct lttng_consumer_channel
*metadata_channel
;
340 struct lttng_consumer_stream
*metadata_stream
;
344 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
349 metadata_channel
= consumer_find_channel(key
);
350 if (!metadata_channel
) {
351 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
356 metadata_stream
= metadata_channel
->metadata_stream
;
357 assert(metadata_stream
);
359 /* Flag once that we have a valid relayd for the stream. */
360 if (relayd_id
!= (uint64_t) -1ULL) {
365 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
370 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
371 metadata_stream
->chan
->tracefile_size
,
372 metadata_stream
->tracefile_count_current
,
373 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
377 metadata_stream
->out_fd
= ret
;
381 health_code_update();
383 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
385 if (ret_read
!= -EAGAIN
) {
386 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
390 /* ret_read is negative at this point so we will exit the loop. */
393 } while (ret_read
>= 0);
396 close_relayd_stream(metadata_stream
);
397 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
399 if (metadata_stream
->out_fd
>= 0) {
400 ret
= close(metadata_stream
->out_fd
);
402 PERROR("Kernel consumer snapshot metadata close out_fd");
404 * Don't go on error here since the snapshot was successful at this
405 * point but somehow the close failed.
408 metadata_stream
->out_fd
= -1;
414 cds_list_del(&metadata_stream
->send_node
);
415 consumer_stream_destroy(metadata_stream
, NULL
);
416 metadata_channel
->metadata_stream
= NULL
;
423 * Receive command from session daemon and process it.
425 * Return 1 on success else a negative value or 0.
427 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
428 int sock
, struct pollfd
*consumer_sockpoll
)
431 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
432 struct lttcomm_consumer_msg msg
;
434 health_code_update();
436 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
437 if (ret
!= sizeof(msg
)) {
439 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
445 health_code_update();
447 /* Deprecated command */
448 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
450 health_code_update();
452 /* relayd needs RCU read-side protection */
455 switch (msg
.cmd_type
) {
456 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
458 /* Session daemon status message are handled in the following call. */
459 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
460 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
461 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
462 msg
.u
.relayd_sock
.relayd_session_id
);
465 case LTTNG_CONSUMER_ADD_CHANNEL
:
467 struct lttng_consumer_channel
*new_channel
;
470 health_code_update();
472 /* First send a status message before receiving the fds. */
473 ret
= consumer_send_status_msg(sock
, ret_code
);
475 /* Somehow, the session daemon is not responding anymore. */
479 health_code_update();
481 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
482 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
483 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
484 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
485 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
486 msg
.u
.channel
.tracefile_size
,
487 msg
.u
.channel
.tracefile_count
, 0,
488 msg
.u
.channel
.monitor
,
489 msg
.u
.channel
.live_timer_interval
,
491 if (new_channel
== NULL
) {
492 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
495 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
496 switch (msg
.u
.channel
.output
) {
497 case LTTNG_EVENT_SPLICE
:
498 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
500 case LTTNG_EVENT_MMAP
:
501 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
504 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
508 /* Translate and save channel type. */
509 switch (msg
.u
.channel
.type
) {
510 case CONSUMER_CHANNEL_TYPE_DATA
:
511 case CONSUMER_CHANNEL_TYPE_METADATA
:
512 new_channel
->type
= msg
.u
.channel
.type
;
519 health_code_update();
521 if (ctx
->on_recv_channel
!= NULL
) {
522 ret_recv
= ctx
->on_recv_channel(new_channel
);
524 ret
= consumer_add_channel(new_channel
, ctx
);
525 } else if (ret_recv
< 0) {
529 ret
= consumer_add_channel(new_channel
, ctx
);
531 if (CONSUMER_CHANNEL_TYPE_DATA
) {
532 consumer_timer_live_start(new_channel
,
533 msg
.u
.channel
.live_timer_interval
);
536 health_code_update();
538 /* If we received an error in add_channel, we need to report it. */
540 ret
= consumer_send_status_msg(sock
, ret
);
549 case LTTNG_CONSUMER_ADD_STREAM
:
552 struct lttng_pipe
*stream_pipe
;
553 struct lttng_consumer_stream
*new_stream
;
554 struct lttng_consumer_channel
*channel
;
558 * Get stream's channel reference. Needed when adding the stream to the
561 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
564 * We could not find the channel. Can happen if cpu hotplug
565 * happens while tearing down.
567 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
568 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
571 health_code_update();
573 /* First send a status message before receiving the fds. */
574 ret
= consumer_send_status_msg(sock
, ret_code
);
576 /* Somehow, the session daemon is not responding anymore. */
580 health_code_update();
582 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
583 /* Channel was not found. */
589 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
595 health_code_update();
597 /* Get stream file descriptor from socket */
598 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
599 if (ret
!= sizeof(fd
)) {
600 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
605 health_code_update();
608 * Send status code to session daemon only if the recv works. If the
609 * above recv() failed, the session daemon is notified through the
610 * error socket and the teardown is eventually done.
612 ret
= consumer_send_status_msg(sock
, ret_code
);
614 /* Somehow, the session daemon is not responding anymore. */
618 health_code_update();
620 new_stream
= consumer_allocate_stream(channel
->key
,
622 LTTNG_CONSUMER_ACTIVE_STREAM
,
632 if (new_stream
== NULL
) {
637 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
643 new_stream
->chan
= channel
;
644 new_stream
->wait_fd
= fd
;
645 switch (channel
->output
) {
646 case CONSUMER_CHANNEL_SPLICE
:
647 new_stream
->output
= LTTNG_EVENT_SPLICE
;
648 ret
= utils_create_pipe(new_stream
->splice_pipe
);
653 case CONSUMER_CHANNEL_MMAP
:
654 new_stream
->output
= LTTNG_EVENT_MMAP
;
657 ERR("Stream output unknown %d", channel
->output
);
662 * We've just assigned the channel to the stream so increment the
663 * refcount right now. We don't need to increment the refcount for
664 * streams in no monitor because we handle manually the cleanup of
665 * those. It is very important to make sure there is NO prior
666 * consumer_del_stream() calls or else the refcount will be unbalanced.
668 if (channel
->monitor
) {
669 uatomic_inc(&new_stream
->chan
->refcount
);
673 * The buffer flush is done on the session daemon side for the kernel
674 * so no need for the stream "hangup_flush_done" variable to be
675 * tracked. This is important for a kernel stream since we don't rely
676 * on the flush state of the stream to read data. It's not the case for
677 * user space tracing.
679 new_stream
->hangup_flush_done
= 0;
681 health_code_update();
683 if (ctx
->on_recv_stream
) {
684 ret
= ctx
->on_recv_stream(new_stream
);
686 consumer_stream_free(new_stream
);
691 health_code_update();
693 if (new_stream
->metadata_flag
) {
694 channel
->metadata_stream
= new_stream
;
697 /* Do not monitor this stream. */
698 if (!channel
->monitor
) {
699 DBG("Kernel consumer add stream %s in no monitor mode with "
700 "relayd id %" PRIu64
, new_stream
->name
,
701 new_stream
->net_seq_idx
);
702 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
706 /* Send stream to relayd if the stream has an ID. */
707 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
708 ret
= consumer_send_relayd_stream(new_stream
,
709 new_stream
->chan
->pathname
);
711 consumer_stream_free(new_stream
);
716 * If adding an extra stream to an already
717 * existing channel (e.g. cpu hotplug), we need
718 * to send the "streams_sent" command to relayd.
720 if (channel
->streams_sent_to_relayd
) {
721 ret
= consumer_send_relayd_streams_sent(
722 new_stream
->net_seq_idx
);
729 /* Get the right pipe where the stream will be sent. */
730 if (new_stream
->metadata_flag
) {
731 ret
= consumer_add_metadata_stream(new_stream
);
733 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
735 consumer_stream_free(new_stream
);
738 stream_pipe
= ctx
->consumer_metadata_pipe
;
740 ret
= consumer_add_data_stream(new_stream
);
742 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
744 consumer_stream_free(new_stream
);
747 stream_pipe
= ctx
->consumer_data_pipe
;
750 /* Vitible to other threads */
751 new_stream
->globally_visible
= 1;
753 health_code_update();
755 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
757 ERR("Consumer write %s stream to pipe %d",
758 new_stream
->metadata_flag
? "metadata" : "data",
759 lttng_pipe_get_writefd(stream_pipe
));
760 if (new_stream
->metadata_flag
) {
761 consumer_del_stream_for_metadata(new_stream
);
763 consumer_del_stream_for_data(new_stream
);
768 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
769 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
772 case LTTNG_CONSUMER_STREAMS_SENT
:
774 struct lttng_consumer_channel
*channel
;
777 * Get stream's channel reference. Needed when adding the stream to the
780 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
783 * We could not find the channel. Can happen if cpu hotplug
784 * happens while tearing down.
786 ERR("Unable to find channel key %" PRIu64
,
787 msg
.u
.sent_streams
.channel_key
);
788 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
791 health_code_update();
794 * Send status code to session daemon.
796 ret
= consumer_send_status_msg(sock
, ret_code
);
797 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
798 /* Somehow, the session daemon is not responding anymore. */
802 health_code_update();
805 * We should not send this message if we don't monitor the
806 * streams in this channel.
808 if (!channel
->monitor
) {
812 health_code_update();
813 /* Send stream to relayd if the stream has an ID. */
814 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
815 ret
= consumer_send_relayd_streams_sent(
816 msg
.u
.sent_streams
.net_seq_idx
);
820 channel
->streams_sent_to_relayd
= true;
824 case LTTNG_CONSUMER_UPDATE_STREAM
:
829 case LTTNG_CONSUMER_DESTROY_RELAYD
:
831 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
832 struct consumer_relayd_sock_pair
*relayd
;
834 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
836 /* Get relayd reference if exists. */
837 relayd
= consumer_find_relayd(index
);
838 if (relayd
== NULL
) {
839 DBG("Unable to find relayd %" PRIu64
, index
);
840 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
844 * Each relayd socket pair has a refcount of stream attached to it
845 * which tells if the relayd is still active or not depending on the
848 * This will set the destroy flag of the relayd object and destroy it
849 * if the refcount reaches zero when called.
851 * The destroy can happen either here or when a stream fd hangs up.
854 consumer_flag_relayd_for_destroy(relayd
);
857 health_code_update();
859 ret
= consumer_send_status_msg(sock
, ret_code
);
861 /* Somehow, the session daemon is not responding anymore. */
867 case LTTNG_CONSUMER_DATA_PENDING
:
870 uint64_t id
= msg
.u
.data_pending
.session_id
;
872 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
874 ret
= consumer_data_pending(id
);
876 health_code_update();
878 /* Send back returned value to session daemon */
879 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
881 PERROR("send data pending ret code");
886 * No need to send back a status message since the data pending
887 * returned value is the response.
891 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
893 if (msg
.u
.snapshot_channel
.metadata
== 1) {
894 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
895 msg
.u
.snapshot_channel
.pathname
,
896 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
898 ERR("Snapshot metadata failed");
899 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
902 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
903 msg
.u
.snapshot_channel
.pathname
,
904 msg
.u
.snapshot_channel
.relayd_id
,
905 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
908 ERR("Snapshot channel failed");
909 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
913 health_code_update();
915 ret
= consumer_send_status_msg(sock
, ret_code
);
917 /* Somehow, the session daemon is not responding anymore. */
922 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
924 uint64_t key
= msg
.u
.destroy_channel
.key
;
925 struct lttng_consumer_channel
*channel
;
927 channel
= consumer_find_channel(key
);
929 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
930 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
933 health_code_update();
935 ret
= consumer_send_status_msg(sock
, ret_code
);
937 /* Somehow, the session daemon is not responding anymore. */
941 health_code_update();
943 /* Stop right now if no channel was found. */
949 * This command should ONLY be issued for channel with streams set in
952 assert(!channel
->monitor
);
955 * The refcount should ALWAYS be 0 in the case of a channel in no
958 assert(!uatomic_sub_return(&channel
->refcount
, 1));
960 consumer_del_channel(channel
);
964 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
967 struct lttng_consumer_channel
*channel
;
968 uint64_t id
= msg
.u
.discarded_events
.session_id
;
969 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
971 DBG("Kernel consumer discarded events command for session id %"
972 PRIu64
", channel key %" PRIu64
, id
, key
);
974 channel
= consumer_find_channel(key
);
976 ERR("Kernel consumer discarded events channel %"
977 PRIu64
" not found", key
);
980 ret
= channel
->discarded_events
;
983 health_code_update();
985 /* Send back returned value to session daemon */
986 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
988 PERROR("send discarded events");
994 case LTTNG_CONSUMER_LOST_PACKETS
:
997 struct lttng_consumer_channel
*channel
;
998 uint64_t id
= msg
.u
.lost_packets
.session_id
;
999 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1001 DBG("Kernel consumer lost packets command for session id %"
1002 PRIu64
", channel key %" PRIu64
, id
, key
);
1004 channel
= consumer_find_channel(key
);
1006 ERR("Kernel consumer lost packets channel %"
1007 PRIu64
" not found", key
);
1010 ret
= channel
->lost_packets
;
1013 health_code_update();
1015 /* Send back returned value to session daemon */
1016 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1018 PERROR("send lost packets");
1032 * Return 1 to indicate success since the 0 value can be a socket
1033 * shutdown during the recv() or send() call.
1035 health_code_update();
1040 /* This will issue a consumer stop. */
1045 * Populate index values of a kernel stream. Values are set in big endian order.
1047 * Return 0 on success or else a negative value.
1049 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1053 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1055 PERROR("kernctl_get_timestamp_begin");
1058 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1060 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1062 PERROR("kernctl_get_timestamp_end");
1065 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1067 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1069 PERROR("kernctl_get_events_discarded");
1072 index
->events_discarded
= htobe64(index
->events_discarded
);
1074 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1076 PERROR("kernctl_get_content_size");
1079 index
->content_size
= htobe64(index
->content_size
);
1081 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1083 PERROR("kernctl_get_packet_size");
1086 index
->packet_size
= htobe64(index
->packet_size
);
1088 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1090 PERROR("kernctl_get_stream_id");
1093 index
->stream_id
= htobe64(index
->stream_id
);
1095 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1097 if (ret
== -ENOTTY
) {
1098 /* Command not implemented by lttng-modules. */
1099 index
->stream_instance_id
= -1ULL;
1102 PERROR("kernctl_get_instance_id");
1106 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1108 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1110 if (ret
== -ENOTTY
) {
1111 /* Command not implemented by lttng-modules. */
1112 index
->packet_seq_num
= -1ULL;
1115 PERROR("kernctl_get_sequence_number");
1119 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1125 * Sync metadata meaning request them to the session daemon and snapshot to the
1126 * metadata thread can consumer them.
1128 * Metadata stream lock MUST be acquired.
1130 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1131 * is empty or a negative value on error.
1133 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1139 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1141 ERR("Failed to flush kernel stream");
1145 ret
= kernctl_snapshot(metadata
->wait_fd
);
1147 if (ret
!= -EAGAIN
) {
1148 ERR("Sync metadata, taking kernel snapshot failed.");
1151 DBG("Sync metadata, no new kernel metadata");
1152 /* No new metadata, exit. */
1162 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1165 uint64_t seq
, discarded
;
1167 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1169 if (ret
== -ENOTTY
) {
1170 /* Command not implemented by lttng-modules. */
1174 PERROR("kernctl_get_sequence_number");
1180 * Start the sequence when we extract the first packet in case we don't
1181 * start at 0 (for example if a consumer is not connected to the
1182 * session immediately after the beginning).
1184 if (stream
->last_sequence_number
== -1ULL) {
1185 stream
->last_sequence_number
= seq
;
1186 } else if (seq
> stream
->last_sequence_number
) {
1187 stream
->chan
->lost_packets
+= seq
-
1188 stream
->last_sequence_number
- 1;
1190 /* seq <= last_sequence_number */
1191 ERR("Sequence number inconsistent : prev = %" PRIu64
1192 ", current = %" PRIu64
,
1193 stream
->last_sequence_number
, seq
);
1197 stream
->last_sequence_number
= seq
;
1199 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1201 PERROR("kernctl_get_events_discarded");
1204 if (discarded
< stream
->last_discarded_events
) {
1206 * Overflow has occurred. We assume only one wrap-around
1209 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1210 stream
->last_discarded_events
+ discarded
;
1212 stream
->chan
->discarded_events
+= discarded
-
1213 stream
->last_discarded_events
;
1215 stream
->last_discarded_events
= discarded
;
1223 * Check if the local version of the metadata stream matches with the version
1224 * of the metadata stream in the kernel. If it was updated, set the reset flag
1228 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1231 uint64_t cur_version
;
1233 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1235 if (ret
== -ENOTTY
) {
1237 * LTTng-modules does not implement this
1243 ERR("Failed to get the metadata version");
1247 if (stream
->metadata_version
== cur_version
) {
1252 DBG("New metadata version detected");
1253 stream
->metadata_version
= cur_version
;
1254 stream
->reset_metadata_flag
= 1;
1262 * Consume data on a file descriptor and write it on a trace file.
1264 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1265 struct lttng_consumer_local_data
*ctx
)
1267 unsigned long len
, subbuf_size
, padding
;
1268 int err
, write_index
= 1;
1270 int infd
= stream
->wait_fd
;
1271 struct ctf_packet_index index
;
1273 DBG("In read_subbuffer (infd : %d)", infd
);
1275 /* Get the next subbuffer */
1276 err
= kernctl_get_next_subbuf(infd
);
1279 * This is a debug message even for single-threaded consumer,
1280 * because poll() have more relaxed criterions than get subbuf,
1281 * so get_subbuf may fail for short race windows where poll()
1282 * would issue wakeups.
1284 DBG("Reserving sub buffer failed (everything is normal, "
1285 "it is due to concurrency)");
1290 /* Get the full subbuffer size including padding */
1291 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1293 PERROR("Getting sub-buffer len failed.");
1294 err
= kernctl_put_subbuf(infd
);
1296 if (err
== -EFAULT
) {
1297 PERROR("Error in unreserving sub buffer\n");
1298 } else if (err
== -EIO
) {
1299 /* Should never happen with newer LTTng versions */
1300 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1309 if (!stream
->metadata_flag
) {
1310 ret
= get_index_values(&index
, infd
);
1312 err
= kernctl_put_subbuf(infd
);
1314 if (err
== -EFAULT
) {
1315 PERROR("Error in unreserving sub buffer\n");
1316 } else if (err
== -EIO
) {
1317 /* Should never happen with newer LTTng versions */
1318 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1325 ret
= update_stream_stats(stream
);
1327 err
= kernctl_put_subbuf(infd
);
1329 if (err
== -EFAULT
) {
1330 PERROR("Error in unreserving sub buffer\n");
1331 } else if (err
== -EIO
) {
1332 /* Should never happen with newer LTTng versions */
1333 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1342 ret
= metadata_stream_check_version(infd
, stream
);
1344 err
= kernctl_put_subbuf(infd
);
1346 if (err
== -EFAULT
) {
1347 PERROR("Error in unreserving sub buffer\n");
1348 } else if (err
== -EIO
) {
1349 /* Should never happen with newer LTTng versions */
1350 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1359 switch (stream
->chan
->output
) {
1360 case CONSUMER_CHANNEL_SPLICE
:
1362 * XXX: The lttng-modules splice "actor" does not handle copying
1363 * partial pages hence only using the subbuffer size without the
1364 * padding makes the splice fail.
1369 /* splice the subbuffer to the tracefile */
1370 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1373 * XXX: Splice does not support network streaming so the return value
1374 * is simply checked against subbuf_size and not like the mmap() op.
1376 if (ret
!= subbuf_size
) {
1378 * display the error but continue processing to try
1379 * to release the subbuffer
1381 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1386 case CONSUMER_CHANNEL_MMAP
:
1387 /* Get subbuffer size without padding */
1388 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1390 PERROR("Getting sub-buffer len failed.");
1391 err
= kernctl_put_subbuf(infd
);
1393 if (err
== -EFAULT
) {
1394 PERROR("Error in unreserving sub buffer\n");
1395 } else if (err
== -EIO
) {
1396 /* Should never happen with newer LTTng versions */
1397 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1406 /* Make sure the tracer is not gone mad on us! */
1407 assert(len
>= subbuf_size
);
1409 padding
= len
- subbuf_size
;
1411 /* write the subbuffer to the tracefile */
1412 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1415 * The mmap operation should write subbuf_size amount of data when
1416 * network streaming or the full padding (len) size when we are _not_
1419 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1420 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1422 * Display the error but continue processing to try to release the
1423 * subbuffer. This is a DBG statement since this is possible to
1424 * happen without being a critical error.
1426 DBG("Error writing to tracefile "
1427 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1428 ret
, len
, subbuf_size
);
1433 ERR("Unknown output method");
1437 err
= kernctl_put_next_subbuf(infd
);
1439 if (err
== -EFAULT
) {
1440 PERROR("Error in unreserving sub buffer\n");
1441 } else if (err
== -EIO
) {
1442 /* Should never happen with newer LTTng versions */
1443 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1449 /* Write index if needed. */
1454 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1456 * In live, block until all the metadata is sent.
1458 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1459 assert(!stream
->missed_metadata_flush
);
1460 stream
->waiting_on_metadata
= true;
1461 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1463 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1465 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1466 stream
->waiting_on_metadata
= false;
1467 if (stream
->missed_metadata_flush
) {
1468 stream
->missed_metadata_flush
= false;
1469 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1470 (void) consumer_flush_kernel_index(stream
);
1472 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1479 err
= consumer_stream_write_index(stream
, &index
);
1488 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1495 * Don't create anything if this is set for streaming or should not be
1498 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1499 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1500 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1501 stream
->uid
, stream
->gid
, NULL
);
1505 stream
->out_fd
= ret
;
1506 stream
->tracefile_size_current
= 0;
1508 if (!stream
->metadata_flag
) {
1509 struct lttng_index_file
*index_file
;
1511 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1512 stream
->name
, stream
->uid
, stream
->gid
,
1513 stream
->chan
->tracefile_size
,
1514 stream
->tracefile_count_current
,
1515 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1519 stream
->index_file
= index_file
;
1523 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1524 /* get the len of the mmap region */
1525 unsigned long mmap_len
;
1527 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1529 PERROR("kernctl_get_mmap_len");
1530 goto error_close_fd
;
1532 stream
->mmap_len
= (size_t) mmap_len
;
1534 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1535 MAP_PRIVATE
, stream
->wait_fd
, 0);
1536 if (stream
->mmap_base
== MAP_FAILED
) {
1537 PERROR("Error mmaping");
1539 goto error_close_fd
;
1543 /* we return 0 to let the library handle the FD internally */
1547 if (stream
->out_fd
>= 0) {
1550 err
= close(stream
->out_fd
);
1552 stream
->out_fd
= -1;
1559 * Check if data is still being extracted from the buffers for a specific
1560 * stream. Consumer data lock MUST be acquired before calling this function
1561 * and the stream lock.
1563 * Return 1 if the traced data are still getting read else 0 meaning that the
1564 * data is available for trace viewer reading.
1566 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1572 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1577 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1579 /* There is still data so let's put back this subbuffer. */
1580 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1582 ret
= 1; /* Data is pending */
1586 /* Data is NOT pending and ready to be read. */