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