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