| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 5 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * BabelTrace - LTTng-live client Component |
| 9 | */ |
| 10 | |
| 11 | #ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H |
| 12 | #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H |
| 13 | |
| 14 | #include <glib.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include <babeltrace2/babeltrace.h> |
| 18 | |
| 19 | #include "../common/src/metadata/tsdl/decoder.hpp" |
| 20 | #include "../common/src/msg-iter/msg-iter.hpp" |
| 21 | #include "viewer-connection.hpp" |
| 22 | |
| 23 | enum lttng_live_stream_state |
| 24 | { |
| 25 | /* This stream won't have data until some known time in the future. */ |
| 26 | LTTNG_LIVE_STREAM_QUIESCENT, |
| 27 | /* |
| 28 | * This stream won't have data until some known time in the future and |
| 29 | * the message iterator inactivity message was already sent downstream. |
| 30 | */ |
| 31 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, /* */ |
| 32 | /* This stream has data ready to be consumed. */ |
| 33 | LTTNG_LIVE_STREAM_ACTIVE_DATA, |
| 34 | /* |
| 35 | * This stream has no data left to consume. We should asked the relay |
| 36 | * for more. |
| 37 | */ |
| 38 | LTTNG_LIVE_STREAM_ACTIVE_NO_DATA, |
| 39 | /* This stream won't have anymore data, ever. */ |
| 40 | LTTNG_LIVE_STREAM_EOF, |
| 41 | }; |
| 42 | |
| 43 | /* Iterator over a live stream. */ |
| 44 | struct lttng_live_stream_iterator |
| 45 | { |
| 46 | bt_logging_level log_level = (bt_logging_level) 0; |
| 47 | bt_self_component *self_comp = nullptr; |
| 48 | |
| 49 | /* Owned by this. */ |
| 50 | bt_stream *stream = nullptr; |
| 51 | |
| 52 | /* Weak reference. */ |
| 53 | struct lttng_live_trace *trace = nullptr; |
| 54 | |
| 55 | /* |
| 56 | * Since only a single iterator per viewer connection, we have |
| 57 | * only a single message iterator per stream. |
| 58 | */ |
| 59 | struct ctf_msg_iter *msg_iter = nullptr; |
| 60 | |
| 61 | uint64_t viewer_stream_id = 0; |
| 62 | |
| 63 | struct |
| 64 | { |
| 65 | bool is_set = false; |
| 66 | uint64_t value = 0; |
| 67 | } ctf_stream_class_id; |
| 68 | |
| 69 | /* base offset in current index. */ |
| 70 | uint64_t base_offset = 0; |
| 71 | /* len to read in current index. */ |
| 72 | uint64_t len = 0; |
| 73 | /* offset in current index. */ |
| 74 | uint64_t offset = 0; |
| 75 | |
| 76 | /* |
| 77 | * Clock Snapshot value of the last message iterator inactivity message |
| 78 | * sent downstream. |
| 79 | */ |
| 80 | struct |
| 81 | { |
| 82 | bool is_set = false; |
| 83 | uint64_t value = 0; |
| 84 | } last_inactivity_ts; |
| 85 | |
| 86 | /* |
| 87 | * Clock Snapshot value of the current message iterator inactivity |
| 88 | * message we might want to send downstream. |
| 89 | */ |
| 90 | uint64_t current_inactivity_ts = 0; |
| 91 | |
| 92 | enum lttng_live_stream_state state = LTTNG_LIVE_STREAM_QUIESCENT; |
| 93 | |
| 94 | /* |
| 95 | * The current message produced by this live stream iterator. Owned by |
| 96 | * this. |
| 97 | */ |
| 98 | const bt_message *current_msg = nullptr; |
| 99 | |
| 100 | /* Timestamp in nanoseconds of the current message (current_msg). */ |
| 101 | int64_t current_msg_ts_ns = 0; |
| 102 | |
| 103 | /* Owned by this. */ |
| 104 | uint8_t *buf = nullptr; |
| 105 | size_t buflen = 0; |
| 106 | |
| 107 | /* Owned by this. */ |
| 108 | GString *name = nullptr; |
| 109 | |
| 110 | bool has_stream_hung_up = false; |
| 111 | }; |
| 112 | |
| 113 | struct lttng_live_metadata |
| 114 | { |
| 115 | bt_logging_level log_level = (bt_logging_level) 0; |
| 116 | bt_self_component *self_comp = nullptr; |
| 117 | |
| 118 | uint64_t stream_id = 0; |
| 119 | /* Weak reference. */ |
| 120 | struct ctf_metadata_decoder *decoder = nullptr; |
| 121 | }; |
| 122 | |
| 123 | enum lttng_live_metadata_stream_state |
| 124 | { |
| 125 | /* |
| 126 | * The metadata needs to be updated. This is either because we just |
| 127 | * created the trace and haven't asked yet, or the relay specifically |
| 128 | * told us that new metadata is available. |
| 129 | */ |
| 130 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED, |
| 131 | /* |
| 132 | * The metadata was updated and the relay has not told us we need to |
| 133 | * update it yet. |
| 134 | */ |
| 135 | LTTNG_LIVE_METADATA_STREAM_STATE_NOT_NEEDED, |
| 136 | /* |
| 137 | * The relay has closed this metadata stream. We set this in reaction |
| 138 | * to a LTTNG_VIEWER_METADATA_ERR reply to a LTTNG_VIEWER_GET_METADATA |
| 139 | * command to the relay. If this field is set, we have received all the |
| 140 | * metadata that we are ever going to get for that metadata stream. |
| 141 | */ |
| 142 | LTTNG_LIVE_METADATA_STREAM_STATE_CLOSED, |
| 143 | }; |
| 144 | |
| 145 | struct lttng_live_trace |
| 146 | { |
| 147 | bt_logging_level log_level = (bt_logging_level) 0; |
| 148 | bt_self_component *self_comp = nullptr; |
| 149 | |
| 150 | /* Back reference to session. */ |
| 151 | struct lttng_live_session *session = nullptr; |
| 152 | |
| 153 | /* ctf trace ID within the session. */ |
| 154 | uint64_t id = 0; |
| 155 | |
| 156 | /* Owned by this. */ |
| 157 | bt_trace *trace = nullptr; |
| 158 | |
| 159 | /* Weak reference. */ |
| 160 | bt_trace_class *trace_class = nullptr; |
| 161 | |
| 162 | struct lttng_live_metadata *metadata = nullptr; |
| 163 | |
| 164 | const bt_clock_class *clock_class = nullptr; |
| 165 | |
| 166 | /* Array of pointers to struct lttng_live_stream_iterator. */ |
| 167 | /* Owned by this. */ |
| 168 | GPtrArray *stream_iterators = nullptr; |
| 169 | |
| 170 | enum lttng_live_metadata_stream_state metadata_stream_state = |
| 171 | LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; |
| 172 | }; |
| 173 | |
| 174 | struct lttng_live_session |
| 175 | { |
| 176 | bt_logging_level log_level = (bt_logging_level) 0; |
| 177 | bt_self_component *self_comp = nullptr; |
| 178 | |
| 179 | /* Weak reference. */ |
| 180 | struct lttng_live_msg_iter *lttng_live_msg_iter = nullptr; |
| 181 | |
| 182 | /* Owned by this. */ |
| 183 | GString *hostname = nullptr; |
| 184 | |
| 185 | /* Owned by this. */ |
| 186 | GString *session_name = nullptr; |
| 187 | |
| 188 | uint64_t id = 0; |
| 189 | |
| 190 | /* Array of pointers to struct lttng_live_trace. */ |
| 191 | GPtrArray *traces = nullptr; |
| 192 | |
| 193 | bool attached = false; |
| 194 | bool new_streams_needed = false; |
| 195 | bool lazy_stream_msg_init = false; |
| 196 | bool closed = false; |
| 197 | }; |
| 198 | |
| 199 | enum session_not_found_action |
| 200 | { |
| 201 | SESSION_NOT_FOUND_ACTION_CONTINUE, |
| 202 | SESSION_NOT_FOUND_ACTION_FAIL, |
| 203 | SESSION_NOT_FOUND_ACTION_END, |
| 204 | }; |
| 205 | |
| 206 | /* |
| 207 | * A component instance is an iterator on a single session. |
| 208 | */ |
| 209 | struct lttng_live_component |
| 210 | { |
| 211 | bt_logging_level log_level = (bt_logging_level) 0; |
| 212 | |
| 213 | /* Weak reference. */ |
| 214 | bt_self_component *self_comp = nullptr; |
| 215 | |
| 216 | struct |
| 217 | { |
| 218 | GString *url = nullptr; |
| 219 | enum session_not_found_action sess_not_found_act = SESSION_NOT_FOUND_ACTION_CONTINUE; |
| 220 | } params; |
| 221 | |
| 222 | size_t max_query_size = 0; |
| 223 | |
| 224 | /* |
| 225 | * Keeps track of whether the downstream component already has a |
| 226 | * message iterator on this component. |
| 227 | */ |
| 228 | bool has_msg_iter = false; |
| 229 | }; |
| 230 | |
| 231 | struct lttng_live_msg_iter |
| 232 | { |
| 233 | bt_logging_level log_level = (bt_logging_level) 0; |
| 234 | bt_self_component *self_comp = nullptr; |
| 235 | |
| 236 | /* Weak reference. */ |
| 237 | struct lttng_live_component *lttng_live_comp = nullptr; |
| 238 | |
| 239 | /* Weak reference. */ |
| 240 | bt_self_message_iterator *self_msg_iter = nullptr; |
| 241 | |
| 242 | /* Owned by this. */ |
| 243 | struct live_viewer_connection *viewer_connection = nullptr; |
| 244 | |
| 245 | /* Array of pointers to struct lttng_live_session. */ |
| 246 | GPtrArray *sessions = nullptr; |
| 247 | |
| 248 | /* Number of live stream iterator this message iterator has.*/ |
| 249 | uint64_t active_stream_iter = 0; |
| 250 | |
| 251 | /* Timestamp in nanosecond of the last message sent downstream. */ |
| 252 | int64_t last_msg_ts_ns = 0; |
| 253 | |
| 254 | /* True if the iterator was interrupted. */ |
| 255 | bool was_interrupted = false; |
| 256 | }; |
| 257 | |
| 258 | enum lttng_live_iterator_status |
| 259 | { |
| 260 | /** Iterator state has progressed. Continue iteration immediately. */ |
| 261 | LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3, |
| 262 | /** No message available for now. Try again later. */ |
| 263 | LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2, |
| 264 | /** No more CTF_LTTNG_LIVEs to be delivered. */ |
| 265 | LTTNG_LIVE_ITERATOR_STATUS_END = 1, |
| 266 | /** No error, okay. */ |
| 267 | LTTNG_LIVE_ITERATOR_STATUS_OK = 0, |
| 268 | /** Invalid arguments. */ |
| 269 | LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1, |
| 270 | /** General error. */ |
| 271 | LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2, |
| 272 | /** Out of memory. */ |
| 273 | LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3, |
| 274 | /** Unsupported iterator feature. */ |
| 275 | LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4, |
| 276 | }; |
| 277 | |
| 278 | bt_component_class_initialize_method_status |
| 279 | lttng_live_component_init(bt_self_component_source *self_comp, |
| 280 | bt_self_component_source_configuration *config, const bt_value *params, |
| 281 | void *init_method_data); |
| 282 | |
| 283 | bt_component_class_query_method_status lttng_live_query(bt_self_component_class_source *comp_class, |
| 284 | bt_private_query_executor *priv_query_exec, |
| 285 | const char *object, const bt_value *params, |
| 286 | void *method_data, const bt_value **result); |
| 287 | |
| 288 | void lttng_live_component_finalize(bt_self_component_source *component); |
| 289 | |
| 290 | bt_message_iterator_class_next_method_status |
| 291 | lttng_live_msg_iter_next(bt_self_message_iterator *iterator, bt_message_array_const msgs, |
| 292 | uint64_t capacity, uint64_t *count); |
| 293 | |
| 294 | bt_message_iterator_class_initialize_method_status |
| 295 | lttng_live_msg_iter_init(bt_self_message_iterator *self_msg_it, |
| 296 | bt_self_message_iterator_configuration *config, |
| 297 | bt_self_component_port_output *self_port); |
| 298 | |
| 299 | void lttng_live_msg_iter_finalize(bt_self_message_iterator *it); |
| 300 | |
| 301 | enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_session *session, |
| 302 | bt_self_message_iterator *self_msg_iter); |
| 303 | |
| 304 | enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_session *session); |
| 305 | |
| 306 | enum lttng_live_iterator_status |
| 307 | lttng_live_session_get_new_streams(struct lttng_live_session *session, |
| 308 | bt_self_message_iterator *self_msg_iter); |
| 309 | |
| 310 | struct lttng_live_trace * |
| 311 | lttng_live_session_borrow_or_create_trace_by_id(struct lttng_live_session *session, |
| 312 | uint64_t trace_id); |
| 313 | |
| 314 | int lttng_live_add_session(struct lttng_live_msg_iter *lttng_live_msg_iter, uint64_t session_id, |
| 315 | const char *hostname, const char *session_name); |
| 316 | |
| 317 | /* |
| 318 | * lttng_live_get_one_metadata_packet() asks the Relay Daemon for new metadata. |
| 319 | * If new metadata is received, the function writes it to the provided file |
| 320 | * handle and updates the reply_len output parameter. This function should be |
| 321 | * called in loop until _END status is received to ensure all metadata is |
| 322 | * written to the file. |
| 323 | */ |
| 324 | enum lttng_live_get_one_metadata_status |
| 325 | lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, size_t *reply_len); |
| 326 | |
| 327 | enum lttng_live_iterator_status |
| 328 | lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter, |
| 329 | struct lttng_live_stream_iterator *stream, struct packet_index *index); |
| 330 | |
| 331 | enum ctf_msg_iter_medium_status |
| 332 | lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter, |
| 333 | struct lttng_live_stream_iterator *stream, uint8_t *buf, |
| 334 | uint64_t offset, uint64_t req_len, uint64_t *recv_len); |
| 335 | |
| 336 | bool lttng_live_graph_is_canceled(struct lttng_live_msg_iter *msg_iter); |
| 337 | |
| 338 | void lttng_live_stream_iterator_set_state(struct lttng_live_stream_iterator *stream_iter, |
| 339 | enum lttng_live_stream_state new_state); |
| 340 | |
| 341 | #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_H */ |