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