2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
46 #include "kernel-consumer.h"
48 extern struct lttng_consumer_global_data consumer_data
;
49 extern int consumer_poll_timeout
;
50 extern volatile int consumer_quit
;
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 * Get the produced position
73 * Returns 0 on success, < 0 on error
75 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
79 int infd
= stream
->wait_fd
;
81 ret
= kernctl_snapshot_get_produced(infd
, pos
);
83 PERROR("kernctl_snapshot_get_produced");
90 * Get the consumerd position
92 * Returns 0 on success, < 0 on error
94 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
98 int infd
= stream
->wait_fd
;
100 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
102 PERROR("kernctl_snapshot_get_consumed");
109 * Take a snapshot of all the stream of a channel
111 * Returns 0 on success, < 0 on error
113 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
114 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
115 struct lttng_consumer_local_data
*ctx
)
118 struct lttng_consumer_channel
*channel
;
119 struct lttng_consumer_stream
*stream
;
121 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
125 channel
= consumer_find_channel(key
);
127 ERR("No channel found for key %" PRIu64
, key
);
132 /* Splice is not supported yet for channel snapshot. */
133 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
134 ERR("Unsupported output %d", channel
->output
);
139 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
140 /* Are we at a position _before_ the first available packet ? */
141 bool before_first_packet
= true;
142 unsigned long consumed_pos
, produced_pos
;
144 health_code_update();
147 * Lock stream because we are about to change its state.
149 pthread_mutex_lock(&stream
->lock
);
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
155 stream
->net_seq_idx
= relayd_id
;
156 channel
->relayd_id
= relayd_id
;
157 if (relayd_id
!= (uint64_t) -1ULL) {
158 ret
= consumer_send_relayd_stream(stream
, path
);
160 ERR("sending stream to relayd");
164 ret
= utils_create_stream_file(path
, stream
->name
,
165 stream
->chan
->tracefile_size
,
166 stream
->tracefile_count_current
,
167 stream
->uid
, stream
->gid
, NULL
);
169 ERR("utils_create_stream_file");
173 stream
->out_fd
= ret
;
174 stream
->tracefile_size_current
= 0;
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
177 path
, stream
->name
, stream
->key
);
179 if (relayd_id
!= -1ULL) {
180 ret
= consumer_send_relayd_streams_sent(relayd_id
);
182 ERR("sending streams sent to relayd");
185 channel
->streams_sent_to_relayd
= true;
188 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
191 * Doing a buffer flush which does not take into
192 * account empty packets. This is not perfect
193 * for stream intersection, but required as a
194 * fall-back when "flush_empty" is not
195 * implemented by lttng-modules.
197 ret
= kernctl_buffer_flush(stream
->wait_fd
);
199 ERR("Failed to flush kernel stream");
205 ret
= lttng_kconsumer_take_snapshot(stream
);
207 ERR("Taking kernel snapshot");
211 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
213 ERR("Produced kernel snapshot position");
217 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
219 ERR("Consumerd kernel snapshot position");
223 if (stream
->max_sb_size
== 0) {
224 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
225 &stream
->max_sb_size
);
227 ERR("Getting kernel max_sb_size");
232 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
233 produced_pos
, nb_packets_per_stream
,
234 stream
->max_sb_size
);
236 while (consumed_pos
< produced_pos
) {
238 unsigned long len
, padded_len
;
241 health_code_update();
243 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
245 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
247 if (ret
!= -EAGAIN
) {
248 PERROR("kernctl_get_subbuf snapshot");
251 DBG("Kernel consumer get subbuf failed. Skipping it.");
252 consumed_pos
+= stream
->max_sb_size
;
255 * Start accounting lost packets only when we
256 * already have extracted packets (to match the
257 * content of the final snapshot).
259 if (!before_first_packet
) {
265 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
267 ERR("Snapshot kernctl_get_subbuf_size");
268 goto error_put_subbuf
;
271 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
273 ERR("Snapshot kernctl_get_padded_subbuf_size");
274 goto error_put_subbuf
;
277 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
278 padded_len
- len
, NULL
);
280 * We write the padded len in local tracefiles but the data len
281 * when using a relay. Display the error but continue processing
282 * to try to release the subbuffer.
284 if (relayd_id
!= (uint64_t) -1ULL) {
285 if (read_len
!= len
) {
286 ERR("Error sending to the relay (ret: %zd != len: %lu)",
290 if (read_len
!= padded_len
) {
291 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
292 read_len
, padded_len
);
296 ret
= kernctl_put_subbuf(stream
->wait_fd
);
298 ERR("Snapshot kernctl_put_subbuf");
301 consumed_pos
+= stream
->max_sb_size
;
304 * Only account lost packets located between
305 * succesfully extracted packets (do not account before
306 * and after since they are not visible in the
307 * resulting snapshot).
309 stream
->chan
->lost_packets
+= lost_packet
;
311 before_first_packet
= false;
314 if (relayd_id
== (uint64_t) -1ULL) {
315 if (stream
->out_fd
>= 0) {
316 ret
= close(stream
->out_fd
);
318 PERROR("Kernel consumer snapshot close out_fd");
324 close_relayd_stream(stream
);
325 stream
->net_seq_idx
= (uint64_t) -1ULL;
327 pthread_mutex_unlock(&stream
->lock
);
335 ret
= kernctl_put_subbuf(stream
->wait_fd
);
337 ERR("Snapshot kernctl_put_subbuf error path");
340 pthread_mutex_unlock(&stream
->lock
);
347 * Read the whole metadata available for a snapshot.
349 * Returns 0 on success, < 0 on error
351 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
352 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
354 int ret
, use_relayd
= 0;
356 struct lttng_consumer_channel
*metadata_channel
;
357 struct lttng_consumer_stream
*metadata_stream
;
361 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
366 metadata_channel
= consumer_find_channel(key
);
367 if (!metadata_channel
) {
368 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
373 metadata_stream
= metadata_channel
->metadata_stream
;
374 assert(metadata_stream
);
376 /* Flag once that we have a valid relayd for the stream. */
377 if (relayd_id
!= (uint64_t) -1ULL) {
382 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
387 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
388 metadata_stream
->chan
->tracefile_size
,
389 metadata_stream
->tracefile_count_current
,
390 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
394 metadata_stream
->out_fd
= ret
;
398 health_code_update();
400 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
402 if (ret_read
!= -EAGAIN
) {
403 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
407 /* ret_read is negative at this point so we will exit the loop. */
410 } while (ret_read
>= 0);
413 close_relayd_stream(metadata_stream
);
414 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
416 if (metadata_stream
->out_fd
>= 0) {
417 ret
= close(metadata_stream
->out_fd
);
419 PERROR("Kernel consumer snapshot metadata close out_fd");
421 * Don't go on error here since the snapshot was successful at this
422 * point but somehow the close failed.
425 metadata_stream
->out_fd
= -1;
431 cds_list_del(&metadata_stream
->send_node
);
432 consumer_stream_destroy(metadata_stream
, NULL
);
433 metadata_channel
->metadata_stream
= NULL
;
440 * Receive command from session daemon and process it.
442 * Return 1 on success else a negative value or 0.
444 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
445 int sock
, struct pollfd
*consumer_sockpoll
)
448 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
449 struct lttcomm_consumer_msg msg
;
451 health_code_update();
453 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
454 if (ret
!= sizeof(msg
)) {
456 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
462 health_code_update();
464 /* Deprecated command */
465 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
467 health_code_update();
469 /* relayd needs RCU read-side protection */
472 switch (msg
.cmd_type
) {
473 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
475 /* Session daemon status message are handled in the following call. */
476 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
477 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
478 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
479 msg
.u
.relayd_sock
.relayd_session_id
);
482 case LTTNG_CONSUMER_ADD_CHANNEL
:
484 struct lttng_consumer_channel
*new_channel
;
487 health_code_update();
489 /* First send a status message before receiving the fds. */
490 ret
= consumer_send_status_msg(sock
, ret_code
);
492 /* Somehow, the session daemon is not responding anymore. */
496 health_code_update();
498 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
499 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
500 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
501 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
502 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
503 msg
.u
.channel
.tracefile_size
,
504 msg
.u
.channel
.tracefile_count
, 0,
505 msg
.u
.channel
.monitor
,
506 msg
.u
.channel
.live_timer_interval
,
508 if (new_channel
== NULL
) {
509 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
512 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
513 switch (msg
.u
.channel
.output
) {
514 case LTTNG_EVENT_SPLICE
:
515 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
517 case LTTNG_EVENT_MMAP
:
518 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
521 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
525 /* Translate and save channel type. */
526 switch (msg
.u
.channel
.type
) {
527 case CONSUMER_CHANNEL_TYPE_DATA
:
528 case CONSUMER_CHANNEL_TYPE_METADATA
:
529 new_channel
->type
= msg
.u
.channel
.type
;
536 health_code_update();
538 if (ctx
->on_recv_channel
!= NULL
) {
539 ret_recv
= ctx
->on_recv_channel(new_channel
);
541 ret
= consumer_add_channel(new_channel
, ctx
);
542 } else if (ret_recv
< 0) {
546 ret
= consumer_add_channel(new_channel
, ctx
);
548 if (CONSUMER_CHANNEL_TYPE_DATA
) {
549 consumer_timer_live_start(new_channel
,
550 msg
.u
.channel
.live_timer_interval
);
553 health_code_update();
555 /* If we received an error in add_channel, we need to report it. */
557 ret
= consumer_send_status_msg(sock
, ret
);
566 case LTTNG_CONSUMER_ADD_STREAM
:
569 struct lttng_pipe
*stream_pipe
;
570 struct lttng_consumer_stream
*new_stream
;
571 struct lttng_consumer_channel
*channel
;
575 * Get stream's channel reference. Needed when adding the stream to the
578 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
581 * We could not find the channel. Can happen if cpu hotplug
582 * happens while tearing down.
584 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
585 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
588 health_code_update();
590 /* First send a status message before receiving the fds. */
591 ret
= consumer_send_status_msg(sock
, ret_code
);
593 /* Somehow, the session daemon is not responding anymore. */
597 health_code_update();
599 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
600 /* Channel was not found. */
606 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
612 health_code_update();
614 /* Get stream file descriptor from socket */
615 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
616 if (ret
!= sizeof(fd
)) {
617 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
622 health_code_update();
625 * Send status code to session daemon only if the recv works. If the
626 * above recv() failed, the session daemon is notified through the
627 * error socket and the teardown is eventually done.
629 ret
= consumer_send_status_msg(sock
, ret_code
);
631 /* Somehow, the session daemon is not responding anymore. */
635 health_code_update();
637 new_stream
= consumer_allocate_stream(channel
->key
,
639 LTTNG_CONSUMER_ACTIVE_STREAM
,
649 if (new_stream
== NULL
) {
654 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
660 new_stream
->chan
= channel
;
661 new_stream
->wait_fd
= fd
;
662 switch (channel
->output
) {
663 case CONSUMER_CHANNEL_SPLICE
:
664 new_stream
->output
= LTTNG_EVENT_SPLICE
;
665 ret
= utils_create_pipe(new_stream
->splice_pipe
);
670 case CONSUMER_CHANNEL_MMAP
:
671 new_stream
->output
= LTTNG_EVENT_MMAP
;
674 ERR("Stream output unknown %d", channel
->output
);
679 * We've just assigned the channel to the stream so increment the
680 * refcount right now. We don't need to increment the refcount for
681 * streams in no monitor because we handle manually the cleanup of
682 * those. It is very important to make sure there is NO prior
683 * consumer_del_stream() calls or else the refcount will be unbalanced.
685 if (channel
->monitor
) {
686 uatomic_inc(&new_stream
->chan
->refcount
);
690 * The buffer flush is done on the session daemon side for the kernel
691 * so no need for the stream "hangup_flush_done" variable to be
692 * tracked. This is important for a kernel stream since we don't rely
693 * on the flush state of the stream to read data. It's not the case for
694 * user space tracing.
696 new_stream
->hangup_flush_done
= 0;
698 health_code_update();
700 if (ctx
->on_recv_stream
) {
701 ret
= ctx
->on_recv_stream(new_stream
);
703 consumer_stream_free(new_stream
);
708 health_code_update();
710 if (new_stream
->metadata_flag
) {
711 channel
->metadata_stream
= new_stream
;
714 /* Do not monitor this stream. */
715 if (!channel
->monitor
) {
716 DBG("Kernel consumer add stream %s in no monitor mode with "
717 "relayd id %" PRIu64
, new_stream
->name
,
718 new_stream
->net_seq_idx
);
719 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
723 /* Send stream to relayd if the stream has an ID. */
724 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
725 ret
= consumer_send_relayd_stream(new_stream
,
726 new_stream
->chan
->pathname
);
728 consumer_stream_free(new_stream
);
733 * If adding an extra stream to an already
734 * existing channel (e.g. cpu hotplug), we need
735 * to send the "streams_sent" command to relayd.
737 if (channel
->streams_sent_to_relayd
) {
738 ret
= consumer_send_relayd_streams_sent(
739 new_stream
->net_seq_idx
);
746 /* Get the right pipe where the stream will be sent. */
747 if (new_stream
->metadata_flag
) {
748 ret
= consumer_add_metadata_stream(new_stream
);
750 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
752 consumer_stream_free(new_stream
);
755 stream_pipe
= ctx
->consumer_metadata_pipe
;
757 ret
= consumer_add_data_stream(new_stream
);
759 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
761 consumer_stream_free(new_stream
);
764 stream_pipe
= ctx
->consumer_data_pipe
;
767 /* Vitible to other threads */
768 new_stream
->globally_visible
= 1;
770 health_code_update();
772 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
774 ERR("Consumer write %s stream to pipe %d",
775 new_stream
->metadata_flag
? "metadata" : "data",
776 lttng_pipe_get_writefd(stream_pipe
));
777 if (new_stream
->metadata_flag
) {
778 consumer_del_stream_for_metadata(new_stream
);
780 consumer_del_stream_for_data(new_stream
);
785 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
786 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
789 case LTTNG_CONSUMER_STREAMS_SENT
:
791 struct lttng_consumer_channel
*channel
;
794 * Get stream's channel reference. Needed when adding the stream to the
797 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
800 * We could not find the channel. Can happen if cpu hotplug
801 * happens while tearing down.
803 ERR("Unable to find channel key %" PRIu64
,
804 msg
.u
.sent_streams
.channel_key
);
805 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
808 health_code_update();
811 * Send status code to session daemon.
813 ret
= consumer_send_status_msg(sock
, ret_code
);
814 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
815 /* Somehow, the session daemon is not responding anymore. */
819 health_code_update();
822 * We should not send this message if we don't monitor the
823 * streams in this channel.
825 if (!channel
->monitor
) {
829 health_code_update();
830 /* Send stream to relayd if the stream has an ID. */
831 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
832 ret
= consumer_send_relayd_streams_sent(
833 msg
.u
.sent_streams
.net_seq_idx
);
837 channel
->streams_sent_to_relayd
= true;
841 case LTTNG_CONSUMER_UPDATE_STREAM
:
846 case LTTNG_CONSUMER_DESTROY_RELAYD
:
848 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
849 struct consumer_relayd_sock_pair
*relayd
;
851 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
853 /* Get relayd reference if exists. */
854 relayd
= consumer_find_relayd(index
);
855 if (relayd
== NULL
) {
856 DBG("Unable to find relayd %" PRIu64
, index
);
857 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
861 * Each relayd socket pair has a refcount of stream attached to it
862 * which tells if the relayd is still active or not depending on the
865 * This will set the destroy flag of the relayd object and destroy it
866 * if the refcount reaches zero when called.
868 * The destroy can happen either here or when a stream fd hangs up.
871 consumer_flag_relayd_for_destroy(relayd
);
874 health_code_update();
876 ret
= consumer_send_status_msg(sock
, ret_code
);
878 /* Somehow, the session daemon is not responding anymore. */
884 case LTTNG_CONSUMER_DATA_PENDING
:
887 uint64_t id
= msg
.u
.data_pending
.session_id
;
889 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
891 ret
= consumer_data_pending(id
);
893 health_code_update();
895 /* Send back returned value to session daemon */
896 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
898 PERROR("send data pending ret code");
903 * No need to send back a status message since the data pending
904 * returned value is the response.
908 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
910 if (msg
.u
.snapshot_channel
.metadata
== 1) {
911 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
912 msg
.u
.snapshot_channel
.pathname
,
913 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
915 ERR("Snapshot metadata failed");
916 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
919 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
920 msg
.u
.snapshot_channel
.pathname
,
921 msg
.u
.snapshot_channel
.relayd_id
,
922 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
925 ERR("Snapshot channel failed");
926 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
930 health_code_update();
932 ret
= consumer_send_status_msg(sock
, ret_code
);
934 /* Somehow, the session daemon is not responding anymore. */
939 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
941 uint64_t key
= msg
.u
.destroy_channel
.key
;
942 struct lttng_consumer_channel
*channel
;
944 channel
= consumer_find_channel(key
);
946 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
947 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
950 health_code_update();
952 ret
= consumer_send_status_msg(sock
, ret_code
);
954 /* Somehow, the session daemon is not responding anymore. */
958 health_code_update();
960 /* Stop right now if no channel was found. */
966 * This command should ONLY be issued for channel with streams set in
969 assert(!channel
->monitor
);
972 * The refcount should ALWAYS be 0 in the case of a channel in no
975 assert(!uatomic_sub_return(&channel
->refcount
, 1));
977 consumer_del_channel(channel
);
981 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
984 struct lttng_consumer_channel
*channel
;
985 uint64_t id
= msg
.u
.discarded_events
.session_id
;
986 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
988 DBG("Kernel consumer discarded events command for session id %"
989 PRIu64
", channel key %" PRIu64
, id
, key
);
991 channel
= consumer_find_channel(key
);
993 ERR("Kernel consumer discarded events channel %"
994 PRIu64
" not found", key
);
997 ret
= channel
->discarded_events
;
1000 health_code_update();
1002 /* Send back returned value to session daemon */
1003 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1005 PERROR("send discarded events");
1011 case LTTNG_CONSUMER_LOST_PACKETS
:
1014 struct lttng_consumer_channel
*channel
;
1015 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1016 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1018 DBG("Kernel consumer lost packets command for session id %"
1019 PRIu64
", channel key %" PRIu64
, id
, key
);
1021 channel
= consumer_find_channel(key
);
1023 ERR("Kernel consumer lost packets channel %"
1024 PRIu64
" not found", key
);
1027 ret
= channel
->lost_packets
;
1030 health_code_update();
1032 /* Send back returned value to session daemon */
1033 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1035 PERROR("send lost packets");
1049 * Return 1 to indicate success since the 0 value can be a socket
1050 * shutdown during the recv() or send() call.
1052 health_code_update();
1057 /* This will issue a consumer stop. */
1062 * Populate index values of a kernel stream. Values are set in big endian order.
1064 * Return 0 on success or else a negative value.
1066 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1070 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1072 PERROR("kernctl_get_timestamp_begin");
1075 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1077 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1079 PERROR("kernctl_get_timestamp_end");
1082 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1084 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1086 PERROR("kernctl_get_events_discarded");
1089 index
->events_discarded
= htobe64(index
->events_discarded
);
1091 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1093 PERROR("kernctl_get_content_size");
1096 index
->content_size
= htobe64(index
->content_size
);
1098 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1100 PERROR("kernctl_get_packet_size");
1103 index
->packet_size
= htobe64(index
->packet_size
);
1105 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1107 PERROR("kernctl_get_stream_id");
1110 index
->stream_id
= htobe64(index
->stream_id
);
1112 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1114 if (ret
== -ENOTTY
) {
1115 /* Command not implemented by lttng-modules. */
1116 index
->stream_instance_id
= -1ULL;
1119 PERROR("kernctl_get_instance_id");
1123 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1125 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1127 if (ret
== -ENOTTY
) {
1128 /* Command not implemented by lttng-modules. */
1129 index
->packet_seq_num
= -1ULL;
1132 PERROR("kernctl_get_sequence_number");
1136 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1142 * Sync metadata meaning request them to the session daemon and snapshot to the
1143 * metadata thread can consumer them.
1145 * Metadata stream lock MUST be acquired.
1147 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1148 * is empty or a negative value on error.
1150 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1156 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1158 ERR("Failed to flush kernel stream");
1162 ret
= kernctl_snapshot(metadata
->wait_fd
);
1164 if (ret
!= -EAGAIN
) {
1165 ERR("Sync metadata, taking kernel snapshot failed.");
1168 DBG("Sync metadata, no new kernel metadata");
1169 /* No new metadata, exit. */
1179 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1182 uint64_t seq
, discarded
;
1184 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1186 if (ret
== -ENOTTY
) {
1187 /* Command not implemented by lttng-modules. */
1191 PERROR("kernctl_get_sequence_number");
1197 * Start the sequence when we extract the first packet in case we don't
1198 * start at 0 (for example if a consumer is not connected to the
1199 * session immediately after the beginning).
1201 if (stream
->last_sequence_number
== -1ULL) {
1202 stream
->last_sequence_number
= seq
;
1203 } else if (seq
> stream
->last_sequence_number
) {
1204 stream
->chan
->lost_packets
+= seq
-
1205 stream
->last_sequence_number
- 1;
1207 /* seq <= last_sequence_number */
1208 ERR("Sequence number inconsistent : prev = %" PRIu64
1209 ", current = %" PRIu64
,
1210 stream
->last_sequence_number
, seq
);
1214 stream
->last_sequence_number
= seq
;
1216 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1218 PERROR("kernctl_get_events_discarded");
1221 if (discarded
< stream
->last_discarded_events
) {
1223 * Overflow has occurred. We assume only one wrap-around
1226 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1227 stream
->last_discarded_events
+ discarded
;
1229 stream
->chan
->discarded_events
+= discarded
-
1230 stream
->last_discarded_events
;
1232 stream
->last_discarded_events
= discarded
;
1240 * Check if the local version of the metadata stream matches with the version
1241 * of the metadata stream in the kernel. If it was updated, set the reset flag
1245 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1248 uint64_t cur_version
;
1250 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1252 if (ret
== -ENOTTY
) {
1254 * LTTng-modules does not implement this
1260 ERR("Failed to get the metadata version");
1264 if (stream
->metadata_version
== cur_version
) {
1269 DBG("New metadata version detected");
1270 stream
->metadata_version
= cur_version
;
1271 stream
->reset_metadata_flag
= 1;
1279 * Consume data on a file descriptor and write it on a trace file.
1281 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1282 struct lttng_consumer_local_data
*ctx
)
1284 unsigned long len
, subbuf_size
, padding
;
1285 int err
, write_index
= 1;
1287 int infd
= stream
->wait_fd
;
1288 struct ctf_packet_index index
;
1290 DBG("In read_subbuffer (infd : %d)", infd
);
1292 /* Get the next subbuffer */
1293 err
= kernctl_get_next_subbuf(infd
);
1296 * This is a debug message even for single-threaded consumer,
1297 * because poll() have more relaxed criterions than get subbuf,
1298 * so get_subbuf may fail for short race windows where poll()
1299 * would issue wakeups.
1301 DBG("Reserving sub buffer failed (everything is normal, "
1302 "it is due to concurrency)");
1307 /* Get the full subbuffer size including padding */
1308 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1310 PERROR("Getting sub-buffer len failed.");
1311 err
= kernctl_put_subbuf(infd
);
1313 if (err
== -EFAULT
) {
1314 PERROR("Error in unreserving sub buffer\n");
1315 } else if (err
== -EIO
) {
1316 /* Should never happen with newer LTTng versions */
1317 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1326 if (!stream
->metadata_flag
) {
1327 ret
= get_index_values(&index
, infd
);
1329 err
= kernctl_put_subbuf(infd
);
1331 if (err
== -EFAULT
) {
1332 PERROR("Error in unreserving sub buffer\n");
1333 } else if (err
== -EIO
) {
1334 /* Should never happen with newer LTTng versions */
1335 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1342 ret
= update_stream_stats(stream
);
1344 err
= kernctl_put_subbuf(infd
);
1346 if (err
== -EFAULT
) {
1347 PERROR("Error in unreserving sub buffer\n");
1348 } else if (err
== -EIO
) {
1349 /* Should never happen with newer LTTng versions */
1350 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1359 ret
= metadata_stream_check_version(infd
, stream
);
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 switch (stream
->chan
->output
) {
1377 case CONSUMER_CHANNEL_SPLICE
:
1379 * XXX: The lttng-modules splice "actor" does not handle copying
1380 * partial pages hence only using the subbuffer size without the
1381 * padding makes the splice fail.
1386 /* splice the subbuffer to the tracefile */
1387 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1390 * XXX: Splice does not support network streaming so the return value
1391 * is simply checked against subbuf_size and not like the mmap() op.
1393 if (ret
!= subbuf_size
) {
1395 * display the error but continue processing to try
1396 * to release the subbuffer
1398 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1403 case CONSUMER_CHANNEL_MMAP
:
1404 /* Get subbuffer size without padding */
1405 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1407 PERROR("Getting sub-buffer len failed.");
1408 err
= kernctl_put_subbuf(infd
);
1410 if (err
== -EFAULT
) {
1411 PERROR("Error in unreserving sub buffer\n");
1412 } else if (err
== -EIO
) {
1413 /* Should never happen with newer LTTng versions */
1414 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1423 /* Make sure the tracer is not gone mad on us! */
1424 assert(len
>= subbuf_size
);
1426 padding
= len
- subbuf_size
;
1428 /* write the subbuffer to the tracefile */
1429 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1432 * The mmap operation should write subbuf_size amount of data when
1433 * network streaming or the full padding (len) size when we are _not_
1436 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1437 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1439 * Display the error but continue processing to try to release the
1440 * subbuffer. This is a DBG statement since this is possible to
1441 * happen without being a critical error.
1443 DBG("Error writing to tracefile "
1444 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1445 ret
, len
, subbuf_size
);
1450 ERR("Unknown output method");
1454 err
= kernctl_put_next_subbuf(infd
);
1456 if (err
== -EFAULT
) {
1457 PERROR("Error in unreserving sub buffer\n");
1458 } else if (err
== -EIO
) {
1459 /* Should never happen with newer LTTng versions */
1460 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1466 /* Write index if needed. */
1471 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1473 * In live, block until all the metadata is sent.
1475 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1476 assert(!stream
->missed_metadata_flush
);
1477 stream
->waiting_on_metadata
= true;
1478 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1480 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1482 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1483 stream
->waiting_on_metadata
= false;
1484 if (stream
->missed_metadata_flush
) {
1485 stream
->missed_metadata_flush
= false;
1486 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1487 (void) consumer_flush_kernel_index(stream
);
1489 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1496 err
= consumer_stream_write_index(stream
, &index
);
1505 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1512 * Don't create anything if this is set for streaming or should not be
1515 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1516 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1517 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1518 stream
->uid
, stream
->gid
, NULL
);
1522 stream
->out_fd
= ret
;
1523 stream
->tracefile_size_current
= 0;
1525 if (!stream
->metadata_flag
) {
1526 struct lttng_index_file
*index_file
;
1528 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1529 stream
->name
, stream
->uid
, stream
->gid
,
1530 stream
->chan
->tracefile_size
,
1531 stream
->tracefile_count_current
,
1532 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1536 stream
->index_file
= index_file
;
1540 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1541 /* get the len of the mmap region */
1542 unsigned long mmap_len
;
1544 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1546 PERROR("kernctl_get_mmap_len");
1547 goto error_close_fd
;
1549 stream
->mmap_len
= (size_t) mmap_len
;
1551 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1552 MAP_PRIVATE
, stream
->wait_fd
, 0);
1553 if (stream
->mmap_base
== MAP_FAILED
) {
1554 PERROR("Error mmaping");
1556 goto error_close_fd
;
1560 /* we return 0 to let the library handle the FD internally */
1564 if (stream
->out_fd
>= 0) {
1567 err
= close(stream
->out_fd
);
1569 stream
->out_fd
= -1;
1576 * Check if data is still being extracted from the buffers for a specific
1577 * stream. Consumer data lock MUST be acquired before calling this function
1578 * and the stream lock.
1580 * Return 1 if the traced data are still getting read else 0 meaning that the
1581 * data is available for trace viewer reading.
1583 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1589 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1594 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1596 /* There is still data so let's put back this subbuffer. */
1597 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1599 ret
= 1; /* Data is pending */
1603 /* Data is NOT pending and ready to be read. */