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
)) {};
81 * If too much data has been written in a tracefile before we received the
82 * rotation command, we have to move the excess data to the new tracefile and
83 * perform the rotation. This can happen because the control and data
84 * connections are separate, the indexes as well as the commands arrive from
85 * the control connection and we have no control over the order so we could be
86 * in a situation where too much data has been received on the data connection
87 * before the rotation command on the control connection arrives.
89 static int rotate_truncate_stream(struct relay_stream
*stream
)
93 uint64_t diff
, pos
= 0;
94 char buf
[FILE_IO_STACK_BUFFER_SIZE
];
96 assert(!stream
->is_metadata
);
98 assert(stream
->tracefile_size_current
>
99 stream
->pos_after_last_complete_data_index
);
100 diff
= stream
->tracefile_size_current
-
101 stream
->pos_after_last_complete_data_index
;
103 /* Create the new tracefile. */
104 new_fd
= utils_create_stream_file(stream
->path_name
,
105 stream
->channel_name
,
106 stream
->tracefile_size
, stream
->tracefile_count
,
107 /* uid */ -1, /* gid */ -1, /* suffix */ NULL
);
109 ERR("Failed to create new stream file at path %s for channel %s",
110 stream
->path_name
, stream
->channel_name
);
116 * Rewind the current tracefile to the position at which the rotation
117 * should have occurred.
119 lseek_ret
= lseek(stream
->stream_fd
->fd
,
120 stream
->pos_after_last_complete_data_index
, SEEK_SET
);
122 PERROR("seek truncate stream");
127 /* Move data from the old file to the new file. */
129 uint64_t count
, bytes_left
;
132 bytes_left
= diff
- pos
;
133 count
= bytes_left
> sizeof(buf
) ? sizeof(buf
) : bytes_left
;
134 assert(count
<= SIZE_MAX
);
136 io_ret
= lttng_read(stream
->stream_fd
->fd
, buf
, count
);
137 if (io_ret
< (ssize_t
) count
) {
138 char error_string
[256];
140 snprintf(error_string
, sizeof(error_string
),
141 "Failed to read %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
142 count
, stream
->stream_fd
->fd
, io_ret
);
144 PERROR("%s", error_string
);
146 ERR("%s", error_string
);
152 io_ret
= lttng_write(new_fd
, buf
, count
);
153 if (io_ret
< (ssize_t
) count
) {
154 char error_string
[256];
156 snprintf(error_string
, sizeof(error_string
),
157 "Failed to write %" PRIu64
" bytes from fd %i in rotate_truncate_stream(), returned %zi",
158 count
, new_fd
, io_ret
);
160 PERROR("%s", error_string
);
162 ERR("%s", error_string
);
171 /* Truncate the file to get rid of the excess data. */
172 ret
= ftruncate(stream
->stream_fd
->fd
,
173 stream
->pos_after_last_complete_data_index
);
179 ret
= close(stream
->stream_fd
->fd
);
181 PERROR("Closing tracefile");
186 * Update the offset and FD of all the eventual indexes created by the
187 * data connection before the rotation command arrived.
189 ret
= relay_index_switch_all_files(stream
);
191 ERR("Failed to rotate index file");
195 stream
->stream_fd
->fd
= new_fd
;
196 stream
->tracefile_size_current
= diff
;
197 stream
->pos_after_last_complete_data_index
= 0;
198 stream_complete_rotation(stream
);
206 static int stream_create_data_output_file_from_trace_chunk(
207 struct relay_stream
*stream
,
208 struct lttng_trace_chunk
*trace_chunk
,
210 struct stream_fd
**out_stream_fd
)
213 char stream_path
[LTTNG_PATH_MAX
];
214 enum lttng_trace_chunk_status status
;
215 const int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
216 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
218 ASSERT_LOCKED(stream
->lock
);
219 assert(stream
->trace_chunk
);
221 ret
= utils_stream_file_path(stream
->path_name
, stream
->channel_name
,
222 stream
->tracefile_size
, stream
->tracefile_current_index
,
223 NULL
, stream_path
, sizeof(stream_path
));
228 if (stream
->tracefile_wrapped_around
|| force_unlink
) {
230 * The on-disk ring-buffer has wrapped around.
231 * Newly created stream files will replace existing files. Since
232 * live clients may be consuming existing files, the file about
233 * to be replaced is unlinked in order to not overwrite its
236 status
= lttng_trace_chunk_unlink_file(trace_chunk
,
238 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
239 PERROR("Failed to unlink stream file \"%s\" during trace file rotation",
242 * Don't abort if the file doesn't exist, it is
243 * unexpected, but should not be a fatal error.
245 if (errno
!= ENOENT
) {
252 status
= lttng_trace_chunk_open_file(
253 trace_chunk
, stream_path
, flags
, mode
, &fd
);
254 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
255 ERR("Failed to open stream file \"%s\"", stream
->channel_name
);
260 *out_stream_fd
= stream_fd_create(fd
);
261 if (!*out_stream_fd
) {
263 PERROR("Error closing stream file descriptor %d", ret
);
272 static int stream_rotate_data_file(struct relay_stream
*stream
)
276 DBG("Rotating stream %" PRIu64
" data file",
277 stream
->stream_handle
);
279 if (stream
->stream_fd
) {
280 stream_fd_put(stream
->stream_fd
);
281 stream
->stream_fd
= NULL
;
284 stream
->tracefile_wrapped_around
= false;
285 stream
->tracefile_current_index
= 0;
287 if (stream
->ongoing_rotation
.value
.next_trace_chunk
) {
288 struct stream_fd
*new_stream_fd
= NULL
;
289 enum lttng_trace_chunk_status chunk_status
;
291 chunk_status
= lttng_trace_chunk_create_subdirectory(
292 stream
->ongoing_rotation
.value
.next_trace_chunk
,
294 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
299 /* Rotate the data file. */
300 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
301 stream
->ongoing_rotation
.value
.next_trace_chunk
,
302 false, &new_stream_fd
);
303 stream
->stream_fd
= new_stream_fd
;
305 ERR("Failed to rotate stream data file");
309 stream
->tracefile_size_current
= 0;
310 stream
->pos_after_last_complete_data_index
= 0;
311 stream
->ongoing_rotation
.value
.data_rotated
= true;
313 if (stream
->ongoing_rotation
.value
.index_rotated
) {
314 /* Rotation completed; reset its state. */
315 stream_complete_rotation(stream
);
322 * Check if a stream's data file (as opposed to index) should be rotated
323 * (for session rotation).
324 * Must be called with the stream lock held.
326 * Return 0 on success, a negative value on error.
328 static int try_rotate_stream_data(struct relay_stream
*stream
)
332 if (caa_likely(!stream
->ongoing_rotation
.is_set
)) {
333 /* No rotation expected. */
337 if (stream
->ongoing_rotation
.value
.data_rotated
) {
338 /* Rotation of the data file has already occurred. */
342 if (stream
->prev_data_seq
== -1ULL ||
343 stream
->prev_data_seq
+ 1 < stream
->ongoing_rotation
.value
.seq_num
) {
345 * The next packet that will be written is not part of the next
348 DBG("Stream %" PRIu64
" not yet ready for rotation (rotate_at_seq_num = %" PRIu64
349 ", prev_data_seq = %" PRIu64
")",
350 stream
->stream_handle
,
351 stream
->ongoing_rotation
.value
.seq_num
,
352 stream
->prev_data_seq
);
354 } else if (stream
->prev_data_seq
> stream
->ongoing_rotation
.value
.seq_num
) {
356 * prev_data_seq is checked here since indexes and rotation
357 * commands are serialized with respect to each other.
359 DBG("Rotation after too much data has been written in tracefile "
360 "for stream %" PRIu64
", need to truncate before "
361 "rotating", stream
->stream_handle
);
362 ret
= rotate_truncate_stream(stream
);
364 ERR("Failed to truncate stream");
368 ret
= stream_rotate_data_file(stream
);
376 * Close the current index file if it is open, and create a new one.
378 * Return 0 on success, -1 on error.
380 static int create_index_file(struct relay_stream
*stream
,
381 struct lttng_trace_chunk
*chunk
)
384 uint32_t major
, minor
;
385 char *index_subpath
= NULL
;
386 enum lttng_trace_chunk_status status
;
388 ASSERT_LOCKED(stream
->lock
);
390 /* Put ref on previous index_file. */
391 if (stream
->index_file
) {
392 lttng_index_file_put(stream
->index_file
);
393 stream
->index_file
= NULL
;
395 major
= stream
->trace
->session
->major
;
396 minor
= stream
->trace
->session
->minor
;
402 ret
= asprintf(&index_subpath
, "%s/%s", stream
->path_name
,
408 status
= lttng_trace_chunk_create_subdirectory(chunk
,
411 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
415 stream
->index_file
= lttng_index_file_create_from_trace_chunk(
416 chunk
, stream
->path_name
,
417 stream
->channel_name
, stream
->tracefile_size
,
418 stream
->tracefile_current_index
,
419 lttng_to_index_major(major
, minor
),
420 lttng_to_index_minor(major
, minor
), true);
421 if (!stream
->index_file
) {
433 * Check if a stream's index file should be rotated (for session rotation).
434 * Must be called with the stream lock held.
436 * Return 0 on success, a negative value on error.
438 static int try_rotate_stream_index(struct relay_stream
*stream
)
442 if (!stream
->ongoing_rotation
.is_set
) {
443 /* No rotation expected. */
447 if (stream
->ongoing_rotation
.value
.index_rotated
) {
448 /* Rotation of the index has already occurred. */
452 if (stream
->prev_index_seq
== -1ULL ||
453 stream
->prev_index_seq
+ 1 < stream
->ongoing_rotation
.value
.seq_num
) {
454 DBG("Stream %" PRIu64
" index not yet ready for rotation (rotate_at_seq_num = %" PRIu64
", prev_index_seq = %" PRIu64
")",
455 stream
->stream_handle
,
456 stream
->ongoing_rotation
.value
.seq_num
,
457 stream
->prev_index_seq
);
460 /* The next index belongs to the new trace chunk; rotate. */
461 assert(stream
->prev_index_seq
+ 1 ==
462 stream
->ongoing_rotation
.value
.seq_num
);
463 DBG("Rotating stream %" PRIu64
" index file",
464 stream
->stream_handle
);
465 ret
= create_index_file(stream
,
466 stream
->ongoing_rotation
.value
.next_trace_chunk
);
467 stream
->ongoing_rotation
.value
.index_rotated
= true;
469 if (stream
->ongoing_rotation
.value
.data_rotated
&&
470 stream
->ongoing_rotation
.value
.index_rotated
) {
471 /* Rotation completed; reset its state. */
472 DBG("Rotation completed for stream %" PRIu64
,
473 stream
->stream_handle
);
474 stream_complete_rotation(stream
);
482 static int stream_set_trace_chunk(struct relay_stream
*stream
,
483 struct lttng_trace_chunk
*chunk
)
486 enum lttng_trace_chunk_status status
;
487 bool acquired_reference
;
488 struct stream_fd
*new_stream_fd
= NULL
;
490 status
= lttng_trace_chunk_create_subdirectory(chunk
,
492 if (status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
497 lttng_trace_chunk_put(stream
->trace_chunk
);
498 acquired_reference
= lttng_trace_chunk_get(chunk
);
499 assert(acquired_reference
);
500 stream
->trace_chunk
= chunk
;
502 if (stream
->stream_fd
) {
503 stream_fd_put(stream
->stream_fd
);
504 stream
->stream_fd
= NULL
;
506 ret
= stream_create_data_output_file_from_trace_chunk(stream
, chunk
,
507 false, &new_stream_fd
);
508 stream
->stream_fd
= new_stream_fd
;
514 * We keep ownership of path_name and channel_name.
516 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
517 uint64_t stream_handle
, char *path_name
,
518 char *channel_name
, uint64_t tracefile_size
,
519 uint64_t tracefile_count
)
522 struct relay_stream
*stream
= NULL
;
523 struct relay_session
*session
= trace
->session
;
524 bool acquired_reference
= false;
525 struct lttng_trace_chunk
*current_trace_chunk
;
527 stream
= zmalloc(sizeof(struct relay_stream
));
528 if (stream
== NULL
) {
529 PERROR("relay stream zmalloc");
533 stream
->stream_handle
= stream_handle
;
534 stream
->prev_data_seq
= -1ULL;
535 stream
->prev_index_seq
= -1ULL;
536 stream
->last_net_seq_num
= -1ULL;
537 stream
->ctf_stream_id
= -1ULL;
538 stream
->tracefile_size
= tracefile_size
;
539 stream
->tracefile_count
= tracefile_count
;
540 stream
->path_name
= path_name
;
541 stream
->channel_name
= channel_name
;
542 stream
->beacon_ts_end
= -1ULL;
543 lttng_ht_node_init_u64(&stream
->node
, stream
->stream_handle
);
544 pthread_mutex_init(&stream
->lock
, NULL
);
545 urcu_ref_init(&stream
->ref
);
546 ctf_trace_get(trace
);
547 stream
->trace
= trace
;
549 pthread_mutex_lock(&trace
->session
->lock
);
550 current_trace_chunk
= trace
->session
->current_trace_chunk
;
551 if (current_trace_chunk
) {
552 acquired_reference
= lttng_trace_chunk_get(current_trace_chunk
);
554 pthread_mutex_unlock(&trace
->session
->lock
);
555 if (!acquired_reference
) {
556 ERR("Cannot create stream for channel \"%s\" as a reference to the session's current trace chunk could not be acquired",
562 stream
->indexes_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
563 if (!stream
->indexes_ht
) {
564 ERR("Cannot created indexes_ht");
569 pthread_mutex_lock(&stream
->lock
);
570 ret
= stream_set_trace_chunk(stream
, current_trace_chunk
);
571 pthread_mutex_unlock(&stream
->lock
);
573 ERR("Failed to set the current trace chunk of session \"%s\" on newly created stream of channel \"%s\"",
574 trace
->session
->session_name
,
575 stream
->channel_name
);
579 stream
->tfa
= tracefile_array_create(stream
->tracefile_count
);
585 stream
->is_metadata
= !strcmp(stream
->channel_name
,
586 DEFAULT_METADATA_NAME
);
587 stream
->in_recv_list
= true;
590 * Add the stream in the recv list of the session. Once the end stream
591 * message is received, all session streams are published.
593 pthread_mutex_lock(&session
->recv_list_lock
);
594 cds_list_add_rcu(&stream
->recv_node
, &session
->recv_list
);
595 session
->stream_count
++;
596 pthread_mutex_unlock(&session
->recv_list_lock
);
599 * Both in the ctf_trace object and the global stream ht since the data
600 * side of the relayd does not have the concept of session.
602 lttng_ht_add_unique_u64(relay_streams_ht
, &stream
->node
);
603 stream
->in_stream_ht
= true;
605 DBG("Relay new stream added %s with ID %" PRIu64
, stream
->channel_name
,
606 stream
->stream_handle
);
611 if (stream
->stream_fd
) {
612 stream_fd_put(stream
->stream_fd
);
613 stream
->stream_fd
= NULL
;
618 lttng_trace_chunk_put(current_trace_chunk
);
623 * path_name and channel_name need to be freed explicitly here
624 * because we cannot rely on stream_put().
632 * Called with the session lock held.
634 void stream_publish(struct relay_stream
*stream
)
636 struct relay_session
*session
;
638 pthread_mutex_lock(&stream
->lock
);
639 if (stream
->published
) {
643 session
= stream
->trace
->session
;
645 pthread_mutex_lock(&session
->recv_list_lock
);
646 if (stream
->in_recv_list
) {
647 cds_list_del_rcu(&stream
->recv_node
);
648 stream
->in_recv_list
= false;
650 pthread_mutex_unlock(&session
->recv_list_lock
);
652 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
653 cds_list_add_rcu(&stream
->stream_node
, &stream
->trace
->stream_list
);
654 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
656 stream
->published
= true;
658 pthread_mutex_unlock(&stream
->lock
);
662 * Stream must be protected by holding the stream lock or by virtue of being
663 * called from stream_destroy.
665 static void stream_unpublish(struct relay_stream
*stream
)
667 if (stream
->in_stream_ht
) {
668 struct lttng_ht_iter iter
;
671 iter
.iter
.node
= &stream
->node
.node
;
672 ret
= lttng_ht_del(relay_streams_ht
, &iter
);
674 stream
->in_stream_ht
= false;
676 if (stream
->published
) {
677 pthread_mutex_lock(&stream
->trace
->stream_list_lock
);
678 cds_list_del_rcu(&stream
->stream_node
);
679 pthread_mutex_unlock(&stream
->trace
->stream_list_lock
);
680 stream
->published
= false;
684 static void stream_destroy(struct relay_stream
*stream
)
686 if (stream
->indexes_ht
) {
688 * Calling lttng_ht_destroy in call_rcu worker thread so
689 * we don't hold the RCU read-side lock while calling
692 lttng_ht_destroy(stream
->indexes_ht
);
695 tracefile_array_destroy(stream
->tfa
);
697 free(stream
->path_name
);
698 free(stream
->channel_name
);
702 static void stream_destroy_rcu(struct rcu_head
*rcu_head
)
704 struct relay_stream
*stream
=
705 caa_container_of(rcu_head
, struct relay_stream
, rcu_node
);
707 stream_destroy(stream
);
711 * No need to take stream->lock since this is only called on the final
712 * stream_put which ensures that a single thread may act on the stream.
714 static void stream_release(struct urcu_ref
*ref
)
716 struct relay_stream
*stream
=
717 caa_container_of(ref
, struct relay_stream
, ref
);
718 struct relay_session
*session
;
720 session
= stream
->trace
->session
;
722 DBG("Releasing stream id %" PRIu64
, stream
->stream_handle
);
724 pthread_mutex_lock(&session
->recv_list_lock
);
725 session
->stream_count
--;
726 if (stream
->in_recv_list
) {
727 cds_list_del_rcu(&stream
->recv_node
);
728 stream
->in_recv_list
= false;
730 pthread_mutex_unlock(&session
->recv_list_lock
);
732 stream_unpublish(stream
);
734 if (stream
->stream_fd
) {
735 stream_fd_put(stream
->stream_fd
);
736 stream
->stream_fd
= NULL
;
738 if (stream
->index_file
) {
739 lttng_index_file_put(stream
->index_file
);
740 stream
->index_file
= NULL
;
743 ctf_trace_put(stream
->trace
);
744 stream
->trace
= NULL
;
746 stream_complete_rotation(stream
);
747 lttng_trace_chunk_put(stream
->trace_chunk
);
748 stream
->trace_chunk
= NULL
;
750 call_rcu(&stream
->rcu_node
, stream_destroy_rcu
);
753 void stream_put(struct relay_stream
*stream
)
756 assert(stream
->ref
.refcount
!= 0);
758 * Wait until we have processed all the stream packets before
759 * actually putting our last stream reference.
761 urcu_ref_put(&stream
->ref
, stream_release
);
765 int stream_set_pending_rotation(struct relay_stream
*stream
,
766 struct lttng_trace_chunk
*next_trace_chunk
,
767 uint64_t rotation_sequence_number
)
770 const struct relay_stream_rotation rotation
= {
771 .seq_num
= rotation_sequence_number
,
772 .next_trace_chunk
= next_trace_chunk
,
775 if (stream
->ongoing_rotation
.is_set
) {
776 ERR("Attempted to set a pending rotation on a stream already being rotated (protocol error)");
781 if (next_trace_chunk
) {
782 const bool reference_acquired
=
783 lttng_trace_chunk_get(next_trace_chunk
);
785 assert(reference_acquired
);
787 LTTNG_OPTIONAL_SET(&stream
->ongoing_rotation
, rotation
);
789 DBG("Setting pending rotation: stream_id = %" PRIu64
", rotation_seq_num = %" PRIu64
,
790 stream
->stream_handle
, rotation_sequence_number
);
791 if (stream
->is_metadata
) {
793 * A metadata stream has no index; consider it already rotated.
795 stream
->ongoing_rotation
.value
.index_rotated
= true;
796 ret
= stream_rotate_data_file(stream
);
798 ret
= try_rotate_stream_data(stream
);
803 ret
= try_rotate_stream_index(stream
);
812 void try_stream_close(struct relay_stream
*stream
)
814 bool session_aborted
;
815 struct relay_session
*session
= stream
->trace
->session
;
817 DBG("Trying to close stream %" PRIu64
, stream
->stream_handle
);
819 pthread_mutex_lock(&session
->lock
);
820 session_aborted
= session
->aborted
;
821 pthread_mutex_unlock(&session
->lock
);
823 pthread_mutex_lock(&stream
->lock
);
825 * Can be called concurently by connection close and reception of last
828 if (stream
->closed
) {
829 pthread_mutex_unlock(&stream
->lock
);
830 DBG("closing stream %" PRIu64
" aborted since it is already marked as closed", stream
->stream_handle
);
834 stream
->close_requested
= true;
836 if (stream
->last_net_seq_num
== -1ULL) {
838 * Handle connection close without explicit stream close
841 * We can be clever about indexes partially received in
842 * cases where we received the data socket part, but not
843 * the control socket part: since we're currently closing
844 * the stream on behalf of the control socket, we *know*
845 * there won't be any more control information for this
846 * socket. Therefore, we can destroy all indexes for
847 * which we have received only the file descriptor (from
848 * data socket). This takes care of consumerd crashes
849 * between sending the data and control information for
850 * a packet. Since those are sent in that order, we take
851 * care of consumerd crashes.
853 DBG("relay_index_close_partial_fd");
854 relay_index_close_partial_fd(stream
);
856 * Use the highest net_seq_num we currently have pending
857 * As end of stream indicator. Leave last_net_seq_num
858 * at -1ULL if we cannot find any index.
860 stream
->last_net_seq_num
= relay_index_find_last(stream
);
861 DBG("Updating stream->last_net_seq_num to %" PRIu64
, stream
->last_net_seq_num
);
862 /* Fall-through into the next check. */
865 if (stream
->last_net_seq_num
!= -1ULL &&
866 ((int64_t) (stream
->prev_data_seq
- stream
->last_net_seq_num
)) < 0
867 && !session_aborted
) {
869 * Don't close since we still have data pending. This
870 * handles cases where an explicit close command has
871 * been received for this stream, and cases where the
872 * connection has been closed, and we are awaiting for
873 * index information from the data socket. It is
874 * therefore expected that all the index fd information
875 * we need has already been received on the control
876 * socket. Matching index information from data socket
877 * should be Expected Soon(TM).
879 * TODO: We should implement a timer to garbage collect
880 * streams after a timeout to be resilient against a
881 * consumerd implementation that would not match this
884 pthread_mutex_unlock(&stream
->lock
);
885 DBG("closing stream %" PRIu64
" aborted since it still has data pending", stream
->stream_handle
);
889 * We received all the indexes we can expect.
891 stream_unpublish(stream
);
892 stream
->closed
= true;
893 /* Relay indexes are only used by the "consumer/sessiond" end. */
894 relay_index_close_all(stream
);
895 pthread_mutex_unlock(&stream
->lock
);
896 DBG("Succeeded in closing stream %" PRIu64
, stream
->stream_handle
);
900 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
,
905 ASSERT_LOCKED(stream
->lock
);
906 if (caa_likely(stream
->tracefile_size
== 0)) {
907 /* No size limit set; nothing to check. */
912 * Check if writing the new packet would exceed the maximal file size.
914 if (caa_unlikely((stream
->tracefile_size_current
+ packet_size
) >
915 stream
->tracefile_size
)) {
916 const uint64_t new_file_index
=
917 (stream
->tracefile_current_index
+ 1) %
918 stream
->tracefile_count
;
920 if (new_file_index
< stream
->tracefile_current_index
) {
921 stream
->tracefile_wrapped_around
= true;
923 DBG("New stream packet causes stream file rotation: stream_id = %" PRIu64
924 ", current_file_size = %" PRIu64
925 ", packet_size = %zu, current_file_index = %" PRIu64
926 " new_file_index = %" PRIu64
,
927 stream
->stream_handle
,
928 stream
->tracefile_size_current
, packet_size
,
929 stream
->tracefile_current_index
, new_file_index
);
930 tracefile_array_file_rotate(stream
->tfa
);
931 stream
->tracefile_current_index
= new_file_index
;
933 if (stream
->stream_fd
) {
934 stream_fd_put(stream
->stream_fd
);
935 stream
->stream_fd
= NULL
;
937 ret
= stream_create_data_output_file_from_trace_chunk(stream
,
938 stream
->trace_chunk
, false, &stream
->stream_fd
);
940 ERR("Failed to perform trace file rotation of stream %" PRIu64
,
941 stream
->stream_handle
);
946 * Reset current size because we just performed a stream
949 stream
->tracefile_size_current
= 0;
950 *file_rotated
= true;
952 *file_rotated
= false;
958 /* Note that the packet is not necessarily complete. */
959 int stream_write(struct relay_stream
*stream
,
960 const struct lttng_buffer_view
*packet
, size_t padding_len
)
964 size_t padding_to_write
= padding_len
;
965 char padding_buffer
[FILE_IO_STACK_BUFFER_SIZE
];
967 ASSERT_LOCKED(stream
->lock
);
968 memset(padding_buffer
, 0,
969 min(sizeof(padding_buffer
), padding_to_write
));
972 write_ret
= lttng_write(stream
->stream_fd
->fd
,
973 packet
->data
, packet
->size
);
974 if (write_ret
!= packet
->size
) {
975 PERROR("Failed to write to stream file of %sstream %" PRIu64
,
976 stream
->is_metadata
? "metadata " : "",
977 stream
->stream_handle
);
983 while (padding_to_write
> 0) {
984 const size_t padding_to_write_this_pass
=
985 min(padding_to_write
, sizeof(padding_buffer
));
987 write_ret
= lttng_write(stream
->stream_fd
->fd
,
988 padding_buffer
, padding_to_write_this_pass
);
989 if (write_ret
!= padding_to_write_this_pass
) {
990 PERROR("Failed to write padding to file of %sstream %" PRIu64
,
991 stream
->is_metadata
? "metadata " : "",
992 stream
->stream_handle
);
996 padding_to_write
-= padding_to_write_this_pass
;
999 if (stream
->is_metadata
) {
1000 stream
->metadata_received
+= packet
->size
+ padding_len
;
1003 DBG("Wrote to %sstream %" PRIu64
": data_length = %zu, padding_length = %zu",
1004 stream
->is_metadata
? "metadata " : "",
1005 stream
->stream_handle
,
1006 packet
? packet
->size
: (size_t) 0, padding_len
);
1012 * Update index after receiving a packet for a data stream.
1014 * Called with the stream lock held.
1016 * Return 0 on success else a negative value.
1018 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
1019 bool rotate_index
, bool *flushed
, uint64_t total_size
)
1022 uint64_t data_offset
;
1023 struct relay_index
*index
;
1025 ASSERT_LOCKED(stream
->lock
);
1026 /* Get data offset because we are about to update the index. */
1027 data_offset
= htobe64(stream
->tracefile_size_current
);
1029 DBG("handle_index_data: stream %" PRIu64
" net_seq_num %" PRIu64
" data offset %" PRIu64
,
1030 stream
->stream_handle
, net_seq_num
, stream
->tracefile_size_current
);
1033 * Lookup for an existing index for that stream id/sequence
1034 * number. If it exists, the control thread has already received the
1035 * data for it, thus we need to write it to disk.
1037 index
= relay_index_get_by_id_or_create(stream
, net_seq_num
);
1043 if (rotate_index
|| !stream
->index_file
) {
1044 ret
= create_index_file(stream
, stream
->trace_chunk
);
1046 ERR("Failed to create index file for stream %" PRIu64
,
1047 stream
->stream_handle
);
1048 /* Put self-ref for this index due to error. */
1049 relay_index_put(index
);
1055 if (relay_index_set_file(index
, stream
->index_file
, data_offset
)) {
1057 /* Put self-ref for this index due to error. */
1058 relay_index_put(index
);
1063 ret
= relay_index_try_flush(index
);
1065 tracefile_array_commit_seq(stream
->tfa
);
1066 stream
->index_received_seqcount
++;
1068 } else if (ret
> 0) {
1069 index
->total_size
= total_size
;
1076 * relay_index_try_flush is responsible for the self-reference
1077 * put of the index object on error.
1079 ERR("relay_index_try_flush error %d", ret
);
1086 int stream_complete_packet(struct relay_stream
*stream
, size_t packet_total_size
,
1087 uint64_t sequence_number
, bool index_flushed
)
1091 ASSERT_LOCKED(stream
->lock
);
1093 stream
->tracefile_size_current
+= packet_total_size
;
1094 if (index_flushed
) {
1095 stream
->pos_after_last_complete_data_index
=
1096 stream
->tracefile_size_current
;
1097 stream
->prev_index_seq
= sequence_number
;
1098 ret
= try_rotate_stream_index(stream
);
1104 stream
->prev_data_seq
= sequence_number
;
1105 ret
= try_rotate_stream_data(stream
);
1113 int stream_add_index(struct relay_stream
*stream
,
1114 const struct lttcomm_relayd_index
*index_info
)
1117 struct relay_index
*index
;
1119 ASSERT_LOCKED(stream
->lock
);
1121 /* Live beacon handling */
1122 if (index_info
->packet_size
== 0) {
1123 DBG("Received live beacon for stream %" PRIu64
,
1124 stream
->stream_handle
);
1127 * Only flag a stream inactive when it has already
1128 * received data and no indexes are in flight.
1130 if (stream
->index_received_seqcount
> 0
1131 && stream
->indexes_in_flight
== 0) {
1132 stream
->beacon_ts_end
= index_info
->timestamp_end
;
1137 stream
->beacon_ts_end
= -1ULL;
1140 if (stream
->ctf_stream_id
== -1ULL) {
1141 stream
->ctf_stream_id
= index_info
->stream_id
;
1144 index
= relay_index_get_by_id_or_create(stream
, index_info
->net_seq_num
);
1147 ERR("Failed to get or create index %" PRIu64
,
1148 index_info
->net_seq_num
);
1151 if (relay_index_set_control_data(index
, index_info
,
1152 stream
->trace
->session
->minor
)) {
1153 ERR("set_index_control_data error");
1154 relay_index_put(index
);
1158 ret
= relay_index_try_flush(index
);
1160 tracefile_array_commit_seq(stream
->tfa
);
1161 stream
->index_received_seqcount
++;
1162 stream
->pos_after_last_complete_data_index
+= index
->total_size
;
1163 stream
->prev_index_seq
= index_info
->net_seq_num
;
1165 ret
= try_rotate_stream_index(stream
);
1169 } else if (ret
> 0) {
1176 * relay_index_try_flush is responsible for the self-reference
1177 * put of the index object on error.
1179 ERR("relay_index_try_flush error %d", ret
);
1186 static void print_stream_indexes(struct relay_stream
*stream
)
1188 struct lttng_ht_iter iter
;
1189 struct relay_index
*index
;
1192 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
, index
,
1194 DBG("index %p net_seq_num %" PRIu64
" refcount %ld"
1195 " stream %" PRIu64
" trace %" PRIu64
1196 " session %" PRIu64
,
1199 stream
->ref
.refcount
,
1200 index
->stream
->stream_handle
,
1201 index
->stream
->trace
->id
,
1202 index
->stream
->trace
->session
->id
);
1207 int stream_reset_file(struct relay_stream
*stream
)
1209 ASSERT_LOCKED(stream
->lock
);
1211 if (stream
->stream_fd
) {
1212 stream_fd_put(stream
->stream_fd
);
1213 stream
->stream_fd
= NULL
;
1216 stream
->tracefile_size_current
= 0;
1217 stream
->prev_data_seq
= 0;
1218 stream
->prev_index_seq
= 0;
1219 /* Note that this does not reset the tracefile array. */
1220 stream
->tracefile_current_index
= 0;
1221 stream
->pos_after_last_complete_data_index
= 0;
1223 return stream_create_data_output_file_from_trace_chunk(stream
,
1224 stream
->trace_chunk
, true, &stream
->stream_fd
);
1227 void print_relay_streams(void)
1229 struct lttng_ht_iter iter
;
1230 struct relay_stream
*stream
;
1232 if (!relay_streams_ht
) {
1237 cds_lfht_for_each_entry(relay_streams_ht
->ht
, &iter
.iter
, stream
,
1239 if (!stream_get(stream
)) {
1242 DBG("stream %p refcount %ld stream %" PRIu64
" trace %" PRIu64
1243 " session %" PRIu64
,
1245 stream
->ref
.refcount
,
1246 stream
->stream_handle
,
1248 stream
->trace
->session
->id
);
1249 print_stream_indexes(stream
);