2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
46 #include "kernel-consumer.h"
48 extern struct lttng_consumer_global_data consumer_data
;
49 extern int consumer_poll_timeout
;
50 extern volatile int consumer_quit
;
53 * Take a snapshot for a specific fd
55 * Returns 0 on success, < 0 on error
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
60 int infd
= stream
->wait_fd
;
62 ret
= kernctl_snapshot(infd
);
64 PERROR("Getting sub-buffer snapshot.");
72 * Get the produced position
74 * Returns 0 on success, < 0 on error
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
80 int infd
= stream
->wait_fd
;
82 ret
= kernctl_snapshot_get_produced(infd
, pos
);
84 PERROR("kernctl_snapshot_get_produced");
92 * Get the consumerd position
94 * Returns 0 on success, < 0 on error
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
100 int infd
= stream
->wait_fd
;
102 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
104 PERROR("kernctl_snapshot_get_consumed");
112 * Take a snapshot of all the stream of a channel
114 * Returns 0 on success, < 0 on error
116 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
117 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
118 struct lttng_consumer_local_data
*ctx
)
121 unsigned long consumed_pos
, produced_pos
;
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 /* Are we at a position _before_ the first available packet ? */
145 bool before_first_packet
= true;
147 health_code_update();
150 * Lock stream because we are about to change its state.
152 pthread_mutex_lock(&stream
->lock
);
155 * Assign the received relayd ID so we can use it for streaming. The streams
156 * are not visible to anyone so this is OK to change it.
158 stream
->net_seq_idx
= relayd_id
;
159 channel
->relayd_id
= relayd_id
;
160 if (relayd_id
!= (uint64_t) -1ULL) {
161 ret
= consumer_send_relayd_stream(stream
, path
);
163 ERR("sending stream to relayd");
167 ret
= utils_create_stream_file(path
, stream
->name
,
168 stream
->chan
->tracefile_size
,
169 stream
->tracefile_count_current
,
170 stream
->uid
, stream
->gid
, NULL
);
172 ERR("utils_create_stream_file");
176 stream
->out_fd
= ret
;
177 stream
->tracefile_size_current
= 0;
179 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
180 path
, stream
->name
, stream
->key
);
182 if (relayd_id
!= -1ULL) {
183 ret
= consumer_send_relayd_streams_sent(relayd_id
);
185 ERR("sending streams sent to relayd");
190 ret
= kernctl_buffer_flush(stream
->wait_fd
);
192 ERR("Failed to flush kernel stream");
197 ret
= lttng_kconsumer_take_snapshot(stream
);
199 ERR("Taking kernel snapshot");
203 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
205 ERR("Produced kernel snapshot position");
209 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
211 ERR("Consumerd kernel snapshot position");
215 if (stream
->max_sb_size
== 0) {
216 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
217 &stream
->max_sb_size
);
219 ERR("Getting kernel max_sb_size");
225 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
226 produced_pos
, nb_packets_per_stream
,
227 stream
->max_sb_size
);
229 while (consumed_pos
< produced_pos
) {
231 unsigned long len
, padded_len
;
234 health_code_update();
236 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
238 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
240 if (errno
!= EAGAIN
) {
241 PERROR("kernctl_get_subbuf snapshot");
245 DBG("Kernel consumer get subbuf failed. Skipping it.");
246 consumed_pos
+= stream
->max_sb_size
;
249 * Start accounting lost packets only when we
250 * already have extracted packets (to match the
251 * content of the final snapshot).
253 if (!before_first_packet
) {
259 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
261 ERR("Snapshot kernctl_get_subbuf_size");
263 goto error_put_subbuf
;
266 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
268 ERR("Snapshot kernctl_get_padded_subbuf_size");
270 goto error_put_subbuf
;
273 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
274 padded_len
- len
, NULL
);
276 * We write the padded len in local tracefiles but the data len
277 * when using a relay. Display the error but continue processing
278 * to try to release the subbuffer.
280 if (relayd_id
!= (uint64_t) -1ULL) {
281 if (read_len
!= len
) {
282 ERR("Error sending to the relay (ret: %zd != len: %lu)",
286 if (read_len
!= padded_len
) {
287 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
288 read_len
, padded_len
);
292 ret
= kernctl_put_subbuf(stream
->wait_fd
);
294 ERR("Snapshot kernctl_put_subbuf");
298 consumed_pos
+= stream
->max_sb_size
;
301 * Only account lost packets located between
302 * succesfully extracted packets (do not account before
303 * and after since they are not visible in the
304 * resulting snapshot).
306 stream
->chan
->lost_packets
+= lost_packet
;
308 before_first_packet
= false;
311 if (relayd_id
== (uint64_t) -1ULL) {
312 if (stream
->out_fd
>= 0) {
313 ret
= close(stream
->out_fd
);
315 PERROR("Kernel consumer snapshot close out_fd");
321 close_relayd_stream(stream
);
322 stream
->net_seq_idx
= (uint64_t) -1ULL;
324 pthread_mutex_unlock(&stream
->lock
);
332 ret
= kernctl_put_subbuf(stream
->wait_fd
);
335 ERR("Snapshot kernctl_put_subbuf error path");
338 pthread_mutex_unlock(&stream
->lock
);
345 * Read the whole metadata available for a snapshot.
347 * Returns 0 on success, < 0 on error
349 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
350 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
352 int ret
, use_relayd
= 0;
354 struct lttng_consumer_channel
*metadata_channel
;
355 struct lttng_consumer_stream
*metadata_stream
;
359 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
364 metadata_channel
= consumer_find_channel(key
);
365 if (!metadata_channel
) {
366 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
371 metadata_stream
= metadata_channel
->metadata_stream
;
372 assert(metadata_stream
);
374 /* Flag once that we have a valid relayd for the stream. */
375 if (relayd_id
!= (uint64_t) -1ULL) {
380 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
385 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
386 metadata_stream
->chan
->tracefile_size
,
387 metadata_stream
->tracefile_count_current
,
388 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
392 metadata_stream
->out_fd
= ret
;
396 health_code_update();
398 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
400 if (ret_read
!= -EAGAIN
) {
401 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
405 /* ret_read is negative at this point so we will exit the loop. */
408 } while (ret_read
>= 0);
411 close_relayd_stream(metadata_stream
);
412 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
414 if (metadata_stream
->out_fd
>= 0) {
415 ret
= close(metadata_stream
->out_fd
);
417 PERROR("Kernel consumer snapshot metadata close out_fd");
419 * Don't go on error here since the snapshot was successful at this
420 * point but somehow the close failed.
423 metadata_stream
->out_fd
= -1;
429 cds_list_del(&metadata_stream
->send_node
);
430 consumer_stream_destroy(metadata_stream
, NULL
);
431 metadata_channel
->metadata_stream
= NULL
;
438 * Receive command from session daemon and process it.
440 * Return 1 on success else a negative value or 0.
442 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
443 int sock
, struct pollfd
*consumer_sockpoll
)
446 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
447 struct lttcomm_consumer_msg msg
;
449 health_code_update();
451 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
452 if (ret
!= sizeof(msg
)) {
454 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
460 health_code_update();
462 /* Deprecated command */
463 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
465 health_code_update();
467 /* relayd needs RCU read-side protection */
470 switch (msg
.cmd_type
) {
471 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
473 /* Session daemon status message are handled in the following call. */
474 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
475 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
476 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
477 msg
.u
.relayd_sock
.relayd_session_id
);
480 case LTTNG_CONSUMER_ADD_CHANNEL
:
482 struct lttng_consumer_channel
*new_channel
;
485 health_code_update();
487 /* First send a status message before receiving the fds. */
488 ret
= consumer_send_status_msg(sock
, ret_code
);
490 /* Somehow, the session daemon is not responding anymore. */
494 health_code_update();
496 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
497 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
498 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
499 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
500 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
501 msg
.u
.channel
.tracefile_size
,
502 msg
.u
.channel
.tracefile_count
, 0,
503 msg
.u
.channel
.monitor
,
504 msg
.u
.channel
.live_timer_interval
,
506 if (new_channel
== NULL
) {
507 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
510 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
511 switch (msg
.u
.channel
.output
) {
512 case LTTNG_EVENT_SPLICE
:
513 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
515 case LTTNG_EVENT_MMAP
:
516 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
519 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
523 /* Translate and save channel type. */
524 switch (msg
.u
.channel
.type
) {
525 case CONSUMER_CHANNEL_TYPE_DATA
:
526 case CONSUMER_CHANNEL_TYPE_METADATA
:
527 new_channel
->type
= msg
.u
.channel
.type
;
534 health_code_update();
536 if (ctx
->on_recv_channel
!= NULL
) {
537 ret_recv
= ctx
->on_recv_channel(new_channel
);
539 ret
= consumer_add_channel(new_channel
, ctx
);
540 } else if (ret_recv
< 0) {
544 ret
= consumer_add_channel(new_channel
, ctx
);
546 if (CONSUMER_CHANNEL_TYPE_DATA
) {
547 consumer_timer_live_start(new_channel
,
548 msg
.u
.channel
.live_timer_interval
);
551 health_code_update();
553 /* If we received an error in add_channel, we need to report it. */
555 ret
= consumer_send_status_msg(sock
, ret
);
564 case LTTNG_CONSUMER_ADD_STREAM
:
567 struct lttng_pipe
*stream_pipe
;
568 struct lttng_consumer_stream
*new_stream
;
569 struct lttng_consumer_channel
*channel
;
573 * Get stream's channel reference. Needed when adding the stream to the
576 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
579 * We could not find the channel. Can happen if cpu hotplug
580 * happens while tearing down.
582 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
583 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
586 health_code_update();
588 /* First send a status message before receiving the fds. */
589 ret
= consumer_send_status_msg(sock
, ret_code
);
591 /* Somehow, the session daemon is not responding anymore. */
595 health_code_update();
597 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
598 /* Channel was not found. */
604 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
610 health_code_update();
612 /* Get stream file descriptor from socket */
613 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
614 if (ret
!= sizeof(fd
)) {
615 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
620 health_code_update();
623 * Send status code to session daemon only if the recv works. If the
624 * above recv() failed, the session daemon is notified through the
625 * error socket and the teardown is eventually done.
627 ret
= consumer_send_status_msg(sock
, ret_code
);
629 /* Somehow, the session daemon is not responding anymore. */
633 health_code_update();
635 new_stream
= consumer_allocate_stream(channel
->key
,
637 LTTNG_CONSUMER_ACTIVE_STREAM
,
647 if (new_stream
== NULL
) {
652 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
658 new_stream
->chan
= channel
;
659 new_stream
->wait_fd
= fd
;
660 switch (channel
->output
) {
661 case CONSUMER_CHANNEL_SPLICE
:
662 new_stream
->output
= LTTNG_EVENT_SPLICE
;
663 ret
= utils_create_pipe(new_stream
->splice_pipe
);
668 case CONSUMER_CHANNEL_MMAP
:
669 new_stream
->output
= LTTNG_EVENT_MMAP
;
672 ERR("Stream output unknown %d", channel
->output
);
677 * We've just assigned the channel to the stream so increment the
678 * refcount right now. We don't need to increment the refcount for
679 * streams in no monitor because we handle manually the cleanup of
680 * those. It is very important to make sure there is NO prior
681 * consumer_del_stream() calls or else the refcount will be unbalanced.
683 if (channel
->monitor
) {
684 uatomic_inc(&new_stream
->chan
->refcount
);
688 * The buffer flush is done on the session daemon side for the kernel
689 * so no need for the stream "hangup_flush_done" variable to be
690 * tracked. This is important for a kernel stream since we don't rely
691 * on the flush state of the stream to read data. It's not the case for
692 * user space tracing.
694 new_stream
->hangup_flush_done
= 0;
696 health_code_update();
698 if (ctx
->on_recv_stream
) {
699 ret
= ctx
->on_recv_stream(new_stream
);
701 consumer_stream_free(new_stream
);
706 health_code_update();
708 if (new_stream
->metadata_flag
) {
709 channel
->metadata_stream
= new_stream
;
712 /* Do not monitor this stream. */
713 if (!channel
->monitor
) {
714 DBG("Kernel consumer add stream %s in no monitor mode with "
715 "relayd id %" PRIu64
, new_stream
->name
,
716 new_stream
->net_seq_idx
);
717 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
721 /* Send stream to relayd if the stream has an ID. */
722 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
723 ret
= consumer_send_relayd_stream(new_stream
,
724 new_stream
->chan
->pathname
);
726 consumer_stream_free(new_stream
);
731 /* Get the right pipe where the stream will be sent. */
732 if (new_stream
->metadata_flag
) {
733 ret
= consumer_add_metadata_stream(new_stream
);
735 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
737 consumer_stream_free(new_stream
);
740 stream_pipe
= ctx
->consumer_metadata_pipe
;
742 ret
= consumer_add_data_stream(new_stream
);
744 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
746 consumer_stream_free(new_stream
);
749 stream_pipe
= ctx
->consumer_data_pipe
;
752 /* Vitible to other threads */
753 new_stream
->globally_visible
= 1;
755 health_code_update();
757 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
759 ERR("Consumer write %s stream to pipe %d",
760 new_stream
->metadata_flag
? "metadata" : "data",
761 lttng_pipe_get_writefd(stream_pipe
));
762 if (new_stream
->metadata_flag
) {
763 consumer_del_stream_for_metadata(new_stream
);
765 consumer_del_stream_for_data(new_stream
);
770 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
771 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
774 case LTTNG_CONSUMER_STREAMS_SENT
:
776 struct lttng_consumer_channel
*channel
;
779 * Get stream's channel reference. Needed when adding the stream to the
782 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
785 * We could not find the channel. Can happen if cpu hotplug
786 * happens while tearing down.
788 ERR("Unable to find channel key %" PRIu64
,
789 msg
.u
.sent_streams
.channel_key
);
790 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
793 health_code_update();
796 * Send status code to session daemon.
798 ret
= consumer_send_status_msg(sock
, ret_code
);
799 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
800 /* Somehow, the session daemon is not responding anymore. */
804 health_code_update();
807 * We should not send this message if we don't monitor the
808 * streams in this channel.
810 if (!channel
->monitor
) {
814 health_code_update();
815 /* Send stream to relayd if the stream has an ID. */
816 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
817 ret
= consumer_send_relayd_streams_sent(
818 msg
.u
.sent_streams
.net_seq_idx
);
825 case LTTNG_CONSUMER_UPDATE_STREAM
:
830 case LTTNG_CONSUMER_DESTROY_RELAYD
:
832 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
833 struct consumer_relayd_sock_pair
*relayd
;
835 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
837 /* Get relayd reference if exists. */
838 relayd
= consumer_find_relayd(index
);
839 if (relayd
== NULL
) {
840 DBG("Unable to find relayd %" PRIu64
, index
);
841 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
845 * Each relayd socket pair has a refcount of stream attached to it
846 * which tells if the relayd is still active or not depending on the
849 * This will set the destroy flag of the relayd object and destroy it
850 * if the refcount reaches zero when called.
852 * The destroy can happen either here or when a stream fd hangs up.
855 consumer_flag_relayd_for_destroy(relayd
);
858 health_code_update();
860 ret
= consumer_send_status_msg(sock
, ret_code
);
862 /* Somehow, the session daemon is not responding anymore. */
868 case LTTNG_CONSUMER_DATA_PENDING
:
871 uint64_t id
= msg
.u
.data_pending
.session_id
;
873 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
875 ret
= consumer_data_pending(id
);
877 health_code_update();
879 /* Send back returned value to session daemon */
880 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
882 PERROR("send data pending ret code");
887 * No need to send back a status message since the data pending
888 * returned value is the response.
892 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
894 if (msg
.u
.snapshot_channel
.metadata
== 1) {
895 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
896 msg
.u
.snapshot_channel
.pathname
,
897 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
899 ERR("Snapshot metadata failed");
900 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
903 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
904 msg
.u
.snapshot_channel
.pathname
,
905 msg
.u
.snapshot_channel
.relayd_id
,
906 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
909 ERR("Snapshot channel failed");
910 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
914 health_code_update();
916 ret
= consumer_send_status_msg(sock
, ret_code
);
918 /* Somehow, the session daemon is not responding anymore. */
923 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
925 uint64_t key
= msg
.u
.destroy_channel
.key
;
926 struct lttng_consumer_channel
*channel
;
928 channel
= consumer_find_channel(key
);
930 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
931 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
934 health_code_update();
936 ret
= consumer_send_status_msg(sock
, ret_code
);
938 /* Somehow, the session daemon is not responding anymore. */
942 health_code_update();
944 /* Stop right now if no channel was found. */
950 * This command should ONLY be issued for channel with streams set in
953 assert(!channel
->monitor
);
956 * The refcount should ALWAYS be 0 in the case of a channel in no
959 assert(!uatomic_sub_return(&channel
->refcount
, 1));
961 consumer_del_channel(channel
);
965 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
968 struct lttng_consumer_channel
*channel
;
969 uint64_t id
= msg
.u
.discarded_events
.session_id
;
970 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
972 DBG("Kernel consumer discarded events command for session id %"
973 PRIu64
", channel key %" PRIu64
, id
, key
);
975 channel
= consumer_find_channel(key
);
977 ERR("Kernel consumer discarded events channel %"
978 PRIu64
" not found", key
);
981 ret
= channel
->discarded_events
;
984 health_code_update();
986 /* Send back returned value to session daemon */
987 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
989 PERROR("send discarded events");
995 case LTTNG_CONSUMER_LOST_PACKETS
:
998 struct lttng_consumer_channel
*channel
;
999 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1000 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1002 DBG("Kernel consumer lost packets command for session id %"
1003 PRIu64
", channel key %" PRIu64
, id
, key
);
1005 channel
= consumer_find_channel(key
);
1007 ERR("Kernel consumer lost packets channel %"
1008 PRIu64
" not found", key
);
1011 ret
= channel
->lost_packets
;
1014 health_code_update();
1016 /* Send back returned value to session daemon */
1017 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1019 PERROR("send lost packets");
1033 * Return 1 to indicate success since the 0 value can be a socket
1034 * shutdown during the recv() or send() call.
1036 health_code_update();
1041 /* This will issue a consumer stop. */
1046 * Populate index values of a kernel stream. Values are set in big endian order.
1048 * Return 0 on success or else a negative value.
1050 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1054 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1056 PERROR("kernctl_get_timestamp_begin");
1059 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1061 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1063 PERROR("kernctl_get_timestamp_end");
1066 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1068 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1070 PERROR("kernctl_get_events_discarded");
1073 index
->events_discarded
= htobe64(index
->events_discarded
);
1075 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1077 PERROR("kernctl_get_content_size");
1080 index
->content_size
= htobe64(index
->content_size
);
1082 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1084 PERROR("kernctl_get_packet_size");
1087 index
->packet_size
= htobe64(index
->packet_size
);
1089 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1091 PERROR("kernctl_get_stream_id");
1094 index
->stream_id
= htobe64(index
->stream_id
);
1096 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1098 PERROR("kernctl_get_instance_id");
1101 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1103 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1105 PERROR("kernctl_get_sequence_number");
1108 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1114 * Sync metadata meaning request them to the session daemon and snapshot to the
1115 * metadata thread can consumer them.
1117 * Metadata stream lock MUST be acquired.
1119 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1120 * is empty or a negative value on error.
1122 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1128 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1130 ERR("Failed to flush kernel stream");
1134 ret
= kernctl_snapshot(metadata
->wait_fd
);
1136 if (errno
!= EAGAIN
) {
1137 ERR("Sync metadata, taking kernel snapshot failed.");
1140 DBG("Sync metadata, no new kernel metadata");
1141 /* No new metadata, exit. */
1151 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1154 uint64_t seq
, discarded
;
1156 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1158 PERROR("kernctl_get_sequence_number");
1163 * Start the sequence when we extract the first packet in case we don't
1164 * start at 0 (for example if a consumer is not connected to the
1165 * session immediately after the beginning).
1167 if (stream
->last_sequence_number
== -1ULL) {
1168 stream
->last_sequence_number
= seq
;
1169 } else if (seq
> stream
->last_sequence_number
) {
1170 stream
->chan
->lost_packets
+= seq
-
1171 stream
->last_sequence_number
- 1;
1173 /* seq <= last_sequence_number */
1174 ERR("Sequence number inconsistent : prev = %" PRIu64
1175 ", current = %" PRIu64
,
1176 stream
->last_sequence_number
, seq
);
1180 stream
->last_sequence_number
= seq
;
1182 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1184 PERROR("kernctl_get_events_discarded");
1187 if (discarded
< stream
->last_discarded_events
) {
1189 * Overflow has occured. We assume only one wrap-around
1192 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1193 stream
->last_discarded_events
+ discarded
;
1195 stream
->chan
->discarded_events
+= discarded
-
1196 stream
->last_discarded_events
;
1198 stream
->last_discarded_events
= discarded
;
1206 * Check if the local version of the metadata stream matches with the version
1207 * of the metadata stream in the kernel. If it was updated, set the reset flag
1211 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1214 uint64_t cur_version
;
1216 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1218 ERR("Failed to get the metadata version");
1222 if (stream
->metadata_version
== cur_version
) {
1227 DBG("New metadata version detected");
1228 stream
->metadata_version
= cur_version
;
1229 stream
->reset_metadata_flag
= 1;
1237 * Consume data on a file descriptor and write it on a trace file.
1239 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1240 struct lttng_consumer_local_data
*ctx
)
1242 unsigned long len
, subbuf_size
, padding
;
1243 int err
, write_index
= 1;
1245 int infd
= stream
->wait_fd
;
1246 struct ctf_packet_index index
;
1248 DBG("In read_subbuffer (infd : %d)", infd
);
1250 /* Get the next subbuffer */
1251 err
= kernctl_get_next_subbuf(infd
);
1254 * This is a debug message even for single-threaded consumer,
1255 * because poll() have more relaxed criterions than get subbuf,
1256 * so get_subbuf may fail for short race windows where poll()
1257 * would issue wakeups.
1259 DBG("Reserving sub buffer failed (everything is normal, "
1260 "it is due to concurrency)");
1265 /* Get the full subbuffer size including padding */
1266 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1268 PERROR("Getting sub-buffer len failed.");
1269 err
= kernctl_put_subbuf(infd
);
1271 if (errno
== EFAULT
) {
1272 PERROR("Error in unreserving sub buffer\n");
1273 } else if (errno
== EIO
) {
1274 /* Should never happen with newer LTTng versions */
1275 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1284 if (!stream
->metadata_flag
) {
1285 ret
= get_index_values(&index
, infd
);
1287 err
= kernctl_put_subbuf(infd
);
1289 if (errno
== EFAULT
) {
1290 PERROR("Error in unreserving sub buffer\n");
1291 } else if (errno
== EIO
) {
1292 /* Should never happen with newer LTTng versions */
1293 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1300 ret
= update_stream_stats(stream
);
1306 ret
= metadata_stream_check_version(infd
, stream
);
1312 switch (stream
->chan
->output
) {
1313 case CONSUMER_CHANNEL_SPLICE
:
1315 * XXX: The lttng-modules splice "actor" does not handle copying
1316 * partial pages hence only using the subbuffer size without the
1317 * padding makes the splice fail.
1322 /* splice the subbuffer to the tracefile */
1323 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1326 * XXX: Splice does not support network streaming so the return value
1327 * is simply checked against subbuf_size and not like the mmap() op.
1329 if (ret
!= subbuf_size
) {
1331 * display the error but continue processing to try
1332 * to release the subbuffer
1334 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1339 case CONSUMER_CHANNEL_MMAP
:
1340 /* Get subbuffer size without padding */
1341 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1343 PERROR("Getting sub-buffer len failed.");
1344 err
= kernctl_put_subbuf(infd
);
1346 if (errno
== EFAULT
) {
1347 PERROR("Error in unreserving sub buffer\n");
1348 } else if (errno
== EIO
) {
1349 /* Should never happen with newer LTTng versions */
1350 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1359 /* Make sure the tracer is not gone mad on us! */
1360 assert(len
>= subbuf_size
);
1362 padding
= len
- subbuf_size
;
1364 /* write the subbuffer to the tracefile */
1365 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1368 * The mmap operation should write subbuf_size amount of data when
1369 * network streaming or the full padding (len) size when we are _not_
1372 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1373 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1375 * Display the error but continue processing to try to release the
1376 * subbuffer. This is a DBG statement since this is possible to
1377 * happen without being a critical error.
1379 DBG("Error writing to tracefile "
1380 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1381 ret
, len
, subbuf_size
);
1386 ERR("Unknown output method");
1390 err
= kernctl_put_next_subbuf(infd
);
1392 if (errno
== EFAULT
) {
1393 PERROR("Error in unreserving sub buffer\n");
1394 } else if (errno
== EIO
) {
1395 /* Should never happen with newer LTTng versions */
1396 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1402 /* Write index if needed. */
1407 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1409 * In live, block until all the metadata is sent.
1411 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1412 assert(!stream
->missed_metadata_flush
);
1413 stream
->waiting_on_metadata
= true;
1414 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1416 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1418 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1419 stream
->waiting_on_metadata
= false;
1420 if (stream
->missed_metadata_flush
) {
1421 stream
->missed_metadata_flush
= false;
1422 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1423 (void) consumer_flush_kernel_index(stream
);
1425 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1432 err
= consumer_stream_write_index(stream
, &index
);
1441 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1448 * Don't create anything if this is set for streaming or should not be
1451 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1452 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1453 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1454 stream
->uid
, stream
->gid
, NULL
);
1458 stream
->out_fd
= ret
;
1459 stream
->tracefile_size_current
= 0;
1461 if (!stream
->metadata_flag
) {
1462 ret
= index_create_file(stream
->chan
->pathname
,
1463 stream
->name
, stream
->uid
, stream
->gid
,
1464 stream
->chan
->tracefile_size
,
1465 stream
->tracefile_count_current
);
1469 stream
->index_fd
= ret
;
1473 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1474 /* get the len of the mmap region */
1475 unsigned long mmap_len
;
1477 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1479 PERROR("kernctl_get_mmap_len");
1481 goto error_close_fd
;
1483 stream
->mmap_len
= (size_t) mmap_len
;
1485 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1486 MAP_PRIVATE
, stream
->wait_fd
, 0);
1487 if (stream
->mmap_base
== MAP_FAILED
) {
1488 PERROR("Error mmaping");
1490 goto error_close_fd
;
1494 /* we return 0 to let the library handle the FD internally */
1498 if (stream
->out_fd
>= 0) {
1501 err
= close(stream
->out_fd
);
1503 stream
->out_fd
= -1;
1510 * Check if data is still being extracted from the buffers for a specific
1511 * stream. Consumer data lock MUST be acquired before calling this function
1512 * and the stream lock.
1514 * Return 1 if the traced data are still getting read else 0 meaning that the
1515 * data is available for trace viewer reading.
1517 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1523 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1528 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1530 /* There is still data so let's put back this subbuffer. */
1531 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1533 ret
= 1; /* Data is pending */
1537 /* Data is NOT pending and ready to be read. */