2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/compat/endian.h>
40 #include <common/pipe.h>
41 #include <common/relayd/relayd.h>
42 #include <common/utils.h>
43 #include <common/consumer/consumer-stream.h>
44 #include <common/index/index.h>
45 #include <common/consumer/consumer-timer.h>
47 #include "kernel-consumer.h"
49 extern struct lttng_consumer_global_data consumer_data
;
50 extern int consumer_poll_timeout
;
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 * -EAGAIN is not an error, it just means that there is no data to
67 if (ret
!= 0 && ret
!= -EAGAIN
) {
68 PERROR("Getting sub-buffer snapshot.");
75 * Sample consumed and produced positions for a specific fd.
77 * Returns 0 on success, < 0 on error.
79 int lttng_kconsumer_sample_snapshot_positions(
80 struct lttng_consumer_stream
*stream
)
84 return kernctl_snapshot_sample_positions(stream
->wait_fd
);
88 * Get the produced position
90 * Returns 0 on success, < 0 on error
92 int lttng_kconsumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
96 int infd
= stream
->wait_fd
;
98 ret
= kernctl_snapshot_get_produced(infd
, pos
);
100 PERROR("kernctl_snapshot_get_produced");
107 * Get the consumerd position
109 * Returns 0 on success, < 0 on error
111 int lttng_kconsumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
115 int infd
= stream
->wait_fd
;
117 ret
= kernctl_snapshot_get_consumed(infd
, pos
);
119 PERROR("kernctl_snapshot_get_consumed");
126 * Take a snapshot of all the stream of a channel
128 * Returns 0 on success, < 0 on error
130 int lttng_kconsumer_snapshot_channel(uint64_t key
, char *path
,
131 uint64_t relayd_id
, uint64_t nb_packets_per_stream
,
132 struct lttng_consumer_local_data
*ctx
)
135 struct lttng_consumer_channel
*channel
;
136 struct lttng_consumer_stream
*stream
;
138 DBG("Kernel consumer snapshot channel %" PRIu64
, key
);
142 channel
= consumer_find_channel(key
);
144 ERR("No channel found for key %" PRIu64
, key
);
149 /* Splice is not supported yet for channel snapshot. */
150 if (channel
->output
!= CONSUMER_CHANNEL_MMAP
) {
151 ERR("Unsupported output %d", channel
->output
);
156 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
157 unsigned long consumed_pos
, produced_pos
;
159 health_code_update();
162 * Lock stream because we are about to change its state.
164 pthread_mutex_lock(&stream
->lock
);
167 * Assign the received relayd ID so we can use it for streaming. The streams
168 * are not visible to anyone so this is OK to change it.
170 stream
->net_seq_idx
= relayd_id
;
171 channel
->relayd_id
= relayd_id
;
172 if (relayd_id
!= (uint64_t) -1ULL) {
173 ret
= consumer_send_relayd_stream(stream
, path
);
175 ERR("sending stream to relayd");
179 ret
= utils_create_stream_file(path
, stream
->name
,
180 stream
->chan
->tracefile_size
,
181 stream
->tracefile_count_current
,
182 stream
->uid
, stream
->gid
, NULL
);
184 ERR("utils_create_stream_file");
188 stream
->out_fd
= ret
;
189 stream
->tracefile_size_current
= 0;
191 DBG("Kernel consumer snapshot stream %s/%s (%" PRIu64
")",
192 path
, stream
->name
, stream
->key
);
195 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
198 * Doing a buffer flush which does not take into
199 * account empty packets. This is not perfect
200 * for stream intersection, but required as a
201 * fall-back when "flush_empty" is not
202 * implemented by lttng-modules.
204 ret
= kernctl_buffer_flush(stream
->wait_fd
);
206 ERR("Failed to flush kernel stream");
212 ret
= lttng_kconsumer_take_snapshot(stream
);
214 ERR("Taking kernel snapshot");
218 ret
= lttng_kconsumer_get_produced_snapshot(stream
, &produced_pos
);
220 ERR("Produced kernel snapshot position");
224 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
226 ERR("Consumerd kernel snapshot position");
230 if (stream
->max_sb_size
== 0) {
231 ret
= kernctl_get_max_subbuf_size(stream
->wait_fd
,
232 &stream
->max_sb_size
);
234 ERR("Getting kernel max_sb_size");
239 consumed_pos
= consumer_get_consume_start_pos(consumed_pos
,
240 produced_pos
, nb_packets_per_stream
,
241 stream
->max_sb_size
);
243 while (consumed_pos
< produced_pos
) {
245 unsigned long len
, padded_len
;
247 health_code_update();
249 DBG("Kernel consumer taking snapshot at pos %lu", consumed_pos
);
251 ret
= kernctl_get_subbuf(stream
->wait_fd
, &consumed_pos
);
253 if (ret
!= -EAGAIN
) {
254 PERROR("kernctl_get_subbuf snapshot");
257 DBG("Kernel consumer get subbuf failed. Skipping it.");
258 consumed_pos
+= stream
->max_sb_size
;
259 stream
->chan
->lost_packets
++;
263 ret
= kernctl_get_subbuf_size(stream
->wait_fd
, &len
);
265 ERR("Snapshot kernctl_get_subbuf_size");
266 goto error_put_subbuf
;
269 ret
= kernctl_get_padded_subbuf_size(stream
->wait_fd
, &padded_len
);
271 ERR("Snapshot kernctl_get_padded_subbuf_size");
272 goto error_put_subbuf
;
275 read_len
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
,
276 padded_len
- len
, NULL
);
278 * We write the padded len in local tracefiles but the data len
279 * when using a relay. Display the error but continue processing
280 * to try to release the subbuffer.
282 if (relayd_id
!= (uint64_t) -1ULL) {
283 if (read_len
!= len
) {
284 ERR("Error sending to the relay (ret: %zd != len: %lu)",
288 if (read_len
!= padded_len
) {
289 ERR("Error writing to tracefile (ret: %zd != len: %lu)",
290 read_len
, padded_len
);
294 ret
= kernctl_put_subbuf(stream
->wait_fd
);
296 ERR("Snapshot kernctl_put_subbuf");
299 consumed_pos
+= stream
->max_sb_size
;
302 if (relayd_id
== (uint64_t) -1ULL) {
303 if (stream
->out_fd
>= 0) {
304 ret
= close(stream
->out_fd
);
306 PERROR("Kernel consumer snapshot close out_fd");
312 close_relayd_stream(stream
);
313 stream
->net_seq_idx
= (uint64_t) -1ULL;
315 pthread_mutex_unlock(&stream
->lock
);
323 ret
= kernctl_put_subbuf(stream
->wait_fd
);
325 ERR("Snapshot kernctl_put_subbuf error path");
328 pthread_mutex_unlock(&stream
->lock
);
335 * Read the whole metadata available for a snapshot.
337 * Returns 0 on success, < 0 on error
339 int lttng_kconsumer_snapshot_metadata(uint64_t key
, char *path
,
340 uint64_t relayd_id
, struct lttng_consumer_local_data
*ctx
)
342 int ret
, use_relayd
= 0;
344 struct lttng_consumer_channel
*metadata_channel
;
345 struct lttng_consumer_stream
*metadata_stream
;
349 DBG("Kernel consumer snapshot metadata with key %" PRIu64
" at path %s",
354 metadata_channel
= consumer_find_channel(key
);
355 if (!metadata_channel
) {
356 ERR("Kernel snapshot metadata not found for key %" PRIu64
, key
);
361 metadata_stream
= metadata_channel
->metadata_stream
;
362 assert(metadata_stream
);
364 /* Flag once that we have a valid relayd for the stream. */
365 if (relayd_id
!= (uint64_t) -1ULL) {
370 ret
= consumer_send_relayd_stream(metadata_stream
, path
);
375 ret
= utils_create_stream_file(path
, metadata_stream
->name
,
376 metadata_stream
->chan
->tracefile_size
,
377 metadata_stream
->tracefile_count_current
,
378 metadata_stream
->uid
, metadata_stream
->gid
, NULL
);
382 metadata_stream
->out_fd
= ret
;
386 health_code_update();
388 ret_read
= lttng_kconsumer_read_subbuffer(metadata_stream
, ctx
);
390 if (ret_read
!= -EAGAIN
) {
391 ERR("Kernel snapshot reading metadata subbuffer (ret: %zd)",
395 /* ret_read is negative at this point so we will exit the loop. */
398 } while (ret_read
>= 0);
401 close_relayd_stream(metadata_stream
);
402 metadata_stream
->net_seq_idx
= (uint64_t) -1ULL;
404 if (metadata_stream
->out_fd
>= 0) {
405 ret
= close(metadata_stream
->out_fd
);
407 PERROR("Kernel consumer snapshot metadata close out_fd");
409 * Don't go on error here since the snapshot was successful at this
410 * point but somehow the close failed.
413 metadata_stream
->out_fd
= -1;
419 cds_list_del(&metadata_stream
->send_node
);
420 consumer_stream_destroy(metadata_stream
, NULL
);
421 metadata_channel
->metadata_stream
= NULL
;
428 * When a channel has finished the rotation of all its streams, inform the
432 int rotate_notify_sessiond(struct lttng_consumer_local_data
*ctx
,
438 ret
= write(ctx
->channel_rotate_pipe
, &key
, sizeof(key
));
439 } while (ret
== -1 && errno
== EINTR
);
441 PERROR("write to the channel rotate pipe");
443 DBG("Sent channel rotation notification for channel key %"
451 * Performs the stream rotation for the rotate session feature if needed.
452 * It must be called with the stream and channel locks held.
454 * Return 0 on success, a negative number of error.
457 int stream_rotation(struct lttng_consumer_local_data
*ctx
,
458 struct lttng_consumer_stream
*stream
)
461 unsigned long consumed_pos
;
463 if (!stream
->rotate_position
&& !stream
->rotate_ready
) {
469 * If we don't have the rotate_ready flag, check the consumed position
470 * to determine if we need to rotate.
472 if (!stream
->rotate_ready
) {
473 ret
= lttng_kconsumer_sample_snapshot_positions(stream
);
475 ERR("Taking kernel snapshot positions");
479 ret
= lttng_kconsumer_get_consumed_snapshot(stream
, &consumed_pos
);
481 ERR("Produced kernel snapshot position");
485 fprintf(stderr
, "packet %lu, pos %lu\n", stream
->key
, consumed_pos
);
486 /* Rotate position not reached yet. */
487 if (consumed_pos
< stream
->rotate_position
) {
491 fprintf(stderr
, "Rotate position %lu (expected %lu) reached for stream %lu\n",
492 consumed_pos
, stream
->rotate_position
, stream
->key
);
494 fprintf(stderr
, "Rotate position reached for stream %lu\n",
498 ret
= close(stream
->out_fd
);
500 PERROR("Closing tracefile");
504 fprintf(stderr
, "Rotating stream %lu to %s/%s\n", stream
->key
,
505 stream
->chan
->pathname
, stream
->name
);
506 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
507 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
508 stream
->uid
, stream
->gid
, NULL
);
512 stream
->out_fd
= ret
;
513 stream
->tracefile_size_current
= 0;
515 if (!stream
->metadata_flag
) {
516 struct lttng_index_file
*index_file
;
518 lttng_index_file_put(stream
->index_file
);
520 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
521 stream
->name
, stream
->uid
, stream
->gid
,
522 stream
->chan
->tracefile_size
,
523 stream
->tracefile_count_current
,
524 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
528 stream
->index_file
= index_file
;
529 stream
->out_fd_offset
= 0;
532 * Reset the position of what has been read from the metadata
533 * cache to 0 so we can dump it again.
535 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
537 ERR("Failed to dump the metadata cache after rotation");
542 stream
->rotate_position
= 0;
543 stream
->rotate_ready
= 0;
545 if (--stream
->chan
->nr_stream_rotate_pending
== 0) {
546 rotate_notify_sessiond(ctx
, stream
->chan
->key
);
547 fprintf(stderr
, "SENT %lu\n", stream
->chan
->key
);
560 * Sample the rotate position for all the streams of a channel.
562 * Returns 0 on success, < 0 on error
565 int lttng_kconsumer_rotate_channel(uint64_t key
, char *path
,
566 uint64_t relayd_id
, uint32_t metadata
,
567 struct lttng_consumer_local_data
*ctx
)
570 struct lttng_consumer_channel
*channel
;
571 struct lttng_consumer_stream
*stream
;
572 struct lttng_ht_iter iter
;
573 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
575 DBG("Kernel consumer sample rotate position for channel %" PRIu64
, key
);
579 channel
= consumer_find_channel(key
);
581 ERR("No channel found for key %" PRIu64
, key
);
585 pthread_mutex_lock(&channel
->lock
);
586 snprintf(channel
->pathname
, PATH_MAX
, "%s", path
);
588 cds_lfht_for_each_entry_duplicate(ht
->ht
,
589 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
590 ht
->match_fct
, &channel
->key
, &iter
.iter
,
591 stream
, node_channel_id
.node
) {
592 health_code_update();
595 * Lock stream because we are about to change its state.
597 pthread_mutex_lock(&stream
->lock
);
598 ret
= lttng_kconsumer_sample_snapshot_positions(stream
);
600 ERR("Taking kernel snapshot positions");
603 uint64_t consumed_pos
;
605 ret
= lttng_kconsumer_get_produced_snapshot(stream
,
606 &stream
->rotate_position
);
608 ERR("Produced kernel snapshot position");
611 fprintf(stderr
, "Stream %lu should rotate after %lu to %s\n",
612 stream
->key
, stream
->rotate_position
,
614 lttng_kconsumer_get_consumed_snapshot(stream
,
616 fprintf(stderr
, "consumed %lu\n", consumed_pos
);
617 if (consumed_pos
== stream
->rotate_position
) {
618 stream
->rotate_ready
= 1;
619 fprintf(stderr
, "Stream %lu ready to rotate to %s\n",
620 stream
->key
, channel
->pathname
);
623 channel
->nr_stream_rotate_pending
++;
625 ret
= kernctl_buffer_flush(stream
->wait_fd
);
627 ERR("Failed to flush kernel stream");
631 pthread_mutex_unlock(&stream
->lock
);
635 goto end_unlock_channel
;
638 pthread_mutex_unlock(&stream
->lock
);
640 pthread_mutex_unlock(&channel
->lock
);
647 * Rotate all the ready streams.
649 * This is especially important for low throughput streams that have already
650 * been consumed, we cannot wait for their next packet to perform the
653 * Returns 0 on success, < 0 on error
656 int lttng_kconsumer_rotate_ready_streams(uint64_t key
,
657 struct lttng_consumer_local_data
*ctx
)
660 struct lttng_consumer_channel
*channel
;
661 struct lttng_consumer_stream
*stream
;
662 struct lttng_ht_iter iter
;
663 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
667 channel
= consumer_find_channel(key
);
669 ERR("No channel found for key %" PRIu64
, key
);
673 cds_lfht_for_each_entry_duplicate(ht
->ht
,
674 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
675 ht
->match_fct
, &channel
->key
, &iter
.iter
,
676 stream
, node_channel_id
.node
) {
677 health_code_update();
680 * Lock stream because we are about to change its state.
682 pthread_mutex_lock(&stream
->lock
);
683 if (stream
->rotate_ready
== 0) {
684 pthread_mutex_unlock(&stream
->lock
);
687 ret
= stream_rotation(ctx
, stream
);
689 pthread_mutex_unlock(&stream
->lock
);
690 ERR("Stream rotation error");
694 pthread_mutex_unlock(&stream
->lock
);
705 * Receive command from session daemon and process it.
707 * Return 1 on success else a negative value or 0.
709 int lttng_kconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
710 int sock
, struct pollfd
*consumer_sockpoll
)
713 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
714 struct lttcomm_consumer_msg msg
;
716 health_code_update();
718 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
719 if (ret
!= sizeof(msg
)) {
721 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
727 health_code_update();
729 /* Deprecated command */
730 assert(msg
.cmd_type
!= LTTNG_CONSUMER_STOP
);
732 health_code_update();
734 /* relayd needs RCU read-side protection */
737 switch (msg
.cmd_type
) {
738 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
740 /* Session daemon status message are handled in the following call. */
741 consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
742 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
743 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
,
744 msg
.u
.relayd_sock
.relayd_session_id
);
747 case LTTNG_CONSUMER_ADD_CHANNEL
:
749 struct lttng_consumer_channel
*new_channel
;
752 health_code_update();
754 /* First send a status message before receiving the fds. */
755 ret
= consumer_send_status_msg(sock
, ret_code
);
757 /* Somehow, the session daemon is not responding anymore. */
761 health_code_update();
763 DBG("consumer_add_channel %" PRIu64
, msg
.u
.channel
.channel_key
);
764 new_channel
= consumer_allocate_channel(msg
.u
.channel
.channel_key
,
765 msg
.u
.channel
.session_id
, msg
.u
.channel
.pathname
,
766 msg
.u
.channel
.name
, msg
.u
.channel
.uid
, msg
.u
.channel
.gid
,
767 msg
.u
.channel
.relayd_id
, msg
.u
.channel
.output
,
768 msg
.u
.channel
.tracefile_size
,
769 msg
.u
.channel
.tracefile_count
, 0,
770 msg
.u
.channel
.monitor
,
771 msg
.u
.channel
.live_timer_interval
,
773 if (new_channel
== NULL
) {
774 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
777 new_channel
->nb_init_stream_left
= msg
.u
.channel
.nb_init_streams
;
778 switch (msg
.u
.channel
.output
) {
779 case LTTNG_EVENT_SPLICE
:
780 new_channel
->output
= CONSUMER_CHANNEL_SPLICE
;
782 case LTTNG_EVENT_MMAP
:
783 new_channel
->output
= CONSUMER_CHANNEL_MMAP
;
786 ERR("Channel output unknown %d", msg
.u
.channel
.output
);
790 /* Translate and save channel type. */
791 switch (msg
.u
.channel
.type
) {
792 case CONSUMER_CHANNEL_TYPE_DATA
:
793 case CONSUMER_CHANNEL_TYPE_METADATA
:
794 new_channel
->type
= msg
.u
.channel
.type
;
801 health_code_update();
803 if (ctx
->on_recv_channel
!= NULL
) {
804 ret_recv
= ctx
->on_recv_channel(new_channel
);
806 ret
= consumer_add_channel(new_channel
, ctx
);
807 } else if (ret_recv
< 0) {
811 ret
= consumer_add_channel(new_channel
, ctx
);
813 if (msg
.u
.channel
.type
== CONSUMER_CHANNEL_TYPE_DATA
&& !ret
) {
814 int monitor_start_ret
;
816 DBG("Consumer starting monitor timer");
817 consumer_timer_live_start(new_channel
,
818 msg
.u
.channel
.live_timer_interval
);
819 monitor_start_ret
= consumer_timer_monitor_start(
821 msg
.u
.channel
.monitor_timer_interval
);
822 if (monitor_start_ret
< 0) {
823 ERR("Starting channel monitoring timer failed");
829 health_code_update();
831 /* If we received an error in add_channel, we need to report it. */
833 ret
= consumer_send_status_msg(sock
, ret
);
842 case LTTNG_CONSUMER_ADD_STREAM
:
845 struct lttng_pipe
*stream_pipe
;
846 struct lttng_consumer_stream
*new_stream
;
847 struct lttng_consumer_channel
*channel
;
851 * Get stream's channel reference. Needed when adding the stream to the
854 channel
= consumer_find_channel(msg
.u
.stream
.channel_key
);
857 * We could not find the channel. Can happen if cpu hotplug
858 * happens while tearing down.
860 ERR("Unable to find channel key %" PRIu64
, msg
.u
.stream
.channel_key
);
861 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
864 health_code_update();
866 /* First send a status message before receiving the fds. */
867 ret
= consumer_send_status_msg(sock
, ret_code
);
869 /* Somehow, the session daemon is not responding anymore. */
873 health_code_update();
875 if (ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
876 /* Channel was not found. */
882 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
888 health_code_update();
890 /* Get stream file descriptor from socket */
891 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
892 if (ret
!= sizeof(fd
)) {
893 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
898 health_code_update();
901 * Send status code to session daemon only if the recv works. If the
902 * above recv() failed, the session daemon is notified through the
903 * error socket and the teardown is eventually done.
905 ret
= consumer_send_status_msg(sock
, ret_code
);
907 /* Somehow, the session daemon is not responding anymore. */
911 health_code_update();
913 new_stream
= consumer_allocate_stream(channel
->key
,
915 LTTNG_CONSUMER_ACTIVE_STREAM
,
925 if (new_stream
== NULL
) {
930 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
936 new_stream
->chan
= channel
;
937 new_stream
->wait_fd
= fd
;
938 switch (channel
->output
) {
939 case CONSUMER_CHANNEL_SPLICE
:
940 new_stream
->output
= LTTNG_EVENT_SPLICE
;
941 ret
= utils_create_pipe(new_stream
->splice_pipe
);
946 case CONSUMER_CHANNEL_MMAP
:
947 new_stream
->output
= LTTNG_EVENT_MMAP
;
950 ERR("Stream output unknown %d", channel
->output
);
955 * We've just assigned the channel to the stream so increment the
956 * refcount right now. We don't need to increment the refcount for
957 * streams in no monitor because we handle manually the cleanup of
958 * those. It is very important to make sure there is NO prior
959 * consumer_del_stream() calls or else the refcount will be unbalanced.
961 if (channel
->monitor
) {
962 uatomic_inc(&new_stream
->chan
->refcount
);
966 * The buffer flush is done on the session daemon side for the kernel
967 * so no need for the stream "hangup_flush_done" variable to be
968 * tracked. This is important for a kernel stream since we don't rely
969 * on the flush state of the stream to read data. It's not the case for
970 * user space tracing.
972 new_stream
->hangup_flush_done
= 0;
974 health_code_update();
976 if (ctx
->on_recv_stream
) {
977 ret
= ctx
->on_recv_stream(new_stream
);
979 consumer_stream_free(new_stream
);
984 health_code_update();
986 if (new_stream
->metadata_flag
) {
987 channel
->metadata_stream
= new_stream
;
990 /* Do not monitor this stream. */
991 if (!channel
->monitor
) {
992 DBG("Kernel consumer add stream %s in no monitor mode with "
993 "relayd id %" PRIu64
, new_stream
->name
,
994 new_stream
->net_seq_idx
);
995 cds_list_add(&new_stream
->send_node
, &channel
->streams
.head
);
999 /* Send stream to relayd if the stream has an ID. */
1000 if (new_stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1001 ret
= consumer_send_relayd_stream(new_stream
,
1002 new_stream
->chan
->pathname
);
1004 consumer_stream_free(new_stream
);
1009 * If adding an extra stream to an already
1010 * existing channel (e.g. cpu hotplug), we need
1011 * to send the "streams_sent" command to relayd.
1013 if (channel
->streams_sent_to_relayd
) {
1014 ret
= consumer_send_relayd_streams_sent(
1015 new_stream
->net_seq_idx
);
1022 /* Get the right pipe where the stream will be sent. */
1023 if (new_stream
->metadata_flag
) {
1024 ret
= consumer_add_metadata_stream(new_stream
);
1026 ERR("Consumer add metadata stream %" PRIu64
" failed. Continuing",
1028 consumer_stream_free(new_stream
);
1031 stream_pipe
= ctx
->consumer_metadata_pipe
;
1033 ret
= consumer_add_data_stream(new_stream
);
1035 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
1037 consumer_stream_free(new_stream
);
1040 stream_pipe
= ctx
->consumer_data_pipe
;
1043 /* Vitible to other threads */
1044 new_stream
->globally_visible
= 1;
1046 health_code_update();
1048 ret
= lttng_pipe_write(stream_pipe
, &new_stream
, sizeof(new_stream
));
1050 ERR("Consumer write %s stream to pipe %d",
1051 new_stream
->metadata_flag
? "metadata" : "data",
1052 lttng_pipe_get_writefd(stream_pipe
));
1053 if (new_stream
->metadata_flag
) {
1054 consumer_del_stream_for_metadata(new_stream
);
1056 consumer_del_stream_for_data(new_stream
);
1061 DBG("Kernel consumer ADD_STREAM %s (fd: %d) with relayd id %" PRIu64
,
1062 new_stream
->name
, fd
, new_stream
->relayd_stream_id
);
1065 case LTTNG_CONSUMER_STREAMS_SENT
:
1067 struct lttng_consumer_channel
*channel
;
1070 * Get stream's channel reference. Needed when adding the stream to the
1071 * global hash table.
1073 channel
= consumer_find_channel(msg
.u
.sent_streams
.channel_key
);
1076 * We could not find the channel. Can happen if cpu hotplug
1077 * happens while tearing down.
1079 ERR("Unable to find channel key %" PRIu64
,
1080 msg
.u
.sent_streams
.channel_key
);
1081 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1084 health_code_update();
1087 * Send status code to session daemon.
1089 ret
= consumer_send_status_msg(sock
, ret_code
);
1090 if (ret
< 0 || ret_code
!= LTTCOMM_CONSUMERD_SUCCESS
) {
1091 /* Somehow, the session daemon is not responding anymore. */
1095 health_code_update();
1098 * We should not send this message if we don't monitor the
1099 * streams in this channel.
1101 if (!channel
->monitor
) {
1105 health_code_update();
1106 /* Send stream to relayd if the stream has an ID. */
1107 if (msg
.u
.sent_streams
.net_seq_idx
!= (uint64_t) -1ULL) {
1108 ret
= consumer_send_relayd_streams_sent(
1109 msg
.u
.sent_streams
.net_seq_idx
);
1113 channel
->streams_sent_to_relayd
= true;
1117 case LTTNG_CONSUMER_UPDATE_STREAM
:
1122 case LTTNG_CONSUMER_DESTROY_RELAYD
:
1124 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
1125 struct consumer_relayd_sock_pair
*relayd
;
1127 DBG("Kernel consumer destroying relayd %" PRIu64
, index
);
1129 /* Get relayd reference if exists. */
1130 relayd
= consumer_find_relayd(index
);
1131 if (relayd
== NULL
) {
1132 DBG("Unable to find relayd %" PRIu64
, index
);
1133 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
1137 * Each relayd socket pair has a refcount of stream attached to it
1138 * which tells if the relayd is still active or not depending on the
1141 * This will set the destroy flag of the relayd object and destroy it
1142 * if the refcount reaches zero when called.
1144 * The destroy can happen either here or when a stream fd hangs up.
1147 consumer_flag_relayd_for_destroy(relayd
);
1150 health_code_update();
1152 ret
= consumer_send_status_msg(sock
, ret_code
);
1154 /* Somehow, the session daemon is not responding anymore. */
1160 case LTTNG_CONSUMER_DATA_PENDING
:
1163 uint64_t id
= msg
.u
.data_pending
.session_id
;
1165 DBG("Kernel consumer data pending command for id %" PRIu64
, id
);
1167 ret
= consumer_data_pending(id
);
1169 health_code_update();
1171 /* Send back returned value to session daemon */
1172 ret
= lttcomm_send_unix_sock(sock
, &ret
, sizeof(ret
));
1174 PERROR("send data pending ret code");
1179 * No need to send back a status message since the data pending
1180 * returned value is the response.
1184 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL
:
1186 if (msg
.u
.snapshot_channel
.metadata
== 1) {
1187 ret
= lttng_kconsumer_snapshot_metadata(msg
.u
.snapshot_channel
.key
,
1188 msg
.u
.snapshot_channel
.pathname
,
1189 msg
.u
.snapshot_channel
.relayd_id
, ctx
);
1191 ERR("Snapshot metadata failed");
1192 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
1195 ret
= lttng_kconsumer_snapshot_channel(msg
.u
.snapshot_channel
.key
,
1196 msg
.u
.snapshot_channel
.pathname
,
1197 msg
.u
.snapshot_channel
.relayd_id
,
1198 msg
.u
.snapshot_channel
.nb_packets_per_stream
,
1201 ERR("Snapshot channel failed");
1202 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1206 health_code_update();
1208 ret
= consumer_send_status_msg(sock
, ret_code
);
1210 /* Somehow, the session daemon is not responding anymore. */
1215 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
1217 uint64_t key
= msg
.u
.destroy_channel
.key
;
1218 struct lttng_consumer_channel
*channel
;
1220 channel
= consumer_find_channel(key
);
1222 ERR("Kernel consumer destroy channel %" PRIu64
" not found", key
);
1223 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1226 health_code_update();
1228 ret
= consumer_send_status_msg(sock
, ret_code
);
1230 /* Somehow, the session daemon is not responding anymore. */
1234 health_code_update();
1236 /* Stop right now if no channel was found. */
1242 * This command should ONLY be issued for channel with streams set in
1245 assert(!channel
->monitor
);
1248 * The refcount should ALWAYS be 0 in the case of a channel in no
1251 assert(!uatomic_sub_return(&channel
->refcount
, 1));
1253 consumer_del_channel(channel
);
1257 case LTTNG_CONSUMER_DISCARDED_EVENTS
:
1261 struct lttng_consumer_channel
*channel
;
1262 uint64_t id
= msg
.u
.discarded_events
.session_id
;
1263 uint64_t key
= msg
.u
.discarded_events
.channel_key
;
1265 DBG("Kernel consumer discarded events command for session id %"
1266 PRIu64
", channel key %" PRIu64
, id
, key
);
1268 channel
= consumer_find_channel(key
);
1270 ERR("Kernel consumer discarded events channel %"
1271 PRIu64
" not found", key
);
1274 count
= channel
->discarded_events
;
1277 health_code_update();
1279 /* Send back returned value to session daemon */
1280 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1282 PERROR("send discarded events");
1288 case LTTNG_CONSUMER_LOST_PACKETS
:
1292 struct lttng_consumer_channel
*channel
;
1293 uint64_t id
= msg
.u
.lost_packets
.session_id
;
1294 uint64_t key
= msg
.u
.lost_packets
.channel_key
;
1296 DBG("Kernel consumer lost packets command for session id %"
1297 PRIu64
", channel key %" PRIu64
, id
, key
);
1299 channel
= consumer_find_channel(key
);
1301 ERR("Kernel consumer lost packets channel %"
1302 PRIu64
" not found", key
);
1305 count
= channel
->lost_packets
;
1308 health_code_update();
1310 /* Send back returned value to session daemon */
1311 ret
= lttcomm_send_unix_sock(sock
, &count
, sizeof(count
));
1313 PERROR("send lost packets");
1319 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE
:
1321 int channel_monitor_pipe
;
1323 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1324 /* Successfully received the command's type. */
1325 ret
= consumer_send_status_msg(sock
, ret_code
);
1330 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_monitor_pipe
,
1332 if (ret
!= sizeof(channel_monitor_pipe
)) {
1333 ERR("Failed to receive channel monitor pipe");
1337 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe
);
1338 ret
= consumer_timer_thread_set_channel_monitor_pipe(
1339 channel_monitor_pipe
);
1343 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1344 /* Set the pipe as non-blocking. */
1345 ret
= fcntl(channel_monitor_pipe
, F_GETFL
, 0);
1347 PERROR("fcntl get flags of the channel monitoring pipe");
1352 ret
= fcntl(channel_monitor_pipe
, F_SETFL
,
1353 flags
| O_NONBLOCK
);
1355 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
1358 DBG("Channel monitor pipe set as non-blocking");
1360 ret_code
= LTTCOMM_CONSUMERD_ALREADY_SET
;
1362 ret
= consumer_send_status_msg(sock
, ret_code
);
1368 case LTTNG_CONSUMER_SET_CHANNEL_ROTATE_PIPE
:
1370 int channel_rotate_pipe
;
1373 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1374 /* Successfully received the command's type. */
1375 ret
= consumer_send_status_msg(sock
, ret_code
);
1380 ret
= lttcomm_recv_fds_unix_sock(sock
, &channel_rotate_pipe
,
1382 if (ret
!= sizeof(channel_rotate_pipe
)) {
1383 ERR("Failed to receive channel rotate pipe");
1387 DBG("Received channel rotate pipe (%d)", channel_rotate_pipe
);
1388 ctx
->channel_rotate_pipe
= channel_rotate_pipe
;
1389 /* Set the pipe as non-blocking. */
1390 ret
= fcntl(channel_rotate_pipe
, F_GETFL
, 0);
1392 PERROR("fcntl get flags of the channel rotate pipe");
1397 ret
= fcntl(channel_rotate_pipe
, F_SETFL
,
1398 flags
| O_NONBLOCK
);
1400 PERROR("fcntl set O_NONBLOCK flag of the channel rotate pipe");
1403 DBG("Channel rotate pipe set as non-blocking");
1404 ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
1405 ret
= consumer_send_status_msg(sock
, ret_code
);
1411 case LTTNG_CONSUMER_ROTATE_CHANNEL
:
1413 ret
= lttng_kconsumer_rotate_channel(msg
.u
.rotate_channel
.key
,
1414 msg
.u
.rotate_channel
.pathname
,
1415 msg
.u
.rotate_channel
.relayd_id
,
1416 msg
.u
.rotate_channel
.metadata
,
1419 ERR("Rotate channel failed");
1420 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1423 health_code_update();
1425 ret
= consumer_send_status_msg(sock
, ret_code
);
1427 /* Somehow, the session daemon is not responding anymore. */
1432 * Rotate the streams that are ready right now.
1433 * FIXME: this is a second consecutive iteration over the
1434 * streams in a channel, there is probably a better way to
1435 * handle this, but it needs to be after the
1436 * consumer_send_status_msg() call.
1438 ret
= lttng_kconsumer_rotate_ready_streams(
1439 msg
.u
.rotate_channel
.key
, ctx
);
1441 ERR("Rotate channel failed");
1442 ret_code
= LTTCOMM_CONSUMERD_CHAN_NOT_FOUND
;
1454 * Return 1 to indicate success since the 0 value can be a socket
1455 * shutdown during the recv() or send() call.
1457 health_code_update();
1462 /* This will issue a consumer stop. */
1467 * Populate index values of a kernel stream. Values are set in big endian order.
1469 * Return 0 on success or else a negative value.
1471 static int get_index_values(struct ctf_packet_index
*index
, int infd
)
1475 ret
= kernctl_get_timestamp_begin(infd
, &index
->timestamp_begin
);
1477 PERROR("kernctl_get_timestamp_begin");
1480 index
->timestamp_begin
= htobe64(index
->timestamp_begin
);
1482 ret
= kernctl_get_timestamp_end(infd
, &index
->timestamp_end
);
1484 PERROR("kernctl_get_timestamp_end");
1487 index
->timestamp_end
= htobe64(index
->timestamp_end
);
1489 ret
= kernctl_get_events_discarded(infd
, &index
->events_discarded
);
1491 PERROR("kernctl_get_events_discarded");
1494 index
->events_discarded
= htobe64(index
->events_discarded
);
1496 ret
= kernctl_get_content_size(infd
, &index
->content_size
);
1498 PERROR("kernctl_get_content_size");
1501 index
->content_size
= htobe64(index
->content_size
);
1503 ret
= kernctl_get_packet_size(infd
, &index
->packet_size
);
1505 PERROR("kernctl_get_packet_size");
1508 index
->packet_size
= htobe64(index
->packet_size
);
1510 ret
= kernctl_get_stream_id(infd
, &index
->stream_id
);
1512 PERROR("kernctl_get_stream_id");
1515 index
->stream_id
= htobe64(index
->stream_id
);
1517 ret
= kernctl_get_instance_id(infd
, &index
->stream_instance_id
);
1519 if (ret
== -ENOTTY
) {
1520 /* Command not implemented by lttng-modules. */
1521 index
->stream_instance_id
= -1ULL;
1523 PERROR("kernctl_get_instance_id");
1527 index
->stream_instance_id
= htobe64(index
->stream_instance_id
);
1529 ret
= kernctl_get_sequence_number(infd
, &index
->packet_seq_num
);
1531 if (ret
== -ENOTTY
) {
1532 /* Command not implemented by lttng-modules. */
1533 index
->packet_seq_num
= -1ULL;
1536 PERROR("kernctl_get_sequence_number");
1540 index
->packet_seq_num
= htobe64(index
->packet_seq_num
);
1546 * Sync metadata meaning request them to the session daemon and snapshot to the
1547 * metadata thread can consumer them.
1549 * Metadata stream lock MUST be acquired.
1551 * Return 0 if new metadatda is available, EAGAIN if the metadata stream
1552 * is empty or a negative value on error.
1554 int lttng_kconsumer_sync_metadata(struct lttng_consumer_stream
*metadata
)
1560 ret
= kernctl_buffer_flush(metadata
->wait_fd
);
1562 ERR("Failed to flush kernel stream");
1566 ret
= kernctl_snapshot(metadata
->wait_fd
);
1568 if (ret
!= -EAGAIN
) {
1569 ERR("Sync metadata, taking kernel snapshot failed.");
1572 DBG("Sync metadata, no new kernel metadata");
1573 /* No new metadata, exit. */
1583 int update_stream_stats(struct lttng_consumer_stream
*stream
)
1586 uint64_t seq
, discarded
;
1588 ret
= kernctl_get_sequence_number(stream
->wait_fd
, &seq
);
1590 if (ret
== -ENOTTY
) {
1591 /* Command not implemented by lttng-modules. */
1594 PERROR("kernctl_get_sequence_number");
1600 * Start the sequence when we extract the first packet in case we don't
1601 * start at 0 (for example if a consumer is not connected to the
1602 * session immediately after the beginning).
1604 if (stream
->last_sequence_number
== -1ULL) {
1605 stream
->last_sequence_number
= seq
;
1606 } else if (seq
> stream
->last_sequence_number
) {
1607 stream
->chan
->lost_packets
+= seq
-
1608 stream
->last_sequence_number
- 1;
1610 /* seq <= last_sequence_number */
1611 ERR("Sequence number inconsistent : prev = %" PRIu64
1612 ", current = %" PRIu64
,
1613 stream
->last_sequence_number
, seq
);
1617 stream
->last_sequence_number
= seq
;
1619 ret
= kernctl_get_events_discarded(stream
->wait_fd
, &discarded
);
1621 PERROR("kernctl_get_events_discarded");
1624 if (discarded
< stream
->last_discarded_events
) {
1626 * Overflow has occurred. We assume only one wrap-around
1629 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
1630 stream
->last_discarded_events
+ discarded
;
1632 stream
->chan
->discarded_events
+= discarded
-
1633 stream
->last_discarded_events
;
1635 stream
->last_discarded_events
= discarded
;
1643 * Check if the local version of the metadata stream matches with the version
1644 * of the metadata stream in the kernel. If it was updated, set the reset flag
1648 int metadata_stream_check_version(int infd
, struct lttng_consumer_stream
*stream
)
1651 uint64_t cur_version
;
1653 ret
= kernctl_get_metadata_version(infd
, &cur_version
);
1655 if (ret
== -ENOTTY
) {
1657 * LTTng-modules does not implement this
1663 ERR("Failed to get the metadata version");
1667 if (stream
->metadata_version
== cur_version
) {
1672 DBG("New metadata version detected");
1673 stream
->metadata_version
= cur_version
;
1674 stream
->reset_metadata_flag
= 1;
1682 * Consume data on a file descriptor and write it on a trace file.
1684 ssize_t
lttng_kconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1685 struct lttng_consumer_local_data
*ctx
)
1687 unsigned long len
, subbuf_size
, padding
;
1688 int err
, write_index
= 1, rotation_ret
;
1690 int infd
= stream
->wait_fd
;
1691 struct ctf_packet_index index
;
1693 DBG("In read_subbuffer (infd : %d)", infd
);
1695 /* Get the next subbuffer */
1696 err
= kernctl_get_next_subbuf(infd
);
1699 * This is a debug message even for single-threaded consumer,
1700 * because poll() have more relaxed criterions than get subbuf,
1701 * so get_subbuf may fail for short race windows where poll()
1702 * would issue wakeups.
1704 DBG("Reserving sub buffer failed (everything is normal, "
1705 "it is due to concurrency)");
1710 /* Get the full subbuffer size including padding */
1711 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
1713 PERROR("Getting sub-buffer len failed.");
1714 err
= kernctl_put_subbuf(infd
);
1716 if (err
== -EFAULT
) {
1717 PERROR("Error in unreserving sub buffer\n");
1718 } else if (err
== -EIO
) {
1719 /* Should never happen with newer LTTng versions */
1720 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1729 if (!stream
->metadata_flag
) {
1730 ret
= get_index_values(&index
, infd
);
1732 err
= kernctl_put_subbuf(infd
);
1734 if (err
== -EFAULT
) {
1735 PERROR("Error in unreserving sub buffer\n");
1736 } else if (err
== -EIO
) {
1737 /* Should never happen with newer LTTng versions */
1738 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1745 ret
= update_stream_stats(stream
);
1747 err
= kernctl_put_subbuf(infd
);
1749 if (err
== -EFAULT
) {
1750 PERROR("Error in unreserving sub buffer\n");
1751 } else if (err
== -EIO
) {
1752 /* Should never happen with newer LTTng versions */
1753 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1762 ret
= metadata_stream_check_version(infd
, stream
);
1764 err
= kernctl_put_subbuf(infd
);
1766 if (err
== -EFAULT
) {
1767 PERROR("Error in unreserving sub buffer\n");
1768 } else if (err
== -EIO
) {
1769 /* Should never happen with newer LTTng versions */
1770 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1779 switch (stream
->chan
->output
) {
1780 case CONSUMER_CHANNEL_SPLICE
:
1782 * XXX: The lttng-modules splice "actor" does not handle copying
1783 * partial pages hence only using the subbuffer size without the
1784 * padding makes the splice fail.
1789 /* splice the subbuffer to the tracefile */
1790 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, subbuf_size
,
1793 * XXX: Splice does not support network streaming so the return value
1794 * is simply checked against subbuf_size and not like the mmap() op.
1796 if (ret
!= subbuf_size
) {
1798 * display the error but continue processing to try
1799 * to release the subbuffer
1801 ERR("Error splicing to tracefile (ret: %zd != len: %lu)",
1806 case CONSUMER_CHANNEL_MMAP
:
1807 /* Get subbuffer size without padding */
1808 err
= kernctl_get_subbuf_size(infd
, &subbuf_size
);
1810 PERROR("Getting sub-buffer len failed.");
1811 err
= kernctl_put_subbuf(infd
);
1813 if (err
== -EFAULT
) {
1814 PERROR("Error in unreserving sub buffer\n");
1815 } else if (err
== -EIO
) {
1816 /* Should never happen with newer LTTng versions */
1817 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1826 /* Make sure the tracer is not gone mad on us! */
1827 assert(len
>= subbuf_size
);
1829 padding
= len
- subbuf_size
;
1831 /* write the subbuffer to the tracefile */
1832 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
,
1835 * The mmap operation should write subbuf_size amount of data when
1836 * network streaming or the full padding (len) size when we are _not_
1839 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1840 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1842 * Display the error but continue processing to try to release the
1843 * subbuffer. This is a DBG statement since this is possible to
1844 * happen without being a critical error.
1846 DBG("Error writing to tracefile "
1847 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1848 ret
, len
, subbuf_size
);
1853 ERR("Unknown output method");
1857 err
= kernctl_put_next_subbuf(infd
);
1859 if (err
== -EFAULT
) {
1860 PERROR("Error in unreserving sub buffer\n");
1861 } else if (err
== -EIO
) {
1862 /* Should never happen with newer LTTng versions */
1863 PERROR("Reader has been pushed by the writer, last sub-buffer corrupted.");
1869 /* Write index if needed. */
1874 if (stream
->chan
->live_timer_interval
&& !stream
->metadata_flag
) {
1876 * In live, block until all the metadata is sent.
1878 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1879 assert(!stream
->missed_metadata_flush
);
1880 stream
->waiting_on_metadata
= true;
1881 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1883 err
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
1885 pthread_mutex_lock(&stream
->metadata_timer_lock
);
1886 stream
->waiting_on_metadata
= false;
1887 if (stream
->missed_metadata_flush
) {
1888 stream
->missed_metadata_flush
= false;
1889 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1890 (void) consumer_flush_kernel_index(stream
);
1892 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
1899 err
= consumer_stream_write_index(stream
, &index
);
1905 pthread_mutex_lock(&stream
->chan
->lock
);
1906 rotation_ret
= stream_rotation(ctx
, stream
);
1907 if (rotation_ret
< 0) {
1908 pthread_mutex_unlock(&stream
->chan
->lock
);
1909 ERR("Stream rotation error");
1912 pthread_mutex_unlock(&stream
->chan
->lock
);
1916 int lttng_kconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1923 * Don't create anything if this is set for streaming or should not be
1926 if (stream
->net_seq_idx
== (uint64_t) -1ULL && stream
->chan
->monitor
) {
1927 ret
= utils_create_stream_file(stream
->chan
->pathname
, stream
->name
,
1928 stream
->chan
->tracefile_size
, stream
->tracefile_count_current
,
1929 stream
->uid
, stream
->gid
, NULL
);
1933 stream
->out_fd
= ret
;
1934 stream
->tracefile_size_current
= 0;
1936 if (!stream
->metadata_flag
) {
1937 struct lttng_index_file
*index_file
;
1939 index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1940 stream
->name
, stream
->uid
, stream
->gid
,
1941 stream
->chan
->tracefile_size
,
1942 stream
->tracefile_count_current
,
1943 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1947 assert(!stream
->index_file
);
1948 stream
->index_file
= index_file
;
1952 if (stream
->output
== LTTNG_EVENT_MMAP
) {
1953 /* get the len of the mmap region */
1954 unsigned long mmap_len
;
1956 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
1958 PERROR("kernctl_get_mmap_len");
1959 goto error_close_fd
;
1961 stream
->mmap_len
= (size_t) mmap_len
;
1963 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
, PROT_READ
,
1964 MAP_PRIVATE
, stream
->wait_fd
, 0);
1965 if (stream
->mmap_base
== MAP_FAILED
) {
1966 PERROR("Error mmaping");
1968 goto error_close_fd
;
1972 /* we return 0 to let the library handle the FD internally */
1976 if (stream
->out_fd
>= 0) {
1979 err
= close(stream
->out_fd
);
1981 stream
->out_fd
= -1;
1988 * Check if data is still being extracted from the buffers for a specific
1989 * stream. Consumer data lock MUST be acquired before calling this function
1990 * and the stream lock.
1992 * Return 1 if the traced data are still getting read else 0 meaning that the
1993 * data is available for trace viewer reading.
1995 int lttng_kconsumer_data_pending(struct lttng_consumer_stream
*stream
)
2001 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
2006 ret
= kernctl_get_next_subbuf(stream
->wait_fd
);
2008 /* There is still data so let's put back this subbuffer. */
2009 ret
= kernctl_put_subbuf(stream
->wait_fd
);
2011 ret
= 1; /* Data is pending */
2015 /* Data is NOT pending and ready to be read. */