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 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/graph/component.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/component-sink.h>
34 #include <babeltrace/graph/private-component-sink.h>
35 #include <babeltrace/graph/private-port.h>
36 #include <babeltrace/graph/private-connection.h>
37 #include <babeltrace/graph/notification.h>
38 #include <babeltrace/graph/notification-iterator.h>
39 #include <babeltrace/graph/notification-event.h>
40 #include <babeltrace/graph/notification-packet.h>
41 #include <babeltrace/graph/notification-stream.h>
42 #include <plugins-common.h>
49 gboolean
empty_ht(gpointer key
, gpointer value
, gpointer user_data
)
55 void destroy_writer_component_data(struct writer_component
*writer_component
)
57 bt_put(writer_component
->input_iterator
);
59 g_hash_table_foreach_remove(writer_component
->stream_class_map
,
61 g_hash_table_destroy(writer_component
->stream_class_map
);
63 g_hash_table_foreach_remove(writer_component
->stream_map
,
65 g_hash_table_destroy(writer_component
->stream_map
);
67 g_hash_table_foreach_remove(writer_component
->trace_map
,
69 g_hash_table_destroy(writer_component
->trace_map
);
71 g_string_free(writer_component
->base_path
, true);
72 g_string_free(writer_component
->trace_name_base
, true);
76 void writer_component_finalize(struct bt_private_component
*component
)
78 struct writer_component
*writer_component
= (struct writer_component
*)
79 bt_private_component_get_user_data(component
);
81 destroy_writer_component_data(writer_component
);
82 g_free(writer_component
);
86 void unref_stream_class(struct bt_ctf_stream_class
*writer_stream_class
)
88 bt_put(writer_stream_class
);
92 void unref_stream(struct bt_ctf_stream_class
*writer_stream
)
94 bt_put(writer_stream
);
98 void free_fs_writer(struct fs_writer
*fs_writer
)
100 bt_put(fs_writer
->writer
);
105 struct writer_component
*create_writer_component(void)
107 struct writer_component
*writer_component
;
109 writer_component
= g_new0(struct writer_component
, 1);
110 if (!writer_component
) {
114 writer_component
->err
= stderr
;
115 writer_component
->trace_id
= 0;
116 writer_component
->trace_name_base
= g_string_new("trace");
117 writer_component
->processed_first_event
= false;
118 if (!writer_component
->trace_name_base
) {
119 g_free(writer_component
);
120 writer_component
= NULL
;
125 * Reader to writer corresponding structures.
127 writer_component
->trace_map
= g_hash_table_new_full(g_direct_hash
,
128 g_direct_equal
, NULL
, (GDestroyNotify
) free_fs_writer
);
129 writer_component
->stream_class_map
= g_hash_table_new_full(g_direct_hash
,
130 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream_class
);
131 writer_component
->stream_map
= g_hash_table_new_full(g_direct_hash
,
132 g_direct_equal
, NULL
, (GDestroyNotify
) unref_stream
);
135 return writer_component
;
139 enum bt_component_status
handle_notification(
140 struct writer_component
*writer_component
,
141 struct bt_notification
*notification
)
143 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
145 if (!writer_component
) {
146 ret
= BT_COMPONENT_STATUS_ERROR
;
150 switch (bt_notification_get_type(notification
)) {
151 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
153 struct bt_ctf_packet
*packet
=
154 bt_notification_packet_begin_get_packet(notification
);
157 ret
= BT_COMPONENT_STATUS_ERROR
;
161 ret
= writer_new_packet(writer_component
, packet
);
165 case BT_NOTIFICATION_TYPE_PACKET_END
:
167 struct bt_ctf_packet
*packet
=
168 bt_notification_packet_end_get_packet(notification
);
171 ret
= BT_COMPONENT_STATUS_ERROR
;
174 ret
= writer_close_packet(writer_component
, packet
);
178 case BT_NOTIFICATION_TYPE_EVENT
:
180 struct bt_ctf_event
*event
= bt_notification_event_get_event(
184 ret
= BT_COMPONENT_STATUS_ERROR
;
187 ret
= writer_output_event(writer_component
, event
);
189 if (ret
!= BT_COMPONENT_STATUS_OK
) {
194 case BT_NOTIFICATION_TYPE_STREAM_END
:
196 struct bt_ctf_stream
*stream
=
197 bt_notification_stream_end_get_stream(notification
);
200 ret
= BT_COMPONENT_STATUS_ERROR
;
203 ret
= writer_stream_end(writer_component
, stream
);
208 puts("Unhandled notification type");
215 void writer_component_port_connected(
216 struct bt_private_component
*component
,
217 struct bt_private_port
*self_port
,
218 struct bt_port
*other_port
)
220 struct bt_private_connection
*connection
;
221 struct writer_component
*writer
;
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_END
,
227 BT_NOTIFICATION_TYPE_SENTINEL
,
230 writer
= bt_private_component_get_user_data(component
);
232 assert(!writer
->input_iterator
);
233 connection
= bt_private_port_get_private_connection(self_port
);
235 writer
->input_iterator
=
236 bt_private_connection_create_notification_iterator(connection
,
239 if (!writer
->input_iterator
) {
240 writer
->error
= true;
247 enum bt_component_status
writer_run(struct bt_private_component
*component
)
249 enum bt_component_status ret
;
250 struct bt_notification
*notification
= NULL
;
251 struct bt_notification_iterator
*it
;
252 struct writer_component
*writer_component
=
253 bt_private_component_get_user_data(component
);
254 enum bt_notification_iterator_status it_ret
;
256 if (unlikely(writer_component
->error
)) {
257 ret
= BT_COMPONENT_STATUS_ERROR
;
261 it
= writer_component
->input_iterator
;
263 it_ret
= bt_notification_iterator_next(it
);
266 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
267 ret
= BT_COMPONENT_STATUS_END
;
268 BT_PUT(writer_component
->input_iterator
);
270 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
271 ret
= BT_COMPONENT_STATUS_AGAIN
;
273 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
276 ret
= BT_COMPONENT_STATUS_ERROR
;
280 notification
= bt_notification_iterator_get_notification(it
);
281 assert(notification
);
282 ret
= handle_notification(writer_component
, notification
);
284 bt_put(notification
);
289 enum bt_component_status
writer_component_init(
290 struct bt_private_component
*component
, struct bt_value
*params
,
291 UNUSED_VAR
void *init_method_data
)
293 enum bt_component_status ret
;
294 enum bt_value_status value_ret
;
295 struct writer_component
*writer_component
= create_writer_component();
296 struct bt_value
*value
= NULL
;
300 if (!writer_component
) {
301 ret
= BT_COMPONENT_STATUS_NOMEM
;
305 priv_port
= bt_private_component_sink_add_input_private_port(component
,
308 ret
= BT_COMPONENT_STATUS_NOMEM
;
314 value
= bt_value_map_get(params
, "path");
315 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
316 fprintf(writer_component
->err
,
317 "[error] output path parameter required\n");
318 ret
= BT_COMPONENT_STATUS_INVALID
;
322 value_ret
= bt_value_string_get(value
, &path
);
323 if (value_ret
!= BT_VALUE_STATUS_OK
) {
324 ret
= BT_COMPONENT_STATUS_INVALID
;
329 writer_component
->base_path
= g_string_new(path
);
330 if (!writer_component
) {
331 ret
= BT_COMPONENT_STATUS_ERROR
;
335 ret
= bt_private_component_set_user_data(component
, writer_component
);
336 if (ret
!= BT_COMPONENT_STATUS_OK
) {
343 destroy_writer_component_data(writer_component
);
344 g_free(writer_component
);