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