2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/socket.h>
27 #include <sys/types.h>
32 #include <bin/lttng-consumerd/health-consumerd.h>
33 #include <common/common.h>
34 #include <common/kernel-ctl/kernel-ctl.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/compat/fcntl.h>
38 #include <common/compat/endian.h>
39 #include <common/pipe.h>
40 #include <common/relayd/relayd.h>
41 #include <common/utils.h>
42 #include <common/consumer/consumer-stream.h>
43 #include <common/index/index.h>
44 #include <common/consumer/consumer-timer.h>
46 #include "kernel-consumer.h"
48 extern struct lttng_consumer_global_data consumer_data
;
49 extern int consumer_poll_timeout
;
50 extern volatile int consumer_quit
;
53 * Take a snapshot for a specific fd
55 * Returns 0 on success, < 0 on error
57 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
60 int infd
= stream
->wait_fd
;
62 ret
= kernctl_snapshot(infd
);
64 PERROR("Getting sub-buffer snapshot.");
72 * Get the produced position
74 * Returns 0 on success, < 0 on error
76 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
80 int infd
= stream
->wait_fd
;
82 ret
= kernctl_snapshot_get_produced(infd
, pos
);
84 PERROR("kernctl_snapshot_get_produced");
92 * Get the consumerd position
94 * Returns 0 on success, < 0 on error
96 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
100 int infd
= stream
->wait_fd
;
102 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
104 PERROR("kernctl_snapshot_get_consumed");
112 * Take a snapshot of all the stream of a channel
114 * Returns 0 on success, < 0 on error
116 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
117 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
118 struct lttng_consumer_local_data
*ctx
)
121 struct lttng_consumer_channel
*channel
;
122 struct lttng_consumer_stream
*stream
;
124 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
128 channel
= consumer_find_channel(key
);
130 ERR("No channel found for key %" PRIu64
, key
);
135 /* Splice is not supported yet for channel snapshot. */
136 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
137 ERR("Unsupported output %d", channel
->output
);
142 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
143 /* Are we at a position _before_ the first available packet ? */
144 bool before_first_packet
= true;
145 unsigned long consumed_pos
, produced_pos
;
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");
188 channel
->streams_sent_to_relayd
= true;
191 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
194 * Doing a buffer flush which does not take into
195 * account empty packets. This is not perfect
196 * for stream intersection, but required as a
197 * fall-back when "flush_empty" is not
198 * implemented by lttng-modules.
200 ret
= kernctl_buffer_flush(stream
->wait_fd
);
202 ERR("Failed to flush kernel stream");
207 ret
= lttng_kconsumer_take_snapshot(stream
);
209 ERR("Taking kernel snapshot");
213 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
215 ERR("Produced kernel snapshot position");
219 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
221 ERR("Consumerd kernel snapshot position");
225 if (stream
->max_sb_size
== 0) {
226 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
227 &stream
->max_sb_size
);
229 ERR("Getting kernel max_sb_size");
235 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
236 produced_pos
, nb_packets_per_stream
,
237 stream
->max_sb_size
);
239 while (consumed_pos
< produced_pos
) {
241 unsigned long len
, padded_len
;
244 health_code_update();
246 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
248 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
250 if (errno
!= EAGAIN
) {
251 PERROR("kernctl_get_subbuf snapshot");
255 DBG("Kernel consumer get subbuf failed. Skipping it.");
256 consumed_pos
+= stream
->max_sb_size
;
259 * Start accounting lost packets only when we
260 * already have extracted packets (to match the
261 * content of the final snapshot).
263 if (!before_first_packet
) {
269 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
271 ERR("Snapshot kernctl_get_subbuf_size");
273 goto error_put_subbuf
;
276 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
278 ERR("Snapshot kernctl_get_padded_subbuf_size");
280 goto error_put_subbuf
;
283 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
284 padded_len
- len
, NULL
);
286 * We write the padded len in local tracefiles but the data len
287 * when using a relay. Display the error but continue processing
288 * to try to release the subbuffer.
290 if (relayd_id
!= (uint64_t) -1ULL) {
291 if (read_len
!= len
) {
292 ERR("Error sending to the relay (ret: %zd != len: %lu)",
296 if (read_len
!= padded_len
) {
297 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
298 read_len
, padded_len
);
302 ret
= kernctl_put_subbuf(stream
->wait_fd
);
304 ERR("Snapshot kernctl_put_subbuf");
308 consumed_pos
+= stream
->max_sb_size
;
311 * Only account lost packets located between
312 * succesfully extracted packets (do not account before
313 * and after since they are not visible in the
314 * resulting snapshot).
316 stream
->chan
->lost_packets
+= lost_packet
;
318 before_first_packet
= false;
321 if (relayd_id
== (uint64_t) -1ULL) {
322 if (stream
->out_fd
>= 0) {
323 ret
= close(stream
->out_fd
);
325 PERROR("Kernel consumer snapshot close out_fd");
331 close_relayd_stream(stream
);
332 stream
->net_seq_idx
= (uint64_t) -1ULL;
334 pthread_mutex_unlock(&stream
->lock
);
342 ret
= kernctl_put_subbuf(stream
->wait_fd
);
345 ERR("Snapshot kernctl_put_subbuf error path");
348 pthread_mutex_unlock(&stream
->lock
);
355 * Read the whole metadata available for a snapshot.
357 * Returns 0 on success, < 0 on error
359 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
360 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
362 int ret
, use_relayd
= 0;
364 struct lttng_consumer_channel
*metadata_channel
;
365 struct lttng_consumer_stream
*metadata_stream
;
369 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
374 metadata_channel
= consumer_find_channel(key
);
375 if (!metadata_channel
) {
376 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
381 metadata_stream
= metadata_channel
->metadata_stream
;
382 assert(metadata_stream
);
384 /* Flag once that we have a valid relayd for the stream. */
385 if (relayd_id
!= (uint64_t) -1ULL) {
390 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
395 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
396 metadata_stream
->chan
->tracefile_size
,
397 metadata_stream
->tracefile_count_current
,
398 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
402 metadata_stream
->out_fd
= ret
;
406 health_code_update();
408 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
410 if (ret_read
!= -EAGAIN
) {
411 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
415 /* ret_read is negative at this point so we will exit the loop. */
418 } while (ret_read
>= 0);
421 close_relayd_stream(metadata_stream
);
422 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
424 if (metadata_stream
->out_fd
>= 0) {
425 ret
= close(metadata_stream
->out_fd
);
427 PERROR("Kernel consumer snapshot metadata close out_fd");
429 * Don't go on error here since the snapshot was successful at this
430 * point but somehow the close failed.
433 metadata_stream
->out_fd
= -1;
439 cds_list_del(&metadata_stream
->send_node
);
440 consumer_stream_destroy(metadata_stream
, NULL
);
441 metadata_channel
->metadata_stream
= NULL
;
448 * Receive command from session daemon and process it.
450 * Return 1 on success else a negative value or 0.
452 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
453 int sock
, struct pollfd
*consumer_sockpoll
)
456 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
457 struct lttcomm_consumer_msg msg
;
459 health_code_update();
461 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
462 if (ret
!= sizeof(msg
)) {
464 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
470 health_code_update();
472 /* Deprecated command */
473 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
475 health_code_update();
477 /* relayd needs RCU read-side protection */
480 switch (msg
.cmd_type
) {
481 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
483 /* Session daemon status message are handled in the following call. */
484 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
485 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
486 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
487 msg
.u
.relayd_sock
.relayd_session_id
);
490 case LTTNG_CONSUMER_ADD_CHANNEL
:
492 struct lttng_consumer_channel
*new_channel
;
495 health_code_update();
497 /* First send a status message before receiving the fds. */
498 ret
= consumer_send_status_msg(sock
, ret_code
);
500 /* Somehow, the session daemon is not responding anymore. */
504 health_code_update();
506 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
507 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
508 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
509 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
510 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
511 msg
.u
.channel
.tracefile_size
,
512 msg
.u
.channel
.tracefile_count
, 0,
513 msg
.u
.channel
.monitor
,
514 msg
.u
.channel
.live_timer_interval
,
516 if (new_channel
== NULL
) {
517 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
520 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
521 switch (msg
.u
.channel
.output
) {
522 case LTTNG_EVENT_SPLICE
:
523 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
525 case LTTNG_EVENT_MMAP
:
526 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
529 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
533 /* Translate and save channel type. */
534 switch (msg
.u
.channel
.type
) {
535 case CONSUMER_CHANNEL_TYPE_DATA
:
536 case CONSUMER_CHANNEL_TYPE_METADATA
:
537 new_channel
->type
= msg
.u
.channel
.type
;
544 health_code_update();
546 if (ctx
->on_recv_channel
!= NULL
) {
547 ret_recv
= ctx
->on_recv_channel(new_channel
);
549 ret
= consumer_add_channel(new_channel
, ctx
);
550 } else if (ret_recv
< 0) {
554 ret
= consumer_add_channel(new_channel
, ctx
);
556 if (CONSUMER_CHANNEL_TYPE_DATA
) {
557 consumer_timer_live_start(new_channel
,
558 msg
.u
.channel
.live_timer_interval
);
561 health_code_update();
563 /* If we received an error in add_channel, we need to report it. */
565 ret
= consumer_send_status_msg(sock
, ret
);
574 case LTTNG_CONSUMER_ADD_STREAM
:
577 struct lttng_pipe
*stream_pipe
;
578 struct lttng_consumer_stream
*new_stream
;
579 struct lttng_consumer_channel
*channel
;
583 * Get stream's channel reference. Needed when adding the stream to the
586 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
589 * We could not find the channel. Can happen if cpu hotplug
590 * happens while tearing down.
592 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
593 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
596 health_code_update();
598 /* First send a status message before receiving the fds. */
599 ret
= consumer_send_status_msg(sock
, ret_code
);
601 /* Somehow, the session daemon is not responding anymore. */
605 health_code_update();
607 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
608 /* Channel was not found. */
614 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
620 health_code_update();
622 /* Get stream file descriptor from socket */
623 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
624 if (ret
!= sizeof(fd
)) {
625 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
630 health_code_update();
633 * Send status code to session daemon only if the recv works. If the
634 * above recv() failed, the session daemon is notified through the
635 * error socket and the teardown is eventually done.
637 ret
= consumer_send_status_msg(sock
, ret_code
);
639 /* Somehow, the session daemon is not responding anymore. */
643 health_code_update();
645 new_stream
= consumer_allocate_stream(channel
->key
,
647 LTTNG_CONSUMER_ACTIVE_STREAM
,
657 if (new_stream
== NULL
) {
662 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
668 new_stream
->chan
= channel
;
669 new_stream
->wait_fd
= fd
;
670 switch (channel
->output
) {
671 case CONSUMER_CHANNEL_SPLICE
:
672 new_stream
->output
= LTTNG_EVENT_SPLICE
;
673 ret
= utils_create_pipe(new_stream
->splice_pipe
);
678 case CONSUMER_CHANNEL_MMAP
:
679 new_stream
->output
= LTTNG_EVENT_MMAP
;
682 ERR("Stream output unknown %d", channel
->output
);
687 * We've just assigned the channel to the stream so increment the
688 * refcount right now. We don't need to increment the refcount for
689 * streams in no monitor because we handle manually the cleanup of
690 * those. It is very important to make sure there is NO prior
691 * consumer_del_stream() calls or else the refcount will be unbalanced.
693 if (channel
->monitor
) {
694 uatomic_inc(&new_stream
->chan
->refcount
);
698 * The buffer flush is done on the session daemon side for the kernel
699 * so no need for the stream "hangup_flush_done" variable to be
700 * tracked. This is important for a kernel stream since we don't rely
701 * on the flush state of the stream to read data. It's not the case for
702 * user space tracing.
704 new_stream
->hangup_flush_done
= 0;
706 health_code_update();
708 if (ctx
->on_recv_stream
) {
709 ret
= ctx
->on_recv_stream(new_stream
);
711 consumer_stream_free(new_stream
);
716 health_code_update();
718 if (new_stream
->metadata_flag
) {
719 channel
->metadata_stream
= new_stream
;
722 /* Do not monitor this stream. */
723 if (!channel
->monitor
) {
724 DBG("Kernel consumer add stream %s in no monitor mode with "
725 "relayd id %" PRIu64
, new_stream
->name
,
726 new_stream
->net_seq_idx
);
727 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
731 /* Send stream to relayd if the stream has an ID. */
732 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
733 ret
= consumer_send_relayd_stream(new_stream
,
734 new_stream
->chan
->pathname
);
736 consumer_stream_free(new_stream
);
741 * If adding an extra stream to an already
742 * existing channel (e.g. cpu hotplug), we need
743 * to send the "streams_sent" command to relayd.
745 if (channel
->streams_sent_to_relayd
) {
746 ret
= consumer_send_relayd_streams_sent(
747 new_stream
->net_seq_idx
);
754 /* Get the right pipe where the stream will be sent. */
755 if (new_stream
->metadata_flag
) {
756 ret
= consumer_add_metadata_stream(new_stream
);
758 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
760 consumer_stream_free(new_stream
);
763 stream_pipe
= ctx
->consumer_metadata_pipe
;
765 ret
= consumer_add_data_stream(new_stream
);
767 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
769 consumer_stream_free(new_stream
);
772 stream_pipe
= ctx
->consumer_data_pipe
;
775 /* Vitible to other threads */
776 new_stream
->globally_visible
= 1;
778 health_code_update();
780 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
782 ERR("Consumer write %s stream to pipe %d",
783 new_stream
->metadata_flag
? "metadata" : "data",
784 lttng_pipe_get_writefd(stream_pipe
));
785 if (new_stream
->metadata_flag
) {
786 consumer_del_stream_for_metadata(new_stream
);
788 consumer_del_stream_for_data(new_stream
);
793 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
794 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
797 case LTTNG_CONSUMER_STREAMS_SENT
:
799 struct lttng_consumer_channel
*channel
;
802 * Get stream's channel reference. Needed when adding the stream to the
805 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
808 * We could not find the channel. Can happen if cpu hotplug
809 * happens while tearing down.
811 ERR("Unable to find channel key %" PRIu64
,
812 msg
.u
.sent_streams
.channel_key
);
813 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
816 health_code_update();
819 * Send status code to session daemon.
821 ret
= consumer_send_status_msg(sock
, ret_code
);
822 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
823 /* Somehow, the session daemon is not responding anymore. */
827 health_code_update();
830 * We should not send this message if we don't monitor the
831 * streams in this channel.
833 if (!channel
->monitor
) {
837 health_code_update();
838 /* Send stream to relayd if the stream has an ID. */
839 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
840 ret
= consumer_send_relayd_streams_sent(
841 msg
.u
.sent_streams
.net_seq_idx
);
845 channel
->streams_sent_to_relayd
= true;
849 case LTTNG_CONSUMER_UPDATE_STREAM
:
854 case LTTNG_CONSUMER_DESTROY_RELAYD
:
856 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
857 struct consumer_relayd_sock_pair
*relayd
;
859 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
861 /* Get relayd reference if exists. */
862 relayd
= consumer_find_relayd(index
);
863 if (relayd
== NULL
) {
864 DBG("Unable to find relayd %" PRIu64
, index
);
865 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
869 * Each relayd socket pair has a refcount of stream attached to it
870 * which tells if the relayd is still active or not depending on the
873 * This will set the destroy flag of the relayd object and destroy it
874 * if the refcount reaches zero when called.
876 * The destroy can happen either here or when a stream fd hangs up.
879 consumer_flag_relayd_for_destroy(relayd
);
882 health_code_update();
884 ret
= consumer_send_status_msg(sock
, ret_code
);
886 /* Somehow, the session daemon is not responding anymore. */
892 case LTTNG_CONSUMER_DATA_PENDING
:
895 uint64_t id
= msg
.u
.data_pending
.session_id
;
897 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
899 ret
= consumer_data_pending(id
);
901 health_code_update();
903 /* Send back returned value to session daemon */
904 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
906 PERROR("send data pending ret code");
911 * No need to send back a status message since the data pending
912 * returned value is the response.
916 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
918 if (msg
.u
.snapshot_channel
.metadata
== 1) {
919 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
920 msg
.u
.snapshot_channel
.pathname
,
921 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
923 ERR("Snapshot metadata failed");
924 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
927 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
928 msg
.u
.snapshot_channel
.pathname
,
929 msg
.u
.snapshot_channel
.relayd_id
,
930 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
933 ERR("Snapshot channel failed");
934 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
938 health_code_update();
940 ret
= consumer_send_status_msg(sock
, ret_code
);
942 /* Somehow, the session daemon is not responding anymore. */
947 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
949 uint64_t key
= msg
.u
.destroy_channel
.key
;
950 struct lttng_consumer_channel
*channel
;
952 channel
= consumer_find_channel(key
);
954 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
955 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
958 health_code_update();
960 ret
= consumer_send_status_msg(sock
, ret_code
);
962 /* Somehow, the session daemon is not responding anymore. */
966 health_code_update();
968 /* Stop right now if no channel was found. */
974 * This command should ONLY be issued for channel with streams set in
977 assert(!channel
->monitor
);
980 * The refcount should ALWAYS be 0 in the case of a channel in no
983 assert(!uatomic_sub_return(&channel
->refcount
, 1));
985 consumer_del_channel(channel
);
989 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
992 struct lttng_consumer_channel
*channel
;
993 uint64_t id
= msg
.u
.discarded_events
.session_id
;
994 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
996 DBG("Kernel consumer discarded events command for session id %"
997 PRIu64
", channel key %" PRIu64
, id
, key
);
999 channel
= consumer_find_channel(key
);
1001 ERR("Kernel consumer discarded events channel %"
1002 PRIu64
" not found", key
);
1005 ret
= channel
->discarded_events
;
1008 health_code_update();
1010 /* Send back returned value to session daemon */
1011 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1013 PERROR("send discarded events");
1019 case LTTNG_CONSUMER_LOST_PACKETS
:
1022 struct lttng_consumer_channel
*channel
;
1023 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1024 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1026 DBG("Kernel consumer lost packets command for session id %"
1027 PRIu64
", channel key %" PRIu64
, id
, key
);
1029 channel
= consumer_find_channel(key
);
1031 ERR("Kernel consumer lost packets channel %"
1032 PRIu64
" not found", key
);
1035 ret
= channel
->lost_packets
;
1038 health_code_update();
1040 /* Send back returned value to session daemon */
1041 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1043 PERROR("send lost packets");
1057 * Return 1 to indicate success since the 0 value can be a socket
1058 * shutdown during the recv() or send() call.
1060 health_code_update();
1065 /* This will issue a consumer stop. */
1070 * Populate index values of a kernel stream. Values are set in big endian order.
1072 * Return 0 on success or else a negative value.
1074 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1078 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1080 PERROR("kernctl_get_timestamp_begin");
1083 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1085 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1087 PERROR("kernctl_get_timestamp_end");
1090 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1092 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1094 PERROR("kernctl_get_events_discarded");
1097 index
->events_discarded
= htobe64(index
->events_discarded
);
1099 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1101 PERROR("kernctl_get_content_size");
1104 index
->content_size
= htobe64(index
->content_size
);
1106 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1108 PERROR("kernctl_get_packet_size");
1111 index
->packet_size
= htobe64(index
->packet_size
);
1113 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1115 PERROR("kernctl_get_stream_id");
1118 index
->stream_id
= htobe64(index
->stream_id
);
1120 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1122 if (ret
== -ENOTTY
) {
1123 /* Command not implemented by lttng-modules. */
1124 index
->stream_instance_id
= -1ULL;
1127 PERROR("kernctl_get_instance_id");
1131 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1133 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1135 if (ret
== -ENOTTY
) {
1136 /* Command not implemented by lttng-modules. */
1137 index
->packet_seq_num
= -1ULL;
1140 PERROR("kernctl_get_sequence_number");
1144 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1150 * Sync metadata meaning request them to the session daemon and snapshot to the
1151 * metadata thread can consumer them.
1153 * Metadata stream lock MUST be acquired.
1155 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1156 * is empty or a negative value on error.
1158 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1164 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1166 ERR("Failed to flush kernel stream");
1170 ret
= kernctl_snapshot(metadata
->wait_fd
);
1172 if (errno
!= EAGAIN
) {
1173 ERR("Sync metadata, taking kernel snapshot failed.");
1176 DBG("Sync metadata, no new kernel metadata");
1177 /* No new metadata, exit. */
1187 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1190 uint64_t seq
, discarded
;
1192 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1194 if (ret
== -ENOTTY
) {
1195 /* Command not implemented by lttng-modules. */
1199 PERROR("kernctl_get_sequence_number");
1205 * Start the sequence when we extract the first packet in case we don't
1206 * start at 0 (for example if a consumer is not connected to the
1207 * session immediately after the beginning).
1209 if (stream
->last_sequence_number
== -1ULL) {
1210 stream
->last_sequence_number
= seq
;
1211 } else if (seq
> stream
->last_sequence_number
) {
1212 stream
->chan
->lost_packets
+= seq
-
1213 stream
->last_sequence_number
- 1;
1215 /* seq <= last_sequence_number */
1216 ERR("Sequence number inconsistent : prev = %" PRIu64
1217 ", current = %" PRIu64
,
1218 stream
->last_sequence_number
, seq
);
1222 stream
->last_sequence_number
= seq
;
1224 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1226 PERROR("kernctl_get_events_discarded");
1229 if (discarded
< stream
->last_discarded_events
) {
1231 * Overflow has occurred. We assume only one wrap-around
1234 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1235 stream
->last_discarded_events
+ discarded
;
1237 stream
->chan
->discarded_events
+= discarded
-
1238 stream
->last_discarded_events
;
1240 stream
->last_discarded_events
= discarded
;
1248 * Check if the local version of the metadata stream matches with the version
1249 * of the metadata stream in the kernel. If it was updated, set the reset flag
1253 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1256 uint64_t cur_version
;
1258 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1260 if (ret
== -ENOTTY
) {
1262 * LTTng-modules does not implement this
1268 ERR("Failed to get the metadata version");
1272 if (stream
->metadata_version
== cur_version
) {
1277 DBG("New metadata version detected");
1278 stream
->metadata_version
= cur_version
;
1279 stream
->reset_metadata_flag
= 1;
1287 * Consume data on a file descriptor and write it on a trace file.
1289 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1290 struct lttng_consumer_local_data
*ctx
)
1292 unsigned long len
, subbuf_size
, padding
;
1293 int err
, write_index
= 1;
1295 int infd
= stream
->wait_fd
;
1296 struct ctf_packet_index index
;
1298 DBG("In read_subbuffer (infd : %d)", infd
);
1300 /* Get the next subbuffer */
1301 err
= kernctl_get_next_subbuf(infd
);
1304 * This is a debug message even for single-threaded consumer,
1305 * because poll() have more relaxed criterions than get subbuf,
1306 * so get_subbuf may fail for short race windows where poll()
1307 * would issue wakeups.
1309 DBG("Reserving sub buffer failed (everything is normal, "
1310 "it is due to concurrency)");
1315 /* Get the full subbuffer size including padding */
1316 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1318 PERROR("Getting sub-buffer len failed.");
1319 err
= kernctl_put_subbuf(infd
);
1321 if (errno
== EFAULT
) {
1322 PERROR("Error in unreserving sub buffer\n");
1323 } else if (errno
== EIO
) {
1324 /* Should never happen with newer LTTng versions */
1325 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1334 if (!stream
->metadata_flag
) {
1335 ret
= get_index_values(&index
, infd
);
1337 err
= kernctl_put_subbuf(infd
);
1339 if (errno
== EFAULT
) {
1340 PERROR("Error in unreserving sub buffer\n");
1341 } else if (errno
== EIO
) {
1342 /* Should never happen with newer LTTng versions */
1343 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1350 ret
= update_stream_stats(stream
);
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.");
1367 ret
= metadata_stream_check_version(infd
, stream
);
1369 err
= kernctl_put_subbuf(infd
);
1371 if (err
== -EFAULT
) {
1372 PERROR("Error in unreserving sub buffer\n");
1373 } else if (err
== -EIO
) {
1374 /* Should never happen with newer LTTng versions */
1375 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1384 switch (stream
->chan
->output
) {
1385 case CONSUMER_CHANNEL_SPLICE
:
1387 * XXX: The lttng-modules splice "actor" does not handle copying
1388 * partial pages hence only using the subbuffer size without the
1389 * padding makes the splice fail.
1394 /* splice the subbuffer to the tracefile */
1395 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1398 * XXX: Splice does not support network streaming so the return value
1399 * is simply checked against subbuf_size and not like the mmap() op.
1401 if (ret
!= subbuf_size
) {
1403 * display the error but continue processing to try
1404 * to release the subbuffer
1406 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1411 case CONSUMER_CHANNEL_MMAP
:
1412 /* Get subbuffer size without padding */
1413 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1415 PERROR("Getting sub-buffer len failed.");
1416 err
= kernctl_put_subbuf(infd
);
1418 if (errno
== EFAULT
) {
1419 PERROR("Error in unreserving sub buffer\n");
1420 } else if (errno
== EIO
) {
1421 /* Should never happen with newer LTTng versions */
1422 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1431 /* Make sure the tracer is not gone mad on us! */
1432 assert(len
>= subbuf_size
);
1434 padding
= len
- subbuf_size
;
1436 /* write the subbuffer to the tracefile */
1437 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1440 * The mmap operation should write subbuf_size amount of data when
1441 * network streaming or the full padding (len) size when we are _not_
1444 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1445 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1447 * Display the error but continue processing to try to release the
1448 * subbuffer. This is a DBG statement since this is possible to
1449 * happen without being a critical error.
1451 DBG("Error writing to tracefile "
1452 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1453 ret
, len
, subbuf_size
);
1458 ERR("Unknown output method");
1462 err
= kernctl_put_next_subbuf(infd
);
1464 if (errno
== EFAULT
) {
1465 PERROR("Error in unreserving sub buffer\n");
1466 } else if (errno
== EIO
) {
1467 /* Should never happen with newer LTTng versions */
1468 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1474 /* Write index if needed. */
1479 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1481 * In live, block until all the metadata is sent.
1483 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1484 assert(!stream
->missed_metadata_flush
);
1485 stream
->waiting_on_metadata
= true;
1486 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1488 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1490 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1491 stream
->waiting_on_metadata
= false;
1492 if (stream
->missed_metadata_flush
) {
1493 stream
->missed_metadata_flush
= false;
1494 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1495 (void) consumer_flush_kernel_index(stream
);
1497 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1504 err
= consumer_stream_write_index(stream
, &index
);
1513 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1520 * Don't create anything if this is set for streaming or should not be
1523 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1524 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1525 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1526 stream
->uid
, stream
->gid
, NULL
);
1530 stream
->out_fd
= ret
;
1531 stream
->tracefile_size_current
= 0;
1533 if (!stream
->metadata_flag
) {
1534 struct lttng_index_file
*index_file
;
1536 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1537 stream
->name
, stream
->uid
, stream
->gid
,
1538 stream
->chan
->tracefile_size
,
1539 stream
->tracefile_count_current
,
1540 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1544 stream
->index_file
= index_file
;
1548 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1549 /* get the len of the mmap region */
1550 unsigned long mmap_len
;
1552 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1554 PERROR("kernctl_get_mmap_len");
1556 goto error_close_fd
;
1558 stream
->mmap_len
= (size_t) mmap_len
;
1560 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1561 MAP_PRIVATE
, stream
->wait_fd
, 0);
1562 if (stream
->mmap_base
== MAP_FAILED
) {
1563 PERROR("Error mmaping");
1565 goto error_close_fd
;
1569 /* we return 0 to let the library handle the FD internally */
1573 if (stream
->out_fd
>= 0) {
1576 err
= close(stream
->out_fd
);
1578 stream
->out_fd
= -1;
1585 * Check if data is still being extracted from the buffers for a specific
1586 * stream. Consumer data lock MUST be acquired before calling this function
1587 * and the stream lock.
1589 * Return 1 if the traced data are still getting read else 0 meaning that the
1590 * data is available for trace viewer reading.
1592 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1598 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1603 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1605 /* There is still data so let's put back this subbuffer. */
1606 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1608 ret
= 1; /* Data is pending */
1612 /* Data is NOT pending and ready to be read. */