Fix: src.ctf.lttng-live: omitting stream end on stream hang up
[babeltrace.git] / src / plugins / ctf / lttng-live / lttng-live.c
CommitLineData
f3bc2010
JG
1/*
2 * lttng-live.c
3 *
4 * Babeltrace CTF LTTng-live Client Component
5 *
14f28187 6 * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
f3bc2010 7 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7cdc2bab 8 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
f3bc2010
JG
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
2ece7dd0 31#define BT_COMP_LOG_SELF_COMP self_comp
c01594de 32#define BT_LOG_OUTPUT_LEVEL log_level
350ad6c1 33#define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE"
d9c39b0a 34#include "logging/comp-logging.h"
020bc26f 35
14f28187
FD
36#include <glib.h>
37#include <inttypes.h>
38#include <unistd.h>
39
578e048b 40#include "common/assert.h"
3fadfbc0 41#include <babeltrace2/babeltrace.h>
578e048b 42#include "compat/compiler.h"
3fadfbc0 43#include <babeltrace2/types.h>
f3bc2010 44
7cdc2bab
MD
45#include "data-stream.h"
46#include "metadata.h"
14f28187 47#include "lttng-live.h"
7cdc2bab 48
14f28187
FD
49#define MAX_QUERY_SIZE (256*1024)
50#define URL_PARAM "url"
51#define SESS_NOT_FOUND_ACTION_PARAM "session-not-found-action"
52#define SESS_NOT_FOUND_ACTION_CONTINUE_STR "continue"
53#define SESS_NOT_FOUND_ACTION_FAIL_STR "fail"
54#define SESS_NOT_FOUND_ACTION_END_STR "end"
7cdc2bab 55
2ece7dd0 56#define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__)
7cdc2bab 57
14f28187
FD
58static
59const char *print_live_iterator_status(enum lttng_live_iterator_status status)
60{
61 switch (status) {
62 case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE:
63 return "LTTNG_LIVE_ITERATOR_STATUS_CONTINUE";
64 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
65 return "LTTNG_LIVE_ITERATOR_STATUS_AGAIN";
66 case LTTNG_LIVE_ITERATOR_STATUS_END:
67 return "LTTNG_LIVE_ITERATOR_STATUS_END";
68 case LTTNG_LIVE_ITERATOR_STATUS_OK:
69 return "LTTNG_LIVE_ITERATOR_STATUS_OK";
70 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
71 return "LTTNG_LIVE_ITERATOR_STATUS_INVAL";
72 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
73 return "LTTNG_LIVE_ITERATOR_STATUS_ERROR";
74 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
75 return "LTTNG_LIVE_ITERATOR_STATUS_NOMEM";
76 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
77 return "LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED";
78 default:
79 abort();
80 }
81}
82
83static
84const char *print_state(struct lttng_live_stream_iterator *s)
7cdc2bab
MD
85{
86 switch (s->state) {
87 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
88 return "ACTIVE_NO_DATA";
89 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
90 return "QUIESCENT_NO_DATA";
91 case LTTNG_LIVE_STREAM_QUIESCENT:
92 return "QUIESCENT";
93 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
94 return "ACTIVE_DATA";
95 case LTTNG_LIVE_STREAM_EOF:
96 return "EOF";
97 default:
98 return "ERROR";
99 }
100}
7cdc2bab 101
14f28187
FD
102#define print_stream_state(live_stream_iter) \
103 do { \
2ece7dd0 104 BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \
14f28187
FD
105 ", curr_inact_ts %" PRId64, \
106 print_state(live_stream_iter), \
107 live_stream_iter->last_inactivity_ts, \
108 live_stream_iter->current_inactivity_ts); \
109 } while (0);
6f79a7cf
MD
110
111BT_HIDDEN
9b4f9b42 112bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter)
6f79a7cf 113{
14f28187 114 bool ret;
6f79a7cf 115
9b4f9b42 116 if (!msg_iter) {
14f28187
FD
117 ret = false;
118 goto end;
7cdc2bab 119 }
7cdc2bab 120
9b4f9b42
PP
121 ret = bt_self_message_iterator_is_interrupted(
122 msg_iter->self_msg_iter);
4bf0e537 123
14f28187
FD
124end:
125 return ret;
7cdc2bab
MD
126}
127
128static
129struct lttng_live_trace *lttng_live_find_trace(struct lttng_live_session *session,
130 uint64_t trace_id)
d3e4dcd8 131{
14f28187
FD
132 uint64_t trace_idx;
133 struct lttng_live_trace *ret_trace = NULL;
7cdc2bab 134
14f28187
FD
135 for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
136 struct lttng_live_trace *trace =
137 g_ptr_array_index(session->traces, trace_idx);
7cdc2bab 138 if (trace->id == trace_id) {
14f28187
FD
139 ret_trace = trace;
140 goto end;
7cdc2bab
MD
141 }
142 }
14f28187
FD
143
144end:
145 return ret_trace;
d3eb6e8f
PP
146}
147
7cdc2bab 148static
14f28187 149void lttng_live_destroy_trace(struct lttng_live_trace *trace)
7cdc2bab 150{
c01594de 151 bt_logging_level log_level = trace->log_level;
2ece7dd0 152 bt_self_component *self_comp = trace->self_comp;
c01594de 153
2ece7dd0 154 BT_COMP_LOGD("Destroy lttng_live_trace");
7cdc2bab 155
14f28187
FD
156 BT_ASSERT(trace->stream_iterators);
157 g_ptr_array_free(trace->stream_iterators, TRUE);
5bd230f4 158
14f28187
FD
159 BT_TRACE_PUT_REF_AND_RESET(trace->trace);
160 BT_TRACE_CLASS_PUT_REF_AND_RESET(trace->trace_class);
5bd230f4 161
7cdc2bab 162 lttng_live_metadata_fini(trace);
7cdc2bab
MD
163 g_free(trace);
164}
165
166static
167struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *session,
168 uint64_t trace_id)
169{
170 struct lttng_live_trace *trace = NULL;
c01594de 171 bt_logging_level log_level = session->log_level;
2ece7dd0 172 bt_self_component *self_comp = session->self_comp;
7cdc2bab
MD
173
174 trace = g_new0(struct lttng_live_trace, 1);
175 if (!trace) {
176 goto error;
177 }
c01594de 178 trace->log_level = session->log_level;
2ece7dd0 179 trace->self_comp = session->self_comp;
7cdc2bab
MD
180 trace->session = session;
181 trace->id = trace_id;
14f28187
FD
182 trace->trace_class = NULL;
183 trace->trace = NULL;
184 trace->stream_iterators = g_ptr_array_new_with_free_func(
185 (GDestroyNotify) lttng_live_stream_iterator_destroy);
186 BT_ASSERT(trace->stream_iterators);
7cdc2bab 187 trace->new_metadata_needed = true;
14f28187
FD
188 g_ptr_array_add(session->traces, trace);
189
2ece7dd0 190 BT_COMP_LOGI("Create trace");
7cdc2bab
MD
191 goto end;
192error:
193 g_free(trace);
194 trace = NULL;
195end:
196 return trace;
197}
198
199BT_HIDDEN
14f28187
FD
200struct lttng_live_trace *lttng_live_borrow_trace(
201 struct lttng_live_session *session, uint64_t trace_id)
7cdc2bab
MD
202{
203 struct lttng_live_trace *trace;
204
205 trace = lttng_live_find_trace(session, trace_id);
206 if (trace) {
14f28187 207 goto end;
7cdc2bab 208 }
7cdc2bab 209
14f28187
FD
210 /* The session is the owner of the newly created trace. */
211 trace = lttng_live_create_trace(session, trace_id);
7cdc2bab 212
14f28187
FD
213end:
214 return trace;
7cdc2bab
MD
215}
216
217BT_HIDDEN
14f28187 218int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter,
06994c71
MD
219 uint64_t session_id, const char *hostname,
220 const char *session_name)
7cdc2bab
MD
221{
222 int ret = 0;
14f28187 223 struct lttng_live_session *session;
c01594de 224 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 225 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
7cdc2bab 226
14f28187
FD
227 session = g_new0(struct lttng_live_session, 1);
228 if (!session) {
7cdc2bab
MD
229 goto error;
230 }
231
c01594de 232 session->log_level = lttng_live_msg_iter->log_level;
2ece7dd0 233 session->self_comp = lttng_live_msg_iter->self_comp;
14f28187
FD
234 session->id = session_id;
235 session->traces = g_ptr_array_new_with_free_func(
236 (GDestroyNotify) lttng_live_destroy_trace);
237 BT_ASSERT(session->traces);
238 session->lttng_live_msg_iter = lttng_live_msg_iter;
239 session->new_streams_needed = true;
240 session->hostname = g_string_new(hostname);
241 BT_ASSERT(session->hostname);
242
243 session->session_name = g_string_new(session_name);
244 BT_ASSERT(session->session_name);
7cdc2bab 245
2ece7dd0 246 BT_COMP_LOGI("Reading from session: %" PRIu64 " hostname: %s session_name: %s",
14f28187
FD
247 session->id, hostname, session_name);
248 g_ptr_array_add(lttng_live_msg_iter->sessions, session);
7cdc2bab
MD
249 goto end;
250error:
2ece7dd0 251 BT_COMP_LOGE("Error adding session");
14f28187 252 g_free(session);
7cdc2bab
MD
253 ret = -1;
254end:
255 return ret;
256}
257
258static
259void lttng_live_destroy_session(struct lttng_live_session *session)
260{
c01594de 261 bt_logging_level log_level;
2ece7dd0 262 bt_self_component *self_comp;
14f28187
FD
263
264 if (!session) {
265 goto end;
266 }
7cdc2bab 267
c01594de 268 log_level = session->log_level;
2ece7dd0
PP
269 self_comp = session->self_comp;
270 BT_COMP_LOGD("Destroy lttng live session");
7cdc2bab
MD
271 if (session->id != -1ULL) {
272 if (lttng_live_detach_session(session)) {
14f28187 273 if (session->lttng_live_msg_iter &&
9b4f9b42
PP
274 !lttng_live_graph_is_canceled(
275 session->lttng_live_msg_iter)) {
4c66436f 276 /* Old relayd cannot detach sessions. */
2ece7dd0 277 BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64,
4c66436f
MD
278 session->id);
279 }
7cdc2bab
MD
280 }
281 session->id = -1ULL;
282 }
14f28187
FD
283
284 if (session->traces) {
285 g_ptr_array_free(session->traces, TRUE);
7cdc2bab 286 }
14f28187 287
06994c71
MD
288 if (session->hostname) {
289 g_string_free(session->hostname, TRUE);
290 }
291 if (session->session_name) {
292 g_string_free(session->session_name, TRUE);
293 }
7cdc2bab 294 g_free(session);
14f28187
FD
295
296end:
297 return;
7cdc2bab
MD
298}
299
14f28187
FD
300static
301void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab 302{
14f28187
FD
303 if (!lttng_live_msg_iter) {
304 goto end;
7cdc2bab 305 }
7cdc2bab 306
14f28187
FD
307 if (lttng_live_msg_iter->sessions) {
308 g_ptr_array_free(lttng_live_msg_iter->sessions, TRUE);
7cdc2bab 309 }
14f28187
FD
310
311 BT_OBJECT_PUT_REF_AND_RESET(lttng_live_msg_iter->viewer_connection);
312 BT_ASSERT(lttng_live_msg_iter->lttng_live_comp);
313 BT_ASSERT(lttng_live_msg_iter->lttng_live_comp->has_msg_iter);
314
315 /* All stream iterators must be destroyed at this point. */
316 BT_ASSERT(lttng_live_msg_iter->active_stream_iter == 0);
317 lttng_live_msg_iter->lttng_live_comp->has_msg_iter = false;
318
319 g_free(lttng_live_msg_iter);
320
321end:
322 return;
323}
324
325BT_HIDDEN
326void lttng_live_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
327{
328 struct lttng_live_msg_iter *lttng_live_msg_iter;
329
330 BT_ASSERT(self_msg_iter);
331
332 lttng_live_msg_iter = bt_self_message_iterator_get_data(self_msg_iter);
333 BT_ASSERT(lttng_live_msg_iter);
334 lttng_live_msg_iter_destroy(lttng_live_msg_iter);
7cdc2bab
MD
335}
336
337static
14f28187 338enum lttng_live_iterator_status lttng_live_iterator_next_check_stream_state(
7cdc2bab
MD
339 struct lttng_live_stream_iterator *lttng_live_stream)
340{
c01594de 341 bt_logging_level log_level = lttng_live_stream->log_level;
2ece7dd0 342 bt_self_component *self_comp = lttng_live_stream->self_comp;
c01594de 343
7cdc2bab
MD
344 switch (lttng_live_stream->state) {
345 case LTTNG_LIVE_STREAM_QUIESCENT:
346 case LTTNG_LIVE_STREAM_ACTIVE_DATA:
347 break;
348 case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA:
349 /* Invalid state. */
2ece7dd0 350 BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\"");
087bc060 351 abort();
7cdc2bab
MD
352 case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA:
353 /* Invalid state. */
2ece7dd0 354 BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\"");
087bc060 355 abort();
7cdc2bab
MD
356 case LTTNG_LIVE_STREAM_EOF:
357 break;
358 }
14f28187 359 return LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
360}
361
362/*
363 * For active no data stream, fetch next data. It can be either:
364 * - quiescent: need to put it in the prio heap at quiescent end
365 * timestamp,
366 * - have data: need to wire up first event into the prio heap,
367 * - have no data on this stream at this point: need to retry (AGAIN) or
368 * return EOF.
369 */
370static
14f28187
FD
371enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stream(
372 struct lttng_live_msg_iter *lttng_live_msg_iter,
7cdc2bab
MD
373 struct lttng_live_stream_iterator *lttng_live_stream)
374{
c01594de 375 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 376 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
14f28187
FD
377 enum lttng_live_iterator_status ret =
378 LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
379 struct packet_index index;
380 enum lttng_live_stream_state orig_state = lttng_live_stream->state;
381
382 if (lttng_live_stream->trace->new_metadata_needed) {
14f28187 383 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
384 goto end;
385 }
386 if (lttng_live_stream->trace->session->new_streams_needed) {
14f28187 387 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
388 goto end;
389 }
14f28187
FD
390 if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_NO_DATA &&
391 lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA) {
7cdc2bab
MD
392 goto end;
393 }
14f28187
FD
394 ret = lttng_live_get_next_index(lttng_live_msg_iter, lttng_live_stream,
395 &index);
396 if (ret != LTTNG_LIVE_ITERATOR_STATUS_OK) {
7cdc2bab
MD
397 goto end;
398 }
f6ccaed9 399 BT_ASSERT(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF);
7cdc2bab 400 if (lttng_live_stream->state == LTTNG_LIVE_STREAM_QUIESCENT) {
14f28187
FD
401 uint64_t last_inact_ts = lttng_live_stream->last_inactivity_ts,
402 curr_inact_ts = lttng_live_stream->current_inactivity_ts;
403
404 if (orig_state == LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA &&
405 last_inact_ts == curr_inact_ts) {
406 ret = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
7cdc2bab
MD
407 print_stream_state(lttng_live_stream);
408 } else {
14f28187 409 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab
MD
410 }
411 goto end;
412 }
413 lttng_live_stream->base_offset = index.offset;
414 lttng_live_stream->offset = index.offset;
415 lttng_live_stream->len = index.packet_size / CHAR_BIT;
416end:
14f28187
FD
417 if (ret == LTTNG_LIVE_ITERATOR_STATUS_OK) {
418 ret = lttng_live_iterator_next_check_stream_state(lttng_live_stream);
7cdc2bab
MD
419 }
420 return ret;
421}
422
423/*
14f28187
FD
424 * Creation of the message requires the ctf trace class to be created
425 * beforehand, but the live protocol gives us all streams (including metadata)
426 * at once. So we split it in three steps: getting streams, getting metadata
427 * (which creates the ctf trace class), and then creating the per-stream
428 * messages.
7cdc2bab
MD
429 */
430static
14f28187
FD
431enum lttng_live_iterator_status lttng_live_get_session(
432 struct lttng_live_msg_iter *lttng_live_msg_iter,
7cdc2bab
MD
433 struct lttng_live_session *session)
434{
14f28187
FD
435 enum lttng_live_iterator_status status;
436 uint64_t trace_idx;
437 int ret = 0;
7cdc2bab 438
14f28187
FD
439 if (!session->attached) {
440 ret = lttng_live_attach_session(session);
441 if (ret) {
42521b69 442 if (lttng_live_msg_iter && lttng_live_graph_is_canceled(
9b4f9b42 443 lttng_live_msg_iter)) {
14f28187
FD
444 status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
445 } else {
446 status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
447 }
448 goto end;
4c66436f 449 }
7cdc2bab 450 }
14f28187 451
7cdc2bab 452 status = lttng_live_get_new_streams(session);
14f28187
FD
453 if (status != LTTNG_LIVE_ITERATOR_STATUS_OK &&
454 status != LTTNG_LIVE_ITERATOR_STATUS_END) {
455 goto end;
7cdc2bab 456 }
14f28187
FD
457 for (trace_idx = 0; trace_idx < session->traces->len; trace_idx++) {
458 struct lttng_live_trace *trace =
459 g_ptr_array_index(session->traces, trace_idx);
460
7cdc2bab 461 status = lttng_live_metadata_update(trace);
14f28187
FD
462 if (status != LTTNG_LIVE_ITERATOR_STATUS_OK &&
463 status != LTTNG_LIVE_ITERATOR_STATUS_END) {
464 goto end;
7cdc2bab
MD
465 }
466 }
14f28187
FD
467 status = lttng_live_lazy_msg_init(session);
468
469end:
470 return status;
7cdc2bab
MD
471}
472
473BT_HIDDEN
14f28187 474void lttng_live_need_new_streams(struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab 475{
14f28187 476 uint64_t session_idx;
7cdc2bab 477
14f28187
FD
478 for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len;
479 session_idx++) {
480 struct lttng_live_session *session =
481 g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx);
7cdc2bab
MD
482 session->new_streams_needed = true;
483 }
484}
485
486static
14f28187 487void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab 488{
14f28187 489 uint64_t session_idx, trace_idx;
7cdc2bab 490
14f28187
FD
491 for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len;
492 session_idx++) {
493 struct lttng_live_session *session =
494 g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx);
7cdc2bab 495 session->new_streams_needed = true;
14f28187
FD
496 for (trace_idx = 0; trace_idx < session->traces->len;
497 trace_idx++) {
498 struct lttng_live_trace *trace =
499 g_ptr_array_index(session->traces, trace_idx);
7cdc2bab
MD
500 trace->new_metadata_needed = true;
501 }
502 }
503}
504
505static
14f28187
FD
506enum lttng_live_iterator_status
507lttng_live_iterator_handle_new_streams_and_metadata(
508 struct lttng_live_msg_iter *lttng_live_msg_iter)
7cdc2bab 509{
14f28187
FD
510 enum lttng_live_iterator_status ret =
511 LTTNG_LIVE_ITERATOR_STATUS_OK;
512 uint64_t session_idx = 0, nr_sessions_opened = 0;
513 struct lttng_live_session *session;
514 enum session_not_found_action sess_not_found_act =
515 lttng_live_msg_iter->lttng_live_comp->params.sess_not_found_act;
516
7cdc2bab 517 /*
14f28187 518 * In a remotely distant future, we could add a "new
7cdc2bab
MD
519 * session" flag to the protocol, which would tell us that we
520 * need to query for new sessions even though we have sessions
521 * currently ongoing.
522 */
14f28187
FD
523 if (lttng_live_msg_iter->sessions->len == 0) {
524 if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) {
525 ret = LTTNG_LIVE_ITERATOR_STATUS_END;
526 goto end;
527 } else {
528 /*
529 * Retry to create a viewer session for the requested
530 * session name.
531 */
532 if (lttng_live_create_viewer_session(lttng_live_msg_iter)) {
533 ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
534 goto end;
535 }
536 }
7cdc2bab 537 }
14f28187
FD
538
539 for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len;
540 session_idx++) {
541 session = g_ptr_array_index(lttng_live_msg_iter->sessions,
542 session_idx);
543 ret = lttng_live_get_session(lttng_live_msg_iter, session);
7cdc2bab 544 switch (ret) {
14f28187 545 case LTTNG_LIVE_ITERATOR_STATUS_OK:
7cdc2bab 546 break;
14f28187
FD
547 case LTTNG_LIVE_ITERATOR_STATUS_END:
548 ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
549 break;
550 default:
551 goto end;
552 }
553 if (!session->closed) {
554 nr_sessions_opened++;
555 }
556 }
557end:
14f28187
FD
558 if (ret == LTTNG_LIVE_ITERATOR_STATUS_OK &&
559 sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE &&
560 nr_sessions_opened == 0) {
561 ret = LTTNG_LIVE_ITERATOR_STATUS_END;
7cdc2bab
MD
562 }
563 return ret;
564}
565
566static
14f28187
FD
567enum lttng_live_iterator_status emit_inactivity_message(
568 struct lttng_live_msg_iter *lttng_live_msg_iter,
569 struct lttng_live_stream_iterator *stream_iter,
570 bt_message **message, uint64_t timestamp)
7cdc2bab 571{
14f28187
FD
572 enum lttng_live_iterator_status ret =
573 LTTNG_LIVE_ITERATOR_STATUS_OK;
574 bt_message *msg = NULL;
7cdc2bab 575
14f28187
FD
576 BT_ASSERT(stream_iter->trace->clock_class);
577
578 msg = bt_message_message_iterator_inactivity_create(
579 lttng_live_msg_iter->self_msg_iter,
580 stream_iter->trace->clock_class,
581 timestamp);
d6e69534 582 if (!msg) {
7cdc2bab
MD
583 goto error;
584 }
14f28187 585
d6e69534 586 *message = msg;
7cdc2bab 587end:
7cdc2bab
MD
588 return ret;
589
590error:
14f28187 591 ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
d6e69534 592 bt_message_put_ref(msg);
7cdc2bab
MD
593 goto end;
594}
595
596static
14f28187
FD
597enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream(
598 struct lttng_live_msg_iter *lttng_live_msg_iter,
7cdc2bab 599 struct lttng_live_stream_iterator *lttng_live_stream,
14f28187 600 bt_message **message)
7cdc2bab 601{
14f28187
FD
602 enum lttng_live_iterator_status ret =
603 LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
604
605 if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) {
14f28187 606 return LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab
MD
607 }
608
14f28187
FD
609 if (lttng_live_stream->current_inactivity_ts ==
610 lttng_live_stream->last_inactivity_ts) {
7cdc2bab 611 lttng_live_stream->state = LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA;
14f28187
FD
612 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
613 goto end;
614 }
615
616 ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream,
617 message, lttng_live_stream->current_inactivity_ts);
618
619 lttng_live_stream->last_inactivity_ts =
620 lttng_live_stream->current_inactivity_ts;
621end:
622 return ret;
623}
624
625static
626int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter,
627 struct lttng_live_msg_iter *lttng_live_msg_iter,
628 const bt_message *msg, int64_t last_msg_ts_ns,
629 int64_t *ts_ns)
630{
631 const bt_clock_class *clock_class = NULL;
632 const bt_clock_snapshot *clock_snapshot = NULL;
633 int ret = 0;
c01594de 634 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 635 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
14f28187
FD
636
637 BT_ASSERT(msg);
638 BT_ASSERT(ts_ns);
639
2ece7dd0 640 BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, "
14f28187
FD
641 "last-msg-ts=%" PRId64, lttng_live_msg_iter, msg,
642 last_msg_ts_ns);
643
644 switch (bt_message_get_type(msg)) {
645 case BT_MESSAGE_TYPE_EVENT:
646 clock_class =
647 bt_message_event_borrow_stream_class_default_clock_class_const(
648 msg);
649 BT_ASSERT(clock_class);
650
0cbc2c33
PP
651 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
652 msg);
14f28187
FD
653 break;
654 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
655 clock_class =
656 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
657 msg);
658 BT_ASSERT(clock_class);
659
0cbc2c33
PP
660 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
661 msg);
14f28187
FD
662 break;
663 case BT_MESSAGE_TYPE_PACKET_END:
664 clock_class =
665 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
666 msg);
667 BT_ASSERT(clock_class);
668
0cbc2c33
PP
669 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
670 msg);
14f28187
FD
671 break;
672 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
673 clock_class =
674 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
675 msg);
676 BT_ASSERT(clock_class);
677
9b24b6aa 678 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 679 msg);
14f28187
FD
680 break;
681 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
682 clock_class =
683 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
684 msg);
685 BT_ASSERT(clock_class);
686
9b24b6aa 687 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 688 msg);
14f28187 689 break;
14f28187 690 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
0cbc2c33 691 clock_snapshot =
14f28187 692 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
0cbc2c33 693 msg);
14f28187
FD
694 break;
695 default:
696 /* All the other messages have a higher priority */
2ece7dd0 697 BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp.");
14f28187 698 *ts_ns = last_msg_ts_ns;
7cdc2bab
MD
699 goto end;
700 }
701
14f28187
FD
702 clock_class = bt_clock_snapshot_borrow_clock_class_const(clock_snapshot);
703 BT_ASSERT(clock_class);
704
705 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns);
706 if (ret) {
2ece7dd0 707 BT_COMP_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: "
14f28187
FD
708 "clock-snapshot-addr=%p", clock_snapshot);
709 goto error;
710 }
711
712 goto end;
713
14f28187
FD
714error:
715 ret = -1;
7cdc2bab 716
7cdc2bab 717end:
14f28187 718 if (ret == 0) {
2ece7dd0 719 BT_COMP_LOGD("Found message's timestamp: "
14f28187
FD
720 "iter-data-addr=%p, msg-addr=%p, "
721 "last-msg-ts=%" PRId64 ", ts=%" PRId64,
722 lttng_live_msg_iter, msg, last_msg_ts_ns, *ts_ns);
723 }
724
7cdc2bab
MD
725 return ret;
726}
727
728static
14f28187
FD
729enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_stream(
730 struct lttng_live_msg_iter *lttng_live_msg_iter,
7cdc2bab 731 struct lttng_live_stream_iterator *lttng_live_stream,
14f28187 732 bt_message **message)
7cdc2bab 733{
14f28187 734 enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
d6e69534 735 enum bt_msg_iter_status status;
14f28187 736 uint64_t session_idx, trace_idx;
c01594de 737 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 738 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
7cdc2bab 739
14f28187
FD
740 for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len;
741 session_idx++) {
742 struct lttng_live_session *session =
743 g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx);
7cdc2bab
MD
744
745 if (session->new_streams_needed) {
14f28187
FD
746 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
747 goto end;
7cdc2bab 748 }
14f28187
FD
749 for (trace_idx = 0; trace_idx < session->traces->len;
750 trace_idx++) {
751 struct lttng_live_trace *trace =
752 g_ptr_array_index(session->traces, trace_idx);
7cdc2bab 753 if (trace->new_metadata_needed) {
14f28187
FD
754 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
755 goto end;
7cdc2bab
MD
756 }
757 }
758 }
759
760 if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) {
14f28187
FD
761 ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
762 goto end;
7cdc2bab 763 }
14f28187
FD
764
765 status = bt_msg_iter_get_next_message(lttng_live_stream->msg_iter,
766 lttng_live_msg_iter->self_msg_iter, message);
7cdc2bab 767 switch (status) {
d6e69534 768 case BT_MSG_ITER_STATUS_EOF:
14f28187 769 ret = LTTNG_LIVE_ITERATOR_STATUS_END;
7cdc2bab 770 break;
d6e69534 771 case BT_MSG_ITER_STATUS_OK:
14f28187 772 ret = LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab 773 break;
d6e69534 774 case BT_MSG_ITER_STATUS_AGAIN:
7cdc2bab
MD
775 /*
776 * Continue immediately (end of packet). The next
777 * get_index may return AGAIN to delay the following
778 * attempt.
779 */
14f28187 780 ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE;
7cdc2bab 781 break;
d6e69534 782 case BT_MSG_ITER_STATUS_INVAL:
7cdc2bab 783 /* No argument provided by the user, so don't return INVAL. */
d6e69534 784 case BT_MSG_ITER_STATUS_ERROR:
7cdc2bab 785 default:
14f28187 786 ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
2ece7dd0 787 BT_COMP_LOGW("CTF msg iterator return an error or failed msg_iter=%p",
14f28187 788 lttng_live_stream->msg_iter);
7cdc2bab
MD
789 break;
790 }
14f28187
FD
791
792end:
7cdc2bab
MD
793 return ret;
794}
795
4a39caef
FD
796static
797enum lttng_live_iterator_status lttng_live_iterator_close_stream(
798 struct lttng_live_msg_iter *lttng_live_msg_iter,
799 struct lttng_live_stream_iterator *stream_iter,
800 bt_message **curr_msg)
801{
802 enum lttng_live_iterator_status live_status =
803 LTTNG_LIVE_ITERATOR_STATUS_OK;
804 /*
805 * The viewer has hung up on us so we are closing the stream. The
806 * `bt_msg_iter` should simply realize that it needs to close the
807 * stream properly by emitting the necessary stream end message.
808 */
809 enum bt_msg_iter_status status =
810 bt_msg_iter_get_next_message(stream_iter->msg_iter,
811 lttng_live_msg_iter->self_msg_iter, curr_msg);
812
813 if (status == BT_MSG_ITER_STATUS_ERROR) {
814 live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
815 goto end;
816 }
817
818 BT_ASSERT(status == BT_MSG_ITER_STATUS_OK);
819
820end:
821 return live_status;
822}
823
7cdc2bab
MD
824/*
825 * helper function:
826 * handle_no_data_streams()
827 * retry:
828 * - for each ACTIVE_NO_DATA stream:
829 * - query relayd for stream data, or quiescence info.
830 * - if need metadata, get metadata, goto retry.
831 * - if new stream, get new stream as ACTIVE_NO_DATA, goto retry
832 * - if quiescent, move to QUIESCENT streams
833 * - if fetched data, move to ACTIVE_DATA streams
834 * (at this point each stream either has data, or is quiescent)
835 *
836 *
837 * iterator_next:
838 * handle_new_streams_and_metadata()
839 * - query relayd for known streams, add them as ACTIVE_NO_DATA
840 * - query relayd for metadata
841 *
842 * call handle_active_no_data_streams()
843 *
844 * handle_quiescent_streams()
845 * - if at least one stream is ACTIVE_DATA:
846 * - peek stream event with lowest timestamp -> next_ts
847 * - for each quiescent stream
848 * - if next_ts >= quiescent end
849 * - set state to ACTIVE_NO_DATA
850 * - else
851 * - for each quiescent stream
852 * - set state to ACTIVE_NO_DATA
853 *
854 * call handle_active_no_data_streams()
855 *
856 * handle_active_data_streams()
857 * - if at least one stream is ACTIVE_DATA:
858 * - get stream event with lowest timestamp from heap
d6e69534 859 * - make that stream event the current message.
7cdc2bab
MD
860 * - move this stream heap position to its next event
861 * - if we need to fetch data from relayd, move
862 * stream to ACTIVE_NO_DATA.
863 * - return OK
864 * - return AGAIN
865 *
866 * end criterion: ctrl-c on client. If relayd exits or the session
867 * closes on the relay daemon side, we keep on waiting for streams.
868 * Eventually handle --end timestamp (also an end criterion).
869 *
870 * When disconnected from relayd: try to re-connect endlessly.
871 */
872static
14f28187
FD
873enum lttng_live_iterator_status lttng_live_iterator_next_on_stream(
874 struct lttng_live_msg_iter *lttng_live_msg_iter,
875 struct lttng_live_stream_iterator *stream_iter,
876 bt_message **curr_msg)
7cdc2bab 877{
c01594de 878 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 879 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
14f28187 880 enum lttng_live_iterator_status live_status;
7cdc2bab 881
4a39caef
FD
882 if (stream_iter->has_stream_hung_up) {
883 /*
884 * The stream has hung up and the stream was properly closed
885 * during the last call to the current function. Return _END
886 * status now so that this stream iterator is removed for the
887 * stream iterator list.
888 */
889 live_status = LTTNG_LIVE_ITERATOR_STATUS_END;
890 goto end;
891 }
892
7cdc2bab
MD
893retry:
894 print_stream_state(stream_iter);
14f28187
FD
895 live_status = lttng_live_iterator_handle_new_streams_and_metadata(
896 lttng_live_msg_iter);
897 if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
7cdc2bab
MD
898 goto end;
899 }
14f28187
FD
900 live_status = lttng_live_iterator_next_handle_one_no_data_stream(
901 lttng_live_msg_iter, stream_iter);
4a39caef 902
14f28187 903 if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
4a39caef
FD
904 if (live_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
905 /*
906 * We overwrite `live_status` since `curr_msg` is
907 * likely set to a valid message in this function.
908 */
909 live_status = lttng_live_iterator_close_stream(
910 lttng_live_msg_iter, stream_iter, curr_msg);
911 }
7cdc2bab
MD
912 goto end;
913 }
14f28187
FD
914 live_status = lttng_live_iterator_next_handle_one_quiescent_stream(
915 lttng_live_msg_iter, stream_iter, curr_msg);
916 if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
5084732e 917 BT_ASSERT(!*curr_msg);
7cdc2bab
MD
918 goto end;
919 }
14f28187 920 if (*curr_msg) {
7cdc2bab
MD
921 goto end;
922 }
14f28187
FD
923 live_status = lttng_live_iterator_next_handle_one_active_data_stream(
924 lttng_live_msg_iter, stream_iter, curr_msg);
925 if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
5084732e 926 BT_ASSERT(!*curr_msg);
7cdc2bab
MD
927 }
928
929end:
14f28187 930 if (live_status == LTTNG_LIVE_ITERATOR_STATUS_CONTINUE) {
7cdc2bab 931 goto retry;
7cdc2bab 932 }
14f28187
FD
933
934 return live_status;
7cdc2bab
MD
935}
936
937static
14f28187
FD
938enum lttng_live_iterator_status next_stream_iterator_for_trace(
939 struct lttng_live_msg_iter *lttng_live_msg_iter,
940 struct lttng_live_trace *live_trace,
941 struct lttng_live_stream_iterator **candidate_stream_iter)
7cdc2bab 942{
14f28187
FD
943 struct lttng_live_stream_iterator *curr_candidate_stream_iter = NULL;
944 enum lttng_live_iterator_status stream_iter_status;;
945 int64_t curr_candidate_msg_ts = INT64_MAX;
946 uint64_t stream_iter_idx;
c01594de 947 bt_logging_level log_level = lttng_live_msg_iter->log_level;
2ece7dd0 948 bt_self_component *self_comp = lttng_live_msg_iter->self_comp;
7cdc2bab 949
14f28187
FD
950 BT_ASSERT(live_trace);
951 BT_ASSERT(live_trace->stream_iterators);
952 /*
953 * Update the current message of every stream iterators of this trace.
954 * The current msg of every stream must have a timestamp equal or
955 * larger than the last message returned by this iterator. We must
956 * ensure monotonicity.
957 */
958 stream_iter_idx = 0;
959 while (stream_iter_idx < live_trace->stream_iterators->len) {
960 bool stream_iter_is_ended = false;
961 struct lttng_live_stream_iterator *stream_iter =
962 g_ptr_array_index(live_trace->stream_iterators,
963 stream_iter_idx);
964
965 /*
966 * Find if there is are now current message for this stream
967 * iterator get it.
968 */
969 while (!stream_iter->current_msg) {
970 bt_message *msg = NULL;
971 int64_t curr_msg_ts_ns = INT64_MAX;
972 stream_iter_status = lttng_live_iterator_next_on_stream(
973 lttng_live_msg_iter, stream_iter, &msg);
974
2ece7dd0 975 BT_COMP_LOGD("live stream iterator returned status :%s",
14f28187
FD
976 print_live_iterator_status(stream_iter_status));
977 if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
978 stream_iter_is_ended = true;
979 break;
980 }
981
982 if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
983 goto end;
984 }
985
986 BT_ASSERT(msg);
987
988 /*
989 * Get the timestamp in nanoseconds from origin of this
990 * messsage.
991 */
992 live_get_msg_ts_ns(stream_iter, lttng_live_msg_iter,
993 msg, lttng_live_msg_iter->last_msg_ts_ns,
994 &curr_msg_ts_ns);
995
996 /*
997 * Check if the message of the current live stream
998 * iterator occured at the exact same time or after the
999 * last message returned by this component's message
1000 * iterator. If not, we return an error.
1001 */
1002 if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) {
1003 stream_iter->current_msg = msg;
1004 stream_iter->current_msg_ts_ns = curr_msg_ts_ns;
1005 } else {
1006 /*
1007 * We received a message in the past. To ensure
1008 * monotonicity, we can't send it forward.
1009 */
2ece7dd0 1010 BT_COMP_LOGE("Message's timestamp is less than "
14f28187
FD
1011 "lttng-live's message iterator's last "
1012 "returned timestamp: "
1013 "lttng-live-msg-iter-addr=%p, ts=%" PRId64 ", "
1014 "last-msg-ts=%" PRId64,
1015 lttng_live_msg_iter, curr_msg_ts_ns,
1016 lttng_live_msg_iter->last_msg_ts_ns);
1017 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR;
1018 goto end;
1019 }
1020 }
1021
1022 if (!stream_iter_is_ended &&
1023 stream_iter->current_msg_ts_ns <= curr_candidate_msg_ts) {
1024 /*
1025 * Update the current best candidate message for the
1026 * stream iterator of thise live trace to be forwarded
1027 * downstream.
1028 */
1029 curr_candidate_msg_ts = stream_iter->current_msg_ts_ns;
1030 curr_candidate_stream_iter = stream_iter;
1031 }
1032
1033 if (stream_iter_is_ended) {
1034 /*
1035 * The live stream iterator is ENDed. We remove that
1036 * iterator from the list and we restart the iteration
1037 * at the beginning of the live stream iterator array
1038 * to because the removal will shuffle the array.
1039 */
1040 g_ptr_array_remove_index_fast(live_trace->stream_iterators,
1041 stream_iter_idx);
1042 stream_iter_idx = 0;
1043 } else {
1044 stream_iter_idx++;
1045 }
1046 }
1047
1048 if (curr_candidate_stream_iter) {
1049 *candidate_stream_iter = curr_candidate_stream_iter;
1050 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK;
1051 } else {
1052 /*
1053 * The only case where we don't have a candidate for this trace
1054 * is if we reached the end of all the iterators.
1055 */
1056 BT_ASSERT(live_trace->stream_iterators->len == 0);
1057 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END;
1058 }
1059
1060end:
1061 return stream_iter_status;
1062}
1063
1064static
1065enum lttng_live_iterator_status next_stream_iterator_for_session(
1066 struct lttng_live_msg_iter *lttng_live_msg_iter,
1067 struct lttng_live_session *session,
1068 struct lttng_live_stream_iterator **candidate_session_stream_iter)
1069{
1070 enum lttng_live_iterator_status stream_iter_status;
1071 uint64_t trace_idx = 0;
1072 int64_t curr_candidate_msg_ts = INT64_MAX;
1073 struct lttng_live_stream_iterator *curr_candidate_stream_iter = NULL;
1074
1075 /*
1076 * Make sure we are attached to the session and look for new streams
1077 * and metadata.
1078 */
1079 stream_iter_status = lttng_live_get_session(lttng_live_msg_iter, session);
1080 if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK &&
1081 stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_CONTINUE &&
1082 stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_END) {
7cdc2bab
MD
1083 goto end;
1084 }
14f28187
FD
1085
1086 BT_ASSERT(session->traces);
1087
1088 /*
1089 * Use while loops here rather then for loops so we can restart the
1090 * iteration if an element is removed from the array during the
1091 * looping.
1092 */
1093 while (trace_idx < session->traces->len) {
1094 bool trace_is_ended = false;
1095 struct lttng_live_stream_iterator *stream_iter;
1096 struct lttng_live_trace *trace =
1097 g_ptr_array_index(session->traces, trace_idx);
1098
1099 stream_iter_status = next_stream_iterator_for_trace(
1100 lttng_live_msg_iter, trace, &stream_iter);
1101 if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
1102 /*
1103 * All the live stream iterators for this trace are
1104 * ENDed. Remove the trace from this session.
1105 */
1106 trace_is_ended = true;
1107 } else if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
1108 goto end;
1109 }
1110
1111 if (!trace_is_ended) {
1112 BT_ASSERT(stream_iter);
1113
1114 if (stream_iter->current_msg_ts_ns <= curr_candidate_msg_ts) {
1115 curr_candidate_msg_ts = stream_iter->current_msg_ts_ns;
1116 curr_candidate_stream_iter = stream_iter;
1117 }
1118 trace_idx++;
1119 } else {
1120 g_ptr_array_remove_index_fast(session->traces, trace_idx);
1121 trace_idx = 0;
1122 }
1123 }
1124 if (curr_candidate_stream_iter) {
1125 *candidate_session_stream_iter = curr_candidate_stream_iter;
1126 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK;
7cdc2bab 1127 } else {
14f28187
FD
1128 /*
1129 * The only cases where we don't have a candidate for this
1130 * trace is:
1131 * 1. if we reached the end of all the iterators of all the
1132 * traces of this session,
1133 * 2. if we never had live stream iterator in the first place.
1134 *
1135 * In either cases, we return END.
1136 */
1137 BT_ASSERT(session->traces->len == 0);
1138 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END;
7cdc2bab
MD
1139 }
1140end:
14f28187
FD
1141 return stream_iter_status;
1142}
1143
1144static inline
1145void put_messages(bt_message_array_const msgs, uint64_t count)
1146{
1147 uint64_t i;
1148
1149 for (i = 0; i < count; i++) {
1150 BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]);
7cdc2bab 1151 }
7cdc2bab
MD
1152}
1153
d3eb6e8f 1154BT_HIDDEN
d24d5663 1155bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next(
14f28187
FD
1156 bt_self_message_iterator *self_msg_it,
1157 bt_message_array_const msgs, uint64_t capacity,
1158 uint64_t *count)
d3eb6e8f 1159{
d24d5663 1160 bt_component_class_message_iterator_next_method_status status;
14f28187
FD
1161 struct lttng_live_msg_iter *lttng_live_msg_iter =
1162 bt_self_message_iterator_get_data(self_msg_it);
1163 struct lttng_live_component *lttng_live =
1164 lttng_live_msg_iter->lttng_live_comp;
1165 enum lttng_live_iterator_status stream_iter_status;
1166 uint64_t session_idx;
1167
1168 *count = 0;
1169
1170 BT_ASSERT(lttng_live_msg_iter);
1171
1172 /*
1173 * Clear all the invalid message reference that might be left over in
1174 * the output array.
1175 */
1176 memset(msgs, 0, capacity * sizeof(*msgs));
1177
1178 /*
1179 * If no session are exposed on the relay found at the url provided by
1180 * the user, session count will be 0. In this case, we return status
1181 * end to return gracefully.
1182 */
1183 if (lttng_live_msg_iter->sessions->len == 0) {
1184 if (lttng_live->params.sess_not_found_act !=
1185 SESSION_NOT_FOUND_ACTION_CONTINUE) {
d24d5663 1186 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
14f28187
FD
1187 goto no_session;
1188 } else {
1189 /*
1190 * The are no more active session for this session
1191 * name. Retry to create a viewer session for the
1192 * requested session name.
1193 */
1194 if (lttng_live_create_viewer_session(lttng_live_msg_iter)) {
d24d5663 1195 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
14f28187
FD
1196 goto no_session;
1197 }
1198 }
1199 }
1200
1201 if (lttng_live_msg_iter->active_stream_iter == 0) {
1202 lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter);
1203 }
1204
1205 /*
1206 * Here the muxing of message is done.
1207 *
1208 * We need to iterate over all the streams of all the traces of all the
1209 * viewer sessions in order to get the message with the smallest
1210 * timestamp. In this case, a session is a viewer session and there is
1211 * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or
1212 * kernel). Each viewer session can have multiple traces, for example,
1213 * 64bit UST viewer sessions could have multiple per-pid traces.
1214 *
1215 * We iterate over the streams of each traces to update and see what is
1216 * their next message's timestamp. From those timestamps, we select the
1217 * message with the smallest timestamp as the best candidate message
1218 * for that trace and do the same thing across all the sessions.
1219 *
1220 * We then compare the timestamp of best candidate message of all the
1221 * sessions to pick the message with the smallest timestamp and we
1222 * return it.
1223 */
1224 while (*count < capacity) {
1225 struct lttng_live_stream_iterator *next_stream_iter = NULL,
1226 *candidate_stream_iter = NULL;
1227 int64_t next_msg_ts_ns = INT64_MAX;
1228
1229 BT_ASSERT(lttng_live_msg_iter->sessions);
1230 session_idx = 0;
1231 /*
1232 * Use a while loop instead of a for loop so we can restart the
1233 * iteration if we remove an element. We can safely call
1234 * next_stream_iterator_for_session() multiple times on the
1235 * same session as we only fetch a new message if there is no
1236 * current next message for each live stream iterator.
1237 * If all live stream iterator of that session already have a
1238 * current next message, the function will simply exit return
1239 * the same candidate live stream iterator every time.
1240 */
1241 while (session_idx < lttng_live_msg_iter->sessions->len) {
1242 struct lttng_live_session *session =
1243 g_ptr_array_index(lttng_live_msg_iter->sessions,
1244 session_idx);
1245
1246 /* Find the best candidate message to send downstream. */
1247 stream_iter_status = next_stream_iterator_for_session(
1248 lttng_live_msg_iter, session,
1249 &candidate_stream_iter);
1250
1251 /* If we receive an END status, it means that either:
1252 * - Those traces never had active streams (UST with no
1253 * data produced yet),
1254 * - All live stream iterators have ENDed.*/
1255 if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) {
1256 if (session->closed && session->traces->len == 0) {
1257 /*
1258 * Remove the session from the list and restart the
1259 * iteration at the beginning of the array since the
1260 * removal shuffle the elements of the array.
1261 */
1262 g_ptr_array_remove_index_fast(
1263 lttng_live_msg_iter->sessions,
1264 session_idx);
1265 session_idx = 0;
1266 } else {
1267 session_idx++;
1268 }
1269 continue;
1270 }
1271
1272 if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) {
1273 goto end;
1274 }
1275
1276 if (candidate_stream_iter->current_msg_ts_ns <= next_msg_ts_ns) {
1277 next_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns;
1278 next_stream_iter = candidate_stream_iter;
1279 }
1280
1281 session_idx++;
1282 }
1283
1284 if (!next_stream_iter) {
1285 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN;
1286 goto end;
1287 }
1288
1289 BT_ASSERT(next_stream_iter->current_msg);
1290 /* Ensure monotonicity. */
1291 BT_ASSERT(lttng_live_msg_iter->last_msg_ts_ns <=
1292 next_stream_iter->current_msg_ts_ns);
1293
1294 /*
1295 * Insert the next message to the message batch. This will set
1296 * stream iterator current messsage to NULL so that next time
1297 * we fetch the next message of that stream iterator
1298 */
1299 BT_MESSAGE_MOVE_REF(msgs[*count], next_stream_iter->current_msg);
1300 (*count)++;
1301
1302 /* Update the last timestamp in nanoseconds sent downstream. */
1303 lttng_live_msg_iter->last_msg_ts_ns = next_msg_ts_ns;
1304 next_stream_iter->current_msg_ts_ns = INT64_MAX;
1305
1306 stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK;
1307 }
1308end:
1309 switch (stream_iter_status) {
1310 case LTTNG_LIVE_ITERATOR_STATUS_OK:
1311 case LTTNG_LIVE_ITERATOR_STATUS_AGAIN:
1312 if (*count > 0) {
1313 /*
1314 * We received a again status but we have some messages
1315 * to send downstream. We send them and return OK for
1316 * now. On the next call we return again if there are
1317 * still no new message to send.
1318 */
d24d5663 1319 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
14f28187 1320 } else {
d24d5663 1321 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN;
14f28187 1322 }
7cdc2bab 1323 break;
14f28187 1324 case LTTNG_LIVE_ITERATOR_STATUS_END:
d24d5663 1325 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
7cdc2bab 1326 break;
14f28187 1327 case LTTNG_LIVE_ITERATOR_STATUS_NOMEM:
d24d5663 1328 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
14f28187
FD
1329 break;
1330 case LTTNG_LIVE_ITERATOR_STATUS_ERROR:
1331 case LTTNG_LIVE_ITERATOR_STATUS_INVAL:
1332 case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED:
d24d5663 1333 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
14f28187
FD
1334 /* Put all existing messages on error. */
1335 put_messages(msgs, *count);
7cdc2bab 1336 break;
14f28187
FD
1337 default:
1338 abort();
7cdc2bab 1339 }
14f28187
FD
1340
1341no_session:
1342 return status;
7cdc2bab 1343}
41a2b7ae 1344
7cdc2bab 1345BT_HIDDEN
d24d5663 1346bt_component_class_message_iterator_init_method_status lttng_live_msg_iter_init(
14f28187
FD
1347 bt_self_message_iterator *self_msg_it,
1348 bt_self_component_source *self_comp_src,
1349 bt_self_component_port_output *self_port)
7cdc2bab 1350{
d24d5663
PP
1351 bt_component_class_message_iterator_init_method_status ret =
1352 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
14f28187 1353 bt_self_component *self_comp =
d24d5663 1354 bt_self_component_source_as_self_component(self_comp_src);
14f28187
FD
1355 struct lttng_live_component *lttng_live;
1356 struct lttng_live_msg_iter *lttng_live_msg_iter;
c01594de 1357 bt_logging_level log_level;
14f28187
FD
1358
1359 BT_ASSERT(self_msg_it);
1360
1361 lttng_live = bt_self_component_get_data(self_comp);
c01594de 1362 log_level = lttng_live->log_level;
2ece7dd0 1363 self_comp = lttng_live->self_comp;
14f28187
FD
1364
1365 /* There can be only one downstream iterator at the same time. */
1366 BT_ASSERT(!lttng_live->has_msg_iter);
1367 lttng_live->has_msg_iter = true;
1368
1369 lttng_live_msg_iter = g_new0(struct lttng_live_msg_iter, 1);
1370 if (!lttng_live_msg_iter) {
d24d5663 1371 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
14f28187 1372 goto end;
7cdc2bab 1373 }
14f28187 1374
c01594de 1375 lttng_live_msg_iter->log_level = lttng_live->log_level;
2ece7dd0 1376 lttng_live_msg_iter->self_comp = lttng_live->self_comp;
14f28187
FD
1377 lttng_live_msg_iter->lttng_live_comp = lttng_live;
1378 lttng_live_msg_iter->self_msg_iter = self_msg_it;
1379
1380 lttng_live_msg_iter->active_stream_iter = 0;
1381 lttng_live_msg_iter->last_msg_ts_ns = INT64_MIN;
1382 lttng_live_msg_iter->sessions = g_ptr_array_new_with_free_func(
1383 (GDestroyNotify) lttng_live_destroy_session);
1384 BT_ASSERT(lttng_live_msg_iter->sessions);
1385
1386 lttng_live_msg_iter->viewer_connection =
1387 live_viewer_connection_create(lttng_live->params.url->str, false,
550004b4 1388 lttng_live_msg_iter, log_level);
14f28187
FD
1389 if (!lttng_live_msg_iter->viewer_connection) {
1390 goto error;
1391 }
1392
1393 if (lttng_live_create_viewer_session(lttng_live_msg_iter)) {
1394 goto error;
1395 }
1396 if (lttng_live_msg_iter->sessions->len == 0) {
1397 switch (lttng_live->params.sess_not_found_act) {
1398 case SESSION_NOT_FOUND_ACTION_CONTINUE:
2ece7dd0 1399 BT_COMP_LOGI("Unable to connect to the requested live viewer "
14f28187
FD
1400 "session. Keep trying to connect because of "
1401 "%s=\"%s\" component parameter: url=\"%s\"",
1402 SESS_NOT_FOUND_ACTION_PARAM,
1403 SESS_NOT_FOUND_ACTION_CONTINUE_STR,
1404 lttng_live->params.url->str);
1405 break;
1406 case SESSION_NOT_FOUND_ACTION_FAIL:
2ece7dd0 1407 BT_COMP_LOGE("Unable to connect to the requested live viewer "
14f28187
FD
1408 "session. Fail the message iterator"
1409 "initialization because of %s=\"%s\" "
1410 "component parameter: url =\"%s\"",
1411 SESS_NOT_FOUND_ACTION_PARAM,
1412 SESS_NOT_FOUND_ACTION_FAIL_STR,
1413 lttng_live->params.url->str);
7cdc2bab 1414 goto error;
14f28187 1415 case SESSION_NOT_FOUND_ACTION_END:
2ece7dd0 1416 BT_COMP_LOGI("Unable to connect to the requested live viewer "
14f28187
FD
1417 "session. End gracefully at the first _next() "
1418 "call because of %s=\"%s\" component parameter: "
1419 "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM,
1420 SESS_NOT_FOUND_ACTION_END_STR,
1421 lttng_live->params.url->str);
1422 break;
bce81ff2
JR
1423 case SESSION_NOT_FOUND_ACTION_UNKNOWN:
1424 default:
1425 /* Fallthrough */
1426 BT_COMP_LOGE("Unknown action for session not found"
1427 "error. Fail the message iterator"
1428 "initialization because of %s=\"%s\" "
1429 "component parameter: url =\"%s\"",
1430 SESS_NOT_FOUND_ACTION_PARAM,
1431 SESS_NOT_FOUND_ACTION_FAIL_STR,
1432 lttng_live->params.url->str);
1433 break;
7cdc2bab 1434 }
7cdc2bab
MD
1435 }
1436
14f28187
FD
1437 bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter);
1438
1439 goto end;
1440error:
d24d5663 1441 ret = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
14f28187 1442 lttng_live_msg_iter_destroy(lttng_live_msg_iter);
7cdc2bab 1443end:
41a2b7ae 1444 return ret;
7cdc2bab
MD
1445}
1446
1447static
d24d5663
PP
1448bt_component_class_query_method_status lttng_live_query_list_sessions(
1449 const bt_value *params, const bt_value **result,
1450 bt_logging_level log_level)
7cdc2bab 1451{
d24d5663
PP
1452 bt_component_class_query_method_status status =
1453 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
14f28187 1454 const bt_value *url_value = NULL;
7cdc2bab 1455 const char *url;
14f28187 1456 struct live_viewer_connection *viewer_connection = NULL;
2ece7dd0 1457 bt_self_component *self_comp = NULL;
7cdc2bab 1458
14f28187
FD
1459 url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM);
1460 if (!url_value) {
a635e507
SM
1461 BT_COMP_LOGE("Mandatory `%s` parameter missing", URL_PARAM);
1462 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
1463 goto error;
1464 }
1465
14f28187 1466 if (!bt_value_is_string(url_value)) {
a635e507 1467 BT_COMP_LOGE("`%s` parameter is required to be a string value",
14f28187 1468 URL_PARAM);
a635e507 1469 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
7cdc2bab
MD
1470 goto error;
1471 }
1472
14f28187
FD
1473 url = bt_value_string_get(url_value);
1474
550004b4
PP
1475 viewer_connection = live_viewer_connection_create(url, true, NULL,
1476 log_level);
7cdc2bab 1477 if (!viewer_connection) {
7cdc2bab
MD
1478 goto error;
1479 }
1480
14f28187
FD
1481 status = live_viewer_connection_list_sessions(viewer_connection,
1482 result);
d24d5663 1483 if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) {
c7eee084
PP
1484 goto error;
1485 }
1486
7cdc2bab 1487 goto end;
c7eee084 1488
7cdc2bab 1489error:
14f28187 1490 BT_VALUE_PUT_REF_AND_RESET(*result);
c7eee084 1491
14f28187 1492 if (status >= 0) {
d24d5663 1493 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
c7eee084
PP
1494 }
1495
7cdc2bab
MD
1496end:
1497 if (viewer_connection) {
14f28187 1498 live_viewer_connection_destroy(viewer_connection);
7cdc2bab 1499 }
14f28187 1500 return status;
7cdc2bab
MD
1501}
1502
1503BT_HIDDEN
d24d5663
PP
1504bt_component_class_query_method_status lttng_live_query(
1505 bt_self_component_class_source *comp_class,
3c729b9a 1506 bt_private_query_executor *priv_query_exec,
14f28187 1507 const char *object, const bt_value *params,
7c14d641 1508 __attribute__((unused)) void *method_data,
3c729b9a 1509 const bt_value **result)
7cdc2bab 1510{
d24d5663
PP
1511 bt_component_class_query_method_status status =
1512 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
2ece7dd0 1513 bt_self_component *self_comp = NULL;
3c729b9a
PP
1514 bt_logging_level log_level = bt_query_executor_get_logging_level(
1515 bt_private_query_executor_as_query_executor_const(
1516 priv_query_exec));
c7eee084 1517
7cdc2bab 1518 if (strcmp(object, "sessions") == 0) {
c01594de
PP
1519 status = lttng_live_query_list_sessions(params, result,
1520 log_level);
14f28187 1521 } else {
32a09ecd 1522 BT_COMP_LOGI("Unknown query object `%s`", object);
76b6c2f7 1523 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT;
14f28187 1524 goto end;
7cdc2bab 1525 }
14f28187
FD
1526
1527end:
1528 return status;
7cdc2bab
MD
1529}
1530
1531static
1532void lttng_live_component_destroy_data(struct lttng_live_component *lttng_live)
1533{
19bdff20
FD
1534 if (!lttng_live) {
1535 return;
1536 }
14f28187
FD
1537 if (lttng_live->params.url) {
1538 g_string_free(lttng_live->params.url, TRUE);
7cdc2bab
MD
1539 }
1540 g_free(lttng_live);
1541}
1542
1543BT_HIDDEN
14f28187 1544void lttng_live_component_finalize(bt_self_component_source *component)
7cdc2bab 1545{
14f28187
FD
1546 void *data = bt_self_component_get_data(
1547 bt_self_component_source_as_self_component(component));
7cdc2bab
MD
1548
1549 if (!data) {
1550 return;
1551 }
1552 lttng_live_component_destroy_data(data);
1553}
1554
1555static
14f28187
FD
1556enum session_not_found_action parse_session_not_found_action_param(
1557 const bt_value *no_session_param)
1558{
1559 enum session_not_found_action action;
1560 const char *no_session_act_str;
1561 no_session_act_str = bt_value_string_get(no_session_param);
1562 if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) {
1563 action = SESSION_NOT_FOUND_ACTION_CONTINUE;
1564 } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) {
1565 action = SESSION_NOT_FOUND_ACTION_FAIL;
1566 } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0) {
1567 action = SESSION_NOT_FOUND_ACTION_END;
1568 } else {
bce81ff2 1569 action = SESSION_NOT_FOUND_ACTION_UNKNOWN;
14f28187
FD
1570 }
1571
1572 return action;
1573}
1574
c01594de 1575struct lttng_live_component *lttng_live_component_create(const bt_value *params,
2ece7dd0 1576 bt_logging_level log_level, bt_self_component *self_comp)
7cdc2bab
MD
1577{
1578 struct lttng_live_component *lttng_live;
14f28187 1579 const bt_value *value = NULL;
7cdc2bab 1580 const char *url;
7cdc2bab
MD
1581
1582 lttng_live = g_new0(struct lttng_live_component, 1);
1583 if (!lttng_live) {
1584 goto end;
1585 }
c01594de 1586 lttng_live->log_level = log_level;
2ece7dd0 1587 lttng_live->self_comp = self_comp;
7cdc2bab 1588 lttng_live->max_query_size = MAX_QUERY_SIZE;
14f28187
FD
1589 lttng_live->has_msg_iter = false;
1590
1591 value = bt_value_map_borrow_entry_value_const(params, URL_PARAM);
1592 if (!value || !bt_value_is_string(value)) {
2ece7dd0 1593 BT_COMP_LOGW("Mandatory `%s` parameter missing or not a string",
14f28187 1594 URL_PARAM);
7cdc2bab
MD
1595 goto error;
1596 }
601b0d3c 1597 url = bt_value_string_get(value);
14f28187
FD
1598 lttng_live->params.url = g_string_new(url);
1599 if (!lttng_live->params.url) {
7cdc2bab
MD
1600 goto error;
1601 }
14f28187
FD
1602
1603 value = bt_value_map_borrow_entry_value_const(params,
1604 SESS_NOT_FOUND_ACTION_PARAM);
1605
1606 if (value && bt_value_is_string(value)) {
1607 lttng_live->params.sess_not_found_act =
1608 parse_session_not_found_action_param(value);
bce81ff2 1609 if (lttng_live->params.sess_not_found_act == SESSION_NOT_FOUND_ACTION_UNKNOWN) {
2ece7dd0 1610 BT_COMP_LOGE("Unexpected value for `%s` parameter: "
14f28187
FD
1611 "value=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM,
1612 bt_value_string_get(value));
1613 goto error;
1614 }
1615 } else {
2ece7dd0 1616 BT_COMP_LOGW("Optional `%s` parameter is missing or "
14f28187
FD
1617 "not a string value. Defaulting to %s=\"%s\".",
1618 SESS_NOT_FOUND_ACTION_PARAM,
1619 SESS_NOT_FOUND_ACTION_PARAM,
1620 SESS_NOT_FOUND_ACTION_CONTINUE_STR);
1621 lttng_live->params.sess_not_found_act =
1622 SESSION_NOT_FOUND_ACTION_CONTINUE;
7cdc2bab 1623 }
4c66436f 1624
7cdc2bab
MD
1625 goto end;
1626
1627error:
1628 lttng_live_component_destroy_data(lttng_live);
1629 lttng_live = NULL;
1630end:
1631 return lttng_live;
d3e4dcd8
PP
1632}
1633
f3bc2010 1634BT_HIDDEN
d24d5663 1635bt_component_class_init_method_status lttng_live_component_init(
c01594de 1636 bt_self_component_source *self_comp_src,
c88dd1cb 1637 const bt_value *params, __attribute__((unused)) void *init_method_data)
f3bc2010 1638{
7cdc2bab 1639 struct lttng_live_component *lttng_live;
d24d5663
PP
1640 bt_component_class_init_method_status ret =
1641 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
c01594de
PP
1642 bt_self_component *self_comp =
1643 bt_self_component_source_as_self_component(self_comp_src);
1644 bt_logging_level log_level = bt_component_get_logging_level(
1645 bt_self_component_as_component(self_comp));
d24d5663 1646 bt_self_component_add_port_status add_port_status;
7cdc2bab 1647
2ece7dd0 1648 lttng_live = lttng_live_component_create(params, log_level, self_comp);
7cdc2bab 1649 if (!lttng_live) {
d24d5663 1650 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
19bdff20 1651 goto error;
7cdc2bab
MD
1652 }
1653
d24d5663
PP
1654 add_port_status = bt_self_component_source_add_output_port(
1655 self_comp_src, "out", NULL, NULL);
1656 switch (add_port_status) {
1657 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
1658 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
1659 goto error;
1660 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
1661 ret = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
19bdff20 1662 goto error;
d24d5663
PP
1663 default:
1664 break;
147337a3 1665 }
7cdc2bab 1666
c01594de 1667 bt_self_component_set_data(self_comp, lttng_live);
19bdff20 1668 goto end;
7cdc2bab 1669
19bdff20
FD
1670error:
1671 lttng_live_component_destroy_data(lttng_live);
1672 lttng_live = NULL;
7cdc2bab
MD
1673end:
1674 return ret;
d85ef162 1675}
This page took 0.133883 seconds and 4 git commands to generate.