2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License, version 2 only, as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <common/common.h>
23 #include <common/utils.h>
24 #include <common/defaults.h>
25 #include <common/sessiond-comm/relayd.h>
26 #include <urcu/rculist.h>
29 #include "lttng-relayd.h"
32 #include "viewer-stream.h"
34 #include <sys/types.h>
37 #define FILE_IO_STACK_BUFFER_SIZE 65536
39 /* Should be called with RCU read-side lock held. */
40 bool stream_get(struct relay_stream
*stream
)
42 return urcu_ref_get_unless_zero(&stream
->ref
);
46 * Get stream from stream id from the streams hash table. Return stream
47 * if found else NULL. A stream reference is taken when a stream is
48 * returned. stream_put() must be called on that stream.
50 struct relay_stream
*stream_get_by_id(uint64_t stream_id
)
52 struct lttng_ht_node_u64
*node
;
53 struct lttng_ht_iter iter
;
54 struct relay_stream
*stream
= NULL
;
57 lttng_ht_lookup(relay_streams_ht
, &stream_id
, &iter
);
58 node
= lttng_ht_iter_get_node_u64(&iter
);
60 DBG("Relay stream %" PRIu64
" not found", stream_id
);
63 stream
= caa_container_of(node
, struct relay_stream
, node
);
64 if (!stream_get(stream
)) {
72 static void stream_complete_rotation(struct relay_stream
*stream
)
74 DBG("Rotation completed for stream %" PRIu64
, stream
->stream_handle
);
75 lttng_trace_chunk_put(stream
->trace_chunk
);
76 stream
->trace_chunk
= stream
->ongoing_rotation
.value
.next_trace_chunk
;
77 stream
->ongoing_rotation
= (typeof(stream
->ongoing_rotation
)) {};
80 static int stream_create_data_output_file_from_trace_chunk(
81 struct relay_stream
*stream
,
82 struct lttng_trace_chunk
*trace_chunk
,
84 struct stream_fd
**out_stream_fd
)
87 char stream_path
[LTTNG_PATH_MAX
];
88 enum lttng_trace_chunk_status status
;
89 const int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
90 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
92 ASSERT_LOCKED(stream
->lock
);
94 ret
= utils_stream_file_path(stream
->path_name
, stream
->channel_name
,
95 stream
->tracefile_size
, stream
->tracefile_current_index
,
96 NULL
, stream_path
, sizeof(stream_path
));
101 if (stream
->tracefile_wrapped_around
|| force_unlink
) {
103 * The on-disk ring-buffer has wrapped around.
104 * Newly created stream files will replace existing files. Since
105 * live clients may be consuming existing files, the file about
106 * to be replaced is unlinked in order to not overwrite its
109 status
= lttng_trace_chunk_unlink_file(trace_chunk
,
111 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
112 PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
115 * Don't abort if the file doesn't exist, it is
116 * unexpected, but should not be a fatal error.
118 if (errno
!= ENOENT
) {
125 status
= lttng_trace_chunk_open_file(
126 trace_chunk
, stream_path
, flags
, mode
, &fd
);
127 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
128 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
133 *out_stream_fd
= stream_fd_create(fd
);
134 if (!*out_stream_fd
) {
136 PERROR("Error closing stream file descriptor %d", ret
);
145 static int stream_rotate_data_file(struct relay_stream
*stream
)
149 DBG("Rotating stream %" PRIu64
" data file",
150 stream
->stream_handle
);
152 if (stream
->stream_fd
) {
153 stream_fd_put(stream
->stream_fd
);
154 stream
->stream_fd
= NULL
;
157 stream
->tracefile_wrapped_around
= false;
158 stream
->tracefile_current_index
= 0;
160 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
161 struct stream_fd
*new_stream_fd
= NULL
;
162 enum lttng_trace_chunk_status chunk_status
;
164 chunk_status
= lttng_trace_chunk_create_subdirectory(
165 stream
->ongoing_rotation
.value
.next_trace_chunk
,
167 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
172 /* Rotate the data file. */
173 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
174 stream
->ongoing_rotation
.value
.next_trace_chunk
,
175 false, &new_stream_fd
);
176 stream
->stream_fd
= new_stream_fd
;
178 ERR("Failed to rotate stream data file");
182 stream
->tracefile_size_current
= 0;
183 stream
->pos_after_last_complete_data_index
= 0;
184 stream
->ongoing_rotation
.value
.data_rotated
= true;
186 if (stream
->ongoing_rotation
.value
.index_rotated
) {
187 /* Rotation completed; reset its state. */
188 stream_complete_rotation(stream
);
195 * If too much data has been written in a tracefile before we received the
196 * rotation command, we have to move the excess data to the new tracefile and
197 * perform the rotation. This can happen because the control and data
198 * connections are separate, the indexes as well as the commands arrive from
199 * the control connection and we have no control over the order so we could be
200 * in a situation where too much data has been received on the data connection
201 * before the rotation command on the control connection arrives.
203 static int rotate_truncate_stream(struct relay_stream
*stream
)
206 off_t lseek_ret
, previous_stream_copy_origin
;
207 uint64_t copy_bytes_left
, misplaced_data_size
;
208 bool acquired_reference
;
209 struct stream_fd
*previous_stream_fd
= NULL
;
210 struct lttng_trace_chunk
*previous_chunk
= NULL
;
212 if (!LTTNG_OPTIONAL_GET(stream
->ongoing_rotation
).next_trace_chunk
) {
213 ERR("Protocol error encoutered in %s(): stream rotation "
214 "sequence number is before the current sequence number "
215 "and the next trace chunk is unset. Honoring this "
216 "rotation command would result in data loss",
222 ASSERT_LOCKED(stream
->lock
);
224 * Acquire a reference to the current trace chunk to ensure
225 * it is not reclaimed when `stream_rotate_data_file` is called.
226 * Failing to do so would violate the contract of the trace
227 * chunk API as an active file descriptor would outlive the
230 acquired_reference
= lttng_trace_chunk_get(stream
->trace_chunk
);
231 assert(acquired_reference
);
232 previous_chunk
= stream
->trace_chunk
;
235 * Steal the stream's reference to its stream_fd. A new
236 * stream_fd will be created when the rotation completes and
237 * the orinal stream_fd will be used to copy the "extra" data
240 assert(stream
->stream_fd
);
241 previous_stream_fd
= stream
->stream_fd
;
242 stream
->stream_fd
= NULL
;
244 assert(!stream
->is_metadata
);
245 assert(stream
->tracefile_size_current
>
246 stream
->pos_after_last_complete_data_index
);
247 misplaced_data_size
= stream
->tracefile_size_current
-
248 stream
->pos_after_last_complete_data_index
;
249 copy_bytes_left
= misplaced_data_size
;
250 previous_stream_copy_origin
= stream
->pos_after_last_complete_data_index
;
252 ret
= stream_rotate_data_file(stream
);
257 assert(stream
->stream_fd
);
259 * Seek the current tracefile to the position at which the rotation
260 * should have occurred.
262 lseek_ret
= lseek(previous_stream_fd
->fd
, previous_stream_copy_origin
,
265 PERROR("Failed to seek to offset %" PRIu64
266 " while copying extra data received before a stream rotation",
267 (uint64_t) previous_stream_copy_origin
);
272 /* Move data from the old file to the new file. */
273 while (copy_bytes_left
) {
275 char copy_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
276 const off_t copy_size_this_pass
= min_t(
277 off_t
, copy_bytes_left
, sizeof(copy_buffer
));
279 io_ret
= lttng_read(previous_stream_fd
->fd
, copy_buffer
,
280 copy_size_this_pass
);
281 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
283 PERROR("Failed to read %" PRIu64
284 " bytes from fd %i in %s(), returned %zi",
286 previous_stream_fd
->fd
,
287 __FUNCTION__
, io_ret
);
289 ERR("Failed to read %" PRIu64
290 " bytes from fd %i in %s(), returned %zi",
292 previous_stream_fd
->fd
,
293 __FUNCTION__
, io_ret
);
299 io_ret
= lttng_write(stream
->stream_fd
->fd
, copy_buffer
,
300 copy_size_this_pass
);
301 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
303 PERROR("Failed to write %" PRIu64
304 " bytes from fd %i in %s(), returned %zi",
306 stream
->stream_fd
->fd
,
307 __FUNCTION__
, io_ret
);
309 ERR("Failed to write %" PRIu64
310 " bytes from fd %i in %s(), returned %zi",
312 stream
->stream_fd
->fd
,
313 __FUNCTION__
, io_ret
);
318 copy_bytes_left
-= copy_size_this_pass
;
321 /* Truncate the file to get rid of the excess data. */
322 ret
= ftruncate(previous_stream_fd
->fd
, previous_stream_copy_origin
);
324 PERROR("Failed to truncate current stream file to offset %" PRIu64
,
325 previous_stream_copy_origin
);
330 * Update the offset and FD of all the eventual indexes created by the
331 * data connection before the rotation command arrived.
333 ret
= relay_index_switch_all_files(stream
);
335 ERR("Failed to rotate index file");
339 stream
->tracefile_size_current
= misplaced_data_size
;
340 /* Index and data contents are back in sync. */
341 stream
->pos_after_last_complete_data_index
= 0;
344 lttng_trace_chunk_put(previous_chunk
);
345 stream_fd_put(previous_stream_fd
);
350 * Check if a stream's data file (as opposed to index) should be rotated
351 * (for session rotation).
352 * Must be called with the stream lock held.
354 * Return 0 on success, a negative value on error.
356 static int try_rotate_stream_data(struct relay_stream
*stream
)
360 if (caa_likely(!stream
->ongoing_rotation
.is_set
)) {
361 /* No rotation expected. */
365 if (stream
->ongoing_rotation
.value
.data_rotated
) {
366 /* Rotation of the data file has already occurred. */
370 if (stream
->prev_data_seq
== -1ULL ||
371 stream
->ongoing_rotation
.value
.prev_data_net_seq
== -1ULL ||
372 stream
->prev_data_seq
<
373 stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
375 * The next packet that will be written is not part of the next
378 DBG("Stream %" PRIu64
" data not yet ready for rotation "
379 "(rotate_at_index_packet_seq_num = %" PRIu64
380 ", rotate_at_prev_data_net_seq = %" PRIu64
381 ", prev_data_seq = %" PRIu64
")",
382 stream
->stream_handle
,
383 stream
->ongoing_rotation
.value
.packet_seq_num
,
384 stream
->ongoing_rotation
.value
.prev_data_net_seq
,
385 stream
->prev_data_seq
);
387 } else if (stream
->prev_data_seq
> stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
389 * prev_data_seq is checked here since indexes and rotation
390 * commands are serialized with respect to each other.
392 DBG("Rotation after too much data has been written in tracefile "
393 "for stream %" PRIu64
", need to truncate before "
394 "rotating", stream
->stream_handle
);
395 ret
= rotate_truncate_stream(stream
);
397 ERR("Failed to truncate stream");
401 ret
= stream_rotate_data_file(stream
);
409 * Close the current index file if it is open, and create a new one.
411 * Return 0 on success, -1 on error.
413 static int create_index_file(struct relay_stream
*stream
,
414 struct lttng_trace_chunk
*chunk
)
417 uint32_t major
, minor
;
418 char *index_subpath
= NULL
;
419 enum lttng_trace_chunk_status status
;
421 ASSERT_LOCKED(stream
->lock
);
423 /* Put ref on previous index_file. */
424 if (stream
->index_file
) {
425 lttng_index_file_put(stream
->index_file
);
426 stream
->index_file
= NULL
;
428 major
= stream
->trace
->session
->major
;
429 minor
= stream
->trace
->session
->minor
;
435 ret
= asprintf(&index_subpath
, "%s/%s", stream
->path_name
,
441 status
= lttng_trace_chunk_create_subdirectory(chunk
,
444 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
448 stream
->index_file
= lttng_index_file_create_from_trace_chunk(
449 chunk
, stream
->path_name
,
450 stream
->channel_name
, stream
->tracefile_size
,
451 stream
->tracefile_current_index
,
452 lttng_to_index_major(major
, minor
),
453 lttng_to_index_minor(major
, minor
), true);
454 if (!stream
->index_file
) {
466 * Check if a stream's index file should be rotated (for session rotation).
467 * Must be called with the stream lock held.
469 * Return 0 on success, a negative value on error.
471 static int try_rotate_stream_index(struct relay_stream
*stream
)
475 if (!stream
->ongoing_rotation
.is_set
) {
476 /* No rotation expected. */
480 if (stream
->ongoing_rotation
.value
.index_rotated
) {
481 /* Rotation of the index has already occurred. */
485 if (!stream
->received_packet_seq_num
.is_set
||
486 LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 <
487 stream
->ongoing_rotation
.value
.packet_seq_num
) {
488 DBG("Stream %" PRIu64
" index not yet ready for rotation "
489 "(rotate_at_packet_seq_num = %" PRIu64
490 ", received_packet_seq_num = "
491 "(value = %" PRIu64
", is_set = %" PRIu8
"))",
492 stream
->stream_handle
,
493 stream
->ongoing_rotation
.value
.packet_seq_num
,
494 stream
->received_packet_seq_num
.value
,
495 stream
->received_packet_seq_num
.is_set
);
499 * The next index belongs to the new trace chunk; rotate.
500 * In overwrite mode, the packet seq num may jump over the
503 assert(LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 >=
504 stream
->ongoing_rotation
.value
.packet_seq_num
);
505 DBG("Rotating stream %" PRIu64
" index file",
506 stream
->stream_handle
);
507 ret
= create_index_file(stream
,
508 stream
->ongoing_rotation
.value
.next_trace_chunk
);
509 stream
->ongoing_rotation
.value
.index_rotated
= true;
512 * Set the rotation pivot position for the data, now that we have the
513 * net_seq_num matching the packet_seq_num index pivot position.
515 stream
->ongoing_rotation
.value
.prev_data_net_seq
=
516 stream
->prev_index_seq
;
517 if (stream
->ongoing_rotation
.value
.data_rotated
&&
518 stream
->ongoing_rotation
.value
.index_rotated
) {
519 /* Rotation completed; reset its state. */
520 DBG("Rotation completed for stream %" PRIu64
,
521 stream
->stream_handle
);
522 stream_complete_rotation(stream
);
530 static int stream_set_trace_chunk(struct relay_stream
*stream
,
531 struct lttng_trace_chunk
*chunk
)
534 enum lttng_trace_chunk_status status
;
535 bool acquired_reference
;
536 struct stream_fd
*new_stream_fd
= NULL
;
538 status
= lttng_trace_chunk_create_subdirectory(chunk
,
540 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
545 lttng_trace_chunk_put(stream
->trace_chunk
);
546 acquired_reference
= lttng_trace_chunk_get(chunk
);
547 assert(acquired_reference
);
548 stream
->trace_chunk
= chunk
;
550 if (stream
->stream_fd
) {
551 stream_fd_put(stream
->stream_fd
);
552 stream
->stream_fd
= NULL
;
554 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
,
555 false, &new_stream_fd
);
556 stream
->stream_fd
= new_stream_fd
;
562 * We keep ownership of path_name and channel_name.
564 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
565 uint64_t stream_handle
, char *path_name
,
566 char *channel_name
, uint64_t tracefile_size
,
567 uint64_t tracefile_count
)
570 struct relay_stream
*stream
= NULL
;
571 struct relay_session
*session
= trace
->session
;
572 bool acquired_reference
= false;
573 struct lttng_trace_chunk
*current_trace_chunk
;
575 stream
= zmalloc(sizeof(struct relay_stream
));
576 if (stream
== NULL
) {
577 PERROR("relay stream zmalloc");
581 stream
->stream_handle
= stream_handle
;
582 stream
->prev_data_seq
= -1ULL;
583 stream
->prev_index_seq
= -1ULL;
584 stream
->last_net_seq_num
= -1ULL;
585 stream
->ctf_stream_id
= -1ULL;
586 stream
->tracefile_size
= tracefile_size
;
587 stream
->tracefile_count
= tracefile_count
;
588 stream
->path_name
= path_name
;
589 stream
->channel_name
= channel_name
;
590 stream
->beacon_ts_end
= -1ULL;
591 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
592 pthread_mutex_init(&stream
->lock
, NULL
);
593 urcu_ref_init(&stream
->ref
);
594 ctf_trace_get(trace
);
595 stream
->trace
= trace
;
597 pthread_mutex_lock(&trace
->session
->lock
);
598 current_trace_chunk
= trace
->session
->current_trace_chunk
;
599 if (current_trace_chunk
) {
600 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
602 pthread_mutex_unlock(&trace
->session
->lock
);
603 if (!acquired_reference
) {
604 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
610 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
611 if (!stream
->indexes_ht
) {
612 ERR("Cannot created indexes_ht");
617 pthread_mutex_lock(&stream
->lock
);
618 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
619 pthread_mutex_unlock(&stream
->lock
);
621 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
622 trace
->session
->session_name
,
623 stream
->channel_name
);
627 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
633 stream
->is_metadata
= !strcmp(stream
->channel_name
,
634 DEFAULT_METADATA_NAME
);
635 stream
->in_recv_list
= true;
638 * Add the stream in the recv list of the session. Once the end stream
639 * message is received, all session streams are published.
641 pthread_mutex_lock(&session
->recv_list_lock
);
642 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
643 session
->stream_count
++;
644 pthread_mutex_unlock(&session
->recv_list_lock
);
647 * Both in the ctf_trace object and the global stream ht since the data
648 * side of the relayd does not have the concept of session.
650 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
651 stream
->in_stream_ht
= true;
653 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
654 stream
->stream_handle
);
659 if (stream
->stream_fd
) {
660 stream_fd_put(stream
->stream_fd
);
661 stream
->stream_fd
= NULL
;
666 if (acquired_reference
) {
667 lttng_trace_chunk_put(current_trace_chunk
);
673 * path_name and channel_name need to be freed explicitly here
674 * because we cannot rely on stream_put().
682 * Called with the session lock held.
684 void stream_publish(struct relay_stream
*stream
)
686 struct relay_session
*session
;
688 pthread_mutex_lock(&stream
->lock
);
689 if (stream
->published
) {
693 session
= stream
->trace
->session
;
695 pthread_mutex_lock(&session
->recv_list_lock
);
696 if (stream
->in_recv_list
) {
697 cds_list_del_rcu(&stream
->recv_node
);
698 stream
->in_recv_list
= false;
700 pthread_mutex_unlock(&session
->recv_list_lock
);
702 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
703 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
704 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
706 stream
->published
= true;
708 pthread_mutex_unlock(&stream
->lock
);
712 * Stream must be protected by holding the stream lock or by virtue of being
713 * called from stream_destroy.
715 static void stream_unpublish(struct relay_stream
*stream
)
717 if (stream
->in_stream_ht
) {
718 struct lttng_ht_iter iter
;
721 iter
.iter
.node
= &stream
->node
.node
;
722 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
724 stream
->in_stream_ht
= false;
726 if (stream
->published
) {
727 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
728 cds_list_del_rcu(&stream
->stream_node
);
729 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
730 stream
->published
= false;
734 static void stream_destroy(struct relay_stream
*stream
)
736 if (stream
->indexes_ht
) {
738 * Calling lttng_ht_destroy in call_rcu worker thread so
739 * we don't hold the RCU read-side lock while calling
742 lttng_ht_destroy(stream
->indexes_ht
);
745 tracefile_array_destroy(stream
->tfa
);
747 free(stream
->path_name
);
748 free(stream
->channel_name
);
752 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
754 struct relay_stream
*stream
=
755 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
757 stream_destroy(stream
);
761 * No need to take stream->lock since this is only called on the final
762 * stream_put which ensures that a single thread may act on the stream.
764 static void stream_release(struct urcu_ref
*ref
)
766 struct relay_stream
*stream
=
767 caa_container_of(ref
, struct relay_stream
, ref
);
768 struct relay_session
*session
;
770 session
= stream
->trace
->session
;
772 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
774 pthread_mutex_lock(&session
->recv_list_lock
);
775 session
->stream_count
--;
776 if (stream
->in_recv_list
) {
777 cds_list_del_rcu(&stream
->recv_node
);
778 stream
->in_recv_list
= false;
780 pthread_mutex_unlock(&session
->recv_list_lock
);
782 stream_unpublish(stream
);
784 if (stream
->stream_fd
) {
785 stream_fd_put(stream
->stream_fd
);
786 stream
->stream_fd
= NULL
;
788 if (stream
->index_file
) {
789 lttng_index_file_put(stream
->index_file
);
790 stream
->index_file
= NULL
;
793 ctf_trace_put(stream
->trace
);
794 stream
->trace
= NULL
;
796 stream_complete_rotation(stream
);
797 lttng_trace_chunk_put(stream
->trace_chunk
);
798 stream
->trace_chunk
= NULL
;
800 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
803 void stream_put(struct relay_stream
*stream
)
806 assert(stream
->ref
.refcount
!= 0);
808 * Wait until we have processed all the stream packets before
809 * actually putting our last stream reference.
811 urcu_ref_put(&stream
->ref
, stream_release
);
815 int stream_set_pending_rotation(struct relay_stream
*stream
,
816 struct lttng_trace_chunk
*next_trace_chunk
,
817 uint64_t rotation_sequence_number
)
820 const struct relay_stream_rotation rotation
= {
821 .data_rotated
= false,
822 .index_rotated
= false,
823 .packet_seq_num
= rotation_sequence_number
,
824 .prev_data_net_seq
= -1ULL,
825 .next_trace_chunk
= next_trace_chunk
,
828 if (stream
->ongoing_rotation
.is_set
) {
829 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
834 if (next_trace_chunk
) {
835 const bool reference_acquired
=
836 lttng_trace_chunk_get(next_trace_chunk
);
838 assert(reference_acquired
);
840 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
842 DBG("Setting pending rotation: stream_id = %" PRIu64
843 ", rotate_at_packet_seq_num = %" PRIu64
,
844 stream
->stream_handle
, rotation_sequence_number
);
845 if (stream
->is_metadata
) {
847 * A metadata stream has no index; consider it already rotated.
849 stream
->ongoing_rotation
.value
.index_rotated
= true;
850 ret
= stream_rotate_data_file(stream
);
852 ret
= try_rotate_stream_index(stream
);
857 ret
= try_rotate_stream_data(stream
);
866 void try_stream_close(struct relay_stream
*stream
)
868 bool session_aborted
;
869 struct relay_session
*session
= stream
->trace
->session
;
871 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
873 pthread_mutex_lock(&session
->lock
);
874 session_aborted
= session
->aborted
;
875 pthread_mutex_unlock(&session
->lock
);
877 pthread_mutex_lock(&stream
->lock
);
879 * Can be called concurently by connection close and reception of last
882 if (stream
->closed
) {
883 pthread_mutex_unlock(&stream
->lock
);
884 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
888 stream
->close_requested
= true;
890 if (stream
->last_net_seq_num
== -1ULL) {
892 * Handle connection close without explicit stream close
895 * We can be clever about indexes partially received in
896 * cases where we received the data socket part, but not
897 * the control socket part: since we're currently closing
898 * the stream on behalf of the control socket, we *know*
899 * there won't be any more control information for this
900 * socket. Therefore, we can destroy all indexes for
901 * which we have received only the file descriptor (from
902 * data socket). This takes care of consumerd crashes
903 * between sending the data and control information for
904 * a packet. Since those are sent in that order, we take
905 * care of consumerd crashes.
907 DBG("relay_index_close_partial_fd");
908 relay_index_close_partial_fd(stream
);
910 * Use the highest net_seq_num we currently have pending
911 * As end of stream indicator. Leave last_net_seq_num
912 * at -1ULL if we cannot find any index.
914 stream
->last_net_seq_num
= relay_index_find_last(stream
);
915 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
916 /* Fall-through into the next check. */
919 if (stream
->last_net_seq_num
!= -1ULL &&
920 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
921 && !session_aborted
) {
923 * Don't close since we still have data pending. This
924 * handles cases where an explicit close command has
925 * been received for this stream, and cases where the
926 * connection has been closed, and we are awaiting for
927 * index information from the data socket. It is
928 * therefore expected that all the index fd information
929 * we need has already been received on the control
930 * socket. Matching index information from data socket
931 * should be Expected Soon(TM).
933 * TODO: We should implement a timer to garbage collect
934 * streams after a timeout to be resilient against a
935 * consumerd implementation that would not match this
938 pthread_mutex_unlock(&stream
->lock
);
939 DBG("closing stream %" PRIu64
" aborted since it still has data pending", stream
->stream_handle
);
943 * We received all the indexes we can expect.
945 stream_unpublish(stream
);
946 stream
->closed
= true;
947 /* Relay indexes are only used by the "consumer/sessiond" end. */
948 relay_index_close_all(stream
);
951 * If we are closed by an application exiting (per-pid buffers),
952 * we need to put our reference on the stream trace chunk right
953 * away, because otherwise still holding the reference on the
954 * trace chunk could allow a viewer stream (which holds a reference
955 * to the stream) to postpone destroy waiting for the chunk to cease
956 * to exist endlessly until the viewer is detached.
959 /* Put stream fd before put chunk. */
960 if (stream
->stream_fd
) {
961 stream_fd_put(stream
->stream_fd
);
962 stream
->stream_fd
= NULL
;
964 if (stream
->index_file
) {
965 lttng_index_file_put(stream
->index_file
);
966 stream
->index_file
= NULL
;
968 lttng_trace_chunk_put(stream
->trace_chunk
);
969 stream
->trace_chunk
= NULL
;
970 pthread_mutex_unlock(&stream
->lock
);
971 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
975 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
,
980 ASSERT_LOCKED(stream
->lock
);
982 if (!stream
->stream_fd
|| !stream
->trace_chunk
) {
983 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
984 stream
->stream_handle
, stream
->channel_name
);
989 if (caa_likely(stream
->tracefile_size
== 0)) {
990 /* No size limit set; nothing to check. */
995 * Check if writing the new packet would exceed the maximal file size.
997 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) >
998 stream
->tracefile_size
)) {
999 const uint64_t new_file_index
=
1000 (stream
->tracefile_current_index
+ 1) %
1001 stream
->tracefile_count
;
1003 if (new_file_index
< stream
->tracefile_current_index
) {
1004 stream
->tracefile_wrapped_around
= true;
1006 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
1007 ", current_file_size = %" PRIu64
1008 ", packet_size = %zu, current_file_index = %" PRIu64
1009 " new_file_index = %" PRIu64
,
1010 stream
->stream_handle
,
1011 stream
->tracefile_size_current
, packet_size
,
1012 stream
->tracefile_current_index
, new_file_index
);
1013 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_WRITE
);
1014 stream
->tracefile_current_index
= new_file_index
;
1016 if (stream
->stream_fd
) {
1017 stream_fd_put(stream
->stream_fd
);
1018 stream
->stream_fd
= NULL
;
1020 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
1021 stream
->trace_chunk
, false, &stream
->stream_fd
);
1023 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
1024 stream
->stream_handle
);
1029 * Reset current size because we just performed a stream
1032 stream
->tracefile_size_current
= 0;
1033 *file_rotated
= true;
1035 *file_rotated
= false;
1041 /* Note that the packet is not necessarily complete. */
1042 int stream_write(struct relay_stream
*stream
,
1043 const struct lttng_buffer_view
*packet
, size_t padding_len
)
1047 size_t padding_to_write
= padding_len
;
1048 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
1050 ASSERT_LOCKED(stream
->lock
);
1051 memset(padding_buffer
, 0,
1052 min(sizeof(padding_buffer
), padding_to_write
));
1054 if (!stream
->stream_fd
|| !stream
->trace_chunk
) {
1055 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
1056 stream
->stream_handle
, stream
->channel_name
);
1061 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1062 packet
->data
, packet
->size
);
1063 if (write_ret
!= packet
->size
) {
1064 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
1065 stream
->is_metadata
? "metadata " : "",
1066 stream
->stream_handle
);
1072 while (padding_to_write
> 0) {
1073 const size_t padding_to_write_this_pass
=
1074 min(padding_to_write
, sizeof(padding_buffer
));
1076 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1077 padding_buffer
, padding_to_write_this_pass
);
1078 if (write_ret
!= padding_to_write_this_pass
) {
1079 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
1080 stream
->is_metadata
? "metadata " : "",
1081 stream
->stream_handle
);
1085 padding_to_write
-= padding_to_write_this_pass
;
1088 if (stream
->is_metadata
) {
1089 stream
->metadata_received
+= packet
? packet
->size
: 0;
1090 stream
->metadata_received
+= padding_len
;
1093 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1094 stream
->is_metadata
? "metadata " : "",
1095 stream
->stream_handle
,
1096 packet
? packet
->size
: (size_t) 0, padding_len
);
1102 * Update index after receiving a packet for a data stream.
1104 * Called with the stream lock held.
1106 * Return 0 on success else a negative value.
1108 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
1109 bool rotate_index
, bool *flushed
, uint64_t total_size
)
1112 uint64_t data_offset
;
1113 struct relay_index
*index
;
1115 assert(stream
->trace_chunk
);
1116 ASSERT_LOCKED(stream
->lock
);
1117 /* Get data offset because we are about to update the index. */
1118 data_offset
= htobe64(stream
->tracefile_size_current
);
1120 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1121 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
1124 * Lookup for an existing index for that stream id/sequence
1125 * number. If it exists, the control thread has already received the
1126 * data for it, thus we need to write it to disk.
1128 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1134 if (rotate_index
|| !stream
->index_file
) {
1135 ret
= create_index_file(stream
, stream
->trace_chunk
);
1137 ERR("Failed to create index file for stream %" PRIu64
,
1138 stream
->stream_handle
);
1139 /* Put self-ref for this index due to error. */
1140 relay_index_put(index
);
1146 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1148 /* Put self-ref for this index due to error. */
1149 relay_index_put(index
);
1154 ret
= relay_index_try_flush(index
);
1156 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1157 tracefile_array_commit_seq(stream
->tfa
);
1158 stream
->index_received_seqcount
++;
1159 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1160 be64toh(index
->index_data
.packet_seq_num
));
1162 } else if (ret
> 0) {
1163 index
->total_size
= total_size
;
1170 * relay_index_try_flush is responsible for the self-reference
1171 * put of the index object on error.
1173 ERR("relay_index_try_flush error %d", ret
);
1180 int stream_complete_packet(struct relay_stream
*stream
, size_t packet_total_size
,
1181 uint64_t sequence_number
, bool index_flushed
)
1185 ASSERT_LOCKED(stream
->lock
);
1187 stream
->tracefile_size_current
+= packet_total_size
;
1188 if (index_flushed
) {
1189 stream
->pos_after_last_complete_data_index
=
1190 stream
->tracefile_size_current
;
1191 stream
->prev_index_seq
= sequence_number
;
1192 ret
= try_rotate_stream_index(stream
);
1198 stream
->prev_data_seq
= sequence_number
;
1199 ret
= try_rotate_stream_data(stream
);
1205 int stream_add_index(struct relay_stream
*stream
,
1206 const struct lttcomm_relayd_index
*index_info
)
1209 struct relay_index
*index
;
1211 ASSERT_LOCKED(stream
->lock
);
1213 /* Live beacon handling */
1214 if (index_info
->packet_size
== 0) {
1215 DBG("Received live beacon for stream %" PRIu64
,
1216 stream
->stream_handle
);
1219 * Only flag a stream inactive when it has already
1220 * received data and no indexes are in flight.
1222 if (stream
->index_received_seqcount
> 0
1223 && stream
->indexes_in_flight
== 0) {
1224 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1229 stream
->beacon_ts_end
= -1ULL;
1232 if (stream
->ctf_stream_id
== -1ULL) {
1233 stream
->ctf_stream_id
= index_info
->stream_id
;
1236 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1239 ERR("Failed to get or create index %" PRIu64
,
1240 index_info
->net_seq_num
);
1243 if (relay_index_set_control_data(index
, index_info
,
1244 stream
->trace
->session
->minor
)) {
1245 ERR("set_index_control_data error");
1246 relay_index_put(index
);
1250 ret
= relay_index_try_flush(index
);
1252 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1253 tracefile_array_commit_seq(stream
->tfa
);
1254 stream
->index_received_seqcount
++;
1255 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1256 stream
->prev_index_seq
= index_info
->net_seq_num
;
1257 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1258 index_info
->packet_seq_num
);
1260 ret
= try_rotate_stream_index(stream
);
1264 ret
= try_rotate_stream_data(stream
);
1268 } else if (ret
> 0) {
1275 * relay_index_try_flush is responsible for the self-reference
1276 * put of the index object on error.
1278 ERR("relay_index_try_flush error %d", ret
);
1285 static void print_stream_indexes(struct relay_stream
*stream
)
1287 struct lttng_ht_iter iter
;
1288 struct relay_index
*index
;
1291 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
1293 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1294 " stream %" PRIu64
" trace %" PRIu64
1295 " session %" PRIu64
,
1298 stream
->ref
.refcount
,
1299 index
->stream
->stream_handle
,
1300 index
->stream
->trace
->id
,
1301 index
->stream
->trace
->session
->id
);
1306 int stream_reset_file(struct relay_stream
*stream
)
1308 ASSERT_LOCKED(stream
->lock
);
1310 if (stream
->stream_fd
) {
1311 stream_fd_put(stream
->stream_fd
);
1312 stream
->stream_fd
= NULL
;
1315 stream
->tracefile_size_current
= 0;
1316 stream
->prev_data_seq
= 0;
1317 stream
->prev_index_seq
= 0;
1318 /* Note that this does not reset the tracefile array. */
1319 stream
->tracefile_current_index
= 0;
1320 stream
->pos_after_last_complete_data_index
= 0;
1322 return stream_create_data_output_file_from_trace_chunk(stream
,
1323 stream
->trace_chunk
, true, &stream
->stream_fd
);
1326 void print_relay_streams(void)
1328 struct lttng_ht_iter iter
;
1329 struct relay_stream
*stream
;
1331 if (!relay_streams_ht
) {
1336 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1338 if (!stream_get(stream
)) {
1341 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1342 " session %" PRIu64
,
1344 stream
->ref
.refcount
,
1345 stream
->stream_handle
,
1347 stream
->trace
->session
->id
);
1348 print_stream_indexes(stream
);