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