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.");
72 * Get the produced position
74 * Returns 0 on success, < 0 on error
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
80 int infd
= stream
->wait_fd
;
82 ret
= kernctl_snapshot_get_produced(infd
, pos
);
84 PERROR("kernctl_snapshot_get_produced");
92 * Get the consumerd position
94 * Returns 0 on success, < 0 on error
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
100 int infd
= stream
->wait_fd
;
102 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
104 PERROR("kernctl_snapshot_get_consumed");
112 * Take a snapshot of all the stream of a channel
114 * Returns 0 on success, < 0 on error
116 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
117 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
118 struct lttng_consumer_local_data
*ctx
)
121 struct lttng_consumer_channel
*channel
;
122 struct lttng_consumer_stream
*stream
;
124 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
128 channel
= consumer_find_channel(key
);
130 ERR("No channel found for key %" PRIu64
, key
);
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
137 ERR("Unsupported output %d", channel
->output
);
142 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
143 unsigned long consumed_pos
, produced_pos
;
145 health_code_update();
148 * Lock stream because we are about to change its state.
150 pthread_mutex_lock(&stream
->lock
);
153 * Assign the received relayd ID so we can use it for streaming. The streams
154 * are not visible to anyone so this is OK to change it.
156 stream
->net_seq_idx
= relayd_id
;
157 channel
->relayd_id
= relayd_id
;
158 if (relayd_id
!= (uint64_t) -1ULL) {
159 ret
= consumer_send_relayd_stream(stream
, path
);
161 ERR("sending stream to relayd");
165 ret
= utils_create_stream_file(path
, stream
->name
,
166 stream
->chan
->tracefile_size
,
167 stream
->tracefile_count_current
,
168 stream
->uid
, stream
->gid
, NULL
);
170 ERR("utils_create_stream_file");
174 stream
->out_fd
= ret
;
175 stream
->tracefile_size_current
= 0;
177 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
178 path
, stream
->name
, stream
->key
);
180 if (relayd_id
!= -1ULL) {
181 ret
= consumer_send_relayd_streams_sent(relayd_id
);
183 ERR("sending streams sent to relayd");
186 channel
->streams_sent_to_relayd
= true;
189 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
192 * Doing a buffer flush which does not take into
193 * account empty packets. This is not perfect
194 * for stream intersection, but required as a
195 * fall-back when "flush_empty" is not
196 * implemented by lttng-modules.
198 ret
= kernctl_buffer_flush(stream
->wait_fd
);
200 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");
233 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
234 produced_pos
, nb_packets_per_stream
,
235 stream
->max_sb_size
);
237 while (consumed_pos
< produced_pos
) {
239 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 (errno
!= EAGAIN
) {
248 PERROR("kernctl_get_subbuf snapshot");
252 DBG("Kernel consumer get subbuf failed. Skipping it.");
253 consumed_pos
+= stream
->max_sb_size
;
254 stream
->chan
->lost_packets
++;
258 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
260 ERR("Snapshot kernctl_get_subbuf_size");
262 goto error_put_subbuf
;
265 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
267 ERR("Snapshot kernctl_get_padded_subbuf_size");
269 goto error_put_subbuf
;
272 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
273 padded_len
- len
, NULL
);
275 * We write the padded len in local tracefiles but the data len
276 * when using a relay. Display the error but continue processing
277 * to try to release the subbuffer.
279 if (relayd_id
!= (uint64_t) -1ULL) {
280 if (read_len
!= len
) {
281 ERR("Error sending to the relay (ret: %zd != len: %lu)",
285 if (read_len
!= padded_len
) {
286 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
287 read_len
, padded_len
);
291 ret
= kernctl_put_subbuf(stream
->wait_fd
);
293 ERR("Snapshot kernctl_put_subbuf");
297 consumed_pos
+= stream
->max_sb_size
;
300 if (relayd_id
== (uint64_t) -1ULL) {
301 if (stream
->out_fd
>= 0) {
302 ret
= close(stream
->out_fd
);
304 PERROR("Kernel consumer snapshot close out_fd");
310 close_relayd_stream(stream
);
311 stream
->net_seq_idx
= (uint64_t) -1ULL;
313 pthread_mutex_unlock(&stream
->lock
);
321 ret
= kernctl_put_subbuf(stream
->wait_fd
);
324 ERR("Snapshot kernctl_put_subbuf error path");
327 pthread_mutex_unlock(&stream
->lock
);
334 * Read the whole metadata available for a snapshot.
336 * Returns 0 on success, < 0 on error
338 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
339 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
341 int ret
, use_relayd
= 0;
343 struct lttng_consumer_channel
*metadata_channel
;
344 struct lttng_consumer_stream
*metadata_stream
;
348 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
353 metadata_channel
= consumer_find_channel(key
);
354 if (!metadata_channel
) {
355 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
360 metadata_stream
= metadata_channel
->metadata_stream
;
361 assert(metadata_stream
);
363 /* Flag once that we have a valid relayd for the stream. */
364 if (relayd_id
!= (uint64_t) -1ULL) {
369 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
374 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
375 metadata_stream
->chan
->tracefile_size
,
376 metadata_stream
->tracefile_count_current
,
377 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
381 metadata_stream
->out_fd
= ret
;
385 health_code_update();
387 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
389 if (ret_read
!= -EAGAIN
) {
390 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
394 /* ret_read is negative at this point so we will exit the loop. */
397 } while (ret_read
>= 0);
400 close_relayd_stream(metadata_stream
);
401 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
403 if (metadata_stream
->out_fd
>= 0) {
404 ret
= close(metadata_stream
->out_fd
);
406 PERROR("Kernel consumer snapshot metadata close out_fd");
408 * Don't go on error here since the snapshot was successful at this
409 * point but somehow the close failed.
412 metadata_stream
->out_fd
= -1;
418 cds_list_del(&metadata_stream
->send_node
);
419 consumer_stream_destroy(metadata_stream
, NULL
);
420 metadata_channel
->metadata_stream
= NULL
;
427 * Receive command from session daemon and process it.
429 * Return 1 on success else a negative value or 0.
431 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
432 int sock
, struct pollfd
*consumer_sockpoll
)
435 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
436 struct lttcomm_consumer_msg msg
;
438 health_code_update();
440 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
441 if (ret
!= sizeof(msg
)) {
443 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
449 health_code_update();
451 /* Deprecated command */
452 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
454 health_code_update();
456 /* relayd needs RCU read-side protection */
459 switch (msg
.cmd_type
) {
460 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
462 /* Session daemon status message are handled in the following call. */
463 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
464 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
465 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
466 msg
.u
.relayd_sock
.relayd_session_id
);
469 case LTTNG_CONSUMER_ADD_CHANNEL
:
471 struct lttng_consumer_channel
*new_channel
;
474 health_code_update();
476 /* First send a status message before receiving the fds. */
477 ret
= consumer_send_status_msg(sock
, ret_code
);
479 /* Somehow, the session daemon is not responding anymore. */
483 health_code_update();
485 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
486 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
487 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
488 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
489 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
490 msg
.u
.channel
.tracefile_size
,
491 msg
.u
.channel
.tracefile_count
, 0,
492 msg
.u
.channel
.monitor
,
493 msg
.u
.channel
.live_timer_interval
,
495 if (new_channel
== NULL
) {
496 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
499 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
500 switch (msg
.u
.channel
.output
) {
501 case LTTNG_EVENT_SPLICE
:
502 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
504 case LTTNG_EVENT_MMAP
:
505 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
508 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
512 /* Translate and save channel type. */
513 switch (msg
.u
.channel
.type
) {
514 case CONSUMER_CHANNEL_TYPE_DATA
:
515 case CONSUMER_CHANNEL_TYPE_METADATA
:
516 new_channel
->type
= msg
.u
.channel
.type
;
523 health_code_update();
525 if (ctx
->on_recv_channel
!= NULL
) {
526 ret_recv
= ctx
->on_recv_channel(new_channel
);
528 ret
= consumer_add_channel(new_channel
, ctx
);
529 } else if (ret_recv
< 0) {
533 ret
= consumer_add_channel(new_channel
, ctx
);
535 if (CONSUMER_CHANNEL_TYPE_DATA
) {
536 consumer_timer_live_start(new_channel
,
537 msg
.u
.channel
.live_timer_interval
);
540 health_code_update();
542 /* If we received an error in add_channel, we need to report it. */
544 ret
= consumer_send_status_msg(sock
, ret
);
553 case LTTNG_CONSUMER_ADD_STREAM
:
556 struct lttng_pipe
*stream_pipe
;
557 struct lttng_consumer_stream
*new_stream
;
558 struct lttng_consumer_channel
*channel
;
562 * Get stream's channel reference. Needed when adding the stream to the
565 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
568 * We could not find the channel. Can happen if cpu hotplug
569 * happens while tearing down.
571 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
572 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
575 health_code_update();
577 /* First send a status message before receiving the fds. */
578 ret
= consumer_send_status_msg(sock
, ret_code
);
580 /* Somehow, the session daemon is not responding anymore. */
584 health_code_update();
586 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
587 /* Channel was not found. */
593 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
599 health_code_update();
601 /* Get stream file descriptor from socket */
602 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
603 if (ret
!= sizeof(fd
)) {
604 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
609 health_code_update();
612 * Send status code to session daemon only if the recv works. If the
613 * above recv() failed, the session daemon is notified through the
614 * error socket and the teardown is eventually done.
616 ret
= consumer_send_status_msg(sock
, ret_code
);
618 /* Somehow, the session daemon is not responding anymore. */
622 health_code_update();
624 new_stream
= consumer_allocate_stream(channel
->key
,
626 LTTNG_CONSUMER_ACTIVE_STREAM
,
636 if (new_stream
== NULL
) {
641 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
647 new_stream
->chan
= channel
;
648 new_stream
->wait_fd
= fd
;
649 switch (channel
->output
) {
650 case CONSUMER_CHANNEL_SPLICE
:
651 new_stream
->output
= LTTNG_EVENT_SPLICE
;
652 ret
= utils_create_pipe(new_stream
->splice_pipe
);
657 case CONSUMER_CHANNEL_MMAP
:
658 new_stream
->output
= LTTNG_EVENT_MMAP
;
661 ERR("Stream output unknown %d", channel
->output
);
666 * We've just assigned the channel to the stream so increment the
667 * refcount right now. We don't need to increment the refcount for
668 * streams in no monitor because we handle manually the cleanup of
669 * those. It is very important to make sure there is NO prior
670 * consumer_del_stream() calls or else the refcount will be unbalanced.
672 if (channel
->monitor
) {
673 uatomic_inc(&new_stream
->chan
->refcount
);
677 * The buffer flush is done on the session daemon side for the kernel
678 * so no need for the stream "hangup_flush_done" variable to be
679 * tracked. This is important for a kernel stream since we don't rely
680 * on the flush state of the stream to read data. It's not the case for
681 * user space tracing.
683 new_stream
->hangup_flush_done
= 0;
685 health_code_update();
687 if (ctx
->on_recv_stream
) {
688 ret
= ctx
->on_recv_stream(new_stream
);
690 consumer_stream_free(new_stream
);
695 health_code_update();
697 if (new_stream
->metadata_flag
) {
698 channel
->metadata_stream
= new_stream
;
701 /* Do not monitor this stream. */
702 if (!channel
->monitor
) {
703 DBG("Kernel consumer add stream %s in no monitor mode with "
704 "relayd id %" PRIu64
, new_stream
->name
,
705 new_stream
->net_seq_idx
);
706 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
710 /* Send stream to relayd if the stream has an ID. */
711 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
712 ret
= consumer_send_relayd_stream(new_stream
,
713 new_stream
->chan
->pathname
);
715 consumer_stream_free(new_stream
);
720 * If adding an extra stream to an already
721 * existing channel (e.g. cpu hotplug), we need
722 * to send the "streams_sent" command to relayd.
724 if (channel
->streams_sent_to_relayd
) {
725 ret
= consumer_send_relayd_streams_sent(
726 new_stream
->net_seq_idx
);
733 /* Get the right pipe where the stream will be sent. */
734 if (new_stream
->metadata_flag
) {
735 ret
= consumer_add_metadata_stream(new_stream
);
737 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
739 consumer_stream_free(new_stream
);
742 stream_pipe
= ctx
->consumer_metadata_pipe
;
744 ret
= consumer_add_data_stream(new_stream
);
746 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
748 consumer_stream_free(new_stream
);
751 stream_pipe
= ctx
->consumer_data_pipe
;
754 /* Vitible to other threads */
755 new_stream
->globally_visible
= 1;
757 health_code_update();
759 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
761 ERR("Consumer write %s stream to pipe %d",
762 new_stream
->metadata_flag
? "metadata" : "data",
763 lttng_pipe_get_writefd(stream_pipe
));
764 if (new_stream
->metadata_flag
) {
765 consumer_del_stream_for_metadata(new_stream
);
767 consumer_del_stream_for_data(new_stream
);
772 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
773 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
776 case LTTNG_CONSUMER_STREAMS_SENT
:
778 struct lttng_consumer_channel
*channel
;
781 * Get stream's channel reference. Needed when adding the stream to the
784 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
787 * We could not find the channel. Can happen if cpu hotplug
788 * happens while tearing down.
790 ERR("Unable to find channel key %" PRIu64
,
791 msg
.u
.sent_streams
.channel_key
);
792 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
795 health_code_update();
798 * Send status code to session daemon.
800 ret
= consumer_send_status_msg(sock
, ret_code
);
801 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
802 /* Somehow, the session daemon is not responding anymore. */
806 health_code_update();
809 * We should not send this message if we don't monitor the
810 * streams in this channel.
812 if (!channel
->monitor
) {
816 health_code_update();
817 /* Send stream to relayd if the stream has an ID. */
818 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
819 ret
= consumer_send_relayd_streams_sent(
820 msg
.u
.sent_streams
.net_seq_idx
);
824 channel
->streams_sent_to_relayd
= true;
828 case LTTNG_CONSUMER_UPDATE_STREAM
:
833 case LTTNG_CONSUMER_DESTROY_RELAYD
:
835 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
836 struct consumer_relayd_sock_pair
*relayd
;
838 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
840 /* Get relayd reference if exists. */
841 relayd
= consumer_find_relayd(index
);
842 if (relayd
== NULL
) {
843 DBG("Unable to find relayd %" PRIu64
, index
);
844 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
848 * Each relayd socket pair has a refcount of stream attached to it
849 * which tells if the relayd is still active or not depending on the
852 * This will set the destroy flag of the relayd object and destroy it
853 * if the refcount reaches zero when called.
855 * The destroy can happen either here or when a stream fd hangs up.
858 consumer_flag_relayd_for_destroy(relayd
);
861 health_code_update();
863 ret
= consumer_send_status_msg(sock
, ret_code
);
865 /* Somehow, the session daemon is not responding anymore. */
871 case LTTNG_CONSUMER_DATA_PENDING
:
874 uint64_t id
= msg
.u
.data_pending
.session_id
;
876 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
878 ret
= consumer_data_pending(id
);
880 health_code_update();
882 /* Send back returned value to session daemon */
883 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
885 PERROR("send data pending ret code");
890 * No need to send back a status message since the data pending
891 * returned value is the response.
895 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
897 if (msg
.u
.snapshot_channel
.metadata
== 1) {
898 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
899 msg
.u
.snapshot_channel
.pathname
,
900 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
902 ERR("Snapshot metadata failed");
903 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
906 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
907 msg
.u
.snapshot_channel
.pathname
,
908 msg
.u
.snapshot_channel
.relayd_id
,
909 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
912 ERR("Snapshot channel failed");
913 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
917 health_code_update();
919 ret
= consumer_send_status_msg(sock
, ret_code
);
921 /* Somehow, the session daemon is not responding anymore. */
926 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
928 uint64_t key
= msg
.u
.destroy_channel
.key
;
929 struct lttng_consumer_channel
*channel
;
931 channel
= consumer_find_channel(key
);
933 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
934 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
937 health_code_update();
939 ret
= consumer_send_status_msg(sock
, ret_code
);
941 /* Somehow, the session daemon is not responding anymore. */
945 health_code_update();
947 /* Stop right now if no channel was found. */
953 * This command should ONLY be issued for channel with streams set in
956 assert(!channel
->monitor
);
959 * The refcount should ALWAYS be 0 in the case of a channel in no
962 assert(!uatomic_sub_return(&channel
->refcount
, 1));
964 consumer_del_channel(channel
);
968 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
971 struct lttng_consumer_channel
*channel
;
972 uint64_t id
= msg
.u
.discarded_events
.session_id
;
973 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
975 DBG("Kernel consumer discarded events command for session id %"
976 PRIu64
", channel key %" PRIu64
, id
, key
);
978 channel
= consumer_find_channel(key
);
980 ERR("Kernel consumer discarded events channel %"
981 PRIu64
" not found", key
);
984 ret
= channel
->discarded_events
;
987 health_code_update();
989 /* Send back returned value to session daemon */
990 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
992 PERROR("send discarded events");
998 case LTTNG_CONSUMER_LOST_PACKETS
:
1001 struct lttng_consumer_channel
*channel
;
1002 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1003 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1005 DBG("Kernel consumer lost packets command for session id %"
1006 PRIu64
", channel key %" PRIu64
, id
, key
);
1008 channel
= consumer_find_channel(key
);
1010 ERR("Kernel consumer lost packets channel %"
1011 PRIu64
" not found", key
);
1014 ret
= channel
->lost_packets
;
1017 health_code_update();
1019 /* Send back returned value to session daemon */
1020 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1022 PERROR("send lost packets");
1036 * Return 1 to indicate success since the 0 value can be a socket
1037 * shutdown during the recv() or send() call.
1039 health_code_update();
1044 /* This will issue a consumer stop. */
1049 * Populate index values of a kernel stream. Values are set in big endian order.
1051 * Return 0 on success or else a negative value.
1053 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1057 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1059 PERROR("kernctl_get_timestamp_begin");
1062 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1064 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1066 PERROR("kernctl_get_timestamp_end");
1069 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1071 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1073 PERROR("kernctl_get_events_discarded");
1076 index
->events_discarded
= htobe64(index
->events_discarded
);
1078 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1080 PERROR("kernctl_get_content_size");
1083 index
->content_size
= htobe64(index
->content_size
);
1085 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1087 PERROR("kernctl_get_packet_size");
1090 index
->packet_size
= htobe64(index
->packet_size
);
1092 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1094 PERROR("kernctl_get_stream_id");
1097 index
->stream_id
= htobe64(index
->stream_id
);
1099 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1101 if (ret
== -ENOTTY
) {
1102 /* Command not implemented by lttng-modules. */
1103 index
->stream_instance_id
= -1ULL;
1106 PERROR("kernctl_get_instance_id");
1110 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1112 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1114 if (ret
== -ENOTTY
) {
1115 /* Command not implemented by lttng-modules. */
1116 index
->packet_seq_num
= -1ULL;
1119 PERROR("kernctl_get_sequence_number");
1123 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1129 * Sync metadata meaning request them to the session daemon and snapshot to the
1130 * metadata thread can consumer them.
1132 * Metadata stream lock MUST be acquired.
1134 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1135 * is empty or a negative value on error.
1137 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1143 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1145 ERR("Failed to flush kernel stream");
1149 ret
= kernctl_snapshot(metadata
->wait_fd
);
1151 if (errno
!= EAGAIN
) {
1152 ERR("Sync metadata, taking kernel snapshot failed.");
1155 DBG("Sync metadata, no new kernel metadata");
1156 /* No new metadata, exit. */
1166 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1169 uint64_t seq
, discarded
;
1171 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1173 if (ret
== -ENOTTY
) {
1174 /* Command not implemented by lttng-modules. */
1178 PERROR("kernctl_get_sequence_number");
1184 * Start the sequence when we extract the first packet in case we don't
1185 * start at 0 (for example if a consumer is not connected to the
1186 * session immediately after the beginning).
1188 if (stream
->last_sequence_number
== -1ULL) {
1189 stream
->last_sequence_number
= seq
;
1190 } else if (seq
> stream
->last_sequence_number
) {
1191 stream
->chan
->lost_packets
+= seq
-
1192 stream
->last_sequence_number
- 1;
1194 /* seq <= last_sequence_number */
1195 ERR("Sequence number inconsistent : prev = %" PRIu64
1196 ", current = %" PRIu64
,
1197 stream
->last_sequence_number
, seq
);
1201 stream
->last_sequence_number
= seq
;
1203 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1205 PERROR("kernctl_get_events_discarded");
1208 if (discarded
< stream
->last_discarded_events
) {
1210 * Overflow has occurred. We assume only one wrap-around
1213 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1214 stream
->last_discarded_events
+ discarded
;
1216 stream
->chan
->discarded_events
+= discarded
-
1217 stream
->last_discarded_events
;
1219 stream
->last_discarded_events
= discarded
;
1227 * Check if the local version of the metadata stream matches with the version
1228 * of the metadata stream in the kernel. If it was updated, set the reset flag
1232 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1235 uint64_t cur_version
;
1237 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1239 if (ret
== -ENOTTY
) {
1241 * LTTng-modules does not implement this
1247 ERR("Failed to get the metadata version");
1251 if (stream
->metadata_version
== cur_version
) {
1256 DBG("New metadata version detected");
1257 stream
->metadata_version
= cur_version
;
1258 stream
->reset_metadata_flag
= 1;
1266 * Consume data on a file descriptor and write it on a trace file.
1268 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1269 struct lttng_consumer_local_data
*ctx
)
1271 unsigned long len
, subbuf_size
, padding
;
1272 int err
, write_index
= 1;
1274 int infd
= stream
->wait_fd
;
1275 struct ctf_packet_index index
;
1277 DBG("In read_subbuffer (infd : %d)", infd
);
1279 /* Get the next subbuffer */
1280 err
= kernctl_get_next_subbuf(infd
);
1283 * This is a debug message even for single-threaded consumer,
1284 * because poll() have more relaxed criterions than get subbuf,
1285 * so get_subbuf may fail for short race windows where poll()
1286 * would issue wakeups.
1288 DBG("Reserving sub buffer failed (everything is normal, "
1289 "it is due to concurrency)");
1294 /* Get the full subbuffer size including padding */
1295 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1297 PERROR("Getting sub-buffer len failed.");
1298 err
= kernctl_put_subbuf(infd
);
1300 if (errno
== EFAULT
) {
1301 PERROR("Error in unreserving sub buffer\n");
1302 } else if (errno
== EIO
) {
1303 /* Should never happen with newer LTTng versions */
1304 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1313 if (!stream
->metadata_flag
) {
1314 ret
= get_index_values(&index
, infd
);
1316 err
= kernctl_put_subbuf(infd
);
1318 if (errno
== EFAULT
) {
1319 PERROR("Error in unreserving sub buffer\n");
1320 } else if (errno
== EIO
) {
1321 /* Should never happen with newer LTTng versions */
1322 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1329 ret
= update_stream_stats(stream
);
1331 err
= kernctl_put_subbuf(infd
);
1333 if (err
== -EFAULT
) {
1334 PERROR("Error in unreserving sub buffer\n");
1335 } else if (err
== -EIO
) {
1336 /* Should never happen with newer LTTng versions */
1337 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1346 ret
= metadata_stream_check_version(infd
, stream
);
1348 err
= kernctl_put_subbuf(infd
);
1350 if (err
== -EFAULT
) {
1351 PERROR("Error in unreserving sub buffer\n");
1352 } else if (err
== -EIO
) {
1353 /* Should never happen with newer LTTng versions */
1354 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1363 switch (stream
->chan
->output
) {
1364 case CONSUMER_CHANNEL_SPLICE
:
1366 * XXX: The lttng-modules splice "actor" does not handle copying
1367 * partial pages hence only using the subbuffer size without the
1368 * padding makes the splice fail.
1373 /* splice the subbuffer to the tracefile */
1374 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1377 * XXX: Splice does not support network streaming so the return value
1378 * is simply checked against subbuf_size and not like the mmap() op.
1380 if (ret
!= subbuf_size
) {
1382 * display the error but continue processing to try
1383 * to release the subbuffer
1385 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1390 case CONSUMER_CHANNEL_MMAP
:
1391 /* Get subbuffer size without padding */
1392 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1394 PERROR("Getting sub-buffer len failed.");
1395 err
= kernctl_put_subbuf(infd
);
1397 if (errno
== EFAULT
) {
1398 PERROR("Error in unreserving sub buffer\n");
1399 } else if (errno
== EIO
) {
1400 /* Should never happen with newer LTTng versions */
1401 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1410 /* Make sure the tracer is not gone mad on us! */
1411 assert(len
>= subbuf_size
);
1413 padding
= len
- subbuf_size
;
1415 /* write the subbuffer to the tracefile */
1416 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1419 * The mmap operation should write subbuf_size amount of data when
1420 * network streaming or the full padding (len) size when we are _not_
1423 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1424 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1426 * Display the error but continue processing to try to release the
1427 * subbuffer. This is a DBG statement since this is possible to
1428 * happen without being a critical error.
1430 DBG("Error writing to tracefile "
1431 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1432 ret
, len
, subbuf_size
);
1437 ERR("Unknown output method");
1441 err
= kernctl_put_next_subbuf(infd
);
1443 if (errno
== EFAULT
) {
1444 PERROR("Error in unreserving sub buffer\n");
1445 } else if (errno
== EIO
) {
1446 /* Should never happen with newer LTTng versions */
1447 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1453 /* Write index if needed. */
1458 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1460 * In live, block until all the metadata is sent.
1462 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1463 assert(!stream
->missed_metadata_flush
);
1464 stream
->waiting_on_metadata
= true;
1465 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1467 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1469 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1470 stream
->waiting_on_metadata
= false;
1471 if (stream
->missed_metadata_flush
) {
1472 stream
->missed_metadata_flush
= false;
1473 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1474 (void) consumer_flush_kernel_index(stream
);
1476 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1483 err
= consumer_stream_write_index(stream
, &index
);
1492 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1499 * Don't create anything if this is set for streaming or should not be
1502 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1503 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1504 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1505 stream
->uid
, stream
->gid
, NULL
);
1509 stream
->out_fd
= ret
;
1510 stream
->tracefile_size_current
= 0;
1512 if (!stream
->metadata_flag
) {
1513 struct lttng_index_file
*index_file
;
1515 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1516 stream
->name
, stream
->uid
, stream
->gid
,
1517 stream
->chan
->tracefile_size
,
1518 stream
->tracefile_count_current
,
1519 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1523 stream
->index_file
= index_file
;
1527 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1528 /* get the len of the mmap region */
1529 unsigned long mmap_len
;
1531 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1533 PERROR("kernctl_get_mmap_len");
1535 goto error_close_fd
;
1537 stream
->mmap_len
= (size_t) mmap_len
;
1539 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1540 MAP_PRIVATE
, stream
->wait_fd
, 0);
1541 if (stream
->mmap_base
== MAP_FAILED
) {
1542 PERROR("Error mmaping");
1544 goto error_close_fd
;
1548 /* we return 0 to let the library handle the FD internally */
1552 if (stream
->out_fd
>= 0) {
1555 err
= close(stream
->out_fd
);
1557 stream
->out_fd
= -1;
1564 * Check if data is still being extracted from the buffers for a specific
1565 * stream. Consumer data lock MUST be acquired before calling this function
1566 * and the stream lock.
1568 * Return 1 if the traced data are still getting read else 0 meaning that the
1569 * data is available for trace viewer reading.
1571 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1577 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1582 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1584 /* There is still data so let's put back this subbuffer. */
1585 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1587 ret
= 1; /* Data is pending */
1591 /* Data is NOT pending and ready to be read. */