Rename: bt_put(), bt_get() -> bt_object_put_ref(), bt_object_get_ref()
[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
3d85ffe1
JD
29#define BT_LOG_TAG "PLUGIN-CTF-FS-SINK-WRITER"
30#include "logging.h"
31
e0831b38 32#include <babeltrace/babeltrace.h>
7d61fa8e 33#include <plugins-common.h>
bc506aa5
JD
34#include <stdio.h>
35#include <stdbool.h>
36#include <glib.h>
37#include "writer.h"
8b45963b 38#include <babeltrace/assert-internal.h>
bc506aa5 39
1c78e839 40static
a619fcb7 41gboolean empty_trace_map(gpointer key, gpointer value, gpointer user_data)
f3168545 42{
a619fcb7
JD
43 struct fs_writer *fs_writer = value;
44 struct writer_component *writer_component = user_data;
45
3b41eaba 46 fs_writer->trace_static = 1;
a619fcb7
JD
47 writer_close(writer_component, fs_writer);
48
f3168545
JD
49 return TRUE;
50}
51
bc506aa5
JD
52static
53void destroy_writer_component_data(struct writer_component *writer_component)
54{
8138bfe1 55 bt_object_put_ref(writer_component->input_iterator);
f3168545 56
f3168545 57 g_hash_table_foreach_remove(writer_component->trace_map,
a619fcb7 58 empty_trace_map, writer_component);
bc506aa5 59 g_hash_table_destroy(writer_component->trace_map);
f3168545 60
9057f037
JD
61 g_string_free(writer_component->base_path, true);
62 g_string_free(writer_component->trace_name_base, true);
bc506aa5
JD
63}
64
d8866baa
PP
65BT_HIDDEN
66void writer_component_finalize(struct bt_private_component *component)
bc506aa5
JD
67{
68 struct writer_component *writer_component = (struct writer_component *)
890882ef 69 bt_private_component_get_user_data(component);
bc506aa5
JD
70
71 destroy_writer_component_data(writer_component);
72 g_free(writer_component);
73}
74
bc506aa5 75static
f3168545 76void free_fs_writer(struct fs_writer *fs_writer)
bc506aa5 77{
8138bfe1 78 bt_object_put_ref(fs_writer->writer);
f3168545 79 g_free(fs_writer);
bc506aa5
JD
80}
81
82static
83struct 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;
9057f037
JD
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 }
bc506aa5
JD
100
101 /*
102 * Reader to writer corresponding structures.
103 */
104 writer_component->trace_map = g_hash_table_new_full(g_direct_hash,
f3168545 105 g_direct_equal, NULL, (GDestroyNotify) free_fs_writer);
bc506aa5
JD
106
107end:
108 return writer_component;
109}
110
111static
112enum 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)) {
ea0e619e 124 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
bc506aa5 125 {
839d52a5 126 struct bt_packet *packet =
ea0e619e 127 bt_notification_packet_begin_get_packet(notification);
bc506aa5
JD
128
129 if (!packet) {
130 ret = BT_COMPONENT_STATUS_ERROR;
131 goto end;
132 }
133
134 ret = writer_new_packet(writer_component, packet);
8138bfe1 135 bt_object_put_ref(packet);
bc506aa5
JD
136 break;
137 }
138 case BT_NOTIFICATION_TYPE_PACKET_END:
139 {
839d52a5 140 struct bt_packet *packet =
ea0e619e 141 bt_notification_packet_end_get_packet(notification);
bc506aa5
JD
142
143 if (!packet) {
144 ret = BT_COMPONENT_STATUS_ERROR;
145 goto end;
146 }
147 ret = writer_close_packet(writer_component, packet);
8138bfe1 148 bt_object_put_ref(packet);
bc506aa5
JD
149 break;
150 }
151 case BT_NOTIFICATION_TYPE_EVENT:
152 {
839d52a5 153 struct bt_event *event = bt_notification_event_get_event(
bc506aa5
JD
154 notification);
155
156 if (!event) {
157 ret = BT_COMPONENT_STATUS_ERROR;
158 goto end;
159 }
bc506aa5 160 ret = writer_output_event(writer_component, event);
8138bfe1 161 bt_object_put_ref(event);
bc506aa5
JD
162 if (ret != BT_COMPONENT_STATUS_OK) {
163 goto end;
164 }
165 break;
166 }
f384901f
JD
167 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
168 {
839d52a5 169 struct bt_stream *stream =
f384901f
JD
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);
8138bfe1 177 bt_object_put_ref(stream);
f384901f
JD
178 break;
179 }
bc506aa5 180 case BT_NOTIFICATION_TYPE_STREAM_END:
f3168545 181 {
839d52a5 182 struct bt_stream *stream =
f3168545
JD
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);
8138bfe1 190 bt_object_put_ref(stream);
bc506aa5 191 break;
f3168545 192 }
bc506aa5 193 default:
6ff151ad 194 break;
bc506aa5
JD
195 }
196end:
197 return ret;
198}
199
d8866baa 200BT_HIDDEN
0d8b4d8e 201void writer_component_port_connected(
890882ef 202 struct bt_private_component *component,
8f4799f7
PP
203 struct bt_private_port *self_port,
204 struct bt_port *other_port)
c70ed359 205{
890882ef 206 struct bt_private_connection *connection;
c70ed359 207 struct writer_component *writer;
73d5c1ad 208 enum bt_connection_status conn_status;
c70ed359 209
890882ef 210 writer = bt_private_component_get_user_data(component);
8b45963b
PP
211 BT_ASSERT(writer);
212 BT_ASSERT(!writer->input_iterator);
890882ef 213 connection = bt_private_port_get_private_connection(self_port);
8b45963b 214 BT_ASSERT(connection);
73d5c1ad 215 conn_status = bt_private_connection_create_notification_iterator(
6ff151ad 216 connection, &writer->input_iterator);
73d5c1ad 217 if (conn_status != BT_CONNECTION_STATUS_OK) {
0d8b4d8e 218 writer->error = true;
c70ed359 219 }
0d8b4d8e 220
8138bfe1 221 bt_object_put_ref(connection);
c70ed359
JG
222}
223
d8866baa
PP
224BT_HIDDEN
225enum bt_component_status writer_run(struct bt_private_component *component)
bc506aa5
JD
226{
227 enum bt_component_status ret;
228 struct bt_notification *notification = NULL;
229 struct bt_notification_iterator *it;
230 struct writer_component *writer_component =
890882ef 231 bt_private_component_get_user_data(component);
7cdc2bab 232 enum bt_notification_iterator_status it_ret;
bc506aa5 233
0d8b4d8e
PP
234 if (unlikely(writer_component->error)) {
235 ret = BT_COMPONENT_STATUS_ERROR;
236 goto end;
237 }
238
7cdc2bab 239 it = writer_component->input_iterator;
8b45963b 240 BT_ASSERT(it);
7cdc2bab 241 it_ret = bt_notification_iterator_next(it);
bc506aa5 242
7cdc2bab
MD
243 switch (it_ret) {
244 case BT_NOTIFICATION_ITERATOR_STATUS_END:
245 ret = BT_COMPONENT_STATUS_END;
8138bfe1 246 BT_OBJECT_PUT_REF_AND_RESET(writer_component->input_iterator);
7cdc2bab
MD
247 goto end;
248 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
249 ret = BT_COMPONENT_STATUS_AGAIN;
250 goto end;
251 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
252 break;
253 default:
72b913fb 254 ret = BT_COMPONENT_STATUS_ERROR;
996a07a1
JG
255 goto end;
256 }
257
7cdc2bab 258 notification = bt_notification_iterator_get_notification(it);
8b45963b 259 BT_ASSERT(notification);
bc506aa5
JD
260 ret = handle_notification(writer_component, notification);
261end:
8138bfe1 262 bt_object_put_ref(notification);
bc506aa5
JD
263 return ret;
264}
265
cf5d5317
JD
266static
267enum bt_component_status apply_one_bool(const char *key,
268 struct bt_value *params,
269 bool *option,
270 bool *found)
271{
272 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
273 struct bt_value *value = NULL;
274 enum bt_value_status status;
275 bt_bool bool_val;
276
277 value = bt_value_map_get(params, key);
278 if (!value) {
279 goto end;
280 }
281 status = bt_value_bool_get(value, &bool_val);
3be61dc2 282 if (status != BT_VALUE_STATUS_OK) {
cf5d5317
JD
283 ret = BT_COMPONENT_STATUS_ERROR;
284 goto end;
285 }
3be61dc2 286
cf5d5317
JD
287 *option = (bool) bool_val;
288 if (found) {
289 *found = true;
290 }
291end:
8138bfe1 292 bt_object_put_ref(value);
cf5d5317
JD
293 return ret;
294}
295
d8866baa 296BT_HIDDEN
bc506aa5 297enum bt_component_status writer_component_init(
890882ef 298 struct bt_private_component *component, struct bt_value *params,
7d61fa8e 299 UNUSED_VAR void *init_method_data)
bc506aa5
JD
300{
301 enum bt_component_status ret;
302 enum bt_value_status value_ret;
303 struct writer_component *writer_component = create_writer_component();
304 struct bt_value *value = NULL;
305 const char *path;
306
307 if (!writer_component) {
308 ret = BT_COMPONENT_STATUS_NOMEM;
309 goto end;
310 }
311
147337a3
PP
312 ret = bt_private_component_sink_add_input_private_port(component,
313 "in", NULL, NULL);
314 if (ret != BT_COMPONENT_STATUS_OK) {
b9d103be
PP
315 goto end;
316 }
317
bc506aa5
JD
318 value = bt_value_map_get(params, "path");
319 if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) {
3d85ffe1 320 BT_LOGE_STR("Missing mandatory \"path\" parameter.");
bc506aa5
JD
321 ret = BT_COMPONENT_STATUS_INVALID;
322 goto error;
323 }
324
325 value_ret = bt_value_string_get(value, &path);
326 if (value_ret != BT_VALUE_STATUS_OK) {
327 ret = BT_COMPONENT_STATUS_INVALID;
328 goto error;
329 }
8138bfe1 330 bt_object_put_ref(value);
bc506aa5 331
9057f037 332 writer_component->base_path = g_string_new(path);
414860a6 333 if (!writer_component->base_path) {
9057f037
JD
334 ret = BT_COMPONENT_STATUS_ERROR;
335 goto error;
336 }
bc506aa5 337
cf5d5317
JD
338 writer_component->single_trace = false;
339 ret = apply_one_bool("single-trace", params,
340 &writer_component->single_trace, NULL);
341 if (ret != BT_COMPONENT_STATUS_OK) {
342 goto end;
343 }
344
890882ef 345 ret = bt_private_component_set_user_data(component, writer_component);
bc506aa5
JD
346 if (ret != BT_COMPONENT_STATUS_OK) {
347 goto error;
348 }
349
bc506aa5
JD
350end:
351 return ret;
352error:
353 destroy_writer_component_data(writer_component);
56fa591a 354 g_free(writer_component);
bc506aa5
JD
355 return ret;
356}
This page took 0.052007 seconds and 4 git commands to generate.