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