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