44a89779421c94669944153bafc57e1d6ea3dbc5
[lttng-tools.git] / src / bin / lttng-relayd / stream.hpp
1 #ifndef _STREAM_H
2 #define _STREAM_H
3
4 /*
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>
8 *
9 * SPDX-License-Identifier: GPL-2.0-only
10 *
11 */
12
13 #include "session.hpp"
14 #include "tracefile-array.hpp"
15
16 #include <common/buffer-view.hpp>
17 #include <common/hashtable/hashtable.hpp>
18 #include <common/optional.hpp>
19 #include <common/trace-chunk.hpp>
20
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <urcu/list.h>
25
26 struct lttcomm_relayd_index;
27
28 struct relay_stream_rotation {
29 /*
30 * Indicates if the stream's data and index have been rotated. A
31 * rotation is considered completed when both rotations have occurred.
32 */
33 bool data_rotated;
34 bool index_rotated;
35 /*
36 * Packet sequence number of the first packet of the new trace chunk to
37 * which the stream is rotating.
38 */
39 uint64_t packet_seq_num;
40 /*
41 * Monotonically increasing previous network sequence number of first
42 * data packet of the new trace chunk to which the stream is rotating.
43 */
44 uint64_t prev_data_net_seq;
45 struct lttng_trace_chunk *next_trace_chunk;
46 };
47
48 /*
49 * Represents a stream in the relay
50 */
51 struct relay_stream {
52 uint64_t stream_handle;
53
54 struct urcu_ref ref;
55 /* Back reference to trace. Protected by refcount on trace object. */
56 struct ctf_trace *trace;
57
58 /*
59 * To protect from concurrent read/update.The stream lock nests
60 * inside the ctf_trace lock.
61 */
62 pthread_mutex_t lock;
63 /* previous data sequence number written to disk. */
64 uint64_t prev_data_seq;
65 /* previous index sequence number written to disk. */
66 uint64_t prev_index_seq;
67 /* seq num to encounter before closing. */
68 uint64_t last_net_seq_num;
69
70 struct fs_handle *file;
71 /* index file on which to write the index data. */
72 struct lttng_index_file *index_file;
73
74 char *path_name;
75 char *channel_name;
76
77 /* On-disk circular buffer of tracefiles. */
78 uint64_t tracefile_size;
79 uint64_t tracefile_size_current;
80 /* Max number of trace files for this stream. */
81 uint64_t tracefile_count;
82 /*
83 * Index of the currently active file for this stream's on-disk
84 * ring buffer.
85 */
86 uint64_t tracefile_current_index;
87 /*
88 * Indicates that the on-disk buffer has wrapped around. Stream
89 * files shall be unlinked before being opened after this has occurred.
90 */
91 bool tracefile_wrapped_around;
92
93 /*
94 * Position in the tracefile where we have the full index also on disk.
95 */
96 uint64_t pos_after_last_complete_data_index;
97
98 /*
99 * Counts the number of received indexes. The "tag" associated
100 * with an index is taken before incrementing this seqcount.
101 * Therefore, the sequence tag associated with the last index
102 * received is always index_received_seqcount - 1.
103 */
104 uint64_t index_received_seqcount;
105
106 /*
107 * Packet sequence number of the last received packet index.
108 * Only populated when interacting with CTF_INDEX 1.1+.
109 */
110 LTTNG_OPTIONAL(uint64_t) received_packet_seq_num;
111
112 /*
113 * Tracefile array is an index of the stream trace files,
114 * indexed by position. It allows keeping track of the oldest
115 * available indexes when overwriting trace files in tracefile
116 * rotation.
117 */
118 struct tracefile_array *tfa;
119
120 bool closed; /* Stream is closed. */
121 bool close_requested; /* Close command has been received. */
122
123 /*
124 * Counts number of indexes in indexes_ht. Redundant info.
125 * Protected by stream lock.
126 */
127 int indexes_in_flight;
128 struct lttng_ht *indexes_ht;
129
130 /*
131 * If the stream is inactive, this field is updated with the
132 * live beacon timestamp end, when it is active, this
133 * field == -1ULL.
134 */
135 uint64_t beacon_ts_end;
136
137 /* CTF stream ID, -1ULL when unset (first packet not received yet). */
138 uint64_t ctf_stream_id;
139
140 /* Indicate if the stream was initialized for a data pending command. */
141 bool data_pending_check_done;
142
143 /* Is this stream a metadata stream ? */
144 bool is_metadata;
145 /* Amount of metadata received (bytes). */
146 uint64_t metadata_received;
147
148 /*
149 * Member of the stream list in struct ctf_trace.
150 * Updates are protected by the stream_list_lock.
151 * Traversals are protected by RCU.
152 */
153 struct cds_list_head stream_node;
154 /*
155 * Temporary list belonging to the connection until all streams
156 * are received for that connection.
157 * Member of the stream recv list in the connection.
158 * Updates are protected by the stream_recv_list_lock.
159 * Traversals are protected by RCU.
160 */
161 bool in_recv_list;
162 struct cds_list_head recv_node;
163 /* Protected by session lock. */
164 bool published;
165 /* Notified viewer that no new metadata is available. */
166 bool no_new_metadata_notified;
167 /*
168 * Node of stream within global stream hash table.
169 */
170 struct lttng_ht_node_u64 node;
171 bool in_stream_ht; /* is stream in stream hash table. */
172 struct rcu_head rcu_node; /* For call_rcu teardown. */
173 /*
174 * The trace chunk to which the file currently being produced (if any)
175 * belongs.
176 *
177 * Note that a relay stream can have no output trace chunk. For
178 * instance, after a session stop followed by a session clear,
179 * streams will not have an output trace chunk until the session
180 * is resumed.
181 */
182 struct lttng_trace_chunk *trace_chunk;
183 LTTNG_OPTIONAL(struct relay_stream_rotation) ongoing_rotation;
184 uint64_t completed_rotation_count;
185 };
186
187 struct relay_stream *stream_create(struct ctf_trace *trace,
188 uint64_t stream_handle,
189 char *path_name,
190 char *channel_name,
191 uint64_t tracefile_size,
192 uint64_t tracefile_count);
193
194 struct relay_stream *stream_get_by_id(uint64_t stream_id);
195 bool stream_get(struct relay_stream *stream);
196 void stream_put(struct relay_stream *stream);
197 int stream_rotate_output_files(struct relay_session *session, struct relay_stream *stream);
198 int stream_set_pending_rotation(struct relay_stream *stream,
199 struct lttng_trace_chunk *next_trace_chunk,
200 uint64_t rotation_sequence_number);
201 void try_stream_close(struct relay_stream *stream);
202 void stream_publish(struct relay_stream *stream);
203 int stream_init_packet(struct relay_stream *stream, size_t packet_size, bool *file_rotated);
204 int stream_write(struct relay_stream *stream,
205 const struct lttng_buffer_view *packet,
206 size_t padding_len);
207 /* Called after the reception of a complete data packet. */
208 int stream_update_index(struct relay_stream *stream,
209 uint64_t net_seq_num,
210 bool rotate_index,
211 bool *flushed,
212 uint64_t total_size);
213 int stream_complete_packet(struct relay_stream *stream,
214 size_t packet_total_size,
215 uint64_t sequence_number,
216 bool index_flushed);
217 /* Index info is in host endianness. */
218 int stream_add_index(struct relay_stream *stream, const struct lttcomm_relayd_index *index_info);
219 int stream_reset_file(struct relay_stream *stream);
220
221 void print_relay_streams(void);
222
223 #endif /* _STREAM_H */
This page took 0.03623 seconds and 3 git commands to generate.