2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/compat/endian.h>
40 #include <common/pipe.h>
41 #include <common/relayd/relayd.h>
42 #include <common/utils.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer/consumer-timer.h>
47 #include "kernel-consumer.h"
49 extern struct lttng_consumer_global_data consumer_data
;
50 extern int consumer_poll_timeout
;
51 extern volatile int consumer_quit
;
54 * Take a snapshot for a specific fd
56 * Returns 0 on success, < 0 on error
58 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
61 int infd
= stream
->wait_fd
;
63 ret
= kernctl_snapshot(infd
);
65 PERROR("Getting sub-buffer snapshot.");
72 * 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");
91 * Get the consumerd position
93 * Returns 0 on success, < 0 on error
95 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
99 int infd
= stream
->wait_fd
;
101 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
103 PERROR("kernctl_snapshot_get_consumed");
110 * Take a snapshot of all the stream of a channel
112 * Returns 0 on success, < 0 on error
114 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
115 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
116 struct lttng_consumer_local_data
*ctx
)
119 struct lttng_consumer_channel
*channel
;
120 struct lttng_consumer_stream
*stream
;
122 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
126 channel
= consumer_find_channel(key
);
128 ERR("No channel found for key %" PRIu64
, key
);
133 /* Splice is not supported yet for channel snapshot. */
134 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
135 ERR("Unsupported output %d", channel
->output
);
140 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
141 /* Are we at a position _before_ the first available packet ? */
142 bool before_first_packet
= true;
143 unsigned long consumed_pos
, produced_pos
;
145 health_code_update();
148 * Lock stream because we are about to change its state.
150 pthread_mutex_lock(&stream
->lock
);
153 * Assign the received relayd ID so we can use it for streaming. The streams
154 * are not visible to anyone so this is OK to change it.
156 stream
->net_seq_idx
= relayd_id
;
157 channel
->relayd_id
= relayd_id
;
158 if (relayd_id
!= (uint64_t) -1ULL) {
159 ret
= consumer_send_relayd_stream(stream
, path
);
161 ERR("sending stream to relayd");
165 ret
= utils_create_stream_file(path
, stream
->name
,
166 stream
->chan
->tracefile_size
,
167 stream
->tracefile_count_current
,
168 stream
->uid
, stream
->gid
, NULL
);
170 ERR("utils_create_stream_file");
174 stream
->out_fd
= ret
;
175 stream
->tracefile_size_current
= 0;
177 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
178 path
, stream
->name
, stream
->key
);
180 if (relayd_id
!= -1ULL) {
181 ret
= consumer_send_relayd_streams_sent(relayd_id
);
183 ERR("sending streams sent to relayd");
188 ret
= kernctl_buffer_flush(stream
->wait_fd
);
190 ERR("Failed to flush kernel stream");
194 ret
= lttng_kconsumer_take_snapshot(stream
);
196 ERR("Taking kernel snapshot");
200 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
202 ERR("Produced kernel snapshot position");
206 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
208 ERR("Consumerd kernel snapshot position");
212 if (stream
->max_sb_size
== 0) {
213 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
214 &stream
->max_sb_size
);
216 ERR("Getting kernel max_sb_size");
221 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
222 produced_pos
, nb_packets_per_stream
,
223 stream
->max_sb_size
);
225 while (consumed_pos
< produced_pos
) {
227 unsigned long len
, padded_len
;
230 health_code_update();
232 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
234 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
236 if (ret
!= -EAGAIN
) {
237 PERROR("kernctl_get_subbuf snapshot");
240 DBG("Kernel consumer get subbuf failed. Skipping it.");
241 consumed_pos
+= stream
->max_sb_size
;
244 * Start accounting lost packets only when we
245 * already have extracted packets (to match the
246 * content of the final snapshot).
248 if (!before_first_packet
) {
254 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
256 ERR("Snapshot kernctl_get_subbuf_size");
257 goto error_put_subbuf
;
260 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
262 ERR("Snapshot kernctl_get_padded_subbuf_size");
263 goto error_put_subbuf
;
266 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
267 padded_len
- len
, NULL
);
269 * We write the padded len in local tracefiles but the data len
270 * when using a relay. Display the error but continue processing
271 * to try to release the subbuffer.
273 if (relayd_id
!= (uint64_t) -1ULL) {
274 if (read_len
!= len
) {
275 ERR("Error sending to the relay (ret: %zd != len: %lu)",
279 if (read_len
!= padded_len
) {
280 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
281 read_len
, padded_len
);
285 ret
= kernctl_put_subbuf(stream
->wait_fd
);
287 ERR("Snapshot kernctl_put_subbuf");
290 consumed_pos
+= stream
->max_sb_size
;
293 * Only account lost packets located between
294 * succesfully extracted packets (do not account before
295 * and after since they are not visible in the
296 * resulting snapshot).
298 stream
->chan
->lost_packets
+= lost_packet
;
300 before_first_packet
= false;
303 if (relayd_id
== (uint64_t) -1ULL) {
304 if (stream
->out_fd
>= 0) {
305 ret
= close(stream
->out_fd
);
307 PERROR("Kernel consumer snapshot close out_fd");
313 close_relayd_stream(stream
);
314 stream
->net_seq_idx
= (uint64_t) -1ULL;
316 pthread_mutex_unlock(&stream
->lock
);
324 ret
= kernctl_put_subbuf(stream
->wait_fd
);
326 ERR("Snapshot kernctl_put_subbuf error path");
329 pthread_mutex_unlock(&stream
->lock
);
336 * Read the whole metadata available for a snapshot.
338 * Returns 0 on success, < 0 on error
340 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
341 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
343 int ret
, use_relayd
= 0;
345 struct lttng_consumer_channel
*metadata_channel
;
346 struct lttng_consumer_stream
*metadata_stream
;
350 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
355 metadata_channel
= consumer_find_channel(key
);
356 if (!metadata_channel
) {
357 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
362 metadata_stream
= metadata_channel
->metadata_stream
;
363 assert(metadata_stream
);
365 /* Flag once that we have a valid relayd for the stream. */
366 if (relayd_id
!= (uint64_t) -1ULL) {
371 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
376 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
377 metadata_stream
->chan
->tracefile_size
,
378 metadata_stream
->tracefile_count_current
,
379 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
383 metadata_stream
->out_fd
= ret
;
387 health_code_update();
389 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
391 if (ret_read
!= -EAGAIN
) {
392 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
396 /* ret_read is negative at this point so we will exit the loop. */
399 } while (ret_read
>= 0);
402 close_relayd_stream(metadata_stream
);
403 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
405 if (metadata_stream
->out_fd
>= 0) {
406 ret
= close(metadata_stream
->out_fd
);
408 PERROR("Kernel consumer snapshot metadata close out_fd");
410 * Don't go on error here since the snapshot was successful at this
411 * point but somehow the close failed.
414 metadata_stream
->out_fd
= -1;
420 cds_list_del(&metadata_stream
->send_node
);
421 consumer_stream_destroy(metadata_stream
, NULL
);
422 metadata_channel
->metadata_stream
= NULL
;
429 * Receive command from session daemon and process it.
431 * Return 1 on success else a negative value or 0.
433 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
434 int sock
, struct pollfd
*consumer_sockpoll
)
437 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
438 struct lttcomm_consumer_msg msg
;
440 health_code_update();
442 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
443 if (ret
!= sizeof(msg
)) {
445 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
451 health_code_update();
453 /* Deprecated command */
454 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
456 health_code_update();
458 /* relayd needs RCU read-side protection */
461 switch (msg
.cmd_type
) {
462 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
464 /* Session daemon status message are handled in the following call. */
465 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
466 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
467 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
468 msg
.u
.relayd_sock
.relayd_session_id
);
471 case LTTNG_CONSUMER_ADD_CHANNEL
:
473 struct lttng_consumer_channel
*new_channel
;
476 health_code_update();
478 /* First send a status message before receiving the fds. */
479 ret
= consumer_send_status_msg(sock
, ret_code
);
481 /* Somehow, the session daemon is not responding anymore. */
485 health_code_update();
487 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
488 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
489 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
490 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
491 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
492 msg
.u
.channel
.tracefile_size
,
493 msg
.u
.channel
.tracefile_count
, 0,
494 msg
.u
.channel
.monitor
,
495 msg
.u
.channel
.live_timer_interval
,
497 if (new_channel
== NULL
) {
498 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
501 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
502 switch (msg
.u
.channel
.output
) {
503 case LTTNG_EVENT_SPLICE
:
504 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
506 case LTTNG_EVENT_MMAP
:
507 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
510 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
514 /* Translate and save channel type. */
515 switch (msg
.u
.channel
.type
) {
516 case CONSUMER_CHANNEL_TYPE_DATA
:
517 case CONSUMER_CHANNEL_TYPE_METADATA
:
518 new_channel
->type
= msg
.u
.channel
.type
;
525 health_code_update();
527 if (ctx
->on_recv_channel
!= NULL
) {
528 ret_recv
= ctx
->on_recv_channel(new_channel
);
530 ret
= consumer_add_channel(new_channel
, ctx
);
531 } else if (ret_recv
< 0) {
535 ret
= consumer_add_channel(new_channel
, ctx
);
537 if (CONSUMER_CHANNEL_TYPE_DATA
) {
538 consumer_timer_live_start(new_channel
,
539 msg
.u
.channel
.live_timer_interval
);
542 health_code_update();
544 /* If we received an error in add_channel, we need to report it. */
546 ret
= consumer_send_status_msg(sock
, ret
);
555 case LTTNG_CONSUMER_ADD_STREAM
:
558 struct lttng_pipe
*stream_pipe
;
559 struct lttng_consumer_stream
*new_stream
;
560 struct lttng_consumer_channel
*channel
;
564 * Get stream's channel reference. Needed when adding the stream to the
567 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
570 * We could not find the channel. Can happen if cpu hotplug
571 * happens while tearing down.
573 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
574 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
577 health_code_update();
579 /* First send a status message before receiving the fds. */
580 ret
= consumer_send_status_msg(sock
, ret_code
);
582 /* Somehow, the session daemon is not responding anymore. */
586 health_code_update();
588 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
589 /* Channel was not found. */
595 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
601 health_code_update();
603 /* Get stream file descriptor from socket */
604 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
605 if (ret
!= sizeof(fd
)) {
606 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
611 health_code_update();
614 * Send status code to session daemon only if the recv works. If the
615 * above recv() failed, the session daemon is notified through the
616 * error socket and the teardown is eventually done.
618 ret
= consumer_send_status_msg(sock
, ret_code
);
620 /* Somehow, the session daemon is not responding anymore. */
624 health_code_update();
626 new_stream
= consumer_allocate_stream(channel
->key
,
628 LTTNG_CONSUMER_ACTIVE_STREAM
,
638 if (new_stream
== NULL
) {
643 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
649 new_stream
->chan
= channel
;
650 new_stream
->wait_fd
= fd
;
651 switch (channel
->output
) {
652 case CONSUMER_CHANNEL_SPLICE
:
653 new_stream
->output
= LTTNG_EVENT_SPLICE
;
654 ret
= utils_create_pipe(new_stream
->splice_pipe
);
659 case CONSUMER_CHANNEL_MMAP
:
660 new_stream
->output
= LTTNG_EVENT_MMAP
;
663 ERR("Stream output unknown %d", channel
->output
);
668 * We've just assigned the channel to the stream so increment the
669 * refcount right now. We don't need to increment the refcount for
670 * streams in no monitor because we handle manually the cleanup of
671 * those. It is very important to make sure there is NO prior
672 * consumer_del_stream() calls or else the refcount will be unbalanced.
674 if (channel
->monitor
) {
675 uatomic_inc(&new_stream
->chan
->refcount
);
679 * The buffer flush is done on the session daemon side for the kernel
680 * so no need for the stream "hangup_flush_done" variable to be
681 * tracked. This is important for a kernel stream since we don't rely
682 * on the flush state of the stream to read data. It's not the case for
683 * user space tracing.
685 new_stream
->hangup_flush_done
= 0;
687 health_code_update();
689 if (ctx
->on_recv_stream
) {
690 ret
= ctx
->on_recv_stream(new_stream
);
692 consumer_stream_free(new_stream
);
697 health_code_update();
699 if (new_stream
->metadata_flag
) {
700 channel
->metadata_stream
= new_stream
;
703 /* Do not monitor this stream. */
704 if (!channel
->monitor
) {
705 DBG("Kernel consumer add stream %s in no monitor mode with "
706 "relayd id %" PRIu64
, new_stream
->name
,
707 new_stream
->net_seq_idx
);
708 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
712 /* Send stream to relayd if the stream has an ID. */
713 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
714 ret
= consumer_send_relayd_stream(new_stream
,
715 new_stream
->chan
->pathname
);
717 consumer_stream_free(new_stream
);
722 /* Get the right pipe where the stream will be sent. */
723 if (new_stream
->metadata_flag
) {
724 ret
= consumer_add_metadata_stream(new_stream
);
726 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
728 consumer_stream_free(new_stream
);
731 stream_pipe
= ctx
->consumer_metadata_pipe
;
733 ret
= consumer_add_data_stream(new_stream
);
735 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
737 consumer_stream_free(new_stream
);
740 stream_pipe
= ctx
->consumer_data_pipe
;
743 /* Vitible to other threads */
744 new_stream
->globally_visible
= 1;
746 health_code_update();
748 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
750 ERR("Consumer write %s stream to pipe %d",
751 new_stream
->metadata_flag
? "metadata" : "data",
752 lttng_pipe_get_writefd(stream_pipe
));
753 if (new_stream
->metadata_flag
) {
754 consumer_del_stream_for_metadata(new_stream
);
756 consumer_del_stream_for_data(new_stream
);
761 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
762 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
765 case LTTNG_CONSUMER_STREAMS_SENT
:
767 struct lttng_consumer_channel
*channel
;
770 * Get stream's channel reference. Needed when adding the stream to the
773 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
776 * We could not find the channel. Can happen if cpu hotplug
777 * happens while tearing down.
779 ERR("Unable to find channel key %" PRIu64
,
780 msg
.u
.sent_streams
.channel_key
);
781 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
784 health_code_update();
787 * Send status code to session daemon.
789 ret
= consumer_send_status_msg(sock
, ret_code
);
790 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
791 /* Somehow, the session daemon is not responding anymore. */
795 health_code_update();
798 * We should not send this message if we don't monitor the
799 * streams in this channel.
801 if (!channel
->monitor
) {
805 health_code_update();
806 /* Send stream to relayd if the stream has an ID. */
807 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
808 ret
= consumer_send_relayd_streams_sent(
809 msg
.u
.sent_streams
.net_seq_idx
);
816 case LTTNG_CONSUMER_UPDATE_STREAM
:
821 case LTTNG_CONSUMER_DESTROY_RELAYD
:
823 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
824 struct consumer_relayd_sock_pair
*relayd
;
826 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
828 /* Get relayd reference if exists. */
829 relayd
= consumer_find_relayd(index
);
830 if (relayd
== NULL
) {
831 DBG("Unable to find relayd %" PRIu64
, index
);
832 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
836 * Each relayd socket pair has a refcount of stream attached to it
837 * which tells if the relayd is still active or not depending on the
840 * This will set the destroy flag of the relayd object and destroy it
841 * if the refcount reaches zero when called.
843 * The destroy can happen either here or when a stream fd hangs up.
846 consumer_flag_relayd_for_destroy(relayd
);
849 health_code_update();
851 ret
= consumer_send_status_msg(sock
, ret_code
);
853 /* Somehow, the session daemon is not responding anymore. */
859 case LTTNG_CONSUMER_DATA_PENDING
:
862 uint64_t id
= msg
.u
.data_pending
.session_id
;
864 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
866 ret
= consumer_data_pending(id
);
868 health_code_update();
870 /* Send back returned value to session daemon */
871 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
873 PERROR("send data pending ret code");
878 * No need to send back a status message since the data pending
879 * returned value is the response.
883 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
885 if (msg
.u
.snapshot_channel
.metadata
== 1) {
886 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
887 msg
.u
.snapshot_channel
.pathname
,
888 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
890 ERR("Snapshot metadata failed");
891 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
894 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
895 msg
.u
.snapshot_channel
.pathname
,
896 msg
.u
.snapshot_channel
.relayd_id
,
897 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
900 ERR("Snapshot channel failed");
901 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
905 health_code_update();
907 ret
= consumer_send_status_msg(sock
, ret_code
);
909 /* Somehow, the session daemon is not responding anymore. */
914 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
916 uint64_t key
= msg
.u
.destroy_channel
.key
;
917 struct lttng_consumer_channel
*channel
;
919 channel
= consumer_find_channel(key
);
921 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
922 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
925 health_code_update();
927 ret
= consumer_send_status_msg(sock
, ret_code
);
929 /* Somehow, the session daemon is not responding anymore. */
933 health_code_update();
935 /* Stop right now if no channel was found. */
941 * This command should ONLY be issued for channel with streams set in
944 assert(!channel
->monitor
);
947 * The refcount should ALWAYS be 0 in the case of a channel in no
950 assert(!uatomic_sub_return(&channel
->refcount
, 1));
952 consumer_del_channel(channel
);
956 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
959 struct lttng_consumer_channel
*channel
;
960 uint64_t id
= msg
.u
.discarded_events
.session_id
;
961 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
963 DBG("Kernel consumer discarded events command for session id %"
964 PRIu64
", channel key %" PRIu64
, id
, key
);
966 channel
= consumer_find_channel(key
);
968 ERR("Kernel consumer discarded events channel %"
969 PRIu64
" not found", key
);
972 ret
= channel
->discarded_events
;
975 health_code_update();
977 /* Send back returned value to session daemon */
978 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
980 PERROR("send discarded events");
986 case LTTNG_CONSUMER_LOST_PACKETS
:
989 struct lttng_consumer_channel
*channel
;
990 uint64_t id
= msg
.u
.lost_packets
.session_id
;
991 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
993 DBG("Kernel consumer lost packets command for session id %"
994 PRIu64
", channel key %" PRIu64
, id
, key
);
996 channel
= consumer_find_channel(key
);
998 ERR("Kernel consumer lost packets channel %"
999 PRIu64
" not found", key
);
1002 ret
= channel
->lost_packets
;
1005 health_code_update();
1007 /* Send back returned value to session daemon */
1008 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1010 PERROR("send lost packets");
1016 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1018 int channel_monitor_pipe
;
1020 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1021 /* Successfully received the command's type. */
1022 ret
= consumer_send_status_msg(sock
, ret_code
);
1027 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1029 if (ret
!= sizeof(channel_monitor_pipe
)) {
1030 ERR("Failed to receive channel monitor pipe");
1034 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1035 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1036 channel_monitor_pipe
);
1040 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1041 /* Set the pipe as non-blocking. */
1042 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1044 PERROR("fcntl get flags of the channel monitoring pipe");
1049 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1050 flags
| O_NONBLOCK
);
1052 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1055 DBG("Channel monitor pipe set as non-blocking");
1057 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1059 ret
= consumer_send_status_msg(sock
, ret_code
);
1073 * Return 1 to indicate success since the 0 value can be a socket
1074 * shutdown during the recv() or send() call.
1076 health_code_update();
1081 /* This will issue a consumer stop. */
1086 * Populate index values of a kernel stream. Values are set in big endian order.
1088 * Return 0 on success or else a negative value.
1090 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1094 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1096 PERROR("kernctl_get_timestamp_begin");
1099 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1101 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1103 PERROR("kernctl_get_timestamp_end");
1106 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1108 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1110 PERROR("kernctl_get_events_discarded");
1113 index
->events_discarded
= htobe64(index
->events_discarded
);
1115 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1117 PERROR("kernctl_get_content_size");
1120 index
->content_size
= htobe64(index
->content_size
);
1122 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1124 PERROR("kernctl_get_packet_size");
1127 index
->packet_size
= htobe64(index
->packet_size
);
1129 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1131 PERROR("kernctl_get_stream_id");
1134 index
->stream_id
= htobe64(index
->stream_id
);
1136 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1138 if (ret
== -ENOTTY
) {
1139 /* Command not implemented by lttng-modules. */
1140 index
->stream_instance_id
= -1ULL;
1143 PERROR("kernctl_get_instance_id");
1147 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1149 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1151 if (ret
== -ENOTTY
) {
1152 /* Command not implemented by lttng-modules. */
1153 index
->packet_seq_num
= -1ULL;
1156 PERROR("kernctl_get_sequence_number");
1160 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1166 * Sync metadata meaning request them to the session daemon and snapshot to the
1167 * metadata thread can consumer them.
1169 * Metadata stream lock MUST be acquired.
1171 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1172 * is empty or a negative value on error.
1174 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1180 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1182 ERR("Failed to flush kernel stream");
1186 ret
= kernctl_snapshot(metadata
->wait_fd
);
1188 if (ret
!= -EAGAIN
) {
1189 ERR("Sync metadata, taking kernel snapshot failed.");
1192 DBG("Sync metadata, no new kernel metadata");
1193 /* No new metadata, exit. */
1203 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1206 uint64_t seq
, discarded
;
1208 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1210 if (ret
== -ENOTTY
) {
1211 /* Command not implemented by lttng-modules. */
1215 PERROR("kernctl_get_sequence_number");
1221 * Start the sequence when we extract the first packet in case we don't
1222 * start at 0 (for example if a consumer is not connected to the
1223 * session immediately after the beginning).
1225 if (stream
->last_sequence_number
== -1ULL) {
1226 stream
->last_sequence_number
= seq
;
1227 } else if (seq
> stream
->last_sequence_number
) {
1228 stream
->chan
->lost_packets
+= seq
-
1229 stream
->last_sequence_number
- 1;
1231 /* seq <= last_sequence_number */
1232 ERR("Sequence number inconsistent : prev = %" PRIu64
1233 ", current = %" PRIu64
,
1234 stream
->last_sequence_number
, seq
);
1238 stream
->last_sequence_number
= seq
;
1240 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1242 PERROR("kernctl_get_events_discarded");
1245 if (discarded
< stream
->last_discarded_events
) {
1247 * Overflow has occurred. We assume only one wrap-around
1250 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1251 stream
->last_discarded_events
+ discarded
;
1253 stream
->chan
->discarded_events
+= discarded
-
1254 stream
->last_discarded_events
;
1256 stream
->last_discarded_events
= discarded
;
1264 * Check if the local version of the metadata stream matches with the version
1265 * of the metadata stream in the kernel. If it was updated, set the reset flag
1269 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1272 uint64_t cur_version
;
1274 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1276 if (ret
== -ENOTTY
) {
1278 * LTTng-modules does not implement this
1284 ERR("Failed to get the metadata version");
1288 if (stream
->metadata_version
== cur_version
) {
1293 DBG("New metadata version detected");
1294 stream
->metadata_version
= cur_version
;
1295 stream
->reset_metadata_flag
= 1;
1303 * Consume data on a file descriptor and write it on a trace file.
1305 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1306 struct lttng_consumer_local_data
*ctx
)
1308 unsigned long len
, subbuf_size
, padding
;
1309 int err
, write_index
= 1;
1311 int infd
= stream
->wait_fd
;
1312 struct ctf_packet_index index
;
1314 DBG("In read_subbuffer (infd : %d)", infd
);
1316 /* Get the next subbuffer */
1317 err
= kernctl_get_next_subbuf(infd
);
1320 * This is a debug message even for single-threaded consumer,
1321 * because poll() have more relaxed criterions than get subbuf,
1322 * so get_subbuf may fail for short race windows where poll()
1323 * would issue wakeups.
1325 DBG("Reserving sub buffer failed (everything is normal, "
1326 "it is due to concurrency)");
1331 /* Get the full subbuffer size including padding */
1332 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1334 PERROR("Getting sub-buffer len failed.");
1335 err
= kernctl_put_subbuf(infd
);
1337 if (err
== -EFAULT
) {
1338 PERROR("Error in unreserving sub buffer\n");
1339 } else if (err
== -EIO
) {
1340 /* Should never happen with newer LTTng versions */
1341 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1350 if (!stream
->metadata_flag
) {
1351 ret
= get_index_values(&index
, infd
);
1353 err
= kernctl_put_subbuf(infd
);
1355 if (err
== -EFAULT
) {
1356 PERROR("Error in unreserving sub buffer\n");
1357 } else if (err
== -EIO
) {
1358 /* Should never happen with newer LTTng versions */
1359 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1366 ret
= update_stream_stats(stream
);
1368 err
= kernctl_put_subbuf(infd
);
1370 if (err
== -EFAULT
) {
1371 PERROR("Error in unreserving sub buffer\n");
1372 } else if (err
== -EIO
) {
1373 /* Should never happen with newer LTTng versions */
1374 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1383 ret
= metadata_stream_check_version(infd
, stream
);
1385 err
= kernctl_put_subbuf(infd
);
1387 if (err
== -EFAULT
) {
1388 PERROR("Error in unreserving sub buffer\n");
1389 } else if (err
== -EIO
) {
1390 /* Should never happen with newer LTTng versions */
1391 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1400 switch (stream
->chan
->output
) {
1401 case CONSUMER_CHANNEL_SPLICE
:
1403 * XXX: The lttng-modules splice "actor" does not handle copying
1404 * partial pages hence only using the subbuffer size without the
1405 * padding makes the splice fail.
1410 /* splice the subbuffer to the tracefile */
1411 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1414 * XXX: Splice does not support network streaming so the return value
1415 * is simply checked against subbuf_size and not like the mmap() op.
1417 if (ret
!= subbuf_size
) {
1419 * display the error but continue processing to try
1420 * to release the subbuffer
1422 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1427 case CONSUMER_CHANNEL_MMAP
:
1428 /* Get subbuffer size without padding */
1429 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1431 PERROR("Getting sub-buffer len failed.");
1432 err
= kernctl_put_subbuf(infd
);
1434 if (err
== -EFAULT
) {
1435 PERROR("Error in unreserving sub buffer\n");
1436 } else if (err
== -EIO
) {
1437 /* Should never happen with newer LTTng versions */
1438 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1447 /* Make sure the tracer is not gone mad on us! */
1448 assert(len
>= subbuf_size
);
1450 padding
= len
- subbuf_size
;
1452 /* write the subbuffer to the tracefile */
1453 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1456 * The mmap operation should write subbuf_size amount of data when
1457 * network streaming or the full padding (len) size when we are _not_
1460 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1461 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1463 * Display the error but continue processing to try to release the
1464 * subbuffer. This is a DBG statement since this is possible to
1465 * happen without being a critical error.
1467 DBG("Error writing to tracefile "
1468 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1469 ret
, len
, subbuf_size
);
1474 ERR("Unknown output method");
1478 err
= kernctl_put_next_subbuf(infd
);
1480 if (err
== -EFAULT
) {
1481 PERROR("Error in unreserving sub buffer\n");
1482 } else if (err
== -EIO
) {
1483 /* Should never happen with newer LTTng versions */
1484 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1490 /* Write index if needed. */
1495 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1497 * In live, block until all the metadata is sent.
1499 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1500 assert(!stream
->missed_metadata_flush
);
1501 stream
->waiting_on_metadata
= true;
1502 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1504 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1506 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1507 stream
->waiting_on_metadata
= false;
1508 if (stream
->missed_metadata_flush
) {
1509 stream
->missed_metadata_flush
= false;
1510 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1511 (void) consumer_flush_kernel_index(stream
);
1513 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1520 err
= consumer_stream_write_index(stream
, &index
);
1529 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1536 * Don't create anything if this is set for streaming or should not be
1539 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1540 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1541 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1542 stream
->uid
, stream
->gid
, NULL
);
1546 stream
->out_fd
= ret
;
1547 stream
->tracefile_size_current
= 0;
1549 if (!stream
->metadata_flag
) {
1550 struct lttng_index_file
*index_file
;
1552 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1553 stream
->name
, stream
->uid
, stream
->gid
,
1554 stream
->chan
->tracefile_size
,
1555 stream
->tracefile_count_current
,
1556 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1560 assert(!stream
->index_file
);
1561 stream
->index_file
= index_file
;
1565 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1566 /* get the len of the mmap region */
1567 unsigned long mmap_len
;
1569 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1571 PERROR("kernctl_get_mmap_len");
1572 goto error_close_fd
;
1574 stream
->mmap_len
= (size_t) mmap_len
;
1576 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1577 MAP_PRIVATE
, stream
->wait_fd
, 0);
1578 if (stream
->mmap_base
== MAP_FAILED
) {
1579 PERROR("Error mmaping");
1581 goto error_close_fd
;
1585 /* we return 0 to let the library handle the FD internally */
1589 if (stream
->out_fd
>= 0) {
1592 err
= close(stream
->out_fd
);
1594 stream
->out_fd
= -1;
1601 * Check if data is still being extracted from the buffers for a specific
1602 * stream. Consumer data lock MUST be acquired before calling this function
1603 * and the stream lock.
1605 * Return 1 if the traced data are still getting read else 0 meaning that the
1606 * data is available for trace viewer reading.
1608 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1614 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1619 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1621 /* There is still data so let's put back this subbuffer. */
1622 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1624 ret
= 1; /* Data is pending */
1628 /* Data is NOT pending and ready to be read. */