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
);
180 ret
= kernctl_buffer_flush(stream
->wait_fd
);
182 ERR("Failed to flush kernel stream");
187 ret
= lttng_kconsumer_take_snapshot(stream
);
189 ERR("Taking kernel snapshot");
193 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
195 ERR("Produced kernel snapshot position");
199 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
201 ERR("Consumerd kernel snapshot position");
205 if (stream
->max_sb_size
== 0) {
206 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
207 &stream
->max_sb_size
);
209 ERR("Getting kernel max_sb_size");
216 * The original value is sent back if max stream size is larger than
217 * the possible size of the snapshot. Also, we asume that the session
218 * daemon should never send a maximum stream size that is lower than
221 consumed_pos
= consumer_get_consumed_maxsize(consumed_pos
,
222 produced_pos
, max_stream_size
);
224 while (consumed_pos
< produced_pos
) {
226 unsigned long len
, padded_len
;
228 health_code_update();
230 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
232 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
234 if (errno
!= EAGAIN
) {
235 PERROR("kernctl_get_subbuf snapshot");
239 DBG("Kernel consumer get subbuf failed. Skipping it.");
240 consumed_pos
+= stream
->max_sb_size
;
244 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
246 ERR("Snapshot kernctl_get_subbuf_size");
248 goto error_put_subbuf
;
251 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
253 ERR("Snapshot kernctl_get_padded_subbuf_size");
255 goto error_put_subbuf
;
258 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
259 padded_len
- len
, NULL
);
261 * We write the padded len in local tracefiles but the data len
262 * when using a relay. Display the error but continue processing
263 * to try to release the subbuffer.
265 if (relayd_id
!= (uint64_t) -1ULL) {
266 if (read_len
!= len
) {
267 ERR("Error sending to the relay (ret: %zd != len: %lu)",
271 if (read_len
!= padded_len
) {
272 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
273 read_len
, padded_len
);
277 ret
= kernctl_put_subbuf(stream
->wait_fd
);
279 ERR("Snapshot kernctl_put_subbuf");
283 consumed_pos
+= stream
->max_sb_size
;
286 if (relayd_id
== (uint64_t) -1ULL) {
287 if (stream
->out_fd
>= 0) {
288 ret
= close(stream
->out_fd
);
290 PERROR("Kernel consumer snapshot close out_fd");
296 close_relayd_stream(stream
);
297 stream
->net_seq_idx
= (uint64_t) -1ULL;
299 pthread_mutex_unlock(&stream
->lock
);
307 ret
= kernctl_put_subbuf(stream
->wait_fd
);
310 ERR("Snapshot kernctl_put_subbuf error path");
313 pthread_mutex_unlock(&stream
->lock
);
320 * Read the whole metadata available for a snapshot.
322 * Returns 0 on success, < 0 on error
324 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
325 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
327 int ret
, use_relayd
= 0;
329 struct lttng_consumer_channel
*metadata_channel
;
330 struct lttng_consumer_stream
*metadata_stream
;
334 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
339 metadata_channel
= consumer_find_channel(key
);
340 if (!metadata_channel
) {
341 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
346 metadata_stream
= metadata_channel
->metadata_stream
;
347 assert(metadata_stream
);
349 /* Flag once that we have a valid relayd for the stream. */
350 if (relayd_id
!= (uint64_t) -1ULL) {
355 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
360 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
361 metadata_stream
->chan
->tracefile_size
,
362 metadata_stream
->tracefile_count_current
,
363 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
367 metadata_stream
->out_fd
= ret
;
371 health_code_update();
373 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
375 if (ret_read
!= -EAGAIN
) {
376 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
380 /* ret_read is negative at this point so we will exit the loop. */
383 } while (ret_read
>= 0);
386 close_relayd_stream(metadata_stream
);
387 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
389 if (metadata_stream
->out_fd
>= 0) {
390 ret
= close(metadata_stream
->out_fd
);
392 PERROR("Kernel consumer snapshot metadata close out_fd");
394 * Don't go on error here since the snapshot was successful at this
395 * point but somehow the close failed.
398 metadata_stream
->out_fd
= -1;
404 cds_list_del(&metadata_stream
->send_node
);
405 consumer_stream_destroy(metadata_stream
, NULL
);
406 metadata_channel
->metadata_stream
= NULL
;
413 * Receive command from session daemon and process it.
415 * Return 1 on success else a negative value or 0.
417 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
418 int sock
, struct pollfd
*consumer_sockpoll
)
421 enum lttng_error_code ret_code
= LTTNG_OK
;
422 struct lttcomm_consumer_msg msg
;
424 health_code_update();
426 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
427 if (ret
!= sizeof(msg
)) {
429 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
435 health_code_update();
437 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
439 * Notify the session daemon that the command is completed.
441 * On transport layer error, the function call will print an error
442 * message so handling the returned code is a bit useless since we
443 * return an error code anyway.
445 (void) consumer_send_status_msg(sock
, ret_code
);
449 health_code_update();
451 /* relayd needs RCU read-side protection */
454 switch (msg
.cmd_type
) {
455 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
457 /* Session daemon status message are handled in the following call. */
458 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
459 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
460 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
461 msg
.u
.relayd_sock
.relayd_session_id
);
464 case LTTNG_CONSUMER_ADD_CHANNEL
:
466 struct lttng_consumer_channel
*new_channel
;
469 health_code_update();
471 /* First send a status message before receiving the fds. */
472 ret
= consumer_send_status_msg(sock
, ret_code
);
474 /* Somehow, the session daemon is not responding anymore. */
478 health_code_update();
480 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
481 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
482 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
483 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
484 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
485 msg
.u
.channel
.tracefile_size
,
486 msg
.u
.channel
.tracefile_count
, 0,
487 msg
.u
.channel
.monitor
,
488 msg
.u
.channel
.live_timer_interval
);
489 if (new_channel
== NULL
) {
490 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
493 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
494 switch (msg
.u
.channel
.output
) {
495 case LTTNG_EVENT_SPLICE
:
496 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
498 case LTTNG_EVENT_MMAP
:
499 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
502 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
506 /* Translate and save channel type. */
507 switch (msg
.u
.channel
.type
) {
508 case CONSUMER_CHANNEL_TYPE_DATA
:
509 case CONSUMER_CHANNEL_TYPE_METADATA
:
510 new_channel
->type
= msg
.u
.channel
.type
;
517 health_code_update();
519 if (ctx
->on_recv_channel
!= NULL
) {
520 ret_recv
= ctx
->on_recv_channel(new_channel
);
522 ret
= consumer_add_channel(new_channel
, ctx
);
523 } else if (ret_recv
< 0) {
527 ret
= consumer_add_channel(new_channel
, ctx
);
529 if (CONSUMER_CHANNEL_TYPE_DATA
) {
530 consumer_timer_live_start(new_channel
,
531 msg
.u
.channel
.live_timer_interval
);
534 health_code_update();
536 /* If we received an error in add_channel, we need to report it. */
538 ret
= consumer_send_status_msg(sock
, ret
);
547 case LTTNG_CONSUMER_ADD_STREAM
:
550 struct lttng_pipe
*stream_pipe
;
551 struct lttng_consumer_stream
*new_stream
;
552 struct lttng_consumer_channel
*channel
;
556 * Get stream's channel reference. Needed when adding the stream to the
559 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
562 * We could not find the channel. Can happen if cpu hotplug
563 * happens while tearing down.
565 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
566 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
569 health_code_update();
571 /* First send a status message before receiving the fds. */
572 ret
= consumer_send_status_msg(sock
, ret_code
);
574 /* Somehow, the session daemon is not responding anymore. */
578 health_code_update();
580 if (ret_code
!= LTTNG_OK
) {
581 /* Channel was not found. */
587 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
594 health_code_update();
596 /* Get stream file descriptor from socket */
597 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
598 if (ret
!= sizeof(fd
)) {
599 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
604 health_code_update();
607 * Send status code to session daemon only if the recv works. If the
608 * above recv() failed, the session daemon is notified through the
609 * error socket and the teardown is eventually done.
611 ret
= consumer_send_status_msg(sock
, ret_code
);
613 /* Somehow, the session daemon is not responding anymore. */
617 health_code_update();
619 new_stream
= consumer_allocate_stream(channel
->key
,
621 LTTNG_CONSUMER_ACTIVE_STREAM
,
631 if (new_stream
== NULL
) {
636 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
642 new_stream
->chan
= channel
;
643 new_stream
->wait_fd
= fd
;
644 switch (channel
->output
) {
645 case CONSUMER_CHANNEL_SPLICE
:
646 new_stream
->output
= LTTNG_EVENT_SPLICE
;
648 case CONSUMER_CHANNEL_MMAP
:
649 new_stream
->output
= LTTNG_EVENT_MMAP
;
652 ERR("Stream output unknown %d", channel
->output
);
657 * We've just assigned the channel to the stream so increment the
658 * refcount right now. We don't need to increment the refcount for
659 * streams in no monitor because we handle manually the cleanup of
660 * those. It is very important to make sure there is NO prior
661 * consumer_del_stream() calls or else the refcount will be unbalanced.
663 if (channel
->monitor
) {
664 uatomic_inc(&new_stream
->chan
->refcount
);
668 * The buffer flush is done on the session daemon side for the kernel
669 * so no need for the stream "hangup_flush_done" variable to be
670 * tracked. This is important for a kernel stream since we don't rely
671 * on the flush state of the stream to read data. It's not the case for
672 * user space tracing.
674 new_stream
->hangup_flush_done
= 0;
676 health_code_update();
678 if (ctx
->on_recv_stream
) {
679 ret
= ctx
->on_recv_stream(new_stream
);
681 consumer_stream_free(new_stream
);
686 health_code_update();
688 if (new_stream
->metadata_flag
) {
689 channel
->metadata_stream
= new_stream
;
692 /* Do not monitor this stream. */
693 if (!channel
->monitor
) {
694 DBG("Kernel consumer add stream %s in no monitor mode with "
695 "relayd id %" PRIu64
, new_stream
->name
,
696 new_stream
->net_seq_idx
);
697 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
701 /* Send stream to relayd if the stream has an ID. */
702 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
703 ret
= consumer_send_relayd_stream(new_stream
,
704 new_stream
->chan
->pathname
);
706 consumer_stream_free(new_stream
);
711 /* Get the right pipe where the stream will be sent. */
712 if (new_stream
->metadata_flag
) {
713 ret
= consumer_add_metadata_stream(new_stream
);
715 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
717 consumer_stream_free(new_stream
);
720 stream_pipe
= ctx
->consumer_metadata_pipe
;
722 ret
= consumer_add_data_stream(new_stream
);
724 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
726 consumer_stream_free(new_stream
);
729 stream_pipe
= ctx
->consumer_data_pipe
;
732 /* Vitible to other threads */
733 new_stream
->globally_visible
= 1;
735 health_code_update();
737 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
739 ERR("Consumer write %s stream to pipe %d",
740 new_stream
->metadata_flag
? "metadata" : "data",
741 lttng_pipe_get_writefd(stream_pipe
));
742 if (new_stream
->metadata_flag
) {
743 consumer_del_stream_for_metadata(new_stream
);
745 consumer_del_stream_for_data(new_stream
);
750 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
751 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
754 case LTTNG_CONSUMER_UPDATE_STREAM
:
759 case LTTNG_CONSUMER_DESTROY_RELAYD
:
761 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
762 struct consumer_relayd_sock_pair
*relayd
;
764 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
766 /* Get relayd reference if exists. */
767 relayd
= consumer_find_relayd(index
);
768 if (relayd
== NULL
) {
769 DBG("Unable to find relayd %" PRIu64
, index
);
770 ret_code
= LTTNG_ERR_NO_CONSUMER
;
774 * Each relayd socket pair has a refcount of stream attached to it
775 * which tells if the relayd is still active or not depending on the
778 * This will set the destroy flag of the relayd object and destroy it
779 * if the refcount reaches zero when called.
781 * The destroy can happen either here or when a stream fd hangs up.
784 consumer_flag_relayd_for_destroy(relayd
);
787 health_code_update();
789 ret
= consumer_send_status_msg(sock
, ret_code
);
791 /* Somehow, the session daemon is not responding anymore. */
797 case LTTNG_CONSUMER_DATA_PENDING
:
800 uint64_t id
= msg
.u
.data_pending
.session_id
;
802 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
804 ret
= consumer_data_pending(id
);
806 health_code_update();
808 /* Send back returned value to session daemon */
809 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
811 PERROR("send data pending ret code");
816 * No need to send back a status message since the data pending
817 * returned value is the response.
821 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
823 if (msg
.u
.snapshot_channel
.metadata
== 1) {
824 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
825 msg
.u
.snapshot_channel
.pathname
,
826 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
828 ERR("Snapshot metadata failed");
829 ret_code
= LTTNG_ERR_KERN_META_FAIL
;
832 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
833 msg
.u
.snapshot_channel
.pathname
,
834 msg
.u
.snapshot_channel
.relayd_id
,
835 msg
.u
.snapshot_channel
.max_stream_size
,
838 ERR("Snapshot channel failed");
839 ret_code
= LTTNG_ERR_KERN_CHAN_FAIL
;
843 health_code_update();
845 ret
= consumer_send_status_msg(sock
, ret_code
);
847 /* Somehow, the session daemon is not responding anymore. */
852 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
854 uint64_t key
= msg
.u
.destroy_channel
.key
;
855 struct lttng_consumer_channel
*channel
;
857 channel
= consumer_find_channel(key
);
859 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
860 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
863 health_code_update();
865 ret
= consumer_send_status_msg(sock
, ret_code
);
867 /* Somehow, the session daemon is not responding anymore. */
871 health_code_update();
874 * This command should ONLY be issued for channel with streams set in
877 assert(!channel
->monitor
);
880 * The refcount should ALWAYS be 0 in the case of a channel in no
883 assert(!uatomic_sub_return(&channel
->refcount
, 1));
885 consumer_del_channel(channel
);
897 * Return 1 to indicate success since the 0 value can be a socket
898 * shutdown during the recv() or send() call.
900 health_code_update();
905 /* This will issue a consumer stop. */
910 * Populate index values of a kernel stream. Values are set in big endian order.
912 * Return 0 on success or else a negative value.
914 static int get_index_values(struct lttng_packet_index
*index
, int infd
)
918 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
920 PERROR("kernctl_get_timestamp_begin");
923 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
925 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
927 PERROR("kernctl_get_timestamp_end");
930 index
->timestamp_end
= htobe64(index
->timestamp_end
);
932 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
934 PERROR("kernctl_get_events_discarded");
937 index
->events_discarded
= htobe64(index
->events_discarded
);
939 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
941 PERROR("kernctl_get_content_size");
944 index
->content_size
= htobe64(index
->content_size
);
946 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
948 PERROR("kernctl_get_packet_size");
951 index
->packet_size
= htobe64(index
->packet_size
);
953 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
955 PERROR("kernctl_get_stream_id");
958 index
->stream_id
= htobe64(index
->stream_id
);
964 * Sync metadata meaning request them to the session daemon and snapshot to the
965 * metadata thread can consumer them.
967 * Metadata stream lock MUST be acquired.
969 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
970 * is empty or a negative value on error.
972 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
978 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
980 ERR("Failed to flush kernel stream");
984 ret
= kernctl_snapshot(metadata
->wait_fd
);
986 if (errno
!= EAGAIN
) {
987 ERR("Sync metadata, taking kernel snapshot failed.");
990 DBG("Sync metadata, no new kernel metadata");
991 /* No new metadata, exit. */
1001 * Consume data on a file descriptor and write it on a trace file.
1003 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1004 struct lttng_consumer_local_data
*ctx
)
1006 unsigned long len
, subbuf_size
, padding
;
1007 int err
, write_index
= 1;
1009 int infd
= stream
->wait_fd
;
1010 struct lttng_packet_index index
;
1012 DBG("In read_subbuffer (infd : %d)", infd
);
1014 /* Get the next subbuffer */
1015 err
= kernctl_get_next_subbuf(infd
);
1018 * This is a debug message even for single-threaded consumer,
1019 * because poll() have more relaxed criterions than get subbuf,
1020 * so get_subbuf may fail for short race windows where poll()
1021 * would issue wakeups.
1023 DBG("Reserving sub buffer failed (everything is normal, "
1024 "it is due to concurrency)");
1029 /* Get the full subbuffer size including padding */
1030 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1032 perror("Getting sub-buffer len failed.");
1037 if (!stream
->metadata_flag
) {
1038 ret
= get_index_values(&index
, infd
);
1046 switch (stream
->chan
->output
) {
1047 case CONSUMER_CHANNEL_SPLICE
:
1049 * XXX: The lttng-modules splice "actor" does not handle copying
1050 * partial pages hence only using the subbuffer size without the
1051 * padding makes the splice fail.
1056 /* splice the subbuffer to the tracefile */
1057 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1060 * XXX: Splice does not support network streaming so the return value
1061 * is simply checked against subbuf_size and not like the mmap() op.
1063 if (ret
!= subbuf_size
) {
1065 * display the error but continue processing to try
1066 * to release the subbuffer
1068 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1073 case CONSUMER_CHANNEL_MMAP
:
1074 /* Get subbuffer size without padding */
1075 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1077 perror("Getting sub-buffer len failed.");
1082 /* Make sure the tracer is not gone mad on us! */
1083 assert(len
>= subbuf_size
);
1085 padding
= len
- subbuf_size
;
1087 /* write the subbuffer to the tracefile */
1088 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1091 * The mmap operation should write subbuf_size amount of data when
1092 * network streaming or the full padding (len) size when we are _not_
1095 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1096 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1098 * Display the error but continue processing to try to release the
1101 ERR("Error writing to tracefile "
1102 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1103 ret
, len
, subbuf_size
);
1108 ERR("Unknown output method");
1112 err
= kernctl_put_next_subbuf(infd
);
1114 if (errno
== EFAULT
) {
1115 perror("Error in unreserving sub buffer\n");
1116 } else if (errno
== EIO
) {
1117 /* Should never happen with newer LTTng versions */
1118 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
1124 /* Write index if needed. */
1129 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1131 * In live, block until all the metadata is sent.
1133 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1139 err
= consumer_stream_write_index(stream
, &index
);
1148 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1155 * Don't create anything if this is set for streaming or should not be
1158 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1159 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1160 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1161 stream
->uid
, stream
->gid
, NULL
);
1165 stream
->out_fd
= ret
;
1166 stream
->tracefile_size_current
= 0;
1168 if (!stream
->metadata_flag
) {
1169 ret
= index_create_file(stream
->chan
->pathname
,
1170 stream
->name
, stream
->uid
, stream
->gid
,
1171 stream
->chan
->tracefile_size
,
1172 stream
->tracefile_count_current
);
1176 stream
->index_fd
= ret
;
1180 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1181 /* get the len of the mmap region */
1182 unsigned long mmap_len
;
1184 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1186 PERROR("kernctl_get_mmap_len");
1188 goto error_close_fd
;
1190 stream
->mmap_len
= (size_t) mmap_len
;
1192 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1193 MAP_PRIVATE
, stream
->wait_fd
, 0);
1194 if (stream
->mmap_base
== MAP_FAILED
) {
1195 PERROR("Error mmaping");
1197 goto error_close_fd
;
1201 /* we return 0 to let the library handle the FD internally */
1205 if (stream
->out_fd
>= 0) {
1208 err
= close(stream
->out_fd
);
1210 stream
->out_fd
= -1;
1217 * Check if data is still being extracted from the buffers for a specific
1218 * stream. Consumer data lock MUST be acquired before calling this function
1219 * and the stream lock.
1221 * Return 1 if the traced data are still getting read else 0 meaning that the
1222 * data is available for trace viewer reading.
1224 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1230 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
1235 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
1237 /* There is still data so let's put back this subbuffer. */
1238 ret
= kernctl_put_subbuf(stream
->wait_fd
);
1240 ret
= 1; /* Data is pending */
1244 /* Data is NOT pending and ready to be read. */