X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fplugins%2Fctf%2Flttng-live%2Flttng-live.c;h=bc8ea07d4d5e841774bc96596862b372d95ec408;hb=883ffb2f79116ad331926162546b3cc257c82a65;hp=e236655815a5d68f7b9854bc2b052075a06761a6;hpb=f79c2d7a3575db7cd07f502b39e1db76619b70a6;p=babeltrace.git diff --git a/src/plugins/ctf/lttng-live/lttng-live.c b/src/plugins/ctf/lttng-live/lttng-live.c index e2366558..bc8ea07d 100644 --- a/src/plugins/ctf/lttng-live/lttng-live.c +++ b/src/plugins/ctf/lttng-live/lttng-live.c @@ -1,31 +1,11 @@ /* - * lttng-live.c - * - * Babeltrace CTF LTTng-live Client Component + * SPDX-License-Identifier: MIT * * Copyright 2019 Francis Deslauriers * Copyright 2016 Jérémie Galarneau * Copyright 2016 Mathieu Desnoyers * - * Author: Jérémie Galarneau - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Babeltrace CTF LTTng-live Client Component */ #define BT_COMP_LOG_SELF_COMP self_comp @@ -62,7 +42,8 @@ #define print_dbg(fmt, ...) BT_COMP_LOGD(fmt, ## __VA_ARGS__) static -const char *print_live_iterator_status(enum lttng_live_iterator_status status) +const char *lttng_live_iterator_status_string( + enum lttng_live_iterator_status status) { switch (status) { case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: @@ -87,9 +68,9 @@ const char *print_live_iterator_status(enum lttng_live_iterator_status status) } static -const char *print_state(struct lttng_live_stream_iterator *s) +const char *lttng_live_stream_state_string(enum lttng_live_stream_state state) { - switch (s->state) { + switch (state) { case LTTNG_LIVE_STREAM_ACTIVE_NO_DATA: return "ACTIVE_NO_DATA"; case LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA: @@ -105,11 +86,11 @@ const char *print_state(struct lttng_live_stream_iterator *s) } } -#define print_stream_state(live_stream_iter) \ +#define LTTNG_LIVE_LOGD_STREAM_ITER(live_stream_iter) \ do { \ - BT_COMP_LOGD("stream state %s last_inact_ts %" PRId64 \ - ", curr_inact_ts %" PRId64, \ - print_state(live_stream_iter), \ + BT_COMP_LOGD("Live stream iterator state=%s, last-inact-ts=%" PRId64 \ + ", curr-inact-ts %" PRId64, \ + lttng_live_stream_state_string(live_stream_iter->state), \ live_stream_iter->last_inactivity_ts, \ live_stream_iter->current_inactivity_ts); \ } while (0); @@ -132,7 +113,7 @@ end: } static -struct lttng_live_trace *lttng_live_find_trace(struct lttng_live_session *session, +struct lttng_live_trace *lttng_live_session_borrow_trace_by_id(struct lttng_live_session *session, uint64_t trace_id) { uint64_t trace_idx; @@ -157,7 +138,7 @@ void lttng_live_destroy_trace(struct lttng_live_trace *trace) bt_logging_level log_level = trace->log_level; bt_self_component *self_comp = trace->self_comp; - BT_COMP_LOGD("Destroy lttng_live_trace"); + BT_COMP_LOGD("Destroying live trace: trace-id=%"PRIu64, trace->id); BT_ASSERT(trace->stream_iterators); g_ptr_array_free(trace->stream_iterators, TRUE); @@ -177,6 +158,9 @@ struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *sess bt_logging_level log_level = session->log_level; bt_self_component *self_comp = session->self_comp; + BT_COMP_LOGD("Creating live trace: " + "session-id=%"PRIu64", trace-id=%"PRIu64, + session->id, trace_id); trace = g_new0(struct lttng_live_trace, 1); if (!trace) { BT_COMP_LOGE_APPEND_CAUSE(self_comp, @@ -192,10 +176,9 @@ struct lttng_live_trace *lttng_live_create_trace(struct lttng_live_session *sess trace->stream_iterators = g_ptr_array_new_with_free_func( (GDestroyNotify) lttng_live_stream_iterator_destroy); BT_ASSERT(trace->stream_iterators); - trace->new_metadata_needed = true; + trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; g_ptr_array_add(session->traces, trace); - BT_COMP_LOGI("Create trace"); goto end; error: g_free(trace); @@ -205,12 +188,12 @@ end: } BT_HIDDEN -struct lttng_live_trace *lttng_live_borrow_trace( +struct lttng_live_trace *lttng_live_session_borrow_or_create_trace_by_id( struct lttng_live_session *session, uint64_t trace_id) { struct lttng_live_trace *trace; - trace = lttng_live_find_trace(session, trace_id); + trace = lttng_live_session_borrow_trace_by_id(session, trace_id); if (trace) { goto end; } @@ -232,6 +215,10 @@ int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, bt_logging_level log_level = lttng_live_msg_iter->log_level; bt_self_component *self_comp = lttng_live_msg_iter->self_comp; + BT_COMP_LOGD("Adding live session: " + "session-id=%" PRIu64 ", hostname=\"%s\" session-name=\"%s\"", + session_id, hostname, session_name); + session = g_new0(struct lttng_live_session, 1); if (!session) { BT_COMP_LOGE_APPEND_CAUSE(self_comp, @@ -253,8 +240,6 @@ int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, session->session_name = g_string_new(session_name); BT_ASSERT(session->session_name); - BT_COMP_LOGI("Reading from session: %" PRIu64 " hostname: %s session_name: %s", - session->id, hostname, session_name); g_ptr_array_add(lttng_live_msg_iter->sessions, session); goto end; error: @@ -276,9 +261,11 @@ void lttng_live_destroy_session(struct lttng_live_session *session) log_level = session->log_level; self_comp = session->self_comp; - BT_COMP_LOGD("Destroy lttng live session"); + BT_COMP_LOGD("Destroying live session: " + "session-id=%"PRIu64", session-name=\"%s\"", + session->id, session->session_name->str); if (session->id != -1ULL) { - if (lttng_live_detach_session(session)) { + if (lttng_live_session_detach(session)) { if (!lttng_live_graph_is_canceled( session->lttng_live_msg_iter)) { /* Old relayd cannot detach sessions. */ @@ -388,7 +375,8 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stre enum lttng_live_stream_state orig_state = lttng_live_stream->state; struct packet_index index; - if (lttng_live_stream->trace->new_metadata_needed) { + if (lttng_live_stream->trace->metadata_stream_state == + LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; goto end; } @@ -413,7 +401,7 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_no_data_stre if (orig_state == LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA && last_inact_ts == curr_inact_ts) { ret = LTTNG_LIVE_ITERATOR_STATUS_AGAIN; - print_stream_state(lttng_live_stream); + LTTNG_LIVE_LOGD_STREAM_ITER(lttng_live_stream); } else { ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; } @@ -446,9 +434,13 @@ enum lttng_live_iterator_status lttng_live_get_session( enum lttng_live_iterator_status status; uint64_t trace_idx; + BT_COMP_LOGD("Updating all streams for session: " + "session-id=%"PRIu64", session-name=\"%s\"", + session->id, session->session_name->str); + if (!session->attached) { enum lttng_live_viewer_status attach_status = - lttng_live_attach_session(session, + lttng_live_session_attach(session, lttng_live_msg_iter->self_msg_iter); if (attach_status != LTTNG_LIVE_VIEWER_STATUS_OK) { if (lttng_live_graph_is_canceled(lttng_live_msg_iter)) { @@ -469,7 +461,7 @@ enum lttng_live_iterator_status lttng_live_get_session( } } - status = lttng_live_get_new_streams(session, + status = lttng_live_session_get_new_streams(session, lttng_live_msg_iter->self_msg_iter); if (status != LTTNG_LIVE_ITERATOR_STATUS_OK && status != LTTNG_LIVE_ITERATOR_STATUS_END) { @@ -482,23 +474,19 @@ enum lttng_live_iterator_status lttng_live_get_session( status = lttng_live_metadata_update(trace); switch (status) { + case LTTNG_LIVE_ITERATOR_STATUS_END: case LTTNG_LIVE_ITERATOR_STATUS_OK: trace_idx++; break; - case LTTNG_LIVE_ITERATOR_STATUS_END: - /* - * The trace has ended. Remove it of the array an - * continue the iteration. - * We can remove the trace safely when using the - * g_ptr_array_remove_index_fast because it replaces - * the element at trace_idx with the array's last - * element. trace_idx is not incremented because of - * that. - */ - (void) g_ptr_array_remove_index_fast(session->traces, - trace_idx); - break; + case LTTNG_LIVE_ITERATOR_STATUS_CONTINUE: + case LTTNG_LIVE_ITERATOR_STATUS_AGAIN: + goto end; default: + BT_COMP_LOGE_APPEND_CAUSE(self_comp, + "Error updating trace metadata: " + "stream-iter-status=%s, trace-id=%"PRIu64, + lttng_live_iterator_status_string(status), + trace->id); goto end; } } @@ -509,19 +497,6 @@ end: return status; } -BT_HIDDEN -void lttng_live_need_new_streams(struct lttng_live_msg_iter *lttng_live_msg_iter) -{ - uint64_t session_idx; - - for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; - session_idx++) { - struct lttng_live_session *session = - g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); - session->new_streams_needed = true; - } -} - static void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng_live_msg_iter) { @@ -536,7 +511,11 @@ void lttng_live_force_new_streams_and_metadata(struct lttng_live_msg_iter *lttng trace_idx++) { struct lttng_live_trace *trace = g_ptr_array_index(session->traces, trace_idx); - trace->new_metadata_needed = true; + + BT_ASSERT(trace->metadata_stream_state != + LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED); + + trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; } } } @@ -555,6 +534,8 @@ lttng_live_iterator_handle_new_streams_and_metadata( enum session_not_found_action sess_not_found_act = lttng_live_msg_iter->lttng_live_comp->params.sess_not_found_act; + BT_COMP_LOGD("Update data and metadata of all sessions: " + "live-msg-iter-addr=%p", lttng_live_msg_iter); /* * In a remotely distant future, we could add a "new * session" flag to the protocol, which would tell us that we @@ -563,9 +544,11 @@ lttng_live_iterator_handle_new_streams_and_metadata( */ if (lttng_live_msg_iter->sessions->len == 0) { if (sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { + BT_COMP_LOGD("No session found. Exiting in accordance with the `session-not-found-action` parameter"); status = LTTNG_LIVE_ITERATOR_STATUS_END; goto end; } else { + BT_COMP_LOGD("No session found. Try creating a new one in accordance with the `session-not-found-action` parameter"); /* * Retry to create a viewer session for the requested * session name. @@ -620,7 +603,7 @@ static enum lttng_live_iterator_status emit_inactivity_message( struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_live_stream_iterator *stream_iter, - bt_message **message, uint64_t timestamp) + const bt_message **message, uint64_t timestamp) { enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; bt_logging_level log_level = lttng_live_msg_iter->log_level; @@ -652,7 +635,7 @@ static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_quiescent_stream( struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_live_stream_iterator *lttng_live_stream, - bt_message **message) + const bt_message **message) { enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; @@ -737,7 +720,7 @@ int live_get_msg_ts_ns(struct lttng_live_stream_iterator *stream_iter, msg); break; case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: - clock_snapshot = bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( + clock_snapshot = bt_message_message_iterator_inactivity_borrow_clock_snapshot_const( msg); break; default: @@ -778,7 +761,7 @@ static enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_stream( struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_live_stream_iterator *lttng_live_stream, - bt_message **message) + const bt_message **message) { enum lttng_live_iterator_status ret = LTTNG_LIVE_ITERATOR_STATUS_OK; bt_logging_level log_level = lttng_live_msg_iter->log_level; @@ -799,7 +782,7 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_ trace_idx++) { struct lttng_live_trace *trace = g_ptr_array_index(session->traces, trace_idx); - if (trace->new_metadata_needed) { + if (trace->metadata_stream_state == LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED) { ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; goto end; } @@ -808,6 +791,10 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_ if (lttng_live_stream->state != LTTNG_LIVE_STREAM_ACTIVE_DATA) { ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; + BT_COMP_LOGE_APPEND_CAUSE(self_comp, + "Invalid state of live stream iterator" + "stream-iter-status=%s", + lttng_live_stream_state_string(lttng_live_stream->state)); goto end; } @@ -828,14 +815,14 @@ enum lttng_live_iterator_status lttng_live_iterator_next_handle_one_active_data_ */ ret = LTTNG_LIVE_ITERATOR_STATUS_CONTINUE; break; - case CTF_MSG_ITER_STATUS_INVAL: - /* No argument provided by the user, so don't return INVAL. */ case CTF_MSG_ITER_STATUS_ERROR: default: ret = LTTNG_LIVE_ITERATOR_STATUS_ERROR; BT_COMP_LOGE_APPEND_CAUSE(self_comp, - "CTF message iterator return an error or failed: " - "msg_iter=%p", lttng_live_stream->msg_iter); + "CTF message iterator failed to get next message: " + "msg-iter=%p, msg-iter-status=%s", + lttng_live_stream->msg_iter, + ctf_msg_iter_status_string(status)); break; } @@ -847,7 +834,7 @@ static enum lttng_live_iterator_status lttng_live_iterator_close_stream( struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_live_stream_iterator *stream_iter, - bt_message **curr_msg) + const bt_message **curr_msg) { enum lttng_live_iterator_status live_status = LTTNG_LIVE_ITERATOR_STATUS_OK; @@ -866,6 +853,10 @@ enum lttng_live_iterator_status lttng_live_iterator_close_stream( "Error getting the next message from CTF message iterator"); live_status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; goto end; + } else if (status == CTF_MSG_ITER_STATUS_EOF) { + BT_COMP_LOGI("Reached the end of the live stream iterator."); + live_status = LTTNG_LIVE_ITERATOR_STATUS_END; + goto end; } BT_ASSERT(status == CTF_MSG_ITER_STATUS_OK); @@ -926,12 +917,15 @@ static enum lttng_live_iterator_status lttng_live_iterator_next_msg_on_stream( struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_live_stream_iterator *stream_iter, - bt_message **curr_msg) + const bt_message **curr_msg) { bt_logging_level log_level = lttng_live_msg_iter->log_level; bt_self_component *self_comp = lttng_live_msg_iter->self_comp; enum lttng_live_iterator_status live_status; + BT_COMP_LOGD("Finding the next message for stream iterator: " + "stream-name=\"%s\"", stream_iter->name->str); + if (stream_iter->has_stream_hung_up) { /* * The stream has hung up and the stream was properly closed @@ -944,7 +938,7 @@ enum lttng_live_iterator_status lttng_live_iterator_next_msg_on_stream( } retry: - print_stream_state(stream_iter); + LTTNG_LIVE_LOGD_STREAM_ITER(stream_iter); live_status = lttng_live_iterator_handle_new_streams_and_metadata( lttng_live_msg_iter); if (live_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { @@ -987,6 +981,77 @@ end: return live_status; } +static +bool is_discarded_packet_or_event_message(const bt_message *msg) +{ + const enum bt_message_type msg_type = bt_message_get_type(msg); + + return msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS || + msg_type == BT_MESSAGE_TYPE_DISCARDED_PACKETS; +} + +static +enum lttng_live_iterator_status adjust_discarded_packets_message( + bt_self_message_iterator *iter, + const bt_stream *stream, + const bt_message *msg_in, bt_message **msg_out, + uint64_t new_begin_ts) +{ + enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; + enum bt_property_availability availability; + const bt_clock_snapshot *clock_snapshot; + uint64_t end_ts; + uint64_t count; + + clock_snapshot = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msg_in); + end_ts = bt_clock_snapshot_get_value(clock_snapshot); + + availability = bt_message_discarded_packets_get_count(msg_in, &count); + BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); + + *msg_out = bt_message_discarded_packets_create_with_default_clock_snapshots( + iter, stream, new_begin_ts, end_ts); + if (!*msg_out) { + status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; + goto end; + } + + bt_message_discarded_packets_set_count(*msg_out, count); +end: + return status; +} + +static +enum lttng_live_iterator_status adjust_discarded_events_message( + bt_self_message_iterator *iter, + const bt_stream *stream, + const bt_message *msg_in, bt_message **msg_out, + uint64_t new_begin_ts) +{ + enum lttng_live_iterator_status status = LTTNG_LIVE_ITERATOR_STATUS_OK; + enum bt_property_availability availability; + const bt_clock_snapshot *clock_snapshot; + uint64_t end_ts; + uint64_t count; + + clock_snapshot = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msg_in); + end_ts = bt_clock_snapshot_get_value(clock_snapshot); + + availability = bt_message_discarded_events_get_count(msg_in, &count); + BT_ASSERT_DBG(availability == BT_PROPERTY_AVAILABILITY_AVAILABLE); + + *msg_out = bt_message_discarded_events_create_with_default_clock_snapshots( + iter, stream, new_begin_ts, end_ts); + if (!*msg_out) { + status = LTTNG_LIVE_ITERATOR_STATUS_NOMEM; + goto end; + } + + bt_message_discarded_events_set_count(*msg_out, count); +end: + return status; +} + static enum lttng_live_iterator_status next_stream_iterator_for_trace( struct lttng_live_msg_iter *lttng_live_msg_iter, @@ -1002,6 +1067,9 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace( BT_ASSERT_DBG(live_trace); BT_ASSERT_DBG(live_trace->stream_iterators); + + BT_COMP_LOGD("Finding the next stream iterator for trace: " + "trace-id=%"PRIu64, live_trace->id); /* * Update the current message of every stream iterators of this trace. * The current msg of every stream must have a timestamp equal or @@ -1016,17 +1084,17 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace( stream_iter_idx); /* - * Find if there is are now current message for this stream - * iterator get it. + * If there is no current message for this stream, go fetch + * one. */ while (!stream_iter->current_msg) { - bt_message *msg = NULL; + const bt_message *msg = NULL; int64_t curr_msg_ts_ns = INT64_MAX; stream_iter_status = lttng_live_iterator_next_msg_on_stream( lttng_live_msg_iter, stream_iter, &msg); BT_COMP_LOGD("live stream iterator returned status :%s", - print_live_iterator_status(stream_iter_status)); + lttng_live_iterator_status_string(stream_iter_status)); if (stream_iter_status == LTTNG_LIVE_ITERATOR_STATUS_END) { stream_iter_is_ended = true; break; @@ -1048,13 +1116,66 @@ enum lttng_live_iterator_status next_stream_iterator_for_trace( /* * Check if the message of the current live stream - * iterator occured at the exact same time or after the + * iterator occurred at the exact same time or after the * last message returned by this component's message * iterator. If not, we return an error. */ if (curr_msg_ts_ns >= lttng_live_msg_iter->last_msg_ts_ns) { stream_iter->current_msg = msg; stream_iter->current_msg_ts_ns = curr_msg_ts_ns; + } else if (stream_iter->last_inactivity_ts > curr_msg_ts_ns && + is_discarded_packet_or_event_message(msg)) { + /* + * The CTF message iterator emits Discarded + * Packets and Events with synthesized begin and + * end timestamps from the bounds of the last + * known packet and the newly decoded packet + * header. + * + * The CTF message iterator is not aware of + * stream inactivity beacons. Hence, we have + * to adjust the begin timestamp of those types + * of messages if a stream signaled its + * inactivity up until _after_ the last known + * packet's begin timestamp. + * + * Otherwise, the monotonicity guarantee would + * not be preserved. + */ + const enum bt_message_type msg_type = + bt_message_get_type(msg); + enum lttng_live_iterator_status adjust_status = + LTTNG_LIVE_ITERATOR_STATUS_OK; + bt_message *adjusted_message; + + switch (msg_type) { + case BT_MESSAGE_TYPE_DISCARDED_EVENTS: + adjust_status = adjust_discarded_events_message( + lttng_live_msg_iter->self_msg_iter, + stream_iter->stream, + msg, &adjusted_message, + stream_iter->last_inactivity_ts); + break; + case BT_MESSAGE_TYPE_DISCARDED_PACKETS: + adjust_status = adjust_discarded_packets_message( + lttng_live_msg_iter->self_msg_iter, + stream_iter->stream, + msg, &adjusted_message, + stream_iter->last_inactivity_ts); + break; + default: + bt_common_abort(); + } + + if (adjust_status != LTTNG_LIVE_ITERATOR_STATUS_OK) { + stream_iter_status = adjust_status; + goto end; + } + + BT_ASSERT_DBG(adjusted_message); + stream_iter->current_msg = adjusted_message; + stream_iter->current_msg_ts_ns = + stream_iter->last_inactivity_ts; } else { /* * We received a message in the past. To ensure @@ -1161,6 +1282,8 @@ enum lttng_live_iterator_status next_stream_iterator_for_session( int64_t youngest_candidate_msg_ts = INT64_MAX; struct lttng_live_stream_iterator *youngest_candidate_stream_iter = NULL; + BT_COMP_LOGD("Finding the next stream iterator for session: " + "session-id=%"PRIu64, session->id); /* * Make sure we are attached to the session and look for new streams * and metadata. @@ -1264,12 +1387,12 @@ void put_messages(bt_message_array_const msgs, uint64_t count) } BT_HIDDEN -bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next( +bt_message_iterator_class_next_method_status lttng_live_msg_iter_next( bt_self_message_iterator *self_msg_it, bt_message_array_const msgs, uint64_t capacity, uint64_t *count) { - bt_component_class_message_iterator_next_method_status status; + bt_message_iterator_class_next_method_status status; enum lttng_live_viewer_status viewer_status; struct lttng_live_msg_iter *lttng_live_msg_iter = bt_self_message_iterator_get_data(self_msg_it); @@ -1293,7 +1416,7 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next( * is to prevent other graph users from using this live * iterator in an messed up internal state. */ - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Message iterator was interrupted during a previous call to the `next()` and currently does not support continuing after such event."); goto end; @@ -1313,7 +1436,7 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next( if (lttng_live_msg_iter->sessions->len == 0) { if (lttng_live->params.sess_not_found_act != SESSION_NOT_FOUND_ACTION_CONTINUE) { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; goto end; } else { /* @@ -1324,11 +1447,11 @@ bt_component_class_message_iterator_next_method_status lttng_live_msg_iter_next( viewer_status = lttng_live_create_viewer_session(lttng_live_msg_iter); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error creating LTTng live viewer session"); } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; } else { bt_common_abort(); } @@ -1479,21 +1602,30 @@ return_status: * doesn't support restarting after an interruption. */ if (*count > 0) { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK; } else { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_AGAIN; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_AGAIN; } break; case LTTNG_LIVE_ITERATOR_STATUS_END: - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; break; case LTTNG_LIVE_ITERATOR_STATUS_NOMEM: - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; + BT_COMP_LOGE_APPEND_CAUSE(self_comp, + "Memory error preparing the next batch of messages: " + "live-iter-status=%s", + lttng_live_iterator_status_string(stream_iter_status)); + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR; break; case LTTNG_LIVE_ITERATOR_STATUS_ERROR: case LTTNG_LIVE_ITERATOR_STATUS_INVAL: case LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED: - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; + BT_COMP_LOGE_APPEND_CAUSE(self_comp, + "Error preparing the next batch of messages: " + "live-iter-status=%s", + lttng_live_iterator_status_string(stream_iter_status)); + + status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; /* Put all existing messages on error. */ put_messages(msgs, *count); break; @@ -1505,52 +1637,72 @@ end: return status; } +static +struct lttng_live_msg_iter *lttng_live_msg_iter_create( + struct lttng_live_component *lttng_live_comp, + bt_self_message_iterator *self_msg_it) +{ + bt_self_component *self_comp = lttng_live_comp->self_comp; + bt_logging_level log_level = lttng_live_comp->log_level; + + struct lttng_live_msg_iter *lttng_live_msg_iter = + g_new0(struct lttng_live_msg_iter, 1); + if (!lttng_live_msg_iter) { + BT_COMP_LOGE_APPEND_CAUSE(self_comp, + "Failed to allocate lttng_live_msg_iter"); + goto end; + } + + lttng_live_msg_iter->log_level = lttng_live_comp->log_level; + lttng_live_msg_iter->self_comp = lttng_live_comp->self_comp; + lttng_live_msg_iter->lttng_live_comp = lttng_live_comp; + lttng_live_msg_iter->self_msg_iter = self_msg_it; + + lttng_live_msg_iter->active_stream_iter = 0; + lttng_live_msg_iter->last_msg_ts_ns = INT64_MIN; + lttng_live_msg_iter->was_interrupted = false; + + lttng_live_msg_iter->sessions = g_ptr_array_new_with_free_func( + (GDestroyNotify) lttng_live_destroy_session); + BT_ASSERT(lttng_live_msg_iter->sessions); + +end: + return lttng_live_msg_iter; + +} + BT_HIDDEN -bt_component_class_message_iterator_initialize_method_status lttng_live_msg_iter_init( +bt_message_iterator_class_initialize_method_status lttng_live_msg_iter_init( bt_self_message_iterator *self_msg_it, bt_self_message_iterator_configuration *config, - bt_self_component_source *self_comp_src, bt_self_component_port_output *self_port) { - bt_component_class_message_iterator_initialize_method_status status; - bt_self_component *self_comp = - bt_self_component_source_as_self_component(self_comp_src); + bt_message_iterator_class_initialize_method_status status; struct lttng_live_component *lttng_live; struct lttng_live_msg_iter *lttng_live_msg_iter; enum lttng_live_viewer_status viewer_status; bt_logging_level log_level; - - BT_ASSERT(self_msg_it); + bt_self_component *self_comp = + bt_self_message_iterator_borrow_component(self_msg_it); lttng_live = bt_self_component_get_data(self_comp); log_level = lttng_live->log_level; self_comp = lttng_live->self_comp; + /* There can be only one downstream iterator at the same time. */ BT_ASSERT(!lttng_live->has_msg_iter); lttng_live->has_msg_iter = true; - lttng_live_msg_iter = g_new0(struct lttng_live_msg_iter, 1); + lttng_live_msg_iter = lttng_live_msg_iter_create(lttng_live, + self_msg_it); if (!lttng_live_msg_iter) { - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; + status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; BT_COMP_LOGE_APPEND_CAUSE(self_comp, - "Failed to allocate lttng_live_msg_iter"); - goto end; + "Failed to create lttng_live_msg_iter"); + goto error; } - lttng_live_msg_iter->log_level = lttng_live->log_level; - lttng_live_msg_iter->self_comp = lttng_live->self_comp; - lttng_live_msg_iter->lttng_live_comp = lttng_live; - lttng_live_msg_iter->self_msg_iter = self_msg_it; - - lttng_live_msg_iter->active_stream_iter = 0; - lttng_live_msg_iter->last_msg_ts_ns = INT64_MIN; - lttng_live_msg_iter->was_interrupted = false; - - lttng_live_msg_iter->sessions = g_ptr_array_new_with_free_func( - (GDestroyNotify) lttng_live_destroy_session); - BT_ASSERT(lttng_live_msg_iter->sessions); - viewer_status = live_viewer_connection_create(self_comp, NULL, log_level, lttng_live->params.url->str, false, lttng_live_msg_iter, <tng_live_msg_iter->viewer_connection); @@ -1615,11 +1767,11 @@ bt_component_class_message_iterator_initialize_method_status lttng_live_msg_iter } bt_self_message_iterator_set_data(self_msg_it, lttng_live_msg_iter); - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK; + status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; goto end; error: - status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR; + status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR; lttng_live_msg_iter_destroy(lttng_live_msg_iter); end: return status;