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
, false);
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 status
= 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 &stream
->index_file
);
455 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
467 * Check if a stream's index file should be rotated (for session rotation).
468 * Must be called with the stream lock held.
470 * Return 0 on success, a negative value on error.
472 static int try_rotate_stream_index(struct relay_stream
*stream
)
476 if (!stream
->ongoing_rotation
.is_set
) {
477 /* No rotation expected. */
481 if (stream
->ongoing_rotation
.value
.index_rotated
) {
482 /* Rotation of the index has already occurred. */
486 if (!stream
->received_packet_seq_num
.is_set
||
487 LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 <
488 stream
->ongoing_rotation
.value
.packet_seq_num
) {
489 DBG("Stream %" PRIu64
" index not yet ready for rotation "
490 "(rotate_at_packet_seq_num = %" PRIu64
491 ", received_packet_seq_num = "
492 "(value = %" PRIu64
", is_set = %" PRIu8
"))",
493 stream
->stream_handle
,
494 stream
->ongoing_rotation
.value
.packet_seq_num
,
495 stream
->received_packet_seq_num
.value
,
496 stream
->received_packet_seq_num
.is_set
);
500 * The next index belongs to the new trace chunk; rotate.
501 * In overwrite mode, the packet seq num may jump over the
504 assert(LTTNG_OPTIONAL_GET(stream
->received_packet_seq_num
) + 1 >=
505 stream
->ongoing_rotation
.value
.packet_seq_num
);
506 DBG("Rotating stream %" PRIu64
" index file",
507 stream
->stream_handle
);
508 ret
= create_index_file(stream
,
509 stream
->ongoing_rotation
.value
.next_trace_chunk
);
510 stream
->ongoing_rotation
.value
.index_rotated
= true;
513 * Set the rotation pivot position for the data, now that we have the
514 * net_seq_num matching the packet_seq_num index pivot position.
516 stream
->ongoing_rotation
.value
.prev_data_net_seq
=
517 stream
->prev_index_seq
;
518 if (stream
->ongoing_rotation
.value
.data_rotated
&&
519 stream
->ongoing_rotation
.value
.index_rotated
) {
520 /* Rotation completed; reset its state. */
521 DBG("Rotation completed for stream %" PRIu64
,
522 stream
->stream_handle
);
523 stream_complete_rotation(stream
);
531 static int stream_set_trace_chunk(struct relay_stream
*stream
,
532 struct lttng_trace_chunk
*chunk
)
535 enum lttng_trace_chunk_status status
;
536 bool acquired_reference
;
537 struct stream_fd
*new_stream_fd
= NULL
;
539 status
= lttng_trace_chunk_create_subdirectory(chunk
,
541 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
546 lttng_trace_chunk_put(stream
->trace_chunk
);
547 acquired_reference
= lttng_trace_chunk_get(chunk
);
548 assert(acquired_reference
);
549 stream
->trace_chunk
= chunk
;
551 if (stream
->stream_fd
) {
552 stream_fd_put(stream
->stream_fd
);
553 stream
->stream_fd
= NULL
;
555 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
,
556 false, &new_stream_fd
);
557 stream
->stream_fd
= new_stream_fd
;
563 * We keep ownership of path_name and channel_name.
565 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
566 uint64_t stream_handle
, char *path_name
,
567 char *channel_name
, uint64_t tracefile_size
,
568 uint64_t tracefile_count
)
571 struct relay_stream
*stream
= NULL
;
572 struct relay_session
*session
= trace
->session
;
573 bool acquired_reference
= false;
574 struct lttng_trace_chunk
*current_trace_chunk
;
576 stream
= zmalloc(sizeof(struct relay_stream
));
577 if (stream
== NULL
) {
578 PERROR("relay stream zmalloc");
582 stream
->stream_handle
= stream_handle
;
583 stream
->prev_data_seq
= -1ULL;
584 stream
->prev_index_seq
= -1ULL;
585 stream
->last_net_seq_num
= -1ULL;
586 stream
->ctf_stream_id
= -1ULL;
587 stream
->tracefile_size
= tracefile_size
;
588 stream
->tracefile_count
= tracefile_count
;
589 stream
->path_name
= path_name
;
590 stream
->channel_name
= channel_name
;
591 stream
->beacon_ts_end
= -1ULL;
592 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
593 pthread_mutex_init(&stream
->lock
, NULL
);
594 urcu_ref_init(&stream
->ref
);
595 ctf_trace_get(trace
);
596 stream
->trace
= trace
;
598 pthread_mutex_lock(&trace
->session
->lock
);
599 current_trace_chunk
= trace
->session
->current_trace_chunk
;
600 if (current_trace_chunk
) {
601 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
603 pthread_mutex_unlock(&trace
->session
->lock
);
604 if (!acquired_reference
) {
605 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
611 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
612 if (!stream
->indexes_ht
) {
613 ERR("Cannot created indexes_ht");
618 pthread_mutex_lock(&stream
->lock
);
619 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
620 pthread_mutex_unlock(&stream
->lock
);
622 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
623 trace
->session
->session_name
,
624 stream
->channel_name
);
628 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
634 stream
->is_metadata
= !strcmp(stream
->channel_name
,
635 DEFAULT_METADATA_NAME
);
636 stream
->in_recv_list
= true;
639 * Add the stream in the recv list of the session. Once the end stream
640 * message is received, all session streams are published.
642 pthread_mutex_lock(&session
->recv_list_lock
);
643 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
644 session
->stream_count
++;
645 pthread_mutex_unlock(&session
->recv_list_lock
);
648 * Both in the ctf_trace object and the global stream ht since the data
649 * side of the relayd does not have the concept of session.
651 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
652 stream
->in_stream_ht
= true;
654 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
655 stream
->stream_handle
);
660 if (stream
->stream_fd
) {
661 stream_fd_put(stream
->stream_fd
);
662 stream
->stream_fd
= NULL
;
667 if (acquired_reference
) {
668 lttng_trace_chunk_put(current_trace_chunk
);
674 * path_name and channel_name need to be freed explicitly here
675 * because we cannot rely on stream_put().
683 * Called with the session lock held.
685 void stream_publish(struct relay_stream
*stream
)
687 struct relay_session
*session
;
689 pthread_mutex_lock(&stream
->lock
);
690 if (stream
->published
) {
694 session
= stream
->trace
->session
;
696 pthread_mutex_lock(&session
->recv_list_lock
);
697 if (stream
->in_recv_list
) {
698 cds_list_del_rcu(&stream
->recv_node
);
699 stream
->in_recv_list
= false;
701 pthread_mutex_unlock(&session
->recv_list_lock
);
703 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
704 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
705 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
707 stream
->published
= true;
709 pthread_mutex_unlock(&stream
->lock
);
713 * Stream must be protected by holding the stream lock or by virtue of being
714 * called from stream_destroy.
716 static void stream_unpublish(struct relay_stream
*stream
)
718 if (stream
->in_stream_ht
) {
719 struct lttng_ht_iter iter
;
722 iter
.iter
.node
= &stream
->node
.node
;
723 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
725 stream
->in_stream_ht
= false;
727 if (stream
->published
) {
728 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
729 cds_list_del_rcu(&stream
->stream_node
);
730 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
731 stream
->published
= false;
735 static void stream_destroy(struct relay_stream
*stream
)
737 if (stream
->indexes_ht
) {
739 * Calling lttng_ht_destroy in call_rcu worker thread so
740 * we don't hold the RCU read-side lock while calling
743 lttng_ht_destroy(stream
->indexes_ht
);
746 tracefile_array_destroy(stream
->tfa
);
748 free(stream
->path_name
);
749 free(stream
->channel_name
);
753 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
755 struct relay_stream
*stream
=
756 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
758 stream_destroy(stream
);
762 * No need to take stream->lock since this is only called on the final
763 * stream_put which ensures that a single thread may act on the stream.
765 static void stream_release(struct urcu_ref
*ref
)
767 struct relay_stream
*stream
=
768 caa_container_of(ref
, struct relay_stream
, ref
);
769 struct relay_session
*session
;
771 session
= stream
->trace
->session
;
773 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
775 pthread_mutex_lock(&session
->recv_list_lock
);
776 session
->stream_count
--;
777 if (stream
->in_recv_list
) {
778 cds_list_del_rcu(&stream
->recv_node
);
779 stream
->in_recv_list
= false;
781 pthread_mutex_unlock(&session
->recv_list_lock
);
783 stream_unpublish(stream
);
785 if (stream
->stream_fd
) {
786 stream_fd_put(stream
->stream_fd
);
787 stream
->stream_fd
= NULL
;
789 if (stream
->index_file
) {
790 lttng_index_file_put(stream
->index_file
);
791 stream
->index_file
= NULL
;
794 ctf_trace_put(stream
->trace
);
795 stream
->trace
= NULL
;
797 stream_complete_rotation(stream
);
798 lttng_trace_chunk_put(stream
->trace_chunk
);
799 stream
->trace_chunk
= NULL
;
801 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
804 void stream_put(struct relay_stream
*stream
)
807 assert(stream
->ref
.refcount
!= 0);
809 * Wait until we have processed all the stream packets before
810 * actually putting our last stream reference.
812 urcu_ref_put(&stream
->ref
, stream_release
);
816 int stream_set_pending_rotation(struct relay_stream
*stream
,
817 struct lttng_trace_chunk
*next_trace_chunk
,
818 uint64_t rotation_sequence_number
)
821 const struct relay_stream_rotation rotation
= {
822 .data_rotated
= false,
823 .index_rotated
= false,
824 .packet_seq_num
= rotation_sequence_number
,
825 .prev_data_net_seq
= -1ULL,
826 .next_trace_chunk
= next_trace_chunk
,
829 if (stream
->ongoing_rotation
.is_set
) {
830 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
835 if (next_trace_chunk
) {
836 const bool reference_acquired
=
837 lttng_trace_chunk_get(next_trace_chunk
);
839 assert(reference_acquired
);
841 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
843 DBG("Setting pending rotation: stream_id = %" PRIu64
844 ", rotate_at_packet_seq_num = %" PRIu64
,
845 stream
->stream_handle
, rotation_sequence_number
);
846 if (stream
->is_metadata
) {
848 * A metadata stream has no index; consider it already rotated.
850 stream
->ongoing_rotation
.value
.index_rotated
= true;
851 ret
= stream_rotate_data_file(stream
);
853 ret
= try_rotate_stream_index(stream
);
858 ret
= try_rotate_stream_data(stream
);
867 void try_stream_close(struct relay_stream
*stream
)
869 bool session_aborted
;
870 struct relay_session
*session
= stream
->trace
->session
;
872 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
874 pthread_mutex_lock(&session
->lock
);
875 session_aborted
= session
->aborted
;
876 pthread_mutex_unlock(&session
->lock
);
878 pthread_mutex_lock(&stream
->lock
);
880 * Can be called concurently by connection close and reception of last
883 if (stream
->closed
) {
884 pthread_mutex_unlock(&stream
->lock
);
885 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
889 stream
->close_requested
= true;
891 if (stream
->last_net_seq_num
== -1ULL) {
893 * Handle connection close without explicit stream close
896 * We can be clever about indexes partially received in
897 * cases where we received the data socket part, but not
898 * the control socket part: since we're currently closing
899 * the stream on behalf of the control socket, we *know*
900 * there won't be any more control information for this
901 * socket. Therefore, we can destroy all indexes for
902 * which we have received only the file descriptor (from
903 * data socket). This takes care of consumerd crashes
904 * between sending the data and control information for
905 * a packet. Since those are sent in that order, we take
906 * care of consumerd crashes.
908 DBG("relay_index_close_partial_fd");
909 relay_index_close_partial_fd(stream
);
911 * Use the highest net_seq_num we currently have pending
912 * As end of stream indicator. Leave last_net_seq_num
913 * at -1ULL if we cannot find any index.
915 stream
->last_net_seq_num
= relay_index_find_last(stream
);
916 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
917 /* Fall-through into the next check. */
920 if (stream
->last_net_seq_num
!= -1ULL &&
921 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
922 && !session_aborted
) {
924 * Don't close since we still have data pending. This
925 * handles cases where an explicit close command has
926 * been received for this stream, and cases where the
927 * connection has been closed, and we are awaiting for
928 * index information from the data socket. It is
929 * therefore expected that all the index fd information
930 * we need has already been received on the control
931 * socket. Matching index information from data socket
932 * should be Expected Soon(TM).
934 * TODO: We should implement a timer to garbage collect
935 * streams after a timeout to be resilient against a
936 * consumerd implementation that would not match this
939 pthread_mutex_unlock(&stream
->lock
);
940 DBG("closing stream %" PRIu64
" aborted since it still has data pending", stream
->stream_handle
);
944 * We received all the indexes we can expect.
946 stream_unpublish(stream
);
947 stream
->closed
= true;
948 /* Relay indexes are only used by the "consumer/sessiond" end. */
949 relay_index_close_all(stream
);
952 * If we are closed by an application exiting (per-pid buffers),
953 * we need to put our reference on the stream trace chunk right
954 * away, because otherwise still holding the reference on the
955 * trace chunk could allow a viewer stream (which holds a reference
956 * to the stream) to postpone destroy waiting for the chunk to cease
957 * to exist endlessly until the viewer is detached.
960 /* Put stream fd before put chunk. */
961 if (stream
->stream_fd
) {
962 stream_fd_put(stream
->stream_fd
);
963 stream
->stream_fd
= NULL
;
965 if (stream
->index_file
) {
966 lttng_index_file_put(stream
->index_file
);
967 stream
->index_file
= NULL
;
969 lttng_trace_chunk_put(stream
->trace_chunk
);
970 stream
->trace_chunk
= NULL
;
971 pthread_mutex_unlock(&stream
->lock
);
972 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
976 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
,
981 ASSERT_LOCKED(stream
->lock
);
983 if (!stream
->stream_fd
|| !stream
->trace_chunk
) {
984 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
985 stream
->stream_handle
, stream
->channel_name
);
990 if (caa_likely(stream
->tracefile_size
== 0)) {
991 /* No size limit set; nothing to check. */
996 * Check if writing the new packet would exceed the maximal file size.
998 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) >
999 stream
->tracefile_size
)) {
1000 const uint64_t new_file_index
=
1001 (stream
->tracefile_current_index
+ 1) %
1002 stream
->tracefile_count
;
1004 if (new_file_index
< stream
->tracefile_current_index
) {
1005 stream
->tracefile_wrapped_around
= true;
1007 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
1008 ", current_file_size = %" PRIu64
1009 ", packet_size = %zu, current_file_index = %" PRIu64
1010 " new_file_index = %" PRIu64
,
1011 stream
->stream_handle
,
1012 stream
->tracefile_size_current
, packet_size
,
1013 stream
->tracefile_current_index
, new_file_index
);
1014 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_WRITE
);
1015 stream
->tracefile_current_index
= new_file_index
;
1017 if (stream
->stream_fd
) {
1018 stream_fd_put(stream
->stream_fd
);
1019 stream
->stream_fd
= NULL
;
1021 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
1022 stream
->trace_chunk
, false, &stream
->stream_fd
);
1024 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
1025 stream
->stream_handle
);
1030 * Reset current size because we just performed a stream
1033 stream
->tracefile_size_current
= 0;
1034 *file_rotated
= true;
1036 *file_rotated
= false;
1042 /* Note that the packet is not necessarily complete. */
1043 int stream_write(struct relay_stream
*stream
,
1044 const struct lttng_buffer_view
*packet
, size_t padding_len
)
1048 size_t padding_to_write
= padding_len
;
1049 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
1051 ASSERT_LOCKED(stream
->lock
);
1052 memset(padding_buffer
, 0,
1053 min(sizeof(padding_buffer
), padding_to_write
));
1055 if (!stream
->stream_fd
|| !stream
->trace_chunk
) {
1056 ERR("Protocol error: received a packet for a stream that doesn't have a current trace chunk: stream_id = %" PRIu64
", channel_name = %s",
1057 stream
->stream_handle
, stream
->channel_name
);
1062 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1063 packet
->data
, packet
->size
);
1064 if (write_ret
!= packet
->size
) {
1065 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
1066 stream
->is_metadata
? "metadata " : "",
1067 stream
->stream_handle
);
1073 while (padding_to_write
> 0) {
1074 const size_t padding_to_write_this_pass
=
1075 min(padding_to_write
, sizeof(padding_buffer
));
1077 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1078 padding_buffer
, padding_to_write_this_pass
);
1079 if (write_ret
!= padding_to_write_this_pass
) {
1080 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
1081 stream
->is_metadata
? "metadata " : "",
1082 stream
->stream_handle
);
1086 padding_to_write
-= padding_to_write_this_pass
;
1089 if (stream
->is_metadata
) {
1092 recv_len
= packet
? packet
->size
: 0;
1093 recv_len
+= padding_len
;
1094 stream
->metadata_received
+= recv_len
;
1096 stream
->no_new_metadata_notified
= false;
1100 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1101 stream
->is_metadata
? "metadata " : "",
1102 stream
->stream_handle
,
1103 packet
? packet
->size
: (size_t) 0, padding_len
);
1109 * Update index after receiving a packet for a data stream.
1111 * Called with the stream lock held.
1113 * Return 0 on success else a negative value.
1115 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
1116 bool rotate_index
, bool *flushed
, uint64_t total_size
)
1119 uint64_t data_offset
;
1120 struct relay_index
*index
;
1122 assert(stream
->trace_chunk
);
1123 ASSERT_LOCKED(stream
->lock
);
1124 /* Get data offset because we are about to update the index. */
1125 data_offset
= htobe64(stream
->tracefile_size_current
);
1127 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1128 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
1131 * Lookup for an existing index for that stream id/sequence
1132 * number. If it exists, the control thread has already received the
1133 * data for it, thus we need to write it to disk.
1135 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1141 if (rotate_index
|| !stream
->index_file
) {
1142 ret
= create_index_file(stream
, stream
->trace_chunk
);
1144 ERR("Failed to create index file for stream %" PRIu64
,
1145 stream
->stream_handle
);
1146 /* Put self-ref for this index due to error. */
1147 relay_index_put(index
);
1153 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1155 /* Put self-ref for this index due to error. */
1156 relay_index_put(index
);
1161 ret
= relay_index_try_flush(index
);
1163 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1164 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1165 stream
->index_received_seqcount
++;
1166 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1167 be64toh(index
->index_data
.packet_seq_num
));
1169 } else if (ret
> 0) {
1170 index
->total_size
= total_size
;
1177 * relay_index_try_flush is responsible for the self-reference
1178 * put of the index object on error.
1180 ERR("relay_index_try_flush error %d", ret
);
1187 int stream_complete_packet(struct relay_stream
*stream
, size_t packet_total_size
,
1188 uint64_t sequence_number
, bool index_flushed
)
1192 ASSERT_LOCKED(stream
->lock
);
1194 stream
->tracefile_size_current
+= packet_total_size
;
1195 if (index_flushed
) {
1196 stream
->pos_after_last_complete_data_index
=
1197 stream
->tracefile_size_current
;
1198 stream
->prev_index_seq
= sequence_number
;
1199 ret
= try_rotate_stream_index(stream
);
1205 stream
->prev_data_seq
= sequence_number
;
1206 ret
= try_rotate_stream_data(stream
);
1212 int stream_add_index(struct relay_stream
*stream
,
1213 const struct lttcomm_relayd_index
*index_info
)
1216 struct relay_index
*index
;
1218 ASSERT_LOCKED(stream
->lock
);
1220 /* Live beacon handling */
1221 if (index_info
->packet_size
== 0) {
1222 DBG("Received live beacon for stream %" PRIu64
,
1223 stream
->stream_handle
);
1226 * Only flag a stream inactive when it has already
1227 * received data and no indexes are in flight.
1229 if (stream
->index_received_seqcount
> 0
1230 && stream
->indexes_in_flight
== 0) {
1231 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1236 stream
->beacon_ts_end
= -1ULL;
1239 if (stream
->ctf_stream_id
== -1ULL) {
1240 stream
->ctf_stream_id
= index_info
->stream_id
;
1243 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1246 ERR("Failed to get or create index %" PRIu64
,
1247 index_info
->net_seq_num
);
1250 if (relay_index_set_control_data(index
, index_info
,
1251 stream
->trace
->session
->minor
)) {
1252 ERR("set_index_control_data error");
1253 relay_index_put(index
);
1257 ret
= relay_index_try_flush(index
);
1259 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1260 tracefile_array_commit_seq(stream
->tfa
, stream
->index_received_seqcount
);
1261 stream
->index_received_seqcount
++;
1262 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1263 stream
->prev_index_seq
= index_info
->net_seq_num
;
1264 LTTNG_OPTIONAL_SET(&stream
->received_packet_seq_num
,
1265 index_info
->packet_seq_num
);
1267 ret
= try_rotate_stream_index(stream
);
1271 ret
= try_rotate_stream_data(stream
);
1275 } else if (ret
> 0) {
1282 * relay_index_try_flush is responsible for the self-reference
1283 * put of the index object on error.
1285 ERR("relay_index_try_flush error %d", ret
);
1292 static void print_stream_indexes(struct relay_stream
*stream
)
1294 struct lttng_ht_iter iter
;
1295 struct relay_index
*index
;
1298 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
1300 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1301 " stream %" PRIu64
" trace %" PRIu64
1302 " session %" PRIu64
,
1305 stream
->ref
.refcount
,
1306 index
->stream
->stream_handle
,
1307 index
->stream
->trace
->id
,
1308 index
->stream
->trace
->session
->id
);
1313 int stream_reset_file(struct relay_stream
*stream
)
1315 ASSERT_LOCKED(stream
->lock
);
1317 if (stream
->stream_fd
) {
1318 stream_fd_put(stream
->stream_fd
);
1319 stream
->stream_fd
= NULL
;
1322 stream
->tracefile_size_current
= 0;
1323 stream
->prev_data_seq
= 0;
1324 stream
->prev_index_seq
= 0;
1325 /* Note that this does not reset the tracefile array. */
1326 stream
->tracefile_current_index
= 0;
1327 stream
->pos_after_last_complete_data_index
= 0;
1329 return stream_create_data_output_file_from_trace_chunk(stream
,
1330 stream
->trace_chunk
, true, &stream
->stream_fd
);
1333 void print_relay_streams(void)
1335 struct lttng_ht_iter iter
;
1336 struct relay_stream
*stream
;
1338 if (!relay_streams_ht
) {
1343 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1345 if (!stream_get(stream
)) {
1348 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1349 " session %" PRIu64
,
1351 stream
->ref
.refcount
,
1352 stream
->stream_handle
,
1354 stream
->trace
->session
->id
);
1355 print_stream_indexes(stream
);