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 * -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 * Get the produced position
77 * Returns 0 on success, < 0 on error
79 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
83 int infd
= stream
->wait_fd
;
85 ret
= kernctl_snapshot_get_produced(infd
, pos
);
87 PERROR("kernctl_snapshot_get_produced");
94 * Get the consumerd position
96 * Returns 0 on success, < 0 on error
98 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
102 int infd
= stream
->wait_fd
;
104 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
106 PERROR("kernctl_snapshot_get_consumed");
113 * Take a snapshot of all the stream of a channel
115 * Returns 0 on success, < 0 on error
117 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
118 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
119 struct lttng_consumer_local_data
*ctx
)
122 struct lttng_consumer_channel
*channel
;
123 struct lttng_consumer_stream
*stream
;
125 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
129 channel
= consumer_find_channel(key
);
131 ERR("No channel found for key %" PRIu64
, key
);
136 /* Splice is not supported yet for channel snapshot. */
137 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
138 ERR("Unsupported output %d", channel
->output
);
143 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
144 unsigned long consumed_pos
, produced_pos
;
146 health_code_update();
149 * Lock stream because we are about to change its state.
151 pthread_mutex_lock(&stream
->lock
);
154 * Assign the received relayd ID so we can use it for streaming. The streams
155 * are not visible to anyone so this is OK to change it.
157 stream
->net_seq_idx
= relayd_id
;
158 channel
->relayd_id
= relayd_id
;
159 if (relayd_id
!= (uint64_t) -1ULL) {
160 ret
= consumer_send_relayd_stream(stream
, path
);
162 ERR("sending stream to relayd");
166 ret
= utils_create_stream_file(path
, stream
->name
,
167 stream
->chan
->tracefile_size
,
168 stream
->tracefile_count_current
,
169 stream
->uid
, stream
->gid
, NULL
);
171 ERR("utils_create_stream_file");
175 stream
->out_fd
= ret
;
176 stream
->tracefile_size_current
= 0;
178 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
179 path
, stream
->name
, stream
->key
);
181 if (relayd_id
!= -1ULL) {
182 ret
= consumer_send_relayd_streams_sent(relayd_id
);
184 ERR("sending streams sent to relayd");
187 channel
->streams_sent_to_relayd
= true;
190 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
193 * Doing a buffer flush which does not take into
194 * account empty packets. This is not perfect
195 * for stream intersection, but required as a
196 * fall-back when "flush_empty" is not
197 * implemented by lttng-modules.
199 ret
= kernctl_buffer_flush(stream
->wait_fd
);
201 ERR("Failed to flush kernel stream");
207 ret
= lttng_kconsumer_take_snapshot(stream
);
209 ERR("Taking kernel snapshot");
213 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
215 ERR("Produced kernel snapshot position");
219 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
221 ERR("Consumerd kernel snapshot position");
225 if (stream
->max_sb_size
== 0) {
226 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
227 &stream
->max_sb_size
);
229 ERR("Getting kernel max_sb_size");
234 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
235 produced_pos
, nb_packets_per_stream
,
236 stream
->max_sb_size
);
238 while ((long) (consumed_pos
- produced_pos
) < 0) {
240 unsigned long len
, padded_len
;
242 health_code_update();
244 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
246 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
248 if (ret
!= -EAGAIN
) {
249 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");
261 goto error_put_subbuf
;
264 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
266 ERR("Snapshot kernctl_get_padded_subbuf_size");
267 goto error_put_subbuf
;
270 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
271 padded_len
- len
, NULL
);
273 * We write the padded len in local tracefiles but the data len
274 * when using a relay. Display the error but continue processing
275 * to try to release the subbuffer.
277 if (relayd_id
!= (uint64_t) -1ULL) {
278 if (read_len
!= len
) {
279 ERR("Error sending to the relay (ret: %zd != len: %lu)",
283 if (read_len
!= padded_len
) {
284 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
285 read_len
, padded_len
);
289 ret
= kernctl_put_subbuf(stream
->wait_fd
);
291 ERR("Snapshot kernctl_put_subbuf");
294 consumed_pos
+= stream
->max_sb_size
;
297 if (relayd_id
== (uint64_t) -1ULL) {
298 if (stream
->out_fd
>= 0) {
299 ret
= close(stream
->out_fd
);
301 PERROR("Kernel consumer snapshot close out_fd");
307 close_relayd_stream(stream
);
308 stream
->net_seq_idx
= (uint64_t) -1ULL;
310 pthread_mutex_unlock(&stream
->lock
);
318 ret
= kernctl_put_subbuf(stream
->wait_fd
);
320 ERR("Snapshot kernctl_put_subbuf error path");
323 pthread_mutex_unlock(&stream
->lock
);
330 * Read the whole metadata available for a snapshot.
332 * Returns 0 on success, < 0 on error
334 static int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
335 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
337 int ret
, use_relayd
= 0;
339 struct lttng_consumer_channel
*metadata_channel
;
340 struct lttng_consumer_stream
*metadata_stream
;
344 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
349 metadata_channel
= consumer_find_channel(key
);
350 if (!metadata_channel
) {
351 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
353 goto error_no_channel
;
356 metadata_stream
= metadata_channel
->metadata_stream
;
357 assert(metadata_stream
);
358 pthread_mutex_lock(&metadata_stream
->lock
);
360 /* Flag once that we have a valid relayd for the stream. */
361 if (relayd_id
!= (uint64_t) -1ULL) {
366 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
371 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
372 metadata_stream
->chan
->tracefile_size
,
373 metadata_stream
->tracefile_count_current
,
374 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
378 metadata_stream
->out_fd
= ret
;
382 health_code_update();
384 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
386 if (ret_read
!= -EAGAIN
) {
387 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
392 /* ret_read is negative at this point so we will exit the loop. */
395 } while (ret_read
>= 0);
398 close_relayd_stream(metadata_stream
);
399 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
401 if (metadata_stream
->out_fd
>= 0) {
402 ret
= close(metadata_stream
->out_fd
);
404 PERROR("Kernel consumer snapshot metadata close out_fd");
406 * Don't go on error here since the snapshot was successful at this
407 * point but somehow the close failed.
410 metadata_stream
->out_fd
= -1;
416 pthread_mutex_unlock(&metadata_stream
->lock
);
417 cds_list_del(&metadata_stream
->send_node
);
418 consumer_stream_destroy(metadata_stream
, NULL
);
419 metadata_channel
->metadata_stream
= NULL
;
426 * Receive command from session daemon and process it.
428 * Return 1 on success else a negative value or 0.
430 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
431 int sock
, struct pollfd
*consumer_sockpoll
)
434 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
435 struct lttcomm_consumer_msg msg
;
437 health_code_update();
439 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
440 if (ret
!= sizeof(msg
)) {
442 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
448 health_code_update();
450 /* Deprecated command */
451 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
453 health_code_update();
455 /* relayd needs RCU read-side protection */
458 switch (msg
.cmd_type
) {
459 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
461 /* Session daemon status message are handled in the following call. */
462 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
463 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
464 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
465 msg
.u
.relayd_sock
.relayd_session_id
);
468 case LTTNG_CONSUMER_ADD_CHANNEL
:
470 struct lttng_consumer_channel
*new_channel
;
473 health_code_update();
475 /* First send a status message before receiving the fds. */
476 ret
= consumer_send_status_msg(sock
, ret_code
);
478 /* Somehow, the session daemon is not responding anymore. */
482 health_code_update();
484 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
485 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
486 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
487 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
488 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
489 msg
.u
.channel
.tracefile_size
,
490 msg
.u
.channel
.tracefile_count
, 0,
491 msg
.u
.channel
.monitor
,
492 msg
.u
.channel
.live_timer_interval
,
494 if (new_channel
== NULL
) {
495 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
498 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
499 switch (msg
.u
.channel
.output
) {
500 case LTTNG_EVENT_SPLICE
:
501 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
503 case LTTNG_EVENT_MMAP
:
504 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
507 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
511 /* Translate and save channel type. */
512 switch (msg
.u
.channel
.type
) {
513 case CONSUMER_CHANNEL_TYPE_DATA
:
514 case CONSUMER_CHANNEL_TYPE_METADATA
:
515 new_channel
->type
= msg
.u
.channel
.type
;
522 health_code_update();
524 if (ctx
->on_recv_channel
!= NULL
) {
525 ret_recv
= ctx
->on_recv_channel(new_channel
);
527 ret
= consumer_add_channel(new_channel
, ctx
);
528 } else if (ret_recv
< 0) {
532 ret
= consumer_add_channel(new_channel
, ctx
);
534 if (CONSUMER_CHANNEL_TYPE_DATA
) {
535 consumer_timer_live_start(new_channel
,
536 msg
.u
.channel
.live_timer_interval
);
539 health_code_update();
541 /* If we received an error in add_channel, we need to report it. */
543 ret
= consumer_send_status_msg(sock
, ret
);
552 case LTTNG_CONSUMER_ADD_STREAM
:
555 struct lttng_pipe
*stream_pipe
;
556 struct lttng_consumer_stream
*new_stream
;
557 struct lttng_consumer_channel
*channel
;
561 * Get stream's channel reference. Needed when adding the stream to the
564 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
567 * We could not find the channel. Can happen if cpu hotplug
568 * happens while tearing down.
570 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
571 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
574 health_code_update();
576 /* First send a status message before receiving the fds. */
577 ret
= consumer_send_status_msg(sock
, ret_code
);
579 /* Somehow, the session daemon is not responding anymore. */
583 health_code_update();
585 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
586 /* Channel was not found. */
592 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
598 health_code_update();
600 /* Get stream file descriptor from socket */
601 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
602 if (ret
!= sizeof(fd
)) {
603 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
608 health_code_update();
611 * Send status code to session daemon only if the recv works. If the
612 * above recv() failed, the session daemon is notified through the
613 * error socket and the teardown is eventually done.
615 ret
= consumer_send_status_msg(sock
, ret_code
);
617 /* Somehow, the session daemon is not responding anymore. */
621 health_code_update();
623 new_stream
= consumer_allocate_stream(channel
->key
,
625 LTTNG_CONSUMER_ACTIVE_STREAM
,
635 if (new_stream
== NULL
) {
640 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
646 new_stream
->chan
= channel
;
647 new_stream
->wait_fd
= fd
;
648 switch (channel
->output
) {
649 case CONSUMER_CHANNEL_SPLICE
:
650 new_stream
->output
= LTTNG_EVENT_SPLICE
;
651 ret
= utils_create_pipe(new_stream
->splice_pipe
);
656 case CONSUMER_CHANNEL_MMAP
:
657 new_stream
->output
= LTTNG_EVENT_MMAP
;
660 ERR("Stream output unknown %d", channel
->output
);
665 * We've just assigned the channel to the stream so increment the
666 * refcount right now. We don't need to increment the refcount for
667 * streams in no monitor because we handle manually the cleanup of
668 * those. It is very important to make sure there is NO prior
669 * consumer_del_stream() calls or else the refcount will be unbalanced.
671 if (channel
->monitor
) {
672 uatomic_inc(&new_stream
->chan
->refcount
);
676 * The buffer flush is done on the session daemon side for the kernel
677 * so no need for the stream "hangup_flush_done" variable to be
678 * tracked. This is important for a kernel stream since we don't rely
679 * on the flush state of the stream to read data. It's not the case for
680 * user space tracing.
682 new_stream
->hangup_flush_done
= 0;
684 health_code_update();
686 if (ctx
->on_recv_stream
) {
687 ret
= ctx
->on_recv_stream(new_stream
);
689 consumer_stream_free(new_stream
);
694 health_code_update();
696 if (new_stream
->metadata_flag
) {
697 channel
->metadata_stream
= new_stream
;
700 /* Do not monitor this stream. */
701 if (!channel
->monitor
) {
702 DBG("Kernel consumer add stream %s in no monitor mode with "
703 "relayd id %" PRIu64
, new_stream
->name
,
704 new_stream
->net_seq_idx
);
705 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
709 /* Send stream to relayd if the stream has an ID. */
710 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
711 ret
= consumer_send_relayd_stream(new_stream
,
712 new_stream
->chan
->pathname
);
714 consumer_stream_free(new_stream
);
719 * If adding an extra stream to an already
720 * existing channel (e.g. cpu hotplug), we need
721 * to send the "streams_sent" command to relayd.
723 if (channel
->streams_sent_to_relayd
) {
724 ret
= consumer_send_relayd_streams_sent(
725 new_stream
->net_seq_idx
);
732 /* Get the right pipe where the stream will be sent. */
733 if (new_stream
->metadata_flag
) {
734 ret
= consumer_add_metadata_stream(new_stream
);
736 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
738 consumer_stream_free(new_stream
);
741 stream_pipe
= ctx
->consumer_metadata_pipe
;
743 ret
= consumer_add_data_stream(new_stream
);
745 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
747 consumer_stream_free(new_stream
);
750 stream_pipe
= ctx
->consumer_data_pipe
;
753 /* Vitible to other threads */
754 new_stream
->globally_visible
= 1;
756 health_code_update();
758 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
760 ERR("Consumer write %s stream to pipe %d",
761 new_stream
->metadata_flag
? "metadata" : "data",
762 lttng_pipe_get_writefd(stream_pipe
));
763 if (new_stream
->metadata_flag
) {
764 consumer_del_stream_for_metadata(new_stream
);
766 consumer_del_stream_for_data(new_stream
);
771 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
772 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
775 case LTTNG_CONSUMER_STREAMS_SENT
:
777 struct lttng_consumer_channel
*channel
;
780 * Get stream's channel reference. Needed when adding the stream to the
783 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
786 * We could not find the channel. Can happen if cpu hotplug
787 * happens while tearing down.
789 ERR("Unable to find channel key %" PRIu64
,
790 msg
.u
.sent_streams
.channel_key
);
791 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
794 health_code_update();
797 * Send status code to session daemon.
799 ret
= consumer_send_status_msg(sock
, ret_code
);
800 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
801 /* Somehow, the session daemon is not responding anymore. */
805 health_code_update();
808 * We should not send this message if we don't monitor the
809 * streams in this channel.
811 if (!channel
->monitor
) {
815 health_code_update();
816 /* Send stream to relayd if the stream has an ID. */
817 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
818 ret
= consumer_send_relayd_streams_sent(
819 msg
.u
.sent_streams
.net_seq_idx
);
823 channel
->streams_sent_to_relayd
= true;
827 case LTTNG_CONSUMER_UPDATE_STREAM
:
832 case LTTNG_CONSUMER_DESTROY_RELAYD
:
834 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
835 struct consumer_relayd_sock_pair
*relayd
;
837 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
839 /* Get relayd reference if exists. */
840 relayd
= consumer_find_relayd(index
);
841 if (relayd
== NULL
) {
842 DBG("Unable to find relayd %" PRIu64
, index
);
843 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
847 * Each relayd socket pair has a refcount of stream attached to it
848 * which tells if the relayd is still active or not depending on the
851 * This will set the destroy flag of the relayd object and destroy it
852 * if the refcount reaches zero when called.
854 * The destroy can happen either here or when a stream fd hangs up.
857 consumer_flag_relayd_for_destroy(relayd
);
860 health_code_update();
862 ret
= consumer_send_status_msg(sock
, ret_code
);
864 /* Somehow, the session daemon is not responding anymore. */
870 case LTTNG_CONSUMER_DATA_PENDING
:
873 uint64_t id
= msg
.u
.data_pending
.session_id
;
875 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
877 ret
= consumer_data_pending(id
);
879 health_code_update();
881 /* Send back returned value to session daemon */
882 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
884 PERROR("send data pending ret code");
889 * No need to send back a status message since the data pending
890 * returned value is the response.
894 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
896 if (msg
.u
.snapshot_channel
.metadata
== 1) {
897 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
898 msg
.u
.snapshot_channel
.pathname
,
899 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
901 ERR("Snapshot metadata failed");
902 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
905 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
906 msg
.u
.snapshot_channel
.pathname
,
907 msg
.u
.snapshot_channel
.relayd_id
,
908 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
911 ERR("Snapshot channel failed");
912 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
916 health_code_update();
918 ret
= consumer_send_status_msg(sock
, ret_code
);
920 /* Somehow, the session daemon is not responding anymore. */
925 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
927 uint64_t key
= msg
.u
.destroy_channel
.key
;
928 struct lttng_consumer_channel
*channel
;
930 channel
= consumer_find_channel(key
);
932 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
933 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
936 health_code_update();
938 ret
= consumer_send_status_msg(sock
, ret_code
);
940 /* Somehow, the session daemon is not responding anymore. */
944 health_code_update();
946 /* Stop right now if no channel was found. */
952 * This command should ONLY be issued for channel with streams set in
955 assert(!channel
->monitor
);
958 * The refcount should ALWAYS be 0 in the case of a channel in no
961 assert(!uatomic_sub_return(&channel
->refcount
, 1));
963 consumer_del_channel(channel
);
967 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
970 struct lttng_consumer_channel
*channel
;
971 uint64_t id
= msg
.u
.discarded_events
.session_id
;
972 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
974 DBG("Kernel consumer discarded events command for session id %"
975 PRIu64
", channel key %" PRIu64
, id
, key
);
977 channel
= consumer_find_channel(key
);
979 ERR("Kernel consumer discarded events channel %"
980 PRIu64
" not found", key
);
983 ret
= channel
->discarded_events
;
986 health_code_update();
988 /* Send back returned value to session daemon */
989 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
991 PERROR("send discarded events");
997 case LTTNG_CONSUMER_LOST_PACKETS
:
1000 struct lttng_consumer_channel
*channel
;
1001 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1002 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1004 DBG("Kernel consumer lost packets command for session id %"
1005 PRIu64
", channel key %" PRIu64
, id
, key
);
1007 channel
= consumer_find_channel(key
);
1009 ERR("Kernel consumer lost packets channel %"
1010 PRIu64
" not found", key
);
1013 ret
= channel
->lost_packets
;
1016 health_code_update();
1018 /* Send back returned value to session daemon */
1019 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1021 PERROR("send lost packets");
1035 * Return 1 to indicate success since the 0 value can be a socket
1036 * shutdown during the recv() or send() call.
1038 health_code_update();
1043 /* This will issue a consumer stop. */
1048 * Populate index values of a kernel stream. Values are set in big endian order.
1050 * Return 0 on success or else a negative value.
1052 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1056 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1058 PERROR("kernctl_get_timestamp_begin");
1061 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1063 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1065 PERROR("kernctl_get_timestamp_end");
1068 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1070 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1072 PERROR("kernctl_get_events_discarded");
1075 index
->events_discarded
= htobe64(index
->events_discarded
);
1077 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1079 PERROR("kernctl_get_content_size");
1082 index
->content_size
= htobe64(index
->content_size
);
1084 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1086 PERROR("kernctl_get_packet_size");
1089 index
->packet_size
= htobe64(index
->packet_size
);
1091 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1093 PERROR("kernctl_get_stream_id");
1096 index
->stream_id
= htobe64(index
->stream_id
);
1098 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1100 if (ret
== -ENOTTY
) {
1101 /* Command not implemented by lttng-modules. */
1102 index
->stream_instance_id
= -1ULL;
1105 PERROR("kernctl_get_instance_id");
1109 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1111 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1113 if (ret
== -ENOTTY
) {
1114 /* Command not implemented by lttng-modules. */
1115 index
->packet_seq_num
= -1ULL;
1118 PERROR("kernctl_get_sequence_number");
1122 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1128 * Sync metadata meaning request them to the session daemon and snapshot to the
1129 * metadata thread can consumer them.
1131 * Metadata stream lock MUST be acquired.
1133 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1134 * is empty or a negative value on error.
1136 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1142 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1144 ERR("Failed to flush kernel stream");
1148 ret
= kernctl_snapshot(metadata
->wait_fd
);
1150 if (ret
!= -EAGAIN
) {
1151 ERR("Sync metadata, taking kernel snapshot failed.");
1154 DBG("Sync metadata, no new kernel metadata");
1155 /* No new metadata, exit. */
1165 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1168 uint64_t seq
, discarded
;
1170 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1172 if (ret
== -ENOTTY
) {
1173 /* Command not implemented by lttng-modules. */
1177 PERROR("kernctl_get_sequence_number");
1183 * Start the sequence when we extract the first packet in case we don't
1184 * start at 0 (for example if a consumer is not connected to the
1185 * session immediately after the beginning).
1187 if (stream
->last_sequence_number
== -1ULL) {
1188 stream
->last_sequence_number
= seq
;
1189 } else if (seq
> stream
->last_sequence_number
) {
1190 stream
->chan
->lost_packets
+= seq
-
1191 stream
->last_sequence_number
- 1;
1193 /* seq <= last_sequence_number */
1194 ERR("Sequence number inconsistent : prev = %" PRIu64
1195 ", current = %" PRIu64
,
1196 stream
->last_sequence_number
, seq
);
1200 stream
->last_sequence_number
= seq
;
1202 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1204 PERROR("kernctl_get_events_discarded");
1207 if (discarded
< stream
->last_discarded_events
) {
1209 * Overflow has occurred. We assume only one wrap-around
1212 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1213 stream
->last_discarded_events
+ discarded
;
1215 stream
->chan
->discarded_events
+= discarded
-
1216 stream
->last_discarded_events
;
1218 stream
->last_discarded_events
= discarded
;
1226 * Check if the local version of the metadata stream matches with the version
1227 * of the metadata stream in the kernel. If it was updated, set the reset flag
1231 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1234 uint64_t cur_version
;
1236 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1238 if (ret
== -ENOTTY
) {
1240 * LTTng-modules does not implement this
1246 ERR("Failed to get the metadata version");
1250 if (stream
->metadata_version
== cur_version
) {
1255 DBG("New metadata version detected");
1256 stream
->metadata_version
= cur_version
;
1257 stream
->reset_metadata_flag
= 1;
1265 * Consume data on a file descriptor and write it on a trace file.
1267 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1268 struct lttng_consumer_local_data
*ctx
)
1270 unsigned long len
, subbuf_size
, padding
;
1271 int err
, write_index
= 1;
1273 int infd
= stream
->wait_fd
;
1274 struct ctf_packet_index index
;
1276 DBG("In read_subbuffer (infd : %d)", infd
);
1278 /* Get the next subbuffer */
1279 err
= kernctl_get_next_subbuf(infd
);
1282 * This is a debug message even for single-threaded consumer,
1283 * because poll() have more relaxed criterions than get subbuf,
1284 * so get_subbuf may fail for short race windows where poll()
1285 * would issue wakeups.
1287 DBG("Reserving sub buffer failed (everything is normal, "
1288 "it is due to concurrency)");
1293 /* Get the full subbuffer size including padding */
1294 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1296 PERROR("Getting sub-buffer len failed.");
1297 err
= kernctl_put_subbuf(infd
);
1299 if (err
== -EFAULT
) {
1300 PERROR("Error in unreserving sub buffer\n");
1301 } else if (err
== -EIO
) {
1302 /* Should never happen with newer LTTng versions */
1303 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1312 if (!stream
->metadata_flag
) {
1313 ret
= get_index_values(&index
, infd
);
1315 err
= kernctl_put_subbuf(infd
);
1317 if (err
== -EFAULT
) {
1318 PERROR("Error in unreserving sub buffer\n");
1319 } else if (err
== -EIO
) {
1320 /* Should never happen with newer LTTng versions */
1321 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1328 ret
= update_stream_stats(stream
);
1330 err
= kernctl_put_subbuf(infd
);
1332 if (err
== -EFAULT
) {
1333 PERROR("Error in unreserving sub buffer\n");
1334 } else if (err
== -EIO
) {
1335 /* Should never happen with newer LTTng versions */
1336 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1345 ret
= metadata_stream_check_version(infd
, stream
);
1347 err
= kernctl_put_subbuf(infd
);
1349 if (err
== -EFAULT
) {
1350 PERROR("Error in unreserving sub buffer\n");
1351 } else if (err
== -EIO
) {
1352 /* Should never happen with newer LTTng versions */
1353 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1362 switch (stream
->chan
->output
) {
1363 case CONSUMER_CHANNEL_SPLICE
:
1365 * XXX: The lttng-modules splice "actor" does not handle copying
1366 * partial pages hence only using the subbuffer size without the
1367 * padding makes the splice fail.
1372 /* splice the subbuffer to the tracefile */
1373 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1376 * XXX: Splice does not support network streaming so the return value
1377 * is simply checked against subbuf_size and not like the mmap() op.
1379 if (ret
!= subbuf_size
) {
1381 * display the error but continue processing to try
1382 * to release the subbuffer
1384 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1389 case CONSUMER_CHANNEL_MMAP
:
1390 /* Get subbuffer size without padding */
1391 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1393 PERROR("Getting sub-buffer len failed.");
1394 err
= kernctl_put_subbuf(infd
);
1396 if (err
== -EFAULT
) {
1397 PERROR("Error in unreserving sub buffer\n");
1398 } else if (err
== -EIO
) {
1399 /* Should never happen with newer LTTng versions */
1400 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1409 /* Make sure the tracer is not gone mad on us! */
1410 assert(len
>= subbuf_size
);
1412 padding
= len
- subbuf_size
;
1414 /* write the subbuffer to the tracefile */
1415 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1418 * The mmap operation should write subbuf_size amount of data when
1419 * network streaming or the full padding (len) size when we are _not_
1422 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1423 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1425 * Display the error but continue processing to try to release the
1426 * subbuffer. This is a DBG statement since this is possible to
1427 * happen without being a critical error.
1429 DBG("Error writing to tracefile "
1430 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1431 ret
, len
, subbuf_size
);
1436 ERR("Unknown output method");
1440 err
= kernctl_put_next_subbuf(infd
);
1442 if (err
== -EFAULT
) {
1443 PERROR("Error in unreserving sub buffer\n");
1444 } else if (err
== -EIO
) {
1445 /* Should never happen with newer LTTng versions */
1446 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1452 /* Write index if needed. */
1457 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1459 * In live, block until all the metadata is sent.
1461 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1462 assert(!stream
->missed_metadata_flush
);
1463 stream
->waiting_on_metadata
= true;
1464 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1466 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1468 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1469 stream
->waiting_on_metadata
= false;
1470 if (stream
->missed_metadata_flush
) {
1471 stream
->missed_metadata_flush
= false;
1472 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1473 (void) consumer_flush_kernel_index(stream
);
1475 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1482 err
= consumer_stream_write_index(stream
, &index
);
1491 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1498 * Don't create anything if this is set for streaming or should not be
1501 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1502 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1503 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1504 stream
->uid
, stream
->gid
, NULL
);
1508 stream
->out_fd
= ret
;
1509 stream
->tracefile_size_current
= 0;
1511 if (!stream
->metadata_flag
) {
1512 struct lttng_index_file
*index_file
;
1514 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1515 stream
->name
, stream
->uid
, stream
->gid
,
1516 stream
->chan
->tracefile_size
,
1517 stream
->tracefile_count_current
,
1518 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1522 stream
->index_file
= index_file
;
1526 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1527 /* get the len of the mmap region */
1528 unsigned long mmap_len
;
1530 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1532 PERROR("kernctl_get_mmap_len");
1533 goto error_close_fd
;
1535 stream
->mmap_len
= (size_t) mmap_len
;
1537 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1538 MAP_PRIVATE
, stream
->wait_fd
, 0);
1539 if (stream
->mmap_base
== MAP_FAILED
) {
1540 PERROR("Error mmaping");
1542 goto error_close_fd
;
1546 /* we return 0 to let the library handle the FD internally */
1550 if (stream
->out_fd
>= 0) {
1553 err
= close(stream
->out_fd
);
1555 stream
->out_fd
= -1;
1562 * Check if data is still being extracted from the buffers for a specific
1563 * stream. Consumer data lock MUST be acquired before calling this function
1564 * and the stream lock.
1566 * Return 1 if the traced data are still getting read else 0 meaning that the
1567 * data is available for trace viewer reading.
1569 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1575 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1580 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1582 /* There is still data so let's put back this subbuffer. */
1583 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1585 ret
= 1; /* Data is pending */
1589 /* Data is NOT pending and ready to be read. */