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