Commit | Line | Data |
---|---|---|
f3bc2010 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
f3bc2010 | 3 | * |
14f28187 | 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
f3bc2010 | 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
7cdc2bab | 6 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc2010 | 7 | * |
0235b0db | 8 | * Babeltrace CTF LTTng-live Client Component |
f3bc2010 JG |
9 | */ |
10 | ||
2ece7dd0 | 11 | #define BT_COMP_LOG_SELF_COMP self_comp |
c01594de | 12 | #define BT_LOG_OUTPUT_LEVEL log_level |
350ad6c1 | 13 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE" |
d9c39b0a | 14 | #include "logging/comp-logging.h" |
020bc26f | 15 | |
14f28187 | 16 | #include <inttypes.h> |
c4f23e30 | 17 | #include <stdbool.h> |
14f28187 FD |
18 | #include <unistd.h> |
19 | ||
3c22a242 FD |
20 | #include <glib.h> |
21 | ||
578e048b | 22 | #include "common/assert.h" |
3fadfbc0 | 23 | #include <babeltrace2/babeltrace.h> |
578e048b | 24 | #include "compat/compiler.h" |
3fadfbc0 | 25 | #include <babeltrace2/types.h> |
f3bc2010 | 26 | |
376fc2bd | 27 | #include "plugins/common/muxing/muxing.h" |
80aff5ef | 28 | #include "plugins/common/param-validation/param-validation.h" |
376fc2bd | 29 | |
7cdc2bab MD |
30 | #include "data-stream.h" |
31 | #include "metadata.h" | |
14f28187 | 32 | #include "lttng-live.h" |
7cdc2bab | 33 | |
14f28187 FD |
34 | #define MAX_QUERY_SIZE (256*1024) |
35 | #define URL_PARAM "url" | |
312df793 | 36 | #define INPUTS_PARAM "inputs" |
14f28187 FD |
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" | |
7cdc2bab | 41 | |
2ece7dd0 | 42 | #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__) |
7cdc2bab | 43 | |
14f28187 | 44 | static |
b607a4f7 FD |
45 | const char *lttng_live_iterator_status_string( |
46 | enum lttng_live_iterator_status status) | |
14f28187 FD |
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: | |
498e7994 | 66 | bt_common_abort(); |
14f28187 FD |
67 | } |
68 | } | |
69 | ||
70 | static | |
ecb4ba8a | 71 | const char *lttng_live_stream_state_string(enum lttng_live_stream_state state) |
7cdc2bab | 72 | { |
ecb4ba8a | 73 | switch (state) { |
7cdc2bab MD |
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 | } | |
7cdc2bab | 88 | |
34533ae0 FD |
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 | ||
ecb4ba8a | 104 | #define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \ |
14f28187 | 105 | do { \ |
a043c362 FD |
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, \ | |
ecb4ba8a | 109 | lttng_live_stream_state_string(live_stream_iter->state), \ |
a043c362 FD |
110 | live_stream_iter->last_inactivity_ts.is_set, \ |
111 | live_stream_iter->last_inactivity_ts.value, \ | |
14f28187 FD |
112 | live_stream_iter->current_inactivity_ts); \ |
113 | } while (0); | |
6f79a7cf MD |
114 | |
115 | BT_HIDDEN | |
9b4f9b42 | 116 | bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter) |
6f79a7cf | 117 | { |
14f28187 | 118 | bool ret; |
6f79a7cf | 119 | |
9b4f9b42 | 120 | if (!msg_iter) { |
14f28187 FD |
121 | ret = false; |
122 | goto end; | |
7cdc2bab | 123 | } |
7cdc2bab | 124 | |
9b4f9b42 PP |
125 | ret = bt_self_message_iterator_is_interrupted( |
126 | msg_iter->self_msg_iter); | |
4bf0e537 | 127 | |
14f28187 FD |
128 | end: |
129 | return ret; | |
7cdc2bab MD |
130 | } |
131 | ||
132 | static | |
36e94ad6 | 133 | struct lttng_live_trace *lttng_live_session_borrow_trace_by_id(struct lttng_live_session *session, |
7cdc2bab | 134 | uint64_t trace_id) |
d3e4dcd8 | 135 | { |
14f28187 FD |
136 | uint64_t trace_idx; |
137 | struct lttng_live_trace *ret_trace = NULL; | |
7cdc2bab | 138 | |
14f28187 FD |
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); | |
7cdc2bab | 142 | if (trace->id == trace_id) { |
14f28187 FD |
143 | ret_trace = trace; |
144 | goto end; | |
7cdc2bab MD |
145 | } |
146 | } | |
14f28187 FD |
147 | |
148 | end: | |
149 | return ret_trace; | |
d3eb6e8f PP |
150 | } |
151 | ||
7cdc2bab | 152 | static |
14f28187 | 153 | void lttng_live_destroy_trace(struct lttng_live_trace *trace) |
7cdc2bab | 154 | { |
c01594de | 155 | bt_logging_level log_level = trace->log_level; |
2ece7dd0 | 156 | bt_self_component *self_comp = trace->self_comp; |
c01594de | 157 | |
ecb4ba8a | 158 | BT_COMP_LOGD("Destroying live trace: trace-id=%"PRIu64, trace->id); |
7cdc2bab | 159 | |
14f28187 FD |
160 | BT_ASSERT(trace->stream_iterators); |
161 | g_ptr_array_free(trace->stream_iterators, TRUE); | |
5bd230f4 | 162 | |
14f28187 FD |
163 | BT_TRACE_PUT_REF_AND_RESET(trace->trace); |
164 | BT_TRACE_CLASS_PUT_REF_AND_RESET(trace->trace_class); | |
5bd230f4 | 165 | |
7cdc2bab | 166 | lttng_live_metadata_fini(trace); |
7cdc2bab MD |
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; | |
c01594de | 175 | bt_logging_level log_level = session->log_level; |
2ece7dd0 | 176 | bt_self_component *self_comp = session->self_comp; |
7cdc2bab | 177 | |
ecb4ba8a FD |
178 | BT_COMP_LOGD("Creating live trace: " |
179 | "session-id=%"PRIu64", trace-id=%"PRIu64, | |
180 | session->id, trace_id); | |
7cdc2bab MD |
181 | trace = g_new0(struct lttng_live_trace, 1); |
182 | if (!trace) { | |
1419db2b FD |
183 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
184 | "Failed to allocate live trace"); | |
7cdc2bab MD |
185 | goto error; |
186 | } | |
c01594de | 187 | trace->log_level = session->log_level; |
2ece7dd0 | 188 | trace->self_comp = session->self_comp; |
7cdc2bab MD |
189 | trace->session = session; |
190 | trace->id = trace_id; | |
14f28187 FD |
191 | trace->trace_class = NULL; |
192 | trace->trace = NULL; | |
193 | trace->stream_iterators = g_ptr_array_new_with_free_func( | |
0f1979c3 | 194 | (GDestroyNotify) lttng_live_stream_iterator_destroy); |
14f28187 | 195 | BT_ASSERT(trace->stream_iterators); |
76bbaebc | 196 | trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; |
14f28187 FD |
197 | g_ptr_array_add(session->traces, trace); |
198 | ||
7cdc2bab MD |
199 | goto end; |
200 | error: | |
201 | g_free(trace); | |
202 | trace = NULL; | |
203 | end: | |
204 | return trace; | |
205 | } | |
206 | ||
207 | BT_HIDDEN | |
36e94ad6 | 208 | struct lttng_live_trace *lttng_live_session_borrow_or_create_trace_by_id( |
14f28187 | 209 | struct lttng_live_session *session, uint64_t trace_id) |
7cdc2bab MD |
210 | { |
211 | struct lttng_live_trace *trace; | |
212 | ||
36e94ad6 | 213 | trace = lttng_live_session_borrow_trace_by_id(session, trace_id); |
7cdc2bab | 214 | if (trace) { |
14f28187 | 215 | goto end; |
7cdc2bab | 216 | } |
7cdc2bab | 217 | |
14f28187 FD |
218 | /* The session is the owner of the newly created trace. */ |
219 | trace = lttng_live_create_trace(session, trace_id); | |
7cdc2bab | 220 | |
14f28187 FD |
221 | end: |
222 | return trace; | |
7cdc2bab MD |
223 | } |
224 | ||
225 | BT_HIDDEN | |
14f28187 | 226 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, |
06994c71 MD |
227 | uint64_t session_id, const char *hostname, |
228 | const char *session_name) | |
7cdc2bab MD |
229 | { |
230 | int ret = 0; | |
14f28187 | 231 | struct lttng_live_session *session; |
c01594de | 232 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 233 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
7cdc2bab | 234 | |
ecb4ba8a FD |
235 | BT_COMP_LOGD("Adding live session: " |
236 | "session-id=%" PRIu64 ", hostname=\"%s\" session-name=\"%s\"", | |
237 | session_id, hostname, session_name); | |
238 | ||
14f28187 FD |
239 | session = g_new0(struct lttng_live_session, 1); |
240 | if (!session) { | |
1419db2b FD |
241 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
242 | "Failed to allocate live session"); | |
7cdc2bab MD |
243 | goto error; |
244 | } | |
245 | ||
c01594de | 246 | session->log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 247 | session->self_comp = lttng_live_msg_iter->self_comp; |
14f28187 FD |
248 | session->id = session_id; |
249 | session->traces = g_ptr_array_new_with_free_func( | |
0f1979c3 | 250 | (GDestroyNotify) lttng_live_destroy_trace); |
14f28187 FD |
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); | |
7cdc2bab | 259 | |
14f28187 | 260 | g_ptr_array_add(lttng_live_msg_iter->sessions, session); |
7cdc2bab MD |
261 | goto end; |
262 | error: | |
14f28187 | 263 | g_free(session); |
7cdc2bab MD |
264 | ret = -1; |
265 | end: | |
266 | return ret; | |
267 | } | |
268 | ||
269 | static | |
270 | void lttng_live_destroy_session(struct lttng_live_session *session) | |
271 | { | |
c01594de | 272 | bt_logging_level log_level; |
2ece7dd0 | 273 | bt_self_component *self_comp; |
14f28187 FD |
274 | |
275 | if (!session) { | |
276 | goto end; | |
277 | } | |
7cdc2bab | 278 | |
c01594de | 279 | log_level = session->log_level; |
2ece7dd0 | 280 | self_comp = session->self_comp; |
ecb4ba8a FD |
281 | BT_COMP_LOGD("Destroying live session: " |
282 | "session-id=%"PRIu64", session-name=\"%s\"", | |
283 | session->id, session->session_name->str); | |
7cdc2bab | 284 | if (session->id != -1ULL) { |
36e94ad6 | 285 | if (lttng_live_session_detach(session)) { |
0757916e FD |
286 | if (!lttng_live_graph_is_canceled( |
287 | session->lttng_live_msg_iter)) { | |
4c66436f | 288 | /* Old relayd cannot detach sessions. */ |
2ece7dd0 | 289 | BT_COMP_LOGD("Unable to detach lttng live session %" PRIu64, |
4c66436f MD |
290 | session->id); |
291 | } | |
7cdc2bab MD |
292 | } |
293 | session->id = -1ULL; | |
294 | } | |
14f28187 FD |
295 | |
296 | if (session->traces) { | |
297 | g_ptr_array_free(session->traces, TRUE); | |
7cdc2bab | 298 | } |
14f28187 | 299 | |
06994c71 MD |
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 | } | |
7cdc2bab | 306 | g_free(session); |
14f28187 FD |
307 | |
308 | end: | |
309 | return; | |
7cdc2bab MD |
310 | } |
311 | ||
14f28187 FD |
312 | static |
313 | void lttng_live_msg_iter_destroy(struct lttng_live_msg_iter *lttng_live_msg_iter) | |
7cdc2bab | 314 | { |
14f28187 FD |
315 | if (!lttng_live_msg_iter) { |
316 | goto end; | |
7cdc2bab | 317 | } |
7cdc2bab | 318 | |
14f28187 FD |
319 | if (lttng_live_msg_iter->sessions) { |
320 | g_ptr_array_free(lttng_live_msg_iter->sessions, TRUE); | |
7cdc2bab | 321 | } |
14f28187 | 322 | |
b9e6ec43 FD |
323 | if (lttng_live_msg_iter->viewer_connection) { |
324 | live_viewer_connection_destroy(lttng_live_msg_iter->viewer_connection); | |
325 | } | |
14f28187 FD |
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); | |
7cdc2bab MD |
349 | } |
350 | ||
351 | static | |
14f28187 | 352 | enum lttng_live_iterator_status lttng_live_iterator_next_check_stream_state( |
7cdc2bab MD |
353 | struct lttng_live_stream_iterator *lttng_live_stream) |
354 | { | |
c01594de | 355 | bt_logging_level log_level = lttng_live_stream->log_level; |
2ece7dd0 | 356 | bt_self_component *self_comp = lttng_live_stream->self_comp; |
c01594de | 357 | |
7cdc2bab MD |
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. */ | |
2ece7dd0 | 364 | BT_COMP_LOGF("Unexpected stream state \"ACTIVE_NO_DATA\""); |
498e7994 | 365 | bt_common_abort(); |
7cdc2bab MD |
366 | case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA: |
367 | /* Invalid state. */ | |
2ece7dd0 | 368 | BT_COMP_LOGF("Unexpected stream state \"QUIESCENT_NO_DATA\""); |
498e7994 | 369 | bt_common_abort(); |
7cdc2bab MD |
370 | case LTTNG_LIVE_STREAM_EOF: |
371 | break; | |
372 | } | |
14f28187 | 373 | return LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab MD |
374 | } |
375 | ||
376 | /* | |
f93afbf9 FD |
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. | |
7cdc2bab MD |
383 | */ |
384 | static | |
14f28187 FD |
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, | |
7cdc2bab MD |
387 | struct lttng_live_stream_iterator *lttng_live_stream) |
388 | { | |
c01594de | 389 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 390 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
0f1979c3 | 391 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab | 392 | enum lttng_live_stream_state orig_state = lttng_live_stream->state; |
0f1979c3 | 393 | struct packet_index index; |
7cdc2bab | 394 | |
76bbaebc FD |
395 | if (lttng_live_stream->trace->metadata_stream_state == |
396 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { | |
f93afbf9 FD |
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); | |
14f28187 | 399 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
7cdc2bab MD |
400 | goto end; |
401 | } | |
f93afbf9 | 402 | |
7cdc2bab | 403 | if (lttng_live_stream->trace->session->new_streams_needed) { |
f93afbf9 FD |
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); | |
14f28187 | 406 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
7cdc2bab MD |
407 | goto end; |
408 | } | |
f93afbf9 | 409 | |
14f28187 FD |
410 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_NO_DATA && |
411 | lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA) { | |
7cdc2bab MD |
412 | goto end; |
413 | } | |
14f28187 | 414 | ret = lttng_live_get_next_index(lttng_live_msg_iter, lttng_live_stream, |
f93afbf9 | 415 | &index); |
14f28187 | 416 | if (ret != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
7cdc2bab MD |
417 | goto end; |
418 | } | |
f93afbf9 | 419 | |
98b15851 | 420 | BT_ASSERT_DBG(lttng_live_stream->state != LTTNG_LIVE_STREAM_EOF); |
f93afbf9 | 421 | |
7cdc2bab | 422 | if (lttng_live_stream->state == LTTNG_LIVE_STREAM_QUIESCENT) { |
a043c362 | 423 | uint64_t last_inact_ts = lttng_live_stream->last_inactivity_ts.value, |
14f28187 FD |
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) { | |
a043c362 FD |
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 | ||
14f28187 | 435 | ret = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
ecb4ba8a | 436 | LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream); |
7cdc2bab | 437 | } else { |
14f28187 | 438 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
7cdc2bab MD |
439 | } |
440 | goto end; | |
441 | } | |
f93afbf9 | 442 | |
7cdc2bab MD |
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; | |
f93afbf9 FD |
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 | ||
7cdc2bab | 455 | end: |
14f28187 FD |
456 | if (ret == LTTNG_LIVE_ITERATOR_STATUS_OK) { |
457 | ret = lttng_live_iterator_next_check_stream_state(lttng_live_stream); | |
7cdc2bab MD |
458 | } |
459 | return ret; | |
460 | } | |
461 | ||
462 | /* | |
14f28187 FD |
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. | |
7cdc2bab MD |
468 | */ |
469 | static | |
14f28187 FD |
470 | enum lttng_live_iterator_status lttng_live_get_session( |
471 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
7cdc2bab MD |
472 | struct lttng_live_session *session) |
473 | { | |
1419db2b FD |
474 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
475 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
14f28187 FD |
476 | enum lttng_live_iterator_status status; |
477 | uint64_t trace_idx; | |
7cdc2bab | 478 | |
14f28187 | 479 | if (!session->attached) { |
f93afbf9 FD |
480 | BT_COMP_LOGD("Attach to session: session-id=%" PRIu64, |
481 | session->id); | |
f79c2d7a | 482 | enum lttng_live_viewer_status attach_status = |
36e94ad6 | 483 | lttng_live_session_attach(session, |
851de941 | 484 | lttng_live_msg_iter->self_msg_iter); |
f79c2d7a | 485 | if (attach_status != LTTNG_LIVE_VIEWER_STATUS_OK) { |
0757916e | 486 | if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { |
1419db2b FD |
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(); | |
14f28187 FD |
494 | status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
495 | } else { | |
496 | status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1419db2b FD |
497 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
498 | "Error attaching to LTTng live session"); | |
14f28187 FD |
499 | } |
500 | goto end; | |
4c66436f | 501 | } |
7cdc2bab | 502 | } |
14f28187 | 503 | |
f93afbf9 FD |
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 | ||
36e94ad6 | 508 | status = lttng_live_session_get_new_streams(session, |
851de941 | 509 | lttng_live_msg_iter->self_msg_iter); |
14f28187 FD |
510 | if (status != LTTNG_LIVE_ITERATOR_STATUS_OK && |
511 | status != LTTNG_LIVE_ITERATOR_STATUS_END) { | |
512 | goto end; | |
7cdc2bab | 513 | } |
f93afbf9 | 514 | |
c28512ab FD |
515 | trace_idx = 0; |
516 | while (trace_idx < session->traces->len) { | |
14f28187 FD |
517 | struct lttng_live_trace *trace = |
518 | g_ptr_array_index(session->traces, trace_idx); | |
519 | ||
7cdc2bab | 520 | status = lttng_live_metadata_update(trace); |
c28512ab | 521 | switch (status) { |
0ca5c4f9 | 522 | case LTTNG_LIVE_ITERATOR_STATUS_END: |
c28512ab FD |
523 | case LTTNG_LIVE_ITERATOR_STATUS_OK: |
524 | trace_idx++; | |
525 | break; | |
0ca5c4f9 FD |
526 | case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: |
527 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
528 | goto end; | |
c28512ab | 529 | default: |
ecb4ba8a FD |
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); | |
14f28187 | 535 | goto end; |
7cdc2bab MD |
536 | } |
537 | } | |
f93afbf9 FD |
538 | |
539 | /* | |
540 | * Now that we have the metadata we can initialize the downstream | |
541 | * iterator. | |
542 | */ | |
851de941 SM |
543 | status = lttng_live_lazy_msg_init(session, |
544 | lttng_live_msg_iter->self_msg_iter); | |
14f28187 FD |
545 | |
546 | end: | |
547 | return status; | |
7cdc2bab MD |
548 | } |
549 | ||
7cdc2bab | 550 | static |
14f28187 | 551 | void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) |
7cdc2bab | 552 | { |
14f28187 | 553 | uint64_t session_idx, trace_idx; |
f93afbf9 FD |
554 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
555 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
7cdc2bab | 556 | |
14f28187 FD |
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); | |
f93afbf9 FD |
561 | BT_COMP_LOGD("Force marking session as needing new streams: " |
562 | "session-id=%" PRIu64, session->id); | |
7cdc2bab | 563 | session->new_streams_needed = true; |
14f28187 FD |
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); | |
f93afbf9 FD |
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); | |
76bbaebc FD |
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; | |
7cdc2bab MD |
576 | } |
577 | } | |
578 | } | |
579 | ||
580 | static | |
14f28187 FD |
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) | |
7cdc2bab | 584 | { |
f79c2d7a FD |
585 | enum lttng_live_iterator_status status; |
586 | enum lttng_live_viewer_status viewer_status; | |
1419db2b FD |
587 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
588 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
14f28187 FD |
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 | ||
883ffb2f | 594 | BT_COMP_LOGD("Update data and metadata of all sessions: " |
ecb4ba8a | 595 | "live-msg-iter-addr=%p", lttng_live_msg_iter); |
7cdc2bab | 596 | /* |
14f28187 | 597 | * In a remotely distant future, we could add a "new |
7cdc2bab MD |
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 | */ | |
14f28187 FD |
602 | if (lttng_live_msg_iter->sessions->len == 0) { |
603 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
ecb4ba8a | 604 | BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter"); |
f79c2d7a | 605 | status = LTTNG_LIVE_ITERATOR_STATUS_END; |
14f28187 FD |
606 | goto end; |
607 | } else { | |
ecb4ba8a | 608 | BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter"); |
14f28187 FD |
609 | /* |
610 | * Retry to create a viewer session for the requested | |
611 | * session name. | |
612 | */ | |
f79c2d7a FD |
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 | } | |
14f28187 FD |
624 | goto end; |
625 | } | |
626 | } | |
7cdc2bab | 627 | } |
14f28187 FD |
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); | |
f79c2d7a FD |
633 | status = lttng_live_get_session(lttng_live_msg_iter, session); |
634 | switch (status) { | |
14f28187 | 635 | case LTTNG_LIVE_ITERATOR_STATUS_OK: |
7cdc2bab | 636 | break; |
14f28187 | 637 | case LTTNG_LIVE_ITERATOR_STATUS_END: |
f79c2d7a | 638 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab MD |
639 | break; |
640 | default: | |
641 | goto end; | |
642 | } | |
643 | if (!session->closed) { | |
644 | nr_sessions_opened++; | |
645 | } | |
646 | } | |
f79c2d7a FD |
647 | |
648 | if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE && | |
14f28187 | 649 | nr_sessions_opened == 0) { |
f79c2d7a FD |
650 | status = LTTNG_LIVE_ITERATOR_STATUS_END; |
651 | } else { | |
652 | status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
7cdc2bab | 653 | } |
f79c2d7a FD |
654 | |
655 | end: | |
656 | return status; | |
7cdc2bab MD |
657 | } |
658 | ||
659 | static | |
14f28187 FD |
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, | |
cad707e2 | 663 | const bt_message **message, uint64_t timestamp) |
7cdc2bab | 664 | { |
0f1979c3 | 665 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1419db2b FD |
666 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
667 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
14f28187 | 668 | bt_message *msg = NULL; |
7cdc2bab | 669 | |
14f28187 FD |
670 | BT_ASSERT(stream_iter->trace->clock_class); |
671 | ||
f93afbf9 FD |
672 | BT_COMP_LOGD("Emitting inactivity message for stream: ctf-stream-id=%" PRIu64 |
673 | ", viewer-stream-id=%" PRIu64 ", timestamp=%" PRIu64, | |
1a258819 | 674 | stream_iter->ctf_stream_class_id.value, |
f93afbf9 FD |
675 | stream_iter->viewer_stream_id, timestamp); |
676 | ||
14f28187 | 677 | msg = bt_message_message_iterator_inactivity_create( |
0f1979c3 FD |
678 | lttng_live_msg_iter->self_msg_iter, |
679 | stream_iter->trace->clock_class, timestamp); | |
d6e69534 | 680 | if (!msg) { |
1419db2b FD |
681 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
682 | "Error emitting message iterator inactivity message"); | |
7cdc2bab MD |
683 | goto error; |
684 | } | |
14f28187 | 685 | |
d6e69534 | 686 | *message = msg; |
7cdc2bab | 687 | end: |
7cdc2bab MD |
688 | return ret; |
689 | ||
690 | error: | |
14f28187 | 691 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
d6e69534 | 692 | bt_message_put_ref(msg); |
7cdc2bab MD |
693 | goto end; |
694 | } | |
695 | ||
696 | static | |
14f28187 FD |
697 | enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream( |
698 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
7cdc2bab | 699 | struct lttng_live_stream_iterator *lttng_live_stream, |
cad707e2 | 700 | const bt_message **message) |
7cdc2bab | 701 | { |
0f1979c3 | 702 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab MD |
703 | |
704 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_QUIESCENT) { | |
14f28187 | 705 | return LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab MD |
706 | } |
707 | ||
f93afbf9 FD |
708 | /* |
709 | * Check if we already sent an inactivty message downstream for this | |
710 | * `current_inactivity_ts` value. | |
711 | */ | |
a043c362 FD |
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) { | |
34533ae0 FD |
715 | lttng_live_stream_iterator_set_state(lttng_live_stream, |
716 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA); | |
717 | ||
14f28187 FD |
718 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
719 | goto end; | |
720 | } | |
721 | ||
722 | ret = emit_inactivity_message(lttng_live_msg_iter, lttng_live_stream, | |
0f1979c3 | 723 | message, lttng_live_stream->current_inactivity_ts); |
14f28187 | 724 | |
a043c362 | 725 | lttng_live_stream->last_inactivity_ts.value = |
0f1979c3 | 726 | lttng_live_stream->current_inactivity_ts; |
a043c362 | 727 | lttng_live_stream->last_inactivity_ts.is_set = true; |
14f28187 FD |
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; | |
c01594de | 741 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 742 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
14f28187 | 743 | |
98b15851 PP |
744 | BT_ASSERT_DBG(msg); |
745 | BT_ASSERT_DBG(ts_ns); | |
14f28187 | 746 | |
2ece7dd0 | 747 | BT_COMP_LOGD("Getting message's timestamp: iter-data-addr=%p, msg-addr=%p, " |
14f28187 FD |
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: | |
0f1979c3 | 753 | clock_class = bt_message_event_borrow_stream_class_default_clock_class_const( |
14f28187 | 754 | msg); |
98b15851 | 755 | BT_ASSERT_DBG(clock_class); |
14f28187 | 756 | |
0cbc2c33 PP |
757 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
758 | msg); | |
14f28187 FD |
759 | break; |
760 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
0f1979c3 | 761 | clock_class = bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( |
14f28187 FD |
762 | msg); |
763 | BT_ASSERT(clock_class); | |
764 | ||
0cbc2c33 PP |
765 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
766 | msg); | |
14f28187 FD |
767 | break; |
768 | case BT_MESSAGE_TYPE_PACKET_END: | |
0f1979c3 | 769 | clock_class = bt_message_packet_end_borrow_stream_class_default_clock_class_const( |
14f28187 FD |
770 | msg); |
771 | BT_ASSERT(clock_class); | |
772 | ||
0cbc2c33 PP |
773 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const( |
774 | msg); | |
14f28187 FD |
775 | break; |
776 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
0f1979c3 | 777 | clock_class = bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
14f28187 FD |
778 | msg); |
779 | BT_ASSERT(clock_class); | |
780 | ||
9b24b6aa | 781 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 782 | msg); |
14f28187 FD |
783 | break; |
784 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
0f1979c3 | 785 | clock_class = bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( |
14f28187 FD |
786 | msg); |
787 | BT_ASSERT(clock_class); | |
788 | ||
9b24b6aa | 789 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
0cbc2c33 | 790 | msg); |
14f28187 | 791 | break; |
14f28187 | 792 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
60d02328 | 793 | clock_snapshot = bt_message_message_iterator_inactivity_borrow_clock_snapshot_const( |
0f1979c3 | 794 | msg); |
14f28187 FD |
795 | break; |
796 | default: | |
797 | /* All the other messages have a higher priority */ | |
2ece7dd0 | 798 | BT_COMP_LOGD_STR("Message has no timestamp: using the last message timestamp."); |
14f28187 | 799 | *ts_ns = last_msg_ts_ns; |
7cdc2bab MD |
800 | goto end; |
801 | } | |
802 | ||
14f28187 | 803 | clock_class = bt_clock_snapshot_borrow_clock_class_const(clock_snapshot); |
98b15851 | 804 | BT_ASSERT_DBG(clock_class); |
14f28187 FD |
805 | |
806 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); | |
807 | if (ret) { | |
1419db2b FD |
808 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
809 | "Cannot get nanoseconds from Epoch of clock snapshot: " | |
14f28187 FD |
810 | "clock-snapshot-addr=%p", clock_snapshot); |
811 | goto error; | |
812 | } | |
813 | ||
814 | goto end; | |
815 | ||
14f28187 FD |
816 | error: |
817 | ret = -1; | |
7cdc2bab | 818 | |
7cdc2bab | 819 | end: |
14f28187 | 820 | if (ret == 0) { |
2ece7dd0 | 821 | BT_COMP_LOGD("Found message's timestamp: " |
14f28187 FD |
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 | ||
7cdc2bab MD |
827 | return ret; |
828 | } | |
829 | ||
830 | static | |
14f28187 FD |
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, | |
7cdc2bab | 833 | struct lttng_live_stream_iterator *lttng_live_stream, |
cad707e2 | 834 | const bt_message **message) |
7cdc2bab | 835 | { |
14f28187 | 836 | enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
c01594de | 837 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 838 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
18a1979b | 839 | enum ctf_msg_iter_status status; |
0f1979c3 | 840 | uint64_t session_idx, trace_idx; |
7cdc2bab | 841 | |
14f28187 FD |
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); | |
7cdc2bab MD |
846 | |
847 | if (session->new_streams_needed) { | |
f93afbf9 FD |
848 | BT_COMP_LOGD("Need an update for streams: " |
849 | "session-id=%" PRIu64, session->id); | |
14f28187 FD |
850 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
851 | goto end; | |
7cdc2bab | 852 | } |
14f28187 FD |
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); | |
76bbaebc | 857 | if (trace->metadata_stream_state == LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { |
f93afbf9 FD |
858 | BT_COMP_LOGD("Need an update for metadata stream: " |
859 | "session-id=%" PRIu64 ", trace-id=%" PRIu64, | |
860 | session->id, trace->id); | |
14f28187 FD |
861 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
862 | goto end; | |
7cdc2bab MD |
863 | } |
864 | } | |
865 | } | |
866 | ||
867 | if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) { | |
14f28187 | 868 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
ecb4ba8a FD |
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)); | |
14f28187 | 873 | goto end; |
7cdc2bab | 874 | } |
14f28187 | 875 | |
851de941 SM |
876 | status = ctf_msg_iter_get_next_message( |
877 | lttng_live_stream->msg_iter, message); | |
7cdc2bab | 878 | switch (status) { |
18a1979b | 879 | case CTF_MSG_ITER_STATUS_EOF: |
14f28187 | 880 | ret = LTTNG_LIVE_ITERATOR_STATUS_END; |
7cdc2bab | 881 | break; |
18a1979b | 882 | case CTF_MSG_ITER_STATUS_OK: |
14f28187 | 883 | ret = LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab | 884 | break; |
18a1979b | 885 | case CTF_MSG_ITER_STATUS_AGAIN: |
7cdc2bab MD |
886 | /* |
887 | * Continue immediately (end of packet). The next | |
888 | * get_index may return AGAIN to delay the following | |
889 | * attempt. | |
890 | */ | |
14f28187 | 891 | ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; |
7cdc2bab | 892 | break; |
18a1979b | 893 | case CTF_MSG_ITER_STATUS_ERROR: |
7cdc2bab | 894 | default: |
14f28187 | 895 | ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1419db2b | 896 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
ecb4ba8a FD |
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)); | |
7cdc2bab MD |
901 | break; |
902 | } | |
14f28187 FD |
903 | |
904 | end: | |
7cdc2bab MD |
905 | return ret; |
906 | } | |
907 | ||
4a39caef FD |
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, | |
cad707e2 | 912 | const bt_message **curr_msg) |
4a39caef FD |
913 | { |
914 | enum lttng_live_iterator_status live_status = | |
915 | LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1419db2b FD |
916 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
917 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
f93afbf9 FD |
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 | ||
4a39caef FD |
923 | /* |
924 | * The viewer has hung up on us so we are closing the stream. The | |
18a1979b | 925 | * `ctf_msg_iter` should simply realize that it needs to close the |
4a39caef FD |
926 | * stream properly by emitting the necessary stream end message. |
927 | */ | |
18a1979b | 928 | enum ctf_msg_iter_status status = ctf_msg_iter_get_next_message( |
851de941 | 929 | stream_iter->msg_iter, curr_msg); |
4a39caef | 930 | |
18a1979b | 931 | if (status == CTF_MSG_ITER_STATUS_ERROR) { |
1419db2b FD |
932 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
933 | "Error getting the next message from CTF message iterator"); | |
4a39caef FD |
934 | live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
935 | goto end; | |
1cda4ff4 FD |
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; | |
4a39caef FD |
940 | } |
941 | ||
18a1979b | 942 | BT_ASSERT(status == CTF_MSG_ITER_STATUS_OK); |
4a39caef FD |
943 | |
944 | end: | |
945 | return live_status; | |
946 | } | |
947 | ||
7cdc2bab MD |
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 | |
d6e69534 | 983 | * - make that stream event the current message. |
7cdc2bab MD |
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 | |
a31f4d8b | 997 | enum lttng_live_iterator_status lttng_live_iterator_next_msg_on_stream( |
14f28187 FD |
998 | struct lttng_live_msg_iter *lttng_live_msg_iter, |
999 | struct lttng_live_stream_iterator *stream_iter, | |
cad707e2 | 1000 | const bt_message **curr_msg) |
7cdc2bab | 1001 | { |
c01594de | 1002 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
2ece7dd0 | 1003 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
14f28187 | 1004 | enum lttng_live_iterator_status live_status; |
7cdc2bab | 1005 | |
f93afbf9 FD |
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); | |
ecb4ba8a | 1009 | |
4a39caef FD |
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 | ||
7cdc2bab | 1021 | retry: |
ecb4ba8a | 1022 | LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter); |
f93afbf9 FD |
1023 | |
1024 | /* | |
1025 | * Make sure we have the most recent metadata and possibly some new | |
1026 | * streams. | |
1027 | */ | |
14f28187 | 1028 | live_status = lttng_live_iterator_handle_new_streams_and_metadata( |
0f1979c3 | 1029 | lttng_live_msg_iter); |
14f28187 | 1030 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
7cdc2bab MD |
1031 | goto end; |
1032 | } | |
f93afbf9 | 1033 | |
14f28187 | 1034 | live_status = lttng_live_iterator_next_handle_one_no_data_stream( |
0f1979c3 | 1035 | lttng_live_msg_iter, stream_iter); |
14f28187 | 1036 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
4a39caef FD |
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 | } | |
7cdc2bab MD |
1045 | goto end; |
1046 | } | |
f93afbf9 | 1047 | |
14f28187 | 1048 | live_status = lttng_live_iterator_next_handle_one_quiescent_stream( |
0f1979c3 | 1049 | lttng_live_msg_iter, stream_iter, curr_msg); |
14f28187 | 1050 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
5084732e | 1051 | BT_ASSERT(!*curr_msg); |
7cdc2bab MD |
1052 | goto end; |
1053 | } | |
14f28187 | 1054 | if (*curr_msg) { |
7cdc2bab MD |
1055 | goto end; |
1056 | } | |
14f28187 | 1057 | live_status = lttng_live_iterator_next_handle_one_active_data_stream( |
0f1979c3 | 1058 | lttng_live_msg_iter, stream_iter, curr_msg); |
14f28187 | 1059 | if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
5084732e | 1060 | BT_ASSERT(!*curr_msg); |
7cdc2bab MD |
1061 | } |
1062 | ||
1063 | end: | |
14f28187 | 1064 | if (live_status == LTTNG_LIVE_ITERATOR_STATUS_CONTINUE) { |
f93afbf9 | 1065 | BT_COMP_LOGD("Ask the relay daemon for an updated view of the data and metadata streams"); |
7cdc2bab | 1066 | goto retry; |
7cdc2bab | 1067 | } |
14f28187 | 1068 | |
f93afbf9 FD |
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 | ||
14f28187 | 1074 | return live_status; |
7cdc2bab MD |
1075 | } |
1076 | ||
8ec4d5ff JG |
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 | ||
285951be FD |
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; | |
cefd84f8 FD |
1161 | enum lttng_live_iterator_status adjust_status; |
1162 | bt_message *adjusted_message; | |
1163 | ||
1164 | /* | |
1165 | * The timestamp of the current message is before the last message sent | |
1166 | * by this component. We CANNOT send it as is. | |
1167 | * | |
1168 | * The only expected scenario in which that could happen is the | |
1169 | * following, everything else is a bug in this component, relay deamon, | |
1170 | * or CTF parser. | |
1171 | * | |
1172 | * Expected scenario: The CTF message iterator emitted discarded | |
1173 | * packets and discarded events with synthesized beginning and end | |
1174 | * timestamps from the bounds of the last known packet and the newly | |
1175 | * decoded packet header. The CTF message iterator is not aware of | |
1176 | * stream inactivity beacons. Hence, we have to adjust the beginning | |
1177 | * timestamp of those types of messages if a stream signalled its | |
1178 | * inactivity up until _after_ the last known packet's beginning | |
1179 | * timestamp. | |
1180 | * | |
1181 | * Otherwise, the monotonicity guarantee of message timestamps would | |
1182 | * not be preserved. | |
1183 | * | |
1184 | * In short, the only scenario in which it's okay and fixable to | |
1185 | * received a late message is when: | |
1186 | * 1. the late message is a discarded packets or discarded events | |
1187 | * message, | |
1188 | * 2. this stream produced an inactivity message downstream, and | |
1189 | * 3. the timestamp of the late message is within the inactivity | |
1190 | * timespan we sent downstream through the inactivity message. | |
1191 | */ | |
1192 | ||
1193 | BT_COMP_LOGD("Handling late message on live stream iterator: " | |
1194 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
1195 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
1196 | ||
1197 | if (!stream_iter->last_inactivity_ts.is_set) { | |
1198 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Invalid live stream state: " | |
1199 | "have a late message when no inactivity message " | |
1200 | "was ever sent for that stream."); | |
1201 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1202 | goto end; | |
1203 | } | |
1204 | ||
1205 | if (!is_discarded_packet_or_event_message(late_msg)) { | |
1206 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Invalid live stream state: " | |
1207 | "have a late message that is not a packet discarded or " | |
1208 | "event discarded message: late-msg-type=%s", | |
1209 | bt_common_message_type_string(bt_message_get_type(late_msg))); | |
1210 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1211 | goto end; | |
1212 | } | |
285951be FD |
1213 | |
1214 | stream_class = bt_stream_borrow_class_const(stream_iter->stream); | |
1215 | clock_class = bt_stream_class_borrow_default_clock_class_const(stream_class); | |
1216 | ||
1217 | ts_ns_status = bt_clock_class_cycles_to_ns_from_origin(clock_class, | |
a043c362 | 1218 | stream_iter->last_inactivity_ts.value, &last_inactivity_ts_ns); |
285951be | 1219 | if (ts_ns_status != BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OK) { |
cefd84f8 FD |
1220 | BT_COMP_LOGE_APPEND_CAUSE(self_comp,"Error converting last " |
1221 | "inactivity message timestamp to nanoseconds"); | |
285951be FD |
1222 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; |
1223 | goto end; | |
1224 | } | |
1225 | ||
cefd84f8 FD |
1226 | if (last_inactivity_ts_ns <= late_msg_ts_ns) { |
1227 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Invalid live stream state: " | |
1228 | "have a late message that is none included in a stream " | |
1229 | "inactivity timespan: last-inactivity-ts-ns=%" PRIu64 | |
1230 | "late-msg-ts-ns=%" PRIu64, last_inactivity_ts_ns, late_msg_ts_ns); | |
1231 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
1232 | goto end; | |
1233 | } | |
285951be | 1234 | |
cefd84f8 FD |
1235 | /* |
1236 | * We now know that it's okay for this message to be late, we can now | |
1237 | * adjust its timestamp to ensure monotonicity. | |
1238 | */ | |
1239 | BT_COMP_LOGD("Adjusting the timestamp of late message: late-msg-type=%s, " | |
1240 | "msg-new-ts-ns=%" PRIu64, | |
1241 | bt_common_message_type_string(bt_message_get_type(late_msg)), | |
1242 | stream_iter->last_inactivity_ts.value); | |
1243 | switch (bt_message_get_type(late_msg)) { | |
1244 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1245 | adjust_status = adjust_discarded_events_message( | |
1246 | lttng_live_msg_iter->self_msg_iter, | |
1247 | stream_iter->stream, | |
1248 | late_msg, &adjusted_message, | |
1249 | stream_iter->last_inactivity_ts.value); | |
1250 | break; | |
1251 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1252 | adjust_status = adjust_discarded_packets_message( | |
1253 | lttng_live_msg_iter->self_msg_iter, | |
1254 | stream_iter->stream, | |
1255 | late_msg, &adjusted_message, | |
1256 | stream_iter->last_inactivity_ts.value); | |
1257 | break; | |
1258 | default: | |
1259 | bt_common_abort(); | |
1260 | } | |
285951be | 1261 | |
cefd84f8 FD |
1262 | if (adjust_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { |
1263 | stream_iter_status = adjust_status; | |
285951be FD |
1264 | goto end; |
1265 | } | |
cefd84f8 FD |
1266 | |
1267 | BT_ASSERT_DBG(adjusted_message); | |
1268 | stream_iter->current_msg = adjusted_message; | |
1269 | stream_iter->current_msg_ts_ns = last_inactivity_ts_ns; | |
6d1a2dc7 | 1270 | bt_message_put_ref(late_msg); |
cefd84f8 | 1271 | |
285951be FD |
1272 | end: |
1273 | return stream_iter_status; | |
1274 | } | |
1275 | ||
7cdc2bab | 1276 | static |
14f28187 FD |
1277 | enum lttng_live_iterator_status next_stream_iterator_for_trace( |
1278 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1279 | struct lttng_live_trace *live_trace, | |
a31f4d8b | 1280 | struct lttng_live_stream_iterator **youngest_trace_stream_iter) |
7cdc2bab | 1281 | { |
a31f4d8b | 1282 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; |
0f1979c3 FD |
1283 | bt_logging_level log_level = lttng_live_msg_iter->log_level; |
1284 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; | |
14f28187 | 1285 | enum lttng_live_iterator_status stream_iter_status;; |
a31f4d8b | 1286 | int64_t youngest_candidate_msg_ts = INT64_MAX; |
14f28187 | 1287 | uint64_t stream_iter_idx; |
7cdc2bab | 1288 | |
98b15851 PP |
1289 | BT_ASSERT_DBG(live_trace); |
1290 | BT_ASSERT_DBG(live_trace->stream_iterators); | |
ecb4ba8a FD |
1291 | |
1292 | BT_COMP_LOGD("Finding the next stream iterator for trace: " | |
1293 | "trace-id=%"PRIu64, live_trace->id); | |
14f28187 FD |
1294 | /* |
1295 | * Update the current message of every stream iterators of this trace. | |
1296 | * The current msg of every stream must have a timestamp equal or | |
1297 | * larger than the last message returned by this iterator. We must | |
1298 | * ensure monotonicity. | |
1299 | */ | |
1300 | stream_iter_idx = 0; | |
1301 | while (stream_iter_idx < live_trace->stream_iterators->len) { | |
1302 | bool stream_iter_is_ended = false; | |
1303 | struct lttng_live_stream_iterator *stream_iter = | |
1304 | g_ptr_array_index(live_trace->stream_iterators, | |
0f1979c3 | 1305 | stream_iter_idx); |
14f28187 FD |
1306 | |
1307 | /* | |
f080b7e2 FD |
1308 | * If there is no current message for this stream, go fetch |
1309 | * one. | |
14f28187 FD |
1310 | */ |
1311 | while (!stream_iter->current_msg) { | |
cad707e2 | 1312 | const bt_message *msg = NULL; |
14f28187 | 1313 | int64_t curr_msg_ts_ns = INT64_MAX; |
f93afbf9 | 1314 | |
a31f4d8b | 1315 | stream_iter_status = lttng_live_iterator_next_msg_on_stream( |
0f1979c3 | 1316 | lttng_live_msg_iter, stream_iter, &msg); |
14f28187 | 1317 | |
14f28187 FD |
1318 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { |
1319 | stream_iter_is_ended = true; | |
1320 | break; | |
1321 | } | |
1322 | ||
1323 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1324 | goto end; | |
1325 | } | |
1326 | ||
98b15851 | 1327 | BT_ASSERT_DBG(msg); |
14f28187 | 1328 | |
f93afbf9 FD |
1329 | BT_COMP_LOGD("Live stream iterator returned message: msg-type=%s" |
1330 | "stream-name=\"%s\", viewer-stream-id=%" PRIu64, | |
1331 | bt_common_message_type_string(bt_message_get_type(msg)), | |
1332 | stream_iter->name->str, stream_iter->viewer_stream_id); | |
1333 | ||
14f28187 FD |
1334 | /* |
1335 | * Get the timestamp in nanoseconds from origin of this | |
1336 | * messsage. | |
1337 | */ | |
1338 | live_get_msg_ts_ns(stream_iter, lttng_live_msg_iter, | |
1339 | msg, lttng_live_msg_iter->last_msg_ts_ns, | |
1340 | &curr_msg_ts_ns); | |
1341 | ||
1342 | /* | |
1343 | * Check if the message of the current live stream | |
118ae153 | 1344 | * iterator occurred at the exact same time or after the |
14f28187 | 1345 | * last message returned by this component's message |
cefd84f8 | 1346 | * iterator. If not, we need to handle it with care. |
14f28187 FD |
1347 | */ |
1348 | if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) { | |
1349 | stream_iter->current_msg = msg; | |
1350 | stream_iter->current_msg_ts_ns = curr_msg_ts_ns; | |
6488b176 | 1351 | } else { |
285951be FD |
1352 | /* |
1353 | * We received a message from the past. This | |
1354 | * may be fixable but it can also be an error. | |
1355 | */ | |
1356 | stream_iter_status = handle_late_message( | |
1357 | lttng_live_msg_iter, stream_iter, | |
1358 | curr_msg_ts_ns, msg); | |
1359 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
6488b176 | 1360 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
285951be FD |
1361 | "Late message could not be handled correctly: " |
1362 | "lttng-live-msg-iter-addr=%p, " | |
1363 | "stream-name=\"%s\", " | |
1364 | "curr-msg-ts=%" PRId64 | |
1365 | ", last-msg-ts=%" PRId64, | |
1366 | lttng_live_msg_iter, | |
1367 | stream_iter->name->str, | |
1368 | curr_msg_ts_ns, | |
6488b176 JG |
1369 | lttng_live_msg_iter->last_msg_ts_ns); |
1370 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
8ec4d5ff JG |
1371 | goto end; |
1372 | } | |
14f28187 FD |
1373 | } |
1374 | } | |
1375 | ||
98b15851 | 1376 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); |
07afb6bc FD |
1377 | |
1378 | if (!stream_iter_is_ended) { | |
a31f4d8b FD |
1379 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || |
1380 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
07afb6bc FD |
1381 | /* |
1382 | * Update the current best candidate message | |
1383 | * for the stream iterator of this live trace | |
1384 | * to be forwarded downstream. | |
1385 | */ | |
a31f4d8b FD |
1386 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; |
1387 | youngest_candidate_stream_iter = stream_iter; | |
1388 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
07afb6bc FD |
1389 | /* |
1390 | * Order the messages in an arbitrary but | |
1391 | * deterministic way. | |
1392 | */ | |
98b15851 | 1393 | BT_ASSERT_DBG(stream_iter != youngest_candidate_stream_iter); |
07afb6bc FD |
1394 | int ret = common_muxing_compare_messages( |
1395 | stream_iter->current_msg, | |
a31f4d8b | 1396 | youngest_candidate_stream_iter->current_msg); |
07afb6bc FD |
1397 | if (ret < 0) { |
1398 | /* | |
a31f4d8b | 1399 | * The `youngest_candidate_stream_iter->current_msg` |
07afb6bc FD |
1400 | * should go first. Update the next |
1401 | * iterator and the current timestamp. | |
1402 | */ | |
a31f4d8b FD |
1403 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; |
1404 | youngest_candidate_stream_iter = stream_iter; | |
07afb6bc FD |
1405 | } else if (ret == 0) { |
1406 | /* | |
1407 | * Unable to pick which one should go | |
1408 | * first. | |
1409 | */ | |
1410 | BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1411 | "stream-iter-addr=%p" | |
1412 | "stream-iter-addr=%p", | |
1413 | stream_iter, | |
a31f4d8b | 1414 | youngest_candidate_stream_iter); |
07afb6bc FD |
1415 | } |
1416 | } | |
14f28187 | 1417 | |
07afb6bc FD |
1418 | stream_iter_idx++; |
1419 | } else { | |
14f28187 | 1420 | /* |
fba9b58e JG |
1421 | * The live stream iterator has ended. That |
1422 | * iterator is removed from the array, but | |
1423 | * there is no need to increment | |
1424 | * stream_iter_idx as | |
1425 | * g_ptr_array_remove_index_fast replaces the | |
1426 | * removed element with the array's last | |
1427 | * element. | |
14f28187 | 1428 | */ |
fba9b58e JG |
1429 | g_ptr_array_remove_index_fast( |
1430 | live_trace->stream_iterators, | |
14f28187 | 1431 | stream_iter_idx); |
14f28187 FD |
1432 | } |
1433 | } | |
1434 | ||
a31f4d8b FD |
1435 | if (youngest_candidate_stream_iter) { |
1436 | *youngest_trace_stream_iter = youngest_candidate_stream_iter; | |
14f28187 FD |
1437 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
1438 | } else { | |
1439 | /* | |
1440 | * The only case where we don't have a candidate for this trace | |
1441 | * is if we reached the end of all the iterators. | |
1442 | */ | |
1443 | BT_ASSERT(live_trace->stream_iterators->len == 0); | |
1444 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
1445 | } | |
1446 | ||
1447 | end: | |
1448 | return stream_iter_status; | |
1449 | } | |
1450 | ||
1451 | static | |
1452 | enum lttng_live_iterator_status next_stream_iterator_for_session( | |
1453 | struct lttng_live_msg_iter *lttng_live_msg_iter, | |
1454 | struct lttng_live_session *session, | |
a31f4d8b | 1455 | struct lttng_live_stream_iterator **youngest_session_stream_iter) |
14f28187 | 1456 | { |
07afb6bc FD |
1457 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
1458 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
14f28187 FD |
1459 | enum lttng_live_iterator_status stream_iter_status; |
1460 | uint64_t trace_idx = 0; | |
a31f4d8b FD |
1461 | int64_t youngest_candidate_msg_ts = INT64_MAX; |
1462 | struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; | |
14f28187 | 1463 | |
ecb4ba8a FD |
1464 | BT_COMP_LOGD("Finding the next stream iterator for session: " |
1465 | "session-id=%"PRIu64, session->id); | |
14f28187 FD |
1466 | /* |
1467 | * Make sure we are attached to the session and look for new streams | |
1468 | * and metadata. | |
1469 | */ | |
1470 | stream_iter_status = lttng_live_get_session(lttng_live_msg_iter, session); | |
1471 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK && | |
1472 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_CONTINUE && | |
1473 | stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_END) { | |
7cdc2bab MD |
1474 | goto end; |
1475 | } | |
14f28187 | 1476 | |
98b15851 | 1477 | BT_ASSERT_DBG(session->traces); |
14f28187 | 1478 | |
14f28187 FD |
1479 | while (trace_idx < session->traces->len) { |
1480 | bool trace_is_ended = false; | |
1481 | struct lttng_live_stream_iterator *stream_iter; | |
1482 | struct lttng_live_trace *trace = | |
1483 | g_ptr_array_index(session->traces, trace_idx); | |
1484 | ||
1485 | stream_iter_status = next_stream_iterator_for_trace( | |
1486 | lttng_live_msg_iter, trace, &stream_iter); | |
1487 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1488 | /* | |
1489 | * All the live stream iterators for this trace are | |
1490 | * ENDed. Remove the trace from this session. | |
1491 | */ | |
1492 | trace_is_ended = true; | |
1493 | } else if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
1494 | goto end; | |
1495 | } | |
1496 | ||
1497 | if (!trace_is_ended) { | |
98b15851 | 1498 | BT_ASSERT_DBG(stream_iter); |
14f28187 | 1499 | |
a31f4d8b FD |
1500 | if (G_UNLIKELY(youngest_candidate_stream_iter == NULL) || |
1501 | stream_iter->current_msg_ts_ns < youngest_candidate_msg_ts) { | |
1502 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; | |
1503 | youngest_candidate_stream_iter = stream_iter; | |
1504 | } else if (stream_iter->current_msg_ts_ns == youngest_candidate_msg_ts) { | |
07afb6bc FD |
1505 | /* |
1506 | * Order the messages in an arbitrary but | |
1507 | * deterministic way. | |
1508 | */ | |
1509 | int ret = common_muxing_compare_messages( | |
1510 | stream_iter->current_msg, | |
a31f4d8b | 1511 | youngest_candidate_stream_iter->current_msg); |
07afb6bc FD |
1512 | if (ret < 0) { |
1513 | /* | |
a31f4d8b | 1514 | * The `youngest_candidate_stream_iter->current_msg` |
07afb6bc FD |
1515 | * should go first. Update the next iterator |
1516 | * and the current timestamp. | |
1517 | */ | |
a31f4d8b FD |
1518 | youngest_candidate_msg_ts = stream_iter->current_msg_ts_ns; |
1519 | youngest_candidate_stream_iter = stream_iter; | |
07afb6bc FD |
1520 | } else if (ret == 0) { |
1521 | /* Unable to pick which one should go first. */ | |
1522 | BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1523 | "stream-iter-addr=%p" "stream-iter-addr=%p", | |
a31f4d8b | 1524 | stream_iter, youngest_candidate_stream_iter); |
07afb6bc | 1525 | } |
14f28187 FD |
1526 | } |
1527 | trace_idx++; | |
1528 | } else { | |
ba90bce7 JG |
1529 | /* |
1530 | * trace_idx is not incremented since | |
1531 | * g_ptr_array_remove_index_fast replaces the | |
1532 | * element at trace_idx with the array's last element. | |
1533 | */ | |
1534 | g_ptr_array_remove_index_fast(session->traces, | |
1535 | trace_idx); | |
14f28187 FD |
1536 | } |
1537 | } | |
a31f4d8b FD |
1538 | if (youngest_candidate_stream_iter) { |
1539 | *youngest_session_stream_iter = youngest_candidate_stream_iter; | |
14f28187 | 1540 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; |
7cdc2bab | 1541 | } else { |
14f28187 FD |
1542 | /* |
1543 | * The only cases where we don't have a candidate for this | |
1544 | * trace is: | |
1545 | * 1. if we reached the end of all the iterators of all the | |
1546 | * traces of this session, | |
1547 | * 2. if we never had live stream iterator in the first place. | |
1548 | * | |
1549 | * In either cases, we return END. | |
1550 | */ | |
1551 | BT_ASSERT(session->traces->len == 0); | |
1552 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_END; | |
7cdc2bab MD |
1553 | } |
1554 | end: | |
14f28187 FD |
1555 | return stream_iter_status; |
1556 | } | |
1557 | ||
1558 | static inline | |
1559 | void put_messages(bt_message_array_const msgs, uint64_t count) | |
1560 | { | |
1561 | uint64_t i; | |
1562 | ||
1563 | for (i = 0; i < count; i++) { | |
1564 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
7cdc2bab | 1565 | } |
7cdc2bab MD |
1566 | } |
1567 | ||
d3eb6e8f | 1568 | BT_HIDDEN |
a3f0c7db | 1569 | bt_message_iterator_class_next_method_status lttng_live_msg_iter_next( |
14f28187 FD |
1570 | bt_self_message_iterator *self_msg_it, |
1571 | bt_message_array_const msgs, uint64_t capacity, | |
1572 | uint64_t *count) | |
d3eb6e8f | 1573 | { |
a3f0c7db | 1574 | bt_message_iterator_class_next_method_status status; |
f79c2d7a | 1575 | enum lttng_live_viewer_status viewer_status; |
14f28187 FD |
1576 | struct lttng_live_msg_iter *lttng_live_msg_iter = |
1577 | bt_self_message_iterator_get_data(self_msg_it); | |
1578 | struct lttng_live_component *lttng_live = | |
1579 | lttng_live_msg_iter->lttng_live_comp; | |
376fc2bd FD |
1580 | bt_self_component *self_comp = lttng_live_msg_iter->self_comp; |
1581 | bt_logging_level log_level = lttng_live_msg_iter->log_level; | |
14f28187 FD |
1582 | enum lttng_live_iterator_status stream_iter_status; |
1583 | uint64_t session_idx; | |
1584 | ||
1585 | *count = 0; | |
1586 | ||
98b15851 | 1587 | BT_ASSERT_DBG(lttng_live_msg_iter); |
14f28187 | 1588 | |
f79c2d7a FD |
1589 | if (G_UNLIKELY(lttng_live_msg_iter->was_interrupted)) { |
1590 | /* | |
1591 | * The iterator was interrupted in a previous call to the | |
1592 | * `_next()` method. We currently do not support generating | |
1593 | * messages after such event. The babeltrace2 CLI should never | |
1594 | * be running the graph after being interrupted. So this check | |
1595 | * is to prevent other graph users from using this live | |
1596 | * iterator in an messed up internal state. | |
1597 | */ | |
a3f0c7db | 1598 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
f79c2d7a FD |
1599 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
1600 | "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event."); | |
1601 | goto end; | |
1602 | } | |
1603 | ||
14f28187 FD |
1604 | /* |
1605 | * Clear all the invalid message reference that might be left over in | |
1606 | * the output array. | |
1607 | */ | |
1608 | memset(msgs, 0, capacity * sizeof(*msgs)); | |
1609 | ||
1610 | /* | |
1611 | * If no session are exposed on the relay found at the url provided by | |
1612 | * the user, session count will be 0. In this case, we return status | |
1613 | * end to return gracefully. | |
1614 | */ | |
1615 | if (lttng_live_msg_iter->sessions->len == 0) { | |
1616 | if (lttng_live->params.sess_not_found_act != | |
1617 | SESSION_NOT_FOUND_ACTION_CONTINUE) { | |
a3f0c7db | 1618 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
f79c2d7a | 1619 | goto end; |
14f28187 FD |
1620 | } else { |
1621 | /* | |
1622 | * The are no more active session for this session | |
1623 | * name. Retry to create a viewer session for the | |
1624 | * requested session name. | |
1625 | */ | |
f79c2d7a FD |
1626 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1627 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1628 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
a3f0c7db | 1629 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
f79c2d7a FD |
1630 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
1631 | "Error creating LTTng live viewer session"); | |
1632 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
a3f0c7db | 1633 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; |
f79c2d7a FD |
1634 | } else { |
1635 | bt_common_abort(); | |
1636 | } | |
1637 | goto end; | |
14f28187 FD |
1638 | } |
1639 | } | |
1640 | } | |
1641 | ||
1642 | if (lttng_live_msg_iter->active_stream_iter == 0) { | |
1643 | lttng_live_force_new_streams_and_metadata(lttng_live_msg_iter); | |
1644 | } | |
1645 | ||
1646 | /* | |
1647 | * Here the muxing of message is done. | |
1648 | * | |
1649 | * We need to iterate over all the streams of all the traces of all the | |
1650 | * viewer sessions in order to get the message with the smallest | |
1651 | * timestamp. In this case, a session is a viewer session and there is | |
1652 | * one viewer session per consumer daemon. (UST 32bit, UST 64bit and/or | |
1653 | * kernel). Each viewer session can have multiple traces, for example, | |
1654 | * 64bit UST viewer sessions could have multiple per-pid traces. | |
1655 | * | |
1656 | * We iterate over the streams of each traces to update and see what is | |
1657 | * their next message's timestamp. From those timestamps, we select the | |
1658 | * message with the smallest timestamp as the best candidate message | |
1659 | * for that trace and do the same thing across all the sessions. | |
1660 | * | |
1661 | * We then compare the timestamp of best candidate message of all the | |
1662 | * sessions to pick the message with the smallest timestamp and we | |
1663 | * return it. | |
1664 | */ | |
1665 | while (*count < capacity) { | |
a31f4d8b FD |
1666 | struct lttng_live_stream_iterator *youngest_stream_iter = NULL, |
1667 | *candidate_stream_iter = NULL; | |
1668 | int64_t youngest_msg_ts_ns = INT64_MAX; | |
14f28187 | 1669 | |
98b15851 | 1670 | BT_ASSERT_DBG(lttng_live_msg_iter->sessions); |
14f28187 | 1671 | session_idx = 0; |
14f28187 FD |
1672 | while (session_idx < lttng_live_msg_iter->sessions->len) { |
1673 | struct lttng_live_session *session = | |
1674 | g_ptr_array_index(lttng_live_msg_iter->sessions, | |
1675 | session_idx); | |
1676 | ||
1677 | /* Find the best candidate message to send downstream. */ | |
1678 | stream_iter_status = next_stream_iterator_for_session( | |
1679 | lttng_live_msg_iter, session, | |
1680 | &candidate_stream_iter); | |
1681 | ||
1682 | /* If we receive an END status, it means that either: | |
1683 | * - Those traces never had active streams (UST with no | |
1684 | * data produced yet), | |
1685 | * - All live stream iterators have ENDed.*/ | |
1686 | if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { | |
1687 | if (session->closed && session->traces->len == 0) { | |
1688 | /* | |
7d632b96 JG |
1689 | * Remove the session from the list. |
1690 | * session_idx is not modified since | |
1691 | * g_ptr_array_remove_index_fast | |
1692 | * replaces the the removed element with | |
1693 | * the array's last element. | |
14f28187 FD |
1694 | */ |
1695 | g_ptr_array_remove_index_fast( | |
1696 | lttng_live_msg_iter->sessions, | |
1697 | session_idx); | |
14f28187 FD |
1698 | } else { |
1699 | session_idx++; | |
1700 | } | |
1701 | continue; | |
1702 | } | |
1703 | ||
1704 | if (stream_iter_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { | |
f79c2d7a | 1705 | goto return_status; |
14f28187 FD |
1706 | } |
1707 | ||
a31f4d8b | 1708 | if (G_UNLIKELY(youngest_stream_iter == NULL) || |
1d314141 | 1709 | candidate_stream_iter->current_msg_ts_ns < youngest_msg_ts_ns) { |
a31f4d8b FD |
1710 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; |
1711 | youngest_stream_iter = candidate_stream_iter; | |
1712 | } else if (candidate_stream_iter->current_msg_ts_ns == youngest_msg_ts_ns) { | |
376fc2bd FD |
1713 | /* |
1714 | * The currently selected message to be sent | |
1715 | * downstream next has the exact same timestamp | |
1716 | * that of the current candidate message. We | |
1717 | * must break the tie in a predictable manner. | |
1718 | */ | |
1719 | BT_COMP_LOGD_STR("Two of the next message candidates have the same timestamps, pick one deterministically."); | |
1720 | /* | |
1721 | * Order the messages in an arbitrary but | |
07afb6bc | 1722 | * deterministic way. |
376fc2bd | 1723 | */ |
07afb6bc FD |
1724 | int ret = common_muxing_compare_messages( |
1725 | candidate_stream_iter->current_msg, | |
a31f4d8b | 1726 | youngest_stream_iter->current_msg); |
376fc2bd FD |
1727 | if (ret < 0) { |
1728 | /* | |
1729 | * The `candidate_stream_iter->current_msg` | |
1730 | * should go first. Update the next | |
1731 | * iterator and the current timestamp. | |
1732 | */ | |
a31f4d8b FD |
1733 | youngest_msg_ts_ns = candidate_stream_iter->current_msg_ts_ns; |
1734 | youngest_stream_iter = candidate_stream_iter; | |
376fc2bd FD |
1735 | } else if (ret == 0) { |
1736 | /* Unable to pick which one should go first. */ | |
1737 | BT_COMP_LOGW("Cannot deterministically pick next live stream message iterator because they have identical next messages: " | |
1738 | "next-stream-iter-addr=%p" "candidate-stream-iter-addr=%p", | |
a31f4d8b | 1739 | youngest_stream_iter, candidate_stream_iter); |
376fc2bd | 1740 | } |
14f28187 FD |
1741 | } |
1742 | ||
1743 | session_idx++; | |
1744 | } | |
1745 | ||
a31f4d8b | 1746 | if (!youngest_stream_iter) { |
14f28187 | 1747 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; |
f79c2d7a | 1748 | goto return_status; |
14f28187 FD |
1749 | } |
1750 | ||
98b15851 | 1751 | BT_ASSERT_DBG(youngest_stream_iter->current_msg); |
14f28187 | 1752 | /* Ensure monotonicity. */ |
98b15851 | 1753 | BT_ASSERT_DBG(lttng_live_msg_iter->last_msg_ts_ns <= |
a31f4d8b | 1754 | youngest_stream_iter->current_msg_ts_ns); |
14f28187 FD |
1755 | |
1756 | /* | |
1757 | * Insert the next message to the message batch. This will set | |
1758 | * stream iterator current messsage to NULL so that next time | |
1759 | * we fetch the next message of that stream iterator | |
1760 | */ | |
a31f4d8b | 1761 | BT_MESSAGE_MOVE_REF(msgs[*count], youngest_stream_iter->current_msg); |
14f28187 FD |
1762 | (*count)++; |
1763 | ||
1764 | /* Update the last timestamp in nanoseconds sent downstream. */ | |
a31f4d8b FD |
1765 | lttng_live_msg_iter->last_msg_ts_ns = youngest_msg_ts_ns; |
1766 | youngest_stream_iter->current_msg_ts_ns = INT64_MAX; | |
14f28187 FD |
1767 | |
1768 | stream_iter_status = LTTNG_LIVE_ITERATOR_STATUS_OK; | |
1769 | } | |
f79c2d7a FD |
1770 | |
1771 | return_status: | |
14f28187 FD |
1772 | switch (stream_iter_status) { |
1773 | case LTTNG_LIVE_ITERATOR_STATUS_OK: | |
1774 | case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: | |
f79c2d7a FD |
1775 | /* |
1776 | * If we gathered messages, return _OK even if the graph was | |
1777 | * interrupted. This allows for the components downstream to at | |
1778 | * least get the thoses messages. If the graph was indeed | |
1779 | * interrupted there should not be another _next() call as the | |
1780 | * application will tear down the graph. This component class | |
1781 | * doesn't support restarting after an interruption. | |
1782 | */ | |
14f28187 | 1783 | if (*count > 0) { |
a3f0c7db | 1784 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; |
14f28187 | 1785 | } else { |
a3f0c7db | 1786 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; |
14f28187 | 1787 | } |
7cdc2bab | 1788 | break; |
14f28187 | 1789 | case LTTNG_LIVE_ITERATOR_STATUS_END: |
a3f0c7db | 1790 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
7cdc2bab | 1791 | break; |
14f28187 | 1792 | case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: |
b607a4f7 FD |
1793 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
1794 | "Memory error preparing the next batch of messages: " | |
1795 | "live-iter-status=%s", | |
1796 | lttng_live_iterator_status_string(stream_iter_status)); | |
a3f0c7db | 1797 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; |
14f28187 FD |
1798 | break; |
1799 | case LTTNG_LIVE_ITERATOR_STATUS_ERROR: | |
1800 | case LTTNG_LIVE_ITERATOR_STATUS_INVAL: | |
1801 | case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: | |
b607a4f7 FD |
1802 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
1803 | "Error preparing the next batch of messages: " | |
1804 | "live-iter-status=%s", | |
1805 | lttng_live_iterator_status_string(stream_iter_status)); | |
1806 | ||
a3f0c7db | 1807 | status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
14f28187 FD |
1808 | /* Put all existing messages on error. */ |
1809 | put_messages(msgs, *count); | |
7cdc2bab | 1810 | break; |
14f28187 | 1811 | default: |
498e7994 | 1812 | bt_common_abort(); |
7cdc2bab | 1813 | } |
14f28187 | 1814 | |
f79c2d7a | 1815 | end: |
14f28187 | 1816 | return status; |
7cdc2bab | 1817 | } |
41a2b7ae | 1818 | |
4f74db52 FD |
1819 | static |
1820 | struct lttng_live_msg_iter *lttng_live_msg_iter_create( | |
1821 | struct lttng_live_component *lttng_live_comp, | |
1822 | bt_self_message_iterator *self_msg_it) | |
1823 | { | |
1824 | bt_self_component *self_comp = lttng_live_comp->self_comp; | |
1825 | bt_logging_level log_level = lttng_live_comp->log_level; | |
1826 | ||
1827 | struct lttng_live_msg_iter *lttng_live_msg_iter = | |
1828 | g_new0(struct lttng_live_msg_iter, 1); | |
1829 | if (!lttng_live_msg_iter) { | |
1830 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1831 | "Failed to allocate lttng_live_msg_iter"); | |
1832 | goto end; | |
1833 | } | |
1834 | ||
1835 | lttng_live_msg_iter->log_level = lttng_live_comp->log_level; | |
1836 | lttng_live_msg_iter->self_comp = lttng_live_comp->self_comp; | |
1837 | lttng_live_msg_iter->lttng_live_comp = lttng_live_comp; | |
1838 | lttng_live_msg_iter->self_msg_iter = self_msg_it; | |
1839 | ||
1840 | lttng_live_msg_iter->active_stream_iter = 0; | |
1841 | lttng_live_msg_iter->last_msg_ts_ns = INT64_MIN; | |
1842 | lttng_live_msg_iter->was_interrupted = false; | |
1843 | ||
1844 | lttng_live_msg_iter->sessions = g_ptr_array_new_with_free_func( | |
1845 | (GDestroyNotify) lttng_live_destroy_session); | |
1846 | BT_ASSERT(lttng_live_msg_iter->sessions); | |
1847 | ||
1848 | end: | |
1849 | return lttng_live_msg_iter; | |
1850 | ||
1851 | } | |
1852 | ||
7cdc2bab | 1853 | BT_HIDDEN |
a3f0c7db | 1854 | bt_message_iterator_class_initialize_method_status lttng_live_msg_iter_init( |
14f28187 | 1855 | bt_self_message_iterator *self_msg_it, |
8d8b141d | 1856 | bt_self_message_iterator_configuration *config, |
14f28187 | 1857 | bt_self_component_port_output *self_port) |
7cdc2bab | 1858 | { |
a3f0c7db | 1859 | bt_message_iterator_class_initialize_method_status status; |
14f28187 FD |
1860 | struct lttng_live_component *lttng_live; |
1861 | struct lttng_live_msg_iter *lttng_live_msg_iter; | |
f79c2d7a | 1862 | enum lttng_live_viewer_status viewer_status; |
c01594de | 1863 | bt_logging_level log_level; |
f615b250 PP |
1864 | bt_self_component *self_comp = |
1865 | bt_self_message_iterator_borrow_component(self_msg_it); | |
14f28187 FD |
1866 | |
1867 | lttng_live = bt_self_component_get_data(self_comp); | |
c01594de | 1868 | log_level = lttng_live->log_level; |
2ece7dd0 | 1869 | self_comp = lttng_live->self_comp; |
14f28187 | 1870 | |
4f74db52 | 1871 | |
14f28187 FD |
1872 | /* There can be only one downstream iterator at the same time. */ |
1873 | BT_ASSERT(!lttng_live->has_msg_iter); | |
1874 | lttng_live->has_msg_iter = true; | |
1875 | ||
4f74db52 FD |
1876 | lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, |
1877 | self_msg_it); | |
14f28187 | 1878 | if (!lttng_live_msg_iter) { |
a3f0c7db | 1879 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
f79c2d7a | 1880 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
4f74db52 FD |
1881 | "Failed to create lttng_live_msg_iter"); |
1882 | goto error; | |
7cdc2bab | 1883 | } |
14f28187 | 1884 | |
f79c2d7a FD |
1885 | viewer_status = live_viewer_connection_create(self_comp, NULL, |
1886 | log_level, lttng_live->params.url->str, false, | |
1887 | lttng_live_msg_iter, <tng_live_msg_iter->viewer_connection); | |
1888 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1889 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1890 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1891 | "Failed to create viewer connection"); | |
1892 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1893 | /* | |
1894 | * Interruption in the _iter_init() method is not | |
1895 | * supported. Return an error. | |
1896 | */ | |
1897 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1898 | "Interrupted while creating viewer connection"); | |
1899 | } | |
14f28187 FD |
1900 | goto error; |
1901 | } | |
1902 | ||
f79c2d7a FD |
1903 | viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); |
1904 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1905 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1906 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1907 | "Failed to create viewer session"); | |
1908 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
1909 | /* | |
1910 | * Interruption in the _iter_init() method is not | |
1911 | * supported. Return an error. | |
1912 | */ | |
1913 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, | |
1914 | "Interrupted when creating viewer session"); | |
1915 | } | |
14f28187 FD |
1916 | goto error; |
1917 | } | |
f79c2d7a | 1918 | |
14f28187 FD |
1919 | if (lttng_live_msg_iter->sessions->len == 0) { |
1920 | switch (lttng_live->params.sess_not_found_act) { | |
1921 | case SESSION_NOT_FOUND_ACTION_CONTINUE: | |
f79c2d7a | 1922 | BT_COMP_LOGI("Unable to connect to the requested live viewer session. Keep trying to connect because of " |
14f28187 FD |
1923 | "%s=\"%s\" component parameter: url=\"%s\"", |
1924 | SESS_NOT_FOUND_ACTION_PARAM, | |
1925 | SESS_NOT_FOUND_ACTION_CONTINUE_STR, | |
1926 | lttng_live->params.url->str); | |
1927 | break; | |
1928 | case SESSION_NOT_FOUND_ACTION_FAIL: | |
f79c2d7a FD |
1929 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, |
1930 | "Unable to connect to the requested live viewer session. Fail the message iterator initialization because of %s=\"%s\" " | |
14f28187 FD |
1931 | "component parameter: url =\"%s\"", |
1932 | SESS_NOT_FOUND_ACTION_PARAM, | |
1933 | SESS_NOT_FOUND_ACTION_FAIL_STR, | |
1934 | lttng_live->params.url->str); | |
7cdc2bab | 1935 | goto error; |
14f28187 | 1936 | case SESSION_NOT_FOUND_ACTION_END: |
f79c2d7a | 1937 | BT_COMP_LOGI("Unable to connect to the requested live viewer session. End gracefully at the first _next() " |
14f28187 FD |
1938 | "call because of %s=\"%s\" component parameter: " |
1939 | "url=\"%s\"", SESS_NOT_FOUND_ACTION_PARAM, | |
1940 | SESS_NOT_FOUND_ACTION_END_STR, | |
1941 | lttng_live->params.url->str); | |
1942 | break; | |
bce81ff2 | 1943 | default: |
498e7994 | 1944 | bt_common_abort(); |
7cdc2bab | 1945 | } |
7cdc2bab MD |
1946 | } |
1947 | ||
14f28187 | 1948 | bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter); |
a3f0c7db | 1949 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; |
14f28187 | 1950 | goto end; |
f79c2d7a | 1951 | |
14f28187 | 1952 | error: |
a3f0c7db | 1953 | status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
14f28187 | 1954 | lttng_live_msg_iter_destroy(lttng_live_msg_iter); |
7cdc2bab | 1955 | end: |
f79c2d7a | 1956 | return status; |
7cdc2bab MD |
1957 | } |
1958 | ||
80aff5ef SM |
1959 | static struct bt_param_validation_map_value_entry_descr list_sessions_params[] = { |
1960 | { URL_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_STRING } }, | |
1961 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
1962 | }; | |
1963 | ||
7cdc2bab | 1964 | static |
d24d5663 PP |
1965 | bt_component_class_query_method_status lttng_live_query_list_sessions( |
1966 | const bt_value *params, const bt_value **result, | |
80aff5ef | 1967 | bt_self_component_class *self_comp_class, |
d24d5663 | 1968 | bt_logging_level log_level) |
7cdc2bab | 1969 | { |
80aff5ef | 1970 | bt_component_class_query_method_status status; |
14f28187 | 1971 | const bt_value *url_value = NULL; |
7cdc2bab | 1972 | const char *url; |
14f28187 | 1973 | struct live_viewer_connection *viewer_connection = NULL; |
f79c2d7a | 1974 | enum lttng_live_viewer_status viewer_status; |
80aff5ef SM |
1975 | enum bt_param_validation_status validation_status; |
1976 | gchar *validate_error = NULL; | |
7cdc2bab | 1977 | |
80aff5ef SM |
1978 | validation_status = bt_param_validation_validate(params, |
1979 | list_sessions_params, &validate_error); | |
1980 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
1981 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
7cdc2bab | 1982 | goto error; |
80aff5ef | 1983 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
a635e507 | 1984 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
1419db2b FD |
1985 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "%s", |
1986 | validate_error); | |
7cdc2bab MD |
1987 | goto error; |
1988 | } | |
1989 | ||
80aff5ef | 1990 | url_value = bt_value_map_borrow_entry_value_const(params, URL_PARAM); |
14f28187 FD |
1991 | url = bt_value_string_get(url_value); |
1992 | ||
f79c2d7a FD |
1993 | viewer_status = live_viewer_connection_create(NULL, self_comp_class, |
1994 | log_level, url, true, NULL, &viewer_connection); | |
1995 | if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { | |
1996 | if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { | |
1997 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, | |
1998 | "Failed to create viewer connection"); | |
1999 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
2000 | } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { | |
2001 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; | |
2002 | } else { | |
2003 | bt_common_abort(); | |
2004 | } | |
7cdc2bab MD |
2005 | goto error; |
2006 | } | |
2007 | ||
14f28187 | 2008 | status = live_viewer_connection_list_sessions(viewer_connection, |
80aff5ef | 2009 | result); |
d24d5663 | 2010 | if (status != BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK) { |
1419db2b FD |
2011 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
2012 | "Failed to list viewer sessions"); | |
c7eee084 PP |
2013 | goto error; |
2014 | } | |
2015 | ||
7cdc2bab | 2016 | goto end; |
c7eee084 | 2017 | |
7cdc2bab | 2018 | error: |
14f28187 | 2019 | BT_VALUE_PUT_REF_AND_RESET(*result); |
c7eee084 | 2020 | |
14f28187 | 2021 | if (status >= 0) { |
d24d5663 | 2022 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
c7eee084 PP |
2023 | } |
2024 | ||
7cdc2bab MD |
2025 | end: |
2026 | if (viewer_connection) { | |
14f28187 | 2027 | live_viewer_connection_destroy(viewer_connection); |
7cdc2bab | 2028 | } |
80aff5ef SM |
2029 | |
2030 | g_free(validate_error); | |
2031 | ||
14f28187 | 2032 | return status; |
7cdc2bab MD |
2033 | } |
2034 | ||
312df793 PP |
2035 | static |
2036 | bt_component_class_query_method_status lttng_live_query_support_info( | |
2037 | const bt_value *params, const bt_value **result, | |
1419db2b | 2038 | bt_self_component_class *self_comp_class, |
312df793 PP |
2039 | bt_logging_level log_level) |
2040 | { | |
2041 | bt_component_class_query_method_status status = | |
2042 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
2043 | const bt_value *input_type_value; | |
2044 | const bt_value *input_value; | |
2045 | double weight = 0; | |
2046 | struct bt_common_lttng_live_url_parts parts = { 0 }; | |
2047 | ||
2048 | /* Used by the logging macros */ | |
2049 | __attribute__((unused)) bt_self_component *self_comp = NULL; | |
2050 | ||
2051 | *result = NULL; | |
2052 | input_type_value = bt_value_map_borrow_entry_value_const(params, | |
2053 | "type"); | |
2054 | if (!input_type_value) { | |
1419db2b FD |
2055 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
2056 | "Missing expected `type` parameter."); | |
312df793 PP |
2057 | goto error; |
2058 | } | |
2059 | ||
2060 | if (!bt_value_is_string(input_type_value)) { | |
1419db2b FD |
2061 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
2062 | "`type` parameter is not a string value."); | |
312df793 PP |
2063 | goto error; |
2064 | } | |
2065 | ||
2066 | if (strcmp(bt_value_string_get(input_type_value), "string") != 0) { | |
2067 | /* We don't handle file system paths */ | |
2068 | goto create_result; | |
2069 | } | |
2070 | ||
2071 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); | |
2072 | if (!input_value) { | |
1419db2b FD |
2073 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
2074 | "Missing expected `input` parameter."); | |
312df793 PP |
2075 | goto error; |
2076 | } | |
2077 | ||
2078 | if (!bt_value_is_string(input_value)) { | |
1419db2b FD |
2079 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
2080 | "`input` parameter is not a string value."); | |
312df793 PP |
2081 | goto error; |
2082 | } | |
2083 | ||
2084 | parts = bt_common_parse_lttng_live_url(bt_value_string_get(input_value), | |
2085 | NULL, 0); | |
2086 | if (parts.session_name) { | |
2087 | /* | |
2088 | * Looks pretty much like an LTTng live URL: we got the | |
2089 | * session name part, which forms a complete URL. | |
2090 | */ | |
2091 | weight = .75; | |
2092 | } | |
2093 | ||
2094 | create_result: | |
2095 | *result = bt_value_real_create_init(weight); | |
2096 | if (!*result) { | |
2097 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; | |
2098 | goto error; | |
2099 | } | |
2100 | ||
2101 | goto end; | |
2102 | ||
2103 | error: | |
2104 | if (status >= 0) { | |
2105 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; | |
2106 | } | |
2107 | ||
2108 | BT_ASSERT(!*result); | |
2109 | ||
2110 | end: | |
2111 | bt_common_destroy_lttng_live_url_parts(&parts); | |
2112 | return status; | |
2113 | } | |
2114 | ||
7cdc2bab | 2115 | BT_HIDDEN |
d24d5663 PP |
2116 | bt_component_class_query_method_status lttng_live_query( |
2117 | bt_self_component_class_source *comp_class, | |
3c729b9a | 2118 | bt_private_query_executor *priv_query_exec, |
14f28187 | 2119 | const char *object, const bt_value *params, |
7c14d641 | 2120 | __attribute__((unused)) void *method_data, |
3c729b9a | 2121 | const bt_value **result) |
7cdc2bab | 2122 | { |
d24d5663 PP |
2123 | bt_component_class_query_method_status status = |
2124 | BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; | |
2ece7dd0 | 2125 | bt_self_component *self_comp = NULL; |
80aff5ef SM |
2126 | bt_self_component_class *self_comp_class = |
2127 | bt_self_component_class_source_as_self_component_class(comp_class); | |
3c729b9a PP |
2128 | bt_logging_level log_level = bt_query_executor_get_logging_level( |
2129 | bt_private_query_executor_as_query_executor_const( | |
2130 | priv_query_exec)); | |
c7eee084 | 2131 | |
7cdc2bab | 2132 | if (strcmp(object, "sessions") == 0) { |
c01594de | 2133 | status = lttng_live_query_list_sessions(params, result, |
80aff5ef | 2134 | self_comp_class, log_level); |
312df793 PP |
2135 | } else if (strcmp(object, "babeltrace.support-info") == 0) { |
2136 | status = lttng_live_query_support_info(params, result, | |
1419db2b | 2137 | self_comp_class, log_level); |
14f28187 | 2138 | } else { |
32a09ecd | 2139 | BT_COMP_LOGI("Unknown query object `%s`", object); |
76b6c2f7 | 2140 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_UNKNOWN_OBJECT; |
14f28187 | 2141 | goto end; |
7cdc2bab | 2142 | } |
14f28187 FD |
2143 | |
2144 | end: | |
2145 | return status; | |
7cdc2bab MD |
2146 | } |
2147 | ||
2148 | static | |
2149 | void lttng_live_component_destroy_data(struct lttng_live_component *lttng_live) | |
2150 | { | |
19bdff20 FD |
2151 | if (!lttng_live) { |
2152 | return; | |
2153 | } | |
14f28187 FD |
2154 | if (lttng_live->params.url) { |
2155 | g_string_free(lttng_live->params.url, TRUE); | |
7cdc2bab MD |
2156 | } |
2157 | g_free(lttng_live); | |
2158 | } | |
2159 | ||
2160 | BT_HIDDEN | |
14f28187 | 2161 | void lttng_live_component_finalize(bt_self_component_source *component) |
7cdc2bab | 2162 | { |
14f28187 | 2163 | void *data = bt_self_component_get_data( |
0f1979c3 | 2164 | bt_self_component_source_as_self_component(component)); |
7cdc2bab MD |
2165 | |
2166 | if (!data) { | |
2167 | return; | |
2168 | } | |
2169 | lttng_live_component_destroy_data(data); | |
2170 | } | |
2171 | ||
2172 | static | |
14f28187 FD |
2173 | enum session_not_found_action parse_session_not_found_action_param( |
2174 | const bt_value *no_session_param) | |
2175 | { | |
2176 | enum session_not_found_action action; | |
80aff5ef SM |
2177 | const char *no_session_act_str = bt_value_string_get(no_session_param); |
2178 | ||
14f28187 FD |
2179 | if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_CONTINUE_STR) == 0) { |
2180 | action = SESSION_NOT_FOUND_ACTION_CONTINUE; | |
2181 | } else if (strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_FAIL_STR) == 0) { | |
2182 | action = SESSION_NOT_FOUND_ACTION_FAIL; | |
14f28187 | 2183 | } else { |
80aff5ef SM |
2184 | BT_ASSERT(strcmp(no_session_act_str, SESS_NOT_FOUND_ACTION_END_STR) == 0); |
2185 | action = SESSION_NOT_FOUND_ACTION_END; | |
14f28187 FD |
2186 | } |
2187 | ||
2188 | return action; | |
2189 | } | |
2190 | ||
80aff5ef SM |
2191 | static struct bt_param_validation_value_descr inputs_elem_descr = { |
2192 | .type = BT_VALUE_TYPE_STRING, | |
2193 | }; | |
2194 | ||
2195 | static const char *sess_not_found_action_choices[] = { | |
2196 | SESS_NOT_FOUND_ACTION_CONTINUE_STR, | |
2197 | SESS_NOT_FOUND_ACTION_FAIL_STR, | |
2198 | SESS_NOT_FOUND_ACTION_END_STR, | |
2199 | }; | |
2200 | ||
2201 | static struct bt_param_validation_map_value_entry_descr params_descr[] = { | |
2202 | { INPUTS_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = { | |
2203 | .min_length = 1, | |
2204 | .max_length = 1, | |
2205 | .element_type = &inputs_elem_descr, | |
2206 | } } }, | |
2207 | { SESS_NOT_FOUND_ACTION_PARAM, BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { BT_VALUE_TYPE_STRING, .string = { | |
2208 | .choices = sess_not_found_action_choices, | |
2209 | } } }, | |
2210 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
2211 | }; | |
2212 | ||
70384700 | 2213 | static |
80aff5ef SM |
2214 | bt_component_class_initialize_method_status lttng_live_component_create( |
2215 | const bt_value *params, | |
2216 | bt_logging_level log_level, | |
2217 | bt_self_component *self_comp, | |
2218 | struct lttng_live_component **component) | |
7cdc2bab | 2219 | { |
80aff5ef | 2220 | struct lttng_live_component *lttng_live = NULL; |
312df793 PP |
2221 | const bt_value *inputs_value; |
2222 | const bt_value *url_value; | |
2223 | const bt_value *value; | |
7cdc2bab | 2224 | const char *url; |
80aff5ef SM |
2225 | enum bt_param_validation_status validation_status; |
2226 | gchar *validation_error = NULL; | |
2227 | bt_component_class_initialize_method_status status; | |
2228 | ||
2229 | validation_status = bt_param_validation_validate(params, params_descr, | |
2230 | &validation_error); | |
2231 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
2232 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
2233 | goto error; | |
2234 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
2235 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validation_error); | |
2236 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
2237 | goto error; | |
2238 | } | |
7cdc2bab MD |
2239 | |
2240 | lttng_live = g_new0(struct lttng_live_component, 1); | |
2241 | if (!lttng_live) { | |
80aff5ef | 2242 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
7cdc2bab MD |
2243 | goto end; |
2244 | } | |
c01594de | 2245 | lttng_live->log_level = log_level; |
2ece7dd0 | 2246 | lttng_live->self_comp = self_comp; |
7cdc2bab | 2247 | lttng_live->max_query_size = MAX_QUERY_SIZE; |
14f28187 FD |
2248 | lttng_live->has_msg_iter = false; |
2249 | ||
80aff5ef SM |
2250 | inputs_value = |
2251 | bt_value_map_borrow_entry_value_const(params, INPUTS_PARAM); | |
2252 | url_value = | |
2253 | bt_value_array_borrow_element_by_index_const(inputs_value, 0); | |
312df793 | 2254 | url = bt_value_string_get(url_value); |
80aff5ef | 2255 | |
14f28187 FD |
2256 | lttng_live->params.url = g_string_new(url); |
2257 | if (!lttng_live->params.url) { | |
80aff5ef | 2258 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
7cdc2bab MD |
2259 | goto error; |
2260 | } | |
14f28187 FD |
2261 | |
2262 | value = bt_value_map_borrow_entry_value_const(params, | |
2263 | SESS_NOT_FOUND_ACTION_PARAM); | |
312df793 | 2264 | if (value) { |
14f28187 FD |
2265 | lttng_live->params.sess_not_found_act = |
2266 | parse_session_not_found_action_param(value); | |
14f28187 | 2267 | } else { |
312df793 PP |
2268 | BT_COMP_LOGI("Optional `%s` parameter is missing: " |
2269 | "defaulting to `%s`.", | |
14f28187 FD |
2270 | SESS_NOT_FOUND_ACTION_PARAM, |
2271 | SESS_NOT_FOUND_ACTION_CONTINUE_STR); | |
2272 | lttng_live->params.sess_not_found_act = | |
2273 | SESSION_NOT_FOUND_ACTION_CONTINUE; | |
7cdc2bab | 2274 | } |
4c66436f | 2275 | |
80aff5ef | 2276 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
7cdc2bab MD |
2277 | goto end; |
2278 | ||
2279 | error: | |
2280 | lttng_live_component_destroy_data(lttng_live); | |
2281 | lttng_live = NULL; | |
2282 | end: | |
80aff5ef SM |
2283 | g_free(validation_error); |
2284 | ||
2285 | *component = lttng_live; | |
2286 | return status; | |
d3e4dcd8 PP |
2287 | } |
2288 | ||
f3bc2010 | 2289 | BT_HIDDEN |
21a9f056 | 2290 | bt_component_class_initialize_method_status lttng_live_component_init( |
c01594de | 2291 | bt_self_component_source *self_comp_src, |
59225a3e SM |
2292 | bt_self_component_source_configuration *config, |
2293 | const bt_value *params, | |
2294 | __attribute__((unused)) void *init_method_data) | |
f3bc2010 | 2295 | { |
7cdc2bab | 2296 | struct lttng_live_component *lttng_live; |
80aff5ef | 2297 | bt_component_class_initialize_method_status ret; |
c01594de PP |
2298 | bt_self_component *self_comp = |
2299 | bt_self_component_source_as_self_component(self_comp_src); | |
2300 | bt_logging_level log_level = bt_component_get_logging_level( | |
2301 | bt_self_component_as_component(self_comp)); | |
d24d5663 | 2302 | bt_self_component_add_port_status add_port_status; |
7cdc2bab | 2303 | |
80aff5ef SM |
2304 | ret = lttng_live_component_create(params, log_level, self_comp, <tng_live); |
2305 | if (ret != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { | |
19bdff20 | 2306 | goto error; |
7cdc2bab MD |
2307 | } |
2308 | ||
d24d5663 PP |
2309 | add_port_status = bt_self_component_source_add_output_port( |
2310 | self_comp_src, "out", NULL, NULL); | |
80aff5ef SM |
2311 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
2312 | ret = (int) add_port_status; | |
2313 | goto end; | |
147337a3 | 2314 | } |
7cdc2bab | 2315 | |
c01594de | 2316 | bt_self_component_set_data(self_comp, lttng_live); |
19bdff20 | 2317 | goto end; |
7cdc2bab | 2318 | |
19bdff20 FD |
2319 | error: |
2320 | lttng_live_component_destroy_data(lttng_live); | |
2321 | lttng_live = NULL; | |
7cdc2bab MD |
2322 | end: |
2323 | return ret; | |
d85ef162 | 2324 | } |