Commit | Line | Data |
---|---|---|
14f28187 FD |
1 | #ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H |
2 | #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H | |
3 | ||
4 | /* | |
5 | * BabelTrace - LTTng-live client Component | |
6 | * | |
7 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> | |
8 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
10 | * | |
11 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
12 | * | |
13 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
14 | * of this software and associated documentation files (the "Software"), to deal | |
15 | * in the Software without restriction, including without limitation the rights | |
16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
17 | * copies of the Software, and to permit persons to whom the Software is | |
18 | * furnished to do so, subject to the following conditions: | |
19 | * | |
20 | * The above copyright notice and this permission notice shall be included in | |
21 | * all copies or substantial portions of the Software. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
29 | * SOFTWARE. | |
30 | */ | |
31 | ||
32 | #include <stdbool.h> | |
33 | ||
91d81473 | 34 | #include "common/macros.h" |
3fadfbc0 | 35 | #include <babeltrace2/babeltrace.h> |
14f28187 FD |
36 | |
37 | #include "../common/metadata/decoder.h" | |
38 | #include "../common/msg-iter/msg-iter.h" | |
39 | ||
40 | #include "viewer-connection.h" | |
41 | ||
42 | struct lttng_live_component; | |
43 | struct lttng_live_session; | |
44 | struct lttng_live_msg_iter; | |
45 | ||
46 | enum lttng_live_stream_state { | |
47 | /* This stream won't have data until some known time in the future. */ | |
48 | LTTNG_LIVE_STREAM_QUIESCENT, | |
49 | /* | |
50 | * This stream won't have data until some known time in the future and | |
51 | * the message iterator inactivity message was already sent downstream. | |
52 | */ | |
53 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */ | |
54 | /* This stream has data ready to be consumed. */ | |
55 | LTTNG_LIVE_STREAM_ACTIVE_DATA, | |
56 | /* | |
57 | * This stream has no data left to consume. We should asked the relay | |
58 | * for more. | |
59 | */ | |
60 | LTTNG_LIVE_STREAM_ACTIVE_NO_DATA, | |
61 | /* This stream won't have anymore data, ever. */ | |
62 | LTTNG_LIVE_STREAM_EOF, | |
63 | }; | |
64 | ||
65 | /* Iterator over a live stream. */ | |
66 | struct lttng_live_stream_iterator { | |
c01594de PP |
67 | bt_logging_level log_level; |
68 | ||
14f28187 FD |
69 | /* Owned by this. */ |
70 | bt_stream *stream; | |
71 | ||
72 | /* Weak reference. */ | |
73 | struct lttng_live_trace *trace; | |
74 | ||
75 | /* | |
76 | * Since only a single iterator per viewer connection, we have | |
77 | * only a single message iterator per stream. | |
78 | */ | |
79 | struct bt_msg_iter *msg_iter; | |
80 | ||
81 | uint64_t viewer_stream_id; | |
82 | ||
83 | uint64_t ctf_stream_class_id; | |
84 | ||
85 | /* base offset in current index. */ | |
86 | uint64_t base_offset; | |
87 | /* len to read in current index. */ | |
88 | uint64_t len; | |
89 | /* offset in current index. */ | |
90 | uint64_t offset; | |
91 | ||
92 | /* | |
93 | * Clock Snapshot value of the last message iterator inactivity message | |
94 | * sent downstream. | |
95 | */ | |
96 | uint64_t last_inactivity_ts; | |
97 | ||
98 | /* | |
99 | * Clock Snapshot value of the current message iterator inactivity | |
100 | * message we might want to send downstream. | |
101 | */ | |
102 | uint64_t current_inactivity_ts; | |
103 | ||
104 | enum lttng_live_stream_state state; | |
105 | ||
106 | /* | |
107 | * The current message produced by this live stream iterator. Owned by | |
108 | * this. | |
109 | */ | |
110 | bt_message *current_msg; | |
111 | ||
112 | /* Timestamp in nanoseconds of the current message (current_msg). */ | |
113 | int64_t current_msg_ts_ns; | |
114 | ||
115 | /* Owned by this. */ | |
116 | uint8_t *buf; | |
117 | size_t buflen; | |
118 | ||
119 | /* Owned by this. */ | |
120 | GString *name; | |
121 | }; | |
122 | ||
123 | struct lttng_live_metadata { | |
c01594de PP |
124 | bt_logging_level log_level; |
125 | ||
14f28187 FD |
126 | /* Weak reference. */ |
127 | struct lttng_live_trace *trace; | |
128 | ||
129 | uint64_t stream_id; | |
130 | /* Weak reference. */ | |
131 | struct ctf_metadata_decoder *decoder; | |
132 | ||
133 | bool closed; | |
134 | }; | |
135 | ||
136 | struct lttng_live_trace { | |
c01594de PP |
137 | bt_logging_level log_level; |
138 | ||
14f28187 FD |
139 | /* Back reference to session. */ |
140 | struct lttng_live_session *session; | |
141 | ||
142 | /* ctf trace ID within the session. */ | |
143 | uint64_t id; | |
144 | ||
145 | /* Owned by this. */ | |
146 | bt_trace *trace; | |
147 | ||
148 | /* Weak reference. */ | |
149 | bt_trace_class *trace_class; | |
150 | ||
151 | struct lttng_live_metadata *metadata; | |
152 | ||
153 | const bt_clock_class *clock_class; | |
154 | ||
155 | /* Array of pointers to struct lttng_live_stream_iterator. */ | |
156 | /* Owned by this. */ | |
157 | GPtrArray *stream_iterators; | |
158 | ||
159 | bool new_metadata_needed; | |
160 | }; | |
161 | ||
162 | struct lttng_live_session { | |
c01594de PP |
163 | bt_logging_level log_level; |
164 | ||
14f28187 FD |
165 | /* Weak reference. */ |
166 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
167 | ||
168 | /* Owned by this. */ | |
169 | GString *hostname; | |
170 | ||
171 | /* Owned by this. */ | |
172 | GString *session_name; | |
173 | ||
174 | uint64_t id; | |
175 | ||
176 | /* Array of pointers to struct lttng_live_trace. */ | |
177 | GPtrArray *traces; | |
178 | ||
179 | bool attached; | |
180 | bool new_streams_needed; | |
181 | bool lazy_stream_msg_init; | |
182 | bool closed; | |
183 | }; | |
184 | ||
185 | enum session_not_found_action { | |
186 | SESSION_NOT_FOUND_ACTION_CONTINUE, | |
187 | SESSION_NOT_FOUND_ACTION_FAIL, | |
188 | SESSION_NOT_FOUND_ACTION_END, | |
189 | }; | |
190 | ||
191 | /* | |
192 | * A component instance is an iterator on a single session. | |
193 | */ | |
194 | struct lttng_live_component { | |
c01594de PP |
195 | bt_logging_level log_level; |
196 | ||
14f28187 FD |
197 | /* Weak reference. */ |
198 | bt_self_component_source *self_comp; | |
199 | ||
200 | struct { | |
201 | GString *url; | |
202 | enum session_not_found_action sess_not_found_act; | |
203 | } params; | |
204 | ||
205 | size_t max_query_size; | |
206 | ||
207 | /* | |
208 | * Keeps track of whether the downstream component already has a | |
209 | * message iterator on this component. | |
210 | */ | |
211 | bool has_msg_iter; | |
212 | }; | |
213 | ||
214 | struct lttng_live_msg_iter { | |
c01594de PP |
215 | bt_logging_level log_level; |
216 | ||
14f28187 FD |
217 | /* Weak reference. */ |
218 | struct lttng_live_component *lttng_live_comp; | |
219 | ||
220 | /* Weak reference. */ | |
221 | bt_self_message_iterator *self_msg_iter; | |
222 | ||
223 | /* Owned by this. */ | |
224 | struct live_viewer_connection *viewer_connection; | |
225 | ||
226 | /* Array of pointers to struct lttng_live_session. */ | |
227 | GPtrArray *sessions; | |
228 | ||
229 | /* Number of live stream iterator this message iterator has.*/ | |
230 | uint64_t active_stream_iter; | |
231 | ||
232 | /* Timestamp in nanosecond of the last message sent downstream. */ | |
233 | int64_t last_msg_ts_ns; | |
234 | }; | |
235 | ||
236 | enum lttng_live_iterator_status { | |
237 | /** Iterator state has progressed. Continue iteration immediately. */ | |
238 | LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3, | |
239 | /** No message available for now. Try again later. */ | |
240 | LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2, | |
241 | /** No more CTF_LTTNG_LIVEs to be delivered. */ | |
242 | LTTNG_LIVE_ITERATOR_STATUS_END = 1, | |
243 | /** No error, okay. */ | |
244 | LTTNG_LIVE_ITERATOR_STATUS_OK = 0, | |
245 | /** Invalid arguments. */ | |
246 | LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1, | |
247 | /** General error. */ | |
248 | LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2, | |
249 | /** Out of memory. */ | |
250 | LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3, | |
251 | /** Unsupported iterator feature. */ | |
252 | LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4, | |
253 | }; | |
254 | ||
255 | bt_self_component_status lttng_live_component_init( | |
256 | bt_self_component_source *self_comp, | |
257 | const bt_value *params, void *init_method_data); | |
258 | ||
259 | bt_query_status lttng_live_query( | |
260 | bt_self_component_class_source *comp_class, | |
261 | const bt_query_executor *query_exec, | |
262 | const char *object, const bt_value *params, | |
f4e38e70 | 263 | bt_logging_level log_level, |
14f28187 FD |
264 | const bt_value **result); |
265 | ||
266 | void lttng_live_component_finalize(bt_self_component_source *component); | |
267 | ||
268 | bt_self_message_iterator_status lttng_live_msg_iter_next( | |
269 | bt_self_message_iterator *iterator, | |
270 | bt_message_array_const msgs, uint64_t capacity, | |
271 | uint64_t *count); | |
272 | ||
273 | bt_self_message_iterator_status lttng_live_msg_iter_init( | |
274 | bt_self_message_iterator *self_msg_it, | |
275 | bt_self_component_source *self_comp, | |
276 | bt_self_component_port_output *self_port); | |
277 | ||
278 | void lttng_live_msg_iter_finalize(bt_self_message_iterator *it); | |
279 | ||
280 | int lttng_live_create_viewer_session(struct lttng_live_msg_iter *lttng_live_msg_iter); | |
281 | int lttng_live_attach_session(struct lttng_live_session *session); | |
282 | int lttng_live_detach_session(struct lttng_live_session *session); | |
283 | enum lttng_live_iterator_status lttng_live_get_new_streams( | |
284 | struct lttng_live_session *session); | |
285 | ||
286 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
287 | uint64_t session_id, | |
288 | const char *hostname, | |
289 | const char *session_name); | |
290 | ||
291 | ssize_t lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, | |
292 | FILE *fp); | |
293 | enum lttng_live_iterator_status lttng_live_get_next_index( | |
294 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
295 | struct lttng_live_stream_iterator *stream, | |
296 | struct packet_index *index); | |
297 | ||
298 | enum bt_msg_iter_medium_status lttng_live_get_stream_bytes( | |
299 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
300 | struct lttng_live_stream_iterator *stream, uint8_t *buf, | |
301 | uint64_t offset, uint64_t req_len, uint64_t *recv_len); | |
302 | void lttng_live_add_stream_iterator(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
303 | struct lttng_live_stream_iterator *stream_iter); | |
304 | void lttng_live_remove_stream_iterator(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
305 | struct lttng_live_stream_iterator *stream_iter); | |
306 | ||
307 | struct lttng_live_trace *lttng_live_borrow_trace( | |
308 | struct lttng_live_session *session, uint64_t trace_id); | |
309 | void lttng_live_need_new_streams(struct lttng_live_msg_iter *lttng_live_msg_iter); | |
310 | ||
42521b69 | 311 | bool lttng_live_graph_is_canceled(struct lttng_live_component *lttng_live); |
14f28187 FD |
312 | |
313 | #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */ |