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