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 /* Are we at a position _before_ the first available packet ? */
141 bool before_first_packet
= true;
142 unsigned long consumed_pos
, produced_pos
;
144 health_code_update();
147 * Lock stream because we are about to change its state.
149 pthread_mutex_lock(&stream
->lock
);
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
155 stream
->net_seq_idx
= relayd_id
;
156 channel
->relayd_id
= relayd_id
;
157 if (relayd_id
!= (uint64_t) -1ULL) {
158 ret
= consumer_send_relayd_stream(stream
, path
);
160 ERR("sending stream to relayd");
164 ret
= utils_create_stream_file(path
, stream
->name
,
165 stream
->chan
->tracefile_size
,
166 stream
->tracefile_count_current
,
167 stream
->uid
, stream
->gid
, NULL
);
169 ERR("utils_create_stream_file");
173 stream
->out_fd
= ret
;
174 stream
->tracefile_size_current
= 0;
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
177 path
, stream
->name
, stream
->key
);
179 if (relayd_id
!= -1ULL) {
180 ret
= consumer_send_relayd_streams_sent(relayd_id
);
182 ERR("sending streams sent to relayd");
187 ret
= kernctl_buffer_flush(stream
->wait_fd
);
189 ERR("Failed to flush kernel stream");
193 ret
= lttng_kconsumer_take_snapshot(stream
);
195 ERR("Taking kernel snapshot");
199 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
201 ERR("Produced kernel snapshot position");
205 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
207 ERR("Consumerd kernel snapshot position");
211 if (stream
->max_sb_size
== 0) {
212 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
213 &stream
->max_sb_size
);
215 ERR("Getting kernel max_sb_size");
220 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
221 produced_pos
, nb_packets_per_stream
,
222 stream
->max_sb_size
);
224 while (consumed_pos
< produced_pos
) {
226 unsigned long len
, padded_len
;
229 health_code_update();
231 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
233 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
235 if (ret
!= -EAGAIN
) {
236 PERROR("kernctl_get_subbuf snapshot");
239 DBG("Kernel consumer get subbuf failed. Skipping it.");
240 consumed_pos
+= stream
->max_sb_size
;
243 * Start accounting lost packets only when we
244 * already have extracted packets (to match the
245 * content of the final snapshot).
247 if (!before_first_packet
) {
253 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
255 ERR("Snapshot kernctl_get_subbuf_size");
256 goto error_put_subbuf
;
259 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
261 ERR("Snapshot kernctl_get_padded_subbuf_size");
262 goto error_put_subbuf
;
265 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
266 padded_len
- len
, NULL
);
268 * We write the padded len in local tracefiles but the data len
269 * when using a relay. Display the error but continue processing
270 * to try to release the subbuffer.
272 if (relayd_id
!= (uint64_t) -1ULL) {
273 if (read_len
!= len
) {
274 ERR("Error sending to the relay (ret: %zd != len: %lu)",
278 if (read_len
!= padded_len
) {
279 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
280 read_len
, padded_len
);
284 ret
= kernctl_put_subbuf(stream
->wait_fd
);
286 ERR("Snapshot kernctl_put_subbuf");
289 consumed_pos
+= stream
->max_sb_size
;
292 * Only account lost packets located between
293 * succesfully extracted packets (do not account before
294 * and after since they are not visible in the
295 * resulting snapshot).
297 stream
->chan
->lost_packets
+= lost_packet
;
299 before_first_packet
= false;
302 if (relayd_id
== (uint64_t) -1ULL) {
303 if (stream
->out_fd
>= 0) {
304 ret
= close(stream
->out_fd
);
306 PERROR("Kernel consumer snapshot close out_fd");
312 close_relayd_stream(stream
);
313 stream
->net_seq_idx
= (uint64_t) -1ULL;
315 pthread_mutex_unlock(&stream
->lock
);
323 ret
= kernctl_put_subbuf(stream
->wait_fd
);
325 ERR("Snapshot kernctl_put_subbuf error path");
328 pthread_mutex_unlock(&stream
->lock
);
335 * Read the whole metadata available for a snapshot.
337 * Returns 0 on success, < 0 on error
339 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
340 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
342 int ret
, use_relayd
= 0;
344 struct lttng_consumer_channel
*metadata_channel
;
345 struct lttng_consumer_stream
*metadata_stream
;
349 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
354 metadata_channel
= consumer_find_channel(key
);
355 if (!metadata_channel
) {
356 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
361 metadata_stream
= metadata_channel
->metadata_stream
;
362 assert(metadata_stream
);
364 /* Flag once that we have a valid relayd for the stream. */
365 if (relayd_id
!= (uint64_t) -1ULL) {
370 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
375 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
376 metadata_stream
->chan
->tracefile_size
,
377 metadata_stream
->tracefile_count_current
,
378 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
382 metadata_stream
->out_fd
= ret
;
386 health_code_update();
388 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
390 if (ret_read
!= -EAGAIN
) {
391 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
395 /* ret_read is negative at this point so we will exit the loop. */
398 } while (ret_read
>= 0);
401 close_relayd_stream(metadata_stream
);
402 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
404 if (metadata_stream
->out_fd
>= 0) {
405 ret
= close(metadata_stream
->out_fd
);
407 PERROR("Kernel consumer snapshot metadata close out_fd");
409 * Don't go on error here since the snapshot was successful at this
410 * point but somehow the close failed.
413 metadata_stream
->out_fd
= -1;
419 cds_list_del(&metadata_stream
->send_node
);
420 consumer_stream_destroy(metadata_stream
, NULL
);
421 metadata_channel
->metadata_stream
= NULL
;
428 * Receive command from session daemon and process it.
430 * Return 1 on success else a negative value or 0.
432 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
433 int sock
, struct pollfd
*consumer_sockpoll
)
436 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
437 struct lttcomm_consumer_msg msg
;
439 health_code_update();
441 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
442 if (ret
!= sizeof(msg
)) {
444 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
450 health_code_update();
452 /* Deprecated command */
453 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
455 health_code_update();
457 /* relayd needs RCU read-side protection */
460 switch (msg
.cmd_type
) {
461 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
463 /* Session daemon status message are handled in the following call. */
464 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
465 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
466 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
467 msg
.u
.relayd_sock
.relayd_session_id
);
470 case LTTNG_CONSUMER_ADD_CHANNEL
:
472 struct lttng_consumer_channel
*new_channel
;
475 health_code_update();
477 /* First send a status message before receiving the fds. */
478 ret
= consumer_send_status_msg(sock
, ret_code
);
480 /* Somehow, the session daemon is not responding anymore. */
484 health_code_update();
486 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
487 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
488 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
489 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
490 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
491 msg
.u
.channel
.tracefile_size
,
492 msg
.u
.channel
.tracefile_count
, 0,
493 msg
.u
.channel
.monitor
,
494 msg
.u
.channel
.live_timer_interval
,
496 if (new_channel
== NULL
) {
497 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
500 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
501 switch (msg
.u
.channel
.output
) {
502 case LTTNG_EVENT_SPLICE
:
503 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
505 case LTTNG_EVENT_MMAP
:
506 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
509 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
513 /* Translate and save channel type. */
514 switch (msg
.u
.channel
.type
) {
515 case CONSUMER_CHANNEL_TYPE_DATA
:
516 case CONSUMER_CHANNEL_TYPE_METADATA
:
517 new_channel
->type
= msg
.u
.channel
.type
;
524 health_code_update();
526 if (ctx
->on_recv_channel
!= NULL
) {
527 ret_recv
= ctx
->on_recv_channel(new_channel
);
529 ret
= consumer_add_channel(new_channel
, ctx
);
530 } else if (ret_recv
< 0) {
534 ret
= consumer_add_channel(new_channel
, ctx
);
536 if (CONSUMER_CHANNEL_TYPE_DATA
) {
537 consumer_timer_live_start(new_channel
,
538 msg
.u
.channel
.live_timer_interval
);
541 health_code_update();
543 /* If we received an error in add_channel, we need to report it. */
545 ret
= consumer_send_status_msg(sock
, ret
);
554 case LTTNG_CONSUMER_ADD_STREAM
:
557 struct lttng_pipe
*stream_pipe
;
558 struct lttng_consumer_stream
*new_stream
;
559 struct lttng_consumer_channel
*channel
;
563 * Get stream's channel reference. Needed when adding the stream to the
566 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
569 * We could not find the channel. Can happen if cpu hotplug
570 * happens while tearing down.
572 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
573 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
576 health_code_update();
578 /* First send a status message before receiving the fds. */
579 ret
= consumer_send_status_msg(sock
, ret_code
);
581 /* Somehow, the session daemon is not responding anymore. */
585 health_code_update();
587 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
588 /* Channel was not found. */
594 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
600 health_code_update();
602 /* Get stream file descriptor from socket */
603 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
604 if (ret
!= sizeof(fd
)) {
605 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
610 health_code_update();
613 * Send status code to session daemon only if the recv works. If the
614 * above recv() failed, the session daemon is notified through the
615 * error socket and the teardown is eventually done.
617 ret
= consumer_send_status_msg(sock
, ret_code
);
619 /* Somehow, the session daemon is not responding anymore. */
623 health_code_update();
625 new_stream
= consumer_allocate_stream(channel
->key
,
627 LTTNG_CONSUMER_ACTIVE_STREAM
,
637 if (new_stream
== NULL
) {
642 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
648 new_stream
->chan
= channel
;
649 new_stream
->wait_fd
= fd
;
650 switch (channel
->output
) {
651 case CONSUMER_CHANNEL_SPLICE
:
652 new_stream
->output
= LTTNG_EVENT_SPLICE
;
653 ret
= utils_create_pipe(new_stream
->splice_pipe
);
658 case CONSUMER_CHANNEL_MMAP
:
659 new_stream
->output
= LTTNG_EVENT_MMAP
;
662 ERR("Stream output unknown %d", channel
->output
);
667 * We've just assigned the channel to the stream so increment the
668 * refcount right now. We don't need to increment the refcount for
669 * streams in no monitor because we handle manually the cleanup of
670 * those. It is very important to make sure there is NO prior
671 * consumer_del_stream() calls or else the refcount will be unbalanced.
673 if (channel
->monitor
) {
674 uatomic_inc(&new_stream
->chan
->refcount
);
678 * The buffer flush is done on the session daemon side for the kernel
679 * so no need for the stream "hangup_flush_done" variable to be
680 * tracked. This is important for a kernel stream since we don't rely
681 * on the flush state of the stream to read data. It's not the case for
682 * user space tracing.
684 new_stream
->hangup_flush_done
= 0;
686 health_code_update();
688 if (ctx
->on_recv_stream
) {
689 ret
= ctx
->on_recv_stream(new_stream
);
691 consumer_stream_free(new_stream
);
696 health_code_update();
698 if (new_stream
->metadata_flag
) {
699 channel
->metadata_stream
= new_stream
;
702 /* Do not monitor this stream. */
703 if (!channel
->monitor
) {
704 DBG("Kernel consumer add stream %s in no monitor mode with "
705 "relayd id %" PRIu64
, new_stream
->name
,
706 new_stream
->net_seq_idx
);
707 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
711 /* Send stream to relayd if the stream has an ID. */
712 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
713 ret
= consumer_send_relayd_stream(new_stream
,
714 new_stream
->chan
->pathname
);
716 consumer_stream_free(new_stream
);
721 /* Get the right pipe where the stream will be sent. */
722 if (new_stream
->metadata_flag
) {
723 ret
= consumer_add_metadata_stream(new_stream
);
725 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
727 consumer_stream_free(new_stream
);
730 stream_pipe
= ctx
->consumer_metadata_pipe
;
732 ret
= consumer_add_data_stream(new_stream
);
734 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
736 consumer_stream_free(new_stream
);
739 stream_pipe
= ctx
->consumer_data_pipe
;
742 /* Vitible to other threads */
743 new_stream
->globally_visible
= 1;
745 health_code_update();
747 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
749 ERR("Consumer write %s stream to pipe %d",
750 new_stream
->metadata_flag
? "metadata" : "data",
751 lttng_pipe_get_writefd(stream_pipe
));
752 if (new_stream
->metadata_flag
) {
753 consumer_del_stream_for_metadata(new_stream
);
755 consumer_del_stream_for_data(new_stream
);
760 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
761 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
764 case LTTNG_CONSUMER_STREAMS_SENT
:
766 struct lttng_consumer_channel
*channel
;
769 * Get stream's channel reference. Needed when adding the stream to the
772 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
775 * We could not find the channel. Can happen if cpu hotplug
776 * happens while tearing down.
778 ERR("Unable to find channel key %" PRIu64
,
779 msg
.u
.sent_streams
.channel_key
);
780 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
783 health_code_update();
786 * Send status code to session daemon.
788 ret
= consumer_send_status_msg(sock
, ret_code
);
789 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
790 /* Somehow, the session daemon is not responding anymore. */
794 health_code_update();
797 * We should not send this message if we don't monitor the
798 * streams in this channel.
800 if (!channel
->monitor
) {
804 health_code_update();
805 /* Send stream to relayd if the stream has an ID. */
806 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
807 ret
= consumer_send_relayd_streams_sent(
808 msg
.u
.sent_streams
.net_seq_idx
);
815 case LTTNG_CONSUMER_UPDATE_STREAM
:
820 case LTTNG_CONSUMER_DESTROY_RELAYD
:
822 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
823 struct consumer_relayd_sock_pair
*relayd
;
825 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
827 /* Get relayd reference if exists. */
828 relayd
= consumer_find_relayd(index
);
829 if (relayd
== NULL
) {
830 DBG("Unable to find relayd %" PRIu64
, index
);
831 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
835 * Each relayd socket pair has a refcount of stream attached to it
836 * which tells if the relayd is still active or not depending on the
839 * This will set the destroy flag of the relayd object and destroy it
840 * if the refcount reaches zero when called.
842 * The destroy can happen either here or when a stream fd hangs up.
845 consumer_flag_relayd_for_destroy(relayd
);
848 health_code_update();
850 ret
= consumer_send_status_msg(sock
, ret_code
);
852 /* Somehow, the session daemon is not responding anymore. */
858 case LTTNG_CONSUMER_DATA_PENDING
:
861 uint64_t id
= msg
.u
.data_pending
.session_id
;
863 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
865 ret
= consumer_data_pending(id
);
867 health_code_update();
869 /* Send back returned value to session daemon */
870 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
872 PERROR("send data pending ret code");
877 * No need to send back a status message since the data pending
878 * returned value is the response.
882 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
884 if (msg
.u
.snapshot_channel
.metadata
== 1) {
885 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
886 msg
.u
.snapshot_channel
.pathname
,
887 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
889 ERR("Snapshot metadata failed");
890 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
893 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
894 msg
.u
.snapshot_channel
.pathname
,
895 msg
.u
.snapshot_channel
.relayd_id
,
896 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
899 ERR("Snapshot channel failed");
900 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
904 health_code_update();
906 ret
= consumer_send_status_msg(sock
, ret_code
);
908 /* Somehow, the session daemon is not responding anymore. */
913 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
915 uint64_t key
= msg
.u
.destroy_channel
.key
;
916 struct lttng_consumer_channel
*channel
;
918 channel
= consumer_find_channel(key
);
920 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
921 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
924 health_code_update();
926 ret
= consumer_send_status_msg(sock
, ret_code
);
928 /* Somehow, the session daemon is not responding anymore. */
932 health_code_update();
934 /* Stop right now if no channel was found. */
940 * This command should ONLY be issued for channel with streams set in
943 assert(!channel
->monitor
);
946 * The refcount should ALWAYS be 0 in the case of a channel in no
949 assert(!uatomic_sub_return(&channel
->refcount
, 1));
951 consumer_del_channel(channel
);
955 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
958 struct lttng_consumer_channel
*channel
;
959 uint64_t id
= msg
.u
.discarded_events
.session_id
;
960 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
962 DBG("Kernel consumer discarded events command for session id %"
963 PRIu64
", channel key %" PRIu64
, id
, key
);
965 channel
= consumer_find_channel(key
);
967 ERR("Kernel consumer discarded events channel %"
968 PRIu64
" not found", key
);
971 ret
= channel
->discarded_events
;
974 health_code_update();
976 /* Send back returned value to session daemon */
977 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
979 PERROR("send discarded events");
985 case LTTNG_CONSUMER_LOST_PACKETS
:
988 struct lttng_consumer_channel
*channel
;
989 uint64_t id
= msg
.u
.lost_packets
.session_id
;
990 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
992 DBG("Kernel consumer lost packets command for session id %"
993 PRIu64
", channel key %" PRIu64
, id
, key
);
995 channel
= consumer_find_channel(key
);
997 ERR("Kernel consumer lost packets channel %"
998 PRIu64
" not found", key
);
1001 ret
= channel
->lost_packets
;
1004 health_code_update();
1006 /* Send back returned value to session daemon */
1007 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1009 PERROR("send lost packets");
1023 * Return 1 to indicate success since the 0 value can be a socket
1024 * shutdown during the recv() or send() call.
1026 health_code_update();
1031 /* This will issue a consumer stop. */
1036 * Populate index values of a kernel stream. Values are set in big endian order.
1038 * Return 0 on success or else a negative value.
1040 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1044 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1046 PERROR("kernctl_get_timestamp_begin");
1049 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1051 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1053 PERROR("kernctl_get_timestamp_end");
1056 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1058 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1060 PERROR("kernctl_get_events_discarded");
1063 index
->events_discarded
= htobe64(index
->events_discarded
);
1065 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1067 PERROR("kernctl_get_content_size");
1070 index
->content_size
= htobe64(index
->content_size
);
1072 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1074 PERROR("kernctl_get_packet_size");
1077 index
->packet_size
= htobe64(index
->packet_size
);
1079 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1081 PERROR("kernctl_get_stream_id");
1084 index
->stream_id
= htobe64(index
->stream_id
);
1086 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1088 PERROR("kernctl_get_instance_id");
1091 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1093 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1095 PERROR("kernctl_get_sequence_number");
1098 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1104 * Sync metadata meaning request them to the session daemon and snapshot to the
1105 * metadata thread can consumer them.
1107 * Metadata stream lock MUST be acquired.
1109 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1110 * is empty or a negative value on error.
1112 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1118 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1120 ERR("Failed to flush kernel stream");
1124 ret
= kernctl_snapshot(metadata
->wait_fd
);
1126 if (ret
!= -EAGAIN
) {
1127 ERR("Sync metadata, taking kernel snapshot failed.");
1130 DBG("Sync metadata, no new kernel metadata");
1131 /* No new metadata, exit. */
1141 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1144 uint64_t seq
, discarded
;
1146 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1148 PERROR("kernctl_get_sequence_number");
1153 * Start the sequence when we extract the first packet in case we don't
1154 * start at 0 (for example if a consumer is not connected to the
1155 * session immediately after the beginning).
1157 if (stream
->last_sequence_number
== -1ULL) {
1158 stream
->last_sequence_number
= seq
;
1159 } else if (seq
> stream
->last_sequence_number
) {
1160 stream
->chan
->lost_packets
+= seq
-
1161 stream
->last_sequence_number
- 1;
1163 /* seq <= last_sequence_number */
1164 ERR("Sequence number inconsistent : prev = %" PRIu64
1165 ", current = %" PRIu64
,
1166 stream
->last_sequence_number
, seq
);
1170 stream
->last_sequence_number
= seq
;
1172 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1174 PERROR("kernctl_get_events_discarded");
1177 if (discarded
< stream
->last_discarded_events
) {
1179 * Overflow has occurred. We assume only one wrap-around
1182 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1183 stream
->last_discarded_events
+ discarded
;
1185 stream
->chan
->discarded_events
+= discarded
-
1186 stream
->last_discarded_events
;
1188 stream
->last_discarded_events
= discarded
;
1196 * Check if the local version of the metadata stream matches with the version
1197 * of the metadata stream in the kernel. If it was updated, set the reset flag
1201 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1204 uint64_t cur_version
;
1206 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1208 ERR("Failed to get the metadata version");
1212 if (stream
->metadata_version
== cur_version
) {
1217 DBG("New metadata version detected");
1218 stream
->metadata_version
= cur_version
;
1219 stream
->reset_metadata_flag
= 1;
1227 * Consume data on a file descriptor and write it on a trace file.
1229 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1230 struct lttng_consumer_local_data
*ctx
)
1232 unsigned long len
, subbuf_size
, padding
;
1233 int err
, write_index
= 1;
1235 int infd
= stream
->wait_fd
;
1236 struct ctf_packet_index index
;
1238 DBG("In read_subbuffer (infd : %d)", infd
);
1240 /* Get the next subbuffer */
1241 err
= kernctl_get_next_subbuf(infd
);
1244 * This is a debug message even for single-threaded consumer,
1245 * because poll() have more relaxed criterions than get subbuf,
1246 * so get_subbuf may fail for short race windows where poll()
1247 * would issue wakeups.
1249 DBG("Reserving sub buffer failed (everything is normal, "
1250 "it is due to concurrency)");
1255 /* Get the full subbuffer size including padding */
1256 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1258 PERROR("Getting sub-buffer len failed.");
1259 err
= kernctl_put_subbuf(infd
);
1261 if (err
== -EFAULT
) {
1262 PERROR("Error in unreserving sub buffer\n");
1263 } else if (err
== -EIO
) {
1264 /* Should never happen with newer LTTng versions */
1265 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1274 if (!stream
->metadata_flag
) {
1275 ret
= get_index_values(&index
, infd
);
1277 err
= kernctl_put_subbuf(infd
);
1279 if (err
== -EFAULT
) {
1280 PERROR("Error in unreserving sub buffer\n");
1281 } else if (err
== -EIO
) {
1282 /* Should never happen with newer LTTng versions */
1283 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1290 ret
= update_stream_stats(stream
);
1296 ret
= metadata_stream_check_version(infd
, stream
);
1302 switch (stream
->chan
->output
) {
1303 case CONSUMER_CHANNEL_SPLICE
:
1305 * XXX: The lttng-modules splice "actor" does not handle copying
1306 * partial pages hence only using the subbuffer size without the
1307 * padding makes the splice fail.
1312 /* splice the subbuffer to the tracefile */
1313 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1316 * XXX: Splice does not support network streaming so the return value
1317 * is simply checked against subbuf_size and not like the mmap() op.
1319 if (ret
!= subbuf_size
) {
1321 * display the error but continue processing to try
1322 * to release the subbuffer
1324 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1329 case CONSUMER_CHANNEL_MMAP
:
1330 /* Get subbuffer size without padding */
1331 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1333 PERROR("Getting sub-buffer len failed.");
1334 err
= kernctl_put_subbuf(infd
);
1336 if (err
== -EFAULT
) {
1337 PERROR("Error in unreserving sub buffer\n");
1338 } else if (err
== -EIO
) {
1339 /* Should never happen with newer LTTng versions */
1340 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1349 /* Make sure the tracer is not gone mad on us! */
1350 assert(len
>= subbuf_size
);
1352 padding
= len
- subbuf_size
;
1354 /* write the subbuffer to the tracefile */
1355 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1358 * The mmap operation should write subbuf_size amount of data when
1359 * network streaming or the full padding (len) size when we are _not_
1362 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1363 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1365 * Display the error but continue processing to try to release the
1366 * subbuffer. This is a DBG statement since this is possible to
1367 * happen without being a critical error.
1369 DBG("Error writing to tracefile "
1370 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1371 ret
, len
, subbuf_size
);
1376 ERR("Unknown output method");
1380 err
= kernctl_put_next_subbuf(infd
);
1382 if (err
== -EFAULT
) {
1383 PERROR("Error in unreserving sub buffer\n");
1384 } else if (err
== -EIO
) {
1385 /* Should never happen with newer LTTng versions */
1386 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1392 /* Write index if needed. */
1397 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1399 * In live, block until all the metadata is sent.
1401 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1402 assert(!stream
->missed_metadata_flush
);
1403 stream
->waiting_on_metadata
= true;
1404 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1406 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1408 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1409 stream
->waiting_on_metadata
= false;
1410 if (stream
->missed_metadata_flush
) {
1411 stream
->missed_metadata_flush
= false;
1412 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1413 (void) consumer_flush_kernel_index(stream
);
1415 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1422 err
= consumer_stream_write_index(stream
, &index
);
1431 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1438 * Don't create anything if this is set for streaming or should not be
1441 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1442 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1443 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1444 stream
->uid
, stream
->gid
, NULL
);
1448 stream
->out_fd
= ret
;
1449 stream
->tracefile_size_current
= 0;
1451 if (!stream
->metadata_flag
) {
1452 ret
= index_create_file(stream
->chan
->pathname
,
1453 stream
->name
, stream
->uid
, stream
->gid
,
1454 stream
->chan
->tracefile_size
,
1455 stream
->tracefile_count_current
);
1459 stream
->index_fd
= ret
;
1463 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1464 /* get the len of the mmap region */
1465 unsigned long mmap_len
;
1467 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1469 PERROR("kernctl_get_mmap_len");
1470 goto error_close_fd
;
1472 stream
->mmap_len
= (size_t) mmap_len
;
1474 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1475 MAP_PRIVATE
, stream
->wait_fd
, 0);
1476 if (stream
->mmap_base
== MAP_FAILED
) {
1477 PERROR("Error mmaping");
1479 goto error_close_fd
;
1483 /* we return 0 to let the library handle the FD internally */
1487 if (stream
->out_fd
>= 0) {
1490 err
= close(stream
->out_fd
);
1492 stream
->out_fd
= -1;
1499 * Check if data is still being extracted from the buffers for a specific
1500 * stream. Consumer data lock MUST be acquired before calling this function
1501 * and the stream lock.
1503 * Return 1 if the traced data are still getting read else 0 meaning that the
1504 * data is available for trace viewer reading.
1506 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1512 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1517 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1519 /* There is still data so let's put back this subbuffer. */
1520 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1522 ret
= 1; /* Data is pending */
1526 /* Data is NOT pending and ready to be read. */