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/pipe.h>
39 #include <common/relayd/relayd.h>
40 #include <common/utils.h>
41 #include <common/consumer-stream.h>
42 #include <common/index/index.h>
43 #include <common/consumer-timer.h>
45 #include "kernel-consumer.h"
47 extern struct lttng_consumer_global_data consumer_data
;
48 extern int consumer_poll_timeout
;
49 extern volatile int consumer_quit
;
52 * Take a snapshot for a specific fd
54 * Returns 0 on success, < 0 on error
56 int lttng_kconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
59 int infd
= stream
->wait_fd
;
61 ret
= kernctl_snapshot(infd
);
63 perror("Getting sub-buffer snapshot.");
71 * Get the produced position
73 * Returns 0 on success, < 0 on error
75 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
79 int infd
= stream
->wait_fd
;
81 ret
= kernctl_snapshot_get_produced(infd
, pos
);
83 perror("kernctl_snapshot_get_produced");
91 * Get the consumerd position
93 * Returns 0 on success, < 0 on error
95 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
99 int infd
= stream
->wait_fd
;
101 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
103 perror("kernctl_snapshot_get_consumed");
111 * Take a snapshot of all the stream of a channel
113 * Returns 0 on success, < 0 on error
115 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
116 uint64_t relayd_id
, uint64_t max_stream_size
,
117 struct lttng_consumer_local_data
*ctx
)
120 unsigned long consumed_pos
, produced_pos
;
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
) {
144 health_code_update();
147 * Lock stream because we are about to change its state.
149 pthread_mutex_lock(&stream
->lock
);
152 * Assign the received relayd ID so we can use it for streaming. The streams
153 * are not visible to anyone so this is OK to change it.
155 stream
->net_seq_idx
= relayd_id
;
156 channel
->relayd_id
= relayd_id
;
157 if (relayd_id
!= (uint64_t) -1ULL) {
158 ret
= consumer_send_relayd_stream(stream
, path
);
160 ERR("sending stream to relayd");
164 ret
= utils_create_stream_file(path
, stream
->name
,
165 stream
->chan
->tracefile_size
,
166 stream
->tracefile_count_current
,
167 stream
->uid
, stream
->gid
, NULL
);
169 ERR("utils_create_stream_file");
173 stream
->out_fd
= ret
;
174 stream
->tracefile_size_current
= 0;
176 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
177 path
, stream
->name
, stream
->key
);
179 ret
= consumer_send_relayd_streams_sent(relayd_id
);
181 ERR("sending streams sent to relayd");
185 ret
= kernctl_buffer_flush(stream
->wait_fd
);
187 ERR("Failed to flush kernel stream");
192 ret
= lttng_kconsumer_take_snapshot(stream
);
194 ERR("Taking kernel snapshot");
198 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
200 ERR("Produced kernel snapshot position");
204 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
206 ERR("Consumerd kernel snapshot position");
210 if (stream
->max_sb_size
== 0) {
211 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
212 &stream
->max_sb_size
);
214 ERR("Getting kernel max_sb_size");
221 * The original value is sent back if max stream size is larger than
222 * the possible size of the snapshot. Also, we asume that the session
223 * daemon should never send a maximum stream size that is lower than
226 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
227 produced_pos
, max_stream_size
);
229 while (consumed_pos
< produced_pos
) {
231 unsigned long len
, padded_len
;
233 health_code_update();
235 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
237 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
239 if (errno
!= EAGAIN
) {
240 PERROR("kernctl_get_subbuf snapshot");
244 DBG("Kernel consumer get subbuf failed. Skipping it.");
245 consumed_pos
+= stream
->max_sb_size
;
249 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
251 ERR("Snapshot kernctl_get_subbuf_size");
253 goto error_put_subbuf
;
256 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
258 ERR("Snapshot kernctl_get_padded_subbuf_size");
260 goto error_put_subbuf
;
263 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
264 padded_len
- len
, NULL
);
266 * We write the padded len in local tracefiles but the data len
267 * when using a relay. Display the error but continue processing
268 * to try to release the subbuffer.
270 if (relayd_id
!= (uint64_t) -1ULL) {
271 if (read_len
!= len
) {
272 ERR("Error sending to the relay (ret: %zd != len: %lu)",
276 if (read_len
!= padded_len
) {
277 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
278 read_len
, padded_len
);
282 ret
= kernctl_put_subbuf(stream
->wait_fd
);
284 ERR("Snapshot kernctl_put_subbuf");
288 consumed_pos
+= stream
->max_sb_size
;
291 if (relayd_id
== (uint64_t) -1ULL) {
292 if (stream
->out_fd
>= 0) {
293 ret
= close(stream
->out_fd
);
295 PERROR("Kernel consumer snapshot close out_fd");
301 close_relayd_stream(stream
);
302 stream
->net_seq_idx
= (uint64_t) -1ULL;
304 pthread_mutex_unlock(&stream
->lock
);
312 ret
= kernctl_put_subbuf(stream
->wait_fd
);
315 ERR("Snapshot kernctl_put_subbuf error path");
318 pthread_mutex_unlock(&stream
->lock
);
325 * Read the whole metadata available for a snapshot.
327 * Returns 0 on success, < 0 on error
329 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
330 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
332 int ret
, use_relayd
= 0;
334 struct lttng_consumer_channel
*metadata_channel
;
335 struct lttng_consumer_stream
*metadata_stream
;
339 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
344 metadata_channel
= consumer_find_channel(key
);
345 if (!metadata_channel
) {
346 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
351 metadata_stream
= metadata_channel
->metadata_stream
;
352 assert(metadata_stream
);
354 /* Flag once that we have a valid relayd for the stream. */
355 if (relayd_id
!= (uint64_t) -1ULL) {
360 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
365 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
366 metadata_stream
->chan
->tracefile_size
,
367 metadata_stream
->tracefile_count_current
,
368 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
372 metadata_stream
->out_fd
= ret
;
376 health_code_update();
378 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
380 if (ret_read
!= -EAGAIN
) {
381 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
385 /* ret_read is negative at this point so we will exit the loop. */
388 } while (ret_read
>= 0);
391 close_relayd_stream(metadata_stream
);
392 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
394 if (metadata_stream
->out_fd
>= 0) {
395 ret
= close(metadata_stream
->out_fd
);
397 PERROR("Kernel consumer snapshot metadata close out_fd");
399 * Don't go on error here since the snapshot was successful at this
400 * point but somehow the close failed.
403 metadata_stream
->out_fd
= -1;
409 cds_list_del(&metadata_stream
->send_node
);
410 consumer_stream_destroy(metadata_stream
, NULL
);
411 metadata_channel
->metadata_stream
= NULL
;
418 * Receive command from session daemon and process it.
420 * Return 1 on success else a negative value or 0.
422 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
423 int sock
, struct pollfd
*consumer_sockpoll
)
426 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
427 struct lttcomm_consumer_msg msg
;
429 health_code_update();
431 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
432 if (ret
!= sizeof(msg
)) {
434 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
440 health_code_update();
442 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
444 * Notify the session daemon that the command is completed.
446 * On transport layer error, the function call will print an error
447 * message so handling the returned code is a bit useless since we
448 * return an error code anyway.
450 (void) consumer_send_status_msg(sock
, ret_code
);
454 health_code_update();
456 /* relayd needs RCU read-side protection */
459 switch (msg
.cmd_type
) {
460 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
462 /* Session daemon status message are handled in the following call. */
463 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
464 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
465 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
466 msg
.u
.relayd_sock
.relayd_session_id
);
469 case LTTNG_CONSUMER_ADD_CHANNEL
:
471 struct lttng_consumer_channel
*new_channel
;
474 health_code_update();
476 /* First send a status message before receiving the fds. */
477 ret
= consumer_send_status_msg(sock
, ret_code
);
479 /* Somehow, the session daemon is not responding anymore. */
483 health_code_update();
485 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
486 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
487 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
488 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
489 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
490 msg
.u
.channel
.tracefile_size
,
491 msg
.u
.channel
.tracefile_count
, 0,
492 msg
.u
.channel
.monitor
,
493 msg
.u
.channel
.live_timer_interval
);
494 if (new_channel
== NULL
) {
495 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
498 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
499 switch (msg
.u
.channel
.output
) {
500 case LTTNG_EVENT_SPLICE
:
501 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
503 case LTTNG_EVENT_MMAP
:
504 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
507 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
511 /* Translate and save channel type. */
512 switch (msg
.u
.channel
.type
) {
513 case CONSUMER_CHANNEL_TYPE_DATA
:
514 case CONSUMER_CHANNEL_TYPE_METADATA
:
515 new_channel
->type
= msg
.u
.channel
.type
;
522 health_code_update();
524 if (ctx
->on_recv_channel
!= NULL
) {
525 ret_recv
= ctx
->on_recv_channel(new_channel
);
527 ret
= consumer_add_channel(new_channel
, ctx
);
528 } else if (ret_recv
< 0) {
532 ret
= consumer_add_channel(new_channel
, ctx
);
534 if (CONSUMER_CHANNEL_TYPE_DATA
) {
535 consumer_timer_live_start(new_channel
,
536 msg
.u
.channel
.live_timer_interval
);
539 health_code_update();
541 /* If we received an error in add_channel, we need to report it. */
543 ret
= consumer_send_status_msg(sock
, ret
);
552 case LTTNG_CONSUMER_ADD_STREAM
:
555 struct lttng_pipe
*stream_pipe
;
556 struct lttng_consumer_stream
*new_stream
;
557 struct lttng_consumer_channel
*channel
;
561 * Get stream's channel reference. Needed when adding the stream to the
564 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
567 * We could not find the channel. Can happen if cpu hotplug
568 * happens while tearing down.
570 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
571 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
574 health_code_update();
576 /* First send a status message before receiving the fds. */
577 ret
= consumer_send_status_msg(sock
, ret_code
);
579 /* Somehow, the session daemon is not responding anymore. */
583 health_code_update();
585 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
586 /* Channel was not found. */
592 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
599 health_code_update();
601 /* Get stream file descriptor from socket */
602 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
603 if (ret
!= sizeof(fd
)) {
604 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
609 health_code_update();
612 * Send status code to session daemon only if the recv works. If the
613 * above recv() failed, the session daemon is notified through the
614 * error socket and the teardown is eventually done.
616 ret
= consumer_send_status_msg(sock
, ret_code
);
618 /* Somehow, the session daemon is not responding anymore. */
622 health_code_update();
624 new_stream
= consumer_allocate_stream(channel
->key
,
626 LTTNG_CONSUMER_ACTIVE_STREAM
,
636 if (new_stream
== NULL
) {
641 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
647 new_stream
->chan
= channel
;
648 new_stream
->wait_fd
= fd
;
649 switch (channel
->output
) {
650 case CONSUMER_CHANNEL_SPLICE
:
651 new_stream
->output
= LTTNG_EVENT_SPLICE
;
653 case CONSUMER_CHANNEL_MMAP
:
654 new_stream
->output
= LTTNG_EVENT_MMAP
;
657 ERR("Stream output unknown %d", channel
->output
);
662 * We've just assigned the channel to the stream so increment the
663 * refcount right now. We don't need to increment the refcount for
664 * streams in no monitor because we handle manually the cleanup of
665 * those. It is very important to make sure there is NO prior
666 * consumer_del_stream() calls or else the refcount will be unbalanced.
668 if (channel
->monitor
) {
669 uatomic_inc(&new_stream
->chan
->refcount
);
673 * The buffer flush is done on the session daemon side for the kernel
674 * so no need for the stream "hangup_flush_done" variable to be
675 * tracked. This is important for a kernel stream since we don't rely
676 * on the flush state of the stream to read data. It's not the case for
677 * user space tracing.
679 new_stream
->hangup_flush_done
= 0;
681 health_code_update();
683 if (ctx
->on_recv_stream
) {
684 ret
= ctx
->on_recv_stream(new_stream
);
686 consumer_stream_free(new_stream
);
691 health_code_update();
693 if (new_stream
->metadata_flag
) {
694 channel
->metadata_stream
= new_stream
;
697 /* Do not monitor this stream. */
698 if (!channel
->monitor
) {
699 DBG("Kernel consumer add stream %s in no monitor mode with "
700 "relayd id %" PRIu64
, new_stream
->name
,
701 new_stream
->net_seq_idx
);
702 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
706 /* Send stream to relayd if the stream has an ID. */
707 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
708 ret
= consumer_send_relayd_stream(new_stream
,
709 new_stream
->chan
->pathname
);
711 consumer_stream_free(new_stream
);
716 /* Get the right pipe where the stream will be sent. */
717 if (new_stream
->metadata_flag
) {
718 ret
= consumer_add_metadata_stream(new_stream
);
720 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
722 consumer_stream_free(new_stream
);
725 stream_pipe
= ctx
->consumer_metadata_pipe
;
727 ret
= consumer_add_data_stream(new_stream
);
729 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
731 consumer_stream_free(new_stream
);
734 stream_pipe
= ctx
->consumer_data_pipe
;
737 /* Vitible to other threads */
738 new_stream
->globally_visible
= 1;
740 health_code_update();
742 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
744 ERR("Consumer write %s stream to pipe %d",
745 new_stream
->metadata_flag
? "metadata" : "data",
746 lttng_pipe_get_writefd(stream_pipe
));
747 if (new_stream
->metadata_flag
) {
748 consumer_del_stream_for_metadata(new_stream
);
750 consumer_del_stream_for_data(new_stream
);
755 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
756 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
759 case LTTNG_CONSUMER_STREAMS_SENT
:
761 struct lttng_consumer_channel
*channel
;
764 * Get stream's channel reference. Needed when adding the stream to the
767 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
770 * We could not find the channel. Can happen if cpu hotplug
771 * happens while tearing down.
773 ERR("Unable to find channel key %" PRIu64
,
774 msg
.u
.sent_streams
.channel_key
);
775 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
778 health_code_update();
781 * Send status code to session daemon.
783 ret
= consumer_send_status_msg(sock
, ret_code
);
785 /* Somehow, the session daemon is not responding anymore. */
789 health_code_update();
792 * We should not send this message if we don't monitor the
793 * streams in this channel.
795 if (!channel
->monitor
) {
799 health_code_update();
800 /* Send stream to relayd if the stream has an ID. */
801 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
802 ret
= consumer_send_relayd_streams_sent(
803 msg
.u
.sent_streams
.net_seq_idx
);
810 case LTTNG_CONSUMER_UPDATE_STREAM
:
815 case LTTNG_CONSUMER_DESTROY_RELAYD
:
817 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
818 struct consumer_relayd_sock_pair
*relayd
;
820 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
822 /* Get relayd reference if exists. */
823 relayd
= consumer_find_relayd(index
);
824 if (relayd
== NULL
) {
825 DBG("Unable to find relayd %" PRIu64
, index
);
826 ret_code
= LTTNG_ERR_NO_CONSUMER
;
830 * Each relayd socket pair has a refcount of stream attached to it
831 * which tells if the relayd is still active or not depending on the
834 * This will set the destroy flag of the relayd object and destroy it
835 * if the refcount reaches zero when called.
837 * The destroy can happen either here or when a stream fd hangs up.
840 consumer_flag_relayd_for_destroy(relayd
);
843 health_code_update();
845 ret
= consumer_send_status_msg(sock
, ret_code
);
847 /* Somehow, the session daemon is not responding anymore. */
853 case LTTNG_CONSUMER_DATA_PENDING
:
856 uint64_t id
= msg
.u
.data_pending
.session_id
;
858 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
860 ret
= consumer_data_pending(id
);
862 health_code_update();
864 /* Send back returned value to session daemon */
865 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
867 PERROR("send data pending ret code");
872 * No need to send back a status message since the data pending
873 * returned value is the response.
877 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
879 if (msg
.u
.snapshot_channel
.metadata
== 1) {
880 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
881 msg
.u
.snapshot_channel
.pathname
,
882 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
884 ERR("Snapshot metadata failed");
885 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
888 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
889 msg
.u
.snapshot_channel
.pathname
,
890 msg
.u
.snapshot_channel
.relayd_id
,
891 msg
.u
.snapshot_channel
.max_stream_size
,
894 ERR("Snapshot channel failed");
895 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
899 health_code_update();
901 ret
= consumer_send_status_msg(sock
, ret_code
);
903 /* Somehow, the session daemon is not responding anymore. */
908 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
910 uint64_t key
= msg
.u
.destroy_channel
.key
;
911 struct lttng_consumer_channel
*channel
;
913 channel
= consumer_find_channel(key
);
915 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
916 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
919 health_code_update();
921 ret
= consumer_send_status_msg(sock
, ret_code
);
923 /* Somehow, the session daemon is not responding anymore. */
927 health_code_update();
930 * This command should ONLY be issued for channel with streams set in
933 assert(!channel
->monitor
);
936 * The refcount should ALWAYS be 0 in the case of a channel in no
939 assert(!uatomic_sub_return(&channel
->refcount
, 1));
941 consumer_del_channel(channel
);
953 * Return 1 to indicate success since the 0 value can be a socket
954 * shutdown during the recv() or send() call.
956 health_code_update();
961 /* This will issue a consumer stop. */
966 * Populate index values of a kernel stream. Values are set in big endian order.
968 * Return 0 on success or else a negative value.
970 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
974 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
976 PERROR("kernctl_get_timestamp_begin");
979 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
981 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
983 PERROR("kernctl_get_timestamp_end");
986 index
->timestamp_end
= htobe64(index
->timestamp_end
);
988 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
990 PERROR("kernctl_get_events_discarded");
993 index
->events_discarded
= htobe64(index
->events_discarded
);
995 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
997 PERROR("kernctl_get_content_size");
1000 index
->content_size
= htobe64(index
->content_size
);
1002 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1004 PERROR("kernctl_get_packet_size");
1007 index
->packet_size
= htobe64(index
->packet_size
);
1009 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1011 PERROR("kernctl_get_stream_id");
1014 index
->stream_id
= htobe64(index
->stream_id
);
1020 * Sync metadata meaning request them to the session daemon and snapshot to the
1021 * metadata thread can consumer them.
1023 * Metadata stream lock MUST be acquired.
1025 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1026 * is empty or a negative value on error.
1028 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1034 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1036 ERR("Failed to flush kernel stream");
1040 ret
= kernctl_snapshot(metadata
->wait_fd
);
1042 if (errno
!= EAGAIN
) {
1043 ERR("Sync metadata, taking kernel snapshot failed.");
1046 DBG("Sync metadata, no new kernel metadata");
1047 /* No new metadata, exit. */
1057 * Consume data on a file descriptor and write it on a trace file.
1059 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1060 struct lttng_consumer_local_data
*ctx
)
1062 unsigned long len
, subbuf_size
, padding
;
1063 int err
, write_index
= 1;
1065 int infd
= stream
->wait_fd
;
1066 struct ctf_packet_index index
;
1068 DBG("In read_subbuffer (infd : %d)", infd
);
1070 /* Get the next subbuffer */
1071 err
= kernctl_get_next_subbuf(infd
);
1074 * This is a debug message even for single-threaded consumer,
1075 * because poll() have more relaxed criterions than get subbuf,
1076 * so get_subbuf may fail for short race windows where poll()
1077 * would issue wakeups.
1079 DBG("Reserving sub buffer failed (everything is normal, "
1080 "it is due to concurrency)");
1085 /* Get the full subbuffer size including padding */
1086 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1088 perror("Getting sub-buffer len failed.");
1093 if (!stream
->metadata_flag
) {
1094 ret
= get_index_values(&index
, infd
);
1102 switch (stream
->chan
->output
) {
1103 case CONSUMER_CHANNEL_SPLICE
:
1105 * XXX: The lttng-modules splice "actor" does not handle copying
1106 * partial pages hence only using the subbuffer size without the
1107 * padding makes the splice fail.
1112 /* splice the subbuffer to the tracefile */
1113 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1116 * XXX: Splice does not support network streaming so the return value
1117 * is simply checked against subbuf_size and not like the mmap() op.
1119 if (ret
!= subbuf_size
) {
1121 * display the error but continue processing to try
1122 * to release the subbuffer
1124 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1129 case CONSUMER_CHANNEL_MMAP
:
1130 /* Get subbuffer size without padding */
1131 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1133 perror("Getting sub-buffer len failed.");
1138 /* Make sure the tracer is not gone mad on us! */
1139 assert(len
>= subbuf_size
);
1141 padding
= len
- subbuf_size
;
1143 /* write the subbuffer to the tracefile */
1144 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1147 * The mmap operation should write subbuf_size amount of data when
1148 * network streaming or the full padding (len) size when we are _not_
1151 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1152 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1154 * Display the error but continue processing to try to release the
1157 ERR("Error writing to tracefile "
1158 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1159 ret
, len
, subbuf_size
);
1164 ERR("Unknown output method");
1168 err
= kernctl_put_next_subbuf(infd
);
1170 if (errno
== EFAULT
) {
1171 perror("Error in unreserving sub buffer\n");
1172 } else if (errno
== EIO
) {
1173 /* Should never happen with newer LTTng versions */
1174 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1180 /* Write index if needed. */
1185 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1187 * In live, block until all the metadata is sent.
1189 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1195 err
= consumer_stream_write_index(stream
, &index
);
1204 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1211 * Don't create anything if this is set for streaming or should not be
1214 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1215 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1216 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1217 stream
->uid
, stream
->gid
, NULL
);
1221 stream
->out_fd
= ret
;
1222 stream
->tracefile_size_current
= 0;
1224 if (!stream
->metadata_flag
) {
1225 ret
= index_create_file(stream
->chan
->pathname
,
1226 stream
->name
, stream
->uid
, stream
->gid
,
1227 stream
->chan
->tracefile_size
,
1228 stream
->tracefile_count_current
);
1232 stream
->index_fd
= ret
;
1236 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1237 /* get the len of the mmap region */
1238 unsigned long mmap_len
;
1240 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1242 PERROR("kernctl_get_mmap_len");
1244 goto error_close_fd
;
1246 stream
->mmap_len
= (size_t) mmap_len
;
1248 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1249 MAP_PRIVATE
, stream
->wait_fd
, 0);
1250 if (stream
->mmap_base
== MAP_FAILED
) {
1251 PERROR("Error mmaping");
1253 goto error_close_fd
;
1257 /* we return 0 to let the library handle the FD internally */
1261 if (stream
->out_fd
>= 0) {
1264 err
= close(stream
->out_fd
);
1266 stream
->out_fd
= -1;
1273 * Check if data is still being extracted from the buffers for a specific
1274 * stream. Consumer data lock MUST be acquired before calling this function
1275 * and the stream lock.
1277 * Return 1 if the traced data are still getting read else 0 meaning that the
1278 * data is available for trace viewer reading.
1280 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1286 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1291 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1293 /* There is still data so let's put back this subbuffer. */
1294 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1296 ret
= 1; /* Data is pending */
1300 /* Data is NOT pending and ready to be read. */