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