Commit | Line | Data |
---|---|---|
7591bab1 MD |
1 | #ifndef _STREAM_H |
2 | #define _STREAM_H | |
3 | ||
2a174661 DG |
4 | /* |
5 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
6 | * David Goulet <dgoulet@efficios.com> | |
7591bab1 | 7 | * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
2a174661 DG |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License, version 2 only, as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along with | |
19 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
2a174661 DG |
23 | #include <limits.h> |
24 | #include <inttypes.h> | |
25 | #include <pthread.h> | |
26 | #include <urcu/list.h> | |
27 | ||
28 | #include <common/hashtable/hashtable.h> | |
348a81dc | 29 | #include <common/trace-chunk.h> |
c35f9726 JG |
30 | #include <common/optional.h> |
31 | #include <common/buffer-view.h> | |
2a174661 DG |
32 | |
33 | #include "session.h" | |
7591bab1 | 34 | #include "stream-fd.h" |
a44ca2ca | 35 | #include "tracefile-array.h" |
2a174661 | 36 | |
c35f9726 JG |
37 | struct lttcomm_relayd_index; |
38 | ||
39 | struct relay_stream_rotation { | |
40 | /* | |
41 | * Indicates if the stream's data and index have been rotated. A | |
42 | * rotation is considered completed when both rotations have occurred. | |
43 | */ | |
44 | bool data_rotated; | |
45 | bool index_rotated; | |
46 | /* | |
0f83d1cc MD |
47 | * Packet sequence number of the first packet of the new trace chunk to |
48 | * which the stream is rotating. | |
c35f9726 | 49 | */ |
0f83d1cc MD |
50 | uint64_t packet_seq_num; |
51 | /* | |
52 | * Monotonically increasing previous network sequence number of first | |
53 | * data packet of the new trace chunk to which the stream is rotating. | |
54 | */ | |
55 | uint64_t prev_data_net_seq; | |
c35f9726 JG |
56 | struct lttng_trace_chunk *next_trace_chunk; |
57 | }; | |
58 | ||
2a174661 DG |
59 | /* |
60 | * Represents a stream in the relay | |
61 | */ | |
62 | struct relay_stream { | |
63 | uint64_t stream_handle; | |
7591bab1 | 64 | |
7591bab1 MD |
65 | struct urcu_ref ref; |
66 | /* Back reference to trace. Protected by refcount on trace object. */ | |
67 | struct ctf_trace *trace; | |
2a174661 | 68 | |
7591bab1 MD |
69 | /* |
70 | * To protect from concurrent read/update. The viewer stream | |
71 | * lock nests inside the stream lock. The stream lock nests | |
72 | * inside the ctf_trace lock. | |
73 | */ | |
74 | pthread_mutex_t lock; | |
a8f9f353 JG |
75 | /* previous data sequence number written to disk. */ |
76 | uint64_t prev_data_seq; | |
7a45c7e6 JG |
77 | /* previous index sequence number written to disk. */ |
78 | uint64_t prev_index_seq; | |
a8f9f353 JG |
79 | /* seq num to encounter before closing. */ |
80 | uint64_t last_net_seq_num; | |
7591bab1 MD |
81 | |
82 | /* FD on which to write the stream data. */ | |
83 | struct stream_fd *stream_fd; | |
f8f3885c MD |
84 | /* index file on which to write the index data. */ |
85 | struct lttng_index_file *index_file; | |
2a174661 DG |
86 | |
87 | char *path_name; | |
88 | char *channel_name; | |
7591bab1 MD |
89 | |
90 | /* On-disk circular buffer of tracefiles. */ | |
2a174661 DG |
91 | uint64_t tracefile_size; |
92 | uint64_t tracefile_size_current; | |
c35f9726 | 93 | /* Max number of trace files for this stream. */ |
2a174661 | 94 | uint64_t tracefile_count; |
c35f9726 JG |
95 | /* |
96 | * Index of the currently active file for this stream's on-disk | |
97 | * ring buffer. | |
98 | */ | |
99 | uint64_t tracefile_current_index; | |
100 | /* | |
101 | * Indicates that the on-disk buffer has wrapped around. Stream | |
102 | * files shall be unlinked before being opened after this has occurred. | |
103 | */ | |
104 | bool tracefile_wrapped_around; | |
7591bab1 | 105 | |
d3ecc550 JD |
106 | /* |
107 | * Position in the tracefile where we have the full index also on disk. | |
108 | */ | |
109 | uint64_t pos_after_last_complete_data_index; | |
110 | ||
a44ca2ca MD |
111 | /* |
112 | * Counts the number of received indexes. The "tag" associated | |
113 | * with an index is taken before incrementing this seqcount. | |
114 | * Therefore, the sequence tag associated with the last index | |
115 | * received is always index_received_seqcount - 1. | |
116 | */ | |
117 | uint64_t index_received_seqcount; | |
2a174661 | 118 | |
0f83d1cc MD |
119 | /* |
120 | * Packet sequence number of the last received packet index. | |
121 | * Only populated when interacting with CTF_INDEX 1.1+. | |
122 | */ | |
123 | LTTNG_OPTIONAL(uint64_t) received_packet_seq_num; | |
124 | ||
a44ca2ca MD |
125 | /* |
126 | * Tracefile array is an index of the stream trace files, | |
127 | * indexed by position. It allows keeping track of the oldest | |
128 | * available indexes when overwriting trace files in tracefile | |
129 | * rotation. | |
130 | */ | |
131 | struct tracefile_array *tfa; | |
2a174661 | 132 | |
bda7c7b9 JG |
133 | bool closed; /* Stream is closed. */ |
134 | bool close_requested; /* Close command has been received. */ | |
2a174661 DG |
135 | |
136 | /* | |
7591bab1 MD |
137 | * Counts number of indexes in indexes_ht. Redundant info. |
138 | * Protected by stream lock. | |
6e7241fe JD |
139 | */ |
140 | int indexes_in_flight; | |
7591bab1 MD |
141 | struct lttng_ht *indexes_ht; |
142 | ||
528f2ffa | 143 | /* |
7591bab1 MD |
144 | * If the stream is inactive, this field is updated with the |
145 | * live beacon timestamp end, when it is active, this | |
146 | * field == -1ULL. | |
528f2ffa | 147 | */ |
7591bab1 MD |
148 | uint64_t beacon_ts_end; |
149 | ||
150 | /* CTF stream ID, -1ULL when unset (first packet not received yet). */ | |
528f2ffa | 151 | uint64_t ctf_stream_id; |
2a174661 | 152 | |
7591bab1 MD |
153 | /* Indicate if the stream was initialized for a data pending command. */ |
154 | bool data_pending_check_done; | |
155 | ||
156 | /* Is this stream a metadata stream ? */ | |
819cb727 | 157 | bool is_metadata; |
7591bab1 MD |
158 | /* Amount of metadata received (bytes). */ |
159 | uint64_t metadata_received; | |
160 | ||
2a174661 | 161 | /* |
7591bab1 MD |
162 | * Member of the stream list in struct ctf_trace. |
163 | * Updates are protected by the stream_list_lock. | |
164 | * Traversals are protected by RCU. | |
2a174661 | 165 | */ |
7591bab1 | 166 | struct cds_list_head stream_node; |
2a174661 | 167 | /* |
7591bab1 MD |
168 | * Temporary list belonging to the connection until all streams |
169 | * are received for that connection. | |
170 | * Member of the stream recv list in the connection. | |
171 | * Updates are protected by the stream_recv_list_lock. | |
172 | * Traversals are protected by RCU. | |
2a174661 | 173 | */ |
7591bab1 MD |
174 | bool in_recv_list; |
175 | struct cds_list_head recv_node; | |
c35f9726 JG |
176 | /* Protected by session lock. */ |
177 | bool published; | |
94f73d08 MD |
178 | /* Notified viewer that no new metadata is available. */ |
179 | bool no_new_metadata_notified; | |
2a174661 | 180 | /* |
7591bab1 | 181 | * Node of stream within global stream hash table. |
2a174661 | 182 | */ |
7591bab1 | 183 | struct lttng_ht_node_u64 node; |
77f7bd85 | 184 | bool in_stream_ht; /* is stream in stream hash table. */ |
7591bab1 | 185 | struct rcu_head rcu_node; /* For call_rcu teardown. */ |
d3ecc550 | 186 | /* |
c35f9726 JG |
187 | * The trace chunk to which the file currently being produced (if any) |
188 | * belongs. | |
d3ecc550 | 189 | */ |
348a81dc | 190 | struct lttng_trace_chunk *trace_chunk; |
c35f9726 | 191 | LTTNG_OPTIONAL(struct relay_stream_rotation) ongoing_rotation; |
2a174661 DG |
192 | }; |
193 | ||
7591bab1 MD |
194 | struct relay_stream *stream_create(struct ctf_trace *trace, |
195 | uint64_t stream_handle, char *path_name, | |
196 | char *channel_name, uint64_t tracefile_size, | |
348a81dc | 197 | uint64_t tracefile_count); |
7591bab1 MD |
198 | |
199 | struct relay_stream *stream_get_by_id(uint64_t stream_id); | |
200 | bool stream_get(struct relay_stream *stream); | |
201 | void stream_put(struct relay_stream *stream); | |
c35f9726 JG |
202 | int stream_rotate_output_files(struct relay_session *session, |
203 | struct relay_stream *stream); | |
204 | int stream_set_pending_rotation(struct relay_stream *stream, | |
205 | struct lttng_trace_chunk *next_trace_chunk, | |
206 | uint64_t rotation_sequence_number); | |
bda7c7b9 | 207 | void try_stream_close(struct relay_stream *stream); |
7591bab1 | 208 | void stream_publish(struct relay_stream *stream); |
c35f9726 JG |
209 | int stream_init_packet(struct relay_stream *stream, size_t packet_size, |
210 | bool *file_rotated); | |
211 | int stream_write(struct relay_stream *stream, | |
212 | const struct lttng_buffer_view *packet, size_t padding_len); | |
213 | /* Called after the reception of a complete data packet. */ | |
214 | int stream_update_index(struct relay_stream *stream, uint64_t net_seq_num, | |
215 | bool rotate_index, bool *flushed, uint64_t total_size); | |
216 | int stream_complete_packet(struct relay_stream *stream, | |
217 | size_t packet_total_size, uint64_t sequence_number, | |
218 | bool index_flushed); | |
219 | /* Index info is in host endianness. */ | |
220 | int stream_add_index(struct relay_stream *stream, | |
221 | const struct lttcomm_relayd_index *index_info); | |
222 | int stream_reset_file(struct relay_stream *stream); | |
223 | ||
7591bab1 | 224 | void print_relay_streams(void); |
2a174661 DG |
225 | |
226 | #endif /* _STREAM_H */ |