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