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