| 1 | /* |
| 2 | * writer.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer Output Plugin |
| 5 | * |
| 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/ctf-ir/packet.h> |
| 30 | #include <babeltrace/plugin/plugin-dev.h> |
| 31 | #include <babeltrace/graph/component.h> |
| 32 | #include <babeltrace/graph/private-component.h> |
| 33 | #include <babeltrace/graph/component-sink.h> |
| 34 | #include <babeltrace/graph/private-component-sink.h> |
| 35 | #include <babeltrace/graph/private-port.h> |
| 36 | #include <babeltrace/graph/private-connection.h> |
| 37 | #include <babeltrace/graph/notification.h> |
| 38 | #include <babeltrace/graph/notification-iterator.h> |
| 39 | #include <babeltrace/graph/notification-event.h> |
| 40 | #include <babeltrace/graph/notification-packet.h> |
| 41 | #include <babeltrace/graph/notification-stream.h> |
| 42 | #include <plugins-common.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdbool.h> |
| 45 | #include <glib.h> |
| 46 | #include "writer.h" |
| 47 | #include <assert.h> |
| 48 | |
| 49 | gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data) |
| 50 | { |
| 51 | struct fs_writer *fs_writer = value; |
| 52 | struct writer_component *writer_component = user_data; |
| 53 | |
| 54 | fs_writer->trace_static = 1; |
| 55 | writer_close(writer_component, fs_writer); |
| 56 | |
| 57 | return TRUE; |
| 58 | } |
| 59 | |
| 60 | static |
| 61 | void destroy_writer_component_data(struct writer_component *writer_component) |
| 62 | { |
| 63 | bt_put(writer_component->input_iterator); |
| 64 | |
| 65 | g_hash_table_foreach_remove(writer_component->trace_map, |
| 66 | empty_trace_map, writer_component); |
| 67 | g_hash_table_destroy(writer_component->trace_map); |
| 68 | |
| 69 | g_string_free(writer_component->base_path, true); |
| 70 | g_string_free(writer_component->trace_name_base, true); |
| 71 | } |
| 72 | |
| 73 | BT_HIDDEN |
| 74 | void writer_component_finalize(struct bt_private_component *component) |
| 75 | { |
| 76 | struct writer_component *writer_component = (struct writer_component *) |
| 77 | bt_private_component_get_user_data(component); |
| 78 | |
| 79 | destroy_writer_component_data(writer_component); |
| 80 | g_free(writer_component); |
| 81 | } |
| 82 | |
| 83 | static |
| 84 | void free_fs_writer(struct fs_writer *fs_writer) |
| 85 | { |
| 86 | bt_put(fs_writer->writer); |
| 87 | g_free(fs_writer); |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | struct writer_component *create_writer_component(void) |
| 92 | { |
| 93 | struct writer_component *writer_component; |
| 94 | |
| 95 | writer_component = g_new0(struct writer_component, 1); |
| 96 | if (!writer_component) { |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | writer_component->err = stderr; |
| 101 | writer_component->trace_id = 0; |
| 102 | writer_component->trace_name_base = g_string_new("trace"); |
| 103 | if (!writer_component->trace_name_base) { |
| 104 | g_free(writer_component); |
| 105 | writer_component = NULL; |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Reader to writer corresponding structures. |
| 111 | */ |
| 112 | writer_component->trace_map = g_hash_table_new_full(g_direct_hash, |
| 113 | g_direct_equal, NULL, (GDestroyNotify) free_fs_writer); |
| 114 | |
| 115 | end: |
| 116 | return writer_component; |
| 117 | } |
| 118 | |
| 119 | static |
| 120 | enum bt_component_status handle_notification( |
| 121 | struct writer_component *writer_component, |
| 122 | struct bt_notification *notification) |
| 123 | { |
| 124 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 125 | |
| 126 | if (!writer_component) { |
| 127 | ret = BT_COMPONENT_STATUS_ERROR; |
| 128 | goto end; |
| 129 | } |
| 130 | |
| 131 | switch (bt_notification_get_type(notification)) { |
| 132 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 133 | { |
| 134 | struct bt_ctf_packet *packet = |
| 135 | bt_notification_packet_begin_get_packet(notification); |
| 136 | |
| 137 | if (!packet) { |
| 138 | ret = BT_COMPONENT_STATUS_ERROR; |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | ret = writer_new_packet(writer_component, packet); |
| 143 | bt_put(packet); |
| 144 | break; |
| 145 | } |
| 146 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 147 | { |
| 148 | struct bt_ctf_packet *packet = |
| 149 | bt_notification_packet_end_get_packet(notification); |
| 150 | |
| 151 | if (!packet) { |
| 152 | ret = BT_COMPONENT_STATUS_ERROR; |
| 153 | goto end; |
| 154 | } |
| 155 | ret = writer_close_packet(writer_component, packet); |
| 156 | bt_put(packet); |
| 157 | break; |
| 158 | } |
| 159 | case BT_NOTIFICATION_TYPE_EVENT: |
| 160 | { |
| 161 | struct bt_ctf_event *event = bt_notification_event_get_event( |
| 162 | notification); |
| 163 | |
| 164 | if (!event) { |
| 165 | ret = BT_COMPONENT_STATUS_ERROR; |
| 166 | goto end; |
| 167 | } |
| 168 | ret = writer_output_event(writer_component, event); |
| 169 | bt_put(event); |
| 170 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 171 | goto end; |
| 172 | } |
| 173 | break; |
| 174 | } |
| 175 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: |
| 176 | { |
| 177 | struct bt_ctf_stream *stream = |
| 178 | bt_notification_stream_begin_get_stream(notification); |
| 179 | |
| 180 | if (!stream) { |
| 181 | ret = BT_COMPONENT_STATUS_ERROR; |
| 182 | goto end; |
| 183 | } |
| 184 | ret = writer_stream_begin(writer_component, stream); |
| 185 | bt_put(stream); |
| 186 | break; |
| 187 | } |
| 188 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 189 | { |
| 190 | struct bt_ctf_stream *stream = |
| 191 | bt_notification_stream_end_get_stream(notification); |
| 192 | |
| 193 | if (!stream) { |
| 194 | ret = BT_COMPONENT_STATUS_ERROR; |
| 195 | goto end; |
| 196 | } |
| 197 | ret = writer_stream_end(writer_component, stream); |
| 198 | bt_put(stream); |
| 199 | break; |
| 200 | } |
| 201 | default: |
| 202 | puts("Unhandled notification type"); |
| 203 | } |
| 204 | end: |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | BT_HIDDEN |
| 209 | void writer_component_port_connected( |
| 210 | struct bt_private_component *component, |
| 211 | struct bt_private_port *self_port, |
| 212 | struct bt_port *other_port) |
| 213 | { |
| 214 | struct bt_private_connection *connection; |
| 215 | struct writer_component *writer; |
| 216 | static const enum bt_notification_type notif_types[] = { |
| 217 | BT_NOTIFICATION_TYPE_EVENT, |
| 218 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, |
| 219 | BT_NOTIFICATION_TYPE_PACKET_END, |
| 220 | BT_NOTIFICATION_TYPE_STREAM_BEGIN, |
| 221 | BT_NOTIFICATION_TYPE_STREAM_END, |
| 222 | BT_NOTIFICATION_TYPE_SENTINEL, |
| 223 | }; |
| 224 | |
| 225 | writer = bt_private_component_get_user_data(component); |
| 226 | assert(writer); |
| 227 | assert(!writer->input_iterator); |
| 228 | connection = bt_private_port_get_private_connection(self_port); |
| 229 | assert(connection); |
| 230 | writer->input_iterator = |
| 231 | bt_private_connection_create_notification_iterator(connection, |
| 232 | notif_types); |
| 233 | |
| 234 | if (!writer->input_iterator) { |
| 235 | writer->error = true; |
| 236 | } |
| 237 | |
| 238 | bt_put(connection); |
| 239 | } |
| 240 | |
| 241 | BT_HIDDEN |
| 242 | enum bt_component_status writer_run(struct bt_private_component *component) |
| 243 | { |
| 244 | enum bt_component_status ret; |
| 245 | struct bt_notification *notification = NULL; |
| 246 | struct bt_notification_iterator *it; |
| 247 | struct writer_component *writer_component = |
| 248 | bt_private_component_get_user_data(component); |
| 249 | enum bt_notification_iterator_status it_ret; |
| 250 | |
| 251 | if (unlikely(writer_component->error)) { |
| 252 | ret = BT_COMPONENT_STATUS_ERROR; |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | it = writer_component->input_iterator; |
| 257 | assert(it); |
| 258 | it_ret = bt_notification_iterator_next(it); |
| 259 | |
| 260 | switch (it_ret) { |
| 261 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 262 | ret = BT_COMPONENT_STATUS_END; |
| 263 | BT_PUT(writer_component->input_iterator); |
| 264 | goto end; |
| 265 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 266 | ret = BT_COMPONENT_STATUS_AGAIN; |
| 267 | goto end; |
| 268 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
| 269 | break; |
| 270 | default: |
| 271 | ret = BT_COMPONENT_STATUS_ERROR; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | notification = bt_notification_iterator_get_notification(it); |
| 276 | assert(notification); |
| 277 | ret = handle_notification(writer_component, notification); |
| 278 | end: |
| 279 | bt_put(notification); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | BT_HIDDEN |
| 284 | enum bt_component_status writer_component_init( |
| 285 | struct bt_private_component *component, struct bt_value *params, |
| 286 | UNUSED_VAR void *init_method_data) |
| 287 | { |
| 288 | enum bt_component_status ret; |
| 289 | enum bt_value_status value_ret; |
| 290 | struct writer_component *writer_component = create_writer_component(); |
| 291 | struct bt_value *value = NULL; |
| 292 | const char *path; |
| 293 | void *priv_port; |
| 294 | |
| 295 | if (!writer_component) { |
| 296 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 297 | goto end; |
| 298 | } |
| 299 | |
| 300 | priv_port = bt_private_component_sink_add_input_private_port(component, |
| 301 | "in", NULL); |
| 302 | if (!priv_port) { |
| 303 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 304 | goto end; |
| 305 | } |
| 306 | |
| 307 | bt_put(priv_port); |
| 308 | |
| 309 | value = bt_value_map_get(params, "path"); |
| 310 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { |
| 311 | fprintf(writer_component->err, |
| 312 | "[error] output path parameter required\n"); |
| 313 | ret = BT_COMPONENT_STATUS_INVALID; |
| 314 | goto error; |
| 315 | } |
| 316 | |
| 317 | value_ret = bt_value_string_get(value, &path); |
| 318 | if (value_ret != BT_VALUE_STATUS_OK) { |
| 319 | ret = BT_COMPONENT_STATUS_INVALID; |
| 320 | goto error; |
| 321 | } |
| 322 | bt_put(value); |
| 323 | |
| 324 | writer_component->base_path = g_string_new(path); |
| 325 | if (!writer_component) { |
| 326 | ret = BT_COMPONENT_STATUS_ERROR; |
| 327 | goto error; |
| 328 | } |
| 329 | |
| 330 | ret = bt_private_component_set_user_data(component, writer_component); |
| 331 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 332 | goto error; |
| 333 | } |
| 334 | |
| 335 | end: |
| 336 | return ret; |
| 337 | error: |
| 338 | destroy_writer_component_data(writer_component); |
| 339 | g_free(writer_component); |
| 340 | return ret; |
| 341 | } |