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 * Sample consumed and produced positions for a specific fd.
74 * Returns 0 on success, < 0 on error.
76 int lttng_kconsumer_sample_snapshot_positions(
77 struct lttng_consumer_stream
*stream
)
81 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
85 * Get the produced position
87 * Returns 0 on success, < 0 on error
89 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
93 int infd
= stream
->wait_fd
;
95 ret
= kernctl_snapshot_get_produced(infd
, pos
);
97 PERROR("kernctl_snapshot_get_produced");
104 * Get the consumerd position
106 * Returns 0 on success, < 0 on error
108 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
112 int infd
= stream
->wait_fd
;
114 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
116 PERROR("kernctl_snapshot_get_consumed");
123 * Take a snapshot of all the stream of a channel
125 * Returns 0 on success, < 0 on error
127 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
128 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
129 struct lttng_consumer_local_data
*ctx
)
132 struct lttng_consumer_channel
*channel
;
133 struct lttng_consumer_stream
*stream
;
135 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
139 channel
= consumer_find_channel(key
);
141 ERR("No channel found for key %" PRIu64
, key
);
146 /* Splice is not supported yet for channel snapshot. */
147 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
148 ERR("Unsupported output %d", channel
->output
);
153 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
154 /* Are we at a position _before_ the first available packet ? */
155 bool before_first_packet
= true;
156 unsigned long consumed_pos
, produced_pos
;
158 health_code_update();
161 * Lock stream because we are about to change its state.
163 pthread_mutex_lock(&stream
->lock
);
166 * Assign the received relayd ID so we can use it for streaming. The streams
167 * are not visible to anyone so this is OK to change it.
169 stream
->net_seq_idx
= relayd_id
;
170 channel
->relayd_id
= relayd_id
;
171 if (relayd_id
!= (uint64_t) -1ULL) {
172 ret
= consumer_send_relayd_stream(stream
, path
);
174 ERR("sending stream to relayd");
178 ret
= utils_create_stream_file(path
, stream
->name
,
179 stream
->chan
->tracefile_size
,
180 stream
->tracefile_count_current
,
181 stream
->uid
, stream
->gid
, NULL
);
183 ERR("utils_create_stream_file");
187 stream
->out_fd
= ret
;
188 stream
->tracefile_size_current
= 0;
190 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
191 path
, stream
->name
, stream
->key
);
193 if (relayd_id
!= -1ULL) {
194 ret
= consumer_send_relayd_streams_sent(relayd_id
);
196 ERR("sending streams sent to relayd");
201 ret
= kernctl_buffer_flush(stream
->wait_fd
);
203 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
;
243 health_code_update();
245 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
247 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
249 if (ret
!= -EAGAIN
) {
250 PERROR("kernctl_get_subbuf snapshot");
253 DBG("Kernel consumer get subbuf failed. Skipping it.");
254 consumed_pos
+= stream
->max_sb_size
;
257 * Start accounting lost packets only when we
258 * already have extracted packets (to match the
259 * content of the final snapshot).
261 if (!before_first_packet
) {
267 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
269 ERR("Snapshot kernctl_get_subbuf_size");
270 goto error_put_subbuf
;
273 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
275 ERR("Snapshot kernctl_get_padded_subbuf_size");
276 goto error_put_subbuf
;
279 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
280 padded_len
- len
, NULL
);
282 * We write the padded len in local tracefiles but the data len
283 * when using a relay. Display the error but continue processing
284 * to try to release the subbuffer.
286 if (relayd_id
!= (uint64_t) -1ULL) {
287 if (read_len
!= len
) {
288 ERR("Error sending to the relay (ret: %zd != len: %lu)",
292 if (read_len
!= padded_len
) {
293 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
294 read_len
, padded_len
);
298 ret
= kernctl_put_subbuf(stream
->wait_fd
);
300 ERR("Snapshot kernctl_put_subbuf");
303 consumed_pos
+= stream
->max_sb_size
;
306 * Only account lost packets located between
307 * succesfully extracted packets (do not account before
308 * and after since they are not visible in the
309 * resulting snapshot).
311 stream
->chan
->lost_packets
+= lost_packet
;
313 before_first_packet
= false;
316 if (relayd_id
== (uint64_t) -1ULL) {
317 if (stream
->out_fd
>= 0) {
318 ret
= close(stream
->out_fd
);
320 PERROR("Kernel consumer snapshot close out_fd");
326 close_relayd_stream(stream
);
327 stream
->net_seq_idx
= (uint64_t) -1ULL;
329 pthread_mutex_unlock(&stream
->lock
);
337 ret
= kernctl_put_subbuf(stream
->wait_fd
);
339 ERR("Snapshot kernctl_put_subbuf error path");
342 pthread_mutex_unlock(&stream
->lock
);
349 * Read the whole metadata available for a snapshot.
351 * Returns 0 on success, < 0 on error
353 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
354 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
356 int ret
, use_relayd
= 0;
358 struct lttng_consumer_channel
*metadata_channel
;
359 struct lttng_consumer_stream
*metadata_stream
;
363 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
368 metadata_channel
= consumer_find_channel(key
);
369 if (!metadata_channel
) {
370 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
375 metadata_stream
= metadata_channel
->metadata_stream
;
376 assert(metadata_stream
);
378 /* Flag once that we have a valid relayd for the stream. */
379 if (relayd_id
!= (uint64_t) -1ULL) {
384 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
389 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
390 metadata_stream
->chan
->tracefile_size
,
391 metadata_stream
->tracefile_count_current
,
392 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
396 metadata_stream
->out_fd
= ret
;
400 health_code_update();
402 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
404 if (ret_read
!= -EAGAIN
) {
405 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
409 /* ret_read is negative at this point so we will exit the loop. */
412 } while (ret_read
>= 0);
415 close_relayd_stream(metadata_stream
);
416 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
418 if (metadata_stream
->out_fd
>= 0) {
419 ret
= close(metadata_stream
->out_fd
);
421 PERROR("Kernel consumer snapshot metadata close out_fd");
423 * Don't go on error here since the snapshot was successful at this
424 * point but somehow the close failed.
427 metadata_stream
->out_fd
= -1;
433 cds_list_del(&metadata_stream
->send_node
);
434 consumer_stream_destroy(metadata_stream
, NULL
);
435 metadata_channel
->metadata_stream
= NULL
;
442 * Receive command from session daemon and process it.
444 * Return 1 on success else a negative value or 0.
446 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
447 int sock
, struct pollfd
*consumer_sockpoll
)
450 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
451 struct lttcomm_consumer_msg msg
;
453 health_code_update();
455 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
456 if (ret
!= sizeof(msg
)) {
458 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
464 health_code_update();
466 /* Deprecated command */
467 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
469 health_code_update();
471 /* relayd needs RCU read-side protection */
474 switch (msg
.cmd_type
) {
475 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
477 /* Session daemon status message are handled in the following call. */
478 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
479 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
480 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
481 msg
.u
.relayd_sock
.relayd_session_id
);
484 case LTTNG_CONSUMER_ADD_CHANNEL
:
486 struct lttng_consumer_channel
*new_channel
;
489 health_code_update();
491 /* First send a status message before receiving the fds. */
492 ret
= consumer_send_status_msg(sock
, ret_code
);
494 /* Somehow, the session daemon is not responding anymore. */
498 health_code_update();
500 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
501 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
502 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
503 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
504 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
505 msg
.u
.channel
.tracefile_size
,
506 msg
.u
.channel
.tracefile_count
, 0,
507 msg
.u
.channel
.monitor
,
508 msg
.u
.channel
.live_timer_interval
,
510 if (new_channel
== NULL
) {
511 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
514 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
515 switch (msg
.u
.channel
.output
) {
516 case LTTNG_EVENT_SPLICE
:
517 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
519 case LTTNG_EVENT_MMAP
:
520 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
523 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
527 /* Translate and save channel type. */
528 switch (msg
.u
.channel
.type
) {
529 case CONSUMER_CHANNEL_TYPE_DATA
:
530 case CONSUMER_CHANNEL_TYPE_METADATA
:
531 new_channel
->type
= msg
.u
.channel
.type
;
538 health_code_update();
540 if (ctx
->on_recv_channel
!= NULL
) {
541 ret_recv
= ctx
->on_recv_channel(new_channel
);
543 ret
= consumer_add_channel(new_channel
, ctx
);
544 } else if (ret_recv
< 0) {
548 ret
= consumer_add_channel(new_channel
, ctx
);
550 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
551 int monitor_start_ret
;
553 DBG("Consumer starting monitor timer");
554 consumer_timer_live_start(new_channel
,
555 msg
.u
.channel
.live_timer_interval
);
556 monitor_start_ret
= consumer_timer_monitor_start(
558 msg
.u
.channel
.monitor_timer_interval
);
559 if (monitor_start_ret
< 0) {
560 ERR("Starting channel monitoring timer failed");
566 health_code_update();
568 /* If we received an error in add_channel, we need to report it. */
570 ret
= consumer_send_status_msg(sock
, ret
);
579 case LTTNG_CONSUMER_ADD_STREAM
:
582 struct lttng_pipe
*stream_pipe
;
583 struct lttng_consumer_stream
*new_stream
;
584 struct lttng_consumer_channel
*channel
;
588 * Get stream's channel reference. Needed when adding the stream to the
591 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
594 * We could not find the channel. Can happen if cpu hotplug
595 * happens while tearing down.
597 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
598 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
601 health_code_update();
603 /* First send a status message before receiving the fds. */
604 ret
= consumer_send_status_msg(sock
, ret_code
);
606 /* Somehow, the session daemon is not responding anymore. */
610 health_code_update();
612 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
613 /* Channel was not found. */
619 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
625 health_code_update();
627 /* Get stream file descriptor from socket */
628 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
629 if (ret
!= sizeof(fd
)) {
630 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
635 health_code_update();
638 * Send status code to session daemon only if the recv works. If the
639 * above recv() failed, the session daemon is notified through the
640 * error socket and the teardown is eventually done.
642 ret
= consumer_send_status_msg(sock
, ret_code
);
644 /* Somehow, the session daemon is not responding anymore. */
648 health_code_update();
650 new_stream
= consumer_allocate_stream(channel
->key
,
652 LTTNG_CONSUMER_ACTIVE_STREAM
,
662 if (new_stream
== NULL
) {
667 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
673 new_stream
->chan
= channel
;
674 new_stream
->wait_fd
= fd
;
675 switch (channel
->output
) {
676 case CONSUMER_CHANNEL_SPLICE
:
677 new_stream
->output
= LTTNG_EVENT_SPLICE
;
678 ret
= utils_create_pipe(new_stream
->splice_pipe
);
683 case CONSUMER_CHANNEL_MMAP
:
684 new_stream
->output
= LTTNG_EVENT_MMAP
;
687 ERR("Stream output unknown %d", channel
->output
);
692 * We've just assigned the channel to the stream so increment the
693 * refcount right now. We don't need to increment the refcount for
694 * streams in no monitor because we handle manually the cleanup of
695 * those. It is very important to make sure there is NO prior
696 * consumer_del_stream() calls or else the refcount will be unbalanced.
698 if (channel
->monitor
) {
699 uatomic_inc(&new_stream
->chan
->refcount
);
703 * The buffer flush is done on the session daemon side for the kernel
704 * so no need for the stream "hangup_flush_done" variable to be
705 * tracked. This is important for a kernel stream since we don't rely
706 * on the flush state of the stream to read data. It's not the case for
707 * user space tracing.
709 new_stream
->hangup_flush_done
= 0;
711 health_code_update();
713 if (ctx
->on_recv_stream
) {
714 ret
= ctx
->on_recv_stream(new_stream
);
716 consumer_stream_free(new_stream
);
721 health_code_update();
723 if (new_stream
->metadata_flag
) {
724 channel
->metadata_stream
= new_stream
;
727 /* Do not monitor this stream. */
728 if (!channel
->monitor
) {
729 DBG("Kernel consumer add stream %s in no monitor mode with "
730 "relayd id %" PRIu64
, new_stream
->name
,
731 new_stream
->net_seq_idx
);
732 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
736 /* Send stream to relayd if the stream has an ID. */
737 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
738 ret
= consumer_send_relayd_stream(new_stream
,
739 new_stream
->chan
->pathname
);
741 consumer_stream_free(new_stream
);
746 /* Get the right pipe where the stream will be sent. */
747 if (new_stream
->metadata_flag
) {
748 ret
= consumer_add_metadata_stream(new_stream
);
750 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
752 consumer_stream_free(new_stream
);
755 stream_pipe
= ctx
->consumer_metadata_pipe
;
757 ret
= consumer_add_data_stream(new_stream
);
759 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
761 consumer_stream_free(new_stream
);
764 stream_pipe
= ctx
->consumer_data_pipe
;
767 /* Vitible to other threads */
768 new_stream
->globally_visible
= 1;
770 health_code_update();
772 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
774 ERR("Consumer write %s stream to pipe %d",
775 new_stream
->metadata_flag
? "metadata" : "data",
776 lttng_pipe_get_writefd(stream_pipe
));
777 if (new_stream
->metadata_flag
) {
778 consumer_del_stream_for_metadata(new_stream
);
780 consumer_del_stream_for_data(new_stream
);
785 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
786 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
789 case LTTNG_CONSUMER_STREAMS_SENT
:
791 struct lttng_consumer_channel
*channel
;
794 * Get stream's channel reference. Needed when adding the stream to the
797 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
800 * We could not find the channel. Can happen if cpu hotplug
801 * happens while tearing down.
803 ERR("Unable to find channel key %" PRIu64
,
804 msg
.u
.sent_streams
.channel_key
);
805 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
808 health_code_update();
811 * Send status code to session daemon.
813 ret
= consumer_send_status_msg(sock
, ret_code
);
814 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
815 /* Somehow, the session daemon is not responding anymore. */
819 health_code_update();
822 * We should not send this message if we don't monitor the
823 * streams in this channel.
825 if (!channel
->monitor
) {
829 health_code_update();
830 /* Send stream to relayd if the stream has an ID. */
831 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
832 ret
= consumer_send_relayd_streams_sent(
833 msg
.u
.sent_streams
.net_seq_idx
);
840 case LTTNG_CONSUMER_UPDATE_STREAM
:
845 case LTTNG_CONSUMER_DESTROY_RELAYD
:
847 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
848 struct consumer_relayd_sock_pair
*relayd
;
850 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
852 /* Get relayd reference if exists. */
853 relayd
= consumer_find_relayd(index
);
854 if (relayd
== NULL
) {
855 DBG("Unable to find relayd %" PRIu64
, index
);
856 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
860 * Each relayd socket pair has a refcount of stream attached to it
861 * which tells if the relayd is still active or not depending on the
864 * This will set the destroy flag of the relayd object and destroy it
865 * if the refcount reaches zero when called.
867 * The destroy can happen either here or when a stream fd hangs up.
870 consumer_flag_relayd_for_destroy(relayd
);
873 health_code_update();
875 ret
= consumer_send_status_msg(sock
, ret_code
);
877 /* Somehow, the session daemon is not responding anymore. */
883 case LTTNG_CONSUMER_DATA_PENDING
:
886 uint64_t id
= msg
.u
.data_pending
.session_id
;
888 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
890 ret
= consumer_data_pending(id
);
892 health_code_update();
894 /* Send back returned value to session daemon */
895 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
897 PERROR("send data pending ret code");
902 * No need to send back a status message since the data pending
903 * returned value is the response.
907 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
909 if (msg
.u
.snapshot_channel
.metadata
== 1) {
910 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
911 msg
.u
.snapshot_channel
.pathname
,
912 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
914 ERR("Snapshot metadata failed");
915 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
918 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
919 msg
.u
.snapshot_channel
.pathname
,
920 msg
.u
.snapshot_channel
.relayd_id
,
921 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
924 ERR("Snapshot channel failed");
925 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
929 health_code_update();
931 ret
= consumer_send_status_msg(sock
, ret_code
);
933 /* Somehow, the session daemon is not responding anymore. */
938 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
940 uint64_t key
= msg
.u
.destroy_channel
.key
;
941 struct lttng_consumer_channel
*channel
;
943 channel
= consumer_find_channel(key
);
945 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
946 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
949 health_code_update();
951 ret
= consumer_send_status_msg(sock
, ret_code
);
953 /* Somehow, the session daemon is not responding anymore. */
957 health_code_update();
959 /* Stop right now if no channel was found. */
965 * This command should ONLY be issued for channel with streams set in
968 assert(!channel
->monitor
);
971 * The refcount should ALWAYS be 0 in the case of a channel in no
974 assert(!uatomic_sub_return(&channel
->refcount
, 1));
976 consumer_del_channel(channel
);
980 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
983 struct lttng_consumer_channel
*channel
;
984 uint64_t id
= msg
.u
.discarded_events
.session_id
;
985 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
987 DBG("Kernel consumer discarded events command for session id %"
988 PRIu64
", channel key %" PRIu64
, id
, key
);
990 channel
= consumer_find_channel(key
);
992 ERR("Kernel consumer discarded events channel %"
993 PRIu64
" not found", key
);
996 ret
= channel
->discarded_events
;
999 health_code_update();
1001 /* Send back returned value to session daemon */
1002 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1004 PERROR("send discarded events");
1010 case LTTNG_CONSUMER_LOST_PACKETS
:
1013 struct lttng_consumer_channel
*channel
;
1014 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1015 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1017 DBG("Kernel consumer lost packets command for session id %"
1018 PRIu64
", channel key %" PRIu64
, id
, key
);
1020 channel
= consumer_find_channel(key
);
1022 ERR("Kernel consumer lost packets channel %"
1023 PRIu64
" not found", key
);
1026 ret
= channel
->lost_packets
;
1029 health_code_update();
1031 /* Send back returned value to session daemon */
1032 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1034 PERROR("send lost packets");
1040 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1042 int channel_monitor_pipe
;
1044 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1045 /* Successfully received the command's type. */
1046 ret
= consumer_send_status_msg(sock
, ret_code
);
1051 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1053 if (ret
!= sizeof(channel_monitor_pipe
)) {
1054 ERR("Failed to receive channel monitor pipe");
1058 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1059 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1060 channel_monitor_pipe
);
1064 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1065 /* Set the pipe as non-blocking. */
1066 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1068 PERROR("fcntl get flags of the channel monitoring pipe");
1073 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1074 flags
| O_NONBLOCK
);
1076 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1079 DBG("Channel monitor pipe set as non-blocking");
1081 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1083 ret
= consumer_send_status_msg(sock
, ret_code
);
1097 * Return 1 to indicate success since the 0 value can be a socket
1098 * shutdown during the recv() or send() call.
1100 health_code_update();
1105 /* This will issue a consumer stop. */
1110 * Populate index values of a kernel stream. Values are set in big endian order.
1112 * Return 0 on success or else a negative value.
1114 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1118 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1120 PERROR("kernctl_get_timestamp_begin");
1123 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1125 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1127 PERROR("kernctl_get_timestamp_end");
1130 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1132 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1134 PERROR("kernctl_get_events_discarded");
1137 index
->events_discarded
= htobe64(index
->events_discarded
);
1139 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1141 PERROR("kernctl_get_content_size");
1144 index
->content_size
= htobe64(index
->content_size
);
1146 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1148 PERROR("kernctl_get_packet_size");
1151 index
->packet_size
= htobe64(index
->packet_size
);
1153 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1155 PERROR("kernctl_get_stream_id");
1158 index
->stream_id
= htobe64(index
->stream_id
);
1160 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1162 if (ret
== -ENOTTY
) {
1163 /* Command not implemented by lttng-modules. */
1164 index
->stream_instance_id
= -1ULL;
1167 PERROR("kernctl_get_instance_id");
1171 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1173 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1175 if (ret
== -ENOTTY
) {
1176 /* Command not implemented by lttng-modules. */
1177 index
->packet_seq_num
= -1ULL;
1180 PERROR("kernctl_get_sequence_number");
1184 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1190 * Sync metadata meaning request them to the session daemon and snapshot to the
1191 * metadata thread can consumer them.
1193 * Metadata stream lock MUST be acquired.
1195 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1196 * is empty or a negative value on error.
1198 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1204 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1206 ERR("Failed to flush kernel stream");
1210 ret
= kernctl_snapshot(metadata
->wait_fd
);
1212 if (ret
!= -EAGAIN
) {
1213 ERR("Sync metadata, taking kernel snapshot failed.");
1216 DBG("Sync metadata, no new kernel metadata");
1217 /* No new metadata, exit. */
1227 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1230 uint64_t seq
, discarded
;
1232 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1234 if (ret
== -ENOTTY
) {
1235 /* Command not implemented by lttng-modules. */
1239 PERROR("kernctl_get_sequence_number");
1245 * Start the sequence when we extract the first packet in case we don't
1246 * start at 0 (for example if a consumer is not connected to the
1247 * session immediately after the beginning).
1249 if (stream
->last_sequence_number
== -1ULL) {
1250 stream
->last_sequence_number
= seq
;
1251 } else if (seq
> stream
->last_sequence_number
) {
1252 stream
->chan
->lost_packets
+= seq
-
1253 stream
->last_sequence_number
- 1;
1255 /* seq <= last_sequence_number */
1256 ERR("Sequence number inconsistent : prev = %" PRIu64
1257 ", current = %" PRIu64
,
1258 stream
->last_sequence_number
, seq
);
1262 stream
->last_sequence_number
= seq
;
1264 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1266 PERROR("kernctl_get_events_discarded");
1269 if (discarded
< stream
->last_discarded_events
) {
1271 * Overflow has occurred. We assume only one wrap-around
1274 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1275 stream
->last_discarded_events
+ discarded
;
1277 stream
->chan
->discarded_events
+= discarded
-
1278 stream
->last_discarded_events
;
1280 stream
->last_discarded_events
= discarded
;
1288 * Check if the local version of the metadata stream matches with the version
1289 * of the metadata stream in the kernel. If it was updated, set the reset flag
1293 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1296 uint64_t cur_version
;
1298 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1300 if (ret
== -ENOTTY
) {
1302 * LTTng-modules does not implement this
1308 ERR("Failed to get the metadata version");
1312 if (stream
->metadata_version
== cur_version
) {
1317 DBG("New metadata version detected");
1318 stream
->metadata_version
= cur_version
;
1319 stream
->reset_metadata_flag
= 1;
1327 * Consume data on a file descriptor and write it on a trace file.
1329 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1330 struct lttng_consumer_local_data
*ctx
)
1332 unsigned long len
, subbuf_size
, padding
;
1333 int err
, write_index
= 1;
1335 int infd
= stream
->wait_fd
;
1336 struct ctf_packet_index index
;
1338 DBG("In read_subbuffer (infd : %d)", infd
);
1340 /* Get the next subbuffer */
1341 err
= kernctl_get_next_subbuf(infd
);
1344 * This is a debug message even for single-threaded consumer,
1345 * because poll() have more relaxed criterions than get subbuf,
1346 * so get_subbuf may fail for short race windows where poll()
1347 * would issue wakeups.
1349 DBG("Reserving sub buffer failed (everything is normal, "
1350 "it is due to concurrency)");
1355 /* Get the full subbuffer size including padding */
1356 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1358 PERROR("Getting sub-buffer len failed.");
1359 err
= kernctl_put_subbuf(infd
);
1361 if (err
== -EFAULT
) {
1362 PERROR("Error in unreserving sub buffer\n");
1363 } else if (err
== -EIO
) {
1364 /* Should never happen with newer LTTng versions */
1365 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1374 if (!stream
->metadata_flag
) {
1375 ret
= get_index_values(&index
, infd
);
1377 err
= kernctl_put_subbuf(infd
);
1379 if (err
== -EFAULT
) {
1380 PERROR("Error in unreserving sub buffer\n");
1381 } else if (err
== -EIO
) {
1382 /* Should never happen with newer LTTng versions */
1383 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1390 ret
= update_stream_stats(stream
);
1392 err
= kernctl_put_subbuf(infd
);
1394 if (err
== -EFAULT
) {
1395 PERROR("Error in unreserving sub buffer\n");
1396 } else if (err
== -EIO
) {
1397 /* Should never happen with newer LTTng versions */
1398 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1407 ret
= metadata_stream_check_version(infd
, stream
);
1409 err
= kernctl_put_subbuf(infd
);
1411 if (err
== -EFAULT
) {
1412 PERROR("Error in unreserving sub buffer\n");
1413 } else if (err
== -EIO
) {
1414 /* Should never happen with newer LTTng versions */
1415 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1424 switch (stream
->chan
->output
) {
1425 case CONSUMER_CHANNEL_SPLICE
:
1427 * XXX: The lttng-modules splice "actor" does not handle copying
1428 * partial pages hence only using the subbuffer size without the
1429 * padding makes the splice fail.
1434 /* splice the subbuffer to the tracefile */
1435 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1438 * XXX: Splice does not support network streaming so the return value
1439 * is simply checked against subbuf_size and not like the mmap() op.
1441 if (ret
!= subbuf_size
) {
1443 * display the error but continue processing to try
1444 * to release the subbuffer
1446 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1451 case CONSUMER_CHANNEL_MMAP
:
1452 /* Get subbuffer size without padding */
1453 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1455 PERROR("Getting sub-buffer len failed.");
1456 err
= kernctl_put_subbuf(infd
);
1458 if (err
== -EFAULT
) {
1459 PERROR("Error in unreserving sub buffer\n");
1460 } else if (err
== -EIO
) {
1461 /* Should never happen with newer LTTng versions */
1462 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1471 /* Make sure the tracer is not gone mad on us! */
1472 assert(len
>= subbuf_size
);
1474 padding
= len
- subbuf_size
;
1476 /* write the subbuffer to the tracefile */
1477 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1480 * The mmap operation should write subbuf_size amount of data when
1481 * network streaming or the full padding (len) size when we are _not_
1484 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1485 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1487 * Display the error but continue processing to try to release the
1488 * subbuffer. This is a DBG statement since this is possible to
1489 * happen without being a critical error.
1491 DBG("Error writing to tracefile "
1492 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1493 ret
, len
, subbuf_size
);
1498 ERR("Unknown output method");
1502 err
= kernctl_put_next_subbuf(infd
);
1504 if (err
== -EFAULT
) {
1505 PERROR("Error in unreserving sub buffer\n");
1506 } else if (err
== -EIO
) {
1507 /* Should never happen with newer LTTng versions */
1508 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1514 /* Write index if needed. */
1519 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1521 * In live, block until all the metadata is sent.
1523 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1524 assert(!stream
->missed_metadata_flush
);
1525 stream
->waiting_on_metadata
= true;
1526 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1528 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1530 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1531 stream
->waiting_on_metadata
= false;
1532 if (stream
->missed_metadata_flush
) {
1533 stream
->missed_metadata_flush
= false;
1534 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1535 (void) consumer_flush_kernel_index(stream
);
1537 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1544 err
= consumer_stream_write_index(stream
, &index
);
1553 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1560 * Don't create anything if this is set for streaming or should not be
1563 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1564 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1565 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1566 stream
->uid
, stream
->gid
, NULL
);
1570 stream
->out_fd
= ret
;
1571 stream
->tracefile_size_current
= 0;
1573 if (!stream
->metadata_flag
) {
1574 struct lttng_index_file
*index_file
;
1576 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1577 stream
->name
, stream
->uid
, stream
->gid
,
1578 stream
->chan
->tracefile_size
,
1579 stream
->tracefile_count_current
,
1580 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1584 assert(!stream
->index_file
);
1585 stream
->index_file
= index_file
;
1589 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1590 /* get the len of the mmap region */
1591 unsigned long mmap_len
;
1593 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1595 PERROR("kernctl_get_mmap_len");
1596 goto error_close_fd
;
1598 stream
->mmap_len
= (size_t) mmap_len
;
1600 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1601 MAP_PRIVATE
, stream
->wait_fd
, 0);
1602 if (stream
->mmap_base
== MAP_FAILED
) {
1603 PERROR("Error mmaping");
1605 goto error_close_fd
;
1609 /* we return 0 to let the library handle the FD internally */
1613 if (stream
->out_fd
>= 0) {
1616 err
= close(stream
->out_fd
);
1618 stream
->out_fd
= -1;
1625 * Check if data is still being extracted from the buffers for a specific
1626 * stream. Consumer data lock MUST be acquired before calling this function
1627 * and the stream lock.
1629 * Return 1 if the traced data are still getting read else 0 meaning that the
1630 * data is available for trace viewer reading.
1632 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1638 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1643 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1645 /* There is still data so let's put back this subbuffer. */
1646 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1648 ret
= 1; /* Data is pending */
1652 /* Data is NOT pending and ready to be read. */