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