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