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