4 * Babeltrace CTF Writer Output Plugin
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "PLUGIN-CTF-FS-SINK-WRITER"
32 #include <babeltrace/babeltrace.h>
33 #include <plugins-common.h>
38 #include <babeltrace/assert-internal.h>
41 gboolean
empty_trace_map(gpointer key
, gpointer value
, gpointer user_data
)
43 struct fs_writer
*fs_writer
= value
;
44 struct writer_component
*writer_component
= user_data
;
46 fs_writer
->trace_static
= 1;
47 writer_close(writer_component
, fs_writer
);
53 void destroy_writer_component_data(struct writer_component
*writer_component
)
55 bt_object_put_ref(writer_component
->input_iterator
);
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
);
61 g_string_free(writer_component
->base_path
, true);
62 g_string_free(writer_component
->trace_name_base
, true);
66 void writer_component_finalize(bt_self_component
*component
)
68 struct writer_component
*writer_component
= (struct writer_component
*)
69 bt_self_component_get_user_data(component
);
71 destroy_writer_component_data(writer_component
);
72 g_free(writer_component
);
76 void free_fs_writer(struct fs_writer
*fs_writer
)
78 bt_object_put_ref(fs_writer
->writer
);
83 struct writer_component
*create_writer_component(void)
85 struct writer_component
*writer_component
;
87 writer_component
= g_new0(struct writer_component
, 1);
88 if (!writer_component
) {
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
;
102 * Reader to writer corresponding structures.
104 writer_component
->trace_map
= g_hash_table_new_full(g_direct_hash
,
105 g_direct_equal
, NULL
, (GDestroyNotify
) free_fs_writer
);
108 return writer_component
;
112 bt_component_status
handle_message(
113 struct writer_component
*writer_component
,
114 const bt_message
*message
)
116 bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
118 if (!writer_component
) {
119 ret
= BT_COMPONENT_STATUS_ERROR
;
123 switch (bt_message_get_type(message
)) {
124 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
126 const bt_packet
*packet
=
127 bt_message_packet_beginning_get_packet(message
);
130 ret
= BT_COMPONENT_STATUS_ERROR
;
134 ret
= writer_new_packet(writer_component
, packet
);
135 bt_packet_put_ref(packet
);
138 case BT_MESSAGE_TYPE_PACKET_END
:
140 const bt_packet
*packet
=
141 bt_message_packet_end_get_packet(message
);
144 ret
= BT_COMPONENT_STATUS_ERROR
;
147 ret
= writer_close_packet(writer_component
, packet
);
148 bt_packet_put_ref(packet
);
151 case BT_MESSAGE_TYPE_EVENT
:
153 const bt_event
*event
= bt_message_event_get_event(
157 ret
= BT_COMPONENT_STATUS_ERROR
;
160 ret
= writer_output_event(writer_component
, event
);
161 bt_object_put_ref(event
);
162 if (ret
!= BT_COMPONENT_STATUS_OK
) {
167 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
169 const bt_stream
*stream
=
170 bt_message_stream_beginning_get_stream(message
);
173 ret
= BT_COMPONENT_STATUS_ERROR
;
176 ret
= writer_stream_begin(writer_component
, stream
);
177 bt_stream_put_ref(stream
);
180 case BT_MESSAGE_TYPE_STREAM_END
:
182 const bt_stream
*stream
=
183 bt_message_stream_end_get_stream(message
);
186 ret
= BT_COMPONENT_STATUS_ERROR
;
189 ret
= writer_stream_end(writer_component
, stream
);
190 bt_stream_put_ref(stream
);
201 void writer_component_port_connected(
202 bt_self_component
*component
,
203 struct bt_private_port
*self_port
,
204 const bt_port
*other_port
)
206 struct bt_private_connection
*connection
;
207 struct writer_component
*writer
;
208 bt_connection_status conn_status
;
210 writer
= bt_self_component_get_user_data(component
);
212 BT_ASSERT(!writer
->input_iterator
);
213 connection
= bt_private_port_get_connection(self_port
);
214 BT_ASSERT(connection
);
215 conn_status
= bt_private_connection_create_message_iterator(
216 connection
, &writer
->input_iterator
);
217 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
218 writer
->error
= true;
221 bt_object_put_ref(connection
);
225 bt_component_status
writer_run(bt_self_component
*component
)
227 bt_component_status ret
;
228 const bt_message
*message
= NULL
;
229 bt_message_iterator
*it
;
230 struct writer_component
*writer_component
=
231 bt_self_component_get_user_data(component
);
232 bt_message_iterator_status it_ret
;
234 if (unlikely(writer_component
->error
)) {
235 ret
= BT_COMPONENT_STATUS_ERROR
;
239 it
= writer_component
->input_iterator
;
241 it_ret
= bt_message_iterator_next(it
);
244 case BT_MESSAGE_ITERATOR_STATUS_END
:
245 ret
= BT_COMPONENT_STATUS_END
;
246 BT_OBJECT_PUT_REF_AND_RESET(writer_component
->input_iterator
);
248 case BT_MESSAGE_ITERATOR_STATUS_AGAIN
:
249 ret
= BT_COMPONENT_STATUS_AGAIN
;
251 case BT_MESSAGE_ITERATOR_STATUS_OK
:
254 ret
= BT_COMPONENT_STATUS_ERROR
;
258 message
= bt_message_iterator_get_message(it
);
260 ret
= handle_message(writer_component
, message
);
262 bt_object_put_ref(message
);
267 bt_component_status
apply_one_bool(const char *key
,
272 bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
273 bt_value
*value
= NULL
;
274 bt_value_status status
;
277 value
= bt_value_map_get(params
, key
);
281 bool_val
= bt_value_bool_get(value
);
283 *option
= (bool) bool_val
;
288 bt_value_put_ref(value
);
293 bt_component_status
writer_component_init(
294 bt_self_component
*component
, bt_value
*params
,
295 UNUSED_VAR
void *init_method_data
)
297 bt_component_status ret
;
298 bt_value_status value_ret
;
299 struct writer_component
*writer_component
= create_writer_component();
300 bt_value
*value
= NULL
;
303 if (!writer_component
) {
304 ret
= BT_COMPONENT_STATUS_NOMEM
;
308 ret
= bt_self_component_sink_add_input_port(component
,
310 if (ret
!= BT_COMPONENT_STATUS_OK
) {
314 value
= bt_value_map_get(params
, "path");
315 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
316 BT_LOGE_STR("Missing mandatory \"path\" parameter.");
317 ret
= BT_COMPONENT_STATUS_INVALID
;
321 value_ret
= bt_value_string_get(value
, &path
);
322 if (value_ret
!= BT_VALUE_STATUS_OK
) {
323 ret
= BT_COMPONENT_STATUS_INVALID
;
326 bt_object_put_ref(value
);
328 writer_component
->base_path
= g_string_new(path
);
329 if (!writer_component
->base_path
) {
330 ret
= BT_COMPONENT_STATUS_ERROR
;
334 writer_component
->single_trace
= false;
335 ret
= apply_one_bool("single-trace", params
,
336 &writer_component
->single_trace
, NULL
);
337 if (ret
!= BT_COMPONENT_STATUS_OK
) {
341 ret
= bt_self_component_set_user_data(component
, writer_component
);
342 if (ret
!= BT_COMPONENT_STATUS_OK
) {
349 destroy_writer_component_data(writer_component
);
350 g_free(writer_component
);