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_ctf_stream_class_destroy(struct bt_object
*obj
);
57 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
59 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
61 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
63 struct bt_ctf_stream_class
*stream_class
;
66 BT_LOGD("Creating default stream class object: name=\"%s\"", name
);
67 stream_class
= bt_ctf_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_ctf_stream_class
*bt_ctf_stream_class_create_empty(const char *name
)
96 struct bt_ctf_stream_class
*stream_class
= NULL
;
98 BT_LOGD("Creating empty stream class object: name=\"%s\"", name
);
100 if (name
&& bt_ctf_validate_identifier(name
)) {
101 BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: "
102 "name=\"%s\"", name
);
106 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
108 BT_LOGE_STR("Failed to allocate one stream class.");
112 stream_class
->name
= g_string_new(name
);
113 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
114 (GDestroyNotify
) bt_object_release
);
115 if (!stream_class
->event_classes
) {
116 BT_LOGE_STR("Failed to allocate a GPtrArray.");
120 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
121 g_int64_equal
, g_free
, NULL
);
122 if (!stream_class
->event_classes_ht
) {
123 BT_LOGE_STR("Failed to allocate a GHashTable.");
127 stream_class
->packet_context_type
= bt_ctf_field_type_structure_create();
128 if (!stream_class
->packet_context_type
) {
129 BT_LOGE_STR("Cannot create stream class's initial packet context field type.");
133 stream_class
->event_header_type
= bt_ctf_field_type_structure_create();
134 if (!stream_class
->event_header_type
) {
135 BT_LOGE_STR("Cannot create stream class's initial event header field type.");
139 stream_class
->event_context_type
= bt_ctf_field_type_structure_create();
140 if (!stream_class
->event_context_type
) {
141 BT_LOGE_STR("Cannot create stream class's initial event context field type.");
145 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
146 BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"",
151 BT_PUT(stream_class
);
155 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
156 struct bt_ctf_stream_class
*stream_class
)
158 return stream_class
?
159 bt_get(bt_ctf_stream_class_borrow_trace(stream_class
)) :
163 const char *bt_ctf_stream_class_get_name(
164 struct bt_ctf_stream_class
*stream_class
)
166 const char *name
= NULL
;
169 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
173 name
= stream_class
->name
->str
;
178 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
184 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
189 if (stream_class
->frozen
) {
190 BT_LOGW("Invalid parameter: stream class is frozen: "
191 "addr=%p, name=\"%s\", id=%" PRId64
,
192 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
193 bt_ctf_stream_class_get_id(stream_class
));
198 g_string_assign(stream_class
->name
, name
);
199 BT_LOGV("Set stream class's name: "
200 "addr=%p, name=\"%s\", id=%" PRId64
,
201 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
202 bt_ctf_stream_class_get_id(stream_class
));
207 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
208 struct bt_ctf_stream_class
*stream_class
)
210 struct bt_ctf_clock
*clock
= NULL
;
213 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
217 if (!stream_class
->clock
) {
218 BT_LOGV("Stream class has no clock: "
219 "addr=%p, name=\"%s\", id=%" PRId64
,
220 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
221 bt_ctf_stream_class_get_id(stream_class
));
225 clock
= bt_get(stream_class
->clock
);
230 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
231 struct bt_ctf_clock
*clock
)
234 struct bt_ctf_field_type
*timestamp_field
= NULL
;
236 if (!stream_class
|| !clock
) {
237 BT_LOGW("Invalid parameter: stream class or clock is NULL: "
238 "stream-class-addr=%p, clock-addr=%p",
239 stream_class
, clock
);
244 if (stream_class
->frozen
) {
245 BT_LOGW("Invalid parameter: stream class is frozen: "
246 "addr=%p, name=\"%s\", id=%" PRId64
,
247 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
248 bt_ctf_stream_class_get_id(stream_class
));
253 /* Replace the current clock of this stream class. */
254 bt_put(stream_class
->clock
);
255 stream_class
->clock
= bt_get(clock
);
256 BT_LOGV("Set stream class's clock: "
257 "addr=%p, name=\"%s\", id=%" PRId64
", "
258 "clock-addr=%p, clock-name=\"%s\"",
259 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
260 bt_ctf_stream_class_get_id(stream_class
),
262 bt_ctf_clock_get_name(stream_class
->clock
));
265 bt_put(timestamp_field
);
269 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
274 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
279 if (!stream_class
->id_set
) {
280 BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"",
282 bt_ctf_stream_class_get_name(stream_class
));
287 ret
= stream_class
->id
;
293 void _bt_ctf_stream_class_set_id(
294 struct bt_ctf_stream_class
*stream_class
, int64_t id
)
296 assert(stream_class
);
297 stream_class
->id
= id
;
298 stream_class
->id_set
= 1;
299 BT_LOGV("Set stream class's ID (internal): "
300 "addr=%p, name=\"%s\", id=%" PRId64
,
301 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
302 bt_ctf_stream_class_get_id(stream_class
));
305 struct event_class_set_stream_class_id_data
{
306 int64_t stream_class_id
;
311 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
313 struct event_class_set_stream_class_id_data
*typed_data
= data
;
315 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
316 typed_data
->stream_class_id
);
320 int bt_ctf_stream_class_set_id_no_check(
321 struct bt_ctf_stream_class
*stream_class
, int64_t id
)
324 struct event_class_set_stream_class_id_data data
=
325 { .stream_class_id
= id
, .ret
= 0 };
328 * Make sure all event classes have their "stream_id" attribute
331 g_ptr_array_foreach(stream_class
->event_classes
,
332 event_class_set_stream_id
, &data
);
335 BT_LOGE("Cannot set the IDs of all stream class's event classes: "
336 "addr=%p, name=\"%s\", id=%" PRId64
,
337 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
338 bt_ctf_stream_class_get_id(stream_class
));
342 _bt_ctf_stream_class_set_id(stream_class
, id
);
347 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
351 int64_t id
= (int64_t) id_param
;
354 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
359 if (stream_class
->frozen
) {
360 BT_LOGW("Invalid parameter: stream class is frozen: "
361 "addr=%p, name=\"%s\", id=%" PRId64
,
362 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
363 bt_ctf_stream_class_get_id(stream_class
));
369 BT_LOGW("Invalid parameter: invalid stream class's ID: "
370 "stream-class-addr=%p, stream-class-name=\"%s\", "
371 "stream-class-id=%" PRId64
", id=%" PRIu64
,
372 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
373 bt_ctf_stream_class_get_id(stream_class
),
379 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
381 BT_LOGV("Set stream class's ID: "
382 "addr=%p, name=\"%s\", id=%" PRId64
,
383 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
384 bt_ctf_stream_class_get_id(stream_class
));
391 void event_class_exists(gpointer element
, gpointer query
)
393 struct bt_ctf_event_class
*event_class_a
= element
;
394 struct search_query
*search_query
= query
;
395 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
398 if (search_query
->value
== element
) {
399 search_query
->found
= 1;
404 * Two event classes cannot share the same ID in a given
407 id_a
= bt_ctf_event_class_get_id(event_class_a
);
408 id_b
= bt_ctf_event_class_get_id(event_class_b
);
410 if (id_a
< 0 || id_b
< 0) {
411 /* at least one ID is not set: will be automatically set later */
416 BT_LOGW("Event class with this ID already exists in the stream class: "
417 "id=%" PRId64
", name=\"%s\"",
418 id_a
, bt_ctf_event_class_get_name(event_class_a
));
419 search_query
->found
= 1;
427 int bt_ctf_stream_class_add_event_class(
428 struct bt_ctf_stream_class
*stream_class
,
429 struct bt_ctf_event_class
*event_class
)
432 int64_t *event_id
= NULL
;
433 struct bt_ctf_trace
*trace
= NULL
;
434 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
435 struct bt_ctf_validation_output validation_output
= { 0 };
436 struct bt_ctf_field_type
*packet_header_type
= NULL
;
437 struct bt_ctf_field_type
*packet_context_type
= NULL
;
438 struct bt_ctf_field_type
*event_header_type
= NULL
;
439 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
440 struct bt_ctf_field_type
*event_context_type
= NULL
;
441 struct bt_ctf_field_type
*event_payload_type
= NULL
;
442 const enum bt_ctf_validation_flag validation_flags
=
443 BT_CTF_VALIDATION_FLAG_EVENT
;
445 if (!stream_class
|| !event_class
) {
446 BT_LOGW("Invalid parameter: stream class or event class is NULL: "
447 "stream-class-addr=%p, event-class-addr=%p",
448 stream_class
, event_class
);
453 BT_LOGD("Adding event class to stream class: "
454 "stream-class-addr=%p, stream-class-name=\"%s\", "
455 "stream-class-id=%" PRId64
", event-class-addr=%p, "
456 "event-class-name=\"%s\", event-class-id=%" PRId64
,
457 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
458 bt_ctf_stream_class_get_id(stream_class
),
460 bt_ctf_event_class_get_name(event_class
),
461 bt_ctf_event_class_get_id(event_class
));
463 trace
= bt_ctf_stream_class_get_trace(stream_class
);
464 if (trace
&& trace
->is_static
) {
469 event_id
= g_new(int64_t, 1);
471 BT_LOGE_STR("Failed to allocate one int64_t.");
476 /* Check for duplicate event classes */
477 struct search_query query
= { .value
= event_class
, .found
= 0 };
478 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
481 BT_LOGW_STR("Another event class part of this stream class has the same ID.");
486 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
487 if (old_stream_class
) {
488 /* Event class is already associated to a stream class. */
489 BT_LOGW("Event class is already part of another stream class: "
490 "event-class-stream-class-addr=%p, "
491 "event-class-stream-class-name=\"%s\", "
492 "event-class-stream-class-id=%" PRId64
,
494 bt_ctf_stream_class_get_name(old_stream_class
),
495 bt_ctf_stream_class_get_id(old_stream_class
));
502 * If the stream class is associated with a trace, then
503 * both those objects are frozen. Also, this event class
504 * is about to be frozen.
506 * Therefore the event class must be validated here.
507 * The trace and stream class should be valid at this
510 assert(trace
->valid
);
511 assert(stream_class
->valid
);
513 bt_ctf_trace_get_packet_header_type(trace
);
514 packet_context_type
=
515 bt_ctf_stream_class_get_packet_context_type(
518 bt_ctf_stream_class_get_event_header_type(stream_class
);
519 stream_event_ctx_type
=
520 bt_ctf_stream_class_get_event_context_type(
523 bt_ctf_event_class_get_context_type(event_class
);
525 bt_ctf_event_class_get_payload_type(event_class
);
526 ret
= bt_ctf_validate_class_types(
527 trace
->environment
, packet_header_type
,
528 packet_context_type
, event_header_type
,
529 stream_event_ctx_type
, event_context_type
,
530 event_payload_type
, trace
->valid
,
531 stream_class
->valid
, event_class
->valid
,
532 &validation_output
, validation_flags
);
533 BT_PUT(packet_header_type
);
534 BT_PUT(packet_context_type
);
535 BT_PUT(event_header_type
);
536 BT_PUT(stream_event_ctx_type
);
537 BT_PUT(event_context_type
);
538 BT_PUT(event_payload_type
);
542 * This means something went wrong during the
543 * validation process, not that the objects are
546 BT_LOGE("Failed to validate event class: ret=%d", ret
);
550 if ((validation_output
.valid_flags
& validation_flags
) !=
552 /* Invalid event class */
553 BT_LOGW("Invalid trace, stream class, or event class: "
555 validation_output
.valid_flags
);
561 /* Only set an event ID if none was explicitly set before */
562 *event_id
= bt_ctf_event_class_get_id(event_class
);
564 BT_LOGV("Event class has no ID: automatically setting it: "
565 "id=%" PRId64
, stream_class
->next_event_id
);
567 if (bt_ctf_event_class_set_id(event_class
,
568 stream_class
->next_event_id
)) {
569 BT_LOGE("Cannot set event class's ID: id=%" PRId64
,
570 stream_class
->next_event_id
);
574 stream_class
->next_event_id
++;
575 *event_id
= stream_class
->next_event_id
;
578 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
580 BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d",
585 bt_object_set_parent(event_class
, stream_class
);
589 * At this point we know that the function will be
590 * successful. Therefore we can replace the event
591 * class's field types with what's in the validation
592 * output structure and mark this event class as valid.
594 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
595 &validation_output
, validation_flags
);
596 event_class
->valid
= 1;
599 * Put what was not moved in
600 * bt_ctf_validation_replace_types().
602 bt_ctf_validation_output_put_types(&validation_output
);
605 /* Add to the event classes of the stream class */
606 g_ptr_array_add(stream_class
->event_classes
, event_class
);
607 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
611 /* Freeze the event class */
612 bt_ctf_event_class_freeze(event_class
);
614 /* Notifiy listeners of the trace's schema modification. */
616 struct bt_ctf_object obj
= { .object
= event_class
,
617 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
619 (void) bt_ctf_trace_object_modification(&obj
, trace
);
622 BT_LOGD("Added event class to stream class: "
623 "stream-class-addr=%p, stream-class-name=\"%s\", "
624 "stream-class-id=%" PRId64
", event-class-addr=%p, "
625 "event-class-name=\"%s\", event-class-id=%" PRId64
,
626 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
627 bt_ctf_stream_class_get_id(stream_class
),
629 bt_ctf_event_class_get_name(event_class
),
630 bt_ctf_event_class_get_id(event_class
));
634 BT_PUT(old_stream_class
);
635 bt_ctf_validation_output_put_types(&validation_output
);
636 assert(!packet_header_type
);
637 assert(!packet_context_type
);
638 assert(!event_header_type
);
639 assert(!stream_event_ctx_type
);
640 assert(!event_context_type
);
641 assert(!event_payload_type
);
647 int64_t bt_ctf_stream_class_get_event_class_count(
648 struct bt_ctf_stream_class
*stream_class
)
653 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
658 ret
= (int64_t) stream_class
->event_classes
->len
;
663 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_index(
664 struct bt_ctf_stream_class
*stream_class
, uint64_t index
)
666 struct bt_ctf_event_class
*event_class
= NULL
;
669 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
673 if (index
>= stream_class
->event_classes
->len
) {
674 BT_LOGW("Invalid parameter: index is out of bounds: "
675 "addr=%p, name=\"%s\", id=%" PRId64
", "
676 "index=%" PRIu64
", count=%u",
677 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
678 bt_ctf_stream_class_get_id(stream_class
),
679 index
, stream_class
->event_classes
->len
);
683 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
689 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
690 struct bt_ctf_stream_class
*stream_class
, uint64_t id
)
692 int64_t id_key
= (int64_t) id
;
693 struct bt_ctf_event_class
*event_class
= NULL
;
696 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
701 BT_LOGW("Invalid parameter: invalid event class's ID: "
702 "stream-class-addr=%p, stream-class-name=\"%s\", "
703 "stream-class-id=%" PRId64
", event-class-id=%" PRIu64
,
705 bt_ctf_stream_class_get_name(stream_class
),
706 bt_ctf_stream_class_get_id(stream_class
), id
);
710 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
717 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
718 struct bt_ctf_stream_class
*stream_class
)
720 struct bt_ctf_field_type
*ret
= NULL
;
723 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
727 bt_get(stream_class
->packet_context_type
);
728 ret
= stream_class
->packet_context_type
;
733 int bt_ctf_stream_class_set_packet_context_type(
734 struct bt_ctf_stream_class
*stream_class
,
735 struct bt_ctf_field_type
*packet_context_type
)
740 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
745 if (stream_class
->frozen
) {
746 BT_LOGW("Invalid parameter: stream class is frozen: "
747 "addr=%p, name=\"%s\", id=%" PRId64
,
748 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
749 bt_ctf_stream_class_get_id(stream_class
));
754 if (packet_context_type
&&
755 bt_ctf_field_type_get_type_id(packet_context_type
) !=
756 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
757 /* A packet context must be a structure. */
758 BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: "
759 "addr=%p, name=\"%s\", id=%" PRId64
", "
760 "packet-context-ft-addr=%p, packet-context-ft-id=%s",
761 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
762 bt_ctf_stream_class_get_id(stream_class
),
764 bt_ctf_field_type_id_string(
765 bt_ctf_field_type_get_type_id(packet_context_type
)));
770 bt_put(stream_class
->packet_context_type
);
771 bt_get(packet_context_type
);
772 stream_class
->packet_context_type
= packet_context_type
;
773 BT_LOGV("Set stream class's packet context field type: "
774 "addr=%p, name=\"%s\", id=%" PRId64
", "
775 "packet-context-ft-addr=%p",
776 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
777 bt_ctf_stream_class_get_id(stream_class
),
778 packet_context_type
);
784 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
785 struct bt_ctf_stream_class
*stream_class
)
787 struct bt_ctf_field_type
*ret
= NULL
;
790 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
794 if (!stream_class
->event_header_type
) {
795 BT_LOGV("Stream class has no event header field type: "
796 "addr=%p, name=\"%s\", id=%" PRId64
,
797 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
798 bt_ctf_stream_class_get_id(stream_class
));
802 bt_get(stream_class
->event_header_type
);
803 ret
= stream_class
->event_header_type
;
808 int bt_ctf_stream_class_set_event_header_type(
809 struct bt_ctf_stream_class
*stream_class
,
810 struct bt_ctf_field_type
*event_header_type
)
815 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
820 if (stream_class
->frozen
) {
821 BT_LOGW("Invalid parameter: stream class is frozen: "
822 "addr=%p, name=\"%s\", id=%" PRId64
,
823 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
824 bt_ctf_stream_class_get_id(stream_class
));
829 if (event_header_type
&&
830 bt_ctf_field_type_get_type_id(event_header_type
) !=
831 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
832 /* An event header must be a structure. */
833 BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: "
834 "addr=%p, name=\"%s\", id=%" PRId64
", "
835 "event-header-ft-addr=%p, event-header-ft-id=%s",
836 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
837 bt_ctf_stream_class_get_id(stream_class
),
839 bt_ctf_field_type_id_string(
840 bt_ctf_field_type_get_type_id(event_header_type
)));
845 bt_put(stream_class
->event_header_type
);
846 stream_class
->event_header_type
= bt_get(event_header_type
);
847 BT_LOGV("Set stream class's event header field type: "
848 "addr=%p, name=\"%s\", id=%" PRId64
", "
849 "event-header-ft-addr=%p",
850 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
851 bt_ctf_stream_class_get_id(stream_class
),
857 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
858 struct bt_ctf_stream_class
*stream_class
)
860 struct bt_ctf_field_type
*ret
= NULL
;
863 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
867 if (!stream_class
->event_context_type
) {
871 bt_get(stream_class
->event_context_type
);
872 ret
= stream_class
->event_context_type
;
877 int bt_ctf_stream_class_set_event_context_type(
878 struct bt_ctf_stream_class
*stream_class
,
879 struct bt_ctf_field_type
*event_context_type
)
884 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
889 if (stream_class
->frozen
) {
890 BT_LOGW("Invalid parameter: stream class is frozen: "
891 "addr=%p, name=\"%s\", id=%" PRId64
,
892 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
893 bt_ctf_stream_class_get_id(stream_class
));
898 if (event_context_type
&&
899 bt_ctf_field_type_get_type_id(event_context_type
) !=
900 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
901 /* A packet context must be a structure. */
902 BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: "
903 "addr=%p, name=\"%s\", id=%" PRId64
", "
904 "event-context-ft-addr=%p, event-context-ft-id=%s",
905 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
906 bt_ctf_stream_class_get_id(stream_class
),
908 bt_ctf_field_type_id_string(
909 bt_ctf_field_type_get_type_id(event_context_type
)));
914 bt_put(stream_class
->event_context_type
);
915 stream_class
->event_context_type
= bt_get(event_context_type
);
916 BT_LOGV("Set stream class's event context field type: "
917 "addr=%p, name=\"%s\", id=%" PRId64
", "
918 "event-context-ft-addr=%p",
919 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
920 bt_ctf_stream_class_get_id(stream_class
),
926 /* Pre-2.0 CTF writer backward compatibility */
927 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
929 bt_get(stream_class
);
932 /* Pre-2.0 CTF writer backward compatibility */
933 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
935 bt_put(stream_class
);
939 int64_t get_event_class_count(void *element
)
941 return bt_ctf_stream_class_get_event_class_count(
942 (struct bt_ctf_stream_class
*) element
);
946 void *get_event_class(void *element
, int i
)
948 return bt_ctf_stream_class_get_event_class_by_index(
949 (struct bt_ctf_stream_class
*) element
, i
);
953 int visit_event_class(void *object
, bt_ctf_visitor visitor
,void *data
)
955 struct bt_ctf_object obj
=
957 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
959 return visitor(&obj
, data
);
962 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
963 bt_ctf_visitor visitor
, void *data
)
966 struct bt_ctf_object obj
=
967 { .object
= stream_class
,
968 .type
= BT_CTF_OBJECT_TYPE_STREAM_CLASS
};
970 if (!stream_class
|| !visitor
) {
971 BT_LOGW("Invalid parameter: stream class or visitor is NULL: "
972 "stream-class-addr=%p, visitor=%p",
973 stream_class
, visitor
);
978 ret
= visitor_helper(&obj
, get_event_class_count
,
980 visit_event_class
, visitor
, data
);
981 BT_LOGV("visitor_helper() returned: ret=%d", ret
);
987 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
989 if (!stream_class
|| stream_class
->frozen
) {
993 BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64
,
994 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
995 bt_ctf_stream_class_get_id(stream_class
));
996 stream_class
->frozen
= 1;
997 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
998 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
999 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
1001 if (stream_class
->clock
) {
1002 bt_ctf_clock_class_freeze(stream_class
->clock
->clock_class
);
1007 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
1008 struct metadata_context
*context
)
1013 BT_LOGD("Serializing stream class's metadata: "
1014 "stream-class-addr=%p, stream-class-name=\"%s\", "
1015 "stream-class-id=%" PRId64
", metadata-context-addr=%p",
1016 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1017 bt_ctf_stream_class_get_id(stream_class
), context
);
1018 g_string_assign(context
->field_name
, "");
1019 context
->current_indentation_level
= 1;
1020 if (!stream_class
->id_set
) {
1021 BT_LOGW_STR("Stream class's ID is not set.");
1026 g_string_append_printf(context
->string
,
1027 "stream {\n\tid = %" PRId64
";\n", stream_class
->id
);
1028 if (stream_class
->event_header_type
) {
1029 BT_LOGD_STR("Serializing stream class's event header field type's metadata.");
1030 g_string_append(context
->string
, "\tevent.header := ");
1031 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
1034 BT_LOGW("Cannot serialize stream class's event header field type's metadata: "
1038 g_string_append(context
->string
, ";");
1042 if (stream_class
->packet_context_type
) {
1043 BT_LOGD_STR("Serializing stream class's packet context field type's metadata.");
1044 g_string_append(context
->string
, "\n\n\tpacket.context := ");
1045 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
1048 BT_LOGW("Cannot serialize stream class's packet context field type's metadata: "
1052 g_string_append(context
->string
, ";");
1055 if (stream_class
->event_context_type
) {
1056 BT_LOGD_STR("Serializing stream class's event context field type's metadata.");
1057 g_string_append(context
->string
, "\n\n\tevent.context := ");
1058 ret
= bt_ctf_field_type_serialize(
1059 stream_class
->event_context_type
, context
);
1061 BT_LOGW("Cannot serialize stream class's event context field type's metadata: "
1065 g_string_append(context
->string
, ";");
1068 g_string_append(context
->string
, "\n};\n\n");
1070 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
1071 struct bt_ctf_event_class
*event_class
=
1072 stream_class
->event_classes
->pdata
[i
];
1074 ret
= bt_ctf_event_class_serialize(event_class
, context
);
1076 BT_LOGW("Cannot serialize event class's metadata: "
1077 "event-class-addr=%p, event-class-name=\"%s\", "
1078 "event-class-id=%" PRId64
,
1080 bt_ctf_event_class_get_name(event_class
),
1081 bt_ctf_event_class_get_id(event_class
));
1086 context
->current_indentation_level
= 0;
1091 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
1093 struct bt_ctf_stream_class
*stream_class
;
1095 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
1096 BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64
,
1097 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1098 bt_ctf_stream_class_get_id(stream_class
));
1099 bt_put(stream_class
->clock
);
1101 if (stream_class
->event_classes_ht
) {
1102 g_hash_table_destroy(stream_class
->event_classes_ht
);
1104 if (stream_class
->event_classes
) {
1105 BT_LOGD_STR("Destroying event classes.");
1106 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
1109 if (stream_class
->name
) {
1110 g_string_free(stream_class
->name
, TRUE
);
1113 BT_LOGD_STR("Putting event header field type.");
1114 bt_put(stream_class
->event_header_type
);
1115 BT_LOGD_STR("Putting packet context field type.");
1116 bt_put(stream_class
->packet_context_type
);
1117 BT_LOGD_STR("Putting event context field type.");
1118 bt_put(stream_class
->event_context_type
);
1119 g_free(stream_class
);
1123 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
1126 struct bt_ctf_field_type
*event_header_type
=
1127 bt_ctf_field_type_structure_create();
1128 struct bt_ctf_field_type
*_uint32_t
=
1129 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
1130 struct bt_ctf_field_type
*_uint64_t
=
1131 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
1133 if (!event_header_type
) {
1134 BT_LOGE_STR("Cannot create empty structure field type.");
1139 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
1142 BT_LOGE_STR("Cannot add `id` field to event header field type.");
1146 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
1147 _uint64_t
, "timestamp");
1149 BT_LOGE_STR("Cannot add `timestamp` field to event header field type.");
1153 BT_MOVE(stream_class
->event_header_type
, event_header_type
);
1156 bt_put(event_header_type
);
1165 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
1168 struct bt_ctf_field_type
*packet_context_type
=
1169 bt_ctf_field_type_structure_create();
1170 struct bt_ctf_field_type
*_uint64_t
=
1171 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
1172 struct bt_ctf_field_type
*ts_begin_end_uint64_t
;
1174 if (!packet_context_type
) {
1175 BT_LOGE_STR("Cannot create empty structure field type.");
1180 ts_begin_end_uint64_t
= bt_ctf_field_type_copy(_uint64_t
);
1181 if (!ts_begin_end_uint64_t
) {
1182 BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields.");
1188 * We create a stream packet context as proposed in the CTF
1191 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1192 ts_begin_end_uint64_t
, "timestamp_begin");
1194 BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type.");
1198 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1199 ts_begin_end_uint64_t
, "timestamp_end");
1201 BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type.");
1205 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1206 _uint64_t
, "content_size");
1208 BT_LOGE_STR("Cannot add `content_size` field to event header field type.");
1212 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1213 _uint64_t
, "packet_size");
1215 BT_LOGE_STR("Cannot add `packet_size` field to event header field type.");
1219 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1220 _uint64_t
, "events_discarded");
1222 BT_LOGE_STR("Cannot add `events_discarded` field to event header field type.");
1226 BT_MOVE(stream_class
->packet_context_type
, packet_context_type
);
1229 bt_put(packet_context_type
);
1234 bt_put(ts_begin_end_uint64_t
);
1239 int try_map_clock_class(struct bt_ctf_stream_class
*stream_class
,
1240 struct bt_ctf_field_type
*ft
)
1242 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1246 /* Field does not exist: not an error */
1250 assert(bt_ctf_field_type_is_integer(ft
));
1251 mapped_clock_class
=
1252 bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
1253 if (!mapped_clock_class
) {
1254 if (!stream_class
->clock
) {
1255 BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: "
1256 "stream-class-addr=%p, stream-class-name=\"%s\", "
1257 "stream-class-id=%" PRId64
", ft-addr=%p",
1258 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1259 bt_ctf_stream_class_get_id(stream_class
), ft
);
1264 ret
= bt_ctf_field_type_integer_set_mapped_clock_class_no_check(
1265 ft
, stream_class
->clock
->clock_class
);
1267 BT_LOGW("Cannot set field type's mapped clock class: "
1268 "stream-class-addr=%p, stream-class-name=\"%s\", "
1269 "stream-class-id=%" PRId64
", ft-addr=%p",
1270 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1271 bt_ctf_stream_class_get_id(stream_class
), ft
);
1275 BT_LOGV("Automatically mapped field type to stream class's clock class: "
1276 "stream-class-addr=%p, stream-class-name=\"%s\", "
1277 "stream-class-id=%" PRId64
", ft-addr=%p",
1278 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1279 bt_ctf_stream_class_get_id(stream_class
), ft
);
1283 bt_put(mapped_clock_class
);
1288 int bt_ctf_stream_class_map_clock_class(
1289 struct bt_ctf_stream_class
*stream_class
,
1290 struct bt_ctf_field_type
*packet_context_type
,
1291 struct bt_ctf_field_type
*event_header_type
)
1293 struct bt_ctf_field_type
*ft
= NULL
;
1296 assert(stream_class
);
1298 if (packet_context_type
) {
1299 ft
= bt_ctf_field_type_structure_get_field_type_by_name(
1300 packet_context_type
, "timestamp_begin");
1301 if (try_map_clock_class(stream_class
, ft
)) {
1302 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class.");
1308 ft
= bt_ctf_field_type_structure_get_field_type_by_name(
1309 packet_context_type
, "timestamp_end");
1310 if (try_map_clock_class(stream_class
, ft
)) {
1311 BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class.");
1319 if (event_header_type
) {
1320 ft
= bt_ctf_field_type_structure_get_field_type_by_name(
1321 event_header_type
, "timestamp");
1322 if (try_map_clock_class(stream_class
, ft
)) {
1323 BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class.");