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.
19 #include "common/buffer-view.h"
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <bin/lttng-consumerd/health-consumerd.h>
35 #include <common/common.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/sessiond-comm/relayd.h>
39 #include <common/compat/fcntl.h>
40 #include <common/compat/endian.h>
41 #include <common/pipe.h>
42 #include <common/relayd/relayd.h>
43 #include <common/utils.h>
44 #include <common/consumer/consumer-stream.h>
45 #include <common/index/index.h>
46 #include <common/consumer/consumer-timer.h>
48 #include "kernel-consumer.h"
50 extern struct lttng_consumer_global_data consumer_data
;
51 extern int consumer_poll_timeout
;
52 extern volatile int consumer_quit
;
55 * Take a snapshot for a specific fd
57 * Returns 0 on success, < 0 on error
59 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
62 int infd
= stream
->wait_fd
;
64 ret
= kernctl_snapshot(infd
);
66 * -EAGAIN is not an error, it just means that there is no data to
69 if (ret
!= 0 && ret
!= -EAGAIN
) {
70 PERROR("Getting sub-buffer snapshot.");
77 * Get the produced position
79 * Returns 0 on success, < 0 on error
81 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
85 int infd
= stream
->wait_fd
;
87 ret
= kernctl_snapshot_get_produced(infd
, pos
);
89 PERROR("kernctl_snapshot_get_produced");
96 * Get the consumerd position
98 * Returns 0 on success, < 0 on error
100 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
104 int infd
= stream
->wait_fd
;
106 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
108 PERROR("kernctl_snapshot_get_consumed");
115 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
119 unsigned long mmap_offset
;
120 const char *mmap_base
= stream
->mmap_base
;
122 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
124 PERROR("Failed to get mmap read offset");
128 *addr
= mmap_base
+ mmap_offset
;
134 * Take a snapshot of all the stream of a channel
136 * Returns 0 on success, < 0 on error
138 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
139 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
140 struct lttng_consumer_local_data
*ctx
)
143 struct lttng_consumer_channel
*channel
;
144 struct lttng_consumer_stream
*stream
;
146 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
150 channel
= consumer_find_channel(key
);
152 ERR("No channel found for key %" PRIu64
, key
);
157 /* Splice is not supported yet for channel snapshot. */
158 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
159 ERR("Unsupported output %d", channel
->output
);
164 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
165 unsigned long consumed_pos
, produced_pos
;
167 health_code_update();
170 * Lock stream because we are about to change its state.
172 pthread_mutex_lock(&stream
->lock
);
175 * Assign the received relayd ID so we can use it for streaming. The streams
176 * are not visible to anyone so this is OK to change it.
178 stream
->relayd_id
= relayd_id
;
179 channel
->relayd_id
= relayd_id
;
180 if (relayd_id
!= (uint64_t) -1ULL) {
181 ret
= consumer_send_relayd_stream(stream
, path
);
183 ERR("sending stream to relayd");
187 ret
= utils_create_stream_file(path
, stream
->name
,
188 stream
->chan
->tracefile_size
,
189 stream
->tracefile_count_current
,
190 stream
->uid
, stream
->gid
, NULL
);
192 ERR("utils_create_stream_file");
196 stream
->out_fd
= ret
;
197 stream
->tracefile_size_current
= 0;
199 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
200 path
, stream
->name
, stream
->key
);
202 if (relayd_id
!= -1ULL) {
203 ret
= consumer_send_relayd_streams_sent(relayd_id
);
205 ERR("sending streams sent to relayd");
208 channel
->streams_sent_to_relayd
= true;
211 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
214 * Doing a buffer flush which does not take into
215 * account empty packets. This is not perfect
216 * for stream intersection, but required as a
217 * fall-back when "flush_empty" is not
218 * implemented by lttng-modules.
220 ret
= kernctl_buffer_flush(stream
->wait_fd
);
222 ERR("Failed to flush kernel stream");
228 ret
= lttng_kconsumer_take_snapshot(stream
);
230 ERR("Taking kernel snapshot");
234 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
236 ERR("Produced kernel snapshot position");
240 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
242 ERR("Consumerd kernel snapshot position");
246 if (stream
->max_sb_size
== 0) {
247 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
248 &stream
->max_sb_size
);
250 ERR("Getting kernel max_sb_size");
255 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
256 produced_pos
, nb_packets_per_stream
,
257 stream
->max_sb_size
);
259 while (consumed_pos
< produced_pos
) {
261 unsigned long len
, padded_len
;
262 const char *subbuf_addr
;
263 struct lttng_buffer_view subbuf_view
;
265 health_code_update();
266 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
268 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
270 if (ret
!= -EAGAIN
) {
271 PERROR("kernctl_get_subbuf snapshot");
274 DBG("Kernel consumer get subbuf failed. Skipping it.");
275 consumed_pos
+= stream
->max_sb_size
;
276 stream
->chan
->lost_packets
++;
280 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
282 ERR("Snapshot kernctl_get_subbuf_size");
283 goto error_put_subbuf
;
286 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
288 ERR("Snapshot kernctl_get_padded_subbuf_size");
289 goto error_put_subbuf
;
292 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
294 goto error_put_subbuf
;
297 subbuf_view
= lttng_buffer_view_init(
298 subbuf_addr
, 0, padded_len
);
299 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
300 stream
, &subbuf_view
,
301 padded_len
- len
, NULL
);
303 * We write the padded len in local tracefiles but the data len
304 * when using a relay. Display the error but continue processing
305 * to try to release the subbuffer.
307 if (relayd_id
!= (uint64_t) -1ULL) {
308 if (read_len
!= len
) {
309 ERR("Error sending to the relay (ret: %zd != len: %lu)",
313 if (read_len
!= padded_len
) {
314 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
315 read_len
, padded_len
);
319 ret
= kernctl_put_subbuf(stream
->wait_fd
);
321 ERR("Snapshot kernctl_put_subbuf");
324 consumed_pos
+= stream
->max_sb_size
;
327 if (relayd_id
== (uint64_t) -1ULL) {
328 if (stream
->out_fd
>= 0) {
329 ret
= close(stream
->out_fd
);
331 PERROR("Kernel consumer snapshot close out_fd");
337 close_relayd_stream(stream
);
338 stream
->relayd_id
= (uint64_t) -1ULL;
340 pthread_mutex_unlock(&stream
->lock
);
348 ret
= kernctl_put_subbuf(stream
->wait_fd
);
350 ERR("Snapshot kernctl_put_subbuf error path");
353 pthread_mutex_unlock(&stream
->lock
);
360 * Read the whole metadata available for a snapshot.
362 * Returns 0 on success, < 0 on error
364 static int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
365 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
367 int ret
, use_relayd
= 0;
369 struct lttng_consumer_channel
*metadata_channel
;
370 struct lttng_consumer_stream
*metadata_stream
;
374 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
379 metadata_channel
= consumer_find_channel(key
);
380 if (!metadata_channel
) {
381 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
383 goto error_no_channel
;
386 metadata_stream
= metadata_channel
->metadata_stream
;
387 assert(metadata_stream
);
388 pthread_mutex_lock(&metadata_stream
->lock
);
390 /* Flag once that we have a valid relayd for the stream. */
391 if (relayd_id
!= (uint64_t) -1ULL) {
396 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
401 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
402 metadata_stream
->chan
->tracefile_size
,
403 metadata_stream
->tracefile_count_current
,
404 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
408 metadata_stream
->out_fd
= ret
;
412 health_code_update();
414 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
416 if (ret_read
!= -EAGAIN
) {
417 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
422 /* ret_read is negative at this point so we will exit the loop. */
425 } while (ret_read
>= 0);
428 close_relayd_stream(metadata_stream
);
429 metadata_stream
->relayd_id
= (uint64_t) -1ULL;
431 if (metadata_stream
->out_fd
>= 0) {
432 ret
= close(metadata_stream
->out_fd
);
434 PERROR("Kernel consumer snapshot metadata close out_fd");
436 * Don't go on error here since the snapshot was successful at this
437 * point but somehow the close failed.
440 metadata_stream
->out_fd
= -1;
446 pthread_mutex_unlock(&metadata_stream
->lock
);
447 cds_list_del(&metadata_stream
->send_node
);
448 consumer_stream_destroy(metadata_stream
, NULL
);
449 metadata_channel
->metadata_stream
= NULL
;
456 * Receive command from session daemon and process it.
458 * Return 1 on success else a negative value or 0.
460 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
461 int sock
, struct pollfd
*consumer_sockpoll
)
464 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
465 struct lttcomm_consumer_msg msg
;
467 health_code_update();
469 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
470 if (ret
!= sizeof(msg
)) {
472 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
478 health_code_update();
480 /* Deprecated command */
481 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
483 health_code_update();
485 /* relayd needs RCU read-side protection */
488 switch (msg
.cmd_type
) {
489 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
491 /* Session daemon status message are handled in the following call. */
492 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
493 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
494 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
495 msg
.u
.relayd_sock
.relayd_session_id
);
498 case LTTNG_CONSUMER_ADD_CHANNEL
:
500 struct lttng_consumer_channel
*new_channel
;
503 health_code_update();
505 /* First send a status message before receiving the fds. */
506 ret
= consumer_send_status_msg(sock
, ret_code
);
508 /* Somehow, the session daemon is not responding anymore. */
512 health_code_update();
514 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
515 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
516 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
517 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
518 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
519 msg
.u
.channel
.tracefile_size
,
520 msg
.u
.channel
.tracefile_count
, 0,
521 msg
.u
.channel
.monitor
,
522 msg
.u
.channel
.live_timer_interval
,
524 if (new_channel
== NULL
) {
525 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
528 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
529 switch (msg
.u
.channel
.output
) {
530 case LTTNG_EVENT_SPLICE
:
531 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
533 case LTTNG_EVENT_MMAP
:
534 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
537 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
541 /* Translate and save channel type. */
542 switch (msg
.u
.channel
.type
) {
543 case CONSUMER_CHANNEL_TYPE_DATA
:
544 case CONSUMER_CHANNEL_TYPE_METADATA
:
545 new_channel
->type
= msg
.u
.channel
.type
;
552 health_code_update();
554 if (ctx
->on_recv_channel
!= NULL
) {
555 ret_recv
= ctx
->on_recv_channel(new_channel
);
557 ret
= consumer_add_channel(new_channel
, ctx
);
558 } else if (ret_recv
< 0) {
562 ret
= consumer_add_channel(new_channel
, ctx
);
564 if (CONSUMER_CHANNEL_TYPE_DATA
) {
565 consumer_timer_live_start(new_channel
,
566 msg
.u
.channel
.live_timer_interval
);
569 health_code_update();
571 /* If we received an error in add_channel, we need to report it. */
573 ret
= consumer_send_status_msg(sock
, ret
);
582 case LTTNG_CONSUMER_ADD_STREAM
:
585 struct lttng_pipe
*stream_pipe
;
586 struct lttng_consumer_stream
*new_stream
;
587 struct lttng_consumer_channel
*channel
;
591 * Get stream's channel reference. Needed when adding the stream to the
594 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
597 * We could not find the channel. Can happen if cpu hotplug
598 * happens while tearing down.
600 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
601 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
604 health_code_update();
606 /* First send a status message before receiving the fds. */
607 ret
= consumer_send_status_msg(sock
, ret_code
);
609 /* Somehow, the session daemon is not responding anymore. */
613 health_code_update();
615 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
616 /* Channel was not found. */
622 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
628 health_code_update();
630 /* Get stream file descriptor from socket */
631 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
632 if (ret
!= sizeof(fd
)) {
633 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
638 health_code_update();
641 * Send status code to session daemon only if the recv works. If the
642 * above recv() failed, the session daemon is notified through the
643 * error socket and the teardown is eventually done.
645 ret
= consumer_send_status_msg(sock
, ret_code
);
647 /* Somehow, the session daemon is not responding anymore. */
651 health_code_update();
653 pthread_mutex_lock(&channel
->lock
);
654 new_stream
= consumer_allocate_stream(
658 LTTNG_CONSUMER_ACTIVE_STREAM
,
668 if (new_stream
== NULL
) {
673 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
676 pthread_mutex_unlock(&channel
->lock
);
680 new_stream
->wait_fd
= fd
;
681 switch (channel
->output
) {
682 case CONSUMER_CHANNEL_SPLICE
:
683 new_stream
->output
= LTTNG_EVENT_SPLICE
;
684 ret
= utils_create_pipe(new_stream
->splice_pipe
);
689 case CONSUMER_CHANNEL_MMAP
:
690 new_stream
->output
= LTTNG_EVENT_MMAP
;
693 ERR("Stream output unknown %d", channel
->output
);
698 * We've just assigned the channel to the stream so increment the
699 * refcount right now. We don't need to increment the refcount for
700 * streams in no monitor because we handle manually the cleanup of
701 * those. It is very important to make sure there is NO prior
702 * consumer_del_stream() calls or else the refcount will be unbalanced.
704 if (channel
->monitor
) {
705 uatomic_inc(&new_stream
->chan
->refcount
);
709 * The buffer flush is done on the session daemon side for the kernel
710 * so no need for the stream "hangup_flush_done" variable to be
711 * tracked. This is important for a kernel stream since we don't rely
712 * on the flush state of the stream to read data. It's not the case for
713 * user space tracing.
715 new_stream
->hangup_flush_done
= 0;
717 health_code_update();
719 if (ctx
->on_recv_stream
) {
720 ret
= ctx
->on_recv_stream(new_stream
);
722 consumer_stream_free(new_stream
);
727 health_code_update();
729 if (new_stream
->metadata_flag
) {
730 channel
->metadata_stream
= new_stream
;
733 /* Do not monitor this stream. */
734 if (!channel
->monitor
) {
735 DBG("Kernel consumer add stream %s in no monitor mode with "
736 "relayd id %" PRIu64
, new_stream
->name
,
737 new_stream
->relayd_id
);
738 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
739 pthread_mutex_unlock(&channel
->lock
);
743 /* Send stream to relayd if the stream has an ID. */
744 if (new_stream
->relayd_id
!= (uint64_t) -1ULL) {
745 ret
= consumer_send_relayd_stream(new_stream
,
746 new_stream
->chan
->pathname
);
748 pthread_mutex_unlock(&channel
->lock
);
749 consumer_stream_free(new_stream
);
754 * If adding an extra stream to an already
755 * existing channel (e.g. cpu hotplug), we need
756 * to send the "streams_sent" command to relayd.
758 if (channel
->streams_sent_to_relayd
) {
759 ret
= consumer_send_relayd_streams_sent(
760 new_stream
->relayd_id
);
762 pthread_mutex_unlock(&channel
->lock
);
767 pthread_mutex_unlock(&channel
->lock
);
769 /* Get the right pipe where the stream will be sent. */
770 if (new_stream
->metadata_flag
) {
771 ret
= consumer_add_metadata_stream(new_stream
);
773 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
775 consumer_stream_free(new_stream
);
778 stream_pipe
= ctx
->consumer_metadata_pipe
;
780 ret
= consumer_add_data_stream(new_stream
);
782 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
784 consumer_stream_free(new_stream
);
787 stream_pipe
= ctx
->consumer_data_pipe
;
790 /* Vitible to other threads */
791 new_stream
->globally_visible
= 1;
793 health_code_update();
795 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
797 ERR("Consumer write %s stream to pipe %d",
798 new_stream
->metadata_flag
? "metadata" : "data",
799 lttng_pipe_get_writefd(stream_pipe
));
800 if (new_stream
->metadata_flag
) {
801 consumer_del_stream_for_metadata(new_stream
);
803 consumer_del_stream_for_data(new_stream
);
808 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
809 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
812 case LTTNG_CONSUMER_STREAMS_SENT
:
814 struct lttng_consumer_channel
*channel
;
817 * Get stream's channel reference. Needed when adding the stream to the
820 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
823 * We could not find the channel. Can happen if cpu hotplug
824 * happens while tearing down.
826 ERR("Unable to find channel key %" PRIu64
,
827 msg
.u
.sent_streams
.channel_key
);
828 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
831 health_code_update();
834 * Send status code to session daemon.
836 ret
= consumer_send_status_msg(sock
, ret_code
);
837 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
838 /* Somehow, the session daemon is not responding anymore. */
842 health_code_update();
845 * We should not send this message if we don't monitor the
846 * streams in this channel.
848 if (!channel
->monitor
) {
852 health_code_update();
853 /* Send stream to relayd if the stream has an ID. */
854 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
855 ret
= consumer_send_relayd_streams_sent(
856 msg
.u
.sent_streams
.net_seq_idx
);
860 channel
->streams_sent_to_relayd
= true;
864 case LTTNG_CONSUMER_UPDATE_STREAM
:
869 case LTTNG_CONSUMER_DESTROY_RELAYD
:
871 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
872 struct consumer_relayd_sock_pair
*relayd
;
874 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
876 /* Get relayd reference if exists. */
877 relayd
= consumer_find_relayd(index
);
878 if (relayd
== NULL
) {
879 DBG("Unable to find relayd %" PRIu64
, index
);
880 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
884 * Each relayd socket pair has a refcount of stream attached to it
885 * which tells if the relayd is still active or not depending on the
888 * This will set the destroy flag of the relayd object and destroy it
889 * if the refcount reaches zero when called.
891 * The destroy can happen either here or when a stream fd hangs up.
894 consumer_flag_relayd_for_destroy(relayd
);
897 health_code_update();
899 ret
= consumer_send_status_msg(sock
, ret_code
);
901 /* Somehow, the session daemon is not responding anymore. */
907 case LTTNG_CONSUMER_DATA_PENDING
:
910 uint64_t id
= msg
.u
.data_pending
.session_id
;
912 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
914 ret
= consumer_data_pending(id
);
916 health_code_update();
918 /* Send back returned value to session daemon */
919 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
921 PERROR("send data pending ret code");
926 * No need to send back a status message since the data pending
927 * returned value is the response.
931 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
933 if (msg
.u
.snapshot_channel
.metadata
== 1) {
934 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
935 msg
.u
.snapshot_channel
.pathname
,
936 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
938 ERR("Snapshot metadata failed");
939 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
942 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
943 msg
.u
.snapshot_channel
.pathname
,
944 msg
.u
.snapshot_channel
.relayd_id
,
945 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
948 ERR("Snapshot channel failed");
949 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
953 health_code_update();
955 ret
= consumer_send_status_msg(sock
, ret_code
);
957 /* Somehow, the session daemon is not responding anymore. */
962 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
964 uint64_t key
= msg
.u
.destroy_channel
.key
;
965 struct lttng_consumer_channel
*channel
;
967 channel
= consumer_find_channel(key
);
969 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
970 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
973 health_code_update();
975 ret
= consumer_send_status_msg(sock
, ret_code
);
977 /* Somehow, the session daemon is not responding anymore. */
981 health_code_update();
983 /* Stop right now if no channel was found. */
989 * This command should ONLY be issued for channel with streams set in
992 assert(!channel
->monitor
);
995 * The refcount should ALWAYS be 0 in the case of a channel in no
998 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1000 consumer_del_channel(channel
);
1004 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1007 struct lttng_consumer_channel
*channel
;
1008 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1009 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1011 DBG("Kernel consumer discarded events command for session id %"
1012 PRIu64
", channel key %" PRIu64
, id
, key
);
1014 channel
= consumer_find_channel(key
);
1016 ERR("Kernel consumer discarded events channel %"
1017 PRIu64
" not found", key
);
1020 ret
= channel
->discarded_events
;
1023 health_code_update();
1025 /* Send back returned value to session daemon */
1026 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1028 PERROR("send discarded events");
1034 case LTTNG_CONSUMER_LOST_PACKETS
:
1037 struct lttng_consumer_channel
*channel
;
1038 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1039 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1041 DBG("Kernel consumer lost packets command for session id %"
1042 PRIu64
", channel key %" PRIu64
, id
, key
);
1044 channel
= consumer_find_channel(key
);
1046 ERR("Kernel consumer lost packets channel %"
1047 PRIu64
" not found", key
);
1050 ret
= channel
->lost_packets
;
1053 health_code_update();
1055 /* Send back returned value to session daemon */
1056 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1058 PERROR("send lost packets");
1072 * Return 1 to indicate success since the 0 value can be a socket
1073 * shutdown during the recv() or send() call.
1075 health_code_update();
1080 /* This will issue a consumer stop. */
1085 * Populate index values of a kernel stream. Values are set in big endian order.
1087 * Return 0 on success or else a negative value.
1089 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1093 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1095 PERROR("kernctl_get_timestamp_begin");
1098 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1100 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1102 PERROR("kernctl_get_timestamp_end");
1105 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1107 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1109 PERROR("kernctl_get_events_discarded");
1112 index
->events_discarded
= htobe64(index
->events_discarded
);
1114 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1116 PERROR("kernctl_get_content_size");
1119 index
->content_size
= htobe64(index
->content_size
);
1121 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1123 PERROR("kernctl_get_packet_size");
1126 index
->packet_size
= htobe64(index
->packet_size
);
1128 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1130 PERROR("kernctl_get_stream_id");
1133 index
->stream_id
= htobe64(index
->stream_id
);
1135 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1137 if (ret
== -ENOTTY
) {
1138 /* Command not implemented by lttng-modules. */
1139 index
->stream_instance_id
= -1ULL;
1142 PERROR("kernctl_get_instance_id");
1146 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1148 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1150 if (ret
== -ENOTTY
) {
1151 /* Command not implemented by lttng-modules. */
1152 index
->packet_seq_num
= -1ULL;
1155 PERROR("kernctl_get_sequence_number");
1159 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1165 * Sync metadata meaning request them to the session daemon and snapshot to the
1166 * metadata thread can consumer them.
1168 * Metadata stream lock MUST be acquired.
1170 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1171 * is empty or a negative value on error.
1173 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1179 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1181 ERR("Failed to flush kernel stream");
1185 ret
= kernctl_snapshot(metadata
->wait_fd
);
1187 if (ret
!= -EAGAIN
) {
1188 ERR("Sync metadata, taking kernel snapshot failed.");
1191 DBG("Sync metadata, no new kernel metadata");
1192 /* No new metadata, exit. */
1202 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1205 uint64_t seq
, discarded
;
1207 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1209 if (ret
== -ENOTTY
) {
1210 /* Command not implemented by lttng-modules. */
1214 PERROR("kernctl_get_sequence_number");
1220 * Start the sequence when we extract the first packet in case we don't
1221 * start at 0 (for example if a consumer is not connected to the
1222 * session immediately after the beginning).
1224 if (stream
->last_sequence_number
== -1ULL) {
1225 stream
->last_sequence_number
= seq
;
1226 } else if (seq
> stream
->last_sequence_number
) {
1227 stream
->chan
->lost_packets
+= seq
-
1228 stream
->last_sequence_number
- 1;
1230 /* seq <= last_sequence_number */
1231 ERR("Sequence number inconsistent : prev = %" PRIu64
1232 ", current = %" PRIu64
,
1233 stream
->last_sequence_number
, seq
);
1237 stream
->last_sequence_number
= seq
;
1239 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1241 PERROR("kernctl_get_events_discarded");
1244 if (discarded
< stream
->last_discarded_events
) {
1246 * Overflow has occurred. We assume only one wrap-around
1249 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1250 stream
->last_discarded_events
+ discarded
;
1252 stream
->chan
->discarded_events
+= discarded
-
1253 stream
->last_discarded_events
;
1255 stream
->last_discarded_events
= discarded
;
1263 * Check if the local version of the metadata stream matches with the version
1264 * of the metadata stream in the kernel. If it was updated, set the reset flag
1268 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1271 uint64_t cur_version
;
1273 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1275 if (ret
== -ENOTTY
) {
1277 * LTTng-modules does not implement this
1283 ERR("Failed to get the metadata version");
1287 if (stream
->metadata_version
== cur_version
) {
1292 DBG("New metadata version detected");
1293 stream
->metadata_version
= cur_version
;
1294 stream
->reset_metadata_flag
= 1;
1302 * Consume data on a file descriptor and write it on a trace file.
1304 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1305 struct lttng_consumer_local_data
*ctx
)
1307 unsigned long len
, subbuf_size
, padding
;
1308 int err
, write_index
= 1;
1310 int infd
= stream
->wait_fd
;
1311 struct ctf_packet_index index
;
1313 DBG("In read_subbuffer (infd : %d)", infd
);
1315 /* Get the next subbuffer */
1316 err
= kernctl_get_next_subbuf(infd
);
1319 * This is a debug message even for single-threaded consumer,
1320 * because poll() have more relaxed criterions than get subbuf,
1321 * so get_subbuf may fail for short race windows where poll()
1322 * would issue wakeups.
1324 DBG("Reserving sub buffer failed (everything is normal, "
1325 "it is due to concurrency)");
1330 /* Get the full subbuffer size including padding */
1331 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1333 PERROR("Getting sub-buffer len failed.");
1334 err
= kernctl_put_subbuf(infd
);
1336 if (err
== -EFAULT
) {
1337 PERROR("Error in unreserving sub buffer\n");
1338 } else if (err
== -EIO
) {
1339 /* Should never happen with newer LTTng versions */
1340 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1349 if (!stream
->metadata_flag
) {
1350 ret
= get_index_values(&index
, infd
);
1352 err
= kernctl_put_subbuf(infd
);
1354 if (err
== -EFAULT
) {
1355 PERROR("Error in unreserving sub buffer\n");
1356 } else if (err
== -EIO
) {
1357 /* Should never happen with newer LTTng versions */
1358 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1365 ret
= update_stream_stats(stream
);
1367 err
= kernctl_put_subbuf(infd
);
1369 if (err
== -EFAULT
) {
1370 PERROR("Error in unreserving sub buffer\n");
1371 } else if (err
== -EIO
) {
1372 /* Should never happen with newer LTTng versions */
1373 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1382 ret
= metadata_stream_check_version(infd
, stream
);
1384 err
= kernctl_put_subbuf(infd
);
1386 if (err
== -EFAULT
) {
1387 PERROR("Error in unreserving sub buffer\n");
1388 } else if (err
== -EIO
) {
1389 /* Should never happen with newer LTTng versions */
1390 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1399 switch (stream
->chan
->output
) {
1400 case CONSUMER_CHANNEL_SPLICE
:
1402 * XXX: The lttng-modules splice "actor" does not handle copying
1403 * partial pages hence only using the subbuffer size without the
1404 * padding makes the splice fail.
1409 /* splice the subbuffer to the tracefile */
1410 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1413 * XXX: Splice does not support network streaming so the return value
1414 * is simply checked against subbuf_size and not like the mmap() op.
1416 if (ret
!= subbuf_size
) {
1418 * display the error but continue processing to try
1419 * to release the subbuffer
1421 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1426 case CONSUMER_CHANNEL_MMAP
:
1428 const char *subbuf_addr
;
1429 struct lttng_buffer_view subbuf_view
;
1431 /* Get subbuffer size without padding */
1432 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1434 PERROR("Getting sub-buffer len failed.");
1435 err
= kernctl_put_subbuf(infd
);
1437 if (err
== -EFAULT
) {
1438 PERROR("Error in unreserving sub buffer\n");
1439 } else if (err
== -EIO
) {
1440 /* Should never happen with newer LTTng versions */
1441 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1450 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
1452 goto error_put_subbuf
;
1455 /* Make sure the tracer is not gone mad on us! */
1456 assert(len
>= subbuf_size
);
1458 padding
= len
- subbuf_size
;
1460 subbuf_view
= lttng_buffer_view_init(subbuf_addr
, 0, len
);
1462 /* write the subbuffer to the tracefile */
1463 ret
= lttng_consumer_on_read_subbuffer_mmap(
1464 ctx
, stream
, &subbuf_view
, padding
, &index
);
1466 * The mmap operation should write subbuf_size amount of data
1467 * when network streaming or the full padding (len) size when we
1468 * are _not_ streaming.
1470 if ((ret
!= subbuf_size
&& stream
->relayd_id
!= (uint64_t) -1ULL) ||
1471 (ret
!= len
&& stream
->relayd_id
== (uint64_t) -1ULL)) {
1473 * Display the error but continue processing to try to release the
1474 * subbuffer. This is a DBG statement since this is possible to
1475 * happen without being a critical error.
1477 DBG("Error writing to tracefile "
1478 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1479 ret
, len
, subbuf_size
);
1485 ERR("Unknown output method");
1489 err
= kernctl_put_next_subbuf(infd
);
1491 if (err
== -EFAULT
) {
1492 PERROR("Error in unreserving sub buffer\n");
1493 } else if (err
== -EIO
) {
1494 /* Should never happen with newer LTTng versions */
1495 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1501 /* Write index if needed. */
1506 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1508 * In live, block until all the metadata is sent.
1510 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1511 assert(!stream
->missed_metadata_flush
);
1512 stream
->waiting_on_metadata
= true;
1513 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1515 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1517 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1518 stream
->waiting_on_metadata
= false;
1519 if (stream
->missed_metadata_flush
) {
1520 stream
->missed_metadata_flush
= false;
1521 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1522 (void) consumer_flush_kernel_index(stream
);
1524 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1531 err
= consumer_stream_write_index(stream
, &index
);
1540 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1547 * Don't create anything if this is set for streaming or should not be
1550 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1551 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1552 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1553 stream
->uid
, stream
->gid
, NULL
);
1557 stream
->out_fd
= ret
;
1558 stream
->tracefile_size_current
= 0;
1560 if (!stream
->metadata_flag
) {
1561 struct lttng_index_file
*index_file
;
1563 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1564 stream
->name
, stream
->uid
, stream
->gid
,
1565 stream
->chan
->tracefile_size
,
1566 stream
->tracefile_count_current
,
1567 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1571 stream
->index_file
= index_file
;
1575 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1576 /* get the len of the mmap region */
1577 unsigned long mmap_len
;
1579 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1581 PERROR("kernctl_get_mmap_len");
1582 goto error_close_fd
;
1584 stream
->mmap_len
= (size_t) mmap_len
;
1586 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1587 MAP_PRIVATE
, stream
->wait_fd
, 0);
1588 if (stream
->mmap_base
== MAP_FAILED
) {
1589 PERROR("Error mmaping");
1591 goto error_close_fd
;
1595 /* we return 0 to let the library handle the FD internally */
1599 if (stream
->out_fd
>= 0) {
1602 err
= close(stream
->out_fd
);
1604 stream
->out_fd
= -1;
1611 * Check if data is still being extracted from the buffers for a specific
1612 * stream. Consumer data lock MUST be acquired before calling this function
1613 * and the stream lock.
1615 * Return 1 if the traced data are still getting read else 0 meaning that the
1616 * data is available for trace viewer reading.
1618 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1624 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1629 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1631 /* There is still data so let's put back this subbuffer. */
1632 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1634 ret
= 1; /* Data is pending */
1638 /* Data is NOT pending and ready to be read. */