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
);
93 assert(stream
->trace_chunk
);
95 ret
= utils_stream_file_path(stream
->path_name
, stream
->channel_name
,
96 stream
->tracefile_size
, stream
->tracefile_current_index
,
97 NULL
, stream_path
, sizeof(stream_path
));
102 if (stream
->tracefile_wrapped_around
|| force_unlink
) {
104 * The on-disk ring-buffer has wrapped around.
105 * Newly created stream files will replace existing files. Since
106 * live clients may be consuming existing files, the file about
107 * to be replaced is unlinked in order to not overwrite its
110 status
= lttng_trace_chunk_unlink_file(trace_chunk
,
112 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
113 PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
116 * Don't abort if the file doesn't exist, it is
117 * unexpected, but should not be a fatal error.
119 if (errno
!= ENOENT
) {
126 status
= lttng_trace_chunk_open_file(
127 trace_chunk
, stream_path
, flags
, mode
, &fd
);
128 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
129 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
134 *out_stream_fd
= stream_fd_create(fd
);
135 if (!*out_stream_fd
) {
137 PERROR("Error closing stream file descriptor %d", ret
);
146 static int stream_rotate_data_file(struct relay_stream
*stream
)
150 DBG("Rotating stream %" PRIu64
" data file",
151 stream
->stream_handle
);
153 if (stream
->stream_fd
) {
154 stream_fd_put(stream
->stream_fd
);
155 stream
->stream_fd
= NULL
;
158 stream
->tracefile_wrapped_around
= false;
159 stream
->tracefile_current_index
= 0;
161 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
162 struct stream_fd
*new_stream_fd
= NULL
;
163 enum lttng_trace_chunk_status chunk_status
;
165 chunk_status
= lttng_trace_chunk_create_subdirectory(
166 stream
->ongoing_rotation
.value
.next_trace_chunk
,
168 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
173 /* Rotate the data file. */
174 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
175 stream
->ongoing_rotation
.value
.next_trace_chunk
,
176 false, &new_stream_fd
);
177 stream
->stream_fd
= new_stream_fd
;
179 ERR("Failed to rotate stream data file");
183 stream
->tracefile_size_current
= 0;
184 stream
->pos_after_last_complete_data_index
= 0;
185 stream
->ongoing_rotation
.value
.data_rotated
= true;
187 if (stream
->ongoing_rotation
.value
.index_rotated
) {
188 /* Rotation completed; reset its state. */
189 stream_complete_rotation(stream
);
196 * If too much data has been written in a tracefile before we received the
197 * rotation command, we have to move the excess data to the new tracefile and
198 * perform the rotation. This can happen because the control and data
199 * connections are separate, the indexes as well as the commands arrive from
200 * the control connection and we have no control over the order so we could be
201 * in a situation where too much data has been received on the data connection
202 * before the rotation command on the control connection arrives.
204 static int rotate_truncate_stream(struct relay_stream
*stream
)
207 off_t lseek_ret
, previous_stream_copy_origin
;
208 uint64_t copy_bytes_left
, misplaced_data_size
;
209 bool acquired_reference
;
210 struct stream_fd
*previous_stream_fd
= NULL
;
211 struct lttng_trace_chunk
*previous_chunk
= NULL
;
213 if (!LTTNG_OPTIONAL_GET(&stream
->ongoing_rotation
)->next_trace_chunk
) {
214 ERR("Protocol error encoutered in %s(): stream rotation "
215 "sequence number is before the current sequence number "
216 "and the next trace chunk is unset. Honoring this "
217 "rotation command would result in data loss",
223 ASSERT_LOCKED(stream
->lock
);
225 * Acquire a reference to the current trace chunk to ensure
226 * it is not reclaimed when `stream_rotate_data_file` is called.
227 * Failing to do so would violate the contract of the trace
228 * chunk API as an active file descriptor would outlive the
231 acquired_reference
= lttng_trace_chunk_get(stream
->trace_chunk
);
232 assert(acquired_reference
);
233 previous_chunk
= stream
->trace_chunk
;
236 * Steal the stream's reference to its stream_fd. A new
237 * stream_fd will be created when the rotation completes and
238 * the orinal stream_fd will be used to copy the "extra" data
241 assert(stream
->stream_fd
);
242 previous_stream_fd
= stream
->stream_fd
;
243 stream
->stream_fd
= NULL
;
245 assert(!stream
->is_metadata
);
246 assert(stream
->tracefile_size_current
>
247 stream
->pos_after_last_complete_data_index
);
248 misplaced_data_size
= stream
->tracefile_size_current
-
249 stream
->pos_after_last_complete_data_index
;
250 copy_bytes_left
= misplaced_data_size
;
251 previous_stream_copy_origin
= stream
->pos_after_last_complete_data_index
;
253 ret
= stream_rotate_data_file(stream
);
258 assert(stream
->stream_fd
);
260 * Seek the current tracefile to the position at which the rotation
261 * should have occurred.
263 lseek_ret
= lseek(previous_stream_fd
->fd
, previous_stream_copy_origin
,
266 PERROR("Failed to seek to offset %" PRIu64
267 " while copying extra data received before a stream rotation",
268 (uint64_t) previous_stream_copy_origin
);
273 /* Move data from the old file to the new file. */
274 while (copy_bytes_left
) {
276 char copy_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
277 const off_t copy_size_this_pass
= min_t(
278 off_t
, copy_bytes_left
, sizeof(copy_buffer
));
280 io_ret
= lttng_read(previous_stream_fd
->fd
, copy_buffer
,
281 copy_size_this_pass
);
282 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
284 PERROR("Failed to read %" PRIu64
285 " bytes from fd %i in %s(), returned %zi",
287 previous_stream_fd
->fd
,
288 __FUNCTION__
, io_ret
);
290 ERR("Failed to read %" PRIu64
291 " bytes from fd %i in %s(), returned %zi",
293 previous_stream_fd
->fd
,
294 __FUNCTION__
, io_ret
);
300 io_ret
= lttng_write(stream
->stream_fd
->fd
, copy_buffer
,
301 copy_size_this_pass
);
302 if (io_ret
< (ssize_t
) copy_size_this_pass
) {
304 PERROR("Failed to write %" PRIu64
305 " bytes from fd %i in %s(), returned %zi",
307 stream
->stream_fd
->fd
,
308 __FUNCTION__
, io_ret
);
310 ERR("Failed to write %" PRIu64
311 " bytes from fd %i in %s(), returned %zi",
313 stream
->stream_fd
->fd
,
314 __FUNCTION__
, io_ret
);
319 copy_bytes_left
-= copy_size_this_pass
;
322 /* Truncate the file to get rid of the excess data. */
323 ret
= ftruncate(previous_stream_fd
->fd
, previous_stream_copy_origin
);
325 PERROR("Failed to truncate current stream file to offset %" PRIu64
,
326 previous_stream_copy_origin
);
331 * Update the offset and FD of all the eventual indexes created by the
332 * data connection before the rotation command arrived.
334 ret
= relay_index_switch_all_files(stream
);
336 ERR("Failed to rotate index file");
340 stream
->tracefile_size_current
= misplaced_data_size
;
341 /* Index and data contents are back in sync. */
342 stream
->pos_after_last_complete_data_index
= 0;
345 lttng_trace_chunk_put(previous_chunk
);
346 stream_fd_put(previous_stream_fd
);
351 * Check if a stream's data file (as opposed to index) should be rotated
352 * (for session rotation).
353 * Must be called with the stream lock held.
355 * Return 0 on success, a negative value on error.
357 static int try_rotate_stream_data(struct relay_stream
*stream
)
361 if (caa_likely(!stream
->ongoing_rotation
.is_set
)) {
362 /* No rotation expected. */
366 if (stream
->ongoing_rotation
.value
.data_rotated
) {
367 /* Rotation of the data file has already occurred. */
371 if (stream
->prev_data_seq
== -1ULL ||
372 stream
->prev_data_seq
+ 1 < stream
->ongoing_rotation
.value
.seq_num
) {
374 * The next packet that will be written is not part of the next
377 DBG("Stream %" PRIu64
" not yet ready for rotation (rotate_at_seq_num = %" PRIu64
378 ", prev_data_seq = %" PRIu64
")",
379 stream
->stream_handle
,
380 stream
->ongoing_rotation
.value
.seq_num
,
381 stream
->prev_data_seq
);
383 } else if (stream
->prev_data_seq
> stream
->ongoing_rotation
.value
.seq_num
) {
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 stream
->index_file
= 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 if (!stream
->index_file
) {
462 * Check if a stream's index file should be rotated (for session rotation).
463 * Must be called with the stream lock held.
465 * Return 0 on success, a negative value on error.
467 static int try_rotate_stream_index(struct relay_stream
*stream
)
471 if (!stream
->ongoing_rotation
.is_set
) {
472 /* No rotation expected. */
476 if (stream
->ongoing_rotation
.value
.index_rotated
) {
477 /* Rotation of the index has already occurred. */
481 if (stream
->prev_index_seq
== -1ULL ||
482 stream
->prev_index_seq
+ 1 < stream
->ongoing_rotation
.value
.seq_num
) {
483 DBG("Stream %" PRIu64
" index not yet ready for rotation (rotate_at_seq_num = %" PRIu64
", prev_index_seq = %" PRIu64
")",
484 stream
->stream_handle
,
485 stream
->ongoing_rotation
.value
.seq_num
,
486 stream
->prev_index_seq
);
489 /* The next index belongs to the new trace chunk; rotate. */
490 assert(stream
->prev_index_seq
+ 1 ==
491 stream
->ongoing_rotation
.value
.seq_num
);
492 DBG("Rotating stream %" PRIu64
" index file",
493 stream
->stream_handle
);
494 ret
= create_index_file(stream
,
495 stream
->ongoing_rotation
.value
.next_trace_chunk
);
496 stream
->ongoing_rotation
.value
.index_rotated
= true;
498 if (stream
->ongoing_rotation
.value
.data_rotated
&&
499 stream
->ongoing_rotation
.value
.index_rotated
) {
500 /* Rotation completed; reset its state. */
501 DBG("Rotation completed for stream %" PRIu64
,
502 stream
->stream_handle
);
503 stream_complete_rotation(stream
);
511 static int stream_set_trace_chunk(struct relay_stream
*stream
,
512 struct lttng_trace_chunk
*chunk
)
515 enum lttng_trace_chunk_status status
;
516 bool acquired_reference
;
517 struct stream_fd
*new_stream_fd
= NULL
;
519 status
= lttng_trace_chunk_create_subdirectory(chunk
,
521 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
526 lttng_trace_chunk_put(stream
->trace_chunk
);
527 acquired_reference
= lttng_trace_chunk_get(chunk
);
528 assert(acquired_reference
);
529 stream
->trace_chunk
= chunk
;
531 if (stream
->stream_fd
) {
532 stream_fd_put(stream
->stream_fd
);
533 stream
->stream_fd
= NULL
;
535 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
,
536 false, &new_stream_fd
);
537 stream
->stream_fd
= new_stream_fd
;
543 * We keep ownership of path_name and channel_name.
545 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
546 uint64_t stream_handle
, char *path_name
,
547 char *channel_name
, uint64_t tracefile_size
,
548 uint64_t tracefile_count
)
551 struct relay_stream
*stream
= NULL
;
552 struct relay_session
*session
= trace
->session
;
553 bool acquired_reference
= false;
554 struct lttng_trace_chunk
*current_trace_chunk
;
556 stream
= zmalloc(sizeof(struct relay_stream
));
557 if (stream
== NULL
) {
558 PERROR("relay stream zmalloc");
562 stream
->stream_handle
= stream_handle
;
563 stream
->prev_data_seq
= -1ULL;
564 stream
->prev_index_seq
= -1ULL;
565 stream
->last_net_seq_num
= -1ULL;
566 stream
->ctf_stream_id
= -1ULL;
567 stream
->tracefile_size
= tracefile_size
;
568 stream
->tracefile_count
= tracefile_count
;
569 stream
->path_name
= path_name
;
570 stream
->channel_name
= channel_name
;
571 stream
->beacon_ts_end
= -1ULL;
572 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
573 pthread_mutex_init(&stream
->lock
, NULL
);
574 urcu_ref_init(&stream
->ref
);
575 ctf_trace_get(trace
);
576 stream
->trace
= trace
;
578 pthread_mutex_lock(&trace
->session
->lock
);
579 current_trace_chunk
= trace
->session
->current_trace_chunk
;
580 if (current_trace_chunk
) {
581 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
583 pthread_mutex_unlock(&trace
->session
->lock
);
584 if (!acquired_reference
) {
585 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
591 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
592 if (!stream
->indexes_ht
) {
593 ERR("Cannot created indexes_ht");
598 pthread_mutex_lock(&stream
->lock
);
599 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
600 pthread_mutex_unlock(&stream
->lock
);
602 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
603 trace
->session
->session_name
,
604 stream
->channel_name
);
608 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
614 stream
->is_metadata
= !strcmp(stream
->channel_name
,
615 DEFAULT_METADATA_NAME
);
616 stream
->in_recv_list
= true;
619 * Add the stream in the recv list of the session. Once the end stream
620 * message is received, all session streams are published.
622 pthread_mutex_lock(&session
->recv_list_lock
);
623 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
624 session
->stream_count
++;
625 pthread_mutex_unlock(&session
->recv_list_lock
);
628 * Both in the ctf_trace object and the global stream ht since the data
629 * side of the relayd does not have the concept of session.
631 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
632 stream
->in_stream_ht
= true;
634 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
635 stream
->stream_handle
);
640 if (stream
->stream_fd
) {
641 stream_fd_put(stream
->stream_fd
);
642 stream
->stream_fd
= NULL
;
647 if (acquired_reference
) {
648 lttng_trace_chunk_put(current_trace_chunk
);
654 * path_name and channel_name need to be freed explicitly here
655 * because we cannot rely on stream_put().
663 * Called with the session lock held.
665 void stream_publish(struct relay_stream
*stream
)
667 struct relay_session
*session
;
669 pthread_mutex_lock(&stream
->lock
);
670 if (stream
->published
) {
674 session
= stream
->trace
->session
;
676 pthread_mutex_lock(&session
->recv_list_lock
);
677 if (stream
->in_recv_list
) {
678 cds_list_del_rcu(&stream
->recv_node
);
679 stream
->in_recv_list
= false;
681 pthread_mutex_unlock(&session
->recv_list_lock
);
683 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
684 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
685 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
687 stream
->published
= true;
689 pthread_mutex_unlock(&stream
->lock
);
693 * Stream must be protected by holding the stream lock or by virtue of being
694 * called from stream_destroy.
696 static void stream_unpublish(struct relay_stream
*stream
)
698 if (stream
->in_stream_ht
) {
699 struct lttng_ht_iter iter
;
702 iter
.iter
.node
= &stream
->node
.node
;
703 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
705 stream
->in_stream_ht
= false;
707 if (stream
->published
) {
708 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
709 cds_list_del_rcu(&stream
->stream_node
);
710 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
711 stream
->published
= false;
715 static void stream_destroy(struct relay_stream
*stream
)
717 if (stream
->indexes_ht
) {
719 * Calling lttng_ht_destroy in call_rcu worker thread so
720 * we don't hold the RCU read-side lock while calling
723 lttng_ht_destroy(stream
->indexes_ht
);
726 tracefile_array_destroy(stream
->tfa
);
728 free(stream
->path_name
);
729 free(stream
->channel_name
);
733 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
735 struct relay_stream
*stream
=
736 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
738 stream_destroy(stream
);
742 * No need to take stream->lock since this is only called on the final
743 * stream_put which ensures that a single thread may act on the stream.
745 static void stream_release(struct urcu_ref
*ref
)
747 struct relay_stream
*stream
=
748 caa_container_of(ref
, struct relay_stream
, ref
);
749 struct relay_session
*session
;
751 session
= stream
->trace
->session
;
753 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
755 pthread_mutex_lock(&session
->recv_list_lock
);
756 session
->stream_count
--;
757 if (stream
->in_recv_list
) {
758 cds_list_del_rcu(&stream
->recv_node
);
759 stream
->in_recv_list
= false;
761 pthread_mutex_unlock(&session
->recv_list_lock
);
763 stream_unpublish(stream
);
765 if (stream
->stream_fd
) {
766 stream_fd_put(stream
->stream_fd
);
767 stream
->stream_fd
= NULL
;
769 if (stream
->index_file
) {
770 lttng_index_file_put(stream
->index_file
);
771 stream
->index_file
= NULL
;
774 ctf_trace_put(stream
->trace
);
775 stream
->trace
= NULL
;
777 stream_complete_rotation(stream
);
778 lttng_trace_chunk_put(stream
->trace_chunk
);
779 stream
->trace_chunk
= NULL
;
781 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
784 void stream_put(struct relay_stream
*stream
)
787 assert(stream
->ref
.refcount
!= 0);
789 * Wait until we have processed all the stream packets before
790 * actually putting our last stream reference.
792 urcu_ref_put(&stream
->ref
, stream_release
);
796 int stream_set_pending_rotation(struct relay_stream
*stream
,
797 struct lttng_trace_chunk
*next_trace_chunk
,
798 uint64_t rotation_sequence_number
)
801 const struct relay_stream_rotation rotation
= {
802 .seq_num
= rotation_sequence_number
,
803 .next_trace_chunk
= next_trace_chunk
,
806 if (stream
->ongoing_rotation
.is_set
) {
807 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
812 if (next_trace_chunk
) {
813 const bool reference_acquired
=
814 lttng_trace_chunk_get(next_trace_chunk
);
816 assert(reference_acquired
);
818 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
820 DBG("Setting pending rotation: stream_id = %" PRIu64
", rotation_seq_num = %" PRIu64
,
821 stream
->stream_handle
, rotation_sequence_number
);
822 if (stream
->is_metadata
) {
824 * A metadata stream has no index; consider it already rotated.
826 stream
->ongoing_rotation
.value
.index_rotated
= true;
827 ret
= stream_rotate_data_file(stream
);
829 ret
= try_rotate_stream_data(stream
);
834 ret
= try_rotate_stream_index(stream
);
843 void try_stream_close(struct relay_stream
*stream
)
845 bool session_aborted
;
846 struct relay_session
*session
= stream
->trace
->session
;
848 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
850 pthread_mutex_lock(&session
->lock
);
851 session_aborted
= session
->aborted
;
852 pthread_mutex_unlock(&session
->lock
);
854 pthread_mutex_lock(&stream
->lock
);
856 * Can be called concurently by connection close and reception of last
859 if (stream
->closed
) {
860 pthread_mutex_unlock(&stream
->lock
);
861 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
865 stream
->close_requested
= true;
867 if (stream
->last_net_seq_num
== -1ULL) {
869 * Handle connection close without explicit stream close
872 * We can be clever about indexes partially received in
873 * cases where we received the data socket part, but not
874 * the control socket part: since we're currently closing
875 * the stream on behalf of the control socket, we *know*
876 * there won't be any more control information for this
877 * socket. Therefore, we can destroy all indexes for
878 * which we have received only the file descriptor (from
879 * data socket). This takes care of consumerd crashes
880 * between sending the data and control information for
881 * a packet. Since those are sent in that order, we take
882 * care of consumerd crashes.
884 DBG("relay_index_close_partial_fd");
885 relay_index_close_partial_fd(stream
);
887 * Use the highest net_seq_num we currently have pending
888 * As end of stream indicator. Leave last_net_seq_num
889 * at -1ULL if we cannot find any index.
891 stream
->last_net_seq_num
= relay_index_find_last(stream
);
892 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
893 /* Fall-through into the next check. */
896 if (stream
->last_net_seq_num
!= -1ULL &&
897 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
898 && !session_aborted
) {
900 * Don't close since we still have data pending. This
901 * handles cases where an explicit close command has
902 * been received for this stream, and cases where the
903 * connection has been closed, and we are awaiting for
904 * index information from the data socket. It is
905 * therefore expected that all the index fd information
906 * we need has already been received on the control
907 * socket. Matching index information from data socket
908 * should be Expected Soon(TM).
910 * TODO: We should implement a timer to garbage collect
911 * streams after a timeout to be resilient against a
912 * consumerd implementation that would not match this
915 pthread_mutex_unlock(&stream
->lock
);
916 DBG("closing stream %" PRIu64
" aborted since it still has data pending", stream
->stream_handle
);
920 * We received all the indexes we can expect.
922 stream_unpublish(stream
);
923 stream
->closed
= true;
924 /* Relay indexes are only used by the "consumer/sessiond" end. */
925 relay_index_close_all(stream
);
928 * If we are closed by an application exiting (per-pid buffers),
929 * we need to put our reference on the stream trace chunk right
930 * away, because otherwise still holding the reference on the
931 * trace chunk could allow a viewer stream (which holds a reference
932 * to the stream) to postpone destroy waiting for the chunk to cease
933 * to exist endlessly until the viewer is detached.
936 /* Put stream fd before put chunk. */
937 if (stream
->stream_fd
) {
938 stream_fd_put(stream
->stream_fd
);
939 stream
->stream_fd
= NULL
;
941 if (stream
->index_file
) {
942 lttng_index_file_put(stream
->index_file
);
943 stream
->index_file
= NULL
;
945 lttng_trace_chunk_put(stream
->trace_chunk
);
946 stream
->trace_chunk
= NULL
;
947 pthread_mutex_unlock(&stream
->lock
);
948 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
952 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
,
957 ASSERT_LOCKED(stream
->lock
);
958 if (caa_likely(stream
->tracefile_size
== 0)) {
959 /* No size limit set; nothing to check. */
964 * Check if writing the new packet would exceed the maximal file size.
966 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) >
967 stream
->tracefile_size
)) {
968 const uint64_t new_file_index
=
969 (stream
->tracefile_current_index
+ 1) %
970 stream
->tracefile_count
;
972 if (new_file_index
< stream
->tracefile_current_index
) {
973 stream
->tracefile_wrapped_around
= true;
975 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
976 ", current_file_size = %" PRIu64
977 ", packet_size = %zu, current_file_index = %" PRIu64
978 " new_file_index = %" PRIu64
,
979 stream
->stream_handle
,
980 stream
->tracefile_size_current
, packet_size
,
981 stream
->tracefile_current_index
, new_file_index
);
982 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_WRITE
);
983 stream
->tracefile_current_index
= new_file_index
;
985 if (stream
->stream_fd
) {
986 stream_fd_put(stream
->stream_fd
);
987 stream
->stream_fd
= NULL
;
989 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
990 stream
->trace_chunk
, false, &stream
->stream_fd
);
992 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
993 stream
->stream_handle
);
998 * Reset current size because we just performed a stream
1001 stream
->tracefile_size_current
= 0;
1002 *file_rotated
= true;
1004 *file_rotated
= false;
1010 /* Note that the packet is not necessarily complete. */
1011 int stream_write(struct relay_stream
*stream
,
1012 const struct lttng_buffer_view
*packet
, size_t padding_len
)
1016 size_t padding_to_write
= padding_len
;
1017 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
1019 ASSERT_LOCKED(stream
->lock
);
1020 memset(padding_buffer
, 0,
1021 min(sizeof(padding_buffer
), padding_to_write
));
1024 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1025 packet
->data
, packet
->size
);
1026 if (write_ret
!= packet
->size
) {
1027 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
1028 stream
->is_metadata
? "metadata " : "",
1029 stream
->stream_handle
);
1035 while (padding_to_write
> 0) {
1036 const size_t padding_to_write_this_pass
=
1037 min(padding_to_write
, sizeof(padding_buffer
));
1039 write_ret
= lttng_write(stream
->stream_fd
->fd
,
1040 padding_buffer
, padding_to_write_this_pass
);
1041 if (write_ret
!= padding_to_write_this_pass
) {
1042 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
1043 stream
->is_metadata
? "metadata " : "",
1044 stream
->stream_handle
);
1048 padding_to_write
-= padding_to_write_this_pass
;
1051 if (stream
->is_metadata
) {
1052 stream
->metadata_received
+= packet
? packet
->size
: 0;
1053 stream
->metadata_received
+= padding_len
;
1056 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1057 stream
->is_metadata
? "metadata " : "",
1058 stream
->stream_handle
,
1059 packet
? packet
->size
: (size_t) 0, padding_len
);
1065 * Update index after receiving a packet for a data stream.
1067 * Called with the stream lock held.
1069 * Return 0 on success else a negative value.
1071 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
1072 bool rotate_index
, bool *flushed
, uint64_t total_size
)
1075 uint64_t data_offset
;
1076 struct relay_index
*index
;
1078 assert(stream
->trace_chunk
);
1079 ASSERT_LOCKED(stream
->lock
);
1080 /* Get data offset because we are about to update the index. */
1081 data_offset
= htobe64(stream
->tracefile_size_current
);
1083 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1084 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
1087 * Lookup for an existing index for that stream id/sequence
1088 * number. If it exists, the control thread has already received the
1089 * data for it, thus we need to write it to disk.
1091 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1097 if (rotate_index
|| !stream
->index_file
) {
1098 ret
= create_index_file(stream
, stream
->trace_chunk
);
1100 ERR("Failed to create index file for stream %" PRIu64
,
1101 stream
->stream_handle
);
1102 /* Put self-ref for this index due to error. */
1103 relay_index_put(index
);
1109 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1111 /* Put self-ref for this index due to error. */
1112 relay_index_put(index
);
1117 ret
= relay_index_try_flush(index
);
1119 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1120 tracefile_array_commit_seq(stream
->tfa
);
1121 stream
->index_received_seqcount
++;
1123 } else if (ret
> 0) {
1124 index
->total_size
= total_size
;
1131 * relay_index_try_flush is responsible for the self-reference
1132 * put of the index object on error.
1134 ERR("relay_index_try_flush error %d", ret
);
1141 int stream_complete_packet(struct relay_stream
*stream
, size_t packet_total_size
,
1142 uint64_t sequence_number
, bool index_flushed
)
1146 ASSERT_LOCKED(stream
->lock
);
1148 stream
->tracefile_size_current
+= packet_total_size
;
1149 if (index_flushed
) {
1150 stream
->pos_after_last_complete_data_index
=
1151 stream
->tracefile_size_current
;
1152 stream
->prev_index_seq
= sequence_number
;
1153 ret
= try_rotate_stream_index(stream
);
1159 stream
->prev_data_seq
= sequence_number
;
1160 ret
= try_rotate_stream_data(stream
);
1166 int stream_add_index(struct relay_stream
*stream
,
1167 const struct lttcomm_relayd_index
*index_info
)
1170 struct relay_index
*index
;
1172 ASSERT_LOCKED(stream
->lock
);
1174 /* Live beacon handling */
1175 if (index_info
->packet_size
== 0) {
1176 DBG("Received live beacon for stream %" PRIu64
,
1177 stream
->stream_handle
);
1180 * Only flag a stream inactive when it has already
1181 * received data and no indexes are in flight.
1183 if (stream
->index_received_seqcount
> 0
1184 && stream
->indexes_in_flight
== 0) {
1185 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1190 stream
->beacon_ts_end
= -1ULL;
1193 if (stream
->ctf_stream_id
== -1ULL) {
1194 stream
->ctf_stream_id
= index_info
->stream_id
;
1197 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1200 ERR("Failed to get or create index %" PRIu64
,
1201 index_info
->net_seq_num
);
1204 if (relay_index_set_control_data(index
, index_info
,
1205 stream
->trace
->session
->minor
)) {
1206 ERR("set_index_control_data error");
1207 relay_index_put(index
);
1211 ret
= relay_index_try_flush(index
);
1213 tracefile_array_file_rotate(stream
->tfa
, TRACEFILE_ROTATE_READ
);
1214 tracefile_array_commit_seq(stream
->tfa
);
1215 stream
->index_received_seqcount
++;
1216 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1217 stream
->prev_index_seq
= index_info
->net_seq_num
;
1219 ret
= try_rotate_stream_index(stream
);
1223 } else if (ret
> 0) {
1230 * relay_index_try_flush is responsible for the self-reference
1231 * put of the index object on error.
1233 ERR("relay_index_try_flush error %d", ret
);
1240 static void print_stream_indexes(struct relay_stream
*stream
)
1242 struct lttng_ht_iter iter
;
1243 struct relay_index
*index
;
1246 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
1248 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1249 " stream %" PRIu64
" trace %" PRIu64
1250 " session %" PRIu64
,
1253 stream
->ref
.refcount
,
1254 index
->stream
->stream_handle
,
1255 index
->stream
->trace
->id
,
1256 index
->stream
->trace
->session
->id
);
1261 int stream_reset_file(struct relay_stream
*stream
)
1263 ASSERT_LOCKED(stream
->lock
);
1265 if (stream
->stream_fd
) {
1266 stream_fd_put(stream
->stream_fd
);
1267 stream
->stream_fd
= NULL
;
1270 stream
->tracefile_size_current
= 0;
1271 stream
->prev_data_seq
= 0;
1272 stream
->prev_index_seq
= 0;
1273 /* Note that this does not reset the tracefile array. */
1274 stream
->tracefile_current_index
= 0;
1275 stream
->pos_after_last_complete_data_index
= 0;
1277 return stream_create_data_output_file_from_trace_chunk(stream
,
1278 stream
->trace_chunk
, true, &stream
->stream_fd
);
1281 void print_relay_streams(void)
1283 struct lttng_ht_iter iter
;
1284 struct relay_stream
*stream
;
1286 if (!relay_streams_ht
) {
1291 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1293 if (!stream_get(stream
)) {
1296 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1297 " session %" PRIu64
,
1299 stream
->ref
.refcount
,
1300 stream
->stream_handle
,
1302 stream
->trace
->session
->id
);
1303 print_stream_indexes(stream
);