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