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 | 30 | #include <babeltrace/plugin/plugin-dev.h> |
b2e0c907 PP |
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> | |
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 | ||
d8866baa PP |
59 | BT_HIDDEN |
60 | void writer_component_finalize(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 | { | |
9ae49d3d | 72 | bt_put(writer_stream_class); |
bc506aa5 JD |
73 | } |
74 | ||
75 | static | |
76 | void unref_stream(struct bt_ctf_stream_class *writer_stream) | |
77 | { | |
1e0002d7 | 78 | bt_put(writer_stream); |
bc506aa5 JD |
79 | } |
80 | ||
81 | static | |
82 | void unref_trace(struct bt_ctf_writer *writer) | |
83 | { | |
9ae49d3d | 84 | bt_put(writer); |
bc506aa5 JD |
85 | } |
86 | ||
87 | static | |
88 | struct writer_component *create_writer_component(void) | |
89 | { | |
90 | struct writer_component *writer_component; | |
91 | ||
92 | writer_component = g_new0(struct writer_component, 1); | |
93 | if (!writer_component) { | |
94 | goto end; | |
95 | } | |
96 | ||
97 | writer_component->err = stderr; | |
98 | writer_component->trace_id = 0; | |
9057f037 | 99 | writer_component->trace_name_base = g_string_new("trace"); |
8654d306 | 100 | writer_component->processed_first_event = false; |
9057f037 JD |
101 | if (!writer_component->trace_name_base) { |
102 | g_free(writer_component); | |
103 | writer_component = NULL; | |
104 | goto end; | |
105 | } | |
bc506aa5 JD |
106 | |
107 | /* | |
108 | * Reader to writer corresponding structures. | |
109 | */ | |
110 | writer_component->trace_map = g_hash_table_new_full(g_direct_hash, | |
111 | g_direct_equal, NULL, (GDestroyNotify) unref_trace); | |
112 | writer_component->stream_class_map = g_hash_table_new_full(g_direct_hash, | |
113 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); | |
114 | writer_component->stream_map = g_hash_table_new_full(g_direct_hash, | |
115 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); | |
116 | ||
117 | end: | |
118 | return writer_component; | |
119 | } | |
120 | ||
121 | static | |
122 | enum bt_component_status handle_notification( | |
123 | struct writer_component *writer_component, | |
124 | struct bt_notification *notification) | |
125 | { | |
126 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
127 | ||
128 | if (!writer_component) { | |
129 | ret = BT_COMPONENT_STATUS_ERROR; | |
130 | goto end; | |
131 | } | |
132 | ||
133 | switch (bt_notification_get_type(notification)) { | |
ea0e619e | 134 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
bc506aa5 JD |
135 | { |
136 | struct bt_ctf_packet *packet = | |
ea0e619e | 137 | bt_notification_packet_begin_get_packet(notification); |
bc506aa5 JD |
138 | |
139 | if (!packet) { | |
140 | ret = BT_COMPONENT_STATUS_ERROR; | |
141 | goto end; | |
142 | } | |
143 | ||
144 | ret = writer_new_packet(writer_component, packet); | |
145 | bt_put(packet); | |
146 | break; | |
147 | } | |
148 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
149 | { | |
150 | struct bt_ctf_packet *packet = | |
ea0e619e | 151 | bt_notification_packet_end_get_packet(notification); |
bc506aa5 JD |
152 | |
153 | if (!packet) { | |
154 | ret = BT_COMPONENT_STATUS_ERROR; | |
155 | goto end; | |
156 | } | |
157 | ret = writer_close_packet(writer_component, packet); | |
158 | bt_put(packet); | |
159 | break; | |
160 | } | |
161 | case BT_NOTIFICATION_TYPE_EVENT: | |
162 | { | |
163 | struct bt_ctf_event *event = bt_notification_event_get_event( | |
164 | notification); | |
165 | ||
166 | if (!event) { | |
167 | ret = BT_COMPONENT_STATUS_ERROR; | |
168 | goto end; | |
169 | } | |
170 | ret = BT_COMPONENT_STATUS_OK; | |
171 | ret = writer_output_event(writer_component, event); | |
172 | bt_put(event); | |
173 | if (ret != BT_COMPONENT_STATUS_OK) { | |
174 | goto end; | |
175 | } | |
176 | break; | |
177 | } | |
178 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
179 | break; | |
180 | default: | |
181 | puts("Unhandled notification type"); | |
182 | } | |
183 | end: | |
184 | return ret; | |
185 | } | |
186 | ||
d8866baa | 187 | BT_HIDDEN |
0d8b4d8e | 188 | void writer_component_port_connected( |
890882ef | 189 | struct bt_private_component *component, |
8f4799f7 PP |
190 | struct bt_private_port *self_port, |
191 | struct bt_port *other_port) | |
c70ed359 | 192 | { |
890882ef | 193 | struct bt_private_connection *connection; |
c70ed359 | 194 | struct writer_component *writer; |
93fc16a5 JD |
195 | static const enum bt_notification_type notif_types[] = { |
196 | BT_NOTIFICATION_TYPE_PACKET_BEGIN, | |
197 | BT_NOTIFICATION_TYPE_PACKET_END, | |
198 | BT_NOTIFICATION_TYPE_SENTINEL, | |
199 | }; | |
c70ed359 | 200 | |
890882ef | 201 | writer = bt_private_component_get_user_data(component); |
c70ed359 JG |
202 | assert(writer); |
203 | assert(!writer->input_iterator); | |
890882ef | 204 | connection = bt_private_port_get_private_connection(self_port); |
72b913fb | 205 | assert(connection); |
fa054faf PP |
206 | writer->input_iterator = |
207 | bt_private_connection_create_notification_iterator(connection, | |
93fc16a5 | 208 | notif_types); |
c70ed359 JG |
209 | |
210 | if (!writer->input_iterator) { | |
0d8b4d8e | 211 | writer->error = true; |
c70ed359 | 212 | } |
0d8b4d8e | 213 | |
72b913fb | 214 | bt_put(connection); |
c70ed359 JG |
215 | } |
216 | ||
d8866baa PP |
217 | BT_HIDDEN |
218 | enum bt_component_status writer_run(struct bt_private_component *component) | |
bc506aa5 JD |
219 | { |
220 | enum bt_component_status ret; | |
221 | struct bt_notification *notification = NULL; | |
222 | struct bt_notification_iterator *it; | |
223 | struct writer_component *writer_component = | |
890882ef | 224 | bt_private_component_get_user_data(component); |
7cdc2bab | 225 | enum bt_notification_iterator_status it_ret; |
bc506aa5 | 226 | |
0d8b4d8e PP |
227 | if (unlikely(writer_component->error)) { |
228 | ret = BT_COMPONENT_STATUS_ERROR; | |
229 | goto end; | |
230 | } | |
231 | ||
7cdc2bab MD |
232 | it = writer_component->input_iterator; |
233 | assert(it); | |
234 | it_ret = bt_notification_iterator_next(it); | |
bc506aa5 | 235 | |
7cdc2bab MD |
236 | switch (it_ret) { |
237 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
238 | ret = BT_COMPONENT_STATUS_END; | |
239 | BT_PUT(writer_component->input_iterator); | |
240 | goto end; | |
241 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
242 | ret = BT_COMPONENT_STATUS_AGAIN; | |
243 | goto end; | |
244 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: | |
245 | break; | |
246 | default: | |
72b913fb | 247 | ret = BT_COMPONENT_STATUS_ERROR; |
996a07a1 JG |
248 | goto end; |
249 | } | |
250 | ||
7cdc2bab MD |
251 | notification = bt_notification_iterator_get_notification(it); |
252 | assert(notification); | |
bc506aa5 JD |
253 | ret = handle_notification(writer_component, notification); |
254 | end: | |
bc506aa5 JD |
255 | bt_put(notification); |
256 | return ret; | |
257 | } | |
258 | ||
d8866baa | 259 | BT_HIDDEN |
bc506aa5 | 260 | enum bt_component_status writer_component_init( |
890882ef | 261 | struct bt_private_component *component, struct bt_value *params, |
7d61fa8e | 262 | UNUSED_VAR void *init_method_data) |
bc506aa5 JD |
263 | { |
264 | enum bt_component_status ret; | |
265 | enum bt_value_status value_ret; | |
266 | struct writer_component *writer_component = create_writer_component(); | |
267 | struct bt_value *value = NULL; | |
268 | const char *path; | |
b9d103be | 269 | void *priv_port; |
bc506aa5 JD |
270 | |
271 | if (!writer_component) { | |
272 | ret = BT_COMPONENT_STATUS_NOMEM; | |
273 | goto end; | |
274 | } | |
275 | ||
b9d103be PP |
276 | priv_port = bt_private_component_sink_add_input_private_port(component, |
277 | "in", NULL); | |
278 | if (!priv_port) { | |
279 | ret = BT_COMPONENT_STATUS_NOMEM; | |
280 | goto end; | |
281 | } | |
282 | ||
283 | bt_put(priv_port); | |
284 | ||
bc506aa5 JD |
285 | value = bt_value_map_get(params, "path"); |
286 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { | |
287 | fprintf(writer_component->err, | |
288 | "[error] output path parameter required\n"); | |
289 | ret = BT_COMPONENT_STATUS_INVALID; | |
290 | goto error; | |
291 | } | |
292 | ||
293 | value_ret = bt_value_string_get(value, &path); | |
294 | if (value_ret != BT_VALUE_STATUS_OK) { | |
295 | ret = BT_COMPONENT_STATUS_INVALID; | |
296 | goto error; | |
297 | } | |
d00b90bf | 298 | bt_put(value); |
bc506aa5 | 299 | |
9057f037 JD |
300 | writer_component->base_path = g_string_new(path); |
301 | if (!writer_component) { | |
302 | ret = BT_COMPONENT_STATUS_ERROR; | |
303 | goto error; | |
304 | } | |
bc506aa5 | 305 | |
890882ef | 306 | ret = bt_private_component_set_user_data(component, writer_component); |
bc506aa5 JD |
307 | if (ret != BT_COMPONENT_STATUS_OK) { |
308 | goto error; | |
309 | } | |
310 | ||
bc506aa5 JD |
311 | end: |
312 | return ret; | |
313 | error: | |
314 | destroy_writer_component_data(writer_component); | |
56fa591a | 315 | g_free(writer_component); |
bc506aa5 JD |
316 | return ret; |
317 | } |