2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * SPDX-License-Identifier: GPL-2.0-only
13 #include "lttng-relayd.hpp"
15 #include "viewer-stream.hpp"
17 #include <common/common.hpp>
18 #include <common/defaults.hpp>
19 #include <common/fs-handle.hpp>
20 #include <common/sessiond-comm/relayd.hpp>
21 #include <common/urcu.hpp>
22 #include <common/utils.hpp>
27 #include <sys/types.h>
28 #include <urcu/rculist.h>
30 #define FILE_IO_STACK_BUFFER_SIZE 65536
32 /* Should be called with RCU read-side lock held. */
33 bool stream_get(struct relay_stream
*stream
)
35 ASSERT_RCU_READ_LOCKED();
37 return urcu_ref_get_unless_zero(&stream
->ref
);
41 * Get stream from stream id from the streams hash table. Return stream
42 * if found else NULL. A stream reference is taken when a stream is
43 * returned. stream_put() must be called on that stream.
45 struct relay_stream
*stream_get_by_id(uint64_t stream_id
)
47 struct lttng_ht_node_u64
*node
;
48 struct lttng_ht_iter iter
;
49 struct relay_stream
*stream
= nullptr;
51 lttng::urcu::read_lock_guard read_lock
;
52 lttng_ht_lookup(relay_streams_ht
, &stream_id
, &iter
);
53 node
= lttng_ht_iter_get_node_u64(&iter
);
55 DBG("Relay stream %" PRIu64
" not found", stream_id
);
58 stream
= lttng::utils::container_of(node
, &relay_stream::node
);
59 if (!stream_get(stream
)) {
66 static void stream_complete_rotation(struct relay_stream
*stream
)
68 DBG("Rotation completed for stream %" PRIu64
, stream
->stream_handle
);
69 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
70 tracefile_array_reset(stream
->tfa
);
71 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
73 lttng_trace_chunk_put(stream
->trace_chunk
);
74 stream
->trace_chunk
= stream
->ongoing_rotation
.value
.next_trace_chunk
;
75 stream
->ongoing_rotation
= LTTNG_OPTIONAL_INIT_UNSET
;
76 stream
->completed_rotation_count
++;
79 static int stream_create_data_output_file_from_trace_chunk(struct relay_stream
*stream
,
80 struct lttng_trace_chunk
*trace_chunk
,
82 struct fs_handle
**out_file
)
85 char stream_path
[LTTNG_PATH_MAX
];
86 enum lttng_trace_chunk_status status
;
87 const int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
88 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
90 ASSERT_LOCKED(stream
->lock
);
92 ret
= utils_stream_file_path(stream
->path_name
,
94 stream
->tracefile_size
,
95 stream
->tracefile_current_index
,
103 if (stream
->tracefile_wrapped_around
|| force_unlink
) {
105 * The on-disk ring-buffer has wrapped around.
106 * Newly created stream files will replace existing files. Since
107 * live clients may be consuming existing files, the file about
108 * to be replaced is unlinked in order to not overwrite its
111 status
= (lttng_trace_chunk_status
) lttng_trace_chunk_unlink_file(trace_chunk
,
113 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
114 PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
117 * Don't abort if the file doesn't exist, it is
118 * unexpected, but should not be a fatal error.
120 if (errno
!= ENOENT
) {
127 status
= lttng_trace_chunk_open_fs_handle(
128 trace_chunk
, stream_path
, flags
, mode
, out_file
, false);
129 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
130 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
138 static int stream_rotate_data_file(struct relay_stream
*stream
)
142 DBG("Rotating stream %" PRIu64
" data file with size %" PRIu64
,
143 stream
->stream_handle
,
144 stream
->tracefile_size_current
);
147 fs_handle_close(stream
->file
);
148 stream
->file
= nullptr;
151 stream
->tracefile_wrapped_around
= false;
152 stream
->tracefile_current_index
= 0;
154 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
155 enum lttng_trace_chunk_status chunk_status
;
157 chunk_status
= lttng_trace_chunk_create_subdirectory(
158 stream
->ongoing_rotation
.value
.next_trace_chunk
, stream
->path_name
);
159 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
164 /* Rotate the data file. */
165 ret
= stream_create_data_output_file_from_trace_chunk(
167 stream
->ongoing_rotation
.value
.next_trace_chunk
,
171 ERR("Failed to rotate stream data file");
175 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
177 stream
->stream_handle
,
178 stream
->tracefile_size_current
);
179 stream
->tracefile_size_current
= 0;
180 stream
->pos_after_last_complete_data_index
= 0;
181 stream
->ongoing_rotation
.value
.data_rotated
= true;
183 if (stream
->ongoing_rotation
.value
.index_rotated
) {
184 /* Rotation completed; reset its state. */
185 stream_complete_rotation(stream
);
192 * If too much data has been written in a tracefile before we received the
193 * rotation command, we have to move the excess data to the new tracefile and
194 * perform the rotation. This can happen because the control and data
195 * connections are separate, the indexes as well as the commands arrive from
196 * the control connection and we have no control over the order so we could be
197 * in a situation where too much data has been received on the data connection
198 * before the rotation command on the control connection arrives.
200 static int rotate_truncate_stream(struct relay_stream
*stream
)
203 off_t lseek_ret
, previous_stream_copy_origin
;
204 uint64_t copy_bytes_left
, misplaced_data_size
;
205 bool acquired_reference
;
206 struct fs_handle
*previous_stream_file
= nullptr;
207 struct lttng_trace_chunk
*previous_chunk
= nullptr;
209 if (!LTTNG_OPTIONAL_GET(stream
->ongoing_rotation
).next_trace_chunk
) {
210 ERR("Protocol error encoutered in %s(): stream rotation "
211 "sequence number is before the current sequence number "
212 "and the next trace chunk is unset. Honoring this "
213 "rotation command would result in data loss",
219 ASSERT_LOCKED(stream
->lock
);
221 * Acquire a reference to the current trace chunk to ensure
222 * it is not reclaimed when `stream_rotate_data_file` is called.
223 * Failing to do so would violate the contract of the trace
224 * chunk API as an active file descriptor would outlive the
227 acquired_reference
= lttng_trace_chunk_get(stream
->trace_chunk
);
228 LTTNG_ASSERT(acquired_reference
);
229 previous_chunk
= stream
->trace_chunk
;
232 * Steal the stream's reference to its stream_fd. A new
233 * stream_fd will be created when the rotation completes and
234 * the orinal stream_fd will be used to copy the "extra" data
237 LTTNG_ASSERT(stream
->file
);
238 previous_stream_file
= stream
->file
;
239 stream
->file
= nullptr;
241 LTTNG_ASSERT(!stream
->is_metadata
);
242 LTTNG_ASSERT(stream
->tracefile_size_current
> stream
->pos_after_last_complete_data_index
);
243 misplaced_data_size
=
244 stream
->tracefile_size_current
- stream
->pos_after_last_complete_data_index
;
245 copy_bytes_left
= misplaced_data_size
;
246 previous_stream_copy_origin
= stream
->pos_after_last_complete_data_index
;
248 ret
= stream_rotate_data_file(stream
);
253 LTTNG_ASSERT(stream
->file
);
255 * Seek the current tracefile to the position at which the rotation
256 * should have occurred.
258 lseek_ret
= fs_handle_seek(previous_stream_file
, previous_stream_copy_origin
, SEEK_SET
);
260 PERROR("Failed to seek to offset %" PRIu64
261 " while copying extra data received before a stream rotation",
262 (uint64_t) previous_stream_copy_origin
);
267 /* Move data from the old file to the new file. */
268 while (copy_bytes_left
) {
270 char copy_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
271 const off_t copy_size_this_pass
=
272 std::min
<uint64_t>(copy_bytes_left
, sizeof(copy_buffer
));
274 io_ret
= fs_handle_read(previous_stream_file
, copy_buffer
, copy_size_this_pass
);
275 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
277 PERROR("Failed to read %" PRIu64
278 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
282 stream
->stream_handle
);
284 ERR("Failed to read %" PRIu64
285 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
289 stream
->stream_handle
);
295 io_ret
= fs_handle_write(stream
->file
, copy_buffer
, copy_size_this_pass
);
296 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
298 PERROR("Failed to write %" PRIu64
299 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
303 stream
->stream_handle
);
305 ERR("Failed to write %" PRIu64
306 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
310 stream
->stream_handle
);
315 copy_bytes_left
-= copy_size_this_pass
;
318 /* Truncate the file to get rid of the excess data. */
319 ret
= fs_handle_truncate(previous_stream_file
, previous_stream_copy_origin
);
321 PERROR("Failed to truncate current stream file to offset %" PRIu64
,
322 previous_stream_copy_origin
);
327 * Update the offset and FD of all the eventual indexes created by the
328 * data connection before the rotation command arrived.
330 ret
= relay_index_switch_all_files(stream
);
332 ERR("Failed to rotate index file");
336 stream
->tracefile_size_current
= misplaced_data_size
;
337 /* Index and data contents are back in sync. */
338 stream
->pos_after_last_complete_data_index
= 0;
341 lttng_trace_chunk_put(previous_chunk
);
346 * Check if a stream's data file (as opposed to index) should be rotated
347 * (for session rotation).
348 * Must be called with the stream lock held.
350 * Return 0 on success, a negative value on error.
352 static int try_rotate_stream_data(struct relay_stream
*stream
)
356 if (caa_likely(!stream
->ongoing_rotation
.is_set
)) {
357 /* No rotation expected. */
361 if (stream
->ongoing_rotation
.value
.data_rotated
) {
362 /* Rotation of the data file has already occurred. */
366 DBG("%s: Stream %" PRIu64
" (rotate_at_index_packet_seq_num = %" PRIu64
367 ", rotate_at_prev_data_net_seq = %" PRIu64
", prev_data_seq = %" PRIu64
")",
369 stream
->stream_handle
,
370 stream
->ongoing_rotation
.value
.packet_seq_num
,
371 stream
->ongoing_rotation
.value
.prev_data_net_seq
,
372 stream
->prev_data_seq
);
374 if (stream
->prev_data_seq
== -1ULL ||
375 stream
->ongoing_rotation
.value
.prev_data_net_seq
== -1ULL ||
376 stream
->prev_data_seq
< stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
378 * The next packet that will be written is not part of the next
381 DBG("Stream %" PRIu64
" data not yet ready for rotation "
382 "(rotate_at_index_packet_seq_num = %" PRIu64
383 ", rotate_at_prev_data_net_seq = %" PRIu64
", prev_data_seq = %" PRIu64
")",
384 stream
->stream_handle
,
385 stream
->ongoing_rotation
.value
.packet_seq_num
,
386 stream
->ongoing_rotation
.value
.prev_data_net_seq
,
387 stream
->prev_data_seq
);
389 } else if (stream
->prev_data_seq
> stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
391 * prev_data_seq is checked here since indexes and rotation
392 * commands are serialized with respect to each other.
394 DBG("Rotation after too much data has been written in tracefile "
395 "for stream %" PRIu64
", need to truncate before "
397 stream
->stream_handle
);
398 ret
= rotate_truncate_stream(stream
);
400 ERR("Failed to truncate stream");
404 ret
= stream_rotate_data_file(stream
);
412 * Close the current index file if it is open, and create a new one.
414 * Return 0 on success, -1 on error.
416 static int create_index_file(struct relay_stream
*stream
, struct lttng_trace_chunk
*chunk
)
419 uint32_t major
, minor
;
420 char *index_subpath
= nullptr;
421 enum lttng_trace_chunk_status status
;
423 ASSERT_LOCKED(stream
->lock
);
425 /* Put ref on previous index_file. */
426 if (stream
->index_file
) {
427 lttng_index_file_put(stream
->index_file
);
428 stream
->index_file
= nullptr;
430 major
= stream
->trace
->session
->major
;
431 minor
= stream
->trace
->session
->minor
;
437 ret
= asprintf(&index_subpath
, "%s/%s", stream
->path_name
, DEFAULT_INDEX_DIR
);
442 status
= lttng_trace_chunk_create_subdirectory(chunk
, index_subpath
);
444 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
448 status
= lttng_index_file_create_from_trace_chunk(chunk
,
450 stream
->channel_name
,
451 stream
->tracefile_size
,
452 stream
->tracefile_current_index
,
453 lttng_to_index_major(major
, minor
),
454 lttng_to_index_minor(major
, minor
),
456 &stream
->index_file
);
457 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
469 * Check if a stream's index file should be rotated (for session rotation).
470 * Must be called with the stream lock held.
472 * Return 0 on success, a negative value on error.
474 static int try_rotate_stream_index(struct relay_stream
*stream
)
478 if (!stream
->ongoing_rotation
.is_set
) {
479 /* No rotation expected. */
483 if (stream
->ongoing_rotation
.value
.index_rotated
) {
484 /* Rotation of the index has already occurred. */
488 DBG("%s: Stream %" PRIu64
" (rotate_at_packet_seq_num = %" PRIu64
489 ", received_packet_seq_num = "
490 "(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
);
497 if (!stream
->received_packet_seq_num
.is_set
||
498 LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 <
499 stream
->ongoing_rotation
.value
.packet_seq_num
) {
500 DBG("Stream %" PRIu64
" index not yet ready for rotation "
501 "(rotate_at_packet_seq_num = %" PRIu64
", received_packet_seq_num = "
502 "(value = %" PRIu64
", is_set = %" PRIu8
"))",
503 stream
->stream_handle
,
504 stream
->ongoing_rotation
.value
.packet_seq_num
,
505 stream
->received_packet_seq_num
.value
,
506 stream
->received_packet_seq_num
.is_set
);
510 * The next index belongs to the new trace chunk; rotate.
511 * In overwrite mode, the packet seq num may jump over the
514 LTTNG_ASSERT(LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 >=
515 stream
->ongoing_rotation
.value
.packet_seq_num
);
516 DBG("Rotating stream %" PRIu64
" index file", stream
->stream_handle
);
517 if (stream
->index_file
) {
518 lttng_index_file_put(stream
->index_file
);
519 stream
->index_file
= nullptr;
521 stream
->ongoing_rotation
.value
.index_rotated
= true;
524 * Set the rotation pivot position for the data, now that we have the
525 * net_seq_num matching the packet_seq_num index pivot position.
527 stream
->ongoing_rotation
.value
.prev_data_net_seq
= stream
->prev_index_seq
;
528 if (stream
->ongoing_rotation
.value
.data_rotated
&&
529 stream
->ongoing_rotation
.value
.index_rotated
) {
530 /* Rotation completed; reset its state. */
531 DBG("Rotation completed for stream %" PRIu64
, stream
->stream_handle
);
532 stream_complete_rotation(stream
);
540 static int stream_set_trace_chunk(struct relay_stream
*stream
, struct lttng_trace_chunk
*chunk
)
543 enum lttng_trace_chunk_status status
;
544 bool acquired_reference
;
546 status
= lttng_trace_chunk_create_subdirectory(chunk
, stream
->path_name
);
547 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
552 lttng_trace_chunk_put(stream
->trace_chunk
);
553 acquired_reference
= lttng_trace_chunk_get(chunk
);
554 LTTNG_ASSERT(acquired_reference
);
555 stream
->trace_chunk
= chunk
;
558 fs_handle_close(stream
->file
);
559 stream
->file
= nullptr;
561 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
, false, &stream
->file
);
567 * We keep ownership of path_name and channel_name.
569 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
570 uint64_t stream_handle
,
573 uint64_t tracefile_size
,
574 uint64_t tracefile_count
)
577 struct relay_stream
*stream
= nullptr;
578 struct relay_session
*session
= trace
->session
;
579 bool acquired_reference
= false;
580 struct lttng_trace_chunk
*current_trace_chunk
;
582 stream
= zmalloc
<relay_stream
>();
583 if (stream
== nullptr) {
584 PERROR("relay stream zmalloc");
588 stream
->stream_handle
= stream_handle
;
589 stream
->prev_data_seq
= -1ULL;
590 stream
->prev_index_seq
= -1ULL;
591 stream
->last_net_seq_num
= -1ULL;
592 stream
->ctf_stream_id
= -1ULL;
593 stream
->tracefile_size
= tracefile_size
;
594 stream
->tracefile_count
= tracefile_count
;
595 stream
->path_name
= path_name
;
596 stream
->channel_name
= channel_name
;
597 stream
->beacon_ts_end
= -1ULL;
598 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
599 pthread_mutex_init(&stream
->lock
, nullptr);
600 urcu_ref_init(&stream
->ref
);
601 ctf_trace_get(trace
);
602 stream
->trace
= trace
;
604 pthread_mutex_lock(&trace
->session
->lock
);
605 current_trace_chunk
= trace
->session
->current_trace_chunk
;
606 if (current_trace_chunk
) {
607 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
609 pthread_mutex_unlock(&trace
->session
->lock
);
610 if (!acquired_reference
) {
611 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
617 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
618 if (!stream
->indexes_ht
) {
619 ERR("Cannot created indexes_ht");
624 pthread_mutex_lock(&stream
->lock
);
625 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
626 pthread_mutex_unlock(&stream
->lock
);
628 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
629 trace
->session
->session_name
,
630 stream
->channel_name
);
634 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
640 stream
->is_metadata
= !strcmp(stream
->channel_name
, DEFAULT_METADATA_NAME
);
641 stream
->in_recv_list
= true;
644 * Add the stream in the recv list of the session. Once the end stream
645 * message is received, all session streams are published.
647 pthread_mutex_lock(&session
->recv_list_lock
);
648 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
649 session
->stream_count
++;
650 pthread_mutex_unlock(&session
->recv_list_lock
);
653 * Both in the ctf_trace object and the global stream ht since the data
654 * side of the relayd does not have the concept of session.
656 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
657 stream
->in_stream_ht
= true;
659 DBG("Relay new stream added %s with ID %" PRIu64
,
660 stream
->channel_name
,
661 stream
->stream_handle
);
667 fs_handle_close(stream
->file
);
668 stream
->file
= nullptr;
673 if (acquired_reference
) {
674 lttng_trace_chunk_put(current_trace_chunk
);
680 * path_name and channel_name need to be freed explicitly here
681 * because we cannot rely on stream_put().
689 * Called with the session lock held.
691 void stream_publish(struct relay_stream
*stream
)
693 struct relay_session
*session
;
695 pthread_mutex_lock(&stream
->lock
);
696 if (stream
->published
) {
700 session
= stream
->trace
->session
;
702 pthread_mutex_lock(&session
->recv_list_lock
);
703 if (stream
->in_recv_list
) {
704 cds_list_del_rcu(&stream
->recv_node
);
705 stream
->in_recv_list
= false;
707 pthread_mutex_unlock(&session
->recv_list_lock
);
709 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
710 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
711 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
713 stream
->published
= true;
715 pthread_mutex_unlock(&stream
->lock
);
719 * Stream must be protected by holding the stream lock or by virtue of being
720 * called from stream_destroy.
722 static void stream_unpublish(struct relay_stream
*stream
)
724 if (stream
->in_stream_ht
) {
725 struct lttng_ht_iter iter
;
728 iter
.iter
.node
= &stream
->node
.node
;
729 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
731 stream
->in_stream_ht
= false;
733 if (stream
->published
) {
734 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
735 cds_list_del_rcu(&stream
->stream_node
);
736 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
737 stream
->published
= false;
741 static void stream_destroy(struct relay_stream
*stream
)
743 if (stream
->indexes_ht
) {
745 * Calling lttng_ht_destroy in call_rcu worker thread so
746 * we don't hold the RCU read-side lock while calling
749 lttng_ht_destroy(stream
->indexes_ht
);
752 tracefile_array_destroy(stream
->tfa
);
754 free(stream
->path_name
);
755 free(stream
->channel_name
);
759 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
761 struct relay_stream
*stream
= lttng::utils::container_of(rcu_head
, &relay_stream::rcu_node
);
763 stream_destroy(stream
);
767 * No need to take stream->lock since this is only called on the final
768 * stream_put which ensures that a single thread may act on the stream.
770 static void stream_release(struct urcu_ref
*ref
)
772 struct relay_stream
*stream
= lttng::utils::container_of(ref
, &relay_stream::ref
);
773 struct relay_session
*session
;
775 session
= stream
->trace
->session
;
777 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
779 pthread_mutex_lock(&session
->recv_list_lock
);
780 session
->stream_count
--;
781 if (stream
->in_recv_list
) {
782 cds_list_del_rcu(&stream
->recv_node
);
783 stream
->in_recv_list
= false;
785 pthread_mutex_unlock(&session
->recv_list_lock
);
787 stream_unpublish(stream
);
790 fs_handle_close(stream
->file
);
791 stream
->file
= nullptr;
793 if (stream
->index_file
) {
794 lttng_index_file_put(stream
->index_file
);
795 stream
->index_file
= nullptr;
798 ctf_trace_put(stream
->trace
);
799 stream
->trace
= nullptr;
801 stream_complete_rotation(stream
);
802 lttng_trace_chunk_put(stream
->trace_chunk
);
803 stream
->trace_chunk
= nullptr;
805 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
808 void stream_put(struct relay_stream
*stream
)
810 lttng::urcu::read_lock_guard read_lock
;
811 LTTNG_ASSERT(stream
->ref
.refcount
!= 0);
813 * Wait until we have processed all the stream packets before
814 * actually putting our last stream reference.
816 urcu_ref_put(&stream
->ref
, stream_release
);
819 int stream_set_pending_rotation(struct relay_stream
*stream
,
820 struct lttng_trace_chunk
*next_trace_chunk
,
821 uint64_t rotation_sequence_number
)
824 const struct relay_stream_rotation rotation
= {
825 .data_rotated
= false,
826 .index_rotated
= false,
827 .packet_seq_num
= rotation_sequence_number
,
828 .prev_data_net_seq
= -1ULL,
829 .next_trace_chunk
= next_trace_chunk
,
832 if (stream
->ongoing_rotation
.is_set
) {
833 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
838 if (next_trace_chunk
) {
839 const bool reference_acquired
= lttng_trace_chunk_get(next_trace_chunk
);
841 LTTNG_ASSERT(reference_acquired
);
843 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
845 DBG("Setting pending rotation: stream_id = %" PRIu64
846 ", rotate_at_packet_seq_num = %" PRIu64
,
847 stream
->stream_handle
,
848 rotation_sequence_number
);
849 if (stream
->is_metadata
) {
851 * A metadata stream has no index; consider it already rotated.
853 stream
->ongoing_rotation
.value
.index_rotated
= true;
854 if (next_trace_chunk
) {
856 * The metadata will be received again in the new chunk.
858 stream
->metadata_received
= 0;
860 ret
= stream_rotate_data_file(stream
);
862 ret
= try_rotate_stream_index(stream
);
867 ret
= try_rotate_stream_data(stream
);
876 void try_stream_close(struct relay_stream
*stream
)
878 bool session_aborted
;
879 struct relay_session
*session
= stream
->trace
->session
;
881 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
883 pthread_mutex_lock(&session
->lock
);
884 session_aborted
= session
->aborted
;
885 pthread_mutex_unlock(&session
->lock
);
887 pthread_mutex_lock(&stream
->lock
);
889 * Can be called concurently by connection close and reception of last
892 if (stream
->closed
) {
893 pthread_mutex_unlock(&stream
->lock
);
894 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed",
895 stream
->stream_handle
);
899 stream
->close_requested
= true;
901 if (stream
->last_net_seq_num
== -1ULL) {
903 * Handle connection close without explicit stream close
906 * We can be clever about indexes partially received in
907 * cases where we received the data socket part, but not
908 * the control socket part: since we're currently closing
909 * the stream on behalf of the control socket, we *know*
910 * there won't be any more control information for this
911 * socket. Therefore, we can destroy all indexes for
912 * which we have received only the file descriptor (from
913 * data socket). This takes care of consumerd crashes
914 * between sending the data and control information for
915 * a packet. Since those are sent in that order, we take
916 * care of consumerd crashes.
918 DBG("relay_index_close_partial_fd");
919 relay_index_close_partial_fd(stream
);
921 * Use the highest net_seq_num we currently have pending
922 * As end of stream indicator. Leave last_net_seq_num
923 * at -1ULL if we cannot find any index.
925 stream
->last_net_seq_num
= relay_index_find_last(stream
);
926 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
927 /* Fall-through into the next check. */
930 if (stream
->last_net_seq_num
!= -1ULL &&
931 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0 &&
934 * Don't close since we still have data pending. This
935 * handles cases where an explicit close command has
936 * been received for this stream, and cases where the
937 * connection has been closed, and we are awaiting for
938 * index information from the data socket. It is
939 * therefore expected that all the index fd information
940 * we need has already been received on the control
941 * socket. Matching index information from data socket
942 * should be Expected Soon(TM).
944 * TODO: We should implement a timer to garbage collect
945 * streams after a timeout to be resilient against a
946 * consumerd implementation that would not match this
949 pthread_mutex_unlock(&stream
->lock
);
950 DBG("closing stream %" PRIu64
" aborted since it still has data pending",
951 stream
->stream_handle
);
955 * We received all the indexes we can expect.
957 stream_unpublish(stream
);
958 stream
->closed
= true;
959 /* Relay indexes are only used by the "consumer/sessiond" end. */
960 relay_index_close_all(stream
);
963 * If we are closed by an application exiting (per-pid buffers),
964 * we need to put our reference on the stream trace chunk right
965 * away, because otherwise still holding the reference on the
966 * trace chunk could allow a viewer stream (which holds a reference
967 * to the stream) to postpone destroy waiting for the chunk to cease
968 * to exist endlessly until the viewer is detached.
971 /* Put stream fd before put chunk. */
973 fs_handle_close(stream
->file
);
974 stream
->file
= nullptr;
976 if (stream
->index_file
) {
977 lttng_index_file_put(stream
->index_file
);
978 stream
->index_file
= nullptr;
980 lttng_trace_chunk_put(stream
->trace_chunk
);
981 stream
->trace_chunk
= nullptr;
982 pthread_mutex_unlock(&stream
->lock
);
983 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
987 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
, bool *file_rotated
)
991 ASSERT_LOCKED(stream
->lock
);
993 if (!stream
->file
|| !stream
->trace_chunk
) {
994 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
995 ", channel_name = %s",
996 stream
->stream_handle
,
997 stream
->channel_name
);
1002 if (caa_likely(stream
->tracefile_size
== 0)) {
1003 /* No size limit set; nothing to check. */
1008 * Check if writing the new packet would exceed the maximal file size.
1010 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) > stream
->tracefile_size
)) {
1011 const uint64_t new_file_index
=
1012 (stream
->tracefile_current_index
+ 1) % stream
->tracefile_count
;
1014 if (new_file_index
< stream
->tracefile_current_index
) {
1015 stream
->tracefile_wrapped_around
= true;
1017 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
1018 ", current_file_size = %" PRIu64
1019 ", packet_size = %zu, current_file_index = %" PRIu64
1020 " new_file_index = %" PRIu64
,
1021 stream
->stream_handle
,
1022 stream
->tracefile_size_current
,
1024 stream
->tracefile_current_index
,
1026 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_WRITE
);
1027 stream
->tracefile_current_index
= new_file_index
;
1030 fs_handle_close(stream
->file
);
1031 stream
->file
= nullptr;
1033 ret
= stream_create_data_output_file_from_trace_chunk(
1034 stream
, stream
->trace_chunk
, false, &stream
->file
);
1036 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
1037 stream
->stream_handle
);
1042 * Reset current size because we just performed a stream
1045 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
1047 stream
->stream_handle
,
1048 stream
->tracefile_size_current
);
1049 stream
->tracefile_size_current
= 0;
1050 *file_rotated
= true;
1052 *file_rotated
= false;
1058 /* Note that the packet is not necessarily complete. */
1059 int stream_write(struct relay_stream
*stream
,
1060 const struct lttng_buffer_view
*packet
,
1065 size_t padding_to_write
= padding_len
;
1066 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
1068 ASSERT_LOCKED(stream
->lock
);
1069 memset(padding_buffer
, 0, std::min(sizeof(padding_buffer
), padding_to_write
));
1071 if (!stream
->file
|| !stream
->trace_chunk
) {
1072 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
1073 ", channel_name = %s",
1074 stream
->stream_handle
,
1075 stream
->channel_name
);
1080 write_ret
= fs_handle_write(stream
->file
, packet
->data
, packet
->size
);
1081 if (write_ret
!= packet
->size
) {
1082 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
1083 stream
->is_metadata
? "metadata " : "",
1084 stream
->stream_handle
);
1090 while (padding_to_write
> 0) {
1091 const size_t padding_to_write_this_pass
=
1092 std::min(padding_to_write
, sizeof(padding_buffer
));
1095 fs_handle_write(stream
->file
, padding_buffer
, padding_to_write_this_pass
);
1096 if (write_ret
!= padding_to_write_this_pass
) {
1097 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
1098 stream
->is_metadata
? "metadata " : "",
1099 stream
->stream_handle
);
1103 padding_to_write
-= padding_to_write_this_pass
;
1106 if (stream
->is_metadata
) {
1109 recv_len
= packet
? packet
->size
: 0;
1110 recv_len
+= padding_len
;
1111 stream
->metadata_received
+= recv_len
;
1113 stream
->no_new_metadata_notified
= false;
1117 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1118 stream
->is_metadata
? "metadata " : "",
1119 stream
->stream_handle
,
1120 packet
? packet
->size
: (size_t) 0,
1127 * Update index after receiving a packet for a data stream.
1129 * Called with the stream lock held.
1131 * Return 0 on success else a negative value.
1133 int stream_update_index(struct relay_stream
*stream
,
1134 uint64_t net_seq_num
,
1137 uint64_t total_size
)
1140 uint64_t data_offset
;
1141 struct relay_index
*index
;
1143 LTTNG_ASSERT(stream
->trace_chunk
);
1144 ASSERT_LOCKED(stream
->lock
);
1145 /* Get data offset because we are about to update the index. */
1146 data_offset
= htobe64(stream
->tracefile_size_current
);
1148 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1149 stream
->stream_handle
,
1151 stream
->tracefile_size_current
);
1154 * Lookup for an existing index for that stream id/sequence
1155 * number. If it exists, the control thread has already received the
1156 * data for it, thus we need to write it to disk.
1158 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1164 if (rotate_index
|| !stream
->index_file
) {
1165 ret
= create_index_file(stream
, stream
->trace_chunk
);
1167 ERR("Failed to create index file for stream %" PRIu64
,
1168 stream
->stream_handle
);
1169 /* Put self-ref for this index due to error. */
1170 relay_index_put(index
);
1176 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1178 /* Put self-ref for this index due to error. */
1179 relay_index_put(index
);
1184 ret
= relay_index_try_flush(index
);
1186 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1187 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1188 stream
->index_received_seqcount
++;
1189 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1190 be64toh(index
->index_data
.packet_seq_num
));
1192 } else if (ret
> 0) {
1193 index
->total_size
= total_size
;
1200 * relay_index_try_flush is responsible for the self-reference
1201 * put of the index object on error.
1203 ERR("relay_index_try_flush error %d", ret
);
1210 int stream_complete_packet(struct relay_stream
*stream
,
1211 size_t packet_total_size
,
1212 uint64_t sequence_number
,
1217 ASSERT_LOCKED(stream
->lock
);
1219 stream
->tracefile_size_current
+= packet_total_size
;
1220 if (index_flushed
) {
1221 stream
->pos_after_last_complete_data_index
= stream
->tracefile_size_current
;
1222 stream
->prev_index_seq
= sequence_number
;
1223 ret
= try_rotate_stream_index(stream
);
1229 stream
->prev_data_seq
= sequence_number
;
1230 ret
= try_rotate_stream_data(stream
);
1236 int stream_add_index(struct relay_stream
*stream
, const struct lttcomm_relayd_index
*index_info
)
1239 struct relay_index
*index
;
1241 ASSERT_LOCKED(stream
->lock
);
1243 DBG("stream_add_index for stream %" PRIu64
, stream
->stream_handle
);
1245 /* Live beacon handling */
1246 if (index_info
->packet_size
== 0) {
1247 DBG("Received live beacon for stream %" PRIu64
, stream
->stream_handle
);
1250 * Only flag a stream inactive when it has already
1251 * received data and no indexes are in flight.
1253 if (stream
->index_received_seqcount
> 0 && stream
->indexes_in_flight
== 0) {
1254 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1259 stream
->beacon_ts_end
= -1ULL;
1262 if (stream
->ctf_stream_id
== -1ULL) {
1263 stream
->ctf_stream_id
= index_info
->stream_id
;
1266 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1269 ERR("Failed to get or create index %" PRIu64
, index_info
->net_seq_num
);
1272 if (relay_index_set_control_data(index
, index_info
, stream
->trace
->session
->minor
)) {
1273 ERR("set_index_control_data error");
1274 relay_index_put(index
);
1278 ret
= relay_index_try_flush(index
);
1280 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1281 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1282 stream
->index_received_seqcount
++;
1283 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1284 stream
->prev_index_seq
= index_info
->net_seq_num
;
1285 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
, index_info
->packet_seq_num
);
1287 ret
= try_rotate_stream_index(stream
);
1291 ret
= try_rotate_stream_data(stream
);
1295 } else if (ret
> 0) {
1302 * relay_index_try_flush is responsible for the self-reference
1303 * put of the index object on error.
1305 ERR("relay_index_try_flush error %d", ret
);
1312 static void print_stream_indexes(struct relay_stream
*stream
)
1314 struct lttng_ht_iter iter
;
1315 struct relay_index
*index
;
1318 lttng::urcu::read_lock_guard read_lock
;
1320 cds_lfht_for_each_entry (stream
->indexes_ht
->ht
, &iter
.iter
, index
, index_n
.node
) {
1321 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1322 " stream %" PRIu64
" trace %" PRIu64
" session %" PRIu64
,
1325 stream
->ref
.refcount
,
1326 index
->stream
->stream_handle
,
1327 index
->stream
->trace
->id
,
1328 index
->stream
->trace
->session
->id
);
1333 int stream_reset_file(struct relay_stream
*stream
)
1335 ASSERT_LOCKED(stream
->lock
);
1340 ret
= fs_handle_close(stream
->file
);
1342 ERR("Failed to close stream file handle: channel name = \"%s\", id = %" PRIu64
,
1343 stream
->channel_name
,
1344 stream
->stream_handle
);
1346 stream
->file
= nullptr;
1349 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
1351 stream
->stream_handle
,
1352 stream
->tracefile_size_current
);
1353 stream
->tracefile_size_current
= 0;
1354 stream
->prev_data_seq
= 0;
1355 stream
->prev_index_seq
= 0;
1356 /* Note that this does not reset the tracefile array. */
1357 stream
->tracefile_current_index
= 0;
1358 stream
->pos_after_last_complete_data_index
= 0;
1360 return stream_create_data_output_file_from_trace_chunk(
1361 stream
, stream
->trace_chunk
, true, &stream
->file
);
1364 void print_relay_streams()
1366 struct lttng_ht_iter iter
;
1367 struct relay_stream
*stream
;
1369 if (!relay_streams_ht
) {
1374 lttng::urcu::read_lock_guard read_lock
;
1376 cds_lfht_for_each_entry (relay_streams_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1377 if (!stream_get(stream
)) {
1381 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1382 " session %" PRIu64
,
1384 stream
->ref
.refcount
,
1385 stream
->stream_handle
,
1387 stream
->trace
->session
->id
);
1388 print_stream_indexes(stream
);