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-stream.h>
43 #include <common/index/index.h>
44 #include <common/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 max_stream_size
,
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
) {
145 health_code_update();
148 * Lock stream because we are about to change its state.
150 pthread_mutex_lock(&stream
->lock
);
153 * Assign the received relayd ID so we can use it for streaming. The streams
154 * are not visible to anyone so this is OK to change it.
156 stream
->net_seq_idx
= relayd_id
;
157 channel
->relayd_id
= relayd_id
;
158 if (relayd_id
!= (uint64_t) -1ULL) {
159 ret
= consumer_send_relayd_stream(stream
, path
);
161 ERR("sending stream to relayd");
165 ret
= utils_create_stream_file(path
, stream
->name
,
166 stream
->chan
->tracefile_size
,
167 stream
->tracefile_count_current
,
168 stream
->uid
, stream
->gid
, NULL
);
170 ERR("utils_create_stream_file");
174 stream
->out_fd
= ret
;
175 stream
->tracefile_size_current
= 0;
177 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
178 path
, stream
->name
, stream
->key
);
180 if (relayd_id
!= -1ULL) {
181 ret
= consumer_send_relayd_streams_sent(relayd_id
);
183 ERR("sending streams sent to relayd");
188 ret
= kernctl_buffer_flush(stream
->wait_fd
);
190 ERR("Failed to flush kernel stream");
195 ret
= lttng_kconsumer_take_snapshot(stream
);
197 ERR("Taking kernel snapshot");
201 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
203 ERR("Produced kernel snapshot position");
207 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
209 ERR("Consumerd kernel snapshot position");
213 if (stream
->max_sb_size
== 0) {
214 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
215 &stream
->max_sb_size
);
217 ERR("Getting kernel max_sb_size");
224 * The original value is sent back if max stream size is larger than
225 * the possible size of the snapshot. Also, we asume that the session
226 * daemon should never send a maximum stream size that is lower than
229 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
230 produced_pos
, max_stream_size
);
232 while (consumed_pos
< produced_pos
) {
234 unsigned long len
, padded_len
;
236 health_code_update();
238 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
240 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
242 if (errno
!= EAGAIN
) {
243 PERROR("kernctl_get_subbuf snapshot");
247 DBG("Kernel consumer get subbuf failed. Skipping it.");
248 consumed_pos
+= stream
->max_sb_size
;
252 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
254 ERR("Snapshot kernctl_get_subbuf_size");
256 goto error_put_subbuf
;
259 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
261 ERR("Snapshot kernctl_get_padded_subbuf_size");
263 goto error_put_subbuf
;
266 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
267 padded_len
- len
, NULL
);
269 * We write the padded len in local tracefiles but the data len
270 * when using a relay. Display the error but continue processing
271 * to try to release the subbuffer.
273 if (relayd_id
!= (uint64_t) -1ULL) {
274 if (read_len
!= len
) {
275 ERR("Error sending to the relay (ret: %zd != len: %lu)",
279 if (read_len
!= padded_len
) {
280 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
281 read_len
, padded_len
);
285 ret
= kernctl_put_subbuf(stream
->wait_fd
);
287 ERR("Snapshot kernctl_put_subbuf");
291 consumed_pos
+= stream
->max_sb_size
;
294 if (relayd_id
== (uint64_t) -1ULL) {
295 if (stream
->out_fd
>= 0) {
296 ret
= close(stream
->out_fd
);
298 PERROR("Kernel consumer snapshot close out_fd");
304 close_relayd_stream(stream
);
305 stream
->net_seq_idx
= (uint64_t) -1ULL;
307 pthread_mutex_unlock(&stream
->lock
);
315 ret
= kernctl_put_subbuf(stream
->wait_fd
);
318 ERR("Snapshot kernctl_put_subbuf error path");
321 pthread_mutex_unlock(&stream
->lock
);
328 * Read the whole metadata available for a snapshot.
330 * Returns 0 on success, < 0 on error
332 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
333 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
335 int ret
, use_relayd
= 0;
337 struct lttng_consumer_channel
*metadata_channel
;
338 struct lttng_consumer_stream
*metadata_stream
;
342 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
347 metadata_channel
= consumer_find_channel(key
);
348 if (!metadata_channel
) {
349 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
354 metadata_stream
= metadata_channel
->metadata_stream
;
355 assert(metadata_stream
);
357 /* Flag once that we have a valid relayd for the stream. */
358 if (relayd_id
!= (uint64_t) -1ULL) {
363 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
368 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
369 metadata_stream
->chan
->tracefile_size
,
370 metadata_stream
->tracefile_count_current
,
371 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
375 metadata_stream
->out_fd
= ret
;
379 health_code_update();
381 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
383 if (ret_read
!= -EAGAIN
) {
384 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
388 /* ret_read is negative at this point so we will exit the loop. */
391 } while (ret_read
>= 0);
394 close_relayd_stream(metadata_stream
);
395 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
397 if (metadata_stream
->out_fd
>= 0) {
398 ret
= close(metadata_stream
->out_fd
);
400 PERROR("Kernel consumer snapshot metadata close out_fd");
402 * Don't go on error here since the snapshot was successful at this
403 * point but somehow the close failed.
406 metadata_stream
->out_fd
= -1;
412 cds_list_del(&metadata_stream
->send_node
);
413 consumer_stream_destroy(metadata_stream
, NULL
);
414 metadata_channel
->metadata_stream
= NULL
;
421 * Receive command from session daemon and process it.
423 * Return 1 on success else a negative value or 0.
425 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
426 int sock
, struct pollfd
*consumer_sockpoll
)
429 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
430 struct lttcomm_consumer_msg msg
;
432 health_code_update();
434 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
435 if (ret
!= sizeof(msg
)) {
437 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
443 health_code_update();
445 /* Deprecated command */
446 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
448 health_code_update();
450 /* relayd needs RCU read-side protection */
453 switch (msg
.cmd_type
) {
454 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
456 /* Session daemon status message are handled in the following call. */
457 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
458 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
459 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
460 msg
.u
.relayd_sock
.relayd_session_id
);
463 case LTTNG_CONSUMER_ADD_CHANNEL
:
465 struct lttng_consumer_channel
*new_channel
;
468 health_code_update();
470 /* First send a status message before receiving the fds. */
471 ret
= consumer_send_status_msg(sock
, ret_code
);
473 /* Somehow, the session daemon is not responding anymore. */
477 health_code_update();
479 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
480 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
481 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
482 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
483 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
484 msg
.u
.channel
.tracefile_size
,
485 msg
.u
.channel
.tracefile_count
, 0,
486 msg
.u
.channel
.monitor
,
487 msg
.u
.channel
.live_timer_interval
);
488 if (new_channel
== NULL
) {
489 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
492 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
493 switch (msg
.u
.channel
.output
) {
494 case LTTNG_EVENT_SPLICE
:
495 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
497 case LTTNG_EVENT_MMAP
:
498 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
501 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
505 /* Translate and save channel type. */
506 switch (msg
.u
.channel
.type
) {
507 case CONSUMER_CHANNEL_TYPE_DATA
:
508 case CONSUMER_CHANNEL_TYPE_METADATA
:
509 new_channel
->type
= msg
.u
.channel
.type
;
516 health_code_update();
518 if (ctx
->on_recv_channel
!= NULL
) {
519 ret_recv
= ctx
->on_recv_channel(new_channel
);
521 ret
= consumer_add_channel(new_channel
, ctx
);
522 } else if (ret_recv
< 0) {
526 ret
= consumer_add_channel(new_channel
, ctx
);
528 if (CONSUMER_CHANNEL_TYPE_DATA
) {
529 consumer_timer_live_start(new_channel
,
530 msg
.u
.channel
.live_timer_interval
);
533 health_code_update();
535 /* If we received an error in add_channel, we need to report it. */
537 ret
= consumer_send_status_msg(sock
, ret
);
546 case LTTNG_CONSUMER_ADD_STREAM
:
549 struct lttng_pipe
*stream_pipe
;
550 struct lttng_consumer_stream
*new_stream
;
551 struct lttng_consumer_channel
*channel
;
555 * Get stream's channel reference. Needed when adding the stream to the
558 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
561 * We could not find the channel. Can happen if cpu hotplug
562 * happens while tearing down.
564 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
565 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
568 health_code_update();
570 /* First send a status message before receiving the fds. */
571 ret
= consumer_send_status_msg(sock
, ret_code
);
573 /* Somehow, the session daemon is not responding anymore. */
577 health_code_update();
579 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
580 /* Channel was not found. */
586 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
592 health_code_update();
594 /* Get stream file descriptor from socket */
595 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
596 if (ret
!= sizeof(fd
)) {
597 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
602 health_code_update();
605 * Send status code to session daemon only if the recv works. If the
606 * above recv() failed, the session daemon is notified through the
607 * error socket and the teardown is eventually done.
609 ret
= consumer_send_status_msg(sock
, ret_code
);
611 /* Somehow, the session daemon is not responding anymore. */
615 health_code_update();
617 new_stream
= consumer_allocate_stream(channel
->key
,
619 LTTNG_CONSUMER_ACTIVE_STREAM
,
629 if (new_stream
== NULL
) {
634 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
640 new_stream
->chan
= channel
;
641 new_stream
->wait_fd
= fd
;
642 switch (channel
->output
) {
643 case CONSUMER_CHANNEL_SPLICE
:
644 new_stream
->output
= LTTNG_EVENT_SPLICE
;
646 case CONSUMER_CHANNEL_MMAP
:
647 new_stream
->output
= LTTNG_EVENT_MMAP
;
650 ERR("Stream output unknown %d", channel
->output
);
655 * We've just assigned the channel to the stream so increment the
656 * refcount right now. We don't need to increment the refcount for
657 * streams in no monitor because we handle manually the cleanup of
658 * those. It is very important to make sure there is NO prior
659 * consumer_del_stream() calls or else the refcount will be unbalanced.
661 if (channel
->monitor
) {
662 uatomic_inc(&new_stream
->chan
->refcount
);
666 * The buffer flush is done on the session daemon side for the kernel
667 * so no need for the stream "hangup_flush_done" variable to be
668 * tracked. This is important for a kernel stream since we don't rely
669 * on the flush state of the stream to read data. It's not the case for
670 * user space tracing.
672 new_stream
->hangup_flush_done
= 0;
674 health_code_update();
676 if (ctx
->on_recv_stream
) {
677 ret
= ctx
->on_recv_stream(new_stream
);
679 consumer_stream_free(new_stream
);
684 health_code_update();
686 if (new_stream
->metadata_flag
) {
687 channel
->metadata_stream
= new_stream
;
690 /* Do not monitor this stream. */
691 if (!channel
->monitor
) {
692 DBG("Kernel consumer add stream %s in no monitor mode with "
693 "relayd id %" PRIu64
, new_stream
->name
,
694 new_stream
->net_seq_idx
);
695 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
699 /* Send stream to relayd if the stream has an ID. */
700 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
701 ret
= consumer_send_relayd_stream(new_stream
,
702 new_stream
->chan
->pathname
);
704 consumer_stream_free(new_stream
);
709 /* Get the right pipe where the stream will be sent. */
710 if (new_stream
->metadata_flag
) {
711 ret
= consumer_add_metadata_stream(new_stream
);
713 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
715 consumer_stream_free(new_stream
);
718 stream_pipe
= ctx
->consumer_metadata_pipe
;
720 ret
= consumer_add_data_stream(new_stream
);
722 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
724 consumer_stream_free(new_stream
);
727 stream_pipe
= ctx
->consumer_data_pipe
;
730 /* Vitible to other threads */
731 new_stream
->globally_visible
= 1;
733 health_code_update();
735 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
737 ERR("Consumer write %s stream to pipe %d",
738 new_stream
->metadata_flag
? "metadata" : "data",
739 lttng_pipe_get_writefd(stream_pipe
));
740 if (new_stream
->metadata_flag
) {
741 consumer_del_stream_for_metadata(new_stream
);
743 consumer_del_stream_for_data(new_stream
);
748 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
749 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
752 case LTTNG_CONSUMER_STREAMS_SENT
:
754 struct lttng_consumer_channel
*channel
;
757 * Get stream's channel reference. Needed when adding the stream to the
760 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
763 * We could not find the channel. Can happen if cpu hotplug
764 * happens while tearing down.
766 ERR("Unable to find channel key %" PRIu64
,
767 msg
.u
.sent_streams
.channel_key
);
768 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
771 health_code_update();
774 * Send status code to session daemon.
776 ret
= consumer_send_status_msg(sock
, ret_code
);
778 /* Somehow, the session daemon is not responding anymore. */
782 health_code_update();
785 * We should not send this message if we don't monitor the
786 * streams in this channel.
788 if (!channel
->monitor
) {
792 health_code_update();
793 /* Send stream to relayd if the stream has an ID. */
794 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
795 ret
= consumer_send_relayd_streams_sent(
796 msg
.u
.sent_streams
.net_seq_idx
);
803 case LTTNG_CONSUMER_UPDATE_STREAM
:
808 case LTTNG_CONSUMER_DESTROY_RELAYD
:
810 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
811 struct consumer_relayd_sock_pair
*relayd
;
813 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
815 /* Get relayd reference if exists. */
816 relayd
= consumer_find_relayd(index
);
817 if (relayd
== NULL
) {
818 DBG("Unable to find relayd %" PRIu64
, index
);
819 ret_code
= LTTNG_ERR_NO_CONSUMER
;
823 * Each relayd socket pair has a refcount of stream attached to it
824 * which tells if the relayd is still active or not depending on the
827 * This will set the destroy flag of the relayd object and destroy it
828 * if the refcount reaches zero when called.
830 * The destroy can happen either here or when a stream fd hangs up.
833 consumer_flag_relayd_for_destroy(relayd
);
836 health_code_update();
838 ret
= consumer_send_status_msg(sock
, ret_code
);
840 /* Somehow, the session daemon is not responding anymore. */
846 case LTTNG_CONSUMER_DATA_PENDING
:
849 uint64_t id
= msg
.u
.data_pending
.session_id
;
851 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
853 ret
= consumer_data_pending(id
);
855 health_code_update();
857 /* Send back returned value to session daemon */
858 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
860 PERROR("send data pending ret code");
865 * No need to send back a status message since the data pending
866 * returned value is the response.
870 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
872 if (msg
.u
.snapshot_channel
.metadata
== 1) {
873 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
874 msg
.u
.snapshot_channel
.pathname
,
875 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
877 ERR("Snapshot metadata failed");
878 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
881 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
882 msg
.u
.snapshot_channel
.pathname
,
883 msg
.u
.snapshot_channel
.relayd_id
,
884 msg
.u
.snapshot_channel
.max_stream_size
,
887 ERR("Snapshot channel failed");
888 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
892 health_code_update();
894 ret
= consumer_send_status_msg(sock
, ret_code
);
896 /* Somehow, the session daemon is not responding anymore. */
901 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
903 uint64_t key
= msg
.u
.destroy_channel
.key
;
904 struct lttng_consumer_channel
*channel
;
906 channel
= consumer_find_channel(key
);
908 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
909 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
912 health_code_update();
914 ret
= consumer_send_status_msg(sock
, ret_code
);
916 /* Somehow, the session daemon is not responding anymore. */
920 health_code_update();
922 /* Stop right now if no channel was found. */
928 * This command should ONLY be issued for channel with streams set in
931 assert(!channel
->monitor
);
934 * The refcount should ALWAYS be 0 in the case of a channel in no
937 assert(!uatomic_sub_return(&channel
->refcount
, 1));
939 consumer_del_channel(channel
);
951 * Return 1 to indicate success since the 0 value can be a socket
952 * shutdown during the recv() or send() call.
954 health_code_update();
959 /* This will issue a consumer stop. */
964 * Populate index values of a kernel stream. Values are set in big endian order.
966 * Return 0 on success or else a negative value.
968 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
972 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
974 PERROR("kernctl_get_timestamp_begin");
977 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
979 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
981 PERROR("kernctl_get_timestamp_end");
984 index
->timestamp_end
= htobe64(index
->timestamp_end
);
986 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
988 PERROR("kernctl_get_events_discarded");
991 index
->events_discarded
= htobe64(index
->events_discarded
);
993 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
995 PERROR("kernctl_get_content_size");
998 index
->content_size
= htobe64(index
->content_size
);
1000 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1002 PERROR("kernctl_get_packet_size");
1005 index
->packet_size
= htobe64(index
->packet_size
);
1007 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1009 PERROR("kernctl_get_stream_id");
1012 index
->stream_id
= htobe64(index
->stream_id
);
1018 * Sync metadata meaning request them to the session daemon and snapshot to the
1019 * metadata thread can consumer them.
1021 * Metadata stream lock MUST be acquired.
1023 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1024 * is empty or a negative value on error.
1026 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1032 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1034 ERR("Failed to flush kernel stream");
1038 ret
= kernctl_snapshot(metadata
->wait_fd
);
1040 if (errno
!= EAGAIN
) {
1041 ERR("Sync metadata, taking kernel snapshot failed.");
1044 DBG("Sync metadata, no new kernel metadata");
1045 /* No new metadata, exit. */
1055 * Consume data on a file descriptor and write it on a trace file.
1057 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1058 struct lttng_consumer_local_data
*ctx
)
1060 unsigned long len
, subbuf_size
, padding
;
1061 int err
, write_index
= 1;
1063 int infd
= stream
->wait_fd
;
1064 struct ctf_packet_index index
;
1066 DBG("In read_subbuffer (infd : %d)", infd
);
1068 /* Get the next subbuffer */
1069 err
= kernctl_get_next_subbuf(infd
);
1072 * This is a debug message even for single-threaded consumer,
1073 * because poll() have more relaxed criterions than get subbuf,
1074 * so get_subbuf may fail for short race windows where poll()
1075 * would issue wakeups.
1077 DBG("Reserving sub buffer failed (everything is normal, "
1078 "it is due to concurrency)");
1083 /* Get the full subbuffer size including padding */
1084 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1086 perror("Getting sub-buffer len failed.");
1091 if (!stream
->metadata_flag
) {
1092 ret
= get_index_values(&index
, infd
);
1100 switch (stream
->chan
->output
) {
1101 case CONSUMER_CHANNEL_SPLICE
:
1103 * XXX: The lttng-modules splice "actor" does not handle copying
1104 * partial pages hence only using the subbuffer size without the
1105 * padding makes the splice fail.
1110 /* splice the subbuffer to the tracefile */
1111 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1114 * XXX: Splice does not support network streaming so the return value
1115 * is simply checked against subbuf_size and not like the mmap() op.
1117 if (ret
!= subbuf_size
) {
1119 * display the error but continue processing to try
1120 * to release the subbuffer
1122 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1127 case CONSUMER_CHANNEL_MMAP
:
1128 /* Get subbuffer size without padding */
1129 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1131 perror("Getting sub-buffer len failed.");
1136 /* Make sure the tracer is not gone mad on us! */
1137 assert(len
>= subbuf_size
);
1139 padding
= len
- subbuf_size
;
1141 /* write the subbuffer to the tracefile */
1142 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1145 * The mmap operation should write subbuf_size amount of data when
1146 * network streaming or the full padding (len) size when we are _not_
1149 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1150 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1152 * Display the error but continue processing to try to release the
1153 * subbuffer. This is a DBG statement since this is possible to
1154 * happen without being a critical error.
1156 DBG("Error writing to tracefile "
1157 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1158 ret
, len
, subbuf_size
);
1163 ERR("Unknown output method");
1167 err
= kernctl_put_next_subbuf(infd
);
1169 if (errno
== EFAULT
) {
1170 perror("Error in unreserving sub buffer\n");
1171 } else if (errno
== EIO
) {
1172 /* Should never happen with newer LTTng versions */
1173 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1179 /* Write index if needed. */
1184 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1186 * In live, block until all the metadata is sent.
1188 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1194 err
= consumer_stream_write_index(stream
, &index
);
1203 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1210 * Don't create anything if this is set for streaming or should not be
1213 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1214 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1215 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1216 stream
->uid
, stream
->gid
, NULL
);
1220 stream
->out_fd
= ret
;
1221 stream
->tracefile_size_current
= 0;
1223 if (!stream
->metadata_flag
) {
1224 ret
= index_create_file(stream
->chan
->pathname
,
1225 stream
->name
, stream
->uid
, stream
->gid
,
1226 stream
->chan
->tracefile_size
,
1227 stream
->tracefile_count_current
);
1231 stream
->index_fd
= ret
;
1235 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1236 /* get the len of the mmap region */
1237 unsigned long mmap_len
;
1239 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1241 PERROR("kernctl_get_mmap_len");
1243 goto error_close_fd
;
1245 stream
->mmap_len
= (size_t) mmap_len
;
1247 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1248 MAP_PRIVATE
, stream
->wait_fd
, 0);
1249 if (stream
->mmap_base
== MAP_FAILED
) {
1250 PERROR("Error mmaping");
1252 goto error_close_fd
;
1256 /* we return 0 to let the library handle the FD internally */
1260 if (stream
->out_fd
>= 0) {
1263 err
= close(stream
->out_fd
);
1265 stream
->out_fd
= -1;
1272 * Check if data is still being extracted from the buffers for a specific
1273 * stream. Consumer data lock MUST be acquired before calling this function
1274 * and the stream lock.
1276 * Return 1 if the traced data are still getting read else 0 meaning that the
1277 * data is available for trace viewer reading.
1279 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1285 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1290 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1292 /* There is still data so let's put back this subbuffer. */
1293 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1295 ret
= 1; /* Data is pending */
1299 /* Data is NOT pending and ready to be read. */