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