4 * Babeltrace CTF IR - Stream Class
6 * Copyright 2013, 2014 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 "STREAM-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-writer/clock.h>
33 #include <babeltrace/ctf-writer/clock-internal.h>
34 #include <babeltrace/ctf-ir/clock-class-internal.h>
35 #include <babeltrace/ctf-writer/event.h>
36 #include <babeltrace/ctf-ir/event-class-internal.h>
37 #include <babeltrace/ctf-ir/event-internal.h>
38 #include <babeltrace/ctf-ir/field-types-internal.h>
39 #include <babeltrace/ctf-ir/fields-internal.h>
40 #include <babeltrace/ctf-writer/stream.h>
41 #include <babeltrace/ctf-ir/stream-class-internal.h>
42 #include <babeltrace/ctf-ir/validation-internal.h>
43 #include <babeltrace/ctf-ir/visitor-internal.h>
44 #include <babeltrace/ctf-writer/functor-internal.h>
45 #include <babeltrace/ctf-ir/utils.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/compiler-internal.h>
48 #include <babeltrace/align-internal.h>
49 #include <babeltrace/endian-internal.h>
55 void bt_stream_class_destroy(struct bt_object
*obj
);
57 int init_event_header(struct bt_stream_class
*stream_class
);
59 int init_packet_context(struct bt_stream_class
*stream_class
);
61 struct bt_stream_class
*bt_stream_class_create(const char *name
)
63 struct bt_stream_class
*stream_class
;
66 BT_LOGD("Creating default stream class object: name=\"%s\"", name
);
67 stream_class
= bt_stream_class_create_empty(name
);
69 BT_LOGD_STR("Cannot create empty stream class.");
73 ret
= init_event_header(stream_class
);
75 BT_LOGE_STR("Cannot initialize stream class's event header field type.");
79 ret
= init_packet_context(stream_class
);
81 BT_LOGE_STR("Cannot initialize stream class's packet context field type.");
85 BT_LOGD("Created default stream class object: addr=%p, name=\"%s\"",
94 struct bt_stream_class
*bt_stream_class_create_empty(const char *name
)
96 struct bt_stream_class
*stream_class
= NULL
;
98 BT_LOGD("Creating empty stream class object: name=\"%s\"", name
);
100 stream_class
= g_new0(struct bt_stream_class
, 1);
102 BT_LOGE_STR("Failed to allocate one stream class.");
106 stream_class
->name
= g_string_new(name
);
107 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
108 (GDestroyNotify
) bt_object_release
);
109 if (!stream_class
->event_classes
) {
110 BT_LOGE_STR("Failed to allocate a GPtrArray.");
114 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
115 g_int64_equal
, g_free
, NULL
);
116 if (!stream_class
->event_classes_ht
) {
117 BT_LOGE_STR("Failed to allocate a GHashTable.");
121 bt_object_init(stream_class
, bt_stream_class_destroy
);
122 BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
127 BT_PUT(stream_class
);
131 struct bt_trace
*bt_stream_class_get_trace(
132 struct bt_stream_class
*stream_class
)
134 return stream_class
?
135 bt_get(bt_stream_class_borrow_trace(stream_class
)) :
139 const char *bt_stream_class_get_name(
140 struct bt_stream_class
*stream_class
)
142 const char *name
= NULL
;
145 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
149 name
= stream_class
->name
->len
> 0 ? stream_class
->name
->str
: NULL
;
154 int bt_stream_class_set_name(struct bt_stream_class
*stream_class
,
160 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
165 if (stream_class
->frozen
) {
166 BT_LOGW("Invalid parameter: stream class is frozen: "
167 "addr=%p, name=\"%s\", id=%" PRId64
,
168 stream_class
, bt_stream_class_get_name(stream_class
),
169 bt_stream_class_get_id(stream_class
));
175 g_string_assign(stream_class
->name
, "");
177 if (strlen(name
) == 0) {
178 BT_LOGW("Invalid parameter: name is empty.");
183 g_string_assign(stream_class
->name
, name
);
186 BT_LOGV("Set stream class's name: "
187 "addr=%p, name=\"%s\", id=%" PRId64
,
188 stream_class
, bt_stream_class_get_name(stream_class
),
189 bt_stream_class_get_id(stream_class
));
194 struct bt_ctf_clock
*bt_stream_class_get_clock(
195 struct bt_stream_class
*stream_class
)
197 struct bt_ctf_clock
*clock
= NULL
;
200 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
204 if (!stream_class
->clock
) {
205 BT_LOGV("Stream class has no clock: "
206 "addr=%p, name=\"%s\", id=%" PRId64
,
207 stream_class
, bt_stream_class_get_name(stream_class
),
208 bt_stream_class_get_id(stream_class
));
212 clock
= bt_get(stream_class
->clock
);
217 int bt_stream_class_set_clock(struct bt_stream_class
*stream_class
,
218 struct bt_ctf_clock
*clock
)
221 struct bt_field_type
*timestamp_field
= NULL
;
223 if (!stream_class
|| !clock
) {
224 BT_LOGW("Invalid parameter: stream class or clock is NULL: "
225 "stream-class-addr=%p, clock-addr=%p",
226 stream_class
, clock
);
231 if (stream_class
->frozen
) {
232 BT_LOGW("Invalid parameter: stream class is frozen: "
233 "addr=%p, name=\"%s\", id=%" PRId64
,
234 stream_class
, bt_stream_class_get_name(stream_class
),
235 bt_stream_class_get_id(stream_class
));
240 /* Replace the current clock of this stream class. */
241 bt_put(stream_class
->clock
);
242 stream_class
->clock
= bt_get(clock
);
243 BT_LOGV("Set stream class's clock: "
244 "addr=%p, name=\"%s\", id=%" PRId64
", "
245 "clock-addr=%p, clock-name=\"%s\"",
246 stream_class
, bt_stream_class_get_name(stream_class
),
247 bt_stream_class_get_id(stream_class
),
249 bt_ctf_clock_get_name(stream_class
->clock
));
252 bt_put(timestamp_field
);
256 int64_t bt_stream_class_get_id(struct bt_stream_class
*stream_class
)
261 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
266 if (!stream_class
->id_set
) {
267 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
269 bt_stream_class_get_name(stream_class
));
274 ret
= stream_class
->id
;
280 void _bt_stream_class_set_id(
281 struct bt_stream_class
*stream_class
, int64_t id
)
283 assert(stream_class
);
284 stream_class
->id
= id
;
285 stream_class
->id_set
= 1;
286 BT_LOGV("Set stream class's ID (internal): "
287 "addr=%p, name=\"%s\", id=%" PRId64
,
288 stream_class
, bt_stream_class_get_name(stream_class
),
289 bt_stream_class_get_id(stream_class
));
292 struct event_class_set_stream_class_id_data
{
293 int64_t stream_class_id
;
298 int bt_stream_class_set_id_no_check(
299 struct bt_stream_class
*stream_class
, int64_t id
)
301 _bt_stream_class_set_id(stream_class
, id
);
305 int bt_stream_class_set_id(struct bt_stream_class
*stream_class
,
309 int64_t id
= (int64_t) id_param
;
312 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
317 if (stream_class
->frozen
) {
318 BT_LOGW("Invalid parameter: stream class is frozen: "
319 "addr=%p, name=\"%s\", id=%" PRId64
,
320 stream_class
, bt_stream_class_get_name(stream_class
),
321 bt_stream_class_get_id(stream_class
));
327 BT_LOGW("Invalid parameter: invalid stream class's ID: "
328 "stream-class-addr=%p, stream-class-name=\"%s\", "
329 "stream-class-id=%" PRId64
", id=%" PRIu64
,
330 stream_class
, bt_stream_class_get_name(stream_class
),
331 bt_stream_class_get_id(stream_class
),
337 ret
= bt_stream_class_set_id_no_check(stream_class
, id
);
339 BT_LOGV("Set stream class's ID: "
340 "addr=%p, name=\"%s\", id=%" PRId64
,
341 stream_class
, bt_stream_class_get_name(stream_class
),
342 bt_stream_class_get_id(stream_class
));
349 void event_class_exists(gpointer element
, gpointer query
)
351 struct bt_event_class
*event_class_a
= element
;
352 struct search_query
*search_query
= query
;
353 struct bt_event_class
*event_class_b
= search_query
->value
;
356 if (search_query
->value
== element
) {
357 search_query
->found
= 1;
362 * Two event classes cannot share the same ID in a given
365 id_a
= bt_event_class_get_id(event_class_a
);
366 id_b
= bt_event_class_get_id(event_class_b
);
368 if (id_a
< 0 || id_b
< 0) {
369 /* at least one ID is not set: will be automatically set later */
374 BT_LOGW("Event class with this ID already exists in the stream class: "
375 "id=%" PRId64
", name=\"%s\"",
376 id_a
, bt_event_class_get_name(event_class_a
));
377 search_query
->found
= 1;
385 int bt_stream_class_add_event_class(
386 struct bt_stream_class
*stream_class
,
387 struct bt_event_class
*event_class
)
390 int64_t *event_id
= NULL
;
391 struct bt_trace
*trace
= NULL
;
392 struct bt_stream_class
*old_stream_class
= NULL
;
393 struct bt_validation_output validation_output
= { 0 };
394 struct bt_field_type
*packet_header_type
= NULL
;
395 struct bt_field_type
*packet_context_type
= NULL
;
396 struct bt_field_type
*event_header_type
= NULL
;
397 struct bt_field_type
*stream_event_ctx_type
= NULL
;
398 struct bt_field_type
*event_context_type
= NULL
;
399 struct bt_field_type
*event_payload_type
= NULL
;
400 const enum bt_validation_flag validation_flags
=
401 BT_VALIDATION_FLAG_EVENT
;
403 if (!stream_class
|| !event_class
) {
404 BT_LOGW("Invalid parameter: stream class or event class is NULL: "
405 "stream-class-addr=%p, event-class-addr=%p",
406 stream_class
, event_class
);
411 BT_LOGD("Adding event class to stream class: "
412 "stream-class-addr=%p, stream-class-name=\"%s\", "
413 "stream-class-id=%" PRId64
", event-class-addr=%p, "
414 "event-class-name=\"%s\", event-class-id=%" PRId64
,
415 stream_class
, bt_stream_class_get_name(stream_class
),
416 bt_stream_class_get_id(stream_class
),
418 bt_event_class_get_name(event_class
),
419 bt_event_class_get_id(event_class
));
421 trace
= bt_stream_class_get_trace(stream_class
);
422 if (trace
&& trace
->is_static
) {
427 event_id
= g_new(int64_t, 1);
429 BT_LOGE_STR("Failed to allocate one int64_t.");
434 /* Check for duplicate event classes */
435 struct search_query query
= { .value
= event_class
, .found
= 0 };
436 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
439 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
444 old_stream_class
= bt_event_class_get_stream_class(event_class
);
445 if (old_stream_class
) {
446 /* Event class is already associated to a stream class. */
447 BT_LOGW("Event class is already part of another stream class: "
448 "event-class-stream-class-addr=%p, "
449 "event-class-stream-class-name=\"%s\", "
450 "event-class-stream-class-id=%" PRId64
,
452 bt_stream_class_get_name(old_stream_class
),
453 bt_stream_class_get_id(old_stream_class
));
460 * If the stream class is associated with a trace, then
461 * both those objects are frozen. Also, this event class
462 * is about to be frozen.
464 * Therefore the event class must be validated here.
465 * The trace and stream class should be valid at this
468 assert(trace
->valid
);
469 assert(stream_class
->valid
);
471 bt_trace_get_packet_header_type(trace
);
472 packet_context_type
=
473 bt_stream_class_get_packet_context_type(
476 bt_stream_class_get_event_header_type(stream_class
);
477 stream_event_ctx_type
=
478 bt_stream_class_get_event_context_type(
481 bt_event_class_get_context_type(event_class
);
483 bt_event_class_get_payload_type(event_class
);
484 ret
= bt_validate_class_types(
485 trace
->environment
, packet_header_type
,
486 packet_context_type
, event_header_type
,
487 stream_event_ctx_type
, event_context_type
,
488 event_payload_type
, trace
->valid
,
489 stream_class
->valid
, event_class
->valid
,
490 &validation_output
, validation_flags
);
491 BT_PUT(packet_header_type
);
492 BT_PUT(packet_context_type
);
493 BT_PUT(event_header_type
);
494 BT_PUT(stream_event_ctx_type
);
495 BT_PUT(event_context_type
);
496 BT_PUT(event_payload_type
);
500 * This means something went wrong during the
501 * validation process, not that the objects are
504 BT_LOGE("Failed to validate event class: ret=%d", ret
);
508 if ((validation_output
.valid_flags
& validation_flags
) !=
510 /* Invalid event class */
511 BT_LOGW("Invalid trace, stream class, or event class: "
513 validation_output
.valid_flags
);
519 /* Only set an event ID if none was explicitly set before */
520 *event_id
= bt_event_class_get_id(event_class
);
522 BT_LOGV("Event class has no ID: automatically setting it: "
523 "id=%" PRId64
, stream_class
->next_event_id
);
525 if (bt_event_class_set_id(event_class
,
526 stream_class
->next_event_id
)) {
527 BT_LOGE("Cannot set event class's ID: id=%" PRId64
,
528 stream_class
->next_event_id
);
532 stream_class
->next_event_id
++;
533 *event_id
= stream_class
->next_event_id
;
536 bt_object_set_parent(event_class
, stream_class
);
540 * At this point we know that the function will be
541 * successful. Therefore we can replace the event
542 * class's field types with what's in the validation
543 * output structure and mark this event class as valid.
545 bt_validation_replace_types(NULL
, NULL
, event_class
,
546 &validation_output
, validation_flags
);
547 event_class
->valid
= 1;
550 * Put what was not moved in
551 * bt_validation_replace_types().
553 bt_validation_output_put_types(&validation_output
);
556 /* Add to the event classes of the stream class */
557 g_ptr_array_add(stream_class
->event_classes
, event_class
);
558 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
562 /* Freeze the event class */
563 bt_event_class_freeze(event_class
);
565 /* Notifiy listeners of the trace's schema modification. */
567 struct bt_visitor_object obj
= { .object
= event_class
,
568 .type
= BT_VISITOR_OBJECT_TYPE_EVENT_CLASS
};
570 (void) bt_trace_object_modification(&obj
, trace
);
573 BT_LOGD("Added event class to stream class: "
574 "stream-class-addr=%p, stream-class-name=\"%s\", "
575 "stream-class-id=%" PRId64
", event-class-addr=%p, "
576 "event-class-name=\"%s\", event-class-id=%" PRId64
,
577 stream_class
, bt_stream_class_get_name(stream_class
),
578 bt_stream_class_get_id(stream_class
),
580 bt_event_class_get_name(event_class
),
581 bt_event_class_get_id(event_class
));
585 BT_PUT(old_stream_class
);
586 bt_validation_output_put_types(&validation_output
);
587 assert(!packet_header_type
);
588 assert(!packet_context_type
);
589 assert(!event_header_type
);
590 assert(!stream_event_ctx_type
);
591 assert(!event_context_type
);
592 assert(!event_payload_type
);
598 int64_t bt_stream_class_get_event_class_count(
599 struct bt_stream_class
*stream_class
)
604 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
609 ret
= (int64_t) stream_class
->event_classes
->len
;
614 struct bt_event_class
*bt_stream_class_get_event_class_by_index(
615 struct bt_stream_class
*stream_class
, uint64_t index
)
617 struct bt_event_class
*event_class
= NULL
;
620 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
624 if (index
>= stream_class
->event_classes
->len
) {
625 BT_LOGW("Invalid parameter: index is out of bounds: "
626 "addr=%p, name=\"%s\", id=%" PRId64
", "
627 "index=%" PRIu64
", count=%u",
628 stream_class
, bt_stream_class_get_name(stream_class
),
629 bt_stream_class_get_id(stream_class
),
630 index
, stream_class
->event_classes
->len
);
634 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
640 struct bt_event_class
*bt_stream_class_get_event_class_by_id(
641 struct bt_stream_class
*stream_class
, uint64_t id
)
643 int64_t id_key
= (int64_t) id
;
644 struct bt_event_class
*event_class
= NULL
;
647 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
652 BT_LOGW("Invalid parameter: invalid event class's ID: "
653 "stream-class-addr=%p, stream-class-name=\"%s\", "
654 "stream-class-id=%" PRId64
", event-class-id=%" PRIu64
,
656 bt_stream_class_get_name(stream_class
),
657 bt_stream_class_get_id(stream_class
), id
);
661 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
668 struct bt_field_type
*bt_stream_class_get_packet_context_type(
669 struct bt_stream_class
*stream_class
)
671 struct bt_field_type
*ret
= NULL
;
674 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
678 bt_get(stream_class
->packet_context_type
);
679 ret
= stream_class
->packet_context_type
;
684 int bt_stream_class_set_packet_context_type(
685 struct bt_stream_class
*stream_class
,
686 struct bt_field_type
*packet_context_type
)
691 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
696 if (stream_class
->frozen
) {
697 BT_LOGW("Invalid parameter: stream class is frozen: "
698 "addr=%p, name=\"%s\", id=%" PRId64
,
699 stream_class
, bt_stream_class_get_name(stream_class
),
700 bt_stream_class_get_id(stream_class
));
705 if (packet_context_type
&&
706 bt_field_type_get_type_id(packet_context_type
) !=
707 BT_FIELD_TYPE_ID_STRUCT
) {
708 /* A packet context must be a structure. */
709 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
710 "addr=%p, name=\"%s\", id=%" PRId64
", "
711 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
712 stream_class
, bt_stream_class_get_name(stream_class
),
713 bt_stream_class_get_id(stream_class
),
715 bt_field_type_id_string(
716 bt_field_type_get_type_id(packet_context_type
)));
721 bt_put(stream_class
->packet_context_type
);
722 bt_get(packet_context_type
);
723 stream_class
->packet_context_type
= packet_context_type
;
724 BT_LOGV("Set stream class's packet context field type: "
725 "addr=%p, name=\"%s\", id=%" PRId64
", "
726 "packet-context-ft-addr=%p",
727 stream_class
, bt_stream_class_get_name(stream_class
),
728 bt_stream_class_get_id(stream_class
),
729 packet_context_type
);
735 struct bt_field_type
*bt_stream_class_get_event_header_type(
736 struct bt_stream_class
*stream_class
)
738 struct bt_field_type
*ret
= NULL
;
741 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
745 if (!stream_class
->event_header_type
) {
746 BT_LOGV("Stream class has no event header field type: "
747 "addr=%p, name=\"%s\", id=%" PRId64
,
748 stream_class
, bt_stream_class_get_name(stream_class
),
749 bt_stream_class_get_id(stream_class
));
753 bt_get(stream_class
->event_header_type
);
754 ret
= stream_class
->event_header_type
;
759 int bt_stream_class_set_event_header_type(
760 struct bt_stream_class
*stream_class
,
761 struct bt_field_type
*event_header_type
)
766 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
771 if (stream_class
->frozen
) {
772 BT_LOGW("Invalid parameter: stream class is frozen: "
773 "addr=%p, name=\"%s\", id=%" PRId64
,
774 stream_class
, bt_stream_class_get_name(stream_class
),
775 bt_stream_class_get_id(stream_class
));
780 if (event_header_type
&&
781 bt_field_type_get_type_id(event_header_type
) !=
782 BT_FIELD_TYPE_ID_STRUCT
) {
783 /* An event header must be a structure. */
784 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
785 "addr=%p, name=\"%s\", id=%" PRId64
", "
786 "event-header-ft-addr=%p, event-header-ft-id=%s",
787 stream_class
, bt_stream_class_get_name(stream_class
),
788 bt_stream_class_get_id(stream_class
),
790 bt_field_type_id_string(
791 bt_field_type_get_type_id(event_header_type
)));
796 bt_put(stream_class
->event_header_type
);
797 stream_class
->event_header_type
= bt_get(event_header_type
);
798 BT_LOGV("Set stream class's event header field type: "
799 "addr=%p, name=\"%s\", id=%" PRId64
", "
800 "event-header-ft-addr=%p",
801 stream_class
, bt_stream_class_get_name(stream_class
),
802 bt_stream_class_get_id(stream_class
),
808 struct bt_field_type
*bt_stream_class_get_event_context_type(
809 struct bt_stream_class
*stream_class
)
811 struct bt_field_type
*ret
= NULL
;
814 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
818 if (!stream_class
->event_context_type
) {
822 bt_get(stream_class
->event_context_type
);
823 ret
= stream_class
->event_context_type
;
828 int bt_stream_class_set_event_context_type(
829 struct bt_stream_class
*stream_class
,
830 struct bt_field_type
*event_context_type
)
835 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
840 if (stream_class
->frozen
) {
841 BT_LOGW("Invalid parameter: stream class is frozen: "
842 "addr=%p, name=\"%s\", id=%" PRId64
,
843 stream_class
, bt_stream_class_get_name(stream_class
),
844 bt_stream_class_get_id(stream_class
));
849 if (event_context_type
&&
850 bt_field_type_get_type_id(event_context_type
) !=
851 BT_FIELD_TYPE_ID_STRUCT
) {
852 /* A packet context must be a structure. */
853 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
854 "addr=%p, name=\"%s\", id=%" PRId64
", "
855 "event-context-ft-addr=%p, event-context-ft-id=%s",
856 stream_class
, bt_stream_class_get_name(stream_class
),
857 bt_stream_class_get_id(stream_class
),
859 bt_field_type_id_string(
860 bt_field_type_get_type_id(event_context_type
)));
865 bt_put(stream_class
->event_context_type
);
866 stream_class
->event_context_type
= bt_get(event_context_type
);
867 BT_LOGV("Set stream class's event context field type: "
868 "addr=%p, name=\"%s\", id=%" PRId64
", "
869 "event-context-ft-addr=%p",
870 stream_class
, bt_stream_class_get_name(stream_class
),
871 bt_stream_class_get_id(stream_class
),
877 /* Pre-2.0 CTF writer backward compatibility */
878 void bt_ctf_stream_class_get(struct bt_stream_class
*stream_class
)
880 bt_get(stream_class
);
883 /* Pre-2.0 CTF writer backward compatibility */
884 void bt_ctf_stream_class_put(struct bt_stream_class
*stream_class
)
886 bt_put(stream_class
);
890 int64_t get_event_class_count(void *element
)
892 return bt_stream_class_get_event_class_count(
893 (struct bt_stream_class
*) element
);
897 void *get_event_class(void *element
, int i
)
899 return bt_stream_class_get_event_class_by_index(
900 (struct bt_stream_class
*) element
, i
);
904 int visit_event_class(void *object
, bt_visitor visitor
,void *data
)
906 struct bt_visitor_object obj
=
908 .type
= BT_VISITOR_OBJECT_TYPE_EVENT_CLASS
};
910 return visitor(&obj
, data
);
913 int bt_stream_class_visit(struct bt_stream_class
*stream_class
,
914 bt_visitor visitor
, void *data
)
917 struct bt_visitor_object obj
=
918 { .object
= stream_class
,
919 .type
= BT_VISITOR_OBJECT_TYPE_STREAM_CLASS
};
921 if (!stream_class
|| !visitor
) {
922 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
923 "stream-class-addr=%p, visitor=%p",
924 stream_class
, visitor
);
929 ret
= visitor_helper(&obj
, get_event_class_count
,
931 visit_event_class
, visitor
, data
);
932 BT_LOGV("visitor_helper() returned: ret=%d", ret
);
938 void bt_stream_class_freeze(struct bt_stream_class
*stream_class
)
940 if (!stream_class
|| stream_class
->frozen
) {
944 BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64
,
945 stream_class
, bt_stream_class_get_name(stream_class
),
946 bt_stream_class_get_id(stream_class
));
947 stream_class
->frozen
= 1;
948 bt_field_type_freeze(stream_class
->event_header_type
);
949 bt_field_type_freeze(stream_class
->packet_context_type
);
950 bt_field_type_freeze(stream_class
->event_context_type
);
952 if (stream_class
->clock
) {
953 bt_clock_class_freeze(stream_class
->clock
->clock_class
);
958 int bt_stream_class_serialize(struct bt_stream_class
*stream_class
,
959 struct metadata_context
*context
)
963 struct bt_trace
*trace
;
964 struct bt_field_type
*packet_header_type
= NULL
;
966 BT_LOGD("Serializing stream class's metadata: "
967 "stream-class-addr=%p, stream-class-name=\"%s\", "
968 "stream-class-id=%" PRId64
", metadata-context-addr=%p",
969 stream_class
, bt_stream_class_get_name(stream_class
),
970 bt_stream_class_get_id(stream_class
), context
);
971 g_string_assign(context
->field_name
, "");
972 context
->current_indentation_level
= 1;
973 if (!stream_class
->id_set
) {
974 BT_LOGW_STR("Stream class's ID is not set.");
979 g_string_append(context
->string
, "stream {\n");
982 * The reference to the trace is only borrowed since the
983 * serialization of the stream class might have been triggered
984 * by the trace's destruction. In such a case, the trace's
985 * reference count would, unexepectedly, go through the sequence
986 * 1 -> 0 -> 1 -> 0 -> ..., provoking an endless loop of destruction
989 trace
= bt_stream_class_borrow_trace(stream_class
);
991 packet_header_type
= bt_trace_get_packet_header_type(trace
);
993 if (packet_header_type
) {
994 struct bt_field_type
*stream_id_type
;
997 bt_field_type_structure_get_field_type_by_name(
998 packet_header_type
, "stream_id");
999 if (stream_id_type
) {
1001 * Only set the stream's id if the trace's packet header
1002 * contains a stream_id field. This field is only
1003 * needed if the trace contains only one stream
1006 g_string_append_printf(context
->string
,
1007 "\tid = %" PRId64
";\n", stream_class
->id
);
1009 bt_put(stream_id_type
);
1011 if (stream_class
->event_header_type
) {
1012 BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
1013 g_string_append(context
->string
, "\tevent.header := ");
1014 ret
= bt_field_type_serialize(stream_class
->event_header_type
,
1017 BT_LOGW("Cannot serialize stream class's event header field type's metadata: "
1021 g_string_append(context
->string
, ";");
1025 if (stream_class
->packet_context_type
) {
1026 BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
1027 g_string_append(context
->string
, "\n\n\tpacket.context := ");
1028 ret
= bt_field_type_serialize(stream_class
->packet_context_type
,
1031 BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
1035 g_string_append(context
->string
, ";");
1038 if (stream_class
->event_context_type
) {
1039 BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
1040 g_string_append(context
->string
, "\n\n\tevent.context := ");
1041 ret
= bt_field_type_serialize(
1042 stream_class
->event_context_type
, context
);
1044 BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
1048 g_string_append(context
->string
, ";");
1051 g_string_append(context
->string
, "\n};\n\n");
1053 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
1054 struct bt_event_class
*event_class
=
1055 stream_class
->event_classes
->pdata
[i
];
1057 ret
= bt_event_class_serialize(event_class
, context
);
1059 BT_LOGW("Cannot serialize event class's metadata: "
1060 "event-class-addr=%p, event-class-name=\"%s\", "
1061 "event-class-id=%" PRId64
,
1063 bt_event_class_get_name(event_class
),
1064 bt_event_class_get_id(event_class
));
1069 bt_put(packet_header_type
);
1070 context
->current_indentation_level
= 0;
1075 void bt_stream_class_destroy(struct bt_object
*obj
)
1077 struct bt_stream_class
*stream_class
;
1079 stream_class
= container_of(obj
, struct bt_stream_class
, base
);
1080 BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64
,
1081 stream_class
, bt_stream_class_get_name(stream_class
),
1082 bt_stream_class_get_id(stream_class
));
1083 bt_put(stream_class
->clock
);
1085 if (stream_class
->event_classes_ht
) {
1086 g_hash_table_destroy(stream_class
->event_classes_ht
);
1088 if (stream_class
->event_classes
) {
1089 BT_LOGD_STR("Destroying event classes.");
1090 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
1093 if (stream_class
->name
) {
1094 g_string_free(stream_class
->name
, TRUE
);
1097 BT_LOGD_STR("Putting event header field type.");
1098 bt_put(stream_class
->event_header_type
);
1099 BT_LOGD_STR("Putting packet context field type.");
1100 bt_put(stream_class
->packet_context_type
);
1101 BT_LOGD_STR("Putting event context field type.");
1102 bt_put(stream_class
->event_context_type
);
1103 g_free(stream_class
);
1107 int init_event_header(struct bt_stream_class
*stream_class
)
1110 struct bt_field_type
*event_header_type
=
1111 bt_field_type_structure_create();
1112 struct bt_field_type
*_uint32_t
=
1113 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1114 struct bt_field_type
*_uint64_t
=
1115 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
1117 if (!event_header_type
) {
1118 BT_LOGE_STR("Cannot create empty structure field type.");
1123 ret
= bt_field_type_structure_add_field(event_header_type
,
1126 BT_LOGE_STR("Cannot add `id` field to event header field type.");
1130 ret
= bt_field_type_structure_add_field(event_header_type
,
1131 _uint64_t
, "timestamp");
1133 BT_LOGE_STR("Cannot add `timestamp` field to event header field type.");
1137 BT_MOVE(stream_class
->event_header_type
, event_header_type
);
1140 bt_put(event_header_type
);
1149 int init_packet_context(struct bt_stream_class
*stream_class
)
1152 struct bt_field_type
*packet_context_type
=
1153 bt_field_type_structure_create();
1154 struct bt_field_type
*_uint64_t
=
1155 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
1156 struct bt_field_type
*ts_begin_end_uint64_t
;
1158 if (!packet_context_type
) {
1159 BT_LOGE_STR("Cannot create empty structure field type.");
1164 ts_begin_end_uint64_t
= bt_field_type_copy(_uint64_t
);
1165 if (!ts_begin_end_uint64_t
) {
1166 BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields.");
1172 * We create a stream packet context as proposed in the CTF
1175 ret
= bt_field_type_structure_add_field(packet_context_type
,
1176 ts_begin_end_uint64_t
, "timestamp_begin");
1178 BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
1182 ret
= bt_field_type_structure_add_field(packet_context_type
,
1183 ts_begin_end_uint64_t
, "timestamp_end");
1185 BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
1189 ret
= bt_field_type_structure_add_field(packet_context_type
,
1190 _uint64_t
, "content_size");
1192 BT_LOGE_STR("Cannot add `content_size` field to event header field type.");
1196 ret
= bt_field_type_structure_add_field(packet_context_type
,
1197 _uint64_t
, "packet_size");
1199 BT_LOGE_STR("Cannot add `packet_size` field to event header field type.");
1203 ret
= bt_field_type_structure_add_field(packet_context_type
,
1204 _uint64_t
, "events_discarded");
1206 BT_LOGE_STR("Cannot add `events_discarded` field to event header field type.");
1210 BT_MOVE(stream_class
->packet_context_type
, packet_context_type
);
1213 bt_put(packet_context_type
);
1218 bt_put(ts_begin_end_uint64_t
);
1223 int try_map_clock_class(struct bt_stream_class
*stream_class
,
1224 struct bt_field_type
*ft
)
1226 struct bt_clock_class
*mapped_clock_class
= NULL
;
1230 /* Field does not exist: not an error */
1234 assert(bt_field_type_is_integer(ft
));
1235 mapped_clock_class
=
1236 bt_field_type_integer_get_mapped_clock_class(ft
);
1237 if (!mapped_clock_class
) {
1238 if (!stream_class
->clock
) {
1239 BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
1240 "stream-class-addr=%p, stream-class-name=\"%s\", "
1241 "stream-class-id=%" PRId64
", ft-addr=%p",
1242 stream_class
, bt_stream_class_get_name(stream_class
),
1243 bt_stream_class_get_id(stream_class
), ft
);
1248 ret
= bt_field_type_integer_set_mapped_clock_class_no_check(
1249 ft
, stream_class
->clock
->clock_class
);
1251 BT_LOGW("Cannot set field type's mapped clock class: "
1252 "stream-class-addr=%p, stream-class-name=\"%s\", "
1253 "stream-class-id=%" PRId64
", ft-addr=%p",
1254 stream_class
, bt_stream_class_get_name(stream_class
),
1255 bt_stream_class_get_id(stream_class
), ft
);
1259 BT_LOGV("Automatically mapped field type to stream class's clock class: "
1260 "stream-class-addr=%p, stream-class-name=\"%s\", "
1261 "stream-class-id=%" PRId64
", ft-addr=%p",
1262 stream_class
, bt_stream_class_get_name(stream_class
),
1263 bt_stream_class_get_id(stream_class
), ft
);
1267 bt_put(mapped_clock_class
);
1272 int bt_stream_class_map_clock_class(
1273 struct bt_stream_class
*stream_class
,
1274 struct bt_field_type
*packet_context_type
,
1275 struct bt_field_type
*event_header_type
)
1277 struct bt_field_type
*ft
= NULL
;
1280 assert(stream_class
);
1282 if (packet_context_type
) {
1283 ft
= bt_field_type_structure_get_field_type_by_name(
1284 packet_context_type
, "timestamp_begin");
1285 if (try_map_clock_class(stream_class
, ft
)) {
1286 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
1292 ft
= bt_field_type_structure_get_field_type_by_name(
1293 packet_context_type
, "timestamp_end");
1294 if (try_map_clock_class(stream_class
, ft
)) {
1295 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
1303 if (event_header_type
) {
1304 ft
= bt_field_type_structure_get_field_type_by_name(
1305 event_header_type
, "timestamp");
1306 if (try_map_clock_class(stream_class
, ft
)) {
1307 BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");