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