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