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 | /* | |
47 | * Sequence number of the first packet of the new trace chunk to which | |
48 | * the stream is rotating. | |
49 | */ | |
50 | uint64_t seq_num; | |
51 | struct lttng_trace_chunk *next_trace_chunk; | |
52 | }; | |
53 | ||
2a174661 DG |
54 | /* |
55 | * Represents a stream in the relay | |
56 | */ | |
57 | struct relay_stream { | |
58 | uint64_t stream_handle; | |
7591bab1 | 59 | |
7591bab1 MD |
60 | struct urcu_ref ref; |
61 | /* Back reference to trace. Protected by refcount on trace object. */ | |
62 | struct ctf_trace *trace; | |
2a174661 | 63 | |
7591bab1 MD |
64 | /* |
65 | * To protect from concurrent read/update. The viewer stream | |
66 | * lock nests inside the stream lock. The stream lock nests | |
67 | * inside the ctf_trace lock. | |
68 | */ | |
69 | pthread_mutex_t lock; | |
a8f9f353 JG |
70 | /* previous data sequence number written to disk. */ |
71 | uint64_t prev_data_seq; | |
7a45c7e6 JG |
72 | /* previous index sequence number written to disk. */ |
73 | uint64_t prev_index_seq; | |
a8f9f353 JG |
74 | /* seq num to encounter before closing. */ |
75 | uint64_t last_net_seq_num; | |
7591bab1 MD |
76 | |
77 | /* FD on which to write the stream data. */ | |
78 | struct stream_fd *stream_fd; | |
f8f3885c MD |
79 | /* index file on which to write the index data. */ |
80 | struct lttng_index_file *index_file; | |
2a174661 DG |
81 | |
82 | char *path_name; | |
83 | char *channel_name; | |
7591bab1 MD |
84 | |
85 | /* On-disk circular buffer of tracefiles. */ | |
2a174661 DG |
86 | uint64_t tracefile_size; |
87 | uint64_t tracefile_size_current; | |
c35f9726 | 88 | /* Max number of trace files for this stream. */ |
2a174661 | 89 | uint64_t tracefile_count; |
c35f9726 JG |
90 | /* |
91 | * Index of the currently active file for this stream's on-disk | |
92 | * ring buffer. | |
93 | */ | |
94 | uint64_t tracefile_current_index; | |
95 | /* | |
96 | * Indicates that the on-disk buffer has wrapped around. Stream | |
97 | * files shall be unlinked before being opened after this has occurred. | |
98 | */ | |
99 | bool tracefile_wrapped_around; | |
7591bab1 | 100 | |
d3ecc550 JD |
101 | /* |
102 | * Position in the tracefile where we have the full index also on disk. | |
103 | */ | |
104 | uint64_t pos_after_last_complete_data_index; | |
105 | ||
a44ca2ca MD |
106 | /* |
107 | * Counts the number of received indexes. The "tag" associated | |
108 | * with an index is taken before incrementing this seqcount. | |
109 | * Therefore, the sequence tag associated with the last index | |
110 | * received is always index_received_seqcount - 1. | |
111 | */ | |
112 | uint64_t index_received_seqcount; | |
2a174661 | 113 | |
a44ca2ca MD |
114 | /* |
115 | * Tracefile array is an index of the stream trace files, | |
116 | * indexed by position. It allows keeping track of the oldest | |
117 | * available indexes when overwriting trace files in tracefile | |
118 | * rotation. | |
119 | */ | |
120 | struct tracefile_array *tfa; | |
2a174661 | 121 | |
bda7c7b9 JG |
122 | bool closed; /* Stream is closed. */ |
123 | bool close_requested; /* Close command has been received. */ | |
2a174661 DG |
124 | |
125 | /* | |
7591bab1 MD |
126 | * Counts number of indexes in indexes_ht. Redundant info. |
127 | * Protected by stream lock. | |
6e7241fe JD |
128 | */ |
129 | int indexes_in_flight; | |
7591bab1 MD |
130 | struct lttng_ht *indexes_ht; |
131 | ||
528f2ffa | 132 | /* |
7591bab1 MD |
133 | * If the stream is inactive, this field is updated with the |
134 | * live beacon timestamp end, when it is active, this | |
135 | * field == -1ULL. | |
528f2ffa | 136 | */ |
7591bab1 MD |
137 | uint64_t beacon_ts_end; |
138 | ||
139 | /* CTF stream ID, -1ULL when unset (first packet not received yet). */ | |
528f2ffa | 140 | uint64_t ctf_stream_id; |
2a174661 | 141 | |
7591bab1 MD |
142 | /* Indicate if the stream was initialized for a data pending command. */ |
143 | bool data_pending_check_done; | |
144 | ||
145 | /* Is this stream a metadata stream ? */ | |
819cb727 | 146 | bool is_metadata; |
7591bab1 MD |
147 | /* Amount of metadata received (bytes). */ |
148 | uint64_t metadata_received; | |
149 | ||
2a174661 | 150 | /* |
7591bab1 MD |
151 | * Member of the stream list in struct ctf_trace. |
152 | * Updates are protected by the stream_list_lock. | |
153 | * Traversals are protected by RCU. | |
2a174661 | 154 | */ |
7591bab1 | 155 | struct cds_list_head stream_node; |
2a174661 | 156 | /* |
7591bab1 MD |
157 | * Temporary list belonging to the connection until all streams |
158 | * are received for that connection. | |
159 | * Member of the stream recv list in the connection. | |
160 | * Updates are protected by the stream_recv_list_lock. | |
161 | * Traversals are protected by RCU. | |
2a174661 | 162 | */ |
7591bab1 MD |
163 | bool in_recv_list; |
164 | struct cds_list_head recv_node; | |
c35f9726 JG |
165 | /* Protected by session lock. */ |
166 | bool published; | |
2a174661 | 167 | /* |
7591bab1 | 168 | * Node of stream within global stream hash table. |
2a174661 | 169 | */ |
7591bab1 | 170 | struct lttng_ht_node_u64 node; |
77f7bd85 | 171 | bool in_stream_ht; /* is stream in stream hash table. */ |
7591bab1 | 172 | struct rcu_head rcu_node; /* For call_rcu teardown. */ |
d3ecc550 | 173 | /* |
c35f9726 JG |
174 | * The trace chunk to which the file currently being produced (if any) |
175 | * belongs. | |
d3ecc550 | 176 | */ |
348a81dc | 177 | struct lttng_trace_chunk *trace_chunk; |
c35f9726 | 178 | LTTNG_OPTIONAL(struct relay_stream_rotation) ongoing_rotation; |
2a174661 DG |
179 | }; |
180 | ||
7591bab1 MD |
181 | struct relay_stream *stream_create(struct ctf_trace *trace, |
182 | uint64_t stream_handle, char *path_name, | |
183 | char *channel_name, uint64_t tracefile_size, | |
348a81dc | 184 | uint64_t tracefile_count); |
7591bab1 MD |
185 | |
186 | struct relay_stream *stream_get_by_id(uint64_t stream_id); | |
187 | bool stream_get(struct relay_stream *stream); | |
188 | void stream_put(struct relay_stream *stream); | |
c35f9726 JG |
189 | int stream_rotate_output_files(struct relay_session *session, |
190 | struct relay_stream *stream); | |
191 | int stream_set_pending_rotation(struct relay_stream *stream, | |
192 | struct lttng_trace_chunk *next_trace_chunk, | |
193 | uint64_t rotation_sequence_number); | |
bda7c7b9 | 194 | void try_stream_close(struct relay_stream *stream); |
7591bab1 | 195 | void stream_publish(struct relay_stream *stream); |
c35f9726 JG |
196 | int stream_init_packet(struct relay_stream *stream, size_t packet_size, |
197 | bool *file_rotated); | |
198 | int stream_write(struct relay_stream *stream, | |
199 | const struct lttng_buffer_view *packet, size_t padding_len); | |
200 | /* Called after the reception of a complete data packet. */ | |
201 | int stream_update_index(struct relay_stream *stream, uint64_t net_seq_num, | |
202 | bool rotate_index, bool *flushed, uint64_t total_size); | |
203 | int stream_complete_packet(struct relay_stream *stream, | |
204 | size_t packet_total_size, uint64_t sequence_number, | |
205 | bool index_flushed); | |
206 | /* Index info is in host endianness. */ | |
207 | int stream_add_index(struct relay_stream *stream, | |
208 | const struct lttcomm_relayd_index *index_info); | |
209 | int stream_reset_file(struct relay_stream *stream); | |
210 | ||
7591bab1 | 211 | void print_relay_streams(void); |
2a174661 DG |
212 | |
213 | #endif /* _STREAM_H */ |