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