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