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