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