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>
45 #include <common/optional.h>
46 #include <common/buffer-view.h>
47 #include <common/consumer/consumer.h>
50 #include "kernel-consumer.h"
52 extern struct lttng_consumer_global_data consumer_data
;
53 extern int consumer_poll_timeout
;
54 extern volatile int consumer_quit
;
57 * Take a snapshot for a specific fd
59 * Returns 0 on success, < 0 on error
61 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
64 int infd
= stream
->wait_fd
;
66 ret
= kernctl_snapshot(infd
);
68 * -EAGAIN is not an error, it just means that there is no data to
71 if (ret
!= 0 && ret
!= -EAGAIN
) {
72 PERROR("Getting sub-buffer snapshot.");
79 * Get the produced position
81 * Returns 0 on success, < 0 on error
83 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
87 int infd
= stream
->wait_fd
;
89 ret
= kernctl_snapshot_get_produced(infd
, pos
);
91 PERROR("kernctl_snapshot_get_produced");
98 * Get the consumerd position
100 * Returns 0 on success, < 0 on error
102 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
106 int infd
= stream
->wait_fd
;
108 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
110 PERROR("kernctl_snapshot_get_consumed");
117 int get_current_subbuf_addr(struct lttng_consumer_stream
*stream
,
121 unsigned long mmap_offset
;
122 const char *mmap_base
= stream
->mmap_base
;
124 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
126 PERROR("Failed to get mmap read offset");
130 *addr
= mmap_base
+ mmap_offset
;
136 * Take a snapshot of all the stream of a channel
138 * Returns 0 on success, < 0 on error
140 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
141 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
142 struct lttng_consumer_local_data
*ctx
)
145 struct lttng_consumer_channel
*channel
;
146 struct lttng_consumer_stream
*stream
;
148 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
152 channel
= consumer_find_channel(key
);
154 ERR("No channel found for key %" PRIu64
, key
);
159 /* Splice is not supported yet for channel snapshot. */
160 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
161 ERR("Unsupported output %d", channel
->output
);
166 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
167 unsigned long consumed_pos
, produced_pos
;
169 health_code_update();
172 * Lock stream because we are about to change its state.
174 pthread_mutex_lock(&stream
->lock
);
177 * Assign the received relayd ID so we can use it for streaming. The streams
178 * are not visible to anyone so this is OK to change it.
180 stream
->relayd_id
= relayd_id
;
181 channel
->relayd_id
= relayd_id
;
182 if (relayd_id
!= (uint64_t) -1ULL) {
183 ret
= consumer_send_relayd_stream(stream
, path
);
185 ERR("sending stream to relayd");
189 ret
= utils_create_stream_file(path
, stream
->name
,
190 stream
->chan
->tracefile_size
,
191 stream
->tracefile_count_current
,
192 stream
->uid
, stream
->gid
, NULL
);
194 ERR("utils_create_stream_file");
198 stream
->out_fd
= ret
;
199 stream
->tracefile_size_current
= 0;
201 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
202 path
, stream
->name
, stream
->key
);
204 if (relayd_id
!= -1ULL) {
205 ret
= consumer_send_relayd_streams_sent(relayd_id
);
207 ERR("sending streams sent to relayd");
210 channel
->streams_sent_to_relayd
= true;
213 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
216 * Doing a buffer flush which does not take into
217 * account empty packets. This is not perfect
218 * for stream intersection, but required as a
219 * fall-back when "flush_empty" is not
220 * implemented by lttng-modules.
222 ret
= kernctl_buffer_flush(stream
->wait_fd
);
224 ERR("Failed to flush kernel stream");
230 ret
= lttng_kconsumer_take_snapshot(stream
);
232 ERR("Taking kernel snapshot");
236 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
238 ERR("Produced kernel snapshot position");
242 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
244 ERR("Consumerd kernel snapshot position");
248 if (stream
->max_sb_size
== 0) {
249 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
250 &stream
->max_sb_size
);
252 ERR("Getting kernel max_sb_size");
257 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
258 produced_pos
, nb_packets_per_stream
,
259 stream
->max_sb_size
);
261 while (consumed_pos
< produced_pos
) {
263 unsigned long len
, padded_len
;
264 const char *subbuf_addr
;
265 struct lttng_buffer_view subbuf_view
;
267 health_code_update();
268 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
270 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
272 if (ret
!= -EAGAIN
) {
273 PERROR("kernctl_get_subbuf snapshot");
276 DBG("Kernel consumer get subbuf failed. Skipping it.");
277 consumed_pos
+= stream
->max_sb_size
;
278 stream
->chan
->lost_packets
++;
282 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
284 ERR("Snapshot kernctl_get_subbuf_size");
285 goto error_put_subbuf
;
288 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
290 ERR("Snapshot kernctl_get_padded_subbuf_size");
291 goto error_put_subbuf
;
294 ret
= get_current_subbuf_addr(stream
, &subbuf_addr
);
296 goto error_put_subbuf
;
299 subbuf_view
= lttng_buffer_view_init(
300 subbuf_addr
, 0, padded_len
);
301 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
,
302 stream
, &subbuf_view
,
305 * We write the padded len in local tracefiles but the data len
306 * when using a relay. Display the error but continue processing
307 * to try to release the subbuffer.
309 if (relayd_id
!= (uint64_t) -1ULL) {
310 if (read_len
!= len
) {
311 ERR("Error sending to the relay (ret: %zd != len: %lu)",
315 if (read_len
!= padded_len
) {
316 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
317 read_len
, padded_len
);
321 ret
= kernctl_put_subbuf(stream
->wait_fd
);
323 ERR("Snapshot kernctl_put_subbuf");
326 consumed_pos
+= stream
->max_sb_size
;
329 if (relayd_id
== (uint64_t) -1ULL) {
330 if (stream
->out_fd
>= 0) {
331 ret
= close(stream
->out_fd
);
333 PERROR("Kernel consumer snapshot close out_fd");
339 close_relayd_stream(stream
);
340 stream
->relayd_id
= (uint64_t) -1ULL;
342 pthread_mutex_unlock(&stream
->lock
);
350 ret
= kernctl_put_subbuf(stream
->wait_fd
);
352 ERR("Snapshot kernctl_put_subbuf error path");
355 pthread_mutex_unlock(&stream
->lock
);
362 * Read the whole metadata available for a snapshot.
364 * Returns 0 on success, < 0 on error
366 static int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
367 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
369 int ret
, use_relayd
= 0;
371 struct lttng_consumer_channel
*metadata_channel
;
372 struct lttng_consumer_stream
*metadata_stream
;
376 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
381 metadata_channel
= consumer_find_channel(key
);
382 if (!metadata_channel
) {
383 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
385 goto error_no_channel
;
388 metadata_stream
= metadata_channel
->metadata_stream
;
389 assert(metadata_stream
);
390 pthread_mutex_lock(&metadata_stream
->lock
);
392 /* Flag once that we have a valid relayd for the stream. */
393 if (relayd_id
!= (uint64_t) -1ULL) {
398 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
403 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
404 metadata_stream
->chan
->tracefile_size
,
405 metadata_stream
->tracefile_count_current
,
406 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
410 metadata_stream
->out_fd
= ret
;
414 health_code_update();
416 ret_read
= lttng_consumer_read_subbuffer(metadata_stream
, ctx
, true);
418 if (ret_read
!= -EAGAIN
) {
419 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
424 /* ret_read is negative at this point so we will exit the loop. */
427 } while (ret_read
>= 0);
430 close_relayd_stream(metadata_stream
);
431 metadata_stream
->relayd_id
= (uint64_t) -1ULL;
433 if (metadata_stream
->out_fd
>= 0) {
434 ret
= close(metadata_stream
->out_fd
);
436 PERROR("Kernel consumer snapshot metadata close out_fd");
438 * Don't go on error here since the snapshot was successful at this
439 * point but somehow the close failed.
442 metadata_stream
->out_fd
= -1;
448 pthread_mutex_unlock(&metadata_stream
->lock
);
449 cds_list_del(&metadata_stream
->send_node
);
450 consumer_stream_destroy(metadata_stream
, NULL
);
451 metadata_channel
->metadata_stream
= NULL
;
458 * Receive command from session daemon and process it.
460 * Return 1 on success else a negative value or 0.
462 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
463 int sock
, struct pollfd
*consumer_sockpoll
)
466 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
467 struct lttcomm_consumer_msg msg
;
469 health_code_update();
471 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
472 if (ret
!= sizeof(msg
)) {
474 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
480 health_code_update();
482 /* Deprecated command */
483 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
485 health_code_update();
487 /* relayd needs RCU read-side protection */
490 switch (msg
.cmd_type
) {
491 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
493 /* Session daemon status message are handled in the following call. */
494 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
495 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
496 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
497 msg
.u
.relayd_sock
.relayd_session_id
);
500 case LTTNG_CONSUMER_ADD_CHANNEL
:
502 struct lttng_consumer_channel
*new_channel
;
505 health_code_update();
507 /* First send a status message before receiving the fds. */
508 ret
= consumer_send_status_msg(sock
, ret_code
);
510 /* Somehow, the session daemon is not responding anymore. */
514 health_code_update();
516 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
517 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
518 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
519 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
520 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
521 msg
.u
.channel
.tracefile_size
,
522 msg
.u
.channel
.tracefile_count
, 0,
523 msg
.u
.channel
.monitor
,
524 msg
.u
.channel
.live_timer_interval
,
525 msg
.u
.channel
.is_live
,
527 if (new_channel
== NULL
) {
528 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
531 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
532 switch (msg
.u
.channel
.output
) {
533 case LTTNG_EVENT_SPLICE
:
534 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
536 case LTTNG_EVENT_MMAP
:
537 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
540 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
544 /* Translate and save channel type. */
545 switch (msg
.u
.channel
.type
) {
546 case CONSUMER_CHANNEL_TYPE_DATA
:
547 case CONSUMER_CHANNEL_TYPE_METADATA
:
548 new_channel
->type
= msg
.u
.channel
.type
;
555 health_code_update();
557 if (ctx
->on_recv_channel
!= NULL
) {
558 ret_recv
= ctx
->on_recv_channel(new_channel
);
560 ret
= consumer_add_channel(new_channel
, ctx
);
561 } else if (ret_recv
< 0) {
565 ret
= consumer_add_channel(new_channel
, ctx
);
567 if (CONSUMER_CHANNEL_TYPE_DATA
) {
568 consumer_timer_live_start(new_channel
,
569 msg
.u
.channel
.live_timer_interval
);
572 health_code_update();
574 /* If we received an error in add_channel, we need to report it. */
576 ret
= consumer_send_status_msg(sock
, ret
);
585 case LTTNG_CONSUMER_ADD_STREAM
:
588 struct lttng_pipe
*stream_pipe
;
589 struct lttng_consumer_stream
*new_stream
;
590 struct lttng_consumer_channel
*channel
;
594 * Get stream's channel reference. Needed when adding the stream to the
597 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
600 * We could not find the channel. Can happen if cpu hotplug
601 * happens while tearing down.
603 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
604 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
607 health_code_update();
609 /* First send a status message before receiving the fds. */
610 ret
= consumer_send_status_msg(sock
, ret_code
);
612 /* Somehow, the session daemon is not responding anymore. */
616 health_code_update();
618 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
619 /* Channel was not found. */
625 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
631 health_code_update();
633 /* Get stream file descriptor from socket */
634 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
635 if (ret
!= sizeof(fd
)) {
636 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
641 health_code_update();
644 * Send status code to session daemon only if the recv works. If the
645 * above recv() failed, the session daemon is notified through the
646 * error socket and the teardown is eventually done.
648 ret
= consumer_send_status_msg(sock
, ret_code
);
650 /* Somehow, the session daemon is not responding anymore. */
654 health_code_update();
656 pthread_mutex_lock(&channel
->lock
);
657 new_stream
= consumer_stream_create(
661 LTTNG_CONSUMER_ACTIVE_STREAM
,
671 if (new_stream
== NULL
) {
676 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
679 pthread_mutex_unlock(&channel
->lock
);
683 new_stream
->wait_fd
= fd
;
684 ret
= kernctl_get_max_subbuf_size(new_stream
->wait_fd
,
685 &new_stream
->max_sb_size
);
687 pthread_mutex_unlock(&channel
->lock
);
688 ERR("Failed to get kernel maximal subbuffer size");
693 * We've just assigned the channel to the stream so increment the
694 * refcount right now. We don't need to increment the refcount for
695 * streams in no monitor because we handle manually the cleanup of
696 * those. It is very important to make sure there is NO prior
697 * consumer_del_stream() calls or else the refcount will be unbalanced.
699 if (channel
->monitor
) {
700 uatomic_inc(&new_stream
->chan
->refcount
);
704 * The buffer flush is done on the session daemon side for the kernel
705 * so no need for the stream "hangup_flush_done" variable to be
706 * tracked. This is important for a kernel stream since we don't rely
707 * on the flush state of the stream to read data. It's not the case for
708 * user space tracing.
710 new_stream
->hangup_flush_done
= 0;
712 health_code_update();
714 if (ctx
->on_recv_stream
) {
715 ret
= ctx
->on_recv_stream(new_stream
);
717 consumer_stream_free(new_stream
);
722 health_code_update();
724 if (new_stream
->metadata_flag
) {
725 channel
->metadata_stream
= new_stream
;
728 /* Do not monitor this stream. */
729 if (!channel
->monitor
) {
730 DBG("Kernel consumer add stream %s in no monitor mode with "
731 "relayd id %" PRIu64
, new_stream
->name
,
732 new_stream
->relayd_id
);
733 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
734 pthread_mutex_unlock(&channel
->lock
);
738 /* Send stream to relayd if the stream has an ID. */
739 if (new_stream
->relayd_id
!= (uint64_t) -1ULL) {
740 ret
= consumer_send_relayd_stream(new_stream
,
741 new_stream
->chan
->pathname
);
743 pthread_mutex_unlock(&channel
->lock
);
744 consumer_stream_free(new_stream
);
749 * If adding an extra stream to an already
750 * existing channel (e.g. cpu hotplug), we need
751 * to send the "streams_sent" command to relayd.
753 if (channel
->streams_sent_to_relayd
) {
754 ret
= consumer_send_relayd_streams_sent(
755 new_stream
->relayd_id
);
757 pthread_mutex_unlock(&channel
->lock
);
762 pthread_mutex_unlock(&channel
->lock
);
764 /* Get the right pipe where the stream will be sent. */
765 if (new_stream
->metadata_flag
) {
766 ret
= consumer_add_metadata_stream(new_stream
);
768 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
770 consumer_stream_free(new_stream
);
773 stream_pipe
= ctx
->consumer_metadata_pipe
;
775 ret
= consumer_add_data_stream(new_stream
);
777 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
779 consumer_stream_free(new_stream
);
782 stream_pipe
= ctx
->consumer_data_pipe
;
785 /* Vitible to other threads */
786 new_stream
->globally_visible
= 1;
788 health_code_update();
790 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
792 ERR("Consumer write %s stream to pipe %d",
793 new_stream
->metadata_flag
? "metadata" : "data",
794 lttng_pipe_get_writefd(stream_pipe
));
795 if (new_stream
->metadata_flag
) {
796 consumer_del_stream_for_metadata(new_stream
);
798 consumer_del_stream_for_data(new_stream
);
803 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
804 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
807 case LTTNG_CONSUMER_STREAMS_SENT
:
809 struct lttng_consumer_channel
*channel
;
812 * Get stream's channel reference. Needed when adding the stream to the
815 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
818 * We could not find the channel. Can happen if cpu hotplug
819 * happens while tearing down.
821 ERR("Unable to find channel key %" PRIu64
,
822 msg
.u
.sent_streams
.channel_key
);
823 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
826 health_code_update();
829 * Send status code to session daemon.
831 ret
= consumer_send_status_msg(sock
, ret_code
);
832 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
833 /* Somehow, the session daemon is not responding anymore. */
837 health_code_update();
840 * We should not send this message if we don't monitor the
841 * streams in this channel.
843 if (!channel
->monitor
) {
847 health_code_update();
848 /* Send stream to relayd if the stream has an ID. */
849 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
850 ret
= consumer_send_relayd_streams_sent(
851 msg
.u
.sent_streams
.net_seq_idx
);
855 channel
->streams_sent_to_relayd
= true;
859 case LTTNG_CONSUMER_UPDATE_STREAM
:
864 case LTTNG_CONSUMER_DESTROY_RELAYD
:
866 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
867 struct consumer_relayd_sock_pair
*relayd
;
869 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
871 /* Get relayd reference if exists. */
872 relayd
= consumer_find_relayd(index
);
873 if (relayd
== NULL
) {
874 DBG("Unable to find relayd %" PRIu64
, index
);
875 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
879 * Each relayd socket pair has a refcount of stream attached to it
880 * which tells if the relayd is still active or not depending on the
883 * This will set the destroy flag of the relayd object and destroy it
884 * if the refcount reaches zero when called.
886 * The destroy can happen either here or when a stream fd hangs up.
889 consumer_flag_relayd_for_destroy(relayd
);
892 health_code_update();
894 ret
= consumer_send_status_msg(sock
, ret_code
);
896 /* Somehow, the session daemon is not responding anymore. */
902 case LTTNG_CONSUMER_DATA_PENDING
:
905 uint64_t id
= msg
.u
.data_pending
.session_id
;
907 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
909 ret
= consumer_data_pending(id
);
911 health_code_update();
913 /* Send back returned value to session daemon */
914 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
916 PERROR("send data pending ret code");
921 * No need to send back a status message since the data pending
922 * returned value is the response.
926 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
928 if (msg
.u
.snapshot_channel
.metadata
== 1) {
929 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
930 msg
.u
.snapshot_channel
.pathname
,
931 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
933 ERR("Snapshot metadata failed");
934 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
937 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
938 msg
.u
.snapshot_channel
.pathname
,
939 msg
.u
.snapshot_channel
.relayd_id
,
940 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
943 ERR("Snapshot channel failed");
944 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
948 health_code_update();
950 ret
= consumer_send_status_msg(sock
, ret_code
);
952 /* Somehow, the session daemon is not responding anymore. */
957 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
959 uint64_t key
= msg
.u
.destroy_channel
.key
;
960 struct lttng_consumer_channel
*channel
;
962 channel
= consumer_find_channel(key
);
964 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
965 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
968 health_code_update();
970 ret
= consumer_send_status_msg(sock
, ret_code
);
972 /* Somehow, the session daemon is not responding anymore. */
976 health_code_update();
978 /* Stop right now if no channel was found. */
984 * This command should ONLY be issued for channel with streams set in
987 assert(!channel
->monitor
);
990 * The refcount should ALWAYS be 0 in the case of a channel in no
993 assert(!uatomic_sub_return(&channel
->refcount
, 1));
995 consumer_del_channel(channel
);
999 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1002 struct lttng_consumer_channel
*channel
;
1003 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1004 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1006 DBG("Kernel consumer discarded events command for session id %"
1007 PRIu64
", channel key %" PRIu64
, id
, key
);
1009 channel
= consumer_find_channel(key
);
1011 ERR("Kernel consumer discarded events channel %"
1012 PRIu64
" not found", key
);
1015 ret
= channel
->discarded_events
;
1018 health_code_update();
1020 /* Send back returned value to session daemon */
1021 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1023 PERROR("send discarded events");
1029 case LTTNG_CONSUMER_LOST_PACKETS
:
1032 struct lttng_consumer_channel
*channel
;
1033 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1034 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1036 DBG("Kernel consumer lost packets command for session id %"
1037 PRIu64
", channel key %" PRIu64
, id
, key
);
1039 channel
= consumer_find_channel(key
);
1041 ERR("Kernel consumer lost packets channel %"
1042 PRIu64
" not found", key
);
1045 ret
= channel
->lost_packets
;
1048 health_code_update();
1050 /* Send back returned value to session daemon */
1051 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1053 PERROR("send lost packets");
1067 * Return 1 to indicate success since the 0 value can be a socket
1068 * shutdown during the recv() or send() call.
1070 health_code_update();
1075 /* This will issue a consumer stop. */
1080 * Sync metadata meaning request them to the session daemon and snapshot to the
1081 * metadata thread can consumer them.
1083 * Metadata stream lock MUST be acquired.
1085 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1086 * is empty or a negative value on error.
1088 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1094 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1096 ERR("Failed to flush kernel stream");
1100 ret
= kernctl_snapshot(metadata
->wait_fd
);
1102 if (ret
!= -EAGAIN
) {
1103 ERR("Sync metadata, taking kernel snapshot failed.");
1106 DBG("Sync metadata, no new kernel metadata");
1107 /* No new metadata, exit. */
1117 int extract_common_subbuffer_info(struct lttng_consumer_stream
*stream
,
1118 struct stream_subbuffer
*subbuf
)
1122 ret
= kernctl_get_subbuf_size(
1123 stream
->wait_fd
, &subbuf
->info
.data
.subbuf_size
);
1128 ret
= kernctl_get_padded_subbuf_size(
1129 stream
->wait_fd
, &subbuf
->info
.data
.padded_subbuf_size
);
1139 int extract_metadata_subbuffer_info(struct lttng_consumer_stream
*stream
,
1140 struct stream_subbuffer
*subbuf
)
1144 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1149 ret
= kernctl_get_metadata_version(
1150 stream
->wait_fd
, &subbuf
->info
.metadata
.version
);
1160 int extract_data_subbuffer_info(struct lttng_consumer_stream
*stream
,
1161 struct stream_subbuffer
*subbuf
)
1165 ret
= extract_common_subbuffer_info(stream
, subbuf
);
1170 ret
= kernctl_get_packet_size(
1171 stream
->wait_fd
, &subbuf
->info
.data
.packet_size
);
1173 PERROR("Failed to get sub-buffer packet size");
1177 ret
= kernctl_get_content_size(
1178 stream
->wait_fd
, &subbuf
->info
.data
.content_size
);
1180 PERROR("Failed to get sub-buffer content size");
1184 ret
= kernctl_get_timestamp_begin(
1185 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_begin
);
1187 PERROR("Failed to get sub-buffer begin timestamp");
1191 ret
= kernctl_get_timestamp_end(
1192 stream
->wait_fd
, &subbuf
->info
.data
.timestamp_end
);
1194 PERROR("Failed to get sub-buffer end timestamp");
1198 ret
= kernctl_get_events_discarded(
1199 stream
->wait_fd
, &subbuf
->info
.data
.events_discarded
);
1201 PERROR("Failed to get sub-buffer events discarded count");
1205 ret
= kernctl_get_sequence_number(stream
->wait_fd
,
1206 &subbuf
->info
.data
.sequence_number
.value
);
1208 /* May not be supported by older LTTng-modules. */
1209 if (ret
!= -ENOTTY
) {
1210 PERROR("Failed to get sub-buffer sequence number");
1214 subbuf
->info
.data
.sequence_number
.is_set
= true;
1217 ret
= kernctl_get_stream_id(
1218 stream
->wait_fd
, &subbuf
->info
.data
.stream_id
);
1220 PERROR("Failed to get stream id");
1224 ret
= kernctl_get_instance_id(stream
->wait_fd
,
1225 &subbuf
->info
.data
.stream_instance_id
.value
);
1227 /* May not be supported by older LTTng-modules. */
1228 if (ret
!= -ENOTTY
) {
1229 PERROR("Failed to get stream instance id");
1233 subbuf
->info
.data
.stream_instance_id
.is_set
= true;
1240 int get_subbuffer_common(struct lttng_consumer_stream
*stream
,
1241 struct stream_subbuffer
*subbuffer
)
1245 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1250 ret
= stream
->read_subbuffer_ops
.extract_subbuffer_info(
1257 int get_next_subbuffer_splice(struct lttng_consumer_stream
*stream
,
1258 struct stream_subbuffer
*subbuffer
)
1262 ret
= get_subbuffer_common(stream
, subbuffer
);
1267 subbuffer
->buffer
.fd
= stream
->wait_fd
;
1273 int get_next_subbuffer_mmap(struct lttng_consumer_stream
*stream
,
1274 struct stream_subbuffer
*subbuffer
)
1279 ret
= get_subbuffer_common(stream
, subbuffer
);
1284 ret
= get_current_subbuf_addr(stream
, &addr
);
1289 subbuffer
->buffer
.buffer
= lttng_buffer_view_init(
1290 addr
, 0, subbuffer
->info
.data
.padded_subbuf_size
);
1296 int put_next_subbuffer(struct lttng_consumer_stream
*stream
,
1297 struct stream_subbuffer
*subbuffer
)
1299 const int ret
= kernctl_put_next_subbuf(stream
->wait_fd
);
1302 if (ret
== -EFAULT
) {
1303 PERROR("Error in unreserving sub buffer");
1304 } else if (ret
== -EIO
) {
1305 /* Should never happen with newer LTTng versions */
1306 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted");
1313 static void lttng_kconsumer_set_stream_ops(
1314 struct lttng_consumer_stream
*stream
)
1316 if (stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
) {
1317 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1318 get_next_subbuffer_mmap
;
1320 stream
->read_subbuffer_ops
.get_next_subbuffer
=
1321 get_next_subbuffer_splice
;
1324 if (stream
->metadata_flag
) {
1325 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1326 extract_metadata_subbuffer_info
;
1328 stream
->read_subbuffer_ops
.extract_subbuffer_info
=
1329 extract_data_subbuffer_info
;
1330 if (stream
->chan
->is_live
) {
1331 stream
->read_subbuffer_ops
.send_live_beacon
=
1332 consumer_flush_kernel_index
;
1336 stream
->read_subbuffer_ops
.put_next_subbuffer
= put_next_subbuffer
;
1339 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1346 * Don't create anything if this is set for streaming or should not be
1349 if (stream
->relayd_id
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1350 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1351 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1352 stream
->uid
, stream
->gid
, NULL
);
1356 stream
->out_fd
= ret
;
1357 stream
->tracefile_size_current
= 0;
1359 if (!stream
->metadata_flag
) {
1360 struct lttng_index_file
*index_file
;
1362 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1363 stream
->name
, stream
->uid
, stream
->gid
,
1364 stream
->chan
->tracefile_size
,
1365 stream
->tracefile_count_current
,
1366 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1370 stream
->index_file
= index_file
;
1374 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1375 /* get the len of the mmap region */
1376 unsigned long mmap_len
;
1378 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1380 PERROR("kernctl_get_mmap_len");
1381 goto error_close_fd
;
1383 stream
->mmap_len
= (size_t) mmap_len
;
1385 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1386 MAP_PRIVATE
, stream
->wait_fd
, 0);
1387 if (stream
->mmap_base
== MAP_FAILED
) {
1388 PERROR("Error mmaping");
1390 goto error_close_fd
;
1394 lttng_kconsumer_set_stream_ops(stream
);
1396 /* we return 0 to let the library handle the FD internally */
1400 if (stream
->out_fd
>= 0) {
1403 err
= close(stream
->out_fd
);
1405 stream
->out_fd
= -1;
1412 * Check if data is still being extracted from the buffers for a specific
1413 * stream. Consumer data lock MUST be acquired before calling this function
1414 * and the stream lock.
1416 * Return 1 if the traced data are still getting read else 0 meaning that the
1417 * data is available for trace viewer reading.
1419 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1425 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1430 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1432 /* There is still data so let's put back this subbuffer. */
1433 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1435 ret
= 1; /* Data is pending */
1439 /* Data is NOT pending and ready to be read. */