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