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