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