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
);
175 ERR("sending stream to relayd");
179 ret
= utils_create_stream_file(path
, stream
->name
,
180 stream
->chan
->tracefile_size
,
181 stream
->tracefile_count_current
,
182 stream
->uid
, stream
->gid
, NULL
);
184 ERR("utils_create_stream_file");
188 stream
->out_fd
= ret
;
189 stream
->tracefile_size_current
= 0;
191 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
192 path
, stream
->name
, stream
->key
);
195 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
198 * Doing a buffer flush which does not take into
199 * account empty packets. This is not perfect
200 * for stream intersection, but required as a
201 * fall-back when "flush_empty" is not
202 * implemented by lttng-modules.
204 ret
= kernctl_buffer_flush(stream
->wait_fd
);
206 ERR("Failed to flush kernel stream");
212 ret
= lttng_kconsumer_take_snapshot(stream
);
214 ERR("Taking kernel snapshot");
218 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
220 ERR("Produced kernel snapshot position");
224 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
226 ERR("Consumerd kernel snapshot position");
230 if (stream
->max_sb_size
== 0) {
231 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
232 &stream
->max_sb_size
);
234 ERR("Getting kernel max_sb_size");
239 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
240 produced_pos
, nb_packets_per_stream
,
241 stream
->max_sb_size
);
243 while (consumed_pos
< produced_pos
) {
245 unsigned long len
, padded_len
;
247 health_code_update();
249 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
251 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
253 if (ret
!= -EAGAIN
) {
254 PERROR("kernctl_get_subbuf snapshot");
257 DBG("Kernel consumer get subbuf failed. Skipping it.");
258 consumed_pos
+= stream
->max_sb_size
;
259 stream
->chan
->lost_packets
++;
263 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
265 ERR("Snapshot kernctl_get_subbuf_size");
266 goto error_put_subbuf
;
269 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
271 ERR("Snapshot kernctl_get_padded_subbuf_size");
272 goto error_put_subbuf
;
275 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
276 padded_len
- len
, NULL
);
278 * We write the padded len in local tracefiles but the data len
279 * when using a relay. Display the error but continue processing
280 * to try to release the subbuffer.
282 if (relayd_id
!= (uint64_t) -1ULL) {
283 if (read_len
!= len
) {
284 ERR("Error sending to the relay (ret: %zd != len: %lu)",
288 if (read_len
!= padded_len
) {
289 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
290 read_len
, padded_len
);
294 ret
= kernctl_put_subbuf(stream
->wait_fd
);
296 ERR("Snapshot kernctl_put_subbuf");
299 consumed_pos
+= stream
->max_sb_size
;
302 if (relayd_id
== (uint64_t) -1ULL) {
303 if (stream
->out_fd
>= 0) {
304 ret
= close(stream
->out_fd
);
306 PERROR("Kernel consumer snapshot close out_fd");
312 close_relayd_stream(stream
);
313 stream
->net_seq_idx
= (uint64_t) -1ULL;
315 pthread_mutex_unlock(&stream
->lock
);
323 ret
= kernctl_put_subbuf(stream
->wait_fd
);
325 ERR("Snapshot kernctl_put_subbuf error path");
328 pthread_mutex_unlock(&stream
->lock
);
335 * Read the whole metadata available for a snapshot.
337 * Returns 0 on success, < 0 on error
339 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
340 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
342 int ret
, use_relayd
= 0;
344 struct lttng_consumer_channel
*metadata_channel
;
345 struct lttng_consumer_stream
*metadata_stream
;
349 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
354 metadata_channel
= consumer_find_channel(key
);
355 if (!metadata_channel
) {
356 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
361 metadata_stream
= metadata_channel
->metadata_stream
;
362 assert(metadata_stream
);
364 /* Flag once that we have a valid relayd for the stream. */
365 if (relayd_id
!= (uint64_t) -1ULL) {
370 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
375 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
376 metadata_stream
->chan
->tracefile_size
,
377 metadata_stream
->tracefile_count_current
,
378 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
382 metadata_stream
->out_fd
= ret
;
386 health_code_update();
388 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
390 if (ret_read
!= -EAGAIN
) {
391 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
395 /* ret_read is negative at this point so we will exit the loop. */
398 } while (ret_read
>= 0);
401 close_relayd_stream(metadata_stream
);
402 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
404 if (metadata_stream
->out_fd
>= 0) {
405 ret
= close(metadata_stream
->out_fd
);
407 PERROR("Kernel consumer snapshot metadata close out_fd");
409 * Don't go on error here since the snapshot was successful at this
410 * point but somehow the close failed.
413 metadata_stream
->out_fd
= -1;
419 cds_list_del(&metadata_stream
->send_node
);
420 consumer_stream_destroy(metadata_stream
, NULL
);
421 metadata_channel
->metadata_stream
= NULL
;
428 * Receive command from session daemon and process it.
430 * Return 1 on success else a negative value or 0.
432 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
433 int sock
, struct pollfd
*consumer_sockpoll
)
436 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
437 struct lttcomm_consumer_msg msg
;
439 health_code_update();
441 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
442 if (ret
!= sizeof(msg
)) {
444 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
450 health_code_update();
452 /* Deprecated command */
453 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
455 health_code_update();
457 /* relayd needs RCU read-side protection */
460 switch (msg
.cmd_type
) {
461 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
463 /* Session daemon status message are handled in the following call. */
464 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
465 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
466 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
467 msg
.u
.relayd_sock
.relayd_session_id
);
470 case LTTNG_CONSUMER_ADD_CHANNEL
:
472 struct lttng_consumer_channel
*new_channel
;
475 health_code_update();
477 /* First send a status message before receiving the fds. */
478 ret
= consumer_send_status_msg(sock
, ret_code
);
480 /* Somehow, the session daemon is not responding anymore. */
484 health_code_update();
486 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
487 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
488 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
489 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
490 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
491 msg
.u
.channel
.tracefile_size
,
492 msg
.u
.channel
.tracefile_count
, 0,
493 msg
.u
.channel
.monitor
,
494 msg
.u
.channel
.live_timer_interval
,
496 if (new_channel
== NULL
) {
497 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
500 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
501 switch (msg
.u
.channel
.output
) {
502 case LTTNG_EVENT_SPLICE
:
503 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
505 case LTTNG_EVENT_MMAP
:
506 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
509 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
513 /* Translate and save channel type. */
514 switch (msg
.u
.channel
.type
) {
515 case CONSUMER_CHANNEL_TYPE_DATA
:
516 case CONSUMER_CHANNEL_TYPE_METADATA
:
517 new_channel
->type
= msg
.u
.channel
.type
;
524 health_code_update();
526 if (ctx
->on_recv_channel
!= NULL
) {
527 ret_recv
= ctx
->on_recv_channel(new_channel
);
529 ret
= consumer_add_channel(new_channel
, ctx
);
530 } else if (ret_recv
< 0) {
534 ret
= consumer_add_channel(new_channel
, ctx
);
536 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
537 int monitor_start_ret
;
539 DBG("Consumer starting monitor timer");
540 consumer_timer_live_start(new_channel
,
541 msg
.u
.channel
.live_timer_interval
);
542 monitor_start_ret
= consumer_timer_monitor_start(
544 msg
.u
.channel
.monitor_timer_interval
);
545 if (monitor_start_ret
< 0) {
546 ERR("Starting channel monitoring timer failed");
552 health_code_update();
554 /* If we received an error in add_channel, we need to report it. */
556 ret
= consumer_send_status_msg(sock
, ret
);
565 case LTTNG_CONSUMER_ADD_STREAM
:
568 struct lttng_pipe
*stream_pipe
;
569 struct lttng_consumer_stream
*new_stream
;
570 struct lttng_consumer_channel
*channel
;
574 * Get stream's channel reference. Needed when adding the stream to the
577 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
580 * We could not find the channel. Can happen if cpu hotplug
581 * happens while tearing down.
583 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
584 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
587 health_code_update();
589 /* First send a status message before receiving the fds. */
590 ret
= consumer_send_status_msg(sock
, ret_code
);
592 /* Somehow, the session daemon is not responding anymore. */
596 health_code_update();
598 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
599 /* Channel was not found. */
605 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
611 health_code_update();
613 /* Get stream file descriptor from socket */
614 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
615 if (ret
!= sizeof(fd
)) {
616 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
621 health_code_update();
624 * Send status code to session daemon only if the recv works. If the
625 * above recv() failed, the session daemon is notified through the
626 * error socket and the teardown is eventually done.
628 ret
= consumer_send_status_msg(sock
, ret_code
);
630 /* Somehow, the session daemon is not responding anymore. */
634 health_code_update();
636 new_stream
= consumer_allocate_stream(channel
->key
,
638 LTTNG_CONSUMER_ACTIVE_STREAM
,
648 if (new_stream
== NULL
) {
653 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
659 new_stream
->chan
= channel
;
660 new_stream
->wait_fd
= fd
;
661 consumer_stream_update_channel_attributes(new_stream
,
663 switch (channel
->output
) {
664 case CONSUMER_CHANNEL_SPLICE
:
665 new_stream
->output
= LTTNG_EVENT_SPLICE
;
666 ret
= utils_create_pipe(new_stream
->splice_pipe
);
671 case CONSUMER_CHANNEL_MMAP
:
672 new_stream
->output
= LTTNG_EVENT_MMAP
;
675 ERR("Stream output unknown %d", channel
->output
);
680 * We've just assigned the channel to the stream so increment the
681 * refcount right now. We don't need to increment the refcount for
682 * streams in no monitor because we handle manually the cleanup of
683 * those. It is very important to make sure there is NO prior
684 * consumer_del_stream() calls or else the refcount will be unbalanced.
686 if (channel
->monitor
) {
687 uatomic_inc(&new_stream
->chan
->refcount
);
691 * The buffer flush is done on the session daemon side for the kernel
692 * so no need for the stream "hangup_flush_done" variable to be
693 * tracked. This is important for a kernel stream since we don't rely
694 * on the flush state of the stream to read data. It's not the case for
695 * user space tracing.
697 new_stream
->hangup_flush_done
= 0;
699 health_code_update();
701 if (ctx
->on_recv_stream
) {
702 ret
= ctx
->on_recv_stream(new_stream
);
704 consumer_stream_free(new_stream
);
709 health_code_update();
711 if (new_stream
->metadata_flag
) {
712 channel
->metadata_stream
= new_stream
;
715 /* Do not monitor this stream. */
716 if (!channel
->monitor
) {
717 DBG("Kernel consumer add stream %s in no monitor mode with "
718 "relayd id %" PRIu64
, new_stream
->name
,
719 new_stream
->net_seq_idx
);
720 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
724 /* Send stream to relayd if the stream has an ID. */
725 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
726 ret
= consumer_send_relayd_stream(new_stream
,
727 new_stream
->chan
->pathname
);
729 consumer_stream_free(new_stream
);
734 * If adding an extra stream to an already
735 * existing channel (e.g. cpu hotplug), we need
736 * to send the "streams_sent" command to relayd.
738 if (channel
->streams_sent_to_relayd
) {
739 ret
= consumer_send_relayd_streams_sent(
740 new_stream
->net_seq_idx
);
747 /* Get the right pipe where the stream will be sent. */
748 if (new_stream
->metadata_flag
) {
749 consumer_add_metadata_stream(new_stream
);
750 stream_pipe
= ctx
->consumer_metadata_pipe
;
752 consumer_add_data_stream(new_stream
);
753 stream_pipe
= ctx
->consumer_data_pipe
;
756 /* Visible to other threads */
757 new_stream
->globally_visible
= 1;
759 health_code_update();
761 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
763 ERR("Consumer write %s stream to pipe %d",
764 new_stream
->metadata_flag
? "metadata" : "data",
765 lttng_pipe_get_writefd(stream_pipe
));
766 if (new_stream
->metadata_flag
) {
767 consumer_del_stream_for_metadata(new_stream
);
769 consumer_del_stream_for_data(new_stream
);
774 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
775 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
778 case LTTNG_CONSUMER_STREAMS_SENT
:
780 struct lttng_consumer_channel
*channel
;
783 * Get stream's channel reference. Needed when adding the stream to the
786 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
789 * We could not find the channel. Can happen if cpu hotplug
790 * happens while tearing down.
792 ERR("Unable to find channel key %" PRIu64
,
793 msg
.u
.sent_streams
.channel_key
);
794 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
797 health_code_update();
800 * Send status code to session daemon.
802 ret
= consumer_send_status_msg(sock
, ret_code
);
803 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
804 /* Somehow, the session daemon is not responding anymore. */
808 health_code_update();
811 * We should not send this message if we don't monitor the
812 * streams in this channel.
814 if (!channel
->monitor
) {
818 health_code_update();
819 /* Send stream to relayd if the stream has an ID. */
820 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
821 ret
= consumer_send_relayd_streams_sent(
822 msg
.u
.sent_streams
.net_seq_idx
);
826 channel
->streams_sent_to_relayd
= true;
830 case LTTNG_CONSUMER_UPDATE_STREAM
:
835 case LTTNG_CONSUMER_DESTROY_RELAYD
:
837 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
838 struct consumer_relayd_sock_pair
*relayd
;
840 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
842 /* Get relayd reference if exists. */
843 relayd
= consumer_find_relayd(index
);
844 if (relayd
== NULL
) {
845 DBG("Unable to find relayd %" PRIu64
, index
);
846 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
850 * Each relayd socket pair has a refcount of stream attached to it
851 * which tells if the relayd is still active or not depending on the
854 * This will set the destroy flag of the relayd object and destroy it
855 * if the refcount reaches zero when called.
857 * The destroy can happen either here or when a stream fd hangs up.
860 consumer_flag_relayd_for_destroy(relayd
);
863 health_code_update();
865 ret
= consumer_send_status_msg(sock
, ret_code
);
867 /* Somehow, the session daemon is not responding anymore. */
873 case LTTNG_CONSUMER_DATA_PENDING
:
876 uint64_t id
= msg
.u
.data_pending
.session_id
;
878 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
880 ret
= consumer_data_pending(id
);
882 health_code_update();
884 /* Send back returned value to session daemon */
885 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
887 PERROR("send data pending ret code");
892 * No need to send back a status message since the data pending
893 * returned value is the response.
897 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
899 if (msg
.u
.snapshot_channel
.metadata
== 1) {
900 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
901 msg
.u
.snapshot_channel
.pathname
,
902 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
904 ERR("Snapshot metadata failed");
905 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
908 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
909 msg
.u
.snapshot_channel
.pathname
,
910 msg
.u
.snapshot_channel
.relayd_id
,
911 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
914 ERR("Snapshot channel failed");
915 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
919 health_code_update();
921 ret
= consumer_send_status_msg(sock
, ret_code
);
923 /* Somehow, the session daemon is not responding anymore. */
928 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
930 uint64_t key
= msg
.u
.destroy_channel
.key
;
931 struct lttng_consumer_channel
*channel
;
933 channel
= consumer_find_channel(key
);
935 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
936 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
939 health_code_update();
941 ret
= consumer_send_status_msg(sock
, ret_code
);
943 /* Somehow, the session daemon is not responding anymore. */
947 health_code_update();
949 /* Stop right now if no channel was found. */
955 * This command should ONLY be issued for channel with streams set in
958 assert(!channel
->monitor
);
961 * The refcount should ALWAYS be 0 in the case of a channel in no
964 assert(!uatomic_sub_return(&channel
->refcount
, 1));
966 consumer_del_channel(channel
);
970 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
974 struct lttng_consumer_channel
*channel
;
975 uint64_t id
= msg
.u
.discarded_events
.session_id
;
976 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
978 DBG("Kernel consumer discarded events command for session id %"
979 PRIu64
", channel key %" PRIu64
, id
, key
);
981 channel
= consumer_find_channel(key
);
983 ERR("Kernel consumer discarded events channel %"
984 PRIu64
" not found", key
);
987 count
= channel
->discarded_events
;
990 health_code_update();
992 /* Send back returned value to session daemon */
993 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
995 PERROR("send discarded events");
1001 case LTTNG_CONSUMER_LOST_PACKETS
:
1005 struct lttng_consumer_channel
*channel
;
1006 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1007 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1009 DBG("Kernel consumer lost packets command for session id %"
1010 PRIu64
", channel key %" PRIu64
, id
, key
);
1012 channel
= consumer_find_channel(key
);
1014 ERR("Kernel consumer lost packets channel %"
1015 PRIu64
" not found", key
);
1018 count
= channel
->lost_packets
;
1021 health_code_update();
1023 /* Send back returned value to session daemon */
1024 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1026 PERROR("send lost packets");
1032 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1034 int channel_monitor_pipe
;
1036 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1037 /* Successfully received the command's type. */
1038 ret
= consumer_send_status_msg(sock
, ret_code
);
1043 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1045 if (ret
!= sizeof(channel_monitor_pipe
)) {
1046 ERR("Failed to receive channel monitor pipe");
1050 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1051 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1052 channel_monitor_pipe
);
1056 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1057 /* Set the pipe as non-blocking. */
1058 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1060 PERROR("fcntl get flags of the channel monitoring pipe");
1065 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1066 flags
| O_NONBLOCK
);
1068 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1071 DBG("Channel monitor pipe set as non-blocking");
1073 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1075 ret
= consumer_send_status_msg(sock
, ret_code
);
1081 case LTTNG_CONSUMER_SET_CHANNEL_ROTATE_PIPE
:
1083 int channel_rotate_pipe
;
1086 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1087 /* Successfully received the command's type. */
1088 ret
= consumer_send_status_msg(sock
, ret_code
);
1093 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_rotate_pipe
, 1);
1094 if (ret
!= (ssize_t
) sizeof(channel_rotate_pipe
)) {
1095 ERR("Failed to receive channel rotate pipe");
1099 DBG("Received channel rotate pipe (%d)", channel_rotate_pipe
);
1100 ctx
->channel_rotate_pipe
= channel_rotate_pipe
;
1101 /* Set the pipe as non-blocking. */
1102 ret
= fcntl(channel_rotate_pipe
, F_GETFL
, 0);
1104 PERROR("fcntl get flags of the channel rotate pipe");
1109 ret
= fcntl(channel_rotate_pipe
, F_SETFL
, flags
| O_NONBLOCK
);
1111 PERROR("fcntl set O_NONBLOCK flag of the channel rotate pipe");
1114 DBG("Channel rotate pipe set as non-blocking");
1115 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1116 ret
= consumer_send_status_msg(sock
, ret_code
);
1122 case LTTNG_CONSUMER_ROTATE_RENAME
:
1124 DBG("Consumer rename session %" PRIu64
" after rotation, old path = \"%s\", new path = \"%s\"",
1125 msg
.u
.rotate_rename
.session_id
,
1126 msg
.u
.rotate_rename
.old_path
,
1127 msg
.u
.rotate_rename
.new_path
);
1128 ret
= lttng_consumer_rotate_rename(msg
.u
.rotate_rename
.old_path
,
1129 msg
.u
.rotate_rename
.new_path
,
1130 msg
.u
.rotate_rename
.uid
,
1131 msg
.u
.rotate_rename
.gid
,
1132 msg
.u
.rotate_rename
.relayd_id
);
1134 ERR("Rotate rename failed");
1135 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1138 health_code_update();
1140 ret
= consumer_send_status_msg(sock
, ret_code
);
1142 /* Somehow, the session daemon is not responding anymore. */
1147 case LTTNG_CONSUMER_MKDIR
:
1149 DBG("Consumer mkdir %s in session %" PRIu64
,
1151 msg
.u
.mkdir
.session_id
);
1152 ret
= lttng_consumer_mkdir(msg
.u
.mkdir
.path
,
1155 msg
.u
.mkdir
.relayd_id
);
1157 ERR("consumer mkdir failed");
1158 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1161 health_code_update();
1163 ret
= consumer_send_status_msg(sock
, ret_code
);
1165 /* Somehow, the session daemon is not responding anymore. */
1178 * Return 1 to indicate success since the 0 value can be a socket
1179 * shutdown during the recv() or send() call.
1181 health_code_update();
1186 /* This will issue a consumer stop. */
1191 * Populate index values of a kernel stream. Values are set in big endian order.
1193 * Return 0 on success or else a negative value.
1195 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1199 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1201 PERROR("kernctl_get_timestamp_begin");
1204 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1206 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1208 PERROR("kernctl_get_timestamp_end");
1211 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1213 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1215 PERROR("kernctl_get_events_discarded");
1218 index
->events_discarded
= htobe64(index
->events_discarded
);
1220 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1222 PERROR("kernctl_get_content_size");
1225 index
->content_size
= htobe64(index
->content_size
);
1227 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1229 PERROR("kernctl_get_packet_size");
1232 index
->packet_size
= htobe64(index
->packet_size
);
1234 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1236 PERROR("kernctl_get_stream_id");
1239 index
->stream_id
= htobe64(index
->stream_id
);
1241 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1243 if (ret
== -ENOTTY
) {
1244 /* Command not implemented by lttng-modules. */
1245 index
->stream_instance_id
= -1ULL;
1247 PERROR("kernctl_get_instance_id");
1251 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1253 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1255 if (ret
== -ENOTTY
) {
1256 /* Command not implemented by lttng-modules. */
1257 index
->packet_seq_num
= -1ULL;
1260 PERROR("kernctl_get_sequence_number");
1264 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1270 * Sync metadata meaning request them to the session daemon and snapshot to the
1271 * metadata thread can consumer them.
1273 * Metadata stream lock MUST be acquired.
1275 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1276 * is empty or a negative value on error.
1278 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1284 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1286 ERR("Failed to flush kernel stream");
1290 ret
= kernctl_snapshot(metadata
->wait_fd
);
1292 if (ret
!= -EAGAIN
) {
1293 ERR("Sync metadata, taking kernel snapshot failed.");
1296 DBG("Sync metadata, no new kernel metadata");
1297 /* No new metadata, exit. */
1307 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1310 uint64_t seq
, discarded
;
1312 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1314 if (ret
== -ENOTTY
) {
1315 /* Command not implemented by lttng-modules. */
1318 PERROR("kernctl_get_sequence_number");
1324 * Start the sequence when we extract the first packet in case we don't
1325 * start at 0 (for example if a consumer is not connected to the
1326 * session immediately after the beginning).
1328 if (stream
->last_sequence_number
== -1ULL) {
1329 stream
->last_sequence_number
= seq
;
1330 } else if (seq
> stream
->last_sequence_number
) {
1331 stream
->chan
->lost_packets
+= seq
-
1332 stream
->last_sequence_number
- 1;
1334 /* seq <= last_sequence_number */
1335 ERR("Sequence number inconsistent : prev = %" PRIu64
1336 ", current = %" PRIu64
,
1337 stream
->last_sequence_number
, seq
);
1341 stream
->last_sequence_number
= seq
;
1343 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1345 PERROR("kernctl_get_events_discarded");
1348 if (discarded
< stream
->last_discarded_events
) {
1350 * Overflow has occurred. We assume only one wrap-around
1353 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1354 stream
->last_discarded_events
+ discarded
;
1356 stream
->chan
->discarded_events
+= discarded
-
1357 stream
->last_discarded_events
;
1359 stream
->last_discarded_events
= discarded
;
1367 * Check if the local version of the metadata stream matches with the version
1368 * of the metadata stream in the kernel. If it was updated, set the reset flag
1372 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1375 uint64_t cur_version
;
1377 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1379 if (ret
== -ENOTTY
) {
1381 * LTTng-modules does not implement this
1387 ERR("Failed to get the metadata version");
1391 if (stream
->metadata_version
== cur_version
) {
1396 DBG("New metadata version detected");
1397 stream
->metadata_version
= cur_version
;
1398 stream
->reset_metadata_flag
= 1;
1406 * Consume data on a file descriptor and write it on a trace file.
1408 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1409 struct lttng_consumer_local_data
*ctx
)
1411 unsigned long len
, subbuf_size
, padding
;
1412 int err
, write_index
= 1;
1414 int infd
= stream
->wait_fd
;
1415 struct ctf_packet_index index
;
1417 DBG("In read_subbuffer (infd : %d)", infd
);
1419 /* Get the next subbuffer */
1420 err
= kernctl_get_next_subbuf(infd
);
1423 * This is a debug message even for single-threaded consumer,
1424 * because poll() have more relaxed criterions than get subbuf,
1425 * so get_subbuf may fail for short race windows where poll()
1426 * would issue wakeups.
1428 DBG("Reserving sub buffer failed (everything is normal, "
1429 "it is due to concurrency)");
1434 /* Get the full subbuffer size including padding */
1435 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1437 PERROR("Getting sub-buffer len failed.");
1438 err
= kernctl_put_subbuf(infd
);
1440 if (err
== -EFAULT
) {
1441 PERROR("Error in unreserving sub buffer\n");
1442 } else if (err
== -EIO
) {
1443 /* Should never happen with newer LTTng versions */
1444 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1453 if (!stream
->metadata_flag
) {
1454 ret
= get_index_values(&index
, infd
);
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.");
1469 ret
= update_stream_stats(stream
);
1471 err
= kernctl_put_subbuf(infd
);
1473 if (err
== -EFAULT
) {
1474 PERROR("Error in unreserving sub buffer\n");
1475 } else if (err
== -EIO
) {
1476 /* Should never happen with newer LTTng versions */
1477 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1486 ret
= metadata_stream_check_version(infd
, stream
);
1488 err
= kernctl_put_subbuf(infd
);
1490 if (err
== -EFAULT
) {
1491 PERROR("Error in unreserving sub buffer\n");
1492 } else if (err
== -EIO
) {
1493 /* Should never happen with newer LTTng versions */
1494 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1503 switch (stream
->chan
->output
) {
1504 case CONSUMER_CHANNEL_SPLICE
:
1506 * XXX: The lttng-modules splice "actor" does not handle copying
1507 * partial pages hence only using the subbuffer size without the
1508 * padding makes the splice fail.
1513 /* splice the subbuffer to the tracefile */
1514 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1517 * XXX: Splice does not support network streaming so the return value
1518 * is simply checked against subbuf_size and not like the mmap() op.
1520 if (ret
!= subbuf_size
) {
1522 * display the error but continue processing to try
1523 * to release the subbuffer
1525 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1530 case CONSUMER_CHANNEL_MMAP
:
1531 /* Get subbuffer size without padding */
1532 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1534 PERROR("Getting sub-buffer len failed.");
1535 err
= kernctl_put_subbuf(infd
);
1537 if (err
== -EFAULT
) {
1538 PERROR("Error in unreserving sub buffer\n");
1539 } else if (err
== -EIO
) {
1540 /* Should never happen with newer LTTng versions */
1541 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1550 /* Make sure the tracer is not gone mad on us! */
1551 assert(len
>= subbuf_size
);
1553 padding
= len
- subbuf_size
;
1555 /* write the subbuffer to the tracefile */
1556 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1559 * The mmap operation should write subbuf_size amount of data when
1560 * network streaming or the full padding (len) size when we are _not_
1563 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1564 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1566 * Display the error but continue processing to try to release the
1567 * subbuffer. This is a DBG statement since this is possible to
1568 * happen without being a critical error.
1570 DBG("Error writing to tracefile "
1571 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1572 ret
, len
, subbuf_size
);
1577 ERR("Unknown output method");
1581 err
= kernctl_put_next_subbuf(infd
);
1583 if (err
== -EFAULT
) {
1584 PERROR("Error in unreserving sub buffer\n");
1585 } else if (err
== -EIO
) {
1586 /* Should never happen with newer LTTng versions */
1587 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1593 /* Write index if needed. */
1598 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1600 * In live, block until all the metadata is sent.
1602 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1603 assert(!stream
->missed_metadata_flush
);
1604 stream
->waiting_on_metadata
= true;
1605 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1607 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1609 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1610 stream
->waiting_on_metadata
= false;
1611 if (stream
->missed_metadata_flush
) {
1612 stream
->missed_metadata_flush
= false;
1613 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1614 (void) consumer_flush_kernel_index(stream
);
1616 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1623 err
= consumer_stream_write_index(stream
, &index
);
1632 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1639 * Don't create anything if this is set for streaming or should not be
1642 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1643 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1644 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1645 stream
->uid
, stream
->gid
, NULL
);
1649 stream
->out_fd
= ret
;
1650 stream
->tracefile_size_current
= 0;
1652 if (!stream
->metadata_flag
) {
1653 struct lttng_index_file
*index_file
;
1655 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1656 stream
->name
, stream
->uid
, stream
->gid
,
1657 stream
->chan
->tracefile_size
,
1658 stream
->tracefile_count_current
,
1659 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1663 assert(!stream
->index_file
);
1664 stream
->index_file
= index_file
;
1668 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1669 /* get the len of the mmap region */
1670 unsigned long mmap_len
;
1672 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1674 PERROR("kernctl_get_mmap_len");
1675 goto error_close_fd
;
1677 stream
->mmap_len
= (size_t) mmap_len
;
1679 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1680 MAP_PRIVATE
, stream
->wait_fd
, 0);
1681 if (stream
->mmap_base
== MAP_FAILED
) {
1682 PERROR("Error mmaping");
1684 goto error_close_fd
;
1688 /* we return 0 to let the library handle the FD internally */
1692 if (stream
->out_fd
>= 0) {
1695 err
= close(stream
->out_fd
);
1697 stream
->out_fd
= -1;
1704 * Check if data is still being extracted from the buffers for a specific
1705 * stream. Consumer data lock MUST be acquired before calling this function
1706 * and the stream lock.
1708 * Return 1 if the traced data are still getting read else 0 meaning that the
1709 * data is available for trace viewer reading.
1711 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1717 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1722 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1724 /* There is still data so let's put back this subbuffer. */
1725 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1727 ret
= 1; /* Data is pending */
1731 /* Data is NOT pending and ready to be read. */