Commit | Line | Data |
---|---|---|
14f28187 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
14f28187 FD |
3 | * |
4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> | |
5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
6 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
0235b0db | 8 | * BabelTrace - LTTng-live client Component |
14f28187 FD |
9 | */ |
10 | ||
0235b0db MJ |
11 | #ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H |
12 | #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H | |
13 | ||
14f28187 | 14 | #include <stdbool.h> |
3c22a242 FD |
15 | #include <stdint.h> |
16 | ||
17 | #include <glib.h> | |
14f28187 | 18 | |
3fadfbc0 | 19 | #include <babeltrace2/babeltrace.h> |
14f28187 | 20 | |
3c22a242 | 21 | #include "common/macros.h" |
14f28187 FD |
22 | #include "../common/metadata/decoder.h" |
23 | #include "../common/msg-iter/msg-iter.h" | |
14f28187 FD |
24 | #include "viewer-connection.h" |
25 | ||
26 | struct lttng_live_component; | |
27 | struct lttng_live_session; | |
28 | struct lttng_live_msg_iter; | |
29 | ||
30 | enum lttng_live_stream_state { | |
31 | /* This stream won't have data until some known time in the future. */ | |
32 | LTTNG_LIVE_STREAM_QUIESCENT, | |
33 | /* | |
34 | * This stream won't have data until some known time in the future and | |
35 | * the message iterator inactivity message was already sent downstream. | |
36 | */ | |
37 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */ | |
38 | /* This stream has data ready to be consumed. */ | |
39 | LTTNG_LIVE_STREAM_ACTIVE_DATA, | |
40 | /* | |
41 | * This stream has no data left to consume. We should asked the relay | |
42 | * for more. | |
43 | */ | |
44 | LTTNG_LIVE_STREAM_ACTIVE_NO_DATA, | |
45 | /* This stream won't have anymore data, ever. */ | |
46 | LTTNG_LIVE_STREAM_EOF, | |
47 | }; | |
48 | ||
49 | /* Iterator over a live stream. */ | |
50 | struct lttng_live_stream_iterator { | |
c01594de | 51 | bt_logging_level log_level; |
2ece7dd0 | 52 | bt_self_component *self_comp; |
c01594de | 53 | |
14f28187 FD |
54 | /* Owned by this. */ |
55 | bt_stream *stream; | |
56 | ||
57 | /* Weak reference. */ | |
58 | struct lttng_live_trace *trace; | |
59 | ||
60 | /* | |
61 | * Since only a single iterator per viewer connection, we have | |
62 | * only a single message iterator per stream. | |
63 | */ | |
18a1979b | 64 | struct ctf_msg_iter *msg_iter; |
14f28187 FD |
65 | |
66 | uint64_t viewer_stream_id; | |
67 | ||
1a258819 FD |
68 | struct { |
69 | bool is_set; | |
70 | uint64_t value; | |
71 | } ctf_stream_class_id; | |
14f28187 FD |
72 | |
73 | /* base offset in current index. */ | |
74 | uint64_t base_offset; | |
75 | /* len to read in current index. */ | |
76 | uint64_t len; | |
77 | /* offset in current index. */ | |
78 | uint64_t offset; | |
79 | ||
80 | /* | |
81 | * Clock Snapshot value of the last message iterator inactivity message | |
82 | * sent downstream. | |
83 | */ | |
a043c362 FD |
84 | struct { |
85 | bool is_set; | |
86 | uint64_t value; | |
87 | } last_inactivity_ts; | |
14f28187 FD |
88 | |
89 | /* | |
90 | * Clock Snapshot value of the current message iterator inactivity | |
91 | * message we might want to send downstream. | |
92 | */ | |
93 | uint64_t current_inactivity_ts; | |
94 | ||
95 | enum lttng_live_stream_state state; | |
96 | ||
97 | /* | |
98 | * The current message produced by this live stream iterator. Owned by | |
99 | * this. | |
100 | */ | |
cad707e2 | 101 | const bt_message *current_msg; |
14f28187 FD |
102 | |
103 | /* Timestamp in nanoseconds of the current message (current_msg). */ | |
104 | int64_t current_msg_ts_ns; | |
105 | ||
106 | /* Owned by this. */ | |
107 | uint8_t *buf; | |
108 | size_t buflen; | |
109 | ||
110 | /* Owned by this. */ | |
111 | GString *name; | |
4a39caef FD |
112 | |
113 | bool has_stream_hung_up; | |
14f28187 FD |
114 | }; |
115 | ||
116 | struct lttng_live_metadata { | |
c01594de | 117 | bt_logging_level log_level; |
2ece7dd0 | 118 | bt_self_component *self_comp; |
c01594de | 119 | |
14f28187 FD |
120 | uint64_t stream_id; |
121 | /* Weak reference. */ | |
122 | struct ctf_metadata_decoder *decoder; | |
14f28187 FD |
123 | }; |
124 | ||
76bbaebc FD |
125 | enum lttng_live_metadata_stream_state { |
126 | /* | |
127 | * The metadata needs to be updated. This is either because we just | |
128 | * created the trace and haven't asked yet, or the relay specifically | |
129 | * told us that new metadata is available. | |
130 | */ | |
131 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED, | |
132 | /* | |
133 | * The metadata was updated and the relay has not told us we need to | |
134 | * update it yet. | |
135 | */ | |
136 | LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED, | |
137 | /* | |
138 | * The relay has closed this metadata stream. We set this in reaction | |
139 | * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA | |
140 | * command to the relay. If this field is set, we have received all the | |
141 | * metadata that we are ever going to get for that metadata stream. | |
142 | */ | |
143 | LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED, | |
144 | }; | |
145 | ||
14f28187 | 146 | struct lttng_live_trace { |
c01594de | 147 | bt_logging_level log_level; |
2ece7dd0 | 148 | bt_self_component *self_comp; |
c01594de | 149 | |
14f28187 FD |
150 | /* Back reference to session. */ |
151 | struct lttng_live_session *session; | |
152 | ||
153 | /* ctf trace ID within the session. */ | |
154 | uint64_t id; | |
155 | ||
156 | /* Owned by this. */ | |
157 | bt_trace *trace; | |
158 | ||
159 | /* Weak reference. */ | |
160 | bt_trace_class *trace_class; | |
161 | ||
162 | struct lttng_live_metadata *metadata; | |
163 | ||
164 | const bt_clock_class *clock_class; | |
165 | ||
166 | /* Array of pointers to struct lttng_live_stream_iterator. */ | |
167 | /* Owned by this. */ | |
168 | GPtrArray *stream_iterators; | |
169 | ||
76bbaebc | 170 | enum lttng_live_metadata_stream_state metadata_stream_state; |
14f28187 FD |
171 | }; |
172 | ||
173 | struct lttng_live_session { | |
c01594de | 174 | bt_logging_level log_level; |
2ece7dd0 | 175 | bt_self_component *self_comp; |
c01594de | 176 | |
14f28187 FD |
177 | /* Weak reference. */ |
178 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
179 | ||
180 | /* Owned by this. */ | |
181 | GString *hostname; | |
182 | ||
183 | /* Owned by this. */ | |
184 | GString *session_name; | |
185 | ||
186 | uint64_t id; | |
187 | ||
188 | /* Array of pointers to struct lttng_live_trace. */ | |
189 | GPtrArray *traces; | |
190 | ||
191 | bool attached; | |
192 | bool new_streams_needed; | |
193 | bool lazy_stream_msg_init; | |
194 | bool closed; | |
195 | }; | |
196 | ||
197 | enum session_not_found_action { | |
198 | SESSION_NOT_FOUND_ACTION_CONTINUE, | |
199 | SESSION_NOT_FOUND_ACTION_FAIL, | |
200 | SESSION_NOT_FOUND_ACTION_END, | |
201 | }; | |
202 | ||
203 | /* | |
204 | * A component instance is an iterator on a single session. | |
205 | */ | |
206 | struct lttng_live_component { | |
c01594de PP |
207 | bt_logging_level log_level; |
208 | ||
14f28187 | 209 | /* Weak reference. */ |
2ece7dd0 | 210 | bt_self_component *self_comp; |
14f28187 FD |
211 | |
212 | struct { | |
213 | GString *url; | |
214 | enum session_not_found_action sess_not_found_act; | |
215 | } params; | |
216 | ||
217 | size_t max_query_size; | |
218 | ||
219 | /* | |
220 | * Keeps track of whether the downstream component already has a | |
221 | * message iterator on this component. | |
222 | */ | |
223 | bool has_msg_iter; | |
224 | }; | |
225 | ||
226 | struct lttng_live_msg_iter { | |
c01594de | 227 | bt_logging_level log_level; |
2ece7dd0 | 228 | bt_self_component *self_comp; |
c01594de | 229 | |
14f28187 FD |
230 | /* Weak reference. */ |
231 | struct lttng_live_component *lttng_live_comp; | |
232 | ||
233 | /* Weak reference. */ | |
234 | bt_self_message_iterator *self_msg_iter; | |
235 | ||
236 | /* Owned by this. */ | |
237 | struct live_viewer_connection *viewer_connection; | |
238 | ||
239 | /* Array of pointers to struct lttng_live_session. */ | |
240 | GPtrArray *sessions; | |
241 | ||
242 | /* Number of live stream iterator this message iterator has.*/ | |
243 | uint64_t active_stream_iter; | |
244 | ||
245 | /* Timestamp in nanosecond of the last message sent downstream. */ | |
246 | int64_t last_msg_ts_ns; | |
f79c2d7a FD |
247 | |
248 | /* True if the iterator was interrupted. */ | |
249 | bool was_interrupted; | |
14f28187 FD |
250 | }; |
251 | ||
252 | enum lttng_live_iterator_status { | |
253 | /** Iterator state has progressed. Continue iteration immediately. */ | |
254 | LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3, | |
255 | /** No message available for now. Try again later. */ | |
256 | LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2, | |
257 | /** No more CTF_LTTNG_LIVEs to be delivered. */ | |
258 | LTTNG_LIVE_ITERATOR_STATUS_END = 1, | |
259 | /** No error, okay. */ | |
260 | LTTNG_LIVE_ITERATOR_STATUS_OK = 0, | |
261 | /** Invalid arguments. */ | |
262 | LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1, | |
263 | /** General error. */ | |
264 | LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2, | |
265 | /** Out of memory. */ | |
266 | LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3, | |
267 | /** Unsupported iterator feature. */ | |
268 | LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4, | |
269 | }; | |
270 | ||
21a9f056 | 271 | bt_component_class_initialize_method_status lttng_live_component_init( |
14f28187 | 272 | bt_self_component_source *self_comp, |
59225a3e | 273 | bt_self_component_source_configuration *config, |
14f28187 FD |
274 | const bt_value *params, void *init_method_data); |
275 | ||
d24d5663 | 276 | bt_component_class_query_method_status lttng_live_query( |
14f28187 | 277 | bt_self_component_class_source *comp_class, |
3c729b9a | 278 | bt_private_query_executor *priv_query_exec, |
14f28187 | 279 | const char *object, const bt_value *params, |
7c14d641 | 280 | void *method_data, const bt_value **result); |
14f28187 FD |
281 | |
282 | void lttng_live_component_finalize(bt_self_component_source *component); | |
283 | ||
a3f0c7db | 284 | bt_message_iterator_class_next_method_status lttng_live_msg_iter_next( |
14f28187 FD |
285 | bt_self_message_iterator *iterator, |
286 | bt_message_array_const msgs, uint64_t capacity, | |
287 | uint64_t *count); | |
288 | ||
a3f0c7db | 289 | bt_message_iterator_class_initialize_method_status lttng_live_msg_iter_init( |
14f28187 | 290 | bt_self_message_iterator *self_msg_it, |
8d8b141d | 291 | bt_self_message_iterator_configuration *config, |
14f28187 FD |
292 | bt_self_component_port_output *self_port); |
293 | ||
294 | void lttng_live_msg_iter_finalize(bt_self_message_iterator *it); | |
295 | ||
36e94ad6 | 296 | enum lttng_live_viewer_status lttng_live_session_attach( |
851de941 SM |
297 | struct lttng_live_session *session, |
298 | bt_self_message_iterator *self_msg_iter); | |
eee8e741 | 299 | |
36e94ad6 | 300 | enum lttng_live_viewer_status lttng_live_session_detach( |
f79c2d7a FD |
301 | struct lttng_live_session *session); |
302 | ||
36e94ad6 | 303 | enum lttng_live_iterator_status lttng_live_session_get_new_streams( |
851de941 SM |
304 | struct lttng_live_session *session, |
305 | bt_self_message_iterator *self_msg_iter); | |
14f28187 | 306 | |
36e94ad6 FD |
307 | struct lttng_live_trace *lttng_live_session_borrow_or_create_trace_by_id( |
308 | struct lttng_live_session *session, uint64_t trace_id); | |
309 | ||
14f28187 FD |
310 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, |
311 | uint64_t session_id, | |
312 | const char *hostname, | |
313 | const char *session_name); | |
314 | ||
c28512ab FD |
315 | /* |
316 | * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata. | |
317 | * If new metadata is received, the function writes it to the provided file | |
318 | * handle and updates the reply_len output parameter. This function should be | |
319 | * called in loop until _END status is received to ensure all metadata is | |
320 | * written to the file. | |
321 | */ | |
322 | enum lttng_live_get_one_metadata_status lttng_live_get_one_metadata_packet( | |
323 | struct lttng_live_trace *trace, FILE *fp, size_t *reply_len); | |
324 | ||
14f28187 FD |
325 | enum lttng_live_iterator_status lttng_live_get_next_index( |
326 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
327 | struct lttng_live_stream_iterator *stream, | |
328 | struct packet_index *index); | |
329 | ||
18a1979b | 330 | enum ctf_msg_iter_medium_status lttng_live_get_stream_bytes( |
14f28187 FD |
331 | struct lttng_live_msg_iter *lttng_live_msg_iter, |
332 | struct lttng_live_stream_iterator *stream, uint8_t *buf, | |
333 | uint64_t offset, uint64_t req_len, uint64_t *recv_len); | |
14f28187 | 334 | |
9b4f9b42 | 335 | bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter); |
14f28187 | 336 | |
34533ae0 FD |
337 | BT_HIDDEN |
338 | void lttng_live_stream_iterator_set_state( | |
339 | struct lttng_live_stream_iterator *stream_iter, | |
340 | enum lttng_live_stream_state new_state); | |
341 | ||
14f28187 | 342 | #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */ |