Commit | Line | Data |
---|---|---|
f3bc2010 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
f3bc2010 | 3 | * |
14f28187 | 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
f3bc2010 | 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7cdc2bab | 6 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc2010 | 7 | * |
0235b0db | 8 | * Babeltrace CTF LTTng-live Client Component |
f3bc2010 JG |
9 | */ |
10 | ||
c802cacb | 11 | #include <glib.h> |
14f28187 FD |
12 | #include <inttypes.h> |
13 | #include <unistd.h> | |
14 | ||
c802cacb SM |
15 | #include <babeltrace2/babeltrace.h> |
16 | ||
17 | #define BT_COMP_LOG_SELF_COMP self_comp | |
18 | #define BT_LOG_OUTPUT_LEVEL log_level | |
19 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE" | |
20 | #include "logging/comp-logging.h" | |
3c22a242 | 21 | |
578e048b | 22 | #include "common/assert.h" |
f3bc2010 | 23 | |
376fc2bd | 24 | #include "plugins/common/muxing/muxing.h" |
80aff5ef | 25 | #include "plugins/common/param-validation/param-validation.h" |
376fc2bd | 26 | |
087cd0f5 | 27 | #include "data-stream.hpp" |
087cd0f5 | 28 | #include "lttng-live.hpp" |
c802cacb | 29 | #include "metadata.hpp" |
7cdc2bab | 30 | |
4164020e SM |
31 | #define MAX_QUERY_SIZE (256 * 1024) |
32 | #define URL_PARAM "url" | |
33 | #define INPUTS_PARAM "inputs" | |
34 | #define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action" | |
35 | #define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue" | |
36 | #define SESS_NOT_FOUND_ACTION_FAIL_STR "fail" | |
37 | #define SESS_NOT_FOUND_ACTION_END_STR "end" | |
7cdc2bab | 38 | |
4164020e | 39 | #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ##__VA_ARGS__) |
7cdc2bab | 40 | |
4164020e | 41 | static const char *lttng_live_iterator_status_string(enum lttng_live_iterator_status status) |
14f28187 | 42 | { |
4164020e SM |
43 | switch (status) { |
44 | case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: | |
45 | return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE"; | |
46 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
47 | return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN"; | |
48 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
49 | return "LTTNG_LIVE_ITERATOR_STATUS_END"; | |
50 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
51 | return "LTTNG_LIVE_ITERATOR_STATUS_OK"; | |
52 | case LTTNG_LIVE_ITERATOR_STATUS_INVAL: | |
53 | return "LTTNG_LIVE_ITERATOR_STATUS_INVAL"; | |
54 | case LTTNG_LIVE_ITERATOR_STATUS_ERROR: | |
55 | return "LTTNG_LIVE_ITERATOR_STATUS_ERROR"; | |
56 | case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: | |
57 | return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM"; | |
58 | case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: | |
59 | return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED"; | |
60 | default: | |
61 | bt_common_abort(); | |
62 | } | |
14f28187 FD |
63 | } |
64 | ||
4164020e | 65 | static const char *lttng_live_stream_state_string(enum lttng_live_stream_state state) |
7cdc2bab | 66 | { |
4164020e SM |
67 | switch (state) { |
68 | case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA: | |
69 | return "ACTIVE_NO_DATA"; | |
70 | case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA: | |
71 | return "QUIESCENT_NO_DATA"; | |
72 | case LTTNG_LIVE_STREAM_QUIESCENT: | |
73 | return "QUIESCENT"; | |
74 | case LTTNG_LIVE_STREAM_ACTIVE_DATA: | |
75 | return "ACTIVE_DATA"; | |
76 | case LTTNG_LIVE_STREAM_EOF: | |
77 | return "EOF"; | |
78 | default: | |
79 | return "ERROR"; | |
80 | } | |
7cdc2bab | 81 | } |
7cdc2bab | 82 | |
34533ae0 | 83 | void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter, |
4164020e | 84 | enum lttng_live_stream_state new_state) |
34533ae0 | 85 | { |
4164020e SM |
86 | bt_self_component *self_comp = stream_iter->self_comp; |
87 | bt_logging_level log_level = stream_iter->log_level; | |
34533ae0 | 88 | |
4164020e SM |
89 | BT_COMP_LOGD("Setting live stream iterator state: viewer-stream-id=%" PRIu64 |
90 | ", old-state=%s, new-state=%s", | |
91 | stream_iter->viewer_stream_id, lttng_live_stream_state_string(stream_iter->state), | |
92 | lttng_live_stream_state_string(new_state)); | |
34533ae0 | 93 | |
4164020e | 94 | stream_iter->state = new_state; |
34533ae0 FD |
95 | } |
96 | ||
4164020e SM |
97 | #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \ |
98 | do { \ | |
99 | BT_COMP_LOGD("Live stream iterator state=%s, " \ | |
100 | "last-inact-ts-is-set=%d, last-inact-ts-value=%" PRId64 ", " \ | |
101 | "curr-inact-ts=%" PRId64, \ | |
102 | lttng_live_stream_state_string(live_stream_iter->state), \ | |
103 | live_stream_iter->last_inactivity_ts.is_set, \ | |
104 | live_stream_iter->last_inactivity_ts.value, \ | |
105 | live_stream_iter->current_inactivity_ts); \ | |
106 | } while (0); | |
6f79a7cf | 107 | |
9b4f9b42 | 108 | bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter) |
6f79a7cf | 109 | { |
4164020e | 110 | bool ret; |
6f79a7cf | 111 | |
4164020e SM |
112 | if (!msg_iter) { |
113 | ret = false; | |
114 | goto end; | |
115 | } | |
7cdc2bab | 116 | |
4164020e | 117 | ret = bt_self_message_iterator_is_interrupted(msg_iter->self_msg_iter); |
4bf0e537 | 118 | |
14f28187 | 119 | end: |
4164020e | 120 | return ret; |
7cdc2bab MD |
121 | } |
122 | ||
4164020e SM |
123 | static struct lttng_live_trace * |
124 | lttng_live_session_borrow_trace_by_id(struct lttng_live_session *session, uint64_t trace_id) | |
d3e4dcd8 | 125 | { |
4164020e SM |
126 | uint64_t trace_idx; |
127 | struct lttng_live_trace *ret_trace = NULL; | |
128 | ||
129 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
130 | struct lttng_live_trace *trace = | |
131 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
132 | if (trace->id == trace_id) { | |
133 | ret_trace = trace; | |
134 | goto end; | |
135 | } | |
136 | } | |
14f28187 FD |
137 | |
138 | end: | |
4164020e | 139 | return ret_trace; |
d3eb6e8f PP |
140 | } |
141 | ||
4164020e | 142 | static void lttng_live_destroy_trace(struct lttng_live_trace *trace) |
7cdc2bab | 143 | { |
4164020e SM |
144 | bt_logging_level log_level = trace->log_level; |
145 | bt_self_component *self_comp = trace->self_comp; | |
c01594de | 146 | |
4164020e | 147 | BT_COMP_LOGD("Destroying live trace: trace-id=%" PRIu64, trace->id); |
7cdc2bab | 148 | |
4164020e SM |
149 | BT_ASSERT(trace->stream_iterators); |
150 | g_ptr_array_free(trace->stream_iterators, TRUE); | |
5bd230f4 | 151 | |
4164020e SM |
152 | BT_TRACE_PUT_REF_AND_RESET(trace->trace); |
153 | BT_TRACE_CLASS_PUT_REF_AND_RESET(trace->trace_class); | |
5bd230f4 | 154 | |
4164020e SM |
155 | lttng_live_metadata_fini(trace); |
156 | g_free(trace); | |
7cdc2bab MD |
157 | } |
158 | ||
4164020e SM |
159 | static struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *session, |
160 | uint64_t trace_id) | |
7cdc2bab | 161 | { |
4164020e SM |
162 | struct lttng_live_trace *trace = NULL; |
163 | bt_logging_level log_level = session->log_level; | |
164 | bt_self_component *self_comp = session->self_comp; | |
165 | ||
166 | BT_COMP_LOGD("Creating live trace: " | |
167 | "session-id=%" PRIu64 ", trace-id=%" PRIu64, | |
168 | session->id, trace_id); | |
169 | trace = g_new0(struct lttng_live_trace, 1); | |
170 | if (!trace) { | |
171 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate live trace"); | |
172 | goto error; | |
173 | } | |
174 | trace->log_level = session->log_level; | |
175 | trace->self_comp = session->self_comp; | |
176 | trace->session = session; | |
177 | trace->id = trace_id; | |
178 | trace->trace_class = NULL; | |
179 | trace->trace = NULL; | |
180 | trace->stream_iterators = | |
181 | g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_stream_iterator_destroy); | |
182 | BT_ASSERT(trace->stream_iterators); | |
183 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; | |
184 | g_ptr_array_add(session->traces, trace); | |
185 | ||
186 | goto end; | |
7cdc2bab | 187 | error: |
4164020e SM |
188 | g_free(trace); |
189 | trace = NULL; | |
7cdc2bab | 190 | end: |
4164020e | 191 | return trace; |
7cdc2bab MD |
192 | } |
193 | ||
4164020e SM |
194 | struct lttng_live_trace * |
195 | lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session, | |
196 | uint64_t trace_id) | |
7cdc2bab | 197 | { |
4164020e | 198 | struct lttng_live_trace *trace; |
7cdc2bab | 199 | |
4164020e SM |
200 | trace = lttng_live_session_borrow_trace_by_id(session, trace_id); |
201 | if (trace) { | |
202 | goto end; | |
203 | } | |
7cdc2bab | 204 | |
4164020e SM |
205 | /* The session is the owner of the newly created trace. */ |
206 | trace = lttng_live_create_trace(session, trace_id); | |
7cdc2bab | 207 | |
14f28187 | 208 | end: |
4164020e | 209 | return trace; |
7cdc2bab MD |
210 | } |
211 | ||
4164020e SM |
212 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id, |
213 | const char *hostname, const char *session_name) | |
7cdc2bab | 214 | { |
4164020e SM |
215 | int ret = 0; |
216 | struct lttng_live_session *session; | |
217 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
218 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
219 | ||
220 | BT_COMP_LOGD("Adding live session: " | |
221 | "session-id=%" PRIu64 ", hostname=\"%s\" session-name=\"%s\"", | |
222 | session_id, hostname, session_name); | |
223 | ||
224 | session = g_new0(struct lttng_live_session, 1); | |
225 | if (!session) { | |
226 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate live session"); | |
227 | goto error; | |
228 | } | |
229 | ||
230 | session->log_level = lttng_live_msg_iter->log_level; | |
231 | session->self_comp = lttng_live_msg_iter->self_comp; | |
232 | session->id = session_id; | |
233 | session->traces = g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_destroy_trace); | |
234 | BT_ASSERT(session->traces); | |
235 | session->lttng_live_msg_iter = lttng_live_msg_iter; | |
236 | session->new_streams_needed = true; | |
237 | session->hostname = g_string_new(hostname); | |
238 | BT_ASSERT(session->hostname); | |
239 | ||
240 | session->session_name = g_string_new(session_name); | |
241 | BT_ASSERT(session->session_name); | |
242 | ||
243 | g_ptr_array_add(lttng_live_msg_iter->sessions, session); | |
244 | goto end; | |
7cdc2bab | 245 | error: |
4164020e SM |
246 | g_free(session); |
247 | ret = -1; | |
7cdc2bab | 248 | end: |
4164020e | 249 | return ret; |
7cdc2bab MD |
250 | } |
251 | ||
4164020e | 252 | static void lttng_live_destroy_session(struct lttng_live_session *session) |
7cdc2bab | 253 | { |
4164020e SM |
254 | bt_logging_level log_level; |
255 | bt_self_component *self_comp; | |
256 | ||
257 | if (!session) { | |
258 | goto end; | |
259 | } | |
260 | ||
261 | log_level = session->log_level; | |
262 | self_comp = session->self_comp; | |
263 | BT_COMP_LOGD("Destroying live session: " | |
264 | "session-id=%" PRIu64 ", session-name=\"%s\"", | |
265 | session->id, session->session_name->str); | |
266 | if (session->id != -1ULL) { | |
267 | if (lttng_live_session_detach(session)) { | |
268 | if (!lttng_live_graph_is_canceled(session->lttng_live_msg_iter)) { | |
269 | /* Old relayd cannot detach sessions. */ | |
270 | BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64, session->id); | |
271 | } | |
272 | } | |
273 | session->id = -1ULL; | |
274 | } | |
275 | ||
276 | if (session->traces) { | |
277 | g_ptr_array_free(session->traces, TRUE); | |
278 | } | |
279 | ||
280 | if (session->hostname) { | |
281 | g_string_free(session->hostname, TRUE); | |
282 | } | |
283 | if (session->session_name) { | |
284 | g_string_free(session->session_name, TRUE); | |
285 | } | |
286 | g_free(session); | |
14f28187 FD |
287 | |
288 | end: | |
4164020e | 289 | return; |
7cdc2bab MD |
290 | } |
291 | ||
4164020e | 292 | static void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter *lttng_live_msg_iter) |
7cdc2bab | 293 | { |
4164020e SM |
294 | if (!lttng_live_msg_iter) { |
295 | goto end; | |
296 | } | |
7cdc2bab | 297 | |
4164020e SM |
298 | if (lttng_live_msg_iter->sessions) { |
299 | g_ptr_array_free(lttng_live_msg_iter->sessions, TRUE); | |
300 | } | |
14f28187 | 301 | |
4164020e SM |
302 | if (lttng_live_msg_iter->viewer_connection) { |
303 | live_viewer_connection_destroy(lttng_live_msg_iter->viewer_connection); | |
304 | } | |
305 | BT_ASSERT(lttng_live_msg_iter->lttng_live_comp); | |
306 | BT_ASSERT(lttng_live_msg_iter->lttng_live_comp->has_msg_iter); | |
14f28187 | 307 | |
4164020e SM |
308 | /* All stream iterators must be destroyed at this point. */ |
309 | BT_ASSERT(lttng_live_msg_iter->active_stream_iter == 0); | |
310 | lttng_live_msg_iter->lttng_live_comp->has_msg_iter = false; | |
14f28187 | 311 | |
4164020e | 312 | g_free(lttng_live_msg_iter); |
14f28187 FD |
313 | |
314 | end: | |
4164020e | 315 | return; |
14f28187 FD |
316 | } |
317 | ||
14f28187 FD |
318 | void lttng_live_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
319 | { | |
4164020e | 320 | struct lttng_live_msg_iter *lttng_live_msg_iter; |
14f28187 | 321 | |
4164020e | 322 | BT_ASSERT(self_msg_iter); |
14f28187 | 323 | |
4164020e SM |
324 | lttng_live_msg_iter = |
325 | (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_iter); | |
326 | BT_ASSERT(lttng_live_msg_iter); | |
327 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); | |
7cdc2bab MD |
328 | } |
329 | ||
4164020e SM |
330 | static enum lttng_live_iterator_status |
331 | lttng_live_iterator_next_check_stream_state(struct lttng_live_stream_iterator *lttng_live_stream) | |
7cdc2bab | 332 | { |
4164020e SM |
333 | bt_logging_level log_level = lttng_live_stream->log_level; |
334 | bt_self_component *self_comp = lttng_live_stream->self_comp; | |
335 | ||
336 | switch (lttng_live_stream->state) { | |
337 | case LTTNG_LIVE_STREAM_QUIESCENT: | |
338 | case LTTNG_LIVE_STREAM_ACTIVE_DATA: | |
339 | break; | |
340 | case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA: | |
341 | /* Invalid state. */ | |
342 | BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\""); | |
343 | bt_common_abort(); | |
344 | case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA: | |
345 | /* Invalid state. */ | |
346 | BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\""); | |
347 | bt_common_abort(); | |
348 | case LTTNG_LIVE_STREAM_EOF: | |
349 | break; | |
350 | } | |
351 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
7cdc2bab MD |
352 | } |
353 | ||
354 | /* | |
f93afbf9 FD |
355 | * For active no data stream, fetch next index. As a result of that it can |
356 | * become either: | |
357 | * - quiescent: won't have events for a bit, | |
358 | * - have data: need to get that data and produce the event, | |
359 | * - have no data on this stream at this point: need to retry (AGAIN) or return | |
360 | * EOF. | |
7cdc2bab | 361 | */ |
4164020e SM |
362 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stream( |
363 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
364 | struct lttng_live_stream_iterator *lttng_live_stream) | |
7cdc2bab | 365 | { |
4164020e SM |
366 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
367 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
368 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
369 | enum lttng_live_stream_state orig_state = lttng_live_stream->state; | |
370 | struct packet_index index; | |
371 | ||
372 | if (lttng_live_stream->trace->metadata_stream_state == | |
373 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
374 | BT_COMP_LOGD( | |
375 | "Need to get an update for the metadata stream before proceeding further with this stream: " | |
376 | "stream-name=\"%s\"", | |
377 | lttng_live_stream->name->str); | |
378 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
379 | goto end; | |
380 | } | |
381 | ||
382 | if (lttng_live_stream->trace->session->new_streams_needed) { | |
383 | BT_COMP_LOGD( | |
384 | "Need to get an update of all streams before proceeding further with this stream: " | |
385 | "stream-name=\"%s\"", | |
386 | lttng_live_stream->name->str); | |
387 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
388 | goto end; | |
389 | } | |
390 | ||
391 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_NO_DATA && | |
392 | lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA) { | |
393 | goto end; | |
394 | } | |
395 | ret = lttng_live_get_next_index(lttng_live_msg_iter, lttng_live_stream, &index); | |
396 | if (ret != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
397 | goto end; | |
398 | } | |
399 | ||
400 | BT_ASSERT_DBG(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF); | |
401 | ||
402 | if (lttng_live_stream->state == LTTNG_LIVE_STREAM_QUIESCENT) { | |
403 | uint64_t last_inact_ts = lttng_live_stream->last_inactivity_ts.value, | |
404 | curr_inact_ts = lttng_live_stream->current_inactivity_ts; | |
405 | ||
406 | if (orig_state == LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA && last_inact_ts == curr_inact_ts) { | |
407 | /* | |
5cb27175 JG |
408 | * Because the stream is in the QUIESCENT_NO_DATA |
409 | * state, we can assert that the last_inactivity_ts was | |
410 | * set and can be safely used in the `if` above. | |
411 | */ | |
4164020e SM |
412 | BT_ASSERT(lttng_live_stream->last_inactivity_ts.is_set); |
413 | ||
414 | ret = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
415 | LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream); | |
416 | } else { | |
417 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
418 | } | |
419 | goto end; | |
420 | } | |
421 | ||
422 | lttng_live_stream->base_offset = index.offset; | |
423 | lttng_live_stream->offset = index.offset; | |
424 | lttng_live_stream->len = index.packet_size / CHAR_BIT; | |
425 | ||
426 | BT_COMP_LOGD("Setting live stream reading info: stream-name=\"%s\", " | |
427 | "viewer-stream-id=%" PRIu64 ", stream-base-offset=%" PRIu64 | |
428 | ", stream-offset=%" PRIu64 ", stream-len=%" PRIu64, | |
429 | lttng_live_stream->name->str, lttng_live_stream->viewer_stream_id, | |
430 | lttng_live_stream->base_offset, lttng_live_stream->offset, lttng_live_stream->len); | |
f93afbf9 | 431 | |
7cdc2bab | 432 | end: |
4164020e SM |
433 | if (ret == LTTNG_LIVE_ITERATOR_STATUS_OK) { |
434 | ret = lttng_live_iterator_next_check_stream_state(lttng_live_stream); | |
435 | } | |
436 | return ret; | |
7cdc2bab MD |
437 | } |
438 | ||
439 | /* | |
14f28187 FD |
440 | * Creation of the message requires the ctf trace class to be created |
441 | * beforehand, but the live protocol gives us all streams (including metadata) | |
442 | * at once. So we split it in three steps: getting streams, getting metadata | |
443 | * (which creates the ctf trace class), and then creating the per-stream | |
444 | * messages. | |
7cdc2bab | 445 | */ |
4164020e SM |
446 | static enum lttng_live_iterator_status |
447 | lttng_live_get_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
448 | struct lttng_live_session *session) | |
7cdc2bab | 449 | { |
4164020e SM |
450 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
451 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
452 | enum lttng_live_iterator_status status; | |
453 | uint64_t trace_idx; | |
454 | ||
455 | if (!session->attached) { | |
456 | BT_COMP_LOGD("Attach to session: session-id=%" PRIu64, session->id); | |
457 | enum lttng_live_viewer_status attach_status = | |
458 | lttng_live_session_attach(session, lttng_live_msg_iter->self_msg_iter); | |
459 | if (attach_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
460 | if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { | |
461 | /* | |
462 | * Clear any causes appended in | |
463 | * `lttng_live_attach_session()` as we want to | |
464 | * return gracefully since the graph was | |
465 | * cancelled. | |
466 | */ | |
467 | bt_current_thread_clear_error(); | |
468 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
469 | } else { | |
470 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
471 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error attaching to LTTng live session"); | |
472 | } | |
473 | goto end; | |
474 | } | |
475 | } | |
476 | ||
a4f118a3 | 477 | BT_COMP_LOGD("Updating all data streams: " |
4164020e SM |
478 | "session-id=%" PRIu64 ", session-name=\"%s\"", |
479 | session->id, session->session_name->str); | |
480 | ||
481 | status = lttng_live_session_get_new_streams(session, lttng_live_msg_iter->self_msg_iter); | |
a4f118a3 FD |
482 | switch (status) { |
483 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
484 | break; | |
485 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
486 | /* | |
487 | * We received a `_END` from the `_get_new_streams()` function, | |
488 | * which means no more data will ever be received from the data | |
489 | * streams of this session. But it's possible that the metadata | |
490 | * is incomplete. | |
491 | * The live protocol guarantees that we receive all the | |
492 | * metadata needed before we receive data streams needing it. | |
493 | * But it's possible to receive metadata NOT needed by | |
494 | * data streams after the session was closed. For example, this | |
495 | * could happen if a new event is registered and the session is | |
496 | * stopped before any tracepoint for that event is actually | |
497 | * fired. | |
498 | */ | |
499 | BT_COMP_LOGD( | |
500 | "Updating streams returned _END status. Override status to _OK in order fetch any remaining metadata:" | |
501 | "session-id=%" PRIu64 ", session-name=\"%s\"", | |
502 | session->id, session->session_name->str); | |
503 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
504 | break; | |
505 | default: | |
4164020e SM |
506 | goto end; |
507 | } | |
508 | ||
a4f118a3 FD |
509 | BT_COMP_LOGD("Updating metadata stream for session: " |
510 | "session-id=%" PRIu64 ", session-name=\"%s\"", | |
511 | session->id, session->session_name->str); | |
512 | ||
4164020e SM |
513 | trace_idx = 0; |
514 | while (trace_idx < session->traces->len) { | |
515 | struct lttng_live_trace *trace = | |
516 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
517 | ||
518 | status = lttng_live_metadata_update(trace); | |
519 | switch (status) { | |
520 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
521 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
522 | trace_idx++; | |
523 | break; | |
524 | case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: | |
525 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
526 | goto end; | |
527 | default: | |
528 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
529 | "Error updating trace metadata: " | |
530 | "stream-iter-status=%s, trace-id=%" PRIu64, | |
531 | lttng_live_iterator_status_string(status), trace->id); | |
532 | goto end; | |
533 | } | |
534 | } | |
535 | ||
536 | /* | |
537 | * Now that we have the metadata we can initialize the downstream | |
538 | * iterator. | |
539 | */ | |
540 | status = lttng_live_lazy_msg_init(session, lttng_live_msg_iter->self_msg_iter); | |
14f28187 FD |
541 | |
542 | end: | |
4164020e | 543 | return status; |
7cdc2bab MD |
544 | } |
545 | ||
4164020e SM |
546 | static void |
547 | lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) | |
7cdc2bab | 548 | { |
4164020e SM |
549 | uint64_t session_idx, trace_idx; |
550 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
551 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
552 | ||
553 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
554 | struct lttng_live_session *session = | |
555 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
556 | BT_COMP_LOGD("Force marking session as needing new streams: " | |
557 | "session-id=%" PRIu64, | |
558 | session->id); | |
559 | session->new_streams_needed = true; | |
560 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
561 | struct lttng_live_trace *trace = | |
562 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
563 | BT_COMP_LOGD("Force marking trace metadata state as needing an update: " | |
564 | "session-id=%" PRIu64 ", trace-id=%" PRIu64, | |
565 | session->id, trace->id); | |
566 | ||
567 | BT_ASSERT(trace->metadata_stream_state != LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED); | |
568 | ||
569 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; | |
570 | } | |
571 | } | |
7cdc2bab MD |
572 | } |
573 | ||
4164020e SM |
574 | static enum lttng_live_iterator_status |
575 | lttng_live_iterator_handle_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) | |
7cdc2bab | 576 | { |
4164020e SM |
577 | enum lttng_live_iterator_status status; |
578 | enum lttng_live_viewer_status viewer_status; | |
579 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
580 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
581 | uint64_t session_idx = 0, nr_sessions_opened = 0; | |
582 | struct lttng_live_session *session; | |
583 | enum session_not_found_action sess_not_found_act = | |
584 | lttng_live_msg_iter->lttng_live_comp->params.sess_not_found_act; | |
585 | ||
586 | BT_COMP_LOGD("Update data and metadata of all sessions: " | |
587 | "live-msg-iter-addr=%p", | |
588 | lttng_live_msg_iter); | |
589 | /* | |
590 | * In a remotely distant future, we could add a "new | |
591 | * session" flag to the protocol, which would tell us that we | |
592 | * need to query for new sessions even though we have sessions | |
593 | * currently ongoing. | |
594 | */ | |
595 | if (lttng_live_msg_iter->sessions->len == 0) { | |
596 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
597 | BT_COMP_LOGD( | |
598 | "No session found. Exiting in accordance with the `session-not-found-action` parameter"); | |
599 | status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
600 | goto end; | |
601 | } else { | |
602 | BT_COMP_LOGD( | |
603 | "No session found. Try creating a new one in accordance with the `session-not-found-action` parameter"); | |
604 | /* | |
605 | * Retry to create a viewer session for the requested | |
606 | * session name. | |
607 | */ | |
608 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); | |
609 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
610 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
611 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
612 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
613 | "Error creating LTTng live viewer session"); | |
614 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
615 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
616 | } else { | |
617 | bt_common_abort(); | |
618 | } | |
619 | goto end; | |
620 | } | |
621 | } | |
622 | } | |
623 | ||
624 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
625 | session = | |
626 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
627 | status = lttng_live_get_session(lttng_live_msg_iter, session); | |
628 | switch (status) { | |
629 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
4164020e | 630 | case LTTNG_LIVE_ITERATOR_STATUS_END: |
a4f118a3 FD |
631 | /* |
632 | * A session returned `_END`. Other sessions may still | |
633 | * be active so we override the status and continue | |
634 | * looping if needed. | |
635 | */ | |
4164020e SM |
636 | break; |
637 | default: | |
638 | goto end; | |
639 | } | |
640 | if (!session->closed) { | |
641 | nr_sessions_opened++; | |
642 | } | |
643 | } | |
644 | ||
645 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE && nr_sessions_opened == 0) { | |
646 | status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
647 | } else { | |
648 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
649 | } | |
f79c2d7a FD |
650 | |
651 | end: | |
4164020e | 652 | return status; |
7cdc2bab MD |
653 | } |
654 | ||
4164020e SM |
655 | static enum lttng_live_iterator_status |
656 | emit_inactivity_message(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
657 | struct lttng_live_stream_iterator *stream_iter, const bt_message **message, | |
658 | uint64_t timestamp) | |
7cdc2bab | 659 | { |
4164020e SM |
660 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
661 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
662 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
663 | bt_message *msg = NULL; | |
664 | ||
665 | BT_ASSERT(stream_iter->trace->clock_class); | |
666 | ||
667 | BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64 | |
668 | ", viewer-stream-id=%" PRIu64 ", timestamp=%" PRIu64, | |
669 | stream_iter->ctf_stream_class_id.value, stream_iter->viewer_stream_id, timestamp); | |
670 | ||
671 | msg = bt_message_message_iterator_inactivity_create(lttng_live_msg_iter->self_msg_iter, | |
672 | stream_iter->trace->clock_class, timestamp); | |
673 | if (!msg) { | |
674 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error emitting message iterator inactivity message"); | |
675 | goto error; | |
676 | } | |
677 | ||
678 | *message = msg; | |
7cdc2bab | 679 | end: |
4164020e | 680 | return ret; |
7cdc2bab MD |
681 | |
682 | error: | |
4164020e SM |
683 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
684 | bt_message_put_ref(msg); | |
685 | goto end; | |
7cdc2bab MD |
686 | } |
687 | ||
4164020e SM |
688 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream( |
689 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
690 | struct lttng_live_stream_iterator *lttng_live_stream, const bt_message **message) | |
7cdc2bab | 691 | { |
4164020e SM |
692 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
693 | ||
694 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) { | |
695 | return LTTNG_LIVE_ITERATOR_STATUS_OK; | |
696 | } | |
697 | ||
698 | /* | |
699 | * Check if we already sent an inactivty message downstream for this | |
700 | * `current_inactivity_ts` value. | |
701 | */ | |
702 | if (lttng_live_stream->last_inactivity_ts.is_set && | |
703 | lttng_live_stream->current_inactivity_ts == lttng_live_stream->last_inactivity_ts.value) { | |
704 | lttng_live_stream_iterator_set_state(lttng_live_stream, | |
705 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA); | |
706 | ||
707 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
708 | goto end; | |
709 | } | |
710 | ||
711 | ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream, message, | |
712 | lttng_live_stream->current_inactivity_ts); | |
713 | ||
714 | lttng_live_stream->last_inactivity_ts.value = lttng_live_stream->current_inactivity_ts; | |
715 | lttng_live_stream->last_inactivity_ts.is_set = true; | |
14f28187 | 716 | end: |
4164020e | 717 | return ret; |
14f28187 FD |
718 | } |
719 | ||
7d91f1ac | 720 | static int live_get_msg_ts_ns(struct lttng_live_msg_iter *lttng_live_msg_iter, |
4164020e | 721 | const bt_message *msg, int64_t last_msg_ts_ns, int64_t *ts_ns) |
14f28187 | 722 | { |
4164020e SM |
723 | const bt_clock_snapshot *clock_snapshot = NULL; |
724 | int ret = 0; | |
725 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
726 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
727 | ||
728 | BT_ASSERT_DBG(msg); | |
729 | BT_ASSERT_DBG(ts_ns); | |
730 | ||
731 | BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, " | |
732 | "last-msg-ts=%" PRId64, | |
733 | lttng_live_msg_iter, msg, last_msg_ts_ns); | |
734 | ||
735 | switch (bt_message_get_type(msg)) { | |
736 | case BT_MESSAGE_TYPE_EVENT: | |
4164020e SM |
737 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(msg); |
738 | break; | |
739 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
4164020e SM |
740 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(msg); |
741 | break; | |
742 | case BT_MESSAGE_TYPE_PACKET_END: | |
4164020e SM |
743 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(msg); |
744 | break; | |
745 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
4164020e SM |
746 | clock_snapshot = |
747 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msg); | |
748 | break; | |
749 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
4164020e SM |
750 | clock_snapshot = |
751 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msg); | |
752 | break; | |
753 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
754 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msg); | |
755 | break; | |
756 | default: | |
757 | /* All the other messages have a higher priority */ | |
758 | BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp."); | |
759 | *ts_ns = last_msg_ts_ns; | |
760 | goto end; | |
761 | } | |
762 | ||
4164020e SM |
763 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
764 | if (ret) { | |
765 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
766 | "Cannot get nanoseconds from Epoch of clock snapshot: " | |
767 | "clock-snapshot-addr=%p", | |
768 | clock_snapshot); | |
769 | goto error; | |
770 | } | |
771 | ||
772 | goto end; | |
14f28187 | 773 | |
14f28187 | 774 | error: |
4164020e | 775 | ret = -1; |
7cdc2bab | 776 | |
7cdc2bab | 777 | end: |
4164020e SM |
778 | if (ret == 0) { |
779 | BT_COMP_LOGD("Found message's timestamp: " | |
780 | "iter-data-addr=%p, msg-addr=%p, " | |
781 | "last-msg-ts=%" PRId64 ", ts=%" PRId64, | |
782 | lttng_live_msg_iter, msg, last_msg_ts_ns, *ts_ns); | |
783 | } | |
784 | ||
785 | return ret; | |
7cdc2bab MD |
786 | } |
787 | ||
4164020e SM |
788 | static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_stream( |
789 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
790 | struct lttng_live_stream_iterator *lttng_live_stream, const bt_message **message) | |
7cdc2bab | 791 | { |
4164020e SM |
792 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
793 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
794 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
795 | enum ctf_msg_iter_status status; | |
796 | uint64_t session_idx, trace_idx; | |
797 | ||
798 | for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { | |
799 | struct lttng_live_session *session = | |
800 | (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); | |
801 | ||
802 | if (session->new_streams_needed) { | |
803 | BT_COMP_LOGD("Need an update for streams: " | |
804 | "session-id=%" PRIu64, | |
805 | session->id); | |
806 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
807 | goto end; | |
808 | } | |
809 | for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) { | |
810 | struct lttng_live_trace *trace = | |
811 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
812 | if (trace->metadata_stream_state == LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
813 | BT_COMP_LOGD("Need an update for metadata stream: " | |
814 | "session-id=%" PRIu64 ", trace-id=%" PRIu64, | |
815 | session->id, trace->id); | |
816 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
817 | goto end; | |
818 | } | |
819 | } | |
820 | } | |
821 | ||
822 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) { | |
823 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
824 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
825 | "Invalid state of live stream iterator" | |
826 | "stream-iter-status=%s", | |
827 | lttng_live_stream_state_string(lttng_live_stream->state)); | |
828 | goto end; | |
829 | } | |
830 | ||
831 | status = ctf_msg_iter_get_next_message(lttng_live_stream->msg_iter, message); | |
832 | switch (status) { | |
833 | case CTF_MSG_ITER_STATUS_EOF: | |
834 | ret = LTTNG_LIVE_ITERATOR_STATUS_END; | |
835 | break; | |
836 | case CTF_MSG_ITER_STATUS_OK: | |
837 | ret = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
838 | break; | |
839 | case CTF_MSG_ITER_STATUS_AGAIN: | |
840 | /* | |
841 | * Continue immediately (end of packet). The next | |
842 | * get_index may return AGAIN to delay the following | |
843 | * attempt. | |
844 | */ | |
845 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; | |
846 | break; | |
847 | case CTF_MSG_ITER_STATUS_ERROR: | |
848 | default: | |
849 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
850 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
851 | "CTF message iterator failed to get next message: " | |
852 | "msg-iter=%p, msg-iter-status=%s", | |
853 | lttng_live_stream->msg_iter, ctf_msg_iter_status_string(status)); | |
854 | break; | |
855 | } | |
14f28187 FD |
856 | |
857 | end: | |
4164020e | 858 | return ret; |
7cdc2bab MD |
859 | } |
860 | ||
4164020e SM |
861 | static enum lttng_live_iterator_status |
862 | lttng_live_iterator_close_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
863 | struct lttng_live_stream_iterator *stream_iter, | |
864 | const bt_message **curr_msg) | |
4a39caef | 865 | { |
4164020e SM |
866 | enum lttng_live_iterator_status live_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
867 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
868 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
869 | ||
870 | BT_COMP_LOGD("Closing live stream iterator: stream-name=\"%s\", " | |
871 | "viewer-stream-id=%" PRIu64, | |
872 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
873 | ||
874 | /* | |
875 | * The viewer has hung up on us so we are closing the stream. The | |
876 | * `ctf_msg_iter` should simply realize that it needs to close the | |
877 | * stream properly by emitting the necessary stream end message. | |
878 | */ | |
879 | enum ctf_msg_iter_status status = | |
880 | ctf_msg_iter_get_next_message(stream_iter->msg_iter, curr_msg); | |
881 | ||
882 | if (status == CTF_MSG_ITER_STATUS_ERROR) { | |
883 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
884 | "Error getting the next message from CTF message iterator"); | |
885 | live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
886 | goto end; | |
887 | } else if (status == CTF_MSG_ITER_STATUS_EOF) { | |
888 | BT_COMP_LOGI("Reached the end of the live stream iterator."); | |
889 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
890 | goto end; | |
891 | } | |
892 | ||
893 | BT_ASSERT(status == CTF_MSG_ITER_STATUS_OK); | |
4a39caef FD |
894 | |
895 | end: | |
4164020e | 896 | return live_status; |
4a39caef FD |
897 | } |
898 | ||
7cdc2bab MD |
899 | /* |
900 | * helper function: | |
901 | * handle_no_data_streams() | |
902 | * retry: | |
903 | * - for each ACTIVE_NO_DATA stream: | |
904 | * - query relayd for stream data, or quiescence info. | |
905 | * - if need metadata, get metadata, goto retry. | |
906 | * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry | |
907 | * - if quiescent, move to QUIESCENT streams | |
908 | * - if fetched data, move to ACTIVE_DATA streams | |
909 | * (at this point each stream either has data, or is quiescent) | |
910 | * | |
911 | * | |
912 | * iterator_next: | |
913 | * handle_new_streams_and_metadata() | |
914 | * - query relayd for known streams, add them as ACTIVE_NO_DATA | |
915 | * - query relayd for metadata | |
916 | * | |
917 | * call handle_active_no_data_streams() | |
918 | * | |
919 | * handle_quiescent_streams() | |
920 | * - if at least one stream is ACTIVE_DATA: | |
921 | * - peek stream event with lowest timestamp -> next_ts | |
922 | * - for each quiescent stream | |
923 | * - if next_ts >= quiescent end | |
924 | * - set state to ACTIVE_NO_DATA | |
925 | * - else | |
926 | * - for each quiescent stream | |
927 | * - set state to ACTIVE_NO_DATA | |
928 | * | |
929 | * call handle_active_no_data_streams() | |
930 | * | |
931 | * handle_active_data_streams() | |
932 | * - if at least one stream is ACTIVE_DATA: | |
933 | * - get stream event with lowest timestamp from heap | |
d6e69534 | 934 | * - make that stream event the current message. |
7cdc2bab MD |
935 | * - move this stream heap position to its next event |
936 | * - if we need to fetch data from relayd, move | |
937 | * stream to ACTIVE_NO_DATA. | |
938 | * - return OK | |
939 | * - return AGAIN | |
940 | * | |
941 | * end criterion: ctrl-c on client. If relayd exits or the session | |
942 | * closes on the relay daemon side, we keep on waiting for streams. | |
943 | * Eventually handle --end timestamp (also an end criterion). | |
944 | * | |
945 | * When disconnected from relayd: try to re-connect endlessly. | |
946 | */ | |
4164020e SM |
947 | static enum lttng_live_iterator_status |
948 | lttng_live_iterator_next_msg_on_stream(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
949 | struct lttng_live_stream_iterator *stream_iter, | |
950 | const bt_message **curr_msg) | |
7cdc2bab | 951 | { |
4164020e SM |
952 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
953 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
954 | enum lttng_live_iterator_status live_status; | |
955 | ||
956 | BT_COMP_LOGD("Advancing live stream iterator until next message if possible: " | |
957 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
958 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
959 | ||
960 | if (stream_iter->has_stream_hung_up) { | |
961 | /* | |
962 | * The stream has hung up and the stream was properly closed | |
963 | * during the last call to the current function. Return _END | |
964 | * status now so that this stream iterator is removed for the | |
965 | * stream iterator list. | |
966 | */ | |
967 | live_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
968 | goto end; | |
969 | } | |
4a39caef | 970 | |
7cdc2bab | 971 | retry: |
4164020e SM |
972 | LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter); |
973 | ||
974 | /* | |
975 | * Make sure we have the most recent metadata and possibly some new | |
976 | * streams. | |
977 | */ | |
978 | live_status = lttng_live_iterator_handle_new_streams_and_metadata(lttng_live_msg_iter); | |
979 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
980 | goto end; | |
981 | } | |
982 | ||
983 | live_status = | |
984 | lttng_live_iterator_next_handle_one_no_data_stream(lttng_live_msg_iter, stream_iter); | |
985 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
986 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
987 | /* | |
988 | * We overwrite `live_status` since `curr_msg` is | |
989 | * likely set to a valid message in this function. | |
990 | */ | |
991 | live_status = | |
992 | lttng_live_iterator_close_stream(lttng_live_msg_iter, stream_iter, curr_msg); | |
993 | } | |
994 | goto end; | |
995 | } | |
996 | ||
997 | live_status = lttng_live_iterator_next_handle_one_quiescent_stream(lttng_live_msg_iter, | |
998 | stream_iter, curr_msg); | |
999 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1000 | BT_ASSERT(!*curr_msg); | |
1001 | goto end; | |
1002 | } | |
1003 | if (*curr_msg) { | |
1004 | goto end; | |
1005 | } | |
1006 | live_status = lttng_live_iterator_next_handle_one_active_data_stream(lttng_live_msg_iter, | |
1007 | stream_iter, curr_msg); | |
1008 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1009 | BT_ASSERT(!*curr_msg); | |
1010 | } | |
7cdc2bab MD |
1011 | |
1012 | end: | |
4164020e SM |
1013 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_CONTINUE) { |
1014 | BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams"); | |
1015 | goto retry; | |
1016 | } | |
14f28187 | 1017 | |
4164020e SM |
1018 | BT_COMP_LOGD("Returning from advancing live stream iterator: status=%s, " |
1019 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
1020 | lttng_live_iterator_status_string(live_status), stream_iter->name->str, | |
1021 | stream_iter->viewer_stream_id); | |
f93afbf9 | 1022 | |
4164020e | 1023 | return live_status; |
7cdc2bab MD |
1024 | } |
1025 | ||
4164020e | 1026 | static bool is_discarded_packet_or_event_message(const bt_message *msg) |
8ec4d5ff | 1027 | { |
4164020e | 1028 | const enum bt_message_type msg_type = bt_message_get_type(msg); |
8ec4d5ff | 1029 | |
4164020e SM |
1030 | return msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS || |
1031 | msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS; | |
8ec4d5ff JG |
1032 | } |
1033 | ||
4164020e SM |
1034 | static enum lttng_live_iterator_status |
1035 | adjust_discarded_packets_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
1036 | const bt_message *msg_in, bt_message **msg_out, | |
1037 | uint64_t new_begin_ts) | |
8ec4d5ff | 1038 | { |
4164020e SM |
1039 | enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1040 | enum bt_property_availability availability; | |
1041 | const bt_clock_snapshot *clock_snapshot; | |
1042 | uint64_t end_ts; | |
1043 | uint64_t count; | |
1044 | ||
1045 | clock_snapshot = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in); | |
1046 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
1047 | ||
1048 | availability = bt_message_discarded_packets_get_count(msg_in, &count); | |
1049 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
1050 | ||
1051 | *msg_out = bt_message_discarded_packets_create_with_default_clock_snapshots( | |
1052 | iter, stream, new_begin_ts, end_ts); | |
1053 | if (!*msg_out) { | |
1054 | status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
1055 | goto end; | |
1056 | } | |
1057 | ||
1058 | bt_message_discarded_packets_set_count(*msg_out, count); | |
8ec4d5ff | 1059 | end: |
4164020e | 1060 | return status; |
8ec4d5ff JG |
1061 | } |
1062 | ||
4164020e SM |
1063 | static enum lttng_live_iterator_status |
1064 | adjust_discarded_events_message(bt_self_message_iterator *iter, const bt_stream *stream, | |
1065 | const bt_message *msg_in, bt_message **msg_out, | |
1066 | uint64_t new_begin_ts) | |
8ec4d5ff | 1067 | { |
4164020e SM |
1068 | enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1069 | enum bt_property_availability availability; | |
1070 | const bt_clock_snapshot *clock_snapshot; | |
1071 | uint64_t end_ts; | |
1072 | uint64_t count; | |
1073 | ||
1074 | clock_snapshot = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in); | |
1075 | end_ts = bt_clock_snapshot_get_value(clock_snapshot); | |
1076 | ||
1077 | availability = bt_message_discarded_events_get_count(msg_in, &count); | |
1078 | BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); | |
1079 | ||
1080 | *msg_out = bt_message_discarded_events_create_with_default_clock_snapshots( | |
1081 | iter, stream, new_begin_ts, end_ts); | |
1082 | if (!*msg_out) { | |
1083 | status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; | |
1084 | goto end; | |
1085 | } | |
1086 | ||
1087 | bt_message_discarded_events_set_count(*msg_out, count); | |
8ec4d5ff | 1088 | end: |
4164020e | 1089 | return status; |
8ec4d5ff JG |
1090 | } |
1091 | ||
4164020e SM |
1092 | static enum lttng_live_iterator_status |
1093 | handle_late_message(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1094 | struct lttng_live_stream_iterator *stream_iter, int64_t late_msg_ts_ns, | |
1095 | const bt_message *late_msg) | |
285951be | 1096 | { |
4164020e SM |
1097 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
1098 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
1099 | const bt_clock_class *clock_class; | |
1100 | const bt_stream_class *stream_class; | |
1101 | enum bt_clock_class_cycles_to_ns_from_origin_status ts_ns_status; | |
1102 | int64_t last_inactivity_ts_ns; | |
1103 | enum lttng_live_iterator_status stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1104 | enum lttng_live_iterator_status adjust_status; | |
1105 | bt_message *adjusted_message; | |
1106 | ||
1107 | /* | |
1108 | * The timestamp of the current message is before the last message sent | |
1109 | * by this component. We CANNOT send it as is. | |
1110 | * | |
1111 | * The only expected scenario in which that could happen is the | |
1112 | * following, everything else is a bug in this component, relay deamon, | |
1113 | * or CTF parser. | |
1114 | * | |
1115 | * Expected scenario: The CTF message iterator emitted discarded | |
1116 | * packets and discarded events with synthesized beginning and end | |
1117 | * timestamps from the bounds of the last known packet and the newly | |
1118 | * decoded packet header. The CTF message iterator is not aware of | |
1119 | * stream inactivity beacons. Hence, we have to adjust the beginning | |
1120 | * timestamp of those types of messages if a stream signalled its | |
1121 | * inactivity up until _after_ the last known packet's beginning | |
1122 | * timestamp. | |
1123 | * | |
1124 | * Otherwise, the monotonicity guarantee of message timestamps would | |
1125 | * not be preserved. | |
1126 | * | |
1127 | * In short, the only scenario in which it's okay and fixable to | |
1128 | * received a late message is when: | |
1129 | * 1. the late message is a discarded packets or discarded events | |
1130 | * message, | |
1131 | * 2. this stream produced an inactivity message downstream, and | |
1132 | * 3. the timestamp of the late message is within the inactivity | |
1133 | * timespan we sent downstream through the inactivity message. | |
1134 | */ | |
1135 | ||
1136 | BT_COMP_LOGD("Handling late message on live stream iterator: " | |
1137 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
1138 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
1139 | ||
1140 | if (!stream_iter->last_inactivity_ts.is_set) { | |
1141 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Invalid live stream state: " | |
1142 | "have a late message when no inactivity message " | |
1143 | "was ever sent for that stream."); | |
1144 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1145 | goto end; | |
1146 | } | |
1147 | ||
1148 | if (!is_discarded_packet_or_event_message(late_msg)) { | |
1149 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1150 | "Invalid live stream state: " | |
1151 | "have a late message that is not a packet discarded or " | |
1152 | "event discarded message: late-msg-type=%s", | |
1153 | bt_common_message_type_string(bt_message_get_type(late_msg))); | |
1154 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1155 | goto end; | |
1156 | } | |
1157 | ||
1158 | stream_class = bt_stream_borrow_class_const(stream_iter->stream); | |
1159 | clock_class = bt_stream_class_borrow_default_clock_class_const(stream_class); | |
1160 | ||
1161 | ts_ns_status = bt_clock_class_cycles_to_ns_from_origin( | |
1162 | clock_class, stream_iter->last_inactivity_ts.value, &last_inactivity_ts_ns); | |
1163 | if (ts_ns_status != BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK) { | |
1164 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error converting last " | |
1165 | "inactivity message timestamp to nanoseconds"); | |
1166 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1167 | goto end; | |
1168 | } | |
1169 | ||
1170 | if (last_inactivity_ts_ns <= late_msg_ts_ns) { | |
1171 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1172 | "Invalid live stream state: " | |
1173 | "have a late message that is none included in a stream " | |
1174 | "inactivity timespan: last-inactivity-ts-ns=%" PRIu64 | |
1175 | "late-msg-ts-ns=%" PRIu64, | |
1176 | last_inactivity_ts_ns, late_msg_ts_ns); | |
1177 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1178 | goto end; | |
1179 | } | |
1180 | ||
1181 | /* | |
1182 | * We now know that it's okay for this message to be late, we can now | |
1183 | * adjust its timestamp to ensure monotonicity. | |
1184 | */ | |
1185 | BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, " | |
1186 | "msg-new-ts-ns=%" PRIu64, | |
1187 | bt_common_message_type_string(bt_message_get_type(late_msg)), | |
1188 | stream_iter->last_inactivity_ts.value); | |
1189 | switch (bt_message_get_type(late_msg)) { | |
1190 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1191 | adjust_status = adjust_discarded_events_message( | |
1192 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message, | |
1193 | stream_iter->last_inactivity_ts.value); | |
1194 | break; | |
1195 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1196 | adjust_status = adjust_discarded_packets_message( | |
1197 | lttng_live_msg_iter->self_msg_iter, stream_iter->stream, late_msg, &adjusted_message, | |
1198 | stream_iter->last_inactivity_ts.value); | |
1199 | break; | |
1200 | default: | |
1201 | bt_common_abort(); | |
1202 | } | |
1203 | ||
1204 | if (adjust_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1205 | stream_iter_status = adjust_status; | |
1206 | goto end; | |
1207 | } | |
1208 | ||
1209 | BT_ASSERT_DBG(adjusted_message); | |
1210 | stream_iter->current_msg = adjusted_message; | |
1211 | stream_iter->current_msg_ts_ns = last_inactivity_ts_ns; | |
1212 | bt_message_put_ref(late_msg); | |
cefd84f8 | 1213 | |
285951be | 1214 | end: |
4164020e | 1215 | return stream_iter_status; |
285951be FD |
1216 | } |
1217 | ||
4164020e SM |
1218 | static enum lttng_live_iterator_status |
1219 | next_stream_iterator_for_trace(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1220 | struct lttng_live_trace *live_trace, | |
1221 | struct lttng_live_stream_iterator **youngest_trace_stream_iter) | |
7cdc2bab | 1222 | { |
4164020e SM |
1223 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; |
1224 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
1225 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
1226 | enum lttng_live_iterator_status stream_iter_status; | |
1227 | ; | |
1228 | int64_t youngest_candidate_msg_ts = INT64_MAX; | |
1229 | uint64_t stream_iter_idx; | |
1230 | ||
1231 | BT_ASSERT_DBG(live_trace); | |
1232 | BT_ASSERT_DBG(live_trace->stream_iterators); | |
1233 | ||
1234 | BT_COMP_LOGD("Finding the next stream iterator for trace: " | |
1235 | "trace-id=%" PRIu64, | |
1236 | live_trace->id); | |
1237 | /* | |
1238 | * Update the current message of every stream iterators of this trace. | |
1239 | * The current msg of every stream must have a timestamp equal or | |
1240 | * larger than the last message returned by this iterator. We must | |
1241 | * ensure monotonicity. | |
1242 | */ | |
1243 | stream_iter_idx = 0; | |
1244 | while (stream_iter_idx < live_trace->stream_iterators->len) { | |
1245 | bool stream_iter_is_ended = false; | |
1246 | struct lttng_live_stream_iterator *stream_iter = | |
1247 | (lttng_live_stream_iterator *) g_ptr_array_index(live_trace->stream_iterators, | |
1248 | stream_iter_idx); | |
1249 | ||
1250 | /* | |
1251 | * If there is no current message for this stream, go fetch | |
1252 | * one. | |
1253 | */ | |
1254 | while (!stream_iter->current_msg) { | |
1255 | const bt_message *msg = NULL; | |
1256 | int64_t curr_msg_ts_ns = INT64_MAX; | |
1257 | ||
1258 | stream_iter_status = | |
1259 | lttng_live_iterator_next_msg_on_stream(lttng_live_msg_iter, stream_iter, &msg); | |
1260 | ||
1261 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1262 | stream_iter_is_ended = true; | |
1263 | break; | |
1264 | } | |
1265 | ||
1266 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1267 | goto end; | |
1268 | } | |
1269 | ||
1270 | BT_ASSERT_DBG(msg); | |
1271 | ||
1272 | BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s, " | |
1273 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
1274 | bt_common_message_type_string(bt_message_get_type(msg)), | |
1275 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
1276 | ||
1277 | /* | |
1278 | * Get the timestamp in nanoseconds from origin of this | |
1279 | * messsage. | |
1280 | */ | |
7d91f1ac SM |
1281 | live_get_msg_ts_ns(lttng_live_msg_iter, msg, lttng_live_msg_iter->last_msg_ts_ns, |
1282 | &curr_msg_ts_ns); | |
4164020e SM |
1283 | |
1284 | /* | |
1285 | * Check if the message of the current live stream | |
1286 | * iterator occurred at the exact same time or after the | |
1287 | * last message returned by this component's message | |
1288 | * iterator. If not, we need to handle it with care. | |
1289 | */ | |
1290 | if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) { | |
1291 | stream_iter->current_msg = msg; | |
1292 | stream_iter->current_msg_ts_ns = curr_msg_ts_ns; | |
1293 | } else { | |
1294 | /* | |
1295 | * We received a message from the past. This | |
1296 | * may be fixable but it can also be an error. | |
1297 | */ | |
1298 | stream_iter_status = | |
1299 | handle_late_message(lttng_live_msg_iter, stream_iter, curr_msg_ts_ns, msg); | |
1300 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1301 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1302 | "Late message could not be handled correctly: " | |
1303 | "lttng-live-msg-iter-addr=%p, " | |
1304 | "stream-name=\"%s\", " | |
1305 | "curr-msg-ts=%" PRId64 ", last-msg-ts=%" PRId64, | |
1306 | lttng_live_msg_iter, stream_iter->name->str, | |
1307 | curr_msg_ts_ns, lttng_live_msg_iter->last_msg_ts_ns); | |
1308 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1309 | goto end; | |
1310 | } | |
1311 | } | |
1312 | } | |
1313 | ||
1314 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1315 | ||
1316 | if (!stream_iter_is_ended) { | |
1317 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1318 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1319 | /* | |
1320 | * Update the current best candidate message | |
1321 | * for the stream iterator of this live trace | |
1322 | * to be forwarded downstream. | |
1323 | */ | |
1324 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1325 | youngest_candidate_stream_iter = stream_iter; | |
1326 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1327 | /* | |
1328 | * Order the messages in an arbitrary but | |
1329 | * deterministic way. | |
1330 | */ | |
1331 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); | |
1332 | int ret = common_muxing_compare_messages( | |
1333 | stream_iter->current_msg, youngest_candidate_stream_iter->current_msg); | |
1334 | if (ret < 0) { | |
1335 | /* | |
1336 | * The `youngest_candidate_stream_iter->current_msg` | |
1337 | * should go first. Update the next | |
1338 | * iterator and the current timestamp. | |
1339 | */ | |
1340 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1341 | youngest_candidate_stream_iter = stream_iter; | |
1342 | } else if (ret == 0) { | |
1343 | /* | |
1344 | * Unable to pick which one should go | |
1345 | * first. | |
1346 | */ | |
1347 | BT_COMP_LOGW( | |
1348 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1349 | "stream-iter-addr=%p" | |
1350 | "stream-iter-addr=%p", | |
1351 | stream_iter, youngest_candidate_stream_iter); | |
1352 | } | |
1353 | } | |
1354 | ||
1355 | stream_iter_idx++; | |
1356 | } else { | |
1357 | /* | |
1358 | * The live stream iterator has ended. That | |
1359 | * iterator is removed from the array, but | |
1360 | * there is no need to increment | |
1361 | * stream_iter_idx as | |
1362 | * g_ptr_array_remove_index_fast replaces the | |
1363 | * removed element with the array's last | |
1364 | * element. | |
1365 | */ | |
1366 | g_ptr_array_remove_index_fast(live_trace->stream_iterators, stream_iter_idx); | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | if (youngest_candidate_stream_iter) { | |
1371 | *youngest_trace_stream_iter = youngest_candidate_stream_iter; | |
1372 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1373 | } else { | |
1374 | /* | |
1375 | * The only case where we don't have a candidate for this trace | |
1376 | * is if we reached the end of all the iterators. | |
1377 | */ | |
1378 | BT_ASSERT(live_trace->stream_iterators->len == 0); | |
1379 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1380 | } | |
14f28187 FD |
1381 | |
1382 | end: | |
4164020e | 1383 | return stream_iter_status; |
14f28187 FD |
1384 | } |
1385 | ||
4164020e SM |
1386 | static enum lttng_live_iterator_status |
1387 | next_stream_iterator_for_session(struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1388 | struct lttng_live_session *session, | |
1389 | struct lttng_live_stream_iterator **youngest_session_stream_iter) | |
14f28187 | 1390 | { |
4164020e SM |
1391 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
1392 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
1393 | enum lttng_live_iterator_status stream_iter_status; | |
1394 | uint64_t trace_idx = 0; | |
1395 | int64_t youngest_candidate_msg_ts = INT64_MAX; | |
1396 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; | |
1397 | ||
1398 | BT_COMP_LOGD("Finding the next stream iterator for session: " | |
1399 | "session-id=%" PRIu64, | |
1400 | session->id); | |
1401 | /* | |
1402 | * Make sure we are attached to the session and look for new streams | |
1403 | * and metadata. | |
1404 | */ | |
1405 | stream_iter_status = lttng_live_get_session(lttng_live_msg_iter, session); | |
1406 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK && | |
1407 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_CONTINUE && | |
1408 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1409 | goto end; | |
1410 | } | |
1411 | ||
1412 | BT_ASSERT_DBG(session->traces); | |
1413 | ||
1414 | while (trace_idx < session->traces->len) { | |
1415 | bool trace_is_ended = false; | |
1416 | struct lttng_live_stream_iterator *stream_iter; | |
1417 | struct lttng_live_trace *trace = | |
1418 | (lttng_live_trace *) g_ptr_array_index(session->traces, trace_idx); | |
1419 | ||
1420 | stream_iter_status = | |
1421 | next_stream_iterator_for_trace(lttng_live_msg_iter, trace, &stream_iter); | |
1422 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1423 | /* | |
1424 | * All the live stream iterators for this trace are | |
1425 | * ENDed. Remove the trace from this session. | |
1426 | */ | |
1427 | trace_is_ended = true; | |
1428 | } else if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1429 | goto end; | |
1430 | } | |
1431 | ||
1432 | if (!trace_is_ended) { | |
1433 | BT_ASSERT_DBG(stream_iter); | |
1434 | ||
1435 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || | |
1436 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1437 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1438 | youngest_candidate_stream_iter = stream_iter; | |
1439 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
1440 | /* | |
1441 | * Order the messages in an arbitrary but | |
1442 | * deterministic way. | |
1443 | */ | |
1444 | int ret = common_muxing_compare_messages( | |
1445 | stream_iter->current_msg, youngest_candidate_stream_iter->current_msg); | |
1446 | if (ret < 0) { | |
1447 | /* | |
1448 | * The `youngest_candidate_stream_iter->current_msg` | |
1449 | * should go first. Update the next iterator | |
1450 | * and the current timestamp. | |
1451 | */ | |
1452 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1453 | youngest_candidate_stream_iter = stream_iter; | |
1454 | } else if (ret == 0) { | |
1455 | /* Unable to pick which one should go first. */ | |
1456 | BT_COMP_LOGW( | |
1457 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1458 | "stream-iter-addr=%p" | |
1459 | "stream-iter-addr=%p", | |
1460 | stream_iter, youngest_candidate_stream_iter); | |
1461 | } | |
1462 | } | |
1463 | trace_idx++; | |
1464 | } else { | |
1465 | /* | |
1466 | * trace_idx is not incremented since | |
1467 | * g_ptr_array_remove_index_fast replaces the | |
1468 | * element at trace_idx with the array's last element. | |
1469 | */ | |
1470 | g_ptr_array_remove_index_fast(session->traces, trace_idx); | |
1471 | } | |
1472 | } | |
1473 | if (youngest_candidate_stream_iter) { | |
1474 | *youngest_session_stream_iter = youngest_candidate_stream_iter; | |
1475 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1476 | } else { | |
1477 | /* | |
1478 | * The only cases where we don't have a candidate for this | |
1479 | * trace is: | |
1480 | * 1. if we reached the end of all the iterators of all the | |
1481 | * traces of this session, | |
1482 | * 2. if we never had live stream iterator in the first place. | |
1483 | * | |
1484 | * In either cases, we return END. | |
1485 | */ | |
1486 | BT_ASSERT(session->traces->len == 0); | |
1487 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1488 | } | |
7cdc2bab | 1489 | end: |
4164020e | 1490 | return stream_iter_status; |
14f28187 FD |
1491 | } |
1492 | ||
4164020e | 1493 | static inline void put_messages(bt_message_array_const msgs, uint64_t count) |
14f28187 | 1494 | { |
4164020e | 1495 | uint64_t i; |
14f28187 | 1496 | |
4164020e SM |
1497 | for (i = 0; i < count; i++) { |
1498 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
1499 | } | |
7cdc2bab MD |
1500 | } |
1501 | ||
4164020e SM |
1502 | bt_message_iterator_class_next_method_status |
1503 | lttng_live_msg_iter_next(bt_self_message_iterator *self_msg_it, bt_message_array_const msgs, | |
1504 | uint64_t capacity, uint64_t *count) | |
d3eb6e8f | 1505 | { |
4164020e SM |
1506 | bt_message_iterator_class_next_method_status status; |
1507 | enum lttng_live_viewer_status viewer_status; | |
1508 | struct lttng_live_msg_iter *lttng_live_msg_iter = | |
1509 | (struct lttng_live_msg_iter *) bt_self_message_iterator_get_data(self_msg_it); | |
1510 | struct lttng_live_component *lttng_live = lttng_live_msg_iter->lttng_live_comp; | |
1511 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
1512 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
1513 | enum lttng_live_iterator_status stream_iter_status; | |
1514 | uint64_t session_idx; | |
1515 | ||
1516 | *count = 0; | |
1517 | ||
1518 | BT_ASSERT_DBG(lttng_live_msg_iter); | |
1519 | ||
1520 | if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) { | |
1521 | /* | |
1522 | * The iterator was interrupted in a previous call to the | |
1523 | * `_next()` method. We currently do not support generating | |
1524 | * messages after such event. The babeltrace2 CLI should never | |
1525 | * be running the graph after being interrupted. So this check | |
1526 | * is to prevent other graph users from using this live | |
1527 | * iterator in an messed up internal state. | |
1528 | */ | |
1529 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1530 | BT_COMP_LOGE_APPEND_CAUSE( | |
1531 | self_comp, | |
1532 | "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event."); | |
1533 | goto end; | |
1534 | } | |
1535 | ||
1536 | /* | |
1537 | * Clear all the invalid message reference that might be left over in | |
1538 | * the output array. | |
1539 | */ | |
1540 | memset(msgs, 0, capacity * sizeof(*msgs)); | |
1541 | ||
1542 | /* | |
1543 | * If no session are exposed on the relay found at the url provided by | |
1544 | * the user, session count will be 0. In this case, we return status | |
1545 | * end to return gracefully. | |
1546 | */ | |
1547 | if (lttng_live_msg_iter->sessions->len == 0) { | |
1548 | if (lttng_live->params.sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
1549 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1550 | goto end; | |
1551 | } else { | |
1552 | /* | |
1553 | * The are no more active session for this session | |
1554 | * name. Retry to create a viewer session for the | |
1555 | * requested session name. | |
1556 | */ | |
1557 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); | |
1558 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1559 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1560 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1561 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1562 | "Error creating LTTng live viewer session"); | |
1563 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1564 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1565 | } else { | |
1566 | bt_common_abort(); | |
1567 | } | |
1568 | goto end; | |
1569 | } | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | if (lttng_live_msg_iter->active_stream_iter == 0) { | |
1574 | lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter); | |
1575 | } | |
1576 | ||
1577 | /* | |
1578 | * Here the muxing of message is done. | |
1579 | * | |
1580 | * We need to iterate over all the streams of all the traces of all the | |
1581 | * viewer sessions in order to get the message with the smallest | |
1582 | * timestamp. In this case, a session is a viewer session and there is | |
1583 | * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or | |
1584 | * kernel). Each viewer session can have multiple traces, for example, | |
1585 | * 64bit UST viewer sessions could have multiple per-pid traces. | |
1586 | * | |
1587 | * We iterate over the streams of each traces to update and see what is | |
1588 | * their next message's timestamp. From those timestamps, we select the | |
1589 | * message with the smallest timestamp as the best candidate message | |
1590 | * for that trace and do the same thing across all the sessions. | |
1591 | * | |
1592 | * We then compare the timestamp of best candidate message of all the | |
1593 | * sessions to pick the message with the smallest timestamp and we | |
1594 | * return it. | |
1595 | */ | |
1596 | while (*count < capacity) { | |
1597 | struct lttng_live_stream_iterator *youngest_stream_iter = NULL, | |
1598 | *candidate_stream_iter = NULL; | |
1599 | int64_t youngest_msg_ts_ns = INT64_MAX; | |
1600 | ||
1601 | BT_ASSERT_DBG(lttng_live_msg_iter->sessions); | |
1602 | session_idx = 0; | |
1603 | while (session_idx < lttng_live_msg_iter->sessions->len) { | |
1604 | struct lttng_live_session *session = (lttng_live_session *) g_ptr_array_index( | |
1605 | lttng_live_msg_iter->sessions, session_idx); | |
1606 | ||
1607 | /* Find the best candidate message to send downstream. */ | |
1608 | stream_iter_status = next_stream_iterator_for_session(lttng_live_msg_iter, session, | |
1609 | &candidate_stream_iter); | |
1610 | ||
1611 | /* If we receive an END status, it means that either: | |
1612 | * - Those traces never had active streams (UST with no | |
1613 | * data produced yet), | |
1614 | * - All live stream iterators have ENDed.*/ | |
1615 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1616 | if (session->closed && session->traces->len == 0) { | |
1617 | /* | |
1618 | * Remove the session from the list. | |
1619 | * session_idx is not modified since | |
1620 | * g_ptr_array_remove_index_fast | |
1621 | * replaces the the removed element with | |
1622 | * the array's last element. | |
1623 | */ | |
1624 | g_ptr_array_remove_index_fast(lttng_live_msg_iter->sessions, session_idx); | |
1625 | } else { | |
1626 | session_idx++; | |
1627 | } | |
1628 | continue; | |
1629 | } | |
1630 | ||
1631 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1632 | goto return_status; | |
1633 | } | |
1634 | ||
1635 | if (G_UNLIKELY(youngest_stream_iter == NULL) || | |
1636 | candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) { | |
1637 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; | |
1638 | youngest_stream_iter = candidate_stream_iter; | |
1639 | } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_ts_ns) { | |
1640 | /* | |
1641 | * The currently selected message to be sent | |
1642 | * downstream next has the exact same timestamp | |
1643 | * that of the current candidate message. We | |
1644 | * must break the tie in a predictable manner. | |
1645 | */ | |
1646 | BT_COMP_LOGD_STR( | |
1647 | "Two of the next message candidates have the same timestamps, pick one deterministically."); | |
1648 | /* | |
1649 | * Order the messages in an arbitrary but | |
1650 | * deterministic way. | |
1651 | */ | |
1652 | int ret = common_muxing_compare_messages(candidate_stream_iter->current_msg, | |
1653 | youngest_stream_iter->current_msg); | |
1654 | if (ret < 0) { | |
1655 | /* | |
1656 | * The `candidate_stream_iter->current_msg` | |
1657 | * should go first. Update the next | |
1658 | * iterator and the current timestamp. | |
1659 | */ | |
1660 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; | |
1661 | youngest_stream_iter = candidate_stream_iter; | |
1662 | } else if (ret == 0) { | |
1663 | /* Unable to pick which one should go first. */ | |
1664 | BT_COMP_LOGW( | |
1665 | "Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1666 | "next-stream-iter-addr=%p" | |
1667 | "candidate-stream-iter-addr=%p", | |
1668 | youngest_stream_iter, candidate_stream_iter); | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | session_idx++; | |
1673 | } | |
1674 | ||
1675 | if (!youngest_stream_iter) { | |
1676 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; | |
1677 | goto return_status; | |
1678 | } | |
1679 | ||
1680 | BT_ASSERT_DBG(youngest_stream_iter->current_msg); | |
1681 | /* Ensure monotonicity. */ | |
1682 | BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <= | |
1683 | youngest_stream_iter->current_msg_ts_ns); | |
1684 | ||
1685 | /* | |
1686 | * Insert the next message to the message batch. This will set | |
1687 | * stream iterator current messsage to NULL so that next time | |
1688 | * we fetch the next message of that stream iterator | |
1689 | */ | |
1690 | BT_MESSAGE_MOVE_REF(msgs[*count], youngest_stream_iter->current_msg); | |
1691 | (*count)++; | |
1692 | ||
1693 | /* Update the last timestamp in nanoseconds sent downstream. */ | |
1694 | lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns; | |
1695 | youngest_stream_iter->current_msg_ts_ns = INT64_MAX; | |
1696 | ||
1697 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1698 | } | |
f79c2d7a FD |
1699 | |
1700 | return_status: | |
4164020e SM |
1701 | switch (stream_iter_status) { |
1702 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
1703 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
1704 | /* | |
1705 | * If we gathered messages, return _OK even if the graph was | |
1706 | * interrupted. This allows for the components downstream to at | |
1707 | * least get the thoses messages. If the graph was indeed | |
1708 | * interrupted there should not be another _next() call as the | |
1709 | * application will tear down the graph. This component class | |
1710 | * doesn't support restarting after an interruption. | |
1711 | */ | |
1712 | if (*count > 0) { | |
1713 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; | |
1714 | } else { | |
1715 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; | |
1716 | } | |
1717 | break; | |
1718 | case LTTNG_LIVE_ITERATOR_STATUS_END: | |
1719 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; | |
1720 | break; | |
1721 | case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: | |
1722 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1723 | "Memory error preparing the next batch of messages: " | |
1724 | "live-iter-status=%s", | |
1725 | lttng_live_iterator_status_string(stream_iter_status)); | |
1726 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1727 | break; | |
1728 | case LTTNG_LIVE_ITERATOR_STATUS_ERROR: | |
1729 | case LTTNG_LIVE_ITERATOR_STATUS_INVAL: | |
1730 | case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: | |
1731 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1732 | "Error preparing the next batch of messages: " | |
1733 | "live-iter-status=%s", | |
1734 | lttng_live_iterator_status_string(stream_iter_status)); | |
1735 | ||
1736 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; | |
1737 | /* Put all existing messages on error. */ | |
1738 | put_messages(msgs, *count); | |
1739 | break; | |
1740 | default: | |
1741 | bt_common_abort(); | |
1742 | } | |
14f28187 | 1743 | |
f79c2d7a | 1744 | end: |
4164020e | 1745 | return status; |
7cdc2bab | 1746 | } |
41a2b7ae | 1747 | |
4164020e SM |
1748 | static struct lttng_live_msg_iter * |
1749 | lttng_live_msg_iter_create(struct lttng_live_component *lttng_live_comp, | |
1750 | bt_self_message_iterator *self_msg_it) | |
4f74db52 | 1751 | { |
4164020e SM |
1752 | bt_self_component *self_comp = lttng_live_comp->self_comp; |
1753 | bt_logging_level log_level = lttng_live_comp->log_level; | |
4f74db52 | 1754 | |
4164020e SM |
1755 | struct lttng_live_msg_iter *lttng_live_msg_iter = g_new0(struct lttng_live_msg_iter, 1); |
1756 | if (!lttng_live_msg_iter) { | |
1757 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate lttng_live_msg_iter"); | |
1758 | goto end; | |
1759 | } | |
1760 | ||
1761 | lttng_live_msg_iter->log_level = lttng_live_comp->log_level; | |
1762 | lttng_live_msg_iter->self_comp = lttng_live_comp->self_comp; | |
1763 | lttng_live_msg_iter->lttng_live_comp = lttng_live_comp; | |
1764 | lttng_live_msg_iter->self_msg_iter = self_msg_it; | |
1765 | ||
1766 | lttng_live_msg_iter->active_stream_iter = 0; | |
1767 | lttng_live_msg_iter->last_msg_ts_ns = INT64_MIN; | |
1768 | lttng_live_msg_iter->was_interrupted = false; | |
4f74db52 | 1769 | |
4164020e SM |
1770 | lttng_live_msg_iter->sessions = |
1771 | g_ptr_array_new_with_free_func((GDestroyNotify) lttng_live_destroy_session); | |
1772 | BT_ASSERT(lttng_live_msg_iter->sessions); | |
1773 | ||
1774 | end: | |
1775 | return lttng_live_msg_iter; | |
4f74db52 FD |
1776 | } |
1777 | ||
4164020e SM |
1778 | bt_message_iterator_class_initialize_method_status |
1779 | lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it, | |
7d91f1ac | 1780 | bt_self_message_iterator_configuration *, bt_self_component_port_output *) |
7cdc2bab | 1781 | { |
4164020e SM |
1782 | bt_message_iterator_class_initialize_method_status status; |
1783 | struct lttng_live_component *lttng_live; | |
1784 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
1785 | enum lttng_live_viewer_status viewer_status; | |
1786 | bt_logging_level log_level; | |
1787 | bt_self_component *self_comp = bt_self_message_iterator_borrow_component(self_msg_it); | |
1788 | ||
1789 | lttng_live = (lttng_live_component *) bt_self_component_get_data(self_comp); | |
1790 | log_level = lttng_live->log_level; | |
1791 | self_comp = lttng_live->self_comp; | |
1792 | ||
1793 | /* There can be only one downstream iterator at the same time. */ | |
1794 | BT_ASSERT(!lttng_live->has_msg_iter); | |
1795 | lttng_live->has_msg_iter = true; | |
1796 | ||
1797 | lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, self_msg_it); | |
1798 | if (!lttng_live_msg_iter) { | |
4164020e | 1799 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create lttng_live_msg_iter"); |
10265ebe | 1800 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
4164020e SM |
1801 | goto error; |
1802 | } | |
1803 | ||
1804 | viewer_status = live_viewer_connection_create( | |
1805 | self_comp, NULL, log_level, lttng_live->params.url->str, false, lttng_live_msg_iter, | |
1806 | <tng_live_msg_iter->viewer_connection); | |
1807 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1808 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1809 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create viewer connection"); | |
1810 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1811 | /* | |
1812 | * Interruption in the _iter_init() method is not | |
1813 | * supported. Return an error. | |
1814 | */ | |
1815 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Interrupted while creating viewer connection"); | |
1816 | } | |
10265ebe SM |
1817 | |
1818 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
4164020e SM |
1819 | goto error; |
1820 | } | |
1821 | ||
1822 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); | |
1823 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1824 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1825 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to create viewer session"); | |
1826 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1827 | /* | |
1828 | * Interruption in the _iter_init() method is not | |
1829 | * supported. Return an error. | |
1830 | */ | |
1831 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Interrupted when creating viewer session"); | |
1832 | } | |
10265ebe SM |
1833 | |
1834 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
4164020e SM |
1835 | goto error; |
1836 | } | |
1837 | ||
1838 | if (lttng_live_msg_iter->sessions->len == 0) { | |
1839 | switch (lttng_live->params.sess_not_found_act) { | |
1840 | case SESSION_NOT_FOUND_ACTION_CONTINUE: | |
1841 | BT_COMP_LOGI( | |
1842 | "Unable to connect to the requested live viewer session. Keep trying to connect because of " | |
1843 | "%s=\"%s\" component parameter: url=\"%s\"", | |
1844 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR, | |
1845 | lttng_live->params.url->str); | |
1846 | break; | |
1847 | case SESSION_NOT_FOUND_ACTION_FAIL: | |
1848 | BT_COMP_LOGE_APPEND_CAUSE( | |
1849 | self_comp, | |
1850 | "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" " | |
1851 | "component parameter: url =\"%s\"", | |
1852 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_FAIL_STR, | |
1853 | lttng_live->params.url->str); | |
10265ebe | 1854 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
4164020e SM |
1855 | goto error; |
1856 | case SESSION_NOT_FOUND_ACTION_END: | |
1857 | BT_COMP_LOGI( | |
1858 | "Unable to connect to the requested live viewer session. End gracefully at the first _next() " | |
1859 | "call because of %s=\"%s\" component parameter: " | |
1860 | "url=\"%s\"", | |
1861 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_END_STR, | |
1862 | lttng_live->params.url->str); | |
1863 | break; | |
1864 | default: | |
1865 | bt_common_abort(); | |
1866 | } | |
1867 | } | |
1868 | ||
1869 | bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter); | |
1870 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
1871 | goto end; | |
f79c2d7a | 1872 | |
14f28187 | 1873 | error: |
4164020e | 1874 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); |
7cdc2bab | 1875 | end: |
4164020e | 1876 | return status; |
7cdc2bab MD |
1877 | } |
1878 | ||
80aff5ef | 1879 | static struct bt_param_validation_map_value_entry_descr list_sessions_params[] = { |
88730e42 SM |
1880 | {URL_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
1881 | bt_param_validation_value_descr::makeString()}, | |
4164020e SM |
1882 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
1883 | ||
1884 | static bt_component_class_query_method_status | |
1885 | lttng_live_query_list_sessions(const bt_value *params, const bt_value **result, | |
1886 | bt_self_component_class *self_comp_class, bt_logging_level log_level) | |
7cdc2bab | 1887 | { |
4164020e SM |
1888 | bt_component_class_query_method_status status; |
1889 | const bt_value *url_value = NULL; | |
1890 | const char *url; | |
1891 | struct live_viewer_connection *viewer_connection = NULL; | |
1892 | enum lttng_live_viewer_status viewer_status; | |
1893 | enum bt_param_validation_status validation_status; | |
1894 | gchar *validate_error = NULL; | |
1895 | ||
1896 | validation_status = bt_param_validation_validate(params, list_sessions_params, &validate_error); | |
1897 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
1898 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
1899 | goto error; | |
1900 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
1901 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1902 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "%s", validate_error); | |
1903 | goto error; | |
1904 | } | |
1905 | ||
1906 | url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM); | |
1907 | url = bt_value_string_get(url_value); | |
1908 | ||
1909 | viewer_status = live_viewer_connection_create(NULL, self_comp_class, log_level, url, true, NULL, | |
1910 | &viewer_connection); | |
1911 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1912 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1913 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Failed to create viewer connection"); | |
1914 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1915 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1916 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; | |
1917 | } else { | |
1918 | bt_common_abort(); | |
1919 | } | |
1920 | goto error; | |
1921 | } | |
1922 | ||
1923 | status = live_viewer_connection_list_sessions(viewer_connection, result); | |
1924 | if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) { | |
1925 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Failed to list viewer sessions"); | |
1926 | goto error; | |
1927 | } | |
1928 | ||
1929 | goto end; | |
c7eee084 | 1930 | |
7cdc2bab | 1931 | error: |
4164020e | 1932 | BT_VALUE_PUT_REF_AND_RESET(*result); |
c7eee084 | 1933 | |
4164020e SM |
1934 | if (status >= 0) { |
1935 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
1936 | } | |
c7eee084 | 1937 | |
7cdc2bab | 1938 | end: |
4164020e SM |
1939 | if (viewer_connection) { |
1940 | live_viewer_connection_destroy(viewer_connection); | |
1941 | } | |
80aff5ef | 1942 | |
4164020e | 1943 | g_free(validate_error); |
80aff5ef | 1944 | |
4164020e | 1945 | return status; |
7cdc2bab MD |
1946 | } |
1947 | ||
4164020e SM |
1948 | static bt_component_class_query_method_status |
1949 | lttng_live_query_support_info(const bt_value *params, const bt_value **result, | |
1950 | bt_self_component_class *self_comp_class, bt_logging_level log_level) | |
312df793 | 1951 | { |
4164020e SM |
1952 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
1953 | const bt_value *input_type_value; | |
1954 | const bt_value *input_value; | |
1955 | double weight = 0; | |
dd420a9b | 1956 | struct bt_common_lttng_live_url_parts parts = {}; |
4164020e SM |
1957 | |
1958 | /* Used by the logging macros */ | |
1959 | __attribute__((unused)) bt_self_component *self_comp = NULL; | |
1960 | ||
1961 | *result = NULL; | |
1962 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); | |
1963 | if (!input_type_value) { | |
1964 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Missing expected `type` parameter."); | |
1965 | goto error; | |
1966 | } | |
1967 | ||
1968 | if (!bt_value_is_string(input_type_value)) { | |
1969 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "`type` parameter is not a string value."); | |
1970 | goto error; | |
1971 | } | |
1972 | ||
1973 | if (strcmp(bt_value_string_get(input_type_value), "string") != 0) { | |
1974 | /* We don't handle file system paths */ | |
1975 | goto create_result; | |
1976 | } | |
1977 | ||
1978 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
1979 | if (!input_value) { | |
1980 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Missing expected `input` parameter."); | |
1981 | goto error; | |
1982 | } | |
1983 | ||
1984 | if (!bt_value_is_string(input_value)) { | |
1985 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, | |
1986 | "`input` parameter is not a string value."); | |
1987 | goto error; | |
1988 | } | |
1989 | ||
1990 | parts = bt_common_parse_lttng_live_url(bt_value_string_get(input_value), NULL, 0); | |
1991 | if (parts.session_name) { | |
1992 | /* | |
1993 | * Looks pretty much like an LTTng live URL: we got the | |
1994 | * session name part, which forms a complete URL. | |
1995 | */ | |
1996 | weight = .75; | |
1997 | } | |
312df793 PP |
1998 | |
1999 | create_result: | |
4164020e SM |
2000 | *result = bt_value_real_create_init(weight); |
2001 | if (!*result) { | |
2002 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
2003 | goto error; | |
2004 | } | |
312df793 | 2005 | |
4164020e | 2006 | goto end; |
312df793 PP |
2007 | |
2008 | error: | |
4164020e SM |
2009 | if (status >= 0) { |
2010 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
2011 | } | |
312df793 | 2012 | |
4164020e | 2013 | BT_ASSERT(!*result); |
312df793 PP |
2014 | |
2015 | end: | |
4164020e SM |
2016 | bt_common_destroy_lttng_live_url_parts(&parts); |
2017 | return status; | |
312df793 PP |
2018 | } |
2019 | ||
4164020e SM |
2020 | bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class, |
2021 | bt_private_query_executor *priv_query_exec, | |
2022 | const char *object, const bt_value *params, | |
2023 | __attribute__((unused)) void *method_data, | |
2024 | const bt_value **result) | |
7cdc2bab | 2025 | { |
4164020e SM |
2026 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
2027 | bt_self_component *self_comp = NULL; | |
2028 | bt_self_component_class *self_comp_class = | |
2029 | bt_self_component_class_source_as_self_component_class(comp_class); | |
2030 | bt_logging_level log_level = bt_query_executor_get_logging_level( | |
2031 | bt_private_query_executor_as_query_executor_const(priv_query_exec)); | |
2032 | ||
2033 | if (strcmp(object, "sessions") == 0) { | |
2034 | status = lttng_live_query_list_sessions(params, result, self_comp_class, log_level); | |
2035 | } else if (strcmp(object, "babeltrace.support-info") == 0) { | |
2036 | status = lttng_live_query_support_info(params, result, self_comp_class, log_level); | |
2037 | } else { | |
2038 | BT_COMP_LOGI("Unknown query object `%s`", object); | |
2039 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; | |
2040 | goto end; | |
2041 | } | |
14f28187 FD |
2042 | |
2043 | end: | |
4164020e | 2044 | return status; |
7cdc2bab MD |
2045 | } |
2046 | ||
4164020e | 2047 | static void lttng_live_component_destroy_data(struct lttng_live_component *lttng_live) |
7cdc2bab | 2048 | { |
4164020e SM |
2049 | if (!lttng_live) { |
2050 | return; | |
2051 | } | |
2052 | if (lttng_live->params.url) { | |
2053 | g_string_free(lttng_live->params.url, TRUE); | |
2054 | } | |
2055 | g_free(lttng_live); | |
7cdc2bab MD |
2056 | } |
2057 | ||
14f28187 | 2058 | void lttng_live_component_finalize(bt_self_component_source *component) |
7cdc2bab | 2059 | { |
4164020e SM |
2060 | lttng_live_component *data = (lttng_live_component *) bt_self_component_get_data( |
2061 | bt_self_component_source_as_self_component(component)); | |
2062 | ||
2063 | if (!data) { | |
2064 | return; | |
2065 | } | |
2066 | lttng_live_component_destroy_data(data); | |
7cdc2bab MD |
2067 | } |
2068 | ||
4164020e SM |
2069 | static enum session_not_found_action |
2070 | parse_session_not_found_action_param(const bt_value *no_session_param) | |
14f28187 | 2071 | { |
4164020e SM |
2072 | enum session_not_found_action action; |
2073 | const char *no_session_act_str = bt_value_string_get(no_session_param); | |
2074 | ||
2075 | if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) { | |
2076 | action = SESSION_NOT_FOUND_ACTION_CONTINUE; | |
2077 | } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) { | |
2078 | action = SESSION_NOT_FOUND_ACTION_FAIL; | |
2079 | } else { | |
2080 | BT_ASSERT(strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0); | |
2081 | action = SESSION_NOT_FOUND_ACTION_END; | |
2082 | } | |
2083 | ||
2084 | return action; | |
14f28187 FD |
2085 | } |
2086 | ||
88730e42 SM |
2087 | static bt_param_validation_value_descr inputs_elem_descr = |
2088 | bt_param_validation_value_descr::makeString(); | |
80aff5ef SM |
2089 | |
2090 | static const char *sess_not_found_action_choices[] = { | |
4164020e SM |
2091 | SESS_NOT_FOUND_ACTION_CONTINUE_STR, |
2092 | SESS_NOT_FOUND_ACTION_FAIL_STR, | |
2093 | SESS_NOT_FOUND_ACTION_END_STR, | |
80aff5ef SM |
2094 | }; |
2095 | ||
2096 | static struct bt_param_validation_map_value_entry_descr params_descr[] = { | |
88730e42 SM |
2097 | {INPUTS_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, |
2098 | bt_param_validation_value_descr::makeArray(1, 1, inputs_elem_descr)}, | |
2099 | {SESS_NOT_FOUND_ACTION_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, | |
2100 | bt_param_validation_value_descr::makeString(sess_not_found_action_choices)}, | |
4164020e SM |
2101 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; |
2102 | ||
2103 | static bt_component_class_initialize_method_status | |
2104 | lttng_live_component_create(const bt_value *params, bt_logging_level log_level, | |
2105 | bt_self_component *self_comp, struct lttng_live_component **component) | |
7cdc2bab | 2106 | { |
4164020e SM |
2107 | struct lttng_live_component *lttng_live = NULL; |
2108 | const bt_value *inputs_value; | |
2109 | const bt_value *url_value; | |
2110 | const bt_value *value; | |
2111 | const char *url; | |
2112 | enum bt_param_validation_status validation_status; | |
2113 | gchar *validation_error = NULL; | |
2114 | bt_component_class_initialize_method_status status; | |
2115 | ||
2116 | validation_status = bt_param_validation_validate(params, params_descr, &validation_error); | |
2117 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
2118 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2119 | goto error; | |
2120 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
2121 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validation_error); | |
2122 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
2123 | goto error; | |
2124 | } | |
2125 | ||
2126 | lttng_live = g_new0(struct lttng_live_component, 1); | |
2127 | if (!lttng_live) { | |
2128 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2129 | goto end; | |
2130 | } | |
2131 | lttng_live->log_level = log_level; | |
2132 | lttng_live->self_comp = self_comp; | |
2133 | lttng_live->max_query_size = MAX_QUERY_SIZE; | |
2134 | lttng_live->has_msg_iter = false; | |
2135 | ||
2136 | inputs_value = bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM); | |
2137 | url_value = bt_value_array_borrow_element_by_index_const(inputs_value, 0); | |
2138 | url = bt_value_string_get(url_value); | |
2139 | ||
2140 | lttng_live->params.url = g_string_new(url); | |
2141 | if (!lttng_live->params.url) { | |
2142 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2143 | goto error; | |
2144 | } | |
2145 | ||
2146 | value = bt_value_map_borrow_entry_value_const(params, SESS_NOT_FOUND_ACTION_PARAM); | |
2147 | if (value) { | |
2148 | lttng_live->params.sess_not_found_act = parse_session_not_found_action_param(value); | |
2149 | } else { | |
2150 | BT_COMP_LOGI("Optional `%s` parameter is missing: " | |
2151 | "defaulting to `%s`.", | |
2152 | SESS_NOT_FOUND_ACTION_PARAM, SESS_NOT_FOUND_ACTION_CONTINUE_STR); | |
2153 | lttng_live->params.sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE; | |
2154 | } | |
2155 | ||
2156 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
2157 | goto end; | |
7cdc2bab MD |
2158 | |
2159 | error: | |
4164020e SM |
2160 | lttng_live_component_destroy_data(lttng_live); |
2161 | lttng_live = NULL; | |
7cdc2bab | 2162 | end: |
4164020e | 2163 | g_free(validation_error); |
80aff5ef | 2164 | |
4164020e SM |
2165 | *component = lttng_live; |
2166 | return status; | |
d3e4dcd8 PP |
2167 | } |
2168 | ||
4164020e SM |
2169 | bt_component_class_initialize_method_status |
2170 | lttng_live_component_init(bt_self_component_source *self_comp_src, | |
7d91f1ac | 2171 | bt_self_component_source_configuration *, const bt_value *params, void *) |
f3bc2010 | 2172 | { |
4164020e SM |
2173 | struct lttng_live_component *lttng_live; |
2174 | bt_component_class_initialize_method_status ret; | |
2175 | bt_self_component *self_comp = bt_self_component_source_as_self_component(self_comp_src); | |
2176 | bt_logging_level log_level = | |
2177 | bt_component_get_logging_level(bt_self_component_as_component(self_comp)); | |
2178 | bt_self_component_add_port_status add_port_status; | |
2179 | ||
2180 | ret = lttng_live_component_create(params, log_level, self_comp, <tng_live); | |
2181 | if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { | |
2182 | goto error; | |
2183 | } | |
2184 | ||
2185 | add_port_status = bt_self_component_source_add_output_port(self_comp_src, "out", NULL, NULL); | |
2186 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { | |
2187 | ret = (bt_component_class_initialize_method_status) add_port_status; | |
2188 | goto end; | |
2189 | } | |
2190 | ||
2191 | bt_self_component_set_data(self_comp, lttng_live); | |
2192 | goto end; | |
7cdc2bab | 2193 | |
19bdff20 | 2194 | error: |
4164020e SM |
2195 | lttng_live_component_destroy_data(lttng_live); |
2196 | lttng_live = NULL; | |
7cdc2bab | 2197 | end: |
4164020e | 2198 | return ret; |
d85ef162 | 2199 | } |