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 PERROR("Getting sub-buffer snapshot.");
71 * Sample consumed and produced positions for a specific fd.
73 * Returns 0 on success, < 0 on error.
75 int lttng_kconsumer_sample_snapshot_positions(
76 struct lttng_consumer_stream
*stream
)
80 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
84 * Get the produced position
86 * Returns 0 on success, < 0 on error
88 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
92 int infd
= stream
->wait_fd
;
94 ret
= kernctl_snapshot_get_produced(infd
, pos
);
96 PERROR("kernctl_snapshot_get_produced");
103 * Get the consumerd position
105 * Returns 0 on success, < 0 on error
107 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
111 int infd
= stream
->wait_fd
;
113 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
115 PERROR("kernctl_snapshot_get_consumed");
122 * Take a snapshot of all the stream of a channel
124 * Returns 0 on success, < 0 on error
126 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
127 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
128 struct lttng_consumer_local_data
*ctx
)
131 struct lttng_consumer_channel
*channel
;
132 struct lttng_consumer_stream
*stream
;
134 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
138 channel
= consumer_find_channel(key
);
140 ERR("No channel found for key %" PRIu64
, key
);
145 /* Splice is not supported yet for channel snapshot. */
146 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
147 ERR("Unsupported output %d", channel
->output
);
152 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
153 unsigned long consumed_pos
, produced_pos
;
155 health_code_update();
158 * Lock stream because we are about to change its state.
160 pthread_mutex_lock(&stream
->lock
);
163 * Assign the received relayd ID so we can use it for streaming. The streams
164 * are not visible to anyone so this is OK to change it.
166 stream
->net_seq_idx
= relayd_id
;
167 channel
->relayd_id
= relayd_id
;
168 if (relayd_id
!= (uint64_t) -1ULL) {
169 ret
= consumer_send_relayd_stream(stream
, path
);
171 ERR("sending stream to relayd");
175 ret
= utils_create_stream_file(path
, stream
->name
,
176 stream
->chan
->tracefile_size
,
177 stream
->tracefile_count_current
,
178 stream
->uid
, stream
->gid
, NULL
);
180 ERR("utils_create_stream_file");
184 stream
->out_fd
= ret
;
185 stream
->tracefile_size_current
= 0;
187 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
188 path
, stream
->name
, stream
->key
);
190 if (relayd_id
!= -1ULL) {
191 ret
= consumer_send_relayd_streams_sent(relayd_id
);
193 ERR("sending streams sent to relayd");
196 channel
->streams_sent_to_relayd
= true;
199 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
202 * Doing a buffer flush which does not take into
203 * account empty packets. This is not perfect
204 * for stream intersection, but required as a
205 * fall-back when "flush_empty" is not
206 * implemented by lttng-modules.
208 ret
= kernctl_buffer_flush(stream
->wait_fd
);
210 ERR("Failed to flush kernel stream");
216 ret
= lttng_kconsumer_take_snapshot(stream
);
218 ERR("Taking kernel snapshot");
222 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
224 ERR("Produced kernel snapshot position");
228 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
230 ERR("Consumerd kernel snapshot position");
234 if (stream
->max_sb_size
== 0) {
235 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
236 &stream
->max_sb_size
);
238 ERR("Getting kernel max_sb_size");
243 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
244 produced_pos
, nb_packets_per_stream
,
245 stream
->max_sb_size
);
247 while (consumed_pos
< produced_pos
) {
249 unsigned long len
, padded_len
;
251 health_code_update();
253 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
255 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
257 if (ret
!= -EAGAIN
) {
258 PERROR("kernctl_get_subbuf snapshot");
261 DBG("Kernel consumer get subbuf failed. Skipping it.");
262 consumed_pos
+= stream
->max_sb_size
;
263 stream
->chan
->lost_packets
++;
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 if (relayd_id
== (uint64_t) -1ULL) {
307 if (stream
->out_fd
>= 0) {
308 ret
= close(stream
->out_fd
);
310 PERROR("Kernel consumer snapshot close out_fd");
316 close_relayd_stream(stream
);
317 stream
->net_seq_idx
= (uint64_t) -1ULL;
319 pthread_mutex_unlock(&stream
->lock
);
327 ret
= kernctl_put_subbuf(stream
->wait_fd
);
329 ERR("Snapshot kernctl_put_subbuf error path");
332 pthread_mutex_unlock(&stream
->lock
);
339 * Read the whole metadata available for a snapshot.
341 * Returns 0 on success, < 0 on error
343 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
344 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
346 int ret
, use_relayd
= 0;
348 struct lttng_consumer_channel
*metadata_channel
;
349 struct lttng_consumer_stream
*metadata_stream
;
353 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
358 metadata_channel
= consumer_find_channel(key
);
359 if (!metadata_channel
) {
360 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
365 metadata_stream
= metadata_channel
->metadata_stream
;
366 assert(metadata_stream
);
368 /* Flag once that we have a valid relayd for the stream. */
369 if (relayd_id
!= (uint64_t) -1ULL) {
374 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
379 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
380 metadata_stream
->chan
->tracefile_size
,
381 metadata_stream
->tracefile_count_current
,
382 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
386 metadata_stream
->out_fd
= ret
;
390 health_code_update();
392 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
394 if (ret_read
!= -EAGAIN
) {
395 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
399 /* ret_read is negative at this point so we will exit the loop. */
402 } while (ret_read
>= 0);
405 close_relayd_stream(metadata_stream
);
406 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
408 if (metadata_stream
->out_fd
>= 0) {
409 ret
= close(metadata_stream
->out_fd
);
411 PERROR("Kernel consumer snapshot metadata close out_fd");
413 * Don't go on error here since the snapshot was successful at this
414 * point but somehow the close failed.
417 metadata_stream
->out_fd
= -1;
423 cds_list_del(&metadata_stream
->send_node
);
424 consumer_stream_destroy(metadata_stream
, NULL
);
425 metadata_channel
->metadata_stream
= NULL
;
432 * Receive command from session daemon and process it.
434 * Return 1 on success else a negative value or 0.
436 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
437 int sock
, struct pollfd
*consumer_sockpoll
)
440 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
441 struct lttcomm_consumer_msg msg
;
443 health_code_update();
445 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
446 if (ret
!= sizeof(msg
)) {
448 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
454 health_code_update();
456 /* Deprecated command */
457 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
459 health_code_update();
461 /* relayd needs RCU read-side protection */
464 switch (msg
.cmd_type
) {
465 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
467 /* Session daemon status message are handled in the following call. */
468 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
469 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
470 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
471 msg
.u
.relayd_sock
.relayd_session_id
);
474 case LTTNG_CONSUMER_ADD_CHANNEL
:
476 struct lttng_consumer_channel
*new_channel
;
479 health_code_update();
481 /* First send a status message before receiving the fds. */
482 ret
= consumer_send_status_msg(sock
, ret_code
);
484 /* Somehow, the session daemon is not responding anymore. */
488 health_code_update();
490 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
491 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
492 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
493 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
494 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
495 msg
.u
.channel
.tracefile_size
,
496 msg
.u
.channel
.tracefile_count
, 0,
497 msg
.u
.channel
.monitor
,
498 msg
.u
.channel
.live_timer_interval
,
500 if (new_channel
== NULL
) {
501 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
504 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
505 switch (msg
.u
.channel
.output
) {
506 case LTTNG_EVENT_SPLICE
:
507 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
509 case LTTNG_EVENT_MMAP
:
510 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
513 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
517 /* Translate and save channel type. */
518 switch (msg
.u
.channel
.type
) {
519 case CONSUMER_CHANNEL_TYPE_DATA
:
520 case CONSUMER_CHANNEL_TYPE_METADATA
:
521 new_channel
->type
= msg
.u
.channel
.type
;
528 health_code_update();
530 if (ctx
->on_recv_channel
!= NULL
) {
531 ret_recv
= ctx
->on_recv_channel(new_channel
);
533 ret
= consumer_add_channel(new_channel
, ctx
);
534 } else if (ret_recv
< 0) {
538 ret
= consumer_add_channel(new_channel
, ctx
);
540 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
541 int monitor_start_ret
;
543 DBG("Consumer starting monitor timer");
544 consumer_timer_live_start(new_channel
,
545 msg
.u
.channel
.live_timer_interval
);
546 monitor_start_ret
= consumer_timer_monitor_start(
548 msg
.u
.channel
.monitor_timer_interval
);
549 if (monitor_start_ret
< 0) {
550 ERR("Starting channel monitoring timer failed");
556 health_code_update();
558 /* If we received an error in add_channel, we need to report it. */
560 ret
= consumer_send_status_msg(sock
, ret
);
569 case LTTNG_CONSUMER_ADD_STREAM
:
572 struct lttng_pipe
*stream_pipe
;
573 struct lttng_consumer_stream
*new_stream
;
574 struct lttng_consumer_channel
*channel
;
578 * Get stream's channel reference. Needed when adding the stream to the
581 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
584 * We could not find the channel. Can happen if cpu hotplug
585 * happens while tearing down.
587 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
588 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
591 health_code_update();
593 /* First send a status message before receiving the fds. */
594 ret
= consumer_send_status_msg(sock
, ret_code
);
596 /* Somehow, the session daemon is not responding anymore. */
600 health_code_update();
602 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
603 /* Channel was not found. */
609 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
615 health_code_update();
617 /* Get stream file descriptor from socket */
618 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
619 if (ret
!= sizeof(fd
)) {
620 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
625 health_code_update();
628 * Send status code to session daemon only if the recv works. If the
629 * above recv() failed, the session daemon is notified through the
630 * error socket and the teardown is eventually done.
632 ret
= consumer_send_status_msg(sock
, ret_code
);
634 /* Somehow, the session daemon is not responding anymore. */
638 health_code_update();
640 new_stream
= consumer_allocate_stream(channel
->key
,
642 LTTNG_CONSUMER_ACTIVE_STREAM
,
652 if (new_stream
== NULL
) {
657 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
663 new_stream
->chan
= channel
;
664 new_stream
->wait_fd
= fd
;
665 switch (channel
->output
) {
666 case CONSUMER_CHANNEL_SPLICE
:
667 new_stream
->output
= LTTNG_EVENT_SPLICE
;
668 ret
= utils_create_pipe(new_stream
->splice_pipe
);
673 case CONSUMER_CHANNEL_MMAP
:
674 new_stream
->output
= LTTNG_EVENT_MMAP
;
677 ERR("Stream output unknown %d", channel
->output
);
682 * We've just assigned the channel to the stream so increment the
683 * refcount right now. We don't need to increment the refcount for
684 * streams in no monitor because we handle manually the cleanup of
685 * those. It is very important to make sure there is NO prior
686 * consumer_del_stream() calls or else the refcount will be unbalanced.
688 if (channel
->monitor
) {
689 uatomic_inc(&new_stream
->chan
->refcount
);
693 * The buffer flush is done on the session daemon side for the kernel
694 * so no need for the stream "hangup_flush_done" variable to be
695 * tracked. This is important for a kernel stream since we don't rely
696 * on the flush state of the stream to read data. It's not the case for
697 * user space tracing.
699 new_stream
->hangup_flush_done
= 0;
701 health_code_update();
703 if (ctx
->on_recv_stream
) {
704 ret
= ctx
->on_recv_stream(new_stream
);
706 consumer_stream_free(new_stream
);
711 health_code_update();
713 if (new_stream
->metadata_flag
) {
714 channel
->metadata_stream
= new_stream
;
717 /* Do not monitor this stream. */
718 if (!channel
->monitor
) {
719 DBG("Kernel consumer add stream %s in no monitor mode with "
720 "relayd id %" PRIu64
, new_stream
->name
,
721 new_stream
->net_seq_idx
);
722 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
726 /* Send stream to relayd if the stream has an ID. */
727 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
728 ret
= consumer_send_relayd_stream(new_stream
,
729 new_stream
->chan
->pathname
);
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
:
987 struct lttng_consumer_channel
*channel
;
988 uint64_t id
= msg
.u
.discarded_events
.session_id
;
989 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
991 DBG("Kernel consumer discarded events command for session id %"
992 PRIu64
", channel key %" PRIu64
, id
, key
);
994 channel
= consumer_find_channel(key
);
996 ERR("Kernel consumer discarded events channel %"
997 PRIu64
" not found", key
);
1000 ret
= channel
->discarded_events
;
1003 health_code_update();
1005 /* Send back returned value to session daemon */
1006 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1008 PERROR("send discarded events");
1014 case LTTNG_CONSUMER_LOST_PACKETS
:
1017 struct lttng_consumer_channel
*channel
;
1018 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1019 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1021 DBG("Kernel consumer lost packets command for session id %"
1022 PRIu64
", channel key %" PRIu64
, id
, key
);
1024 channel
= consumer_find_channel(key
);
1026 ERR("Kernel consumer lost packets channel %"
1027 PRIu64
" not found", key
);
1030 ret
= channel
->lost_packets
;
1033 health_code_update();
1035 /* Send back returned value to session daemon */
1036 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1038 PERROR("send lost packets");
1044 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1046 int channel_monitor_pipe
;
1048 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1049 /* Successfully received the command's type. */
1050 ret
= consumer_send_status_msg(sock
, ret_code
);
1055 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1057 if (ret
!= sizeof(channel_monitor_pipe
)) {
1058 ERR("Failed to receive channel monitor pipe");
1062 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1063 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1064 channel_monitor_pipe
);
1068 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1069 /* Set the pipe as non-blocking. */
1070 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1072 PERROR("fcntl get flags of the channel monitoring pipe");
1077 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1078 flags
| O_NONBLOCK
);
1080 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1083 DBG("Channel monitor pipe set as non-blocking");
1085 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1087 ret
= consumer_send_status_msg(sock
, ret_code
);
1101 * Return 1 to indicate success since the 0 value can be a socket
1102 * shutdown during the recv() or send() call.
1104 health_code_update();
1109 /* This will issue a consumer stop. */
1114 * Populate index values of a kernel stream. Values are set in big endian order.
1116 * Return 0 on success or else a negative value.
1118 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1122 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1124 PERROR("kernctl_get_timestamp_begin");
1127 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1129 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1131 PERROR("kernctl_get_timestamp_end");
1134 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1136 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1138 PERROR("kernctl_get_events_discarded");
1141 index
->events_discarded
= htobe64(index
->events_discarded
);
1143 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1145 PERROR("kernctl_get_content_size");
1148 index
->content_size
= htobe64(index
->content_size
);
1150 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1152 PERROR("kernctl_get_packet_size");
1155 index
->packet_size
= htobe64(index
->packet_size
);
1157 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1159 PERROR("kernctl_get_stream_id");
1162 index
->stream_id
= htobe64(index
->stream_id
);
1164 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1166 if (ret
== -ENOTTY
) {
1167 /* Command not implemented by lttng-modules. */
1168 index
->stream_instance_id
= -1ULL;
1170 PERROR("kernctl_get_instance_id");
1174 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1176 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1178 if (ret
== -ENOTTY
) {
1179 /* Command not implemented by lttng-modules. */
1180 index
->packet_seq_num
= -1ULL;
1183 PERROR("kernctl_get_sequence_number");
1187 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1193 * Sync metadata meaning request them to the session daemon and snapshot to the
1194 * metadata thread can consumer them.
1196 * Metadata stream lock MUST be acquired.
1198 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1199 * is empty or a negative value on error.
1201 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1207 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1209 ERR("Failed to flush kernel stream");
1213 ret
= kernctl_snapshot(metadata
->wait_fd
);
1215 if (ret
!= -EAGAIN
) {
1216 ERR("Sync metadata, taking kernel snapshot failed.");
1219 DBG("Sync metadata, no new kernel metadata");
1220 /* No new metadata, exit. */
1230 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1233 uint64_t seq
, discarded
;
1235 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1237 if (ret
== -ENOTTY
) {
1238 /* Command not implemented by lttng-modules. */
1241 PERROR("kernctl_get_sequence_number");
1247 * Start the sequence when we extract the first packet in case we don't
1248 * start at 0 (for example if a consumer is not connected to the
1249 * session immediately after the beginning).
1251 if (stream
->last_sequence_number
== -1ULL) {
1252 stream
->last_sequence_number
= seq
;
1253 } else if (seq
> stream
->last_sequence_number
) {
1254 stream
->chan
->lost_packets
+= seq
-
1255 stream
->last_sequence_number
- 1;
1257 /* seq <= last_sequence_number */
1258 ERR("Sequence number inconsistent : prev = %" PRIu64
1259 ", current = %" PRIu64
,
1260 stream
->last_sequence_number
, seq
);
1264 stream
->last_sequence_number
= seq
;
1266 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1268 PERROR("kernctl_get_events_discarded");
1271 if (discarded
< stream
->last_discarded_events
) {
1273 * Overflow has occurred. We assume only one wrap-around
1276 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1277 stream
->last_discarded_events
+ discarded
;
1279 stream
->chan
->discarded_events
+= discarded
-
1280 stream
->last_discarded_events
;
1282 stream
->last_discarded_events
= discarded
;
1290 * Check if the local version of the metadata stream matches with the version
1291 * of the metadata stream in the kernel. If it was updated, set the reset flag
1295 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1298 uint64_t cur_version
;
1300 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1302 if (ret
== -ENOTTY
) {
1304 * LTTng-modules does not implement this
1310 ERR("Failed to get the metadata version");
1314 if (stream
->metadata_version
== cur_version
) {
1319 DBG("New metadata version detected");
1320 stream
->metadata_version
= cur_version
;
1321 stream
->reset_metadata_flag
= 1;
1329 * Consume data on a file descriptor and write it on a trace file.
1331 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1332 struct lttng_consumer_local_data
*ctx
)
1334 unsigned long len
, subbuf_size
, padding
;
1335 int err
, write_index
= 1;
1337 int infd
= stream
->wait_fd
;
1338 struct ctf_packet_index index
;
1340 DBG("In read_subbuffer (infd : %d)", infd
);
1342 /* Get the next subbuffer */
1343 err
= kernctl_get_next_subbuf(infd
);
1346 * This is a debug message even for single-threaded consumer,
1347 * because poll() have more relaxed criterions than get subbuf,
1348 * so get_subbuf may fail for short race windows where poll()
1349 * would issue wakeups.
1351 DBG("Reserving sub buffer failed (everything is normal, "
1352 "it is due to concurrency)");
1357 /* Get the full subbuffer size including padding */
1358 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1360 PERROR("Getting sub-buffer len failed.");
1361 err
= kernctl_put_subbuf(infd
);
1363 if (err
== -EFAULT
) {
1364 PERROR("Error in unreserving sub buffer\n");
1365 } else if (err
== -EIO
) {
1366 /* Should never happen with newer LTTng versions */
1367 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1376 if (!stream
->metadata_flag
) {
1377 ret
= get_index_values(&index
, infd
);
1379 err
= kernctl_put_subbuf(infd
);
1381 if (err
== -EFAULT
) {
1382 PERROR("Error in unreserving sub buffer\n");
1383 } else if (err
== -EIO
) {
1384 /* Should never happen with newer LTTng versions */
1385 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1392 ret
= update_stream_stats(stream
);
1394 err
= kernctl_put_subbuf(infd
);
1396 if (err
== -EFAULT
) {
1397 PERROR("Error in unreserving sub buffer\n");
1398 } else if (err
== -EIO
) {
1399 /* Should never happen with newer LTTng versions */
1400 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1409 ret
= metadata_stream_check_version(infd
, stream
);
1411 err
= kernctl_put_subbuf(infd
);
1413 if (err
== -EFAULT
) {
1414 PERROR("Error in unreserving sub buffer\n");
1415 } else if (err
== -EIO
) {
1416 /* Should never happen with newer LTTng versions */
1417 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1426 switch (stream
->chan
->output
) {
1427 case CONSUMER_CHANNEL_SPLICE
:
1429 * XXX: The lttng-modules splice "actor" does not handle copying
1430 * partial pages hence only using the subbuffer size without the
1431 * padding makes the splice fail.
1436 /* splice the subbuffer to the tracefile */
1437 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1440 * XXX: Splice does not support network streaming so the return value
1441 * is simply checked against subbuf_size and not like the mmap() op.
1443 if (ret
!= subbuf_size
) {
1445 * display the error but continue processing to try
1446 * to release the subbuffer
1448 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1453 case CONSUMER_CHANNEL_MMAP
:
1454 /* Get subbuffer size without padding */
1455 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1457 PERROR("Getting sub-buffer len failed.");
1458 err
= kernctl_put_subbuf(infd
);
1460 if (err
== -EFAULT
) {
1461 PERROR("Error in unreserving sub buffer\n");
1462 } else if (err
== -EIO
) {
1463 /* Should never happen with newer LTTng versions */
1464 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1473 /* Make sure the tracer is not gone mad on us! */
1474 assert(len
>= subbuf_size
);
1476 padding
= len
- subbuf_size
;
1478 /* write the subbuffer to the tracefile */
1479 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1482 * The mmap operation should write subbuf_size amount of data when
1483 * network streaming or the full padding (len) size when we are _not_
1486 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1487 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1489 * Display the error but continue processing to try to release the
1490 * subbuffer. This is a DBG statement since this is possible to
1491 * happen without being a critical error.
1493 DBG("Error writing to tracefile "
1494 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1495 ret
, len
, subbuf_size
);
1500 ERR("Unknown output method");
1504 err
= kernctl_put_next_subbuf(infd
);
1506 if (err
== -EFAULT
) {
1507 PERROR("Error in unreserving sub buffer\n");
1508 } else if (err
== -EIO
) {
1509 /* Should never happen with newer LTTng versions */
1510 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1516 /* Write index if needed. */
1521 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1523 * In live, block until all the metadata is sent.
1525 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1526 assert(!stream
->missed_metadata_flush
);
1527 stream
->waiting_on_metadata
= true;
1528 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1530 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1532 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1533 stream
->waiting_on_metadata
= false;
1534 if (stream
->missed_metadata_flush
) {
1535 stream
->missed_metadata_flush
= false;
1536 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1537 (void) consumer_flush_kernel_index(stream
);
1539 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1546 err
= consumer_stream_write_index(stream
, &index
);
1555 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1562 * Don't create anything if this is set for streaming or should not be
1565 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1566 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1567 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1568 stream
->uid
, stream
->gid
, NULL
);
1572 stream
->out_fd
= ret
;
1573 stream
->tracefile_size_current
= 0;
1575 if (!stream
->metadata_flag
) {
1576 struct lttng_index_file
*index_file
;
1578 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1579 stream
->name
, stream
->uid
, stream
->gid
,
1580 stream
->chan
->tracefile_size
,
1581 stream
->tracefile_count_current
,
1582 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1586 assert(!stream
->index_file
);
1587 stream
->index_file
= index_file
;
1591 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1592 /* get the len of the mmap region */
1593 unsigned long mmap_len
;
1595 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1597 PERROR("kernctl_get_mmap_len");
1598 goto error_close_fd
;
1600 stream
->mmap_len
= (size_t) mmap_len
;
1602 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1603 MAP_PRIVATE
, stream
->wait_fd
, 0);
1604 if (stream
->mmap_base
== MAP_FAILED
) {
1605 PERROR("Error mmaping");
1607 goto error_close_fd
;
1611 /* we return 0 to let the library handle the FD internally */
1615 if (stream
->out_fd
>= 0) {
1618 err
= close(stream
->out_fd
);
1620 stream
->out_fd
= -1;
1627 * Check if data is still being extracted from the buffers for a specific
1628 * stream. Consumer data lock MUST be acquired before calling this function
1629 * and the stream lock.
1631 * Return 1 if the traced data are still getting read else 0 meaning that the
1632 * data is available for trace viewer reading.
1634 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1640 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1645 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1647 /* There is still data so let's put back this subbuffer. */
1648 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1650 ret
= 1; /* Data is pending */
1654 /* Data is NOT pending and ready to be read. */