| 1 | #ifndef BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_INTERNAL_H |
| 2 | #define BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * BabelTrace - LTTng-live client Component |
| 6 | * |
| 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 | #include <stdbool.h> |
| 32 | |
| 33 | #include <babeltrace/babeltrace-internal.h> |
| 34 | #include <babeltrace/graph/component.h> |
| 35 | #include <babeltrace/graph/notification-iterator.h> |
| 36 | #include <babeltrace/graph/clock-class-priority-map.h> |
| 37 | #include <babeltrace/types.h> |
| 38 | #include "viewer-connection.h" |
| 39 | |
| 40 | //TODO: this should not be used by plugins. Should copy code into plugin |
| 41 | //instead. |
| 42 | #include "babeltrace/object-internal.h" |
| 43 | #include "babeltrace/list-internal.h" |
| 44 | #include "../common/metadata/decoder.h" |
| 45 | |
| 46 | #define STREAM_NAME_PREFIX "stream-" |
| 47 | /* Account for u64 max string length. */ |
| 48 | #define U64_STR_MAX_LEN 20 |
| 49 | #define STREAM_NAME_MAX_LEN (sizeof(STREAM_NAME_PREFIX) + U64_STR_MAX_LEN) |
| 50 | |
| 51 | struct lttng_live_component; |
| 52 | struct lttng_live_session; |
| 53 | |
| 54 | enum lttng_live_stream_state { |
| 55 | LTTNG_LIVE_STREAM_ACTIVE_NO_DATA, |
| 56 | LTTNG_LIVE_STREAM_QUIESCENT_NO_DATA, |
| 57 | LTTNG_LIVE_STREAM_QUIESCENT, |
| 58 | LTTNG_LIVE_STREAM_ACTIVE_DATA, |
| 59 | LTTNG_LIVE_STREAM_EOF, |
| 60 | }; |
| 61 | |
| 62 | enum live_stream_type { |
| 63 | LIVE_STREAM_TYPE_NO_STREAM, |
| 64 | LIVE_STREAM_TYPE_STREAM, |
| 65 | }; |
| 66 | |
| 67 | struct lttng_live_stream_iterator_generic { |
| 68 | enum live_stream_type type; |
| 69 | }; |
| 70 | |
| 71 | /* Iterator over a live stream. */ |
| 72 | struct lttng_live_stream_iterator { |
| 73 | struct lttng_live_stream_iterator_generic p; |
| 74 | |
| 75 | struct bt_ctf_stream *stream; |
| 76 | struct lttng_live_trace *trace; |
| 77 | struct bt_private_port *port; /* weak ref. */ |
| 78 | |
| 79 | /* Node of stream list within the trace. */ |
| 80 | struct bt_list_head node; |
| 81 | |
| 82 | /* |
| 83 | * Since only a single iterator per viewer connection, we have |
| 84 | * only a single notification iterator per stream. |
| 85 | */ |
| 86 | struct bt_ctf_notif_iter *notif_iter; |
| 87 | |
| 88 | uint64_t viewer_stream_id; |
| 89 | |
| 90 | uint64_t ctf_stream_class_id; |
| 91 | uint64_t base_offset; /* base offset in current index. */ |
| 92 | uint64_t len; /* len to read in current index. */ |
| 93 | uint64_t offset; /* offset in current index. */ |
| 94 | |
| 95 | int64_t last_returned_inactivity_timestamp; |
| 96 | int64_t current_inactivity_timestamp; |
| 97 | |
| 98 | enum lttng_live_stream_state state; |
| 99 | |
| 100 | uint64_t current_packet_end_timestamp; |
| 101 | struct bt_notification *packet_end_notif_queue; |
| 102 | |
| 103 | uint8_t *buf; |
| 104 | size_t buflen; |
| 105 | |
| 106 | char name[STREAM_NAME_MAX_LEN]; |
| 107 | }; |
| 108 | |
| 109 | struct lttng_live_no_stream_iterator { |
| 110 | struct lttng_live_stream_iterator_generic p; |
| 111 | |
| 112 | struct lttng_live_component *lttng_live; |
| 113 | struct bt_private_port *port; /* weak ref. */ |
| 114 | }; |
| 115 | |
| 116 | struct lttng_live_component_options { |
| 117 | bool opt_dummy : 1; |
| 118 | }; |
| 119 | |
| 120 | struct lttng_live_metadata { |
| 121 | struct lttng_live_trace *trace; |
| 122 | uint64_t stream_id; |
| 123 | uint8_t uuid[16]; |
| 124 | bool is_uuid_set; |
| 125 | int bo; |
| 126 | char *text; |
| 127 | |
| 128 | struct ctf_metadata_decoder *decoder; |
| 129 | |
| 130 | bool closed; |
| 131 | }; |
| 132 | |
| 133 | struct lttng_live_trace { |
| 134 | struct bt_object obj; |
| 135 | |
| 136 | /* Node of trace list within the session. */ |
| 137 | struct bt_list_head node; |
| 138 | |
| 139 | /* Back reference to session. */ |
| 140 | struct lttng_live_session *session; |
| 141 | |
| 142 | uint64_t id; /* ctf trace ID within the session. */ |
| 143 | |
| 144 | struct bt_ctf_trace *trace; |
| 145 | |
| 146 | struct lttng_live_metadata *metadata; |
| 147 | struct bt_clock_class_priority_map *cc_prio_map; |
| 148 | |
| 149 | /* List of struct lttng_live_stream_iterator */ |
| 150 | struct bt_list_head streams; |
| 151 | |
| 152 | bool new_metadata_needed; |
| 153 | }; |
| 154 | |
| 155 | struct lttng_live_session { |
| 156 | /* Node of session list within the component. */ |
| 157 | struct bt_list_head node; |
| 158 | |
| 159 | struct lttng_live_component *lttng_live; |
| 160 | |
| 161 | GString *hostname; |
| 162 | GString *session_name; |
| 163 | |
| 164 | uint64_t id; |
| 165 | |
| 166 | /* List of struct lttng_live_trace */ |
| 167 | struct bt_list_head traces; |
| 168 | |
| 169 | bool attached; |
| 170 | bool new_streams_needed; |
| 171 | bool lazy_stream_notif_init; |
| 172 | bool closed; |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * A component instance is an iterator on a single session. |
| 177 | */ |
| 178 | struct lttng_live_component { |
| 179 | struct bt_object obj; |
| 180 | struct bt_private_component *private_component; /* weak */ |
| 181 | struct bt_live_viewer_connection *viewer_connection; |
| 182 | |
| 183 | /* List of struct lttng_live_session */ |
| 184 | struct bt_list_head sessions; |
| 185 | |
| 186 | GString *url; |
| 187 | size_t max_query_size; |
| 188 | struct lttng_live_component_options options; |
| 189 | |
| 190 | struct bt_private_port *no_stream_port; /* weak */ |
| 191 | struct lttng_live_no_stream_iterator *no_stream_iter; |
| 192 | |
| 193 | struct bt_component *downstream_component; |
| 194 | }; |
| 195 | |
| 196 | enum bt_ctf_lttng_live_iterator_status { |
| 197 | /** Iterator state has progressed. Continue iteration immediately. */ |
| 198 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_CONTINUE = 3, |
| 199 | /** No notification available for now. Try again later. */ |
| 200 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_AGAIN = 2, |
| 201 | /** No more CTF_LTTNG_LIVEs to be delivered. */ |
| 202 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_END = 1, |
| 203 | /** No error, okay. */ |
| 204 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK = 0, |
| 205 | /** Invalid arguments. */ |
| 206 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_INVAL = -1, |
| 207 | /** General error. */ |
| 208 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR = -2, |
| 209 | /** Out of memory. */ |
| 210 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_NOMEM = -3, |
| 211 | /** Unsupported iterator feature. */ |
| 212 | BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_UNSUPPORTED = -4, |
| 213 | }; |
| 214 | |
| 215 | enum bt_component_status lttng_live_component_init(struct bt_private_component *source, |
| 216 | struct bt_value *params, void *init_method_data); |
| 217 | |
| 218 | struct bt_component_class_query_return lttng_live_query( |
| 219 | struct bt_component_class *comp_class, |
| 220 | struct bt_query_executor *query_exec, |
| 221 | const char *object, struct bt_value *params); |
| 222 | |
| 223 | void lttng_live_component_finalize(struct bt_private_component *component); |
| 224 | |
| 225 | struct bt_notification_iterator_next_return lttng_live_iterator_next( |
| 226 | struct bt_private_notification_iterator *iterator); |
| 227 | |
| 228 | enum bt_component_status lttng_live_accept_port_connection( |
| 229 | struct bt_private_component *private_component, |
| 230 | struct bt_private_port *self_private_port, |
| 231 | struct bt_port *other_port); |
| 232 | |
| 233 | enum bt_notification_iterator_status lttng_live_iterator_init( |
| 234 | struct bt_private_notification_iterator *it, |
| 235 | struct bt_private_port *port); |
| 236 | |
| 237 | void lttng_live_iterator_finalize(struct bt_private_notification_iterator *it); |
| 238 | |
| 239 | int lttng_live_create_viewer_session(struct lttng_live_component *lttng_live); |
| 240 | int lttng_live_attach_session(struct lttng_live_session *session); |
| 241 | int lttng_live_detach_session(struct lttng_live_session *session); |
| 242 | enum bt_ctf_lttng_live_iterator_status lttng_live_get_new_streams( |
| 243 | struct lttng_live_session *session); |
| 244 | |
| 245 | int lttng_live_add_session(struct lttng_live_component *lttng_live, |
| 246 | uint64_t session_id, |
| 247 | const char *hostname, |
| 248 | const char *session_name); |
| 249 | |
| 250 | ssize_t lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, |
| 251 | FILE *fp); |
| 252 | enum bt_ctf_lttng_live_iterator_status lttng_live_get_next_index( |
| 253 | struct lttng_live_component *lttng_live, |
| 254 | struct lttng_live_stream_iterator *stream, |
| 255 | struct packet_index *index); |
| 256 | enum bt_ctf_notif_iter_medium_status lttng_live_get_stream_bytes( |
| 257 | struct lttng_live_component *lttng_live, |
| 258 | struct lttng_live_stream_iterator *stream, uint8_t *buf, uint64_t offset, |
| 259 | uint64_t req_len, uint64_t *recv_len); |
| 260 | |
| 261 | int lttng_live_add_port(struct lttng_live_component *lttng_live, |
| 262 | struct lttng_live_stream_iterator *stream_iter); |
| 263 | int lttng_live_remove_port(struct lttng_live_component *lttng_live, |
| 264 | struct bt_private_port *port); |
| 265 | |
| 266 | struct lttng_live_trace *lttng_live_ref_trace( |
| 267 | struct lttng_live_session *session, uint64_t trace_id); |
| 268 | void lttng_live_unref_trace(struct lttng_live_trace *trace); |
| 269 | void lttng_live_need_new_streams(struct lttng_live_component *lttng_live); |
| 270 | |
| 271 | bt_bool lttng_live_is_canceled(struct lttng_live_component *lttng_live); |
| 272 | |
| 273 | #endif /* BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_INTERNAL_H */ |