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