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