4 * Babeltrace CTF Writer Output Plugin Event Handling
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/event.h>
30 #include <babeltrace/ctf-ir/packet.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/clock-class.h>
35 #include <babeltrace/ctf-ir/fields.h>
36 #include <babeltrace/ctf-writer/stream-class.h>
37 #include <babeltrace/ctf-writer/stream.h>
39 #include <ctfcopytrace.h>
44 struct bt_ctf_stream_class
*insert_new_stream_class(
45 struct writer_component
*writer_component
,
46 struct bt_ctf_writer
*ctf_writer
,
47 struct bt_ctf_stream_class
*stream_class
)
49 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
50 struct bt_ctf_trace
*trace
= NULL
, *writer_trace
= NULL
;
51 enum bt_component_status ret
;
53 trace
= bt_ctf_stream_class_get_trace(stream_class
);
55 fprintf(writer_component
->err
,
56 "[error] %s in %s:%d\n", __func__
, __FILE__
,
61 writer_trace
= bt_ctf_writer_get_trace(ctf_writer
);
63 fprintf(writer_component
->err
,
64 "[error] %s in %s:%d\n", __func__
, __FILE__
,
69 ret
= ctf_copy_clock_classes(writer_component
->err
, writer_trace
,
70 writer_stream_class
, trace
);
71 if (ret
!= BT_COMPONENT_STATUS_OK
) {
72 fprintf(writer_component
->err
,
73 "[error] %s in %s:%d\n", __func__
, __FILE__
,
78 writer_stream_class
= ctf_copy_stream_class(writer_component
->err
,
79 stream_class
, writer_trace
, true);
80 if (!writer_stream_class
) {
81 fprintf(writer_component
->err
, "[error] Failed to copy stream class\n");
82 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
83 __func__
, __FILE__
, __LINE__
);
87 g_hash_table_insert(writer_component
->stream_class_map
,
88 (gpointer
) stream_class
, writer_stream_class
);
93 BT_PUT(writer_stream_class
);
97 return writer_stream_class
;
101 struct bt_ctf_stream
*insert_new_stream(
102 struct writer_component
*writer_component
,
103 struct bt_ctf_writer
*ctf_writer
,
104 struct bt_ctf_stream_class
*stream_class
,
105 struct bt_ctf_stream
*stream
)
107 struct bt_ctf_stream
*writer_stream
= NULL
;
108 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
110 writer_stream_class
= g_hash_table_lookup(
111 writer_component
->stream_class_map
,
112 (gpointer
) stream_class
);
113 if (!writer_stream_class
) {
114 writer_stream_class
= insert_new_stream_class(
115 writer_component
, ctf_writer
, stream_class
);
116 if (!writer_stream_class
) {
117 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
118 __func__
, __FILE__
, __LINE__
);
122 bt_get(writer_stream_class
);
124 writer_stream
= bt_ctf_writer_create_stream(ctf_writer
,
125 writer_stream_class
);
126 if (!writer_stream
) {
127 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
128 __func__
, __FILE__
, __LINE__
);
132 g_hash_table_insert(writer_component
->stream_map
, (gpointer
) stream
,
138 BT_PUT(writer_stream
);
140 bt_put(writer_stream_class
);
141 return writer_stream
;
145 struct bt_ctf_stream
*lookup_stream(struct writer_component
*writer_component
,
146 struct bt_ctf_stream
*stream
)
148 return (struct bt_ctf_stream
*) g_hash_table_lookup(
149 writer_component
->stream_map
,
154 struct bt_ctf_event_class
*get_event_class(struct writer_component
*writer_component
,
155 struct bt_ctf_stream_class
*writer_stream_class
,
156 struct bt_ctf_event_class
*event_class
)
158 return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class
,
159 bt_ctf_event_class_get_id(event_class
));
162 struct bt_ctf_writer
*insert_new_writer(
163 struct writer_component
*writer_component
,
164 struct bt_ctf_trace
*trace
)
166 struct bt_ctf_writer
*ctf_writer
= NULL
;
167 struct bt_ctf_trace
*writer_trace
= NULL
;
168 char trace_name
[PATH_MAX
];
169 enum bt_component_status ret
;
171 /* FIXME: replace with trace name when it will work. */
172 snprintf(trace_name
, PATH_MAX
, "%s/%s_%03d",
173 writer_component
->base_path
->str
,
174 writer_component
->trace_name_base
->str
,
175 writer_component
->trace_id
++);
176 printf_verbose("CTF-Writer creating trace in %s\n", trace_name
);
178 ctf_writer
= bt_ctf_writer_create(trace_name
);
180 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
181 __func__
, __FILE__
, __LINE__
);
185 writer_trace
= bt_ctf_writer_get_trace(ctf_writer
);
187 fprintf(writer_component
->err
,
188 "[error] %s in %s:%d\n", __func__
, __FILE__
,
193 ret
= ctf_copy_trace(writer_component
->err
, trace
, writer_trace
);
194 if (ret
!= BT_COMPONENT_STATUS_OK
) {
195 fprintf(writer_component
->err
, "[error] Failed to copy trace\n");
196 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
197 __func__
, __FILE__
, __LINE__
);
201 BT_PUT(writer_trace
);
203 g_hash_table_insert(writer_component
->trace_map
, (gpointer
) trace
,
209 bt_put(writer_trace
);
216 struct bt_ctf_writer
*get_writer(struct writer_component
*writer_component
,
217 struct bt_ctf_stream_class
*stream_class
)
219 struct bt_ctf_trace
*trace
= NULL
;
220 struct bt_ctf_writer
*ctf_writer
= NULL
;
222 trace
= bt_ctf_stream_class_get_trace(stream_class
);
225 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
226 __func__
, __FILE__
, __LINE__
);
230 ctf_writer
= g_hash_table_lookup(writer_component
->trace_map
,
233 ctf_writer
= insert_new_writer(writer_component
, trace
);
246 struct bt_ctf_stream
*get_writer_stream(
247 struct writer_component
*writer_component
,
248 struct bt_ctf_packet
*packet
, struct bt_ctf_stream
*stream
)
250 struct bt_ctf_stream_class
*stream_class
= NULL
;
251 struct bt_ctf_writer
*ctf_writer
= NULL
;
252 struct bt_ctf_stream
*writer_stream
= NULL
;
254 stream_class
= bt_ctf_stream_get_class(stream
);
256 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
257 __func__
, __FILE__
, __LINE__
);
261 ctf_writer
= get_writer(writer_component
, stream_class
);
263 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
264 __func__
, __FILE__
, __LINE__
);
268 writer_stream
= lookup_stream(writer_component
, stream
);
269 if (!writer_stream
) {
270 writer_stream
= insert_new_stream(writer_component
, ctf_writer
,
271 stream_class
, stream
);
273 bt_get(writer_stream
);
278 BT_PUT(writer_stream
);
281 bt_put(stream_class
);
282 return writer_stream
;
286 enum bt_component_status
writer_new_packet(
287 struct writer_component
*writer_component
,
288 struct bt_ctf_packet
*packet
)
290 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
291 struct bt_ctf_field
*writer_packet_context
= NULL
;
292 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
295 stream
= bt_ctf_packet_get_stream(packet
);
297 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
298 __func__
, __FILE__
, __LINE__
);
302 writer_stream
= get_writer_stream(writer_component
, packet
, stream
);
303 if (!writer_stream
) {
304 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
305 __func__
, __FILE__
, __LINE__
);
310 writer_packet_context
= ctf_copy_packet_context(writer_component
->err
,
311 packet
, writer_stream
);
312 if (!writer_packet_context
) {
313 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
314 __func__
, __FILE__
, __LINE__
);
318 int_ret
= bt_ctf_stream_set_packet_context(writer_stream
,
319 writer_packet_context
);
321 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
325 BT_PUT(writer_stream
);
326 BT_PUT(writer_packet_context
);
331 ret
= BT_COMPONENT_STATUS_ERROR
;
333 bt_put(writer_stream
);
334 bt_put(writer_packet_context
);
340 enum bt_component_status
writer_close_packet(
341 struct writer_component
*writer_component
,
342 struct bt_ctf_packet
*packet
)
344 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
345 enum bt_component_status ret
;
347 stream
= bt_ctf_packet_get_stream(packet
);
349 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
350 __func__
, __FILE__
, __LINE__
);
354 writer_stream
= lookup_stream(writer_component
, stream
);
355 if (!writer_stream
) {
356 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
357 __func__
, __FILE__
, __LINE__
);
362 bt_get(writer_stream
);
364 ret
= bt_ctf_stream_flush(writer_stream
);
366 fprintf(writer_component
->err
,
367 "[error] Failed to flush packet\n");
370 BT_PUT(writer_stream
);
372 ret
= BT_COMPONENT_STATUS_OK
;
376 ret
= BT_COMPONENT_STATUS_ERROR
;
378 bt_put(writer_stream
);
384 enum bt_component_status
writer_output_event(
385 struct writer_component
*writer_component
,
386 struct bt_ctf_event
*event
)
388 enum bt_component_status ret
;
389 struct bt_ctf_event_class
*event_class
= NULL
, *writer_event_class
= NULL
;
390 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
391 struct bt_ctf_stream_class
*stream_class
= NULL
, *writer_stream_class
= NULL
;
392 struct bt_ctf_event
*writer_event
= NULL
;
393 const char *event_name
;
396 event_class
= bt_ctf_event_get_class(event
);
398 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
403 event_name
= bt_ctf_event_class_get_name(event_class
);
405 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
410 stream
= bt_ctf_event_get_stream(event
);
412 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
417 writer_stream
= lookup_stream(writer_component
, stream
);
418 if (!writer_stream
|| !bt_get(writer_stream
)) {
419 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
424 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
426 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
431 writer_stream_class
= g_hash_table_lookup(
432 writer_component
->stream_class_map
,
433 (gpointer
) stream_class
);
434 if (!writer_stream_class
|| !bt_get(writer_stream_class
)) {
435 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
440 writer_event_class
= get_event_class(writer_component
,
441 writer_stream_class
, event_class
);
442 if (!writer_event_class
) {
443 writer_event_class
= ctf_copy_event_class(writer_component
->err
,
445 if (!writer_event_class
) {
446 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
447 __func__
, __FILE__
, __LINE__
);
450 int_ret
= bt_ctf_stream_class_add_event_class(
451 writer_stream_class
, writer_event_class
);
453 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
454 __func__
, __FILE__
, __LINE__
);
459 writer_event
= ctf_copy_event(writer_component
->err
, event
,
460 writer_event_class
, true);
462 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
464 fprintf(writer_component
->err
, "[error] Failed to copy event %s\n",
465 bt_ctf_event_class_get_name(writer_event_class
));
469 int_ret
= bt_ctf_stream_append_event(writer_stream
, writer_event
);
471 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
473 fprintf(writer_component
->err
, "[error] Failed to append event %s\n",
474 bt_ctf_event_class_get_name(writer_event_class
));
478 ret
= BT_COMPONENT_STATUS_OK
;
482 ret
= BT_COMPONENT_STATUS_ERROR
;
484 bt_put(writer_event
);
485 bt_put(writer_event_class
);
486 bt_put(writer_stream_class
);
487 bt_put(stream_class
);
488 bt_put(writer_stream
);