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