Commit | Line | Data |
---|---|---|
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 | ||
29 | #include <babeltrace/ctf-ir/packet.h> | |
33b34c43 PP |
30 | #include <babeltrace/plugin/plugin-dev.h> |
31 | #include <babeltrace/component/component.h> | |
890882ef | 32 | #include <babeltrace/component/private-component.h> |
d71dcf2c | 33 | #include <babeltrace/component/component-sink.h> |
890882ef PP |
34 | #include <babeltrace/component/private-component-sink.h> |
35 | #include <babeltrace/component/private-port.h> | |
36 | #include <babeltrace/component/private-connection.h> | |
33b34c43 PP |
37 | #include <babeltrace/component/notification/notification.h> |
38 | #include <babeltrace/component/notification/iterator.h> | |
39 | #include <babeltrace/component/notification/event.h> | |
40 | #include <babeltrace/component/notification/packet.h> | |
7d61fa8e | 41 | #include <plugins-common.h> |
bc506aa5 JD |
42 | #include <stdio.h> |
43 | #include <stdbool.h> | |
44 | #include <glib.h> | |
45 | #include "writer.h" | |
c70ed359 | 46 | #include <assert.h> |
bc506aa5 JD |
47 | |
48 | static | |
49 | void destroy_writer_component_data(struct writer_component *writer_component) | |
50 | { | |
c70ed359 | 51 | bt_put(writer_component->input_iterator); |
bc506aa5 JD |
52 | g_hash_table_destroy(writer_component->stream_map); |
53 | g_hash_table_destroy(writer_component->stream_class_map); | |
54 | g_hash_table_destroy(writer_component->trace_map); | |
9057f037 JD |
55 | g_string_free(writer_component->base_path, true); |
56 | g_string_free(writer_component->trace_name_base, true); | |
bc506aa5 JD |
57 | } |
58 | ||
59 | static | |
64cadc66 | 60 | void finalize_writer_component(struct bt_private_component *component) |
bc506aa5 JD |
61 | { |
62 | struct writer_component *writer_component = (struct writer_component *) | |
890882ef | 63 | bt_private_component_get_user_data(component); |
bc506aa5 JD |
64 | |
65 | destroy_writer_component_data(writer_component); | |
66 | g_free(writer_component); | |
67 | } | |
68 | ||
69 | static | |
70 | void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class) | |
71 | { | |
72 | BT_PUT(writer_stream_class); | |
73 | g_free(writer_stream_class); | |
74 | } | |
75 | ||
76 | static | |
77 | void unref_stream(struct bt_ctf_stream_class *writer_stream) | |
78 | { | |
79 | BT_PUT(writer_stream); | |
80 | g_free(writer_stream); | |
81 | } | |
82 | ||
83 | static | |
84 | void unref_trace(struct bt_ctf_writer *writer) | |
85 | { | |
86 | BT_PUT(writer); | |
87 | g_free(writer); | |
88 | } | |
89 | ||
90 | static | |
91 | struct writer_component *create_writer_component(void) | |
92 | { | |
93 | struct writer_component *writer_component; | |
94 | ||
95 | writer_component = g_new0(struct writer_component, 1); | |
96 | if (!writer_component) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | writer_component->err = stderr; | |
101 | writer_component->trace_id = 0; | |
9057f037 JD |
102 | writer_component->trace_name_base = g_string_new("trace"); |
103 | if (!writer_component->trace_name_base) { | |
104 | g_free(writer_component); | |
105 | writer_component = NULL; | |
106 | goto end; | |
107 | } | |
bc506aa5 JD |
108 | |
109 | /* | |
110 | * Reader to writer corresponding structures. | |
111 | */ | |
112 | writer_component->trace_map = g_hash_table_new_full(g_direct_hash, | |
113 | g_direct_equal, NULL, (GDestroyNotify) unref_trace); | |
114 | writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash, | |
115 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); | |
116 | writer_component->stream_map = g_hash_table_new_full(g_direct_hash, | |
117 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); | |
118 | ||
119 | end: | |
120 | return writer_component; | |
121 | } | |
122 | ||
123 | static | |
124 | enum bt_component_status handle_notification( | |
125 | struct writer_component *writer_component, | |
126 | struct bt_notification *notification) | |
127 | { | |
128 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
129 | ||
130 | if (!writer_component) { | |
131 | ret = BT_COMPONENT_STATUS_ERROR; | |
132 | goto end; | |
133 | } | |
134 | ||
135 | switch (bt_notification_get_type(notification)) { | |
ea0e619e | 136 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
bc506aa5 JD |
137 | { |
138 | struct bt_ctf_packet *packet = | |
ea0e619e | 139 | bt_notification_packet_begin_get_packet(notification); |
bc506aa5 JD |
140 | |
141 | if (!packet) { | |
142 | ret = BT_COMPONENT_STATUS_ERROR; | |
143 | goto end; | |
144 | } | |
145 | ||
146 | ret = writer_new_packet(writer_component, packet); | |
147 | bt_put(packet); | |
148 | break; | |
149 | } | |
150 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
151 | { | |
152 | struct bt_ctf_packet *packet = | |
ea0e619e | 153 | bt_notification_packet_end_get_packet(notification); |
bc506aa5 JD |
154 | |
155 | if (!packet) { | |
156 | ret = BT_COMPONENT_STATUS_ERROR; | |
157 | goto end; | |
158 | } | |
159 | ret = writer_close_packet(writer_component, packet); | |
160 | bt_put(packet); | |
161 | break; | |
162 | } | |
163 | case BT_NOTIFICATION_TYPE_EVENT: | |
164 | { | |
165 | struct bt_ctf_event *event = bt_notification_event_get_event( | |
166 | notification); | |
167 | ||
168 | if (!event) { | |
169 | ret = BT_COMPONENT_STATUS_ERROR; | |
170 | goto end; | |
171 | } | |
172 | ret = BT_COMPONENT_STATUS_OK; | |
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 | } | |
180 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
181 | break; | |
182 | default: | |
183 | puts("Unhandled notification type"); | |
184 | } | |
185 | end: | |
186 | return ret; | |
187 | } | |
188 | ||
c70ed359 | 189 | static |
72b913fb | 190 | enum bt_component_status writer_component_accept_port_connection( |
890882ef | 191 | struct bt_private_component *component, |
8f4799f7 PP |
192 | struct bt_private_port *self_port, |
193 | struct bt_port *other_port) | |
c70ed359 JG |
194 | { |
195 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
890882ef | 196 | struct bt_private_connection *connection; |
c70ed359 JG |
197 | struct writer_component *writer; |
198 | ||
890882ef | 199 | writer = bt_private_component_get_user_data(component); |
c70ed359 JG |
200 | assert(writer); |
201 | assert(!writer->input_iterator); | |
890882ef | 202 | connection = bt_private_port_get_private_connection(self_port); |
72b913fb | 203 | assert(connection); |
890882ef PP |
204 | writer->input_iterator = |
205 | bt_private_connection_create_notification_iterator(connection); | |
c70ed359 JG |
206 | |
207 | if (!writer->input_iterator) { | |
208 | ret = BT_COMPONENT_STATUS_ERROR; | |
209 | } | |
72b913fb | 210 | bt_put(connection); |
c70ed359 JG |
211 | return ret; |
212 | } | |
213 | ||
bc506aa5 | 214 | static |
890882ef | 215 | enum bt_component_status run(struct bt_private_component *component) |
bc506aa5 JD |
216 | { |
217 | enum bt_component_status ret; | |
72b913fb | 218 | enum bt_notification_iterator_status it_status; |
bc506aa5 JD |
219 | struct bt_notification *notification = NULL; |
220 | struct bt_notification_iterator *it; | |
221 | struct writer_component *writer_component = | |
890882ef | 222 | bt_private_component_get_user_data(component); |
bc506aa5 | 223 | |
c70ed359 JG |
224 | it = writer_component->input_iterator; |
225 | assert(it); | |
bc506aa5 | 226 | |
bc506aa5 JD |
227 | notification = bt_notification_iterator_get_notification(it); |
228 | if (!notification) { | |
229 | ret = BT_COMPONENT_STATUS_ERROR; | |
230 | goto end; | |
231 | } | |
232 | ||
72b913fb PP |
233 | it_status = bt_notification_iterator_next(it); |
234 | if (it_status != BT_COMPONENT_STATUS_OK) { | |
235 | ret = BT_COMPONENT_STATUS_ERROR; | |
996a07a1 JG |
236 | goto end; |
237 | } | |
238 | ||
bc506aa5 JD |
239 | ret = handle_notification(writer_component, notification); |
240 | end: | |
241 | bt_put(it); | |
242 | bt_put(notification); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | static | |
247 | enum bt_component_status writer_component_init( | |
890882ef | 248 | struct bt_private_component *component, struct bt_value *params, |
7d61fa8e | 249 | UNUSED_VAR void *init_method_data) |
bc506aa5 JD |
250 | { |
251 | enum bt_component_status ret; | |
252 | enum bt_value_status value_ret; | |
253 | struct writer_component *writer_component = create_writer_component(); | |
254 | struct bt_value *value = NULL; | |
255 | const char *path; | |
256 | ||
257 | if (!writer_component) { | |
258 | ret = BT_COMPONENT_STATUS_NOMEM; | |
259 | goto end; | |
260 | } | |
261 | ||
262 | value = bt_value_map_get(params, "path"); | |
263 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
264 | fprintf(writer_component->err, | |
265 | "[error] output path parameter required\n"); | |
266 | ret = BT_COMPONENT_STATUS_INVALID; | |
267 | goto error; | |
268 | } | |
269 | ||
270 | value_ret = bt_value_string_get(value, &path); | |
271 | if (value_ret != BT_VALUE_STATUS_OK) { | |
272 | ret = BT_COMPONENT_STATUS_INVALID; | |
273 | goto error; | |
274 | } | |
275 | ||
9057f037 JD |
276 | writer_component->base_path = g_string_new(path); |
277 | if (!writer_component) { | |
278 | ret = BT_COMPONENT_STATUS_ERROR; | |
279 | goto error; | |
280 | } | |
bc506aa5 | 281 | |
890882ef | 282 | ret = bt_private_component_set_user_data(component, writer_component); |
bc506aa5 JD |
283 | if (ret != BT_COMPONENT_STATUS_OK) { |
284 | goto error; | |
285 | } | |
286 | ||
bc506aa5 JD |
287 | end: |
288 | return ret; | |
289 | error: | |
290 | destroy_writer_component_data(writer_component); | |
56fa591a | 291 | g_free(writer_component); |
bc506aa5 JD |
292 | return ret; |
293 | } | |
294 | ||
295 | /* Initialize plug-in entry points. */ | |
6ba0b073 | 296 | BT_PLUGIN(writer); |
bc506aa5 JD |
297 | BT_PLUGIN_DESCRIPTION("Babeltrace CTF-Writer output plug-in."); |
298 | BT_PLUGIN_AUTHOR("Jérémie Galarneau"); | |
299 | BT_PLUGIN_LICENSE("MIT"); | |
d3e4dcd8 PP |
300 | BT_PLUGIN_SINK_COMPONENT_CLASS(writer, run); |
301 | BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(writer, writer_component_init); | |
72b913fb PP |
302 | BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(writer, |
303 | writer_component_accept_port_connection); | |
64cadc66 | 304 | BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(writer, finalize_writer_component); |
d3e4dcd8 | 305 | BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(writer, "Formats CTF-IR to CTF."); |