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