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