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>
40 #include <ctfcopytrace.h>
45 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
51 struct bt_ctf_stream_class
*insert_new_stream_class(
52 struct writer_component
*writer_component
,
53 struct bt_ctf_writer
*ctf_writer
,
54 struct bt_ctf_stream_class
*stream_class
)
56 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
57 struct bt_ctf_trace
*trace
= NULL
, *writer_trace
= NULL
;
58 enum bt_component_status ret
;
60 trace
= bt_ctf_stream_class_get_trace(stream_class
);
62 fprintf(writer_component
->err
,
63 "[error] %s in %s:%d\n", __func__
, __FILE__
,
68 writer_trace
= bt_ctf_writer_get_trace(ctf_writer
);
70 fprintf(writer_component
->err
,
71 "[error] %s in %s:%d\n", __func__
, __FILE__
,
76 ret
= ctf_copy_clock_classes(writer_component
->err
, writer_trace
,
77 writer_stream_class
, trace
);
78 if (ret
!= BT_COMPONENT_STATUS_OK
) {
79 fprintf(writer_component
->err
,
80 "[error] %s in %s:%d\n", __func__
, __FILE__
,
85 writer_stream_class
= ctf_copy_stream_class(writer_component
->err
,
86 stream_class
, writer_trace
, true);
87 if (!writer_stream_class
) {
88 fprintf(writer_component
->err
, "[error] Failed to copy stream class\n");
89 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
90 __func__
, __FILE__
, __LINE__
);
94 g_hash_table_insert(writer_component
->stream_class_map
,
95 (gpointer
) stream_class
, writer_stream_class
);
100 BT_PUT(writer_stream_class
);
102 bt_put(writer_trace
);
104 return writer_stream_class
;
108 struct bt_ctf_stream
*insert_new_stream(
109 struct writer_component
*writer_component
,
110 struct bt_ctf_writer
*ctf_writer
,
111 struct bt_ctf_stream_class
*stream_class
,
112 struct bt_ctf_stream
*stream
)
114 struct bt_ctf_stream
*writer_stream
= NULL
;
115 struct bt_ctf_stream_class
*writer_stream_class
= NULL
;
117 writer_stream_class
= g_hash_table_lookup(
118 writer_component
->stream_class_map
,
119 (gpointer
) stream_class
);
120 if (!writer_stream_class
) {
121 writer_stream_class
= insert_new_stream_class(
122 writer_component
, ctf_writer
, stream_class
);
123 if (!writer_stream_class
) {
124 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
125 __func__
, __FILE__
, __LINE__
);
129 bt_get(writer_stream_class
);
131 writer_stream
= bt_ctf_writer_create_stream(ctf_writer
,
132 writer_stream_class
);
133 if (!writer_stream
) {
134 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
135 __func__
, __FILE__
, __LINE__
);
139 g_hash_table_insert(writer_component
->stream_map
, (gpointer
) stream
,
145 BT_PUT(writer_stream
);
147 bt_put(writer_stream_class
);
148 return writer_stream
;
152 struct bt_ctf_stream
*lookup_stream(struct writer_component
*writer_component
,
153 struct bt_ctf_stream
*stream
)
155 return (struct bt_ctf_stream
*) g_hash_table_lookup(
156 writer_component
->stream_map
,
161 struct bt_ctf_event_class
*get_event_class(struct writer_component
*writer_component
,
162 struct bt_ctf_stream_class
*writer_stream_class
,
163 struct bt_ctf_event_class
*event_class
)
165 return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class
,
166 bt_ctf_event_class_get_id(event_class
));
169 struct fs_writer
*insert_new_writer(
170 struct writer_component
*writer_component
,
171 struct bt_ctf_trace
*trace
)
173 struct bt_ctf_writer
*ctf_writer
= NULL
;
174 struct bt_ctf_trace
*writer_trace
= NULL
;
175 char trace_name
[PATH_MAX
];
176 enum bt_component_status ret
;
177 struct fs_writer
*fs_writer
;
179 /* FIXME: replace with trace name when it will work. */
180 snprintf(trace_name
, PATH_MAX
, "%s/%s_%03d",
181 writer_component
->base_path
->str
,
182 writer_component
->trace_name_base
->str
,
183 writer_component
->trace_id
++);
184 printf_verbose("CTF-Writer creating trace in %s\n", trace_name
);
186 ctf_writer
= bt_ctf_writer_create(trace_name
);
188 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
189 __func__
, __FILE__
, __LINE__
);
193 writer_trace
= bt_ctf_writer_get_trace(ctf_writer
);
195 fprintf(writer_component
->err
,
196 "[error] %s in %s:%d\n", __func__
, __FILE__
,
201 ret
= ctf_copy_trace(writer_component
->err
, trace
, writer_trace
);
202 if (ret
!= BT_COMPONENT_STATUS_OK
) {
203 fprintf(writer_component
->err
, "[error] Failed to copy trace\n");
204 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
205 __func__
, __FILE__
, __LINE__
);
210 fs_writer
= g_new0(struct fs_writer
, 1);
212 fprintf(writer_component
->err
,
213 "[error] %s in %s:%d\n", __func__
, __FILE__
,
217 fs_writer
->writer
= ctf_writer
;
218 fs_writer
->writer_trace
= writer_trace
;
219 BT_PUT(writer_trace
);
220 if (bt_ctf_trace_is_static(trace
)) {
221 fs_writer
->trace_static
= 1;
222 fs_writer
->static_listener_id
= -1;
224 ret
= bt_ctf_trace_add_is_static_listener(trace
,
225 trace_is_static_listener
, &fs_writer
->trace_static
);
227 fprintf(writer_component
->err
,
228 "[error] %s in %s:%d\n", __func__
, __FILE__
,
234 fs_writer
->static_listener_id
= ret
;
237 g_hash_table_insert(writer_component
->trace_map
, (gpointer
) trace
,
243 bt_put(writer_trace
);
250 struct fs_writer
*get_fs_writer(struct writer_component
*writer_component
,
251 struct bt_ctf_stream_class
*stream_class
)
253 struct bt_ctf_trace
*trace
= NULL
;
254 struct fs_writer
*fs_writer
;
256 trace
= bt_ctf_stream_class_get_trace(stream_class
);
258 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
259 __func__
, __FILE__
, __LINE__
);
263 fs_writer
= g_hash_table_lookup(writer_component
->trace_map
,
266 fs_writer
= insert_new_writer(writer_component
, trace
);
278 struct bt_ctf_stream
*get_writer_stream(
279 struct writer_component
*writer_component
,
280 struct bt_ctf_packet
*packet
, struct bt_ctf_stream
*stream
)
282 struct bt_ctf_stream_class
*stream_class
= NULL
;
283 struct bt_ctf_writer
*ctf_writer
= NULL
;
284 struct bt_ctf_stream
*writer_stream
= NULL
;
286 stream_class
= bt_ctf_stream_get_class(stream
);
288 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
289 __func__
, __FILE__
, __LINE__
);
293 writer_stream
= lookup_stream(writer_component
, stream
);
294 if (!writer_stream
) {
295 struct fs_writer
*fs_writer
;
297 fs_writer
= get_fs_writer(writer_component
, stream_class
);
299 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
300 __func__
, __FILE__
, __LINE__
);
303 ctf_writer
= bt_get(fs_writer
->writer
);
304 writer_stream
= insert_new_stream(writer_component
, ctf_writer
,
305 stream_class
, stream
);
306 if (!writer_stream
) {
307 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
308 __func__
, __FILE__
, __LINE__
);
311 fs_writer
->active_streams
++;
313 bt_get(writer_stream
);
318 BT_PUT(writer_stream
);
321 bt_put(stream_class
);
322 return writer_stream
;
326 enum bt_component_status
writer_close(
327 struct writer_component
*writer_component
,
328 struct fs_writer
*fs_writer
,
329 struct bt_ctf_trace
*trace
)
331 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
333 if (fs_writer
->static_listener_id
> 0) {
334 bt_ctf_trace_remove_is_static_listener(trace
,
335 fs_writer
->static_listener_id
);
337 g_hash_table_remove(writer_component
->trace_map
, trace
);
342 enum bt_component_status
writer_stream_end(
343 struct writer_component
*writer_component
,
344 struct bt_ctf_stream
*stream
)
346 struct bt_ctf_stream_class
*stream_class
= NULL
;
347 struct fs_writer
*fs_writer
;
348 struct bt_ctf_trace
*trace
= NULL
;
349 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
351 g_hash_table_remove(writer_component
->stream_map
, stream
);
353 stream_class
= bt_ctf_stream_get_class(stream
);
355 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
356 __func__
, __FILE__
, __LINE__
);
360 fs_writer
= get_fs_writer(writer_component
, stream_class
);
362 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
363 __func__
, __FILE__
, __LINE__
);
367 assert(fs_writer
->active_streams
> 0);
368 fs_writer
->active_streams
--;
369 if (fs_writer
->active_streams
== 0 && fs_writer
->trace_static
) {
370 trace
= bt_ctf_stream_class_get_trace(stream_class
);
372 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
373 __func__
, __FILE__
, __LINE__
);
376 ret
= writer_close(writer_component
, fs_writer
, trace
);
382 ret
= BT_COMPONENT_STATUS_ERROR
;
385 BT_PUT(stream_class
);
390 enum bt_component_status
writer_new_packet(
391 struct writer_component
*writer_component
,
392 struct bt_ctf_packet
*packet
)
394 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
395 struct bt_ctf_field
*writer_packet_context
= NULL
;
396 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
399 stream
= bt_ctf_packet_get_stream(packet
);
401 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
402 __func__
, __FILE__
, __LINE__
);
406 writer_stream
= get_writer_stream(writer_component
, packet
, stream
);
407 if (!writer_stream
) {
408 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
409 __func__
, __FILE__
, __LINE__
);
414 writer_packet_context
= ctf_copy_packet_context(writer_component
->err
,
415 packet
, writer_stream
, 1);
416 if (!writer_packet_context
) {
417 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
418 __func__
, __FILE__
, __LINE__
);
422 int_ret
= bt_ctf_stream_set_packet_context(writer_stream
,
423 writer_packet_context
);
425 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
429 BT_PUT(writer_stream
);
430 BT_PUT(writer_packet_context
);
435 ret
= BT_COMPONENT_STATUS_ERROR
;
437 bt_put(writer_stream
);
438 bt_put(writer_packet_context
);
444 enum bt_component_status
writer_close_packet(
445 struct writer_component
*writer_component
,
446 struct bt_ctf_packet
*packet
)
448 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
449 enum bt_component_status ret
;
451 stream
= bt_ctf_packet_get_stream(packet
);
453 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
454 __func__
, __FILE__
, __LINE__
);
458 writer_stream
= lookup_stream(writer_component
, stream
);
459 if (!writer_stream
) {
460 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
461 __func__
, __FILE__
, __LINE__
);
466 bt_get(writer_stream
);
468 ret
= bt_ctf_stream_flush(writer_stream
);
470 fprintf(writer_component
->err
,
471 "[error] Failed to flush packet\n");
474 BT_PUT(writer_stream
);
476 ret
= BT_COMPONENT_STATUS_OK
;
480 ret
= BT_COMPONENT_STATUS_ERROR
;
482 bt_put(writer_stream
);
488 enum bt_component_status
writer_output_event(
489 struct writer_component
*writer_component
,
490 struct bt_ctf_event
*event
)
492 enum bt_component_status ret
;
493 struct bt_ctf_event_class
*event_class
= NULL
, *writer_event_class
= NULL
;
494 struct bt_ctf_stream
*stream
= NULL
, *writer_stream
= NULL
;
495 struct bt_ctf_stream_class
*stream_class
= NULL
, *writer_stream_class
= NULL
;
496 struct bt_ctf_event
*writer_event
= NULL
;
497 const char *event_name
;
500 event_class
= bt_ctf_event_get_class(event
);
502 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
507 event_name
= bt_ctf_event_class_get_name(event_class
);
509 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
514 stream
= bt_ctf_event_get_stream(event
);
516 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
521 writer_stream
= lookup_stream(writer_component
, stream
);
522 if (!writer_stream
|| !bt_get(writer_stream
)) {
523 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
528 stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
530 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
535 writer_stream_class
= g_hash_table_lookup(
536 writer_component
->stream_class_map
,
537 (gpointer
) stream_class
);
538 if (!writer_stream_class
|| !bt_get(writer_stream_class
)) {
539 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
544 writer_event_class
= get_event_class(writer_component
,
545 writer_stream_class
, event_class
);
546 if (!writer_event_class
) {
547 writer_event_class
= ctf_copy_event_class(writer_component
->err
,
549 if (!writer_event_class
) {
550 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
551 __func__
, __FILE__
, __LINE__
);
554 int_ret
= bt_ctf_stream_class_add_event_class(
555 writer_stream_class
, writer_event_class
);
557 fprintf(writer_component
->err
, "[error] %s in %s:%d\n",
558 __func__
, __FILE__
, __LINE__
);
563 writer_event
= ctf_copy_event(writer_component
->err
, event
,
564 writer_event_class
, true);
566 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
568 fprintf(writer_component
->err
, "[error] Failed to copy event %s\n",
569 bt_ctf_event_class_get_name(writer_event_class
));
573 int_ret
= bt_ctf_stream_append_event(writer_stream
, writer_event
);
575 fprintf(writer_component
->err
, "[error] %s in %s:%d\n", __func__
,
577 fprintf(writer_component
->err
, "[error] Failed to append event %s\n",
578 bt_ctf_event_class_get_name(writer_event_class
));
582 ret
= BT_COMPONENT_STATUS_OK
;
586 ret
= BT_COMPONENT_STATUS_ERROR
;
588 bt_put(writer_event
);
589 bt_put(writer_event_class
);
590 bt_put(writer_stream_class
);
591 bt_put(stream_class
);
592 bt_put(writer_stream
);