bt2: split clock value module from clock class module
[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
449f71e3
JD
29#define BT_LOG_TAG "PLUGIN-CTF-FS-SINK-WRITER"
30#include "logging.h"
31
bc506aa5 32#include <babeltrace/ctf-ir/packet.h>
33b34c43 33#include <babeltrace/plugin/plugin-dev.h>
73d5c1ad 34#include <babeltrace/graph/connection.h>
b2e0c907
PP
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>
f3168545 45#include <babeltrace/graph/notification-stream.h>
7d61fa8e 46#include <plugins-common.h>
bc506aa5
JD
47#include <stdio.h>
48#include <stdbool.h>
49#include <glib.h>
50#include "writer.h"
c70ed359 51#include <assert.h>
bc506aa5 52
1c78e839 53static
a619fcb7 54gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
f3168545 55{
a619fcb7
JD
56 struct fs_writer *fs_writer = value;
57 struct writer_component *writer_component = user_data;
58
3b41eaba 59 fs_writer->trace_static = 1;
a619fcb7
JD
60 writer_close(writer_component, fs_writer);
61
f3168545
JD
62 return TRUE;
63}
64
bc506aa5
JD
65static
66void destroy_writer_component_data(struct writer_component *writer_component)
67{
c70ed359 68 bt_put(writer_component->input_iterator);
f3168545 69
f3168545 70 g_hash_table_foreach_remove(writer_component->trace_map,
a619fcb7 71 empty_trace_map, writer_component);
bc506aa5 72 g_hash_table_destroy(writer_component->trace_map);
f3168545 73
9057f037
JD
74 g_string_free(writer_component->base_path, true);
75 g_string_free(writer_component->trace_name_base, true);
bc506aa5
JD
76}
77
d8866baa
PP
78BT_HIDDEN
79void writer_component_finalize(struct bt_private_component *component)
bc506aa5
JD
80{
81 struct writer_component *writer_component = (struct writer_component *)
890882ef 82 bt_private_component_get_user_data(component);
bc506aa5
JD
83
84 destroy_writer_component_data(writer_component);
85 g_free(writer_component);
86}
87
bc506aa5 88static
f3168545 89void free_fs_writer(struct fs_writer *fs_writer)
bc506aa5 90{
f3168545
JD
91 bt_put(fs_writer->writer);
92 g_free(fs_writer);
bc506aa5
JD
93}
94
95static
96struct 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;
9057f037
JD
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 }
bc506aa5
JD
113
114 /*
115 * Reader to writer corresponding structures.
116 */
117 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
f3168545 118 g_direct_equal, NULL, (GDestroyNotify) free_fs_writer);
bc506aa5
JD
119
120end:
121 return writer_component;
122}
123
124static
125enum 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)) {
ea0e619e 137 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
bc506aa5
JD
138 {
139 struct bt_ctf_packet *packet =
ea0e619e 140 bt_notification_packet_begin_get_packet(notification);
bc506aa5
JD
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 =
ea0e619e 154 bt_notification_packet_end_get_packet(notification);
bc506aa5
JD
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 }
bc506aa5
JD
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 }
f384901f
JD
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 }
bc506aa5 193 case BT_NOTIFICATION_TYPE_STREAM_END:
f3168545
JD
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);
bc506aa5 204 break;
f3168545 205 }
bc506aa5
JD
206 default:
207 puts("Unhandled notification type");
208 }
209end:
210 return ret;
211}
212
d8866baa 213BT_HIDDEN
0d8b4d8e 214void writer_component_port_connected(
890882ef 215 struct bt_private_component *component,
8f4799f7
PP
216 struct bt_private_port *self_port,
217 struct bt_port *other_port)
c70ed359 218{
890882ef 219 struct bt_private_connection *connection;
c70ed359 220 struct writer_component *writer;
73d5c1ad 221 enum bt_connection_status conn_status;
93fc16a5 222 static const enum bt_notification_type notif_types[] = {
f3168545 223 BT_NOTIFICATION_TYPE_EVENT,
93fc16a5
JD
224 BT_NOTIFICATION_TYPE_PACKET_BEGIN,
225 BT_NOTIFICATION_TYPE_PACKET_END,
f384901f 226 BT_NOTIFICATION_TYPE_STREAM_BEGIN,
f3168545 227 BT_NOTIFICATION_TYPE_STREAM_END,
93fc16a5
JD
228 BT_NOTIFICATION_TYPE_SENTINEL,
229 };
c70ed359 230
890882ef 231 writer = bt_private_component_get_user_data(component);
c70ed359
JG
232 assert(writer);
233 assert(!writer->input_iterator);
890882ef 234 connection = bt_private_port_get_private_connection(self_port);
72b913fb 235 assert(connection);
73d5c1ad
PP
236 conn_status = bt_private_connection_create_notification_iterator(
237 connection, notif_types, &writer->input_iterator);
238 if (conn_status != BT_CONNECTION_STATUS_OK) {
0d8b4d8e 239 writer->error = true;
c70ed359 240 }
0d8b4d8e 241
72b913fb 242 bt_put(connection);
c70ed359
JG
243}
244
d8866baa
PP
245BT_HIDDEN
246enum bt_component_status writer_run(struct bt_private_component *component)
bc506aa5
JD
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 =
890882ef 252 bt_private_component_get_user_data(component);
7cdc2bab 253 enum bt_notification_iterator_status it_ret;
bc506aa5 254
0d8b4d8e
PP
255 if (unlikely(writer_component->error)) {
256 ret = BT_COMPONENT_STATUS_ERROR;
257 goto end;
258 }
259
7cdc2bab
MD
260 it = writer_component->input_iterator;
261 assert(it);
262 it_ret = bt_notification_iterator_next(it);
bc506aa5 263
7cdc2bab
MD
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:
72b913fb 275 ret = BT_COMPONENT_STATUS_ERROR;
996a07a1
JG
276 goto end;
277 }
278
7cdc2bab
MD
279 notification = bt_notification_iterator_get_notification(it);
280 assert(notification);
bc506aa5
JD
281 ret = handle_notification(writer_component, notification);
282end:
bc506aa5
JD
283 bt_put(notification);
284 return ret;
285}
286
6ca0eb99
JD
287static
288enum 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);
802af08e 303 if (status != BT_VALUE_STATUS_OK) {
6ca0eb99
JD
304 ret = BT_COMPONENT_STATUS_ERROR;
305 goto end;
306 }
802af08e 307
6ca0eb99
JD
308 *option = (bool) bool_val;
309 if (found) {
310 *found = true;
311 }
312end:
313 bt_put(value);
314 return ret;
315}
316
d8866baa 317BT_HIDDEN
bc506aa5 318enum bt_component_status writer_component_init(
890882ef 319 struct bt_private_component *component, struct bt_value *params,
7d61fa8e 320 UNUSED_VAR void *init_method_data)
bc506aa5
JD
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
147337a3
PP
333 ret = bt_private_component_sink_add_input_private_port(component,
334 "in", NULL, NULL);
335 if (ret != BT_COMPONENT_STATUS_OK) {
b9d103be
PP
336 goto end;
337 }
338
bc506aa5
JD
339 value = bt_value_map_get(params, "path");
340 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
449f71e3 341 BT_LOGE_STR("Missing mandatory \"path\" parameter.");
bc506aa5
JD
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 }
d00b90bf 351 bt_put(value);
bc506aa5 352
9057f037 353 writer_component->base_path = g_string_new(path);
da4fc746 354 if (!writer_component->base_path) {
9057f037
JD
355 ret = BT_COMPONENT_STATUS_ERROR;
356 goto error;
357 }
bc506aa5 358
6ca0eb99
JD
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
890882ef 366 ret = bt_private_component_set_user_data(component, writer_component);
bc506aa5
JD
367 if (ret != BT_COMPONENT_STATUS_OK) {
368 goto error;
369 }
370
bc506aa5
JD
371end:
372 return ret;
373error:
374 destroy_writer_component_data(writer_component);
56fa591a 375 g_free(writer_component);
bc506aa5
JD
376 return ret;
377}
This page took 0.049697 seconds and 4 git commands to generate.