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
12 #include <common/common.h>
13 #include <common/defaults.h>
14 #include <common/fs-handle.h>
15 #include <common/sessiond-comm/relayd.h>
16 #include <common/utils.h>
18 #include <urcu/rculist.h>
20 #include "lttng-relayd.h"
23 #include "viewer-stream.h"
25 #include <sys/types.h>
28 #define FILE_IO_STACK_BUFFER_SIZE 65536
30 /* Should be called with RCU read-side lock held. */
31 bool stream_get(struct relay_stream
*stream
)
33 return urcu_ref_get_unless_zero(&stream
->ref
);
37 * Get stream from stream id from the streams hash table. Return stream
38 * if found else NULL. A stream reference is taken when a stream is
39 * returned. stream_put() must be called on that stream.
41 struct relay_stream
*stream_get_by_id(uint64_t stream_id
)
43 struct lttng_ht_node_u64
*node
;
44 struct lttng_ht_iter iter
;
45 struct relay_stream
*stream
= NULL
;
48 lttng_ht_lookup(relay_streams_ht
, &stream_id
, &iter
);
49 node
= lttng_ht_iter_get_node_u64(&iter
);
51 DBG("Relay stream %" PRIu64
" not found", stream_id
);
54 stream
= caa_container_of(node
, struct relay_stream
, node
);
55 if (!stream_get(stream
)) {
63 static void stream_complete_rotation(struct relay_stream
*stream
)
65 DBG("Rotation completed for stream %" PRIu64
, stream
->stream_handle
);
66 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
67 tracefile_array_reset(stream
->tfa
);
68 tracefile_array_commit_seq(stream
->tfa
,
69 stream
->index_received_seqcount
);
71 lttng_trace_chunk_put(stream
->trace_chunk
);
72 stream
->trace_chunk
= stream
->ongoing_rotation
.value
.next_trace_chunk
;
73 stream
->ongoing_rotation
= (typeof(stream
->ongoing_rotation
)) {};
74 stream
->completed_rotation_count
++;
77 static int stream_create_data_output_file_from_trace_chunk(
78 struct relay_stream
*stream
,
79 struct lttng_trace_chunk
*trace_chunk
,
81 struct fs_handle
**out_file
)
84 char stream_path
[LTTNG_PATH_MAX
];
85 enum lttng_trace_chunk_status status
;
86 const int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
87 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
89 ASSERT_LOCKED(stream
->lock
);
91 ret
= utils_stream_file_path(stream
->path_name
, stream
->channel_name
,
92 stream
->tracefile_size
, stream
->tracefile_current_index
,
93 NULL
, stream_path
, sizeof(stream_path
));
98 if (stream
->tracefile_wrapped_around
|| force_unlink
) {
100 * The on-disk ring-buffer has wrapped around.
101 * Newly created stream files will replace existing files. Since
102 * live clients may be consuming existing files, the file about
103 * to be replaced is unlinked in order to not overwrite its
106 status
= lttng_trace_chunk_unlink_file(trace_chunk
,
108 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
109 PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
112 * Don't abort if the file doesn't exist, it is
113 * unexpected, but should not be a fatal error.
115 if (errno
!= ENOENT
) {
122 status
= lttng_trace_chunk_open_fs_handle(trace_chunk
, stream_path
,
123 flags
, mode
, out_file
, false);
124 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
125 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
133 static int stream_rotate_data_file(struct relay_stream
*stream
)
137 DBG("Rotating stream %" PRIu64
" data file with size %" PRIu64
,
138 stream
->stream_handle
, stream
->tracefile_size_current
);
141 fs_handle_close(stream
->file
);
145 stream
->tracefile_wrapped_around
= false;
146 stream
->tracefile_current_index
= 0;
148 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
149 enum lttng_trace_chunk_status chunk_status
;
151 chunk_status
= lttng_trace_chunk_create_subdirectory(
152 stream
->ongoing_rotation
.value
.next_trace_chunk
,
154 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
159 /* Rotate the data file. */
160 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
161 stream
->ongoing_rotation
.value
.next_trace_chunk
,
162 false, &stream
->file
);
164 ERR("Failed to rotate stream data file");
168 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
169 __func__
, stream
->stream_handle
, stream
->tracefile_size_current
);
170 stream
->tracefile_size_current
= 0;
171 stream
->pos_after_last_complete_data_index
= 0;
172 stream
->ongoing_rotation
.value
.data_rotated
= true;
174 if (stream
->ongoing_rotation
.value
.index_rotated
) {
175 /* Rotation completed; reset its state. */
176 stream_complete_rotation(stream
);
183 * If too much data has been written in a tracefile before we received the
184 * rotation command, we have to move the excess data to the new tracefile and
185 * perform the rotation. This can happen because the control and data
186 * connections are separate, the indexes as well as the commands arrive from
187 * the control connection and we have no control over the order so we could be
188 * in a situation where too much data has been received on the data connection
189 * before the rotation command on the control connection arrives.
191 static int rotate_truncate_stream(struct relay_stream
*stream
)
194 off_t lseek_ret
, previous_stream_copy_origin
;
195 uint64_t copy_bytes_left
, misplaced_data_size
;
196 bool acquired_reference
;
197 struct fs_handle
*previous_stream_file
= NULL
;
198 struct lttng_trace_chunk
*previous_chunk
= NULL
;
200 if (!LTTNG_OPTIONAL_GET(stream
->ongoing_rotation
).next_trace_chunk
) {
201 ERR("Protocol error encoutered in %s(): stream rotation "
202 "sequence number is before the current sequence number "
203 "and the next trace chunk is unset. Honoring this "
204 "rotation command would result in data loss",
210 ASSERT_LOCKED(stream
->lock
);
212 * Acquire a reference to the current trace chunk to ensure
213 * it is not reclaimed when `stream_rotate_data_file` is called.
214 * Failing to do so would violate the contract of the trace
215 * chunk API as an active file descriptor would outlive the
218 acquired_reference
= lttng_trace_chunk_get(stream
->trace_chunk
);
219 LTTNG_ASSERT(acquired_reference
);
220 previous_chunk
= stream
->trace_chunk
;
223 * Steal the stream's reference to its stream_fd. A new
224 * stream_fd will be created when the rotation completes and
225 * the orinal stream_fd will be used to copy the "extra" data
228 LTTNG_ASSERT(stream
->file
);
229 previous_stream_file
= stream
->file
;
232 LTTNG_ASSERT(!stream
->is_metadata
);
233 LTTNG_ASSERT(stream
->tracefile_size_current
>
234 stream
->pos_after_last_complete_data_index
);
235 misplaced_data_size
= stream
->tracefile_size_current
-
236 stream
->pos_after_last_complete_data_index
;
237 copy_bytes_left
= misplaced_data_size
;
238 previous_stream_copy_origin
= stream
->pos_after_last_complete_data_index
;
240 ret
= stream_rotate_data_file(stream
);
245 LTTNG_ASSERT(stream
->file
);
247 * Seek the current tracefile to the position at which the rotation
248 * should have occurred.
250 lseek_ret
= fs_handle_seek(previous_stream_file
, previous_stream_copy_origin
, SEEK_SET
);
252 PERROR("Failed to seek to offset %" PRIu64
253 " while copying extra data received before a stream rotation",
254 (uint64_t) previous_stream_copy_origin
);
259 /* Move data from the old file to the new file. */
260 while (copy_bytes_left
) {
262 char copy_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
263 const off_t copy_size_this_pass
= min_t(
264 off_t
, copy_bytes_left
, sizeof(copy_buffer
));
266 io_ret
= fs_handle_read(previous_stream_file
, copy_buffer
,
267 copy_size_this_pass
);
268 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
270 PERROR("Failed to read %" PRIu64
271 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
273 __FUNCTION__
, io_ret
,
274 stream
->stream_handle
);
276 ERR("Failed to read %" PRIu64
277 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
279 __FUNCTION__
, io_ret
,
280 stream
->stream_handle
);
286 io_ret
= fs_handle_write(
287 stream
->file
, copy_buffer
, copy_size_this_pass
);
288 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
290 PERROR("Failed to write %" PRIu64
291 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
293 __FUNCTION__
, io_ret
,
294 stream
->stream_handle
);
296 ERR("Failed to write %" PRIu64
297 " bytes from previous stream file in %s(), returned %zi: stream id = %" PRIu64
,
299 __FUNCTION__
, io_ret
,
300 stream
->stream_handle
);
305 copy_bytes_left
-= copy_size_this_pass
;
308 /* Truncate the file to get rid of the excess data. */
309 ret
= fs_handle_truncate(
310 previous_stream_file
, previous_stream_copy_origin
);
312 PERROR("Failed to truncate current stream file to offset %" PRIu64
,
313 previous_stream_copy_origin
);
318 * Update the offset and FD of all the eventual indexes created by the
319 * data connection before the rotation command arrived.
321 ret
= relay_index_switch_all_files(stream
);
323 ERR("Failed to rotate index file");
327 stream
->tracefile_size_current
= misplaced_data_size
;
328 /* Index and data contents are back in sync. */
329 stream
->pos_after_last_complete_data_index
= 0;
332 lttng_trace_chunk_put(previous_chunk
);
337 * Check if a stream's data file (as opposed to index) should be rotated
338 * (for session rotation).
339 * Must be called with the stream lock held.
341 * Return 0 on success, a negative value on error.
343 static int try_rotate_stream_data(struct relay_stream
*stream
)
347 if (caa_likely(!stream
->ongoing_rotation
.is_set
)) {
348 /* No rotation expected. */
352 if (stream
->ongoing_rotation
.value
.data_rotated
) {
353 /* Rotation of the data file has already occurred. */
357 DBG("%s: Stream %" PRIu64
358 " (rotate_at_index_packet_seq_num = %" PRIu64
359 ", rotate_at_prev_data_net_seq = %" PRIu64
360 ", prev_data_seq = %" PRIu64
")",
361 __func__
, stream
->stream_handle
,
362 stream
->ongoing_rotation
.value
.packet_seq_num
,
363 stream
->ongoing_rotation
.value
.prev_data_net_seq
,
364 stream
->prev_data_seq
);
366 if (stream
->prev_data_seq
== -1ULL ||
367 stream
->ongoing_rotation
.value
.prev_data_net_seq
== -1ULL ||
368 stream
->prev_data_seq
<
369 stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
371 * The next packet that will be written is not part of the next
374 DBG("Stream %" PRIu64
" data not yet ready for rotation "
375 "(rotate_at_index_packet_seq_num = %" PRIu64
376 ", rotate_at_prev_data_net_seq = %" PRIu64
377 ", prev_data_seq = %" PRIu64
")",
378 stream
->stream_handle
,
379 stream
->ongoing_rotation
.value
.packet_seq_num
,
380 stream
->ongoing_rotation
.value
.prev_data_net_seq
,
381 stream
->prev_data_seq
);
383 } else if (stream
->prev_data_seq
> stream
->ongoing_rotation
.value
.prev_data_net_seq
) {
385 * prev_data_seq is checked here since indexes and rotation
386 * commands are serialized with respect to each other.
388 DBG("Rotation after too much data has been written in tracefile "
389 "for stream %" PRIu64
", need to truncate before "
390 "rotating", stream
->stream_handle
);
391 ret
= rotate_truncate_stream(stream
);
393 ERR("Failed to truncate stream");
397 ret
= stream_rotate_data_file(stream
);
405 * Close the current index file if it is open, and create a new one.
407 * Return 0 on success, -1 on error.
409 static int create_index_file(struct relay_stream
*stream
,
410 struct lttng_trace_chunk
*chunk
)
413 uint32_t major
, minor
;
414 char *index_subpath
= NULL
;
415 enum lttng_trace_chunk_status status
;
417 ASSERT_LOCKED(stream
->lock
);
419 /* Put ref on previous index_file. */
420 if (stream
->index_file
) {
421 lttng_index_file_put(stream
->index_file
);
422 stream
->index_file
= NULL
;
424 major
= stream
->trace
->session
->major
;
425 minor
= stream
->trace
->session
->minor
;
431 ret
= asprintf(&index_subpath
, "%s/%s", stream
->path_name
,
437 status
= lttng_trace_chunk_create_subdirectory(chunk
,
440 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
444 status
= lttng_index_file_create_from_trace_chunk(
445 chunk
, stream
->path_name
,
446 stream
->channel_name
, stream
->tracefile_size
,
447 stream
->tracefile_current_index
,
448 lttng_to_index_major(major
, minor
),
449 lttng_to_index_minor(major
, minor
), true,
450 &stream
->index_file
);
451 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
463 * Check if a stream's index file should be rotated (for session rotation).
464 * Must be called with the stream lock held.
466 * Return 0 on success, a negative value on error.
468 static int try_rotate_stream_index(struct relay_stream
*stream
)
472 if (!stream
->ongoing_rotation
.is_set
) {
473 /* No rotation expected. */
477 if (stream
->ongoing_rotation
.value
.index_rotated
) {
478 /* Rotation of the index has already occurred. */
482 DBG("%s: Stream %" PRIu64
483 " (rotate_at_packet_seq_num = %" PRIu64
484 ", received_packet_seq_num = "
485 "(value = %" PRIu64
", is_set = %" PRIu8
"))",
486 __func__
, stream
->stream_handle
,
487 stream
->ongoing_rotation
.value
.packet_seq_num
,
488 stream
->received_packet_seq_num
.value
,
489 stream
->received_packet_seq_num
.is_set
);
491 if (!stream
->received_packet_seq_num
.is_set
||
492 LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 <
493 stream
->ongoing_rotation
.value
.packet_seq_num
) {
494 DBG("Stream %" PRIu64
" index not yet ready for rotation "
495 "(rotate_at_packet_seq_num = %" PRIu64
496 ", received_packet_seq_num = "
497 "(value = %" PRIu64
", is_set = %" PRIu8
"))",
498 stream
->stream_handle
,
499 stream
->ongoing_rotation
.value
.packet_seq_num
,
500 stream
->received_packet_seq_num
.value
,
501 stream
->received_packet_seq_num
.is_set
);
505 * The next index belongs to the new trace chunk; rotate.
506 * In overwrite mode, the packet seq num may jump over the
509 LTTNG_ASSERT(LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 >=
510 stream
->ongoing_rotation
.value
.packet_seq_num
);
511 DBG("Rotating stream %" PRIu64
" index file",
512 stream
->stream_handle
);
513 if (stream
->index_file
) {
514 lttng_index_file_put(stream
->index_file
);
515 stream
->index_file
= NULL
;
517 stream
->ongoing_rotation
.value
.index_rotated
= true;
520 * Set the rotation pivot position for the data, now that we have the
521 * net_seq_num matching the packet_seq_num index pivot position.
523 stream
->ongoing_rotation
.value
.prev_data_net_seq
=
524 stream
->prev_index_seq
;
525 if (stream
->ongoing_rotation
.value
.data_rotated
&&
526 stream
->ongoing_rotation
.value
.index_rotated
) {
527 /* Rotation completed; reset its state. */
528 DBG("Rotation completed for stream %" PRIu64
,
529 stream
->stream_handle
);
530 stream_complete_rotation(stream
);
538 static int stream_set_trace_chunk(struct relay_stream
*stream
,
539 struct lttng_trace_chunk
*chunk
)
542 enum lttng_trace_chunk_status status
;
543 bool acquired_reference
;
545 status
= lttng_trace_chunk_create_subdirectory(chunk
,
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
);
561 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
,
562 false, &stream
->file
);
568 * We keep ownership of path_name and channel_name.
570 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
571 uint64_t stream_handle
, char *path_name
,
572 char *channel_name
, uint64_t tracefile_size
,
573 uint64_t tracefile_count
)
576 struct relay_stream
*stream
= NULL
;
577 struct relay_session
*session
= trace
->session
;
578 bool acquired_reference
= false;
579 struct lttng_trace_chunk
*current_trace_chunk
;
581 stream
= zmalloc(sizeof(struct relay_stream
));
582 if (stream
== NULL
) {
583 PERROR("relay stream zmalloc");
587 stream
->stream_handle
= stream_handle
;
588 stream
->prev_data_seq
= -1ULL;
589 stream
->prev_index_seq
= -1ULL;
590 stream
->last_net_seq_num
= -1ULL;
591 stream
->ctf_stream_id
= -1ULL;
592 stream
->tracefile_size
= tracefile_size
;
593 stream
->tracefile_count
= tracefile_count
;
594 stream
->path_name
= path_name
;
595 stream
->channel_name
= channel_name
;
596 stream
->beacon_ts_end
= -1ULL;
597 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
598 pthread_mutex_init(&stream
->lock
, NULL
);
599 urcu_ref_init(&stream
->ref
);
600 ctf_trace_get(trace
);
601 stream
->trace
= trace
;
603 pthread_mutex_lock(&trace
->session
->lock
);
604 current_trace_chunk
= trace
->session
->current_trace_chunk
;
605 if (current_trace_chunk
) {
606 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
608 pthread_mutex_unlock(&trace
->session
->lock
);
609 if (!acquired_reference
) {
610 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
616 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
617 if (!stream
->indexes_ht
) {
618 ERR("Cannot created indexes_ht");
623 pthread_mutex_lock(&stream
->lock
);
624 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
625 pthread_mutex_unlock(&stream
->lock
);
627 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
628 trace
->session
->session_name
,
629 stream
->channel_name
);
633 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
639 stream
->is_metadata
= !strcmp(stream
->channel_name
,
640 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
, stream
->channel_name
,
660 stream
->stream_handle
);
666 fs_handle_close(stream
->file
);
672 if (acquired_reference
) {
673 lttng_trace_chunk_put(current_trace_chunk
);
679 * path_name and channel_name need to be freed explicitly here
680 * because we cannot rely on stream_put().
688 * Called with the session lock held.
690 void stream_publish(struct relay_stream
*stream
)
692 struct relay_session
*session
;
694 pthread_mutex_lock(&stream
->lock
);
695 if (stream
->published
) {
699 session
= stream
->trace
->session
;
701 pthread_mutex_lock(&session
->recv_list_lock
);
702 if (stream
->in_recv_list
) {
703 cds_list_del_rcu(&stream
->recv_node
);
704 stream
->in_recv_list
= false;
706 pthread_mutex_unlock(&session
->recv_list_lock
);
708 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
709 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
710 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
712 stream
->published
= true;
714 pthread_mutex_unlock(&stream
->lock
);
718 * Stream must be protected by holding the stream lock or by virtue of being
719 * called from stream_destroy.
721 static void stream_unpublish(struct relay_stream
*stream
)
723 if (stream
->in_stream_ht
) {
724 struct lttng_ht_iter iter
;
727 iter
.iter
.node
= &stream
->node
.node
;
728 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
730 stream
->in_stream_ht
= false;
732 if (stream
->published
) {
733 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
734 cds_list_del_rcu(&stream
->stream_node
);
735 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
736 stream
->published
= false;
740 static void stream_destroy(struct relay_stream
*stream
)
742 if (stream
->indexes_ht
) {
744 * Calling lttng_ht_destroy in call_rcu worker thread so
745 * we don't hold the RCU read-side lock while calling
748 lttng_ht_destroy(stream
->indexes_ht
);
751 tracefile_array_destroy(stream
->tfa
);
753 free(stream
->path_name
);
754 free(stream
->channel_name
);
758 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
760 struct relay_stream
*stream
=
761 caa_container_of(rcu_head
, struct 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
=
773 caa_container_of(ref
, struct relay_stream
, ref
);
774 struct relay_session
*session
;
776 session
= stream
->trace
->session
;
778 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
780 pthread_mutex_lock(&session
->recv_list_lock
);
781 session
->stream_count
--;
782 if (stream
->in_recv_list
) {
783 cds_list_del_rcu(&stream
->recv_node
);
784 stream
->in_recv_list
= false;
786 pthread_mutex_unlock(&session
->recv_list_lock
);
788 stream_unpublish(stream
);
791 fs_handle_close(stream
->file
);
794 if (stream
->index_file
) {
795 lttng_index_file_put(stream
->index_file
);
796 stream
->index_file
= NULL
;
799 ctf_trace_put(stream
->trace
);
800 stream
->trace
= NULL
;
802 stream_complete_rotation(stream
);
803 lttng_trace_chunk_put(stream
->trace_chunk
);
804 stream
->trace_chunk
= NULL
;
806 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
809 void stream_put(struct relay_stream
*stream
)
812 LTTNG_ASSERT(stream
->ref
.refcount
!= 0);
814 * Wait until we have processed all the stream packets before
815 * actually putting our last stream reference.
817 urcu_ref_put(&stream
->ref
, stream_release
);
821 int stream_set_pending_rotation(struct relay_stream
*stream
,
822 struct lttng_trace_chunk
*next_trace_chunk
,
823 uint64_t rotation_sequence_number
)
826 const struct relay_stream_rotation rotation
= {
827 .data_rotated
= false,
828 .index_rotated
= false,
829 .packet_seq_num
= rotation_sequence_number
,
830 .prev_data_net_seq
= -1ULL,
831 .next_trace_chunk
= next_trace_chunk
,
834 if (stream
->ongoing_rotation
.is_set
) {
835 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
840 if (next_trace_chunk
) {
841 const bool reference_acquired
=
842 lttng_trace_chunk_get(next_trace_chunk
);
844 LTTNG_ASSERT(reference_acquired
);
846 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
848 DBG("Setting pending rotation: stream_id = %" PRIu64
849 ", rotate_at_packet_seq_num = %" PRIu64
,
850 stream
->stream_handle
, rotation_sequence_number
);
851 if (stream
->is_metadata
) {
853 * A metadata stream has no index; consider it already rotated.
855 stream
->ongoing_rotation
.value
.index_rotated
= true;
856 if (next_trace_chunk
) {
858 * The metadata will be received again in the new chunk.
860 stream
->metadata_received
= 0;
862 ret
= stream_rotate_data_file(stream
);
864 ret
= try_rotate_stream_index(stream
);
869 ret
= try_rotate_stream_data(stream
);
878 void try_stream_close(struct relay_stream
*stream
)
880 bool session_aborted
;
881 struct relay_session
*session
= stream
->trace
->session
;
883 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
885 pthread_mutex_lock(&session
->lock
);
886 session_aborted
= session
->aborted
;
887 pthread_mutex_unlock(&session
->lock
);
889 pthread_mutex_lock(&stream
->lock
);
891 * Can be called concurently by connection close and reception of last
894 if (stream
->closed
) {
895 pthread_mutex_unlock(&stream
->lock
);
896 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
900 stream
->close_requested
= true;
902 if (stream
->last_net_seq_num
== -1ULL) {
904 * Handle connection close without explicit stream close
907 * We can be clever about indexes partially received in
908 * cases where we received the data socket part, but not
909 * the control socket part: since we're currently closing
910 * the stream on behalf of the control socket, we *know*
911 * there won't be any more control information for this
912 * socket. Therefore, we can destroy all indexes for
913 * which we have received only the file descriptor (from
914 * data socket). This takes care of consumerd crashes
915 * between sending the data and control information for
916 * a packet. Since those are sent in that order, we take
917 * care of consumerd crashes.
919 DBG("relay_index_close_partial_fd");
920 relay_index_close_partial_fd(stream
);
922 * Use the highest net_seq_num we currently have pending
923 * As end of stream indicator. Leave last_net_seq_num
924 * at -1ULL if we cannot find any index.
926 stream
->last_net_seq_num
= relay_index_find_last(stream
);
927 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
928 /* Fall-through into the next check. */
931 if (stream
->last_net_seq_num
!= -1ULL &&
932 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
933 && !session_aborted
) {
935 * Don't close since we still have data pending. This
936 * handles cases where an explicit close command has
937 * been received for this stream, and cases where the
938 * connection has been closed, and we are awaiting for
939 * index information from the data socket. It is
940 * therefore expected that all the index fd information
941 * we need has already been received on the control
942 * socket. Matching index information from data socket
943 * should be Expected Soon(TM).
945 * TODO: We should implement a timer to garbage collect
946 * streams after a timeout to be resilient against a
947 * consumerd implementation that would not match this
950 pthread_mutex_unlock(&stream
->lock
);
951 DBG("closing stream %" PRIu64
" aborted since it still has data pending", 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
);
976 if (stream
->index_file
) {
977 lttng_index_file_put(stream
->index_file
);
978 stream
->index_file
= NULL
;
980 lttng_trace_chunk_put(stream
->trace_chunk
);
981 stream
->trace_chunk
= NULL
;
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
,
992 ASSERT_LOCKED(stream
->lock
);
994 if (!stream
->file
|| !stream
->trace_chunk
) {
995 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
996 stream
->stream_handle
, stream
->channel_name
);
1001 if (caa_likely(stream
->tracefile_size
== 0)) {
1002 /* No size limit set; nothing to check. */
1007 * Check if writing the new packet would exceed the maximal file size.
1009 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) >
1010 stream
->tracefile_size
)) {
1011 const uint64_t new_file_index
=
1012 (stream
->tracefile_current_index
+ 1) %
1013 stream
->tracefile_count
;
1015 if (new_file_index
< stream
->tracefile_current_index
) {
1016 stream
->tracefile_wrapped_around
= true;
1018 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
1019 ", current_file_size = %" PRIu64
1020 ", packet_size = %zu, current_file_index = %" PRIu64
1021 " new_file_index = %" PRIu64
,
1022 stream
->stream_handle
,
1023 stream
->tracefile_size_current
, packet_size
,
1024 stream
->tracefile_current_index
, new_file_index
);
1025 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_WRITE
);
1026 stream
->tracefile_current_index
= new_file_index
;
1029 fs_handle_close(stream
->file
);
1030 stream
->file
= NULL
;
1032 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
1033 stream
->trace_chunk
, false, &stream
->file
);
1035 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
1036 stream
->stream_handle
);
1041 * Reset current size because we just performed a stream
1044 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
1045 __func__
, stream
->stream_handle
, stream
->tracefile_size_current
);
1046 stream
->tracefile_size_current
= 0;
1047 *file_rotated
= true;
1049 *file_rotated
= false;
1055 /* Note that the packet is not necessarily complete. */
1056 int stream_write(struct relay_stream
*stream
,
1057 const struct lttng_buffer_view
*packet
, size_t padding_len
)
1061 size_t padding_to_write
= padding_len
;
1062 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
1064 ASSERT_LOCKED(stream
->lock
);
1065 memset(padding_buffer
, 0,
1066 min(sizeof(padding_buffer
), padding_to_write
));
1068 if (!stream
->file
|| !stream
->trace_chunk
) {
1069 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
1070 stream
->stream_handle
, stream
->channel_name
);
1075 write_ret
= fs_handle_write(
1076 stream
->file
, packet
->data
, packet
->size
);
1077 if (write_ret
!= packet
->size
) {
1078 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
1079 stream
->is_metadata
? "metadata " : "",
1080 stream
->stream_handle
);
1086 while (padding_to_write
> 0) {
1087 const size_t padding_to_write_this_pass
=
1088 min(padding_to_write
, sizeof(padding_buffer
));
1090 write_ret
= fs_handle_write(stream
->file
, padding_buffer
,
1091 padding_to_write_this_pass
);
1092 if (write_ret
!= padding_to_write_this_pass
) {
1093 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
1094 stream
->is_metadata
? "metadata " : "",
1095 stream
->stream_handle
);
1099 padding_to_write
-= padding_to_write_this_pass
;
1102 if (stream
->is_metadata
) {
1105 recv_len
= packet
? packet
->size
: 0;
1106 recv_len
+= padding_len
;
1107 stream
->metadata_received
+= recv_len
;
1109 stream
->no_new_metadata_notified
= false;
1113 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1114 stream
->is_metadata
? "metadata " : "",
1115 stream
->stream_handle
,
1116 packet
? packet
->size
: (size_t) 0, padding_len
);
1122 * Update index after receiving a packet for a data stream.
1124 * Called with the stream lock held.
1126 * Return 0 on success else a negative value.
1128 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
1129 bool rotate_index
, bool *flushed
, uint64_t total_size
)
1132 uint64_t data_offset
;
1133 struct relay_index
*index
;
1135 LTTNG_ASSERT(stream
->trace_chunk
);
1136 ASSERT_LOCKED(stream
->lock
);
1137 /* Get data offset because we are about to update the index. */
1138 data_offset
= htobe64(stream
->tracefile_size_current
);
1140 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1141 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
1144 * Lookup for an existing index for that stream id/sequence
1145 * number. If it exists, the control thread has already received the
1146 * data for it, thus we need to write it to disk.
1148 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1154 if (rotate_index
|| !stream
->index_file
) {
1155 ret
= create_index_file(stream
, stream
->trace_chunk
);
1157 ERR("Failed to create index file for stream %" PRIu64
,
1158 stream
->stream_handle
);
1159 /* Put self-ref for this index due to error. */
1160 relay_index_put(index
);
1166 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1168 /* Put self-ref for this index due to error. */
1169 relay_index_put(index
);
1174 ret
= relay_index_try_flush(index
);
1176 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1177 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1178 stream
->index_received_seqcount
++;
1179 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1180 be64toh(index
->index_data
.packet_seq_num
));
1182 } else if (ret
> 0) {
1183 index
->total_size
= total_size
;
1190 * relay_index_try_flush is responsible for the self-reference
1191 * put of the index object on error.
1193 ERR("relay_index_try_flush error %d", ret
);
1200 int stream_complete_packet(struct relay_stream
*stream
, size_t packet_total_size
,
1201 uint64_t sequence_number
, bool index_flushed
)
1205 ASSERT_LOCKED(stream
->lock
);
1207 stream
->tracefile_size_current
+= packet_total_size
;
1208 if (index_flushed
) {
1209 stream
->pos_after_last_complete_data_index
=
1210 stream
->tracefile_size_current
;
1211 stream
->prev_index_seq
= sequence_number
;
1212 ret
= try_rotate_stream_index(stream
);
1218 stream
->prev_data_seq
= sequence_number
;
1219 ret
= try_rotate_stream_data(stream
);
1225 int stream_add_index(struct relay_stream
*stream
,
1226 const struct lttcomm_relayd_index
*index_info
)
1229 struct relay_index
*index
;
1231 ASSERT_LOCKED(stream
->lock
);
1233 DBG("stream_add_index for stream %" PRIu64
, stream
->stream_handle
);
1235 /* Live beacon handling */
1236 if (index_info
->packet_size
== 0) {
1237 DBG("Received live beacon for stream %" PRIu64
,
1238 stream
->stream_handle
);
1241 * Only flag a stream inactive when it has already
1242 * received data and no indexes are in flight.
1244 if (stream
->index_received_seqcount
> 0
1245 && stream
->indexes_in_flight
== 0) {
1246 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1251 stream
->beacon_ts_end
= -1ULL;
1254 if (stream
->ctf_stream_id
== -1ULL) {
1255 stream
->ctf_stream_id
= index_info
->stream_id
;
1258 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1261 ERR("Failed to get or create index %" PRIu64
,
1262 index_info
->net_seq_num
);
1265 if (relay_index_set_control_data(index
, index_info
,
1266 stream
->trace
->session
->minor
)) {
1267 ERR("set_index_control_data error");
1268 relay_index_put(index
);
1272 ret
= relay_index_try_flush(index
);
1274 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1275 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1276 stream
->index_received_seqcount
++;
1277 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1278 stream
->prev_index_seq
= index_info
->net_seq_num
;
1279 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1280 index_info
->packet_seq_num
);
1282 ret
= try_rotate_stream_index(stream
);
1286 ret
= try_rotate_stream_data(stream
);
1290 } else if (ret
> 0) {
1297 * relay_index_try_flush is responsible for the self-reference
1298 * put of the index object on error.
1300 ERR("relay_index_try_flush error %d", ret
);
1307 static void print_stream_indexes(struct relay_stream
*stream
)
1309 struct lttng_ht_iter iter
;
1310 struct relay_index
*index
;
1313 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
1315 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1316 " stream %" PRIu64
" trace %" PRIu64
1317 " session %" PRIu64
,
1320 stream
->ref
.refcount
,
1321 index
->stream
->stream_handle
,
1322 index
->stream
->trace
->id
,
1323 index
->stream
->trace
->session
->id
);
1328 int stream_reset_file(struct relay_stream
*stream
)
1330 ASSERT_LOCKED(stream
->lock
);
1335 ret
= fs_handle_close(stream
->file
);
1337 ERR("Failed to close stream file handle: channel name = \"%s\", id = %" PRIu64
,
1338 stream
->channel_name
,
1339 stream
->stream_handle
);
1341 stream
->file
= NULL
;
1344 DBG("%s: reset tracefile_size_current for stream %" PRIu64
" was %" PRIu64
,
1345 __func__
, stream
->stream_handle
, stream
->tracefile_size_current
);
1346 stream
->tracefile_size_current
= 0;
1347 stream
->prev_data_seq
= 0;
1348 stream
->prev_index_seq
= 0;
1349 /* Note that this does not reset the tracefile array. */
1350 stream
->tracefile_current_index
= 0;
1351 stream
->pos_after_last_complete_data_index
= 0;
1353 return stream_create_data_output_file_from_trace_chunk(stream
,
1354 stream
->trace_chunk
, true, &stream
->file
);
1357 void print_relay_streams(void)
1359 struct lttng_ht_iter iter
;
1360 struct relay_stream
*stream
;
1362 if (!relay_streams_ht
) {
1367 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1369 if (!stream_get(stream
)) {
1372 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1373 " session %" PRIu64
,
1375 stream
->ref
.refcount
,
1376 stream
->stream_handle
,
1378 stream
->trace
->session
->id
);
1379 print_stream_indexes(stream
);