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