Use graph facilities in writer component class
[babeltrace.git] / plugins / writer / 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
PP
30#include <babeltrace/plugin/plugin-dev.h>
31#include <babeltrace/component/component.h>
d71dcf2c 32#include <babeltrace/component/component-sink.h>
c70ed359
JG
33#include <babeltrace/component/component-port.h>
34#include <babeltrace/component/component-connection.h>
33b34c43
PP
35#include <babeltrace/component/notification/notification.h>
36#include <babeltrace/component/notification/iterator.h>
37#include <babeltrace/component/notification/event.h>
38#include <babeltrace/component/notification/packet.h>
7d61fa8e 39#include <plugins-common.h>
bc506aa5
JD
40#include <stdio.h>
41#include <stdbool.h>
42#include <glib.h>
43#include "writer.h"
c70ed359 44#include <assert.h>
bc506aa5
JD
45
46static
47void destroy_writer_component_data(struct writer_component *writer_component)
48{
c70ed359 49 bt_put(writer_component->input_iterator);
bc506aa5
JD
50 g_hash_table_destroy(writer_component->stream_map);
51 g_hash_table_destroy(writer_component->stream_class_map);
52 g_hash_table_destroy(writer_component->trace_map);
9057f037
JD
53 g_string_free(writer_component->base_path, true);
54 g_string_free(writer_component->trace_name_base, true);
bc506aa5
JD
55}
56
57static
58void destroy_writer_component(struct bt_component *component)
59{
60 struct writer_component *writer_component = (struct writer_component *)
61 bt_component_get_private_data(component);
62
63 destroy_writer_component_data(writer_component);
64 g_free(writer_component);
65}
66
67static
68void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class)
69{
70 BT_PUT(writer_stream_class);
71 g_free(writer_stream_class);
72}
73
74static
75void unref_stream(struct bt_ctf_stream_class *writer_stream)
76{
77 BT_PUT(writer_stream);
78 g_free(writer_stream);
79}
80
81static
82void unref_trace(struct bt_ctf_writer *writer)
83{
84 BT_PUT(writer);
85 g_free(writer);
86}
87
88static
89struct writer_component *create_writer_component(void)
90{
91 struct writer_component *writer_component;
92
93 writer_component = g_new0(struct writer_component, 1);
94 if (!writer_component) {
95 goto end;
96 }
97
98 writer_component->err = stderr;
99 writer_component->trace_id = 0;
9057f037
JD
100 writer_component->trace_name_base = g_string_new("trace");
101 if (!writer_component->trace_name_base) {
102 g_free(writer_component);
103 writer_component = NULL;
104 goto end;
105 }
bc506aa5
JD
106
107 /*
108 * Reader to writer corresponding structures.
109 */
110 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
111 g_direct_equal, NULL, (GDestroyNotify) unref_trace);
112 writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash,
113 g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
114 writer_component->stream_map = g_hash_table_new_full(g_direct_hash,
115 g_direct_equal, NULL, (GDestroyNotify) unref_stream);
116
117end:
118 return writer_component;
119}
120
121static
122enum bt_component_status handle_notification(
123 struct writer_component *writer_component,
124 struct bt_notification *notification)
125{
126 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
127
128 if (!writer_component) {
129 ret = BT_COMPONENT_STATUS_ERROR;
130 goto end;
131 }
132
133 switch (bt_notification_get_type(notification)) {
ea0e619e 134 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
bc506aa5
JD
135 {
136 struct bt_ctf_packet *packet =
ea0e619e 137 bt_notification_packet_begin_get_packet(notification);
bc506aa5
JD
138
139 if (!packet) {
140 ret = BT_COMPONENT_STATUS_ERROR;
141 goto end;
142 }
143
144 ret = writer_new_packet(writer_component, packet);
145 bt_put(packet);
146 break;
147 }
148 case BT_NOTIFICATION_TYPE_PACKET_END:
149 {
150 struct bt_ctf_packet *packet =
ea0e619e 151 bt_notification_packet_end_get_packet(notification);
bc506aa5
JD
152
153 if (!packet) {
154 ret = BT_COMPONENT_STATUS_ERROR;
155 goto end;
156 }
157 ret = writer_close_packet(writer_component, packet);
158 bt_put(packet);
159 break;
160 }
161 case BT_NOTIFICATION_TYPE_EVENT:
162 {
163 struct bt_ctf_event *event = bt_notification_event_get_event(
164 notification);
165
166 if (!event) {
167 ret = BT_COMPONENT_STATUS_ERROR;
168 goto end;
169 }
170 ret = BT_COMPONENT_STATUS_OK;
171 ret = writer_output_event(writer_component, event);
172 bt_put(event);
173 if (ret != BT_COMPONENT_STATUS_OK) {
174 goto end;
175 }
176 break;
177 }
178 case BT_NOTIFICATION_TYPE_STREAM_END:
179 break;
180 default:
181 puts("Unhandled notification type");
182 }
183end:
184 return ret;
185}
186
c70ed359
JG
187static
188enum bt_component_status writer_component_new_connection(
189 struct bt_port *own_port, struct bt_connection *connection)
190{
191 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
192 struct bt_component *component;
193 struct writer_component *writer;
194
195 component = bt_port_get_component(own_port);
196 assert(component);
197 writer = bt_component_get_private_data(component);
198 assert(writer);
199 assert(!writer->input_iterator);
200 writer->input_iterator = bt_connection_create_notification_iterator(
201 connection);
202
203 if (!writer->input_iterator) {
204 ret = BT_COMPONENT_STATUS_ERROR;
205 }
206 bt_put(component);
207 return ret;
208}
209
bc506aa5
JD
210static
211enum bt_component_status run(struct bt_component *component)
212{
213 enum bt_component_status ret;
214 struct bt_notification *notification = NULL;
215 struct bt_notification_iterator *it;
216 struct writer_component *writer_component =
217 bt_component_get_private_data(component);
218
c70ed359
JG
219 it = writer_component->input_iterator;
220 assert(it);
bc506aa5 221
bc506aa5
JD
222 notification = bt_notification_iterator_get_notification(it);
223 if (!notification) {
224 ret = BT_COMPONENT_STATUS_ERROR;
225 goto end;
226 }
227
996a07a1
JG
228 ret = bt_notification_iterator_next(it);
229 if (ret != BT_COMPONENT_STATUS_OK) {
230 goto end;
231 }
232
bc506aa5
JD
233 ret = handle_notification(writer_component, notification);
234end:
235 bt_put(it);
236 bt_put(notification);
237 return ret;
238}
239
240static
241enum bt_component_status writer_component_init(
6358c163 242 struct bt_component *component, struct bt_value *params,
7d61fa8e 243 UNUSED_VAR void *init_method_data)
bc506aa5
JD
244{
245 enum bt_component_status ret;
246 enum bt_value_status value_ret;
247 struct writer_component *writer_component = create_writer_component();
248 struct bt_value *value = NULL;
249 const char *path;
250
251 if (!writer_component) {
252 ret = BT_COMPONENT_STATUS_NOMEM;
253 goto end;
254 }
255
256 value = bt_value_map_get(params, "path");
257 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
258 fprintf(writer_component->err,
259 "[error] output path parameter required\n");
260 ret = BT_COMPONENT_STATUS_INVALID;
261 goto error;
262 }
263
264 value_ret = bt_value_string_get(value, &path);
265 if (value_ret != BT_VALUE_STATUS_OK) {
266 ret = BT_COMPONENT_STATUS_INVALID;
267 goto error;
268 }
269
9057f037
JD
270 writer_component->base_path = g_string_new(path);
271 if (!writer_component) {
272 ret = BT_COMPONENT_STATUS_ERROR;
273 goto error;
274 }
bc506aa5 275
bc506aa5
JD
276 ret = bt_component_set_private_data(component, writer_component);
277 if (ret != BT_COMPONENT_STATUS_OK) {
278 goto error;
279 }
280
bc506aa5
JD
281end:
282 return ret;
283error:
284 destroy_writer_component_data(writer_component);
56fa591a 285 g_free(writer_component);
bc506aa5
JD
286 return ret;
287}
288
289/* Initialize plug-in entry points. */
6ba0b073 290BT_PLUGIN(writer);
bc506aa5
JD
291BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in.");
292BT_PLUGIN_AUTHOR("Jérémie Galarneau");
293BT_PLUGIN_LICENSE("MIT");
d3e4dcd8
PP
294BT_PLUGIN_SINK_COMPONENT_CLASS(writer, run);
295BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer, writer_component_init);
c70ed359
JG
296BT_PLUGIN_SINK_COMPONENT_CLASS_NEW_CONNECTION_METHOD(writer,
297 writer_component_new_connection);
d3e4dcd8
PP
298BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(writer, destroy_writer_component);
299BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer, "Formats CTF-IR to CTF.");
This page took 0.035202 seconds and 4 git commands to generate.