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/ctf-ir/packet.h>
33 #include <babeltrace/plugin/plugin-dev.h>
34 #include <babeltrace/graph/connection.h>
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>
45 #include <babeltrace/graph/notification-stream.h>
46 #include <plugins-common.h>
54 gboolean
empty_trace_map(gpointer key
, gpointer value
, gpointer user_data
)
56 struct fs_writer
*fs_writer
= value
;
57 struct writer_component
*writer_component
= user_data
;
59 fs_writer
->trace_static
= 1;
60 writer_close(writer_component
, fs_writer
);
66 void destroy_writer_component_data(struct writer_component
*writer_component
)
68 bt_put(writer_component
->input_iterator
);
70 g_hash_table_foreach_remove(writer_component
->trace_map
,
71 empty_trace_map
, writer_component
);
72 g_hash_table_destroy(writer_component
->trace_map
);
74 g_string_free(writer_component
->base_path
, true);
75 g_string_free(writer_component
->trace_name_base
, true);
79 void writer_component_finalize(struct bt_private_component
*component
)
81 struct writer_component
*writer_component
= (struct writer_component
*)
82 bt_private_component_get_user_data(component
);
84 destroy_writer_component_data(writer_component
);
85 g_free(writer_component
);
89 void free_fs_writer(struct fs_writer
*fs_writer
)
91 bt_put(fs_writer
->writer
);
96 struct writer_component
*create_writer_component(void)
98 struct writer_component
*writer_component
;
100 writer_component
= g_new0(struct writer_component
, 1);
101 if (!writer_component
) {
105 writer_component
->err
= stderr
;
106 writer_component
->trace_id
= 0;
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
;
115 * Reader to writer corresponding structures.
117 writer_component
->trace_map
= g_hash_table_new_full(g_direct_hash
,
118 g_direct_equal
, NULL
, (GDestroyNotify
) free_fs_writer
);
121 return writer_component
;
125 enum bt_component_status
handle_notification(
126 struct writer_component
*writer_component
,
127 struct bt_notification
*notification
)
129 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
131 if (!writer_component
) {
132 ret
= BT_COMPONENT_STATUS_ERROR
;
136 switch (bt_notification_get_type(notification
)) {
137 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
139 struct bt_ctf_packet
*packet
=
140 bt_notification_packet_begin_get_packet(notification
);
143 ret
= BT_COMPONENT_STATUS_ERROR
;
147 ret
= writer_new_packet(writer_component
, packet
);
151 case BT_NOTIFICATION_TYPE_PACKET_END
:
153 struct bt_ctf_packet
*packet
=
154 bt_notification_packet_end_get_packet(notification
);
157 ret
= BT_COMPONENT_STATUS_ERROR
;
160 ret
= writer_close_packet(writer_component
, packet
);
164 case BT_NOTIFICATION_TYPE_EVENT
:
166 struct bt_ctf_event
*event
= bt_notification_event_get_event(
170 ret
= BT_COMPONENT_STATUS_ERROR
;
173 ret
= writer_output_event(writer_component
, event
);
175 if (ret
!= BT_COMPONENT_STATUS_OK
) {
180 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
182 struct bt_ctf_stream
*stream
=
183 bt_notification_stream_begin_get_stream(notification
);
186 ret
= BT_COMPONENT_STATUS_ERROR
;
189 ret
= writer_stream_begin(writer_component
, stream
);
193 case BT_NOTIFICATION_TYPE_STREAM_END
:
195 struct bt_ctf_stream
*stream
=
196 bt_notification_stream_end_get_stream(notification
);
199 ret
= BT_COMPONENT_STATUS_ERROR
;
202 ret
= writer_stream_end(writer_component
, stream
);
207 puts("Unhandled notification type");
214 void writer_component_port_connected(
215 struct bt_private_component
*component
,
216 struct bt_private_port
*self_port
,
217 struct bt_port
*other_port
)
219 struct bt_private_connection
*connection
;
220 struct writer_component
*writer
;
221 enum bt_connection_status conn_status
;
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_BEGIN
,
227 BT_NOTIFICATION_TYPE_STREAM_END
,
228 BT_NOTIFICATION_TYPE_SENTINEL
,
231 writer
= bt_private_component_get_user_data(component
);
233 assert(!writer
->input_iterator
);
234 connection
= bt_private_port_get_private_connection(self_port
);
236 conn_status
= bt_private_connection_create_notification_iterator(
237 connection
, notif_types
, &writer
->input_iterator
);
238 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
239 writer
->error
= true;
246 enum bt_component_status
writer_run(struct bt_private_component
*component
)
248 enum bt_component_status ret
;
249 struct bt_notification
*notification
= NULL
;
250 struct bt_notification_iterator
*it
;
251 struct writer_component
*writer_component
=
252 bt_private_component_get_user_data(component
);
253 enum bt_notification_iterator_status it_ret
;
255 if (unlikely(writer_component
->error
)) {
256 ret
= BT_COMPONENT_STATUS_ERROR
;
260 it
= writer_component
->input_iterator
;
262 it_ret
= bt_notification_iterator_next(it
);
265 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
266 ret
= BT_COMPONENT_STATUS_END
;
267 BT_PUT(writer_component
->input_iterator
);
269 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
270 ret
= BT_COMPONENT_STATUS_AGAIN
;
272 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
275 ret
= BT_COMPONENT_STATUS_ERROR
;
279 notification
= bt_notification_iterator_get_notification(it
);
280 assert(notification
);
281 ret
= handle_notification(writer_component
, notification
);
283 bt_put(notification
);
288 enum bt_component_status
apply_one_bool(const char *key
,
289 struct bt_value
*params
,
293 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
294 struct bt_value
*value
= NULL
;
295 enum bt_value_status status
;
298 value
= bt_value_map_get(params
, key
);
302 status
= bt_value_bool_get(value
, &bool_val
);
303 if (status
!= BT_VALUE_STATUS_OK
) {
304 ret
= BT_COMPONENT_STATUS_ERROR
;
308 *option
= (bool) bool_val
;
318 enum bt_component_status
writer_component_init(
319 struct bt_private_component
*component
, struct bt_value
*params
,
320 UNUSED_VAR
void *init_method_data
)
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
;
328 if (!writer_component
) {
329 ret
= BT_COMPONENT_STATUS_NOMEM
;
333 ret
= bt_private_component_sink_add_input_private_port(component
,
335 if (ret
!= BT_COMPONENT_STATUS_OK
) {
339 value
= bt_value_map_get(params
, "path");
340 if (!value
|| bt_value_is_null(value
) || !bt_value_is_string(value
)) {
341 BT_LOGE_STR("Missing mandatory \"path\" parameter.");
342 ret
= BT_COMPONENT_STATUS_INVALID
;
346 value_ret
= bt_value_string_get(value
, &path
);
347 if (value_ret
!= BT_VALUE_STATUS_OK
) {
348 ret
= BT_COMPONENT_STATUS_INVALID
;
353 writer_component
->base_path
= g_string_new(path
);
354 if (!writer_component
) {
355 ret
= BT_COMPONENT_STATUS_ERROR
;
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
) {
366 ret
= bt_private_component_set_user_data(component
, writer_component
);
367 if (ret
!= BT_COMPONENT_STATUS_OK
) {
374 destroy_writer_component_data(writer_component
);
375 g_free(writer_component
);