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 nb_packets_per_stream
,
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 if (relayd_id
!= -1ULL) {
180 ret
= consumer_send_relayd_streams_sent(relayd_id
);
182 ERR("sending streams sent to relayd");
187 ret
= kernctl_buffer_flush(stream
->wait_fd
);
189 ERR("Failed to flush kernel stream");
194 ret
= lttng_kconsumer_take_snapshot(stream
);
196 ERR("Taking kernel snapshot");
200 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
202 ERR("Produced kernel snapshot position");
206 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
208 ERR("Consumerd kernel snapshot position");
212 if (stream
->max_sb_size
== 0) {
213 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
214 &stream
->max_sb_size
);
216 ERR("Getting kernel max_sb_size");
222 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
223 produced_pos
, nb_packets_per_stream
,
224 stream
->max_sb_size
);
226 while (consumed_pos
< produced_pos
) {
228 unsigned long len
, padded_len
;
230 health_code_update();
232 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
234 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
236 if (errno
!= EAGAIN
) {
237 PERROR("kernctl_get_subbuf snapshot");
241 DBG("Kernel consumer get subbuf failed. Skipping it.");
242 consumed_pos
+= stream
->max_sb_size
;
246 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
248 ERR("Snapshot kernctl_get_subbuf_size");
250 goto error_put_subbuf
;
253 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
255 ERR("Snapshot kernctl_get_padded_subbuf_size");
257 goto error_put_subbuf
;
260 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
261 padded_len
- len
, NULL
);
263 * We write the padded len in local tracefiles but the data len
264 * when using a relay. Display the error but continue processing
265 * to try to release the subbuffer.
267 if (relayd_id
!= (uint64_t) -1ULL) {
268 if (read_len
!= len
) {
269 ERR("Error sending to the relay (ret: %zd != len: %lu)",
273 if (read_len
!= padded_len
) {
274 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
275 read_len
, padded_len
);
279 ret
= kernctl_put_subbuf(stream
->wait_fd
);
281 ERR("Snapshot kernctl_put_subbuf");
285 consumed_pos
+= stream
->max_sb_size
;
288 if (relayd_id
== (uint64_t) -1ULL) {
289 if (stream
->out_fd
>= 0) {
290 ret
= close(stream
->out_fd
);
292 PERROR("Kernel consumer snapshot close out_fd");
298 close_relayd_stream(stream
);
299 stream
->net_seq_idx
= (uint64_t) -1ULL;
301 pthread_mutex_unlock(&stream
->lock
);
309 ret
= kernctl_put_subbuf(stream
->wait_fd
);
312 ERR("Snapshot kernctl_put_subbuf error path");
315 pthread_mutex_unlock(&stream
->lock
);
322 * Read the whole metadata available for a snapshot.
324 * Returns 0 on success, < 0 on error
326 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
327 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
329 int ret
, use_relayd
= 0;
331 struct lttng_consumer_channel
*metadata_channel
;
332 struct lttng_consumer_stream
*metadata_stream
;
336 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
341 metadata_channel
= consumer_find_channel(key
);
342 if (!metadata_channel
) {
343 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
348 metadata_stream
= metadata_channel
->metadata_stream
;
349 assert(metadata_stream
);
351 /* Flag once that we have a valid relayd for the stream. */
352 if (relayd_id
!= (uint64_t) -1ULL) {
357 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
362 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
363 metadata_stream
->chan
->tracefile_size
,
364 metadata_stream
->tracefile_count_current
,
365 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
369 metadata_stream
->out_fd
= ret
;
373 health_code_update();
375 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
377 if (ret_read
!= -EAGAIN
) {
378 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
382 /* ret_read is negative at this point so we will exit the loop. */
385 } while (ret_read
>= 0);
388 close_relayd_stream(metadata_stream
);
389 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
391 if (metadata_stream
->out_fd
>= 0) {
392 ret
= close(metadata_stream
->out_fd
);
394 PERROR("Kernel consumer snapshot metadata close out_fd");
396 * Don't go on error here since the snapshot was successful at this
397 * point but somehow the close failed.
400 metadata_stream
->out_fd
= -1;
406 cds_list_del(&metadata_stream
->send_node
);
407 consumer_stream_destroy(metadata_stream
, NULL
);
408 metadata_channel
->metadata_stream
= NULL
;
415 * Receive command from session daemon and process it.
417 * Return 1 on success else a negative value or 0.
419 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
420 int sock
, struct pollfd
*consumer_sockpoll
)
423 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
424 struct lttcomm_consumer_msg msg
;
426 health_code_update();
428 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
429 if (ret
!= sizeof(msg
)) {
431 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
437 health_code_update();
439 /* Deprecated command */
440 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
442 health_code_update();
444 /* relayd needs RCU read-side protection */
447 switch (msg
.cmd_type
) {
448 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
450 /* Session daemon status message are handled in the following call. */
451 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
452 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
453 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
454 msg
.u
.relayd_sock
.relayd_session_id
);
457 case LTTNG_CONSUMER_ADD_CHANNEL
:
459 struct lttng_consumer_channel
*new_channel
;
462 health_code_update();
464 /* First send a status message before receiving the fds. */
465 ret
= consumer_send_status_msg(sock
, ret_code
);
467 /* Somehow, the session daemon is not responding anymore. */
471 health_code_update();
473 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
474 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
475 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
476 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
477 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
478 msg
.u
.channel
.tracefile_size
,
479 msg
.u
.channel
.tracefile_count
, 0,
480 msg
.u
.channel
.monitor
,
481 msg
.u
.channel
.live_timer_interval
);
482 if (new_channel
== NULL
) {
483 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
486 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
487 switch (msg
.u
.channel
.output
) {
488 case LTTNG_EVENT_SPLICE
:
489 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
491 case LTTNG_EVENT_MMAP
:
492 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
495 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
499 /* Translate and save channel type. */
500 switch (msg
.u
.channel
.type
) {
501 case CONSUMER_CHANNEL_TYPE_DATA
:
502 case CONSUMER_CHANNEL_TYPE_METADATA
:
503 new_channel
->type
= msg
.u
.channel
.type
;
510 health_code_update();
512 if (ctx
->on_recv_channel
!= NULL
) {
513 ret_recv
= ctx
->on_recv_channel(new_channel
);
515 ret
= consumer_add_channel(new_channel
, ctx
);
516 } else if (ret_recv
< 0) {
520 ret
= consumer_add_channel(new_channel
, ctx
);
522 if (CONSUMER_CHANNEL_TYPE_DATA
) {
523 consumer_timer_live_start(new_channel
,
524 msg
.u
.channel
.live_timer_interval
);
527 health_code_update();
529 /* If we received an error in add_channel, we need to report it. */
531 ret
= consumer_send_status_msg(sock
, ret
);
540 case LTTNG_CONSUMER_ADD_STREAM
:
543 struct lttng_pipe
*stream_pipe
;
544 struct lttng_consumer_stream
*new_stream
;
545 struct lttng_consumer_channel
*channel
;
549 * Get stream's channel reference. Needed when adding the stream to the
552 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
555 * We could not find the channel. Can happen if cpu hotplug
556 * happens while tearing down.
558 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
559 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
562 health_code_update();
564 /* First send a status message before receiving the fds. */
565 ret
= consumer_send_status_msg(sock
, ret_code
);
567 /* Somehow, the session daemon is not responding anymore. */
571 health_code_update();
573 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
574 /* Channel was not found. */
580 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
586 health_code_update();
588 /* Get stream file descriptor from socket */
589 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
590 if (ret
!= sizeof(fd
)) {
591 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
596 health_code_update();
599 * Send status code to session daemon only if the recv works. If the
600 * above recv() failed, the session daemon is notified through the
601 * error socket and the teardown is eventually done.
603 ret
= consumer_send_status_msg(sock
, ret_code
);
605 /* Somehow, the session daemon is not responding anymore. */
609 health_code_update();
611 new_stream
= consumer_allocate_stream(channel
->key
,
613 LTTNG_CONSUMER_ACTIVE_STREAM
,
623 if (new_stream
== NULL
) {
628 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
634 new_stream
->chan
= channel
;
635 new_stream
->wait_fd
= fd
;
636 switch (channel
->output
) {
637 case CONSUMER_CHANNEL_SPLICE
:
638 new_stream
->output
= LTTNG_EVENT_SPLICE
;
639 ret
= utils_create_pipe(new_stream
->splice_pipe
);
644 case CONSUMER_CHANNEL_MMAP
:
645 new_stream
->output
= LTTNG_EVENT_MMAP
;
648 ERR("Stream output unknown %d", channel
->output
);
653 * We've just assigned the channel to the stream so increment the
654 * refcount right now. We don't need to increment the refcount for
655 * streams in no monitor because we handle manually the cleanup of
656 * those. It is very important to make sure there is NO prior
657 * consumer_del_stream() calls or else the refcount will be unbalanced.
659 if (channel
->monitor
) {
660 uatomic_inc(&new_stream
->chan
->refcount
);
664 * The buffer flush is done on the session daemon side for the kernel
665 * so no need for the stream "hangup_flush_done" variable to be
666 * tracked. This is important for a kernel stream since we don't rely
667 * on the flush state of the stream to read data. It's not the case for
668 * user space tracing.
670 new_stream
->hangup_flush_done
= 0;
672 health_code_update();
674 if (ctx
->on_recv_stream
) {
675 ret
= ctx
->on_recv_stream(new_stream
);
677 consumer_stream_free(new_stream
);
682 health_code_update();
684 if (new_stream
->metadata_flag
) {
685 channel
->metadata_stream
= new_stream
;
688 /* Do not monitor this stream. */
689 if (!channel
->monitor
) {
690 DBG("Kernel consumer add stream %s in no monitor mode with "
691 "relayd id %" PRIu64
, new_stream
->name
,
692 new_stream
->net_seq_idx
);
693 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
697 /* Send stream to relayd if the stream has an ID. */
698 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
699 ret
= consumer_send_relayd_stream(new_stream
,
700 new_stream
->chan
->pathname
);
702 consumer_stream_free(new_stream
);
707 /* Get the right pipe where the stream will be sent. */
708 if (new_stream
->metadata_flag
) {
709 ret
= consumer_add_metadata_stream(new_stream
);
711 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
713 consumer_stream_free(new_stream
);
716 stream_pipe
= ctx
->consumer_metadata_pipe
;
718 ret
= consumer_add_data_stream(new_stream
);
720 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
722 consumer_stream_free(new_stream
);
725 stream_pipe
= ctx
->consumer_data_pipe
;
728 /* Vitible to other threads */
729 new_stream
->globally_visible
= 1;
731 health_code_update();
733 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
735 ERR("Consumer write %s stream to pipe %d",
736 new_stream
->metadata_flag
? "metadata" : "data",
737 lttng_pipe_get_writefd(stream_pipe
));
738 if (new_stream
->metadata_flag
) {
739 consumer_del_stream_for_metadata(new_stream
);
741 consumer_del_stream_for_data(new_stream
);
746 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
747 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
750 case LTTNG_CONSUMER_STREAMS_SENT
:
752 struct lttng_consumer_channel
*channel
;
755 * Get stream's channel reference. Needed when adding the stream to the
758 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
761 * We could not find the channel. Can happen if cpu hotplug
762 * happens while tearing down.
764 ERR("Unable to find channel key %" PRIu64
,
765 msg
.u
.sent_streams
.channel_key
);
766 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
769 health_code_update();
772 * Send status code to session daemon.
774 ret
= consumer_send_status_msg(sock
, ret_code
);
775 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
776 /* Somehow, the session daemon is not responding anymore. */
780 health_code_update();
783 * We should not send this message if we don't monitor the
784 * streams in this channel.
786 if (!channel
->monitor
) {
790 health_code_update();
791 /* Send stream to relayd if the stream has an ID. */
792 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
793 ret
= consumer_send_relayd_streams_sent(
794 msg
.u
.sent_streams
.net_seq_idx
);
801 case LTTNG_CONSUMER_UPDATE_STREAM
:
806 case LTTNG_CONSUMER_DESTROY_RELAYD
:
808 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
809 struct consumer_relayd_sock_pair
*relayd
;
811 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
813 /* Get relayd reference if exists. */
814 relayd
= consumer_find_relayd(index
);
815 if (relayd
== NULL
) {
816 DBG("Unable to find relayd %" PRIu64
, index
);
817 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
821 * Each relayd socket pair has a refcount of stream attached to it
822 * which tells if the relayd is still active or not depending on the
825 * This will set the destroy flag of the relayd object and destroy it
826 * if the refcount reaches zero when called.
828 * The destroy can happen either here or when a stream fd hangs up.
831 consumer_flag_relayd_for_destroy(relayd
);
834 health_code_update();
836 ret
= consumer_send_status_msg(sock
, ret_code
);
838 /* Somehow, the session daemon is not responding anymore. */
844 case LTTNG_CONSUMER_DATA_PENDING
:
847 uint64_t id
= msg
.u
.data_pending
.session_id
;
849 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
851 ret
= consumer_data_pending(id
);
853 health_code_update();
855 /* Send back returned value to session daemon */
856 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
858 PERROR("send data pending ret code");
863 * No need to send back a status message since the data pending
864 * returned value is the response.
868 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
870 if (msg
.u
.snapshot_channel
.metadata
== 1) {
871 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
872 msg
.u
.snapshot_channel
.pathname
,
873 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
875 ERR("Snapshot metadata failed");
876 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
879 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
880 msg
.u
.snapshot_channel
.pathname
,
881 msg
.u
.snapshot_channel
.relayd_id
,
882 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
885 ERR("Snapshot channel failed");
886 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
890 health_code_update();
892 ret
= consumer_send_status_msg(sock
, ret_code
);
894 /* Somehow, the session daemon is not responding anymore. */
899 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
901 uint64_t key
= msg
.u
.destroy_channel
.key
;
902 struct lttng_consumer_channel
*channel
;
904 channel
= consumer_find_channel(key
);
906 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
907 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
910 health_code_update();
912 ret
= consumer_send_status_msg(sock
, ret_code
);
914 /* Somehow, the session daemon is not responding anymore. */
918 health_code_update();
920 /* Stop right now if no channel was found. */
926 * This command should ONLY be issued for channel with streams set in
929 assert(!channel
->monitor
);
932 * The refcount should ALWAYS be 0 in the case of a channel in no
935 assert(!uatomic_sub_return(&channel
->refcount
, 1));
937 consumer_del_channel(channel
);
949 * Return 1 to indicate success since the 0 value can be a socket
950 * shutdown during the recv() or send() call.
952 health_code_update();
957 /* This will issue a consumer stop. */
962 * Populate index values of a kernel stream. Values are set in big endian order.
964 * Return 0 on success or else a negative value.
966 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
970 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
972 PERROR("kernctl_get_timestamp_begin");
975 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
977 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
979 PERROR("kernctl_get_timestamp_end");
982 index
->timestamp_end
= htobe64(index
->timestamp_end
);
984 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
986 PERROR("kernctl_get_events_discarded");
989 index
->events_discarded
= htobe64(index
->events_discarded
);
991 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
993 PERROR("kernctl_get_content_size");
996 index
->content_size
= htobe64(index
->content_size
);
998 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1000 PERROR("kernctl_get_packet_size");
1003 index
->packet_size
= htobe64(index
->packet_size
);
1005 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1007 PERROR("kernctl_get_stream_id");
1010 index
->stream_id
= htobe64(index
->stream_id
);
1016 * Sync metadata meaning request them to the session daemon and snapshot to the
1017 * metadata thread can consumer them.
1019 * Metadata stream lock MUST be acquired.
1021 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1022 * is empty or a negative value on error.
1024 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1030 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1032 ERR("Failed to flush kernel stream");
1036 ret
= kernctl_snapshot(metadata
->wait_fd
);
1038 if (errno
!= EAGAIN
) {
1039 ERR("Sync metadata, taking kernel snapshot failed.");
1042 DBG("Sync metadata, no new kernel metadata");
1043 /* No new metadata, exit. */
1053 * Consume data on a file descriptor and write it on a trace file.
1055 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1056 struct lttng_consumer_local_data
*ctx
)
1058 unsigned long len
, subbuf_size
, padding
;
1059 int err
, write_index
= 1;
1061 int infd
= stream
->wait_fd
;
1062 struct ctf_packet_index index
;
1064 DBG("In read_subbuffer (infd : %d)", infd
);
1066 /* Get the next subbuffer */
1067 err
= kernctl_get_next_subbuf(infd
);
1070 * This is a debug message even for single-threaded consumer,
1071 * because poll() have more relaxed criterions than get subbuf,
1072 * so get_subbuf may fail for short race windows where poll()
1073 * would issue wakeups.
1075 DBG("Reserving sub buffer failed (everything is normal, "
1076 "it is due to concurrency)");
1081 /* Get the full subbuffer size including padding */
1082 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1084 perror("Getting sub-buffer len failed.");
1089 if (!stream
->metadata_flag
) {
1090 ret
= get_index_values(&index
, infd
);
1098 switch (stream
->chan
->output
) {
1099 case CONSUMER_CHANNEL_SPLICE
:
1101 * XXX: The lttng-modules splice "actor" does not handle copying
1102 * partial pages hence only using the subbuffer size without the
1103 * padding makes the splice fail.
1108 /* splice the subbuffer to the tracefile */
1109 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1112 * XXX: Splice does not support network streaming so the return value
1113 * is simply checked against subbuf_size and not like the mmap() op.
1115 if (ret
!= subbuf_size
) {
1117 * display the error but continue processing to try
1118 * to release the subbuffer
1120 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1125 case CONSUMER_CHANNEL_MMAP
:
1126 /* Get subbuffer size without padding */
1127 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1129 perror("Getting sub-buffer len failed.");
1134 /* Make sure the tracer is not gone mad on us! */
1135 assert(len
>= subbuf_size
);
1137 padding
= len
- subbuf_size
;
1139 /* write the subbuffer to the tracefile */
1140 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1143 * The mmap operation should write subbuf_size amount of data when
1144 * network streaming or the full padding (len) size when we are _not_
1147 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1148 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1150 * Display the error but continue processing to try to release the
1151 * subbuffer. This is a DBG statement since this is possible to
1152 * happen without being a critical error.
1154 DBG("Error writing to tracefile "
1155 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1156 ret
, len
, subbuf_size
);
1161 ERR("Unknown output method");
1165 err
= kernctl_put_next_subbuf(infd
);
1167 if (errno
== EFAULT
) {
1168 perror("Error in unreserving sub buffer\n");
1169 } else if (errno
== EIO
) {
1170 /* Should never happen with newer LTTng versions */
1171 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1177 /* Write index if needed. */
1182 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1184 * In live, block until all the metadata is sent.
1186 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1192 err
= consumer_stream_write_index(stream
, &index
);
1201 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1208 * Don't create anything if this is set for streaming or should not be
1211 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1212 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1213 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1214 stream
->uid
, stream
->gid
, NULL
);
1218 stream
->out_fd
= ret
;
1219 stream
->tracefile_size_current
= 0;
1221 if (!stream
->metadata_flag
) {
1222 ret
= index_create_file(stream
->chan
->pathname
,
1223 stream
->name
, stream
->uid
, stream
->gid
,
1224 stream
->chan
->tracefile_size
,
1225 stream
->tracefile_count_current
);
1229 stream
->index_fd
= ret
;
1233 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1234 /* get the len of the mmap region */
1235 unsigned long mmap_len
;
1237 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1239 PERROR("kernctl_get_mmap_len");
1241 goto error_close_fd
;
1243 stream
->mmap_len
= (size_t) mmap_len
;
1245 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1246 MAP_PRIVATE
, stream
->wait_fd
, 0);
1247 if (stream
->mmap_base
== MAP_FAILED
) {
1248 PERROR("Error mmaping");
1250 goto error_close_fd
;
1254 /* we return 0 to let the library handle the FD internally */
1258 if (stream
->out_fd
>= 0) {
1261 err
= close(stream
->out_fd
);
1263 stream
->out_fd
= -1;
1270 * Check if data is still being extracted from the buffers for a specific
1271 * stream. Consumer data lock MUST be acquired before calling this function
1272 * and the stream lock.
1274 * Return 1 if the traced data are still getting read else 0 meaning that the
1275 * data is available for trace viewer reading.
1277 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1283 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1288 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1290 /* There is still data so let's put back this subbuffer. */
1291 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1293 ret
= 1; /* Data is pending */
1297 /* Data is NOT pending and ready to be read. */