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
;
53 * Take a snapshot for a specific fd
55 * Returns 0 on success, < 0 on error
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
60 int infd
= stream
->wait_fd
;
62 ret
= kernctl_snapshot(infd
);
64 * -EAGAIN is not an error, it just means that there is no data to
67 if (ret
!= 0 && ret
!= -EAGAIN
) {
68 PERROR("Getting sub-buffer snapshot.");
75 * Sample consumed and produced positions for a specific fd.
77 * Returns 0 on success, < 0 on error.
79 int lttng_kconsumer_sample_snapshot_positions(
80 struct lttng_consumer_stream
*stream
)
84 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
88 * Get the produced position
90 * Returns 0 on success, < 0 on error
92 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
96 int infd
= stream
->wait_fd
;
98 ret
= kernctl_snapshot_get_produced(infd
, pos
);
100 PERROR("kernctl_snapshot_get_produced");
107 * Get the consumerd position
109 * Returns 0 on success, < 0 on error
111 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
115 int infd
= stream
->wait_fd
;
117 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
119 PERROR("kernctl_snapshot_get_consumed");
126 * Take a snapshot of all the stream of a channel
128 * Returns 0 on success, < 0 on error
130 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
131 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
132 struct lttng_consumer_local_data
*ctx
)
135 struct lttng_consumer_channel
*channel
;
136 struct lttng_consumer_stream
*stream
;
138 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
142 channel
= consumer_find_channel(key
);
144 ERR("No channel found for key %" PRIu64
, key
);
149 /* Splice is not supported yet for channel snapshot. */
150 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
151 ERR("Unsupported output %d", channel
->output
);
156 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
157 unsigned long consumed_pos
, produced_pos
;
159 health_code_update();
162 * Lock stream because we are about to change its state.
164 pthread_mutex_lock(&stream
->lock
);
167 * Assign the received relayd ID so we can use it for streaming. The streams
168 * are not visible to anyone so this is OK to change it.
170 stream
->net_seq_idx
= relayd_id
;
171 channel
->relayd_id
= relayd_id
;
172 if (relayd_id
!= (uint64_t) -1ULL) {
173 ret
= consumer_send_relayd_stream(stream
, path
,
174 LTTNG_DOMAIN_KERNEL
);
176 ERR("sending stream to relayd");
180 ret
= utils_create_stream_file(path
, stream
->name
,
181 stream
->chan
->tracefile_size
,
182 stream
->tracefile_count_current
,
183 stream
->uid
, stream
->gid
, NULL
);
185 ERR("utils_create_stream_file");
189 stream
->out_fd
= ret
;
190 stream
->tracefile_size_current
= 0;
192 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
193 path
, stream
->name
, stream
->key
);
196 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
199 * Doing a buffer flush which does not take into
200 * account empty packets. This is not perfect
201 * for stream intersection, but required as a
202 * fall-back when "flush_empty" is not
203 * implemented by lttng-modules.
205 ret
= kernctl_buffer_flush(stream
->wait_fd
);
207 ERR("Failed to flush kernel stream");
213 ret
= lttng_kconsumer_take_snapshot(stream
);
215 ERR("Taking kernel snapshot");
219 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
221 ERR("Produced kernel snapshot position");
225 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
227 ERR("Consumerd kernel snapshot position");
231 if (stream
->max_sb_size
== 0) {
232 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
233 &stream
->max_sb_size
);
235 ERR("Getting kernel max_sb_size");
240 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
241 produced_pos
, nb_packets_per_stream
,
242 stream
->max_sb_size
);
244 while (consumed_pos
< produced_pos
) {
246 unsigned long len
, padded_len
;
248 health_code_update();
250 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
252 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
254 if (ret
!= -EAGAIN
) {
255 PERROR("kernctl_get_subbuf snapshot");
258 DBG("Kernel consumer get subbuf failed. Skipping it.");
259 consumed_pos
+= stream
->max_sb_size
;
260 stream
->chan
->lost_packets
++;
264 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
266 ERR("Snapshot kernctl_get_subbuf_size");
267 goto error_put_subbuf
;
270 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
272 ERR("Snapshot kernctl_get_padded_subbuf_size");
273 goto error_put_subbuf
;
276 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
277 padded_len
- len
, NULL
);
279 * We write the padded len in local tracefiles but the data len
280 * when using a relay. Display the error but continue processing
281 * to try to release the subbuffer.
283 if (relayd_id
!= (uint64_t) -1ULL) {
284 if (read_len
!= len
) {
285 ERR("Error sending to the relay (ret: %zd != len: %lu)",
289 if (read_len
!= padded_len
) {
290 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
291 read_len
, padded_len
);
295 ret
= kernctl_put_subbuf(stream
->wait_fd
);
297 ERR("Snapshot kernctl_put_subbuf");
300 consumed_pos
+= stream
->max_sb_size
;
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
,
372 LTTNG_DOMAIN_KERNEL
);
377 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
378 metadata_stream
->chan
->tracefile_size
,
379 metadata_stream
->tracefile_count_current
,
380 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
384 metadata_stream
->out_fd
= ret
;
388 health_code_update();
390 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
392 if (ret_read
!= -EAGAIN
) {
393 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
397 /* ret_read is negative at this point so we will exit the loop. */
400 } while (ret_read
>= 0);
403 close_relayd_stream(metadata_stream
);
404 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
406 if (metadata_stream
->out_fd
>= 0) {
407 ret
= close(metadata_stream
->out_fd
);
409 PERROR("Kernel consumer snapshot metadata close out_fd");
411 * Don't go on error here since the snapshot was successful at this
412 * point but somehow the close failed.
415 metadata_stream
->out_fd
= -1;
421 cds_list_del(&metadata_stream
->send_node
);
422 consumer_stream_destroy(metadata_stream
, NULL
);
423 metadata_channel
->metadata_stream
= NULL
;
430 * Receive command from session daemon and process it.
432 * Return 1 on success else a negative value or 0.
434 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
435 int sock
, struct pollfd
*consumer_sockpoll
)
438 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
439 struct lttcomm_consumer_msg msg
;
441 health_code_update();
443 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
444 if (ret
!= sizeof(msg
)) {
446 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
452 health_code_update();
454 /* Deprecated command */
455 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
457 health_code_update();
459 /* relayd needs RCU read-side protection */
462 switch (msg
.cmd_type
) {
463 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
465 /* Session daemon status message are handled in the following call. */
466 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
467 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
468 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
469 msg
.u
.relayd_sock
.relayd_session_id
);
472 case LTTNG_CONSUMER_ADD_CHANNEL
:
474 struct lttng_consumer_channel
*new_channel
;
477 health_code_update();
479 /* First send a status message before receiving the fds. */
480 ret
= consumer_send_status_msg(sock
, ret_code
);
482 /* Somehow, the session daemon is not responding anymore. */
486 health_code_update();
488 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
489 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
490 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
491 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
492 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
493 msg
.u
.channel
.tracefile_size
,
494 msg
.u
.channel
.tracefile_count
, 0,
495 msg
.u
.channel
.monitor
,
496 msg
.u
.channel
.live_timer_interval
,
498 if (new_channel
== NULL
) {
499 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
502 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
503 switch (msg
.u
.channel
.output
) {
504 case LTTNG_EVENT_SPLICE
:
505 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
507 case LTTNG_EVENT_MMAP
:
508 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
511 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
515 /* Translate and save channel type. */
516 switch (msg
.u
.channel
.type
) {
517 case CONSUMER_CHANNEL_TYPE_DATA
:
518 case CONSUMER_CHANNEL_TYPE_METADATA
:
519 new_channel
->type
= msg
.u
.channel
.type
;
526 health_code_update();
528 if (ctx
->on_recv_channel
!= NULL
) {
529 ret_recv
= ctx
->on_recv_channel(new_channel
);
531 ret
= consumer_add_channel(new_channel
, ctx
);
532 } else if (ret_recv
< 0) {
536 ret
= consumer_add_channel(new_channel
, ctx
);
538 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
539 int monitor_start_ret
;
541 DBG("Consumer starting monitor timer");
542 consumer_timer_live_start(new_channel
,
543 msg
.u
.channel
.live_timer_interval
);
544 monitor_start_ret
= consumer_timer_monitor_start(
546 msg
.u
.channel
.monitor_timer_interval
);
547 if (monitor_start_ret
< 0) {
548 ERR("Starting channel monitoring timer failed");
554 health_code_update();
556 /* If we received an error in add_channel, we need to report it. */
558 ret
= consumer_send_status_msg(sock
, ret
);
567 case LTTNG_CONSUMER_ADD_STREAM
:
570 struct lttng_pipe
*stream_pipe
;
571 struct lttng_consumer_stream
*new_stream
;
572 struct lttng_consumer_channel
*channel
;
576 * Get stream's channel reference. Needed when adding the stream to the
579 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
582 * We could not find the channel. Can happen if cpu hotplug
583 * happens while tearing down.
585 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
586 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
589 health_code_update();
591 /* First send a status message before receiving the fds. */
592 ret
= consumer_send_status_msg(sock
, ret_code
);
594 /* Somehow, the session daemon is not responding anymore. */
598 health_code_update();
600 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
601 /* Channel was not found. */
607 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
613 health_code_update();
615 /* Get stream file descriptor from socket */
616 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
617 if (ret
!= sizeof(fd
)) {
618 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
623 health_code_update();
626 * Send status code to session daemon only if the recv works. If the
627 * above recv() failed, the session daemon is notified through the
628 * error socket and the teardown is eventually done.
630 ret
= consumer_send_status_msg(sock
, ret_code
);
632 /* Somehow, the session daemon is not responding anymore. */
636 health_code_update();
638 new_stream
= consumer_allocate_stream(channel
->key
,
640 LTTNG_CONSUMER_ACTIVE_STREAM
,
650 if (new_stream
== NULL
) {
655 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
661 new_stream
->chan
= channel
;
662 new_stream
->wait_fd
= fd
;
663 consumer_stream_copy_ro_channel_values(new_stream
, channel
);
664 switch (channel
->output
) {
665 case CONSUMER_CHANNEL_SPLICE
:
666 new_stream
->output
= LTTNG_EVENT_SPLICE
;
667 ret
= utils_create_pipe(new_stream
->splice_pipe
);
672 case CONSUMER_CHANNEL_MMAP
:
673 new_stream
->output
= LTTNG_EVENT_MMAP
;
676 ERR("Stream output unknown %d", channel
->output
);
681 * We've just assigned the channel to the stream so increment the
682 * refcount right now. We don't need to increment the refcount for
683 * streams in no monitor because we handle manually the cleanup of
684 * those. It is very important to make sure there is NO prior
685 * consumer_del_stream() calls or else the refcount will be unbalanced.
687 if (channel
->monitor
) {
688 uatomic_inc(&new_stream
->chan
->refcount
);
692 * The buffer flush is done on the session daemon side for the kernel
693 * so no need for the stream "hangup_flush_done" variable to be
694 * tracked. This is important for a kernel stream since we don't rely
695 * on the flush state of the stream to read data. It's not the case for
696 * user space tracing.
698 new_stream
->hangup_flush_done
= 0;
700 health_code_update();
702 if (ctx
->on_recv_stream
) {
703 ret
= ctx
->on_recv_stream(new_stream
);
705 consumer_stream_free(new_stream
);
710 health_code_update();
712 if (new_stream
->metadata_flag
) {
713 channel
->metadata_stream
= new_stream
;
716 /* Do not monitor this stream. */
717 if (!channel
->monitor
) {
718 DBG("Kernel consumer add stream %s in no monitor mode with "
719 "relayd id %" PRIu64
, new_stream
->name
,
720 new_stream
->net_seq_idx
);
721 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
725 /* Send stream to relayd if the stream has an ID. */
726 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
727 ret
= consumer_send_relayd_stream(new_stream
,
728 new_stream
->chan
->pathname
,
729 LTTNG_DOMAIN_KERNEL
);
731 consumer_stream_free(new_stream
);
736 * If adding an extra stream to an already
737 * existing channel (e.g. cpu hotplug), we need
738 * to send the "streams_sent" command to relayd.
740 if (channel
->streams_sent_to_relayd
) {
741 ret
= consumer_send_relayd_streams_sent(
742 new_stream
->net_seq_idx
);
749 /* Get the right pipe where the stream will be sent. */
750 if (new_stream
->metadata_flag
) {
751 ret
= consumer_add_metadata_stream(new_stream
);
753 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
755 consumer_stream_free(new_stream
);
758 stream_pipe
= ctx
->consumer_metadata_pipe
;
760 ret
= consumer_add_data_stream(new_stream
);
762 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
764 consumer_stream_free(new_stream
);
767 stream_pipe
= ctx
->consumer_data_pipe
;
770 /* Vitible to other threads */
771 new_stream
->globally_visible
= 1;
773 health_code_update();
775 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
777 ERR("Consumer write %s stream to pipe %d",
778 new_stream
->metadata_flag
? "metadata" : "data",
779 lttng_pipe_get_writefd(stream_pipe
));
780 if (new_stream
->metadata_flag
) {
781 consumer_del_stream_for_metadata(new_stream
);
783 consumer_del_stream_for_data(new_stream
);
788 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
789 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
792 case LTTNG_CONSUMER_STREAMS_SENT
:
794 struct lttng_consumer_channel
*channel
;
797 * Get stream's channel reference. Needed when adding the stream to the
800 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
803 * We could not find the channel. Can happen if cpu hotplug
804 * happens while tearing down.
806 ERR("Unable to find channel key %" PRIu64
,
807 msg
.u
.sent_streams
.channel_key
);
808 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
811 health_code_update();
814 * Send status code to session daemon.
816 ret
= consumer_send_status_msg(sock
, ret_code
);
817 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
818 /* Somehow, the session daemon is not responding anymore. */
822 health_code_update();
825 * We should not send this message if we don't monitor the
826 * streams in this channel.
828 if (!channel
->monitor
) {
832 health_code_update();
833 /* Send stream to relayd if the stream has an ID. */
834 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
835 ret
= consumer_send_relayd_streams_sent(
836 msg
.u
.sent_streams
.net_seq_idx
);
840 channel
->streams_sent_to_relayd
= true;
844 case LTTNG_CONSUMER_UPDATE_STREAM
:
849 case LTTNG_CONSUMER_DESTROY_RELAYD
:
851 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
852 struct consumer_relayd_sock_pair
*relayd
;
854 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
856 /* Get relayd reference if exists. */
857 relayd
= consumer_find_relayd(index
);
858 if (relayd
== NULL
) {
859 DBG("Unable to find relayd %" PRIu64
, index
);
860 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
864 * Each relayd socket pair has a refcount of stream attached to it
865 * which tells if the relayd is still active or not depending on the
868 * This will set the destroy flag of the relayd object and destroy it
869 * if the refcount reaches zero when called.
871 * The destroy can happen either here or when a stream fd hangs up.
874 consumer_flag_relayd_for_destroy(relayd
);
877 health_code_update();
879 ret
= consumer_send_status_msg(sock
, ret_code
);
881 /* Somehow, the session daemon is not responding anymore. */
887 case LTTNG_CONSUMER_DATA_PENDING
:
890 uint64_t id
= msg
.u
.data_pending
.session_id
;
892 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
894 ret
= consumer_data_pending(id
);
896 health_code_update();
898 /* Send back returned value to session daemon */
899 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
901 PERROR("send data pending ret code");
906 * No need to send back a status message since the data pending
907 * returned value is the response.
911 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
913 if (msg
.u
.snapshot_channel
.metadata
== 1) {
914 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
915 msg
.u
.snapshot_channel
.pathname
,
916 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
918 ERR("Snapshot metadata failed");
919 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
922 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
923 msg
.u
.snapshot_channel
.pathname
,
924 msg
.u
.snapshot_channel
.relayd_id
,
925 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
928 ERR("Snapshot channel failed");
929 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
933 health_code_update();
935 ret
= consumer_send_status_msg(sock
, ret_code
);
937 /* Somehow, the session daemon is not responding anymore. */
942 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
944 uint64_t key
= msg
.u
.destroy_channel
.key
;
945 struct lttng_consumer_channel
*channel
;
947 channel
= consumer_find_channel(key
);
949 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
950 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
953 health_code_update();
955 ret
= consumer_send_status_msg(sock
, ret_code
);
957 /* Somehow, the session daemon is not responding anymore. */
961 health_code_update();
963 /* Stop right now if no channel was found. */
969 * This command should ONLY be issued for channel with streams set in
972 assert(!channel
->monitor
);
975 * The refcount should ALWAYS be 0 in the case of a channel in no
978 assert(!uatomic_sub_return(&channel
->refcount
, 1));
980 consumer_del_channel(channel
);
984 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
988 struct lttng_consumer_channel
*channel
;
989 uint64_t id
= msg
.u
.discarded_events
.session_id
;
990 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
992 DBG("Kernel consumer discarded events command for session id %"
993 PRIu64
", channel key %" PRIu64
, id
, key
);
995 channel
= consumer_find_channel(key
);
997 ERR("Kernel consumer discarded events channel %"
998 PRIu64
" not found", key
);
1001 count
= channel
->discarded_events
;
1004 health_code_update();
1006 /* Send back returned value to session daemon */
1007 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1009 PERROR("send discarded events");
1015 case LTTNG_CONSUMER_LOST_PACKETS
:
1019 struct lttng_consumer_channel
*channel
;
1020 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1021 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1023 DBG("Kernel consumer lost packets command for session id %"
1024 PRIu64
", channel key %" PRIu64
, id
, key
);
1026 channel
= consumer_find_channel(key
);
1028 ERR("Kernel consumer lost packets channel %"
1029 PRIu64
" not found", key
);
1032 count
= channel
->lost_packets
;
1035 health_code_update();
1037 /* Send back returned value to session daemon */
1038 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1040 PERROR("send lost packets");
1046 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1048 int channel_monitor_pipe
;
1050 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1051 /* Successfully received the command's type. */
1052 ret
= consumer_send_status_msg(sock
, ret_code
);
1057 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1059 if (ret
!= sizeof(channel_monitor_pipe
)) {
1060 ERR("Failed to receive channel monitor pipe");
1064 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1065 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1066 channel_monitor_pipe
);
1070 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1071 /* Set the pipe as non-blocking. */
1072 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1074 PERROR("fcntl get flags of the channel monitoring pipe");
1079 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1080 flags
| O_NONBLOCK
);
1082 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1085 DBG("Channel monitor pipe set as non-blocking");
1087 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1089 ret
= consumer_send_status_msg(sock
, ret_code
);
1095 case LTTNG_CONSUMER_SET_CHANNEL_ROTATE_PIPE
:
1097 int channel_rotate_pipe
;
1100 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1101 /* Successfully received the command's type. */
1102 ret
= consumer_send_status_msg(sock
, ret_code
);
1107 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_rotate_pipe
,
1109 if (ret
!= sizeof(channel_rotate_pipe
)) {
1110 ERR("Failed to receive channel rotate pipe");
1114 DBG("Received channel rotate pipe (%d)", channel_rotate_pipe
);
1115 ctx
->channel_rotate_pipe
= channel_rotate_pipe
;
1116 /* Set the pipe as non-blocking. */
1117 ret
= fcntl(channel_rotate_pipe
, F_GETFL
, 0);
1119 PERROR("fcntl get flags of the channel rotate pipe");
1124 ret
= fcntl(channel_rotate_pipe
, F_SETFL
,
1125 flags
| O_NONBLOCK
);
1127 PERROR("fcntl set O_NONBLOCK flag of the channel rotate pipe");
1130 DBG("Channel rotate pipe set as non-blocking");
1131 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1132 ret
= consumer_send_status_msg(sock
, ret_code
);
1138 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1140 ret
= lttng_consumer_rotate_channel(msg
.u
.rotate_channel
.key
,
1141 msg
.u
.rotate_channel
.pathname
,
1142 msg
.u
.rotate_channel
.relayd_id
,
1143 msg
.u
.rotate_channel
.metadata
,
1144 msg
.u
.rotate_channel
.new_chunk_id
,
1147 ERR("Rotate channel failed");
1148 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1151 health_code_update();
1153 ret
= consumer_send_status_msg(sock
, ret_code
);
1155 /* Somehow, the session daemon is not responding anymore. */
1160 * Rotate the streams that are ready right now.
1161 * FIXME: this is a second consecutive iteration over the
1162 * streams in a channel, there is probably a better way to
1163 * handle this, but it needs to be after the
1164 * consumer_send_status_msg() call.
1166 ret
= lttng_consumer_rotate_ready_streams(
1167 msg
.u
.rotate_channel
.key
, ctx
);
1169 ERR("Rotate channel failed");
1170 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1174 case LTTNG_CONSUMER_ROTATE_RENAME
:
1176 DBG("Consumer rename session %" PRIu64
" after rotation",
1177 msg
.u
.rotate_rename
.session_id
);
1178 ret
= lttng_consumer_rotate_rename(msg
.u
.rotate_rename
.current_path
,
1179 msg
.u
.rotate_rename
.new_path
,
1180 msg
.u
.rotate_rename
.uid
,
1181 msg
.u
.rotate_rename
.gid
,
1182 msg
.u
.rotate_rename
.relayd_id
);
1184 ERR("Rotate rename failed");
1185 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1188 health_code_update();
1190 ret
= consumer_send_status_msg(sock
, ret_code
);
1192 /* Somehow, the session daemon is not responding anymore. */
1197 case LTTNG_CONSUMER_ROTATE_PENDING_RELAY
:
1201 DBG("Consumer rotate pending on relay for session %" PRIu64
,
1202 msg
.u
.rotate_pending_relay
.session_id
);
1203 pending
= lttng_consumer_rotate_pending_relay(
1204 msg
.u
.rotate_pending_relay
.session_id
,
1205 msg
.u
.rotate_pending_relay
.relayd_id
,
1206 msg
.u
.rotate_pending_relay
.chunk_id
);
1208 ERR("Rotate pending relay failed");
1209 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1212 health_code_update();
1214 ret
= consumer_send_status_msg(sock
, ret_code
);
1216 /* Somehow, the session daemon is not responding anymore. */
1220 /* Send back returned value to session daemon */
1221 ret
= lttcomm_send_unix_sock(sock
, &pending
, sizeof(pending
));
1223 PERROR("send data pending ret code");
1236 * Return 1 to indicate success since the 0 value can be a socket
1237 * shutdown during the recv() or send() call.
1239 health_code_update();
1244 /* This will issue a consumer stop. */
1249 * Populate index values of a kernel stream. Values are set in big endian order.
1251 * Return 0 on success or else a negative value.
1253 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1257 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1259 PERROR("kernctl_get_timestamp_begin");
1262 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1264 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1266 PERROR("kernctl_get_timestamp_end");
1269 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1271 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1273 PERROR("kernctl_get_events_discarded");
1276 index
->events_discarded
= htobe64(index
->events_discarded
);
1278 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1280 PERROR("kernctl_get_content_size");
1283 index
->content_size
= htobe64(index
->content_size
);
1285 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1287 PERROR("kernctl_get_packet_size");
1290 index
->packet_size
= htobe64(index
->packet_size
);
1292 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1294 PERROR("kernctl_get_stream_id");
1297 index
->stream_id
= htobe64(index
->stream_id
);
1299 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1301 if (ret
== -ENOTTY
) {
1302 /* Command not implemented by lttng-modules. */
1303 index
->stream_instance_id
= -1ULL;
1305 PERROR("kernctl_get_instance_id");
1309 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1311 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1313 if (ret
== -ENOTTY
) {
1314 /* Command not implemented by lttng-modules. */
1315 index
->packet_seq_num
= -1ULL;
1318 PERROR("kernctl_get_sequence_number");
1322 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1328 * Sync metadata meaning request them to the session daemon and snapshot to the
1329 * metadata thread can consumer them.
1331 * Metadata stream lock MUST be acquired.
1333 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1334 * is empty or a negative value on error.
1336 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1342 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1344 ERR("Failed to flush kernel stream");
1348 ret
= kernctl_snapshot(metadata
->wait_fd
);
1350 if (ret
!= -EAGAIN
) {
1351 ERR("Sync metadata, taking kernel snapshot failed.");
1354 DBG("Sync metadata, no new kernel metadata");
1355 /* No new metadata, exit. */
1365 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1368 uint64_t seq
, discarded
;
1370 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1372 if (ret
== -ENOTTY
) {
1373 /* Command not implemented by lttng-modules. */
1376 PERROR("kernctl_get_sequence_number");
1382 * Start the sequence when we extract the first packet in case we don't
1383 * start at 0 (for example if a consumer is not connected to the
1384 * session immediately after the beginning).
1386 if (stream
->last_sequence_number
== -1ULL) {
1387 stream
->last_sequence_number
= seq
;
1388 } else if (seq
> stream
->last_sequence_number
) {
1389 stream
->chan
->lost_packets
+= seq
-
1390 stream
->last_sequence_number
- 1;
1392 /* seq <= last_sequence_number */
1393 ERR("Sequence number inconsistent : prev = %" PRIu64
1394 ", current = %" PRIu64
,
1395 stream
->last_sequence_number
, seq
);
1399 stream
->last_sequence_number
= seq
;
1401 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1403 PERROR("kernctl_get_events_discarded");
1406 if (discarded
< stream
->last_discarded_events
) {
1408 * Overflow has occurred. We assume only one wrap-around
1411 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1412 stream
->last_discarded_events
+ discarded
;
1414 stream
->chan
->discarded_events
+= discarded
-
1415 stream
->last_discarded_events
;
1417 stream
->last_discarded_events
= discarded
;
1425 * Check if the local version of the metadata stream matches with the version
1426 * of the metadata stream in the kernel. If it was updated, set the reset flag
1430 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1433 uint64_t cur_version
;
1435 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1437 if (ret
== -ENOTTY
) {
1439 * LTTng-modules does not implement this
1445 ERR("Failed to get the metadata version");
1449 if (stream
->metadata_version
== cur_version
) {
1454 DBG("New metadata version detected");
1455 stream
->metadata_version
= cur_version
;
1456 stream
->reset_metadata_flag
= 1;
1464 * Consume data on a file descriptor and write it on a trace file.
1466 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1467 struct lttng_consumer_local_data
*ctx
)
1469 unsigned long len
, subbuf_size
, padding
;
1470 int err
, write_index
= 1, rotation_ret
, rotate_ready
;
1472 int infd
= stream
->wait_fd
;
1473 struct ctf_packet_index index
;
1475 DBG("In read_subbuffer (infd : %d)", infd
);
1477 /* Get the next subbuffer */
1478 err
= kernctl_get_next_subbuf(infd
);
1481 * This is a debug message even for single-threaded consumer,
1482 * because poll() have more relaxed criterions than get subbuf,
1483 * so get_subbuf may fail for short race windows where poll()
1484 * would issue wakeups.
1486 DBG("Reserving sub buffer failed (everything is normal, "
1487 "it is due to concurrency)");
1492 /* Get the full subbuffer size including padding */
1493 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1495 PERROR("Getting sub-buffer len failed.");
1496 err
= kernctl_put_subbuf(infd
);
1498 if (err
== -EFAULT
) {
1499 PERROR("Error in unreserving sub buffer\n");
1500 } else if (err
== -EIO
) {
1501 /* Should never happen with newer LTTng versions */
1502 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1511 rotate_ready
= lttng_consumer_stream_is_rotate_ready(stream
, len
);
1512 if (rotate_ready
< 0) {
1513 ERR("Failed to check if stream is ready for rotation");
1514 err
= kernctl_put_subbuf(infd
);
1516 if (err
== -EFAULT
) {
1517 PERROR("Error in unreserving sub buffer\n");
1518 } else if (err
== -EIO
) {
1519 /* Should never happen with newer LTTng versions */
1520 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1528 stream
->rotate_ready
= rotate_ready
;
1530 if (!stream
->metadata_flag
) {
1531 ret
= get_index_values(&index
, infd
);
1533 err
= kernctl_put_subbuf(infd
);
1535 if (err
== -EFAULT
) {
1536 PERROR("Error in unreserving sub buffer\n");
1537 } else if (err
== -EIO
) {
1538 /* Should never happen with newer LTTng versions */
1539 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1546 ret
= update_stream_stats(stream
);
1548 err
= kernctl_put_subbuf(infd
);
1550 if (err
== -EFAULT
) {
1551 PERROR("Error in unreserving sub buffer\n");
1552 } else if (err
== -EIO
) {
1553 /* Should never happen with newer LTTng versions */
1554 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1563 ret
= metadata_stream_check_version(infd
, stream
);
1565 err
= kernctl_put_subbuf(infd
);
1567 if (err
== -EFAULT
) {
1568 PERROR("Error in unreserving sub buffer\n");
1569 } else if (err
== -EIO
) {
1570 /* Should never happen with newer LTTng versions */
1571 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1580 switch (stream
->chan
->output
) {
1581 case CONSUMER_CHANNEL_SPLICE
:
1583 * XXX: The lttng-modules splice "actor" does not handle copying
1584 * partial pages hence only using the subbuffer size without the
1585 * padding makes the splice fail.
1590 /* splice the subbuffer to the tracefile */
1591 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1594 * XXX: Splice does not support network streaming so the return value
1595 * is simply checked against subbuf_size and not like the mmap() op.
1597 if (ret
!= subbuf_size
) {
1599 * display the error but continue processing to try
1600 * to release the subbuffer
1602 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1607 case CONSUMER_CHANNEL_MMAP
:
1608 /* Get subbuffer size without padding */
1609 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1611 PERROR("Getting sub-buffer len failed.");
1612 err
= kernctl_put_subbuf(infd
);
1614 if (err
== -EFAULT
) {
1615 PERROR("Error in unreserving sub buffer\n");
1616 } else if (err
== -EIO
) {
1617 /* Should never happen with newer LTTng versions */
1618 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1627 /* Make sure the tracer is not gone mad on us! */
1628 assert(len
>= subbuf_size
);
1630 padding
= len
- subbuf_size
;
1632 /* write the subbuffer to the tracefile */
1633 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1636 * The mmap operation should write subbuf_size amount of data when
1637 * network streaming or the full padding (len) size when we are _not_
1640 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1641 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1643 * Display the error but continue processing to try to release the
1644 * subbuffer. This is a DBG statement since this is possible to
1645 * happen without being a critical error.
1647 DBG("Error writing to tracefile "
1648 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1649 ret
, len
, subbuf_size
);
1654 ERR("Unknown output method");
1658 err
= kernctl_put_next_subbuf(infd
);
1660 if (err
== -EFAULT
) {
1661 PERROR("Error in unreserving sub buffer\n");
1662 } else if (err
== -EIO
) {
1663 /* Should never happen with newer LTTng versions */
1664 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1670 /* Write index if needed. */
1675 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1677 * In live, block until all the metadata is sent.
1679 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1680 assert(!stream
->missed_metadata_flush
);
1681 stream
->waiting_on_metadata
= true;
1682 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1684 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1686 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1687 stream
->waiting_on_metadata
= false;
1688 if (stream
->missed_metadata_flush
) {
1689 stream
->missed_metadata_flush
= false;
1690 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1691 (void) consumer_flush_kernel_index(stream
);
1693 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1700 err
= consumer_stream_write_index(stream
, &index
);
1706 if (stream
->rotate_ready
) {
1707 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
1708 if (rotation_ret
< 0) {
1709 ERR("Stream rotation error");
1719 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1726 * Don't create anything if this is set for streaming or should not be
1729 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1730 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1731 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1732 stream
->uid
, stream
->gid
, NULL
);
1736 stream
->out_fd
= ret
;
1737 stream
->tracefile_size_current
= 0;
1739 if (!stream
->metadata_flag
) {
1740 struct lttng_index_file
*index_file
;
1742 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1743 stream
->name
, stream
->uid
, stream
->gid
,
1744 stream
->chan
->tracefile_size
,
1745 stream
->tracefile_count_current
,
1746 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1750 assert(!stream
->index_file
);
1751 stream
->index_file
= index_file
;
1755 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1756 /* get the len of the mmap region */
1757 unsigned long mmap_len
;
1759 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1761 PERROR("kernctl_get_mmap_len");
1762 goto error_close_fd
;
1764 stream
->mmap_len
= (size_t) mmap_len
;
1766 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1767 MAP_PRIVATE
, stream
->wait_fd
, 0);
1768 if (stream
->mmap_base
== MAP_FAILED
) {
1769 PERROR("Error mmaping");
1771 goto error_close_fd
;
1775 /* we return 0 to let the library handle the FD internally */
1779 if (stream
->out_fd
>= 0) {
1782 err
= close(stream
->out_fd
);
1784 stream
->out_fd
= -1;
1791 * Check if data is still being extracted from the buffers for a specific
1792 * stream. Consumer data lock MUST be acquired before calling this function
1793 * and the stream lock.
1795 * Return 1 if the traced data are still getting read else 0 meaning that the
1796 * data is available for trace viewer reading.
1798 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1804 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1809 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1811 /* There is still data so let's put back this subbuffer. */
1812 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1814 ret
= 1; /* Data is pending */
1818 /* Data is NOT pending and ready to be read. */