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