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