fs-sink: move stream and stream_class maps in fs_writer struct
[babeltrace.git] / plugins / ctf / fs-sink / writer.c
CommitLineData
bc506aa5
JD
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>
33b34c43 30#include <babeltrace/plugin/plugin-dev.h>
b2e0c907
PP
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>
f3168545 41#include <babeltrace/graph/notification-stream.h>
7d61fa8e 42#include <plugins-common.h>
bc506aa5
JD
43#include <stdio.h>
44#include <stdbool.h>
45#include <glib.h>
46#include "writer.h"
c70ed359 47#include <assert.h>
bc506aa5 48
a619fcb7 49gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
f3168545 50{
a619fcb7
JD
51 struct fs_writer *fs_writer = value;
52 struct writer_component *writer_component = user_data;
53
54 writer_close(writer_component, fs_writer);
55
f3168545
JD
56 return TRUE;
57}
58
bc506aa5
JD
59static
60void destroy_writer_component_data(struct writer_component *writer_component)
61{
c70ed359 62 bt_put(writer_component->input_iterator);
f3168545 63
f3168545 64 g_hash_table_foreach_remove(writer_component->trace_map,
a619fcb7 65 empty_trace_map, writer_component);
bc506aa5 66 g_hash_table_destroy(writer_component->trace_map);
f3168545 67
9057f037
JD
68 g_string_free(writer_component->base_path, true);
69 g_string_free(writer_component->trace_name_base, true);
bc506aa5
JD
70}
71
d8866baa
PP
72BT_HIDDEN
73void writer_component_finalize(struct bt_private_component *component)
bc506aa5
JD
74{
75 struct writer_component *writer_component = (struct writer_component *)
890882ef 76 bt_private_component_get_user_data(component);
bc506aa5
JD
77
78 destroy_writer_component_data(writer_component);
79 g_free(writer_component);
80}
81
bc506aa5 82static
f3168545 83void free_fs_writer(struct fs_writer *fs_writer)
bc506aa5 84{
f3168545
JD
85 bt_put(fs_writer->writer);
86 g_free(fs_writer);
bc506aa5
JD
87}
88
89static
90struct writer_component *create_writer_component(void)
91{
92 struct writer_component *writer_component;
93
94 writer_component = g_new0(struct writer_component, 1);
95 if (!writer_component) {
96 goto end;
97 }
98
99 writer_component->err = stderr;
100 writer_component->trace_id = 0;
9057f037
JD
101 writer_component->trace_name_base = g_string_new("trace");
102 if (!writer_component->trace_name_base) {
103 g_free(writer_component);
104 writer_component = NULL;
105 goto end;
106 }
bc506aa5
JD
107
108 /*
109 * Reader to writer corresponding structures.
110 */
111 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
f3168545 112 g_direct_equal, NULL, (GDestroyNotify) free_fs_writer);
bc506aa5
JD
113
114end:
115 return writer_component;
116}
117
118static
119enum bt_component_status handle_notification(
120 struct writer_component *writer_component,
121 struct bt_notification *notification)
122{
123 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
124
125 if (!writer_component) {
126 ret = BT_COMPONENT_STATUS_ERROR;
127 goto end;
128 }
129
130 switch (bt_notification_get_type(notification)) {
ea0e619e 131 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
bc506aa5
JD
132 {
133 struct bt_ctf_packet *packet =
ea0e619e 134 bt_notification_packet_begin_get_packet(notification);
bc506aa5
JD
135
136 if (!packet) {
137 ret = BT_COMPONENT_STATUS_ERROR;
138 goto end;
139 }
140
141 ret = writer_new_packet(writer_component, packet);
142 bt_put(packet);
143 break;
144 }
145 case BT_NOTIFICATION_TYPE_PACKET_END:
146 {
147 struct bt_ctf_packet *packet =
ea0e619e 148 bt_notification_packet_end_get_packet(notification);
bc506aa5
JD
149
150 if (!packet) {
151 ret = BT_COMPONENT_STATUS_ERROR;
152 goto end;
153 }
154 ret = writer_close_packet(writer_component, packet);
155 bt_put(packet);
156 break;
157 }
158 case BT_NOTIFICATION_TYPE_EVENT:
159 {
160 struct bt_ctf_event *event = bt_notification_event_get_event(
161 notification);
162
163 if (!event) {
164 ret = BT_COMPONENT_STATUS_ERROR;
165 goto end;
166 }
bc506aa5
JD
167 ret = writer_output_event(writer_component, event);
168 bt_put(event);
169 if (ret != BT_COMPONENT_STATUS_OK) {
170 goto end;
171 }
172 break;
173 }
f384901f
JD
174 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
175 {
176 struct bt_ctf_stream *stream =
177 bt_notification_stream_begin_get_stream(notification);
178
179 if (!stream) {
180 ret = BT_COMPONENT_STATUS_ERROR;
181 goto end;
182 }
183 ret = writer_stream_begin(writer_component, stream);
184 bt_put(stream);
185 break;
186 }
bc506aa5 187 case BT_NOTIFICATION_TYPE_STREAM_END:
f3168545
JD
188 {
189 struct bt_ctf_stream *stream =
190 bt_notification_stream_end_get_stream(notification);
191
192 if (!stream) {
193 ret = BT_COMPONENT_STATUS_ERROR;
194 goto end;
195 }
196 ret = writer_stream_end(writer_component, stream);
197 bt_put(stream);
bc506aa5 198 break;
f3168545 199 }
bc506aa5
JD
200 default:
201 puts("Unhandled notification type");
202 }
203end:
204 return ret;
205}
206
d8866baa 207BT_HIDDEN
0d8b4d8e 208void writer_component_port_connected(
890882ef 209 struct bt_private_component *component,
8f4799f7
PP
210 struct bt_private_port *self_port,
211 struct bt_port *other_port)
c70ed359 212{
890882ef 213 struct bt_private_connection *connection;
c70ed359 214 struct writer_component *writer;
93fc16a5 215 static const enum bt_notification_type notif_types[] = {
f3168545 216 BT_NOTIFICATION_TYPE_EVENT,
93fc16a5
JD
217 BT_NOTIFICATION_TYPE_PACKET_BEGIN,
218 BT_NOTIFICATION_TYPE_PACKET_END,
f384901f 219 BT_NOTIFICATION_TYPE_STREAM_BEGIN,
f3168545 220 BT_NOTIFICATION_TYPE_STREAM_END,
93fc16a5
JD
221 BT_NOTIFICATION_TYPE_SENTINEL,
222 };
c70ed359 223
890882ef 224 writer = bt_private_component_get_user_data(component);
c70ed359
JG
225 assert(writer);
226 assert(!writer->input_iterator);
890882ef 227 connection = bt_private_port_get_private_connection(self_port);
72b913fb 228 assert(connection);
fa054faf
PP
229 writer->input_iterator =
230 bt_private_connection_create_notification_iterator(connection,
93fc16a5 231 notif_types);
c70ed359
JG
232
233 if (!writer->input_iterator) {
0d8b4d8e 234 writer->error = true;
c70ed359 235 }
0d8b4d8e 236
72b913fb 237 bt_put(connection);
c70ed359
JG
238}
239
d8866baa
PP
240BT_HIDDEN
241enum bt_component_status writer_run(struct bt_private_component *component)
bc506aa5
JD
242{
243 enum bt_component_status ret;
244 struct bt_notification *notification = NULL;
245 struct bt_notification_iterator *it;
246 struct writer_component *writer_component =
890882ef 247 bt_private_component_get_user_data(component);
7cdc2bab 248 enum bt_notification_iterator_status it_ret;
bc506aa5 249
0d8b4d8e
PP
250 if (unlikely(writer_component->error)) {
251 ret = BT_COMPONENT_STATUS_ERROR;
252 goto end;
253 }
254
7cdc2bab
MD
255 it = writer_component->input_iterator;
256 assert(it);
257 it_ret = bt_notification_iterator_next(it);
bc506aa5 258
7cdc2bab
MD
259 switch (it_ret) {
260 case BT_NOTIFICATION_ITERATOR_STATUS_END:
261 ret = BT_COMPONENT_STATUS_END;
262 BT_PUT(writer_component->input_iterator);
263 goto end;
264 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
265 ret = BT_COMPONENT_STATUS_AGAIN;
266 goto end;
267 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
268 break;
269 default:
72b913fb 270 ret = BT_COMPONENT_STATUS_ERROR;
996a07a1
JG
271 goto end;
272 }
273
7cdc2bab
MD
274 notification = bt_notification_iterator_get_notification(it);
275 assert(notification);
bc506aa5
JD
276 ret = handle_notification(writer_component, notification);
277end:
bc506aa5
JD
278 bt_put(notification);
279 return ret;
280}
281
d8866baa 282BT_HIDDEN
bc506aa5 283enum bt_component_status writer_component_init(
890882ef 284 struct bt_private_component *component, struct bt_value *params,
7d61fa8e 285 UNUSED_VAR void *init_method_data)
bc506aa5
JD
286{
287 enum bt_component_status ret;
288 enum bt_value_status value_ret;
289 struct writer_component *writer_component = create_writer_component();
290 struct bt_value *value = NULL;
291 const char *path;
b9d103be 292 void *priv_port;
bc506aa5
JD
293
294 if (!writer_component) {
295 ret = BT_COMPONENT_STATUS_NOMEM;
296 goto end;
297 }
298
b9d103be
PP
299 priv_port = bt_private_component_sink_add_input_private_port(component,
300 "in", NULL);
301 if (!priv_port) {
302 ret = BT_COMPONENT_STATUS_NOMEM;
303 goto end;
304 }
305
306 bt_put(priv_port);
307
bc506aa5
JD
308 value = bt_value_map_get(params, "path");
309 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
310 fprintf(writer_component->err,
311 "[error] output path parameter required\n");
312 ret = BT_COMPONENT_STATUS_INVALID;
313 goto error;
314 }
315
316 value_ret = bt_value_string_get(value, &path);
317 if (value_ret != BT_VALUE_STATUS_OK) {
318 ret = BT_COMPONENT_STATUS_INVALID;
319 goto error;
320 }
d00b90bf 321 bt_put(value);
bc506aa5 322
9057f037
JD
323 writer_component->base_path = g_string_new(path);
324 if (!writer_component) {
325 ret = BT_COMPONENT_STATUS_ERROR;
326 goto error;
327 }
bc506aa5 328
890882ef 329 ret = bt_private_component_set_user_data(component, writer_component);
bc506aa5
JD
330 if (ret != BT_COMPONENT_STATUS_OK) {
331 goto error;
332 }
333
bc506aa5
JD
334end:
335 return ret;
336error:
337 destroy_writer_component_data(writer_component);
56fa591a 338 g_free(writer_component);
bc506aa5
JD
339 return ret;
340}
This page took 0.043027 seconds and 4 git commands to generate.