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