5 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
6 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
7 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * SPDX-License-Identifier: GPL-2.0-only
16 #include <urcu/list.h>
18 #include <common/hashtable/hashtable.h>
19 #include <common/trace-chunk.h>
20 #include <common/optional.h>
21 #include <common/buffer-view.h>
24 #include "tracefile-array.h"
26 struct lttcomm_relayd_index
;
28 struct relay_stream_rotation
{
30 * Indicates if the stream's data and index have been rotated. A
31 * rotation is considered completed when both rotations have occurred.
36 * Packet sequence number of the first packet of the new trace chunk to
37 * which the stream is rotating.
39 uint64_t packet_seq_num
;
41 * Monotonically increasing previous network sequence number of first
42 * data packet of the new trace chunk to which the stream is rotating.
44 uint64_t prev_data_net_seq
;
45 struct lttng_trace_chunk
*next_trace_chunk
;
49 * Represents a stream in the relay
52 uint64_t stream_handle
;
55 /* Back reference to trace. Protected by refcount on trace object. */
56 struct ctf_trace
*trace
;
59 * To protect from concurrent read/update. The viewer stream
60 * lock nests inside the stream lock. The stream lock nests
61 * inside the ctf_trace lock.
64 /* previous data sequence number written to disk. */
65 uint64_t prev_data_seq
;
66 /* previous index sequence number written to disk. */
67 uint64_t prev_index_seq
;
68 /* seq num to encounter before closing. */
69 uint64_t last_net_seq_num
;
71 struct fs_handle
*file
;
72 /* index file on which to write the index data. */
73 struct lttng_index_file
*index_file
;
78 /* On-disk circular buffer of tracefiles. */
79 uint64_t tracefile_size
;
80 uint64_t tracefile_size_current
;
81 /* Max number of trace files for this stream. */
82 uint64_t tracefile_count
;
84 * Index of the currently active file for this stream's on-disk
87 uint64_t tracefile_current_index
;
89 * Indicates that the on-disk buffer has wrapped around. Stream
90 * files shall be unlinked before being opened after this has occurred.
92 bool tracefile_wrapped_around
;
95 * Position in the tracefile where we have the full index also on disk.
97 uint64_t pos_after_last_complete_data_index
;
100 * Counts the number of received indexes. The "tag" associated
101 * with an index is taken before incrementing this seqcount.
102 * Therefore, the sequence tag associated with the last index
103 * received is always index_received_seqcount - 1.
105 uint64_t index_received_seqcount
;
108 * Packet sequence number of the last received packet index.
109 * Only populated when interacting with CTF_INDEX 1.1+.
111 LTTNG_OPTIONAL(uint64_t) received_packet_seq_num
;
114 * Tracefile array is an index of the stream trace files,
115 * indexed by position. It allows keeping track of the oldest
116 * available indexes when overwriting trace files in tracefile
119 struct tracefile_array
*tfa
;
121 bool closed
; /* Stream is closed. */
122 bool close_requested
; /* Close command has been received. */
125 * Counts number of indexes in indexes_ht. Redundant info.
126 * Protected by stream lock.
128 int indexes_in_flight
;
129 struct lttng_ht
*indexes_ht
;
132 * If the stream is inactive, this field is updated with the
133 * live beacon timestamp end, when it is active, this
136 uint64_t beacon_ts_end
;
138 /* CTF stream ID, -1ULL when unset (first packet not received yet). */
139 uint64_t ctf_stream_id
;
141 /* Indicate if the stream was initialized for a data pending command. */
142 bool data_pending_check_done
;
144 /* Is this stream a metadata stream ? */
146 /* Amount of metadata received (bytes). */
147 uint64_t metadata_received
;
150 * Member of the stream list in struct ctf_trace.
151 * Updates are protected by the stream_list_lock.
152 * Traversals are protected by RCU.
154 struct cds_list_head stream_node
;
156 * Temporary list belonging to the connection until all streams
157 * are received for that connection.
158 * Member of the stream recv list in the connection.
159 * Updates are protected by the stream_recv_list_lock.
160 * Traversals are protected by RCU.
163 struct cds_list_head recv_node
;
164 /* Protected by session lock. */
166 /* Notified viewer that no new metadata is available. */
167 bool no_new_metadata_notified
;
169 * Node of stream within global stream hash table.
171 struct lttng_ht_node_u64 node
;
172 bool in_stream_ht
; /* is stream in stream hash table. */
173 struct rcu_head rcu_node
; /* For call_rcu teardown. */
175 * The trace chunk to which the file currently being produced (if any)
178 struct lttng_trace_chunk
*trace_chunk
;
179 LTTNG_OPTIONAL(struct relay_stream_rotation
) ongoing_rotation
;
180 uint64_t completed_rotation_count
;
183 struct relay_stream
*stream_create(struct ctf_trace
*trace
,
184 uint64_t stream_handle
, char *path_name
,
185 char *channel_name
, uint64_t tracefile_size
,
186 uint64_t tracefile_count
);
188 struct relay_stream
*stream_get_by_id(uint64_t stream_id
);
189 bool stream_get(struct relay_stream
*stream
);
190 void stream_put(struct relay_stream
*stream
);
191 int stream_rotate_output_files(struct relay_session
*session
,
192 struct relay_stream
*stream
);
193 int stream_set_pending_rotation(struct relay_stream
*stream
,
194 struct lttng_trace_chunk
*next_trace_chunk
,
195 uint64_t rotation_sequence_number
);
196 void try_stream_close(struct relay_stream
*stream
);
197 void stream_publish(struct relay_stream
*stream
);
198 int stream_init_packet(struct relay_stream
*stream
, size_t packet_size
,
200 int stream_write(struct relay_stream
*stream
,
201 const struct lttng_buffer_view
*packet
, size_t padding_len
);
202 /* Called after the reception of a complete data packet. */
203 int stream_update_index(struct relay_stream
*stream
, uint64_t net_seq_num
,
204 bool rotate_index
, bool *flushed
, uint64_t total_size
);
205 int stream_complete_packet(struct relay_stream
*stream
,
206 size_t packet_total_size
, uint64_t sequence_number
,
208 /* Index info is in host endianness. */
209 int stream_add_index(struct relay_stream
*stream
,
210 const struct lttcomm_relayd_index
*index_info
);
211 int stream_reset_file(struct relay_stream
*stream
);
213 void print_relay_streams(void);
215 #endif /* _STREAM_H */